From nobody Wed Dec 17 12:46:56 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 005252327A7 for ; Tue, 10 Dec 2024 16:45:12 +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=1733849114; cv=none; b=bhYZfp9ho0H+qeIZj+UdWY4JCjn4ky8V1rJEvVa35fWUKnSa0H97u0ps2oWjw9WhwwYHNoz3avwaZZThn5UIlX4wwkds1vt0fiz0VoNkHUINiOgQprCDhK1fCbXKXBAO55YDLmmJbGC+rTxMZBGNwgtzmUbGuKXHT9ODkUTB2nI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733849114; c=relaxed/simple; bh=jZWHDBC1em9YSxJA7OCoxzWdArjA3A0TYOkpGg7ZSRk=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mfcTtjcTM1p5om50LKaV9ZE/Y9GRx5jwbvoZnSFfYGdzPjMyU6xLxqzgWAQUA4Om2GTrHJXjDspZeEH2rGSRnNBRsQzVEvuQCgK76c7WYk+PnBX018jtyDfILIl9f0YhcAAwMwkZ1/M5G/LDkDvrNepz1hLWqbU4Km1w/Tg6iOs= 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=ELpVI56O; 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="ELpVI56O" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1733849111; 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=ELpVI56OI6M2CZEq9guCquGNQpzlo2endSyreC6tpsFM2pgdEryd2OkgpbkrxiK2a77ZA/ Sc6czGWoUHI8seAj0GjxjzPjgoYp/57/EyW1W/0WQGjOTQm4zhoVh3kLC4S5O3aojqU0B3 B+/P7e/8pUDKYb//qZZspFkrf5eUI1s= 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-689-JlGozaCIM8eXm6SCrWnjag-1; Tue, 10 Dec 2024 11:45:10 -0500 X-MC-Unique: JlGozaCIM8eXm6SCrWnjag-1 X-Mimecast-MFC-AGG-ID: JlGozaCIM8eXm6SCrWnjag Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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 B943219560BF; Tue, 10 Dec 2024 16:45:09 +0000 (UTC) Received: from server.redhat.com (unknown [10.72.112.152]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 6C9F619560A2; Tue, 10 Dec 2024 16:45:05 +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 v4 1/8] vhost: Add a new parameter in vhost_dev to allow user select kthread Date: Wed, 11 Dec 2024 00:41:40 +0800 Message-ID: <20241210164456.925060-2-lulu@redhat.com> In-Reply-To: <20241210164456.925060-1-lulu@redhat.com> References: <20241210164456.925060-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.12 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 --- 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 12:46:56 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 87751204562 for ; Tue, 10 Dec 2024 16:45:18 +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=1733849120; cv=none; b=aMsRr0W+Lw97Cg123a7w1Z+Z2mXivwcQ05jZ1n5wzsOJkD/NQkIv+IG+z5Lky+YUgrZqmr9jkd1zj7HH8d+iNkkft1rSMLr7pmEn3kwKPDNoc+NWNxrvpz06sGMDj9HhdfAqcrgdGwS9GExCctwQZQcp33eu61alFyk4kxqnYz4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733849120; c=relaxed/simple; bh=O+1OQtxPQvHz6+B0oFWBYj6LQgh9U+Y2ejH4TMx9peE=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YtpKujpDZY6bYJmw4swYzemrnlpBtDVwZaV5JvcWI9iOnL2Q4n2wMvlvfv0lD7g4Dqiz+kuISFLvhtzGAa3WV+iJZagmt//kzHrZ+ZL4fcyq3+M+3ct7ny3VqVJczfZkmgWBZ/Tr3+bpR/NQIwyMV8zEmqxhh3vd+faoteFvOng= 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=KhnQoG8G; 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="KhnQoG8G" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1733849117; 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=KhnQoG8G+2u7ksbueleidv6LG2bdoCwEjMonBwn+vqGiyBXd0jpjCRv0h6E69puXsHCKOC jlgmocgCKWlMEnlYC0QhhvuqU31fAjqHJ3tNJGRki0+uRVNbLi2Qa+zp4Ar4htImBudDu6 rdbDMGB8Iisg6Xe9/fldqZQBbibUC3I= 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-173-FvSuTkfGMTiMtPFHRedEbQ-1; Tue, 10 Dec 2024 11:45:16 -0500 X-MC-Unique: FvSuTkfGMTiMtPFHRedEbQ-1 X-Mimecast-MFC-AGG-ID: FvSuTkfGMTiMtPFHRedEbQ Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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 D583D1955F3C; Tue, 10 Dec 2024 16:45:14 +0000 (UTC) Received: from server.redhat.com (unknown [10.72.112.152]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 8E03819560A2; Tue, 10 Dec 2024 16:45:10 +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 v4 2/8] vhost: Add the vhost_worker to support kthread Date: Wed, 11 Dec 2024 00:41:41 +0800 Message-ID: <20241210164456.925060-3-lulu@redhat.com> In-Reply-To: <20241210164456.925060-1-lulu@redhat.com> References: <20241210164456.925060-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.12 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 --- 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 12:46:56 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 193332309A7 for ; Tue, 10 Dec 2024 16:45:25 +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=1733849127; cv=none; b=fjYHkXdf/RUGUzdX7eHx0POaDisX6+MA7JObDrE0CdpwbgDFoNZNKCXAAlIfl0+uv9MPgRStjvOiRpRsVmaL7j0AATmMnyFV0BPCYFbwQoooQ+Sb8cHT0I751HJ68Q/AsEO2QQHXRbsKotpvf8X5V9KzT2e4YOrkQ0TQfNAx1IM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733849127; c=relaxed/simple; bh=GjvNhNT1bUNL+PcVlyRcZE2ZFweHaD8JxyEhlL1m1vQ=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=u8FO/rXZIGMfkR7L+jtT2niFMfiINCpiVGypcQGtun6EyH2ggHWF1klaeLzeRQfx0ivbXbtx/9QqP3RKBvxHCpN3YmLDbdQ+sSfzNIbaEsaleSxj0yDtQK7zUARJgtv+S+l8VcSkUr58QTWqzvBrgOx+We512kgb3aEfA0A1Mps= 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=WcYD5Xq0; 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="WcYD5Xq0" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1733849125; 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=1rnSWUCg8d3NWTwjzGX7Xro4EhX1KK6nrpYNhJ5J6bk=; b=WcYD5Xq0XrLBIBLV5b6e8iixue2ghCJj+kQA/DLPH4APKmf/xPdqt1kivrk1y4OJAKTIJC Qnv0kJFMwRMtTY5bxZt/p8a8i+8yY0dvpZ1vDuPNyouQ/hMR4Jg+qyCLt6rWxsWZ2KNPZd Q0F7TGN5TOvVec33/0lL48sVcwqS9PM= 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-339-8-My8JCHN9aTDXDrybeBcA-1; Tue, 10 Dec 2024 11:45:21 -0500 X-MC-Unique: 8-My8JCHN9aTDXDrybeBcA-1 X-Mimecast-MFC-AGG-ID: 8-My8JCHN9aTDXDrybeBcA Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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 425BB1954AF2; Tue, 10 Dec 2024 16:45:20 +0000 (UTC) Received: from server.redhat.com (unknown [10.72.112.152]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id B003919560A7; Tue, 10 Dec 2024 16:45:15 +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 v4 3/8] vhost: Add the cgroup related function Date: Wed, 11 Dec 2024 00:41:42 +0800 Message-ID: <20241210164456.925060-4-lulu@redhat.com> In-Reply-To: <20241210164456.925060-1-lulu@redhat.com> References: <20241210164456.925060-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.12 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 --- drivers/vhost/vhost.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index 1feba29abf95..812dfd218bc2 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,38 @@ 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_flush_struct flush; + 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); + + init_completion(&flush.wait_event); + vhost_work_init(&flush.work, vhost_flush_work); + vhost_worker_queue(worker, &flush.work); + wait_for_completion(&flush.wait_event); + + return attach.ret; +} + /* Caller should have device mutex */ bool vhost_dev_has_owner(struct vhost_dev *dev) { --=20 2.45.0 From nobody Wed Dec 17 12:46:56 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 A1A621C57B2 for ; Tue, 10 Dec 2024 16:45:50 +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=1733849152; cv=none; b=lbx1RZa9bChnQd2pNjqaCqJnGb99iEc8Y6WA6+gbtXzedTF2twMJvAPyRxPGbGqM2kBacv0gz/h+IEI9enQgRy5fxxEDIZlo7MMEWAmJt7ZxRKcsd8VuE/CdRm+IYFKpIP+Ml2bk7JCZ8UHEs/qIJvByKdDRfALPSCsi82GHc2o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733849152; c=relaxed/simple; bh=4B0ZpVZ34G6urTCswIW4fjE7WC33IuSy9G1WhtjJ4oc=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RYljVcCrPvh7nJzaRYlvuFfHlSpLIQk9KvnH6KxPH5e7U/0sWtvCbVYxXwTLyJu69E6cMjUq6KHmixFLuLI/CoH25mHR3PG9Yx4mSV5DwENRosVVdh3B9048W6IdlSOHEh0tAG7rz5Hfac7UwlABHMEaFGvU6BffkmIzz4d2IzQ= 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=K/+R2g88; 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="K/+R2g88" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1733849149; 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=OjpO7WFrcW5swnLsneWZbZdM229f02z/cjRoCQsUlIs=; b=K/+R2g880fLxbqXpTIbxQAxVcBBNqpQsbJbxYGeIjLmcLPlOMI6u+RDyK0GguaDq2aDEof 095c9XoZANFWXvajDfBI9iwuqayIPH8fqsIXxcQsS7aAaxHWbXMNpoNb35pauYz/nUX7zV uGxE6NvVKJK5W4AyNfbxH5pJguZMkIY= 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-97-r9atTP5WPIGP6PDHxbghFQ-1; Tue, 10 Dec 2024 11:45:48 -0500 X-MC-Unique: r9atTP5WPIGP6PDHxbghFQ-1 X-Mimecast-MFC-AGG-ID: r9atTP5WPIGP6PDHxbghFQ Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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 95B8719541BA; Tue, 10 Dec 2024 16:45:47 +0000 (UTC) Received: from server.redhat.com (unknown [10.72.112.152]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 2556019560A7; Tue, 10 Dec 2024 16:45:42 +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 v4 4/8] vhost: Add kthread support in function vhost_worker_create Date: Wed, 11 Dec 2024 00:41:43 +0800 Message-ID: <20241210164456.925060-5-lulu@redhat.com> In-Reply-To: <20241210164456.925060-1-lulu@redhat.com> References: <20241210164456.925060-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.12 Content-Type: text/plain; charset="utf-8" Restored the previous functions kthread_wakeup and kthread_stop. Also add 2 new function pointer. The function vhost_worker_create Will initializes this pointer based on the value of inherit_owner. Signed-off-by: Cindy Lu --- drivers/vhost/vhost.c | 84 +++++++++++++++++++++++++++++++++++-------- drivers/vhost/vhost.h | 3 ++ 2 files changed, 73 insertions(+), 14 deletions(-) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index 812dfd218bc2..0175bbf4d8b3 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -721,14 +721,38 @@ static void vhost_workers_free(struct vhost_dev *dev) xa_destroy(&dev->worker_xa); } =20 +static int vhost_task_wakeup_fn(struct vhost_worker *worker) +{ + vhost_task_wake(worker->vtsk); + return 0; +} + +static int vhost_kthread_wakeup_fn(struct vhost_worker *worker) +{ + return wake_up_process(worker->kthread_task); +} + +static int vhost_task_stop_fn(struct vhost_worker *worker) +{ + vhost_task_stop(worker->vtsk); + return 0; +} + +static int vhost_kthread_stop_fn(struct vhost_worker *worker) +{ + return kthread_stop(worker->kthread_task); +} + static struct vhost_worker *vhost_worker_create(struct vhost_dev *dev) { struct vhost_worker *worker; - struct vhost_task *vtsk; + struct vhost_task *vtsk =3D NULL; + struct task_struct *task =3D NULL; char name[TASK_COMM_LEN]; int ret; u32 id; =20 + /* Allocate resources for the worker */ worker =3D kzalloc(sizeof(*worker), GFP_KERNEL_ACCOUNT); if (!worker) return NULL; @@ -736,27 +760,59 @@ static struct vhost_worker *vhost_worker_create(struc= t vhost_dev *dev) worker->dev =3D dev; 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; =20 - vhost_task_start(vtsk); + if (dev->inherit_owner) { + /* + * 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. + */ + vtsk =3D vhost_task_create(vhost_run_work_list, + vhost_worker_killed, worker, name); + if (!vtsk) + goto free_worker; + + worker->vtsk =3D vtsk; + worker->task_wakeup =3D vhost_task_wakeup_fn; + worker->task_stop =3D vhost_task_stop_fn; + + vhost_task_start(vtsk); + ret =3D xa_alloc(&dev->worker_xa, &id, worker, xa_limit_32b, + GFP_KERNEL); + if (ret < 0) + goto stop_worker; + } else { + /* Create and start a kernel thread */ + task =3D kthread_create(vhost_run_work_kthread_list, worker, + "vhost-%d", current->pid); + if (IS_ERR(task)) { + ret =3D PTR_ERR(task); + goto free_worker; + } + worker->kthread_task =3D task; + worker->task_wakeup =3D vhost_kthread_wakeup_fn; + worker->task_stop =3D vhost_kthread_stop_fn; =20 - ret =3D xa_alloc(&dev->worker_xa, &id, worker, xa_limit_32b, GFP_KERNEL); - if (ret < 0) - goto stop_worker; - worker->id =3D id; + wake_up_process(task); + ret =3D xa_alloc(&dev->worker_xa, &id, worker, xa_limit_32b, + GFP_KERNEL); + if (ret < 0) + goto stop_worker; =20 - return worker; + ret =3D vhost_attach_task_to_cgroups(worker); + if (ret) + goto stop_worker; + } =20 + worker->id =3D id; + return worker; stop_worker: - vhost_task_stop(vtsk); + worker->task_stop(worker); free_worker: kfree(worker); return NULL; diff --git a/drivers/vhost/vhost.h b/drivers/vhost/vhost.h index c650c4506c70..a7dc6e168753 100644 --- a/drivers/vhost/vhost.h +++ b/drivers/vhost/vhost.h @@ -27,6 +27,7 @@ struct vhost_work { }; =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. */ @@ -36,6 +37,8 @@ struct vhost_worker { u32 id; int attachment_cnt; bool killed; + int (*task_wakeup)(struct vhost_worker *worker); + int (*task_stop)(struct vhost_worker *worker); }; =20 /* Poll a file (eventfd or socket) */ --=20 2.45.0 From nobody Wed Dec 17 12:46:56 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 1C3F92343DB for ; Tue, 10 Dec 2024 16:45:57 +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=1733849159; cv=none; b=YeNKVIOEVOx7vVbNegzlI9kbQV/Ik+PFvnENbUop8kz/nz0BxVEh6NipK7mLJgTH6FK00BKEa/XvIfebufAZYfsBSGJa1rP3EKrzH19AimD6nJdtgUW7luVoCW9i1oUsaqx2YEyY7/kFM64TXjMI5a8zLVPa9ZxielLYl8ovgSE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733849159; c=relaxed/simple; bh=YOfWaYqxdg6bK9f7Uz3VKPypKGPg3DU2DtQ1pCaatEA=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FRkHngC2Lnw5zdDZWZW62JSAk8o9GTSEOgkxKCgyPtSi+gn9LnBUyDYGosgmxhOLmSJiG1L90U2Of/GkOQBcrt9oT5TrNWx603cjOH7Mfo/paMxNcScHyP/xYxf3SGLk3ICc8TWycH9X0hNWHeLGAxlTC/v3Et85QQ+UkP368fg= 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=LDHqBCe+; 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="LDHqBCe+" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1733849157; 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=+cFVaqbbEd48elKSFqv37dUABY9S1RJqNxZsF1midck=; b=LDHqBCe+Ul9/9vOv+seNLGNswPSjKudA9odo4QJYNZRTbCZFiokQ3uLO927pb3RrZuT0re Y5AVJ+8cJkzWFrHcOOlKeZnNM/pS5ZjNxf5id2BHXuvpbonuUm07DivyTo13iRS00wO8Kb jdS1NqUztmx2brY8LOhl388//dT97fE= 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-606-yhwMGRfEMTaFixChVawAGA-1; Tue, 10 Dec 2024 11:45:53 -0500 X-MC-Unique: yhwMGRfEMTaFixChVawAGA-1 X-Mimecast-MFC-AGG-ID: yhwMGRfEMTaFixChVawAGA Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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 C7D64195609D; Tue, 10 Dec 2024 16:45:52 +0000 (UTC) Received: from server.redhat.com (unknown [10.72.112.152]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 6BD8E19560A2; Tue, 10 Dec 2024 16:45:48 +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 v4 5/8] vhost: Add kthread support in function vhost_worker_queue() Date: Wed, 11 Dec 2024 00:41:44 +0800 Message-ID: <20241210164456.925060-6-lulu@redhat.com> In-Reply-To: <20241210164456.925060-1-lulu@redhat.com> References: <20241210164456.925060-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.12 Content-Type: text/plain; charset="utf-8" The function vhost_worker_queue() uses a function pointer in vhost_worker, which is initialized based on the inherit_owner value. Signed-off-by: Cindy Lu --- drivers/vhost/vhost.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index 0175bbf4d8b3..d1aec41bcd56 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -237,13 +237,16 @@ EXPORT_SYMBOL_GPL(vhost_poll_stop); static void vhost_worker_queue(struct vhost_worker *worker, struct vhost_work *work) { + if (!worker) + return; + if (!test_and_set_bit(VHOST_WORK_QUEUED, &work->flags)) { /* We can only add the work to the list after we're * sure it was not in the list. * test_and_set_bit() implies a memory barrier. */ llist_add(&work->node, &worker->work_list); - vhost_task_wake(worker->vtsk); + worker->task_wakeup(worker); } } =20 --=20 2.45.0 From nobody Wed Dec 17 12:46:56 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 75240234986 for ; Tue, 10 Dec 2024 16:46: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=1733849163; cv=none; b=D+oE5Vd0V1BcB91Q5OgW6K3vXuRnrCbJvZLgD7e0d7gshG0iChjictbkwClsW+3LIY5/DyC4TgRFrbESzef1yWbmZFMba+DM/1Pr3qCenhab+HcEi+ADQ9knJ9g8X5dLVYIlICBGO4j+Z3uGZNyMrBdqWPUepye3nkWmgnUvxrM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733849163; c=relaxed/simple; bh=Kp0iIIE/kSL/BU14HXYAIg5EU4ZZS6Kg8QDyOD4nqiQ=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=AcYDDZlOUBpfHmVUHxXPtUgYdRNX/3mIW3hw9wqglQrWO67g8CPWKKJ+KeVDwoXhSwoee2scw/IJumQ0H4l5EDoekCrl6MrDH2DwvfZDqRUhdXt98GjeH2FYOIjplvGQ4d3lEso2hpuHFce3AmBrBirs5Sx8OpQhxyxXvIRCFFY= 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=B30658la; 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="B30658la" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1733849160; 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=76z9zS4RFMPn2YcEr8rESUAPULegGSBBggeYhtMkPgk=; b=B30658laYUk61noh+MChVD7D64p626p+qKntprZ/kuE+ovG9WL5sWh7oNEj8wdgY0RWFij liMG5watntiaRkIILbo1hkIEYkWfCVzj+lz42/BQmvgRqQM0MsJgKxgyUJhY8EcIgjPMKB zoxPHHtfuTO2xUgY456FGbzqX7R1KsM= 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-531-66HnsfF7NO6ROf4rHCso8A-1; Tue, 10 Dec 2024 11:45:59 -0500 X-MC-Unique: 66HnsfF7NO6ROf4rHCso8A-1 X-Mimecast-MFC-AGG-ID: 66HnsfF7NO6ROf4rHCso8A Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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 E84A01954ADC; Tue, 10 Dec 2024 16:45:57 +0000 (UTC) Received: from server.redhat.com (unknown [10.72.112.152]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 9DB2C19560A2; Tue, 10 Dec 2024 16:45:53 +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 v4 6/8] vhost: Add kthread support in function vhost_worker_destroy() Date: Wed, 11 Dec 2024 00:41:45 +0800 Message-ID: <20241210164456.925060-7-lulu@redhat.com> In-Reply-To: <20241210164456.925060-1-lulu@redhat.com> References: <20241210164456.925060-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.12 Content-Type: text/plain; charset="utf-8" The function vhost_worker_destroy() uses a function pointer in vhost_worker, which is initialized based on the inherit_owner value. Signed-off-by: Cindy Lu --- drivers/vhost/vhost.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index d1aec41bcd56..3e9cb99da1b5 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -701,7 +701,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->task_stop(worker); kfree(worker); } =20 --=20 2.45.0 From nobody Wed Dec 17 12:46:56 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 B0E5F2343A2 for ; Tue, 10 Dec 2024 16:46:06 +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=1733849168; cv=none; b=Y/ynm4398+vWBqoYaqdkRF4sz/Pkf59M67XVB4JD8OY47MrVSH6jsBDdLkLmRvxTZP59bD+MhuVHk+q6sosiJ9oXWrDoyqebI+zEytnaROiCRd3hiHPVfMG3S0VRhkF74On1bftb/d1CFDOYdvmuriRCewwqhrLkFvtbwPHrAdc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733849168; c=relaxed/simple; bh=r66ZyEa0FFdOQ+sN/Yqfp2P2ct7nMHGpyLZwU1tN0m4=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JnTUZd+N31BgKkD4N5204SHDTWc66c8dQyw1TH12UocSWlzj3MpPdYeaIfnLCu1hAyIV4x5tdqTycSz1jrjzHd3LNei5qz0RXlwZheScIM0tOzg+OMieRGryGgiebOxxmH77lxU65S4L33sv6/+EWIeFovFiDmdEshoqyLUWt0Y= 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=bNZib6xj; 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="bNZib6xj" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1733849165; 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=5hDByM3oDkEGXRZwzZgdODbwmAtbhUeP50SV3gbQSuU=; b=bNZib6xj6V79hBW0x6+1EgLo9aw81d7uvpRKepwlyNfceA9RNRl2kGHAzHBi2zsrADsZWn pH3+zqMxZncW9CPhKcDICV3DllmSgmDj0g0FvSjoVH34pPyDOcgkeWFcjwgTINzDYc+bfW Ble71BOD1sHhQ5Y22BfU3bx9+htMzlU= 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-292-oPXyqo6POAybqqfOxKidoQ-1; Tue, 10 Dec 2024 11:46:04 -0500 X-MC-Unique: oPXyqo6POAybqqfOxKidoQ-1 X-Mimecast-MFC-AGG-ID: oPXyqo6POAybqqfOxKidoQ Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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 1C1471956088; Tue, 10 Dec 2024 16:46:03 +0000 (UTC) Received: from server.redhat.com (unknown [10.72.112.152]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id CBFBB19560A2; Tue, 10 Dec 2024 16:45:58 +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 v4 7/8] vhost: Add new UAPI to support change to task mode Date: Wed, 11 Dec 2024 00:41:46 +0800 Message-ID: <20241210164456.925060-8-lulu@redhat.com> In-Reply-To: <20241210164456.925060-1-lulu@redhat.com> References: <20241210164456.925060-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.12 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 --- drivers/vhost/vhost.c | 22 +++++++++++++++++++++- include/uapi/linux/vhost.h | 18 ++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index 3e9cb99da1b5..12c3bf3d1ed4 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -2257,15 +2257,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_SET_INHERIT_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..d7564d62b76d 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_SET_INHERIT_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_SET_INHERIT_FROM_OWNER _IOW(VHOST_VIRTIO, 0x83, __u8) + #endif --=20 2.45.0 From nobody Wed Dec 17 12:46:56 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 A7B4423DE93 for ; Tue, 10 Dec 2024 16:46:33 +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=1733849195; cv=none; b=SDVV/188UuhsZKzl2tNtvG9RkdFX4sT1D0SG4AkMABKF8IoDOgqLri0rm2gLuY+9+Ud/8bErvjxQ/pYr01ixgiZdEC8LwIDMMo4GoozGyLGWbwFjegNTqlSfZEJxpV/YoJE8A7Vk6D1AhYWHW3MMoLKiyhQ1lcjUHgaik8h0Giw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733849195; c=relaxed/simple; bh=ya0jLzzyRV4JPRzvVhGgoWSL9yvZjTh8Fu2M3ibVj+k=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mm9lt8cK2QY3n+NucWlOyx9ReTnR/x1ZediFA9dw2EO2bgzr9eXe1y9MMnr41kysP92tsO5lIe28jZDGTIr0ehN/GBCwDUCqVNG98NHoQNQ28QHjf9kGoncy7d/sPNIH7gvRI67TiCbhxi0MiY7QeI5KEdGxpEmRCGcB5ZGt0Sc= 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=gOA6+eYL; 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="gOA6+eYL" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1733849192; 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=7xX4qyypEHsNe4ER87SJ3OZmlnLp0lW0wrG4GS+lDAg=; b=gOA6+eYLR2k3VK8VDmTtwikxpvNC3GGtkvLNTU7W88KIw84ODHhKYHRUkbAClRK+CvK+Uh 3pEyBNxSYuLaWu22pEYZzKwpqir0iKlj95DrworZ+U1LI5Pg1v9a3mljDjFN/wFp2BmN17 VlxQsO3+as8QdUNvwWIlya035sihFxs= 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-437-PN06NUaXOWS9-zksu7JJqQ-1; Tue, 10 Dec 2024 11:46:31 -0500 X-MC-Unique: PN06NUaXOWS9-zksu7JJqQ-1 X-Mimecast-MFC-AGG-ID: PN06NUaXOWS9-zksu7JJqQ Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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 B0BFD1956057; Tue, 10 Dec 2024 16:46:30 +0000 (UTC) Received: from server.redhat.com (unknown [10.72.112.152]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 185A019560A7; Tue, 10 Dec 2024 16:46:25 +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 v4 8/8] vhost_scsi: Add check for inherit_owner status Date: Wed, 11 Dec 2024 00:41:47 +0800 Message-ID: <20241210164456.925060-9-lulu@redhat.com> In-Reply-To: <20241210164456.925060-1-lulu@redhat.com> References: <20241210164456.925060-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.12 Content-Type: text/plain; charset="utf-8" The vhost_scsi VHOST_NEW_WORKER requires the inherit_owner setting to be true. So we need to implement a check for this. Signed-off-by: Cindy Lu --- drivers/vhost/scsi.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/drivers/vhost/scsi.c b/drivers/vhost/scsi.c index 718fa4e0b31e..0d63b6b5c852 100644 --- a/drivers/vhost/scsi.c +++ b/drivers/vhost/scsi.c @@ -2086,6 +2086,14 @@ vhost_scsi_ioctl(struct file *f, return -EFAULT; return vhost_scsi_set_features(vs, features); 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 (!vs->dev.inherit_owner) + return -EFAULT; + fallthrough; case VHOST_FREE_WORKER: case VHOST_ATTACH_VRING_WORKER: case VHOST_GET_VRING_WORKER: --=20 2.45.0