Skip to contentSkip to conversation

vhost: Add support of kthread API

PATCH Superseded Cindy Lu 4 participants 6 messages Jun 16, 2025 – Jul 11, 2025

Subsystems (4)

v12 of 13 Superseded Latest is v13 →

lore
Share & tools
lore

Cindy Lu

author
#0 lore raw

v12 cover vhost: Add support of kthread API

In commit 6e890c5d5021 ("vhost: use vhost_tasks for worker threads"), the vhost now uses vhost_task and operates as a child of the owner thread. This aligns with containerization principles. However, this change has caused confusion for some legacy userspace applications. Therefore, we are reintroducing support for the kthread API.

In this series, a new UAPI is implemented to allow userspace applications to configure their thread mode.

Changelog v2:
1. Change the module_param's name to enforce_inherit_owner, and the default value is true.
2. Change the UAPI's name to VHOST_SET_INHERIT_FROM_OWNER.

Changelog v3:
1. Change the module_param's name to inherit_owner_default, and the default value is true.
2. Add a structure for task function; the worker will select a different mode based on the value inherit_owner.
3. device will have their own inherit_owner in struct vhost_dev
4. Address other comments

Changelog v4:
1. remove the module_param, only keep the UAPI
2. remove the structure for task function; change to use the function pointer in vhost_worker
3. fix the issue in vhost_worker_create and vhost_dev_ioctl
4. Address other comments

Changelog v5:
1. Change wakeup and stop function pointers in struct vhost_worker to void.
2. merging patches 4, 5, 6 in a single patch
3. Fix spelling issues and address other comments.

Changelog v6:
1. move the check of VHOST_NEW_WORKER from vhost_scsi to vhost
2. Change the ioctl name VHOST_SET_INHERIT_FROM_OWNER to VHOST_FORK_FROM_OWNER
3. reuse the function __vhost_worker_flush
4. use a ops sturct to support worker relates function
5. reset the value of inherit_owner in vhost_dev_reset_owner.

Changelog v7:
1. add a KConfig knob to disable legacy app support
2. Split the changes into two patches to separately introduce the ops and add kthread support.
3. Utilized INX_MAX to avoid modifications in __vhost_worker_flush
4. Rebased on the latest kernel
5. Address other comments

Changelog v8:
1. Rebased on the latest kernel
2. Address some other comments

Changelog v9:
1. Rebased on the latest kernel.
2. Squashed patches 6‑7.
3. Squashed patches 2‑4.
4. Minor fixes in commit log

Changelog v10:
1.Add support for the module_param.
2.Squash patches 3 and 4.
3.Make minor fixes in the commit log.
4.Fix the mismatched tabs in Kconfig.
5.Rebase on the latest kernel.

Changelog v11:
1.make the module_param under Kconfig
2.Make minor fixes in the commit log.
3.change the name inherit_owner to fork_owner
4.add NEW ioctl VHOST_GET_FORK_FROM_OWNER
5.Rebase on the latest kernel

Changelog v12:
1.Squash all patches to 1.
2.Add define for task mode and kthread mode
3.Address some other comments
4.Rebase on the latest kernel

Tested with QEMU with kthread mode/task mode/kthread+task mode

Cindy Lu (1):
vhost: Reintroduces support of kthread API and adds mode selection

 drivers/vhost/Kconfig      |  17 +++
 drivers/vhost/vhost.c      | 244 ++++++++++++++++++++++++++++++++++---
 drivers/vhost/vhost.h      |  22 ++++
 include/uapi/linux/vhost.h |  29 +++++
 4 files changed, 294 insertions(+), 18 deletions(-)
Signature · 1 line
2.45.0
author
#1 lore raw

PATCH v12 1/1 vhost: Reintroduces support of kthread API and adds mode selection

This patch reintroduces kthread mode for vhost workers and provides configuration to select between kthread and task worker.

- Add 'fork_owner' parameter to vhost_dev to let users select kthread or task mode. Default mode is task mode(VHOST_FORK_OWNER_TASK).

- Reintroduce kthread mode support:
* Bring back the original vhost_worker() implementation, and renamed to vhost_run_work_kthread_list().
* Add cgroup support for the kthread
* Introduce struct vhost_worker_ops:
- Encapsulates create / stop / wake‑up callbacks.
- vhost_worker_create() selects the proper ops according to inherit_owner.

