Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795932855; b=jPcqXpGphxvHcJwz8xWU80YqF3f0+ZDcXYKSeyHw70afGNv6VRnH1QtIDnfDbR7iPEKO OTZGbuEJl7hwAp+L6+YguSyvfbxRp9noGGPfU+vPivqp+eMcA0YjOrNmKFwzN6WwTJChx WEqWINyQvXZsTiPHfahXaaiBsAjhMJZchZSXzA9MbuzngmW2++nDCWRbtxU8ZX79G5n+s rnGjQrYw9ebuXHtz9fLY9Z+sn9pU0aC7PQrr+PuLjeVSyv9uqP11zd8r4aezRRzbPkjNr 4A1VE7MX+PRhuanokx1VaFXdSPwHcVyz5U4RNs4I0N9ONCzYQnHt6m5hRjNFdG8NCZA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795932855; h=Received:DKIM-Signature:From:To:Cc:Subject:Message-ID:MIME-Version: Content-Type:Content-Transfer-Encoding:Date; bh=ypuogkNih3IZFgrQR+2G3HhV/6YGfqOCs0Pclsexpq0=; b=M9qQcU5CxcxgaSsItQ+JQwjZQplT/Mu+hhQdKcm9PFKoPSW9A2WBAn1h0VMlK85zhYkC O6ylk/qF9iX6GZPisywiOSRzaBhv/zuoeBPWv9aDt9M5X9J0Tp8Uy8VqLpSFEU6yaR60I NjXVBnEAoD8vEZ0vACo1cQz59pCF6mumwS6THFFhN2eYfrNtD4KlGiJt7ix2Ht7a9G7aH p7OClvgKiejUu8+p58B1qmkKqYFCyl2gZbXL1wUVDMLYykflCUkWD0tPthQnYPobD/3Jq qUItYsZ9UKthx4eCbFPvvNmH/WHswwR5R7QnVPf7rvZfl8aMkRCbH0qAIfQBKDcgEtA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=lqGFiVpZ; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with UTF8SMTPS id 1A2AD606A31C for ; Sun, 29 Nov 2026 06:14:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=lqGFiVpZ Received: by smtp.kernel.org (Postfix) with UTF8SMTPSA id 217641F00898; Sun, 29 Nov 2026 06:14:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795932854; bh=ypuogkNih3IZFgrQR+2G3HhV/6YGfqOCs0Pclsexpq0=; h=From:To:Cc:Subject:Date; b=lqGFiVpZ//iejrK1Xr7qEuF4cYkL7/1THoqpOfaXvLOIX78voM0OIC8M90+1tiYYn 8/62fvyvshmamsiQzWz5/zaFMaDA6LwsCYaPNL15mU3WZFFo/EOC71tW9rq5r+oDEK oXnyih6BofXztEOL2iZpm2l4Uzf01CecK1ko7ms9VRBE6xMIvql5QWvm09Vo09/AIb s4NY9wx/BgnGfpXPAE9OVwNtrD/bNgzgKGQR/MikGIaLrhvGOGG75VJKm34auQ34bC a2QCpAdjqTsBcGQ3zXi5Ne4Wr9o3bSfuyLC5HZRaDIyvKn25KHFMTrINLDkpMqhxGC rlD5rsjWKnSug== From: "syzbot" To: syzkaller-upstream-moderation@googlegroups.com Cc: syzbot@lists.linux.dev Subject: [PATCH RFC] vhost: avoid hang when flushing or stopping frozen vhost_task Message-ID: <547240cb-a870-42e8-bc17-b14d21451f49@mail.kernel.org> X-Mailing-List: syzbot@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Date: Sun, 29 Nov 2026 06:14:14 +0000 (UTC) By 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. Fixes: f9010dbdce91 ("fork, vhost: Use CLONE_THREAD to fix freezer/ps regression") Assisted-by: Gemini:gemini-4-hydrogen Gemini:gemini-4-carbon Gemini:gemini-3.8-flash syzbot Reported-by: syzbot+7f3bbe59e8dd2328a990@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=7f3bbe59e8dd2328a990 Link: https://syzkaller.appspot.com/ai_job?id=39dae304-5492-409b-8909-cc0ce1e0c72b To: =?utf-8?q?Eugenio_P=C3=A9rez?= To: "Jason Wang" To: To: "Michael S. Tsirkin" To: To: To: "Mike Christie" --- diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index dc9732875..b18ed5ff6 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -31,6 +31,7 @@ #include #include #include +#include #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) diff --git a/drivers/vhost/vhost.h b/drivers/vhost/vhost.h index 5a684ba5e..6239abbcc 100644 --- a/drivers/vhost/vhost.h +++ b/drivers/vhost/vhost.h @@ -16,6 +16,7 @@ #include #include +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 { diff --git a/include/linux/sched/vhost_task.h b/include/linux/sched/vhost_task.h index 25446c5d3..6efdba559 100644 --- a/include/linux/sched/vhost_task.h +++ b/include/linux/sched/vhost_task.h @@ -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 */ diff --git a/kernel/vhost_task.c b/kernel/vhost_task.c index 3717885a5..40447494f 100644 --- a/kernel/vhost_task.c +++ b/kernel/vhost_task.c @@ -2,8 +2,11 @@ /* * Copyright (C) 2021 Oracle Corporation */ +#include #include #include +#include +#include #include #include #include @@ -11,30 +14,53 @@ enum vhost_task_flags { VHOST_TASK_FLAGS_STOP, VHOST_TASK_FLAGS_KILLED, + VHOST_TASK_FLAGS_SIGNAL, }; struct vhost_task { bool (*fn)(void *data); void (*handle_sigkill)(void *data); void *data; - struct completion exited; unsigned long flags; struct task_struct *task; - /* serialize SIGKILL and vhost_task_stop calls */ - struct mutex exit_mutex; + /* + * Serializes fn() / handle_sigkill() execution with + * vhost_task_stop() and vhost_task_flush() fallback. + */ + struct mutex lock; + wait_queue_head_t wait; + refcount_t refs; }; +static void vhost_task_put(struct vhost_task *vtsk) +{ + if (refcount_dec_and_test(&vtsk->refs)) { + put_task_struct(vtsk->task); + kfree(vtsk); + } +} + static int vhost_task_fn(void *data) { struct vhost_task *vtsk = data; + mutex_lock(&vtsk->lock); for (;;) { bool did_work; if (signal_pending(current)) { struct ksignal ksig; - - if (get_signal(&ksig)) + bool dead; + + set_bit(VHOST_TASK_FLAGS_SIGNAL, &vtsk->flags); + /* Order VHOST_TASK_FLAGS_SIGNAL before waking waiters. */ + smp_mb__after_atomic(); + mutex_unlock(&vtsk->lock); + wake_up(&vtsk->wait); + dead = get_signal(&ksig); + mutex_lock(&vtsk->lock); + clear_bit(VHOST_TASK_FLAGS_SIGNAL, &vtsk->flags); + if (dead) break; } @@ -47,11 +73,13 @@ static int vhost_task_fn(void *data) } did_work = vtsk->fn(vtsk->data); - if (!did_work) + if (!did_work) { + mutex_unlock(&vtsk->lock); schedule(); + mutex_lock(&vtsk->lock); + } } - mutex_lock(&vtsk->exit_mutex); /* * If a vhost_task_stop and SIGKILL race, we can ignore the SIGKILL. * When the vhost layer has called vhost_task_stop it's already stopped @@ -61,8 +89,9 @@ static int vhost_task_fn(void *data) set_bit(VHOST_TASK_FLAGS_KILLED, &vtsk->flags); vtsk->handle_sigkill(vtsk->data); } - mutex_unlock(&vtsk->exit_mutex); - complete(&vtsk->exited); + mutex_unlock(&vtsk->lock); + wake_up(&vtsk->wait); + vhost_task_put(vtsk); do_exit(0); } @@ -79,6 +108,40 @@ void vhost_task_wake(struct vhost_task *vtsk) } EXPORT_SYMBOL_GPL(vhost_task_wake); +/** + * vhost_task_finish_flush - wake any waiter waiting for flush completion + * @vtsk: vhost_task that completed flush work + */ +void vhost_task_finish_flush(struct vhost_task *vtsk) +{ + wake_up(&vtsk->wait); +} +EXPORT_SYMBOL_GPL(vhost_task_finish_flush); + +/** + * vhost_task_flush - wait for flush completion with fallback if parked + * @vtsk: vhost_task being flushed + * @done: completion signaled by the flush work item + * @flush_fn: fallback function invoked under @vtsk->lock if @vtsk->task + * is parked in get_signal() or exiting + */ +void vhost_task_flush(struct vhost_task *vtsk, struct completion *done, + void (*flush_fn)(void *data)) +{ + wait_event(vtsk->wait, + completion_done(done) || + test_bit(VHOST_TASK_FLAGS_SIGNAL, &vtsk->flags) || + test_bit(VHOST_TASK_FLAGS_KILLED, &vtsk->flags) || + test_bit(VHOST_TASK_FLAGS_STOP, &vtsk->flags)); + if (completion_done(done)) + return; + + guard(mutex)(&vtsk->lock); + if (!completion_done(done)) + flush_fn(vtsk->data); +} +EXPORT_SYMBOL_GPL(vhost_task_flush); + /** * vhost_task_stop - stop a vhost_task * @vtsk: vhost_task to stop @@ -88,20 +151,20 @@ EXPORT_SYMBOL_GPL(vhost_task_wake); */ void vhost_task_stop(struct vhost_task *vtsk) { - mutex_lock(&vtsk->exit_mutex); - if (!test_bit(VHOST_TASK_FLAGS_KILLED, &vtsk->flags)) { - set_bit(VHOST_TASK_FLAGS_STOP, &vtsk->flags); - vhost_task_wake(vtsk); - } - mutex_unlock(&vtsk->exit_mutex); - + set_bit(VHOST_TASK_FLAGS_STOP, &vtsk->flags); + /* Order VHOST_TASK_FLAGS_STOP before waking the task. */ + smp_mb__after_atomic(); + vhost_task_wake(vtsk); /* - * Make sure vhost_task_fn is no longer accessing the vhost_task before - * freeing it below. + * Wait for any in-flight fn() or handle_sigkill() call to finish + * using vtsk->data. If the task is stopped or frozen in + * get_signal(), vtsk->lock is unheld and vhost_task_fn() will + * exit once resumed without accessing vtsk->data. */ - wait_for_completion(&vtsk->exited); - put_task_struct(vtsk->task); - kfree(vtsk); + mutex_lock(&vtsk->lock); + mutex_unlock(&vtsk->lock); + wake_up(&vtsk->wait); + vhost_task_put(vtsk); } EXPORT_SYMBOL_GPL(vhost_task_stop); @@ -134,8 +197,9 @@ struct vhost_task *vhost_task_create(bool (*fn)(void *), vtsk = kzalloc_obj(*vtsk); if (!vtsk) return ERR_PTR(-ENOMEM); - init_completion(&vtsk->exited); - mutex_init(&vtsk->exit_mutex); + mutex_init(&vtsk->lock); + init_waitqueue_head(&vtsk->wait); + refcount_set(&vtsk->refs, 2); vtsk->data = arg; vtsk->fn = fn; vtsk->handle_sigkill = handle_sigkill; base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f -- 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 syzkaller@googlegroups.com.