Kernel KVM virtualization development
 help / color / mirror / Atom feed
* [PATCH] vhost_task: avoid hanging in vhost_task_stop() when worker is frozen
@ 2026-12-02  6:25 Prabhakar Reddy K
  2026-12-03  6:26 ` sashiko-bot
  2026-12-03  7:09 ` Michael S. Tsirkin
  0 siblings, 2 replies; 3+ messages in thread
From: Prabhakar Reddy K @ 2026-12-02  6:25 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 scenario:
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.

Instead of waiting for a frozen or stopped worker to exit, split the
two tasks that wait_for_completion() previously did:

- Lifetime of struct vhost_task: Replace struct completion with a
  refcount_t initialized to 2 (one reference for the worker, one for the
  caller). Whichever side finishes last releases vtsk->task and frees vtsk.
- Lifetime of vtsk->data: Extend exit_mutex to also serialize vtsk->fn().
  In vhost_task_stop(), set VHOST_TASK_FLAGS_STOP and wake the worker
  first (so callbacks sleeping in schedule_timeout() wake up immediately),
  then briefly lock and unlock exit_mutex. This waits for any in-flight
  vtsk->fn() or handle_sigkill() to finish without blocking on a worker
  trapped in get_signal().

When the cgroup is later thawed or the process receives SIGCONT/SIGKILL,
the worker leaves get_signal(), sees VHOST_TASK_FLAGS_STOP, skips
vtsk->fn() and handle_sigkill(), drops its reference, and exits cleanly.

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>
---
Tested with the syzbot cgroup v1 reproducer, custom cgroup v2 and SIGSTOP
reproducers, and a 100-iteration race loop under KASAN and Lockdep with no
warnings or leaked worker threads.

This is one of my first kernel patches, so any feedback on the approach
(especially regarding exit_mutex in vhost_task_fn's loop) is very welcome

 kernel/vhost_task.c | 40 +++++++++++++++++++++++++---------------
 1 file changed, 25 insertions(+), 15 deletions(-)

diff --git a/kernel/vhost_task.c b/kernel/vhost_task.c
index 3717885a5..56b908046 100644
--- a/kernel/vhost_task.c
+++ b/kernel/vhost_task.c
@@ -3,7 +3,7 @@
  * Copyright (C) 2021 Oracle Corporation
  */
 #include <linux/slab.h>
-#include <linux/completion.h>
+#include <linux/refcount.h>
 #include <linux/sched/task.h>
 #include <linux/sched/vhost_task.h>
 #include <linux/sched/signal.h>
@@ -17,13 +17,21 @@ struct vhost_task {
 	bool (*fn)(void *data);
 	void (*handle_sigkill)(void *data);
 	void *data;
-	struct completion exited;
+	refcount_t refcnt;
 	unsigned long flags;
 	struct task_struct *task;
-	/* serialize SIGKILL and vhost_task_stop calls */
+	/* serialize fn, SIGKILL, and vhost_task_stop calls */
 	struct mutex exit_mutex;
 };
 
+static void vhost_task_put(struct vhost_task *vtsk)
+{
+	if (refcount_dec_and_test(&vtsk->refcnt)) {
+		put_task_struct(vtsk->task);
+		kfree(vtsk);
+	}
+}
+
 static int vhost_task_fn(void *data)
 {
 	struct vhost_task *vtsk = data;
@@ -38,15 +46,19 @@ static int vhost_task_fn(void *data)
 				break;
 		}
 
+		mutex_lock(&vtsk->exit_mutex);
 		/* mb paired w/ vhost_task_stop */
 		set_current_state(TASK_INTERRUPTIBLE);
 
 		if (test_bit(VHOST_TASK_FLAGS_STOP, &vtsk->flags)) {
 			__set_current_state(TASK_RUNNING);
+			mutex_unlock(&vtsk->exit_mutex);
 			break;
 		}
 
 		did_work = vtsk->fn(vtsk->data);
+		mutex_unlock(&vtsk->exit_mutex);
+
 		if (!did_work)
 			schedule();
 	}
