From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.174 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796192758; b=Ywf+645+vT5HEQnvl0xnSfYXsN/cOyw2zezUpkl48WMlJsBDJxVWmEo5GNqjz0jvMwfS Wp5IDAI4LjXEu2ESAhtuj515PeL0KdMZ+8KeT7wOyLItaVa5f0yB6Ajo7RuRjBtA//jNo HVUecZML2c8E/6A7hGj6X71Bg/nKBMLjd5P3WeyCkbhfS8V4nO8APB2EG2FcBxndzQ/TC mVZnAv1alKMiELm1TeQ6acnBUx1JrLFbjrPMmEJVUZuL1Ixte3ztQBChWPp/nsXRmPGN9 fRaW8NNIhez+SOsMNojHJzHI2Lq9DxiKK1zV4Lr7oF1v0czYF8vDZieGTY+RATSYWbQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796192758; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:X-Google-Original-From:To:Cc:Subject:Date:Message-ID:X-Mailer: MIME-Version:Content-Transfer-Encoding; bh=X+7mknNTeXVzMByahGIvqNUAFZ+0f7sFciUAVAAdgro=; b=mA56Z4iv3unhP7ryYsorA/NWFV9NULKMp3J6IUOJitRZ4Fjl2vxgaJmGfn+WY+5Pdmu0 XdgG7jXTKzKmK2nUY5j2cN46/LLN+ADlLw878FMIhzX0VG91qao1GNv8NXGRF9rX5HQas 2nIaxMZtMgvitYwPBzil7oQCMLMjEx5PhdGfyER3t7+GQ4PtFDJ4LZXpKTVEfKutuMOGm qLJnxP7bVXCRqsZjvOsFmU8oX+/u9Kxrm1LUHr9d2Oy66h8bBM7Mw9PvO4q/fs6Ve+vEM u+02wMTV4lX4dQZFW/xxGonpnXDvPXAmovttMGRzMZNcMq0KjCbP4qv4DL1b63r03dA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=lUN4ET6N; arc=none smtp.remote-ip=74.125.82.174 Received: from mail-dy1-f174.google.com (mail-dy1-f174.google.com [74.125.82.174]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5AA9F606A31A for ; Wed, 2 Dec 2026 06:25:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 5AA9F606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=lUN4ET6N Received: by mail-dy1-f174.google.com with SMTP id 5a478bee46e88-386d1859e1cso6517009eec.0 for ; Tue, 01 Dec 2026 22:25:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796192757; x=1796797557; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=X+7mknNTeXVzMByahGIvqNUAFZ+0f7sFciUAVAAdgro=; b=lUN4ET6Ng2fslVzlS5qKvr8F/h2w61rlbLKk8IvyI/1qo1SiTHCXdybNUUDZ6Lg0wR uqmvIzXXumCYAC7w+ej+xSD17Pg0b8RiWYLTYvTh5aR/cPTGa+tnvX04liwHNkh+kOVV NTIpQmbgKaKOvvS/xD2Rv7jiKrZG9CCZwJPY60bK94VMGuYcR793tgWU54ZjB4RDeXer CHh1TxWtwj/4W9Uh2HQMFm87wEc7wsr2W16XMhowj7xUiSgJK+BaLyS3N0y5JVxeAYfC pO9aDNtqS5qv06tjZzWS6pLskVaa8ISx4eLwHhrr1AbHfqi1QXRMCy846poNUTBeli6E w+QA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796192757; x=1796797557; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=X+7mknNTeXVzMByahGIvqNUAFZ+0f7sFciUAVAAdgro=; b=H8W/QpfJ9Qe0FCLQAJ0wWw0Uvqn7q8bvnuuyKdVCyIbErdUOI6wEXzAsp/f0lJs409 q9Zmkp/Hfpg1rYViHD3Lp6VTE3FIGNWV38Z+YrfzqG3jM/b5O2KJyf9EUYBQ/3s9BCYA 65Grc8Kk/O+wxQGfUdyjvfqfCeGogPrF994VNhfZ/Rl3Jri9w+I2pctF9NAuU2SLXmIE MBpO+KRs5pv7LbPC/ZI0seLGVE5InTcU4VgvClm9Sd3vqHhZ3O02aMKbrwOzys8ihRR0 6xRH40cQ8gwRHRpK4qp6LabYG/G99ytM9rn/vDl6TgbdJVVd9NyA8LNtBgsJRgeegg9u Mtbg== X-Forwarded-Encrypted: i=1; AL+hErHi5ASXiSb3BxkLCLX/jMRYL1NGUWkuHLkI5Q/KQZ6kUD8AZomYh1DKMdAHcHMKxSHsbeHM+uxif55YNGY=@vger.kernel.org X-Gm-Message-State: APuU4Z/igB8HOYTL1WNiMH51SXvNLS3fV9KJY5/FR4q2YqrGvGIbVSWP aeuO63RP6u6LSm6KATBzlewDdGf7ubpCHwoq3bxcdAdpx/XDMmY80j9XsNePEQTWbStb0g== X-Gm-Gg: AZFdTZeCqBkGonDoyhkxnziQdSc+JfBfnNyf2q9uh9OZ63B+damJOF4VH44FtvUxrkT BRUPASsmn4VnS7LxpDUxv414ABQOa9uzk0xcvXXYZbv+Icxd4a+fsqpTei7ND/2jEmiacuhf2V6 xShlx29v2JR6Ez4DU9XkuH5tiWzEKMaiXfKZJlYNhA+cHQ4kYyLXX9oyV/1aLXejD2An3o9mLnv x5vEyz4CfhvMvL7Ia6vru7AgByE+GR+o9uobFJXlJcarB4RSCw2+LZyi72jZ67DB8zNaD4mWNb7 qpx4hJsxhNvoraTAKrUwb5GqPnZW+37jR99O69TTTqzoDy0yxK+T1QN1YZ2RnAsjcm/6hS4IMor VHvkWrkHRikLKOZOdLdfd8i4YAm+dsooK+xcwV6/b2ZwKk3TVew4XYyCYxoon/glpUCS292wr71 sMODMOrqqjNvJnu23WfdSwv6pG5YGEwwRu5z0mEmSBdq3L5VIxxWSx0uuthmz2NxbkqqUHT3yQS MsQ1LRkuGSuhiHe3U9bFYxBNnqPm4mG35bO224yxfaIrzcrFROE2ELJgi33huJUw0G/z+MM/8Sd evxqazqU8JNgHqIJSFw= X-Received: by 2002:a05:7301:d18b:b0:386:b4f1:dab0 with SMTP id 5a478bee46e88-386b4f1e04fmr30762017eec.1.1796192757242; Tue, 01 Dec 2026 22:25:57 -0800 (PST) Received: from fedora ([2409:4070:8e95:7706:a0da:ff07:8e64:44dc]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-385666e7f59sm121079378eec.6.2026.12.01.22.25.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 22:25:56 -0800 (PST) From: Prabhakar Reddy K X-Google-Original-From: Prabhakar Reddy K To: "Michael S . Tsirkin" , Jason Wang , =?UTF-8?q?Eugenio=20P=C3=A9rez?= Cc: kvm@vger.kernel.org, virtualization@lists.linux.dev, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Prabhakar Reddy K , syzbot+f59eab881ec20d423ab0@syzkaller.appspotmail.com Subject: [PATCH] vhost_task: avoid hanging in vhost_task_stop() when worker is frozen Date: Wed, 2 Dec 2026 11:55:16 +0530 Message-ID: <20261202062516.677164-1-K.Prabhakarr84@gmail.com> X-Mailer: git-send-email 2.56.1 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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