memcg: account the remaining uncharged kernel allocations
Subsystems (16)
cover memcg: account the remaining uncharged kernel allocations
From: Tao Cui <[email protected]>
Kernel memory that scales with user controlled input is supposed to be charged to the memcg of the task that induces it, so that memory.max contains the damage and the accounting is honest. Sweeping the interfaces that let unprivileged or delegated users drive kernel allocations turned up seven more places where the charge is missing. Every fix is one flag; the interesting part is who can pull the lever and how much a pull buys.
An open sg fd reserves up to max_sectors worth of buffer, SG_MAX_QUEUE
(16) commands can be in flight per fd, and the fd count is bounded only by RLIMIT_NOFILE. udmabuf metadata costs about 4MB of SUnreclaim per GB of buffer with size_limit_mb defaulting to INT_MAX. vhost holds roughly 150KB of resident kernel memory per SET_OWNER, and /dev/vhost-net is handed to containers by device plugins. A ftrace instance allocates two full sets of per-cpu ring buffers the moment it is created, about 1.375MB per cpu per set, gated by tracefs permissions alone. iommufd's hw_queue contiguity check allocates an array sized by a u64 from userspace, frees it when the check fails, and can be repeated in a tight loop. io_uring still leaves the ring context, the SQPOLL thread data, io-wq, the alloc cache arrays, the kbuf iovecs and the registered eventfds uncharged. The rsrc and tctx surfaces are already charged by commit 075d5837bafc ("io_uring/rsrc: charge buffer arrays and nodes to memcg") and commit 0b34e7b20a48 ("io_uring: charge tctx nodes and personalities"); patches 6-7 pick up what those did not.
Verified on next-20261104 under qemu, same config with and without the series, everything builtin: eight sg fds with 1MB reserved buffers charge 8MB with the series and nothing without; resizing the global trace buffer to 16MB per cpu charges 67MB against 0 before; four 512MB udmabuf buffers charge around 8MB of metadata; sixteen vhost SET_OWNER calls charge 4MB; 1000 io_uring rings charge 7.6MB on top of what the in-tree charging already accounts. All slab allocations in the series are served by kmalloc caches, so they keep charging under the SLAB_MAY_ACCOUNT opt-in rule queued in slab/for-next; the tracing patch accounts page allocations, which are not affected by it.
Two in-flight series overlap but do not collide. The dma-heap explicit memcg charging series moves dma-buf data page charging to an explicit path and removes __GFP_ACCOUNT from the system heap, while patch 2 here accounts udmabuf metadata only, which that series does not see; if it lands first, the system heap reference in patch 2 wants dropping. The io_uring patches sit on top of the charging Mason and Jens already merged, and cite those commits as the in-tree predecessors.
I am sending these in one thread so the class is visible in one place. If any of you would rather take your piece through your own tree, an ack is all I need and I will split the series.
Tao Cui (7):
scsi: sg and st: account the command buffer memory to memcg dma-buf/udmabuf: account the page array metadata to memcg
vhost: account the per-device kernel allocations to memcg
tracing: account ring buffer pages and trace_array metadata to memcg
iommufd: account the hw_queue page array to memcg
io_uring: account the ring context and SQPOLL thread data to memcg
io_uring: account the remaining per-object allocations to memcg
drivers/dma-buf/udmabuf.c | 7 ++++--- drivers/iommu/iommufd/viommu.c | 3 ++- drivers/scsi/sg.c | 23 ++++++++++++++++++++--- drivers/scsi/st.c | 7 ++++--- drivers/vhost/net.c | 14 ++++++++------ drivers/vhost/vhost.c | 16 ++++++++++------ drivers/vhost/vsock.c | 5 +++-- io_uring/alloc_cache.c | 3 ++- io_uring/eventfd.c | 2 +- io_uring/io-wq.c | 4 ++-- io_uring/io_uring.c | 2 +- io_uring/kbuf.c | 2 +- io_uring/rsrc.c | 3 ++- io_uring/sqpoll.c | 2 +- io_uring/tctx.c | 4 ++-- kernel/trace/ring_buffer.c | 8 +++++++- kernel/trace/trace.c | 6 +++--- 17 files changed, 73 insertions(+), 38 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/7 scsi: sg and st: account the command buffer memory to memcg
From: Tao Cui <[email protected]>
The sg data buffers are unaccounted: the per-fd reserved buffer, the scatter-gather page array, and the order-0..N pages behind it, plus the per-device tape buffer in st. All of it is user controllable. The reserved size is capped only by the device max_sectors, SG_MAX_QUEUE
(16) commands can be in flight per fd, and the fd count is bounded by RLIMIT_NOFILE. Nothing is charged to the memcg of the process driving it. The September 2026 fixes in sg_build_indirect reshaped the element sizing and left the accounting alone.
Mark these allocations __GFP_ACCOUNT so the driving process pays for the memory it induces.
--- drivers/scsi/sg.c | 23 ++++++++++++++++++++--- drivers/scsi/st.c | 7 ++++--- 2 files changed, 24 insertions(+), 6 deletions(-)
drivers/scsi/sg.c +20 -3
@@ -911,6 +911,19 @@ static int put_compat_request_table(struct compat_sg_req_info __user *o, }#endif +/* +* The sg passthrough of the blktrace ioctls has the same exposure as the +* block device path: holding the node is the only requirement today, and +* a created trace outlives the fd. Must match the gate in +* blk_trace_ioctl(). +*/ +static bool sg_is_blktrace_cmd (unsigned int cmd) +{ +return cmd ==BLKTRACESETUP || cmd ==BLKTRACESETUP2 || + cmd ==BLKTRACESTART || cmd ==BLKTRACESTOP || + cmd ==BLKTRACETEARDOWN ; +} +static long sg_ioctl_common (struct file *filp,Sg_device *sdp,Sg_fd *sfp,unsigned int cmd_in,void __user *p)
@@ -920,6 +933,9 @@ sg_ioctl_common(struct file *filp, Sg_device *sdp, Sg_fd *sfp,Sg_request *srp;unsigned long iflags; +if (sg_is_blktrace_cmd (cmd_in) && !capable (CAP_SYS_ADMIN )) +return -EPERM ; +SCSI_LOG_TIMEOUT (3 ,sg_printk (KERN_INFO , sdp,"sg_ioctl: cmd=0x%x\n" , (int ) cmd_in)); read_only = (O_RDWR != (filp->f_flags &O_ACCMODE ));
@@ -1829,7 +1845,7 @@ static int sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize) {int sg_bufflen = tablesize *sizeof (struct page *); -gfp_t gfp_flags =GFP_ATOMIC | __GFP_NOWARN; +gfp_t gfp_flags =GFP_ATOMIC | __GFP_NOWARN | __GFP_ACCOUNT; schp->pages =kzalloc (sg_bufflen, gfp_flags);if (!schp->pages)
@@ -1844,7 +1860,8 @@ sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)int ret_sz =0 , i, k, rem_sz, num, mx_sc_elems;int sg_tablesize = sfp->parentdp->sg_tablesize;int blk_size = buff_size, order; -gfp_t gfp_mask =GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN | __GFP_ZERO; +gfp_t gfp_mask =GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN | + __GFP_ACCOUNT | __GFP_ZERO;if (blk_size <0 )return -EFAULT ;
@@ -2134,7 +2151,7 @@ sg_add_sfp(Sg_device * sdp)unsigned long iflags;int bufflen; - sfp =kzalloc_obj (*sfp,GFP_ATOMIC | __GFP_NOWARN); + sfp =kzalloc_obj (*sfp,GFP_ATOMIC | __GFP_NOWARN | __GFP_ACCOUNT);if (!sfp)return ERR_PTR (-ENOMEM );
drivers/scsi/st.c +4 -3
@@ -4074,7 +4074,7 @@ static struct st_buffer *new_tape_buffer(int max_sg) {struct st_buffer *tb; - tb = kzalloc_obj(struct st_buffer); + tb = kzalloc_obj(struct st_buffer, GFP_KERNEL_ACCOUNT);if (!tb) {printk (KERN_NOTICE "st: Can't allocate new tape buffer.\n" );return NULL ;
@@ -4083,7 +4083,8 @@ static struct st_buffer *new_tape_buffer(int max_sg) tb->use_sg = max_sg; tb->buffer_size =0 ; - tb->reserved_pages = kzalloc_objs(struct page *, max_sg); + tb->reserved_pages = kzalloc_objs(struct page *, max_sg, +GFP_KERNEL_ACCOUNT );if (!tb->reserved_pages) {kfree (tb);return NULL ;
@@ -4109,7 +4110,7 @@ static int enlarge_buffer(struct st_buffer * STbuffer, int new_size) max_segs = STbuffer->use_sg; - priority =GFP_KERNEL | __GFP_NOWARN; + priority =GFP_KERNEL | __GFP_NOWARN | __GFP_ACCOUNT;if (STbuffer->cleared) priority |= __GFP_ZERO;
Signature · 1 line
2.43.0
PATCH 2/7 dma-buf/udmabuf: account the page array metadata to memcg
In reply to Tao CuiFrom: Tao Cui <[email protected]>
udmabuf pins the pages of a memfd and hands them out as a dma-buf. The data pages keep the accounting of the memfd they were instantiated in, but the bookkeeping around them does not: the page pointer array and the pinned_folios array, both sized by the buffer page count, plus the transient folios array sized by the memfd folio count, are plain GFP_KERNEL. A 1GB buffer costs about 4MB of uncharged SUnreclaim memory, and size_limit_mb defaults to INT_MAX.
Charge the three kvmalloc allocations so the creator pays for the metadata it induces. The system heap in the same subsystem already accounts its buffer allocation; the data pages here stay with the memfd.
--- drivers/dma-buf/udmabuf.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/dma-buf/udmabuf.c +4 -3
@@ -190,11 +190,12 @@ static void unpin_all_folios(struct udmabuf *ubuf) static __always_inline int init_udmabuf(struct udmabuf *ubuf, pgoff_t pgcnt) { - ubuf->pages =kvmalloc_objs (*ubuf->pages, pgcnt); + ubuf->pages =kvmalloc_objs (*ubuf->pages, pgcnt,GFP_KERNEL_ACCOUNT );if (!ubuf->pages)return -ENOMEM ; - ubuf->pinned_folios =kvmalloc_objs (*ubuf->pinned_folios, pgcnt); + ubuf->pinned_folios =kvmalloc_objs (*ubuf->pinned_folios, pgcnt, +GFP_KERNEL_ACCOUNT );if (!ubuf->pinned_folios)return -ENOMEM ;
@@ -387,7 +388,7 @@ static long udmabuf_create(struct miscdevice *device,if (ret) goto err; - folios =kvmalloc_objs (*folios, max_nr_folios); + folios =kvmalloc_objs (*folios, max_nr_folios,GFP_KERNEL_ACCOUNT );if (!folios) { ret = -ENOMEM ; goto err;
Signature · 1 line
2.43.0
PATCH 3/7 vhost: account the per-device kernel allocations to memcg
In reply to Tao CuiFrom: Tao Cui <[email protected]>
Everything except the worker struct is allocated with plain GFP_KERNEL:
the per-vq iovec and log arrays at VHOST_SET_OWNER, the memory table at VHOST_SET_MEM_TABLE whose size follows the user supplied region count, the pending message nodes, and the vhost_net/vhost_vsock device structs with their fallback to vmalloc. The worker has been accounted since the vhost_task conversion.
A container that can open /dev/vhost-net or /dev/vhost-vsock, which device plugins hand out with passthrough, holds roughly 150KB of resident kernel memory per device after SET_OWNER. Charge the rest the same way the worker already is.
--- drivers/vhost/net.c | 14 ++++++++------ drivers/vhost/vhost.c | 16 ++++++++++------ drivers/vhost/vsock.c | 5 +++-- 3 files changed, 21 insertions(+), 14 deletions(-)
drivers/vhost/net.c +8 -6
@@ -249,7 +249,7 @@ vhost_net_ubuf_alloc(struct vhost_virtqueue *vq, bool zcopy)/* No zero copy backend? Nothing to count. */ if (!zcopy)return NULL ; - ubufs =kmalloc_obj (*ubufs); + ubufs =kmalloc_obj (*ubufs,GFP_KERNEL_ACCOUNT );if (!ubufs)return ERR_PTR (-ENOMEM );atomic_set (&ubufs->refcount,1 );
@@ -302,7 +302,8 @@ static int vhost_net_set_ubuf_info(struct vhost_net *n)if (!zcopy)continue ; n->vqs[i].ubuf_info = -kmalloc_objs (*n->vqs[i].ubuf_info,UIO_MAXIOV ); +kmalloc_objs (*n->vqs[i].ubuf_info,UIO_MAXIOV , +GFP_KERNEL_ACCOUNT );if (!n->vqs[i].ubuf_info) goto err; }
@@ -1344,17 +1345,18 @@ static int vhost_net_open(struct inode *inode, struct file *f)struct xdp_buff *xdp;int i; - n =kvmalloc_obj (*n,GFP_KERNEL | __GFP_RETRY_MAYFAIL); + n =kvmalloc_obj (*n,GFP_KERNEL | __GFP_RETRY_MAYFAIL | + __GFP_ACCOUNT);if (!n)return -ENOMEM ; - vqs =kmalloc_objs (*vqs,VHOST_NET_VQ_MAX ); + vqs =kmalloc_objs (*vqs,VHOST_NET_VQ_MAX ,GFP_KERNEL_ACCOUNT );if (!vqs) {kvfree (n);return -ENOMEM ; } queue =kmalloc_array (VHOST_NET_BATCH ,sizeof (void *), -GFP_KERNEL ); +GFP_KERNEL_ACCOUNT );if (!queue) {kfree (vqs);kvfree (n);
@@ -1362,7 +1364,7 @@ static int vhost_net_open(struct inode *inode, struct file *f) } n->vqs[VHOST_NET_VQ_RX ].rxq.queue = queue; - xdp =kmalloc_objs (*xdp,VHOST_NET_BATCH ); + xdp =kmalloc_objs (*xdp,VHOST_NET_BATCH ,GFP_KERNEL_ACCOUNT );if (!xdp) {kfree (vqs);kvfree (n);
drivers/vhost/vhost.c +10 -6
@@ -526,11 +526,14 @@ static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)for (i =0 ; i < dev->nvqs; ++i) { vq = dev->vqs[i]; - vq->indirect =kmalloc_objs (*vq->indirect,UIO_MAXIOV ); - vq->log =kmalloc_objs (*vq->log, dev->iov_limit); - vq->heads =kmalloc_objs (*vq->heads, dev->iov_limit); + vq->indirect =kmalloc_objs (*vq->indirect,UIO_MAXIOV , +GFP_KERNEL_ACCOUNT ); + vq->log =kmalloc_objs (*vq->log, dev->iov_limit, +GFP_KERNEL_ACCOUNT ); + vq->heads =kmalloc_objs (*vq->heads, dev->iov_limit, +GFP_KERNEL_ACCOUNT ); vq->nheads =kmalloc_array (dev->iov_limit,sizeof (*vq->nheads), -GFP_KERNEL ); +GFP_KERNEL_ACCOUNT );if (!vq->indirect || !vq->log || !vq->heads || !vq->nheads) goto err_nomem; }
@@ -2053,7 +2056,8 @@ static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)return -E2BIG ;if (max_iotlb_entries <=0 )return -EINVAL ; - newmem =kvzalloc_flex (*newmem, regions, mem.nregions); + newmem =kvzalloc_flex (*newmem, regions, mem.nregions, +GFP_KERNEL_ACCOUNT );if (!newmem)return -ENOMEM ;
@@ -3405,7 +3409,7 @@ EXPORT_SYMBOL_GPL(vhost_disable_notify);struct vhost_msg_node *vhost_new_msg (struct vhost_virtqueue *vq,int type) {/* Make sure all padding within the structure is initialized. */ -struct vhost_msg_node *node =kzalloc_obj (*node); +struct vhost_msg_node *node =kzalloc_obj (*node,GFP_KERNEL_ACCOUNT );if (!node)return NULL ;
drivers/vhost/vsock.c +3 -2
@@ -721,11 +721,12 @@ static int vhost_vsock_dev_open(struct inode *inode, struct file *file)/* This struct is large and allocation could fail, fall back to vmalloc * if there is no other way. */ - vsock =kvmalloc_obj (*vsock,GFP_KERNEL | __GFP_RETRY_MAYFAIL); + vsock =kvmalloc_obj (*vsock,GFP_KERNEL | __GFP_RETRY_MAYFAIL | + __GFP_ACCOUNT);if (!vsock)return -ENOMEM ; - vqs =kmalloc_objs (*vqs,ARRAY_SIZE (vsock->vqs)); + vqs =kmalloc_objs (*vqs,ARRAY_SIZE (vsock->vqs),GFP_KERNEL_ACCOUNT );if (!vqs) { ret = -ENOMEM ; goto out;
Signature · 1 line
2.43.0
PATCH 4/7 tracing: account ring buffer pages and trace_array metadata to memcg
In reply to Tao CuiFrom: Tao Cui <[email protected]>
Creating an instance immediately allocates two full sets of per-cpu ring buffers, array buffer plus snapshot, about 1.375MB per cpu per set on the default config, plus the trace_array and its name. None of it is charged: neither trace.c nor ring_buffer.c passes __GFP_ACCOUNT, while user_events in the same directory accounts everything it allocates.
Instance creation is currently gated by tracefs file permissions alone, so a
rootful container with the host tracefs bound in can create instances until the host runs out of memory. Charge the ring buffer pages and the trace_array metadata to the creating cgroup.
--- kernel/trace/ring_buffer.c | 8 +++++++- kernel/trace/trace.c | 6 +++--- 2 files changed, 10 insertions(+), 4 deletions(-)
kernel/trace/ring_buffer.c +7 -1
@@ -441,7 +441,13 @@ static struct buffer_data_page *alloc_cpu_data(int cpu, int order)* gracefully without invoking oom-killer and the system is not * destabilized. */ - mflags =GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_COMP | __GFP_ZERO; +/* +* The subbuffers are charged to the memcg of whoever caused the +* allocation: the creator of a trace instance, or root at boot for +* the global trace. +*/ + mflags =GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_COMP | __GFP_ZERO | + __GFP_ACCOUNT; page =alloc_pages_node (cpu_to_node (cpu), mflags, order);if (!page)
kernel/trace/trace.c +3 -3
@@ -8693,11 +8693,11 @@ trace_array_create_systems(const char *name, const char *systems,int ret; ret = -ENOMEM ; - tr =kzalloc_obj (*tr); + tr =kzalloc_obj (*tr,GFP_KERNEL_ACCOUNT );if (!tr)return ERR_PTR (ret); - tr->name =kstrdup (name,GFP_KERNEL ); + tr->name =kstrdup (name,GFP_KERNEL_ACCOUNT );if (!tr->name) goto out_free_tr;
@@ -8708,7 +8708,7 @@ trace_array_create_systems(const char *name, const char *systems, goto out_free_tr;if (systems) { - tr->system_names =kstrdup_const (systems,GFP_KERNEL ); + tr->system_names =kstrdup_const (systems,GFP_KERNEL_ACCOUNT );if (!tr->system_names) goto out_free_tr; }
Signature · 1 line
2.43.0
PATCH 5/7 iommufd: account the hw_queue page array to memcg
In reply to Tao CuiFrom: Tao Cui <[email protected]>
IOMMUFD_CMD_HW_QUEUE_ALLOC pins up to max_npages pages just to check that a user supplied queue region is physically contiguous, and the page array for that check is plain GFP_KERNEL. The size comes from cmd->length, a u64 that is only overflow checked, and the array is freed again as soon as the contiguity check fails, so the allocation can be repeated concurrently: a 1GB region costs about 2MB of uncharged memory per attempt.
Every other iommufd allocation that scales with user input is already charged, and the pinned pages charge the mapper at map time. Charge this array like the rest.
--- drivers/iommu/iommufd/viommu.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iommu/iommufd/viommu.c +2 -1
@@ -330,7 +330,8 @@ iommufd_hw_queue_alloc_phys(struct iommu_hw_queue_alloc *cmd,* Use kvcalloc() to avoid memory fragmentation for a large page array. * Set __GFP_NOWARN to avoid syzkaller blowups */ - pages =kvzalloc_objs (*pages, max_npages,GFP_KERNEL | __GFP_NOWARN); + pages =kvzalloc_objs (*pages, max_npages, +GFP_KERNEL | __GFP_NOWARN | __GFP_ACCOUNT);if (!pages)return ERR_PTR (-ENOMEM );
Signature · 1 line
2.43.0
PATCH 6/7 io_uring: account the ring context and SQPOLL thread data to memcg
In reply to Tao CuiFrom: Tao Cui <[email protected]>
The io_ring_ctx and the SQPOLL io_sqd are the last per-ring allocations made with plain GFP_KERNEL. The larger surfaces around them are already charged: io_mapped_ubuf and io_rsrc_node since commit 075d5837bafc ("io_uring/rsrc: charge buffer arrays and nodes to memcg"), the tctx node and its XArrays since commit 0b34e7b20a48 ("io_uring:
charge tctx nodes and personalities").
Charge the context and the SQPOLL thread data the same way.
--- io_uring/io_uring.c | 2 +- io_uring/rsrc.c | 3 ++- io_uring/sqpoll.c | 2 +- io_uring/tctx.c | 4 ++-- 4 files changed, 6 insertions(+), 5 deletions(-)
io_uring/io_uring.c +1 -1
@@ -229,7 +229,7 @@ static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)int hash_bits;bool ret; - ctx =kzalloc_obj (*ctx); + ctx =kzalloc_obj (*ctx,GFP_KERNEL_ACCOUNT );if (!ctx)return NULL ;
io_uring/rsrc.c +2 -1
@@ -825,7 +825,8 @@ static bool io_coalesce_buffer(struct page ***pages, int *nr_pages,unsigned i, j;/* Store head pages only*/ - new_array = kvmalloc_objs(struct page *, nr_folios); + new_array = kvmalloc_objs(struct page *, nr_folios, +GFP_KERNEL_ACCOUNT );if (!new_array)return false;
io_uring/sqpoll.c +1 -1
@@ -172,7 +172,7 @@ static struct io_sq_data *io_get_sq_data(struct io_uring_params *p,return sqd; } - sqd =kzalloc_obj (*sqd); + sqd =kzalloc_obj (*sqd,GFP_KERNEL_ACCOUNT );if (!sqd)return ERR_PTR (-ENOMEM );
io_uring/tctx.c +2 -2
@@ -23,7 +23,7 @@ static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx,mutex_lock (&ctx->uring_lock); hash = ctx->hash_map;if (!hash) { - hash =kzalloc_obj (*hash); + hash =kzalloc_obj (*hash,GFP_KERNEL_ACCOUNT );if (!hash) {mutex_unlock (&ctx->uring_lock);return ERR_PTR (-ENOMEM );
@@ -85,7 +85,7 @@ __cold struct io_uring_task *io_uring_alloc_task_context(struct task_struct *tasstruct io_uring_task *tctx;int ret; - tctx =kzalloc_obj (*tctx); + tctx =kzalloc_obj (*tctx,GFP_KERNEL_ACCOUNT );if (unlikely (!tctx))return ERR_PTR (-ENOMEM );
Signature · 1 line
2.43.0
PATCH 7/7 io_uring: account the remaining per-object allocations to memcg
In reply to Tao CuiFrom: Tao Cui <[email protected]>
A handful of io_uring allocations still scale with user controlled counts on plain GFP_KERNEL: the per-ring io-wq and its workers, the alloc cache entry arrays, the provided buffer peek and import iovec arrays, and the registered eventfd contexts. The tctx side is covered by commit 0b34e7b20a48 ("io_uring: charge tctx nodes and personalities").
Charge what is left.
--- io_uring/alloc_cache.c | 3 ++- io_uring/eventfd.c | 2 +- io_uring/io-wq.c | 4 ++-- io_uring/kbuf.c | 2 +- 4 files changed, 6 insertions(+), 5 deletions(-)
io_uring/alloc_cache.c +2 -1
@@ -22,7 +22,8 @@ bool io_alloc_cache_init(struct io_alloc_cache *cache, unsigned max_nr, unsigned int size, unsigned int init_bytes) { - cache->entries = kvmalloc_array(max_nr, sizeof(void *), GFP_KERNEL); + cache->entries = kvmalloc_array(max_nr, sizeof(void *), +GFP_KERNEL_ACCOUNT );if (!cache->entries)return true;
io_uring/eventfd.c +1 -1
@@ -132,7 +132,7 @@ int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg,if (copy_from_user (&fd, fds,sizeof (*fds)))return -EFAULT ; - ev_fd =kmalloc_obj (*ev_fd); + ev_fd =kmalloc_obj (*ev_fd,GFP_KERNEL_ACCOUNT );if (!ev_fd)return -ENOMEM ;
io_uring/io-wq.c +2 -2
@@ -981,7 +981,7 @@ static bool create_io_worker(struct io_wq *wq, struct io_wq_acct *acct)__set_current_state (TASK_RUNNING ); - worker =kzalloc_obj (*worker); + worker =kzalloc_obj (*worker,GFP_KERNEL_ACCOUNT );if (!worker) { fail:atomic_dec (&acct->nr_running);
@@ -1358,7 +1358,7 @@ struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)if (WARN_ON_ONCE (!bounded))return ERR_PTR (-EINVAL ); - wq = kzalloc_obj(struct io_wq); + wq = kzalloc_obj(struct io_wq, GFP_KERNEL_ACCOUNT);if (!wq)return ERR_PTR (-ENOMEM );
io_uring/kbuf.c +1 -1
@@ -288,7 +288,7 @@ static int io_ring_buffers_peek(struct io_kiocb *req, struct buf_sel_arg *arg,* a speculative peek operation. */ if (arg->mode &KBUF_MODE_EXPAND && nr_avail > nr_iovs && arg->max_len) { - iov = kmalloc_objs(struct iovec, nr_avail); + iov = kmalloc_objs(struct iovec, nr_avail, GFP_KERNEL_ACCOUNT);if (unlikely (!iov))return -ENOMEM ; arg->iovs = iov;
Signature · 1 line
2.43.0
Christian König
maintainerRe: [PATCH 2/7] dma-buf/udmabuf: account the page array metadata to memcg
In reply to Tao CuiQuoted 10 lines · Tao Cui
From: Tao Cui <[email protected]>
udmabuf pins the pages of a memfd and hands them out as a dma-buf. The data pages keep the accounting of the memfd they were instantiated in, but the bookkeeping around them does not: the page pointer array and the
pinned_foliosarray, both sized by the buffer page count, plus the transient folios array sized by the memfd folio count, are plainGFP_KERNEL. A 1GB buffer costs about 4MB of uncharged SUnreclaim memory, andsize_limit_mbdefaults toINT_MAX.
Quoted 4 lines
Charge the three kvmalloc allocations so the creator pays for the metadata it induces. The system heap in the same subsystem already accounts its buffer allocation; the data pages here stay with the memfd.
T.J. needs to take a look here, but I fear that this might break uAPI.
A lot of the DMA-buf resources are intentionally not charged against memcg.
Regards,
Christian.
Quoted 37 lines
Signed-off-by Tao Cui <[email protected]>--- drivers/dma-buf/udmabuf.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)drivers/dma-buf/udmabuf.c +4
-4@@ -190,11 +190,12 @@ static void unpin_all_folios(struct udmabuf *ubuf) static __always_inline int init_udmabuf(struct udmabuf *ubuf, pgoff_t pgcnt) { - ubuf->pages =kvmalloc_objs (*ubuf->pages, pgcnt); + ubuf->pages =kvmalloc_objs (*ubuf->pages, pgcnt,GFP_KERNEL_ACCOUNT );if (!ubuf->pages)return -ENOMEM ; - ubuf->pinned_folios =kvmalloc_objs (*ubuf->pinned_folios, pgcnt); + ubuf->pinned_folios =kvmalloc_objs (*ubuf->pinned_folios, pgcnt, +GFP_KERNEL_ACCOUNT );if (!ubuf->pinned_folios)return -ENOMEM ;@@ -387,7 +388,7 @@ static long udmabuf_create(struct miscdevice *device,if (ret) goto err; - folios =kvmalloc_objs (*folios, max_nr_folios); + folios =kvmalloc_objs (*folios, max_nr_folios,GFP_KERNEL_ACCOUNT );if (!folios) { ret = -ENOMEM ; goto err; --2.43.0
T.J. Mercier
Reviewed-byRe: [PATCH 2/7] dma-buf/udmabuf: account the page array metadata to memcg
Quoted 12 lines · Christian König
Quoted 10 lines · Tao Cui
From: Tao Cui <[email protected]>
udmabuf pins the pages of a memfd and hands them out as a dma-buf. The data pages keep the accounting of the memfd they were instantiated in, but the bookkeeping around them does not: the page pointer array and the
pinned_foliosarray, both sized by the buffer page count, plus the transient folios array sized by the memfd folio count, are plainGFP_KERNEL. A 1GB buffer costs about 4MB of uncharged SUnreclaim memory, andsize_limit_mbdefaults toINT_MAX.
Quoted 3 lines
Quoted 3 lines
Charge the three kvmalloc allocations so the creator pays for the metadata it induces. The system heap in the same subsystem already accounts its buffer allocation;
Only if the mem_accounting param is set to true. We (Android) currently depend on it being disabled because system heap buffers don't have a single owner for their entire lifetime (ownership often changes immediately after they are allocated), and the kernel doesn't have the ability to switch who is charged for the buffer memory quite yet. Albert is working hard on it at the moment.
Could you update the message to note the mem_accounting param pls?
Quoted 2 lines
Quoted 2 lines
the data pages here stay with the
memfd.
memfd / udmabuf is a little special because it uses shmem for the backing store, which is already tracked by memcg under all conditions (except hugetlb). We don't currently use udmabuf, so the lack of memcg charge transfer for udmabuf is not an issue. But once we get charge transfer working for the system heap it'd be good to follow up with udmabuf.
Quoted 1 line
T.J. needs to take a look here, but I fear that this might break uAPI.
Thanks for looping me in Christian. The buffer allocations cost gigabytes, but these metadata arrays are only 4MB per 1GB of buffer, so I don't expect charging them to break anyone. Leaving them uncharged is an accounting gap though, since someone could spam many udmabufs from the same memfd and only pay for the memfd pages once. So this change is ok with me.
Quoted 7 lines
A lot of the DMA-buf resources are intentionally not charged against memcg.
Regards,
Christian.Quoted 2 lines
Signed-off-by Tao Cui <[email protected]>
With the commit message updated to mention the mem_accounting param:
[email protected]
AI reviewRe: [PATCH 1/7] scsi: sg and st: account the command buffer memory to memcg
In reply to Tao CuiThank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [High] scsi: sg: GFP_ATOMIC | __GFP_ACCOUNT in sg_build_indirect() bypasses memcg max - [Medium] scsi: sg…
Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [High] scsi: sg: GFP_ATOMIC | __GFP_ACCOUNT in sg_build_indirect() bypasses memcg max
- [Medium] scsi: sg: unrelated and broken blktrace CAP_SYS_ADMIN gate in sg_ioctl_common()
- [Medium] scsi: st: misattributed GFP_KERNEL_ACCOUNT probe charges in new_tape_buffer()
- [Low] scsi: sg: commit message cites non-existent history in sg_build_indirect()
It also noticed 1 related pre-existing issue this patch does not introduce:
- [High] scsi: sg: shift-out-of-bounds in sg_build_indirect() [1] --
commit ec96b2b0cdd1df3575b4521cd9f81da9824bc2b2
Author: Tao Cui <[email protected]>
scsi: sg and st: account the command buffer memory to memcg
This commit adds __GFP_ACCOUNT to buffer and descriptor allocations in the sg and st drivers to charge them to the calling process's memory cgroup, and adds a CAP_SYS_ADMIN check for blktrace ioctls in sg_ioctl_common().
Quoted 8 lines
The sg data buffers are unaccounted: the per-fd reserved buffer, the scatter-gather page array, and the order-0..N pages behind it, plus the per-device tape buffer in st. All of it is user controllable. The reserved size is capped only by the device
max_sectors,SG_MAX_QUEUE
(16) commands can be in flight per fd, and the fd count is bounded byRLIMIT_NOFILE. Nothing is charged to the memcg of the process driving it. The September 2026 fixes insg_build_indirectreshaped the element sizing and left the accounting alone.
[Severity: Low]
Is the commit message accurate about the history of sg_build_indirect() and the reserved buffer cap?
In git history, sg_build_indirect() has not been modified since aaff5ebaa269 ("scsi: remove the unchecked_isa_dma flag") in 2021, and its element sizing has not changed since 10db10d144c0 ("sg: convert the indirect IO path to use the block layer") in 2008. In addition, the initial per-fd reserved buffer in sg_add_sfp() is capped by min_t(int, def_reserved_size, max_sectors_bytes(sdp->device->request_queue)), while only resizing via SG_SET_RESERVED_SIZE is capped solely by max_sectors_bytes().
Quoted 25 lines
drivers/scsi/sg.c +13
-0@@ -910,6 +910,19 @@ static int put_compat_request_table(struct compat_sg_req_info __user *o, }#endif +/* +* The sg passthrough of the blktrace ioctls has the same exposure as the +* block device path: holding the node is the only requirement today, and +* a created trace outlives the fd. Must match the gate in +* blk_trace_ioctl(). +*/ +static bool sg_is_blktrace_cmd (unsigned int cmd) +{ +return cmd ==BLKTRACESETUP || cmd ==BLKTRACESETUP2 || + cmd ==BLKTRACESTART || cmd ==BLKTRACESTOP || + cmd ==BLKTRACETEARDOWN ; +} +static long sg_ioctl_common (struct file *filp,Sg_device *sdp,Sg_fd *sfp,unsigned int cmd_in,void __user *p)
drivers/scsi/sg.c sg_ioctl_common(struct file *filp, Sg_device *sdp, Sg_fd *sfp, · line 937
Quoted 6 lines
drivers/scsi/sg.c +3
-0@@ -919,6 +932,9 @@ sg_ioctl_common(struct file *filp, Sg_device *sdp, Sg_fd *sfp,Sg_request *srp;unsigned long iflags; +if (sg_is_blktrace_cmd (cmd_in) && !capable (CAP_SYS_ADMIN )) +return -EPERM ; +
[Severity: Medium]
Does this capability check belong in a memcg accounting commit, and does it cause a UAPI regression for unprivileged users with access to /dev/sg*?
Despite the comment, blk_trace_ioctl() in kernel/trace/blktrace.c does not check capable(CAP_SYS_ADMIN); block devices gate these ioctls only on access to the device node. Moreover, sg_is_blktrace_cmd() matches BLKTRACESETUP2 even though sg_ioctl_common() only implements BLKTRACESETUP, BLKTRACESTART, BLKTRACESTOP, and BLKTRACETEARDOWN, so unprivileged callers now get -EPERM instead of -ENOTTY for BLKTRACESETUP2.
Quoted 10 lines
drivers/scsi/sg.c +1
-1@@ -1841,7 +1857,7 @@ static int sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize) {int sg_bufflen = tablesize *sizeof (struct page *); -gfp_t gfp_flags =GFP_ATOMIC | __GFP_NOWARN; +gfp_t gfp_flags =GFP_ATOMIC | __GFP_NOWARN | __GFP_ACCOUNT; schp->pages =kzalloc (sg_bufflen, gfp_flags);if (!schp->pages)
drivers/scsi/sg.c sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size) · line 1879
Quoted 9 lines
drivers/scsi/sg.c +2
-1@@ -1856,7 +1872,8 @@ sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)int ret_sz =0 , i, k, rem_sz, num, mx_sc_elems;int sg_tablesize = sfp->parentdp->sg_tablesize;int blk_size = buff_size, order; -gfp_t gfp_mask =GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN | __GFP_ZERO; +gfp_t gfp_mask =GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN | + __GFP_ACCOUNT | __GFP_ZERO;if (blk_size <0 )return -EFAULT ;
[Severity: High]
This problem wasn't introduced by this patch, but can the ret_sz shift in sg_build_indirect() trigger a signed shift-out-of-bounds [1]?
drivers/scsi/sg.c:sg_build_indirect() {
...
num = scatter_elem_sz;
...
order = get_order(num);
retry:
ret_sz = 1 << (PAGE_SHIFT + order);
...
}On 4 KB page architectures, setting the scatter_elem_sz module parameter near INT_MAX gives order == 19, so 1 << (PAGE_SHIFT + order) evaluates the signed 32-bit shift 1 << 31, triggering UBSAN or yielding a negative ret_sz that corrupts the allocation loop.
drivers/scsi/sg.c sg_add_sfp(Sg_device * sdp) · line 2168
Quoted 8 lines
drivers/scsi/sg.c +1
-1@@ -2146,7 +2163,7 @@ sg_add_sfp(Sg_device * sdp)unsigned long iflags;int bufflen; - sfp =kzalloc_obj (*sfp,GFP_ATOMIC | __GFP_NOWARN); + sfp =kzalloc_obj (*sfp,GFP_ATOMIC | __GFP_NOWARN | __GFP_ACCOUNT);if (!sfp)return ERR_PTR (-ENOMEM );
[Severity: High]
Does combining GFP_ATOMIC with __GFP_ACCOUNT in sg_build_sgat(), sg_build_indirect(), and sg_add_sfp() allow processes to bypass their memcg memory.max limit?
GFP_ATOMIC sets __GFP_HIGH and lacks __GFP_DIRECT_RECLAIM. When a memcg is at memory.max, try_charge_memcg() skips reclaim because the allocation cannot block and then force-charges past memory.max because __GFP_HIGH is set:
mm/memcontrol.c:try_charge_memcg() {
...
nomem:
if (!(gfp_mask & (__GFP_NOFAIL | __GFP_HIGH)))
return -ENOMEM;
force:
page_counter_charge(&memcg->memory, nr_pages);
...
}All callers of sg_add_sfp(), sg_build_sgat(), and sg_build_indirect() (sg_open(), SG_SET_RESERVED_SIZE in sg_ioctl_common(), and sg_start_req()) run in sleepable process context holding only mutexes, so using GFP_KERNEL would allow memcg reclaim and enforce memory.max.
Quoted 14 lines
drivers/scsi/st.c +1
-1@@ -3973,7 +3973,7 @@ static struct st_buffer *new_tape_buffer(int max_sg) {struct st_buffer *tb; - tb = kzalloc_obj(struct st_buffer); + tb = kzalloc_obj(struct st_buffer, GFP_KERNEL_ACCOUNT);if (!tb) {printk (KERN_NOTICE "st: Can't allocate new tape buffer.\n" );return NULL ;
drivers/scsi/st.c static struct st_buffer *new_tape_buffer(int max_sg) · line 3989
Quoted 9 lines
drivers/scsi/st.c +2
-1@@ -3982,7 +3982,8 @@ static struct st_buffer *new_tape_buffer(int max_sg) tb->use_sg = max_sg; tb->buffer_size =0 ; - tb->reserved_pages = kzalloc_objs(struct page *, max_sg); + tb->reserved_pages = kzalloc_objs(struct page *, max_sg, +GFP_KERNEL_ACCOUNT );if (!tb->reserved_pages) {kfree (tb);return NULL ;
[Severity: Medium]
Does using GFP_KERNEL_ACCOUNT in new_tape_buffer() charge the device probe context instead of the process driving tape I/O?
new_tape_buffer() is only called from st_probe() when a tape device is attached, and both tb and tb->reserved_pages live until scsi_tape_release(), so they are never charged to the process performing I/O. Meanwhile, the per-I/O page array allocated in the driving process's context during direct I/O in sgl_map_user_pages() (pages = kmalloc_objs(*pages, max_pages)) remains unaccounted.
[1] https://sashiko.dev/#/bug/linux-ccac9556-cc9e-4b08-b44f-5f7ffb342827
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewRe: [PATCH 2/7] dma-buf/udmabuf: account the page array metadata to memcg
In reply to Tao CuiSashiko has reviewed this patch and found no issues.
Quoted 10 lines
From: Tao Cui <[email protected]>
udmabuf pins the pages of a memfd and hands them out as a dma-buf. The data pages keep the accounting of the memfd they were instantiated in, but the bookkeeping around them does not: the page pointer array and the
pinned_foliosarray, both sized by the buffer page count, plus the transient folios array sized by the memfd folio count, are plainGFP_KERNEL. A 1GB buffer costs about 4MB of uncharged SUnreclaim memory, andsize_limit_mbdefaults toINT_MAX.
Quoted 6 lines
Charge the three kvmalloc allocations so the creator pays for the metadata it induces. The system heap in the same subsystem already accounts its buffer allocation; the data pages here stay with the memfd.
Signed-off-by Tao Cui <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewRe: [PATCH 3/7] vhost: account the per-device kernel allocations to memcg
In reply to Tao CuiSashiko has reviewed this patch and found no issues.
Quoted 9 lines
From: Tao Cui <[email protected]>
Everything except the worker struct is allocated with plain
GFP_KERNEL:
the per-vq iovec and log arrays atVHOST_SET_OWNER, the memory table atVHOST_SET_MEM_TABLEwhose size follows the user supplied region count, the pending message nodes, and the vhost_net/vhost_vsock device structs with their fallback to vmalloc. The worker has been accounted since thevhost_taskconversion.
Quoted 6 lines
A container that can open
/dev/vhost-netor/dev/vhost-vsock, which device plugins hand out with passthrough, holds roughly 150KB of resident kernel memory per device afterSET_OWNER. Charge the rest the same way the worker already is.Signed-off-by Tao Cui <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
[email protected]
AI reviewRe: [PATCH 4/7] tracing: account ring buffer pages and trace_array metadata to memcg
In reply to Tao CuiThank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Medium] tracing: alloc_cpu_data() mischarges global_trace and read-page swaps - [Medium] tracing…
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [Medium] tracing: alloc_cpu_data() mischarges global_trace and read-page swaps
- [Medium] tracing: alloc_cpu_data() memcg limit triggers trace_rb_cpu_prepare() WARN
- [Low] tracing: inaccurate snapshot size claim and inconsistent GFP_KERNEL_ACCOUNT use
It also noticed 1 related pre-existing issue this patch does not introduce:
- [Medium] tracing: memory leak in trace_array_create_systems() [1] --
commit a11da3138f9fda850c1b05bae82b0be7e738b994
Author: Tao Cui <[email protected]>
tracing: account ring buffer pages and trace_array metadata to memcg
This commit adds __GFP_ACCOUNT to ring buffer subbuffer allocations in alloc_cpu_data() and switches trace_array allocations in trace_array_create_systems() to GFP_KERNEL_ACCOUNT.
Quoted 9 lines
kernel/trace/ring_buffer.c +0
-1@@ -441,7 +441,13 @@ static struct buffer_data_page *alloc_cpu_data(int cpu, int order)* gracefully without invoking oom-killer and the system is not * destabilized. */ - mflags =GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_COMP | __GFP_ZERO;
kernel/trace/ring_buffer.c static struct buffer_data_page *alloc_cpu_data(int cpu, int order) · line 453
Quoted 10 lines
kernel/trace/ring_buffer.c +7
-0+/* +* The subbuffers are charged to the memcg of whoever caused the +* allocation: the creator of a trace instance, or root at boot for +* the global trace. +*/ + mflags =GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_COMP | __GFP_ZERO | + __GFP_ACCOUNT; page =alloc_pages_node (cpu_to_node (cpu), mflags, order);if (!page)
[Severity: Medium]
Does charging every alloc_cpu_data() call to current's memcg misattribute global_trace subbuffers and swapped read pages?
At boot, tracer_alloc_buffers() keeps global_trace at its minimum size (ring_buf_size = 1) when ring_buffer_expanded is false. global_trace is only expanded to trace_buf_size at runtime in tracing_update_buffers() or tracing_set_tracer() when a task first enables an event, sets a tracer, or loads a module using trace_printk(). With __GFP_ACCOUNT in alloc_cpu_data(), those global_trace pages are charged to whichever cgroup triggers the lazy expansion and stay pinned to that memcg because global_trace is never freed.
In addition, ring_buffer_alloc_read_page() calls alloc_cpu_data() when reading or splicing from trace_pipe_raw, and ring_buffer_read_page() swaps that reader-allocated page directly into reader->page while handing the old ring buffer page back to the reader to be freed:
kernel/trace/ring_buffer.c:ring_buffer_read_page() {
...
dpage = reader->page;
reader->page = data_page->data;
local_set(&reader->write, 0);
local_set(&reader->entries, 0);
reader->read = 0;
data_page->data = dpage;
...
}A host process reading trace_pipe_raw from a container's instance will replace the container's charged pages with uncharged pages and uncharge the container as the old pages are freed, while a cgroup task reading global_trace will swap pages charged to its own memcg into global_trace. Similarly, trace_rb_cpu_prepare() during runtime CPU hotplug allocates per-CPU buffers for all existing instances in the context of the task onlining the CPU.
[Severity: Medium]
Can hitting the memcg limit in alloc_cpu_data() during instance creation trigger the WARN(1) in trace_rb_cpu_prepare()?
When alloc_buffer() creates a ring buffer, it allocates the first CPU's buffer directly and then calls cpuhp_state_add_instance(CPUHP_TRACE_RB_PREPARE, &buffer->node), which runs trace_rb_cpu_prepare() for each remaining present CPU:
kernel/trace/ring_buffer.c:trace_rb_cpu_prepare() {
...
buffer->buffers[cpu] =
rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
if (!buffer->buffers[cpu]) {
WARN(1, "failed to allocate ring buffer on CPU %u\n",
cpu);
return -ENOMEM;
}
...
}Because __rb_allocate_pages() only checks si_mem_available() against global memory, a cgroup with enough headroom for the first CPU (~1.375 MB) that hits its memcg limit on any subsequent CPU in trace_rb_cpu_prepare() will trigger this WARN(1) and crash the host if panic_on_warn is set.
Quoted 19 lines
kernel/trace/trace.c +2
-2@@ -8677,11 +8677,11 @@ trace_array_create_systems(const char *name, const char *systems,int ret; ret = -ENOMEM ; - tr =kzalloc_obj (*tr); + tr =kzalloc_obj (*tr,GFP_KERNEL_ACCOUNT );if (!tr)return ERR_PTR (ret); - tr->name =kstrdup (name,GFP_KERNEL ); + tr->name =kstrdup (name,GFP_KERNEL_ACCOUNT );if (!tr->name) goto out_free_tr;
kernel/trace/trace.c trace_array_create_systems(const char *name, const char *systems, · line 8698
Quoted 8 lines
kernel/trace/trace.c +1
-1@@ -8692,7 +8692,7 @@ trace_array_create_systems(const char *name, const char *systems, goto out_free_tr;if (systems) { - tr->system_names =kstrdup_const (systems,GFP_KERNEL ); + tr->system_names =kstrdup_const (systems,GFP_KERNEL_ACCOUNT );if (!tr->system_names) goto out_free_tr; }
[Severity: Low]
Should the alloc_cpumask_var() and zalloc_cpumask_var() calls right between tr->name and tr->system_names, as well as alloc_cpu_page() and alloc_cpu_buffer() in ring_buffer.c, also use GFP_KERNEL_ACCOUNT?
Also, the commit message says creating an instance immediately allocates a full ~1.375 MB per-CPU snapshot buffer, but trace_allocate_snapshot() passes size = 1 when allocate_snapshot is false (which is always the case after boot), so snapshot_buffer only allocates the minimum pages until a snapshot is armed.
[Severity: Medium]
This problem wasn't introduced by this patch, but in trace_array_create_systems() [1], does the out_free_tr error path leak tr->tracers when trace_array_create_dir() fails?
trace_array_create_dir() calls __update_tracer() -> add_tracer(), which allocates struct tracers and struct tracer_flags entries on tr->tracers. If a later step in trace_array_create_dir() fails and jumps to out_free_tr, nothing calls free_tracers(tr), leaking the entries already added to tr->tracers.
[1] https://sashiko.dev/#/bug/linux-9449d18e-1233-4181-9199-09eaead7b576
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
[email protected]
AI reviewRe: [PATCH 5/7] iommufd: account the hw_queue page array to memcg
In reply to Tao CuiSashiko has reviewed this patch and found no issues.
Quoted 10 lines
From: Tao Cui <[email protected]>
IOMMUFD_CMD_HW_QUEUE_ALLOCpins up tomax_npagespages just to check that a user supplied queue region is physically contiguous, and the page array for that check is plainGFP_KERNEL. The size comes fromcmd->length, a u64 that is only overflow checked, and the array is freed again as soon as the contiguity check fails, so the allocation can be repeated concurrently: a 1GB region costs about 2MB of uncharged memory per attempt.
Quoted 5 lines
Every other iommufd allocation that scales with user input is already charged, and the pinned pages charge the mapper at map time. Charge this array like the rest.
Signed-off-by Tao Cui <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
[email protected]
AI reviewRe: [PATCH 6/7] io_uring: account the ring context and SQPOLL thread data to memcg
In reply to Tao CuiThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] io_uring: commit message omits rsrc and tctx changes and uncharged allocs --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] io_uring: commit message omits rsrc and tctx changes and uncharged allocs
--
commit 3d47975c4208930279f98824279734ca99b5e9bd
Author: Tao Cui <[email protected]>
Quoted 9 lines
io_uring: account the ring context and SQPOLL thread data to memcgThe
io_ring_ctxand the SQPOLLio_sqdare the last per-ring allocations made with plainGFP_KERNEL. The larger surfaces around them are already charged:io_mapped_ubufandio_rsrc_nodesince commit075d5837bafc("io_uring/rsrc: charge buffer arrays and nodes to memcg"), the tctx node and its XArrays since commit0b34e7b20a48("io_uring:
charge tctx nodes and personalities").
Quoted 3 lines
Charge the context and the SQPOLL thread data the same way.
Signed-off-by Tao Cui <[email protected]>
[Severity: Low]
Does the commit message match the actual changes and current tree state?
Alongside io_ring_ctx and io_sq_data, the patch also switches new_array in io_coalesce_buffer(), ctx->hash_map in io_init_wq_offload(), and io_uring_task in io_uring_alloc_task_context() to GFP_KERNEL_ACCOUNT without mentioning them in the subject or changelog.
The changelog also says these are the last per-ring GFP_KERNEL allocations. While commit "io_uring: account the remaining per-object allocations to memcg" later in the series converts io_alloc_cache_init() and io_wq_create(), percpu_ref_init(&ctx->refs, ...) in io_ring_ctx_alloc(), percpu_counter_init(&tctx->inflight, ...) in io_uring_alloc_task_context(), and alloc_cpumask_var(&wq->cpu_mask, ...) in io_wq_create() still use plain GFP_KERNEL.
Finally, the cited commits 075d5837bafc ("io_uring/rsrc: charge buffer arrays and nodes to memcg") and 0b34e7b20a48 ("io_uring: charge tctx nodes and personalities") are not present in the base tree, so io_alloc_imu(), io_rsrc_node_alloc(), io_tctx_install_node(), tctx->xa, and ctx->personalities remain uncharged.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
[email protected]
AI reviewRe: [PATCH 7/7] io_uring: account the remaining per-object allocations to memcg
In reply to Tao CuiSashiko has reviewed this patch and found no issues.
Quoted 9 lines
From: Tao Cui <[email protected]>
A handful of
io_uringallocations still scale with user controlled counts on plainGFP_KERNEL: the per-ring io-wq and its workers, the alloc cache entry arrays, the provided buffer peek and import iovec arrays, and the registered eventfd contexts. The tctx side is covered by commit0b34e7b20a48("io_uring: charge tctx nodes and personalities").
Quoted 3 lines
Charge what is left.
Signed-off-by Tao Cui <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Re: [PATCH 1/7] scsi: sg and st: account the command buffer memory to memcg
In reply to [email protected]Hi,
Quoted 4 lines · 在 2026/11/7 21:27, [email protected] 写道:
Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [High] scsi: sg:GFP_ATOMIC|__GFP_ACCOUNTinsg_build_indirect()bypasses memcg max
I'll take another look at the allocation context and whether GFP_ATOMIC is still required here.
Quoted 2 lines
- [Medium] scsi: sg: unrelated and broken blktrace
CAP_SYS_ADMINgate insg_ioctl_common()
The capability check doesn't belong in this patch. It was accidentally carried over while splitting the series and should have remained in the separate blktrace patch. I'll remove it from this patch.
Quoted 2 lines
- [Medium] scsi: st: misattributed
GFP_KERNEL_ACCOUNTprobe charges innew_tape_buffer()
Thanks for pointing this out. v2 drops the st side and keeps the accounting on the sg side.
Quoted 2 lines
- [Low] scsi: sg: commit message cites non-existent history in
sg_build_indirect()
The commit message is indeed inaccurate. I'll fix the historical description and clarify the reserved buffer limit in the next revision.
Quoted 3 lines
It also noticed 1 related pre-existing issue this patch does not introduce:
- [High] scsi: sg: shift-out-of-bounds insg_build_indirect()[1]
Thanks for pointing that out. It predates the series, and I'll investigate it separately.
Quoted 185 lines
--
commit
ec96b2b0cdd1df3575b4521cd9f81da9824bc2b2
Author: Tao Cui <[email protected]>scsi: sg and st: account the command buffer memory to memcg
This commit adds
__GFP_ACCOUNTto buffer and descriptor allocations in the sg and st drivers to charge them to the calling process's memory cgroup, and adds aCAP_SYS_ADMINcheck for blktrace ioctls insg_ioctl_common().Quoted 8 lines
The sg data buffers are unaccounted: the per-fd reserved buffer, the scatter-gather page array, and the order-0..N pages behind it, plus the per-device tape buffer in st. All of it is user controllable. The reserved size is capped only by the device
max_sectors,SG_MAX_QUEUE
(16) commands can be in flight per fd, and the fd count is bounded byRLIMIT_NOFILE. Nothing is charged to the memcg of the process driving it. The September 2026 fixes insg_build_indirectreshaped the element sizing and left the accounting alone.[Severity: Low]
Is the commit message accurate about the history ofsg_build_indirect()and the reserved buffer cap?In git history,
sg_build_indirect()has not been modified sinceaaff5ebaa269("scsi: remove theunchecked_isa_dmaflag") in 2021, and its element sizing has not changed since10db10d144c0("sg: convert the indirect IO path to use the block layer") in 2008. In addition, the initial per-fd reserved buffer insg_add_sfp()is capped bymin_t(int, def_reserved_size, max_sectors_bytes(sdp->device->request_queue)), while only resizing viaSG_SET_RESERVED_SIZEis capped solely bymax_sectors_bytes().Quoted 25 lines
drivers/scsi/sg.c +13
-0@@ -910,6 +910,19 @@ static int put_compat_request_table(struct compat_sg_req_info __user *o, }#endif +/* +* The sg passthrough of the blktrace ioctls has the same exposure as the +* block device path: holding the node is the only requirement today, and +* a created trace outlives the fd. Must match the gate in +* blk_trace_ioctl(). +*/ +static bool sg_is_blktrace_cmd (unsigned int cmd) +{ +return cmd ==BLKTRACESETUP || cmd ==BLKTRACESETUP2 || + cmd ==BLKTRACESTART || cmd ==BLKTRACESTOP || + cmd ==BLKTRACETEARDOWN ; +} +static long sg_ioctl_common (struct file *filp,Sg_device *sdp,Sg_fd *sfp,unsigned int cmd_in,void __user *p)drivers/scsi/sg.c sg_ioctl_common(struct file *filp, Sg_device *sdp, Sg_fd *sfp, · line 937
Quoted 6 lines
drivers/scsi/sg.c +3
-0@@ -919,6 +932,9 @@ sg_ioctl_common(struct file *filp, Sg_device *sdp, Sg_fd *sfp,Sg_request *srp;unsigned long iflags; +if (sg_is_blktrace_cmd (cmd_in) && !capable (CAP_SYS_ADMIN )) +return -EPERM ; +[Severity: Medium]
Does this capability check belong in a memcg accounting commit, and does it cause a UAPI regression for unprivileged users with access to/dev/sg*?Despite the comment,
blk_trace_ioctl()inkernel/trace/blktrace.cdoes not checkcapable(CAP_SYS_ADMIN); block devices gate these ioctls only on access to the device node. Moreover,sg_is_blktrace_cmd()matches BLKTRACESETUP2 even thoughsg_ioctl_common()only implements BLKTRACESETUP, BLKTRACESTART, BLKTRACESTOP, and BLKTRACETEARDOWN, so unprivileged callers now get-EPERMinstead of-ENOTTYfor BLKTRACESETUP2.Quoted 10 lines
drivers/scsi/sg.c +1
-1@@ -1841,7 +1857,7 @@ static int sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize) {int sg_bufflen = tablesize *sizeof (struct page *); -gfp_t gfp_flags =GFP_ATOMIC | __GFP_NOWARN; +gfp_t gfp_flags =GFP_ATOMIC | __GFP_NOWARN | __GFP_ACCOUNT; schp->pages =kzalloc (sg_bufflen, gfp_flags);if (!schp->pages)drivers/scsi/sg.c sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size) · line 1879
Quoted 9 lines
drivers/scsi/sg.c +2
-1@@ -1856,7 +1872,8 @@ sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)int ret_sz =0 , i, k, rem_sz, num, mx_sc_elems;int sg_tablesize = sfp->parentdp->sg_tablesize;int blk_size = buff_size, order; -gfp_t gfp_mask =GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN | __GFP_ZERO; +gfp_t gfp_mask =GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN | + __GFP_ACCOUNT | __GFP_ZERO;if (blk_size <0 )return -EFAULT ;[Severity: High]
This problem wasn't introduced by this patch, but can theret_szshift insg_build_indirect()trigger a signed shift-out-of-bounds [1]?drivers/scsi/sg.c:sg_build_indirect() { ... num = scatter_elem_sz; ... order = get_order(num); retry: ret_sz = 1 << (PAGE_SHIFT + order); ... }On 4 KB page architectures, setting the
scatter_elem_szmodule parameter nearINT_MAXgives order == 19, so 1 << (PAGE_SHIFT+ order) evaluates the signed 32-bit shift 1 << 31, triggering UBSAN or yielding a negativeret_szthat corrupts the allocation loop.drivers/scsi/sg.c sg_add_sfp(Sg_device * sdp) · line 2168
Quoted 8 lines
drivers/scsi/sg.c +1
-1@@ -2146,7 +2163,7 @@ sg_add_sfp(Sg_device * sdp)unsigned long iflags;int bufflen; - sfp =kzalloc_obj (*sfp,GFP_ATOMIC | __GFP_NOWARN); + sfp =kzalloc_obj (*sfp,GFP_ATOMIC | __GFP_NOWARN | __GFP_ACCOUNT);if (!sfp)return ERR_PTR (-ENOMEM );[Severity: High]
Does combiningGFP_ATOMICwith__GFP_ACCOUNTinsg_build_sgat(),sg_build_indirect(), andsg_add_sfp()allow processes to bypass their memcg memory.max limit?
GFP_ATOMICsets__GFP_HIGHand lacks__GFP_DIRECT_RECLAIM. When a memcg is at memory.max,try_charge_memcg()skips reclaim because the allocation cannot block and then force-charges past memory.max because__GFP_HIGHis set:mm/memcontrol.c:try_charge_memcg() { ... nomem: if (!(gfp_mask & (__GFP_NOFAIL | __GFP_HIGH))) return -ENOMEM; force: page_counter_charge(&memcg->memory, nr_pages); ... }All callers of
sg_add_sfp(),sg_build_sgat(), andsg_build_indirect()(sg_open(),SG_SET_RESERVED_SIZEinsg_ioctl_common(), andsg_start_req()) run in sleepable process context holding only mutexes, so usingGFP_KERNELwould allow memcg reclaim and enforce memory.max.Quoted 14 lines
drivers/scsi/st.c +1
-1@@ -3973,7 +3973,7 @@ static struct st_buffer *new_tape_buffer(int max_sg) {struct st_buffer *tb; - tb = kzalloc_obj(struct st_buffer); + tb = kzalloc_obj(struct st_buffer, GFP_KERNEL_ACCOUNT);if (!tb) {printk (KERN_NOTICE "st: Can't allocate new tape buffer.\n" );return NULL ;drivers/scsi/st.c static struct st_buffer *new_tape_buffer(int max_sg) · line 3989
Quoted 9 lines
drivers/scsi/st.c +2
-1@@ -3982,7 +3982,8 @@ static struct st_buffer *new_tape_buffer(int max_sg) tb->use_sg = max_sg; tb->buffer_size =0 ; - tb->reserved_pages = kzalloc_objs(struct page *, max_sg); + tb->reserved_pages = kzalloc_objs(struct page *, max_sg, +GFP_KERNEL_ACCOUNT );if (!tb->reserved_pages) {kfree (tb);return NULL ;[Severity: Medium]
Does usingGFP_KERNEL_ACCOUNTinnew_tape_buffer()charge the device probe context instead of the process driving tape I/O?
new_tape_buffer()is only called fromst_probe()when a tape device is attached, and both tb andtb->reserved_pageslive untilscsi_tape_release(), so they are never charged to the process performing I/O. Meanwhile, the per-I/O page array allocated in the driving process's context during direct I/O insgl_map_user_pages()(pages =kmalloc_objs(*pages, max_pages)) remains unaccounted.[1] https://sashiko.dev/#/bug/linux-ccac9556-cc9e-4b08-b44f-5f7ffb342827
Re: [PATCH 2/7] dma-buf/udmabuf: account the page array metadata to memcg
In reply to T.J. MercierHello,Christian,T.J.
Quoted 47 lines · T.J. Mercier
Quoted 12 lines · Christian König
Quoted 10 lines · Tao Cui
From: Tao Cui <[email protected]>
udmabuf pins the pages of a memfd and hands them out as a dma-buf. The data pages keep the accounting of the memfd they were instantiated in, but the bookkeeping around them does not: the page pointer array and the
pinned_foliosarray, both sized by the buffer page count, plus the transient folios array sized by the memfd folio count, are plainGFP_KERNEL. A 1GB buffer costs about 4MB of uncharged SUnreclaim memory, andsize_limit_mbdefaults toINT_MAX.Quoted 3 lines
Quoted 3 lines
Charge the three kvmalloc allocations so the creator pays for the metadata it induces. The system heap in the same subsystem already accounts its buffer allocation;
Only if the
mem_accountingparam is set to true. We (Android) currently depend on it being disabled because system heap buffers don't have a single owner for their entire lifetime (ownership often changes immediately after they are allocated), and the kernel doesn't have the ability to switch who is charged for the buffer memory quite yet. Albert is working hard on it at the moment.Could you update the message to note the
mem_accountingparam pls?Quoted 2 lines
Quoted 2 lines
the data pages here stay with the
memfd.memfd / udmabuf is a little special because it uses shmem for the backing store, which is already tracked by memcg under all conditions (except hugetlb). We don't currently use udmabuf, so the lack of memcg charge transfer for udmabuf is not an issue. But once we get charge transfer working for the system heap it'd be good to follow up with udmabuf.
Quoted 1 line
T.J. needs to take a look here, but I fear that this might break uAPI.
Thanks for looping me in Christian. The buffer allocations cost gigabytes, but these metadata arrays are only 4MB per 1GB of buffer, so I don't expect charging them to break anyone. Leaving them uncharged is an accounting gap though, since someone could spam many udmabufs from the same memfd and only pay for the memfd pages once. So this change is ok with me.
Quoted 2 lines
A lot of the DMA-buf resources are intentionally not charged against memcg.
Quoted 8 lines
Quoted 5 lines
Regards,
Christian.Quoted 2 lines
Signed-off-by Tao Cui <[email protected]>With the commit message updated to mention the
mem_accountingparam:Reviewed-by T.J. Mercier <[email protected]>
Thanks, Christian,T.J.
I'll update the commit message to clarify the mem_accounting parameter and pick up your Reviewed-by tag in v2.
Tao
Re: [PATCH 4/7] tracing: account ring buffer pages and trace_array metadata to memcg
In reply to [email protected]Hi,
Quoted 4 lines · 在 2026/11/7 21:27, [email protected] 写道:
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [Medium] tracing:alloc_cpu_data()mischargesglobal_traceand read-page swaps
Correct. Charging alloc_cpu_data() unconditionally pins the global trace buffers to whoever first triggers the lazy expansion, and the read-page swap can move charges between the reader's memcg and the ring buffer.
Quoted 2 lines
- [Medium] tracing:
alloc_cpu_data()memcg limit triggerstrace_rb_cpu_prepare()WARN
Also possible. With __GFP_ACCOUNT, allocation failures from memcg limits can now reach this path as well, making the existing WARN() reachable from a constrained memcg. I'll avoid charging allocations performed from the hotplug prepare path in the respin.
Quoted 2 lines
- [Low] tracing: inaccurate snapshot size claim and inconsistent
GFP_KERNEL_ACCOUNTuse
The snapshot size description in the changelog will be corrected in the respin.
Quoted 3 lines
It also noticed 1 related pre-existing issue this patch does not introduce:
- [Medium] tracing: memory leak intrace_array_create_systems()[1]
That one predates this series as well. I'll investigate it separately and send a dedicated fix if confirmed.
The v2 respin narrows the accounting. Only buffers belonging to non-root trace instances will be allocated with __GFP_ACCOUNT (via a new RB_FL_ACCOUNT flag). The global trace, read-page pool and CPU hotplug allocations will remain unaccounted, and the changelog will be updated accordingly.
Thank
Quoted 146 lines
--
commit
a11da3138f9fda850c1b05bae82b0be7e738b994
Author: Tao Cui <[email protected]>tracing: account ring buffer pages and
trace_arraymetadata to memcgThis commit adds
__GFP_ACCOUNTto ring buffer subbuffer allocations inalloc_cpu_data()and switchestrace_arrayallocations intrace_array_create_systems()toGFP_KERNEL_ACCOUNT.Quoted 9 lines
kernel/trace/ring_buffer.c +0
-1@@ -441,7 +441,13 @@ static struct buffer_data_page *alloc_cpu_data(int cpu, int order)* gracefully without invoking oom-killer and the system is not * destabilized. */ - mflags =GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_COMP | __GFP_ZERO;kernel/trace/ring_buffer.c static struct buffer_data_page *alloc_cpu_data(int cpu, int order) · line 453
Quoted 10 lines
kernel/trace/ring_buffer.c +7
-0+/* +* The subbuffers are charged to the memcg of whoever caused the +* allocation: the creator of a trace instance, or root at boot for +* the global trace. +*/ + mflags =GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_COMP | __GFP_ZERO | + __GFP_ACCOUNT; page =alloc_pages_node (cpu_to_node (cpu), mflags, order);if (!page)[Severity: Medium]
Does charging everyalloc_cpu_data()call to current's memcg misattributeglobal_tracesubbuffers and swapped read pages?At boot,
tracer_alloc_buffers()keepsglobal_traceat its minimum size (ring_buf_size= 1) whenring_buffer_expandedis false.global_traceis only expanded totrace_buf_sizeat runtime intracing_update_buffers()ortracing_set_tracer()when a task first enables an event, sets a tracer, or loads a module usingtrace_printk(). With__GFP_ACCOUNTinalloc_cpu_data(), thoseglobal_tracepages are charged to whichever cgroup triggers the lazy expansion and stay pinned to that memcg becauseglobal_traceis never freed.In addition,
ring_buffer_alloc_read_page()callsalloc_cpu_data()when reading or splicing fromtrace_pipe_raw, andring_buffer_read_page()swaps that reader-allocated page directly intoreader->pagewhile handing the old ring buffer page back to the reader to be freed:kernel/trace/ring_buffer.c:ring_buffer_read_page() { ... dpage = reader->page; reader->page = data_page->data; local_set(&reader->write, 0); local_set(&reader->entries, 0); reader->read = 0; data_page->data = dpage; ... }A host process reading
trace_pipe_rawfrom a container's instance will replace the container's charged pages with uncharged pages and uncharge the container as the old pages are freed, while a cgroup task readingglobal_tracewill swap pages charged to its own memcg intoglobal_trace. Similarly,trace_rb_cpu_prepare()during runtime CPU hotplug allocates per-CPU buffers for all existing instances in the context of the task onlining the CPU.[Severity: Medium]
Can hitting the memcg limit inalloc_cpu_data()during instance creation trigger the WARN(1) intrace_rb_cpu_prepare()?When
alloc_buffer()creates a ring buffer, it allocates the first CPU's buffer directly and then callscpuhp_state_add_instance(CPUHP_TRACE_RB_PREPARE, &buffer->node), which runstrace_rb_cpu_prepare()for each remaining present CPU:kernel/trace/ring_buffer.c:trace_rb_cpu_prepare() { ... buffer->buffers[cpu] = rb_allocate_cpu_buffer(buffer, nr_pages, cpu); if (!buffer->buffers[cpu]) { WARN(1, "failed to allocate ring buffer on CPU %u\n", cpu); return -ENOMEM; } ... }Because
__rb_allocate_pages()only checkssi_mem_available()against global memory, a cgroup with enough headroom for the first CPU (~1.375 MB) that hits its memcg limit on any subsequent CPU intrace_rb_cpu_prepare()will trigger this WARN(1) and crash the host ifpanic_on_warnis set.Quoted 19 lines
kernel/trace/trace.c +2
-2@@ -8677,11 +8677,11 @@ trace_array_create_systems(const char *name, const char *systems,int ret; ret = -ENOMEM ; - tr =kzalloc_obj (*tr); + tr =kzalloc_obj (*tr,GFP_KERNEL_ACCOUNT );if (!tr)return ERR_PTR (ret); - tr->name =kstrdup (name,GFP_KERNEL ); + tr->name =kstrdup (name,GFP_KERNEL_ACCOUNT );if (!tr->name) goto out_free_tr;kernel/trace/trace.c trace_array_create_systems(const char *name, const char *systems, · line 8698
Quoted 8 lines
kernel/trace/trace.c +1
-1@@ -8692,7 +8692,7 @@ trace_array_create_systems(const char *name, const char *systems, goto out_free_tr;if (systems) { - tr->system_names =kstrdup_const (systems,GFP_KERNEL ); + tr->system_names =kstrdup_const (systems,GFP_KERNEL_ACCOUNT );if (!tr->system_names) goto out_free_tr; }[Severity: Low]
Should thealloc_cpumask_var()andzalloc_cpumask_var()calls right betweentr->nameandtr->system_names, as well asalloc_cpu_page()andalloc_cpu_buffer()inring_buffer.c, also useGFP_KERNEL_ACCOUNT?Also, the commit message says creating an instance immediately allocates a full ~1.375 MB per-CPU snapshot buffer, but
trace_allocate_snapshot()passes size = 1 whenallocate_snapshotis false (which is always the case after boot), sosnapshot_bufferonly allocates the minimum pages until a snapshot is armed.[Severity: Medium]
This problem wasn't introduced by this patch, but intrace_array_create_systems()[1], does theout_free_trerror path leaktr->tracerswhentrace_array_create_dir()fails?
trace_array_create_dir()calls__update_tracer()->add_tracer(), which allocates struct tracers and structtracer_flagsentries ontr->tracers. If a later step intrace_array_create_dir()fails and jumps toout_free_tr, nothing callsfree_tracers(tr), leaking the entries already added totr->tracers.[1] https://sashiko.dev/#/bug/linux-9449d18e-1233-4181-9199-09eaead7b576
Re: [PATCH 6/7] io_uring: account the ring context and SQPOLL thread data to memcg
In reply to [email protected]Hi,
Quoted 4 lines · 在 2026/11/7 21:27, [email protected] 写道:
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low]io_uring: commit message omits rsrc and tctx changes and uncharged allocs
Thanks for catching this.
You're right that the changelog should better match the actual changes. Besides io_ring_ctx and io_sq_data, this patch also switches the allocations in io_coalesce_buffer(), io_init_wq_offload(), and io_uring_alloc_task_context() to GFP_KERNEL_ACCOUNT. I'll mention those explicitly in v2.
You're also right that "the last per-ring allocations" overstates the current state. Patch 7 still leaves some infrastructure allocations, such as the percpu pieces and the io-wq cpumask, unchanged. I'll drop that wording and instead describe what this patch accounts and what remains.
As for commits 075d5837bafc and 0b34e7b20a48, this series is based on linux-next (next-20261104), which already includes the io_uring-7.4-20261029 tag containing those commits. If the review is performed against a tree without that tag, those objects will indeed still appear uncharged.
Thanks,
Tao
Quoted 40 lines
--
commit
3d47975c4208930279f98824279734ca99b5e9bd
Author: Tao Cui <[email protected]>Quoted 9 lines
io_uring: account the ring context and SQPOLL thread data to memcgThe
io_ring_ctxand the SQPOLLio_sqdare the last per-ring allocations made with plainGFP_KERNEL. The larger surfaces around them are already charged:io_mapped_ubufandio_rsrc_nodesince commit075d5837bafc("io_uring/rsrc: charge buffer arrays and nodes to memcg"), the tctx node and its XArrays since commit0b34e7b20a48("io_uring:
charge tctx nodes and personalities").Quoted 3 lines
Charge the context and the SQPOLL thread data the same way.
Signed-off-by Tao Cui <[email protected]>[Severity: Low]
Does the commit message match the actual changes and current tree state?Alongside
io_ring_ctxandio_sq_data, the patch also switchesnew_arrayinio_coalesce_buffer(),ctx->hash_mapinio_init_wq_offload(), andio_uring_taskinio_uring_alloc_task_context()toGFP_KERNEL_ACCOUNTwithout mentioning them in the subject or changelog.The changelog also says these are the last per-ring
GFP_KERNELallocations. While commit "io_uring: account the remaining per-object allocations to memcg" later in the series convertsio_alloc_cache_init()andio_wq_create(),percpu_ref_init(&ctx->refs, ...)inio_ring_ctx_alloc(),percpu_counter_init(&tctx->inflight, ...)inio_uring_alloc_task_context(), andalloc_cpumask_var(&wq->cpu_mask, ...)inio_wq_create()still use plainGFP_KERNEL.Finally, the cited commits
075d5837bafc("io_uring/rsrc: charge buffer arrays and nodes to memcg") and0b34e7b20a48("io_uring: charge tctx nodes and personalities") are not present in the base tree, soio_alloc_imu(),io_rsrc_node_alloc(),io_tctx_install_node(),tctx->xa, andctx->personalitiesremain uncharged.
Christian König
maintainerRe: [PATCH 2/7] dma-buf/udmabuf: account the page array metadata to memcg
In reply to T.J. MercierQuoted 17 lines · T.J. Mercier
Quoted 12 lines · Christian König
Quoted 10 lines · Tao Cui
From: Tao Cui <[email protected]>
udmabuf pins the pages of a memfd and hands them out as a dma-buf. The data pages keep the accounting of the memfd they were instantiated in, but the bookkeeping around them does not: the page pointer array and the
pinned_foliosarray, both sized by the buffer page count, plus the transient folios array sized by the memfd folio count, are plainGFP_KERNEL. A 1GB buffer costs about 4MB of uncharged SUnreclaim memory, andsize_limit_mbdefaults toINT_MAX.Quoted 3 lines
Quoted 3 lines
Charge the three kvmalloc allocations so the creator pays for the metadata it induces. The system heap in the same subsystem already accounts its buffer allocation;
Quoted 6 lines
Only if the
mem_accountingparam is set to true. We (Android) currently depend on it being disabled because system heap buffers don't have a single owner for their entire lifetime (ownership often changes immediately after they are allocated), and the kernel doesn't have the ability to switch who is charged for the buffer memory quite yet. Albert is working hard on it at the moment.
Good point, making sure that Albert is on CC as well.
So basically we not only need to charge the backing store of a DMA-buf to somebody else, but also the housekeeping overhead.
Mhm, this here also complicates the charge transfer Albert is working on.
@Albert not something we need to take care about now, but please keep that in mind while working on that stuff.
Regards,
Christian.
Quoted 32 lines
Could you update the message to note the
mem_accountingparam pls?Quoted 2 lines
Quoted 2 lines
the data pages here stay with the
memfd.memfd / udmabuf is a little special because it uses shmem for the backing store, which is already tracked by memcg under all conditions (except hugetlb). We don't currently use udmabuf, so the lack of memcg charge transfer for udmabuf is not an issue. But once we get charge transfer working for the system heap it'd be good to follow up with udmabuf.
Quoted 1 line
T.J. needs to take a look here, but I fear that this might break uAPI.
Thanks for looping me in Christian. The buffer allocations cost gigabytes, but these metadata arrays are only 4MB per 1GB of buffer, so I don't expect charging them to break anyone. Leaving them uncharged is an accounting gap though, since someone could spam many udmabufs from the same memfd and only pay for the memfd pages once. So this change is ok with me.
Quoted 7 lines
A lot of the DMA-buf resources are intentionally not charged against memcg.
Regards,
Christian.Quoted 2 lines
Signed-off-by Tao Cui <[email protected]>With the commit message updated to mention the
mem_accountingparam:Reviewed-by T.J. Mercier <[email protected]>
Re: [PATCH 2/7] dma-buf/udmabuf: account the page array metadata to memcg
Quoted 26 lines · Christian König
Quoted 17 lines · T.J. Mercier
Quoted 12 lines · Christian König
Quoted 10 lines · Tao Cui
From: Tao Cui <[email protected]>
udmabuf pins the pages of a memfd and hands them out as a dma-buf. The data pages keep the accounting of the memfd they were instantiated in, but the bookkeeping around them does not: the page pointer array and the
pinned_foliosarray, both sized by the buffer page count, plus the transient folios array sized by the memfd folio count, are plainGFP_KERNEL. A 1GB buffer costs about 4MB of uncharged SUnreclaim memory, andsize_limit_mbdefaults toINT_MAX.Quoted 3 lines
Quoted 3 lines
Charge the three kvmalloc allocations so the creator pays for the metadata it induces. The system heap in the same subsystem already accounts its buffer allocation;
Quoted 6 lines
Only if the
mem_accountingparam is set to true. We (Android) currently depend on it being disabled because system heap buffers don't have a single owner for their entire lifetime (ownership often changes immediately after they are allocated), and the kernel doesn't have the ability to switch who is charged for the buffer memory quite yet. Albert is working hard on it at the moment.
Quoted 3 lines
Good point, making sure that Albert is on CC as well.
So basically we not only need to charge the backing store of a DMA-buf to somebody else, but also the housekeeping overhead.
For system heaps the housekeeping size is fixed at creation so it kindof fits the reservation model nicely. We'd only need it to account for the overhead (as part of the MEMCG_DMABUF stat probably).
Quoted 2 lines
Mhm, this here also complicates the charge transfer Albert is working on.
The transfer would just move the charge as a single unit. But yeah, the complexity will come from lazy exporters or any other exporter where the bookkeeping grows or changes.
Quoted 2 lines
@Albert not something we need to take care about now, but please keep that in mind while working on that stuff.
Noted. The issue already exists so my series does not make it worse. I will note this for a follow up series.
Thanks for tagging me!
BR,
Albert
Quoted 37 lines
Regards,
Christian.Quoted 32 lines
Could you update the message to note the
mem_accountingparam pls?Quoted 2 lines
Quoted 2 lines
the data pages here stay with the
memfd.memfd / udmabuf is a little special because it uses shmem for the backing store, which is already tracked by memcg under all conditions (except hugetlb). We don't currently use udmabuf, so the lack of memcg charge transfer for udmabuf is not an issue. But once we get charge transfer working for the system heap it'd be good to follow up with udmabuf.
Quoted 1 line
T.J. needs to take a look here, but I fear that this might break uAPI.
Thanks for looping me in Christian. The buffer allocations cost gigabytes, but these metadata arrays are only 4MB per 1GB of buffer, so I don't expect charging them to break anyone. Leaving them uncharged is an accounting gap though, since someone could spam many udmabufs from the same memfd and only pay for the memfd pages once. So this change is ok with me.
Quoted 7 lines
A lot of the DMA-buf resources are intentionally not charged against memcg.
Regards,
Christian.Quoted 2 lines
Signed-off-by Tao Cui <[email protected]>With the commit message updated to mention the
mem_accountingparam:Reviewed-by T.J. Mercier <[email protected]>
Vincent Donnefort
maintainerRe: [PATCH 4/7] tracing: account ring buffer pages and trace_array metadata to memcg
In reply to Tao CuiQuoted 30 lines · Tao Cui
From: Tao Cui <[email protected]>
Creating an instance immediately allocates two full sets of per-cpu ring buffers, array buffer plus snapshot, about 1.375MB per cpu per set on the default config, plus the
trace_arrayand its name. None of it is charged: neithertrace.cnorring_buffer.cpasses__GFP_ACCOUNT, whileuser_eventsin the same directory accounts everything it allocates.Instance creation is currently gated by tracefs file permissions alone, so a
rootful container with the host tracefs bound in can create instances until the host runs out of memory. Charge the ring buffer pages and thetrace_arraymetadata to the creating cgroup.Signed-off-by Tao Cui <[email protected]>--- kernel/trace/ring_buffer.c | 8 +++++++- kernel/trace/trace.c | 6 +++--- 2 files changed, 10 insertions(+), 4 deletions(-)kernel/trace/ring_buffer.c +0
-1@@ -441,7 +441,13 @@ static struct buffer_data_page *alloc_cpu_data(int cpu, int order)* gracefully without invoking oom-killer and the system is not * destabilized. */ - mflags =GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_COMP | __GFP_ZERO;
kernel/trace/ring_buffer.c static struct buffer_data_page *alloc_cpu_data(int cpu, int order) · line 453
Quoted 10 lines
kernel/trace/ring_buffer.c +7
-0+/* +* The subbuffers are charged to the memcg of whoever caused the +* allocation: the creator of a trace instance, or root at boot for +* the global trace. +*/ + mflags =GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_COMP | __GFP_ZERO | + __GFP_ACCOUNT; page =alloc_pages_node (cpu_to_node (cpu), mflags, order);if (!page)
Hum, by default tracefs is root only.
And even if made accessible to other users:
1. The life span of a trace instance is not tied to the caller.
2. The ring buffer is shared by multiple users.
Overall I am not convinced charging the creator makes much sense.
Eventually a few allocations for iterators could be GFP_ACCOUNT.
Signature · 1 line
Vincent
Quoted 30 lines
kernel/trace/trace.c +3
-3@@ -8693,11 +8693,11 @@ trace_array_create_systems(const char *name, const char *systems,int ret; ret = -ENOMEM ; - tr =kzalloc_obj (*tr); + tr =kzalloc_obj (*tr,GFP_KERNEL_ACCOUNT );if (!tr)return ERR_PTR (ret); - tr->name =kstrdup (name,GFP_KERNEL ); + tr->name =kstrdup (name,GFP_KERNEL_ACCOUNT );if (!tr->name) goto out_free_tr;@@ -8708,7 +8708,7 @@ trace_array_create_systems(const char *name, const char *systems, goto out_free_tr;if (systems) { - tr->system_names =kstrdup_const (systems,GFP_KERNEL ); + tr->system_names =kstrdup_const (systems,GFP_KERNEL_ACCOUNT );if (!tr->system_names) goto out_free_tr; }Signature · 1 line
2.43.0
Steven Rostedt
maintainerRe: [PATCH 4/7] tracing: account ring buffer pages and trace_array metadata to memcg
Quoted 9 lines · On Wed, 11 Nov 2026 16:47:38 +0000 Vincent Donnefort <[email protected]>…
Quoted 8 lines · Tao Cui
From: Tao Cui <[email protected]>
Creating an instance immediately allocates two full sets of per-cpu ring buffers, array buffer plus snapshot, about 1.375MB per cpu per set on the default config, plus the
trace_arrayand its name. None of it is charged: neithertrace.cnorring_buffer.cpasses__GFP_ACCOUNT, whileuser_eventsin the same directory accounts everything it allocates.
As the name suggests, "user_events" are for users and are special.
Quoted 5 lines
Quoted 5 lines
Instance creation is currently gated by tracefs file permissions alone, so a
rootful container with the host tracefs bound in can create instances until the host runs out of memory.
You mean, "works as expected"?
Quoted 31 lines
Charge the ring buffer pages and
Quoted 17 lines
the
trace_arraymetadata to the creating cgroup.Signed-off-by Tao Cui <[email protected]>--- kernel/trace/ring_buffer.c | 8 +++++++- kernel/trace/trace.c | 6 +++--- 2 files changed, 10 insertions(+), 4 deletions(-)kernel/trace/ring_buffer.c +0
-1@@ -441,7 +441,13 @@ static struct buffer_data_page *alloc_cpu_data(int cpu, int order)* gracefully without invoking oom-killer and the system is not * destabilized. */ - mflags =GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_COMP | __GFP_ZERO;kernel/trace/ring_buffer.c static struct buffer_data_page *alloc_cpu_data(int cpu, int order) · line 453
Quoted 10 lines
kernel/trace/ring_buffer.c +7
-0+/* +* The subbuffers are charged to the memcg of whoever caused the +* allocation: the creator of a trace instance, or root at boot for +* the global trace. +*/ + mflags =GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_COMP | __GFP_ZERO | + __GFP_ACCOUNT; page =alloc_pages_node (cpu_to_node (cpu), mflags, order);if (!page)Hum, by default tracefs is root only.
Quoted 4 lines
And even if made accessible to other users:
1. The life span of a trace instance is not tied to the caller.
2. The ring buffer is shared by multiple users.
Right. Is there a use case out there that allows non-admin to create an instance? Tracing ring buffers can expose a lot and should not be given to unprivileged users.
What exactly is this trying to achieve?
-- Steve
Quoted 5 lines
Overall I am not convinced charging the creator makes much sense.
Eventually a few allocations for iterators could be
GFP_ACCOUNT.
Re: [PATCH 5/7] iommufd: account the hw_queue page array to memcg
In reply to Tao CuiQuoted 10 lines · Tao Cui
From: Tao Cui <[email protected]>
IOMMUFD_CMD_HW_QUEUE_ALLOCpins up tomax_npagespages just to check that a user supplied queue region is physically contiguous, and the page array for that check is plainGFP_KERNEL. The size comes fromcmd->length, a u64 that is only overflow checked, and the array is freed again as soon as the contiguity check fails, so the allocation can be repeated concurrently: a 1GB region costs about 2MB of uncharged memory per attempt.
Quoted 8 lines
Every other iommufd allocation that scales with user input is already charged, and the pinned pages charge the mapper at map time. Charge this array like the rest.
Signed-off-by Tao Cui <[email protected]>--- drivers/iommu/iommufd/viommu.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
Jason
Re: [PATCH 5/7] iommufd: account the hw_queue page array to memcg
Quoted 15 lines · Jason Gunthorpe
Quoted 14 lines · Tao Cui
From: Tao Cui <[email protected]>
IOMMUFD_CMD_HW_QUEUE_ALLOCpins up tomax_npagespages just to check that a user supplied queue region is physically contiguous, and the page array for that check is plainGFP_KERNEL. The size comes fromcmd->length, a u64 that is only overflow checked, and the array is freed again as soon as the contiguity check fails, so the allocation can be repeated concurrently: a 1GB region costs about 2MB of uncharged memory per attempt.Every other iommufd allocation that scales with user input is already charged, and the pinned pages charge the mapper at map time. Charge this array like the rest.
Quoted 6 lines
Quoted 4 lines
Signed-off-by Tao Cui <[email protected]>--- drivers/iommu/iommufd/viommu.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)Reviewed-by Jason Gunthorpe <[email protected]>
I'll just apply it, thanks
Jason
Re: [PATCH 3/7] vhost: account the per-device kernel allocations to memcg
In reply to Tao CuiQuoted 9 lines · Tao Cui
From: Tao Cui <[email protected]>
Everything except the worker struct is allocated with plain
GFP_KERNEL:
the per-vq iovec and log arrays atVHOST_SET_OWNER, the memory table atVHOST_SET_MEM_TABLEwhose size follows the user supplied region count, the pending message nodes, and the vhost_net/vhost_vsock device structs with their fallback to vmalloc. The worker has been accounted since thevhost_taskconversion.
Quoted 6 lines
A container that can open
/dev/vhost-netor/dev/vhost-vsock, which device plugins hand out with passthrough, holds roughly 150KB of resident kernel memory per device afterSET_OWNER. Charge the rest the same way the worker already is.Signed-off-by Tao Cui <[email protected]>
should each patch be merged individually?
Quoted 123 lines
--- drivers/vhost/net.c | 14 ++++++++------ drivers/vhost/vhost.c | 16 ++++++++++------ drivers/vhost/vsock.c | 5 +++-- 3 files changed, 21 insertions(+), 14 deletions(-)drivers/vhost/net.c +8
-6@@ -249,7 +249,7 @@ vhost_net_ubuf_alloc(struct vhost_virtqueue *vq, bool zcopy)/* No zero copy backend? Nothing to count. */ if (!zcopy)return NULL ; - ubufs =kmalloc_obj (*ubufs); + ubufs =kmalloc_obj (*ubufs,GFP_KERNEL_ACCOUNT );if (!ubufs)return ERR_PTR (-ENOMEM );atomic_set (&ubufs->refcount,1 );@@ -302,7 +302,8 @@ static int vhost_net_set_ubuf_info(struct vhost_net *n)if (!zcopy)continue ; n->vqs[i].ubuf_info = -kmalloc_objs (*n->vqs[i].ubuf_info,UIO_MAXIOV ); +kmalloc_objs (*n->vqs[i].ubuf_info,UIO_MAXIOV , +GFP_KERNEL_ACCOUNT );if (!n->vqs[i].ubuf_info) goto err; }@@ -1344,17 +1345,18 @@ static int vhost_net_open(struct inode *inode, struct file *f)struct xdp_buff *xdp;int i; - n =kvmalloc_obj (*n,GFP_KERNEL | __GFP_RETRY_MAYFAIL); + n =kvmalloc_obj (*n,GFP_KERNEL | __GFP_RETRY_MAYFAIL | + __GFP_ACCOUNT);if (!n)return -ENOMEM ; - vqs =kmalloc_objs (*vqs,VHOST_NET_VQ_MAX ); + vqs =kmalloc_objs (*vqs,VHOST_NET_VQ_MAX ,GFP_KERNEL_ACCOUNT );if (!vqs) {kvfree (n);return -ENOMEM ; } queue =kmalloc_array (VHOST_NET_BATCH ,sizeof (void *), -GFP_KERNEL ); +GFP_KERNEL_ACCOUNT );if (!queue) {kfree (vqs);kvfree (n);@@ -1362,7 +1364,7 @@ static int vhost_net_open(struct inode *inode, struct file *f) } n->vqs[VHOST_NET_VQ_RX ].rxq.queue = queue; - xdp =kmalloc_objs (*xdp,VHOST_NET_BATCH ); + xdp =kmalloc_objs (*xdp,VHOST_NET_BATCH ,GFP_KERNEL_ACCOUNT );if (!xdp) {kfree (vqs);kvfree (n);drivers/vhost/vhost.c +10
-6@@ -526,11 +526,14 @@ static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)for (i =0 ; i < dev->nvqs; ++i) { vq = dev->vqs[i]; - vq->indirect =kmalloc_objs (*vq->indirect,UIO_MAXIOV ); - vq->log =kmalloc_objs (*vq->log, dev->iov_limit); - vq->heads =kmalloc_objs (*vq->heads, dev->iov_limit); + vq->indirect =kmalloc_objs (*vq->indirect,UIO_MAXIOV , +GFP_KERNEL_ACCOUNT ); + vq->log =kmalloc_objs (*vq->log, dev->iov_limit, +GFP_KERNEL_ACCOUNT ); + vq->heads =kmalloc_objs (*vq->heads, dev->iov_limit, +GFP_KERNEL_ACCOUNT ); vq->nheads =kmalloc_array (dev->iov_limit,sizeof (*vq->nheads), -GFP_KERNEL ); +GFP_KERNEL_ACCOUNT );if (!vq->indirect || !vq->log || !vq->heads || !vq->nheads) goto err_nomem; }@@ -2053,7 +2056,8 @@ static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)return -E2BIG ;if (max_iotlb_entries <=0 )return -EINVAL ; - newmem =kvzalloc_flex (*newmem, regions, mem.nregions); + newmem =kvzalloc_flex (*newmem, regions, mem.nregions, +GFP_KERNEL_ACCOUNT );if (!newmem)return -ENOMEM ;@@ -3405,7 +3409,7 @@ EXPORT_SYMBOL_GPL(vhost_disable_notify);struct vhost_msg_node *vhost_new_msg (struct vhost_virtqueue *vq,int type) {/* Make sure all padding within the structure is initialized. */ -struct vhost_msg_node *node =kzalloc_obj (*node); +struct vhost_msg_node *node =kzalloc_obj (*node,GFP_KERNEL_ACCOUNT );if (!node)return NULL ;drivers/vhost/vsock.c +3
-2@@ -721,11 +721,12 @@ static int vhost_vsock_dev_open(struct inode *inode, struct file *file)/* This struct is large and allocation could fail, fall back to vmalloc * if there is no other way. */ - vsock =kvmalloc_obj (*vsock,GFP_KERNEL | __GFP_RETRY_MAYFAIL); + vsock =kvmalloc_obj (*vsock,GFP_KERNEL | __GFP_RETRY_MAYFAIL | + __GFP_ACCOUNT);if (!vsock)return -ENOMEM ; - vqs =kmalloc_objs (*vqs,ARRAY_SIZE (vsock->vqs)); + vqs =kmalloc_objs (*vqs,ARRAY_SIZE (vsock->vqs),GFP_KERNEL_ACCOUNT );if (!vqs) { ret = -ENOMEM ; goto out;Signature · 1 line
2.43.0
Re: [PATCH 4/7] tracing: account ring buffer pages and trace_array metadata to memcg
In reply to Steven RostedtHello, Steven, Vincent,
Quoted 54 lines · Steven Rostedt
Quoted 9 lines · On Wed, 11 Nov 2026 16:47:38 +0000 Vincent Donnefort <[email protected]>…
Quoted 8 lines · Tao Cui
From: Tao Cui <[email protected]>
Creating an instance immediately allocates two full sets of per-cpu ring buffers, array buffer plus snapshot, about 1.375MB per cpu per set on the default config, plus the
trace_arrayand its name. None of it is charged: neithertrace.cnorring_buffer.cpasses__GFP_ACCOUNT, whileuser_eventsin the same directory accounts everything it allocates.As the name suggests, "
user_events" are for users and are special.Quoted 5 lines
Quoted 5 lines
Instance creation is currently gated by tracefs file permissions alone, so a
rootful container with the host tracefs bound in can create instances until the host runs out of memory.You mean, "works as expected"?
Quoted 31 lines
Charge the ring buffer pages and
Quoted 17 lines
the
trace_arraymetadata to the creating cgroup.Signed-off-by Tao Cui <[email protected]>--- kernel/trace/ring_buffer.c | 8 +++++++- kernel/trace/trace.c | 6 +++--- 2 files changed, 10 insertions(+), 4 deletions(-)kernel/trace/ring_buffer.c +0
-1@@ -441,7 +441,13 @@ static struct buffer_data_page *alloc_cpu_data(int cpu, int order)* gracefully without invoking oom-killer and the system is not * destabilized. */ - mflags =GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_COMP | __GFP_ZERO;kernel/trace/ring_buffer.c static struct buffer_data_page *alloc_cpu_data(int cpu, int order) · line 453
Quoted 10 lines
kernel/trace/ring_buffer.c +7
-0+/* +* The subbuffers are charged to the memcg of whoever caused the +* allocation: the creator of a trace instance, or root at boot for +* the global trace. +*/ + mflags =GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_COMP | __GFP_ZERO | + __GFP_ACCOUNT; page =alloc_pages_node (cpu_to_node (cpu), mflags, order);if (!page)Hum, by default tracefs is root only.
Quoted 9 lines
Quoted 4 lines
And even if made accessible to other users:
1. The life span of a trace instance is not tied to the caller.
2. The ring buffer is shared by multiple users.Right. Is there a use case out there that allows non-admin to create an instance? Tracing ring buffers can expose a lot and should not be given to unprivileged users.
Not that I can point to. Instances are admin-only by default, and the permission layer is the right place for that.
Quoted 2 lines
What exactly is this trying to achieve?
The series closes uncharged kernel allocations that scale with user input. For tracing I misread the tracefs permission check as a gap rather than the intended boundary. Combined with Vincent's points on lifetime and sharing, I'll drop patch.
Thanks,
Tao
Quoted 9 lines
-- Steve
Quoted 5 lines
Overall I am not convinced charging the creator makes much sense.
Eventually a few allocations for iterators could be
GFP_ACCOUNT.
Re: [PATCH 3/7] vhost: account the per-device kernel allocations to memcg
In reply to Michael S. TsirkinHello, Michael,
Quoted 15 lines · Michael S. Tsirkin
Quoted 14 lines · Tao Cui
From: Tao Cui <[email protected]>
Everything except the worker struct is allocated with plain
GFP_KERNEL:
the per-vq iovec and log arrays atVHOST_SET_OWNER, the memory table atVHOST_SET_MEM_TABLEwhose size follows the user supplied region count, the pending message nodes, and the vhost_net/vhost_vsock device structs with their fallback to vmalloc. The worker has been accounted since thevhost_taskconversion.A container that can open
/dev/vhost-netor/dev/vhost-vsock, which device plugins hand out with passthrough, holds roughly 150KB of resident kernel memory per device afterSET_OWNER. Charge the rest the same way the worker already is.
Quoted 7 lines
Quoted 1 line
Signed-off-by Tao Cui <[email protected]>should each patch be merged individually?
Yes, I think merging them individually is the cleaner path here: the series spans six subsystems, and each maintainer taking their own piece keeps things simple. Jason has already taken patch 5 into iommufd. Please feel free to take patch 3 through your tree.
Thanks,
Tao
Quoted 125 lines
Quoted 124 lines
--- drivers/vhost/net.c | 14 ++++++++------ drivers/vhost/vhost.c | 16 ++++++++++------ drivers/vhost/vsock.c | 5 +++-- 3 files changed, 21 insertions(+), 14 deletions(-)drivers/vhost/net.c +8
-6@@ -249,7 +249,7 @@ vhost_net_ubuf_alloc(struct vhost_virtqueue *vq, bool zcopy)/* No zero copy backend? Nothing to count. */ if (!zcopy)return NULL ; - ubufs =kmalloc_obj (*ubufs); + ubufs =kmalloc_obj (*ubufs,GFP_KERNEL_ACCOUNT );if (!ubufs)return ERR_PTR (-ENOMEM );atomic_set (&ubufs->refcount,1 );@@ -302,7 +302,8 @@ static int vhost_net_set_ubuf_info(struct vhost_net *n)if (!zcopy)continue ; n->vqs[i].ubuf_info = -kmalloc_objs (*n->vqs[i].ubuf_info,UIO_MAXIOV ); +kmalloc_objs (*n->vqs[i].ubuf_info,UIO_MAXIOV , +GFP_KERNEL_ACCOUNT );if (!n->vqs[i].ubuf_info) goto err; }@@ -1344,17 +1345,18 @@ static int vhost_net_open(struct inode *inode, struct file *f)struct xdp_buff *xdp;int i; - n =kvmalloc_obj (*n,GFP_KERNEL | __GFP_RETRY_MAYFAIL); + n =kvmalloc_obj (*n,GFP_KERNEL | __GFP_RETRY_MAYFAIL | + __GFP_ACCOUNT);if (!n)return -ENOMEM ; - vqs =kmalloc_objs (*vqs,VHOST_NET_VQ_MAX ); + vqs =kmalloc_objs (*vqs,VHOST_NET_VQ_MAX ,GFP_KERNEL_ACCOUNT );if (!vqs) {kvfree (n);return -ENOMEM ; } queue =kmalloc_array (VHOST_NET_BATCH ,sizeof (void *), -GFP_KERNEL ); +GFP_KERNEL_ACCOUNT );if (!queue) {kfree (vqs);kvfree (n);@@ -1362,7 +1364,7 @@ static int vhost_net_open(struct inode *inode, struct file *f) } n->vqs[VHOST_NET_VQ_RX ].rxq.queue = queue; - xdp =kmalloc_objs (*xdp,VHOST_NET_BATCH ); + xdp =kmalloc_objs (*xdp,VHOST_NET_BATCH ,GFP_KERNEL_ACCOUNT );if (!xdp) {kfree (vqs);kvfree (n);drivers/vhost/vhost.c +10
-6@@ -526,11 +526,14 @@ static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)for (i =0 ; i < dev->nvqs; ++i) { vq = dev->vqs[i]; - vq->indirect =kmalloc_objs (*vq->indirect,UIO_MAXIOV ); - vq->log =kmalloc_objs (*vq->log, dev->iov_limit); - vq->heads =kmalloc_objs (*vq->heads, dev->iov_limit); + vq->indirect =kmalloc_objs (*vq->indirect,UIO_MAXIOV , +GFP_KERNEL_ACCOUNT ); + vq->log =kmalloc_objs (*vq->log, dev->iov_limit, +GFP_KERNEL_ACCOUNT ); + vq->heads =kmalloc_objs (*vq->heads, dev->iov_limit, +GFP_KERNEL_ACCOUNT ); vq->nheads =kmalloc_array (dev->iov_limit,sizeof (*vq->nheads), -GFP_KERNEL ); +GFP_KERNEL_ACCOUNT );if (!vq->indirect || !vq->log || !vq->heads || !vq->nheads) goto err_nomem; }@@ -2053,7 +2056,8 @@ static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)return -E2BIG ;if (max_iotlb_entries <=0 )return -EINVAL ; - newmem =kvzalloc_flex (*newmem, regions, mem.nregions); + newmem =kvzalloc_flex (*newmem, regions, mem.nregions, +GFP_KERNEL_ACCOUNT );if (!newmem)return -ENOMEM ;@@ -3405,7 +3409,7 @@ EXPORT_SYMBOL_GPL(vhost_disable_notify);struct vhost_msg_node *vhost_new_msg (struct vhost_virtqueue *vq,int type) {/* Make sure all padding within the structure is initialized. */ -struct vhost_msg_node *node =kzalloc_obj (*node); +struct vhost_msg_node *node =kzalloc_obj (*node,GFP_KERNEL_ACCOUNT );if (!node)return NULL ;drivers/vhost/vsock.c +3
-2@@ -721,11 +721,12 @@ static int vhost_vsock_dev_open(struct inode *inode, struct file *file)/* This struct is large and allocation could fail, fall back to vmalloc * if there is no other way. */ - vsock =kvmalloc_obj (*vsock,GFP_KERNEL | __GFP_RETRY_MAYFAIL); + vsock =kvmalloc_obj (*vsock,GFP_KERNEL | __GFP_RETRY_MAYFAIL | + __GFP_ACCOUNT);if (!vsock)return -ENOMEM ; - vqs =kmalloc_objs (*vqs,ARRAY_SIZE (vsock->vqs)); + vqs =kmalloc_objs (*vqs,ARRAY_SIZE (vsock->vqs),GFP_KERNEL_ACCOUNT );if (!vqs) { ret = -ENOMEM ; goto out;Signature · 1 line
2.43.0