Skip to contentSkip to conversation

vhost: Add support of kthread API

PATCH Superseded Cindy Lu 4 participants 28 messages Apr 21, 2025 – May 19, 2025

Subsystems (4)

v9 of 13 Superseded Latest is v13 →

lore
Share & tools
lore

Cindy Lu

author
#0 lore raw

v9 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

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

Cindy Lu (4):
vhost: Add a new parameter in vhost_dev to allow user select kthread
vhost: Reintroduce kthread mode support in vhost
vhost: add VHOST_FORK_FROM_OWNER ioctl and validate inherit_owner
vhost: Add a KConfig knob to enable IOCTL VHOST_FORK_FROM_OWNER

 drivers/vhost/Kconfig      |  15 +++
 drivers/vhost/vhost.c      | 219 +++++++++++++++++++++++++++++++++----
 drivers/vhost/vhost.h      |  21 ++++
 include/uapi/linux/vhost.h |  16 +++
 4 files changed, 252 insertions(+), 19 deletions(-)
Signature · 1 line
2.45.0
author
#1 lore raw

PATCH v9 1/4 vhost: Add a new parameter in vhost_dev to allow user select kthread

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 applications, therefore, we are reintroducing kthread API support.

Introduce a new parameter to enable users to choose between kthread and task mode.

By default, this parameter is set to true, so the default behavior remains unchanged by this patch.

---
 drivers/vhost/vhost.c | 1 +
 drivers/vhost/vhost.h | 9 +++++++++
 2 files changed, 10 insertions(+)
drivers/vhost/vhost.c +1 -0
index 63612faeab72..250dc43f1786 100644
@@ -552,6 +552,7 @@ void vhost_dev_init(struct vhost_dev *dev,
 	dev->byte_weight = byte_weight;
 	dev->use_worker = use_worker;
 	dev->msg_handler = msg_handler;
+	dev->inherit_owner = true;
 	init_waitqueue_head(&dev->wait);
 	INIT_LIST_HEAD(&dev->read_list);
 	INIT_LIST_HEAD(&dev->pending_list);
drivers/vhost/vhost.h +9 -0
index bb75a292d50c..19bb94922a0e 100644
@@ -176,6 +176,15 @@ struct vhost_dev {
 	int byte_weight;
 	struct xarray worker_xa;
 	bool use_worker;
+	/*
+	 * If inherit_owner is true we use vhost_tasks to create
+	 * the worker so all settings/limits like cgroups, NPROC,
+	 * scheduler, etc are inherited from the owner. If false,
+	 * we use kthreads and only attach to the same cgroups
+	 * as the owner for compat with older kernels.
+	 * here we use true as default value
+	 */
+	bool inherit_owner;
 	int (*msg_handler)(struct vhost_dev *dev, u32 asid,
 			   struct vhost_iotlb_msg *msg);
 };
Signature · 1 line
2.45.0
Acked-by on v9 1/4
#2 lore raw

Re: [PATCH v9 1/4] vhost: Add a new parameter in vhost_dev to allow user select kthread

Quoted 6 lines · Cindy Lu

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 applications, therefore, we are reintroducing kthread API support.

Quoted 7 lines

Introduce a new parameter to enable users to choose between kthread and task mode.

By default, this parameter is set to true, so the default behavior remains unchanged by this patch.

Thanks

Reviewed-by on v9 1/4
#3 lore raw

Re: [PATCH v9 1/4] vhost: Add a new parameter in vhost_dev to allow user select kthread

In reply to Cindy Lu
Quoted 8 lines · Cindy Lu

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 applications, therefore, we are reintroducing kthread API support.

Introduce a new parameter to enable users to choose between kthread and task mode.

Quoted 8 lines

By default, this parameter is set to true, so the default behavior remains unchanged by this patch.

---
drivers/vhost/vhost.c | 1 +
drivers/vhost/vhost.h | 9 +++++++++

2 files changed, 10 insertions(+)
Quoted 36 lines
drivers/vhost/vhost.c +1 -0
index 63612faeab72..250dc43f1786 100644
@@ -552,6 +552,7 @@ void vhost_dev_init(struct vhost_dev *dev,
 	dev->byte_weight = byte_weight;
 	dev->use_worker = use_worker;
 	dev->msg_handler = msg_handler;
+	dev->inherit_owner = true;
 	init_waitqueue_head(&dev->wait);
 	INIT_LIST_HEAD(&dev->read_list);
 	INIT_LIST_HEAD(&dev->pending_list);
drivers/vhost/vhost.h +9 -0
index bb75a292d50c..19bb94922a0e 100644
@@ -176,6 +176,15 @@ struct vhost_dev {
 	int byte_weight;
 	struct xarray worker_xa;
 	bool use_worker;
+	/*
+	 * If inherit_owner is true we use vhost_tasks to create
+	 * the worker so all settings/limits like cgroups, NPROC,
+	 * scheduler, etc are inherited from the owner. If false,
+	 * we use kthreads and only attach to the same cgroups
+	 * as the owner for compat with older kernels.
+	 * here we use true as default value
+	 */
+	bool inherit_owner;
 	int (*msg_handler)(struct vhost_dev *dev, u32 asid,
 			   struct vhost_iotlb_msg *msg);
 };
Signature · 1 line
2.45.0
author
#4 lore raw

PATCH v9 2/4 vhost: Reintroduce kthread mode support in vhost

In reply to Cindy Lu

This patch reintroduces kthread mode support in vhost, It also introduces struct vhost_worker_ops to abstract worker create/stop/wakeup operations.

* 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.

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/vhost.c | 188 ++++++++++++++++++++++++++++++++++++++----
 drivers/vhost/vhost.h |  12 +++
 2 files changed, 182 insertions(+), 18 deletions(-)
drivers/vhost/vhost.c +170 -18
index 250dc43f1786..be97028a8baf 100644
@@ -22,6 +22,7 @@
 #include <linux/slab.h>
 #include <linux/vmalloc.h>
 #include <linux/kthread.h>
+#include <linux/cgroup.h>
 #include <linux/module.h>
 #include <linux/sort.h>
 #include <linux/sched/mm.h>
@@ -242,7 +243,7 @@ static void vhost_worker_queue(struct vhost_worker *worker,
 		 * test_and_set_bit() implies a memory barrier.
 		 */
 		llist_add(&work->node, &worker->work_list);
-		vhost_task_wake(worker->vtsk);
+		worker->ops->wakeup(worker);
 	}
 }
 
@@ -388,6 +389,44 @@ static void vhost_vq_reset(struct vhost_dev *dev,
 	__vhost_vq_meta_reset(vq);
 }
 
+static int vhost_run_work_kthread_list(void *data)
+{
+	struct vhost_worker *worker = data;
+	struct vhost_work *work, *work_next;
+	struct vhost_dev *dev = 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 = llist_del_all(&worker->work_list);
+		if (!node)
+			schedule();
+
+		node = 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 = data;
@@ -582,6 +621,46 @@ long vhost_dev_check_owner(struct vhost_dev *dev)
 }
 EXPORT_SYMBOL_GPL(vhost_dev_check_owner);
 
+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 = container_of(work, struct vhost_attach_cgroups_struct, work);
+	s->ret = cgroup_attach_task_all(s->owner, current);
+}
+
+static int vhost_attach_task_to_cgroups(struct vhost_worker *worker)
+{
+	struct vhost_attach_cgroups_struct attach;
+	int saved_cnt;
+
+	attach.owner = current;
+
+	vhost_work_init(&attach.work, vhost_attach_cgroups_work);
+	vhost_worker_queue(worker, &attach.work);
+
+	mutex_lock(&worker->mutex);
+
+	/*
+	 * Bypass attachment_cnt check in __vhost_worker_flush:
+	 * Temporarily change it to INT_MAX to bypass the check
+	 */
+	saved_cnt = worker->attachment_cnt;
+	worker->attachment_cnt = INT_MAX;
+	__vhost_worker_flush(worker);
+	worker->attachment_cnt = saved_cnt;
+
+	mutex_unlock(&worker->mutex);
+
+	return attach.ret;
+}
+
 /* Caller should have device mutex */
 bool vhost_dev_has_owner(struct vhost_dev *dev)
 {
@@ -627,7 +706,7 @@ static void vhost_worker_destroy(struct vhost_dev *dev,
 
 	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);
 }
 
@@ -650,42 +729,115 @@ static void vhost_workers_free(struct vhost_dev *dev)
 	xa_destroy(&dev->worker_xa);
 }
 
