On Mon, Nov 23, 2026 at 6:45 PM Qiliang Yuan <qiliang.yuan(a)linux.dev> 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>
Reviewed-by: T.J. Mercier <tjmercier(a)google.com>
On 11/24/26 07:23, Alistair Popple wrote:
> On 2026-11-24 at 05:37 +1100, Christian König <christian.koenig(a)amd.com> wrote...
>> On 11/23/26 17:57, Jason Gunthorpe wrote:
>>> On Mon, Nov 23, 2026 at 02:36:46PM +0100, Christian König wrote:
>>>> 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.
>>>
>>> My plan was always to get there, but I don't think it is reasonable to
>>> start there? There are too many places all over the place that are
>>> just hardwired to do SGT right now.
>
> Right. There are around 70 drivers upstream that make use of the sg_table handed
> down via a dma-buf import, so I don't think it's tractable to convert them all
> in one go. So obviously we will need some way of phasing this in to give time
> for each driver change to be implemented, reviewed, etc.
My suggestion is not to convert all drivers in one go, but rather to come up with some sane representation of the DMA addresses instead of using an SGT in the new interface.
> Obviously some of those will share a common structure/helper and conversions
> will be similar, so that is an over-estimate. But based on an initial poke
> around there are plenty of drivers that bake the sg_table more deeply into their
> data structures.
>
>>> Part of the design of this series was to allow the conversion away
>>> from SGT to be done incrementally without breaking compatibility.
>>
>> Do you know the joke from the hitchhikers guide to the galaxy where the dolphins are happy for exactly the same reasons?
>>
>> I'm pushing against this plan for exactly the same reason because I think it will make the conversation away from SGT even more problematic than it already is.
>>
>>> So it is effectively this:
>>>
>>>> The framework should then either connect the SGT directly if both
>>>> importer and export are using the "classic" interface
>>
>> Yeah but that should not be a separate mapping type, it's rather that we just convert back and forth from DMA addresses to SGT for compatibility reasons.
>
> Right, I think this is actually fairly close to my plan, which is to provide a type
> for each address class an exporter can produce - either a CPU physical address
> or some device context + P2P/MMIO address but in future could be some other
> interconnect type.
>
> The core then negotiates compatible types and adds converters between them,
I have strong objections to that plan and would potentially NAK it.
DMA-buf should *NEVER* accept CPU physical addresses nor provide conversion between different address types.
Keep in mind that DMA-buf is solely a container for DMA addresses, nothing else and it should not provide any mapping functionality whatsoever.
> and having SGT as a type is what allows that. It only exists for unconverted
> exporters and importers and eventually goes away once everything is converted.
> The idea being we don't have to convert everything all at once, whilst also being
> able to add support for different interconnect types. Are you ok with the SGT
> type if it is just transitional?
Not really, it just complicate things.
What we should do with the mapping interface is to add the information in which domain the DMA addresses are to be used.
The conversion between SGT and DMA address array is an exception we can make to aid transition between the old and the new interface, but that is basically a 1 to 1 copy between different data structures.
> As discussed a bit previously types also add enforcement of the rules around when
> the exporter type/location can change.
>
> I do have most of this coded up, so can post a mega series of all patches if
> it's helpful to see the overall direction in code form. I didn't do that because
> this conversion of exporters to not return sg_tables makes it quite a large
> patch series, so I was going to post them as separate RFCs building on top of
> this. Not sure if you have a preference there?
I think we should have the core interface plus some test cases and at least one driver converted over as example of how the new interface would be used.
>>> And this:
>>>
>>>> or convert
>>>> from DMA-addr array to SGT and back if either of them are using this
>>>> new interface.
>>>look like
>>> Is something I imagined as a followup series.
>>>
>>> As this is actually quite complicated and needs discussion exactly
>>> what it will look like.
>>
>> Actually that is the easy step I think because quite a lot of drivers already convert the SGT to a DMA addr array outside of the DMA-buf framework anyway.
>>
>> The more complicated one is then starting to come up with more bus types and doing optimization work.
>
> Individually the actual data structure conversion isn't that hard, but there's
> a lot of them and the mapping types allow you to do them incrementally without
> breaking some unconverted importer importing from a converted exporter.
Breaking classic users is actually completely intentional because we most likely still have people abusing the struct page inside the SGT.
And those will need to be converted over to be using pure DMA addresses similar to how MSM has switched over recently.
So the whole idea is when you have classic to classic interface pass along the SGT between exporter and importer. Then move as much as possible drivers over to the new interface and start identifying where and why some drivers have problems.
Regards,
Christian.
>
> That is also why I think adding the types now rather than later is reasonable.
> The rules for when an exporter may change type or placement are what most of the
> previous review discussion has been about, and I didn't want to drop them.
>
> Thanks for looking.
>
> - Alistair
>
>> Christian.
>>
>>>
>>> Jason
>>
Hi Mark,
The current drm-misc tree has a semantic conflict with the broadcom
tree that breaks the build:
drivers/platform/raspberrypi/vc-sm-cma/vc_sm.c:217:3: error: call to undeclared function 'dma_buf_unmap_attachment_unlocked'
drivers/platform/raspberrypi/vc-sm-cma/vc_sm.c:438:8: error: call to undeclared function 'dma_buf_map_attachment_unlocked'
77e0b2d2f4f3 ("treewide: Rename dma_buf_(un)map_attachment() to
dma_buf_(un)map_sgt()") in the drm-misc tree renames the functions, and
6e0a8f1a8347 ("platform/raspberrypi: Add VideoCore shared memory
support") in the broadcom tree adds a new user of the old names. It is
the only caller in next-20261123 that the rename doesn't cover.
The fix for the merge:
diff --git a/drivers/platform/raspberrypi/vc-sm-cma/vc_sm.c b/drivers/platform/raspberrypi/vc-sm-cma/vc_sm.c
--- a/drivers/platform/raspberrypi/vc-sm-cma/vc_sm.c
+++ b/drivers/platform/raspberrypi/vc-sm-cma/vc_sm.c
@@ -214,9 +214,8 @@ static void vc_sm_clean_up_dmabuf(struct vc_sm_buffer *buffer)
{
/* Handle cleaning up imported dmabufs */
if (buffer->sgt) {
- dma_buf_unmap_attachment_unlocked(buffer->attach,
- buffer->sgt,
- DMA_BIDIRECTIONAL);
+ dma_buf_unmap_sgt_unlocked(buffer->attach, buffer->sgt,
+ DMA_BIDIRECTIONAL);
buffer->sgt = NULL;
}
if (buffer->attach) {
@@ -435,7 +434,7 @@ vc_sm_cma_import_dmabuf_internal(struct dma_buf *dma_buf,
goto error;
}
- sgt = dma_buf_map_attachment_unlocked(attach, DMA_BIDIRECTIONAL);
+ sgt = dma_buf_map_sgt_unlocked(attach, DMA_BIDIRECTIONAL);
if (IS_ERR(sgt)) {
ret = PTR_ERR(sgt);
goto error;
@@ -550,8 +549,7 @@ vc_sm_cma_import_dmabuf_internal(struct dma_buf *dma_buf,
free_kernel_id(import.kernel_id);
kfree(buffer);
if (sgt)
- dma_buf_unmap_attachment_unlocked(attach, sgt,
- DMA_BIDIRECTIONAL);
+ dma_buf_unmap_sgt_unlocked(attach, sgt, DMA_BIDIRECTIONAL);
if (attach)
dma_buf_detach(dma_buf, attach);
dma_buf_put(dma_buf);
--
Thanks,
Sasha
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;
> };
> };
>
>
Shared-memory allocator services (e.g., Android's gralloc)
allocate dma-bufs on behalf of client processes in different
cgroups. Today those allocations are charged to the allocator's
own cgroup, leaving client cgroups with no visibility into their
actual memory consumption and defeating resource limits.
This series adds memory cgroup accounting for dma-buf allocations,
building on T.J. Mercier's earlier series [1] and incorporating
feedback from previous versions and LPC discussions [2].
Patch 1 introduces the MEMCG_DMABUF stat and the
obj_cgroup_reserve_dmabuf()/obj_cgroup_cancel_dmabuf() and
mem_cgroup_commit_dmabuf()/mem_cgroup_uncharge_dmabuf() helpers
in mm/memcontrol.c
Patch 2 wires up the charging policy: dma_buf_export() stores
exp_info.objcg and updates the stat, a new charge_fd field in
DMA_HEAP_IOCTL_ALLOC accepts a pidfd or cgroupfd to identify
the target cgroup, and __GFP_ACCOUNT is removed from system-heap
allocations to avoid double counting.
Patch 3 adds DMA_BUF_IOCTL_XFER_CHARGE, a general ioctl on any
dma-buf fd to transfer charges post-allocation. This covers the
double-hop allocator case (SurfaceFlinger -> gralloc -> kernel)
and works for buffers from any exporter that opts in. Charging
is gated by dmabuf->objcg being set at export time, ensuring that
transfer is only applied for buffers backed by system memory where
size reflects actual bytes. If the receiving cgroup lacks capacity
for the charge, the ioctl returns -ENOMEM and the original charge
is preserved.
Patches 4-6 add access control for cross-cgroup charging. Letting
userspace decide who to charge needs to be a privileged operation
because it has material impact on OOM decisions and resource limits.
Charging outside the caller's own memcg subtree requires
CAP_SYS_RESOURCE. On top of that, LSM hooks gate the two charge_fd
flavours: security_current_memcg_charge_task() for the pidfd path
and security_current_memcg_charge_cgroup() for the cgroupfd path.
Both flavours ultimately charge a cgroup, so the SELinux
implementation gates the two with a single cgroup:charge_to
permission. The target type keeps the distinction.
The checks run at target resolution, before the CAP_SYS_RESOURCE
baseline, and cover the caller's own subtree, so policy needs
a rule even for self-charging. The SELinux controls are covered
by new selinux-testsuite tests [4].
Patch 7 adds selftests. The tests and vmtest script were mostly
written by an LLM, closely following the style of the surrounding
tests and the virtme-ng setup. All code has been reviewed and tested.
Known limitations:
- Single-owner model: a dma-buf carries one memcg charge regardless
of how many processes share it. The owner is deterministic, set
at allocation time and moved only by an explicit XFER_CHARGE.
- Only memcg accounting. CMA heaps would additionally need dmem
controller integration.
- If a transfer fails (e.g., the receiving cgroup is at its limit),
the buffer remains charged to the original cgroup. The allocator
(userspace) is expected to handle failures (retry, free, ...).
- The LSM hooks are intentionally narrow. Broader cgroup access
controls (e.g., for the memfd/kexec handover case [2]) could be
a separate effort reusing the same pattern.
- Only system heap sets exp_info.objcg today. Other exporters
(DRM, V4L2, etc.) can opt in by setting it, but are not
covered by this series.
- Related to the above: lazy backing-store allocation is not supported.
This can be extended later, e.g., by adding a flag on the dma-buf
struct for explicitly marking when memory is committed.
Note on TTM/GPU accounting [3]: that effort charges system memory
allocated for GPU buffers per page, with its own stats (gpu_active
and gpu_reclaim). This series charges at the dma-buf level, for
memory with no memory.stat category of its own; exporters whose
memory is already charged by their allocator must not opt in, so
the two do not overlap.
[1] https://lore.kernel.org/cgroups/20230109213809.418135-1-tjmercier@google.co…
[2] https://lpc.events/event/20/contributions/2416/
[3] https://lore.kernel.org/all/20260706052330.1110909-1-airlied@gmail.com/
[4] https://github.com/SELinuxProject/selinux-testsuite/pull/111
Signed-off-by: Albert Esteve <aesteve(a)redhat.com>
---
Changes in v6:
- Unify SELinux perms into cgroup:charge_to
- Drop the current cred argument from the LSM hooks
and rename signatures to security_current_memcg_charge_*
- Add selinux-testsuite coverage for the charge_to controls
- Rebased to v7.4-rc4
- Link to v5: https://patch.msgid.link/20261117-v2_20230123_tjmercier_google_com-v5-0-1ce…
Changes in v5:
- Reject dma-buf reservations above INT_MAX pages to avoid
nr_pages truncation (Sashiko)
- Require CAP_SYS_RESOURCE for charge requests targeting outside
caller's own memcg subtree (Sashiko)
- Hold an obj_cgroup instead of a memcg in the dma-buf struct (Thomas Hellström)
- Restore mem_cgroup_commit_dmabuf() so dma_buf_export() does not
reach into memcg stat internals
- Resolve the cgroup and run the LSM check against the same file
in get_obj_cgroup_from_fd()
- The cgroupfd hook now takes the file and checks the inode SID,
so genfscon-labeled cgroups are covered
- Rename the LSM hooks to security_memcg_charge_{task,cgroup}()
- Replace dma_buf object class in SELinux with process2:charge_to
and cgroup:charge_to
- Link to v4: https://lore.kernel.org/r/20261104-v2_20230123_tjmercier_google_com-v4-0-18…
Changes in v4:
- Remove memcg_account flag; exp_info.memcg != NULL is the opt-in
- Centralize memcg bookkeeping in dma_buf_export()
- Drop mem_cgroup_commit_dmabuf()
- Make mem_cgroup_transfer_charge() generic (not dma-buf specific)
- Add EXPORT_SYMBOL_GPL for reserve/cancel (system_heap is tristate)
- Reorder: move xfer ioctl before LSM patches
- Serialize dmabuf->memcg updates with the dma-buf resv lock instead of cmpxchg
- Link to v3: https://lore.kernel.org/r/20261027-v2_20230123_tjmercier_google_com-v3-0-78…
Changes in v3:
- Split mem_cgroup_charge_dmabuf() into reserve/commit/cancel three-phase API
- Extract mem_cgroup_get_from_fd() helper shared by allocate and xfer paths
- Rename __padding to flags in struct dma_buf_transfer_charge
- Fix UAF race: move mod_memcg_state() before cmpxchg in transfer
- Add memcg_account opt-in flag; xfer returns -EOPNOTSUPP if unset
- Wrap dma_buf_charge_cgroup hook in #ifdef CONFIG_CGROUPS
- Fix TOCTOU in SELinux charge_cgroup xattr length handling
- Link to v2: https://lore.kernel.org/r/20261021-v2_20230123_tjmercier_google_com-v2-0-ef…
Changes in v2:
- Move charge_pid_fd to charge_fd, accepting cgroupfd in addition
to pidfd per Christian König.
- New DMA_BUF_IOCTL_XFER_CHARGE ioctl for post-allocation charge
transfer on any dma-buf, not just dma-heap buffers per Christian König
and T.J. Mercier.
- Moved __GFP_ACCOUNT removal to patch 1 per Barry Song.
- dma-buf charge/uncharge helpers take struct dma_buf * directly
to avoid the need for caller to manage size calculations.
- Added test_xfer_charge_cgroupfd_self to dmabuf-heap selftests.
- Added LSM and SELinux hooks for cgroupfd-based charging.
- Rebased to v7.3-rc7.
- Link to v1: https://lore.kernel.org/r/20260512-v2_20230123_tjmercier_google_com-v1-0-63…
---
Albert Esteve (6):
dma-heap: charge dma-buf memory via explicit memcg
dma-buf: transfer objcg charges via DMA_BUF_IOCTL_XFER_CHARGE
lsm: Add memcg_charge_task LSM hook
lsm: Add memcg_charge_cgroup LSM hook
selinux: Restrict cross-cgroup memcg charging
selftests/dmabuf-heaps: Add dma-buf memcg accounting tests
T.J. Mercier (1):
memcg: Add MEMCG_DMABUF accounting infrastructure
Documentation/admin-guide/cgroup-v2.rst | 6 +
drivers/dma-buf/dma-buf.c | 45 +++
drivers/dma-buf/dma-heap.c | 27 +-
drivers/dma-buf/heaps/cma_heap.c | 3 +-
drivers/dma-buf/heaps/system_heap.c | 19 +-
drivers/tee/tee_heap.c | 3 +-
include/linux/dma-buf.h | 10 +
include/linux/dma-heap.h | 4 +-
include/linux/lsm_hook_defs.h | 2 +
include/linux/memcontrol.h | 56 +++
include/linux/security.h | 12 +
include/uapi/linux/dma-buf.h | 26 ++
include/uapi/linux/dma-heap.h | 7 +
mm/memcontrol.c | 271 +++++++++++++
security/security.c | 31 ++
security/selinux/hooks.c | 19 +
security/selinux/include/classmap.h | 1 +
tools/testing/selftests/cgroup/Makefile | 2 +-
tools/testing/selftests/cgroup/test_memcontrol.c | 447 ++++++++++++++++++++-
tools/testing/selftests/dmabuf-heaps/config | 1 +
tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c | 237 ++++++++++-
tools/testing/selftests/dmabuf-heaps/vmtest.sh | 226 +++++++++++
22 files changed, 1440 insertions(+), 15 deletions(-)
---
base-commit: 8d7fe9885c62fc9a95bf55d9db244d9672a8aadc
change-id: 20260508-v2_20230123_tjmercier_google_com-f44fcfb16530
Best regards,
--
Albert Esteve <aesteve(a)redhat.com>
On Fri, Nov 20, 2026 at 07:59:11PM +0100, Antoni Pokusinski wrote:
> Document the TX and RX DMA request channels featured
> by the OMAP I2C controller.
>
> Signed-off-by: Antoni Pokusinski <apokusinski01(a)gmail.com>
Missing Krzysztof's ack
Attaching to a zero-sized dma-buf does not make sense at all: there is
nothing for an importer to map.
So reject the attach, using WARN_ON() because a zero-sized dma-buf is
an exporter bug that userspace cannot trigger on its own.
Adding the check to dma_buf_export() instead was considered, which
would prevent zero-sized DMA-bufs from being created in the first
place. It is not done here because drivers may be using zero-sized
DMA-bufs for driver internal buffers, and refusing them at creation
time risks regressing those users.
Fixes: d15bd7ee445d ("dma-buf: Introduce dma buffer sharing mechanism")
Signed-off-by: Lizhi Hou <lizhi.hou(a)amd.com>
---
drivers/dma-buf/dma-buf.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c
index 5c6957f8839a..8be7b6023e4a 100644
--- a/drivers/dma-buf/dma-buf.c
+++ b/drivers/dma-buf/dma-buf.c
@@ -1050,6 +1050,9 @@ dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev,
if (WARN_ON(!dmabuf || !dev))
return ERR_PTR(-EINVAL);
+ if (WARN_ON(!dmabuf->size))
+ return ERR_PTR(-EINVAL);
+
attach = kzalloc_obj(*attach);
if (!attach)
return ERR_PTR(-ENOMEM);
--
2.34.1