This series tightens the alignment requirements for buffers that are shared
between confidential-computing guests and the host, and adds a common
allocator for host-shared memory.
When a guest runs with private memory, buffers shared with the hypervisor
are not only accessed by the guest. They are also accessed by the host
kernel, and the host may manage the corresponding shared/private state at a
granularity larger than the guest page size.
Architectures such as Arm can detect incorrect accesses to Realm physical
address space PFNs through GPC faults. However, relying on that as the only
line of defence is fragile and can still lead to kernel crashes. The risk
is especially visible for shared buffers that are later mmapped into
userspace, such as guest_memfd or dma-buf backed allocations. Once
userspace can access the mapping, the kernel cannot guarantee that
applications will only touch the intended 4K region rather than the whole
host page mapped into their address space. Those userspace addresses may
also be passed back into the kernel and accessed through the linear map,
resulting in a GPC fault.
To avoid this, host-shared buffers must satisfy two constraints:
- the address must be aligned to the CoCo shared-granule size
- the size must be a multiple of that granule size
The series adds a common CoCo shared-memory layer for enforcing these
constraints. It provides shared-granule geometry and range-validation
helpers, byte-oriented private/shared transition helpers, and
alloc_cc_shared() and its node-aware variant. The allocator accepts a byte
count, rounds internally to the sharing granule and a power-of-two number
of pages, and returns a linear-map address. The entire allocation is shared
and zeroed, with its physical base aligned to the allocation size.
cc_shared_alloc_size() reports the actual size from compound-page metadata.
free_cc_shared() restores the complete allocation to private
state before returning it to the buddy allocator. If private state cannot
be restored safely, the allocation is deliberately leaked rather than
returning potentially shared memory for unrelated use.
The series also makes zeroing part of private-to-shared transitions, with
each architecture clearing memory at the appropriate point. Callers must
therefore initialize shared buffers after the transition; existing contents
are not preserved.
The shared-granule size defaults to PAGE_SIZE and can be overridden by an
architecture. The arm64 CCA changes to discover the host's IPA state change
alignment are not included in this series.
GIC ITS command queues and tables and the dma-buf system_cc_shared heap
use the common allocator. DMA direct, atomic DMA pools and SWIOTLB retain
their allocation paths and use checked byte rounding or transition helpers.
Restricted DMA pools are checked for shared-granule alignment.
Hyper-V users of set_memory_encrypted() and set_memory_decrypted() are not
changed to use cc_make_shared() by this series. Those paths are not
currently used by the arm64 CCA code path, and therefore are not part of
the arm64 CCA IPA state change alignment problem addressed here.
Notes:
* Codex assisted with implementation, review and series organization.
Changes from v10:
https://patch.msgid.link/20261116091137.94252-1-aneesh.kumar@kernel.org
* Replace page/size descriptors with byte-based alloc_cc_shared_node() and
alloc_cc_shared(), returning a linear-map address. Recover the full size
from compound metadata in cc_shared_alloc_size() and free_cc_shared().
* Share and zero the entire allocation. Document physical alignment and
low-memory/nonmovable requirements, including without memory encryption.
* Replace layout structures with cc_shared_round_size() for callers that
manage their own backing memory.
* Require a sleepable allocation context regardless of memory encryption.
* Split architecture zeroing changes and caller cleanups into separate
patches. Separate SWIOTLB alignment from transition-helper conversion.
* Add a dedicated MAINTAINERS entry for CoCo memory sharing and
encryption, including the common interfaces and allocator.
Changes from v9:
https://lore.kernel.org/all/20261104164555.3555725-1-aneesh.kumar@kernel.org
* Leak allocations after failed sharing in the common allocator and SWIOTLB.
* Move x86 zeroing from the AMD, TDX and Hyper-V vTOM callbacks to
set_memory_decrypted(), after a successful transition
* Add set_memory_decrypted_nozero() for VGA mappings, which must change
memory attributes without clearing device memory.
* Zero memory even when encryption is inactive in the generic, arm64 and
pSeries implementations of set_memory_decrypted().
* Move pSeries zeroing after sharing. Document why pKVM and s390 must clear
private contents before sharing
* Drop the GIC ITS VPE L1 preallocation patch from this series.
This issue predates the series and will be addressed separately.
* Add mm/cc_shared.c and include/linux/cc_shared.h to the TSM MAINTAINERS
entry.
Changes from v8:
https://lore.kernel.org/all/20260924100529.1398790-1-aneesh.kumar@kernel.org
* Rebase onto the 7.4-rc1
* Move TDX and AMD (including SEV-SNP) zeroing to their finish callbacks
so private-to-shared transitions clear the final shared mapping.
* Clear __GFP_MOVABLE for shared allocations, which cannot be migrated.
* Resolve NUMA_NO_NODE in the common ITS allocation helper to preserve
placement for all ITS callers.
* Stop SWIOTLB allocation retries when the reduced pool would fall below
the shared granule size, avoiding an allocation retry loop.
Changes from v7:
https://lore.kernel.org/all/20260921144847.501151-1-aneesh.kumar@kernel.org
* Add the following new patches:
* "irqchip/gic-v3-its: Preallocate VPE L1 tables"
* "mm: Assert CoCo shared allocations may sleep"
* "mm: Zero memory during shared memory transitions"
* Drop the arm64 RHI and shared granule size patches so that the series can
be rebased on top of upstream to enable Shashiko review.
Changes from v6:
https://lore.kernel.org/all/20260904103452.1197239-1-aneesh.kumar@kernel.org
* Add a common allocator and geometry/transition helpers for CoCo host-shared
memory.
* Convert GIC ITS, dma-direct, atomic DMA pools, and the dma-buf
system_cc_shared heap to the common allocator.
* Limit dma-buf scatterlist entries to the requested buffer size so rounded
backing is not exposed to importers.
Changes from v5:
https://lore.kernel.org/all/20260706060432.1375570-1-aneesh.kumar@kernel.org
* Rebased to latest kernel
* Drop patch arm64: realm: Move Realm memory encryption ops to RSI code
Changes from v4:
https://lore.kernel.org/all/20260427063108.909019-1-aneesh.kumar@kernel.org
* Rename the helpers to use CoCo terminology
(mem_cc_shared_granule_size() / mem_cc_align_to_shared_granule() instead of
mem_decrypt_granule_size() / mem_decrypt_align()).
* Use __DMA_ATTR_ALLOC_CC_SHARED to pass CoCo shared allocation requirements
down to CMA-based allocation helpers.
* Add validation for restricted DMA pools to reject pools that are not aligned
to the shared granule size.
* Add dma-buf system heap handling for cc-shared buffers.
* Split the previous combined DMA/SWIOTLB/ITS change into smaller subsystem
patches covering ITS, DMA direct, SWIOTLB, restricted DMA pools, dma-buf
system heap, and arm64 Realm support.
* Rework arm64 Realm support by moving Realm memory encryption ops into RSI
code and exposing the CCA shared granule size through arm64_mem_crypt_ops.
Changes from v3:
https://lore.kernel.org/all/20260309102625.2315725-1-aneesh.kumar@kernel.org
* Fix build error reported by kernel test robot <lkp(a)intel.com>
Changes from v2:
https://lore.kernel.org/all/20251221160920.297689-1-aneesh.kumar@kernel.org
* Rebase to latest kernel
* Consider swiotlb always decrypted and don't align when allocating from swiotlb.
Changes from v1:
* Rename the helper to mem_encrypt_align
* Improve the commit message
* Handle DMA allocations from contiguous memory
* Handle DMA allocations from the pool
* swiotlb is still considered unencrypted. Support for an encrypted swiotlb pool
is left as TODO and is independent of this series.
Cc: Andrew Morton <akpm(a)linux-foundation.org>
Cc: Catalin Marinas <catalin.marinas(a)arm.com>
Cc: "Christian König" <christian.koenig(a)amd.com>
Cc: Jason Gunthorpe <jgg(a)ziepe.ca>
Cc: Joerg Roedel (AMD) <joro(a)8bytes.org>
Cc: Marc Zyngier <maz(a)kernel.org>
Cc: Marek Szyprowski <m.szyprowski(a)samsung.com>
Cc: Robin Murphy <robin.murphy(a)arm.com>
Cc: Steven Price <steven.price(a)arm.com>
Cc: Sumit Semwal <sumit.semwal(a)linaro.org>
Cc: Suzuki K Poulose <suzuki.poulose(a)arm.com>
Cc: Will Deacon <will(a)kernel.org>
Cc: "T.J. Mercier" <tjmercier(a)google.com>
Cc: Madhavan Srinivasan <maddy(a)linux.ibm.com>
Cc: Heiko Carstens <hca(a)linux.ibm.com>
Cc: Christian Borntraeger <borntraeger(a)linux.ibm.com>
Cc: Sven Schnelle <svens(a)linux.ibm.com>
Cc: Dave Hansen <dave.hansen(a)linux.intel.com>
Cc: Kiryl Shutsemau <kas(a)kernel.org>
Cc: Rick Edgecombe <rick.p.edgecombe(a)intel.com>
Cc: Paolo Bonzini <pbonzini(a)redhat.com>
Cc: Thomas Gleixner <tglx(a)kernel.org>
Cc: Ingo Molnar <mingo(a)redhat.com>
Cc: Borislav Petkov <bp(a)alien8.de>
Cc: Haiyang Zhang <haiyangz(a)microsoft.com>
Cc: Wei Liu <wei.liu(a)kernel.org>
Cc: Dexuan Cui <decui(a)microsoft.com>
Cc: Andy Lutomirski <luto(a)kernel.org>
Cc: Peter Zijlstra <peterz(a)infradead.org>
Cc: Alexander Gordeev <agordeev(a)linux.ibm.com>
Cc: Gerald Schaefer <gerald.schaefer(a)linux.ibm.com>
Cc: Vasily Gorbik <gor(a)linux.ibm.com>
Cc: x86(a)kernel.org
Cc: dri-devel(a)lists.freedesktop.org
Cc: iommu(a)lists.linux.dev
Cc: linaro-mm-sig(a)lists.linaro.org
Cc: linux-arm-kernel(a)lists.infradead.org
Cc: linux-media(a)vger.kernel.org
Cc: linux-mm(a)kvack.org
Cc: kvmarm(a)lists.linux.dev
Cc: patches(a)lists.linux.dev
Aneesh Kumar K.V (Arm) (23):
MAINTAINERS: Add CoCo memory sharing and encryption entry
mm: Add an allocator for CoCo shared memory
x86/mm: Use the non-zeroing decryption helper for VGA mappings
s390/mm: Clear memory before making it shared
powerpc/pseries: Clear memory after making it shared
arm64: Zero memory during private-to-shared transitions
x86/mm: Clear memory after successful decryption transitions
mm: Document zeroing by set_memory_decrypted() and update the fallback
drivers: hv: Remove redundant clearing after decryption
x86/kvmclock: Avoid clearing decrypted clock memory twice
dma-direct: Avoid redundant clearing of shared allocations
mm: Avoid zeroing CoCo shared allocations twice
irqchip/gic-v3-its: Resolve the default NUMA node explicitly
irqchip/gic-v3-its: Allocate shared tables using CoCo shared memory
allocator
dma-contiguous: Derive shared alignment from DMA attributes
dma-pool: Align shared atomic pools to the CoCo sharing granule
dma-direct: Align CoCo shared DMA allocations to the shared granule
size
swiotlb: Align shared IO TLB pools to the shared granule size
swiotlb: Use the common CoCo memory transition helpers
swiotlb: Reject misaligned restricted DMA pools for CoCo guests
dma-buf: system_heap: Limit scatterlist entries to the buffer size
dma-buf: system_heap: Allocate shared buffers using CoCo shared memory
allocator
swiotlb: Make rounded shared pool capacity allocatable
MAINTAINERS | 13 +
arch/arm/mm/dma-mapping.c | 5 +-
arch/arm64/mm/mem_encrypt.c | 8 +-
arch/powerpc/platforms/pseries/svm.c | 13 +-
arch/s390/mm/init.c | 7 +
arch/x86/hyperv/hv_init.c | 1 -
arch/x86/include/asm/set_memory.h | 1 +
arch/x86/include/asm/vga.h | 4 +-
arch/x86/kernel/kvmclock.c | 18 +-
arch/x86/mm/pat/set_memory.c | 19 +-
drivers/dma-buf/heaps/system_heap.c | 125 ++++-----
drivers/firmware/arm_rmm/rsi.c | 9 +
drivers/hv/connection.c | 7 -
drivers/hv/hv.c | 10 +-
drivers/hv/hv_common.c | 2 -
drivers/iommu/dma-iommu.c | 2 +-
drivers/irqchip/irq-gic-v3-its.c | 144 +++++-----
drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c | 11 +
include/linux/cc_shared.h | 25 ++
include/linux/dma-map-ops.h | 9 +-
include/linux/set_memory.h | 8 +-
kernel/dma/contiguous.c | 47 +++-
kernel/dma/direct.c | 78 ++++--
kernel/dma/ops_helpers.c | 2 +-
kernel/dma/pool.c | 29 ++-
kernel/dma/swiotlb.c | 98 ++++---
kernel/kexec_file.c | 3 +-
mm/Makefile | 1 +
mm/cc_shared.c | 245 ++++++++++++++++++
29 files changed, 680 insertions(+), 264 deletions(-)
create mode 100644 include/linux/cc_shared.h
create mode 100644 mm/cc_shared.c
base-commit: 65e4fd942f72a9dba0e4e257290c4fa1aa756854
--
2.43.0
On Wed, Dec 02, 2026 at 02:20:20PM +0000, Lorenzo Stoakes (ARM) wrote:
> Nacked-by: Lorenzo Stoakes <ljs(a)kernel.org>
I didn't expect anything else after that "<... 12 MiB of text ...>" line.
Thanks
>
> --
> Cheers, Lorenzo
>
Hi Dave,
Dave Hansen <dave.hansen(a)intel.com> writes:
> On 12/2/26 01:54, Aneesh Kumar K.V (Arm) wrote:
>> This series tightens the alignment requirements for buffers that are shared
>> between confidential-computing guests and the host, and adds a common
>> allocator for host-shared memory.
>
> Thanks for splitting things out. So, serious question here: Have you,
> Aneesh, the human, gone and looked through this series?
>
> Because, I suspect that you want me, Dave, the human to go through
> these.
>
> There's already quite an asymmetry between submitter bandwidth and
> reviewer bandwidth. If you're not looking at these, in detail, before
> they go out the door but you expect me to do so, I'm not sure wor that
> could possible work.
Yes, I wrote this series. I used Codex to improve the grammar in commit
messages and code comments, and as an additional check when reviewing
changes I made.
This is v11 of the series and incorporates feedback from earlier
reviews. It has evolved through those discussions and is not an entirely
new series.
Was there a particular change, comment, or commit message that prompted
your concern?
-aneesh
On Wed, 2 Dec 2026 15:02:46 +0530
<mhonap(a)nvidia.com> wrote:
> From: Manish Honap <mhonap(a)nvidia.com>
>
> vfio_pci_core_do_io_rw() fills reads with 0xff and drops writes in one
> excluded window given by its caller. vfio_pci_bar_rw() sets it to the
> MSI-X table or to the part of a ROM BAR past the ROM image.
> vfio_pci_vga_rw() and nvgrace-gpu pass an empty window.
>
> The next patch for vfio-cxl series puts the MSI-X table on
> a per-BAR list of excluded ranges, and a BAR can then have more than one
> such range (CXL component register region range)
The "next patch" reference should be avoided in commit logs, series are
an ephemeral thing that exist on the mailing list, and especially here,
it's not even referring to the next patch in this series.
> Add a helper in vfio_pci_bar_rw() that sends the bytes before a range to
> the device in a call of their own, so no device access runs into the
> range, and fills or drops the bytes of the range. Use it for the MSI-X
> table and for the ROM BAR past the ROM image. Remove the window
> arguments from vfio_pci_core_do_io_rw() and its callers.
>
> No functional change.
>
> Signed-off-by: Manish Honap <mhonap(a)nvidia.com>
> ---
> drivers/vfio/pci/nvgrace-gpu/main.c | 4 +-
> drivers/vfio/pci/vfio_pci_rdwr.c | 115 ++++++++++++++++++----------
> include/linux/vfio_pci_core.h | 3 +-
> 3 files changed, 76 insertions(+), 46 deletions(-)
>
> diff --git a/drivers/vfio/pci/nvgrace-gpu/main.c b/drivers/vfio/pci/nvgrace-gpu/main.c
> index 5ddec340b73b..432f6b9c2842 100644
> --- a/drivers/vfio/pci/nvgrace-gpu/main.c
> +++ b/drivers/vfio/pci/nvgrace-gpu/main.c
> @@ -787,7 +787,7 @@ nvgrace_gpu_map_and_read(struct nvgrace_gpu_pci_core_device *nvdev,
> ret = vfio_pci_core_do_io_rw(&nvdev->core_device, false,
> nvdev->resmem.ioaddr,
> buf, offset, mem_count,
> - 0, 0, false, VFIO_PCI_IO_WIDTH_8);
> + false, VFIO_PCI_IO_WIDTH_8);
> }
>
> return ret;
> @@ -925,7 +925,7 @@ nvgrace_gpu_map_and_write(struct nvgrace_gpu_pci_core_device *nvdev,
> ret = vfio_pci_core_do_io_rw(&nvdev->core_device, false,
> nvdev->resmem.ioaddr,
> (char __user *)buf, pos, mem_count,
> - 0, 0, true, VFIO_PCI_IO_WIDTH_8);
> + true, VFIO_PCI_IO_WIDTH_8);
> }
>
> return ret;
> diff --git a/drivers/vfio/pci/vfio_pci_rdwr.c b/drivers/vfio/pci/vfio_pci_rdwr.c
> index df8848087681..68008f28bcbe 100644
> --- a/drivers/vfio/pci/vfio_pci_rdwr.c
> +++ b/drivers/vfio/pci/vfio_pci_rdwr.c
> @@ -12,6 +12,7 @@
>
> #include <linux/fs.h>
> #include <linux/pci.h>
> +#include <linux/range.h>
> #include <linux/uaccess.h>
> #include <linux/io.h>
> #include <linux/vfio.h>
> @@ -126,66 +127,41 @@ VFIO_IORDWR(16)
> VFIO_IORDWR(32)
> VFIO_IORDWR(64)
>
> -/*
> - * Read or write from an __iomem region (MMIO or I/O port) with an excluded
> - * range which is inaccessible. The excluded range drops writes and fills
> - * reads with -1. This is intended for handling MSI-X vector tables and
> - * leftover space for ROM BARs.
> - */
> +/* Read or write from an __iomem region (MMIO or I/O port) */
> ssize_t vfio_pci_core_do_io_rw(struct vfio_pci_core_device *vdev, bool test_mem,
> void __iomem *io, char __user *buf,
> - loff_t off, size_t count, size_t x_start,
> - size_t x_end, bool iswrite,
> + loff_t off, size_t count, bool iswrite,
> enum vfio_pci_io_width max_width)
I think there's a better shape to this series, and I think we should
keep vfio_pci_core_do_io_rw() as the variant driver facing API. I'd
propose the following sequence replacing 1 and 3, interleave the virtio
patch as necessary:
1. Simple conversion from x_start, x_end to a range. Pass NULL for an
empty range rather than {0, 0}.
2. Split static __vfio_pci_do_io_rw() from vfio_pci_core_do_io_rw() as
the helper that reads contiguous chunks, called by the original
function that retains the range information.
3. Convert from a range to a list_head. Callers allocate the list on
the stack and vfio_pci_core_do_io_rw() learns to iterate a list of
excluded ranges.
4. Introduce the per BAR excluded range list, populating MSI-X. ROM
remains a stack allocated list based on pci_map_rom(). Common
vfio_pci_core_do_io_rw() call sites now use the list_head on the
BAR, while ROM, VGA, and variant driver decide whether to use a
list_head or NULL. For example the nvgrace use case could pass the
list_head from the BAR index or NULL.
Thanks,
Alex
On Wed, 2 Dec 2026 15:02:47 +0530
<mhonap(a)nvidia.com> wrote:
> From: Manish Honap <mhonap(a)nvidia.com>
>
> virtiovf_pci_init_device() returns the error of virtiovf_init_legacy_io()
> without undoing vfio_pci_core_init_dev(). vfio_init_device() does not
> call .release when .init fails, so the state vfio_pci_core_init_dev()
> set up is not released.
>
> Today that state needs no freeing. vfio_pci_core_init_dev() is about to
> allocate an excluded-range entry for the MSI-X table, which would leak on
> this path. Call vfio_pci_core_release_dev() when
> virtiovf_init_legacy_io() fails.
>
> Signed-off-by: Manish Honap <mhonap(a)nvidia.com>
> ---
> drivers/vfio/pci/virtio/main.c | 5 ++++-
> 1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/vfio/pci/virtio/main.c b/drivers/vfio/pci/virtio/main.c
> index 123ff5193812..f1ca774889f8 100644
> --- a/drivers/vfio/pci/virtio/main.c
> +++ b/drivers/vfio/pci/virtio/main.c
> @@ -69,7 +69,10 @@ static int virtiovf_pci_init_device(struct vfio_device *core_vdev)
> * The vfio_device_ops.init() callback is set to virtiovf_pci_init_device()
> * only when legacy I/O is supported. Now, let's initialize it.
> */
> - return virtiovf_init_legacy_io(virtvdev);
> + ret = virtiovf_init_legacy_io(virtvdev);
> + if (ret)
> + vfio_pci_core_release_dev(core_vdev);
> + return ret;
> }
> #endif
>
This should be framed as a fix, even without the coming list
allocation, the mutex init vs destroy is unbalanced. The QAT driver is
the only other variant driver with failures after
vfio_pci_core_init_dev() and it correctly calls release. The bug seems
to date back to eb61eca0e8c3.
Also note that in patch 3, you're introducing a call that can fail at
the end of vfio_pci_core_init_dev() that also doesn't unwind. That
needs to be fixed inside vfio_pci_core_init_dev(). Thanks,
Alex
On Wed, 2 Dec 2026 15:02:50 +0530
<mhonap(a)nvidia.com> wrote:
> From: Manish Honap <mhonap(a)nvidia.com>
>
> vfio_pci_bar_rw() keeps the excluded ranges of a BAR off the device, but
> two other paths can reach BAR MMIO without it.
>
> An ioeventfd writes the BAR directly when it fires, through
> vfio_pci_core_iowriteN(). Refuse an ioeventfd whose range overlaps a
> write-excluded range. This replaces the open-coded MSI-X test: the MSI-X
> table is a write-excluded range, and the BAR length test already refuses
> the part of a table past the BAR.
>
> A dma-buf export of a BAR range lets an importer map it for DMA, which
> reaches a range that the BAR keeps off mmap. Refuse an export whose
> ranges overlap an mmap-excluded range in validate_dmabuf_input(), so the
> dma-buf follows the same rule as mmap.
These are different things they fall into different categories. An
ioeventfd is just a write triggered by an eventfd. The WRITE exclusion
series is incomplete without it. MSI-X shouldn't be left open coded
relative to ioeventfds.
dma-buf export is a memory access through DMA. It's more related to
mmap. It should be implemented along with VFIO_PCI_EXCLUDE_MMAP.
Thanks,
Alex
>
> Suggested-by: Alex Williamson <alex(a)shazbot.org>
> Signed-off-by: Manish Honap <mhonap(a)nvidia.com>
> ---
> drivers/vfio/pci/vfio_pci_core.c | 4 ++--
> drivers/vfio/pci/vfio_pci_dmabuf.c | 10 ++++++++--
> drivers/vfio/pci/vfio_pci_priv.h | 3 +++
> drivers/vfio/pci/vfio_pci_rdwr.c | 6 ++----
> 4 files changed, 15 insertions(+), 8 deletions(-)
>
> diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
> index 0b9da823456e..5d54d624982f 100644
> --- a/drivers/vfio/pci/vfio_pci_core.c
> +++ b/drivers/vfio/pci/vfio_pci_core.c
> @@ -988,8 +988,8 @@ static int vfio_pci_for_each_slot_or_bus(struct pci_dev *pdev,
> return walk.ret;
> }
>
> -static bool vfio_pci_bar_range_excluded(struct vfio_pci_core_device *vdev,
> - int bar, u64 start, u64 len, u32 flags)
> +bool vfio_pci_bar_range_excluded(struct vfio_pci_core_device *vdev, int bar,
> + u64 start, u64 len, u32 flags)
> {
> struct range r = DEFINE_RANGE(start, start + len - 1);
> struct vfio_pci_excluded_range *x;
> diff --git a/drivers/vfio/pci/vfio_pci_dmabuf.c b/drivers/vfio/pci/vfio_pci_dmabuf.c
> index 7d1d34029b95..6f28cc285e78 100644
> --- a/drivers/vfio/pci/vfio_pci_dmabuf.c
> +++ b/drivers/vfio/pci/vfio_pci_dmabuf.c
> @@ -193,7 +193,8 @@ int vfio_pci_core_get_dmabuf_phys(struct vfio_pci_core_device *vdev,
> }
> EXPORT_SYMBOL_GPL(vfio_pci_core_get_dmabuf_phys);
>
> -static int validate_dmabuf_input(struct vfio_device_feature_dma_buf *dma_buf,
> +static int validate_dmabuf_input(struct vfio_pci_core_device *vdev,
> + struct vfio_device_feature_dma_buf *dma_buf,
> struct vfio_region_dma_range *dma_ranges,
> size_t *lengthp)
> {
> @@ -207,6 +208,11 @@ static int validate_dmabuf_input(struct vfio_device_feature_dma_buf *dma_buf,
> if (!len || !PAGE_ALIGNED(offset) || !PAGE_ALIGNED(len))
> return -EINVAL;
>
> + if (vfio_pci_bar_range_excluded(vdev, dma_buf->region_index,
> + offset, len,
> + VFIO_PCI_EXCLUDE_MMAP))
> + return -EINVAL;
> +
> if (check_add_overflow(length, len, &length))
> return -EINVAL;
> }
> @@ -260,7 +266,7 @@ int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags,
> if (IS_ERR(dma_ranges))
> return PTR_ERR(dma_ranges);
>
> - ret = validate_dmabuf_input(&get_dma_buf, dma_ranges, &length);
> + ret = validate_dmabuf_input(vdev, &get_dma_buf, dma_ranges, &length);
> if (ret)
> goto err_free_ranges;
>
> diff --git a/drivers/vfio/pci/vfio_pci_priv.h b/drivers/vfio/pci/vfio_pci_priv.h
> index 6def038cb213..a969ae3025c9 100644
> --- a/drivers/vfio/pci/vfio_pci_priv.h
> +++ b/drivers/vfio/pci/vfio_pci_priv.h
> @@ -55,6 +55,9 @@ struct vfio_pci_excluded_range {
> u32 flags;
> };
>
> +bool vfio_pci_bar_range_excluded(struct vfio_pci_core_device *vdev, int bar,
> + u64 start, u64 len, u32 flags);
> +
> #ifdef CONFIG_VFIO_PCI_VGA
> ssize_t vfio_pci_vga_rw(struct vfio_pci_core_device *vdev, char __user *buf,
> size_t count, loff_t *ppos, bool iswrite);
> diff --git a/drivers/vfio/pci/vfio_pci_rdwr.c b/drivers/vfio/pci/vfio_pci_rdwr.c
> index 04aed725325c..e620798f9007 100644
> --- a/drivers/vfio/pci/vfio_pci_rdwr.c
> +++ b/drivers/vfio/pci/vfio_pci_rdwr.c
> @@ -475,10 +475,8 @@ int vfio_pci_ioeventfd(struct vfio_pci_core_device *vdev, loff_t offset,
> if (pos + count > pci_resource_len(pdev, bar))
> return -EINVAL;
>
> - /* Disallow ioeventfds working around MSI-X table writes */
> - if (bar == vdev->msix_bar &&
> - !(pos + count <= vdev->msix_offset ||
> - pos >= vdev->msix_offset + vdev->msix_size))
> + if (vfio_pci_bar_range_excluded(vdev, bar, pos, count,
> + VFIO_PCI_EXCLUDE_WRITE))
> return -EINVAL;
>
> io = vfio_pci_core_get_iomap(vdev, bar);
On Wed, Dec 02, 2026 at 02:54:07PM +1100, Alistair Popple wrote:
> So what I'm hearing is that the exporter should be unmapping as a defense
> against bad importers. I can definitely understand why that might be useful,
> especially as some kind of debug thing, but I don't think we should be basing
> the whole API design around the concept we can't trust importer to implement the
> contract correctly. Especially as that defense might not actually exist in a lot
> of cases anyway (eg. IOMMU disabled), in which case you have no choice but to
> trust the importer got things right.
+1
> I also sense there might be a deeper belief that the mapping of a resource
> (ie. the IOVA) is something owned by the resource rather than the user of that
> resource.
Well, this would be wrong. The IOVA is owned by the DMA API, the DMA
API is owned by its bound driver. In the kernel it is *never* owned by
the memory object being mapped by it.
Jason
On Wed, Dec 02, 2026 at 11:41:46AM +1000, Dave Airlie wrote:
> On Wed, 2 Dec 2026 at 11:30, Jason Gunthorpe <jgg(a)ziepe.ca> wrote:
> >
> > On Wed, Dec 02, 2026 at 10:47:28AM +1000, Dave Airlie wrote:
> >
> > > I think this is clearly where the divide is. I'm with Christian here,
> > > the exporter allocate the resource, then it owns the lifetime of the
> > > resource. In order for it to have complete ownership of the lifetime
> > > of the resource it needs the ability to remove the resource from other
> > > users (i.e. importers). If the exporter wasn't to free some pages, it
> > > should have all the information necessary to do so but traversing a
> > > list of places the object has been imported and a method to forcible
> > > unlink it from that side.
> >
> > If you change iommu mappings without telling the driver you will
> > create data corruption as new mappings can fall to places the device
> > is still doing DMA.
> >
> > The driver MUST be involved in any reclaim/move/revoke process and
> > DMABUF already has APIs for that. Managing this lifetime is one of the
> > main things it does.
> >
> > I don't like this notion we can trust one side more than the other, it
> > isn't like the exporter drivers are amazing clean things either. There
> > is a reasonable level of nannying..
>
> This isn't just about trust, it's about lifetime and ownership. If the
> exporter owns the resource, the exporter should have all necessary
> information to invalidate the resource and give it back to the system.
Importers own their DMA mappings and are the only things that should
ever be touching them. That is the way we expect DMA APIU to be used,
it is why Rust has the DMA API wrapped with the bound driver concept.
It is an extremely abnormal lifetime to suggest that some driver
should be operating the DMA API of someone elses device.
I don't agree at all that having exporters operating the DMA API of
importer devices makes any sense from a kernel lifetime perspective.
> > Importers are expected to unmap. I think we have to consider any one
> > that doesn't to be broken. You can't fix broken with more broken
> > things like force DMA unmapping and hoping that there is an iommu,
> > then hoping no corruption happens to crash the system.
>
> But we should allow the exporter to tell the imported to invalidate,
> wait for a response and then have the exporter free it.
We have that already.
> What the importer does in the invalidate is up to it, but the
> exporter controls the resource lifetime and any things bound to
> it.
Once the exporter asks for it to be invalidated it gets to assume
nothing is touching it anymore.
There is no reason for it to operate the DMA API of another driver,
and doing so doesn't give more protections or a better life time
design.
> If the importer fails to invalidate the resource then that is a
> clear problem. But an exporter can at least control all the mappings
> for the resource and tear them down without leaking things.
It really can't. Without an iommu the dma unmap does nothing. If you
unmap while DMA is still running you risk random corruption from
future maps.
Choosing to unmap without synchronizing in the HW is an extreme
dangerous action, and IMHO, accepting this buggy choice should be in
side the driver that is doing it, not hidden in layers of software.
> > I just saw someone in drivers/media do the most insane thing with the
> > new DMA API, unbelievable. No matter what we build someone will find a
> > creative way to abuse it.
> >
> > We need to have a 3rd party "Switzerland" if you want to keep this
> > direction that limits the exposure of either side to the details.
>
> If I had rust for this I'd be happier, but we don't get a
> switzerland,
Why not? This thread had several offers of some kind of dmabuf related
mapping library whatever you want to call it.
That's a fine Switzerland.
> but we should at least be speaking in terms of ownership and lifetime
> of resources and what can reasonable be defended against.
IMHO most of the problem here is the push to make unreasonable and
difficult design choices because we have to defend against other
drivers in the kernel. While somehow, against all reason, insisting
that the exporting DRM drivers should be given the most sensitive
access of everything.
> Okay maybe on that one we can give in, I do think path + offset/length
> lists might make sense for things like NVLINK/UALINK though were there
> is more arbitrary path to the resource than just what is CPU
> visible,
I think UALINK type devices you want a VRAM offset relative to the end
point. That gets converted into a fabric address through the fabric
mapping hardware.
> but maybe dma_addr_t can encapsulate that, but for some things it
No. Do not abuse dma_addr_t for things that do not come from the DMA
API, I think this has been said many times over the years. :(
> > Even the P2P permitted placement selection shouldn't be open coded in
> > drivers, this is a bit tricky to do well and none of the DRM drivers
> > get there.
>
> Because nobody has written the code to do it?
Lack of abstractions in dmabuf to help manage all of this, IMHO.
> > FWIW it is a huge pita to make things work like this inside iommufd
> > and if it gets messed up then iommufd will explode in a bad way.
> > It is so much easier to feed through phys_addr_t since we already have
> > this infrastructure.
>
> It's easier, but you don't respect lifetimes then.
Ridiculous. iommufd can be trusted to honor the existing invalidate
callback. Far more than we can trust a random DRM driver to operate
the iommu API correctly.
It is completely lifetime inappropriate for any other drivers to be
reaching into the iommu_domain object entirely and fully controlled by
iommufd. It breaks the encapsulation, complicates the code, and
invites creative abuses from the GPU drivers.
Do not use lifetimes as a justification for this backwards design.
Jason
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(a)nvidia.com>
>
> ---
>
> 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;
> + }
> + }