+static void vhost_task_wakeup(struct vhost_worker *worker)
+{
+	return vhost_task_wake(worker->vtsk);
+}
+
+static void vhost_kthread_wakeup(struct vhost_worker *worker)
+{
+	wake_up_process(worker->kthread_task);
+}
+
+static void vhost_task_do_stop(struct vhost_worker *worker)
+{
+	return vhost_task_stop(worker->vtsk);
+}
+
+static void vhost_kthread_do_stop(struct vhost_worker *worker)
+{
+	kthread_stop(worker->kthread_task);
+}
+
+static int vhost_task_worker_create(struct vhost_worker *worker,
+				    struct vhost_dev *dev, const char *name)
+{
+	struct vhost_task *vtsk;
+	u32 id;
+	int ret;
+
+	vtsk = vhost_task_create(vhost_run_work_list, vhost_worker_killed,
+				 worker, name);
+	if (IS_ERR(vtsk))
+		return PTR_ERR(vtsk);
+
+	worker->vtsk = vtsk;
+	vhost_task_start(vtsk);
+	ret = xa_alloc(&dev->worker_xa, &id, worker, xa_limit_32b, GFP_KERNEL);
+	if (ret < 0) {
+		vhost_task_do_stop(worker);
+		return ret;
+	}
+	worker->id = id;
+	return 0;
+}
+
+static int vhost_kthread_worker_create(struct vhost_worker *worker,
+				       struct vhost_dev *dev, const char *name)
+{
+	struct task_struct *task;
+	u32 id;
+	int ret;
+
+	task = kthread_create(vhost_run_work_kthread_list, worker, "%s", name);
+	if (IS_ERR(task))
+		return PTR_ERR(task);
+
+	worker->kthread_task = task;
+	wake_up_process(task);
+	ret = xa_alloc(&dev->worker_xa, &id, worker, xa_limit_32b, GFP_KERNEL);
+	if (ret < 0)
+		goto stop_worker;
+
+	ret = vhost_attach_task_to_cgroups(worker);
+	if (ret)
+		goto stop_worker;
+
+	worker->id = id;
+	return 0;
+
+stop_worker:
+	vhost_kthread_do_stop(worker);
+	return ret;
+}
+
+static const struct vhost_worker_ops kthread_ops = {
+	.create = vhost_kthread_worker_create,
+	.stop = vhost_kthread_do_stop,
+	.wakeup = vhost_kthread_wakeup,
+};
+
+static const struct vhost_worker_ops vhost_task_ops = {
+	.create = vhost_task_worker_create,
+	.stop = vhost_task_do_stop,
+	.wakeup = vhost_task_wakeup,
+};
+
 static struct vhost_worker *vhost_worker_create(struct vhost_dev *dev)
 {
 	struct vhost_worker *worker;
-	struct vhost_task *vtsk;
 	char name[TASK_COMM_LEN];
 	int ret;
-	u32 id;
+	const struct vhost_worker_ops *ops =
+		dev->inherit_owner ? &vhost_task_ops : &kthread_ops;
 
 	worker = kzalloc(sizeof(*worker), GFP_KERNEL_ACCOUNT);
 	if (!worker)
 		return NULL;
 
 	worker->dev = dev;
+	worker->ops = ops;
 	snprintf(name, sizeof(name), "vhost-%d", current->pid);
 
-	vtsk = vhost_task_create(vhost_run_work_list, vhost_worker_killed,
-				 worker, name);
-	if (IS_ERR(vtsk))
-		goto free_worker;
-
 	mutex_init(&worker->mutex);
 	init_llist_head(&worker->work_list);
 	worker->kcov_handle = kcov_common_handle();
-	worker->vtsk = vtsk;
-
-	vhost_task_start(vtsk);
-
-	ret = xa_alloc(&dev->worker_xa, &id, worker, xa_limit_32b, GFP_KERNEL);
+	ret = ops->create(worker, dev, name);
 	if (ret < 0)
-		goto stop_worker;
-	worker->id = id;
+		goto free_worker;
 
 	return worker;
 
-stop_worker:
-	vhost_task_stop(vtsk);
 free_worker:
 	kfree(worker);
 	return NULL;
drivers/vhost/vhost.h +12 -0
index 19bb94922a0e..af4b2f7d3b91 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) */
Signature · 1 line
2.45.0
#5 lore raw

Re: [PATCH v9 2/4] vhost: Reintroduce kthread mode support in vhost

Quoted 119 lines · Cindy Lu

This patch reintroduces kthread mode support in vhost, It also introduces struct vhost_worker_ops to abstract worker create/stop/wakeup operations.

* 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.

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/vhost.c | 188 ++++++++++++++++++++++++++++++++++++++----
 drivers/vhost/vhost.h |  12 +++
 2 files changed, 182 insertions(+), 18 deletions(-)
drivers/vhost/vhost.c +64 -1
index 250dc43f1786..be97028a8baf 100644
@@ -22,6 +22,7 @@
 #include <linux/slab.h>
 #include <linux/vmalloc.h>
 #include <linux/kthread.h>
+#include <linux/cgroup.h>
 #include <linux/module.h>
 #include <linux/sort.h>
 #include <linux/sched/mm.h>
@@ -242,7 +243,7 @@ static void vhost_worker_queue(struct vhost_worker *worker,
                 * test_and_set_bit() implies a memory barrier.
                 */
                llist_add(&work->node, &worker->work_list);
-               vhost_task_wake(worker->vtsk);
+               worker->ops->wakeup(worker);
        }
 }
 
@@ -388,6 +389,44 @@ static void vhost_vq_reset(struct vhost_dev *dev,
        __vhost_vq_meta_reset(vq);
 }
 
+static int vhost_run_work_kthread_list(void *data)
+{
+       struct vhost_worker *worker = data;
+       struct vhost_work *work, *work_next;
+       struct vhost_dev *dev = 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 = llist_del_all(&worker->work_list);
+               if (!node)
+                       schedule();
+
+               node = 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 = data;
@@ -582,6 +621,46 @@ long vhost_dev_check_owner(struct vhost_dev *dev)
 }
 EXPORT_SYMBOL_GPL(vhost_dev_check_owner);
 
+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 = container_of(work, struct vhost_attach_cgroups_struct, work);
+       s->ret = cgroup_attach_task_all(s->owner, current);
+}
+
+static int vhost_attach_task_to_cgroups(struct vhost_worker *worker)
+{
+       struct vhost_attach_cgroups_struct attach;
+       int saved_cnt;
+
+       attach.owner = current;
+
+       vhost_work_init(&attach.work, vhost_attach_cgroups_work);
+       vhost_worker_queue(worker, &attach.work);
+

drivers/vhost/vhost.c long vhost_dev_check_owner(struct vhost_dev *dev) · line 657

Quoted 10 lines
drivers/vhost/vhost.c +10 -0
+       mutex_lock(&worker->mutex);
+
+       /*
+        * Bypass attachment_cnt check in __vhost_worker_flush:
+        * Temporarily change it to INT_MAX to bypass the check
+        */
+       saved_cnt = worker->attachment_cnt;
+       worker->attachment_cnt = INT_MAX;
+       __vhost_worker_flush(worker);
+       worker->attachment_cnt = saved_cnt;

I wonder if it's easier to re-introduce the flush that was used before vhost kthread to avoid the tricks here. We can have flush ops for example.

Thanks

#6 lore raw

Re: [PATCH v9 2/4] vhost: Reintroduce kthread mode support in vhost

Quoted 135 lines · Jason Wang
Quoted 119 lines · Cindy Lu

This patch reintroduces kthread mode support in vhost, It also introduces struct vhost_worker_ops to abstract worker create/stop/wakeup operations.

* 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.

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/vhost.c | 188 ++++++++++++++++++++++++++++++++++++++----
 drivers/vhost/vhost.h |  12 +++
 2 files changed, 182 insertions(+), 18 deletions(-)
drivers/vhost/vhost.c +64 -1
index 250dc43f1786..be97028a8baf 100644
@@ -22,6 +22,7 @@
 #include <linux/slab.h>
 #include <linux/vmalloc.h>
 #include <linux/kthread.h>
+#include <linux/cgroup.h>
 #include <linux/module.h>
 #include <linux/sort.h>
 #include <linux/sched/mm.h>
@@ -242,7 +243,7 @@ static void vhost_worker_queue(struct vhost_worker *worker,
                 * test_and_set_bit() implies a memory barrier.
                 */
                llist_add(&work->node, &worker->work_list);
-               vhost_task_wake(worker->vtsk);
+               worker->ops->wakeup(worker);
        }
 }
 
@@ -388,6 +389,44 @@ static void vhost_vq_reset(struct vhost_dev *dev,
        __vhost_vq_meta_reset(vq);
 }
 
