From nobody Wed Feb 11 04:00:57 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6318336AF5 for ; Sun, 2 Mar 2025 14:33:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740926009; cv=none; b=raG5F3qeyLODD40arinFM8BkQ0HVBqfOKCySODE+C+1WsHqnOj8gkCWVnA3y2lTQ4dGFWIKfvhXwG8cQjCC/NdTjpB902/urOg8y5euLgTSnGay+ZE+1mC+06/B4YO8jtdzvuCfJU1SbsKTrzy2kkwzIx8SPgst4a73wjSZPw34= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740926009; c=relaxed/simple; bh=C/xAItdObRhE+7CYDvFZUYp59fIC72o7p9syeQ2nR0M=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lk2fILgtyN//rNp4Z8JoxNro0Oq34mpG1hNge9qN95w/PGjWIVINq2Buqhftg9K+6CpiZ3ThIRNcNXaGz1ahjiOs8Z0/0y7JRhE5+D0p4U9BOMtWQSZLLtpDmCOvxTKkxRcw+pAO2LbBnFlyLS85dNXXwYsmR+p9INWLd3u04RQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=VEIrSbAO; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="VEIrSbAO" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740926006; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=xGq2Axvzt/J7qgw1KDrXhDAtkMcdMfmrQjoiox/FEqY=; b=VEIrSbAO5Nm75+lhFB+DhUlQEoFzZqnxpGKKFJPWm8wJ2OqAD9eoISGEJO0EfNrHEBaiNe bvPndHSXvIS0YF/la9VJTHTA/cwf9ZodTTRSetVjpXjjjKcbb6srt0qX4u7xIrXgxYHHEZ tymavuxHyflQUX+tsWdzacdiqHON4JY= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-649-8Mwhey9yN2umJaXpiBKHVQ-1; Sun, 02 Mar 2025 09:33:20 -0500 X-MC-Unique: 8Mwhey9yN2umJaXpiBKHVQ-1 X-Mimecast-MFC-AGG-ID: 8Mwhey9yN2umJaXpiBKHVQ_1740925999 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 098391944F04; Sun, 2 Mar 2025 14:33:19 +0000 (UTC) Received: from server.redhat.com (unknown [10.72.112.49]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 752F21800359; Sun, 2 Mar 2025 14:33:14 +0000 (UTC) From: Cindy Lu To: lulu@redhat.com, jasowang@redhat.com, mst@redhat.com, michael.christie@oracle.com, sgarzare@redhat.com, linux-kernel@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org Subject: [PATCH v7 1/8] vhost: Add a new parameter in vhost_dev to allow user select kthread Date: Sun, 2 Mar 2025 22:32:03 +0800 Message-ID: <20250302143259.1221569-2-lulu@redhat.com> In-Reply-To: <20250302143259.1221569-1-lulu@redhat.com> References: <20250302143259.1221569-1-lulu@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 Content-Type: text/plain; charset="utf-8" The vhost now uses vhost_task and workers as a child of the owner thread. While this aligns with containerization principles,it confuses some legacy userspace app, Therefore, we are reintroducing kthread API support. Introduce a new parameter to enable users to choose between kthread and task mode. Signed-off-by: Cindy Lu Tested-by: Lei Yang --- drivers/vhost/vhost.c | 1 + drivers/vhost/vhost.h | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index 63612faeab72..250dc43f1786 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -552,6 +552,7 @@ void vhost_dev_init(struct vhost_dev *dev, dev->byte_weight =3D byte_weight; dev->use_worker =3D use_worker; dev->msg_handler =3D msg_handler; + dev->inherit_owner =3D true; init_waitqueue_head(&dev->wait); INIT_LIST_HEAD(&dev->read_list); INIT_LIST_HEAD(&dev->pending_list); diff --git a/drivers/vhost/vhost.h b/drivers/vhost/vhost.h index bb75a292d50c..19bb94922a0e 100644 --- a/drivers/vhost/vhost.h +++ b/drivers/vhost/vhost.h @@ -176,6 +176,15 @@ struct vhost_dev { int byte_weight; struct xarray worker_xa; bool use_worker; + /* + * If inherit_owner is true we use vhost_tasks to create + * the worker so all settings/limits like cgroups, NPROC, + * scheduler, etc are inherited from the owner. If false, + * we use kthreads and only attach to the same cgroups + * as the owner for compat with older kernels. + * here we use true as default value + */ + bool inherit_owner; int (*msg_handler)(struct vhost_dev *dev, u32 asid, struct vhost_iotlb_msg *msg); }; --=20 2.45.0 From nobody Wed Feb 11 04:00:57 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6E05E1E5B96 for ; Sun, 2 Mar 2025 14:33:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740926021; cv=none; b=m8vppP316uqwUQVro8XTWF15Hjj+P5I/fRuIG7jHxCId1ob9uMaUIqp5krYb51hNyGQp4EA5RHFWHOI3Foh2i34a+I4y4r6LSw/OIP4hEMa1PShlqPY6qh0NE8jRA5XF3LJRch3eC23gZjWiXjJqAI/MGqp7HM26cND+aZC2i8I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740926021; c=relaxed/simple; bh=YUc7c9MXgNwcjHIY/6Aj5gIGWVEuIKPw42UQXK+Og6M=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=t5E8DPks4oTvx3Vns6+B6ZlD16VcYgRakBwr/ZKY1fIiE1OL5i9J3eoUZkROnR+v1HaYMHPHqknxuFiCjS183+6vmHjA6R3dHQGex1m7TB0NHAO1YGaf341d553cIW8LfgdQzlw0coSuLQJLwKI0xxnVVpaOpPF/qbV9oGcnGgc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=GDuP5Szr; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="GDuP5Szr" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740926019; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+Sc1dvTB3dN9eSkDmtrVWCVhuubTwR7VYeyfTnHnq6Y=; b=GDuP5SzrcLQlGMwHIz1ZBQynFlBBP4s/51j636r98Kbk9rn6LGgptXDNgCi+dhA8t/saBg fA2DFzX5bqtCXdhG5aPBlIU9u/mPqaCAlneN+4TgcCjUchjUBWIBVPiZrl+0K+ElcKmeL2 wrmzX2+TA0I73zOhElhmqY2Pj8ztWSY= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-481-vfgRVVF8OSOrnMpKgY6tAw-1; Sun, 02 Mar 2025 09:33:25 -0500 X-MC-Unique: vfgRVVF8OSOrnMpKgY6tAw-1 X-Mimecast-MFC-AGG-ID: vfgRVVF8OSOrnMpKgY6tAw_1740926004 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 405401800570; Sun, 2 Mar 2025 14:33:24 +0000 (UTC) Received: from server.redhat.com (unknown [10.72.112.49]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id D1F821800362; Sun, 2 Mar 2025 14:33:19 +0000 (UTC) From: Cindy Lu To: lulu@redhat.com, jasowang@redhat.com, mst@redhat.com, michael.christie@oracle.com, sgarzare@redhat.com, linux-kernel@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org Subject: [PATCH v7 2/8] vhost: Reintroduce vhost_worker to support kthread Date: Sun, 2 Mar 2025 22:32:04 +0800 Message-ID: <20250302143259.1221569-3-lulu@redhat.com> In-Reply-To: <20250302143259.1221569-1-lulu@redhat.com> References: <20250302143259.1221569-1-lulu@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 Content-Type: text/plain; charset="utf-8" Add the previously removed function vhost_worker() back to support the kthread and rename it to vhost_run_work_kthread_list. The old function vhost_worker was change to support task in commit 6e890c5d5021 ("vhost: use vhost_tasks for worker threads") change to xarray in commit 1cdaafa1b8b4 ("vhost: replace single worker pointer with xarray") Signed-off-by: Cindy Lu Tested-by: Lei Yang --- drivers/vhost/vhost.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index 250dc43f1786..9500e85b42ce 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -388,6 +388,44 @@ static void vhost_vq_reset(struct vhost_dev *dev, __vhost_vq_meta_reset(vq); } =20 +static int vhost_run_work_kthread_list(void *data) +{ + struct vhost_worker *worker =3D data; + struct vhost_work *work, *work_next; + struct vhost_dev *dev =3D worker->dev; + struct llist_node *node; + + kthread_use_mm(dev->mm); + + for (;;) { + /* mb paired w/ kthread_stop */ + set_current_state(TASK_INTERRUPTIBLE); + + if (kthread_should_stop()) { + __set_current_state(TASK_RUNNING); + break; + } + node =3D llist_del_all(&worker->work_list); + if (!node) + schedule(); + + node =3D llist_reverse_order(node); + /* make sure flag is seen after deletion */ + smp_wmb(); + llist_for_each_entry_safe(work, work_next, node, node) { + clear_bit(VHOST_WORK_QUEUED, &work->flags); + __set_current_state(TASK_RUNNING); + kcov_remote_start_common(worker->kcov_handle); + work->fn(work); + kcov_remote_stop(); + cond_resched(); + } + } + kthread_unuse_mm(dev->mm); + + return 0; +} + static bool vhost_run_work_list(void *data) { struct vhost_worker *worker =3D data; --=20 2.45.0 From nobody Wed Feb 11 04:00:57 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EEBAC38384 for ; Sun, 2 Mar 2025 14:33:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740926021; cv=none; b=ngpbCgDIO2XEoC5ZO/zSvSqTEUD54BseVoY7tFXp9p5R0qfueuX10Cm8ApC6lR2UFVRFZePd+oVb7tyasf8xBAsbFjPJr1PfPeHdXgVJ8OStsg0qcQnpE6AD50ttPYNQanu8f0vQJxt6h858SmqJUWSj0sRQYu4w3ZVVXUwNjNY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740926021; c=relaxed/simple; bh=tcyBwdgr2YlItLRbbxG+qVgq+GW1dlqK36utI5TcPo0=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SC/GsBeVkMSa+fNq8JSt0gFOUPfatAEQvr8vnrLMiYK7EehBb3UqAM5+P5fu6oJfEzCSKseZIAx9mBp71DjO0oV+yb8kPbbKtZ7PhUCCD7TbvN2mXdh3Q/rbucrHhYjr1ZIPqiUp8qP3M+9XPJPUOT5FKoHFFvufWaHbLaCPOps= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=WX6g+RoK; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="WX6g+RoK" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740926018; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ePoKzvLFT6RYs8QjlwPr44Qcs7l2YXCu4ibp5I+WQTY=; b=WX6g+RoKP892+bXELH8+YitIKpfLBXV6uSSGHT0wm6qp18jhudsIlbcAx/hMNDSmfkhpVR rHYT/UsCxXeS/MAYgjYXzeQ1RS6mIBVjYaQV+tyD+4lZDKIWU6NgXpPWwzUmSP4t+JrYXt oEgeOvxgYgGTpQZKyyjkicFaPoTas0A= Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-629-9uGaB7YjMDaKKndjTzXKhg-1; Sun, 02 Mar 2025 09:33:30 -0500 X-MC-Unique: 9uGaB7YjMDaKKndjTzXKhg-1 X-Mimecast-MFC-AGG-ID: 9uGaB7YjMDaKKndjTzXKhg_1740926009 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id A6A6F193585F; Sun, 2 Mar 2025 14:33:29 +0000 (UTC) Received: from server.redhat.com (unknown [10.72.112.49]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 093F01800359; Sun, 2 Mar 2025 14:33:24 +0000 (UTC) From: Cindy Lu To: lulu@redhat.com, jasowang@redhat.com, mst@redhat.com, michael.christie@oracle.com, sgarzare@redhat.com, linux-kernel@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org Subject: [PATCH v7 3/8] vhost: Add the cgroup related function Date: Sun, 2 Mar 2025 22:32:05 +0800 Message-ID: <20250302143259.1221569-4-lulu@redhat.com> In-Reply-To: <20250302143259.1221569-1-lulu@redhat.com> References: <20250302143259.1221569-1-lulu@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 Content-Type: text/plain; charset="utf-8" Add back the previously removed cgroup function to support the kthread The biggest change for this part is in vhost_attach_cgroups() and vhost_attach_task_to_cgroups(). The old function was remove in commit 6e890c5d5021 ("vhost: use vhost_tasks for worker threads") Signed-off-by: Cindy Lu Tested-by: Lei Yang --- drivers/vhost/vhost.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index 9500e85b42ce..20571bd6f7bd 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -620,6 +621,46 @@ long vhost_dev_check_owner(struct vhost_dev *dev) } EXPORT_SYMBOL_GPL(vhost_dev_check_owner); =20 +struct vhost_attach_cgroups_struct { + struct vhost_work work; + struct task_struct *owner; + int ret; +}; + +static void vhost_attach_cgroups_work(struct vhost_work *work) +{ + struct vhost_attach_cgroups_struct *s; + + s =3D container_of(work, struct vhost_attach_cgroups_struct, work); + s->ret =3D cgroup_attach_task_all(s->owner, current); +} + +static int vhost_attach_task_to_cgroups(struct vhost_worker *worker) +{ + struct vhost_attach_cgroups_struct attach; + int saved_cnt; + + attach.owner =3D current; + + vhost_work_init(&attach.work, vhost_attach_cgroups_work); + vhost_worker_queue(worker, &attach.work); + + mutex_lock(&worker->mutex); + + /* + * Bypass attachment_cnt check in __vhost_worker_flush: + * Temporarily change it to INT_MAX to bypass the check + */ + saved_cnt =3D worker->attachment_cnt; + worker->attachment_cnt =3D INT_MAX; + __vhost_worker_flush(worker); + worker->attachment_cnt =3D saved_cnt; + + mutex_unlock(&worker->mutex); + + return attach.ret; +} + /* Caller should have device mutex */ bool vhost_dev_has_owner(struct vhost_dev *dev) { --=20 2.45.0 From nobody Wed Feb 11 04:00:57 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 278591E7C08 for ; Sun, 2 Mar 2025 14:34:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740926060; cv=none; b=uL/w6qUR2hp20UrM8RqbkcQEgUgpWiFX0FD3mC2die7s0NlTJ7EOEnJ4OuKCxRFD6Gn5I0A/jNwiv4no4hxOQ0xh48gdZ7tymeX6SAT8nZTIO7UaJ9+hepYpofTFtnKW1Dxm1b7x6ZA1wWaRUTzxeVf2gwBiExxrYftaGA3PqpE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740926060; c=relaxed/simple; bh=rg0331/HCZNBz8afZ83IRC/xl06pHQpnFDX9W2DuPh4=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MVUDkw/iGv/Umr9kkGVaWsjMf9zFDCVOr5FgBGs+tLuHEY2sH/jontwc8OFTKd6P+eud8Bz2/6ICKFnZ/NrwbAT23WwPAe/qQeU4SX6YbBm7mW6l3LsqRWjkatNGaudGye4EbV6FJncgyH6PKICsN6fyXXNWaEkF6fA1gW0K6Zw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=CJcSFoIF; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="CJcSFoIF" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740926057; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jOsU3m3SY4rtF1PFvdPMYDeuuXLw2NBc5ORrRS7hWak=; b=CJcSFoIF2amKCh5mjkZKuDpMjyKa4QDER2Zl2WxYiGY71mPbkSoooxVMb5b9P+51HhM2sH Mc96tJ7jj2YyWWvfSgfeDfVCZEzk4RWRR92T7sndlG9hkVKLP+OSsNR/sIpT2IukR8aZdr gSRhlC7oHmsYU153ddCCCdz0uA/czgI= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-108-ShIiBrarMR2i_YP-vGqbAw-1; Sun, 02 Mar 2025 09:33:58 -0500 X-MC-Unique: ShIiBrarMR2i_YP-vGqbAw-1 X-Mimecast-MFC-AGG-ID: ShIiBrarMR2i_YP-vGqbAw_1740926037 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 7A9461954B1D; Sun, 2 Mar 2025 14:33:57 +0000 (UTC) Received: from server.redhat.com (unknown [10.72.112.49]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id BED6019560AB; Sun, 2 Mar 2025 14:33:52 +0000 (UTC) From: Cindy Lu To: lulu@redhat.com, jasowang@redhat.com, mst@redhat.com, michael.christie@oracle.com, sgarzare@redhat.com, linux-kernel@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org Subject: [PATCH v7 4/8] vhost: Introduce vhost_worker_ops in vhost_worker Date: Sun, 2 Mar 2025 22:32:06 +0800 Message-ID: <20250302143259.1221569-5-lulu@redhat.com> In-Reply-To: <20250302143259.1221569-1-lulu@redhat.com> References: <20250302143259.1221569-1-lulu@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 Content-Type: text/plain; charset="utf-8" Abstract vhost worker operations (create/stop/wakeup) into an ops structure to prepare for kthread mode support. Signed-off-by: Cindy Lu Tested-by: Lei Yang --- drivers/vhost/vhost.c | 63 ++++++++++++++++++++++++++++++------------- drivers/vhost/vhost.h | 11 ++++++++ 2 files changed, 56 insertions(+), 18 deletions(-) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index 20571bd6f7bd..c162ad772f8f 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -243,7 +243,7 @@ static void vhost_worker_queue(struct vhost_worker *wor= ker, * test_and_set_bit() implies a memory barrier. */ llist_add(&work->node, &worker->work_list); - vhost_task_wake(worker->vtsk); + worker->ops->wakeup(worker); } } =20 @@ -706,7 +706,7 @@ static void vhost_worker_destroy(struct vhost_dev *dev, =20 WARN_ON(!llist_empty(&worker->work_list)); xa_erase(&dev->worker_xa, worker->id); - vhost_task_stop(worker->vtsk); + worker->ops->stop(worker); kfree(worker); } =20 @@ -729,42 +729,69 @@ static void vhost_workers_free(struct vhost_dev *dev) xa_destroy(&dev->worker_xa); } =20 +static void vhost_task_wakeup(struct vhost_worker *worker) +{ + return vhost_task_wake(worker->vtsk); +} + +static void vhost_task_do_stop(struct vhost_worker *worker) +{ + return vhost_task_stop(worker->vtsk); +} + +static int vhost_task_worker_create(struct vhost_worker *worker, + struct vhost_dev *dev, const char *name) +{ + struct vhost_task *vtsk; + u32 id; + int ret; + + vtsk =3D vhost_task_create(vhost_run_work_list, vhost_worker_killed, + worker, name); + if (IS_ERR(vtsk)) + return PTR_ERR(vtsk); + + worker->vtsk =3D vtsk; + vhost_task_start(vtsk); + ret =3D xa_alloc(&dev->worker_xa, &id, worker, xa_limit_32b, GFP_KERNEL); + if (ret < 0) { + vhost_task_do_stop(worker); + return ret; + } + worker->id =3D id; + return 0; +} + +static const struct vhost_worker_ops vhost_task_ops =3D { + .create =3D vhost_task_worker_create, + .stop =3D vhost_task_do_stop, + .wakeup =3D vhost_task_wakeup, +}; + static struct vhost_worker *vhost_worker_create(struct vhost_dev *dev) { struct vhost_worker *worker; - struct vhost_task *vtsk; char name[TASK_COMM_LEN]; int ret; - u32 id; + const struct vhost_worker_ops *ops =3D &vhost_task_ops; =20 worker =3D kzalloc(sizeof(*worker), GFP_KERNEL_ACCOUNT); if (!worker) return NULL; =20 worker->dev =3D dev; + worker->ops =3D ops; snprintf(name, sizeof(name), "vhost-%d", current->pid); =20 - vtsk =3D vhost_task_create(vhost_run_work_list, vhost_worker_killed, - worker, name); - if (IS_ERR(vtsk)) - goto free_worker; - mutex_init(&worker->mutex); init_llist_head(&worker->work_list); worker->kcov_handle =3D kcov_common_handle(); - worker->vtsk =3D vtsk; - - vhost_task_start(vtsk); - - ret =3D xa_alloc(&dev->worker_xa, &id, worker, xa_limit_32b, GFP_KERNEL); + ret =3D ops->create(worker, dev, name); if (ret < 0) - goto stop_worker; - worker->id =3D id; + goto free_worker; =20 return worker; =20 -stop_worker: - vhost_task_stop(vtsk); free_worker: kfree(worker); return NULL; diff --git a/drivers/vhost/vhost.h b/drivers/vhost/vhost.h index 19bb94922a0e..98895e299efa 100644 --- a/drivers/vhost/vhost.h +++ b/drivers/vhost/vhost.h @@ -26,6 +26,16 @@ struct vhost_work { unsigned long flags; }; =20 +struct vhost_worker; +struct vhost_dev; + +struct vhost_worker_ops { + int (*create)(struct vhost_worker *worker, struct vhost_dev *dev, + const char *name); + void (*stop)(struct vhost_worker *worker); + void (*wakeup)(struct vhost_worker *worker); +}; + struct vhost_worker { struct vhost_task *vtsk; struct vhost_dev *dev; @@ -36,6 +46,7 @@ struct vhost_worker { u32 id; int attachment_cnt; bool killed; + const struct vhost_worker_ops *ops; }; =20 /* Poll a file (eventfd or socket) */ --=20 2.45.0 From nobody Wed Feb 11 04:00:57 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DF2DE1E9B07 for ; Sun, 2 Mar 2025 14:34:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740926064; cv=none; b=Ty2lbPGSpviqGiutwllcB475/sQAqXpgyFnqDhglDxX9qEdZotOHVw3jM5Ok5STfbVs4oIVJO0fRWRZ9xSxEQLEIb/xnqLOya0pHLsSohSmW2D8jKBZ7isTEu5xf+bkga3keKuA60VqfpSmIWoz9ASLNQINfS8kiq1PK15pwz38= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740926064; c=relaxed/simple; bh=FDMYH+VuvPX7L7xdXXrSDlUL9wJ9IlCFimDvXX9FoIU=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pzDBnFwJz+kDn9itv5PU+sjg388Z/165f6wtrjnfu94H5yvRI5dXBAgN+9xw7ZBCkZ88i1TXrTsJcGpqO+K+sSiuKKUJr1jKJO6jiyl9rlWjPLaavynW2qRhALwXPtraNUjAD0V6Qn25Nf10Dx64jWrMIRQ6+EULUE8k6VUN0tc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=biUCawkf; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="biUCawkf" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740926062; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gHLTuC4KWZdovhJ3UQM5fvJjtH+elhHPHMwqnZsGpc4=; b=biUCawkfGqrqMjiB8AxiFhwv4SX9R6gF9HEZ1fROrkKXtuQjNeVjjoEhryEA8+agfLQlgC tvKZaIRwS6c3gdPEpY4JZS1MvcxO8AK+McTszE34z3hDRf9+lHdhmp1ssp5RD1ff8Kf4YM /ARdpx1Ge1ym7qs8AtjzD391YwD+jKY= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-500-wCVH1V7xMHm6zpkDPX0G7g-1; Sun, 02 Mar 2025 09:34:03 -0500 X-MC-Unique: wCVH1V7xMHm6zpkDPX0G7g-1 X-Mimecast-MFC-AGG-ID: wCVH1V7xMHm6zpkDPX0G7g_1740926042 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id D3C861975AFC; Sun, 2 Mar 2025 14:34:02 +0000 (UTC) Received: from server.redhat.com (unknown [10.72.112.49]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 507A819560AB; Sun, 2 Mar 2025 14:33:57 +0000 (UTC) From: Cindy Lu To: lulu@redhat.com, jasowang@redhat.com, mst@redhat.com, michael.christie@oracle.com, sgarzare@redhat.com, linux-kernel@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org Subject: [PATCH v7 5/8] vhost: Reintroduce kthread mode support in vhost Date: Sun, 2 Mar 2025 22:32:07 +0800 Message-ID: <20250302143259.1221569-6-lulu@redhat.com> In-Reply-To: <20250302143259.1221569-1-lulu@redhat.com> References: <20250302143259.1221569-1-lulu@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 Content-Type: text/plain; charset="utf-8" This commit restores the previously removed functions kthread wake/stop/create, and use ops structure vhost_worker_ops to manage worker wakeup, stop and creation. The function vhost_worker_create initializes these ops pointers based on the value of inherit_owner The old function was remove in commit 6e890c5d5021 ("vhost: use vhost_tasks for worker threads") Signed-off-by: Cindy Lu Tested-by: Lei Yang --- drivers/vhost/vhost.c | 48 ++++++++++++++++++++++++++++++++++++++++++- drivers/vhost/vhost.h | 1 + 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index c162ad772f8f..be97028a8baf 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -734,11 +734,21 @@ static void vhost_task_wakeup(struct vhost_worker *wo= rker) return vhost_task_wake(worker->vtsk); } =20 +static void vhost_kthread_wakeup(struct vhost_worker *worker) +{ + wake_up_process(worker->kthread_task); +} + static void vhost_task_do_stop(struct vhost_worker *worker) { return vhost_task_stop(worker->vtsk); } =20 +static void vhost_kthread_do_stop(struct vhost_worker *worker) +{ + kthread_stop(worker->kthread_task); +} + static int vhost_task_worker_create(struct vhost_worker *worker, struct vhost_dev *dev, const char *name) { @@ -762,6 +772,41 @@ static int vhost_task_worker_create(struct vhost_worke= r *worker, return 0; } =20 +static int vhost_kthread_worker_create(struct vhost_worker *worker, + struct vhost_dev *dev, const char *name) +{ + struct task_struct *task; + u32 id; + int ret; + + task =3D kthread_create(vhost_run_work_kthread_list, worker, "%s", name); + if (IS_ERR(task)) + return PTR_ERR(task); + + worker->kthread_task =3D task; + wake_up_process(task); + ret =3D xa_alloc(&dev->worker_xa, &id, worker, xa_limit_32b, GFP_KERNEL); + if (ret < 0) + goto stop_worker; + + ret =3D vhost_attach_task_to_cgroups(worker); + if (ret) + goto stop_worker; + + worker->id =3D id; + return 0; + +stop_worker: + vhost_kthread_do_stop(worker); + return ret; +} + +static const struct vhost_worker_ops kthread_ops =3D { + .create =3D vhost_kthread_worker_create, + .stop =3D vhost_kthread_do_stop, + .wakeup =3D vhost_kthread_wakeup, +}; + static const struct vhost_worker_ops vhost_task_ops =3D { .create =3D vhost_task_worker_create, .stop =3D vhost_task_do_stop, @@ -773,7 +818,8 @@ static struct vhost_worker *vhost_worker_create(struct = vhost_dev *dev) struct vhost_worker *worker; char name[TASK_COMM_LEN]; int ret; - const struct vhost_worker_ops *ops =3D &vhost_task_ops; + const struct vhost_worker_ops *ops =3D + dev->inherit_owner ? &vhost_task_ops : &kthread_ops; =20 worker =3D kzalloc(sizeof(*worker), GFP_KERNEL_ACCOUNT); if (!worker) diff --git a/drivers/vhost/vhost.h b/drivers/vhost/vhost.h index 98895e299efa..af4b2f7d3b91 100644 --- a/drivers/vhost/vhost.h +++ b/drivers/vhost/vhost.h @@ -37,6 +37,7 @@ struct vhost_worker_ops { }; =20 struct vhost_worker { + struct task_struct *kthread_task; struct vhost_task *vtsk; struct vhost_dev *dev; /* Used to serialize device wide flushing with worker swapping. */ --=20 2.45.0 From nobody Wed Feb 11 04:00:57 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DCBDA1E9B16 for ; Sun, 2 Mar 2025 14:34:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740926065; cv=none; b=H2b2uMqeB9w6CRiNQ4D5YPyszBmSbObRTX1aGnrMb2fuGEnHviO9qHEfHY4S/q945hCYQZ6qdSm0ZcB4tBkbmj/nN3tsSffzO8FtHVU4L2qsxRsh8EHMI6rDcXTUdFrLDQ1M+WqtxhPYo6JIwCjai1zpGchzQdcx0lsZknZO0JU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740926065; c=relaxed/simple; bh=gQlhwm/UPdHEl0lHZ6zSydcD82rexseS8D8eO4RMhFU=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=cuiQYMFmD1DtWHRlsRKoZelZ9zZxwpxaK8IIjvpmQXePyhJaq1e/4co2iukJwHuzCQhz1HVTtNOj47tOkaexO3qfNS6g0arTq6sdDQi+kgc9fLWpt2j354xQ+lsGMAN6+Iv+M4X11WtVQrY9lZRgFtbmWmRE+LyM0SimUguCLok= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=RA2qnqKz; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="RA2qnqKz" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740926062; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=A9IP3Rzux+HyTZ69cLNkjXI40jyB7zRAK3/LInKHI/I=; b=RA2qnqKzWunEB7t4MKbDJ685n11DBwCWz7k0SU8IChZfMFfnFBqQURa/u6ma1vX7plizzn RK+gpkqH1qjFAens3pmvlMKDZytmMu1b9bjr5O9GPxgDz9SO42KDWnrMHpswAxfECckRVz PvNeiOzFRqoJjUWU177m5kLurfpTAek= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-252-sjnB76YwPnuKazIwHqJraw-1; Sun, 02 Mar 2025 09:34:09 -0500 X-MC-Unique: sjnB76YwPnuKazIwHqJraw-1 X-Mimecast-MFC-AGG-ID: sjnB76YwPnuKazIwHqJraw_1740926048 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 508AB1954B1F; Sun, 2 Mar 2025 14:34:08 +0000 (UTC) Received: from server.redhat.com (unknown [10.72.112.49]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id B9747195395A; Sun, 2 Mar 2025 14:34:03 +0000 (UTC) From: Cindy Lu To: lulu@redhat.com, jasowang@redhat.com, mst@redhat.com, michael.christie@oracle.com, sgarzare@redhat.com, linux-kernel@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org Subject: [PATCH v7 6/8] vhost: uapi to control task mode (owner vs kthread) Date: Sun, 2 Mar 2025 22:32:08 +0800 Message-ID: <20250302143259.1221569-7-lulu@redhat.com> In-Reply-To: <20250302143259.1221569-1-lulu@redhat.com> References: <20250302143259.1221569-1-lulu@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 Content-Type: text/plain; charset="utf-8" Add a new UAPI to configure the vhost device to use the kthread mode The userspace application can use IOCTL VHOST_FORK_FROM_OWNER to choose between owner and kthread mode if necessary This setting must be applied before VHOST_SET_OWNER, as the worker will be created in the VHOST_SET_OWNER function Signed-off-by: Cindy Lu Tested-by: Lei Yang --- drivers/vhost/vhost.c | 22 ++++++++++++++++++++-- include/uapi/linux/vhost.h | 15 +++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index be97028a8baf..ff930c2e5b78 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -1134,7 +1134,7 @@ void vhost_dev_reset_owner(struct vhost_dev *dev, str= uct vhost_iotlb *umem) int i; =20 vhost_dev_cleanup(dev); - + dev->inherit_owner =3D true; dev->umem =3D umem; /* We don't need VQ locks below since vhost_dev_cleanup makes sure * VQs aren't running. @@ -2287,7 +2287,25 @@ long vhost_dev_ioctl(struct vhost_dev *d, unsigned i= nt ioctl, void __user *argp) r =3D vhost_dev_set_owner(d); goto done; } - + if (ioctl =3D=3D VHOST_FORK_FROM_OWNER) { + u8 inherit_owner; + /*inherit_owner can only be modified before owner is set*/ + if (vhost_dev_has_owner(d)) { + r =3D -EBUSY; + goto done; + } + if (copy_from_user(&inherit_owner, argp, sizeof(u8))) { + r =3D -EFAULT; + goto done; + } + if (inherit_owner > 1) { + r =3D -EINVAL; + goto done; + } + d->inherit_owner =3D (bool)inherit_owner; + r =3D 0; + goto done; + } /* You must be the owner to do anything else */ r =3D vhost_dev_check_owner(d); if (r) diff --git a/include/uapi/linux/vhost.h b/include/uapi/linux/vhost.h index b95dd84eef2d..547b4fa4c3bd 100644 --- a/include/uapi/linux/vhost.h +++ b/include/uapi/linux/vhost.h @@ -235,4 +235,19 @@ */ #define VHOST_VDPA_GET_VRING_SIZE _IOWR(VHOST_VIRTIO, 0x82, \ struct vhost_vring_state) + +/** + * VHOST_FORK_FROM_OWNER - Set the inherit_owner flag for the vhost device + * + * @param inherit_owner: An 8-bit value that determines the vhost thread m= ode + * + * When inherit_owner is set to 1(default value): + * - Vhost will create tasks similar to processes forked from the owner, + * inheriting all of the owner's attributes.. + * + * When inherit_owner is set to 0: + * - Vhost will create tasks as kernel thread + */ +#define VHOST_FORK_FROM_OWNER _IOW(VHOST_VIRTIO, 0x83, __u8) + #endif --=20 2.45.0 From nobody Wed Feb 11 04:00:57 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 168DE36AF5 for ; Sun, 2 Mar 2025 14:34:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740926058; cv=none; b=hv7myK5mmKGj8xy6a0V7t9IWwjfxWcbj3gmMskAcoOY3ApBnW1914kNPeigS5gMP5XfBCOUVwhAvMY4x93EF5f7fIGIhJ15hgHorZF2BAWoRULrSXgOpAtq8HMMVLNVdGZ+Y+HgYxzKsnyBLOoNIIJovYZ75N73Hsg7eHTo0IJw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740926058; c=relaxed/simple; bh=uvSR+nHkOyddC0QdPQ72//WOuIk64mxGFFY2EA8HABQ=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ql03IRrnrHYHcrukWcjEtHKALgTKDdcWSI9tNMAzmGaDxpyMLApnwbnr355c4tPxLGo8tPvvtlp4NIzveUTuzQoeVslBi3Chf1c8WCfcbJieTnpc5hWoW1bRnk7QRNL5mQ3Sp7VbsUYmWO9EFaFHz0E0nWY4GmwW8j2enwC4YaU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=gESUTHfI; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="gESUTHfI" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740926056; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=rlJT47zLkSfn9zom7PdH4akIw1D+Y3HK6R7VYjb50+Q=; b=gESUTHfI5arX6Po1U+zTU5x4pcPVveAQ+U9+hr5g8SFTslv+BgCblFqNJ9i/mD9nmaBShp ypL/gj730DhFAp4BACNCIak4s/TwmGMxSn/QobvPxMUqXleNXPRygRPJ1XEAwDfBhrUfT+ uVam/tKNUdqMYxb9ne8XhEVkltlC+Ls= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-189-cKQuFv4DNUKVRzrAAVuWDg-1; Sun, 02 Mar 2025 09:34:14 -0500 X-MC-Unique: cKQuFv4DNUKVRzrAAVuWDg-1 X-Mimecast-MFC-AGG-ID: cKQuFv4DNUKVRzrAAVuWDg_1740926053 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id AF5351954B1C; Sun, 2 Mar 2025 14:34:13 +0000 (UTC) Received: from server.redhat.com (unknown [10.72.112.49]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 2799019560AB; Sun, 2 Mar 2025 14:34:08 +0000 (UTC) From: Cindy Lu To: lulu@redhat.com, jasowang@redhat.com, mst@redhat.com, michael.christie@oracle.com, sgarzare@redhat.com, linux-kernel@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org Subject: [PATCH v7 7/8] vhost: Add check for inherit_owner status Date: Sun, 2 Mar 2025 22:32:09 +0800 Message-ID: <20250302143259.1221569-8-lulu@redhat.com> In-Reply-To: <20250302143259.1221569-1-lulu@redhat.com> References: <20250302143259.1221569-1-lulu@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 Content-Type: text/plain; charset="utf-8" The VHOST_NEW_WORKER requires the inherit_owner setting to be true. So we need to add a check for this. Signed-off-by: Cindy Lu Tested-by: Lei Yang --- drivers/vhost/vhost.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index ff930c2e5b78..fb0c7fb43f78 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -1018,6 +1018,13 @@ long vhost_worker_ioctl(struct vhost_dev *dev, unsig= ned int ioctl, switch (ioctl) { /* dev worker ioctls */ case VHOST_NEW_WORKER: + /* + * vhost_tasks will account for worker threads under the parent's + * NPROC value but kthreads do not. To avoid userspace overflowing + * the system with worker threads inherit_owner must be true. + */ + if (!dev->inherit_owner) + return -EFAULT; ret =3D vhost_new_worker(dev, &state); if (!ret && copy_to_user(argp, &state, sizeof(state))) ret =3D -EFAULT; --=20 2.45.0 From nobody Wed Feb 11 04:00:57 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 683F838384 for ; Sun, 2 Mar 2025 14:34:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740926089; cv=none; b=YpW0ibBzed+icsuFnjx4ruEExG5a/JeICPgdDshxmzUq2LcCRMZPNtsnOxOvnoIQ7YhKhkYGY8DovPweiLQr0i5hkhqs/yHYInqmQ36R7nWqUqnCYaPC29hJoMgOf0swkj2khC8MA5Yy/PLwLF8xBl8YC184NO4w5ZZdo7Vwxuw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740926089; c=relaxed/simple; bh=3d1vy7K3Ewc6kBuoFdLtOj9nxEdW0vN1tk9jlWajiE4=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EZiC+5G2nnTq1jkCbVECr4LotyuAWqX3b+ofam6Dwy3kwc3W5RNeF8B6bGHY63e/eqPyoll7Xa+iGdFSTID3OxcklDYGiBTna4nNkjBzLYkJysQBh1LRv7rZhaxzqUNe/2JQB6+2T1h7k9H69XMkROk6FA1+R9Q89yUS+Vv23G8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=E7toa8nX; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="E7toa8nX" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740926085; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=UZogOKk63vf4EDKHQk5Lj385qY5SA+Ws34nKuNOin7A=; b=E7toa8nXLSnlQxNM08jGdIlt8ZOTGBvEeKTYvVOGk0oHGWjOEqPO9VmpqkcD7tOse0rN18 j+jJ1IUEd7WWZMUFj3YERTH6gV3817VTia9foUO6CrR/2Fnln+Sxe+ypjMTUoTipwN18DZ 2EXiuViI+5+2kjiL1vAeRY6apun6ivs= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-86-dEq8IECyNwWicdQhcmOKLg-1; Sun, 02 Mar 2025 09:34:42 -0500 X-MC-Unique: dEq8IECyNwWicdQhcmOKLg-1 X-Mimecast-MFC-AGG-ID: dEq8IECyNwWicdQhcmOKLg_1740926081 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 599311800877; Sun, 2 Mar 2025 14:34:41 +0000 (UTC) Received: from server.redhat.com (unknown [10.72.112.49]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id B62191956048; Sun, 2 Mar 2025 14:34:36 +0000 (UTC) From: Cindy Lu To: lulu@redhat.com, jasowang@redhat.com, mst@redhat.com, michael.christie@oracle.com, sgarzare@redhat.com, linux-kernel@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org Subject: [PATCH v7 8/8] vhost: Add a KConfig knob to enable IOCTL VHOST_FORK_FROM_OWNER Date: Sun, 2 Mar 2025 22:32:10 +0800 Message-ID: <20250302143259.1221569-9-lulu@redhat.com> In-Reply-To: <20250302143259.1221569-1-lulu@redhat.com> References: <20250302143259.1221569-1-lulu@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 Content-Type: text/plain; charset="utf-8" Introduce a new config knob `CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL`, to control the availability of the `VHOST_FORK_FROM_OWNER` ioctl. When CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL is set to n, the ioctl is disabled, and any attempt to use it will result in failure. Signed-off-by: Cindy Lu Tested-by: Lei Yang --- drivers/vhost/Kconfig | 15 +++++++++++++++ drivers/vhost/vhost.c | 11 +++++++++++ 2 files changed, 26 insertions(+) diff --git a/drivers/vhost/Kconfig b/drivers/vhost/Kconfig index b455d9ab6f3d..e5b9dcbf31b6 100644 --- a/drivers/vhost/Kconfig +++ b/drivers/vhost/Kconfig @@ -95,3 +95,18 @@ config VHOST_CROSS_ENDIAN_LEGACY If unsure, say "N". =20 endif + +config VHOST_ENABLE_FORK_OWNER_IOCTL + bool "Enable IOCTL VHOST_FORK_FROM_OWNER" + default n + help + This option enables the IOCTL VHOST_FORK_FROM_OWNER, which allows + userspace applications to modify the thread mode for vhost devices. + + By default, `CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL` is set to `n`, + meaning the ioctl is disabled and any operation using this ioctl + will fail. + When the configuration is enabled (y), the ioctl becomes + available, allowing users to set the mode if needed. + + If unsure, say "N". diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index fb0c7fb43f78..09e5e44dc516 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -2294,6 +2294,8 @@ long vhost_dev_ioctl(struct vhost_dev *d, unsigned in= t ioctl, void __user *argp) r =3D vhost_dev_set_owner(d); goto done; } + +#ifdef CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL if (ioctl =3D=3D VHOST_FORK_FROM_OWNER) { u8 inherit_owner; /*inherit_owner can only be modified before owner is set*/ @@ -2313,6 +2315,15 @@ long vhost_dev_ioctl(struct vhost_dev *d, unsigned i= nt ioctl, void __user *argp) r =3D 0; goto done; } + +#else + if (ioctl =3D=3D VHOST_FORK_FROM_OWNER) { + /* When CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL is 'n', return error */ + r =3D -ENOTTY; + goto done; + } +#endif + /* You must be the owner to do anything else */ r =3D vhost_dev_check_owner(d); if (r) --=20 2.45.0