From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.47 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796398510; b=ZguirESgXquaYm28BE1T3zF4waM8xSQRU2S5Wmxl42UuafrsL4VejDLQjMW8W7fl/0Un 4El6ERngPTdhsFqHqCn9gbyUosdlljoLQo+EsRt+fDKvR0BznX8Z4TQ3zBsA8AuXooLRw xpztzsBfXw3uC7k3JenmW6auiN1oH9wybrB5JajD5KwzLocFGHdKVXyFGZ9mj5il/ez3i mO5Y1BjZ5izfXcFR9conlmaapFLoSQEbPxZ3Ex0fI9LesvWm/N5dUu8E93ZWqEfJ+uel0 EY2pPam8CyqFrLm6EYISwntYl/X2JCVDCAnDIsimVFmVE6ptANXcUgwDZ2DXFBNkD+g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796398510; 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:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=nLRCNDyvggvpMMP49+aeXNdknSl8pVXVbb+3hjrLpw8=; b=wTWo+fcZSPR6uc4iKlAT5Sp2Z/3K6hLxnt7u7QnB4pAaOPrrIdDUEE941nl33dMuOlY/ ZR4Nx4ZAhC3dtBjb/9xNXiHTpaucCZ8eu8ZobtnA5FepYz63O6HpyG3c/SO+Y5Mh5Z70i V+SOsc21yKFNxQG8V5dnM3ZIyEGOVDq3FOigJVVcKl7qL/9l2XATwgH/lD9GEhHCjNcBq n6tSwsmGo1FW5X8a4brv2w8neABS22I2tU8NwYEw6EVbGn12uOC2uNudw9Yb4s7/R5Et1 PP/96kV2FNT9C1y2g/kwBSU1e8q5RJMOTIUPf9LMBND5CNt+D8P3tnOvPVd8c497qPQ== 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=hTcPpEjY; arc=none smtp.remote-ip=209.85.128.47 Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) (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 8ADC0606A335 for ; Fri, 4 Dec 2026 15:35:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8ADC0606A335 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=hTcPpEjY Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-4a9a8d6cea8so11845045e9.1 for ; Fri, 04 Dec 2026 07:35:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796398508; x=1797003308; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=nLRCNDyvggvpMMP49+aeXNdknSl8pVXVbb+3hjrLpw8=; b=hTcPpEjYuOoYaSBtgROMszGrKQmsFOtt/ArnEExEdWSN9IgsJF4po3/Sp26g2/csXi RbUkuGq9ofbMZpxApHODu4Bp+Zd2oeZ8t/2IwJDFFkU/ldd4K5GM9S6eFLPXuEYMviQS Zs9POrpiQ2zTCat4rhE25di5U/Mq/2rWqRGxtba60EPAbCvXFD5mz2C8W8i7nClLyAlJ MV1P6Ezxdc8V7M/8QXbcPJvydMVqEC0cqm1ljTBnm3G3xcig6OMh444YRoVsMQQXbIQO ve6/I1FMSrVb4AOWVgW4V2WPu8brKvhD3tZCwTtIDWM4h2BZZnA9yVy2DhWT8RNZxQb9 8tBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796398508; x=1797003308; h=content-transfer-encoding:mime-version:references:in-reply-to :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=nLRCNDyvggvpMMP49+aeXNdknSl8pVXVbb+3hjrLpw8=; b=0uNta0Ob/4GUb3OHtNBb1vBV9XzWOh8og79V2wzp7vwVOWCoTYzu5OY1g3tF6Ty2iv xjxA+97xxoWpW1O7FlWI6qZgWFONWrP0OI4kqDJJJr/uYYUsP2yLcm/8FNmFa5jFN1Yj rtXHih6C9rnQ5IaEwffgcJEi0SwXHl0lgXcLTqSjDwjFPsz2JXMMvN+USyK2TxAMMMnk sft26BjZUrYA3WqDMXk/r8659lDdVr6x25S0eQky80VtA2KHyHifJ4ul3ZNe3+gYi/Mz kDrUOhqcK0WqGx28nn94As/gGjwB2Pfk0DzJ3qLs6tAxLxy9xpYWxmt3+PZ03MKJgJEV GR+A== X-Forwarded-Encrypted: i=1; AL+hErGZLa5fSfZBb2MbBCetMgX5BBbGeoKSbq2+GYmv8RA5iK3ZPHBko1+D3XuYxWe5Pg9Avkz3coltSPPZz1WoSXiU3JQ=@vger.kernel.org X-Gm-Message-State: APuU4Z95c45n9Uc0urqneDaivGnainEpBYRGexb4OMDG7a4FhPZXmYdZ 3dqa5Sio36hodoLNpdDLSeeLZfVsPjGKhY/bmOJysu1CRhG0ZQD2DPsI X-Gm-Gg: AZFdTZfcmoQWrRO07rCvFn5GW1lqG/Hx2rYKaONxggEm78/+ddlazsSqBBbP99mGk7P u2LvTwzodQjGFVBgPxB9wyQEd0AI4u6ObzZHrCtSfig4s/MiTwa0GPdIvrDNlwS7Kh8t8An8nd3 TOrYKhfNzrWYH+c1DIZVNJA6xXZZ0zIw2tmpTXpVoJOKm+aVOTNxg/o9fcNbXfdVBWFMiR6icqK FwcC4bwE5H5w11IBfn/VGjOnlMPB5gEc27E+A4SONd9u7USHqIFxreypqgwZjRdZUtsN4xsvWTj 6/eovBXYp795/hcSXWvY3xHwEU5MAg66kh2M16ZprIKAd/0bm/FqIrZvA8vJuUdSFHNKsVpkqDL ceBU0KJ45Eabpwe7MwvA39wEhtjxrxIs8ht87reZBfBAgnkVw71z3WwJqoUQCUAh78zG8VeaL+C oiYa2kH2/xen1SFmSWVbaOB/Vx8GDrY4Irq5u9v/xkm3r/qP6iSY90oYCg/Vb5ZimuCDw1W9iJ1 YC08n8oq+XlfpgDN3KZB1djlxe+G5U3K1BsWLAqwjg+4hHh2491xDsqiOkoyAjmLZ5BxVvwjQ7V m2uYCVfRA2Z9bansYxGA60OZEWdSfquVjOQDmCxYI0MeJmnPS7SK9NWMuUP9bMtDK358Uzu+56y Y X-Received: by 2002:a05:600d:864d:10b0:4bb:aa6b:b7fc with SMTP id 5b1f17b1804b1-4bbe1a06a00mr92637725e9.15.1796398507623; Fri, 04 Dec 2026 07:35:07 -0800 (PST) Received: from andreayoga.wind3.hub ([31.189.39.21]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4d173ab8d22sm5874897f8f.39.2026.12.04.07.35.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 07:35:07 -0800 (PST) From: Andrea Parri To: Steven Rostedt , Masami Hiramatsu Cc: Mathieu Desnoyers , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, bpf@vger.kernel.org Subject: [PATCH 1/2] tracepoint: Always use the iterator for faultable tracepoints Date: Fri, 4 Dec 2026 16:34:50 +0100 Message-ID: <20261204153452.17096-2-parri.andrea@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261204153452.17096-1-parri.andrea@gmail.com> References: <20261204153452.17096-1-parri.andrea@gmail.com> X-Mailing-List: linux-trace-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 Closing a perf event on a system call tracepoint can crash a task that is entering a system call at the same time. With perf and ftrace both tracing system call entry, the task calls ftrace_syscall_enter() with the perf probe's NULL data instead of its trace_array: BUG: kernel NULL pointer dereference, address: 0000000000000188 #PF: supervisor read access in kernel mode #PF: error_code(0x0000) - not-present page Oops: Oops: 0000 [#1] SMP NOPTI CPU: 1 UID: 0 PID: 106 Comm: tpvictim Not tainted 7.4.0-rc5-dirty #4 PREEMPT(full) RIP: 0010:ftrace_syscall_enter+0x88/0x310 Call Trace: ? vprintk_emit+0x20c/0x2f0 trace_syscall_enter+0x73/0xc0 do_syscall_64+0x446/0x4f0 entry_SYSCALL_64_after_hwframe+0x77/0x7f When a tracepoint ends up with a single probe (2 -> 1, or 1 -> 0 -> 1), its static call is switched from __traceiter_*() to that probe. Readers load tp->funcs[0].data before the static call, so tp_rcu_cond_sync() first makes sure that an RCU and a tracepoint_srcu grace period have elapsed since the old data was replaced. Since commit a363d27cdbc2 ("tracing: Allow system call tracepoints to handle page faults"), system call tracepoints protect their probes with RCU Tasks Trace only, and neither grace period waits for those readers. A task preempted between the data load and the static call then resumes into the new probe with the old data: CPU 0 CPU 1 __do_trace_sys_enter() guard(rcu_tasks_trace)(); __data = funcs[0].data; // NULL perf_trace_event_unreg() tracepoint_remove_func() tp_rcu_cond_sync(); tracepoint_update_call(); static_call(tp_func_sys_enter)(__data, ...); ftrace_syscall_enter(NULL, ...); Commit 2e8a12b82c40 ("tracing: Fix syscall tracepoint use-after-free") made release_probes() defer freeing old probe arrays until after an RCU Tasks Trace grace period, but left the waits before a static call switch unchanged. Keep the static call of faultable tracepoints on __traceiter_*(), which ignores the data it is passed and loads each probe together with its data from the same tp->funcs entry. Waiting for an RCU Tasks Trace grace period in tp_rcu_cond_sync() would also close the race, but under tracepoints_mutex. System call probes can sleep in their read-side sections while faulting in user memory, and every tracepoint registration would then wait for the fault. The cost is an indirect call per system call while the tracepoint has a single probe, as on kernels without CONFIG_HAVE_STATIC_CALL, which always use __traceiter_*(). The splat above is from x86-64 with CONFIG_PREEMPT=y and a test-only mdelay(), with preemption enabled, between the data load and the static call. A 1 -> 0 -> 1 sequence (perf, then a BPF raw tracepoint) crashes the same way in bpf_trace_run2(). With this patch, neither crashes. The bug was found with the help of an LLM, which was also used to write the fix, the changelog and the tests. Fixes: a363d27cdbc2 ("tracing: Allow system call tracepoints to handle page faults") Signed-off-by: Andrea Parri --- Reproduced on v7.4-rc5 in x86-64 KVM guests (CONFIG_PREEMPT=y) with the test-only hook below, which busy-waits once for 2 s, with preemption enabled, between the data load and the static call of one task. Without such a delay the window is a few instructions, and I did not try to hit it by stress alone. Each sequence oopsed in 3 of 3 runs without this patch and in 0 of 3 runs with it. With this patch, the BPF attach in the 1 -> 0 -> 1 case returns while the delayed task still holds the old data, and that task resumes without crashing. An earlier version of this patch waited for an RCU Tasks Trace grace period in tp_rcu_cond_sync() instead. With a sys_enter_write probe parked in a userfaultfd fault, attaching a BPF raw tracepoint to sched_switch then blocked in tracepoint_add_func() until the fault was resolved (tested on v7.4-rc4). With this patch the attach returns after 54 ms, and after 59 ms without it. Calling __traceiter_*() directly from __DECLARE_TRACE_SYSCALL() would also fix the bug. I kept the static call, now always targeting the iterator, so that tracepoint_update_call() remains the one place that picks the target and the fix stays in kernel/tracepoint.c. I can switch to the direct call if that is preferred. With perf_syscall_enter() as the only sys_enter probe, returning early, getppid() takes 155.9 ns with this patch and 153.7 ns without it (median of 5 runs, each the best of 5 batches of 2M calls, in a nested KVM guest). With no probe attached, it takes 124.9 and 124.4 ns. ftracetest, with tools/testing/selftests/ftrace/config plus CONFIG_PREEMPT=y: 164 passed, 1 unresolved (checkbashisms is not installed), 12 unsupported and 2 xfailed, with and without this patch. Test-only hook, applying to v7.4-rc5 with or without this patch: diff --git a/include/linux/tracepoint.h b/include/linux/tracepoint.h index e0d838c9ce938..863231ad0cfe0 100644 --- a/include/linux/tracepoint.h +++ b/include/linux/tracepoint.h @@ -227,8 +227,23 @@ static inline struct tracepoint *tracepoint_ptr_deref(tracepoint_ptr_t *p) static_call(tp_func_##name)(__data, args); \ } \ } while (0) +/* TEST ONLY: widen the window between loading __data and the static call. */ +extern void tp_syscall_race_delay(void); +#define __DO_TRACE_CALL_SYSCALL(name, args) \ + do { \ + struct tracepoint_func *it_func_ptr; \ + void *__data; \ + it_func_ptr = \ + rcu_dereference_raw((&__tracepoint_##name)->funcs); \ + if (it_func_ptr) { \ + __data = (it_func_ptr)->data; \ + tp_syscall_race_delay(); \ + static_call(tp_func_##name)(__data, args); \ + } \ + } while (0) #else #define __DO_TRACE_CALL(name, args) __traceiter_##name(NULL, args) +#define __DO_TRACE_CALL_SYSCALL(name, args) __DO_TRACE_CALL(name, args) #endif /* CONFIG_HAVE_STATIC_CALL */ /* @@ -336,7 +351,7 @@ static inline struct tracepoint *tracepoint_ptr_deref(tracepoint_ptr_t *p) { \ TRACEPOINT_CHECK(name) \ guard(rcu_tasks_trace)(); \ - __DO_TRACE_CALL(name, TP_ARGS(args)); \ + __DO_TRACE_CALL_SYSCALL(name, TP_ARGS(args)); \ } \ static inline void trace_##name(proto) \ { \ diff --git a/kernel/tracepoint.c b/kernel/tracepoint.c index dffef52a807bc..87d4356078017 100644 --- a/kernel/tracepoint.c +++ b/kernel/tracepoint.c @@ -3,6 +3,8 @@ * Copyright (C) 2008-2014 Mathieu Desnoyers */ #include +#include +#include #include #include #include @@ -66,6 +68,45 @@ static void tp_rcu_cond_sync(enum tp_transition_sync sync) snapshot->ongoing = false; } +/* + * TEST ONLY: one-shot busy-wait inside faultable syscall tracepoints, after + * __data has been loaded and before the static call, for the task whose comm + * matches race_comm. Busy-wait with preemption enabled, so that regular RCU + * (and tracepoint_srcu) grace periods can complete meanwhile, exactly as they + * would if the task were preempted at that point. + * + * /sys/module/tracepoint/parameters/race_comm (write with echo -n) + * /sys/module/tracepoint/parameters/race_delay_ms (cleared once consumed) + * /sys/module/tracepoint/parameters/race_state (0 idle, 1 spinning, 2 done) + */ +static char race_comm[TASK_COMM_LEN]; +module_param_string(race_comm, race_comm, sizeof(race_comm), 0644); +static unsigned int race_delay_ms; +module_param(race_delay_ms, uint, 0644); +static int race_state; +module_param(race_state, int, 0444); + +notrace void tp_syscall_race_delay(void) +{ + unsigned int ms; + + if (likely(!READ_ONCE(race_delay_ms))) + return; + if (strncmp(current->comm, race_comm, TASK_COMM_LEN)) + return; + ms = xchg(&race_delay_ms, 0); + if (!ms) + return; + pr_info("tp-race: %s/%d loaded __data, spinning %u ms (preemptible=%d)\n", + current->comm, current->pid, ms, preemptible()); + WRITE_ONCE(race_state, 1); + mdelay(ms); + WRITE_ONCE(race_state, 2); + pr_info("tp-race: %s/%d resuming into static call\n", + current->comm, current->pid); +} +EXPORT_SYMBOL_GPL(tp_syscall_race_delay); + /* Set to 1 to enable tracepoint debug output */ static const int tracepoint_debug; With the hook, as root, the following crashes v7.4-rc5 in 3 of 3 runs, and does not crash it with this patch. This was run under virtme-ng, with "vng --kconfig" plus CONFIG_PREEMPT=y, CONFIG_FTRACE_SYSCALLS=y, CONFIG_PERF_EVENTS=y and CONFIG_KALLSYMS_ALL=y, booted with preempt=full: P=/sys/module/tracepoint/parameters E=/sys/kernel/tracing/events/syscalls/sys_enter_openat echo -n dd > $P/race_comm # sys_enter probe 1: perf_syscall_enter(), data NULL perf stat -a -e syscalls:sys_enter_openat & PERF=$!; sleep 1 echo 1 > $E/enable # probe 2: ftrace_syscall_enter(), data tr echo 2000 > $P/race_delay_ms dd if=/dev/zero of=/dev/null count=1 & # loads NULL, spins 2 s sleep 0.5; kill -INT $PERF # perf exits: 2 -> 1 sleep 3; dmesg | grep -A12 'BUG:' The trigger script used for the counts above, including the 1 -> 0 -> 1 variant, the userfaultfd test and the benchmark are available on request. kernel/tracepoint.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/kernel/tracepoint.c b/kernel/tracepoint.c index dffef52a807bc..11401df67e7b2 100644 --- a/kernel/tracepoint.c +++ b/kernel/tracepoint.c @@ -275,7 +275,14 @@ static void tracepoint_update_call(struct tracepoint *tp, struct tracepoint_func /* Synthetic events do not have static call sites */ if (!tp->static_call_key) return; - if (nr_func_state(tp_funcs) == TP_FUNC_1) + /* + * Faultable tracepoint readers can be preempted between loading + * funcs[0].data and the static call, and tp_rcu_cond_sync() does not + * wait for them. Keep the iterator, which loads each probe together + * with its data. + */ + if (nr_func_state(tp_funcs) == TP_FUNC_1 && + !tracepoint_is_faultable(tp)) func = tp_funcs[0].func; __static_call_update(tp->static_call_key, tp->static_call_tramp, func); } @@ -319,7 +326,7 @@ static int tracepoint_add_func(struct tracepoint *tp, * 1->0->1 transition sequence. */ tp_rcu_cond_sync(TP_TRANSITION_SYNC_1_0_1); - /* Set static call to first function */ + /* Set static call to first function, unless faultable */ tracepoint_update_call(tp, tp_funcs); /* Both iterator and static call handle NULL tp->funcs */ rcu_assign_pointer(tp->funcs, tp_funcs); @@ -401,7 +408,7 @@ static int tracepoint_remove_func(struct tracepoint *tp, if (tp_funcs[0].data != old[0].data) tp_rcu_get_state(TP_TRANSITION_SYNC_N_2_1); tp_rcu_cond_sync(TP_TRANSITION_SYNC_N_2_1); - /* Set static call to first function */ + /* Set static call to first function, unless faultable */ tracepoint_update_call(tp, tp_funcs); break; case TP_FUNC_2: /* N->N-1 (N>2) */ -- 2.53.0