From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796281779; b=0rQxjaAQrx3zy1zfAqiaLSygCOvetxz9TqIpYEXDbiHlsXc30SSCIO96+e67LtRiNQxT 1WOUzN0kFTbuMbxeks0qzadtMm/9npmgpD48Qrfhx+17JgzQrepwJO15RY1wnafvDTezZ NZqa7ZULj3p3eu8uboWHFTI0v90cjSBveLGs33/ramfevC8kd+Wz2VXIIDy68pm30QDj1 qeB/8geuWY90iRTtO0195pd3/DXW6z9b2c/jKnyJ92NVDsUYs13s+Q9A7E7AcxzeLtaPF TPY9w/qjTP7i70qKo1msneUK88Fj8a/rbM27rUv7V2VMerqA1s+FSLsiYfs7X4vX1aA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796281779; h=DMARC-Filter:DKIM-Signature:Received:X-MC-Unique: X-Mimecast-MFC-AGG-ID:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received: X-Received:Received:Date:From:To:Cc:Subject:Message-ID:References: MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=ETzGdDizTvzfXblD9XT0h92GlxtS52hMloknGgf2l5Q=; b=3kwLyuZNVxN4VM3PZukvJ71vAOW45rOvArP2kV8EtBp8HViN7AraW5VCNsuVXqFKZV9N CtyozFcm/HuF69q+HUVLUt/HEwwaoMPmzDMcL/HpGC37Om9R/m8eLFbTq324kCxF4djJU H6qckkdgnlZdtus5vYWP2DrI6GnR0nn4doyTfby6sOSQgcgZJ6xJhZkWD57jHcAIKphjr ZIRnOnVWnq09udkXS9EYEsUs2VrBHGoSoPXj4aE7sc387Xq6/vu6SIAVNcI0Zt5gZg247 3HlZqGvVqWi1degDX9hJZJc6SmqMfacjqMaG6mj5yGPGo0cyz3Jl34op0PdTNfceUqQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=AZG4nQqH; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=google header.b=t2jxggk+; arc=none smtp.remote-ip=170.10.133.124 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A18E5606A693 for ; Thu, 3 Dec 2026 07:09:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A18E5606A693 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=AZG4nQqH; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=google header.b=t2jxggk+ DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796281777; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=ETzGdDizTvzfXblD9XT0h92GlxtS52hMloknGgf2l5Q=; b=AZG4nQqHIiGx2zaMtMoo+11V5iFfI/u9KOC+KekKyh74JSAjM2n5PvWm1h7wPQv8VxkBnY 256BeewduA255yb8oFMC/ueQw2MJukTQzjiZb9GzupdI+PA1TmUVeVqek8BWsv5ovft7E4 6PIdZLmYQxUDI+0vO1a64V7On655748= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-308-v-UQ41FlM56rqgX9pINT1g-1; Thu, 3 Dec 2026 07:09:33 +0000 X-MC-Unique: v-UQ41FlM56rqgX9pINT1g-1 X-Mimecast-MFC-AGG-ID: v-UQ41FlM56rqgX9pINT1g_1796281772 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-4bb8da397aaso4298315e9.0 for ; Wed, 02 Dec 2026 23:09:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1796281772; x=1796886572; darn=vger.kernel.org; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to:content-type; bh=ETzGdDizTvzfXblD9XT0h92GlxtS52hMloknGgf2l5Q=; b=t2jxggk+ddxnWkxOFeAqtLtH2Tio5h2UC3RhzBXx1g18mWOZha6Nlk/xFjZk1cSKT8 27FBuBO7oI/KeexTC4rwpOsdjVv4RJ3KgvhvEdX/vfGCFmjh3GhFtBEjzaI2E1bjHkYr mBbH7TntKcuW/djbAbfOrQPBTbvR5iYxNnbzO6erCpZgxdY+IIPmJGUU/Mkfou0Kru2X m+0Agtvhwkm0v2KgvCsqVlAOobgpaiEbpcw3MbBT9V3zVceOMilt0QjpxJzThS/3CsL5 nAsN9vUGMRFaFRAgXtUGV6Gy6R5duNdh5qDwAcyMAS7Ckp/mePODLjlMWdu9t8YEhi4F /CsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796281772; x=1796886572; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh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YNdN14b iGlQH2McFJ/R4moZoB2rMMr33FRYRkNyyzaV3HisXnw== X-Received: by 2002:a7b:cd14:0:b0:4a2:10d:5e0d with SMTP id 5b1f17b1804b1-4bbe19f3dbamr15079125e9.14.1796281772237; Wed, 02 Dec 2026 23:09:32 -0800 (PST) X-Received: by 2002:a7b:cd14:0:b0:4a2:10d:5e0d with SMTP id 5b1f17b1804b1-4bbe19f3dbamr15078545e9.14.1796281771567; Wed, 02 Dec 2026 23:09:31 -0800 (PST) Received: from redhat.com ([2a0d:6fc0:153a:4c00:6468:7acf:febf:a046]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ca120cbee5sm13697571f8f.32.2026.12.02.23.09.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2026 23:09:30 -0800 (PST) Date: Thu, 3 Dec 2026 02:09:28 -0500 From: "Michael S. Tsirkin" To: Prabhakar Reddy K Cc: Jason Wang , Eugenio =?iso-8859-1?Q?P=E9rez?= , kvm@vger.kernel.org, virtualization@lists.linux.dev, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, syzbot+f59eab881ec20d423ab0@syzkaller.appspotmail.com Subject: Re: [PATCH] vhost_task: avoid hanging in vhost_task_stop() when worker is frozen Message-ID: <20261203020714-mutt-send-email-mst@kernel.org> References: <20261202062516.677164-1-K.Prabhakarr84@gmail.com> X-Mailing-List: kvm@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20261202062516.677164-1-K.Prabhakarr84@gmail.com> 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 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 > -#include > +#include > #include > #include > #include > @@ -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 >