On 11/20/26 06:55, Alistair Popple wrote:
> From: Jason Gunthorpe <jgg(a)nvidia.com>
>
> The SGT (Scatter Gather Table) DMA mapping type represents the existing
> sg_table/scatterlist based DMA mapping. For now it continues to use a
> sg_table for map/unmap, but this will allow a future patch series to
> convert to using a different data structure for importers without having
> to convert all at once.
I strongly think we shouldn't have an SGT mapping type in the new DMA-buf mapping API.
Instead we should have something like either an array or an xarray of DMA addresses.
The framework should then either connect the SGT directly if both importer and export are using the "classic" interface or convert from DMA-addr array to SGT and back if either of them are using this new interface.
The next step should then probably be to switch over the DRM drivers to use this interface and exercise if everything still works fine.
That should be relatively straight forward because TTM drivers already use a DMA-addr array internally anyway (see function drm_prime_sg_to_dma_addr_array()).
I can also put somebody on that task should we agree that this is the way forward.
Regards,
Christian.
>
> dma_buf_sgt_compat_match_mapping() is used during the conversion of
> exporters to transparently wrap an unaware exporter's sg_table. It is
> removed later in this series once all exporters have been converted.
>
> A mapping is an object allocated by an exporter which embeds the generic
> struct dma_buf_mapping type header which core dma-buf code fills in. For
> the SGT type that is currently a struct dma_buf_sgt_mapping embedding a
> struct sg_table.
>
> Importers continue to receive a struct sg_table from dma_buf_map_sgt()
> and are unaffected. A future series will convert the embedded sg_table
> into a list of DMA addresses more appropriate for importers.
>
> Signed-off-by: Jason Gunthorpe <jgg(a)nvidia.com>
> Signed-off-by: Alistair Popple <apopple(a)nvidia.com>
>
> ---
>
> Changes for v3:
>
> - PCIe P2P will be added as its own type in future, so dropped the P2P
> flags from the SGT type.
> - Exporters allocate and return struct dma_buf_sgt_mapping with the
> sg_table embedded instead of a bare struct sg_table. This ensures
> an unmap happens against the correct type once multiple mapping types
> are possible.
> ---
> drivers/dma-buf/dma-buf-mapping.c | 104 +++++++++++++++++++++++++++++
> include/linux/dma-buf-mapping.h | 135 ++++++++++++++++++++++++++++++++++++++
> include/linux/dma-buf.h | 6 +-
> 3 files changed, 244 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/dma-buf/dma-buf-mapping.c b/drivers/dma-buf/dma-buf-mapping.c
> index d27f02610c2a..cc3d8d6fe009 100644
> --- a/drivers/dma-buf/dma-buf-mapping.c
> +++ b/drivers/dma-buf/dma-buf-mapping.c
> @@ -6,6 +6,7 @@
> #include <linux/dma-buf-mapping.h>
> #include <linux/dma-resv.h>
> #include <linux/dma-buf.h>
> +#include <linux/seq_file.h>
> #include <linux/overflow.h>
> #include <linux/align.h>
>
> @@ -322,3 +323,106 @@ int dma_buf_match_mapping(struct dma_buf_match_args *args,
> return 0;
> }
> EXPORT_SYMBOL_NS_GPL(dma_buf_match_mapping, "DMA_BUF");
> +
> +static void
> +dma_buf_sgt_finish_match(struct dma_buf_match_args *args,
> + const struct dma_buf_mapping_match *exp,
> + const struct dma_buf_mapping_match *imp,
> + struct dma_buf_mapping_match *match)
> +{
> + struct dma_buf_attachment *attach = args->attach;
> +
> + *match = (struct dma_buf_mapping_match) {
> + .type = &dma_buf_mapping_sgt_type,
> + .exp_ops = exp->exp_ops,
> + .sgt_data = {
> + .importing_dma_device = args->dev,
> + },
> + };
> +
> + /*
> + * Setup the SGT type variables stored in attach because importers and
> + * exporters that do not natively use mappings expect them to be there.
> + * When converting to use mappings users should use the match versions
> + * of these instead.
> + */
> + attach->dev = args->dev;
> +}
> +
> +static void dma_buf_sgt_debugfs_dump(struct seq_file *s,
> + struct dma_buf_attachment *attach)
> +{
> + seq_printf(s, " %s", dev_name(dma_buf_sgt_dma_device(attach)));
> +}
> +
> +const struct dma_buf_mapping_type dma_buf_mapping_sgt_type = {
> + .name = "DMA Mapped Scatter Gather Table",
> + .finish_match = dma_buf_sgt_finish_match,
> + .debugfs_dump = dma_buf_sgt_debugfs_dump,
> +};
> +EXPORT_SYMBOL_NS_GPL(dma_buf_mapping_sgt_type, "DMA_BUF");
> +
> +/* An unaware exporter's sg_table presented as an SGT mapping */
> +struct dma_buf_sgt_compat_mapping {
> + struct dma_buf_sgt_mapping mapping;
> + struct sg_table *sgt;
> +};
> +
> +static struct dma_buf_sgt_mapping *
> +dma_buf_sgt_compat_map_dma_buf(struct dma_buf_attachment *attach,
> + enum dma_data_direction dir)
> +{
> + struct dma_buf_sgt_compat_mapping *compat;
> + struct sg_table *sgt;
> +
> + sgt = attach->dmabuf->ops->map_dma_buf(attach, dir);
> + if (!sgt)
> + return ERR_PTR(-ENOMEM);
> + if (IS_ERR(sgt))
> + return ERR_CAST(sgt);
> +
> + compat = kzalloc_obj(*compat);
> + if (!compat) {
> + attach->dmabuf->ops->unmap_dma_buf(attach, sgt, dir);
> + return ERR_PTR(-ENOMEM);
> + }
> +
> + /* The exporter keeps its table; the mapping shares its entries */
> + compat->sgt = sgt;
> + compat->mapping.sgt = *sgt;
> + return &compat->mapping;
> +}
> +
> +static void dma_buf_sgt_compat_unmap_dma_buf(struct dma_buf_attachment *attach,
> + struct dma_buf_sgt_mapping *mapping,
> + enum dma_data_direction dir)
> +{
> + struct dma_buf_sgt_compat_mapping *compat =
> + container_of(mapping, struct dma_buf_sgt_compat_mapping, mapping);
> +
> + attach->dmabuf->ops->unmap_dma_buf(attach, compat->sgt, dir);
> + kfree(compat);
> +}
> +
> +/* Route the classic map/unmap ops through the exp ops for old importers */
> +static const struct dma_buf_mapping_sgt_exp_ops dma_buf_sgt_compat_exp_ops = {
> + .map_dma_buf = dma_buf_sgt_compat_map_dma_buf,
> + .unmap_dma_buf = dma_buf_sgt_compat_unmap_dma_buf,
> +};
> +
> +/*
> + * This entry is used for unaware exporters that do not support
> + * match_mapping(). It wraps the dma_buf ops for SGT mappings into a mapping
> + * type so aware importers can transparently work with unaware exporters. This
> + * does not require p2p because old exporters will check it through the
> + * attach->peer2peer mechanism.
> + */
> +static const struct dma_buf_mapping_match dma_buf_sgt_exp_compat_matches[] = {
> + DMA_BUF_EMAPPING_SGT(&dma_buf_sgt_compat_exp_ops),
> +};
> +
> +int dma_buf_sgt_compat_match_mapping(struct dma_buf_match_args *args)
> +{
> + return dma_buf_match_mapping(args, dma_buf_sgt_exp_compat_matches,
> + ARRAY_SIZE(dma_buf_sgt_exp_compat_matches));
> +}
> diff --git a/include/linux/dma-buf-mapping.h b/include/linux/dma-buf-mapping.h
> index bb826bdc2911..8be03b4ba6cc 100644
> --- a/include/linux/dma-buf-mapping.h
> +++ b/include/linux/dma-buf-mapping.h
> @@ -84,6 +84,30 @@ struct dma_buf_mapping_type {
> struct dma_buf_attachment *attach);
> };
>
> +/**
> + * struct dma_buf_mapping - header of a mapping made through an attachment
> + * @match: The match set entry the mapping was made with
> + *
> + * A mapping is an object allocated by whoever produces it, the exporter for
> + * the SGT type, embedding this header. The core fills the header in once the
> + * producer's map callback has returned and reads it to tear the mapping down.
> + */
> +struct dma_buf_mapping {
> + const struct dma_buf_mapping_match *match;
> +};
> +
> +/**
> + * dma_buf_mapping_type_of - The type a mapping was made with
> + * @mapping: the mapping
> + *
> + * Returns: the type of the match set entry recorded in @mapping.
> + */
> +static inline const struct dma_buf_mapping_type *
> +dma_buf_mapping_type_of(const struct dma_buf_mapping *mapping)
> +{
> + return mapping->match->type;
> +}
> +
> struct sg_table *dma_buf_phys_vec_to_sgt(struct dma_buf_attachment *attach,
> struct p2pdma_provider *provider,
> struct phys_vec *phys_vec,
> @@ -109,4 +133,115 @@ dma_buf_attach_map_type(const struct dma_buf_attachment *attach)
> return &attach->matches[0];
> }
>
> +/*
> + * DMA Mapped Scatterlist Type
> + *
> + * When this type is matched the map/unmap functions are:
> + *
> + * dma_buf_map_sgt()
> + * dma_buf_unmap_sgt()
> + *
> + * The struct sg_table returned by those functions has only the DMA portions
> + * available. The caller must not try to use the struct page * information.
> + *
> + * importing_dma_device is the device passed to dma_buf_dynamic_attach(),
> + * recorded at match time, and is passed to the DMA API to provide the
> + * dma_addr_t's.
> + */
> +extern const struct dma_buf_mapping_type dma_buf_mapping_sgt_type;
> +
> +/**
> + * struct dma_buf_sgt_mapping - A mapping of the SGT type
> + * @base: Common header, filled in by the core
> + * @sgt: The DMA mapped scatterlist, filled in by the exporter
> + *
> + * Allocated by the exporter's &dma_buf_mapping_sgt_exp_ops.map_dma_buf, which
> + * may embed it in a larger structure of its own, and released by its
> + * unmap_dma_buf. The core only fills in @base, with the same values for every
> + * map of an attachment, so an exporter that keeps one table per attachment
> + * may keep one of these per attachment and return it on every map. A mapping
> + * must not be shared between attachments. Importers are handed @sgt by
> + * dma_buf_map_sgt().
> + */
> +struct dma_buf_sgt_mapping {
> + struct dma_buf_mapping base;
> + struct sg_table sgt;
> +};
> +
> +/**
> + * struct dma_buf_mapping_sgt_exp_ops - Exporter ops of the SGT type
> + * @ops: Type tag
> + * @map_dma_buf: Produce a &struct dma_buf_sgt_mapping for @attach, with
> + * &dma_buf_sgt_mapping.sgt mapped for the device returned by
> + * dma_buf_sgt_dma_device(). Called with the dma_resv lock held.
> + * Returns the mapping, or an ERR_PTR; NULL is taken as -ENOMEM.
> + * @unmap_dma_buf: Unmap and release a mapping returned by @map_dma_buf.
> + */
> +struct dma_buf_mapping_sgt_exp_ops {
> + struct dma_buf_mapping_exp_ops ops;
> + struct dma_buf_sgt_mapping *(*map_dma_buf)(struct dma_buf_attachment *attach,
> + enum dma_data_direction dir);
> + void (*unmap_dma_buf)(struct dma_buf_attachment *attach,
> + struct dma_buf_sgt_mapping *mapping,
> + enum dma_data_direction dir);
> +};
> +
> +/**
> + * dma_buf_sgt_dma_device - Return the device to use for DMA mapping
> + * @attach: sgt mapping type attachment
> + *
> + * Called by the exporter to get the struct device to pass to the DMA API
> + * during the map callback. The unmap callback takes it from the mapping with
> + * dma_buf_sgt_mapping_dma_device().
> + *
> + * Returns: the importing device, or NULL if the attachment is not mapped with
> + * the SGT type.
> + */
> +static inline struct device *
> +dma_buf_sgt_dma_device(struct dma_buf_attachment *attach)
> +{
> + if (dma_buf_attach_map_type(attach)->type != &dma_buf_mapping_sgt_type)
> + return NULL;
> + return dma_buf_attach_map_type(attach)->sgt_data.importing_dma_device;
> +}
> +
> +static inline const struct dma_buf_mapping_sgt_exp_ops *
> +dma_buf_get_sgt_ops(struct dma_buf_attachment *attach)
> +{
> + if (dma_buf_attach_map_type(attach)->type != &dma_buf_mapping_sgt_type)
> + return NULL;
> + return container_of(dma_buf_attach_map_type(attach)->exp_ops,
> + struct dma_buf_mapping_sgt_exp_ops, ops);
> +}
> +
> +/**
> + * dma_buf_sgt_mapping_dma_device - Return the device a mapping was made for
> + * @mapping: the mapping passed to the exporter's unmap callback
> + *
> + * Called by the exporter to get the struct device to pass to the DMA API
> + * during the unmap callback. It comes from the match set entry recorded in
> + * the mapping, so it is the device the mapping was DMA mapped for whatever
> + * the attachment is mapped with by the time of the unmap.
> + *
> + * Returns: the device the mapping was DMA mapped for.
> + */
> +static inline struct device *
> +dma_buf_sgt_mapping_dma_device(const struct dma_buf_sgt_mapping *mapping)
> +{
> + return mapping->base.match->sgt_data.importing_dma_device;
> +}
> +
> +/*
> + * Importer SGT entry. Usable in a static table: the importing device is
> + * supplied by dma_buf_dynamic_attach() and recorded in the matched entry.
> + */
> +#define DMA_BUF_IMAPPING_SGT() \
> + ((struct dma_buf_mapping_match){ .type = &dma_buf_mapping_sgt_type })
> +#define DMA_BUF_EMAPPING_SGT(_exp_ops) \
> + ((struct dma_buf_mapping_match){ .type = &dma_buf_mapping_sgt_type, \
> + .exp_ops = &((_exp_ops)->ops) })
> +
> +/* Match on behalf of an exporter that has no match_mapping() */
> +int dma_buf_sgt_compat_match_mapping(struct dma_buf_match_args *args);
> +
> #endif
> diff --git a/include/linux/dma-buf.h b/include/linux/dma-buf.h
> index 81bbcd420d60..6b423e22174a 100644
> --- a/include/linux/dma-buf.h
> +++ b/include/linux/dma-buf.h
> @@ -46,7 +46,11 @@ struct dma_buf_mapping_match {
> const struct dma_buf_mapping_type *type;
> const struct dma_buf_mapping_exp_ops *exp_ops;
> union {
> - /* Each mapping_type has unique match parameters here */
> + /* For dma_buf_mapping_sgt_type */
> + struct {
> + /* Filled from the attaching device, not by importers */
> + struct device *importing_dma_device;
> + } sgt_data;
> };
> };
>
>
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_pages(). The allocator rounds a request to the architecture
shared granule, allocates suitably aligned contiguous pages, transitions
the complete allocation to shared state, and returns the transitioned size
alongside the page.
The corresponding free helper 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 the common layout 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 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: Baoquan He <baoquan.he(a)linux.dev>
Cc: Mike Rapoport <rppt(a)kernel.org>
Cc: Pasha Tatashin <pasha.tatashin(a)soleen.com>
Cc: Pratyush Yadav <pratyush(a)kernel.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: Thomas Gleixner <tglx(a)kernel.org>
Cc: Will Deacon <will(a)kernel.org>
Cc: Russell King <linux(a)armlinux.org.uk>
Cc: Benjamin Gaignard <benjamin.gaignard(a)collabora.com>
Cc: Brian Starkey <Brian.Starkey(a)arm.com>
Cc: John Stultz <jstultz(a)google.com>
Cc: Mark Rutland <mark.rutland(a)arm.com>
Cc: Radu Rendec <radu(a)rendec.net>
Cc: "T.J. Mercier" <tjmercier(a)google.com>
Cc: Madhavan Srinivasan <maddy(a)linux.ibm.com>
Cc: Michael Ellerman <mpe(a)ellerman.id.au>
Cc: Nicholas Piggin <npiggin(a)gmail.com>
Cc: Christophe Leroy (CS GROUP) <chleroy(a)kernel.org>
Cc: Ritesh Harjani (IBM) <ritesh.list(a)gmail.com>
Cc: Shrikanth Hegde <sshegde(a)linux.ibm.com>
Cc: Alexander Gordeev <agordeev(a)linux.ibm.com>
Cc: Gerald Schaefer <gerald.schaefer(a)linux.ibm.com>
Cc: Heiko Carstens <hca(a)linux.ibm.com>
Cc: Vasily Gorbik <gor(a)linux.ibm.com>
Cc: Christian Borntraeger <borntraeger(a)linux.ibm.com>
Cc: Sven Schnelle <svens(a)linux.ibm.com>
Cc: Ingo Molnar <mingo(a)redhat.com>
Cc: Borislav Petkov <bp(a)alien8.de>
Cc: Dave Hansen <dave.hansen(a)linux.intel.com>
Cc: x86(a)kernel.org
Cc: H. Peter Anvin <hpa(a)zytor.com>
Cc: Kiryl Shutsemau <kas(a)kernel.org>
Cc: Rick Edgecombe <rick.p.edgecombe(a)intel.com>
Cc: K. Y. Srinivasan <kys(a)microsoft.com>
Cc: Haiyang Zhang <haiyangz(a)microsoft.com>
Cc: Wei Liu <wei.liu(a)kernel.org>
Cc: Dexuan Cui <decui(a)microsoft.com>
Cc: Long Li <longli(a)microsoft.com>
Cc: Paolo Bonzini <pbonzini(a)redhat.com>
Cc: Vitaly Kuznetsov <vkuznets(a)redhat.com>
Cc: Andy Lutomirski <luto(a)kernel.org>
Cc: Peter Zijlstra <peterz(a)infradead.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) (13):
mm: Add an allocator for CoCo shared memory
mm: Zero memory during shared memory transitions
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: Allocate CoCo atomic pools using CoCo shared memory
allocator
dma-direct: Align CoCo shared DMA allocations to the shared granule
size
swiotlb: Align shared IO TLB pools to the shared granule size
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
mm: Assert CoCo shared allocations may sleep
MAINTAINERS | 2 +
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 | 6 +-
arch/x86/include/asm/set_memory.h | 1 +
arch/x86/include/asm/vga.h | 4 +-
arch/x86/kernel/kvmclock.c | 6 +-
arch/x86/mm/mem_encrypt_amd.c | 1 +
arch/x86/mm/pat/set_memory.c | 19 +-
drivers/dma-buf/heaps/system_heap.c | 128 +++++-----
drivers/firmware/arm_rmm/rsi.c | 9 +
drivers/hv/connection.c | 41 ++-
drivers/hv/hv.c | 11 +-
drivers/hv/hv_common.c | 2 -
drivers/iommu/dma-iommu.c | 2 +-
drivers/irqchip/irq-gic-v3-its.c | 42 ++--
drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c | 4 +
include/linux/cc_shared.h | 39 +++
include/linux/dma-map-ops.h | 9 +-
include/linux/set_memory.h | 8 +-
kernel/dma/contiguous.c | 41 ++-
kernel/dma/direct.c | 73 ++++--
kernel/dma/ops_helpers.c | 2 +-
kernel/dma/pool.c | 27 +-
kernel/dma/swiotlb.c | 94 ++++---
kernel/kexec_file.c | 3 +-
mm/Makefile | 1 +
mm/cc_shared.c | 238 ++++++++++++++++++
30 files changed, 635 insertions(+), 211 deletions(-)
create mode 100644 include/linux/cc_shared.h
create mode 100644 mm/cc_shared.c
base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
prerequisite-patch-id: 121492d71cc22b2adbfdedd668868c5a449ad764
prerequisite-patch-id: ed31fd728f0adba3d60dcd6f3bae8da1a2193bcc
--
2.43.0
Hi Thomas, Natalie, T.J. and Philip,
could you guys take a look at this?
IIRC we wanted to do the same thing for TTM and DMA-buf heaps, but at least the TTM approach failed and we had to revert it.
If I'm not completely mistaken it should now work fine and is quite a nice performance improvement when mapping large stuff into the CPU page tables.
Regards,
Christian.
On 11/22/26 08:37, Qiliang Yuan wrote:
> QEMU and crosvm hand guest memory to the host renderer and display as
> udmabufs for virtio-gpu blob resources. The guest memory is a memfd that
> is often backed by 2 MiB huge pages, and the host process mmap()s the
> dma-buf to access it with the CPU.
>
> udmabuf maps every page with its own PTE: the first fault inserts a PFN
> for each 4 KiB page of the whole VMA. Mapping and touching 4 GiB this way
> takes over 200 ms and a million PTEs, although the memory behind it is
> 2048 huge pages.
>
> Add a ->huge_fault() handler that maps a PMD-sized range with one PMD
> when it is backed by an aligned run of contiguous PFNs, and falls back to
> PTEs otherwise. Have the pre-mapping on the first fault skip the ranges
> the handler will map, so that their PTEs don't take the place of the
> PMDs, and keep pre-mapping everything when the VMA can't have PMDs.
>
> Mapping a udmabuf over a memfd filled with data and reading a byte of
> each 4 KiB page through the mapping, base and patched kernels alternating
> over 3 rounds of 5 runs (7 for shmem 4K), median of the per-round medians
> and range in parentheses:
>
> backing size before (ms) after (ms) change
> hugetlb 256M 13.3 (13.3-13.7) 1.7 (1.7-1.8) -87%
> hugetlb 1G 53.6 (53.0-53.7) 6.5 (4.2-6.6) -88%
> hugetlb 4G 211 (211-212) 23.6 (17.6-25.2) -89%
> hugetlb 8G 423 (422-424) 45.8 (45.8-47.2) -89%
> shmem THP 1G 52.8 (52.5-53.1) 6.3 (5.8-6.8) -88%
> shmem THP 4G 210 (207-214) 20.7 (19.5-21.1) -90%
> shmem 4K 1G 52.0 (51.6-52.7) 51.8 (51.5-52.9) 0%
> shmem 4K 4G 209 (209-211) 208 (208-209) 0%
>
> Signed-off-by: Qiliang Yuan <qiliang.yuan(a)linux.dev>
> ---
> drivers/dma-buf/udmabuf.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++-
> 1 file changed, 68 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c
> index 018937435356e..f9c908f8b53a2 100644
> --- a/drivers/dma-buf/udmabuf.c
> +++ b/drivers/dma-buf/udmabuf.c
> @@ -44,12 +44,63 @@ struct udmabuf {
> struct miscdevice *device;
> };
>
> +/*
> + * Return whether the PMD-sized range of @vma at @addr is backed by one aligned
> + * run of contiguous PFNs, which can then be mapped with a single PMD.
> + */
> +static bool udmabuf_pmd_mappable(struct vm_area_struct *vma, unsigned long addr,
> + unsigned long *pfn)
> +{
> + struct udmabuf *ubuf = vma->vm_private_data;
> + const pgoff_t nr = PMD_SIZE >> PAGE_SHIFT;
> + unsigned long first;
> + pgoff_t pgoff, i;
> +
> + if (!IS_ENABLED(CONFIG_ARCH_SUPPORTS_PMD_PFNMAP))
> + return false;
> +
> + if (!IS_ALIGNED(addr, PMD_SIZE) || addr < vma->vm_start ||
> + vma->vm_end - addr < PMD_SIZE)
> + return false;
> +
> + pgoff = vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT);
> + if (pgoff >= ubuf->pagecount || ubuf->pagecount - pgoff < nr)
> + return false;
> +
> + first = page_to_pfn(ubuf->pages[pgoff]);
> + if (!IS_ALIGNED(first, nr))
> + return false;
> +
> + for (i = 1; i < nr; i++) {
> + if (page_to_pfn(ubuf->pages[pgoff + i]) != first + i)
> + return false;
> + }
> +
> + *pfn = first;
> + return true;
> +}
> +
> +#ifdef CONFIG_ARCH_SUPPORTS_HUGE_PFNMAP
> +static vm_fault_t udmabuf_vm_huge_fault(struct vm_fault *vmf,
> + unsigned int order)
> +{
> + unsigned long pfn;
> +
> + if (order != PMD_ORDER ||
> + !udmabuf_pmd_mappable(vmf->vma, vmf->address & PMD_MASK, &pfn))
> + return VM_FAULT_FALLBACK;
> +
> + return vmf_insert_pfn_pmd(vmf, pfn, vmf->flags & FAULT_FLAG_WRITE);
> +}
> +#endif
> +
> static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf)
> {
> struct vm_area_struct *vma = vmf->vma;
> struct udmabuf *ubuf = vma->vm_private_data;
> pgoff_t pgoff = vmf->pgoff;
> unsigned long addr, pfn;
> + bool pmd_ok;
> vm_fault_t ret;
>
> if (pgoff >= ubuf->pagecount)
> @@ -61,7 +112,12 @@ static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf)
> if (ret & VM_FAULT_ERROR)
> return ret;
>
> - /* pre fault */
> + /*
> + * Pre-fault the rest of the VMA, except for the ranges that
> + * udmabuf_vm_huge_fault() maps with a PMD once they are accessed.
> + */
> + pmd_ok = thp_vma_allowable_order(vma, vma->vm_flags, TVA_PAGEFAULT,
> + PMD_ORDER);
> pgoff = vma->vm_pgoff;
> addr = vma->vm_start;
>
> @@ -69,6 +125,14 @@ static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf)
> if (addr == vmf->address)
> continue;
>
> + if (pmd_ok && IS_ALIGNED(addr, PMD_SIZE) &&
> + (vmf->address & PMD_MASK) != addr &&
> + udmabuf_pmd_mappable(vma, addr, &pfn)) {
> + pgoff += (PMD_SIZE >> PAGE_SHIFT) - 1;
> + addr += PMD_SIZE - PAGE_SIZE;
> + continue;
> + }
> +
> if (WARN_ON(pgoff >= ubuf->pagecount))
> break;
>
> @@ -89,6 +153,9 @@ static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf)
>
> static const struct vm_operations_struct udmabuf_vm_ops = {
> .fault = udmabuf_vm_fault,
> +#ifdef CONFIG_ARCH_SUPPORTS_HUGE_PFNMAP
> + .huge_fault = udmabuf_vm_huge_fault,
> +#endif
> };
>
> static int mmap_udmabuf(struct dma_buf *buf, struct vm_area_struct *vma)
>
> --
> 2.43.0
>
On 11/22/26 08:37, Qiliang Yuan wrote:
> A dma-buf exporter whose buffer is backed by physically contiguous memory
> can map it with PMDs from a ->huge_fault() handler, which only works where
> the user address and the buffer offset are aligned alike to PMD_SIZE.
>
> dma_buf_fops has no ->get_unmapped_area(), so mmap() places a dma-buf at
> whatever page aligned address the default allocator returns. A 4 GiB
> udmabuf, for one, lands 616 KiB past a 2 MiB boundary, and then none of
> its PMD-sized ranges can be mapped with a PMD.
>
> Use thp_get_unmapped_area(), as filesystems that support huge mappings
> do. It aligns a mapping that covers at least one whole PMD to the buffer
> offset and leaves smaller mappings where they would be placed anyway.
>
> Signed-off-by: Qiliang Yuan <qiliang.yuan(a)linux.dev>
Reviewed-by: Christian König <christian.koenig(a)amd.com>
> ---
> drivers/dma-buf/dma-buf.c | 1 +
> 1 file changed, 1 insertion(+)
>
> diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c
> index 5c6957f8839a8..015a03c8d04db 100644
> --- a/drivers/dma-buf/dma-buf.c
> +++ b/drivers/dma-buf/dma-buf.c
> @@ -608,6 +608,7 @@ static void dma_buf_show_fdinfo(struct seq_file *m, struct file *file)
> static const struct file_operations dma_buf_fops = {
> .release = dma_buf_file_release,
> .mmap = dma_buf_mmap_internal,
> + .get_unmapped_area = thp_get_unmapped_area,
> .llseek = dma_buf_llseek,
> .poll = dma_buf_poll,
> .unlocked_ioctl = dma_buf_ioctl,
>
> --
> 2.43.0
>
Hi Olivier,
kernel test robot noticed the following build warnings:
[auto build test WARNING on d922ed738439e1d5b4701df7f4cdd49a09925624]
url: https://github.com/intel-lab-lkp/linux/commits/Olivier-Moysan/dt-bindings-i…
base: d922ed738439e1d5b4701df7f4cdd49a09925624
patch link: https://lore.kernel.org/r/20261118-upstream_mdf_v2-v2-8-7939d3c3720a%40foss…
patch subject: [PATCH v2 08/11] iio: adc: stm32-mdf: add dma buffered acquisition support
config: csky-allmodconfig (https://download.01.org/0day-ci/archive/20261121/202611210936.q9qVaCjc-lkp@…)
compiler: csky-linux-gcc (GCC) 16.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261121/202611210936.q9qVaCjc-lkp@…)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp(a)intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202611210936.q9qVaCjc-lkp@intel.com/
All warnings (new ones prefixed by >>):
>> drivers/iio/adc/stm32-mdf-adc.c:1781:12: warning: 'stm32_mdf_adc_resume' defined but not used [-Wunused-function]
1781 | static int stm32_mdf_adc_resume(struct device *dev)
| ^~~~~~~~~~~~~~~~~~~~
>> drivers/iio/adc/stm32-mdf-adc.c:1766:12: warning: 'stm32_mdf_adc_suspend' defined but not used [-Wunused-function]
1766 | static int stm32_mdf_adc_suspend(struct device *dev)
| ^~~~~~~~~~~~~~~~~~~~~
vim +/stm32_mdf_adc_resume +1781 drivers/iio/adc/stm32-mdf-adc.c
1765
> 1766 static int stm32_mdf_adc_suspend(struct device *dev)
1767 {
1768 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1769 struct stm32_mdf_adc *adc = iio_priv(indio_dev);
1770 int ret = 0;
1771
1772 if (iio_buffer_enabled(indio_dev))
1773 ret = stm32_mdf_adc_predisable(indio_dev);
1774
1775 regcache_cache_only(adc->regmap, true);
1776 regcache_mark_dirty(adc->regmap);
1777
1778 return ret;
1779 }
1780
> 1781 static int stm32_mdf_adc_resume(struct device *dev)
1782 {
1783 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1784 struct stm32_mdf_adc *adc = iio_priv(indio_dev);
1785 int ret;
1786
1787 regcache_cache_only(adc->regmap, false);
1788 ret = regcache_sync(adc->regmap);
1789
1790 if (!ret && iio_buffer_enabled(indio_dev))
1791 ret = stm32_mdf_adc_postenable(indio_dev);
1792
1793 return ret;
1794 }
1795
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
On Fri, Nov 13, 2026 at 03:06:13PM +0800, Mengmeng Zhao wrote:
> From: Mengmeng Zhao <zhaomengmeng(a)kylinos.cn>
>
> This series adds dma-buf read/write support for ext4. The dma-buf direct
> I/O infrastructure allows dma-buf to be handed to file I/O as an iov_iter,
> but currently it only supports raw block devices. To better serve the
> GPU-NVMe direct access through normal filesystems, prior work for XFS has
> been discussed at [1]. We are also interested in this field and working on
> it [2].
Can you an Anuj please work together and merge this into a single
series covering both XFS and ext4?
On Mon, Nov 16, 2026 at 03:21:01PM +0100, Andreas Hindborg wrote:
For the fs and pid_namespace bits:
Acked-by: Christian Brauner <brauner(a)kernel.org>