From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.218 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796298314; b=r2nS/K6Kp/nst/5lxt45UetnZd6JSrkvNtpPfYlWP0v/A+pggbQtmJRzILNGSOqFkEe7 qJTIRmadg5/IltFYGi8DIalR9bSylmvizOpjGY+jmi1PWXCVB7WzOtGUdHTU9cUW+8VrY G/kXSNMlvT2pIIZ3G6RNa2wRMht5aPwIlwUL5nBnwwTIIRWNJulhn2IjYrTikac/bIO/G USxDhcJSC6nK7XEJUc0SlcK+uyoTWu8/56bjYrV+ROpa8VwOfzt9uOSvcmJEj9xu0rK1p PYplnQ/nV8V9D+3nKkCCPfrj4EQIoACYBA45bS2TTtN+htAOsDhvg7iT9/wJfeeoiTg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796298314; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:Message-ID:Date:MIME-Version:User-Agent: Cc:Subject:To:References:From:In-Reply-To:Content-Type: Content-Transfer-Encoding; bh=dnx6jUOTMRWu8VZCKGYeBLbjc17IEL0wPzThIvkj+M4=; b=ANhSAyaLeupQ041GYR36mmiePdvpWZCAVizub9YPEA0AeARoNYaxBQ56UBuwvdV7x8dE GREGn1QQwItO9AP3jeYefeEgfvYsdFMMpdTA4KGzW8lUmXv7htmOYET3ghDlV09NCXpRh QOaQhnwvANgOPUe6oxdmRNsuXb9ihpYcoRSkk1rgFUO1FCqmHyrf0osERIrfg5ZIwGm/L DjbLD4BwZ44ChxoUN+L4kW12A0+tRchi+7VW6nFVeS9p6tVEoB6Gztax8cF29/N9UtGtb iYXFvbKxkMrJvx+0InC0UvqaBYotrFbaTD0mAyYoWPffXDb3HymF+hf3xfU/zUco/tA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b="sSO9T/YZ"; arc=none smtp.remote-ip=91.218.175.218 Received: from mta0.migadu.com (out-218.mta0.migadu.com [91.218.175.218]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0A011606A32D for ; Thu, 3 Dec 2026 11:45:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 0A011606A32D Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=sSO9T/YZ X-Envelope-To: virtualization@lists.linux.dev DKIM-Signature: a=rsa-sha256; bh=4vPvAoB5PpByLr+l6ZqOLUtsx7dViW1nVWFz6y/tzHw=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796298310; v=1; x=1796903110; b=sSO9T/YZ8AzF2xzktN4wGIt/oiP9SZG9GaU6bjd8oWr8PfTIVcUXEpSB5wwAZV8kWhuJq32F YzprQummXBTKIZQpIdIjYR8wZNylGUvPr3CWR6doHXk9b2zupG9zHO2Qkv5sO8cyEy66DzKBJqa F7gUApkWOPkud3zulbl2OG8o= X-Envelope-To: virtualization@lists.linux.dev Received: by smtp.migadu.com with ESMTPS id 95852f3273da7d4a; Thu, 03 Dec 2026 11:45:09 +0000 X-Mizu-Trace-ID: 95852f3273da7d4a X-Migadu-Flow: FLOW_OUT Message-ID: <6b3e8e40-0ba0-484c-808a-d7b4c23335ad@linux.dev> Date: Thu, 3 Dec 2026 19:45:04 +0800 X-Mailing-List: virtualization@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Cc: cui.tao@linux.dev, "Michael S. Tsirkin" , Jason Wang , =?UTF-8?Q?Eugenio_P=C3=A9rez?= , Stefan Hajnoczi , Bobby Eshleman , Andrey Drobyshev , virtualization@lists.linux.dev, kvm@vger.kernel.org, linux-mm@kvack.org, akpm@linux-foundation.org, cuitao@kylinos.cn Subject: Re: [PATCH v2] vhost: account the per-device kernel allocations to memcg To: Stefano Garzarella References: <20261120054517.526275-1-cui.tao@linux.dev> From: Tao Cui In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Hello, Stefano, 在 2026/12/1 18:56, Stefano Garzarella 写道: > On Fri, Nov 20, 2026 at 01:45:17PM +0800, Tao Cui wrote: >> From: Tao Cui >> >> 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 > > Why not including also vhost-scsi and vhost-vdpa in this patch/series? > Oversight on my part, no deeper reason: I did net and vsock first and never went back over the rest of drivers/vhost. v3, following on the v2 thread, extends the accounting to both: device and vq structures, the scsi command pools and tag bitmaps, the target array set at endpoint, and the vdpa address spaces, mapping file and config read buffers. Thanks, Tao > Thanks, > Stefano > >> 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 >> Signed-off-by: Tao Cui >> --- >> 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/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 >>