Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.42 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796147755; b=aM38aCdIAWmZSuNOLSYtYfI9JylraprwNi3fsqosFwubbTP910ZlIB3QiPMHABfowZbm EfYmJi6t/fIAFNgauIzNyJbj8zP79fBTZSowfUn7cG4veTxXm4KOidNGt+hqnWJ8iFDPS mcoHdlkeRZ2YtbEUYgN2pARaouiSSPGeXceuPu8WeKAvecCld81QxZfbeWZFbhf+WhSUo unM5e63HZi7dXp/bsL4FAFi030ISq/xqSuZvLEnSbQJ4b4/bSekc2QfHb5lI+Cfc5griG HQ1k0x3SmTbNyYF5agXtBrFSqTSd+5gGloIJzkK/hhXBMNnLtN3zgxpJaJIrxE35KtA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796147755; 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=llHztJ8QShBbpdHhXOblHBFCJA4QEjsRCFcmWay0uyk=; b=WK2EOZHxRNmrIB0UyZplTmPgErk8mpoRZbERuJdavaLfX1PnpTJQ8kbYShR/mP34J1zq UECZmKYxpZ7ccM+Se9R/qTnM6pb9HG7OAZwp1ijsJ7CTe1mdwM6Ng/BRzKc+m3XzUl0p0 sOT8EV3kRRflbR9RzDAFqb4lDdlQOsjd3OPZZrN+jGr9wHlWvcV86dckhcrInAJat3pzB d04HSNx2/YUlSvYEdMtZVoyKAID5cRFtx5OLnuvMswpQ0QONAsiPfxvyY9duMzLDztSU4 8/5O5x1inhvrfQF3LEXyxE6zSISA3F+SG7fKjIGW5de/IMLGnJSe08IEcIZztDXt90A== 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=u6Xlu5BN; arc=none smtp.remote-ip=209.85.128.42 Received: from mail-wm1-f42.google.com (mail-wm1-f42.google.com [209.85.128.42]) (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 7BF12606A320 for ; Tue, 1 Dec 2026 17:55:54 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 7BF12606A320 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=u6Xlu5BN Received: by mail-wm1-f42.google.com with SMTP id 5b1f17b1804b1-4a214c5c4c0so116575265e9.3 for ; Tue, 01 Dec 2026 09:55:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796147753; x=1796752553; 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=llHztJ8QShBbpdHhXOblHBFCJA4QEjsRCFcmWay0uyk=; b=u6Xlu5BNx7XUqWsug/vlCxKgDriVarMcLPWFK/rpNEtlL7QdhU/pnuPlhT1Q1+L18F lBZC9YqrXpufn60iHkw4BExmuN90KYBPGWsEGXqo6MFwHCwFd0E2MkNjv34UJOfVVV+4 AGVuowJbqUmynQ2qReDaOHFq1wpz9Q692OuW+wyfXaRKaT9xNmwHw5XE+Z/xUsfgGbJz 69/jUL2KDIcBsdGG9jOi41MegfR0tpK7dk+tV2Csr21aBfiy/pwlUruxnNgQdXa/KID0 dSFulVgAmHrfEV2OB8wSWATWc7uiIqOpni1yKCRenDH6vPmHQXPhRS02f7rCgJf+Hdsz YlSQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796147753; x=1796752553; 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=llHztJ8QShBbpdHhXOblHBFCJA4QEjsRCFcmWay0uyk=; b=zNM3JiX9o9pU6YMQRKr7xNPt0qXoZadlPYFff10VTIZ7QOghHkwsk+T4sgtuhwSU+H UAuEaOeKtV6+aTZYo0UGeNbH0bFfsdVwErldw7gnjqBUsScZr4wa0HDHw+0CJdfxKQzl 0MNnG2HeS8zpytxjfeXOs7JMDrGHdE4kyYrKsw3rMuDehlS1B3Rcw+u02Muqql8NbDAg +63tmaXYSuB56Y5diypeEd3p7dAV3hrvcXOCP/h9zKoeAc8IYAZ5s/td9Hv4wt0GyOAk fwSEXBI38QH9Mzdiwy6gF2lQkn8SQW6lwwahC4sp3Gt7goo8O/iVhMBvY8eE2Qy1tMgS 5Mjw== X-Forwarded-Encrypted: i=1; AL+hErEZTpVgHr/vhv53JFYQosqhazs1YIJMd+U145OnZiOKoU28JNiszb9AUqCi1LRtRpOxwnU=@vger.kernel.org X-Gm-Message-State: APuU4Z/Lz2yy4NJl0xjGUXDQLEwtG6VaOKkgm4MQ1BF+ErsIxjMkO6eE 5tj9zHs6Ji6XSD6mo6aCqDSNlWdXQ4gi/WTc8HHSP0TKxAERnQXPgsPbXsKH8MQ1 X-Gm-Gg: AZFdTZez/gBTMoADhAts22ZIw78XIPqdhDCtK8LsBkRLloc+K0Orhr0DLBcib4EBKOO 3QUkg7gQTcupyUxjP07PqcgSKHAvYvpM0i+rm+BIjK8dtIwZx9Awy9/sR0XUzG9ordGxIEhyr1Z NP3mUY1RcMWVRw9f0W0dA+jNW2BD7Yw9R1WhaM+unzx1oBgR0bvJpj/sRBJ43M0IAB9bhGJFNQe 0sPMhh7hHAkiEb5U1xElsYgQaSzzLKlCvSG/S0lAKQcMEkmX3p4pEViXP1A6jszlqpqLUSwEio2 rwrV/zG/78pP2OSsmdqCj9UlYdxsFeC4R5TMHbF2zA52YxYyCuI//Ao2V/gEI5W91/0HxNc6Wuq XrGuRMMrRhfQVgskpgnwGOKLI6OkxVzYxsZuc+TkJaiCbcEc8RGFTXZ1zkRS7D2CEVzgM/BcRRt Yss43dLMnEuQR9+lr5+C1yBGJPqux9UP2TPRHQw06XH0Z5R+pBttjk5mPVmqHn9JSTgrAzfRX+b chJzBeqTrH+p9h+4tMmq611lT2p7j2HabvCrVg9zGBKp6Hw8TKKerKJq+ZAkfqTYBMjMKa0662J EANemcuGcNnx7uSKF+CQVLmB0rJFVrn1Q8XNBSB8h5SM87AM99ANXYzWO+6tS6vXe8sBM1y/fHz x X-Received: by 2002:a05:600c:a15:b0:4b6:267a:dd6e with SMTP id 5b1f17b1804b1-4bb88938a62mr763716265e9.28.1796147752712; Tue, 01 Dec 2026 09:55:52 -0800 (PST) Received: from andreayoga.wind3.hub ([31.189.39.21]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb884865b1sm1050352445e9.5.2026.12.01.09.55.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 09:55:52 -0800 (PST) From: Andrea Parri To: Lai Jiangshan , "Paul E. McKenney" , Josh Triplett , =?UTF-8?q?Onur=20=C3=96zkan?= Cc: Steven Rostedt , Mathieu Desnoyers , Puranjay Mohan , rcu@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] srcu: Make srcu_barrier() wait for nested deferred callbacks Date: Tue, 1 Dec 2026 18:55:31 +0100 Message-ID: <20261201175532.72556-1-parri.andrea@gmail.com> X-Mailer: git-send-email 2.53.0 X-Mailing-List: bpf@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 With interrupts disabled, __call_srcu() defers a callback in two steps: it adds the callback to the srcu_data's ->defer_cbs and, if that list was empty, chains the srcu_data onto the per-CPU srcu_defer list. srcu_defer_flush() walks only the per-CPU list, so it misses callbacks whose srcu_data is not chained yet. Commit 35a26d87042b ("srcu: Make call_srcu() safe to call from any context") accepts this because only a callback whose call_srcu() has not returned can be missed. A call_srcu() nested between the steps, from an NMI or a kprobe, returns unchained, so srcu_barrier() can return before its callback runs: CPU 0 CPU 1 local_irq_disable() call_srcu(ssp, rhp1, f1) llist_add(rhp1, &sdp->defer_cbs) // true, ->defer_cbs was empty call_srcu(ssp, rhp2, f2) llist_add(rhp2, &sdp->defer_cbs) // false, so return unchained tell CPU 1 that rhp2 is queued srcu_barrier(ssp) srcu_defer_flush() // CPU 0's list is empty // returns, rhp2 not invoked free what f2() uses llist_add(&sdp->defer_link, ...) ... f2(rhp2) // use-after-free A test module reproduces this on x86 with a kprobe between the two llist_add() calls, whose int3 handler calls call_srcu() and spins while another CPU calls srcu_barrier(). That returns after 58us, the nested callback runs 3.3ms later, and freeing an object after srcu_barrier() gives: BUG: KASAN: slab-use-after-free in func2+0x6d/0x70 [srcu_defer_nmi] Read of size 8 at addr ffff88800247ebc0 by task kworker/0:1/13 Workqueue: rcu_gp srcu_invoke_callbacks Call Trace: func2+0x6d/0x70 [srcu_defer_nmi] srcu_invoke_callbacks+0x360/0x5c0 process_one_work+0x8d5/0x1b50 worker_thread+0x52d/0xec0 kthread+0x312/0x410 Allocated by task 221: srcu_defer_nmi_init+0xba/0xff0 [srcu_defer_nmi] Freed by task 222: kfree+0x20b/0x520 barrier_fn+0x94/0xa0 [srcu_defer_nmi] No in-tree caller is known to hit this. The window spans only the ->ssp store and the second llist_add(), and the barrier callers checked wait for the interrupted irqs-disabled region to end before their barrier. kernel/bpf/memalloc.c and kernel/bpf/bpf_local_storage.c call synchronize_rcu() before rcu_barrier_tasks_trace(). rcutorture removes its NMI perf events from each CPU's hotplug thread before its barrier. The nested call_srcu() itself is reachable from in-tree code: BPF programs run from perf NMIs and kprobes can call bpf_call_rcu_tasks_trace(), which ends in call_srcu(), and srcu_barrier() is documented to wait for all in-flight call_srcu() callbacks. Fix this by having srcu_barrier() wait for an IPI to each CPU whose ->defer_cbs for this srcu_struct is still non-empty after srcu_defer_flush(). The interrupted staging runs with interrupts disabled, so the IPI completes only after the srcu_data is chained, and a second drain re-issues the callback. This does not depend on how the call_srcu() was nested. The IPI is sent only when the flush leaves callbacks of this srcu_struct staged on a CPU, that is, after a split staging or a call_srcu() racing with srcu_barrier(). srcu_barrier() checks ->defer_cbs under srcu_defer->lock. A drainer takes callbacks off that list and re-issues them under the same lock, so an empty list seen under that lock cannot hide a callback that is not yet enqueued. A CPU going offline leaves cpu_online_mask in take_cpu_down() but runs until it reports itself dead from the idle loop, and smp_call_function_single() returns -ENXIO for it without waiting. For such a CPU, srcu_barrier() drains and sleeps until its ->defer_cbs is empty. That ends: a dead CPU cannot be part-way through staging. rcu_barrier() uses the same pattern: a waiting smp_call_function_single() to each CPU, and a sleep and retry when that fails for a CPU going offline. Draining ->defer_cbs directly from srcu_barrier() would leave the srcu_data chained with an empty list, and the next call_srcu() would chain it a second time. A separate staging lane for NMIs would miss kprobes on arm64, powerpc and s390, where in_nmi() is false in the breakpoint handler. synchronize_rcu() in srcu_barrier() would also work, but adds an RCU grace period to every barrier. The bug was found with the help of an LLM, which was also used to write the fix, the changelog and the test. Fixes: 35a26d87042b ("srcu: Make call_srcu() safe to call from any context") Signed-off-by: Andrea Parri --- Based on v7.4-rc5. It also applies cleanly to paulmck/dev. Tested in a virtme-ng guest (x86_64, 4 vCPUs, srcutree.convert_to_big=0) on a kernel with KASAN, PROVE_LOCKING (with PROVE_RAW_LOCK_NESTING), DEBUG_ATOMIC_SLEEP, PROVE_RCU and CSD_LOCK_WAIT_DEBUG, using the module below. It needs kprobe optimization disabled (sysctl debug.kprobes-optimization=0) and probe_off set to the first instruction of the branch taken when the ->defer_cbs llist_add() returns true, read from objdump of __call_srcu (__call_srcu+0x264 here, with and without this patch). unpatched: srcu_defer_nmi: probe fired=1 cpu=1 in_nmi=1 irqs_disabled=1 spun=1ms srcu_defer_nmi: nested call_srcu() returned, then srcu_barrier() returned after 58us srcu_defer_nmi: FAIL: srcu_barrier() returned before func2; func2 ran 3285us later uaf=1: BUG: KASAN: slab-use-after-free in func2+0x6d/0x70 [srcu_defer_nmi] patched: srcu_defer_nmi: probe fired=1 cpu=1 in_nmi=1 irqs_disabled=1 spun=2000ms srcu_defer_nmi: nested call_srcu() returned, then srcu_barrier() returned after 2003049us srcu_defer_nmi: PASS: func2 ran before srcu_barrier() returned uaf=1: PASS, func2 reads valid memory, no KASAN report With this patch, srcu_barrier() waits for the kprobe handler to return, and the handler waits for srcu_barrier(), so the handler spins until its 2s timeout. No lockdep, sleeping-in-atomic, RCU or csd-lock warnings were reported. A second module, which I can send if useful, covers the fallback for a CPU going offline. It takes a CPU down, splits a staging on that CPU from cpuhp_report_idle_dead(), after the CPU has left cpu_online_mask, and calls srcu_barrier() from another CPU. With this patch, srcu_barrier() slept and retried 25-26 times after failed IPIs and returned only after the nested callback ran, in 3 of 3 runs, again with no warnings. Without it, srcu_barrier() returned after 8-301us and the callback ran about 53-55ms later. srcu_barrier() latency on a private srcu_struct, 16 online vCPUs, no debug options, median p50 over 10 rounds from two boots of each kernel: idle (no callback, no grace period), 16 possible CPUs: 0.56us -> 0.90us idle (no callback, no grace period), 255 possible CPUs: 21.25us -> 31.89us after call_srcu(), irqs enabled or deferred: 3.95-3.97ms, unchanged The extra scan is most visible in the idle benchmark: srcu_barrier() then posts no callbacks and waits for no grace period, so the numbers isolate the cost of one lock and unlock per possible CPU. With a callback pending, the median was essentially unchanged. These runs do not cover the IPI wait or the retry for a CPU going offline, which add latency only when callbacks are still staged after the flush. I can send the benchmark module if useful. rcutorture on v7.4-rc5 plus this patch, 15 minutes per scenario, all ending in SUCCESS with no warnings, stalls or barrier failures: SRCU-N ------- 30054 GPs (33.3933/s) [srcu: g297728 f0x0 total-gps=74533] n_max_cbs: 150000 SRCU-P ------- 3738 GPs (4.15333/s) [srcud: g54596 f0x0 total-gps=13750] n_max_cbs: 150000 TRACE01 ------- 22133 GPs (24.5922/s) [tasks-tracing: g0 f0x0 total-gps=0] n_max_cbs: 50000 TRACE02 ------- 39035 GPs (43.3722/s) [tasks-tracing: g0 f0x0 total-gps=0] n_max_cbs: 100000 The guest has no PMU, so rcutorture's NMI ->call() exerciser does not run there (nmi-calls: 0) and nested staging is exercised only by the two modules. // SPDX-License-Identifier: GPL-2.0 /* * Reproducer for the two-step deferred call_srcu() staging race. * * A stager kthread on @stage_cpu calls call_srcu(rhp1) with IRQs disabled, * which takes the deferred path. A kprobe placed inside the window between * llist_add(rhp, &sdp->defer_cbs) and llist_add(&sdp->defer_link, &sndp->list) * fires there. On x86 a kernel int3 enters via irqentry_nmi_enter(), so the * handler runs with in_nmi() and irqs_disabled() true, standing in for an NMI. * The handler calls call_srcu(rhp2), which returns without chaining the * srcu_data, signals a barrier kthread on @barrier_cpu, and spins until that * kthread's srcu_barrier() has returned (or a timeout). * * srcu_barrier() is obliged to wait for rhp2, since call_srcu(rhp2) returned * before srcu_barrier() was called. The module reports whether it did. With * the fix, srcu_barrier() also waits for the handler to return, so the handler * spins until the timeout. * * With uaf=1 the barrier kthread frees an object after srcu_barrier() * returns, the way a teardown path would, and func2 dereferences it. * * Parameters probe_sym/probe_off locate the window; see the disassembly. */ #include #include #include #include #include #include #include #include static char *probe_sym = "__call_srcu"; module_param(probe_sym, charp, 0444); static unsigned int probe_off; module_param(probe_off, uint, 0444); static int stage_cpu = 1; module_param(stage_cpu, int, 0444); static int barrier_cpu = 2; module_param(barrier_cpu, int, 0444); static bool uaf; module_param(uaf, bool, 0444); static unsigned int spin_ms = 2000; module_param(spin_ms, uint, 0444); struct victim { unsigned long magic; }; static struct srcu_struct test_srcu; static struct rcu_head rhp1, rhp2; static struct victim *victim; static int armed; static int nmi_done; static int barrier_done; static int func1_ran, func2_ran; static bool h_in_nmi, h_irqs_off, h_fired; static int h_cpu; static u64 t_nmi_call_ret, t_barrier_ret, t_func2; static bool func2_ran_at_barrier_ret; static unsigned int spun_ms; static DECLARE_COMPLETION(func2_done); static DECLARE_COMPLETION(barrier_thread_done); static void func1(struct rcu_head *rhp) { WRITE_ONCE(func1_ran, 1); } static void func2(struct rcu_head *rhp) { WRITE_ONCE(t_func2, ktime_get_mono_fast_ns()); if (uaf) pr_info("srcu_defer_nmi: func2: victim->magic=%#lx\n", victim->magic); smp_store_release(&func2_ran, 1); complete(&func2_done); } static int kp_pre(struct kprobe *p, struct pt_regs *regs) { struct srcu_data *sdp; unsigned int i; if (!READ_ONCE(armed) || smp_processor_id() != stage_cpu) return 0; /* Only fire for our own outer call_srcu(rhp1), mid-staging. */ sdp = this_cpu_ptr(test_srcu.sda); if (READ_ONCE(sdp->defer_cbs.first) != (struct llist_node *)&rhp1) return 0; if (cmpxchg(&armed, 1, 0) != 1) return 0; h_fired = true; h_cpu = smp_processor_id(); h_in_nmi = in_nmi(); h_irqs_off = irqs_disabled(); call_srcu(&test_srcu, &rhp2, func2); /* the "NMI" call_srcu() */ t_nmi_call_ret = ktime_get_mono_fast_ns(); smp_store_release(&nmi_done, 1); /* "call_srcu() returned" */ /* Hold the outer call_srcu() inside its window. */ for (i = 0; i < spin_ms && !smp_load_acquire(&barrier_done); i++) mdelay(1); spun_ms = i; return 0; } static struct kprobe kp = { .pre_handler = kp_pre, }; static int barrier_fn(void *unused) { while (!smp_load_acquire(&nmi_done)) { if (kthread_should_stop()) goto out; cpu_relax(); } srcu_barrier(&test_srcu); t_barrier_ret = ktime_get_mono_fast_ns(); func2_ran_at_barrier_ret = smp_load_acquire(&func2_ran); if (uaf && !func2_ran_at_barrier_ret) { /* "Teardown": srcu_barrier() said all callbacks are done. */ kfree(victim); } smp_store_release(&barrier_done, 1); out: complete(&barrier_thread_done); while (!kthread_should_stop()) schedule_timeout_interruptible(HZ / 10); return 0; } static int stage_fn(void *unused) { WRITE_ONCE(armed, 1); local_irq_disable(); call_srcu(&test_srcu, &rhp1, func1); local_irq_enable(); WRITE_ONCE(armed, 0); while (!kthread_should_stop()) schedule_timeout_interruptible(HZ / 10); return 0; } static int __init srcu_defer_nmi_init(void) { struct task_struct *bt, *st; int ret; if (stage_cpu == barrier_cpu || !cpu_online(stage_cpu) || !cpu_online(barrier_cpu)) { pr_err("srcu_defer_nmi: need distinct online stage/barrier CPUs\n"); return -EINVAL; } if (stage_cpu == 0) /* __boot_cpu_id is not exported; assume CPU 0 */ pr_warn("srcu_defer_nmi: stage_cpu is the boot CPU; small-mode srcu_barrier() may block on it\n"); victim = kmalloc(sizeof(*victim), GFP_KERNEL); if (!victim) return -ENOMEM; victim->magic = 0x5ac1f1ceUL; ret = init_srcu_struct(&test_srcu); if (ret) goto out_free; kp.symbol_name = probe_sym; kp.offset = probe_off; ret = register_kprobe(&kp); if (ret) { pr_err("srcu_defer_nmi: register_kprobe(%s+%#x) = %d\n", probe_sym, probe_off, ret); goto out_srcu; } pr_info("srcu_defer_nmi: kprobe at %s+%#x (%pS), optimized=%d\n", probe_sym, probe_off, kp.addr, kprobe_optimized(&kp)); bt = kthread_create_on_cpu(barrier_fn, NULL, barrier_cpu, "srcu_dn_barrier"); if (IS_ERR(bt)) { ret = PTR_ERR(bt); goto out_kp; } st = kthread_create_on_cpu(stage_fn, NULL, stage_cpu, "srcu_dn_stage"); if (IS_ERR(st)) { ret = PTR_ERR(st); kthread_stop(bt); goto out_kp; } wake_up_process(bt); wake_up_process(st); if (!wait_for_completion_timeout(&barrier_thread_done, 10 * HZ)) pr_err("srcu_defer_nmi: barrier thread did not finish\n"); if (!wait_for_completion_timeout(&func2_done, 10 * HZ)) pr_err("srcu_defer_nmi: func2 not invoked within 10s\n"); kthread_stop(st); kthread_stop(bt); unregister_kprobe(&kp); pr_info("srcu_defer_nmi: probe fired=%d cpu=%d in_nmi=%d irqs_disabled=%d spun=%ums\n", h_fired, h_cpu, h_in_nmi, h_irqs_off, spun_ms); if (!h_fired) { pr_err("srcu_defer_nmi: probe never fired in the window; check probe_off\n"); } else if (!READ_ONCE(barrier_done)) { pr_info("srcu_defer_nmi: srcu_barrier() did not return\n"); } else { pr_info("srcu_defer_nmi: nested call_srcu() returned, then srcu_barrier() returned after %lluus\n", (t_barrier_ret - t_nmi_call_ret) / NSEC_PER_USEC); /* * func2_ran is sampled after t_barrier_ret, so check func2's own * timestamp too: a func2 that ran in between is not a PASS. */ if (func2_ran_at_barrier_ret && READ_ONCE(t_func2) <= t_barrier_ret) { pr_info("srcu_defer_nmi: PASS: func2 ran before srcu_barrier() returned\n"); } else if (smp_load_acquire(&func2_ran)) { pr_info("srcu_defer_nmi: FAIL: srcu_barrier() returned before func2; func2 ran %lluus later\n", (READ_ONCE(t_func2) - t_barrier_ret) / NSEC_PER_USEC); } else { pr_info("srcu_defer_nmi: FAIL: srcu_barrier() returned before func2; func2 never ran\n"); } } pr_info("srcu_defer_nmi: func1_ran=%d func2_ran=%d\n", READ_ONCE(func1_ran), READ_ONCE(func2_ran)); srcu_barrier(&test_srcu); cleanup_srcu_struct(&test_srcu); if (!(uaf && barrier_done && !func2_ran_at_barrier_ret)) kfree(victim); /* Report via dmesg only; nothing to keep loaded. */ return -EAGAIN; out_kp: unregister_kprobe(&kp); out_srcu: cleanup_srcu_struct(&test_srcu); out_free: kfree(victim); return ret; } module_init(srcu_defer_nmi_init); MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("Reproducer: NMI-nested deferred call_srcu() vs srcu_barrier()"); kernel/rcu/srcutree.c | 58 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/kernel/rcu/srcutree.c b/kernel/rcu/srcutree.c index 2b0e2b2d2c187..157a4d94f2589 100644 --- a/kernel/rcu/srcutree.c +++ b/kernel/rcu/srcutree.c @@ -112,6 +112,63 @@ static void srcu_defer_flush(void) __srcu_defer_drain(&per_cpu(srcu_defer, cpu)); } +static void srcu_defer_ipi(void *unused) +{ +} + +/* + * Does @sdp have deferred callbacks? A drainer takes them off ->defer_cbs and + * re-issues them under ->lock, so an empty list seen under ->lock means none + * is left mid-drain. + */ +static bool srcu_defer_pending(struct srcu_defer *sndp, struct srcu_data *sdp) +{ + unsigned long flags; + bool ret; + + raw_spin_lock_irqsave(&sndp->lock, flags); + ret = !llist_empty(&sdp->defer_cbs); + raw_spin_unlock_irqrestore(&sndp->lock, flags); + return ret; +} + +/* + * Wait for @ssp's deferred callbacks whose staging was still in progress when + * srcu_defer_flush() ran. __call_srcu() stages with interrupts disabled, in + * two steps: it adds the callback to ->defer_cbs, then chains the srcu_data + * onto the per-CPU list. A call_srcu() nested between the steps, from an NMI + * or a kprobe, returns before the srcu_data is chained, so the flush misses its + * callback and leaves ->defer_cbs non-empty. An IPI to that CPU returns only + * after the interrupted staging has finished, and a second drain then picks + * the callback up. + * + * A CPU going offline leaves cpu_online_mask in take_cpu_down() but runs until + * it reports itself dead from the idle loop, and the IPI fails without waiting + * for it. Drain and sleep until its ->defer_cbs is empty instead. That ends: + * a dead CPU cannot be part-way through staging, so all it staged is chained. + */ +static void srcu_defer_wait_staging(struct srcu_struct *ssp) +{ + int cpu; + + if (!IS_ENABLED(CONFIG_RCU_DEFER)) + return; + + for_each_possible_cpu(cpu) { + struct srcu_data *sdp = per_cpu_ptr(ssp->sda, cpu); + struct srcu_defer *sndp = &per_cpu(srcu_defer, cpu); + + while (srcu_defer_pending(sndp, sdp)) { + int ret = smp_call_function_single(cpu, srcu_defer_ipi, NULL, 1); + + __srcu_defer_drain(sndp); + if (!ret) + break; + schedule_timeout_uninterruptible(1); + } + } +} + /* * Initialize SRCU per-CPU data. Note that statically allocated * srcu_struct structures might already have srcu_read_lock() and @@ -1907,6 +1964,7 @@ void srcu_barrier(struct srcu_struct *ssp) * other srcu_structs'. */ srcu_defer_flush(); + srcu_defer_wait_staging(ssp); s = rcu_seq_snap(&ssp->srcu_sup->srcu_barrier_seq); mutex_lock(&ssp->srcu_sup->srcu_barrier_mutex); base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244 -- 2.53.0