- Userspace configuration interface:
* New IOCTLs:
- VHOST_SET_FORK_FROM_OWNER lets userspace select task mode (VHOST_FORK_OWNER_TASK) or kthread mode (VHOST_FORK_OWNER_KTHREAD)
- VHOST_GET_FORK_FROM_OWNER reads the current worker mode
* Expose module parameter 'fork_from_owner_default' to allow system administrators to configure the default mode for vhost workers
* Kconfig option CONFIG_VHOST_ENABLE_FORK_OWNER_CONTROL controls whether these IOCTLs and the parameter are available (for distros that may want to disable them)

- The VHOST_NEW_WORKER functionality requires fork_owner to be set to true, with validation added to ensure proper configuration

This partially reverts or improves upon:
commit 6e890c5d5021 ("vhost: use vhost_tasks for worker threads") commit 1cdaafa1b8b4 ("vhost: replace single worker pointer with xarray")

---
 drivers/vhost/Kconfig      |  17 +++
 drivers/vhost/vhost.c      | 244 ++++++++++++++++++++++++++++++++++---
 drivers/vhost/vhost.h      |  22 ++++
 include/uapi/linux/vhost.h |  29 +++++
 4 files changed, 294 insertions(+), 18 deletions(-)
drivers/vhost/Kconfig +17 -0
index 020d4fbb947c..1b3602b1f8e2 100644
@@ -95,4 +95,21 @@ config VHOST_CROSS_ENDIAN_LEGACY
 
 	  If unsure, say "N".
 
+config VHOST_ENABLE_FORK_OWNER_CONTROL
+	bool "Enable VHOST_ENABLE_FORK_OWNER_CONTROL"
+	default n
+	help
+	  This option enables two IOCTLs: VHOST_SET_FORK_FROM_OWNER and
+	  VHOST_GET_FORK_FROM_OWNER. These allow userspace applications
+	  to modify the vhost worker mode for vhost devices.
+
+	  Also expose module parameter 'fork_from_owner_default' to allow users
+	  to configure the default mode for vhost workers.
+
+	  By default, `VHOST_ENABLE_FORK_OWNER_CONTROL` is set to `n`,
+	  which disables the IOCTLs and parameter.
+	  When enabled (y), users can change the worker thread mode as needed.
+
+	  If unsure, say "N".
+
 endif
drivers/vhost/vhost.c +226 -18

330 lines not on this page · raw message

drivers/vhost/vhost.h +22 -0
index bb75a292d50c..ab704d84fb34 100644
@@ -26,7 +26,18 @@ struct vhost_work {
 	unsigned long		flags;
 };
 
+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;
 };
 
 /* Poll a file (eventfd or socket) */
@@ -176,6 +188,16 @@ struct vhost_dev {
 	int byte_weight;
 	struct xarray worker_xa;
 	bool use_worker;
+	/*
+	 * If fork_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.
+	 * The default value is set by fork_from_owner_default
+	 */
+	bool fork_owner;
 	int (*msg_handler)(struct vhost_dev *dev, u32 asid,
 			   struct vhost_iotlb_msg *msg);
 };
include/uapi/linux/vhost.h +29 -0
index d4b3e2ae1314..e72f2655459e 100644
@@ -235,4 +235,33 @@
  */
 #define VHOST_VDPA_GET_VRING_SIZE	_IOWR(VHOST_VIRTIO, 0x82,	\
 					      struct vhost_vring_state)
