dri-devel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Alistair Popple" <apopple@nvidia.com>
Cc: dri-devel@lists.freedesktop.org, intel-xe@lists.freedesktop.org,
	linux-rdma@vger.kernel.org, kvm@vger.kernel.org,
	media-ci@linuxtv.org
Subject: Re: [PATCH RFC 04/16] dma-shared-buf: Add dma_addr_list_to_sgt()
Date: Tue, 01 Dec 2026 07:19:35 +0000	[thread overview]
Message-ID: <sashiko-outbox-242755@kernel.org> (raw)
In-Reply-To: <20261201-dmabuf_shared_buf_v1_xe_rc5-v1-4-b672717c7fc9@nvidia.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] dma-shared-buf: integer overflow in dma_addr_list_to_sgt()
  nents calculation
--

commit d053fd21323b983e9a0997bdd63898a8c3072b3f
Author: Alistair Popple <apopple@nvidia.com>

dma-shared-buf: Add dma_addr_list_to_sgt()

This commit adds dma_addr_list_to_sgt() and dma_addr_list_free_sgt() to build
and free a DMA-only sg_table over a struct dma_addr_list.

> diff --git a/drivers/dma-buf/dma-shared-buf.c b/drivers/dma-buf/dma-shared-buf.c
> index d5d179b771f22..c346d4c05de8e 100644
> --- a/drivers/dma-buf/dma-shared-buf.c
> +++ b/drivers/dma-buf/dma-shared-buf.c

[ ... ]

> @@ -207,3 +209,87 @@ int dma_shared_buf_iommu_map(const struct dma_shared_buf *shared_buf,
>  		iommu_unmap(domain, start_iova, iova - start_iova);
>  	return ret;
>  }
> +
> +#define MAX_SG_ENT_SZ ALIGN_DOWN(UINT_MAX, PAGE_SIZE)
> +
> +static struct scatterlist *fill_sg_entry(struct scatterlist *sgl, size_t length,
> +					 dma_addr_t addr)
> +{
> +	size_t len;
> +
> +	while (length) {
> +		len = min(length, MAX_SG_ENT_SZ);
> +		length -= len;
> +		/*
> +		 * The scatterlist has no CPU list, only the DMA list. Always
> +		 * set the page related values to NULL to ensure nothing
> +		 * handed the table can use them. The phys_addr based DMA API
> +		 * does not require the CPU list for mapping or unmapping.
> +		 */
> +		sg_set_page(sgl, NULL, 0, 0);
> +		sg_dma_address(sgl) = addr;
> +		sg_dma_len(sgl) = len;
> +		addr += len;
> +		/* Unconditionally advance. On last segment, this becomes NULL */
> +		sgl = sg_next(sgl);
> +	}
> +
> +	return sgl;
> +}
> +
> +/**
> + * dma_addr_list_to_sgt - Build an sg_table over the DMA addresses of a mapped
> + *                        dma_shared_buf
> + * @sgt: The table to build
> + * @addrs: The DMA addresses dma_shared_buf_map() returned
> + *
> + * For a caller which has to present the mapping as an sg_table. The table
> + * carries the DMA side only. The CPU side is left empty so that nothing handed
> + * the table can reach a struct page. The table does not own the mapping:
> + * @addrs must stay mapped while the table is in use and is still released with
> + * dma_shared_buf_unmap().
> + *
> + * Returns: 0 on success, in which case the table is freed with
> + * dma_addr_list_free_sgt(), or a negative error value.
> + */
> +int dma_addr_list_to_sgt(struct sg_table *sgt,
> +			 const struct dma_addr_list *addrs)
> +{
> +	struct scatterlist *sgl;
> +	unsigned int nents = 0;
> +	unsigned int i;
> +	int ret;
> +
> +	for (i = 0; i < addrs->nr_ranges; i++) {
> +		unsigned int added = DIV_ROUND_UP(addrs->ranges[i].len,
> +						  MAX_SG_ENT_SZ);
> +
> +		if (check_add_overflow(nents, added, &nents))
> +			return -E2BIG;
> +	}

[Severity: High]
Can DIV_ROUND_UP(addrs->ranges[i].len, MAX_SG_ENT_SZ) overflow when computing
added?

MAX_SG_ENT_SZ is UINT_MAX - PAGE_SIZE + 1.  On 32-bit builds, both size_t and
unsigned long are 32-bit, so the numerator addrs->ranges[i].len +
MAX_SG_ENT_SZ - 1 wraps modulo 2^32 for any range with len > PAGE_SIZE and
evaluates to added == 0.  If addrs mixes a single-page range (added == 1) and
a multi-page range (added == 0), sg_alloc_table() allocates too few entries
and fill_sg_entry() advances sgl to NULL via sg_next(sgl), causing a NULL
pointer dereference in sg_set_page().

On 64-bit builds, assigning the quotient to unsigned int added also truncates
the upper 32 bits before check_add_overflow().  Using DIV_ROUND_UP_ULL() into
a u64 before check_add_overflow(nents, added, &nents) avoids both issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261201-dmabuf_shared_buf_v1_xe_rc5-v1-0-b672717c7fc9@nvidia.com?part=4

  reply	other threads:[~2026-12-01  7:19 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01  7:06 [PATCH RFC 00/16] dma-buf: Introduce dma_shared_buf and dma_addr_list to replace sg_table Alistair Popple
2026-12-01  7:06 ` [PATCH RFC 01/16] treewide: Rename dma_buf_(un)map_attachment() to dma_buf_(un)map_sgt() Alistair Popple
2026-12-01  7:06 ` [PATCH RFC 02/16] dma-shared-buf: Add struct dma_shared_buf Alistair Popple
2026-12-01  7:06 ` [PATCH RFC 03/16] dma-shared-buf: Add dma_shared_buf_iommu_map() Alistair Popple
2026-12-01  7:06 ` [PATCH RFC 04/16] dma-shared-buf: Add dma_addr_list_to_sgt() Alistair Popple
2026-12-01  7:19   ` sashiko-bot [this message]
2026-12-01  7:06 ` [PATCH RFC 05/16] dma-shared-buf: Add dma_shared_buf_from_pages() Alistair Popple
2026-12-01  7:06 ` [PATCH RFC 06/16] dma-buf: Allow exporters to hand out a dma_shared_buf Alistair Popple
2026-12-01  7:06 ` [PATCH RFC 07/16] dma-buf: Add dma_buf_iommu_map() for IOMMU domain importers Alistair Popple
2026-12-01  7:06 ` [PATCH RFC 08/16] dma-buf: Factor out the pin and fence wait around the exporter's map Alistair Popple
2026-12-01  7:06 ` [PATCH RFC 09/16] dma-buf: Add dma_buf_map_dma_list() for DMA address only importers Alistair Popple
2026-12-01  7:06 ` [PATCH RFC 10/16] vfio/pci: Export a dma_shared_buf instead of mapping the buffer Alistair Popple
2026-12-01  7:06 ` [PATCH RFC 11/16] RDMA/uverbs: " Alistair Popple
2026-12-01  7:06 ` [PATCH RFC 12/16] dma-buf: Remove dma_buf_phys_vec_to_sgt() and dma_buf_free_sgt() Alistair Popple
2026-12-01  7:06 ` [PATCH RFC 13/16] iommufd: Map DMA-bufs through dma_buf_iommu_map() Alistair Popple
2026-12-01  7:28   ` sashiko-bot
2026-12-01  7:06 ` [PATCH RFC 14/16] vfio/pci: Remove vfio_pci_dma_buf_iommufd_map() Alistair Popple
2026-12-01  7:06 ` [PATCH RFC 15/16] drm/xe/dma-buf: Export a dma_shared_buf instead of mapping the buffer Alistair Popple
2026-12-01  7:26   ` sashiko-bot
2026-12-01  7:06 ` [PATCH RFC 16/16] drm/xe: Import DMA-bufs as a DMA address list Alistair Popple

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-242755@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=apopple@nvidia.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=kvm@vger.kernel.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox