vhost: avoid hang when flushing or stopping frozen vhost_task
syzbot
syzbotBy default (VHOST_FORK_OWNER_TASK), VHOST_SET_OWNER creates a worker thread via vhost_task_create(), which spawns a PF_USER_WORKER thread with CLONE_THREAD | CLONE_SIGHAND in the caller's thread group and cgroups without PF_KTHREAD or PF_NOFREEZE. To allow cgroups containing vhost devices to freeze, vhost_task_fn() calls get_signal() when a signal is pending. When the worker is frozen by the cgroup v1 freezer (__refrigerator() in TASK_FROZEN) or cgroup v2 freezer (do_freezer_trap()), or stopped by SIGSTOP (do_signal_stop() in TASK_STOPPED), it remains parked inside get_signal() and never returns to vhost_task_fn() to process queued work. If another thread in an unfrozen cgroup (such as a sibling thread not migrated via cgroup v1 tasks, or another process sharing the file descriptor) flushes or closes the device, for example via close_range() -> __fput() -> vhost_vsock_dev_release() -> vhost_vsock_flush() -> vhost_dev_flush() -> __vhost_worker_flush(), it queues a vhost_flush_work item, calls wake_up_process(), and waits uninterruptibly in wait_for_completion(). Because wake_up_process() cannot wake TASK_FROZEN or TASK_STOPPED tasks and do_freezer_trap() loops while frozen, the flush work never executes and the caller hangs in TASK_UNINTERRUPTIBLE, triggering a hung task warning.
The same deadlock also affects vhost_task_stop() during teardown (such as vhost_dev_cleanup() or VHOST_FREE_WORKER), because it previously called wake_up_process() and waited uninterruptibly in wait_for_completion(&vtsk->exited), which cannot complete while the worker is frozen or stopped in get_signal().
Fix this by decoupling struct vhost_task lifetime from task exit and providing a flush fallback when the worker is parked or exiting. Replace vtsk->exited and vtsk->exit_mutex with a reference count (refs, shared by the owner and the worker thread), a mutex (lock), a waitqueue (wait), and VHOST_TASK_FLAGS_SIGNAL. vhost_task_fn() holds vtsk->lock around flag checks, fn(), and handle_sigkill(), releasing it across get_signal() (after setting VHOST_TASK_FLAGS_SIGNAL and waking waiters) and idle schedule(), and drops its reference via vhost_task_put() before do_exit(0). vhost_task_stop() sets VHOST_TASK_FLAGS_STOP, wakes the task, acquires and releases vtsk->lock to wait only for any in-flight fn() or handle_sigkill() call to finish using vtsk->data, wakes vtsk->wait, and drops the owner's reference; if the worker is parked in get_signal(), vhost_task_stop() returns without disturbing freezer or job-control state, and the worker will observe VHOST_TASK_FLAGS_STOP and exit once resumed.
Add vhost_task_flush() and vhost_task_finish_flush(), along with a wait_flush callback in struct vhost_worker_ops. For vhost_task workers, __vhost_worker_flush() waits via vhost_task_flush() on vtsk->wait until either the flush completion is signaled (woken by vhost_task_finish_flush() after vhost_flush_work() calls complete()) or VHOST_TASK_FLAGS_SIGNAL, VHOST_TASK_FLAGS_KILLED, or VHOST_TASK_FLAGS_STOP is observed. If the completion is still incomplete, vhost_task_flush() acquires vtsk->lock, re-checks completion_done(), and invokes vhost_worker_flush_fallback(), which runs vhost_run_work_list() with worker->dev->mm attached via kthread_use_mm()/kthread_unuse_mm(), either directly if the caller is a kthread without an mm or via an on-stack system work item otherwise.
To: =?utf-8?q?Eugenio_P=C3=A9rez?= <[email protected]>
To: "Jason Wang" <[email protected]>
To: <[email protected]>
To: "Michael S. Tsirkin" <[email protected]>
To: <[email protected]>
To: <[email protected]>
To: "Mike Christie" <[email protected]>
---
drivers/vhost/vhost.c +64 -1
@@ -31,6 +31,7 @@#include <linux/interval_tree_generic.h> #include <linux/nospec.h> #include <linux/kcov.h> +#include <linux/workqueue.h> #include "vhost.h"
@@ -149,14 +150,19 @@ static void vhost_reset_is_le(struct vhost_virtqueue *vq)struct vhost_flush_struct {struct vhost_work work;struct completion wait_event; +struct vhost_task *vtsk; };static void vhost_flush_work (struct vhost_work *work) {struct vhost_flush_struct *s; +struct vhost_task *vtsk; s = container_of(work, struct vhost_flush_struct, work); + vtsk = s->vtsk;complete (&s->wait_event); +if (vtsk) +vhost_task_finish_flush (vtsk); } static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh,
@@ -286,6 +292,7 @@ static void __vhost_worker_flush(struct vhost_worker *worker)init_completion (&flush.wait_event);vhost_work_init (&flush.work, vhost_flush_work); + flush.vtsk = worker->vtsk;vhost_worker_queue (worker, &flush.work);/*
@@ -293,7 +300,7 @@ static void __vhost_worker_flush(struct vhost_worker *worker)* mutex to force cleanup. */ mutex_unlock (&worker->mutex); -wait_for_completion (&flush.wait_event); + worker->ops->wait_flush (worker, &flush.wait_event);mutex_lock (&worker->mutex); }
@@ -784,6 +791,60 @@ static void vhost_kthread_do_stop(struct vhost_worker *worker)kthread_stop (worker->kthread_task); } +struct vhost_flush_work_ctx { +struct work_struct work; +struct vhost_worker *worker; +}; + +static void vhost_worker_run_list_mm (struct vhost_worker *worker) +{ +struct mm_struct *mm = worker->dev->mm; + +if (mm) +kthread_use_mm (mm); +vhost_run_work_list (worker); +if (mm) +kthread_unuse_mm (mm); +} + +static void vhost_flush_work_fn (struct work_struct *work) +{ +struct vhost_flush_work_ctx *ctx = + container_of(work, struct vhost_flush_work_ctx, work); + +vhost_worker_run_list_mm (ctx->worker); +} + +static void vhost_worker_flush_fallback (void *data) +{ +struct vhost_worker *worker = data; + +if ((current->flags &PF_KTHREAD ) && !current->mm) { +vhost_worker_run_list_mm (worker); + }else { +struct vhost_flush_work_ctx ctx = { + .worker = worker, + }; + +INIT_WORK_ONSTACK (&ctx.work, vhost_flush_work_fn); +schedule_work (&ctx.work); +flush_work (&ctx.work); +destroy_work_on_stack (&ctx.work); + } +} + +static void vhost_task_wait_flush (struct vhost_worker *worker, +struct completion *done) +{ +vhost_task_flush (worker->vtsk, done, vhost_worker_flush_fallback); +} + +static void vhost_kthread_wait_flush (struct vhost_worker *worker, +struct completion *done) +{ +wait_for_completion (done); +} +static int vhost_task_worker_create (struct vhost_worker *worker,struct vhost_dev *dev,const char *name) {
@@ -842,12 +903,14 @@ static const struct vhost_worker_ops kthread_ops = { .create = vhost_kthread_worker_create, .stop = vhost_kthread_do_stop, .wakeup = vhost_kthread_wakeup, + .wait_flush = vhost_kthread_wait_flush, };static const struct vhost_worker_ops vhost_task_ops = { .create = vhost_task_worker_create, .stop = vhost_task_do_stop, .wakeup = vhost_task_wakeup, + .wait_flush = vhost_task_wait_flush, };static struct vhost_worker *vhost_worker_create (struct vhost_dev *dev)
drivers/vhost/vhost.h +3 -0
@@ -16,6 +16,7 @@#include <linux/irqbypass.h> #include <linux/unroll.h> +struct completion ;struct vhost_work ;struct vhost_task ;typedef void (*vhost_work_fn_t )(struct vhost_work *work);
@@ -36,6 +37,8 @@ struct vhost_worker_ops { const char *name);void (*stop)(struct vhost_worker *worker);void (*wakeup)(struct vhost_worker *worker); +void (*wait_flush)(struct vhost_worker *worker, +struct completion *done); };struct vhost_worker {
include/linux/sched/vhost_task.h +4 -0
@@ -2,6 +2,7 @@#ifndef _LINUX_SCHED_VHOST_TASK_H#define _LINUX_SCHED_VHOST_TASK_H +struct completion ;struct vhost_task ; struct vhost_task *vhost_task_create(bool (*fn)(void *),
@@ -10,5 +11,8 @@ struct vhost_task *vhost_task_create(bool (*fn)(void *),void vhost_task_start (struct vhost_task *vtsk);void vhost_task_stop (struct vhost_task *vtsk);void vhost_task_wake (struct vhost_task *vtsk); +void vhost_task_flush (struct vhost_task *vtsk,struct completion *done, +void (*flush_fn)(void *data)); +void vhost_task_finish_flush (struct vhost_task *vtsk);#endif /* _LINUX_SCHED_VHOST_TASK_H */
kernel/vhost_task.c +87 -23
178 lines not on this page · raw message
base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f
Signature · 9 lines
This is an AI-generated patch subject to moderation.
Reply with '#syz upstream' to Sign-off the patch as a human author
and send it to the upstream kernel mailing lists.
Reply with '#syz reject' to reject it ('#syz unreject' to undo).
See https://goo.gle/syzbot-ai-patches for information about AI-generated patches.
You can comment on the patch as usual, syzbot will try to address
the comments and send a new version of the patch if necessary.
syzbot engineers can be reached at [email protected].