[PATCH v3 4/9] vhost: Add kthread support in function vhost_worker_create

From: Cindy Lu
Date: Tue Nov 05 2024 - 02:28:59 EST


Restored the previous functions kthread_wakeup and kthread_stop.
Also add a new structure, vhost_task_fn. The function vhost_worker_create
Will initializes this structure based on the value of inherit_owner.

Signed-off-by: Cindy Lu <lulu@xxxxxxxxxx>
---
drivers/vhost/vhost.c | 71 ++++++++++++++++++++++++++++++++++++-------
drivers/vhost/vhost.h | 6 ++++
2 files changed, 66 insertions(+), 11 deletions(-)

diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
index e40cef3a1fa5..603b146fccc1 100644
--- a/drivers/vhost/vhost.c
+++ b/drivers/vhost/vhost.c
@@ -741,43 +741,92 @@ static void vhost_workers_free(struct vhost_dev *dev)
xa_destroy(&dev->worker_xa);
}

+static int vhost_task_wakeup_fn(void *vtsk)
+{
+ vhost_task_wake((struct vhost_task *)vtsk);
+ return 0;
+}
+static int vhost_kthread_wakeup_fn(void *p)
+{
+ return wake_up_process((struct task_struct *)p);
+}
+static int vhost_task_stop_fn(void *vtsk)
+{
+ vhost_task_stop((struct vhost_task *)vtsk);
+ return 0;
+}
+static int vhost_kthread_stop_fn(void *k)
+{
+ return kthread_stop((struct task_struct *)k);
+}
+
static struct vhost_worker *vhost_worker_create(struct vhost_dev *dev)
{
struct vhost_worker *worker;
- struct vhost_task *vtsk;
+ struct vhost_task *vtsk = NULL;
+ struct task_struct *task = NULL;
char name[TASK_COMM_LEN];
int ret;
u32 id;

+ /* Allocate resources for the worker */
worker = kzalloc(sizeof(*worker), GFP_KERNEL_ACCOUNT);
if (!worker)
return NULL;

+ worker->fn = kzalloc(sizeof(struct vhost_task_fn), GFP_KERNEL_ACCOUNT);
+ if (!worker->fn) {
+ kfree(worker);
+ return NULL;
+ }
+
worker->dev = dev;
snprintf(name, sizeof(name), "vhost-%d", current->pid);

- vtsk = vhost_task_create(vhost_run_work_list, vhost_worker_killed,
- worker, name);
- if (!vtsk)
- goto free_worker;
-
mutex_init(&worker->mutex);
init_llist_head(&worker->work_list);
worker->kcov_handle = kcov_common_handle();
- worker->vtsk = vtsk;

- vhost_task_start(vtsk);
+ if (dev->inherit_owner) {
+ /* Create and start a vhost task */
+ vtsk = vhost_task_create(vhost_run_work_list,
+ vhost_worker_killed, worker, name);
+ if (!vtsk)
+ goto free_worker;
+
+ worker->vtsk = vtsk;
+ worker->fn->wakeup = vhost_task_wakeup_fn;
+ worker->fn->stop = vhost_task_stop_fn;
+
+ vhost_task_start(vtsk);
+ } else {
+ /* Create and start a kernel thread */
+ task = kthread_create(vhost_run_work_kthread_list, worker,
+ "vhost-%d", current->pid);
+ if (IS_ERR(task)) {
+ ret = PTR_ERR(task);
+ goto free_worker;
+ }
+ worker->task = task;
+ worker->fn->wakeup = vhost_kthread_wakeup_fn;
+ worker->fn->stop = vhost_kthread_stop_fn;
+
+ wake_up_process(task);
+ /* Attach to the vhost cgroup */
+ ret = vhost_attach_cgroups(dev);
+ if (ret)
+ goto stop_worker;
+ }

ret = xa_alloc(&dev->worker_xa, &id, worker, xa_limit_32b, GFP_KERNEL);
if (ret < 0)
goto stop_worker;
worker->id = id;
-
return worker;
-
stop_worker:
- vhost_task_stop(vtsk);
+ worker->fn->stop(dev->inherit_owner ? (void *)vtsk : (void *)task);
free_worker:
+ kfree(worker->fn);
kfree(worker);
return NULL;
}
diff --git a/drivers/vhost/vhost.h b/drivers/vhost/vhost.h
index c650c4506c70..ebababa4e340 100644
--- a/drivers/vhost/vhost.h
+++ b/drivers/vhost/vhost.h
@@ -25,8 +25,13 @@ struct vhost_work {
vhost_work_fn_t fn;
unsigned long flags;
};
+struct vhost_task_fn {
+ int (*wakeup)(void *task);
+ int (*stop)(void *task);
+};

struct vhost_worker {
+ struct task_struct *task;
struct vhost_task *vtsk;
struct vhost_dev *dev;
/* Used to serialize device wide flushing with worker swapping. */
@@ -36,6 +41,7 @@ struct vhost_worker {
u32 id;
int attachment_cnt;
bool killed;
+ struct vhost_task_fn *fn;
};

/* Poll a file (eventfd or socket) */
--
2.45.0