* [PATCH v3] vhost: account the per-device kernel allocations to memcg
@ 2026-12-03 12:01 Tao Cui
2026-12-03 12:07 ` Michael S. Tsirkin
2026-12-03 12:45 ` [PATCH v4] " Tao Cui
0 siblings, 2 replies; 5+ messages in thread
From: Tao Cui @ 2026-12-03 12:01 UTC (permalink / raw)
To: Michael S. Tsirkin
Cc: Jason Wang, Eugenio Pérez, Stefan Hajnoczi,
Stefano Garzarella, Bobby Eshleman, Mike Christie,
Andrey Drobyshev, virtualization, kvm, linux-mm, akpm, cuitao
From: Tao Cui <cuitao@kylinos.cn>
Everything except the worker struct is allocated with plain GFP:
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, the vhost_net/vhost_vsock device
structs with their fallback to vmalloc, the vhost-scsi device struct
and its vq arrays at open, the per-vq tag bitmap and command pools
sized by max_cmds and inline_sg_cnt, the target array at
VHOST_SCSI_SET_ENDPOINT, the vhost-vdpa device struct and vq array,
the per-asid allocations, the mapping file, and the GET_CONFIG buffer
sized by config.len. The worker has been accounted since the
vhost_task conversion.
A container that can open /dev/vhost-net, /dev/vhost-vsock,
/dev/vhost-scsi or /dev/vhost-vdpa, which device plugins hand out
with passthrough, holds roughly 150KB of resident kernel memory per
device after SET_OWNER, and a vhost-scsi device holds a few hundred
KB more once its per-vq command pools are built. Charge the rest the
same way the worker already is. Per-command allocations in the
request path, the per-entry IOTLB map nodes, and vhost_iotlb_alloc(),
which drivers/vdpa also calls, stay uncharged.
Acked-by: Michael S. Tsirkin <mst@redhat.com>
Acked-by: Bobby Eshleman <bobbyeshleman@meta.com>
Signed-off-by: Tao Cui <cuitao@kylinos.cn>
---
Changes in v3:
- extend the accounting to the vhost-scsi and vhost-vdpa backends,
whose allocations follow the same per-device pattern, per Stefano's
review on v2
- rebased on next-20261202; the net/vsock/vhost.c hunks are identical
to v2, carrying Michael's and Bobby's acks from the earlier versions
Link to v2: https://lore.kernel.org/r/20261120054517.526275-1-cui.tao@linux.dev
Changes in v2:
- rebased on next-20261118, where the vq->nheads allocation now uses
kmalloc_objs(); the flag addition follows the new form. No
functional change. Carries Michael's Acked-by from v1.
Link to v1: https://lore.kernel.org/r/20261106131829.3228926-4-cui.tao@linux.dev
---
drivers/vhost/net.c | 14 ++++++++------
drivers/vhost/scsi.c | 23 +++++++++++++----------
drivers/vhost/vdpa.c | 14 ++++++++------
drivers/vhost/vhost.c | 17 +++++++++++------
drivers/vhost/vsock.c | 5 +++--
5 files changed, 43 insertions(+), 30 deletions(-)
diff --git a/drivers/vhost/net.c b/drivers/vhost/net.c
index 2adfe3678f76..36f76327610b 100644
--- a/drivers/vhost/net.c
+++ b/drivers/vhost/net.c
@@ -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;
}
@@ -1340,17 +1341,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);
@@ -1358,7 +1360,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);
diff --git a/drivers/vhost/scsi.c b/drivers/vhost/scsi.c
index 8beeff06a7a7..ca1bbb95f4a5 100644
--- a/drivers/vhost/scsi.c
+++ b/drivers/vhost/scsi.c
@@ -2020,18 +2020,19 @@ static int vhost_scsi_setup_vq_cmds(struct vhost_virtqueue *vq, int max_cmds)
if (svq->scsi_cmds)
return 0;
- if (sbitmap_init_node(&svq->scsi_tags, max_cmds, -1, GFP_KERNEL,
+ if (sbitmap_init_node(&svq->scsi_tags, max_cmds, -1, GFP_KERNEL_ACCOUNT,
NUMA_NO_NODE, false, true))
return -ENOMEM;
svq->max_cmds = max_cmds;
- svq->scsi_cmds = kzalloc_objs(*tv_cmd, max_cmds);
+ svq->scsi_cmds = kzalloc_objs(*tv_cmd, max_cmds, GFP_KERNEL_ACCOUNT);
if (!svq->scsi_cmds) {
sbitmap_free(&svq->scsi_tags);
return -ENOMEM;
}
- svq->upages = kzalloc_objs(struct page *, VHOST_SCSI_PREALLOC_UPAGES);
+ svq->upages = kzalloc_objs(struct page *, VHOST_SCSI_PREALLOC_UPAGES,
+ GFP_KERNEL_ACCOUNT);
if (!svq->upages)
goto out;
@@ -2040,7 +2041,8 @@ static int vhost_scsi_setup_vq_cmds(struct vhost_virtqueue *vq, int max_cmds)
if (vs->inline_sg_cnt) {
tv_cmd->sgl = kzalloc_objs(struct scatterlist,
- vs->inline_sg_cnt);
+ vs->inline_sg_cnt,
+ GFP_KERNEL_ACCOUNT);
if (!tv_cmd->sgl) {
pr_err("Unable to allocate tv_cmd->sgl\n");
goto out;
@@ -2050,7 +2052,8 @@ static int vhost_scsi_setup_vq_cmds(struct vhost_virtqueue *vq, int max_cmds)
if (vhost_has_feature(vq, VIRTIO_SCSI_F_T10_PI) &&
vs->inline_sg_cnt) {
tv_cmd->prot_sgl = kzalloc_objs(struct scatterlist,
- vs->inline_sg_cnt);
+ vs->inline_sg_cnt,
+ GFP_KERNEL_ACCOUNT);
if (!tv_cmd->prot_sgl) {
pr_err("Unable to allocate tv_cmd->prot_sgl\n");
goto out;
@@ -2101,7 +2104,7 @@ vhost_scsi_set_endpoint(struct vhost_scsi *vs,
}
len = sizeof(vs_tpg[0]) * VHOST_SCSI_MAX_TARGET;
- vs_tpg = kzalloc(len, GFP_KERNEL);
+ vs_tpg = kzalloc(len, GFP_KERNEL_ACCOUNT);
if (!vs_tpg) {
ret = -ENOMEM;
goto out;
@@ -2380,7 +2383,7 @@ static int vhost_scsi_open(struct inode *inode, struct file *f)
struct vhost_virtqueue **vqs;
int r = -ENOMEM, i, nvqs;
- vs = kvzalloc_obj(*vs);
+ vs = kvzalloc_obj(*vs, GFP_KERNEL_ACCOUNT);
if (!vs)
goto err_vs;
vs->inline_sg_cnt = vhost_scsi_inline_sg_cnt;
@@ -2389,15 +2392,15 @@ static int vhost_scsi_open(struct inode *inode, struct file *f)
nvqs += VHOST_SCSI_VQ_IO;
vs->old_inflight = kmalloc_objs(*vs->old_inflight, nvqs,
- GFP_KERNEL | __GFP_ZERO);
+ GFP_KERNEL_ACCOUNT | __GFP_ZERO);
if (!vs->old_inflight)
goto err_inflight;
- vs->vqs = kvzalloc_objs(*vs->vqs, nvqs);
+ vs->vqs = kvzalloc_objs(*vs->vqs, nvqs, GFP_KERNEL_ACCOUNT);
if (!vs->vqs)
goto err_vqs;
- vqs = kmalloc_objs(*vqs, nvqs);
+ vqs = kmalloc_objs(*vqs, nvqs, GFP_KERNEL_ACCOUNT);
if (!vqs)
goto err_local_vqs;
diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
index c729efd108d0..9a5bd454fe7b 100644
--- a/drivers/vhost/vdpa.c
+++ b/drivers/vhost/vdpa.c
@@ -120,7 +120,7 @@ static struct vhost_vdpa_as *vhost_vdpa_alloc_as(struct vhost_vdpa *v, u32 asid)
if (max_iotlb_entries <= 0)
return NULL;
- as = kmalloc_obj(*as);
+ as = kmalloc_obj(*as, GFP_KERNEL_ACCOUNT);
if (!as)
return NULL;
@@ -385,7 +385,7 @@ static long vhost_vdpa_get_config(struct vhost_vdpa *v,
return -EFAULT;
if (vhost_vdpa_config_validate(v, &config))
return -EINVAL;
- buf = kvzalloc(config.len, GFP_KERNEL);
+ buf = kvzalloc(config.len, GFP_KERNEL_ACCOUNT);
if (!buf)
return -ENOMEM;
@@ -1159,7 +1159,7 @@ static int vhost_vdpa_va_map(struct vhost_vdpa *v,
!(vma->vm_flags & (VM_IO | VM_PFNMAP))))
goto next;
- map_file = kzalloc_obj(*map_file);
+ map_file = kzalloc_obj(*map_file, GFP_KERNEL_ACCOUNT);
if (!map_file) {
ret = -ENOMEM;
break;
@@ -1521,7 +1521,7 @@ static int vhost_vdpa_open(struct inode *inode, struct file *filep)
if (r)
goto err;
- vqs = kmalloc_objs(*vqs, nvqs);
+ vqs = kmalloc_objs(*vqs, nvqs, GFP_KERNEL_ACCOUNT);
if (!vqs) {
r = -ENOMEM;
goto err;
@@ -1689,7 +1689,8 @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa)
(vdpa->ngroups > 1 || vdpa->nas > 1))
return -EOPNOTSUPP;
- v = kzalloc_obj(*v, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
+ v = kzalloc_obj(*v, GFP_KERNEL | __GFP_RETRY_MAYFAIL |
+ __GFP_ACCOUNT);
if (!v)
return -ENOMEM;
@@ -1711,7 +1712,8 @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa)
v->dev.release = vhost_vdpa_release_dev;
v->dev.parent = &vdpa->dev;
v->dev.devt = MKDEV(MAJOR(vhost_vdpa_major), minor);
- v->vqs = kmalloc_objs(struct vhost_virtqueue, v->nvqs);
+ v->vqs = kmalloc_objs(struct vhost_virtqueue, v->nvqs,
+ GFP_KERNEL_ACCOUNT);
if (!v->vqs) {
r = -ENOMEM;
goto err;
diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
index 2d0238aacc6a..f62ff9e5aa24 100644
--- a/drivers/vhost/vhost.c
+++ b/drivers/vhost/vhost.c
@@ -526,10 +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->nheads = kmalloc_objs(*vq->nheads, 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_objs(*vq->nheads, dev->iov_limit,
+ GFP_KERNEL_ACCOUNT);
if (!vq->indirect || !vq->log || !vq->heads || !vq->nheads)
goto err_nomem;
}
@@ -2052,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;
@@ -3414,7 +3419,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;
diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c
index 03a3b833640d..b31262a2b11f 100644
--- a/drivers/vhost/vsock.c
+++ b/drivers/vhost/vsock.c
@@ -719,11 +719,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;
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v3] vhost: account the per-device kernel allocations to memcg
2026-12-03 12:01 [PATCH v3] vhost: account the per-device kernel allocations to memcg Tao Cui
@ 2026-12-03 12:07 ` Michael S. Tsirkin
2026-12-03 12:34 ` Tao Cui
2026-12-03 12:45 ` [PATCH v4] " Tao Cui
1 sibling, 1 reply; 5+ messages in thread
From: Michael S. Tsirkin @ 2026-12-03 12:07 UTC (permalink / raw)
To: Tao Cui
Cc: Jason Wang, Eugenio Pérez, Stefan Hajnoczi,
Stefano Garzarella, Bobby Eshleman, Mike Christie,
Andrey Drobyshev, virtualization, kvm, linux-mm, akpm, cuitao
On Thu, Dec 03, 2026 at 08:01:06PM +0800, Tao Cui wrote:
> From: Tao Cui <cuitao@kylinos.cn>
>
> Everything except the worker struct is allocated with plain GFP:
> 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, the vhost_net/vhost_vsock device
> structs with their fallback to vmalloc, the vhost-scsi device struct
> and its vq arrays at open, the per-vq tag bitmap and command pools
> sized by max_cmds and inline_sg_cnt, the target array at
> VHOST_SCSI_SET_ENDPOINT, the vhost-vdpa device struct and vq array,
> the per-asid allocations, the mapping file, and the GET_CONFIG buffer
> sized by config.len. The worker has been accounted since the
> vhost_task conversion.
>
> A container that can open /dev/vhost-net, /dev/vhost-vsock,
> /dev/vhost-scsi or /dev/vhost-vdpa, which device plugins hand out
> with passthrough, holds roughly 150KB of resident kernel memory per
> device after SET_OWNER, and a vhost-scsi device holds a few hundred
> KB more once its per-vq command pools are built. Charge the rest the
> same way the worker already is. Per-command allocations in the
> request path, the per-entry IOTLB map nodes, and vhost_iotlb_alloc(),
> which drivers/vdpa also calls, stay uncharged.
>
> Acked-by: Michael S. Tsirkin <mst@redhat.com>
> Acked-by: Bobby Eshleman <bobbyeshleman@meta.com>
> Signed-off-by: Tao Cui <cuitao@kylinos.cn>
what tree is this against?
error: sha1 information is lacking or useless (drivers/vhost/net.c).
> ---
> Changes in v3:
> - extend the accounting to the vhost-scsi and vhost-vdpa backends,
> whose allocations follow the same per-device pattern, per Stefano's
> review on v2
> - rebased on next-20261202; the net/vsock/vhost.c hunks are identical
> to v2, carrying Michael's and Bobby's acks from the earlier versions
> Link to v2: https://lore.kernel.org/r/20261120054517.526275-1-cui.tao@linux.dev
>
> Changes in v2:
> - rebased on next-20261118, where the vq->nheads allocation now uses
> kmalloc_objs(); the flag addition follows the new form. No
> functional change. Carries Michael's Acked-by from v1.
> Link to v1: https://lore.kernel.org/r/20261106131829.3228926-4-cui.tao@linux.dev
> ---
> drivers/vhost/net.c | 14 ++++++++------
> drivers/vhost/scsi.c | 23 +++++++++++++----------
> drivers/vhost/vdpa.c | 14 ++++++++------
> drivers/vhost/vhost.c | 17 +++++++++++------
> drivers/vhost/vsock.c | 5 +++--
> 5 files changed, 43 insertions(+), 30 deletions(-)
>
> diff --git a/drivers/vhost/net.c b/drivers/vhost/net.c
> index 2adfe3678f76..36f76327610b 100644
> --- a/drivers/vhost/net.c
> +++ b/drivers/vhost/net.c
> @@ -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;
> }
> @@ -1340,17 +1341,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);
> @@ -1358,7 +1360,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);
> diff --git a/drivers/vhost/scsi.c b/drivers/vhost/scsi.c
> index 8beeff06a7a7..ca1bbb95f4a5 100644
> --- a/drivers/vhost/scsi.c
> +++ b/drivers/vhost/scsi.c
> @@ -2020,18 +2020,19 @@ static int vhost_scsi_setup_vq_cmds(struct vhost_virtqueue *vq, int max_cmds)
> if (svq->scsi_cmds)
> return 0;
>
> - if (sbitmap_init_node(&svq->scsi_tags, max_cmds, -1, GFP_KERNEL,
> + if (sbitmap_init_node(&svq->scsi_tags, max_cmds, -1, GFP_KERNEL_ACCOUNT,
> NUMA_NO_NODE, false, true))
> return -ENOMEM;
> svq->max_cmds = max_cmds;
>
> - svq->scsi_cmds = kzalloc_objs(*tv_cmd, max_cmds);
> + svq->scsi_cmds = kzalloc_objs(*tv_cmd, max_cmds, GFP_KERNEL_ACCOUNT);
> if (!svq->scsi_cmds) {
> sbitmap_free(&svq->scsi_tags);
> return -ENOMEM;
> }
>
> - svq->upages = kzalloc_objs(struct page *, VHOST_SCSI_PREALLOC_UPAGES);
> + svq->upages = kzalloc_objs(struct page *, VHOST_SCSI_PREALLOC_UPAGES,
> + GFP_KERNEL_ACCOUNT);
> if (!svq->upages)
> goto out;
>
> @@ -2040,7 +2041,8 @@ static int vhost_scsi_setup_vq_cmds(struct vhost_virtqueue *vq, int max_cmds)
>
> if (vs->inline_sg_cnt) {
> tv_cmd->sgl = kzalloc_objs(struct scatterlist,
> - vs->inline_sg_cnt);
> + vs->inline_sg_cnt,
> + GFP_KERNEL_ACCOUNT);
> if (!tv_cmd->sgl) {
> pr_err("Unable to allocate tv_cmd->sgl\n");
> goto out;
> @@ -2050,7 +2052,8 @@ static int vhost_scsi_setup_vq_cmds(struct vhost_virtqueue *vq, int max_cmds)
> if (vhost_has_feature(vq, VIRTIO_SCSI_F_T10_PI) &&
> vs->inline_sg_cnt) {
> tv_cmd->prot_sgl = kzalloc_objs(struct scatterlist,
> - vs->inline_sg_cnt);
> + vs->inline_sg_cnt,
> + GFP_KERNEL_ACCOUNT);
> if (!tv_cmd->prot_sgl) {
> pr_err("Unable to allocate tv_cmd->prot_sgl\n");
> goto out;
> @@ -2101,7 +2104,7 @@ vhost_scsi_set_endpoint(struct vhost_scsi *vs,
> }
>
> len = sizeof(vs_tpg[0]) * VHOST_SCSI_MAX_TARGET;
> - vs_tpg = kzalloc(len, GFP_KERNEL);
> + vs_tpg = kzalloc(len, GFP_KERNEL_ACCOUNT);
> if (!vs_tpg) {
> ret = -ENOMEM;
> goto out;
> @@ -2380,7 +2383,7 @@ static int vhost_scsi_open(struct inode *inode, struct file *f)
> struct vhost_virtqueue **vqs;
> int r = -ENOMEM, i, nvqs;
>
> - vs = kvzalloc_obj(*vs);
> + vs = kvzalloc_obj(*vs, GFP_KERNEL_ACCOUNT);
> if (!vs)
> goto err_vs;
> vs->inline_sg_cnt = vhost_scsi_inline_sg_cnt;
> @@ -2389,15 +2392,15 @@ static int vhost_scsi_open(struct inode *inode, struct file *f)
> nvqs += VHOST_SCSI_VQ_IO;
>
> vs->old_inflight = kmalloc_objs(*vs->old_inflight, nvqs,
> - GFP_KERNEL | __GFP_ZERO);
> + GFP_KERNEL_ACCOUNT | __GFP_ZERO);
> if (!vs->old_inflight)
> goto err_inflight;
>
> - vs->vqs = kvzalloc_objs(*vs->vqs, nvqs);
> + vs->vqs = kvzalloc_objs(*vs->vqs, nvqs, GFP_KERNEL_ACCOUNT);
> if (!vs->vqs)
> goto err_vqs;
>
> - vqs = kmalloc_objs(*vqs, nvqs);
> + vqs = kmalloc_objs(*vqs, nvqs, GFP_KERNEL_ACCOUNT);
> if (!vqs)
> goto err_local_vqs;
>
> diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
> index c729efd108d0..9a5bd454fe7b 100644
> --- a/drivers/vhost/vdpa.c
> +++ b/drivers/vhost/vdpa.c
> @@ -120,7 +120,7 @@ static struct vhost_vdpa_as *vhost_vdpa_alloc_as(struct vhost_vdpa *v, u32 asid)
> if (max_iotlb_entries <= 0)
> return NULL;
>
> - as = kmalloc_obj(*as);
> + as = kmalloc_obj(*as, GFP_KERNEL_ACCOUNT);
> if (!as)
> return NULL;
>
> @@ -385,7 +385,7 @@ static long vhost_vdpa_get_config(struct vhost_vdpa *v,
> return -EFAULT;
> if (vhost_vdpa_config_validate(v, &config))
> return -EINVAL;
> - buf = kvzalloc(config.len, GFP_KERNEL);
> + buf = kvzalloc(config.len, GFP_KERNEL_ACCOUNT);
> if (!buf)
> return -ENOMEM;
>
> @@ -1159,7 +1159,7 @@ static int vhost_vdpa_va_map(struct vhost_vdpa *v,
> !(vma->vm_flags & (VM_IO | VM_PFNMAP))))
> goto next;
>
> - map_file = kzalloc_obj(*map_file);
> + map_file = kzalloc_obj(*map_file, GFP_KERNEL_ACCOUNT);
> if (!map_file) {
> ret = -ENOMEM;
> break;
> @@ -1521,7 +1521,7 @@ static int vhost_vdpa_open(struct inode *inode, struct file *filep)
> if (r)
> goto err;
>
> - vqs = kmalloc_objs(*vqs, nvqs);
> + vqs = kmalloc_objs(*vqs, nvqs, GFP_KERNEL_ACCOUNT);
> if (!vqs) {
> r = -ENOMEM;
> goto err;
> @@ -1689,7 +1689,8 @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa)
> (vdpa->ngroups > 1 || vdpa->nas > 1))
> return -EOPNOTSUPP;
>
> - v = kzalloc_obj(*v, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
> + v = kzalloc_obj(*v, GFP_KERNEL | __GFP_RETRY_MAYFAIL |
> + __GFP_ACCOUNT);
> if (!v)
> return -ENOMEM;
>
> @@ -1711,7 +1712,8 @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa)
> v->dev.release = vhost_vdpa_release_dev;
> v->dev.parent = &vdpa->dev;
> v->dev.devt = MKDEV(MAJOR(vhost_vdpa_major), minor);
> - v->vqs = kmalloc_objs(struct vhost_virtqueue, v->nvqs);
> + v->vqs = kmalloc_objs(struct vhost_virtqueue, v->nvqs,
> + GFP_KERNEL_ACCOUNT);
> if (!v->vqs) {
> r = -ENOMEM;
> goto err;
> diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
> index 2d0238aacc6a..f62ff9e5aa24 100644
> --- a/drivers/vhost/vhost.c
> +++ b/drivers/vhost/vhost.c
> @@ -526,10 +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->nheads = kmalloc_objs(*vq->nheads, 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_objs(*vq->nheads, dev->iov_limit,
> + GFP_KERNEL_ACCOUNT);
> if (!vq->indirect || !vq->log || !vq->heads || !vq->nheads)
> goto err_nomem;
> }
> @@ -2052,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;
>
> @@ -3414,7 +3419,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;
>
> diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c
> index 03a3b833640d..b31262a2b11f 100644
> --- a/drivers/vhost/vsock.c
> +++ b/drivers/vhost/vsock.c
> @@ -719,11 +719,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;
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v3] vhost: account the per-device kernel allocations to memcg
2026-12-03 12:07 ` Michael S. Tsirkin
@ 2026-12-03 12:34 ` Tao Cui
2026-12-03 12:40 ` Michael S. Tsirkin
0 siblings, 1 reply; 5+ messages in thread
From: Tao Cui @ 2026-12-03 12:34 UTC (permalink / raw)
To: Michael S. Tsirkin
Cc: cui.tao, Jason Wang, Eugenio Pérez, Stefan Hajnoczi,
Stefano Garzarella, Bobby Eshleman, Mike Christie,
Andrey Drobyshev, virtualization, kvm, linux-mm, akpm, cuitao
Hello, Michael
在 2026/12/3 20:07, Michael S. Tsirkin 写道:
> On Thu, Dec 03, 2026 at 08:01:06PM +0800, Tao Cui wrote:
>> From: Tao Cui <cuitao@kylinos.cn>
>>
>> Everything except the worker struct is allocated with plain GFP:
>> 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, the vhost_net/vhost_vsock device
>> structs with their fallback to vmalloc, the vhost-scsi device struct
>> and its vq arrays at open, the per-vq tag bitmap and command pools
>> sized by max_cmds and inline_sg_cnt, the target array at
>> VHOST_SCSI_SET_ENDPOINT, the vhost-vdpa device struct and vq array,
>> the per-asid allocations, the mapping file, and the GET_CONFIG buffer
>> sized by config.len. The worker has been accounted since the
>> vhost_task conversion.
>>
>> A container that can open /dev/vhost-net, /dev/vhost-vsock,
>> /dev/vhost-scsi or /dev/vhost-vdpa, which device plugins hand out
>> with passthrough, holds roughly 150KB of resident kernel memory per
>> device after SET_OWNER, and a vhost-scsi device holds a few hundred
>> KB more once its per-vq command pools are built. Charge the rest the
>> same way the worker already is. Per-command allocations in the
>> request path, the per-entry IOTLB map nodes, and vhost_iotlb_alloc(),
>> which drivers/vdpa also calls, stay uncharged.
>>
>> Acked-by: Michael S. Tsirkin <mst@redhat.com>
>> Acked-by: Bobby Eshleman <bobbyeshleman@meta.com>
>> Signed-off-by: Tao Cui <cuitao@kylinos.cn>
>
>
> what tree is this against?
>
> error: sha1 information is lacking or useless (drivers/vhost/net.c).
>
next-20261202, it's under the --- in the changelog, easy to miss,
sorry.
Both sides have moved since then: your tree got the used-ring batch
revert, the busy poll fix and the vdpa iotlb changes, while -next
picked up the vsock guest_cid annotations, the mmap_prepare conversion
and the kmalloc_obj refresh that turned the vq->nheads allocation into
kmalloc_array(). The only one that touches this patch is the nheads
one, v4 keeps your kmalloc_array() form and just adds the flag.
v4 against vhost.git linux-next follows in a minute.
Thanks,
Tao
>
>
>> ---
>> Changes in v3:
>> - extend the accounting to the vhost-scsi and vhost-vdpa backends,
>> whose allocations follow the same per-device pattern, per Stefano's
>> review on v2
>> - rebased on next-20261202; the net/vsock/vhost.c hunks are identical
>> to v2, carrying Michael's and Bobby's acks from the earlier versions
>> Link to v2: https://lore.kernel.org/r/20261120054517.526275-1-cui.tao@linux.dev
>>
>> Changes in v2:
>> - rebased on next-20261118, where the vq->nheads allocation now uses
>> kmalloc_objs(); the flag addition follows the new form. No
>> functional change. Carries Michael's Acked-by from v1.
>> Link to v1: https://lore.kernel.org/r/20261106131829.3228926-4-cui.tao@linux.dev
>> ---
>> drivers/vhost/net.c | 14 ++++++++------
>> drivers/vhost/scsi.c | 23 +++++++++++++----------
>> drivers/vhost/vdpa.c | 14 ++++++++------
>> drivers/vhost/vhost.c | 17 +++++++++++------
>> drivers/vhost/vsock.c | 5 +++--
>> 5 files changed, 43 insertions(+), 30 deletions(-)
>>
>> diff --git a/drivers/vhost/net.c b/drivers/vhost/net.c
>> index 2adfe3678f76..36f76327610b 100644
>> --- a/drivers/vhost/net.c
>> +++ b/drivers/vhost/net.c
>> @@ -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;
>> }
>> @@ -1340,17 +1341,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);
>> @@ -1358,7 +1360,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);
>> diff --git a/drivers/vhost/scsi.c b/drivers/vhost/scsi.c
>> index 8beeff06a7a7..ca1bbb95f4a5 100644
>> --- a/drivers/vhost/scsi.c
>> +++ b/drivers/vhost/scsi.c
>> @@ -2020,18 +2020,19 @@ static int vhost_scsi_setup_vq_cmds(struct vhost_virtqueue *vq, int max_cmds)
>> if (svq->scsi_cmds)
>> return 0;
>>
>> - if (sbitmap_init_node(&svq->scsi_tags, max_cmds, -1, GFP_KERNEL,
>> + if (sbitmap_init_node(&svq->scsi_tags, max_cmds, -1, GFP_KERNEL_ACCOUNT,
>> NUMA_NO_NODE, false, true))
>> return -ENOMEM;
>> svq->max_cmds = max_cmds;
>>
>> - svq->scsi_cmds = kzalloc_objs(*tv_cmd, max_cmds);
>> + svq->scsi_cmds = kzalloc_objs(*tv_cmd, max_cmds, GFP_KERNEL_ACCOUNT);
>> if (!svq->scsi_cmds) {
>> sbitmap_free(&svq->scsi_tags);
>> return -ENOMEM;
>> }
>>
>> - svq->upages = kzalloc_objs(struct page *, VHOST_SCSI_PREALLOC_UPAGES);
>> + svq->upages = kzalloc_objs(struct page *, VHOST_SCSI_PREALLOC_UPAGES,
>> + GFP_KERNEL_ACCOUNT);
>> if (!svq->upages)
>> goto out;
>>
>> @@ -2040,7 +2041,8 @@ static int vhost_scsi_setup_vq_cmds(struct vhost_virtqueue *vq, int max_cmds)
>>
>> if (vs->inline_sg_cnt) {
>> tv_cmd->sgl = kzalloc_objs(struct scatterlist,
>> - vs->inline_sg_cnt);
>> + vs->inline_sg_cnt,
>> + GFP_KERNEL_ACCOUNT);
>> if (!tv_cmd->sgl) {
>> pr_err("Unable to allocate tv_cmd->sgl\n");
>> goto out;
>> @@ -2050,7 +2052,8 @@ static int vhost_scsi_setup_vq_cmds(struct vhost_virtqueue *vq, int max_cmds)
>> if (vhost_has_feature(vq, VIRTIO_SCSI_F_T10_PI) &&
>> vs->inline_sg_cnt) {
>> tv_cmd->prot_sgl = kzalloc_objs(struct scatterlist,
>> - vs->inline_sg_cnt);
>> + vs->inline_sg_cnt,
>> + GFP_KERNEL_ACCOUNT);
>> if (!tv_cmd->prot_sgl) {
>> pr_err("Unable to allocate tv_cmd->prot_sgl\n");
>> goto out;
>> @@ -2101,7 +2104,7 @@ vhost_scsi_set_endpoint(struct vhost_scsi *vs,
>> }
>>
>> len = sizeof(vs_tpg[0]) * VHOST_SCSI_MAX_TARGET;
>> - vs_tpg = kzalloc(len, GFP_KERNEL);
>> + vs_tpg = kzalloc(len, GFP_KERNEL_ACCOUNT);
>> if (!vs_tpg) {
>> ret = -ENOMEM;
>> goto out;
>> @@ -2380,7 +2383,7 @@ static int vhost_scsi_open(struct inode *inode, struct file *f)
>> struct vhost_virtqueue **vqs;
>> int r = -ENOMEM, i, nvqs;
>>
>> - vs = kvzalloc_obj(*vs);
>> + vs = kvzalloc_obj(*vs, GFP_KERNEL_ACCOUNT);
>> if (!vs)
>> goto err_vs;
>> vs->inline_sg_cnt = vhost_scsi_inline_sg_cnt;
>> @@ -2389,15 +2392,15 @@ static int vhost_scsi_open(struct inode *inode, struct file *f)
>> nvqs += VHOST_SCSI_VQ_IO;
>>
>> vs->old_inflight = kmalloc_objs(*vs->old_inflight, nvqs,
>> - GFP_KERNEL | __GFP_ZERO);
>> + GFP_KERNEL_ACCOUNT | __GFP_ZERO);
>> if (!vs->old_inflight)
>> goto err_inflight;
>>
>> - vs->vqs = kvzalloc_objs(*vs->vqs, nvqs);
>> + vs->vqs = kvzalloc_objs(*vs->vqs, nvqs, GFP_KERNEL_ACCOUNT);
>> if (!vs->vqs)
>> goto err_vqs;
>>
>> - vqs = kmalloc_objs(*vqs, nvqs);
>> + vqs = kmalloc_objs(*vqs, nvqs, GFP_KERNEL_ACCOUNT);
>> if (!vqs)
>> goto err_local_vqs;
>>
>> diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
>> index c729efd108d0..9a5bd454fe7b 100644
>> --- a/drivers/vhost/vdpa.c
>> +++ b/drivers/vhost/vdpa.c
>> @@ -120,7 +120,7 @@ static struct vhost_vdpa_as *vhost_vdpa_alloc_as(struct vhost_vdpa *v, u32 asid)
>> if (max_iotlb_entries <= 0)
>> return NULL;
>>
>> - as = kmalloc_obj(*as);
>> + as = kmalloc_obj(*as, GFP_KERNEL_ACCOUNT);
>> if (!as)
>> return NULL;
>>
>> @@ -385,7 +385,7 @@ static long vhost_vdpa_get_config(struct vhost_vdpa *v,
>> return -EFAULT;
>> if (vhost_vdpa_config_validate(v, &config))
>> return -EINVAL;
>> - buf = kvzalloc(config.len, GFP_KERNEL);
>> + buf = kvzalloc(config.len, GFP_KERNEL_ACCOUNT);
>> if (!buf)
>> return -ENOMEM;
>>
>> @@ -1159,7 +1159,7 @@ static int vhost_vdpa_va_map(struct vhost_vdpa *v,
>> !(vma->vm_flags & (VM_IO | VM_PFNMAP))))
>> goto next;
>>
>> - map_file = kzalloc_obj(*map_file);
>> + map_file = kzalloc_obj(*map_file, GFP_KERNEL_ACCOUNT);
>> if (!map_file) {
>> ret = -ENOMEM;
>> break;
>> @@ -1521,7 +1521,7 @@ static int vhost_vdpa_open(struct inode *inode, struct file *filep)
>> if (r)
>> goto err;
>>
>> - vqs = kmalloc_objs(*vqs, nvqs);
>> + vqs = kmalloc_objs(*vqs, nvqs, GFP_KERNEL_ACCOUNT);
>> if (!vqs) {
>> r = -ENOMEM;
>> goto err;
>> @@ -1689,7 +1689,8 @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa)
>> (vdpa->ngroups > 1 || vdpa->nas > 1))
>> return -EOPNOTSUPP;
>>
>> - v = kzalloc_obj(*v, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
>> + v = kzalloc_obj(*v, GFP_KERNEL | __GFP_RETRY_MAYFAIL |
>> + __GFP_ACCOUNT);
>> if (!v)
>> return -ENOMEM;
>>
>> @@ -1711,7 +1712,8 @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa)
>> v->dev.release = vhost_vdpa_release_dev;
>> v->dev.parent = &vdpa->dev;
>> v->dev.devt = MKDEV(MAJOR(vhost_vdpa_major), minor);
>> - v->vqs = kmalloc_objs(struct vhost_virtqueue, v->nvqs);
>> + v->vqs = kmalloc_objs(struct vhost_virtqueue, v->nvqs,
>> + GFP_KERNEL_ACCOUNT);
>> if (!v->vqs) {
>> r = -ENOMEM;
>> goto err;
>> diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
>> index 2d0238aacc6a..f62ff9e5aa24 100644
>> --- a/drivers/vhost/vhost.c
>> +++ b/drivers/vhost/vhost.c
>> @@ -526,10 +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->nheads = kmalloc_objs(*vq->nheads, 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_objs(*vq->nheads, dev->iov_limit,
>> + GFP_KERNEL_ACCOUNT);
>> if (!vq->indirect || !vq->log || !vq->heads || !vq->nheads)
>> goto err_nomem;
>> }
>> @@ -2052,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;
>>
>> @@ -3414,7 +3419,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;
>>
>> diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c
>> index 03a3b833640d..b31262a2b11f 100644
>> --- a/drivers/vhost/vsock.c
>> +++ b/drivers/vhost/vsock.c
>> @@ -719,11 +719,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;
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v3] vhost: account the per-device kernel allocations to memcg
2026-12-03 12:34 ` Tao Cui
@ 2026-12-03 12:40 ` Michael S. Tsirkin
0 siblings, 0 replies; 5+ messages in thread
From: Michael S. Tsirkin @ 2026-12-03 12:40 UTC (permalink / raw)
To: Tao Cui
Cc: Jason Wang, Eugenio Pérez, Stefan Hajnoczi,
Stefano Garzarella, Bobby Eshleman, Mike Christie,
Andrey Drobyshev, virtualization, kvm, linux-mm, akpm, cuitao
On Thu, Dec 03, 2026 at 08:34:04PM +0800, Tao Cui wrote:
> Hello, Michael
>
> 在 2026/12/3 20:07, Michael S. Tsirkin 写道:
> > On Thu, Dec 03, 2026 at 08:01:06PM +0800, Tao Cui wrote:
> >> From: Tao Cui <cuitao@kylinos.cn>
> >>
> >> Everything except the worker struct is allocated with plain GFP:
> >> 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, the vhost_net/vhost_vsock device
> >> structs with their fallback to vmalloc, the vhost-scsi device struct
> >> and its vq arrays at open, the per-vq tag bitmap and command pools
> >> sized by max_cmds and inline_sg_cnt, the target array at
> >> VHOST_SCSI_SET_ENDPOINT, the vhost-vdpa device struct and vq array,
> >> the per-asid allocations, the mapping file, and the GET_CONFIG buffer
> >> sized by config.len. The worker has been accounted since the
> >> vhost_task conversion.
> >>
> >> A container that can open /dev/vhost-net, /dev/vhost-vsock,
> >> /dev/vhost-scsi or /dev/vhost-vdpa, which device plugins hand out
> >> with passthrough, holds roughly 150KB of resident kernel memory per
> >> device after SET_OWNER, and a vhost-scsi device holds a few hundred
> >> KB more once its per-vq command pools are built. Charge the rest the
> >> same way the worker already is. Per-command allocations in the
> >> request path, the per-entry IOTLB map nodes, and vhost_iotlb_alloc(),
> >> which drivers/vdpa also calls, stay uncharged.
> >>
> >> Acked-by: Michael S. Tsirkin <mst@redhat.com>
> >> Acked-by: Bobby Eshleman <bobbyeshleman@meta.com>
> >> Signed-off-by: Tao Cui <cuitao@kylinos.cn>
> >
> >
> > what tree is this against?
> >
> > error: sha1 information is lacking or useless (drivers/vhost/net.c).
> >
> next-20261202, it's under the --- in the changelog, easy to miss,
> sorry.
>
> Both sides have moved since then: your tree got the used-ring batch
> revert, the busy poll fix and the vdpa iotlb changes, while -next
> picked up the vsock guest_cid annotations, the mmap_prepare conversion
> and the kmalloc_obj refresh that turned the vq->nheads allocation into
> kmalloc_array(). The only one that touches this patch is the nheads
> one, v4 keeps your kmalloc_array() form and just adds the flag.
>
> v4 against vhost.git linux-next follows in a minute.
>
> Thanks,
> Tao
Generally my advice is to base things on Linus's tree.
I can handle any conflicts, it's ok to give me something to do)
next is not a development tree at all, it's an integration tree.
Do not base patches off of that.
> >
> >
> >> ---
> >> Changes in v3:
> >> - extend the accounting to the vhost-scsi and vhost-vdpa backends,
> >> whose allocations follow the same per-device pattern, per Stefano's
> >> review on v2
> >> - rebased on next-20261202; the net/vsock/vhost.c hunks are identical
> >> to v2, carrying Michael's and Bobby's acks from the earlier versions
> >> Link to v2: https://lore.kernel.org/r/20261120054517.526275-1-cui.tao@linux.dev
> >>
> >> Changes in v2:
> >> - rebased on next-20261118, where the vq->nheads allocation now uses
> >> kmalloc_objs(); the flag addition follows the new form. No
> >> functional change. Carries Michael's Acked-by from v1.
> >> Link to v1: https://lore.kernel.org/r/20261106131829.3228926-4-cui.tao@linux.dev
> >> ---
> >> drivers/vhost/net.c | 14 ++++++++------
> >> drivers/vhost/scsi.c | 23 +++++++++++++----------
> >> drivers/vhost/vdpa.c | 14 ++++++++------
> >> drivers/vhost/vhost.c | 17 +++++++++++------
> >> drivers/vhost/vsock.c | 5 +++--
> >> 5 files changed, 43 insertions(+), 30 deletions(-)
> >>
> >> diff --git a/drivers/vhost/net.c b/drivers/vhost/net.c
> >> index 2adfe3678f76..36f76327610b 100644
> >> --- a/drivers/vhost/net.c
> >> +++ b/drivers/vhost/net.c
> >> @@ -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;
> >> }
> >> @@ -1340,17 +1341,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);
> >> @@ -1358,7 +1360,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);
> >> diff --git a/drivers/vhost/scsi.c b/drivers/vhost/scsi.c
> >> index 8beeff06a7a7..ca1bbb95f4a5 100644
> >> --- a/drivers/vhost/scsi.c
> >> +++ b/drivers/vhost/scsi.c
> >> @@ -2020,18 +2020,19 @@ static int vhost_scsi_setup_vq_cmds(struct vhost_virtqueue *vq, int max_cmds)
> >> if (svq->scsi_cmds)
> >> return 0;
> >>
> >> - if (sbitmap_init_node(&svq->scsi_tags, max_cmds, -1, GFP_KERNEL,
> >> + if (sbitmap_init_node(&svq->scsi_tags, max_cmds, -1, GFP_KERNEL_ACCOUNT,
> >> NUMA_NO_NODE, false, true))
> >> return -ENOMEM;
> >> svq->max_cmds = max_cmds;
> >>
> >> - svq->scsi_cmds = kzalloc_objs(*tv_cmd, max_cmds);
> >> + svq->scsi_cmds = kzalloc_objs(*tv_cmd, max_cmds, GFP_KERNEL_ACCOUNT);
> >> if (!svq->scsi_cmds) {
> >> sbitmap_free(&svq->scsi_tags);
> >> return -ENOMEM;
> >> }
> >>
> >> - svq->upages = kzalloc_objs(struct page *, VHOST_SCSI_PREALLOC_UPAGES);
> >> + svq->upages = kzalloc_objs(struct page *, VHOST_SCSI_PREALLOC_UPAGES,
> >> + GFP_KERNEL_ACCOUNT);
> >> if (!svq->upages)
> >> goto out;
> >>
> >> @@ -2040,7 +2041,8 @@ static int vhost_scsi_setup_vq_cmds(struct vhost_virtqueue *vq, int max_cmds)
> >>
> >> if (vs->inline_sg_cnt) {
> >> tv_cmd->sgl = kzalloc_objs(struct scatterlist,
> >> - vs->inline_sg_cnt);
> >> + vs->inline_sg_cnt,
> >> + GFP_KERNEL_ACCOUNT);
> >> if (!tv_cmd->sgl) {
> >> pr_err("Unable to allocate tv_cmd->sgl\n");
> >> goto out;
> >> @@ -2050,7 +2052,8 @@ static int vhost_scsi_setup_vq_cmds(struct vhost_virtqueue *vq, int max_cmds)
> >> if (vhost_has_feature(vq, VIRTIO_SCSI_F_T10_PI) &&
> >> vs->inline_sg_cnt) {
> >> tv_cmd->prot_sgl = kzalloc_objs(struct scatterlist,
> >> - vs->inline_sg_cnt);
> >> + vs->inline_sg_cnt,
> >> + GFP_KERNEL_ACCOUNT);
> >> if (!tv_cmd->prot_sgl) {
> >> pr_err("Unable to allocate tv_cmd->prot_sgl\n");
> >> goto out;
> >> @@ -2101,7 +2104,7 @@ vhost_scsi_set_endpoint(struct vhost_scsi *vs,
> >> }
> >>
> >> len = sizeof(vs_tpg[0]) * VHOST_SCSI_MAX_TARGET;
> >> - vs_tpg = kzalloc(len, GFP_KERNEL);
> >> + vs_tpg = kzalloc(len, GFP_KERNEL_ACCOUNT);
> >> if (!vs_tpg) {
> >> ret = -ENOMEM;
> >> goto out;
> >> @@ -2380,7 +2383,7 @@ static int vhost_scsi_open(struct inode *inode, struct file *f)
> >> struct vhost_virtqueue **vqs;
> >> int r = -ENOMEM, i, nvqs;
> >>
> >> - vs = kvzalloc_obj(*vs);
> >> + vs = kvzalloc_obj(*vs, GFP_KERNEL_ACCOUNT);
> >> if (!vs)
> >> goto err_vs;
> >> vs->inline_sg_cnt = vhost_scsi_inline_sg_cnt;
> >> @@ -2389,15 +2392,15 @@ static int vhost_scsi_open(struct inode *inode, struct file *f)
> >> nvqs += VHOST_SCSI_VQ_IO;
> >>
> >> vs->old_inflight = kmalloc_objs(*vs->old_inflight, nvqs,
> >> - GFP_KERNEL | __GFP_ZERO);
> >> + GFP_KERNEL_ACCOUNT | __GFP_ZERO);
> >> if (!vs->old_inflight)
> >> goto err_inflight;
> >>
> >> - vs->vqs = kvzalloc_objs(*vs->vqs, nvqs);
> >> + vs->vqs = kvzalloc_objs(*vs->vqs, nvqs, GFP_KERNEL_ACCOUNT);
> >> if (!vs->vqs)
> >> goto err_vqs;
> >>
> >> - vqs = kmalloc_objs(*vqs, nvqs);
> >> + vqs = kmalloc_objs(*vqs, nvqs, GFP_KERNEL_ACCOUNT);
> >> if (!vqs)
> >> goto err_local_vqs;
> >>
> >> diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
> >> index c729efd108d0..9a5bd454fe7b 100644
> >> --- a/drivers/vhost/vdpa.c
> >> +++ b/drivers/vhost/vdpa.c
> >> @@ -120,7 +120,7 @@ static struct vhost_vdpa_as *vhost_vdpa_alloc_as(struct vhost_vdpa *v, u32 asid)
> >> if (max_iotlb_entries <= 0)
> >> return NULL;
> >>
> >> - as = kmalloc_obj(*as);
> >> + as = kmalloc_obj(*as, GFP_KERNEL_ACCOUNT);
> >> if (!as)
> >> return NULL;
> >>
> >> @@ -385,7 +385,7 @@ static long vhost_vdpa_get_config(struct vhost_vdpa *v,
> >> return -EFAULT;
> >> if (vhost_vdpa_config_validate(v, &config))
> >> return -EINVAL;
> >> - buf = kvzalloc(config.len, GFP_KERNEL);
> >> + buf = kvzalloc(config.len, GFP_KERNEL_ACCOUNT);
> >> if (!buf)
> >> return -ENOMEM;
> >>
> >> @@ -1159,7 +1159,7 @@ static int vhost_vdpa_va_map(struct vhost_vdpa *v,
> >> !(vma->vm_flags & (VM_IO | VM_PFNMAP))))
> >> goto next;
> >>
> >> - map_file = kzalloc_obj(*map_file);
> >> + map_file = kzalloc_obj(*map_file, GFP_KERNEL_ACCOUNT);
> >> if (!map_file) {
> >> ret = -ENOMEM;
> >> break;
> >> @@ -1521,7 +1521,7 @@ static int vhost_vdpa_open(struct inode *inode, struct file *filep)
> >> if (r)
> >> goto err;
> >>
> >> - vqs = kmalloc_objs(*vqs, nvqs);
> >> + vqs = kmalloc_objs(*vqs, nvqs, GFP_KERNEL_ACCOUNT);
> >> if (!vqs) {
> >> r = -ENOMEM;
> >> goto err;
> >> @@ -1689,7 +1689,8 @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa)
> >> (vdpa->ngroups > 1 || vdpa->nas > 1))
> >> return -EOPNOTSUPP;
> >>
> >> - v = kzalloc_obj(*v, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
> >> + v = kzalloc_obj(*v, GFP_KERNEL | __GFP_RETRY_MAYFAIL |
> >> + __GFP_ACCOUNT);
> >> if (!v)
> >> return -ENOMEM;
> >>
> >> @@ -1711,7 +1712,8 @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa)
> >> v->dev.release = vhost_vdpa_release_dev;
> >> v->dev.parent = &vdpa->dev;
> >> v->dev.devt = MKDEV(MAJOR(vhost_vdpa_major), minor);
> >> - v->vqs = kmalloc_objs(struct vhost_virtqueue, v->nvqs);
> >> + v->vqs = kmalloc_objs(struct vhost_virtqueue, v->nvqs,
> >> + GFP_KERNEL_ACCOUNT);
> >> if (!v->vqs) {
> >> r = -ENOMEM;
> >> goto err;
> >> diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
> >> index 2d0238aacc6a..f62ff9e5aa24 100644
> >> --- a/drivers/vhost/vhost.c
> >> +++ b/drivers/vhost/vhost.c
> >> @@ -526,10 +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->nheads = kmalloc_objs(*vq->nheads, 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_objs(*vq->nheads, dev->iov_limit,
> >> + GFP_KERNEL_ACCOUNT);
> >> if (!vq->indirect || !vq->log || !vq->heads || !vq->nheads)
> >> goto err_nomem;
> >> }
> >> @@ -2052,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;
> >>
> >> @@ -3414,7 +3419,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;
> >>
> >> diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c
> >> index 03a3b833640d..b31262a2b11f 100644
> >> --- a/drivers/vhost/vsock.c
> >> +++ b/drivers/vhost/vsock.c
> >> @@ -719,11 +719,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;
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v4] vhost: account the per-device kernel allocations to memcg
2026-12-03 12:01 [PATCH v3] vhost: account the per-device kernel allocations to memcg Tao Cui
2026-12-03 12:07 ` Michael S. Tsirkin
@ 2026-12-03 12:45 ` Tao Cui
1 sibling, 0 replies; 5+ messages in thread
From: Tao Cui @ 2026-12-03 12:45 UTC (permalink / raw)
To: Michael S. Tsirkin
Cc: Jason Wang, Eugenio Pérez, Stefan Hajnoczi,
Stefano Garzarella, Bobby Eshleman, Mike Christie, virtualization,
kvm, linux-mm, cuitao, cui.tao
From: Tao Cui <cuitao@kylinos.cn>
Everything except the worker struct is allocated with plain GFP:
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, the vhost_net/vhost_vsock device
structs with their fallback to vmalloc, the vhost-scsi device struct
and its vq arrays at open, the per-vq tag bitmap and command pools
sized by max_cmds and inline_sg_cnt, the target array at
VHOST_SCSI_SET_ENDPOINT, the vhost-vdpa device struct and vq array,
the per-asid allocations, the mapping file, and the GET_CONFIG buffer
sized by config.len. The worker has been accounted since the
vhost_task conversion.
A container that can open /dev/vhost-net, /dev/vhost-vsock,
/dev/vhost-scsi or /dev/vhost-vdpa, which device plugins hand out
with passthrough, holds roughly 150KB of resident kernel memory per
device after SET_OWNER, and a vhost-scsi device holds a few hundred
KB more once its per-vq command pools are built. Charge the rest the
same way the worker already is. Per-command allocations in the
request path, the per-entry IOTLB map nodes, and vhost_iotlb_alloc(),
which drivers/vdpa also calls, stay uncharged.
Acked-by: Michael S. Tsirkin <mst@redhat.com>
Acked-by: Bobby Eshleman <bobbyeshleman@meta.com>
Signed-off-by: Tao Cui <cuitao@kylinos.cn>
---
Changes in v4:
- rebased on vhost.git
Changes in v3:
- extend the accounting to the vhost-scsi and vhost-vdpa backends,
whose allocations follow the same per-device pattern, per Stefano's
review on v2
- rebased on next-20261202; the net/vsock/vhost.c hunks are identical
to v2, carrying Michael's and Bobby's acks from the earlier versions
Link to v2: https://lore.kernel.org/r/20261120054517.526275-1-cui.tao@linux.dev
Changes in v2:
- rebased on next-20261118, where the vq->nheads allocation now uses
kmalloc_objs(); the flag addition follows the new form. No
functional change. Carries Michael's Acked-by from v1.
Link to v1: https://lore.kernel.org/r/20261106131829.3228926-4-cui.tao@linux.dev
---
drivers/vhost/net.c | 14 ++++++++------
drivers/vhost/scsi.c | 23 +++++++++++++----------
drivers/vhost/vdpa.c | 14 ++++++++------
drivers/vhost/vhost.c | 16 ++++++++++------
drivers/vhost/vsock.c | 5 +++--
5 files changed, 42 insertions(+), 30 deletions(-)
diff --git a/drivers/vhost/net.c b/drivers/vhost/net.c
index 4b325e771645..79e3cb641891 100644
--- a/drivers/vhost/net.c
+++ b/drivers/vhost/net.c
@@ -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);
diff --git a/drivers/vhost/scsi.c b/drivers/vhost/scsi.c
index 8beeff06a7a7..ca1bbb95f4a5 100644
--- a/drivers/vhost/scsi.c
+++ b/drivers/vhost/scsi.c
@@ -2020,18 +2020,19 @@ static int vhost_scsi_setup_vq_cmds(struct vhost_virtqueue *vq, int max_cmds)
if (svq->scsi_cmds)
return 0;
- if (sbitmap_init_node(&svq->scsi_tags, max_cmds, -1, GFP_KERNEL,
+ if (sbitmap_init_node(&svq->scsi_tags, max_cmds, -1, GFP_KERNEL_ACCOUNT,
NUMA_NO_NODE, false, true))
return -ENOMEM;
svq->max_cmds = max_cmds;
- svq->scsi_cmds = kzalloc_objs(*tv_cmd, max_cmds);
+ svq->scsi_cmds = kzalloc_objs(*tv_cmd, max_cmds, GFP_KERNEL_ACCOUNT);
if (!svq->scsi_cmds) {
sbitmap_free(&svq->scsi_tags);
return -ENOMEM;
}
- svq->upages = kzalloc_objs(struct page *, VHOST_SCSI_PREALLOC_UPAGES);
+ svq->upages = kzalloc_objs(struct page *, VHOST_SCSI_PREALLOC_UPAGES,
+ GFP_KERNEL_ACCOUNT);
if (!svq->upages)
goto out;
@@ -2040,7 +2041,8 @@ static int vhost_scsi_setup_vq_cmds(struct vhost_virtqueue *vq, int max_cmds)
if (vs->inline_sg_cnt) {
tv_cmd->sgl = kzalloc_objs(struct scatterlist,
- vs->inline_sg_cnt);
+ vs->inline_sg_cnt,
+ GFP_KERNEL_ACCOUNT);
if (!tv_cmd->sgl) {
pr_err("Unable to allocate tv_cmd->sgl\n");
goto out;
@@ -2050,7 +2052,8 @@ static int vhost_scsi_setup_vq_cmds(struct vhost_virtqueue *vq, int max_cmds)
if (vhost_has_feature(vq, VIRTIO_SCSI_F_T10_PI) &&
vs->inline_sg_cnt) {
tv_cmd->prot_sgl = kzalloc_objs(struct scatterlist,
- vs->inline_sg_cnt);
+ vs->inline_sg_cnt,
+ GFP_KERNEL_ACCOUNT);
if (!tv_cmd->prot_sgl) {
pr_err("Unable to allocate tv_cmd->prot_sgl\n");
goto out;
@@ -2101,7 +2104,7 @@ vhost_scsi_set_endpoint(struct vhost_scsi *vs,
}
len = sizeof(vs_tpg[0]) * VHOST_SCSI_MAX_TARGET;
- vs_tpg = kzalloc(len, GFP_KERNEL);
+ vs_tpg = kzalloc(len, GFP_KERNEL_ACCOUNT);
if (!vs_tpg) {
ret = -ENOMEM;
goto out;
@@ -2380,7 +2383,7 @@ static int vhost_scsi_open(struct inode *inode, struct file *f)
struct vhost_virtqueue **vqs;
int r = -ENOMEM, i, nvqs;
- vs = kvzalloc_obj(*vs);
+ vs = kvzalloc_obj(*vs, GFP_KERNEL_ACCOUNT);
if (!vs)
goto err_vs;
vs->inline_sg_cnt = vhost_scsi_inline_sg_cnt;
@@ -2389,15 +2392,15 @@ static int vhost_scsi_open(struct inode *inode, struct file *f)
nvqs += VHOST_SCSI_VQ_IO;
vs->old_inflight = kmalloc_objs(*vs->old_inflight, nvqs,
- GFP_KERNEL | __GFP_ZERO);
+ GFP_KERNEL_ACCOUNT | __GFP_ZERO);
if (!vs->old_inflight)
goto err_inflight;
- vs->vqs = kvzalloc_objs(*vs->vqs, nvqs);
+ vs->vqs = kvzalloc_objs(*vs->vqs, nvqs, GFP_KERNEL_ACCOUNT);
if (!vs->vqs)
goto err_vqs;
- vqs = kmalloc_objs(*vqs, nvqs);
+ vqs = kmalloc_objs(*vqs, nvqs, GFP_KERNEL_ACCOUNT);
if (!vqs)
goto err_local_vqs;
diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
index ac67e7eef7b1..9f2f98f8374f 100644
--- a/drivers/vhost/vdpa.c
+++ b/drivers/vhost/vdpa.c
@@ -120,7 +120,7 @@ static struct vhost_vdpa_as *vhost_vdpa_alloc_as(struct vhost_vdpa *v, u32 asid)
if (max_iotlb_entries <= 0)
return NULL;
- as = kmalloc_obj(*as);
+ as = kmalloc_obj(*as, GFP_KERNEL_ACCOUNT);
if (!as)
return NULL;
@@ -385,7 +385,7 @@ static long vhost_vdpa_get_config(struct vhost_vdpa *v,
return -EFAULT;
if (vhost_vdpa_config_validate(v, &config))
return -EINVAL;
- buf = kvzalloc(config.len, GFP_KERNEL);
+ buf = kvzalloc(config.len, GFP_KERNEL_ACCOUNT);
if (!buf)
return -ENOMEM;
@@ -1162,7 +1162,7 @@ static int vhost_vdpa_va_map(struct vhost_vdpa *v,
!(vma->vm_flags & (VM_IO | VM_PFNMAP))))
goto next;
- map_file = kzalloc_obj(*map_file);
+ map_file = kzalloc_obj(*map_file, GFP_KERNEL_ACCOUNT);
if (!map_file) {
ret = -ENOMEM;
break;
@@ -1524,7 +1524,7 @@ static int vhost_vdpa_open(struct inode *inode, struct file *filep)
if (r)
goto err;
- vqs = kmalloc_objs(*vqs, nvqs);
+ vqs = kmalloc_objs(*vqs, nvqs, GFP_KERNEL_ACCOUNT);
if (!vqs) {
r = -ENOMEM;
goto err;
@@ -1691,7 +1691,8 @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa)
(vdpa->ngroups > 1 || vdpa->nas > 1))
return -EOPNOTSUPP;
- v = kzalloc_obj(*v, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
+ v = kzalloc_obj(*v, GFP_KERNEL | __GFP_RETRY_MAYFAIL |
+ __GFP_ACCOUNT);
if (!v)
return -ENOMEM;
@@ -1713,7 +1714,8 @@ static int vhost_vdpa_probe(struct vdpa_device *vdpa)
v->dev.release = vhost_vdpa_release_dev;
v->dev.parent = &vdpa->dev;
v->dev.devt = MKDEV(MAJOR(vhost_vdpa_major), minor);
- v->vqs = kmalloc_objs(struct vhost_virtqueue, v->nvqs);
+ v->vqs = kmalloc_objs(struct vhost_virtqueue, v->nvqs,
+ GFP_KERNEL_ACCOUNT);
if (!v->vqs) {
r = -ENOMEM;
goto err;
diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
index a00151932e4e..662db163d4a9 100644
--- a/drivers/vhost/vhost.c
+++ b/drivers/vhost/vhost.c
@@ -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;
@@ -3424,7 +3428,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;
diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c
index 0cb87971d749..600daeb05e7b 100644
--- a/drivers/vhost/vsock.c
+++ b/drivers/vhost/vsock.c
@@ -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;
^ permalink raw reply related [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-12-03 12:45 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 12:01 [PATCH v3] vhost: account the per-device kernel allocations to memcg Tao Cui
2026-12-03 12:07 ` Michael S. Tsirkin
2026-12-03 12:34 ` Tao Cui
2026-12-03 12:40 ` Michael S. Tsirkin
2026-12-03 12:45 ` [PATCH v4] " Tao Cui
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox