Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Tao Cui" <cui.tao@linux.dev>, "Michael S. Tsirkin" <mst@redhat.com>
Cc: "Jason Wang" <jasowangio@gmail.com>,
	"Eugenio Pérez" <eperezma@redhat.com>,
	"Stefan Hajnoczi" <stefanha@redhat.com>,
	"Stefano Garzarella" <sgarzare@redhat.com>,
	"Bobby Eshleman" <bobbyeshleman@meta.com>,
	"Mike Christie" <michael.christie@oracle.com>,
	"Andrey Drobyshev" <andrey.drobyshev@virtuozzo.com>,
	virtualization@lists.linux.dev, kvm@vger.kernel.org,
	linux-mm@kvack.org, akpm@linux-foundation.org, cuitao@kylinos.cn
Subject: Re: [PATCH v3] vhost: account the per-device kernel allocations to memcg
Date: Fri, 04 Dec 2026 12:01:31 +0000	[thread overview]
Message-ID: <sashiko-outbox-249744@kernel.org> (raw)
In-Reply-To: <20261203120106.2607381-1-cui.tao@linux.dev>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] vhost/vdpa: __GFP_ACCOUNT in vhost_vdpa_probe() charges wrong
  memcg
- [Low] vhost/scsi: persistent per-vq cmd->tvc_log pool buffer left
  unaccounted
--

commit 96c8a4875392fddf28877578e29b8895152689ec
Author: Tao Cui <cuitao@kylinos.cn>

vhost: account the per-device kernel allocations to memcg

This commit adds __GFP_ACCOUNT / GFP_KERNEL_ACCOUNT to per-device
allocations across vhost core and the net, scsi, vdpa, and vsock
backends so resident kernel memory is charged to the owning memcg.

> diff --git a/drivers/vhost/scsi.c b/drivers/vhost/scsi.c
> index e44044449cef8..d70472a87eae4 100644
> --- a/drivers/vhost/scsi.c
> +++ b/drivers/vhost/scsi.c
> @@ -2000,18 +2000,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;
>  
> @@ -2020,7 +2021,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;
> @@ -2030,7 +2032,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;

[Severity: Low]
Should cmd->tvc_log in vhost_scsi_copy_cmd_log() also use
GFP_KERNEL_ACCOUNT?

drivers/vhost/scsi.c:vhost_scsi_copy_cmd_log() {
	if (!cmd->tvc_log)
		cmd->tvc_log = kmalloc_objs(*cmd->tvc_log, vq->dev->iov_limit);
	...
}

Although allocated lazily on the first logged command, cmd->tvc_log is
preserved across commands in vhost_scsi_get_cmd() alongside cmd->sgl and
cmd->prot_sgl, and freed with the per-vq pool in
vhost_scsi_destroy_vq_cmds().

Without accounting, enabling VHOST_F_LOG_ALL lets a container hold
16 KiB per command slot (2 MiB for a 128-entry virtqueue) outside its
memcg limit.

[ ... ]

> diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
> index c729efd108d07..9a5bd454fe7b5 100644
> --- a/drivers/vhost/vdpa.c
> +++ b/drivers/vhost/vdpa.c

[ ... ]

> @@ -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;

[Severity: Medium]
Won't accounting v and v->vqs in vhost_vdpa_probe() charge the wrong
memcg?

Unlike the other vhost backends that allocate device state on open(),
vhost_vdpa_probe() runs on bus probe and frees v and v->vqs only in
vhost_vdpa_release_dev() when the device is removed.

When a device is created from a non-root provisioning cgroup via
vdpa_nl_cmd_dev_add_set_doit(), v and v->vqs (~19 KiB per virtqueue) are
charged to that probing cgroup for the device's lifetime, pinning a
dying memcg if it exits and risking spurious -ENOMEM on probe.

Conversely, when probed in the root cgroup, containers opening
/dev/vhost-vdpa-N are never charged for them.  Keeping probe-time
allocations unaccounted, or moving v->vqs allocation to
vhost_vdpa_open() and vhost_vdpa_cleanup(), would avoid this.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203120106.2607381-1-cui.tao@linux.dev?part=1

      parent reply	other threads:[~2026-12-04 12:01 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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
2026-12-03 22:11   ` Mike Christie
2026-12-04 12:01 ` sashiko-bot [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-249744@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=andrey.drobyshev@virtuozzo.com \
    --cc=bobbyeshleman@meta.com \
    --cc=cui.tao@linux.dev \
    --cc=cuitao@kylinos.cn \
    --cc=eperezma@redhat.com \
    --cc=jasowangio@gmail.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=michael.christie@oracle.com \
    --cc=mst@redhat.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=sgarzare@redhat.com \
    --cc=stefanha@redhat.com \
    --cc=virtualization@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox