Netdev List
 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
  0 siblings, 0 replies; only message 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] only message in thread

only message in thread, other threads:[~2026-12-03 16:22 UTC | newest]

Thread overview: (only message) (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

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