🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #9173

[PATCH] vhost_task: do the return-to-userspace work between work items

#9173
From: Shakeel Butt
Date: Sat, 31 Oct 2026 18:10
73 lines
10991 bytes
A vhost task is a user worker: a thread of a user process that runs only
kernel code. It never returns to userspace, so it never runs the work
that is queued for the way back. resume_user_mode_work() does that work
when TIF_NOTIFY_RESUME is set. For a vhost task this means:

- memory.high is handled at the wrong time. When a charge goes over
  memory.high, the task counts the pages, and should reclaim, and sleep
  if needed, on its way back to userspace. A vhost task only reclaims
  when more than a batch of pages has built up and the charge can block.
  Then it reclaims and sleeps in the middle of the charge, maybe while
  it holds a lock. vhost-scsi, for example, can fault in guest memory
  while it holds vq->mutex. If the charges can't block, nothing is done
  at all, and the count keeps growing.

- Block cgroup throttling never runs. blkcg_schedule_throttle() asks for
  it, but only resume_user_mode_work() acts on it.

- Task work queued with TWA_RESUME, like a deferred fput() or a NUMA
  balancing scan, waits until the task gets a signal or exits.

io_uring workers had the same problem, and io_run_task_work() calls
resume_user_mode_work() for them. Do the same in vhost_task_fn(): if
TIF_NOTIFY_RESUME is set, call resume_user_mode_work() at the top of the
loop. That is between work items, no locks are held there, and the task
is running. Passing a NULL regs is fine, io_uring and KVM already do it.

This covers every user of vhost_task_create(): the vhost workers and
KVM's NX huge page recovery thread.

Tested in a VM with a version of tools/virtio/vhost_net_test that puts
each RX buffer on a new page, so the vhost worker faults in, and
charges, 256M while its cgroup has memory.high=64M. Before, all 285
calls to __mem_cgroup_handle_over_high() came from try_charge_memcg(),
inside handle_rx() with vq->mutex held. After, all 589 came from
vhost_task_fn(). memory.peak stayed at 64M, and the run took about the
same time (0.66s before, 0.67s after). A lockdep kernel showed no
warnings.

Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
---
 kernel/vhost_task.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/kernel/vhost_task.c b/kernel/vhost_task.c
index 3717885a5992..f0ab21aa2a1c 100644
--- a/kernel/vhost_task.c
+++ b/kernel/vhost_task.c
@@ -7,6 +7,7 @@
 #include <linux/sched/task.h>
 #include <linux/sched/vhost_task.h>
 #include <linux/sched/signal.h>
+#include <linux/resume_user_mode.h>
 
 enum vhost_task_flags {
 	VHOST_TASK_FLAGS_STOP,
@@ -38,6 +39,14 @@ static int vhost_task_fn(void *data)
 				break;
 		}
 
+		/*
+		 * A vhost task never returns to userspace. Do the work that
+		 * would run on the way back, like memory.high reclaim, here
+		 * between work items, while no locks are held.
+		 */
+		if (test_thread_flag(TIF_NOTIFY_RESUME))
+			resume_user_mode_work(NULL);
+
 		/* mb paired w/ vhost_task_stop */
 		set_current_state(TASK_INTERRUPTIBLE);
 
-- 
2.53.0-Meta
Technical Details
Message-ID: <20261031181004.4193627-1-shakeel.butt@linux.dev>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-354119-linux-kernel-netdev=dewy.ru

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-354119-linux-kernel-netdev=dewy.ru
From: shakeel.butt@linux.dev (Shakeel Butt)
Newsgroups: linux.kernel.netdev
Subject: [PATCH] vhost_task: do the return-to-userspace work between work items
Date: Sat, 31 Oct 2026 11:10:04 -0700
Organization: Usenet Network
Message-ID: <20261031181004.4193627-1-shakeel.butt@linux.dev>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="2519392"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:mD6A4ZcWmP3WUd9ll3KuipWAifY= sha256:uaccl6xbIWMrGKnnoL1Pj2yOEe8uUEcVl9OJS3dpfck=
	sha1:qqeLwc53mjBrOG2XEzfXeHQgNrs= sha256:j50I0bhaZrLGPubkv/AbZEFyIK+I2Tf12ZvDututxyo=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id D2242125CB8; Sat, 31 Oct 2026 21:11:48 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.05
