On Fri, Nov 20, 2026 at 01:45:17PM +0800, Tao Cui wrote:
> From: 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.
> 
> Acked-by: Michael S. Tsirkin <[email protected]>
> Signed-off-by: Tao Cui <[email protected]>
> ---
> 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/[email protected]
> 
>  drivers/vhost/net.c   | 14 ++++++++------
>  drivers/vhost/vhost.c | 17 +++++++++++------
>  drivers/vhost/vsock.c |  5 +++--
>  3 files changed, 22 insertions(+), 14 deletions(-)
> 
> diff --git a/drivers/vhost/net.c b/drivers/vhost/net.c
> index 435d8ed3851f..390858056d20 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/vhost.c b/drivers/vhost/vhost.c
> index dc973287567c..e8f9dce52aa7 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;
>  
> @@ -3407,7 +3412,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 88a310390eb0..2fa205967f8a 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;
> -- 
> 2.53.0
> 

The vsock bits look good to me!

Acked-by: Bobby Eshleman <[email protected]>

Reply via email to