@@ -62,8 +74,8 @@ static int vhost_task_fn(void *data)
 		vtsk->handle_sigkill(vtsk->data);
 	}
 	mutex_unlock(&vtsk->exit_mutex);
-	complete(&vtsk->exited);
 
+	vhost_task_put(vtsk);
 	do_exit(0);
 }
 
@@ -88,20 +100,18 @@ 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);
+	set_bit(VHOST_TASK_FLAGS_STOP, &vtsk->flags);
+	if (!test_bit(VHOST_TASK_FLAGS_KILLED, &vtsk->flags))
 		vhost_task_wake(vtsk);
-	}
-	mutex_unlock(&vtsk->exit_mutex);
 
 	/*
-	 * Make sure vhost_task_fn is no longer accessing the vhost_task before
-	 * freeing it below.
+	 * Wait for any in-flight vtsk->fn() or handle_sigkill() to finish
+	 * before returning so callers can safely free vtsk->data.
 	 */
-	wait_for_completion(&vtsk->exited);
-	put_task_struct(vtsk->task);
-	kfree(vtsk);
+	mutex_lock(&vtsk->exit_mutex);
+	mutex_unlock(&vtsk->exit_mutex);
+
+	vhost_task_put(vtsk);
 }
 EXPORT_SYMBOL_GPL(vhost_task_stop);
 
@@ -134,7 +144,7 @@ struct vhost_task *vhost_task_create(bool (*fn)(void *),
 	vtsk = kzalloc_obj(*vtsk);
 	if (!vtsk)
 		return ERR_PTR(-ENOMEM);
-	init_completion(&vtsk->exited);
+	refcount_set(&vtsk->refcnt, 2);
 	mutex_init(&vtsk->exit_mutex);
 	vtsk->data = arg;
 	vtsk->fn = fn;
-- 
2.56.1


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

* Re: [PATCH] vhost_task: avoid hanging in vhost_task_stop() when worker is frozen
  2026-12-02  6:25 [PATCH] vhost_task: avoid hanging in vhost_task_stop() when worker is frozen Prabhakar Reddy K
@ 2026-12-03  6:26 ` sashiko-bot
  2026-12-03  7:09 ` Michael S. Tsirkin
  1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-12-03  6:26 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:
- [Medium] vhost_task: per-iteration exit_mutex lock in vhost_task_fn()
  hot path
--

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

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

This commit replaces vhost_task's exit completion with a refcount_t and
serializes vtsk->fn() with exit_mutex so vhost_task_stop() can return
without waiting for a frozen or stopped worker thread to exit.

> diff --git a/kernel/vhost_task.c b/kernel/vhost_task.c
> index 3717885a5992a..56b9080469ed7 100644
> --- a/kernel/vhost_task.c
> +++ b/kernel/vhost_task.c
[ ... ]
> @@ -38,15 +46,19 @@ static int vhost_task_fn(void *data)
>  				break;
>  		}
>  
> +		mutex_lock(&vtsk->exit_mutex);
>  		/* mb paired w/ vhost_task_stop */
>  		set_current_state(TASK_INTERRUPTIBLE);
>  
>  		if (test_bit(VHOST_TASK_FLAGS_STOP, &vtsk->flags)) {
>  			__set_current_state(TASK_RUNNING);
> +			mutex_unlock(&vtsk->exit_mutex);
>  			break;
>  		}
>  
>  		did_work = vtsk->fn(vtsk->data);
> +		mutex_unlock(&vtsk->exit_mutex);
> +
>  		if (!did_work)
>  			schedule();
>  	}

[Severity: Medium]
Does locking vtsk->exit_mutex on every iteration of vhost_task_fn() add
mutex overhead to the lockless vhost I/O worker hot path and hold the
mutex across multi-second sleeps?

For vhost I/O workers, vhost_run_work_list() drains worker->work_list
locklessly via llist_del_all(), and each wakeup runs at least two loop
iterations (one to process the work and a second to re-check the list
before calling schedule()), incurring at least two mutex_lock() and
mutex_unlock() pairs per wakeup.

In addition, kvm_nx_huge_page_recovery_worker() sleeps in
schedule_timeout() inside vtsk->fn():

arch/x86/kvm/mmu/mmu.c:kvm_nx_huge_page_recovery_worker() {
    ...
	if (remaining_time > 0) {
		schedule_timeout(remaining_time);
		/* check for signals and come back */
		return true;
	}
    ...
}

Holding vtsk->exit_mutex across that sleep forces vhost_task_stop() to
block in TASK_UNINTERRUPTIBLE on exit_mutex until the worker is scheduled
back onto a CPU to return from schedule_timeout(), which can stall if the
worker's cgroup CPU quota is throttled.

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

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

* Re: [PATCH] vhost_task: avoid hanging in vhost_task_stop() when worker is frozen
  2026-12-02  6:25 [PATCH] vhost_task: avoid hanging in vhost_task_stop() when worker is frozen Prabhakar Reddy K
  2026-12-03  6:26 ` sashiko-bot
@ 2026-12-03  7:09 ` Michael S. Tsirkin
  1 sibling, 0 replies; 3+ messages in thread
From: Michael S. Tsirkin @ 2026-12-03  7:09 UTC (permalink / raw)
  To: Prabhakar Reddy K
  Cc: Jason Wang, Eugenio Pérez, kvm, virtualization, netdev,
	linux-kernel, syzbot+f59eab881ec20d423ab0

On Wed, Dec 02, 2026 at 11:55:16AM +0530, Prabhakar Reddy K wrote:
> 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 scenario:
> 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.
> 
> Instead of waiting for a frozen or stopped worker to exit, split the
> two tasks that wait_for_completion() previously did:
> 
> - Lifetime of struct vhost_task: Replace struct completion with a
>   refcount_t initialized to 2 (one reference for the worker, one for the
>   caller). Whichever side finishes last releases vtsk->task and frees vtsk.
> - Lifetime of vtsk->data: Extend exit_mutex to also serialize vtsk->fn().
>   In vhost_task_stop(), set VHOST_TASK_FLAGS_STOP and wake the worker
>   first (so callbacks sleeping in schedule_timeout() wake up immediately),
>   then briefly lock and unlock exit_mutex. This waits for any in-flight
>   vtsk->fn() or handle_sigkill() to finish without blocking on a worker
>   trapped in get_signal().
> 
> When the cgroup is later thawed or the process receives SIGCONT/SIGKILL,
> the worker leaves get_signal(), sees VHOST_TASK_FLAGS_STOP, skips
> vtsk->fn() and handle_sigkill(), drops its reference, and exits cleanly.
> 
> 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>

Sorry I'm not inclined to add extra locking nd refcounting all over the
place for the sole benefit of the slowest of the slow paths of freeze.
Pls find a way to do it without so much overhead.


> ---
> Tested with the syzbot cgroup v1 reproducer, custom cgroup v2 and SIGSTOP
> reproducers, and a 100-iteration race loop under KASAN and Lockdep with no
> warnings or leaked worker threads.
> 
> This is one of my first kernel patches, so any feedback on the approach
> (especially regarding exit_mutex in vhost_task_fn's loop) is very welcome
> 
>  kernel/vhost_task.c | 40 +++++++++++++++++++++++++---------------
>  1 file changed, 25 insertions(+), 15 deletions(-)
> 
> diff --git a/kernel/vhost_task.c b/kernel/vhost_task.c
> index 3717885a5..56b908046 100644
> --- a/kernel/vhost_task.c
> +++ b/kernel/vhost_task.c
> @@ -3,7 +3,7 @@
>   * Copyright (C) 2021 Oracle Corporation
>   */
>  #include <linux/slab.h>
> -#include <linux/completion.h>
> +#include <linux/refcount.h>
>  #include <linux/sched/task.h>
>  #include <linux/sched/vhost_task.h>
>  #include <linux/sched/signal.h>
> @@ -17,13 +17,21 @@ struct vhost_task {
>  	bool (*fn)(void *data);
>  	void (*handle_sigkill)(void *data);
>  	void *data;
> -	struct completion exited;
> +	refcount_t refcnt;
>  	unsigned long flags;
>  	struct task_struct *task;
> -	/* serialize SIGKILL and vhost_task_stop calls */
> +	/* serialize fn, SIGKILL, and vhost_task_stop calls */
>  	struct mutex exit_mutex;
>  };
>  
> +static void vhost_task_put(struct vhost_task *vtsk)
> +{
> +	if (refcount_dec_and_test(&vtsk->refcnt)) {
> +		put_task_struct(vtsk->task);
> +		kfree(vtsk);
> +	}
> +}
> +
>  static int vhost_task_fn(void *data)
>  {
>  	struct vhost_task *vtsk = data;
> @@ -38,15 +46,19 @@ static int vhost_task_fn(void *data)
>  				break;
>  		}
>  
> +		mutex_lock(&vtsk->exit_mutex);
>  		/* mb paired w/ vhost_task_stop */
>  		set_current_state(TASK_INTERRUPTIBLE);
>  
>  		if (test_bit(VHOST_TASK_FLAGS_STOP, &vtsk->flags)) {
>  			__set_current_state(TASK_RUNNING);
> +			mutex_unlock(&vtsk->exit_mutex);
>  			break;
>  		}
>  
>  		did_work = vtsk->fn(vtsk->data);
> +		mutex_unlock(&vtsk->exit_mutex);
> +
>  		if (!did_work)
>  			schedule();
>  	}
> @@ -62,8 +74,8 @@ static int vhost_task_fn(void *data)
>  		vtsk->handle_sigkill(vtsk->data);
>  	}
>  	mutex_unlock(&vtsk->exit_mutex);
> -	complete(&vtsk->exited);
>  
> +	vhost_task_put(vtsk);
>  	do_exit(0);
>  }
>  
> @@ -88,20 +100,18 @@ 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);
> +	set_bit(VHOST_TASK_FLAGS_STOP, &vtsk->flags);
> +	if (!test_bit(VHOST_TASK_FLAGS_KILLED, &vtsk->flags))
>  		vhost_task_wake(vtsk);
> -	}
> -	mutex_unlock(&vtsk->exit_mutex);
>  
>  	/*
> -	 * Make sure vhost_task_fn is no longer accessing the vhost_task before
> -	 * freeing it below.
> +	 * Wait for any in-flight vtsk->fn() or handle_sigkill() to finish
> +	 * before returning so callers can safely free vtsk->data.
>  	 */
> -	wait_for_completion(&vtsk->exited);
> -	put_task_struct(vtsk->task);
> -	kfree(vtsk);
> +	mutex_lock(&vtsk->exit_mutex);
> +	mutex_unlock(&vtsk->exit_mutex);
> +
> +	vhost_task_put(vtsk);
>  }
>  EXPORT_SYMBOL_GPL(vhost_task_stop);
>  
> @@ -134,7 +144,7 @@ struct vhost_task *vhost_task_create(bool (*fn)(void *),
>  	vtsk = kzalloc_obj(*vtsk);
>  	if (!vtsk)
>  		return ERR_PTR(-ENOMEM);
> -	init_completion(&vtsk->exited);
> +	refcount_set(&vtsk->refcnt, 2);
>  	mutex_init(&vtsk->exit_mutex);
>  	vtsk->data = arg;
>  	vtsk->fn = fn;
> -- 
> 2.56.1
> 

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

end of thread, other threads:[~2026-12-03  7:09 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02  6:25 [PATCH] vhost_task: avoid hanging in vhost_task_stop() when worker is frozen Prabhakar Reddy K
2026-12-03  6:26 ` sashiko-bot
2026-12-03  7:09 ` Michael S. Tsirkin

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