[PATCH v5 3/8] dma-buf: transfer objcg charges via DMA_BUF_IOCTL_XFER_CHARGE
From: Albert Esteve
Date: Tue Nov 17 2026 - 03:40:24 EST
dma-heap already supports charging a specific cgroup at allocation
time via charge_fd in DMA_HEAP_IOCTL_ALLOC. However, dma-bufs can
originate from any exporter (DRM, V4L2, etc.), and the allocator may
not know the final owner at allocation time.
Add DMA_BUF_IOCTL_XFER_CHARGE to transfer a dma-buf's objcg charge
to a different cgroup after allocation. The target is identified by
charge_fd, which may be a cgroupfd or a pidfd.
The transfer charges the new cgroup before uncharging the old one,
preserving the original charge on failure. Concurrent transfers are
serialized by the dma-buf reservation lock, so lockdep covers the
locking order of the charge path early. The transfer implementation
is generic (obj_cgroup_transfer_charge) and not dma-buf specific, so
it can be reused by other subsystems.
Not all dma-buf exporters back their buffers with reclaimable system
memory. Some represent device registers, VRAM, or carved-out pools
where size is not a meaningful memory cost. The xfer ioctl returns
-EOPNOTSUPP for buffers whose objcg is not set. Exporters opt in by
passing a objcg through dma_buf_export_info at export time. Currently
only the system heap sets it, other exporters can opt in as needed.
Usage example:
int cg_fd = open("/sys/fs/cgroup/clients/app1", O_RDONLY | O_DIRECTORY);
struct dma_buf_transfer_charge xfer = {
.charge_fd = cg_fd,
};
ioctl(dmabuf_fd, DMA_BUF_IOCTL_XFER_CHARGE, &xfer);
/* charge now on app1's cgroup */
close(cg_fd);
Reviewed-by: Christian König <christian.koenig@xxxxxxx>
Signed-off-by: Albert Esteve <aesteve@xxxxxxxxxx>
---
Documentation/admin-guide/cgroup-v2.rst | 2 +-
drivers/dma-buf/dma-buf.c | 39 ++++++++++++++++++++++
include/linux/dma-buf.h | 3 +-
include/linux/memcontrol.h | 13 ++++++++
include/uapi/linux/dma-buf.h | 26 +++++++++++++++
mm/memcontrol.c | 58 +++++++++++++++++++++++++++++++++
6 files changed, 139 insertions(+), 2 deletions(-)
diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-guide/cgroup-v2.rst
index c6bf2e435d74..8d10ff7ac745 100644
--- a/Documentation/admin-guide/cgroup-v2.rst
+++ b/Documentation/admin-guide/cgroup-v2.rst
@@ -1670,7 +1670,7 @@ The following nested keys are defined.
Amount of memory used for exported DMA buffers allocated by or on
behalf of the cgroup, this currently includes system heap
allocations. Stays with the cgroup regardless of how the buffer
- is shared.
+ is shared, unless moved with DMA_BUF_IOCTL_XFER_CHARGE.
workingset_refault_anon
Number of refaults of previously evicted anonymous pages.
diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c
index 88e0802fa05f..5d4b55b67e99 100644
--- a/drivers/dma-buf/dma-buf.c
+++ b/drivers/dma-buf/dma-buf.c
@@ -540,6 +540,42 @@ static long dma_buf_import_sync_file(struct dma_buf *dmabuf,
}
#endif
+/* Transfer a dma-buf's memcg charge; see struct dma_buf_transfer_charge */
+static long dma_buf_xfer_charge(struct dma_buf *dmabuf,
+ const void __user *user_data)
+{
+ struct dma_buf_transfer_charge arg;
+ struct obj_cgroup *objcg;
+ unsigned int nr_pages;
+ int ret = 0;
+
+ if (copy_from_user(&arg, user_data, sizeof(arg)))
+ return -EFAULT;
+
+ if (arg.flags != 0)
+ return -EINVAL;
+
+ objcg = get_obj_cgroup_from_fd(arg.charge_fd);
+ if (IS_ERR(objcg))
+ return PTR_ERR(objcg);
+
+ dma_resv_lock(dmabuf->resv, NULL);
+
+ if (!dmabuf->objcg) {
+ ret = -EOPNOTSUPP;
+ goto out;
+ }
+
+ nr_pages = PAGE_ALIGN(dmabuf->size) >> PAGE_SHIFT;
+ if (!obj_cgroup_transfer_charge(&dmabuf->objcg, objcg, MEMCG_DMABUF, nr_pages))
+ ret = -ENOMEM;
+
+out:
+ dma_resv_unlock(dmabuf->resv);
+ obj_cgroup_put(objcg);
+ return ret;
+}
+
static long dma_buf_ioctl(struct file *file,
unsigned int cmd, unsigned long arg)
{
@@ -590,6 +626,9 @@ static long dma_buf_ioctl(struct file *file,
return dma_buf_import_sync_file(dmabuf, (const void __user *)arg);
#endif
+ case DMA_BUF_IOCTL_XFER_CHARGE:
+ return dma_buf_xfer_charge(dmabuf, (const void __user *)arg);
+
default:
return -ENOTTY;
}
diff --git a/include/linux/dma-buf.h b/include/linux/dma-buf.h
index ff4678d00d8c..916d73ae6a51 100644
--- a/include/linux/dma-buf.h
+++ b/include/linux/dma-buf.h
@@ -508,7 +508,8 @@ struct dma_buf_attachment {
* @objcg: object cgroup to charge for this buffer's memory. Set only for
* buffers backed by system memory, not accounted elsewhere, where
* @size reflects the actual allocation in bytes. The buffer takes
- * over the caller's reference.
+ * over the caller's reference. Enables charge transfer via
+ * DMA_BUF_IOCTL_XFER_CHARGE.
*
* This structure holds the information required to export the buffer. Used
* with dma_buf_export() only.
diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
index 8e016d9b91bc..37c81be349b8 100644
--- a/include/linux/memcontrol.h
+++ b/include/linux/memcontrol.h
@@ -1827,6 +1827,11 @@ static inline struct obj_cgroup *get_obj_cgroup_from_current(void)
int obj_cgroup_charge(struct obj_cgroup *objcg, gfp_t gfp, size_t size);
void obj_cgroup_uncharge(struct obj_cgroup *objcg, size_t size);
+bool obj_cgroup_transfer_charge(struct obj_cgroup **ptr_objcg,
+ struct obj_cgroup *new_objcg,
+ enum memcg_stat_item item,
+ unsigned int nr_pages);
+
extern struct static_key_false memcg_bpf_enabled_key;
static inline bool memcg_bpf_enabled(void)
{
@@ -1930,6 +1935,14 @@ static inline struct obj_cgroup *get_obj_cgroup_from_current(void)
return NULL;
}
+static inline bool obj_cgroup_transfer_charge(struct obj_cgroup **ptr_objcg,
+ struct obj_cgroup *new_objcg,
+ enum memcg_stat_item item,
+ unsigned int nr_pages)
+{
+ return true;
+}
+
static inline bool memcg_bpf_enabled(void)
{
return false;
diff --git a/include/uapi/linux/dma-buf.h b/include/uapi/linux/dma-buf.h
index e827c9d20c5d..0aaf2e7d593b 100644
--- a/include/uapi/linux/dma-buf.h
+++ b/include/uapi/linux/dma-buf.h
@@ -168,6 +168,31 @@ struct dma_buf_import_sync_file {
__s32 fd;
};
+/**
+ * struct dma_buf_transfer_charge - Transfer a dma-buf's memcg charge
+ *
+ * Any holder of a dma-buf file descriptor can perform a
+ * DMA_BUF_IOCTL_XFER_CHARGE to attribute the buffer's memory
+ * cost to a specific cgroup, or move it from one cgroup to
+ * another. The ioctl charges the new cgroup before uncharging
+ * the old one atomically. On failure the original charge is
+ * preserved. If the buffer has no existing memcg charge, the
+ * ioctl returns -EOPNOTSUPP.
+ */
+struct dma_buf_transfer_charge {
+ /** @flags: Reserved, must be zero */
+ __u32 flags; /* unused for now */
+ /**
+ * @charge_fd: File descriptor identifying the target cgroup.
+ *
+ * May be either a pidfd, in which case the charge moves to the
+ * memory cgroup of the referenced process, or a cgroupfd obtained
+ * by opening a cgroup v2 directory, in which case the charge moves
+ * directly to that cgroup's memory controller.
+ */
+ __u32 charge_fd;
+};
+
#define DMA_BUF_BASE 'b'
#define DMA_BUF_IOCTL_SYNC _IOW(DMA_BUF_BASE, 0, struct dma_buf_sync)
@@ -179,5 +204,6 @@ struct dma_buf_import_sync_file {
#define DMA_BUF_SET_NAME_B _IOW(DMA_BUF_BASE, 1, __u64)
#define DMA_BUF_IOCTL_EXPORT_SYNC_FILE _IOWR(DMA_BUF_BASE, 2, struct dma_buf_export_sync_file)
#define DMA_BUF_IOCTL_IMPORT_SYNC_FILE _IOW(DMA_BUF_BASE, 3, struct dma_buf_import_sync_file)
+#define DMA_BUF_IOCTL_XFER_CHARGE _IOW(DMA_BUF_BASE, 4, struct dma_buf_transfer_charge)
#endif
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index adc0abcf80fa..03cd90d5e623 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -3251,6 +3251,64 @@ struct obj_cgroup *get_obj_cgroup_from_fd(int fd)
return objcg;
}
+/**
+ * obj_cgroup_transfer_charge - move a charge between object cgroups
+ * @ptr_objcg: pointer to the object cgroup to transfer from
+ * @new_objcg: target object cgroup; caller must hold a reference
+ * @item: memcg stat counter to adjust; must not be a node stat
+ * @nr_pages: number of pages to transfer
+ *
+ * Transfers @nr_pages of charge from *@ptr_objcg to @new_objcg and, on
+ * success, stores @new_objcg in *@ptr_objcg. Does nothing if both resolve
+ * to the same memcg; *@ptr_objcg is left untouched in that case.
+ *
+ * Updates to *@ptr_objcg must be serialized by the caller.
+ * Returns %true on success, %false if the charge against @new_objcg
+ * failed.
+ */
+bool obj_cgroup_transfer_charge(struct obj_cgroup **ptr_objcg,
+ struct obj_cgroup *new_objcg,
+ enum memcg_stat_item item,
+ unsigned int nr_pages)
+{
+ struct obj_cgroup *old_objcg = *ptr_objcg;
+ struct mem_cgroup *old_memcg = NULL, *new_memcg;
+ bool ret = true;
+
+ if (!new_objcg || mem_cgroup_disabled())
+ return true;
+
+ new_memcg = get_mem_cgroup_from_objcg(new_objcg);
+ if (old_objcg)
+ old_memcg = get_mem_cgroup_from_objcg(old_objcg);
+
+ if (old_memcg == new_memcg)
+ goto out;
+
+ if (try_charge(new_memcg, GFP_KERNEL, nr_pages)) {
+ ret = false;
+ goto out;
+ }
+
+ mod_memcg_state(new_memcg, item, nr_pages);
+
+ if (old_objcg) {
+ /* Uncharge old cgroup */
+ mod_memcg_state(old_memcg, item, -nr_pages);
+ if (!mem_cgroup_is_root(old_memcg))
+ refill_stock(old_memcg, nr_pages);
+ }
+
+ obj_cgroup_get(new_objcg);
+ *ptr_objcg = new_objcg;
+ obj_cgroup_put(old_objcg);
+
+out:
+ mem_cgroup_put(new_memcg);
+ mem_cgroup_put(old_memcg);
+ return ret;
+}
+
#ifdef CONFIG_MEMCG_NMI_SAFETY_REQUIRES_ATOMIC
static inline void account_kmem_nmi_safe(struct mem_cgroup *memcg, int val)
{
--
2.55.0