From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.44 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796314957; b=rVajRICOeJBmmiQD6DqxakHS+usIk8ykkVgjZ3SkvooqUwoumMgHUAMoDE44u5rHHFSH JNnExdxoUnD6eFRFUhzN6KrSLoytPGFj0T2/fiV9535P4rbYEu25CsanXnTNhKoURjZtZ 4CDDnMlxr5AWDQQG2kiJ9a8nBWTbyrxq8meaJRQ4678t+7Vp1k88+NQdyqAzkTtrittH4 ZBWFP1zo2Bo7ZlVuyrVBAv5Tke4Td+/QqiTxjMH4zntE/H3LP3cPB3ia2s+B/7wSzT3Ss 5GXyErPYPyin0tY0KYz4ppSkVXrPL+vLy/CjqaGeizcBBADLQiPPDbRWuAK+owS3jWg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796314957; 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=rSqrIgaUQuXo3fQQbkB49qFiUsv3luc4MsqREUVeq94=; b=fA4vyBEQ6MPtStdjmcqf6i4aqvGzTMgLrhUsc7fiMvOQ0A8hfe9gxwtdLjZcbzC0y/hq YWB0PzdFiTNi6k4zurL0D002VX0M0I5vC+DZWzArSKjVGcvt/O9TccNJk3QEUmK7CCsbm NmAfLIhcOg88eObtedMcbIBOiTMvswajox3iDbp2sgHdZJm7jrFarEU1xpufawPcPPxpx SLO6nR6NFn4juL6XEyfpZ5qUAsSIhpdfitbzI6d7FfkiKmMbKOOYca8fR1qXsDQQ8HjtU krhmqTbTEE7hfwQXvz00qUNaChwUSx4hFmxllvavxsITY7zJxz502o6xJmfqV9fwq/Q== 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=EX9tYXIg; arc=none smtp.remote-ip=74.125.82.44 Received: from mail-dl1-f44.google.com (mail-dl1-f44.google.com [74.125.82.44]) (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 4DF86606A322 for ; Thu, 3 Dec 2026 16:22:37 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4DF86606A322 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=EX9tYXIg Received: by mail-dl1-f44.google.com with SMTP id a92af1059eb24-14394530ec6so10729963c88.1 for ; Thu, 03 Dec 2026 08:22:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796314956; x=1796919756; 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=rSqrIgaUQuXo3fQQbkB49qFiUsv3luc4MsqREUVeq94=; b=EX9tYXIgkLa9p4+FT6P0HKUPoFqWRlGuGWLcfqXlUoj4QCJxuu5EAFG1h8JCZ+K0/u njfsQ+YAmbyOv7NyKGffhHyXzDPbXgYyRlkIUDfvtZhyP8kWfyGiFuJjV75o+bH8pbpW wo28BYKQTInVnBH+6psUJJuY4fyHrqVK8dTbYGOD1kU0NZU6eurZrFoAFyA/xmbhCmLG LPv7YaWRBbaxxq1+M6JCu8rb0srhUsHIUYpqQSeq91Pv+sXrNMjdhy+L4YVHUcnEtsIz XMmLDAJmnWCBTrV2+ohjDYWl/pxtCXd5Tp/VUckbb8i02vT4iSeZ/2iZxkGmWidRknE9 HwEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796314956; x=1796919756; 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=rSqrIgaUQuXo3fQQbkB49qFiUsv3luc4MsqREUVeq94=; b=aFrWKxQoO158c2LlxN/O8rpylOvNA87t4ie32a9kK7m8DwZT6h8NjGMIbwWGoguTpp mhn1pdhCji97JaDUbO9TrLier+vN2IzwjUVrTWZ9nFabxDgt7v6k3vAzNMIkkeIYBFjE /hnx/Z2dJuCxlxnWoMPaOyaiiBxRJ7ZjL+wtHHRGl96DizgVXN+gF73oWZshC3VStPAu u2deOH5IHM16JdftQL0cgEIiR8axvSwGoOYqKf9Tb+zqud6AxjuDQPRmHHTHSSPtluwh KWMTVyeAzZinCcR8pp1er6WIPbTzjbSzMojvEjinaSV7/Q+XCo3+twb+0aWVMCbJsVZL FXmg== X-Forwarded-Encrypted: i=1; AL+hErHjLWgi7Cyu3JRqV42VhV4zsNGORxmtqJutNLjbhA4NmrCa90GcHz3G2V+jRkaL6Wj5DZpI+74=@vger.kernel.org X-Gm-Message-State: APuU4Z/mMNae0HAoOSt/w/O3MGEVJi5udzDx/wIibXLA0/EKY/IOPgyq u7zgQFPdD/Fg/qBb2KGgOrJY3S1rVvlfu8XLdMwuLA0+ZANxeUVNptao X-Gm-Gg: AZFdTZduKm51mURKVvYgQrjOOLpiLH6Gxrdibl/vvRYS8lTiPN2LKZQenCYypOZcOni cG7/fySAJSSgd6MVL/2GXNVJ06m7Gcjr+AXcdF106wX7rPG0N4nGkZ1nFX8rlWIikVxrN6rcLjd CeV0VX7ZKFcLnEMLnAUirm+FT5BLcxm6KdcxjE4DExHzVSQQdMef5Rekv0HG/ZhRgKRR0wC08IM BxFEZYSynz7A0jRQFiY2T7NMByn5L3jJGviWVr1N2VAoDXUNcK00teQwc6nqabULE/gJcjgqA5X dbXZYHN6pT0hhsK30CMOdM5hGsZt45KiuHo44pSNsREZz1ilDy3LuqgTddgf+pluKpGyXAJCrsD 3s56d1DwIul9Sa4850b94ifSXtsEtoXBn6wYaUOpI7WXS0TpFMHQuQMDc1jrFDN/DDcVxbALudW JxyKXmlFLJ4UgnVvgSqarerUjjyN8XGBKG1ximmu4rWyZ8z5NNn68RC1/XGM187h5B7VPL5PJUH bAreilzhg0I8+nuTfTIAknrvYX3dmVN0cLqptB8Q8iPosti9UJ4XtVRTBHkfBntkBVDCrNlGOOK Ak8J5CgeywlMyrAiS3FiJ78DogAIIH5E82qigXig X-Received: by 2002:a05:7300:6703:b0:387:4409:ac6 with SMTP id 5a478bee46e88-38744090d28mr2693057eec.35.1796314956139; Thu, 03 Dec 2026 08:22:36 -0800 (PST) Received: from fedora ([2409:4070:8e95:7706:a0da:ff07:8e64:44dc]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-3866f299d0bsm115614113eec.19.2026.12.03.08.22.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 08:22:35 -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 v2] vhost_task: avoid hanging in vhost_task_stop() when worker is frozen Date: Thu, 3 Dec 2026 21:51:42 +0530 Message-ID: <20261203162142.787153-1-K.Prabhakarr84@gmail.com> X-Mailer: git-send-email 2.56.1 X-Mailing-List: netdev@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 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 --- 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