+static int vhost_run_work_kthread_list(void *data)
+{
+       struct vhost_worker *worker = data;
+       struct vhost_work *work, *work_next;
+       struct vhost_dev *dev = 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 = llist_del_all(&worker->work_list);
+               if (!node)
+                       schedule();
+
+               node = 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 = data;
@@ -582,6 +621,46 @@ long vhost_dev_check_owner(struct vhost_dev *dev)
 }
 EXPORT_SYMBOL_GPL(vhost_dev_check_owner);
 
+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 = container_of(work, struct vhost_attach_cgroups_struct, work);
+       s->ret = cgroup_attach_task_all(s->owner, current);
+}
+
+static int vhost_attach_task_to_cgroups(struct vhost_worker *worker)
+{
+       struct vhost_attach_cgroups_struct attach;
+       int saved_cnt;
+
+       attach.owner = current;
+
+       vhost_work_init(&attach.work, vhost_attach_cgroups_work);
+       vhost_worker_queue(worker, &attach.work);
+

drivers/vhost/vhost.c long vhost_dev_check_owner(struct vhost_dev *dev) · line 657

Quoted 10 lines
drivers/vhost/vhost.c +10 -0
+       mutex_lock(&worker->mutex);
+
+       /*
+        * Bypass attachment_cnt check in __vhost_worker_flush:
+        * Temporarily change it to INT_MAX to bypass the check
+        */
+       saved_cnt = worker->attachment_cnt;
+       worker->attachment_cnt = INT_MAX;
+       __vhost_worker_flush(worker);
+       worker->attachment_cnt = saved_cnt;

I wonder if it's easier to re-introduce the flush that was used before vhost kthread to avoid the tricks here. We can have flush ops for example.

Quoted 1 line

Thanks

Nah we do not need ops, __vhost_worker_flush is just an internal function. Refactor it so we can call the part without the check.

Signature · 1 line
MST
#7 lore raw

Re: [PATCH v9 2/4] vhost: Reintroduce kthread mode support in vhost

In reply to Cindy Lu
Quoted 118 lines · Cindy Lu

This patch reintroduces kthread mode support in vhost, It also introduces struct vhost_worker_ops to abstract worker create/stop/wakeup operations.

* 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.

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/vhost.c | 188 ++++++++++++++++++++++++++++++++++++++----
 drivers/vhost/vhost.h |  12 +++
 2 files changed, 182 insertions(+), 18 deletions(-)
drivers/vhost/vhost.c +64 -1
index 250dc43f1786..be97028a8baf 100644
@@ -22,6 +22,7 @@
 #include <linux/slab.h>
 #include <linux/vmalloc.h>
 #include <linux/kthread.h>
+#include <linux/cgroup.h>
 #include <linux/module.h>
 #include <linux/sort.h>
 #include <linux/sched/mm.h>
@@ -242,7 +243,7 @@ static void vhost_worker_queue(struct vhost_worker *worker,
 		 * test_and_set_bit() implies a memory barrier.
 		 */
 		llist_add(&work->node, &worker->work_list);
-		vhost_task_wake(worker->vtsk);
+		worker->ops->wakeup(worker);
 	}
 }
 
@@ -388,6 +389,44 @@ static void vhost_vq_reset(struct vhost_dev *dev,
 	__vhost_vq_meta_reset(vq);
 }
 
+static int vhost_run_work_kthread_list(void *data)
+{
+	struct vhost_worker *worker = data;
+	struct vhost_work *work, *work_next;
+	struct vhost_dev *dev = 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 = llist_del_all(&worker->work_list);
+		if (!node)
+			schedule();
+
+		node = 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 = data;
@@ -582,6 +621,46 @@ long vhost_dev_check_owner(struct vhost_dev *dev)
 }
 EXPORT_SYMBOL_GPL(vhost_dev_check_owner);
 
+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 = container_of(work, struct vhost_attach_cgroups_struct, work);
+	s->ret = cgroup_attach_task_all(s->owner, current);
+}
+
+static int vhost_attach_task_to_cgroups(struct vhost_worker *worker)
+{
+	struct vhost_attach_cgroups_struct attach;
+	int saved_cnt;
+
+	attach.owner = current;
+
+	vhost_work_init(&attach.work, vhost_attach_cgroups_work);
+	vhost_worker_queue(worker, &attach.work);
+

drivers/vhost/vhost.c long vhost_dev_check_owner(struct vhost_dev *dev) · line 657

Quoted 10 lines
drivers/vhost/vhost.c +10 -0
+	mutex_lock(&worker->mutex);
+
+	/*
+	 * Bypass attachment_cnt check in __vhost_worker_flush:
+	 * Temporarily change it to INT_MAX to bypass the check
+	 */
+	saved_cnt = worker->attachment_cnt;
+	worker->attachment_cnt = INT_MAX;
+	__vhost_worker_flush(worker);
+	worker->attachment_cnt = saved_cnt;

You mean this one?

        if (!worker->attachment_cnt || worker->killed)
                return;

Just introduce a variant of __vhost_worker_flush that skips this check.

E.g.

Rename __vhost_worker_flush -> _vhost_worker_flush.

then rework:

static void _vhost_worker_flush(struct vhost_worker *worker)

{
        struct vhost_flush_struct flush; 
                                  
        if (!worker->attachment_cnt || worker->killed)
                return;

	__vhost_worker_flush(worker);
}
Quoted 183 lines
drivers/vhost/vhost.c +96 -17
+
+	mutex_unlock(&worker->mutex);
+
+	return attach.ret;
+}
+
 /* Caller should have device mutex */
 bool vhost_dev_has_owner(struct vhost_dev *dev)
 {
@@ -627,7 +706,7 @@ static void vhost_worker_destroy(struct vhost_dev *dev,
 
 	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);
 }
 
@@ -650,42 +729,115 @@ static void vhost_workers_free(struct vhost_dev *dev)
 	xa_destroy(&dev->worker_xa);
 }
 
+static void vhost_task_wakeup(struct vhost_worker *worker)
+{
+	return vhost_task_wake(worker->vtsk);
+}
+
+static void vhost_kthread_wakeup(struct vhost_worker *worker)
+{
+	wake_up_process(worker->kthread_task);
+}
+
+static void vhost_task_do_stop(struct vhost_worker *worker)
+{
+	return vhost_task_stop(worker->vtsk);
+}
+
+static void vhost_kthread_do_stop(struct vhost_worker *worker)
+{
+	kthread_stop(worker->kthread_task);
+}
+
+static int vhost_task_worker_create(struct vhost_worker *worker,
+				    struct vhost_dev *dev, const char *name)
+{
+	struct vhost_task *vtsk;
+	u32 id;
+	int ret;
+
+	vtsk = vhost_task_create(vhost_run_work_list, vhost_worker_killed,
+				 worker, name);
+	if (IS_ERR(vtsk))
+		return PTR_ERR(vtsk);
+
+	worker->vtsk = vtsk;
+	vhost_task_start(vtsk);
+	ret = xa_alloc(&dev->worker_xa, &id, worker, xa_limit_32b, GFP_KERNEL);
+	if (ret < 0) {
+		vhost_task_do_stop(worker);
+		return ret;
+	}
+	worker->id = id;
+	return 0;
+}
+
+static int vhost_kthread_worker_create(struct vhost_worker *worker,
+				       struct vhost_dev *dev, const char *name)
+{
+	struct task_struct *task;
+	u32 id;
+	int ret;
+
+	task = kthread_create(vhost_run_work_kthread_list, worker, "%s", name);
+	if (IS_ERR(task))
+		return PTR_ERR(task);
+
+	worker->kthread_task = task;
+	wake_up_process(task);
+	ret = xa_alloc(&dev->worker_xa, &id, worker, xa_limit_32b, GFP_KERNEL);
+	if (ret < 0)
+		goto stop_worker;
+
+	ret = vhost_attach_task_to_cgroups(worker);
+	if (ret)
+		goto stop_worker;
+
+	worker->id = id;
+	return 0;
+
+stop_worker:
+	vhost_kthread_do_stop(worker);
+	return ret;
+}
+
+static const struct vhost_worker_ops kthread_ops = {
+	.create = vhost_kthread_worker_create,
+	.stop = vhost_kthread_do_stop,
+	.wakeup = vhost_kthread_wakeup,
+};
+
+static const struct vhost_worker_ops vhost_task_ops = {
+	.create = vhost_task_worker_create,
+	.stop = vhost_task_do_stop,
+	.wakeup = vhost_task_wakeup,
+};
+
 static struct vhost_worker *vhost_worker_create(struct vhost_dev *dev)
 {
 	struct vhost_worker *worker;
-	struct vhost_task *vtsk;
 	char name[TASK_COMM_LEN];
 	int ret;
-	u32 id;
+	const struct vhost_worker_ops *ops =
+		dev->inherit_owner ? &vhost_task_ops : &kthread_ops;
 
 	worker = kzalloc(sizeof(*worker), GFP_KERNEL_ACCOUNT);
 	if (!worker)
 		return NULL;
 
 	worker->dev = dev;
+	worker->ops = ops;
 	snprintf(name, sizeof(name), "vhost-%d", current->pid);
 
-	vtsk = vhost_task_create(vhost_run_work_list, vhost_worker_killed,
-				 worker, name);
-	if (IS_ERR(vtsk))
-		goto free_worker;
-
 	mutex_init(&worker->mutex);
 	init_llist_head(&worker->work_list);
 	worker->kcov_handle = kcov_common_handle();
-	worker->vtsk = vtsk;
-
-	vhost_task_start(vtsk);
-
-	ret = xa_alloc(&dev->worker_xa, &id, worker, xa_limit_32b, GFP_KERNEL);
+	ret = ops->create(worker, dev, name);
 	if (ret < 0)
-		goto stop_worker;
-	worker->id = id;
+		goto free_worker;
 
 	return worker;
 
-stop_worker:
-	vhost_task_stop(vtsk);
 free_worker:
 	kfree(worker);
 	return NULL;
drivers/vhost/vhost.h +12 -0
index 19bb94922a0e..af4b2f7d3b91 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) */
Signature · 1 line
2.45.0
author
#8 lore raw

