[PATCH v3 13/24] udmabuf: Convert to the SGT mapping type
sashiko-bot at kernel.org
sashiko-bot at kernel.org
Fri Nov 20 21:58:47 PST 2026
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] udmabuf: premature publication of ubuf->sg in
begin_cpu_udmabuf()
--
commit f138ffdf3b226fef46f9eda6001d750e3e320cdd
Author: Alistair Popple <apopple at nvidia.com>
udmabuf: Convert to the SGT mapping type
This commit converts udmabuf to the SGT mapping type by having
get_sg_table() and put_sg_table() operate on a caller-allocated struct
sg_table and embedding the DMA-buf mapping table in struct
dma_buf_sgt_mapping.
> diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c
> index df6dd00462423..ef77ae063ae4c 100644
> --- a/drivers/dma-buf/udmabuf.c
> +++ b/drivers/dma-buf/udmabuf.c
[ ... ]
> @@ -226,16 +233,19 @@ static int begin_cpu_udmabuf(struct dma_buf *buf,
> struct device *dev = ubuf->device->this_device;
>
> if (!ubuf->sg) {
> - ubuf->sg = get_sg_table(dev, buf, direction);
> - if (IS_ERR(ubuf->sg)) {
> - int ret;
> + int ret;
> +
> + ubuf->sg = kzalloc_obj(*ubuf->sg);
> + if (!ubuf->sg)
> + return -ENOMEM;
>
> - ret = PTR_ERR(ubuf->sg);
> + ret = get_sg_table(dev, buf, ubuf->sg, direction);
> + if (ret) {
> + kfree(ubuf->sg);
> ubuf->sg = NULL;
> return ret;
> - } else {
> - ubuf->sg_dir = direction;
> }
> + ubuf->sg_dir = direction;
> }
>
> dma_sync_sgtable_for_cpu(dev, ubuf->sg, direction);
[Severity: High]
Can assigning ubuf->sg before get_sg_table() finishes expose an
uninitialized, unmapped, or freed sg_table to concurrent
DMA_BUF_IOCTL_SYNC callers?
dma_buf_ioctl() invokes begin_cpu_udmabuf() and end_cpu_udmabuf() without
holding a lock. Previously, get_sg_table() allocated, populated, and
DMA-mapped a local struct sg_table before returning it to be stored in
ubuf->sg. With ubuf->sg assigned immediately after kzalloc_obj(), a
second thread can observe a non-NULL ubuf->sg while the first thread is
still inside get_sg_table() (or after kfree(ubuf->sg) on the error path):
CPU0 CPU1
begin_cpu_udmabuf()
ubuf->sg = kzalloc_obj(...)
get_sg_table(..., ubuf->sg, ...)
sg_alloc_table_from_pages()
begin_cpu_udmabuf()
if (!ubuf->sg) /* false */
dma_sync_sgtable_for_cpu(...,
ubuf->sg,
...)
/* uninitialized or freed sgt */
If two threads both enter the !ubuf->sg block, the second kzalloc_obj()
also overwrites ubuf->sg while the first thread passes ubuf->sg to
get_sg_table(), leaking the first allocation and initializing the same
struct sg_table concurrently.
Allocating into a local struct sg_table *sg and assigning ubuf->sg only
after get_sg_table() succeeds avoids publishing the table prematurely.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261120-dma_mapping_type_v4-v3-0-c0183a409bdf@nvidia.com?part=13
More information about the Linux-nvme
mailing list