From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.43 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796031976; b=IWd5FIy7UconY4kiPMTM82SoUW5YQSoH0jbUagA15daGwq4sIGgB/WFZ5E6Fo1BQE4tI faSbRWkAfjzdsH2auP4yvbAPLMIuojkwJzC6wunv304GkaKXDnE7391Ufmh26+ONdGcph /YMKgNjzuZGb3vQ3ZohFLgdEKlbLPsENpmkiuD6u43RYIeDe5VOcBUvbiMoAyyXUEkHyb UkEFcTiEIt/b6bwJ9Z7Z7d099F+FH/4+e847qo1LrQS241Pqew16d7Gx5XX4eolWk3v1A t0o5QbcZ1xhjnnDpgdZ3t1KcELk1b4ttcEUR6N/lL4H8uBco7w6AcjSt8qjtEnZqPzg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796031976; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=clKnKa74zzxYpZxW7jtUDYmZNVkcz4zcnjY7OKqY+ac=; b=Glzew7UYfhBHpEx4Fd2pT6z0RCQzvuvUS8Ny8A5cWQ5awdzPRCcfaNGz8d4EkN9gPtgW c/q+6q1vjjUpvri9X1LcA2sKLFfot0grc8zygL0/lYRrThJkEVOz+ffm//8PWQI5W6YRe riw7L5pyR5uQ4WJs6r/pFC6VbKpt1BnaQpNu4QQ5BmFZWNp9SG8AtLwGpC2epRo/trxRq L4WZnp98xRiaqsT1Uf4FEDFGhQyxsnWhYoSp+n8SfCr3UiV7YJ/P0nDAHwmNpPqvxWY+H DtnnfYUQl6feZZmYOn/yJivBkZo3wQu9nXRPkt2y8yA2A4eKs0Di3j8e+/itPytspAQ== 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=IIfusatS; arc=none smtp.remote-ip=209.85.128.43 Received: from mail-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.43]) (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 908D3606A31C for ; Mon, 30 Nov 2026 09:46:15 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 908D3606A31C 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=IIfusatS Received: by mail-wm1-f43.google.com with SMTP id 5b1f17b1804b1-4bb77a462c6so79209465e9.1 for ; Mon, 30 Nov 2026 01:46:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796031974; x=1796636774; 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=clKnKa74zzxYpZxW7jtUDYmZNVkcz4zcnjY7OKqY+ac=; b=IIfusatSVIUqWF2n2QhrflXC64D0nv1gbW90aoJJXoHwtQ7vMXDUjywepDsOKn84zV BjsHwi8li9TIn2Gvw8sufMoYV1tu+YnwOBSR93FcGT0mJ1MSpCrpXw0MzglRmgisRkfq AY6T25UphsW5GG+Q7VFVWLF8uf2TZwyCs3rqI2CvCLFwg1ZszycnJXBCYIRxKnSMES57 1ySYizysdINNrNrkdQFJcDA/vMdzfHL0ez4ACE9PcM8qlray07SZAD9djmOGPI7N4iif YettjSRApJzi2M63m238KE52lQ1ptYW1xXa0TeGntakwJt8DybvHCCaXFRl6lL34h0AU 680Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796031974; x=1796636774; 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=clKnKa74zzxYpZxW7jtUDYmZNVkcz4zcnjY7OKqY+ac=; b=QE1PiLfOxDrDU8oP3Jz73fz4y0lpJoZgRgkBpVTWxoliClxKotshVcJgzzpPhW5JhR /KMCP3CLGo8dkNJX5IgHKhPbV3B/YGS6VSDWbbD2Jb+kWfL+LcbUibmgv1+XHDIJd41E 2sSYROy1eb1KZXtFEsM5pQVkqeQSqOmfrzcy19t4zuN9hcroRC64t6qG0H8ouoUepPLf YKCe9Rg327+oj77BCKxVYvRsUkUQI6PoLhU377zodBrSLzXgpqhkns57m7rQG1g5x0Ws Ba/P9POnziqdfNqe5GKVS/tLzHCQSeoTLZFfPAQNC+tnSRUb/ka9NI67s6HdRNoahLho b9zA== X-Forwarded-Encrypted: i=1; AL+hErEOOpYDS8jaFlwLLfEaMEkCHC+RiJH0oIC2kI12neZtBoFgZjRsN7b+EuOLAPcjmCbiQXrBM2mvXV+BSO0=@vger.kernel.org X-Gm-Message-State: APuU4Z+0IQ0SiGameH7odF1UGtS05OLYQEK1MIESWvWdCva34+uj4IXM DZs2rz7sRReVhiMwzoEbuldAzE3mJ7e6FV3qopws8n9BZfOpwZnsZtexerFq7A== X-Gm-Gg: AZFdTZfeuSPCQaZfhg9REs0MDCHP0qY5pCoexFpqCkfjyWrhmIwxiKC/irVEorMnL2v kUUrKe+5R9XAxEzYggFlIxh+FiNXPDztc+UjYPE8Pd4nhMSgmcVNYEOPn63/9wyof/27JjhaqKM d59wZt7WM2UqSJH1KQMQd8g4wxjQ76aBIxg7ZEymOFd0n3M1WxeWp2XBu2sTw2aneFJGuDQeDGc 54VWpbTPox7RCiYYiMF5qr/ZsLQcXc4qBqOKaIjMEmqsG0NvmXjG/xhBvWJEkHx9jp8G92+9xyD ukUR3BYJvt5icO42kY4MyO6TD92qumoNXUEqnt2E59FIfT82G7CFO15kdbazGzX2HDTbOW0WeU4 BxK+crF/3wqZ9Z+c2xfXFbCkKc2N9U2gQqOGOvIF925bplnCih8nf/BCgNNMFe8+KmmYdEw75zY l5jtReyN8Ix0QOGHDfhYUTpWK53R52zT5FdueATiwlmE+hwqEOVcxc3PBZ9UWRLgDHh9N87Isyt 5FobgDgpzLZh4WoukQZC+4JzsMn4iF2DU4doMfiWaBu9gHaP21Jb2AnvRSALrOs83bzG18+XblJ X/GAYCgXdCLQqwvtxIaBBwf47UhWrTKr5pBythhl5CjzFd62YluwLFL2rWPwv60dF2Sq2nEiiA7 s1W8gWrdfbksq7XSwIsnTK54fit/fE3rFlj66SNWzrkT0VYHEF9az182zfgU= X-Received: by 2002:a05:600c:1d09:b0:4bb:66fd:ad68 with SMTP id 5b1f17b1804b1-4bb888160cdmr708242745e9.14.1796031973752; Mon, 30 Nov 2026 01:46:13 -0800 (PST) Received: from andreayoga.localdomain (93-42-14-189.ip84.fastwebnet.it. [93.42.14.189]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bbab390cf5sm482565375e9.7.2026.11.30.01.46.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 01:46:13 -0800 (PST) From: Andrea Parri To: Thomas Gleixner , Peter Zijlstra Cc: Zhu Jun , linux-kernel@vger.kernel.org Subject: [PATCH] cpu/hotplug: Read st->bringup after the barrier in cpuhp_thread_fun() Date: Mon, 30 Nov 2026 10:46:07 +0100 Message-ID: <20261130094608.7691-1-parri.andrea@gmail.com> X-Mailer: git-send-email 2.53.0 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 When an AP hotplug callback fails, cpuhp_kick_ap() inverts st->bringup, kicks the AP thread again to roll back and waits on the completion for the new direction. cpuhp_thread_fun() takes the direction from st->bringup, but loads it before the smp_mb() that pairs with the one in __cpuhp_kick_ap(). Only a control dependency in smpboot_thread_fn() orders that load after the st->should_run load in cpuhp_should_run(), and that does not order loads. If the AP thread has not gone to sleep since completing the failed step, it can observe the new st->should_run with the old st->bringup: BP AP __cpuhp_kick_ap() cpuhp_thread_fun() wait_for_ap_thread(st, true) callback fails complete_ap_thread(st, true) cpuhp_reset_state() smpboot_thread_fn() r1 = st->bringup // true, early st->bringup = false __cpuhp_kick_ap() smp_mb() st->should_run = true r0 = st->should_run // true cpuhp_thread_fun(), bringup = r1 complete_ap_thread(st, true) wait_for_ap_thread(st, false) With bringup still true, cpuhp_next_state() finds st->state above st->target and returns false, so the AP completes st->done_up and the BP waits on st->done_down forever with the hotplug lock held. A failed teardown, rolled back by the same cpuhp_kick_ap(), hangs the same way in the other direction. The window is small. The AP thread must still be on its way from complete_ap_thread() to the st->should_run check when the BP, woken by that completion, kicks it again; if it went to sleep instead, wake_up_process() orders the loads. x86 does not reorder loads and is not affected. Load st->bringup after the smp_mb(), with the rest of the state the barrier is documented to cover. smp_load_acquire() in cpuhp_should_run() would also work, but the barrier and its pairing comment are already in place. With the st->should_run accesses marked with READ_ONCE() and WRITE_ONCE(), the Linux kernel memory model allows the outcome above and forbids it with this change. Left plain, those accesses are a data race of their own. The hang has not been reproduced on hardware. The bug was found with the help of an LLM, which was also used to write the fix, the changelog and the litmus test. Fixes: 4dddfb5faa61 ("smp/hotplug: Rewrite AP state machine core") Signed-off-by: Andrea Parri --- Zhu Jun's pending patch [1] marks the st->should_run accesses with READ_ONCE() and WRITE_ONCE() but leaves the st->bringup load before the barrier. The two changes are independent. They touch adjacent lines, so whichever is applied second needs a trivial rebase. LKMM litmus test for the interleaving in the changelog, with plain accesses to st->bringup as in the code. The test marks the st->should_run accesses, as [1] does; with plain accesses herd7 also flags the race on st->should_run, which [1] addresses. P0 is the BP side, P1 the AP thread (see the comment in the test). herd7 finds the stale value and flags a data race on st->bringup. Moving the r1 load below the second smp_mb() in P1, as this patch does, removes both, so st->bringup needs no READ_ONCE()/WRITE_ONCE() once it is loaded after the barrier: C cpuhp-bringup-buggy (* * Result: Sometimes DATARACE * * P0 is the BP in cpuhp_reset_state() and __cpuhp_kick_ap() during a * rollback. P1 is the AP thread finding ->should_run set without * sleeping: the first smp_mb() stands for set_current_state() in * smpboot_thread_fn(), the second is the one in cpuhp_thread_fun(), * and r2 is the WARN_ON_ONCE() reload of ->should_run. *) { bringup=1; } P0(int *bringup, int *should_run) { *bringup = 0; smp_mb(); WRITE_ONCE(*should_run, 1); } P1(int *bringup, int *should_run) { int r0; int r1; int r2; smp_mb(); r0 = READ_ONCE(*should_run); if (r0) { r1 = *bringup; r2 = READ_ONCE(*should_run); smp_mb(); } } exists (1:r0=1 /\ 1:r1=1) $ herd7 -conf linux-kernel.cfg cpuhp-bringup-buggy.litmus Flag data-race Observation cpuhp-bringup-buggy Sometimes 1 2 $ herd7 -conf linux-kernel.cfg cpuhp-bringup-fixed.litmus Observation cpuhp-bringup-fixed Never 0 2 Build-tested only: kernel/cpu.o with W=1 for x86_64 and arm64 defconfig. [1] https://lore.kernel.org/r/20261111062853.16835-1-zhujun2@cmss.chinamobile.com kernel/cpu.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/kernel/cpu.c b/kernel/cpu.c index 376d297a6292b..6e51b0045cae2 100644 --- a/kernel/cpu.c +++ b/kernel/cpu.c @@ -1065,8 +1065,8 @@ static int cpuhp_should_run(unsigned int cpu) static void cpuhp_thread_fun(unsigned int cpu) { struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state); - bool bringup = st->bringup; enum cpuhp_state state; + bool bringup; if (WARN_ON_ONCE(!st->should_run)) return; @@ -1076,6 +1076,7 @@ static void cpuhp_thread_fun(unsigned int cpu) * that if we see ->should_run we also see the rest of the state. */ smp_mb(); + bringup = st->bringup; /* * The BP holds the hotplug lock, but we're now running on the AP, base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244 -- 2.53.0