+
+/* fork_owner values for vhost */
+#define VHOST_FORK_OWNER_KTHREAD 0
+#define VHOST_FORK_OWNER_TASK 1
+
+/**
+ * VHOST_SET_FORK_FROM_OWNER - Set the fork_owner flag for the vhost device,
+ * This ioctl must called before VHOST_SET_OWNER.
+ * Only available when CONFIG_VHOST_ENABLE_FORK_OWNER_CONTROL=y
+ *
+ * @param fork_owner: An 8-bit value that determines the vhost thread mode
+ *
+ * When fork_owner is set to VHOST_FORK_OWNER_TASK(default value):
+ *   - Vhost will create vhost worker as tasks forked from the owner,
+ *     inheriting all of the owner's attributes.
+ *
+ * When fork_owner is set to VHOST_FORK_OWNER_KTHREAD:
+ *   - Vhost will create vhost workers as kernel threads.
+ */
+#define VHOST_SET_FORK_FROM_OWNER _IOW(VHOST_VIRTIO, 0x83, __u8)
+
+/**
+ * VHOST_GET_FORK_OWNER - Get the current fork_owner flag for the vhost device.
+ * Only available when CONFIG_VHOST_ENABLE_FORK_OWNER_CONTROL=y
+ *
+ * @return: An 8-bit value indicating the current thread mode.
+ */
+#define VHOST_GET_FORK_FROM_OWNER _IOR(VHOST_VIRTIO, 0x84, __u8)
+
 #endif
Signature · 1 line
2.45.0
#2 lore raw

Re: [PATCH v12 1/1] vhost: Reintroduces support of kthread API and adds mode selection

Le 16/06/2025 à 08:28, Cindy Lu a écrit :

Quoted 46 lines

This patch reintroduces kthread mode for vhost workers and provides configuration to select between kthread and task worker.

- Add 'fork_owner' parameter to vhost_dev to let users select kthread or task mode. Default mode is task mode(VHOST_FORK_OWNER_TASK).

- Reintroduce kthread mode support:
* Bring back the original vhost_worker() implementation, and renamed to vhost_run_work_kthread_list().
* Add cgroup support for the kthread
* Introduce struct vhost_worker_ops:
- Encapsulates create / stop / wake‑up callbacks.
- vhost_worker_create() selects the proper ops according to inherit_owner.

- Userspace configuration interface:
* New IOCTLs:
- VHOST_SET_FORK_FROM_OWNER lets userspace select task mode (VHOST_FORK_OWNER_TASK) or kthread mode (VHOST_FORK_OWNER_KTHREAD)
- VHOST_GET_FORK_FROM_OWNER reads the current worker mode
* Expose module parameter 'fork_from_owner_default' to allow system administrators to configure the default mode for vhost workers
* Kconfig option CONFIG_VHOST_ENABLE_FORK_OWNER_CONTROL controls whether these IOCTLs and the parameter are available (for distros that may want to disable them)

- The VHOST_NEW_WORKER functionality requires fork_owner to be set to true, with validation added to ensure proper configuration

This partially reverts or improves upon:
commit 6e890c5d5021 ("vhost: use vhost_tasks for worker threads") commit 1cdaafa1b8b4 ("vhost: replace single worker pointer with xarray")

---
 drivers/vhost/Kconfig      |  17 +++
 drivers/vhost/vhost.c      | 244 ++++++++++++++++++++++++++++++++++---
 drivers/vhost/vhost.h      |  22 ++++
 include/uapi/linux/vhost.h |  29 +++++
 4 files changed, 294 insertions(+), 18 deletions(-)
drivers/vhost/Kconfig +0 -0
index 020d4fbb947c..1b3602b1f8e2 100644

drivers/vhost/Kconfig config VHOST_CROSS_ENDIAN_LEGACY · line 100

Quoted 6 lines
drivers/vhost/Kconfig +3 -0
@@ -95,4 +95,21 @@ config VHOST_CROSS_ENDIAN_LEGACY
 
 	  If unsure, say "N".
 
+config VHOST_ENABLE_FORK_OWNER_CONTROL
+	bool "Enable VHOST_ENABLE_FORK_OWNER_CONTROL"
+	default n

Why disabling this option by default?

Regards,
Nicolas

#3 lore raw

Re: [PATCH v12 1/1] vhost: Reintroduces support of kthread API and adds mode selection

Quoted 49 lines · Nicolas Dichtel
Quoted 47 lines · Cindy Lu

This patch reintroduces kthread mode for vhost workers and provides configuration to select between kthread and task worker.

- Add 'fork_owner' parameter to vhost_dev to let users select kthread or task mode. Default mode is task mode(VHOST_FORK_OWNER_TASK).

- Reintroduce kthread mode support:
* Bring back the original vhost_worker() implementation, and renamed to vhost_run_work_kthread_list().
* Add cgroup support for the kthread
* Introduce struct vhost_worker_ops:
- Encapsulates create / stop / wake‑up callbacks.
- vhost_worker_create() selects the proper ops according to inherit_owner.

