Kernel KVM virtualization development
 help / color / mirror / Atom feed
* [PATCH v2] vhost_task: avoid hanging in vhost_task_stop() when worker is frozen
@ 2026-12-03 16:21 Prabhakar Reddy K
  2026-12-04 15:54 ` Jiaming Fan
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Prabhakar Reddy K @ 2026-12-03 16:21 UTC (permalink / raw)
  To: Michael S . Tsirkin, Jason Wang, Eugenio Pérez
  Cc: kvm, virtualization, netdev, linux-kernel, Prabhakar Reddy K,
	syzbot+f59eab881ec20d423ab0

vhost_task_stop() currently waits for the worker thread to exit by calling
wait_for_completion(&vtsk->exited). But, vhost_task workers run as
PF_USER_WORKER threads and call get_signal() when a signal or freeze
request is pending.

This causes an indefinite hang in the following scenarios:
1. A process holding a vhost_task worker (for example, KVM's
   kvm-nx-lpage-recovery thread) is frozen via cgroup v1 (__refrigerator),
   cgroup v2 (do_freezer_trap), or stopped via SIGSTOP (do_signal_stop).
2. Another unfrozen process closes the last reference to the VM/device and
   calls vhost_task_stop().
3. vhost_task_stop() sets VHOST_TASK_FLAGS_STOP and wakes the worker, but
   get_signal() immediately loops back into the freeze/stop trap without
   returning to vhost_task_fn().
4. complete(&vtsk->exited) is never reached, so vhost_task_stop() blocks
   forever in D state.

Fix this strictly in the signal_pending() slow path by adding a single
internal state bit, VHOST_TASK_FLAGS_IN_SIGNAL, serialized under the
existing exit_mutex:

- Before entering get_signal(), vhost_task_fn() acquires exit_mutex. If
  VHOST_TASK_FLAGS_STOP is already set, it breaks out and exits via the
  normal completion path without entering get_signal(); otherwise it sets
  VHOST_TASK_FLAGS_IN_SIGNAL and releases exit_mutex.
- In vhost_task_stop(), while holding exit_mutex to set
  VHOST_TASK_FLAGS_STOP, check VHOST_TASK_FLAGS_IN_SIGNAL. If set, the
  worker is guaranteed to be inside get_signal() (not running vtsk->fn()),
  so return immediately and transfer vtsk cleanup ownership to the worker.
- When the worker later returns from get_signal() (after thaw, SIGCONT, or
  SIGKILL), it re-acquires exit_mutex. If VHOST_TASK_FLAGS_STOP is set, it
  releases vtsk->task, frees vtsk, and calls do_exit(0) directly without
  invoking vtsk->fn() or handle_sigkill(). Otherwise it clears
  VHOST_TASK_FLAGS_IN_SIGNAL and continues normally.

Because VHOST_TASK_FLAGS_IN_SIGNAL and VHOST_TASK_FLAGS_STOP are paired
under exit_mutex, exactly one side frees vtsk, and the worker can never
access vtsk->data after vhost_task_stop() returns. The !signal_pending()
hot path and non-frozen teardown remain completely unchanged.

Reported-by: syzbot+f59eab881ec20d423ab0@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=f59eab881ec20d423ab0
Fixes: f9010dbdce91 ("fork, vhost: Use CLONE_THREAD to fix freezer/ps regression")
Signed-off-by: Prabhakar Reddy K <K.Prabhakarr84@gmail.com>
---
Changes in v2:
- Removed per-iteration exit_mutex locking in vhost_task_fn() and
  refcount_t on struct vhost_task per Michael S. Tsirkin's feedback.
- Restricted synchronization to the signal_pending() slow path using
  VHOST_TASK_FLAGS_IN_SIGNAL under the existing exit_mutex.

Tested with the syzbot cgroup v1 reproducer, custom cgroup v2 and SIGSTOP
(SIGCONT/SIGKILL) reproducers, and a 50-iteration race loop under KASAN,
Lockdep, and KCSAN with no vhost_task warnings or leaked worker threads.

 kernel/vhost_task.c | 30 +++++++++++++++++++++++++++++-
 1 file changed, 29 insertions(+), 1 deletion(-)

diff --git a/kernel/vhost_task.c b/kernel/vhost_task.c
index 3717885a5..4fd38e092 100644
--- a/kernel/vhost_task.c
+++ b/kernel/vhost_task.c
@@ -11,6 +11,7 @@
 enum vhost_task_flags {
 	VHOST_TASK_FLAGS_STOP,
 	VHOST_TASK_FLAGS_KILLED,
+	VHOST_TASK_FLAGS_IN_SIGNAL,
 };
 
 struct vhost_task {
@@ -33,8 +34,29 @@ static int vhost_task_fn(void *data)
 
 		if (signal_pending(current)) {
 			struct ksignal ksig;
+			bool dead;
 
-			if (get_signal(&ksig))
+			mutex_lock(&vtsk->exit_mutex);
+			if (test_bit(VHOST_TASK_FLAGS_STOP, &vtsk->flags)) {
+				mutex_unlock(&vtsk->exit_mutex);
+				break;
+			}
+			set_bit(VHOST_TASK_FLAGS_IN_SIGNAL, &vtsk->flags);
+			mutex_unlock(&vtsk->exit_mutex);
+
+			dead = get_signal(&ksig);
+
+			mutex_lock(&vtsk->exit_mutex);
+			if (test_bit(VHOST_TASK_FLAGS_STOP, &vtsk->flags)) {
+				mutex_unlock(&vtsk->exit_mutex);
+				put_task_struct(vtsk->task);
+				kfree(vtsk);
+				do_exit(0);
+			}
+			clear_bit(VHOST_TASK_FLAGS_IN_SIGNAL, &vtsk->flags);
+			mutex_unlock(&vtsk->exit_mutex);
+
+			if (dead)
 				break;
 		}
 
@@ -88,13 +110,19 @@ EXPORT_SYMBOL_GPL(vhost_task_wake);
  */
 void vhost_task_stop(struct vhost_task *vtsk)
 {
+	bool in_signal = false;
+
 	mutex_lock(&vtsk->exit_mutex);
 	if (!test_bit(VHOST_TASK_FLAGS_KILLED, &vtsk->flags)) {
 		set_bit(VHOST_TASK_FLAGS_STOP, &vtsk->flags);
+		in_signal = test_bit(VHOST_TASK_FLAGS_IN_SIGNAL, &vtsk->flags);
 		vhost_task_wake(vtsk);
 	}
 	mutex_unlock(&vtsk->exit_mutex);
 
+	if (in_signal)
+		return;
+
 	/*
 	 * Make sure vhost_task_fn is no longer accessing the vhost_task before
 	 * freeing it below.
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] vhost_task: avoid hanging in vhost_task_stop() when worker is frozen
  2026-12-03 16:21 [PATCH v2] vhost_task: avoid hanging in vhost_task_stop() when worker is frozen Prabhakar Reddy K
@ 2026-12-04 15:54 ` Jiaming Fan
  2026-12-04 16:00   ` Jiaming Fan
  2026-12-04 16:09 ` Jiaming Fan
  2026-12-04 16:23 ` sashiko-bot
  2 siblings, 1 reply; 5+ messages in thread
From: Jiaming Fan @ 2026-12-04 15:54 UTC (permalink / raw)
  To: Prabhakar Reddy K
  Cc: Michael S. Tsirkin, Jason Wang, Eugenio Pérez,
	virtualization, kvm

On Thu, Dec 03, 2026 at 09:51:42PM +0530, Prabhakar Reddy K wrote:
[...]
> +			dead = get_signal(&ksig);
> +
> +			mutex_lock(&vtsk->exit_mutex);
> +			if (test_bit(VHOST_TASK_FLAGS_STOP, &vtsk->flags)) {
> +				mutex_unlock(&vtsk->exit_mutex);
> +				put_task_struct(vtsk->task);
> +				kfree(vtsk);
> +				do_exit(0);
> +			}

Hmm... this looks like the classic mutex_unlock() UAF trap.

Acquiring a mutex doesn't mean the previous owner is done with
mutex_unlock(). It may still be touching the mutex itself, as
documented in mutex_unlock()'s kernel-doc.

I think we can get there like this:

  vhost_task_stop()                     vhost_task_fn()
  -----------------                     ---------------
                                        get_signal() returns (thaw)
  mutex_lock(exit_mutex)
  set STOP, sees IN_SIGNAL
  <preempted>
                                        mutex_lock(exit_mutex)
                                          owner not running: queued, sleeps
  vhost_task_wake()                       (woken, not yet running)
  mutex_unlock(exit_mutex)
    __mutex_unlock_slowpath()
      release owner (WAITERS set)
      <interrupt>
                                          runs, takes the lock
                                        sees STOP
                                        mutex_unlock(exit_mutex)
                                        kfree(vtsk)
      raw_spin_lock(&lock->wait_lock)   <-- UAF

Managed to hit this under QEMU + KASAN on vhost-scsi
(VHOST_NEW_WORKER, SIGSTOP, then SIGCONT + VHOST_FREE_WORKER
from a CLONE_VM child), with a few msleep()/mdelay()s to
force the above ordering.

  BUG: KASAN: slab-use-after-free in _raw_spin_lock_irqsave+0x76/0xe0
   __mutex_unlock_slowpath.isra.0+0x110/0x580
   mutex_unlock+0xc2/0xd0
   vhost_task_stop+0x157/0x1e0
   vhost_worker_destroy+0xac/0x100
   ...
  Freed by task 110:
   kfree+0x17c/0x460
   vhost_task_fn+0x504/0x600


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] vhost_task: avoid hanging in vhost_task_stop() when worker is frozen
  2026-12-04 15:54 ` Jiaming Fan
