Re: [PATCH RFC 02/16] dma-shared-buf: Add struct dma_shared_buf
From: Leon Romanovsky
Date: Wed Dec 02 2026 - 07:30:40 EST
On Tue, Dec 01, 2026 at 06:06:18PM +1100, Alistair Popple wrote:
> Currently the sg_table is used by dma-buf exporters to describe the
> range of physical addresses to map to the DMA API. This is then passed
> directly back to importers.
>
> This is problematic for a number of reasons. It exposes exporter side
> information to importers and doesn't have the capability to describe
> memory that isn't backed by struct page which is what MMIO such as PCI
> BAR needs.
>
> Add the dma-shared-buf as a library to handle this. A new type to
> replace the exporter's sg_table, struct dma_shared_buf, is added
> to describe a range of physical regions for mapping by the library.
> dma_shared_buf_map() can then be used to map this for a particular
> device.
>
> The mapping function handles either direct P2P mappings using the PCI
> peer-to-peer core if a p2pdma provider is listed, or system memory
> otherwise. This allows dma-buf exporters to use common logic to
> create mappings instead of open-coding it which is hard to get right,
> especially for P2P cases with complex routing and IOMMU requirements.
>
> This is kept in a file of its own as none of it depends on a DMA-buf and
> it could be a library elsewhere.
>
> Signed-off-by: Alistair Popple <apopple@xxxxxxxxxx>
>
> ---
>
> This is supposed to be used as a library for dma-buf exporters/importers
> so could be moved. I don't love the name so happy for suggestions or
> alternatives.
> ---
> MAINTAINERS | 1 +
> drivers/dma-buf/Makefile | 3 +-
> drivers/dma-buf/dma-shared-buf.c | 138 +++++++++++++++++++++++++++++++++++++++
> include/linux/dma-shared-buf.h | 77 ++++++++++++++++++++++
> 4 files changed, 218 insertions(+), 1 deletion(-)
<...>
> + if (shared_buf->provider) {
> + addrs->attrs |= DMA_ATTR_MMIO;
The patch looks good to me. I have just one nit:
Please move this assignment under PCI_P2PDMA_MAP_THRU_HOST_BRIDGE, since it
does not apply to the PCI_P2PDMA_MAP_BUS_ADDR case.
Thanks
> + switch (pci_p2pdma_map_type(shared_buf->provider, dev)) {
> + case PCI_P2PDMA_MAP_BUS_ADDR:
> + for (i = 0; i < shared_buf->nr_ranges; i++) {
> + const struct phys_vec *vec =
> + &shared_buf->ranges[i];
> +
> + addrs->ranges[i].addr =
> + pci_p2pdma_bus_addr_map(shared_buf->provider,
> + vec->paddr);
> + addrs->ranges[i].len = vec->len;
> + }
> + addrs->bus_addr = true;
> + return addrs;
> + case PCI_P2PDMA_MAP_THRU_HOST_BRIDGE:
> + break;
> + default:
> + ret = -EINVAL;
> + goto err_free;
> + }
> + }