Skip to content

Commit 6e890c5

Browse files
mikechristiebrauner
authored andcommitted
vhost: use vhost_tasks for worker threads
For vhost workers we use the kthread API which inherit's its values from and checks against the kthreadd thread. This results in the wrong RLIMITs being checked, so while tools like libvirt try to control the number of threads based on the nproc rlimit setting we can end up creating more threads than the user wanted. This patch has us use the vhost_task helpers which will inherit its values/checks from the thread that owns the device similar to if we did a clone in userspace. The vhost threads will now be counted in the nproc rlimits. And we get features like cgroups and mm sharing automatically, so we can remove those calls. Signed-off-by: Mike Christie <[email protected]> Acked-by: Michael S. Tsirkin <[email protected]> Signed-off-by: Christian Brauner (Microsoft) <[email protected]> Signed-off-by: Christian Brauner <[email protected]>
1 parent 1a5f809 commit 6e890c5

File tree

2 files changed

+15
-49
lines changed

2 files changed

+15
-49
lines changed

drivers/vhost/vhost.c

Lines changed: 13 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
#include <linux/slab.h>
2323
#include <linux/vmalloc.h>
2424
#include <linux/kthread.h>
25-
#include <linux/cgroup.h>
2625
#include <linux/module.h>
2726
#include <linux/sort.h>
2827
#include <linux/sched/mm.h>
2928
#include <linux/sched/signal.h>
29+
#include <linux/sched/vhost_task.h>
3030
#include <linux/interval_tree_generic.h>
3131
#include <linux/nospec.h>
3232
#include <linux/kcov.h>
@@ -256,7 +256,7 @@ void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work)
256256
* test_and_set_bit() implies a memory barrier.
257257
*/
258258
llist_add(&work->node, &dev->worker->work_list);
259-
wake_up_process(dev->worker->task);
259+
wake_up_process(dev->worker->vtsk->task);
260260
}
261261
}
262262
EXPORT_SYMBOL_GPL(vhost_work_queue);
@@ -336,17 +336,14 @@ static void vhost_vq_reset(struct vhost_dev *dev,
336336
static int vhost_worker(void *data)
337337
{
338338
struct vhost_worker *worker = data;
339-
struct vhost_dev *dev = worker->dev;
340339
struct vhost_work *work, *work_next;
341340
struct llist_node *node;
342341

343-
kthread_use_mm(dev->mm);
344-
345342
for (;;) {
346343
/* mb paired w/ kthread_stop */
347344
set_current_state(TASK_INTERRUPTIBLE);
348345

349-
if (kthread_should_stop()) {
346+
if (vhost_task_should_stop(worker->vtsk)) {
350347
__set_current_state(TASK_RUNNING);
351348
break;
352349
}
@@ -368,7 +365,7 @@ static int vhost_worker(void *data)
368365
schedule();
369366
}
370367
}
371-
kthread_unuse_mm(dev->mm);
368+
372369
return 0;
373370
}
374371

@@ -509,31 +506,6 @@ long vhost_dev_check_owner(struct vhost_dev *dev)
509506
}
510507
EXPORT_SYMBOL_GPL(vhost_dev_check_owner);
511508

512-
struct vhost_attach_cgroups_struct {
513-
struct vhost_work work;
514-
struct task_struct *owner;
515-
int ret;
516-
};
517-
518-
static void vhost_attach_cgroups_work(struct vhost_work *work)
519-
{
520-
struct vhost_attach_cgroups_struct *s;
521-
522-
s = container_of(work, struct vhost_attach_cgroups_struct, work);
523-
s->ret = cgroup_attach_task_all(s->owner, current);
524-
}
525-
526-
static int vhost_attach_cgroups(struct vhost_dev *dev)
527-
{
528-
struct vhost_attach_cgroups_struct attach;
529-
530-
attach.owner = current;
531-
vhost_work_init(&attach.work, vhost_attach_cgroups_work);
532-
vhost_work_queue(dev, &attach.work);
533-
vhost_dev_flush(dev);
534-
return attach.ret;
535-
}
536-
537509
/* Caller should have device mutex */
538510
bool vhost_dev_has_owner(struct vhost_dev *dev)
539511
{
@@ -580,42 +552,36 @@ static void vhost_worker_free(struct vhost_dev *dev)
580552

581553
dev->worker = NULL;
582554
WARN_ON(!llist_empty(&worker->work_list));
583-
kthread_stop(worker->task);
555+
vhost_task_stop(worker->vtsk);
584556
kfree(worker);
585557
}
586558

587559
static int vhost_worker_create(struct vhost_dev *dev)
588560
{
589561
struct vhost_worker *worker;
590-
struct task_struct *task;
562+
struct vhost_task *vtsk;
563+
char name[TASK_COMM_LEN];
591564
int ret;
592565

593566
worker = kzalloc(sizeof(*worker), GFP_KERNEL_ACCOUNT);
594567
if (!worker)
595568
return -ENOMEM;
596569

597570
dev->worker = worker;
598-
worker->dev = dev;
599571
worker->kcov_handle = kcov_common_handle();
600572
init_llist_head(&worker->work_list);
573+
snprintf(name, sizeof(name), "vhost-%d", current->pid);
601574

602-
task = kthread_create(vhost_worker, worker, "vhost-%d", current->pid);
603-
if (IS_ERR(task)) {
604-
ret = PTR_ERR(task);
575+
vtsk = vhost_task_create(vhost_worker, worker, name);
576+
if (!vtsk) {
577+
ret = -ENOMEM;
605578
goto free_worker;
606579
}
607580

608-
worker->task = task;
609-
wake_up_process(task); /* avoid contributing to loadavg */
610-
611-
ret = vhost_attach_cgroups(dev);
612-
if (ret)
613-
goto stop_worker;
614-
581+
worker->vtsk = vtsk;
582+
vhost_task_start(vtsk);
615583
return 0;
616584

617-
stop_worker:
618-
kthread_stop(worker->task);
619585
free_worker:
620586
kfree(worker);
621587
dev->worker = NULL;

drivers/vhost/vhost.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <linux/irqbypass.h>
1717

1818
struct vhost_work;
19+
struct vhost_task;
1920
typedef void (*vhost_work_fn_t)(struct vhost_work *work);
2021

2122
#define VHOST_WORK_QUEUED 1
@@ -26,9 +27,8 @@ struct vhost_work {
2627
};
2728

2829
struct vhost_worker {
29-
struct task_struct *task;
30+
struct vhost_task *vtsk;
3031
struct llist_head work_list;
31-
struct vhost_dev *dev;
3232
u64 kcov_handle;
3333
};
3434

0 commit comments

Comments
 (0)