@ 2026-12-04 16:00   ` Jiaming Fan
  0 siblings, 0 replies; 5+ messages in thread
From: Jiaming Fan @ 2026-12-04 16:00 UTC (permalink / raw)
  To: Prabhakar Reddy K
  Cc: Michael S. Tsirkin, Jason Wang, Eugenio Pérez,
	virtualization, kvm

Maybe we can just use two refs here, one for the owner and one for the
worker. Each side drops its own ref when done, and the last one
frees vtsk.

Your IN_SIGNAL logic can stay as is. We only need to do the refcounting
at create/stop/exit, so no extra cost in the hot path.

	vhost_task_stop():
		...
		mutex_unlock(&vtsk->exit_mutex);
		if (!in_signal)
			wait_for_completion(&vtsk->exited);
		vhost_task_put(vtsk);

	vhost_task_fn(), sees STOP after get_signal():
		mutex_unlock(&vtsk->exit_mutex);
		vhost_task_put(vtsk);
		do_exit(0);

	vhost_task_fn(), normal exit:
		complete(&vtsk->exited);
		vhost_task_put(vtsk);
		do_exit(0);

I tried this with the same delays. The worker still gets the mutex
in that window, but no more KASAN reports.

Actually, this is not my idea. io_uring had a similar problem before
and fixed it this way, see 360395e8dbf9 ("io_uring/sqpoll: don't wait
for the thread to exit").


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] vhost_task: avoid hanging in vhost_task_stop() when worker is frozen
  2026-12-03 16:21 [PATCH v2] vhost_task: avoid hanging in vhost_task_stop() when worker is frozen Prabhakar Reddy K
  2026-12-04 15:54 ` Jiaming Fan
