Netdev List
 help / color / mirror / Atom feed
From: Prabhakar Reddy K <k.prabhakarr84@gmail.com>
To: "Michael S . Tsirkin" <mst@redhat.com>,
	"Jason Wang" <jasowang@gmail.com>,
	"Eugenio Pérez" <eperezma@redhat.com>
Cc: kvm@vger.kernel.org, virtualization@lists.linux.dev,
	netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	Prabhakar Reddy K <K.Prabhakarr84@gmail.com>,
	syzbot+f59eab881ec20d423ab0@syzkaller.appspotmail.com
Subject: [PATCH v2] vhost_task: avoid hanging in vhost_task_stop() when worker is frozen
Date: Thu,  3 Dec 2026 21:51:42 +0530	[thread overview]
Message-ID: <20261203162142.787153-1-K.Prabhakarr84@gmail.com> (raw)

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


                 reply	other threads:[~2026-12-03 16:22 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261203162142.787153-1-K.Prabhakarr84@gmail.com \
    --to=k.prabhakarr84@gmail.com \
    --cc=eperezma@redhat.com \
    --cc=jasowang@gmail.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mst@redhat.com \
    --cc=netdev@vger.kernel.org \
    --cc=syzbot+f59eab881ec20d423ab0@syzkaller.appspotmail.com \
    --cc=virtualization@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox