From nobody Sun Feb 8 21:32:41 2026 Received: from mail-qv1-f49.google.com (mail-qv1-f49.google.com [209.85.219.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F019333D6E6 for ; Fri, 16 Jan 2026 16:03:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768579383; cv=none; b=vAZN376qjkiM4O0V+CSiE0ERvwbZ8Q95AN6k4pNjJqGyZ/M5Cnt6rZPFP5IYx7laczRttwpJAAtWqCHtAUn+f8tETyAmepcTH1944q0hrybqdTIjuAQgKuTEZav52KOM+ciKoPZoZyPM2nBEEx/ga9zPM3wO0WL8Yt41VhZU/4s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768579383; c=relaxed/simple; bh=6c0reEst+EIZA2PaSJbeOygkyAUIcKW0sGGQ6IOzV48=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:Cc; b=X6mPhsxFxnXv9YlegX4WRXkVR8mpQ/CxMHYJXzhb5VEQNrzOvZ7s3V1ML/WvChDhjVuZ4zcZtg8O7ewXH6K3kwY0s8kZwHfi0sEhutM6v1OZiq1qViS8Hw5NcPdWmZ/rmwFoaaoD6DEZfjBN5GaO3F0gmdS3BuLIBlzioc9o8/k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=debian.org; spf=pass smtp.mailfrom=gmail.com; arc=none smtp.client-ip=209.85.219.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=debian.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-qv1-f49.google.com with SMTP id 6a1803df08f44-8887f43b224so40302836d6.1 for ; Fri, 16 Jan 2026 08:03:01 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768579381; x=1769184181; h=cc:to:message-id:content-transfer-encoding:mime-version:subject :date:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=pvIuhV9+sNv3iCssx0o0Pe32iUx/gKwQFY7jwtHcvQU=; b=rc1GSJ96ksPXECOtjPe3/+V67CXXoV6hH2p1o2M4yLGCjLObOmWTkKGPlIcDfhss/a /I1kIy0UwwUEWhO/xfjP/ZCjuf0rXDdVPF0IqVJYGLH08PNn4w85f1F85CIk4UV1/DkF Tx94ciTFjVX34yK3KObIc345sRKSIM0Ux5v0MxT0C+79S3wubxTiIWfvttllzxzubweF sf6Ktb2iDjfv2peQLm4t43kAk7lRSnkC0uQXXnDp6d1BR+n22K2GknAYD+0u4yjd1155 EF+ZVquTU5VCM5ToSRc6X+18SlpRXfpXLcrSJgpNaDxw3Ts4M4ifoJYB/ve82YvNkoP1 Ul4w== X-Forwarded-Encrypted: i=1; AJvYcCWeYEzrX6Om2lLH6zmjRbV4bpYsnM6YvwTmwTCbTTjZna+JKoKiM0VVP3udFwTA2vld4zVbs8Ca3WVQuRU=@vger.kernel.org X-Gm-Message-State: AOJu0YxT2ix9XEWCJIx2UkNlp7zyRFAMyC1/8GshxM6mOaz3HCrQl2ns 75XpgXihD2Sv/gsLOR9fIOJm7yZ+gqd/n0tNyzJeD7IYypmNnTixEYcNXCsHZw== X-Gm-Gg: AY/fxX6xEf2lKnzDclV3GfuTsNrw7KqZJ972sw240RhivSzolVS1wwDyYfv7060NDiN 5UZJcMF/mNMPq/4kE/i2N6dTEcaiYbKCqFeXAjgMTHVft2yOe3BxUE/2mdsFVF0ZJkC11LCJhR1 ijYfSGUara3LmOHnXiRNSMKo1DpyhxsNGafKhAKWoJfPheO+DPTP6OAYwwzl6TdQY8VHiqNBjt8 N5XGi7lM9ZIP9VigP2qQcXjF75vg23BN5+Qurvd4VVyjVqgf1GYlHtuNu47CHqpyDzZWcRzrvih LYD2FDYDEiwAGfS6T1V3/4L2YtoXWfyp4BrXS2xj9e3gvgOwrWrg06MB342hHSoIHK4egLfbSHe G/kuoYkRvbhJ/1r1gO1rEPfrjmdHUwAdgO+RfguhoivokRpwPaW/3wzgB5GNcQBFq1pdqAnW9o/ J3bpotn6kFf2vt X-Received: by 2002:a4a:bb17:0:b0:661:1cbc:45c2 with SMTP id 006d021491bc7-6611cbc4928mr409451eaf.16.1768572623144; Fri, 16 Jan 2026 06:10:23 -0800 (PST) Received: from localhost ([2a03:2880:10ff:57::]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-661186dc702sm1263388eaf.6.2026.01.16.06.10.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Jan 2026 06:10:22 -0800 (PST) From: Breno Leitao Date: Fri, 16 Jan 2026 06:10:11 -0800 Subject: [PATCH] mm/kfence: fix potential deadlock in reboot notifier Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260116-kfence_fix-v1-1-4165a055933f@debian.org> X-B4-Tracking: v=1; b=H4sIAMNGamkC/yXMQQqDMBAF0KsMf20gCa3UXEWk1OmkHYUoiS2Ce PeiXb7N21AkqxQE2pDlq0WnhECuIvD7kV5i9IlA8NbX1rnajFESyz3qaprGXnt/u0THjIowZ4m 6nlnb/V0+/SC8HAP2/QeVxSfzbgAAAA== X-Change-ID: 20260116-kfence_fix-9905b284f1cc To: Alexander Potapenko , Marco Elver , Dmitry Vyukov , Andrew Morton Cc: kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, clm@meta.com, kernel-team@meta.com, Breno Leitao X-Mailer: b4 0.15-dev-47773 X-Developer-Signature: v=1; a=openpgp-sha256; l=3280; i=leitao@debian.org; h=from:subject:message-id; bh=6c0reEst+EIZA2PaSJbeOygkyAUIcKW0sGGQ6IOzV48=; b=owEBbQKS/ZANAwAIATWjk5/8eHdtAcsmYgBpakbN6YhptWCcXUtZJrdJi0Sq9v6y2NypuVlhA yll/P7R01+JAjMEAAEIAB0WIQSshTmm6PRnAspKQ5s1o5Of/Hh3bQUCaWpGzQAKCRA1o5Of/Hh3 bbf8D/4oIWJVhIsaxEVusMGtLueC27JQrU8Qczg0pBer7m8PD7bV1s+HYYH5xxfhf8jnL+fwFX/ Y5UKCbWen/HKE7lnSDIjQRFp/XpFIWYk9cMBu1/yItTsV0deSxajEFTHSHlBvGREXJ2gfntDAVN pJ3sXJrRxGeKsW0ElHzjSOFrwKeYpDHPMb4s0aXMOThEUZYboel+XK+nB1ulTaP+FkFlBiyMcM2 hQG6b7KGmqi7hMUdiHUDm4k9nfdNECUObvqOdVyiJ8NOLs5GwyiIFMWvEjxMzHUTTB7a3IyeIce oK6u/BNhCT5AMpJf+QHz7DTQO2WfSJOb9rXCqe+7B5x+WiOQDWH1AyNVpLDVm8jo+LYFma+XwKN xCOUE9yJlAItKA3dOqNKhYhmgRJlJdRgNlEj2gU4sD3R1IsB4rBx6HLaVWgYYKe8ZAxGPNVE+3q KlF6sEG3hSFkc6foQrKPjVbFGqfcwh5yvRjUJ/k2CzRAA5vWGwRXkDcOSiAgFXbi+BW9WeNJMGO R+ntiHljO8OBuvxiDFIbPQPYiAPZaOLsgkxsMZl3ySAaavBcVryqykSbLsbyVGVsonEY83pEhoh mX9DqV2Q/qGcKbYsBMsGNxYlhe5beqgS7pyFd8rsBc62nZ/tf8S/piARqgtqyC6Mioj3jg3I9rx phWuOGd9v3zpcWw== X-Developer-Key: i=leitao@debian.org; a=openpgp; fpr=AC8539A6E8F46702CA4A439B35A3939FFC78776D The reboot notifier callback can deadlock when calling cancel_delayed_work_sync() if toggle_allocation_gate() is blocked in wait_event_idle() waiting for allocations, that might not happen on shutdown path. The issue is that cancel_delayed_work_sync() waits for the work to complete, but the work is waiting for kfence_allocation_gate > 0 which requires allocations to happen (each allocation is increated by 1) - allocations that may have stopped during shutdown. Fix this by: 1. Using cancel_delayed_work() (non-sync) to avoid blocking. Now the callback succeeds and return. 2. Adding wake_up() to unblock any waiting toggle_allocation_gate() 3. Adding !kfence_enabled to the wait condition so the wake succeeds The static_branch_disable() IPI will still execute after the wake, but at this early point in shutdown (reboot notifier runs with INT_MAX priority), the system is still functional and CPUs can respond to IPIs. Reported-by: Chris Mason Closes: https://lore.kernel.org/all/20260113140234.677117-1-clm@meta.com/ Fixes: ce2bba89566b ("mm/kfence: add reboot notifier to disable KFENCE on s= hutdown") Signed-off-by: Breno Leitao Reviewed-by: Marco Elver --- mm/kfence/core.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/mm/kfence/core.c b/mm/kfence/core.c index 577a1699c553..da0f5b6f5744 100644 --- a/mm/kfence/core.c +++ b/mm/kfence/core.c @@ -823,6 +823,9 @@ static struct notifier_block kfence_check_canary_notifi= er =3D { static struct delayed_work kfence_timer; =20 #ifdef CONFIG_KFENCE_STATIC_KEYS +/* Wait queue to wake up allocation-gate timer task. */ +static DECLARE_WAIT_QUEUE_HEAD(allocation_wait); + static int kfence_reboot_callback(struct notifier_block *nb, unsigned long action, void *data) { @@ -832,7 +835,12 @@ static int kfence_reboot_callback(struct notifier_bloc= k *nb, */ WRITE_ONCE(kfence_enabled, false); /* Cancel any pending timer work */ - cancel_delayed_work_sync(&kfence_timer); + cancel_delayed_work(&kfence_timer); + /* + * Wake up any blocked toggle_allocation_gate() so it can complete + * early while the system is still able to handle IPIs. + */ + wake_up(&allocation_wait); =20 return NOTIFY_OK; } @@ -842,9 +850,6 @@ static struct notifier_block kfence_reboot_notifier =3D= { .priority =3D INT_MAX, /* Run early to stop timers ASAP */ }; =20 -/* Wait queue to wake up allocation-gate timer task. */ -static DECLARE_WAIT_QUEUE_HEAD(allocation_wait); - static void wake_up_kfence_timer(struct irq_work *work) { wake_up(&allocation_wait); @@ -873,7 +878,9 @@ static void toggle_allocation_gate(struct work_struct *= work) /* Enable static key, and await allocation to happen. */ static_branch_enable(&kfence_allocation_key); =20 - wait_event_idle(allocation_wait, atomic_read(&kfence_allocation_gate) > 0= ); + wait_event_idle(allocation_wait, + atomic_read(&kfence_allocation_gate) > 0 || + !READ_ONCE(kfence_enabled)); =20 /* Disable static key and reset timer. */ static_branch_disable(&kfence_allocation_key); --- base-commit: 983d014aafb14ee5e4915465bf8948e8f3a723b5 change-id: 20260116-kfence_fix-9905b284f1cc Best regards, -- =20 Breno Leitao