- Userspace configuration interface:
* New IOCTLs:
- VHOST_SET_FORK_FROM_OWNER lets userspace select task mode (VHOST_FORK_OWNER_TASK) or kthread mode (VHOST_FORK_OWNER_KTHREAD)
- VHOST_GET_FORK_FROM_OWNER reads the current worker mode
* Expose module parameter 'fork_from_owner_default' to allow system administrators to configure the default mode for vhost workers
* Kconfig option CONFIG_VHOST_ENABLE_FORK_OWNER_CONTROL controls whether these IOCTLs and the parameter are available (for distros that may want to disable them)

- The VHOST_NEW_WORKER functionality requires fork_owner to be set to true, with validation added to ensure proper configuration

This partially reverts or improves upon:
commit 6e890c5d5021 ("vhost: use vhost_tasks for worker threads") commit 1cdaafa1b8b4 ("vhost: replace single worker pointer with xarray")

---
 drivers/vhost/Kconfig      |  17 +++
 drivers/vhost/vhost.c      | 244 ++++++++++++++++++++++++++++++++++---
 drivers/vhost/vhost.h      |  22 ++++
 include/uapi/linux/vhost.h |  29 +++++
 4 files changed, 294 insertions(+), 18 deletions(-)
drivers/vhost/Kconfig +0 -0
index 020d4fbb947c..1b3602b1f8e2 100644
@@ -95,4 +95,21 @@ config VHOST_CROSS_ENDIAN_LEGACY
 
Quoted 6 lines

drivers/vhost/Kconfig config VHOST_CROSS_ENDIAN_LEGACY · line 100

Quoted 5 lines
drivers/vhost/Kconfig +3 -0
        If unsure, say "N".
 
+config VHOST_ENABLE_FORK_OWNER_CONTROL
+     bool "Enable VHOST_ENABLE_FORK_OWNER_CONTROL"
+     default n

Why disabling this option by default?

I think we should enable this by default.

Thanks

Quoted 4 lines

Regards,
Nicolas

author
#4 lore raw

Re: [PATCH v12 1/1] vhost: Reintroduces support of kthread API and adds mode selection

Quoted 54 lines · Jason Wang
Quoted 51 lines · Nicolas Dichtel
Quoted 49 lines · Cindy Lu

This patch reintroduces kthread mode for vhost workers and provides configuration to select between kthread and task worker.

- Add 'fork_owner' parameter to vhost_dev to let users select kthread or task mode. Default mode is task mode(VHOST_FORK_OWNER_TASK).

- Reintroduce kthread mode support:
* Bring back the original vhost_worker() implementation, and renamed to vhost_run_work_kthread_list().
* Add cgroup support for the kthread
* Introduce struct vhost_worker_ops:
- Encapsulates create / stop / wake‑up callbacks.
- vhost_worker_create() selects the proper ops according to inherit_owner.

- Userspace configuration interface:
* New IOCTLs:
- VHOST_SET_FORK_FROM_OWNER lets userspace select task mode (VHOST_FORK_OWNER_TASK) or kthread mode (VHOST_FORK_OWNER_KTHREAD)
- VHOST_GET_FORK_FROM_OWNER reads the current worker mode
* Expose module parameter 'fork_from_owner_default' to allow system administrators to configure the default mode for vhost workers
* Kconfig option CONFIG_VHOST_ENABLE_FORK_OWNER_CONTROL controls whether these IOCTLs and the parameter are available (for distros that may want to disable them)

- The VHOST_NEW_WORKER functionality requires fork_owner to be set to true, with validation added to ensure proper configuration

This partially reverts or improves upon:
commit 6e890c5d5021 ("vhost: use vhost_tasks for worker threads") commit 1cdaafa1b8b4 ("vhost: replace single worker pointer with xarray")

---
 drivers/vhost/Kconfig      |  17 +++
 drivers/vhost/vhost.c      | 244 ++++++++++++++++++++++++++++++++++---
 drivers/vhost/vhost.h      |  22 ++++
 include/uapi/linux/vhost.h |  29 +++++
 4 files changed, 294 insertions(+), 18 deletions(-)