X-Spam-Status: No, score=-3.05 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
	DKIM_VALID_AU=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.25,
	MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (1024-bit key)
	header.d=linux.dev
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id iu4V43g1xcbH;
	Sat, 31 Oct 2026 21:11:43 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-354119-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=linux.dev
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
	by mail.dewy.ru (Postfix) with ESMTPS id DDD34125CB6; Sat, 31 Oct 2026 21:11:33 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sea.lore.kernel.org (Postfix) with ESMTP id 493A6280AB; Sat, 31 Oct 2026 18:10:49 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1793470249;
	b=Sjad2sRMv/sOi+ivjpMkp3ArwIYBY1/GP3XJzr61NQvReVpK3+NmiyJAa2lqi+QLJvSm
	 SKWlRGY6dszh6RfMKGHylWGYDklfYrbu4TpiA7+58Pg8ZYIiQasy0unV/tsfGF3Z7ijua
	 oGHLshOO8jAys+OxV2a7hhV9wlPUA6J0+vTtkQGi0gE718+lgfX5BHYIGcGGtriaWq9bY
	 aqm0IXA6xybfiVOizbT2Xum5DzpKESFkFpnTd3PckABafus4sGHoFcCCiKs/rS5pUs6CC
	 0sfw//Ldg8NOPmkRTbzBwP3LqH6TWBOUVRNdzdiLG3gaepuqtPMKnJjWHNfA5hgNaeQ==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1793470249;
	h=X-Original-To:Received:DMARC-Filter:X-Envelope-To:DKIM-Signature:
	 X-Envelope-To:Received:X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:
	 Subject:Date:Message-ID:X-Mailer:X-Mailing-List:List-Id:List-Post:
	 List-Help:List-Subscribe:List-Unsubscribe:List-Owner:Precedence:
	 MIME-Version:Content-Transfer-Encoding;
	bh=gasBqSAnV3vmniUcbI9YXZHfc6XeNj1p84CcMezMQsQ=;
	b=tiCMU2GoZRr8BhIYCaADvvgZaXy/pUILcQKLAIrrwwvdWSAzFuyMuX154KT0M71BoUOG
	 GCgq3XgkU0zSSmIZCctxhmMNfQlyp3j83NuC6gX0pWR4Y6Cdw2pcCyunwzNwvaiY5A6zw
	 S1cjZknqmVsC4D3LlozMMrMWQmNjOPvdw/K9z1GsMcgA/dledUJXYlLlTzyUY3AaIUzbJ
	 BVyv77dCEfS4p+7LujydTbtdG2IrdqQokBC9W7ZQX6LMS94rg2//piqEhu5zaTe7/pQ6S
	 Ur2HM3b13oJCRv9fYdf9XxQYTOxvfUfdhLASxXa02xukPdg1LLAJLpOxsJ+U14PmBeg==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=kD69zA82;
	dmarc=pass header.from=linux.dev;
	spf=pass smtp.mailfrom=linux.dev;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 1F035606A322;
	Sat, 31 Oct 2026 18:10:49 +0000 (UTC)
X-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=kD69zA82
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.23
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1793470248;
	b=I+4489meFn5ZeJezGU4a8IZpT2cl44sKH/Voq/kBMdyslN75PM5vf4INudvsRPN7eJVk
	 9aNH+8t1BM6P8+x3cAQMiSsefIt5NVlZNrN0dZuF+qVQfYPSt+MY64cyYfzMkWfMQE/il
	 75Hb8SWGRC8dbiool1XnsALfftP4f7IucWHMtWjsNp3wjF1ocrLhAEmfWzTgRDJBRnNqf
	 +41yDkYI3Qa32k4vvujbgT/OZBWMLm2YfeMjSWqKSgvBZqohd+aRH9bW+39+IOkVuhQ8H
	 ZKPz2UmJ7l5SUszm/DsxeB9KnLj2Pxr5lslkD+/YeuaSQuZqspBSLXVomoRRjOJnKew==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1793470248;
	h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received:
	 X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-ID:
	 X-Mailer:MIME-Version:Content-Transfer-Encoding;
	bh=gasBqSAnV3vmniUcbI9YXZHfc6XeNj1p84CcMezMQsQ=;
	b=g6luenDZ102pysAgw8SsryY5eIWdfiMQRxK8g3Wsmcd2RlLzSH78Bq1Zo2d/P4clEDiw
	 JBjh28Wrg/VfrxsTamilsZJO1hwdEMh+nyFa+syKZwxEw7iJBTKR2u6oAMAd1C07Xvglg
	 Vgz3S3vXyjNe9WMWGAa9PDzxfnbZxeDhMkghmplhUk80mVN6/QQNWuKjshXEM9NLRBgmc
	 +UF1F//qHz3Z5O3oarbXIv4+c/nBK4CmuvfiojBIyh5my5ZYGGafBOtmRQitiyBW1TKfQ
	 1sSDEalttiHF7OmIKvxTWv5lc9KZnoklRlOVMr28T5D/8bCNbyq9AXTw6SrrQT9sOzg==
X-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=kD69zA82;
	arc=none smtp.remote-ip=95.215.58.23
X-Received: from mta1.migadu.com (out-23.mta1.migadu.com [95.215.58.23])
	(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 C6FC6606A321
	for <netdev@vger.kernel.org>; Sat, 31 Oct 2026 18:10:41 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C6FC6606A321
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev
X-Envelope-To: netdev@vger.kernel.org
X-DKIM-Signature: a=rsa-sha256; bh=HAspga3z+Ons/LXwua+up+NOY/bhAKhpJxJzMMgbKTU=;
 c=simple/simple; d=linux.dev;
 h=from:to:subject:date:message-id:mime-version:content-type; s=key1;
 t=1793470240; v=1; x=1794075040;
 b=kD69zA82aL13dIVOgPDMaa97S0xY3rSHZ8E/Nt5Cldac9PcgCiOR9fXGuQ/gLcn1zZ1DOVZ5
 efcLegUmq03LmVs/rvWgSK9kyRNcqbNmYEJ3rS0Ey0vB62JhJ9M/EPPq0gBUKtOTM6mUgEycmWr
 3Eb4YmOsPGShOSF5n3DGnfiQ=
X-Envelope-To: netdev@vger.kernel.org
X-Received: by smtp.migadu.com with ESMTPS id 5926407d61842773;
	Sat, 31 Oct 2026 18:10:39 +0000
X-Mizu-Trace-ID: 5926407d61842773
X-Migadu-Flow: FLOW_OUT
X-Cc: Meta kernel team <kernel-team@meta.com>,
	cgroups@vger.kernel.org,
	linux-mmc@vger.kernel.org,
	kvm@vger.kernel.org,
	virtualization@lists.linux.dev,
	netdev@vger.kernel.org,
	linux-kernel@vger.kernel.org
X-Mailer: git-send-email 2.53.0
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list