PATCH v9 3/4 vhost: add VHOST_FORK_FROM_OWNER ioctl and validate inherit_owner

In reply to Cindy Lu

Add a new UAPI to configure the vhost device to use the kthread mode. The userspace application can use IOCTL VHOST_FORK_FROM_OWNER to choose between owner and kthread mode if necessary. This setting must be applied before VHOST_SET_OWNER, as the worker will be created in the VHOST_SET_OWNER function.

In addition, the VHOST_NEW_WORKER requires the inherit_owner setting to be true. So we need to add a check for this.

---
 drivers/vhost/vhost.c      | 29 +++++++++++++++++++++++++++--
 include/uapi/linux/vhost.h | 16 ++++++++++++++++
 2 files changed, 43 insertions(+), 2 deletions(-)
drivers/vhost/vhost.c +27 -2
index be97028a8baf..fb0c7fb43f78 100644
@@ -1018,6 +1018,13 @@ long vhost_worker_ioctl(struct vhost_dev *dev, unsigned 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 = vhost_new_worker(dev, &state);
 		if (!ret && copy_to_user(argp, &state, sizeof(state)))
 			ret = -EFAULT;
@@ -1134,7 +1141,7 @@ void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_iotlb *umem)
 	int i;
 
 	vhost_dev_cleanup(dev);
-
+	dev->inherit_owner = true;
 	dev->umem = umem;
 	/* We don't need VQ locks below since vhost_dev_cleanup makes sure
 	 * VQs aren't running.
@@ -2287,7 +2294,25 @@ long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
 		r = vhost_dev_set_owner(d);
 		goto done;
 	}
-
+	if (ioctl == VHOST_FORK_FROM_OWNER) {
+		u8 inherit_owner;
+		/*inherit_owner can only be modified before owner is set*/
+		if (vhost_dev_has_owner(d)) {
+			r = -EBUSY;
+			goto done;
+		}
+		if (copy_from_user(&inherit_owner, argp, sizeof(u8))) {
+			r = -EFAULT;
+			goto done;
+		}
+		if (inherit_owner > 1) {
+			r = -EINVAL;
+			goto done;
+		}
+		d->inherit_owner = (bool)inherit_owner;
+		r = 0;
+		goto done;
+	}
 	/* You must be the owner to do anything else */
 	r = vhost_dev_check_owner(d);
 	if (r)
include/uapi/linux/vhost.h +16 -0
index b95dd84eef2d..1ae0917bfeca 100644
@@ -235,4 +235,20 @@
  */
 #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,
+ * This ioctl must called before VHOST_SET_OWNER.
+ *
+ * @param inherit_owner: An 8-bit value that determines the vhost thread mode
+ *
+ * When inherit_owner is set to 1(default value):
+ *   - Vhost will create tasks similar to processes forked from the owner,
+ *     inheriting all of the owner's attributes.
+ *
+ * When inherit_owner is set to 0:
+ *   - Vhost will create tasks as kernel thread.
+ */
+#define VHOST_FORK_FROM_OWNER _IOW(VHOST_VIRTIO, 0x83, __u8)
+
 #endif
Signature · 1 line
2.45.0
Acked-by on v9 3/4
#9 lore raw

Re: [PATCH v9 3/4] vhost: add VHOST_FORK_FROM_OWNER ioctl and validate inherit_owner

Quoted 11 lines · Cindy Lu

Add a new UAPI to configure the vhost device to use the kthread mode. The userspace application can use IOCTL VHOST_FORK_FROM_OWNER to choose between owner and kthread mode if necessary. This setting must be applied before VHOST_SET_OWNER, as the worker will be created in the VHOST_SET_OWNER function.

In addition, the VHOST_NEW_WORKER requires the inherit_owner setting to be true. So we need to add a check for this.

Thanks

#10 lore raw

Re: [PATCH v9 3/4] vhost: add VHOST_FORK_FROM_OWNER ioctl and validate inherit_owner

In reply to Cindy Lu
Quoted 34 lines · Cindy Lu

Add a new UAPI to configure the vhost device to use the kthread mode. The userspace application can use IOCTL VHOST_FORK_FROM_OWNER to choose between owner and kthread mode if necessary. This setting must be applied before VHOST_SET_OWNER, as the worker will be created in the VHOST_SET_OWNER function.

In addition, the VHOST_NEW_WORKER requires the inherit_owner setting to be true. So we need to add a check for this.

---
drivers/vhost/vhost.c | 29 +++++++++++++++++++++++++++--
include/uapi/linux/vhost.h | 16 ++++++++++++++++

2 files changed, 43 insertions(+), 2 deletions(-)
drivers/vhost/vhost.c +7 -0
index be97028a8baf..fb0c7fb43f78 100644
@@ -1018,6 +1018,13 @@ long vhost_worker_ioctl(struct vhost_dev *dev, unsigned 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 = vhost_new_worker(dev, &state);
 		if (!ret && copy_to_user(argp, &state, sizeof(state)))
 			ret = -EFAULT;

drivers/vhost/vhost.c void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_iotlb *umem) · line 1137

Quoted 4 lines
drivers/vhost/vhost.c +0 -1
@@ -1134,7 +1141,7 @@ void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_iotlb *umem)
 	int i;
 
 	vhost_dev_cleanup(dev);
-

nit: I'd avoid removing this empty line, it helps while reading code.

Quoted 18 lines
drivers/vhost/vhost.c +10 -1
+	dev->inherit_owner = true;
 	dev->umem = umem;
 	/* We don't need VQ locks below since vhost_dev_cleanup makes sure
 	 * VQs aren't running.
@@ -2287,7 +2294,25 @@ long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
 		r = vhost_dev_set_owner(d);
 		goto done;
 	}
-
+	if (ioctl == VHOST_FORK_FROM_OWNER) {
+		u8 inherit_owner;
+		/*inherit_owner can only be modified before owner is set*/
+		if (vhost_dev_has_owner(d)) {
+			r = -EBUSY;
+			goto done;
+		}
+		if (copy_from_user(&inherit_owner, argp, sizeof(u8))) {
+			r = -EFAULT;

drivers/vhost/vhost.c long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp) · line 2315

Quoted 10 lines
drivers/vhost/vhost.c +10 -0
+			goto done;
+		}
+		if (inherit_owner > 1) {
+			r = -EINVAL;
+			goto done;
+		}
+		d->inherit_owner = (bool)inherit_owner;
+		r = 0;
+		goto done;
+	}

Ditto, an empty like should help to separate to the next step.

Quoted 8 lines
drivers/vhost/vhost.c +0 -0
 	/* You must be the owner to do anything else */
 	r = vhost_dev_check_owner(d);
 	if (r)
include/uapi/linux/vhost.h +0 -0
index b95dd84eef2d..1ae0917bfeca 100644

include/uapi/linux/vhost.h line 241

Quoted 7 lines
include/uapi/linux/vhost.h +4 -0
@@ -235,4 +235,20 @@
 */
 #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,
+ * This ioctl must called before VHOST_SET_OWNER.

Shoud we mention that this IOCTL is only supported if CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL is y?

Quoted 16 lines
include/uapi/linux/vhost.h +12 -0
+ *
+ * @param inherit_owner: An 8-bit value that determines the vhost thread mode
+ *
+ * When inherit_owner is set to 1(default value):
+ *   - Vhost will create tasks similar to processes forked from the owner,
+ *     inheriting all of the owner's attributes.
+ *
+ * When inherit_owner is set to 0:
+ *   - Vhost will create tasks as kernel thread.
+ */
+#define VHOST_FORK_FROM_OWNER _IOW(VHOST_VIRTIO, 0x83, __u8)
+
 #endif
Signature · 1 line
2.45.0
author
#11 lore raw

PATCH v9 4/4 vhost: Add a KConfig knob to enable IOCTL VHOST_FORK_FROM_OWNER

In reply to Cindy Lu

Introduce a new config knob CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL, to control the availability of the VHOST_FORK_FROM_OWNER ioctl. When CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL is set to n, the ioctl is disabled, and any attempt to use it will result in failure.

---
 drivers/vhost/Kconfig | 15 +++++++++++++++
 drivers/vhost/vhost.c |  3 +++
 2 files changed, 18 insertions(+)
drivers/vhost/Kconfig +15 -0
index 020d4fbb947c..bc8fadb06f98 100644
@@ -96,3 +96,18 @@ config VHOST_CROSS_ENDIAN_LEGACY
 	  If unsure, say "N".
 
 endif
+
+config VHOST_ENABLE_FORK_OWNER_IOCTL
+	bool "Enable IOCTL VHOST_FORK_FROM_OWNER"
+	default n
+	help
+	  This option enables the IOCTL VHOST_FORK_FROM_OWNER, which allows
+	  userspace applications to modify the thread mode for vhost devices.
+
+          By default, `CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL` is set to `n`,
+          meaning the ioctl is disabled and any operation using this ioctl
+          will fail.
+          When the configuration is enabled (y), the ioctl becomes
+          available, allowing users to set the mode if needed.
+
+	  If unsure, say "N".
drivers/vhost/vhost.c +3 -0
index fb0c7fb43f78..568e43cb54a9 100644
@@ -2294,6 +2294,8 @@ long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
 		r = vhost_dev_set_owner(d);
 		goto done;
 	}
+
+#ifdef CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL
 	if (ioctl == VHOST_FORK_FROM_OWNER) {
 		u8 inherit_owner;
 		/*inherit_owner can only be modified before owner is set*/
@@ -2313,6 +2315,7 @@ long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
 		r = 0;
 		goto done;
 	}
+#endif
 	/* You must be the owner to do anything else */
 	r = vhost_dev_check_owner(d);
 	if (r)
Signature · 1 line
2.45.0
#12 lore raw

Re: [PATCH v9 4/4] vhost: Add a KConfig knob to enable IOCTL VHOST_FORK_FROM_OWNER

Quoted 5 lines · Cindy Lu

Introduce a new config knob CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL, to control the availability of the VHOST_FORK_FROM_OWNER ioctl. When CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL is set to n, the ioctl is disabled, and any attempt to use it will result in failure.

I think we need to describe why the default value was chosen to be false.

What's more, should we document the implications here?

inherit_owner was set to false: this means "legacy" userspace may still be broken unless it can do VHOST_FORK_FROM_OWNER which is impractical. Does this defeat the purpose of this series actually?

Quoted 52 lines
---
 drivers/vhost/Kconfig | 15 +++++++++++++++
 drivers/vhost/vhost.c |  3 +++
 2 files changed, 18 insertions(+)
drivers/vhost/Kconfig +15 -0
index 020d4fbb947c..bc8fadb06f98 100644
@@ -96,3 +96,18 @@ config VHOST_CROSS_ENDIAN_LEGACY
          If unsure, say "N".
 
 endif
+
+config VHOST_ENABLE_FORK_OWNER_IOCTL
+       bool "Enable IOCTL VHOST_FORK_FROM_OWNER"
+       default n
+       help
+         This option enables the IOCTL VHOST_FORK_FROM_OWNER, which allows
+         userspace applications to modify the thread mode for vhost devices.
+
+          By default, `CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL` is set to `n`,
+          meaning the ioctl is disabled and any operation using this ioctl
+          will fail.
+          When the configuration is enabled (y), the ioctl becomes
+          available, allowing users to set the mode if needed.
+
+         If unsure, say "N".
drivers/vhost/vhost.c +3 -1
index fb0c7fb43f78..568e43cb54a9 100644
@@ -2294,6 +2294,8 @@ long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
                r = vhost_dev_set_owner(d);
                goto done;
        }
+
+#ifdef CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL
        if (ioctl == VHOST_FORK_FROM_OWNER) {
                u8 inherit_owner;
                /*inherit_owner can only be modified before owner is set*/
@@ -2313,6 +2315,7 @@ long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
                r = 0;
                goto done;
        }
+#endif
        /* You must be the owner to do anything else */
        r = vhost_dev_check_owner(d);
        if (r)
--
Quoted 2 lines

2.45.0

Thanks

#13 lore raw

Re: [PATCH v9 4/4] vhost: Add a KConfig knob to enable IOCTL VHOST_FORK_FROM_OWNER

Quoted 8 lines · Jason Wang
Quoted 5 lines · Cindy Lu

Introduce a new config knob CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL, to control the availability of the VHOST_FORK_FROM_OWNER ioctl. When CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL is set to n, the ioctl is disabled, and any attempt to use it will result in failure.

Quoted 5 lines

I think we need to describe why the default value was chosen to be false.

What's more, should we document the implications here?

inherit_owner was set to false: this means "legacy" userspace may

I meant "true" actually.

Quoted 51 lines

still be broken unless it can do VHOST_FORK_FROM_OWNER which is impractical. Does this defeat the purpose of this series actually?

Quoted 48 lines
---
 drivers/vhost/Kconfig | 15 +++++++++++++++
 drivers/vhost/vhost.c |  3 +++
 2 files changed, 18 insertions(+)
drivers/vhost/Kconfig +15 -0
index 020d4fbb947c..bc8fadb06f98 100644
@@ -96,3 +96,18 @@ config VHOST_CROSS_ENDIAN_LEGACY
          If unsure, say "N".
 
 endif
+
+config VHOST_ENABLE_FORK_OWNER_IOCTL
+       bool "Enable IOCTL VHOST_FORK_FROM_OWNER"
+       default n
+       help
+         This option enables the IOCTL VHOST_FORK_FROM_OWNER, which allows
+         userspace applications to modify the thread mode for vhost devices.
+
+          By default, `CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL` is set to `n`,
+          meaning the ioctl is disabled and any operation using this ioctl
+          will fail.
+          When the configuration is enabled (y), the ioctl becomes
+          available, allowing users to set the mode if needed.
+
+         If unsure, say "N".
drivers/vhost/vhost.c +3 -0
index fb0c7fb43f78..568e43cb54a9 100644
@@ -2294,6 +2294,8 @@ long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
                r = vhost_dev_set_owner(d);
                goto done;
        }
+
+#ifdef CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL
        if (ioctl == VHOST_FORK_FROM_OWNER) {
                u8 inherit_owner;
                /*inherit_owner can only be modified before owner is set*/
@@ -2313,6 +2315,7 @@ long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
                r = 0;
                goto done;
        }
+#endif
Quoted 8 lines
Quoted 6 lines
drivers/vhost/vhost.c +0 -1
        /* You must be the owner to do anything else */
        r = vhost_dev_check_owner(d);
        if (r)
--

2.45.0

Thanks

Thanks

#14 lore raw

Re: [PATCH v9 4/4] vhost: Add a KConfig knob to enable IOCTL VHOST_FORK_FROM_OWNER

Quoted 10 lines · Jason Wang
Quoted 8 lines · Jason Wang
Quoted 5 lines · Cindy Lu

Introduce a new config knob CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL, to control the availability of the VHOST_FORK_FROM_OWNER ioctl. When CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL is set to n, the ioctl is disabled, and any attempt to use it will result in failure.

Quoted 7 lines
Quoted 5 lines

I think we need to describe why the default value was chosen to be false.

What's more, should we document the implications here?

inherit_owner was set to false: this means "legacy" userspace may

I meant "true" actually.

MIchael, I'd expect inherit_owner to be false. Otherwise legacy applications need to be modified in order to get the behaviour recovered which is an impossible taks.

Any idea on this?

Thanks

#15 lore raw

Re: [PATCH v9 4/4] vhost: Add a KConfig knob to enable IOCTL VHOST_FORK_FROM_OWNER

Quoted 19 lines · Jason Wang
Quoted 10 lines · Jason Wang
Quoted 8 lines · Jason Wang
Quoted 5 lines · Cindy Lu

Introduce a new config knob CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL, to control the availability of the VHOST_FORK_FROM_OWNER ioctl. When CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL is set to n, the ioctl is disabled, and any attempt to use it will result in failure.

Quoted 7 lines
Quoted 5 lines

I think we need to describe why the default value was chosen to be false.

