From nobody Wed Dec 17 02:55:18 2025 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 1618820B814 for ; Sun, 23 Feb 2025 15:41:01 +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=1740325265; cv=none; b=IZtC8PwrS2MYT49ieJxK5NhCd5lIRC4YKGC2k+LCv7crLLkxcRzp9Omkr2vKc+zejQeog8VESTNFI0/l/YO8/hdnCzzhMF+dRyXcEZ4o9YYoNCT9FKJjvX/TI6Wn8bxgjje+D/w0leG2ncNItIUl4MRRKEbIQe0vldtwZ+I/11Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740325265; c=relaxed/simple; bh=jZWHDBC1em9YSxJA7OCoxzWdArjA3A0TYOkpGg7ZSRk=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pZ/2A1nQWQZ/pe9exIRK6+KhDAX3z4TeQ6o3b0OHD23kId1do79aUHAjtDKpsJyiRg8Pffu4eT08J/ifoOKvl/oIduxUHH7ToBGwrcJibRXI+l5DofMREf3QUngQ5zx1rmXvt5PkFohv83TqjACwZ7BzjgX95552iXlXyH8M9M8= 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=ZbNvG7fy; 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="ZbNvG7fy" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740325261; 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=Kx5deBxNNDBqr/ibOJLZi9NoxS8td60GQR/dfMsL+Dk=; b=ZbNvG7fyFWL7/2dRBOJIh2gj3lWcKDZxyc6QdyMNTFC/Zo1luQWO3HALto40CF7azBfHxL RpGhe156BnbpfLdWm/zAb2TW2kkhAlD7HqQdhZM/nkuN/+6PLXX07NQPExU7iCqBn5ywtE LayxGbNgTptolJvYRPjiI5zd35tBof0= 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-679-urbtvVcWOyqwV6w3ExLEig-1; Sun, 23 Feb 2025 10:40:57 -0500 X-MC-Unique: urbtvVcWOyqwV6w3ExLEig-1 X-Mimecast-MFC-AGG-ID: urbtvVcWOyqwV6w3ExLEig_1740325256 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 3E03819560B5; Sun, 23 Feb 2025 15:40:56 +0000 (UTC) Received: from server.redhat.com (unknown [10.72.112.28]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id C04D419412A3; Sun, 23 Feb 2025 15:40:51 +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 v6 1/6] vhost: Add a new parameter in vhost_dev to allow user select kthread Date: Sun, 23 Feb 2025 23:36:16 +0800 Message-ID: <20250223154042.556001-2-lulu@redhat.com> In-Reply-To: <20250223154042.556001-1-lulu@redhat.com> References: <20250223154042.556001-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 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 Acked-by: Jason Wang Tested-by: Lei Yang --- drivers/vhost/vhost.c | 1 + drivers/vhost/vhost.h | 1 + 2 files changed, 2 insertions(+) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index 9ac25d08f473..eaddbd39c29b 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..c650c4506c70 100644 --- a/drivers/vhost/vhost.h +++ b/drivers/vhost/vhost.h @@ -176,6 +176,7 @@ struct vhost_dev { int byte_weight; struct xarray worker_xa; bool use_worker; + bool inherit_owner; int (*msg_handler)(struct vhost_dev *dev, u32 asid, struct vhost_iotlb_msg *msg); }; --=20 2.45.0 From nobody Wed Dec 17 02:55:18 2025 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 0296120B1EA for ; Sun, 23 Feb 2025 15:41:05 +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=1740325267; cv=none; b=PowjTzh/8195c1N+84dP4HR9f5sWwF8ZZIZrqlEIT3LnzXSVdcAh++8A2HxQZexMA/QuGljBAwAlNYRx1rzkFWUZYap+N+RHLPkXBHW+EePEQCbvsOkkaUz/sDk/Ib27CeHkCg2+cyXhY9RwkdtbBvodYcClWK+7SL/H2mJqIFg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740325267; c=relaxed/simple; bh=O+1OQtxPQvHz6+B0oFWBYj6LQgh9U+Y2ejH4TMx9peE=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fJpTNj3qQ6tu6xCB2IUyR+FUuORH0StS6IXkGsiE3OMUZJClRsMjfqTeo5vtoKj/pbG15EYGl001NNORRqpHsJcpiIKylanm1wsB062iMzwTFWmdwFLh4VuOh1KmmRom7qCBk9ubeOZMAB5m+4Kbh3xHYOAAWwJEyw5SMksb72Q= 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=geeGPUny; 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="geeGPUny" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740325264; 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=OaqtC4zyNMItOw1ucAQ9VH5Gv6VtefAKsvYHMah+ZGg=; b=geeGPUny5Kudl8xffSIAQNSKKeaJVQf/IHpCXUfImtjnWJhqWH8WLzUPHQFXMTZPnBVQ06 23cuo+5JnWG5mG7nWy+TsaoI8D1UvGwXBcy9l1MVQ1BYHNdI6biB5R1zHX9O4ZuKyEwTh6 6kR5bLU5aRneeh4obNFk1PqW3nvZ5X4= Received: from mx-prod-mc-02.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-363-gabZXH_mNQ212hodqqxVng-1; Sun, 23 Feb 2025 10:41:02 -0500 X-MC-Unique: gabZXH_mNQ212hodqqxVng-1 X-Mimecast-MFC-AGG-ID: gabZXH_mNQ212hodqqxVng_1740325261 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-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 5543818EB2C9; Sun, 23 Feb 2025 15:41:01 +0000 (UTC) Received: from server.redhat.com (unknown [10.72.112.28]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 1307719412A3; Sun, 23 Feb 2025 15:40:56 +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 v6 2/6] vhost: Reintroduce vhost_worker to support kthread Date: Sun, 23 Feb 2025 23:36:17 +0800 Message-ID: <20250223154042.556001-3-lulu@redhat.com> In-Reply-To: <20250223154042.556001-1-lulu@redhat.com> References: <20250223154042.556001-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 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 Acked-by: Jason Wang 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 eaddbd39c29b..1feba29abf95 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 Dec 17 02:55:18 2025 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 C668C20B1EA for ; Sun, 23 Feb 2025 15:41:10 +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=1740325272; cv=none; b=ki+c3CHQrNp8ylp0Hg8h8UIj2TDMCd2R532S7UMpSB5lTW4z5cBax9QlEP+uSb/wsXFaB/0RRqvF9gB0QuKO4rOCEkVu1BIHloaXSh2N06hQCEjeGXLeegMTZJ2wyAJK6q7fibXov/R5COa/zps2sb3WZ3Dlj2eZo91CT5izEe4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740325272; c=relaxed/simple; bh=mSf1YOOT4rT+xoFBikEdZRvyB8BOLcKa4G4tDStOsjY=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=k1uPfduDI28Z3Lyl6llkThzpWngMC6r0qhjj0R66zpB73Ogd91S5FGdC30CscxWVoO4Bkjmk8RHbM81OjYta+NhmNKZPsESfSrCW5RN6WWyio/Qnlm5MIeJGfV+ynfKYeAPFQbycs+/4k8WlSb61R9hNYbBawX9+/436EjzdJRY= 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=CPheQ5VB; 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="CPheQ5VB" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740325269; 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=4frTMdgbHHhGI0JKe/2KXP6ad1stTd1tmXcRu+Jrt+g=; b=CPheQ5VB53o6u7HpdE7WOu/oVErmfqNa3dIdFCRU0OAXtwLOlSUKzUYixLvq8Ua+ye+7fj Sn4IdcUuLJ/lU0knkHiOo68IqCMB+3Z34Yk4yttoAENU0SRb2mqPyOFm09jemYUeCIJOz/ kLvpc2mPSVhTNZpMfye3LoVFP3jD5Yk= Received: from mx-prod-mc-06.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-369-9mArNKYWPXWjRLorh-iofg-1; Sun, 23 Feb 2025 10:41:07 -0500 X-MC-Unique: 9mArNKYWPXWjRLorh-iofg-1 X-Mimecast-MFC-AGG-ID: 9mArNKYWPXWjRLorh-iofg_1740325266 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-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id C7B7E18004A7; Sun, 23 Feb 2025 15:41:06 +0000 (UTC) Received: from server.redhat.com (unknown [10.72.112.28]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 4489619412A3; Sun, 23 Feb 2025 15:41:01 +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 v6 3/6] vhost: Add the cgroup related function Date: Sun, 23 Feb 2025 23:36:18 +0800 Message-ID: <20250223154042.556001-4-lulu@redhat.com> In-Reply-To: <20250223154042.556001-1-lulu@redhat.com> References: <20250223154042.556001-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 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(). Reuse the function __vhost_worker_flush, but in this situation, the attachment_cnt is 0. Therefore, add a boolean to disable this check. 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 | 42 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index 1feba29abf95..adbb957c8b5f 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -269,11 +270,12 @@ EXPORT_SYMBOL_GPL(vhost_vq_work_queue); * * The worker's flush_mutex must be held. */ -static void __vhost_worker_flush(struct vhost_worker *worker) +static void __vhost_worker_flush(struct vhost_worker *worker, + bool ignore_attachment) { struct vhost_flush_struct flush; =20 - if (!worker->attachment_cnt || worker->killed) + if ((!ignore_attachment && !worker->attachment_cnt) || worker->killed) return; =20 init_completion(&flush.wait_event); @@ -292,7 +294,7 @@ static void __vhost_worker_flush(struct vhost_worker *w= orker) static void vhost_worker_flush(struct vhost_worker *worker) { mutex_lock(&worker->mutex); - __vhost_worker_flush(worker); + __vhost_worker_flush(worker, false); mutex_unlock(&worker->mutex); } =20 @@ -620,6 +622,36 @@ 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; + + attach.owner =3D current; + + vhost_work_init(&attach.work, vhost_attach_cgroups_work); + vhost_worker_queue(worker, &attach.work); + + mutex_lock(&worker->mutex); + __vhost_worker_flush(worker, true); + mutex_unlock(&worker->mutex); + + return attach.ret; +} + /* Caller should have device mutex */ bool vhost_dev_has_owner(struct vhost_dev *dev) { @@ -793,7 +825,7 @@ static void __vhost_vq_attach_worker(struct vhost_virtq= ueue *vq, /* Make sure new vq queue/flush/poll calls see the new worker */ synchronize_rcu(); /* Make sure whatever was queued gets run */ - __vhost_worker_flush(old_worker); + __vhost_worker_flush(old_worker, false); old_worker->attachment_cnt--; mutex_unlock(&old_worker->mutex); } @@ -852,7 +884,7 @@ static int vhost_free_worker(struct vhost_dev *dev, * to zero. Make sure flushes are flushed from the queue before * freeing. */ - __vhost_worker_flush(worker); + __vhost_worker_flush(worker, false); mutex_unlock(&worker->mutex); =20 vhost_worker_destroy(dev, worker); --=20 2.45.0 From nobody Wed Dec 17 02:55:18 2025 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 BADA520967B for ; Sun, 23 Feb 2025 15:41: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=1740325301; cv=none; b=nliEL37mTcTaxAIr5Cgpe8GqRPWOIDpv3eQ5S1bkW6vcRGLbeoSf7X9BVBWfUwv2Ij2ZaiLT9t/RXuO3rOsqXzX6qoXddMGrDyBMdemWMoWkUS7BDOssNLWkL+V1JgX1mPdV2IX3ofzOzQGJ5eD60ELqWX58ZvzNeJxVPATwB4k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740325301; c=relaxed/simple; bh=ZXiEdTTDkzwOV/njsO665pJImpsVwviM91yrhio2Vu4=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=p4ZvEG+wUMGjg5t4MV7rbWiKjKOq7mIE/K9coHc1s5OSccM8a905cU0QCehrV6uVWFtDslzGVN+FRZmZhtR7Xl1iY3AbbrncjTAmNS5Pd1KmUdBX9FqlITj4TfQiHjhu3uGImPCv8jo5KPxtxSe3XzpvBaytdbTNNP+AGdY4RUk= 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=J2qUe9fU; 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="J2qUe9fU" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740325298; 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=S5Ma3bsAkPkhFtnIEw/9PBZEYBfxPhfHkVWn0LCGlvg=; b=J2qUe9fUpYNdDwklKnnHI30ITYHNQiaytEeXZiohE0/O63p7+PcTdkZO0ZMUtiuIki87M+ qBEftDtnJnXQzlK00iNOa4qPFAsL7qsba/QUCs0V0n7J9jaxTdi1VohcG6a8whOtTQ0ynK 0U+PW7vmigRrARmRlNCoeRJco16AyYk= 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-253-2ENMbybrPVK4rSVx4t1vnA-1; Sun, 23 Feb 2025 10:41:35 -0500 X-MC-Unique: 2ENMbybrPVK4rSVx4t1vnA-1 X-Mimecast-MFC-AGG-ID: 2ENMbybrPVK4rSVx4t1vnA_1740325294 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 3AF0219560BC; Sun, 23 Feb 2025 15:41:34 +0000 (UTC) Received: from server.redhat.com (unknown [10.72.112.28]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id B4C251800359; Sun, 23 Feb 2025 15:41:29 +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 v6 4/6] vhost: introduce worker ops to support multiple thread models Date: Sun, 23 Feb 2025 23:36:19 +0800 Message-ID: <20250223154042.556001-5-lulu@redhat.com> In-Reply-To: <20250223154042.556001-1-lulu@redhat.com> References: <20250223154042.556001-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" This commit restores the previously removed functions kthread_wakeup and kthread_stop, and introduces a new ops structure to handle worker wakeup, stop, and creation. The function vhost_worker_create initializes these ops pointers based on the inherit_owner value. Signed-off-by: Cindy Lu Tested-by: Lei Yang --- drivers/vhost/vhost.c | 115 +++++++++++++++++++++++++++++++++++------- drivers/vhost/vhost.h | 12 +++++ 2 files changed, 110 insertions(+), 17 deletions(-) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index adbb957c8b5f..d8c0ea118bb1 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 @@ -697,7 +697,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 @@ -720,42 +720,123 @@ static void vhost_workers_free(struct vhost_dev *dev) xa_destroy(&dev->worker_xa); } =20 +static void vhost_task_wakeup_fn(struct vhost_worker *worker) +{ + return vhost_task_wake(worker->vtsk); +} + +static void vhost_kthread_wakeup_fn(struct vhost_worker *worker) +{ + wake_up_process(worker->kthread_task); +} + +static void vhost_task_stop_fn(struct vhost_worker *worker) +{ + return vhost_task_stop(worker->vtsk); +} + +static void vhost_kthread_stop_fn(struct vhost_worker *worker) +{ + kthread_stop(worker->kthread_task); +} + +static int vhost_task_worker_create_fn(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 (!vtsk) + return -ENOMEM; + + 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_stop_fn(worker); + return ret; + } + worker->id =3D id; + return 0; +} + +static int kthread_worker_create_fn(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_stop_fn(worker); + return ret; +} + +static const struct vhost_worker_ops vhost_task_ops =3D { + .create =3D vhost_task_worker_create_fn, + .stop =3D vhost_task_stop_fn, + .wakeup =3D vhost_task_wakeup_fn, +}; + +static const struct vhost_worker_ops kthread_ops =3D { + .create =3D kthread_worker_create_fn, + .stop =3D vhost_kthread_stop_fn, + .wakeup =3D vhost_kthread_wakeup_fn, +}; + 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 + dev->inherit_owner ? &vhost_task_ops : &kthread_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 (!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); + /* + * 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. + */ =20 - 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 c650c4506c70..029c203147be 100644 --- a/drivers/vhost/vhost.h +++ b/drivers/vhost/vhost.h @@ -26,7 +26,18 @@ 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 task_struct *kthread_task; struct vhost_task *vtsk; struct vhost_dev *dev; /* Used to serialize device wide flushing with worker swapping. */ @@ -36,6 +47,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 Dec 17 02:55:18 2025 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 BB92A20C022 for ; Sun, 23 Feb 2025 15:41: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=1740325308; cv=none; b=Y6tUOIFxlTYjpRdNBUnSVaVYtONKlm+PDcA6wz+znBXDFtpojRvOM2fQQFB0PI/3KbckPPVX4WAp2SqVjF+M1TrSW1Cwv0y2whNqGN897PEjmFYjK2OqBv5gI9yTPCZi56L1BYhmvvduthuY4sPwnNkuQrVwvBNAZGBmMmNvBx4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740325308; c=relaxed/simple; bh=kQKVmVQmSmL+adQEArOg/LOerBpbDGXBClSP3y4DEC8=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=kpD8NO/U6l6itYhQvSoWGocNTASnP9TTDwug2wdzMkf8uJ0Nul+JJyMXDXQD5YSU/fEpqpcnrSJ040XAZ6EgdaPAH+Bd0v7DTBGEZJ1bZp2G9/eEkOUc2mCwFcom5u0xYpScDEadxEceg6or0y+i3wdjwxfmYwR6fIki0gijzhU= 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=Xij+vN3G; 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="Xij+vN3G" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740325305; 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=rjvawlWkTytaHXnsqhHLk8h5P4QoG4NsfjDx6jEZ3uw=; b=Xij+vN3GYEB2cCwvb18B3vfk4ocjx7XXPVP1mIjQyN7UkBqUzu0Utn16bSXzQBeZyXosNl SVMGzQ6N4Cbl1Q/ixjjSfMAfjuJoCeEJdgq5dYF+dOSyCcJb/iRMXLRecVq6MSXU67yxUf h9ifdNMbRoEur2Ruf4t6Bmg059Sj5ZM= 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-654-96eYb0OwOgKQKyG8SfbhvA-1; Sun, 23 Feb 2025 10:41:40 -0500 X-MC-Unique: 96eYb0OwOgKQKyG8SfbhvA-1 X-Mimecast-MFC-AGG-ID: 96eYb0OwOgKQKyG8SfbhvA_1740325299 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 4901C1800374; Sun, 23 Feb 2025 15:41:39 +0000 (UTC) Received: from server.redhat.com (unknown [10.72.112.28]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 0E2D3180035F; Sun, 23 Feb 2025 15:41:34 +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 v6 5/6] vhost: Add new UAPI to support change to task mode Date: Sun, 23 Feb 2025 23:36:20 +0800 Message-ID: <20250223154042.556001-6-lulu@redhat.com> In-Reply-To: <20250223154042.556001-1-lulu@redhat.com> References: <20250223154042.556001-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 a new UAPI to enable setting the vhost device to task mode. The userspace application can use VHOST_SET_INHERIT_FROM_OWNER to configure the 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 | 24 ++++++++++++++++++++++-- include/uapi/linux/vhost.h | 18 ++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index d8c0ea118bb1..45d8f5c5bca9 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -1133,7 +1133,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. @@ -2278,15 +2278,35 @@ long vhost_dev_ioctl(struct vhost_dev *d, unsigned = int ioctl, void __user *argp) { struct eventfd_ctx *ctx; u64 p; - long r; + long r =3D 0; int i, fd; + u8 inherit_owner; =20 /* If you are not the owner, you can become one */ if (ioctl =3D=3D VHOST_SET_OWNER) { r =3D vhost_dev_set_owner(d); goto done; } + if (ioctl =3D=3D VHOST_FORK_FROM_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; + } + /* Validate the inherit_owner value, ensuring it is either 0 or 1 */ + if (inherit_owner > 1) { + r =3D -EINVAL; + goto done; + } + + d->inherit_owner =3D (bool)inherit_owner; =20 + 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..8f558b433536 100644 --- a/include/uapi/linux/vhost.h +++ b/include/uapi/linux/vhost.h @@ -235,4 +235,22 @@ */ #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: + * - The VHOST worker threads inherit its values/checks from + * the thread that owns the VHOST device, The vhost threads will + * be counted in the nproc rlimits. + * + * When inherit_owner is set to 0: + * - The VHOST worker threads will use the traditional kernel thread (kt= hread) + * implementation, which may be preferred by older userspace applicati= ons that + * do not utilize the newer vhost_task concept. + */ +#define VHOST_FORK_FROM_OWNER _IOW(VHOST_VIRTIO, 0x83, __u8) + #endif --=20 2.45.0 From nobody Wed Dec 17 02:55:18 2025 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 A278A20C485 for ; Sun, 23 Feb 2025 15:41:51 +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=1740325313; cv=none; b=mQ3nF5Q1mNQ4hZ1RV3fvFjH7kqmsIJOL+sHzaXcH1W4zmr0Fdh20HoGnetxytGejxOhI9+mgNqUOY4d/2ddzSHSYuG1aEv4+0BieUb/pFYa9uvpf/HaEYEPl48A1xK7HPMG/Cf8auennXirhHkDIZ2WEi2yOp3oeArkg2/ZDgOo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740325313; c=relaxed/simple; bh=ZGGr8CpUC2KsJpl4JTjnc3CvTn1h9z7S+oOYdP7bE20=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mTUP6BR7WM6ko4mEioWEdMBQ2H7erQ+mvq81WmIVe1xqHePY+FQNO3w40BV1Va8aOti5ZY085i3NqYpRvMGpILgca5b9mIsb2ni0eQUj3Wm7+HuzP6Fh1vyvitkDd+9CTnOQDzQxqP6lvSYX6QbfpTzNAgQKgXe/PoG8C0piDS8= 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=ekcyK6do; 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="ekcyK6do" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740325310; 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=qvywjBt9qiVEE9rGZyn5DkXXMEjJbM4TB/T7cbHYBEo=; b=ekcyK6doPUsgd4oGouRVmFk54JqHpXUzte+i/MqkMb7rxnppRJuSDxvELwS+aRRVsAtqOQ FfFt9NAc/sxlpJT0i+z2rEaAZJqjd+urSzU0mYIR9DNArw/fRWPqLC9SsdeGjZsajZEg1I xdApWG14J80Nx1CWd2aIYffpFNH5yzc= 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-191-B0X9Vo8lOo6-dJW4ojwo4g-1; Sun, 23 Feb 2025 10:41:47 -0500 X-MC-Unique: B0X9Vo8lOo6-dJW4ojwo4g-1 X-Mimecast-MFC-AGG-ID: B0X9Vo8lOo6-dJW4ojwo4g_1740325304 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-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 9510519783BA; Sun, 23 Feb 2025 15:41:44 +0000 (UTC) Received: from server.redhat.com (unknown [10.72.112.28]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 1EEEE1800359; Sun, 23 Feb 2025 15:41:39 +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 v6 6/6] vhost: Add check for inherit_owner status Date: Sun, 23 Feb 2025 23:36:21 +0800 Message-ID: <20250223154042.556001-7-lulu@redhat.com> In-Reply-To: <20250223154042.556001-1-lulu@redhat.com> References: <20250223154042.556001-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_NEW_WORKER requires the inherit_owner setting to be true. So we need to add a check for this. Signed-off-by: Cindy Lu Acked-by: Jason Wang 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 45d8f5c5bca9..26da561c6685 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -1017,6 +1017,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