@ 2026-12-04 16:09 ` Jiaming Fan
  2026-12-04 16:23 ` sashiko-bot
  2 siblings, 0 replies; 5+ messages in thread
From: Jiaming Fan @ 2026-12-04 16:09 UTC (permalink / raw)
  To: Prabhakar Reddy K
  Cc: Michael S. Tsirkin, Jason Wang, Eugenio Pérez,
	virtualization, kvm

On Thu, Dec 03, 2026 at 09:51:42PM +0530, Prabhakar Reddy K wrote:
[...]
>  void vhost_task_stop(struct vhost_task *vtsk)
>  {
> +	bool in_signal = false;
> +
>  	mutex_lock(&vtsk->exit_mutex);
>  	if (!test_bit(VHOST_TASK_FLAGS_KILLED, &vtsk->flags)) {
>  		set_bit(VHOST_TASK_FLAGS_STOP, &vtsk->flags);
> +		in_signal = test_bit(VHOST_TASK_FLAGS_IN_SIGNAL, &vtsk->flags);
>  		vhost_task_wake(vtsk);
>  	}
>  	mutex_unlock(&vtsk->exit_mutex);
>  
> +	if (in_signal)
> +		return;
> +

BTW, don't forget to update the kernel-doc for vhost_task_stop().

If the worker is parked in get_signal(), we don't wait for it at all
anymore. We just make sure fn() and handle_sigkill() are done and
won't be called again.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] vhost_task: avoid hanging in vhost_task_stop() when worker is frozen
  2026-12-03 16:21 [PATCH v2] vhost_task: avoid hanging in vhost_task_stop() when worker is frozen Prabhakar Reddy K
  2026-12-04 15:54 ` Jiaming Fan
  2026-12-04 16:09 ` Jiaming Fan
@ 2026-12-04 16:23 ` sashiko-bot
  2 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-12-04 16:23 UTC (permalink / raw)
  To: Prabhakar Reddy K; +Cc: kvm, virtualization, Michael S. Tsirkin, Eugenio Perez

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Critical] vhost_task: use-after-free of exit_mutex in
  vhost_task_stop()
--

commit a9ac04900e51413d1a9f43439c5bba0416f4c933
Author: Prabhakar Reddy K <k.prabhakarr84@gmail.com>

vhost_task: avoid hanging in vhost_task_stop() when worker is frozen

This commit adds a VHOST_TASK_FLAGS_IN_SIGNAL flag under exit_mutex so
vhost_task_stop() can return without waiting on vtsk->exited when the worker
is inside get_signal(), transferring cleanup of vtsk to the worker.

> diff --git a/kernel/vhost_task.c b/kernel/vhost_task.c
> index 3717885a5992a..4fd38e0924842 100644
> --- a/kernel/vhost_task.c
> +++ b/kernel/vhost_task.c

[ ... ]

> @@ -33,8 +34,29 @@ static int vhost_task_fn(void *data)
>  
>  		if (signal_pending(current)) {
>  			struct ksignal ksig;
> +			bool dead;
>  
> -			if (get_signal(&ksig))
> +			mutex_lock(&vtsk->exit_mutex);
> +			if (test_bit(VHOST_TASK_FLAGS_STOP, &vtsk->flags)) {
> +				mutex_unlock(&vtsk->exit_mutex);
> +				break;
> +			}
> +			set_bit(VHOST_TASK_FLAGS_IN_SIGNAL, &vtsk->flags);
> +			mutex_unlock(&vtsk->exit_mutex);
> +
> +			dead = get_signal(&ksig);
> +
> +			mutex_lock(&vtsk->exit_mutex);
> +			if (test_bit(VHOST_TASK_FLAGS_STOP, &vtsk->flags)) {
> +				mutex_unlock(&vtsk->exit_mutex);
> +				put_task_struct(vtsk->task);
> +				kfree(vtsk);
> +				do_exit(0);
> +			}
> +			clear_bit(VHOST_TASK_FLAGS_IN_SIGNAL, &vtsk->flags);
> +			mutex_unlock(&vtsk->exit_mutex);
> +
> +			if (dead)
>  				break;
>  		}
>  
> @@ -88,13 +110,19 @@ EXPORT_SYMBOL_GPL(vhost_task_wake);
>   */
>  void vhost_task_stop(struct vhost_task *vtsk)
>  {
> +	bool in_signal = false;
> +
>  	mutex_lock(&vtsk->exit_mutex);
>  	if (!test_bit(VHOST_TASK_FLAGS_KILLED, &vtsk->flags)) {
>  		set_bit(VHOST_TASK_FLAGS_STOP, &vtsk->flags);
> +		in_signal = test_bit(VHOST_TASK_FLAGS_IN_SIGNAL, &vtsk->flags);
>  		vhost_task_wake(vtsk);
>  	}
>  	mutex_unlock(&vtsk->exit_mutex);
>  
> +	if (in_signal)
> +		return;

[Severity: Critical]
Can this cause a use-after-free of vtsk->exit_mutex in vhost_task_stop()?

Unlike spinlocks, a mutex must stay alive until mutex_unlock() returns, so
mutex_unlock() cannot hand off kfree(vtsk) ownership to another task.

When vhost_task_fn() returns from get_signal() (for example on SIGKILL or
SIGCONT) and contends on vtsk->exit_mutex while vhost_task_stop() holds it,
__mutex_unlock_slowpath() clears lock->owner before acquiring
lock->wait_lock:

CPU0 (vhost_task_stop)            CPU1 (vhost_task_fn)
mutex_lock(&vtsk->exit_mutex)
  set VHOST_TASK_FLAGS_STOP
  in_signal = true
  vhost_task_wake(vtsk)
                                  get_signal() returns
                                  mutex_lock(&vtsk->exit_mutex)
                                    sets MUTEX_FLAG_WAITERS
mutex_unlock(&vtsk->exit_mutex)
  __mutex_unlock_slowpath()
    clears lock->owner
                                    __mutex_trylock() succeeds
                                  mutex_unlock(&vtsk->exit_mutex)
                                  kfree(vtsk)
    raw_spin_lock(&lock->wait_lock)
      <-- use-after-free

The worker can acquire and unlock exit_mutex and free vtsk before
__mutex_unlock_slowpath() finishes, leaving vhost_task_stop() to access
lock->wait_lock and lock->first_waiter in freed slab memory and risk slab
corruption or a kernel crash.

Keeping vtsk alive until both mutex_unlock() calls return (for example with a
refcount_t dropped after unlocking), or using a spinlock_t for the handoff
instead of a mutex, would avoid this.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203162142.787153-1-K.Prabhakarr84@gmail.com?part=1

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-12-04 16:23 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 16:21 [PATCH v2] vhost_task: avoid hanging in vhost_task_stop() when worker is frozen Prabhakar Reddy K
2026-12-04 15:54 ` Jiaming Fan
2026-12-04 16:00   ` Jiaming Fan
2026-12-04 16:09 ` Jiaming Fan
2026-12-04 16:23 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox