Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Prabhakar Reddy K" <k.prabhakarr84@gmail.com>
Cc: kvm@vger.kernel.org, virtualization@lists.linux.dev,
	"Michael S. Tsirkin" <mst@redhat.com>,
	"Eugenio Perez" <eperezma@redhat.com>
Subject: Re: [PATCH v2] vhost_task: avoid hanging in vhost_task_stop() when worker is frozen
Date: Fri, 04 Dec 2026 16:23:23 +0000	[thread overview]
Message-ID: <sashiko-outbox-250179@kernel.org> (raw)
In-Reply-To: <20261203162142.787153-1-K.Prabhakarr84@gmail.com>

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

      parent reply	other threads:[~2026-12-04 16:23 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

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=sashiko-outbox-250179@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=eperezma@redhat.com \
    --cc=k.prabhakarr84@gmail.com \
    --cc=kvm@vger.kernel.org \
    --cc=mst@redhat.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --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