What's more, should we document the implications here?

inherit_owner was set to false: this means "legacy" userspace may

I meant "true" actually.

Quoted 7 lines

MIchael, I'd expect inherit_owner to be false. Otherwise legacy applications need to be modified in order to get the behaviour recovered which is an impossible taks.

Any idea on this?

Thanks

At this point, as we changed the behaviour, we have two types of legacy applications
- ones expecting inherit_owner false
- ones expecting inherit_owner true

Whatever we do, some of these will have to be changed.
Given current
kernel has it as true, and given it is a cleaner behaviour that will keep working when we disable CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL in 10 years, I think it's the better default.
If you want to change it transparently, look for ways to distinguish between the two types.

The application in question is qemu, is it not?
I do not see how sticking an ioctl call into its source is such a big deal, if this is what we want to do.
A bit of short term pain but we get clear maintainable semantics.

Signature · 1 line
MST
#16 lore raw

Re: [PATCH v9 4/4] vhost: Add a KConfig knob to enable IOCTL VHOST_FORK_FROM_OWNER

Quoted 25 lines · Michael S. Tsirkin
Quoted 23 lines · Jason Wang
Quoted 10 lines · Jason Wang
Quoted 8 lines · Jason Wang
Quoted 5 lines · Cindy Lu

Introduce a new config knob CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL, to control the availability of the VHOST_FORK_FROM_OWNER ioctl. When CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL is set to n, the ioctl is disabled, and any attempt to use it will result in failure.

Quoted 7 lines
Quoted 5 lines

I think we need to describe why the default value was chosen to be false.

What's more, should we document the implications here?

inherit_owner was set to false: this means "legacy" userspace may

I meant "true" actually.

MIchael, I'd expect inherit_owner to be false. Otherwise legacy applications need to be modified in order to get the behaviour recovered which is an impossible taks.

Quoted 7 lines
Quoted 3 lines

Any idea on this?

Thanks

At this point, as we changed the behaviour, we have two types of legacy applications
- ones expecting inherit_owner false
- ones expecting inherit_owner true

Considering vhost has been used for more than a decade, I would expect more will expect inhert_owner to be false.

Quoted 2 lines

Whatever we do, some of these will have to be changed.

If we must choose one to break, I'd expect to break less.

Quoted 4 lines

Given current
kernel has it as true, and given it is a cleaner behaviour that will keep working when we disable CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL in 10 years, I think it's the better default.

The problem is, if we set it to true and only an ioctl will bring the old behavior back. Who will use this ioctl? We can't modify all legacy applications.

Quoted 4 lines

If you want to change it transparently, look for ways to distinguish between the two types.

The application in question is qemu, is it not?

Looks not, it's the application or management layer that tries to set the affinity to the owner of the vhost.

For example, if I start a testpmd + vhost_net and pinn testpmd runs on cpu0. I will get half of the performance since vhost will contend with cpu with testpmd.

Quoted 3 lines

I do not see how sticking an ioctl call into its source is such a big deal, if this is what we want to do.
A bit of short term pain but we get clear maintainable semantics.

What's more important, the way that introduces new fork behaviors without a new uAPI is a bug. We need to fix that by introducing new uAPI for the behaviour. This seems to be the short term pain instead of introducing new uAPI for the old behaviour.

Thanks

Quoted 4 lines

--
MST

#17 lore raw

Re: [PATCH v9 4/4] vhost: Add a KConfig knob to enable IOCTL VHOST_FORK_FROM_OWNER

Quoted 22 lines · Jason Wang
Quoted 21 lines · Michael S. Tsirkin
Quoted 19 lines · Jason Wang
Quoted 17 lines · Jason Wang
Quoted 8 lines · Jason Wang
Quoted 5 lines · Cindy Lu

Introduce a new config knob CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL, to control the availability of the VHOST_FORK_FROM_OWNER ioctl. When CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL is set to n, the ioctl is disabled, and any attempt to use it will result in failure.

Quoted 5 lines

I think we need to describe why the default value was chosen to be false.

What's more, should we document the implications here?

inherit_owner was set to false: this means "legacy" userspace may

I meant "true" actually.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

MIchael, I'd expect inherit_owner to be false. Otherwise legacy applications need to be modified in order to get the behaviour recovered which is an impossible taks.

Any idea on this?

Thanks

So, let's say we had a modparam? Enough for this customer? WDYT?

Signature · 1 line
MST
#18 lore raw

Re: [PATCH v9 4/4] vhost: Add a KConfig knob to enable IOCTL VHOST_FORK_FROM_OWNER

Quoted 28 lines · Michael S. Tsirkin
Quoted 26 lines · Jason Wang
Quoted 25 lines · Michael S. Tsirkin
Quoted 23 lines · Jason Wang
Quoted 10 lines · Jason Wang
Quoted 8 lines · Jason Wang
Quoted 5 lines · Cindy Lu

Introduce a new config knob CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL, to control the availability of the VHOST_FORK_FROM_OWNER ioctl. When CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL is set to n, the ioctl is disabled, and any attempt to use it will result in failure.

Quoted 7 lines
Quoted 5 lines

I think we need to describe why the default value was chosen to be false.

What's more, should we document the implications here?

inherit_owner was set to false: this means "legacy" userspace may

I meant "true" actually.

MIchael, I'd expect inherit_owner to be false. Otherwise legacy applications need to be modified in order to get the behaviour recovered which is an impossible taks.

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Any idea on this?

Thanks

So, let's say we had a modparam? Enough for this customer? WDYT?

Just to make sure I understand the proposal.

Did you mean a module parameter like "inherit_owner_by_default"? I think it would be fine if we make it false by default.

Thanks

Quoted 4 lines

--
MST

#19 lore raw

Re: [PATCH v9 4/4] vhost: Add a KConfig knob to enable IOCTL VHOST_FORK_FROM_OWNER

Quoted 36 lines · Jason Wang
Quoted 28 lines · Michael S. Tsirkin
Quoted 26 lines · Jason Wang
Quoted 25 lines · Michael S. Tsirkin
Quoted 23 lines · Jason Wang
Quoted 10 lines · Jason Wang
Quoted 8 lines · Jason Wang
Quoted 5 lines · Cindy Lu

Introduce a new config knob CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL, to control the availability of the VHOST_FORK_FROM_OWNER ioctl. When CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL is set to n, the ioctl is disabled, and any attempt to use it will result in failure.

Quoted 7 lines
Quoted 5 lines

I think we need to describe why the default value was chosen to be false.

What's more, should we document the implications here?

inherit_owner was set to false: this means "legacy" userspace may

I meant "true" actually.

MIchael, I'd expect inherit_owner to be false. Otherwise legacy applications need to be modified in order to get the behaviour recovered which is an impossible taks.

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Any idea on this?

Thanks

So, let's say we had a modparam? Enough for this customer? WDYT?

Quoted 6 lines

Just to make sure I understand the proposal.

Did you mean a module parameter like "inherit_owner_by_default"? I think it would be fine if we make it false by default.

Thanks

I think we should keep it true by default, changing the default risks regressing what we already fixes. The specific customer can flip the modparam and be happy.

Quoted 4 lines
Quoted 4 lines

--
MST

#20 lore raw

Re: [PATCH v9 4/4] vhost: Add a KConfig knob to enable IOCTL VHOST_FORK_FROM_OWNER

Quoted 40 lines · Michael S. Tsirkin
Quoted 38 lines · Jason Wang
Quoted 28 lines · Michael S. Tsirkin
Quoted 26 lines · Jason Wang
Quoted 25 lines · Michael S. Tsirkin
Quoted 23 lines · Jason Wang
Quoted 10 lines · Jason Wang
Quoted 8 lines · Jason Wang
Quoted 5 lines · Cindy Lu

Introduce a new config knob CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL, to control the availability of the VHOST_FORK_FROM_OWNER ioctl. When CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL is set to n, the ioctl is disabled, and any attempt to use it will result in failure.

Quoted 7 lines
Quoted 5 lines

I think we need to describe why the default value was chosen to be false.

What's more, should we document the implications here?

inherit_owner was set to false: this means "legacy" userspace may

I meant "true" actually.

MIchael, I'd expect inherit_owner to be false. Otherwise legacy applications need to be modified in order to get the behaviour recovered which is an impossible taks.

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Any idea on this?

Thanks

So, let's say we had a modparam? Enough for this customer? WDYT?

Just to make sure I understand the proposal.

Quoted 7 lines
Quoted 4 lines

Did you mean a module parameter like "inherit_owner_by_default"? I think it would be fine if we make it false by default.

Thanks

I think we should keep it true by default, changing the default risks regressing what we already fixes.

I think it's not a regression since it comes since the day vhost is introduced. To my understanding the real regression is the user space noticeable behaviour changes introduced by vhost thread.

Quoted 2 lines

The specific customer can
flip the modparam and be happy.

If you stick to the false as default, I'm fine.

Thanks

Quoted 6 lines
Quoted 5 lines
Quoted 5 lines

--
MST

author
#21 lore raw

Re: [PATCH v9 4/4] vhost: Add a KConfig knob to enable IOCTL VHOST_FORK_FROM_OWNER

Thank you for the comments; I will prepare a new patch version.

Thanks,
Cindy

Quoted 68 lines · Jason Wang
Quoted 40 lines · Michael S. Tsirkin
Quoted 38 lines · Jason Wang
Quoted 28 lines · Michael S. Tsirkin
Quoted 26 lines · Jason Wang
Quoted 25 lines · Michael S. Tsirkin
Quoted 23 lines · Jason Wang
Quoted 10 lines · Jason Wang
Quoted 8 lines · Jason Wang
Quoted 5 lines · Cindy Lu

Introduce a new config knob CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL, to control the availability of the VHOST_FORK_FROM_OWNER ioctl. When CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL is set to n, the ioctl is disabled, and any attempt to use it will result in failure.

Quoted 7 lines
Quoted 5 lines

I think we need to describe why the default value was chosen to be false.

What's more, should we document the implications here?

inherit_owner was set to false: this means "legacy" userspace may

I meant "true" actually.

MIchael, I'd expect inherit_owner to be false. Otherwise legacy applications need to be modified in order to get the behaviour recovered which is an impossible taks.

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Any idea on this?

Thanks

So, let's say we had a modparam? Enough for this customer? WDYT?

Just to make sure I understand the proposal.

Quoted 7 lines
Quoted 4 lines

Did you mean a module parameter like "inherit_owner_by_default"? I think it would be fine if we make it false by default.

Thanks

I think we should keep it true by default, changing the default risks regressing what we already fixes.

I think it's not a regression since it comes since the day vhost is introduced. To my understanding the real regression is the user space noticeable behaviour changes introduced by vhost thread.

Quoted 2 lines

The specific customer can
flip the modparam and be happy.

If you stick to the false as default, I'm fine.

Thanks

Quoted 6 lines
Quoted 5 lines
Quoted 5 lines

--
MST

#22 lore raw

Re: [PATCH v9 4/4] vhost: Add a KConfig knob to enable IOCTL VHOST_FORK_FROM_OWNER

In reply to Jason Wang
Quoted 53 lines · Jason Wang
Quoted 40 lines · Michael S. Tsirkin
Quoted 38 lines · Jason Wang
Quoted 28 lines · Michael S. Tsirkin
Quoted 26 lines · Jason Wang
Quoted 25 lines · Michael S. Tsirkin
Quoted 23 lines · Jason Wang
Quoted 10 lines · Jason Wang
Quoted 8 lines · Jason Wang
Quoted 5 lines · Cindy Lu

Introduce a new config knob CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL, to control the availability of the VHOST_FORK_FROM_OWNER ioctl. When CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL is set to n, the ioctl is disabled, and any attempt to use it will result in failure.

Quoted 7 lines
Quoted 5 lines

I think we need to describe why the default value was chosen to be false.

What's more, should we document the implications here?

inherit_owner was set to false: this means "legacy" userspace may

I meant "true" actually.

MIchael, I'd expect inherit_owner to be false. Otherwise legacy applications need to be modified in order to get the behaviour recovered which is an impossible taks.

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Any idea on this?

Thanks

So, let's say we had a modparam? Enough for this customer? WDYT?

Just to make sure I understand the proposal.

Quoted 7 lines
Quoted 4 lines

Did you mean a module parameter like "inherit_owner_by_default"? I think it would be fine if we make it false by default.

Thanks

I think we should keep it true by default, changing the default risks regressing what we already fixes.

I think it's not a regression since it comes since the day vhost is introduced. To my understanding the real regression is the user space noticeable behaviour changes introduced by vhost thread.

Quoted 6 lines
Quoted 2 lines

The specific customer can
flip the modparam and be happy.

If you stick to the false as default, I'm fine.

Thanks

That would be yet another behaviour change.
I think one was enough, don't you think?

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines

--
MST

#23 lore raw

Re: [PATCH v9 4/4] vhost: Add a KConfig knob to enable IOCTL VHOST_FORK_FROM_OWNER

Quoted 55 lines · Michael S. Tsirkin
Quoted 53 lines · Jason Wang
Quoted 40 lines · Michael S. Tsirkin
Quoted 38 lines · Jason Wang
Quoted 28 lines · Michael S. Tsirkin
Quoted 26 lines · Jason Wang
Quoted 25 lines · Michael S. Tsirkin
Quoted 23 lines · Jason Wang
Quoted 10 lines · Jason Wang
Quoted 8 lines · Jason Wang
Quoted 5 lines · Cindy Lu

Introduce a new config knob CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL, to control the availability of the VHOST_FORK_FROM_OWNER ioctl. When CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL is set to n, the ioctl is disabled, and any attempt to use it will result in failure.

Quoted 7 lines
Quoted 5 lines

I think we need to describe why the default value was chosen to be false.

What's more, should we document the implications here?

inherit_owner was set to false: this means "legacy" userspace may

I meant "true" actually.

MIchael, I'd expect inherit_owner to be false. Otherwise legacy applications need to be modified in order to get the behaviour recovered which is an impossible taks.

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Any idea on this?

Thanks

So, let's say we had a modparam? Enough for this customer? WDYT?

Just to make sure I understand the proposal.

Quoted 7 lines
Quoted 4 lines

Did you mean a module parameter like "inherit_owner_by_default"? I think it would be fine if we make it false by default.

Thanks

I think we should keep it true by default, changing the default risks regressing what we already fixes.

I think it's not a regression since it comes since the day vhost is introduced. To my understanding the real regression is the user space noticeable behaviour changes introduced by vhost thread.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

The specific customer can
flip the modparam and be happy.

If you stick to the false as default, I'm fine.

Thanks

That would be yet another behaviour change.

Back to the original behaviour.

Quoted 1 line

I think one was enough, don't you think?

I think such kind of change is unavoidable if we want to fix the usersapce behaviour change.

Thanks

Quoted 9 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 7 lines

--
MST

#24 lore raw

Re: [PATCH v9 4/4] vhost: Add a KConfig knob to enable IOCTL VHOST_FORK_FROM_OWNER

Quoted 59 lines · Jason Wang
Quoted 58 lines · Michael S. Tsirkin
Quoted 56 lines · Jason Wang
Quoted 40 lines · Michael S. Tsirkin
Quoted 38 lines · Jason Wang
Quoted 28 lines · Michael S. Tsirkin
Quoted 26 lines · Jason Wang
Quoted 25 lines · Michael S. Tsirkin
Quoted 23 lines · Jason Wang
Quoted 10 lines · Jason Wang
Quoted 8 lines · Jason Wang
Quoted 5 lines · Cindy Lu

Introduce a new config knob CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL, to control the availability of the VHOST_FORK_FROM_OWNER ioctl. When CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL is set to n, the ioctl is disabled, and any attempt to use it will result in failure.

Quoted 7 lines
Quoted 5 lines

I think we need to describe why the default value was chosen to be false.

What's more, should we document the implications here?

inherit_owner was set to false: this means "legacy" userspace may

I meant "true" actually.

MIchael, I'd expect inherit_owner to be false. Otherwise legacy applications need to be modified in order to get the behaviour recovered which is an impossible taks.

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Any idea on this?

Thanks

So, let's say we had a modparam? Enough for this customer? WDYT?

Just to make sure I understand the proposal.

Quoted 7 lines
Quoted 4 lines

Did you mean a module parameter like "inherit_owner_by_default"? I think it would be fine if we make it false by default.

Thanks

I think we should keep it true by default, changing the default risks regressing what we already fixes.

I think it's not a regression since it comes since the day vhost is introduced. To my understanding the real regression is the user space noticeable behaviour changes introduced by vhost thread.

Quoted 2 lines

The specific customer can
flip the modparam and be happy.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

If you stick to the false as default, I'm fine.

Thanks

That would be yet another behaviour change.

Back to the original behaviour.

yes but the original was also a bugfix.

Quoted 6 lines
Quoted 1 line

I think one was enough, don't you think?

I think such kind of change is unavoidable if we want to fix the usersapce behaviour change.

Thanks

I feel it is too late to "fix". the new behaviour is generally ok, and I feel the right thing so to give management control knobs do pick the desired behaviour.
And really modparam is wrong here because different userspace can have different requirements, and in ~10 years I want to see us disable the legacy behaviour altogether.
But given your time constraints, a modparam knob as a quick workaround for the specific customer is kind of not very terrible.

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 7 lines

