All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Alistair Popple" <apopple@nvidia.com>,
	dri-devel@lists.freedesktop.org, intel-xe@lists.freedesktop.org,
	amd-gfx@lists.freedesktop.org, linux-rdma@vger.kernel.org,
	linux-nvme@lists.infradead.org
Cc: media-ci@linuxtv.org
Subject: Re: [PATCH v3 13/24] udmabuf: Convert to the SGT mapping type
Date: Sat, 21 Nov 2026 05:58:47 +0000	[thread overview]
Message-ID: <sashiko-outbox-226681@kernel.org> (raw)
In-Reply-To: <20261120-dma_mapping_type_v4-v3-13-c0183a409bdf@nvidia.com>

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@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

  reply	other threads:[~2026-11-21  5:59 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20  5:55 [PATCH v3 00/24] DMA-buf mapping types Alistair Popple
2026-11-20  5:55 ` [PATCH v3 01/24] treewide: Rename dma_buf_(un)map_attachment() to dma_buf_(un)map_sgt() Alistair Popple
2026-11-20  5:55 ` [PATCH v3 02/24] dma-buf: Introduce DMA-buf mapping types Alistair Popple
2026-11-20  5:55 ` [PATCH v3 03/24] dma-buf: Add the SGT DMA mapping type Alistair Popple
2026-11-20  5:55 ` [PATCH v3 04/24] dma-buf: Negotiate the mapping type in dma_buf_dynamic_attach() Alistair Popple
2026-11-20  5:55 ` [PATCH v3 05/24] dma-buf: Route SGT related actions through the attachment's mapping type Alistair Popple
2026-11-20  5:55 ` [PATCH v3 06/24] dma-buf: Allow single exporter drivers to avoid the match_mapping function Alistair Popple
2026-11-20  5:55 ` [PATCH v3 07/24] dma-buf: Convert exporters allocating a table per map to the SGT mapping type Alistair Popple
2026-11-20  9:02   ` Karol Wachowski
2026-11-20  5:55 ` [PATCH v3 08/24] iommufd/selftest: Convert the test exporter " Alistair Popple
2026-11-20  5:55 ` [PATCH v3 09/24] dma-buf: Convert exporters keeping a table per attachment " Alistair Popple
2026-11-20  5:55 ` [PATCH v3 10/24] drm: Convert drm_gem_map_dma_buf() and its users " Alistair Popple
2026-11-20  5:55 ` [PATCH v3 11/24] drm/xe/dma-buf: Use " Alistair Popple
2026-11-20  5:55 ` [PATCH v3 12/24] drm/amdgpu: " Alistair Popple
2026-11-20  5:55 ` [PATCH v3 13/24] udmabuf: Convert to " Alistair Popple
2026-11-21  5:58   ` sashiko-bot [this message]
2026-11-20  5:55 ` [PATCH v3 14/24] drm/omapdrm: " Alistair Popple
2026-11-20  5:55 ` [PATCH v3 15/24] accel/ivpu: Convert the userptr exporter " Alistair Popple
2026-11-20  5:55 ` [PATCH v3 16/24] drm/vmwgfx: Use match_mapping instead of dummy calls Alistair Popple
2026-11-20  5:55 ` [PATCH v3 17/24] dma-buf: Convert dma_buf_phys_vec_to_sgt() and its users to the SGT mapping type Alistair Popple
2026-11-20  5:55 ` [PATCH v3 18/24] iio: buffer: convert to use " Alistair Popple
2026-11-20  5:55 ` [PATCH v3 19/24] functionfs: " Alistair Popple
2026-11-20  5:55 ` [PATCH v3 20/24] dma-buf: Remove unused SGT stuff from the common structures Alistair Popple
2026-11-20  5:55 ` [PATCH v3 21/24] dma-buf: Add kunit tests for mapping type Alistair Popple
2026-11-20  5:56 ` [PATCH v3 22/24] dma-buf: Let the exporter select mapping type at pin/map Alistair Popple
2026-11-20  5:56 ` [PATCH v3 23/24] dma-buf: Add dma_buf_mapping_map() returning a type-tagged mapping and implement it for sgt Alistair Popple
2026-11-21  5:58   ` sashiko-bot
2026-11-20  5:56 ` [PATCH v3 24/24] dma-buf: Add kunit tests for mapping type selection Alistair Popple
2026-11-21  5:58   ` sashiko-bot
2026-11-21  5:58 ` [PATCH v3 00/24] DMA-buf mapping types sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-226681@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=amd-gfx@lists.freedesktop.org \
    --cc=apopple@nvidia.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=linux-nvme@lists.infradead.org \
    --cc=linux-rdma@vger.kernel.org \
    --cc=media-ci@linuxtv.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.