drivers/vhost/Kconfig +0 -0
index 020d4fbb947c..1b3602b1f8e2 100644
@@ -95,4 +95,21 @@ config VHOST_CROSS_ENDIAN_LEGACY
 
        If unsure, say "N".
 
Quoted 9 lines
Quoted 4 lines

drivers/vhost/Kconfig config VHOST_CROSS_ENDIAN_LEGACY · line 100

Quoted 3 lines
drivers/vhost/Kconfig +3 -0
+config VHOST_ENABLE_FORK_OWNER_CONTROL
+     bool "Enable VHOST_ENABLE_FORK_OWNER_CONTROL"
+     default n

Why disabling this option by default?

I think we should enable this by default.

Thanks

Thanks jason, I will send a new version
Thanks
cindy

Quoted 5 lines
Quoted 4 lines

Regards,
Nicolas

Tested-by on the v12 series
#5 lore raw
In reply to Cindy Lu

Tested this series of patches with virtio-net regression tests, everything works fine.

Quoted 92 lines · Cindy Lu

In commit 6e890c5d5021 ("vhost: use vhost_tasks for worker threads"), the vhost now uses vhost_task and operates as a child of the owner thread. This aligns with containerization principles. However, this change has caused confusion for some legacy userspace applications. Therefore, we are reintroducing support for the kthread API.

In this series, a new UAPI is implemented to allow userspace applications to configure their thread mode.

Changelog v2:
1. Change the module_param's name to enforce_inherit_owner, and the default value is true.
2. Change the UAPI's name to VHOST_SET_INHERIT_FROM_OWNER.

Changelog v3:
1. Change the module_param's name to inherit_owner_default, and the default value is true.
2. Add a structure for task function; the worker will select a different mode based on the value inherit_owner.
3. device will have their own inherit_owner in struct vhost_dev
4. Address other comments

Changelog v4:
1. remove the module_param, only keep the UAPI
2. remove the structure for task function; change to use the function pointer in vhost_worker
3. fix the issue in vhost_worker_create and vhost_dev_ioctl
4. Address other comments

Changelog v5:
1. Change wakeup and stop function pointers in struct vhost_worker to void.
2. merging patches 4, 5, 6 in a single patch
3. Fix spelling issues and address other comments.

Changelog v6:
1. move the check of VHOST_NEW_WORKER from vhost_scsi to vhost
2. Change the ioctl name VHOST_SET_INHERIT_FROM_OWNER to VHOST_FORK_FROM_OWNER
3. reuse the function __vhost_worker_flush
4. use a ops sturct to support worker relates function
5. reset the value of inherit_owner in vhost_dev_reset_owner.

Changelog v7:
1. add a KConfig knob to disable legacy app support
2. Split the changes into two patches to separately introduce the ops and add kthread support.
3. Utilized INX_MAX to avoid modifications in __vhost_worker_flush
4. Rebased on the latest kernel
5. Address other comments

Changelog v8:
1. Rebased on the latest kernel
2. Address some other comments

Changelog v9:
1. Rebased on the latest kernel.
2. Squashed patches 6‑7.
3. Squashed patches 2‑4.
4. Minor fixes in commit log

Changelog v10:
1.Add support for the module_param.
2.Squash patches 3 and 4.
3.Make minor fixes in the commit log.
4.Fix the mismatched tabs in Kconfig.
5.Rebase on the latest kernel.

Changelog v11:
1.make the module_param under Kconfig
2.Make minor fixes in the commit log.
3.change the name inherit_owner to fork_owner
4.add NEW ioctl VHOST_GET_FORK_FROM_OWNER
5.Rebase on the latest kernel

Changelog v12:
1.Squash all patches to 1.
2.Add define for task mode and kthread mode
3.Address some other comments
4.Rebase on the latest kernel

Tested with QEMU with kthread mode/task mode/kthread+task mode

Cindy Lu (1):
vhost: Reintroduces support of kthread API and adds mode selection

 drivers/vhost/Kconfig      |  17 +++
 drivers/vhost/vhost.c      | 244 ++++++++++++++++++++++++++++++++++---
 drivers/vhost/vhost.h      |  22 ++++
 include/uapi/linux/vhost.h |  29 +++++
 4 files changed, 294 insertions(+), 18 deletions(-)

--
2.45.0