--
MST

#25 lore raw

Re: [PATCH v9 4/4] vhost: Add a KConfig knob to enable IOCTL VHOST_FORK_FROM_OWNER

Quoted 78 lines · Michael S. Tsirkin
Quoted 59 lines · Jason Wang
Quoted 58 lines · Michael S. Tsirkin
Quoted 56 lines · Jason Wang
Quoted 40 lines · Michael S. Tsirkin
Quoted 38 lines · Jason Wang
Quoted 28 lines · Michael S. Tsirkin
Quoted 26 lines · Jason Wang
Quoted 25 lines · Michael S. Tsirkin
Quoted 23 lines · Jason Wang
Quoted 10 lines · Jason Wang
Quoted 8 lines · Jason Wang
Quoted 5 lines · Cindy Lu

Introduce a new config knob `CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL`,
to control the availability of the `VHOST_FORK_FROM_OWNER` ioctl.
When CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL is set to n, the ioctl
is disabled, and any attempt to use it will result in failure.

Quoted 7 lines
Quoted 5 lines

I think we need to describe why the default value was chosen to be false.

What's more, should we document the implications here?

inherit_owner was set to false: this means "legacy" userspace may

I meant "true" actually.

MIchael, I'd expect inherit_owner to be false. Otherwise legacy applications need to be modified in order to get the behaviour recovered which is an impossible taks.

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Any idea on this?

Thanks

So, let's say we had a modparam? Enough for this customer? WDYT?

Just to make sure I understand the proposal.

Quoted 7 lines
Quoted 4 lines

Did you mean a module parameter like "inherit_owner_by_default"? I think it would be fine if we make it false by default.

Thanks

I think we should keep it true by default, changing the default risks regressing what we already fixes.

I think it's not a regression since it comes since the day vhost is introduced. To my understanding the real regression is the user space noticeable behaviour changes introduced by vhost thread.

Quoted 2 lines

The specific customer can
flip the modparam and be happy.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

If you stick to the false as default, I'm fine.

Thanks

That would be yet another behaviour change.

Back to the original behaviour.

yes but the original was also a bugfix.

Quoted 6 lines
Quoted 1 line

I think one was enough, don't you think?

I think such kind of change is unavoidable if we want to fix the usersapce behaviour change.

Thanks

Quoted 9 lines

I feel it is too late to "fix". the new behaviour is generally ok, and I feel the right thing so to give management control knobs do pick the desired behaviour.
And really modparam is wrong here because different userspace can have different requirements, and in ~10 years I want to see us disable the legacy behaviour altogether.
But given your time constraints, a modparam knob as a quick workaround for the specific customer is kind of not very terrible.

Ok, that makes sense.

Thanks

#26 lore raw

Re: [PATCH v9 4/4] vhost: Add a KConfig knob to enable IOCTL VHOST_FORK_FROM_OWNER

In reply to Cindy Lu
Quoted 22 lines · Cindy Lu

Introduce a new config knob CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL, to control the availability of the VHOST_FORK_FROM_OWNER ioctl. When CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL is set to n, the ioctl is disabled, and any attempt to use it will result in failure.

---
drivers/vhost/Kconfig | 15 +++++++++++++++
drivers/vhost/vhost.c | 3 +++

2 files changed, 18 insertions(+)
drivers/vhost/Kconfig +0 -0
index 020d4fbb947c..bc8fadb06f98 100644
@@ -96,3 +96,18 @@ config VHOST_CROSS_ENDIAN_LEGACY
 	  If unsure, say "N".
 

endif

drivers/vhost/Kconfig +3 -0
+
+config VHOST_ENABLE_FORK_OWNER_IOCTL
+	bool "Enable IOCTL VHOST_FORK_FROM_OWNER"

drivers/vhost/Kconfig config VHOST_CROSS_ENDIAN_LEGACY · line 111

Quoted 10 lines
drivers/vhost/Kconfig +10 -0
+	default n
+	help
+	  This option enables the IOCTL VHOST_FORK_FROM_OWNER, which allows
+	  userspace applications to modify the thread mode for vhost devices.
+
+          By default, `CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL` is set to `n`,
+          meaning the ioctl is disabled and any operation using this ioctl
+          will fail.
+          When the configuration is enabled (y), the ioctl becomes
+          available, allowing users to set the mode if needed.

I think I already pointed out, but here there is a mix of tabs and spaces that IMHO we should fix.

drivers/vhost/vhost.c long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp) · line 2298

Quoted 12 lines
drivers/vhost/Kconfig +2 -0
+
+	  If unsure, say "N".
drivers/vhost/vhost.c +2 -0
index fb0c7fb43f78..568e43cb54a9 100644
@@ -2294,6 +2294,8 @@ long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
 		r = vhost_dev_set_owner(d);
 		goto done;
 	}
+
+#ifdef CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL

As I mentioned in the previous version, IMO this patch should be merged with the previous patch. I don't think it is good for bisection to have a commit with an IOCTL supported in any case and in the next commit instead supported only through a config.

Maybe I'm missing something, but what's the point of having a separate patch for this?

Thanks,
Stefano

Quoted 14 lines
drivers/vhost/vhost.c +1 -0
 	if (ioctl == VHOST_FORK_FROM_OWNER) {
 		u8 inherit_owner;
 		/*inherit_owner can only be modified before owner is set*/
@@ -2313,6 +2315,7 @@ long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
 		r = 0;
 		goto done;
 	}
+#endif
 	/* You must be the owner to do anything else */
 	r = vhost_dev_check_owner(d);
 	if (r)
Signature · 1 line
2.45.0
author
#27 lore raw

Re: [PATCH v9 4/4] vhost: Add a KConfig knob to enable IOCTL VHOST_FORK_FROM_OWNER

Quoted 29 lines · Stefano Garzarella
Quoted 27 lines · Cindy Lu

Introduce a new config knob CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL, to control the availability of the VHOST_FORK_FROM_OWNER ioctl. When CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL is set to n, the ioctl is disabled, and any attempt to use it will result in failure.

---
drivers/vhost/Kconfig | 15 +++++++++++++++
drivers/vhost/vhost.c | 3 +++

2 files changed, 18 insertions(+)
drivers/vhost/Kconfig +0 -0
index 020d4fbb947c..bc8fadb06f98 100644
@@ -96,3 +96,18 @@ config VHOST_CROSS_ENDIAN_LEGACY
        If unsure, say "N".
 

endif

drivers/vhost/Kconfig +8 -0
+
+config VHOST_ENABLE_FORK_OWNER_IOCTL
+      bool "Enable IOCTL VHOST_FORK_FROM_OWNER"
+      default n
+      help
+        This option enables the IOCTL VHOST_FORK_FROM_OWNER, which allows
+        userspace applications to modify the thread mode for vhost devices.
+
Quoted 9 lines

drivers/vhost/Kconfig config VHOST_CROSS_ENDIAN_LEGACY · line 111

Quoted 5 lines
drivers/vhost/Kconfig +5 -0
+          By default, `CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL` is set to `n`,
+          meaning the ioctl is disabled and any operation using this ioctl
+          will fail.
+          When the configuration is enabled (y), the ioctl becomes
+          available, allowing users to set the mode if needed.

I think I already pointed out, but here there is a mix of tabs and spaces that IMHO we should fix.

Sorry, I missed this comment while preparing the patch; I’ll fix it.

Quoted 18 lines

drivers/vhost/vhost.c long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp) · line 2298

Quoted 12 lines
drivers/vhost/Kconfig +2 -0
+
+        If unsure, say "N".
drivers/vhost/vhost.c +2 -0
index fb0c7fb43f78..568e43cb54a9 100644
@@ -2294,6 +2294,8 @@ long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
              r = vhost_dev_set_owner(d);
              goto done;
      }
+
+#ifdef CONFIG_VHOST_ENABLE_FORK_OWNER_IOCTL

As I mentioned in the previous version, IMO this patch should be merged with the previous patch. I don't think it is good for bisection to have a commit with an IOCTL supported in any case and in the next commit instead supported only through a config.

Quoted 6 lines

Maybe I'm missing something, but what's the point of having a separate patch for this?

Thanks,
Stefano

will fix this
thanks
cindy

Quoted 15 lines
Quoted 14 lines
drivers/vhost/vhost.c +1 -1
      if (ioctl == VHOST_FORK_FROM_OWNER) {
              u8 inherit_owner;
              /*inherit_owner can only be modified before owner is set*/
@@ -2313,6 +2315,7 @@ long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
              r = 0;
              goto done;
      }
+#endif
      /* You must be the owner to do anything else */
      r = vhost_dev_check_owner(d);
      if (r)
--

2.45.0