tracing: wprobe: x86: Add wprobe for watchpoint
Subsystems (7)
Masami Hiramatsu (Google)
maintainerv13 cover tracing: wprobe: x86: Add wprobe for watchpoint
Hi,
Here is the 13th version of the series for adding new wprobe (watch probe) which provides memory access tracing event. Moreover, this can be used via event trigger. Thus it can trace memory access on dynamically allocated objects too.
The previous version is here:
https://lore.kernel.org/all/178611679753.237811.10190793347982785419.stgit@devnote2/
This version adds RCU protection for kprobe_blacklist, fixes trigger reference counting and error paths, eliminates race conditions in selftests, and addresses review comments. See below for more details.
Changes in v13:
- kprobes: Protect kprobe_blacklist with RCU to allow safe lockless traversals from atomic/NMI sections.
- tracing/wprobe: Use %pS to format symbol and offset in trace_wprobe_show().
- tracing/wprobe: Align WPROBE_DEFAULT_CLEAR_ADDRESS to 8 bytes using a dedicated u64 variable.
- tracing/wprobe: Remove manual trigger count decrement to rely on core event_trigger_count().
- tracing/wprobe: Check for field tokens before field parsing in clear_wprobe to avoid spurious error logs on numeric counts and avoid masking syntax errors.
- tracing/wprobe: Fix trigger reference count leak and error cleanup paths in wprobe_trigger_cmd_parse().
- x86/hw_breakpoints: Add compiler barrier() before checking cpu_dr7_seq.
- HWBP: Add comments to modify_local_hw_breakpoint_addr() clarifying the early bp->attr.bp_addr update behavior during the NMI race window.
- selftests/ftrace: Look up target functions dynamically in trigger-wprobe-syntax-errors.tc.
- selftests/ftrace: Disable set_wprobe trigger before verifying clear_wprobe in trigger-wprobe.tc to prevent sample_timer_cb race conditions.
- Documentation/trace: Add [:COUNT] to trigger syntax synopsis.
Public branch
-------------
I will push this branch as topic/wprobe-v2 to my tree so that it can be easily tested.
https://git.kernel.org/pub/scm/linux/kernel/git/mhiramat/linux.git/log/?h=topic/wprobe-v2
This is based on linux-trace tree's linux-trace/for-next.
Usage
-----
The basic usage of this wprobe is similar to other probes;
w:[GRP/][EVENT] [r|w|rw]@<ADDRESS|SYMBOL[+|-OFFS]>[:LEN] [FETCHARGS]
This defines a new wprobe event. For example, to trace jiffies update, you can do;
echo 'w:my_jiffies w@jiffies:8 value=+0($addr)' >> dynamic_events echo 1 > events/wprobes/my_jiffies/enable
Moreover, this can be combined with event trigger to trace the memory access on slab objects. The trigger syntax is;
set_wprobe:WPROBE_EVENT:FIELD[+|-OFFSET][:COUNT] [if FILTER] clear_wprobe:WPROBE_EVENT[:FIELD[+|-OFFSET][:COUNT]] [if FILTER]
set_wprobe sets WPROBE_EVENT's watch address on FIELD[+|-OFFSET]. clear_wprobe clears WPROBE_EVENT's watch address if it is set to FIELD[+|-OFFSET]. If FIELD is omitted, forcibly clear the watch address when trigger event is hit.
For example, trace the first 8 byte of the dentry data structure passed to do_truncate() until it is deleted by dentry_kill().
(Note: all tracefs setup uses '>>' so that it does not kick do_truncate())
# echo 'w:watch rw@0:8 address=$addr value=+0($addr)' > dynamic_events
# echo 'f:truncate do_truncate dentry=$arg2' >> dynamic_events # echo 'set_wprobe:watch:dentry' >> events/fprobes/truncate/trigger
# echo 'f:dentry_kill dentry_kill dentry=$arg1' >> dynamic_events # echo 'clear_wprobe:watch:dentry' >> events/fprobes/dentry_kill/trigger
# echo 1 >> events/fprobes/truncate/enable # echo 1 >> events/fprobes/dentry_kill/enable
# echo aaa > /tmp/hoge # echo bbb > /tmp/hoge # echo ccc > /tmp/hoge # rm /tmp/hoge
Then, the trace data will show;
# tracer: nop
#
# entries-in-buffer/entries-written: 32/32 #P:8
#
# _-----=> irqs-off/BH-disabled
# / _----=> need-resched
# | / _---=> hardirq/softirq
# || / _--=> preempt-depth
# ||| / _-=> migrate-disable
# |||| / delay
# TASK-PID CPU# ||||| TIMESTAMP FUNCTION
# | | | ||||| | |
sh-107 [004] ...1. 9.990418: dentry_kill: (dentry_kill+0x0/0x2c0) dentry=0xffff888004ad6618
sh-107 [004] ...1. 9.990914: dentry_kill: (dentry_kill+0x0/0x2c0) dentry=0xffff888004b3de78
sh-107 [004] ...1. 9.993175: dentry_kill: (dentry_kill+0x0/0x2c0) dentry=0xffff8880049ddd40
sh-107 [004] ..... 9.995198: truncate: (do_truncate+0x4/0x120) dentry=0xffff8880048083a8
sh-107 [004] ...1. 9.995389: dentry_kill: (dentry_kill+0x0/0x2c0) dentry=0xffff8880049db998
sh-107 [004] ..Zff 9.997503: watch: (lookup_fast+0xaa/0x150) address=0xffff8880048083a8 value=0x8200080
sh-107 [004] ..Zff 9.997509: watch: (path_openat+0x211/0xda0) address=0xffff8880048083a8 value=0x8200080
sh-107 [004] ..Zff 9.997514: watch: (path_openat+0xa56/0xda0) address=0xffff8880048083a8 value=0x8200080
sh-107 [004] ..Zff 9.997518: watch: (path_openat+0xae2/0xda0) address=0xffff8880048083a8 value=0x8200080
sh-107 [004] ..... 9.997521: truncate: (do_truncate+0x4/0x120) dentry=0xffff8880048083a8
sh-107 [004] ...1. 9.997582: dentry_kill: (dentry_kill+0x0/0x2c0) dentry=0xffff888004808270
sh-107 [004] ...1. 9.999365: dentry_kill: (dentry_kill+0x0/0x2c0) dentry=0xffff8880049db728
sh-107 [004] ...1. 9.999388: dentry_kill: (dentry_kill+0x0/0x2c0) dentry=0xffff888004b1c000
rm-113 [005] ..Zff 10.000965: watch: (lookup_fast+0xaa/0x150) address=0xffff8880048083a8 value=0x8200080
rm-113 [005] ..Zff 10.000971: watch: (path_lookupat+0x97/0x1e0) address=0xffff8880048083a8 value=0x8200080
rm-113 [005] ..Zff 10.000984: watch: (lookup_fast+0xaa/0x150) address=0xffff8880048083a8 value=0x8200080
rm-113 [005] ..Zff 10.000988: watch: (path_lookupat+0x97/0x1e0) address=0xffff8880048083a8 value=0x8200080
rm-113 [005] ..Zff 10.001010: watch: (lookup_one_qstr_excl+0x28/0x140) address=0xffff8880048083a8 value=0x8200080
rm-113 [005] ..Zff 10.001014: watch: (lookup_one_qstr_excl+0xd1/0x140) address=0xffff8880048083a8 value=0x8200080
rm-113 [005] ..Zff 10.001018: watch: (may_delete_dentry+0x1c/0x200) address=0xffff8880048083a8 value=0x8200080
rm-113 [005] ..Zff 10.001021: watch: (may_delete_dentry+0x195/0x200) address=0xffff8880048083a8 value=0x8200080
rm-113 [005] ..Zff 10.001031: watch: (vfs_unlink+0x5e/0x260) address=0xffff8880048083a8 value=0x8200080
rm-113 [005] d.Z.. 10.001067: watch: (d_make_discardable+0x1b/0x40) address=0xffff8880048083a8 value=0x8200080
rm-113 [005] d.Z.. 10.001071: watch: (d_make_discardable+0x29/0x40) address=0xffff8880048083a8 value=0x200080
rm-113 [005] ...1. 10.001072: dentry_kill: (dentry_kill+0x0/0x2c0) dentry=0xffff8880048083a8
rm-113 [005] ...1. 10.001218: dentry_kill: (dentry_kill+0x0/0x2c0) dentry=0xffff8880048083a8
sh-107 [004] ...1. 10.001416: dentry_kill: (dentry_kill+0x0/0x2c0) dentry=0xffff8880049db110
sh-107 [004] ...1. 10.001444: dentry_kill: (dentry_kill+0x0/0x2c0) dentry=0xffff8880049db248
sh-107 [004] ...1. 10.001500: dentry_kill: (dentry_kill+0x0/0x2c0) dentry=0xffff888004ad6618
sh-107 [004] ...1. 10.002067: dentry_kill: (dentry_kill+0x0/0x2c0) dentry=0xffff888004b41e78
sh-107 [004] ...1. 10.904920: dentry_kill: (dentry_kill+0x0/0x2c0) dentry=0xffff888004b41e78
sh-107 [004] ...1. 10.905129: dentry_kill: (dentry_kill+0x0/0x2c0) dentry=0xffff888004ad6618Thank you,
---
base-commit: 54f133c6178d06b169dceae18cc92c4945933b8c
Jinchao Wang (2):
x86/hw_breakpoints: Make DR7 updates NMI safe x86/hw_breakpoints: Add arch_modify_local_hw_breakpoint_addr() API
Masami Hiramatsu (Google) (10):
kprobes: Protect kprobe_blacklist with RCU
HWBP: Add modify_local_hw_breakpoint_addr() API tracing/wprobe: Add wprobe (watchpoint probe) trace event support
x86: hw_breakpoint: Add a kconfig to clarify when a breakpoint fires
selftests: tracing: Add a basic testcase for wprobe
selftests: tracing: Add syntax testcase for wprobe tracing/wprobe: Add set_wprobe and clear_wprobe event triggers
selftests: ftrace: Add wprobe trigger testcase tracing/wprobe: Support BTF typecast in fetchargs tracing/wprobe: Support BTF struct offset resolution in set_wprobe trigger
Documentation/trace/index.rst | 1 Documentation/trace/wprobetrace.rst | 185 +++ arch/Kconfig | 18 arch/x86/Kconfig | 2 arch/x86/include/asm/debugreg.h | 37 - arch/x86/include/asm/hw_breakpoint.h | 2 arch/x86/kernel/cpu/mce/core.c | 20 arch/x86/kernel/hw_breakpoint.c | 139 +- arch/x86/kernel/nmi.c | 7 arch/x86/kernel/traps.c | 10 include/linux/hw_breakpoint.h | 6 include/linux/kprobes.h | 1 include/linux/trace_events.h | 3 kernel/events/hw_breakpoint.c | 48 + kernel/kprobes.c | 14 kernel/trace/Kconfig | 24 kernel/trace/Makefile | 1 kernel/trace/trace.c | 9 kernel/trace/trace.h | 6 kernel/trace/trace_events_trigger.c | 2 kernel/trace/trace_probe.c | 36 - kernel/trace/trace_probe.h | 22 kernel/trace/trace_wprobe.c | 1440 ++++++++++++++++++++ tools/testing/selftests/ftrace/config | 3 .../ftrace/test.d/dynevent/add_remove_wprobe.tc | 63 + .../test.d/dynevent/wprobes_syntax_errors.tc | 22 .../test.d/trigger/trigger-wprobe-btf-offset.tc | 74 + .../test.d/trigger/trigger-wprobe-btf-typecast.tc | 72 + .../test.d/trigger/trigger-wprobe-syntax-errors.tc | 37 + .../ftrace/test.d/trigger/trigger-wprobe.tc | 87 + 30 files changed, 2309 insertions(+), 82 deletions(-) create mode 100644 Documentation/trace/wprobetrace.rst create mode 100644 kernel/trace/trace_wprobe.c create mode 100644 tools/testing/selftests/ftrace/test.d/dynevent/add_remove_wprobe.tc create mode 100644 tools/testing/selftests/ftrace/test.d/dynevent/wprobes_syntax_errors.tc create mode 100644 tools/testing/selftests/ftrace/test.d/trigger/trigger-wprobe-btf-offset.tc create mode 100644 tools/testing/selftests/ftrace/test.d/trigger/trigger-wprobe-btf-typecast.tc create mode 100644 tools/testing/selftests/ftrace/test.d/trigger/trigger-wprobe-syntax-errors.tc create mode 100644 tools/testing/selftests/ftrace/test.d/trigger/trigger-wprobe.tc
--
Masami Hiramatsu (Google) <[email protected]>
Masami Hiramatsu (Google)
maintainerPATCH v13 1/12 kprobes: Protect kprobe_blacklist with RCU
From: Masami Hiramatsu (Google) <[email protected]>
__within_kprobe_blacklist() traverses kprobe_blacklist without holding kprobe_mutex. When a module is unloaded, kprobe_remove_area_blacklist() removes blacklist entries and immediately frees them with kfree(). A concurrent call to within_kprobe_blacklist() can therefore dereference freed memory.
Furthermore, within_kprobe_blacklist() can be called in atomic or non-preemptible contexts where the sleeping kprobe_mutex cannot be taken.
Protect kprobe_blacklist with RCU. Use guard(rcu)() and list_for_each_entry_rcu() for traversal, list_add_tail_rcu() for insertions, list_del_rcu() for deletions, and kfree_rcu() to reclaim entries safely after a grace period.
---
Changes in v13:
- Newly added.
--- include/linux/kprobes.h | 1 + kernel/kprobes.c | 14 ++++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-)
include/linux/kprobes.h +1 -0
@@ -181,6 +181,7 @@ struct kprobe_blacklist_entry {struct list_head list;unsigned long start_addr;unsigned long end_addr; +struct rcu_head rcu; };#ifdef CONFIG_KPROBES
kernel/kprobes.c +10 -4
@@ -1447,8 +1447,14 @@ static bool __within_kprobe_blacklist(unsigned long addr)/* * If 'kprobe_blacklist' is defined, check the address and * reject any probe registration in the prohibited area. +* Note: this can return true during transition period where +* (start_addr, end_addr) in the black list is shrinking +* but old entry has not been removed yet. This is acceptable +* because the worst case is that we reject more probes than +* we should. */ -list_for_each_entry (ent, &kprobe_blacklist, list) { +guard (rcu)(); +list_for_each_entry_rcu (ent, &kprobe_blacklist, list) {if (addr >= ent->start_addr && addr < ent->end_addr)return true; }
@@ -2509,7 +2515,7 @@ int kprobe_add_ksym_blacklist(unsigned long entry) ent->start_addr = entry; ent->end_addr = entry + size;INIT_LIST_HEAD (&ent->list); -list_add_tail (&ent->list, &kprobe_blacklist); +list_add_tail_rcu (&ent->list, &kprobe_blacklist);return (int )size; }
@@ -2603,8 +2609,8 @@ static void kprobe_remove_area_blacklist(unsigned long start, unsigned long end)list_for_each_entry_safe (ent, n, &kprobe_blacklist, list) {if (ent->start_addr < start || ent->start_addr >= end)continue ; -list_del (&ent->list); -kfree (ent); +list_del_rcu (&ent->list); +kfree_rcu (ent, rcu); } }
[email protected]
AI reviewRe: [PATCH v13 01/12] kprobes: Protect kprobe_blacklist with RCU
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines
From: Masami Hiramatsu (Google) <[email protected]>
__within_kprobe_blacklist()traverseskprobe_blacklistwithout holdingkprobe_mutex. When a module is unloaded,kprobe_remove_area_blacklist()removes blacklist entries and immediately frees them withkfree(). A concurrent call towithin_kprobe_blacklist()can therefore dereference freed memory.Furthermore,
within_kprobe_blacklist()can be called in atomic or non-preemptible contexts where the sleepingkprobe_mutexcannot be taken.
Quoted 7 lines
Protect
kprobe_blacklistwith RCU. Use guard(rcu)() andlist_for_each_entry_rcu()for traversal,list_add_tail_rcu()for insertions,list_del_rcu()for deletions, andkfree_rcu()to reclaim entries safely after a grace period.Assisted-by Antigravity:gemini-3.7-flashSigned-off-by Masami Hiramatsu (Google) <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/178739053919.1520941.17662338993878200834.stgit@devnote2?part=1
Masami Hiramatsu (Google)
maintainerPATCH v13 2/12 x86/hw_breakpoints: Make DR7 updates NMI safe
In reply to Masami Hiramatsu (Google)From: Jinchao Wang <[email protected]>
Hardware breakpoint installation and removal run with IRQs disabled, but an NMI can still enter the same code through KGDB. The interrupted operation and the NMI can consequently claim the same slot or overwrite each other's DR7 state.
Claim and release per-CPU slots with cmpxchg. Update cpu_dr7 with single-instruction per-CPU operations, and preserve hardware-first disable and hardware-last enable ordering. Add a per-CPU sequence number so interrupted DR7 writers and restore paths detect an NMI update and retry from the latest shadow state.
---
Changes in v13:
- Add compiler barriers (barrier()) before checking cpu_dr7_seq to prevent the compiler from hoisting the sequence check before debug register updates or shadow state modifications.
Changes in v12:
- Use stack variables dr7 and dr7_seq directly in exc_nmi() and remove unnecessary per-CPU nmi_dr7 and nmi_dr7_seq variables.
--- arch/x86/include/asm/debugreg.h | 37 ++++++++++---- arch/x86/kernel/cpu/mce/core.c | 20 +++++--- arch/x86/kernel/hw_breakpoint.c | 101 ++++++++++++++++++++------------------- arch/x86/kernel/nmi.c | 7 ++- arch/x86/kernel/traps.c | 10 +++- 5 files changed, 101 insertions(+), 74 deletions(-)
arch/x86/include/asm/debugreg.h +27 -10
@@ -18,6 +18,7 @@#define DR7_FIXED_1 0x00000400 DECLARE_PER_CPU(unsigned long, cpu_dr7); +DECLARE_PER_CPU(unsigned int, cpu_dr7_seq);#ifndef CONFIG_PARAVIRT_XXL /*
@@ -125,18 +126,20 @@ static __always_inline bool hw_breakpoint_active(void)extern void hw_breakpoint_restore (void ); -static __always_inline unsigned long local_db_save (void ) +static __always_inline void local_db_save(unsigned long *dr7, +unsigned int *dr7_seq) { -unsigned long dr7; + *dr7 =0 ; + *dr7_seq =this_cpu_read (cpu_dr7_seq);if (static_cpu_has (X86_FEATURE_HYPERVISOR ) && !hw_breakpoint_active ()) -return 0 ; +return ; -get_debugreg (dr7,7 ); +get_debugreg (*dr7,7 );/* Architecturally set bit */ - dr7 &= ~DR7_FIXED_1 ; -if (dr7) + *dr7 &= ~DR7_FIXED_1 ; +if (*dr7)set_debugreg (DR7_FIXED_1 ,7 );/*
@@ -145,20 +148,34 @@ static __always_inline unsigned long local_db_save(void)* be good. */ barrier (); - -return dr7; } -static __always_inline void local_db_restore(unsigned long dr7) +static __always_inline void local_db_restore(unsigned long dr7, +unsigned int dr7_seq) { +unsigned int seq; +/* * Ensure the compiler doesn't raise this statement into * the critical section; enabling breakpoints early would * not be good. */ barrier (); -if (dr7) + +do { + seq =this_cpu_read (cpu_dr7_seq); +if (seq == dr7_seq) { +if (!dr7) +return ; + }else { + dr7 =this_cpu_read (cpu_dr7); +if (!dr7) + dr7 =DR7_FIXED_1 ; + } +set_debugreg (dr7,7 ); +barrier (); + }while (unlikely (seq !=this_cpu_read (cpu_dr7_seq))); }#ifdef CONFIG_CPU_SUP_AMD
arch/x86/kernel/cpu/mce/core.c +12 -8
@@ -2139,20 +2139,22 @@ static __always_inline void exc_machine_check_user(struct pt_regs *regs)DEFINE_IDTENTRY_MCE (exc_machine_check) {unsigned long dr7; +unsigned int dr7_seq; - dr7 =local_db_save (); +local_db_save (&dr7, &dr7_seq);exc_machine_check_kernel (regs); -local_db_restore (dr7); +local_db_restore (dr7, dr7_seq); }/* The user mode variant. */ DEFINE_IDTENTRY_MCE_USER (exc_machine_check) {unsigned long dr7; +unsigned int dr7_seq; - dr7 =local_db_save (); +local_db_save (&dr7, &dr7_seq);exc_machine_check_user (regs); -local_db_restore (dr7); +local_db_restore (dr7, dr7_seq); }#ifdef CONFIG_X86_FRED
@@ -2170,13 +2172,14 @@ DEFINE_IDTENTRY_MCE_USER(exc_machine_check)DEFINE_FREDENTRY_MCE (exc_machine_check) {unsigned long dr7; +unsigned int dr7_seq; - dr7 =local_db_save (); +local_db_save (&dr7, &dr7_seq);if (user_mode (regs))exc_machine_check_user (regs);else exc_machine_check_kernel (regs); -local_db_restore (dr7); +local_db_restore (dr7, dr7_seq); }#endif #else
@@ -2184,13 +2187,14 @@ DEFINE_FREDENTRY_MCE(exc_machine_check)DEFINE_IDTENTRY_RAW (exc_machine_check) {unsigned long dr7; +unsigned int dr7_seq; - dr7 =local_db_save (); +local_db_save (&dr7, &dr7_seq);if (user_mode (regs))exc_machine_check_user (regs);else exc_machine_check_kernel (regs); -local_db_restore (dr7); +local_db_restore (dr7, dr7_seq); }#endif
arch/x86/kernel/hw_breakpoint.c +51 -50
156 lines not on this page · raw message
arch/x86/kernel/nmi.c +4 -3
@@ -531,11 +531,12 @@ enum nmi_states { }; static DEFINE_PER_CPU(enum nmi_states, nmi_state); static DEFINE_PER_CPU(unsigned long, nmi_cr2); -static DEFINE_PER_CPU(unsigned long, nmi_dr7);DEFINE_IDTENTRY_RAW (exc_nmi ) {irqentry_state_t irq_state; +unsigned long dr7; +unsigned int dr7_seq;struct nmi_stats *nsp =this_cpu_ptr (&nmi_stats);/*
@@ -572,7 +573,7 @@ DEFINE_IDTENTRY_RAW(exc_nmi)*/ sev_es_ist_enter (regs); -this_cpu_write (nmi_dr7,local_db_save ()); +local_db_save (&dr7, &dr7_seq); irq_state =irqentry_nmi_enter (regs);
@@ -594,7 +595,7 @@ DEFINE_IDTENTRY_RAW(exc_nmi)irqentry_nmi_exit (regs, irq_state); -local_db_restore (this_cpu_read (nmi_dr7)); +local_db_restore (dr7, dr7_seq);sev_es_ist_exit ();
arch/x86/kernel/traps.c +7 -3
@@ -1231,8 +1231,12 @@ static noinstr void exc_debug_kernel(struct pt_regs *regs, unsigned long dr6)* it results in an endless recursion and stack overflow. Thus we stay * with the IDT approach, i.e., save DR7 and disable #DB. */ -unsigned long dr7 =local_db_save (); -irqentry_state_t irq_state =irqentry_nmi_enter (regs); +unsigned long dr7; +unsigned int dr7_seq; +irqentry_state_t irq_state; + +local_db_save (&dr7, &dr7_seq); + irq_state =irqentry_nmi_enter (regs);instrumentation_begin ();/*
@@ -1289,7 +1293,7 @@ static noinstr void exc_debug_kernel(struct pt_regs *regs, unsigned long dr6)instrumentation_end ();irqentry_nmi_exit (regs, irq_state); -local_db_restore (dr7); +local_db_restore (dr7, dr7_seq); } static noinstr void exc_debug_user(struct pt_regs *regs, unsigned long dr6)
[email protected]
AI reviewRe: [PATCH v13 02/12] x86/hw_breakpoints: Make DR7 updates NMI safe
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
New issues:
- [High] local_db_restore overwrites an intentionally cleared hardware DR7 state (e.g., KVM VMENTER) with the host's shadow cpu_dr7 if an NMI modifies the sequence counter.
Pre-existing issues:
- [High] Task migration during user-mode machine check exceptions corrupts hardware debug registers on the new CPU. --
--- Patch [2]: [PATCH v13 02/12] x86/hw_breakpoints: Make DR7 updates NMI safe --- commit 0d8b774ae86048690b3378737c33f417379e85de
Author: Jinchao Wang <[email protected]>
x86/hw_breakpoints: Make DR7 updates NMI safe
Hardware breakpoint installation and removal run with IRQs disabled, but an NMI can still enter the same code through KGDB. The interrupted operation and the NMI can consequently claim the same slot or overwrite each other's DR7 state.
Claim and release per-CPU slots with cmpxchg. Update cpu_dr7 with single-instruction per-CPU operations, and preserve hardware-first disable and hardware-last enable ordering. Add a per-CPU sequence number so interrupted DR7 writers and restore paths detect an NMI update and retry from the latest shadow state.
Quoted 4 lines
arch/x86/include/asm/debugreg.h +0
-0
[ ... ]
Quoted 27 lines
arch/x86/include/asm/debugreg.h +10
-4@@ -145,20 +148,34 @@ static __always_inline unsigned long local_db_save(void)* be good. */ barrier (); - -return dr7; } -static __always_inline void local_db_restore(unsigned long dr7) +static __always_inline void local_db_restore(unsigned long dr7, +unsigned int dr7_seq) { +unsigned int seq; +/* * Ensure the compiler doesn't raise this statement into * the critical section; enabling breakpoints early would * not be good. */ barrier (); -if (dr7) + + do { + seq = this_cpu_read(cpu_dr7_seq); + if (seq == dr7_seq) { + if (!dr7) + return;
arch/x86/include/asm/debugreg.h static __always_inline unsigned long local_db_save(void) · line 179
Quoted 10 lines
arch/x86/include/asm/debugreg.h +8
-0+ }else { + dr7 =this_cpu_read (cpu_dr7); +if (!dr7) + dr7 =DR7_FIXED_1 ; + } +set_debugreg (dr7,7 ); +barrier (); + }while (unlikely (seq !=this_cpu_read (cpu_dr7_seq))); }
[Severity: High]
If KVM intentionally clears the hardware DR7 state to isolate the guest from host breakpoints before VMENTER without modifying the host's cpu_dr7 shadow state:
arch/x86/kvm/x86.c:vcpu_enter_guest() {
...
if (unlikely(vcpu->arch.switch_db_regs &&
!(vcpu->arch.switch_db_regs & KVM_DEBUGREG_AUTO_SWITCH))) {
set_debugreg(DR7_FIXED_1, 7);
...
} else if (unlikely(hw_breakpoint_active())) {
set_debugreg(DR7_FIXED_1, 7);
}
...
}Could an NMI firing shortly after this permanently undo this isolation upon return?
If an NMI (such as a perf event) fires before vmlaunch/vmresume, exc_nmi calls local_db_save, saving dr7=0. If the NMI modifies a breakpoint, cpu_dr7_seq increments.
When exc_nmi calls local_db_restore, the sequence mismatch causes it to discard the saved dr7=0 state and instead unconditionally restore the host's active breakpoints (this_cpu_read(cpu_dr7)) to the hardware DR7. This could leak host hardware breakpoints into a KVM guest and cause unexpected #DB exceptions.
[ ... ]
Quoted 4 lines
arch/x86/kernel/cpu/mce/core.c +0
-0
[ ... ]
Quoted 15 lines
arch/x86/kernel/cpu/mce/core.c +3
-2@@ -2139,20 +2139,22 @@ static __always_inline void exc_machine_check_user(struct pt_regs *regs)DEFINE_IDTENTRY_MCE (exc_machine_check) {unsigned long dr7; +unsigned int dr7_seq; - dr7 =local_db_save (); +local_db_save (&dr7, &dr7_seq);exc_machine_check_kernel (regs); -local_db_restore (dr7); +local_db_restore (dr7, dr7_seq); }/* The user mode variant. */ DEFINE_IDTENTRY_MCE_USER (exc_machine_check)
arch/x86/kernel/cpu/mce/core.c static __always_inline void exc_machine_check_user(struct pt_regs *regs) · line 2158
Quoted 10 lines
arch/x86/kernel/cpu/mce/core.c +3
-2{unsigned long dr7; +unsigned int dr7_seq; - dr7 =local_db_save (); +local_db_save (&dr7, &dr7_seq);exc_machine_check_user (regs); -local_db_restore (dr7); +local_db_restore (dr7, dr7_seq); }
[Severity: High]
This is a pre-existing issue, but does task migration during user-mode machine check exceptions corrupt hardware debug registers on the new CPU?
exc_machine_check_user invokes irqentry_exit_to_user_mode, which processes pending thread work (like TIF_NEED_RESCHED), enabling interrupts and allowing the task to be scheduled out and migrate to a new CPU:
CPU1exc_machine_check()
local_db_save(&dr7, &dr7_seq);
exc_machine_check_user(regs)
irqentry_exit_to_user_mode(regs)
// Task migrates to CPU2CPU2
local_db_restore(dr7, dr7_seq);
When it resumes on the new CPU, local_db_restore is executed with the dr7 and dr7_seq values saved from the old CPU. If the new CPU's cpu_dr7_seq matches the old CPU's saved dr7_seq, it would erroneously write the old CPU's hardware DR7 state to the new CPU's hardware DR7 register. Could this cause spurious #DB exceptions or break hardware breakpoints on the new CPU?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/178739053919.1520941.17662338993878200834.stgit@devnote2?part=2
Masami Hiramatsu (Google)
maintainerRe: [PATCH v13 02/12] x86/hw_breakpoints: Make DR7 updates NMI safe
Quoted 54 lines · On Sat, 22 Aug 2026 09:41:06 +0000 [email protected] wrote:
Quoted 19 lines
arch/x86/include/asm/debugreg.h +10
-2-static __always_inline void local_db_restore(unsigned long dr7) +static __always_inline void local_db_restore(unsigned long dr7, +unsigned int dr7_seq) { +unsigned int seq; +/* * Ensure the compiler doesn't raise this statement into * the critical section; enabling breakpoints early would * not be good. */ barrier (); -if (dr7) + + do { + seq = this_cpu_read(cpu_dr7_seq); + if (seq == dr7_seq) { + if (!dr7) + return;arch/x86/include/asm/debugreg.h static __always_inline unsigned long local_db_save(void) · line 179
Quoted 10 lines
arch/x86/include/asm/debugreg.h +8
-0+ }else { + dr7 =this_cpu_read (cpu_dr7); +if (!dr7) + dr7 =DR7_FIXED_1 ; + } +set_debugreg (dr7,7 ); +barrier (); + }while (unlikely (seq !=this_cpu_read (cpu_dr7_seq))); }[Severity: High]
If KVM intentionally clears the hardware DR7 state to isolate the guest from host breakpoints before VMENTER without modifying the host'scpu_dr7shadow state:arch/x86/kvm/x86.c:vcpu_enter_guest() { ... if (unlikely(vcpu->arch.switch_db_regs && !(vcpu->arch.switch_db_regs & KVM_DEBUGREG_AUTO_SWITCH))) { set_debugreg(DR7_FIXED_1, 7); ... } else if (unlikely(hw_breakpoint_active())) { set_debugreg(DR7_FIXED_1, 7); } ... }Could an NMI firing shortly after this permanently undo this isolation upon return?
If an NMI (such as a perf event) fires before vmlaunch/vmresume,
exc_nmicallslocal_db_save, saving dr7=0. If the NMI modifies a breakpoint,cpu_dr7_seqincrements.
Quoted 5 lines
When
exc_nmicallslocal_db_restore, the sequence mismatch causes it to discard the saved dr7=0 state and instead unconditionally restore the host's active breakpoints (this_cpu_read(cpu_dr7)) to the hardware DR7. This could leak host hardware breakpoints into a KVM guest and cause unexpected #DB exceptions.
OK, this seems only happen when KGDB/KDB sets the watchpoint from NMI context (right after KVM disables dr7 for transient to guest). We can avoid this by early returning if dr7 == 0 in local_db_restore(). (But..., if user intensively set the dr7 in that timing, it will be ignored.)
Quoted 48 lines
[ ... ]
Quoted 4 lines
arch/x86/kernel/cpu/mce/core.c +0
-0[ ... ]
Quoted 15 lines
arch/x86/kernel/cpu/mce/core.c +3
-2@@ -2139,20 +2139,22 @@ static __always_inline void exc_machine_check_user(struct pt_regs *regs)DEFINE_IDTENTRY_MCE (exc_machine_check) {unsigned long dr7; +unsigned int dr7_seq; - dr7 =local_db_save (); +local_db_save (&dr7, &dr7_seq);exc_machine_check_kernel (regs); -local_db_restore (dr7); +local_db_restore (dr7, dr7_seq); }/* The user mode variant. */ DEFINE_IDTENTRY_MCE_USER (exc_machine_check)arch/x86/kernel/cpu/mce/core.c static __always_inline void exc_machine_check_user(struct pt_regs *regs) · line 2158
Quoted 10 lines
arch/x86/kernel/cpu/mce/core.c +3
-2{unsigned long dr7; +unsigned int dr7_seq; - dr7 =local_db_save (); +local_db_save (&dr7, &dr7_seq);exc_machine_check_user (regs); -local_db_restore (dr7); +local_db_restore (dr7, dr7_seq); }[Severity: High]
This is a pre-existing issue, but does task migration during user-mode machine check exceptions corrupt hardware debug registers on the new CPU?
exc_machine_check_userinvokesirqentry_exit_to_user_mode, which processes pending thread work (likeTIF_NEED_RESCHED), enabling interrupts and allowing the task to be scheduled out and migrate to a new CPU:CPU1
exc_machine_check()local_db_save(&dr7, &dr7_seq); exc_machine_check_user(regs) irqentry_exit_to_user_mode(regs) // Task migrates to CPU2
Quoted 8 lines
CPU2
local_db_restore(dr7, dr7_seq);When it resumes on the new CPU,
local_db_restoreis executed with the dr7 anddr7_seqvalues saved from the old CPU. If the new CPU'scpu_dr7_seqmatches the old CPU's saveddr7_seq, it would erroneously write the old CPU's hardware DR7 state to the new CPU's hardware DR7 register. Could this cause spurious #DB exceptions or break hardware breakpoints on the new CPU?
OK, this is another problem to be fixed in a bugfix patch. We should do local_db_save/restore() inside exc_machine_check_user() before calling irqentry_exit_to_user_mode().
Thank you,
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Masami Hiramatsu (Google)
maintainerPATCH v13 3/12 x86/hw_breakpoints: Add arch_modify_local_hw_breakpoint_addr() API
In reply to Masami Hiramatsu (Google)From: Jinchao Wang <[email protected]>
Wprobe needs to move an active per-CPU watchpoint without releasing and reserving its hardware slot.
Add arch_modify_local_hw_breakpoint_addr() as the architecture backend for modify_local_hw_breakpoint_addr(). It validates the new address, finds the installed local slot, and updates the hardware debug register and per-CPU shadow register. The core layer (modify_local_hw_breakpoint_addr()) handles updating bp->attr.bp_addr and counter_arch_bp(bp)->address.
The caller must provide an installed local event and a valid address. Slot ownership, breakpoint type, length, mask and DR7 remain unchanged.
---
Changes in v13:
- Add compiler barrier (barrier()) before checking cpu_dr7_seq to prevent hoisting the sequence check before set_debugreg().
- Clarify in commit log and code comment that the core layer manages updating bp->attr.bp_addr and counter_arch_bp(bp)->address.
Changes in v11:
- Return int error code instead of void.
- Validate the new address using hw_breakpoint_arch_parse() before updating registers.
- Increment cpu_dr7_seq in a sequence loop for NMI protection.
- Return -ENOENT if the breakpoint slot is not found on the local CPU.
--- arch/x86/include/asm/hw_breakpoint.h | 2 ++ arch/x86/kernel/hw_breakpoint.c | 44 ++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+)
arch/x86/include/asm/hw_breakpoint.h +2 -0
@@ -59,6 +59,8 @@ extern int hw_breakpoint_exceptions_notify(struct notifier_block *unused,int arch_install_hw_breakpoint (struct perf_event *bp); +int arch_modify_local_hw_breakpoint_addr (struct perf_event *bp, +unsigned long addr);void arch_uninstall_hw_breakpoint (struct perf_event *bp);void hw_breakpoint_pmu_read (struct perf_event *bp);void hw_breakpoint_pmu_unthrottle (struct perf_event *bp);
arch/x86/kernel/hw_breakpoint.c +44 -0
50 lines not on this page · raw message
[email protected]
AI reviewRe: [PATCH v13 03/12] x86/hw_breakpoints: Add arch_modify_local_hw_breakpoint_addr() API
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines
From: Jinchao Wang <[email protected]>
Wprobe needs to move an active per-CPU watchpoint without releasing and reserving its hardware slot.
Add
arch_modify_local_hw_breakpoint_addr()as the architecture backend formodify_local_hw_breakpoint_addr(). It validates the new address, finds the installed local slot, and updates the hardware debug register and per-CPU shadow register. The core layer (modify_local_hw_breakpoint_addr()) handles updatingbp->attr.bp_addrand counter_arch_bp(bp)->address.
Quoted 7 lines
The caller must provide an installed local event and a valid address. Slot ownership, breakpoint type, length, mask and DR7 remain unchanged.
Signed-off-by Jinchao Wang <[email protected]>Signed-off-by Masami Hiramatsu (Google) <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/178739053919.1520941.17662338993878200834.stgit@devnote2?part=3
Masami Hiramatsu (Google)
maintainerPATCH v13 4/12 HWBP: Add modify_local_hw_breakpoint_addr() API
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
Add modify_local_hw_breakpoint_addr() to update only the watched address of an installed hardware breakpoint on the local CPU without releasing and reserving its hardware slot. This is available when the architecture selects CONFIG_HAVE_MODIFY_LOCAL_HW_BREAKPOINT_ADDR.
The caller must provide an installed local event and a valid address, and update other CPUs separately.
---
Changes in v13:
- Add comment to modify_local_hw_breakpoint_addr() clarifying the early update behavior of bp->attr.bp_addr during the NMI race window.
Changes in v12:
- Update bp->attr.bp_addr and counter_arch_bp(bp)->address before calling arch_modify_local_hw_breakpoint_addr() to prevent race conditions during interrupt handling.
- Restore counter_arch_bp(bp)->address and bp->attr.bp_addr if arch_modify_local_hw_breakpoint_addr() fails.
Changes in v11:
- Check return value of arch_modify_local_hw_breakpoint_addr() and return error on failure.
- Update bp->attr.bp_addr and counter_arch_bp(bp)->address only after arch modification succeeds.
--- arch/Kconfig | 8 +++++++ arch/x86/Kconfig | 1 + include/linux/hw_breakpoint.h | 6 +++++ kernel/events/hw_breakpoint.c | 48 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+)
arch/Kconfig +8 -0
@@ -457,6 +457,14 @@ config HAVE_MIXED_BREAKPOINTS_REGS Select this option if your arch implements breakpoints under the latter fashion. +config HAVE_MODIFY_LOCAL_HW_BREAKPOINT_ADDR + bool + depends on HAVE_HW_BREAKPOINT + help + Select this if the architecture can modify the address of an + installed hardware breakpoint on the local CPU without releasing + and reserving its hardware slot. + config HAVE_USER_RETURN_NOTIFIER bool
arch/x86/Kconfig +1 -0
@@ -247,6 +247,7 @@ config X86 select HAVE_FUNCTION_TRACER select HAVE_GCC_PLUGINS select HAVE_HW_BREAKPOINT + select HAVE_MODIFY_LOCAL_HW_BREAKPOINT_ADDR select HAVE_IOREMAP_PROT select HAVE_IRQ_EXIT_ON_IRQ_STACK if X86_64 select HAVE_IRQ_TIME_ACCOUNTING
include/linux/hw_breakpoint.h +6 -0
@@ -81,6 +81,9 @@ register_wide_hw_breakpoint(struct perf_event_attr *attr,perf_overflow_handler_t triggered, void *context); +int modify_local_hw_breakpoint_addr (struct perf_event *bp, +unsigned long addr); +extern int register_perf_hw_breakpoint (struct perf_event *bp);extern void unregister_hw_breakpoint (struct perf_event *bp);extern void unregister_wide_hw_breakpoint (struct perf_event *__percpu *cpu_events);
@@ -124,6 +127,9 @@ register_wide_hw_breakpoint(struct perf_event_attr *attr,perf_overflow_handler_t triggered, void *context) { return NULL; }static inline int +modify_local_hw_breakpoint_addr (struct perf_event *bp, +unsigned long addr) {return -EOPNOTSUPP ; } +static inline int register_perf_hw_breakpoint (struct perf_event *bp) {return -ENOSYS ; }static inline void unregister_hw_breakpoint (struct perf_event *bp) { } static inline void
kernel/events/hw_breakpoint.c +48 -0
54 lines not on this page · raw message
[email protected]
AI reviewRe: [PATCH v13 04/12] HWBP: Add modify_local_hw_breakpoint_addr() API
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines
From: Masami Hiramatsu (Google) <[email protected]>
Add
modify_local_hw_breakpoint_addr()to update only the watched address of an installed hardware breakpoint on the local CPU without releasing and reserving its hardware slot. This is available when the architecture selectsCONFIG_HAVE_MODIFY_LOCAL_HW_BREAKPOINT_ADDR.
Quoted 7 lines
The caller must provide an installed local event and a valid address, and update other CPUs separately.
Signed-off-by Masami Hiramatsu (Google) <[email protected]>Signed-off-by Jinchao Wang <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/178739053919.1520941.17662338993878200834.stgit@devnote2?part=4
Masami Hiramatsu (Google)
maintainerPATCH v13 5/12 tracing/wprobe: Add wprobe (watchpoint probe) trace event support
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
Add hardware-breakpoint-based dynamic trace event support (wprobe). Wprobe creates a dynamic event on data read/write accesses using hardware breakpoints and logs the access context and fetchargs.
---
Changes in v13:
- Fix documentation notation to SYMBOL[+|-OFFS] as same as others.
- Fix documentation indents and add default value.
- Fix README file to add [<args>].
- Add a note about module unloading.
Changes in v12:
- Fix syntax comment typo ('b' to 'w') and remove dead 'ret = 0' initialization in __trace_wprobe_create().
- Fix documentation notation to SYMBOL[[+|-]OFFS].
Changes in v11:
- Add trace_wprobe_is_busy() to prevent releasing busy events.
Changes in v9:
- Use kzalloc_flex for alloc_trace_wprobe.
--- Documentation/trace/index.rst | 1 Documentation/trace/wprobetrace.rst | 78 ++++ include/linux/trace_events.h | 2 kernel/trace/Kconfig | 13 + kernel/trace/Makefile | 1 kernel/trace/trace.c | 9 kernel/trace/trace.h | 5 kernel/trace/trace_probe.c | 21 + kernel/trace/trace_probe.h | 10 kernel/trace/trace_wprobe.c | 758 +++++++++++++++++++++++++++++++++++ 10 files changed, 894 insertions(+), 4 deletions(-) create mode 100644 Documentation/trace/wprobetrace.rst create mode 100644 kernel/trace/trace_wprobe.c
Documentation/trace/index.rst +1 -0
@@ -36,6 +36,7 @@ the Linux kernel. kprobes kprobetrace fprobetrace + wprobetrace eprobetrace fprobe ring-buffer-design
Documentation/trace/wprobetrace.rst new file +78 -0
78 lines not on this page · raw message
include/linux/trace_events.h +2 -0
@@ -337,6 +337,7 @@ enum {TRACE_EVENT_FL_UPROBE_BIT ,TRACE_EVENT_FL_EPROBE_BIT ,TRACE_EVENT_FL_FPROBE_BIT , +TRACE_EVENT_FL_WPROBE_BIT ,TRACE_EVENT_FL_CUSTOM_BIT , TRACE_EVENT_FL_TEST_STR_BIT, };
@@ -367,6 +368,7 @@ enum {TRACE_EVENT_FL_UPROBE = (1 <<TRACE_EVENT_FL_UPROBE_BIT ),TRACE_EVENT_FL_EPROBE = (1 <<TRACE_EVENT_FL_EPROBE_BIT ),TRACE_EVENT_FL_FPROBE = (1 <<TRACE_EVENT_FL_FPROBE_BIT ), +TRACE_EVENT_FL_WPROBE = (1 <<TRACE_EVENT_FL_WPROBE_BIT ),TRACE_EVENT_FL_CUSTOM = (1 <<TRACE_EVENT_FL_CUSTOM_BIT ), TRACE_EVENT_FL_TEST_STR = (1 << TRACE_EVENT_FL_TEST_STR_BIT), };
kernel/trace/Kconfig +13 -0
@@ -862,6 +862,19 @@ config EPROBE_EVENTS convert the type of an event field. For example, turn an address into a string. +config WPROBE_EVENTS + bool "Enable wprobe-based dynamic events" + depends on TRACING + depends on HAVE_HW_BREAKPOINT + select PROBE_EVENTS + select DYNAMIC_EVENTS + help + This allows the user to add watchpoint tracing events based on + hardware breakpoints on the fly via the ftrace interface. + + Those events can be inserted wherever hardware breakpoints can be + set, and record accessed memory address and values. + config BPF_EVENTS depends on BPF_SYSCALL depends on (KPROBE_EVENTS || UPROBE_EVENTS) && PERF_EVENTS
kernel/trace/Makefile +1 -0
@@ -126,6 +126,7 @@ obj-$(CONFIG_FTRACE_RECORD_RECURSION) += trace_recursion_record.o obj-$(CONFIG_FPROBE) += fprobe.o obj-$(CONFIG_RETHOOK) += rethook.o obj-$(CONFIG_FPROBE_EVENTS) += trace_fprobe.o +obj-$(CONFIG_WPROBE_EVENTS) += trace_wprobe.o obj-$(CONFIG_TRACEPOINT_BENCHMARK) += trace_benchmark.o obj-$(CONFIG_RV) += rv/
kernel/trace/trace.c +8 -1
@@ -4294,8 +4294,12 @@ static const char readme_msg[] =" uprobe_events\t\t- Create/append/remove/show the userspace dynamic events\n" "\t\t\t Write into this file to define/undefine new trace events.\n"#endif +#ifdef CONFIG_WPROBE_EVENTS +" wprobe_events\t\t- Create/append/remove/show the hardware breakpoint dynamic events\n" + "\t\t\t Write into this file to define/undefine new trace events.\n" +#endif #if defined(CONFIG_KPROBE_EVENTS ) || defined(CONFIG_UPROBE_EVENTS ) || \ - defined(CONFIG_FPROBE_EVENTS ) + defined(CONFIG_FPROBE_EVENTS ) || defined(CONFIG_WPROBE_EVENTS ) "\t accepts: event-definitions (one definition per line)\n"#if defined(CONFIG_KPROBE_EVENTS ) || defined(CONFIG_UPROBE_EVENTS )"\t Format: p[:[<group>/][<event>]] <place> [<args>]\n"
@@ -4305,6 +4309,9 @@ static const char readme_msg[] ="\t f[:[<group>/][<event>]] <func-name>[%return] [<args>]\n" "\t t[:[<group>/][<event>]] <tracepoint> [<args>]\n"#endif +#ifdef CONFIG_WPROBE_EVENTS + "\t w[:[<group>/][<event>]] [r|w|rw]@<addr>[:<len>] [<args>]\n" +#endif #ifdef CONFIG_HIST_TRIGGERS "\t s:[synthetic/]<event> <field> [<field>]\n"#endif
kernel/trace/trace.h +5 -0
@@ -179,6 +179,11 @@ struct fexit_trace_entry_head {unsigned long ret_ip; }; +struct wprobe_trace_entry_head { +struct trace_entry ent; +unsigned long ip; +}; +#define TRACE_BUF_SIZE 1024struct trace_array ;
kernel/trace/trace_probe.c +19 -2
@@ -1402,6 +1402,23 @@ static int parse_probe_vars(char *orig_arg, const struct fetch_type *t,return 0 ; } +/* wprobe only support "$addr" and "$value" variable */ +if (ctx->flags &TPARG_FL_WPROBE ) { +if (!strcmp (arg,"addr" )) { + code->op =FETCH_OP_BADDR ; +return 0 ; + } +if (!strcmp (arg,"value" )) { + code->op =FETCH_OP_BADDR ; + code++; + code->op =FETCH_OP_DEREF ; + code->offset =0 ; + *pcode = code; +return 0 ; + } + goto inval; + } +if (strcmp (arg,"comm" ) ==0 ||strcmp (arg,"COMM" ) ==0 ) { code->op =FETCH_OP_COMM ;return 0 ;
@@ -1461,8 +1478,8 @@ static int parse_probe_arg_register(char *arg, struct fetch_insn *code, {int ret; -if (ctx->flags & (TPARG_FL_TEVENT |TPARG_FL_FPROBE )) { -/* eprobe and fprobe do not handle registers */ +if (ctx->flags & (TPARG_FL_TEVENT |TPARG_FL_FPROBE |TPARG_FL_WPROBE )) { +/* eprobe, fprobe and wprobe do not handle registers */ trace_probe_log_err (ctx->offset,BAD_VAR );return -EINVAL ; }
kernel/trace/trace_probe.h +9 -1
@@ -90,6 +90,7 @@ typedef int (*print_type_func_t)(struct trace_seq *, void *, void *);FETCH_OP (STACK , param),/* Stack: .param = index */ \FETCH_OP (STACKP , none),/* Stack pointer */ \FETCH_OP (RETVAL , none),/* Return value */ \ +FETCH_OP (BADDR , none),/* Break address */ \FETCH_OP (IMM , imm),/* Immediate: .immediate */ \FETCH_OP (COMM , none),/* Current comm */ \ FETCH_OP(CURRENT, none), /* Current task_struct address */\
@@ -418,6 +419,7 @@ static inline int traceprobe_get_entry_data_size(struct trace_probe *tp)#define TPARG_FL_USER BIT(4)#define TPARG_FL_FPROBE BIT(5)#define TPARG_FL_TPOINT BIT(6) +#define TPARG_FL_WPROBE BIT(7)#define TPARG_FL_LOC_MASK GENMASK(4, 0)static inline bool tparg_is_function_entry (unsigned int flags)
@@ -544,6 +546,10 @@ extern int traceprobe_define_arg_fields(struct trace_event_call *event_call,C (ARG_TOO_LONG ,"Argument expression is too long" ), \C (ARRAY_NO_CLOSE ,"Array is not closed" ), \C (ARRAY_TOO_BIG ,"Array number is too big" ), \ +C (BAD_ACCESS_ADDR ,"Invalid access memory address" ), \ +C (BAD_ACCESS_FMT ,"Access memory address requires @" ), \ +C (BAD_ACCESS_LEN ,"This memory access length is not supported" ), \ +C (BAD_ACCESS_TYPE ,"Bad memory access type" ), \C (BAD_ADDR_SUFFIX ,"Invalid probed address suffix" ), \C (BAD_ARG_NAME ,"Argument name must follow the same rules as C identifiers" ), \ C(BAD_ARG_NUM, "Invalid argument number"), \
@@ -626,7 +632,9 @@ extern int traceprobe_define_arg_fields(struct trace_event_call *event_call,C (TYPECAST_NOT_EVENT ,"Typecasts are only for eprobe fields" ), \C (TYPECAST_REQ_FIELD ,"Typecast requires a field access" ), \C (TYPECAST_SYM_OFFSET ,"@SYM+/-OFFSET with typecast needs parentheses" ), \ -C (USED_ARG_NAME ,"This argument name is already used" ), +C (USED_ARG_NAME ,"This argument name is already used" ), \ +C (WPROBE_NO_MAXACT ,"Watchpoint probe does not support maxactive" ), \ +C (WPROBE_NO_SIBLING ,"Watchpoint probe does not support sibling probes" ), #undef C #define C(a, b) TP_ERR_##a
kernel/trace/trace_wprobe.c new file +758 -0
758 lines not on this page · raw message
[email protected]
AI reviewRe: [PATCH v13 05/12] tracing/wprobe: Add wprobe (watchpoint probe) trace event support
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] The trace_wprobe_show function uses %pS to format the symbol address, breaking dynamic event…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] The trace_wprobe_show function uses %pS to format the symbol address, breaking dynamic event restoration because kstrtoint() in parse_address_spec cannot parse the appended symbol size.
- [Medium] Address validation in parse_address_spec incorrectly exempts address 0 from the kernel space TASK_SIZE check, allowing user-space tracing at address 0. --
--- Patch [5]: [PATCH v13 05/12] tracing/wprobe: Add wprobe (watchpoint probe) trace event support --- commit 8eff80da8ae4225daa9b5646b430ded1a73ae7ae
Author: Masami Hiramatsu (Google) <[email protected]>
tracing/wprobe: Add wprobe (watchpoint probe) trace event support
Add hardware-breakpoint-based dynamic trace event support (wprobe). Wprobe creates a dynamic event on data read/write accesses using hardware breakpoints and logs the access context and fetchargs.
Quoted 5 lines
kernel/trace/trace_wprobe.c new file +0
-0
[ ... ]
Quoted 3 lines
kernel/trace/trace_wprobe.c new file +3
-0+static int parse_address_spec (const char *spec,unsigned long *addr,int *type, +int *len,char **symbol) +{
[ ... ]
Quoted 13 lines
kernel/trace/trace_wprobe.c new file +13
-0+if (kstrtoul (at +1 ,0 , &_addr) !=0 ) { +char *off_str =strpbrk (at +1 ,"+-" ); +int offset =0 ; + +if (off_str) { +if (kstrtoint (off_str,0 , &offset) !=0 ) { +trace_probe_log_err (off_str - _spec,BAD_PROBE_ADDR ); +return -EINVAL ; + } + *off_str ='\0' ; + } + _addr =kallsyms_lookup_name (at +1 ); +if (!_addr) {
kernel/trace/trace_wprobe.c line 582
Quoted 10 lines
kernel/trace/trace_wprobe.c new file +10
-0+trace_probe_log_err (at +1 - _spec,BAD_ACCESS_ADDR ); +return -ENOENT ; + } + _addr += offset; + *symbol =kstrdup (at +1 ,GFP_KERNEL ); +if (!*symbol) +return -ENOMEM ; + } + +if (_addr !=0 && _addr <TASK_SIZE ) {
[Severity: Medium]
Does this allow user-space tracing at address 0?
The condition explicitly exempts _addr == 0. If a user specifies an address of 0, it appears this would bypass the TASK_SIZE check and successfully allow setting a hardware breakpoint in user space.
Quoted 9 lines
kernel/trace/trace_wprobe.c new file +9
-0+trace_probe_log_err (at +1 - _spec,BAD_ACCESS_ADDR ); +return -EINVAL ; + } + + *addr = _addr; + *type = _type; + *len = _len; +return 0 ; +}
[ ... ]
Quoted 2 lines
kernel/trace/trace_wprobe.c new file +2
-0+static int trace_wprobe_show (struct seq_file *m,struct dyn_event *ev) +{
[ ... ]
kernel/trace/trace_wprobe.c line 742
Quoted 11 lines
kernel/trace/trace_wprobe.c new file +11
-0+if (tw->len ==HW_BREAKPOINT_LEN_1 ) + len =1 ; +else if (tw->len ==HW_BREAKPOINT_LEN_2 ) + len =2 ; +else if (tw->len ==HW_BREAKPOINT_LEN_4 ) + len =4 ; +else + len =8 ; + +if (tw->symbol) +seq_printf (m," %s@%pS:%d" , type_str, (void *)tw->addr, len);
[Severity: High]
Will formatting the address with %pS break dynamic event restoration?
Using %pS appends the symbol size to the output string, which results in formats like symbol+0x0/0x100.
When this string is saved and written back to restore the event, parse_address_spec() extracts the offset part (+0x0/0x100) and passes it to kstrtoint(). Since kstrtoint() requires a strict integer format, won't it fail on the /size suffix and return -EINVAL, preventing restoration?
Quoted 9 lines
kernel/trace/trace_wprobe.c new file +9
-0+else +seq_printf (m," %s@0x%lx:%d" , type_str, tw->addr, len); + +for (i =0 ; i < tw->tp.nr_args; i++) +seq_printf (m," %s=%s" , tw->tp.args[i].name, tw->tp.args[i].comm); +seq_putc (m,'\n' ); + +return 0 ; +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/178739053919.1520941.17662338993878200834.stgit@devnote2?part=5
Masami Hiramatsu (Google)
maintainerRe: [PATCH v13 05/12] tracing/wprobe: Add wprobe (watchpoint probe) trace event support
Quoted 2 lines · On Sat, 22 Aug 2026 09:49:13 +0000 [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Thetrace_wprobe_showfunction uses %pS to format the symbol address, breaking dynamic event restoration becausekstrtoint()inparse_address_speccannot parse the appended symbol size.
Oops, it is my fault. Let me fix it.
Quoted 1 line
- [Medium] Address validation in
parse_address_specincorrectly exempts address 0 from the kernel spaceTASK_SIZEcheck, allowing user-space tracing at address 0.
Ah, address 0 will hit in the userspace. OK, that should be fixed.
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Masami Hiramatsu (Google)
maintainerPATCH v13 6/12 x86: hw_breakpoint: Add a kconfig to clarify when a breakpoint fires
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
Add CONFIG_HAVE_POST_BREAKPOINT_HOOK which indicates the hw_breakpoint on that architecture fires after the target memory has been modified. This is currently x86 only behavior.
---
Changes in v11:
- Move select to alphabetically sorted place.
--- arch/Kconfig | 10 ++++++++++ arch/x86/Kconfig | 1 + kernel/trace/Kconfig | 1 + 3 files changed, 12 insertions(+)
arch/Kconfig +10 -0
@@ -457,6 +457,16 @@ config HAVE_MIXED_BREAKPOINTS_REGS Select this option if your arch implements breakpoints under the latter fashion. +config HAVE_POST_BREAKPOINT_HOOK + bool + depends on HAVE_HW_BREAKPOINT + help + Depending on the arch implementation of hardware breakpoints, + some of them provide breakpoint hook after the target memory + is modified. + Select this option if your arch implements breakpoints overflow + handler hooks after the target memory is modified. + config HAVE_MODIFY_LOCAL_HW_BREAKPOINT_ADDR bool depends on HAVE_HW_BREAKPOINT
arch/x86/Kconfig +1 -0
@@ -287,6 +287,7 @@ config X86 select MMU_GATHER_RCU_TABLE_FREE select MMU_GATHER_MERGE_VMAS select HAVE_POSIX_CPU_TIMERS_TASK_WORK + select HAVE_POST_BREAKPOINT_HOOK select HAVE_REGS_AND_STACK_ACCESS_API select HAVE_RELIABLE_STACKTRACE if UNWINDER_ORC || STACK_VALIDATION select HAVE_FUNCTION_ARG_ACCESS_API
kernel/trace/Kconfig +1 -0
@@ -866,6 +866,7 @@ config WPROBE_EVENTS bool "Enable wprobe-based dynamic events" depends on TRACING depends on HAVE_HW_BREAKPOINT + depends on HAVE_POST_BREAKPOINT_HOOK select PROBE_EVENTS select DYNAMIC_EVENTS help
[email protected]
AI reviewRe: [PATCH v13 06/12] x86: hw_breakpoint: Add a kconfig to clarify when a breakpoint fires
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines
From: Masami Hiramatsu (Google) <[email protected]>
Add
CONFIG_HAVE_POST_BREAKPOINT_HOOKwhich indicates thehw_breakpointon that architecture fires after the target memory has been modified. This is currently x86 only behavior.Signed-off-by Masami Hiramatsu (Google) <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/178739053919.1520941.17662338993878200834.stgit@devnote2?part=6
Masami Hiramatsu (Google)
maintainerPATCH v13 7/12 selftests: tracing: Add a basic testcase for wprobe
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
Add 'add_remove_wprobe.tc' testcase for testing wprobe event that tests adding and removing operations of the wprobe event.
---
Changes in v9:
- Fix command check logic to prevent early exit under 'set -e' (errexit) when grep or test fails.
- Simplify enable/disable status checks by removing cat pipes.
Changes in v8:
- Fixed silently test failure path.
--- tools/testing/selftests/ftrace/config | 1 .../ftrace/test.d/dynevent/add_remove_wprobe.tc | 63 ++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 tools/testing/selftests/ftrace/test.d/dynevent/add_remove_wprobe.tc
tools/testing/selftests/ftrace/config +1 -0
@@ -27,3 +27,4 @@ CONFIG_STACK_TRACER=y CONFIG_TRACER_SNAPSHOT=y CONFIG_UPROBES=y CONFIG_UPROBE_EVENTS=y +CONFIG_WPROBE_EVENTS=y
tools/testing/selftests/ftrace/test.d/dynevent/add_remove_wprobe.tc new file +63 -0
63 lines not on this page · raw message
[email protected]
AI reviewRe: [PATCH v13 07/12] selftests: tracing: Add a basic testcase for wprobe
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Unreachable error handling logic due to early exit under 'set -e'. --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Unreachable error handling logic due to early exit under 'set -e'. --
--- Patch [7]: [PATCH v13 07/12] selftests: tracing: Add a basic testcase for wprobe --- commit 6d72a05750c5dfc08de6b03a6fc65e5144b20b9f
Author: Masami Hiramatsu (Google) <[email protected]>
selftests: tracing: Add a basic testcase for wprobe
Add an add_remove_wprobe.tc testcase for testing wprobe events that tests adding and removing operations.
Quoted 12 lines
tools/testing/selftests/ftrace/test.d/dynevent/add_remove_wprobe.tc new file +7
-0@@ -0,0 +1,63 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 +# description: Generic dynamic event - add/remove wprobe events +# requires: dynamic_events "w[:[<group>/][<event>]] [r|w|rw]@<addr>[:<len>]":README + +echo 0 > events/enable +echo > dynamic_events
tools/testing/selftests/ftrace/test.d/dynevent/add_remove_wprobe.tc line 17
Quoted 10 lines
tools/testing/selftests/ftrace/test.d/dynevent/add_remove_wprobe.tc new file +10
-0+ +# Use jiffies as a variable that is frequently written to. +TARGET=jiffies + +echo "w:my_wprobe w@$TARGET" >> dynamic_events + +if ! grep -q my_wprobe dynamic_events; then + echo "Failed to create wprobe event" + exit_fail +fi
[Severity: Medium]
Since the ftracetest framework runs these scripts with set -e (errexit) enabled, will this script exit immediately if the kernel rejects the event creation and the echo command fails?
If so, the subsequent diagnostic grep check and exit_fail call wouldn't be reached, leading to a silent failure without the intended error message.
tools/testing/selftests/ftrace/test.d/dynevent/add_remove_wprobe.tc line 30
Quoted 13 lines
tools/testing/selftests/ftrace/test.d/dynevent/add_remove_wprobe.tc new file +13
-0+ +if [ ! -d events/wprobes/my_wprobe ]; then + echo "Failed to create wprobe event directory" + exit_fail +fi + +echo 1 > events/wprobes/my_wprobe/enable + +# Check if the event is enabled +if ! grep -q 1 events/wprobes/my_wprobe/enable; then + echo "Failed to enable wprobe event" + exit_fail +fi
[Severity: Medium]
Similarly, if enabling the wprobe fails at the sysfs boundary, would this echo cause an early exit and bypass the diagnostic error message here as well?
[ ... ]
tools/testing/selftests/ftrace/test.d/dynevent/add_remove_wprobe.tc line 54
Quoted 6 lines
tools/testing/selftests/ftrace/test.d/dynevent/add_remove_wprobe.tc new file +6
-0+echo "-:my_wprobe" >> dynamic_events + +if grep -q my_wprobe dynamic_events; then + echo "Failed to remove wprobe event" + exit_fail +fi
[Severity: Medium]
Could this echo also trigger an early exit if removing the wprobe fails, bypassing the failure message?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/178739053919.1520941.17662338993878200834.stgit@devnote2?part=7
Masami Hiramatsu (Google)
maintainerRe: [PATCH v13 07/12] selftests: tracing: Add a basic testcase for wprobe
Quoted 41 lines · On Sat, 22 Aug 2026 09:36:38 +0000 [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Unreachable error handling logic due to early exit under 'set -e'. ----- Patch [7]: [PATCH v13 07/12] selftests: tracing: Add a basic testcase for wprobe --- commit
6d72a05750c5dfc08de6b03a6fc65e5144b20b9f
Author: Masami Hiramatsu (Google) <[email protected]>selftests: tracing: Add a basic testcase for wprobe
Add an add_remove_wprobe.tc testcase for testing wprobe events that tests adding and removing operations.
Quoted 12 lines
tools/testing/selftests/ftrace/test.d/dynevent/add_remove_wprobe.tc new file +7
-0@@ -0,0 +1,63 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 +# description: Generic dynamic event - add/remove wprobe events +# requires: dynamic_events "w[:[<group>/][<event>]] [r|w|rw]@<addr>[:<len>]":README + +echo 0 > events/enable +echo > dynamic_eventstools/testing/selftests/ftrace/test.d/dynevent/add_remove_wprobe.tc line 17
Quoted 10 lines
tools/testing/selftests/ftrace/test.d/dynevent/add_remove_wprobe.tc new file +10
-0+ +# Use jiffies as a variable that is frequently written to. +TARGET=jiffies + +echo "w:my_wprobe w@$TARGET" >> dynamic_events + +if ! grep -q my_wprobe dynamic_events; then + echo "Failed to create wprobe event" + exit_fail +fi[Severity: Medium]
Since the ftracetest framework runs these scripts with set -e (errexit) enabled, will this script exit immediately if the kernel rejects the event creation and the echo command fails?
Quoted 2 lines
If so, the subsequent diagnostic grep check and
exit_failcall wouldn't be reached, leading to a silent failure without the intended error message.
This is expected, the later grep checks whether if creation command succeeded but the event is not shown in the dynamic_events. Different check.
Quoted 9 lines
Quoted 9 lines
tools/testing/selftests/ftrace/test.d/dynevent/add_remove_wprobe.tc new file +9
-0+ +if [ ! -d events/wprobes/my_wprobe ]; then + echo "Failed to create wprobe event directory" + exit_fail +fi + +echo 1 > events/wprobes/my_wprobe/enable + +# Check if the event is enabled
Quoted 8 lines
tools/testing/selftests/ftrace/test.d/dynevent/add_remove_wprobe.tc line 30
Quoted 4 lines
tools/testing/selftests/ftrace/test.d/dynevent/add_remove_wprobe.tc new file +4
-0+if ! grep -q 1 events/wprobes/my_wprobe/enable; then + echo "Failed to enable wprobe event" + exit_fail +fi[Severity: Medium]
Similarly, if enabling the wprobe fails at the sysfs boundary, would this echo cause an early exit and bypass the diagnostic error message here as well?
Also, this is a kind of double check. Even if writing 1 to enable succeeded, it is failed to set internal flag. This checks such problem.
Quoted 13 lines
[ ... ]
tools/testing/selftests/ftrace/test.d/dynevent/add_remove_wprobe.tc line 54
Quoted 6 lines
tools/testing/selftests/ftrace/test.d/dynevent/add_remove_wprobe.tc new file +6
-0+echo "-:my_wprobe" >> dynamic_events + +if grep -q my_wprobe dynamic_events; then + echo "Failed to remove wprobe event" + exit_fail +fi[Severity: Medium]
Could this echo also trigger an early exit if removing the wprobe fails, bypassing the failure message?
Ditto.
Thanks,
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Masami Hiramatsu (Google)
maintainerPATCH v13 8/12 selftests: tracing: Add syntax testcase for wprobe
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
Add "wprobe_syntax_errors.tc" testcase for testing syntax errors of the watch probe events.
---
Changes in v12:
- Add NO_ARG_BODY error case.
Changes in v11:
- Add WPROBE_NO_MAXACT syntax error case.
--- .../test.d/dynevent/wprobes_syntax_errors.tc | 22 ++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 tools/testing/selftests/ftrace/test.d/dynevent/wprobes_syntax_errors.tc
tools/testing/selftests/ftrace/test.d/dynevent/wprobes_syntax_errors.tc new file +22 -0
@@ -0,0 +1,22 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 +# description: Watch probe event parser error log check +# requires: dynamic_events "w[:[<group>/][<event>]] [r|w|rw]@<addr>[:<len>]":README + +check_error() { # command-with-error-pos-by-^ + ftrace_errlog_check 'wprobe' "$1" 'dynamic_events' +} + +check_error '^w' # NO_ARG_BODY +check_error 'w^10 w@jiffies' # WPROBE_NO_MAXACT +check_error 'w ^symbol' # BAD_ACCESS_FMT +check_error 'w ^a@symbol' # BAD_ACCESS_TYPE +check_error 'w w@^symbol' # BAD_ACCESS_ADDR +check_error 'w w@jiffies^+offset' # BAD_ACCESS_ADDR +check_error 'w w@jiffies:^100' # BAD_ACCESS_LEN +check_error 'w w@jiffies ^$arg1' # BAD_VAR +check_error 'w w@jiffies ^$retval' # BAD_VAR +check_error 'w w@jiffies ^$stack' # BAD_VAR +check_error 'w w@jiffies ^%ax' # BAD_VAR + +exit 0
[email protected]
AI reviewRe: [PATCH v13 08/12] selftests: tracing: Add syntax testcase for wprobe
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The test comment `# BAD_ACCESS_ADDR` does not match the actual error logged by the kernel, which is…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The test comment # BAD_ACCESS_ADDR does not match the actual error logged by the kernel, which is BAD_PROBE_ADDR. --
--- Patch [8]: [PATCH v13 08/12] selftests: tracing: Add syntax testcase for wprobe --- commit fc72e33e91d484bd38ebdde9110dabef02740205
Author: Masami Hiramatsu (Google) <[email protected]>
selftests: tracing: Add syntax testcase for wprobe
This commit adds a new testcase, wprobe_syntax_errors.tc, to verify the syntax error handling of watch probe events.
Quoted 4 lines
tools/testing/selftests/ftrace/test.d/dynevent/wprobes_syntax_errors.tc new file +0
-0
[ ... ]
tools/testing/selftests/ftrace/test.d/dynevent/wprobes_syntax_errors.tc line 15
Quoted 6 lines
tools/testing/selftests/ftrace/test.d/dynevent/wprobes_syntax_errors.tc new file +6
-0+check_error '^w' # NO_ARG_BODY +check_error 'w^10 w@jiffies' # WPROBE_NO_MAXACT +check_error 'w ^symbol' # BAD_ACCESS_FMT +check_error 'w ^a@symbol' # BAD_ACCESS_TYPE +check_error 'w w@^symbol' # BAD_ACCESS_ADDR +check_error 'w w@jiffies^+offset' # BAD_ACCESS_ADDR
[Severity: Low]
This isn't a bug, but should this comment say BAD_PROBE_ADDR instead of BAD_ACCESS_ADDR?
Looking at parse_address_spec() in kernel/trace/trace_wprobe.c, when the offset fails to parse, it logs BAD_PROBE_ADDR rather than BAD_ACCESS_ADDR:
if (kstrtoint(off_str, 0, &offset) != 0) {
trace_probe_log_err(off_str - _spec, BAD_PROBE_ADDR);
return -EINVAL;
}Could the comment in the test script be updated to match the actual error constant logged by the kernel?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/178739053919.1520941.17662338993878200834.stgit@devnote2?part=8
Masami Hiramatsu (Google)
maintainerRe: [PATCH v13 08/12] selftests: tracing: Add syntax testcase for wprobe
Quoted 18 lines · On Sat, 22 Aug 2026 09:35:02 +0000 [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The test comment# BAD_ACCESS_ADDRdoes not match the actual error logged by the kernel, which isBAD_PROBE_ADDR. ----- Patch [8]: [PATCH v13 08/12] selftests: tracing: Add syntax testcase for wprobe --- commit
fc72e33e91d484bd38ebdde9110dabef02740205
Author: Masami Hiramatsu (Google) <[email protected]>selftests: tracing: Add syntax testcase for wprobe
This commit adds a new testcase, wprobe_syntax_errors.tc, to verify the syntax error handling of watch probe events.
Quoted 4 lines
tools/testing/selftests/ftrace/test.d/dynevent/wprobes_syntax_errors.tc new file +0
-0[ ... ]
Quoted 10 lines
tools/testing/selftests/ftrace/test.d/dynevent/wprobes_syntax_errors.tc line 15
Quoted 6 lines
tools/testing/selftests/ftrace/test.d/dynevent/wprobes_syntax_errors.tc new file +6
-0+check_error '^w' # NO_ARG_BODY +check_error 'w^10 w@jiffies' # WPROBE_NO_MAXACT +check_error 'w ^symbol' # BAD_ACCESS_FMT +check_error 'w ^a@symbol' # BAD_ACCESS_TYPE +check_error 'w w@^symbol' # BAD_ACCESS_ADDR +check_error 'w w@jiffies^+offset' # BAD_ACCESS_ADDR[Severity: Low]
This isn't a bug, but should this comment sayBAD_PROBE_ADDRinstead ofBAD_ACCESS_ADDR?
Ah, good catch! ;)
Thanks!
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Masami Hiramatsu (Google)
maintainerPATCH v13 9/12 tracing/wprobe: Add set_wprobe and clear_wprobe event triggers
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
Add set_wprobe and clear_wprobe event triggers to dynamically attach and detach hardware breakpoint address monitoring based on event field contents.
---
Changes in v13:
- Align WPROBE_DEFAULT_CLEAR_ADDRESS to 8 bytes using dedicated u64.
- Remove manual count decrement to rely on core event_trigger_count().
- Parse optional FIELD argument in clear_wprobe command.
- Fix trigger reference count leak and error cleanup in command parsing.
- Update document to add [:COUNT] to trigger syntax and fix adjust forms.
- Add notes about the exclusive trigger setting behavior.
- Remove unused dyn_event_ops_mutex.
Changes in v12:
- Decrement trigger data->count only when watchpoint state is actually changed.
- Remove dyn_event_ops_mutex to fix lockdep circular dependency deadlock.
- Add event_trigger_init() call to prevent premature freeing of trigger_data.
--- Documentation/trace/wprobetrace.rst | 107 +++++++ include/linux/trace_events.h | 1 kernel/trace/Kconfig | 10 + kernel/trace/trace.h | 1 kernel/trace/trace_events_trigger.c | 2 kernel/trace/trace_probe.c | 2 kernel/trace/trace_probe.h | 9 - kernel/trace/trace_wprobe.c | 569 +++++++++++++++++++++++++++++++++++ 8 files changed, 698 insertions(+), 3 deletions(-)
Documentation/trace/wprobetrace.rst +107 -0
@@ -76,3 +76,110 @@ Wprobe event does not disable itself even if the module is unloaded. For example, if you add a wprobe event on a module variable, and then unload the module, the wprobe event will still be enabled. This is for watching the address is used unexpectedly after the module is unloaded. + +Combination with trigger action +------------------------------- +The event trigger action can extend the utilization of this wprobe. + +- set_wprobe:WPEVENT:FIELD[+|-ADJUST][:COUNT] +- clear_wprobe:WPEVENT[:FIELD[+|-ADJUST][:COUNT]] + +Set these triggers to the target event, then the WPROBE event will be +setup to trace the memory access at FIELD[+|-ADJUST] address. +When clear_wprobe is hit, if FIELD is NOT specified, the WPEVENT is +forcibly cleared. If FIELD[+|-ADJUST] is set, it clears WPEVENT only +if its watching address is the same as the FIELD[+|-ADJUST] value. +If COUNT is specified, it will set/clear WPEVENT only if it hits COUNT +times. + +Notes: +- set_wprobe only works on the wprobe which is NOT set a valid address yet, + and it must be enabled after the trigger is set. +- clear_wprobe only works on the wprobe which is set a valid address, and it + will be soft-disabled after the trigger is cleared. +- Therefore, if a trigger sets/clears a wprobe, other/same trigger events + will not work (on the same event) while the wprobe is set. + +The set_wprobe trigger does not change the type and length, these +must be set when creating a new wprobe. + +The WPROBE event must be disabled when setting the new trigger +and it will be busy afterwards. Recommended usage is to add a new +wprobe at NULL address and keep disabled. + +Wprobe triggers only support target addresses in kernel memory. If a +set_wprobe trigger evaluates to a user-space memory address or NULL +pointer, the trigger action ignores the update and skips setting the +watchpoint. + +Wprobe triggers are not supported on kprobe_events, because kprobes +themselves can use software breakpoints which conflicts with wprobe +operation. + + +For example, trace the first 8 bytes of the dentry data structure passed +to do_truncate() until it is deleted by dentry_kill(). +(Note: all tracefs setup uses '>>' so that it does not kick do_truncate()) +:: + + # echo 'w:watch rw@0:8 address=$addr value=+0($addr)' >> dynamic_events + # echo 'f:truncate do_truncate dentry=$arg2' >> dynamic_events + # echo 'set_wprobe:watch:dentry' >> events/fprobes/truncate/trigger + # echo 'f:dentry_kill dentry_kill dentry=$arg1' >> dynamic_events + # echo 'clear_wprobe:watch:dentry' >> events/fprobes/dentry_kill/trigger + # echo 1 >> events/fprobes/truncate/enable + # echo 1 >> events/fprobes/dentry_kill/enable + + # echo aaa > /tmp/hoge + # echo bbb > /tmp/hoge + # echo ccc > /tmp/hoge + # rm /tmp/hoge + +Then, the trace data will show:: + + # tracer: nop + # + # entries-in-buffer/entries-written: 32/32 #P:8 + # + # _-----=> irqs-off/BH-disabled + # / _----=> need-resched + # | / _---=> hardirq/softirq + # || / _--=> preempt-depth + # ||| / _-=> migrate-disable + # |||| / delay + # TASK-PID CPU# ||||| TIMESTAMP FUNCTION + # | | | ||||| | | + sh-107 [004] ...1. 9.990418: dentry_kill: (dentry_kill+0x0/0x2c0) dentry=0xffff888004ad6618 + sh-107 [004] ...1. 9.990914: dentry_kill: (dentry_kill+0x0/0x2c0) dentry=0xffff888004b3de78 + sh-107 [004] ...1. 9.993175: dentry_kill: (dentry_kill+0x0/0x2c0) dentry=0xffff8880049ddd40 + sh-107 [004] ..... 9.995198: truncate: (do_truncate+0x4/0x120) dentry=0xffff8880048083a8 + sh-107 [004] ...1. 9.995389: dentry_kill: (dentry_kill+0x0/0x2c0) dentry=0xffff8880049db998 + sh-107 [004] ..Zff 9.997503: watch: (lookup_fast+0xaa/0x150) address=0xffff8880048083a8 value=0x8200080 + sh-107 [004] ..Zff 9.997509: watch: (path_openat+0x211/0xda0) address=0xffff8880048083a8 value=0x8200080 + sh-107 [004] ..Zff 9.997514: watch: (path_openat+0xa56/0xda0) address=0xffff8880048083a8 value=0x8200080 + sh-107 [004] ..Zff 9.997518: watch: (path_openat+0xae2/0xda0) address=0xffff8880048083a8 value=0x8200080 + sh-107 [004] ..... 9.997521: truncate: (do_truncate+0x4/0x120) dentry=0xffff8880048083a8 + sh-107 [004] ...1. 9.997582: dentry_kill: (dentry_kill+0x0/0x2c0) dentry=0xffff888004808270 + sh-107 [004] ...1. 9.999365: dentry_kill: (dentry_kill+0x0/0x2c0) dentry=0xffff8880049db728 + sh-107 [004] ...1. 9.999388: dentry_kill: (dentry_kill+0x0/0x2c0) dentry=0xffff888004b1c000 + rm-113 [005] ..Zff 10.000965: watch: (lookup_fast+0xaa/0x150) address=0xffff8880048083a8 value=0x8200080 + rm-113 [005] ..Zff 10.000971: watch: (path_lookupat+0x97/0x1e0) address=0xffff8880048083a8 value=0x8200080 + rm-113 [005] ..Zff 10.000984: watch: (lookup_fast+0xaa/0x150) address=0xffff8880048083a8 value=0x8200080 + rm-113 [005] ..Zff 10.000988: watch: (path_lookupat+0x97/0x1e0) address=0xffff8880048083a8 value=0x8200080 + rm-113 [005] ..Zff 10.001010: watch: (lookup_one_qstr_excl+0x28/0x140) address=0xffff8880048083a8 value=0x8200080 + rm-113 [005] ..Zff 10.001014: watch: (lookup_one_qstr_excl+0xd1/0x140) address=0xffff8880048083a8 value=0x8200080 + rm-113 [005] ..Zff 10.001018: watch: (may_delete_dentry+0x1c/0x200) address=0xffff8880048083a8 value=0x8200080 + rm-113 [005] ..Zff 10.001021: watch: (may_delete_dentry+0x195/0x200) address=0xffff8880048083a8 value=0x8200080 + rm-113 [005] ..Zff 10.001031: watch: (vfs_unlink+0x5e/0x260) address=0xffff8880048083a8 value=0x8200080 + rm-113 [005] d.Z.. 10.001067: watch: (d_make_discardable+0x1b/0x40) address=0xffff8880048083a8 value=0x8200080 + rm-113 [005] d.Z.. 10.001071: watch: (d_make_discardable+0x29/0x40) address=0xffff8880048083a8 value=0x200080 + rm-113 [005] ...1. 10.001072: dentry_kill: (dentry_kill+0x0/0x2c0) dentry=0xffff8880048083a8 + rm-113 [005] ...1. 10.001218: dentry_kill: (dentry_kill+0x0/0x2c0) dentry=0xffff8880048083a8 + sh-107 [004] ...1. 10.001416: dentry_kill: (dentry_kill+0x0/0x2c0) dentry=0xffff8880049db110 + sh-107 [004] ...1. 10.001444: dentry_kill: (dentry_kill+0x0/0x2c0) dentry=0xffff8880049db248 + sh-107 [004] ...1. 10.001500: dentry_kill: (dentry_kill+0x0/0x2c0) dentry=0xffff888004ad6618 + sh-107 [004] ...1. 10.002067: dentry_kill: (dentry_kill+0x0/0x2c0) dentry=0xffff888004b41e78 + sh-107 [004] ...1. 10.904920: dentry_kill: (dentry_kill+0x0/0x2c0) dentry=0xffff888004b41e78 + sh-107 [004] ...1. 10.905129: dentry_kill: (dentry_kill+0x0/0x2c0) dentry=0xffff888004ad6618 + +You can see the watch event is correctly configured on the dentry.
include/linux/trace_events.h +1 -0
@@ -738,6 +738,7 @@ enum event_trigger_type {ETT_EVENT_HIST = (1 <<4 ),ETT_HIST_ENABLE = (1 <<5 ),ETT_EVENT_EPROBE = (1 <<6 ), + ETT_EVENT_WPROBE = (1 << 7), };extern int filter_match_preds (struct event_filter *filter,void *rec);
kernel/trace/Kconfig +10 -0
@@ -876,6 +876,16 @@ config WPROBE_EVENTS Those events can be inserted wherever hardware breakpoints can be set, and record accessed memory address and values. +config WPROBE_TRIGGERS + depends on WPROBE_EVENTS + depends on HAVE_MODIFY_LOCAL_HW_BREAKPOINT_ADDR + bool + default y + help + This adds an event trigger which will set the wprobe on a specific + field of an event. This allows user to trace the memory access of + an address pointed by the event field. + config BPF_EVENTS depends on BPF_SYSCALL depends on (KPROBE_EVENTS || UPROBE_EVENTS) && PERF_EVENTS
kernel/trace/trace.h +1 -0
@@ -1983,6 +1983,7 @@ trigger_data_alloc(struct event_command *cmd_ops, char *cmd, char *param, void *private_data);extern void trigger_data_free (struct event_trigger_data *data);extern int event_trigger_init (struct event_trigger_data *data); +extern void event_trigger_free (struct event_trigger_data *data);extern int trace_event_trigger_enable_disable (struct trace_event_file *file,int trigger_enable);extern void update_cond_flag (struct trace_event_file *file);
kernel/trace/trace_events_trigger.c +1 -1
@@ -589,7 +589,7 @@ int event_trigger_init(struct event_trigger_data *data)* Usually used directly as the @free method in event trigger * implementations. */ -static void +void event_trigger_free (struct event_trigger_data *data) {if (WARN_ON_ONCE (data->ref <=0 ))
kernel/trace/trace_probe.c +1 -1
@@ -20,7 +20,7 @@#undef C#define C (a, b) b -static const char *trace_probe_err_text[] = {ERRORS }; +const char *trace_probe_err_text[] = {ERRORS };static const char *reserved_field_names[] = {"common_type" ,
kernel/trace/trace_probe.h +8 -1
@@ -634,7 +634,12 @@ extern int traceprobe_define_arg_fields(struct trace_event_call *event_call,C (TYPECAST_SYM_OFFSET ,"@SYM+/-OFFSET with typecast needs parentheses" ), \C (USED_ARG_NAME ,"This argument name is already used" ), \C (WPROBE_NO_MAXACT ,"Watchpoint probe does not support maxactive" ), \ -C (WPROBE_NO_SIBLING ,"Watchpoint probe does not support sibling probes" ), +C (WPROBE_NO_SIBLING ,"Watchpoint probe does not support sibling probes" ), \ +C (WPROBE_ON_KPROBE ,"Wprobe trigger is not supported on kprobe event" ), \ +C (WPROBE_NOT_FOUND ,"Target wprobe event is not found" ), \ +C (WPROBE_BUSY ,"Target wprobe event is already enabled" ), \ +C (WPROBE_NEED_FIELD ,"Wprobe trigger requires a target field" ), \ +C (WPROBE_BAD_FIELD ,"Target field must be pointer size" ), #undef C #define C(a, b) TP_ERR_##a
@@ -658,6 +663,8 @@ void __trace_probe_log_err(int offset, int err); DEFINE_FREE(trace_probe_log_clear, const char *, if (_T) trace_probe_log_clear()) +extern const char *trace_probe_err_text[]; +#define trace_probe_log_err (offs, err) \ __trace_probe_log_err(offs, TP_ERR_##err)
kernel/trace/trace_wprobe.c +569 -0
616 lines not on this page · raw message
[email protected]
AI reviewRe: [PATCH v13 09/12] tracing/wprobe: Add set_wprobe and clear_wprobe event triggers
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Medium] Leaked `trigger_data` and `wprobe_data` when trigger registration fails due to unbalanced…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Medium] Leaked trigger_data and wprobe_data when trigger registration fails due to unbalanced reference counts.
- [Medium] Hardware breakpoints are inadvertently left monitoring the initial wprobe address (unparked) instead of the parked dummy address upon enablement.
- [Low] The clear_wprobe trigger cannot be removed by echoing its exact printed output if a trigger count is specified.
--
--- Patch [9]: [PATCH v13 09/12] tracing/wprobe: Add set_wprobe and clear_wprobe event triggers --- commit e794ff7412c73348b335e9a50aceecbbd82d9a0c
Author: Masami Hiramatsu (Google) <[email protected]>
tracing/wprobe: Add set_wprobe and clear_wprobe event triggers
Add set_wprobe and clear_wprobe event triggers to dynamically attach and detach hardware breakpoint address monitoring based on event field contents.
Quoted 4 lines
kernel/trace/trace_wprobe.c +0
-0
[ ... ]
Quoted 1 line
kernel/trace/trace_wprobe.c +0
-0@@ -756,3 +827,501 @@ static __init int init_wprobe_trace(void)
[ ... ]
Quoted 5 lines
kernel/trace/trace_wprobe.c +5
-0+static int wprobe_trigger_cmd_parse (struct event_command *cmd_ops, +struct trace_event_file *file, +char *glob,char *cmd, +char *param_and_filter) +{
[ ... ]
kernel/trace/trace_wprobe.c static __init int init_wprobe_trace(void) · line 1112
Quoted 4 lines
kernel/trace/trace_wprobe.c +4
-0+ }else if (param && (isalpha (param[0 ]) || param[0 ] =='_' )) { +char *offs; + + field_str =strsep (¶m,":" );
[Severity: Low]
Does this code prevent removing the clear_wprobe trigger if it has a count but no field?
If the user sets a count on a clear_wprobe trigger, wprobe_trigger_print() outputs the trigger as ending with ":count=N". When attempting to remove it by writing the exact string prepended with '!', this parser checks if the parameter starts with a letter.
Since "count=N" starts with a letter ('c'), it treats it as an event field name, and fails with -ENOENT in wprobe_trigger_field_parse(), preventing the trigger from being unregistered.
[ ... ]
kernel/trace/trace_wprobe.c static __init int init_wprobe_trace(void) · line 1186
Quoted 12 lines
kernel/trace/trace_wprobe.c +12
-0+/* Soft-enable (register) wprobe event on WPROBE_DEFAULT_CLEAR_ADDRESS */ +if (!trace_event_try_get_ref (wprobe_file->event_call)) { + ret = -ENODEV ; + goto out_free_trigger; + } + + ret =trace_event_enable_disable (wprobe_file,1 ,1 ); +if (ret <0 ) + goto out_put; + +if (!clear) +WRITE_ONCE (tw->addr,WPROBE_DEFAULT_CLEAR_ADDRESS );
[Severity: Medium]
Should we update the target address before soft-enabling the event?
The documentation suggests initializing a new wprobe at address 0 and keeping it disabled. When we call trace_event_enable_disable(wprobe_file, 1, 1) here, it installs hardware breakpoints monitoring the current address (e.g., 0).
The parked dummy address WPROBE_DEFAULT_CLEAR_ADDRESS is only assigned to tw->addr afterwards, which means the hardware breakpoints are left actively monitoring address 0 and could generate spurious debug exceptions.
[ ... ]
kernel/trace/trace_wprobe.c static __init int init_wprobe_trace(void) · line 1203
Quoted 8 lines
kernel/trace/trace_wprobe.c +8
-0+out_disable: +trace_event_enable_disable (wprobe_file,0 ,1 ); +out_put: +trace_event_put_ref (wprobe_file->event_call); +out_free_trigger: +event_trigger_reset_filter (cmd_ops, trigger_data); +event_trigger_free (trigger_data); +return ret;
[Severity: Medium]
Can this leak trigger_data and wprobe_data on the error path?
When wprobe_trigger_cmd_parse() executes, it increments the trigger_data refcount to 1 via event_trigger_init().
It then calls event_trigger_register() which calls wprobe_register_trigger() and increments the refcount to 2 via data->cmd_ops->init(data).
If trace_event_trigger_enable_disable() fails inside wprobe_register_trigger(), it returns an error without balancing the reference count:
kernel/trace/trace_wprobe.c:wprobe_register_trigger() {
...
ret = trace_event_trigger_enable_disable(file, 1);
if (ret < 0) {
list_del_rcu(&data->list);
update_cond_flag(file);
}
return ret;
}The error path here then calls event_trigger_free(trigger_data), which only decrements the refcount from 2 to 1 and skips freeing the objects.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/178739053919.1520941.17662338993878200834.stgit@devnote2?part=9
Masami Hiramatsu (Google)
maintainerRe: [PATCH v13 09/12] tracing/wprobe: Add set_wprobe and clear_wprobe event triggers
Quoted 37 lines · On Sat, 22 Aug 2026 09:42:17 +0000 [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Medium] Leakedtrigger_dataandwprobe_datawhen trigger registration fails due to unbalanced reference counts.
- [Medium] Hardware breakpoints are inadvertently left monitoring the initial wprobe address (unparked) instead of the parked dummy address upon enablement.
- [Low] Theclear_wprobetrigger cannot be removed by echoing its exact printed output if a trigger count is specified.
----- Patch [9]: [PATCH v13 09/12] tracing/wprobe: Add
set_wprobeandclear_wprobeevent triggers --- commite794ff7412c73348b335e9a50aceecbbd82d9a0c
Author: Masami Hiramatsu (Google) <[email protected]>tracing/wprobe: Add
set_wprobeandclear_wprobeevent triggersAdd
set_wprobeandclear_wprobeevent triggers to dynamically attach and detach hardware breakpoint address monitoring based on event field contents.Quoted 4 lines
kernel/trace/trace_wprobe.c +0
-0[ ... ]
Quoted 1 line
kernel/trace/trace_wprobe.c +0
-0@@ -756,3 +827,501 @@ static __init int init_wprobe_trace(void)[ ... ]
Quoted 5 lines
kernel/trace/trace_wprobe.c +5
-0+static int wprobe_trigger_cmd_parse (struct event_command *cmd_ops, +struct trace_event_file *file, +char *glob,char *cmd, +char *param_and_filter) +{[ ... ]
Quoted 8 lines
kernel/trace/trace_wprobe.c static __init int init_wprobe_trace(void) · line 1112
Quoted 4 lines
kernel/trace/trace_wprobe.c +4
-0+ }else if (param && (isalpha (param[0 ]) || param[0 ] =='_' )) { +char *offs; + + field_str =strsep (¶m,":" );[Severity: Low]
Does this code prevent removing theclear_wprobetrigger if it has a count but no field?
Ah, indeed. The trigger shows ":count=N" or ":unlimited" for counter instead of digit value.
Quoted 9 lines
If the user sets a count on a
clear_wprobetrigger,wprobe_trigger_print()outputs the trigger as ending with ":count=N". When attempting to remove it by writing the exact string prepended with '!', this parser checks if the parameter starts with a letter.Since "count=N" starts with a letter ('c'), it treats it as an event field name, and fails with
-ENOENTinwprobe_trigger_field_parse(), preventing the trigger from being unregistered.
OK, let me fix it.
Quoted 18 lines
[ ... ]
kernel/trace/trace_wprobe.c static __init int init_wprobe_trace(void) · line 1186
Quoted 12 lines
kernel/trace/trace_wprobe.c +12
-0+/* Soft-enable (register) wprobe event on WPROBE_DEFAULT_CLEAR_ADDRESS */ +if (!trace_event_try_get_ref (wprobe_file->event_call)) { + ret = -ENODEV ; + goto out_free_trigger; + } + + ret =trace_event_enable_disable (wprobe_file,1 ,1 ); +if (ret <0 ) + goto out_put; + +if (!clear) +WRITE_ONCE (tw->addr,WPROBE_DEFAULT_CLEAR_ADDRESS );[Severity: Medium]
Should we update the target address before soft-enabling the event?
Ah, indeed. We also need to roll it back if soft-enable fails.
Quoted 42 lines
The documentation suggests initializing a new wprobe at address 0 and keeping it disabled. When we call
trace_event_enable_disable(wprobe_file, 1, 1)here, it installs hardware breakpoints monitoring the current address (e.g., 0).The parked dummy address
WPROBE_DEFAULT_CLEAR_ADDRESSis only assigned totw->addrafterwards, which means the hardware breakpoints are left actively monitoring address 0 and could generate spurious debug exceptions.[ ... ]
kernel/trace/trace_wprobe.c static __init int init_wprobe_trace(void) · line 1203
Quoted 8 lines
kernel/trace/trace_wprobe.c +8
-0+out_disable: +trace_event_enable_disable (wprobe_file,0 ,1 ); +out_put: +trace_event_put_ref (wprobe_file->event_call); +out_free_trigger: +event_trigger_reset_filter (cmd_ops, trigger_data); +event_trigger_free (trigger_data); +return ret;[Severity: Medium]
Can this leaktrigger_dataandwprobe_dataon the error path?When
wprobe_trigger_cmd_parse()executes, it increments thetrigger_datarefcount to 1 viaevent_trigger_init().It then calls
event_trigger_register()which callswprobe_register_trigger()and increments the refcount to 2 viadata->cmd_ops->init(data).If
trace_event_trigger_enable_disable()fails insidewprobe_register_trigger(), it returns an error without balancing the reference count:kernel/trace/trace_wprobe.c:wprobe_register_trigger() { ... ret = trace_event_trigger_enable_disable(file, 1); if (ret < 0) { list_del_rcu(&data->list); update_cond_flag(file); } return ret; }
Quoted 2 lines
The error path here then calls
event_trigger_free(trigger_data), which only decrements the refcount from 2 to 1 and skips freeing the objects.
Ah, OK. It should call the data->cmd_ops->free() in failure path.
Thanks,
Quoted 3 lines
Signature · 2 lines
-- Sashiko AI review · https://sashiko.dev/#/patchset/178739053919.1520941.17662338993878200834.stgit@devnote2?part=9
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Masami Hiramatsu (Google)
maintainerPATCH v13 10/12 selftests: ftrace: Add wprobe trigger testcase
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
Add a testcase for checking wprobe trigger. This sets set_wprobe and clear_wprobe triggers on fprobe event and static trace event to monitor memory accesses within the trace-events-sample kernel module. Also add a testcase for verifying wprobe trigger syntax error logging.
---
Changes in v13:
- Look up target function dynamically in trigger-wprobe-syntax-errors.tc.
- Disable set_wprobe trigger before verifying clear_wprobe in trigger-wprobe.tc to avoid race condition with sample_timer_cb.
Changes in v12:
- Add trigger-wprobe-syntax-errors.tc for verifying wprobe trigger syntax error logging.
- Fix requires line in trigger-wprobe-syntax-errors.tc so test is not evaluated as unsupported prior to execution.
- Define dfd=$arg1 fetcharg on testevent for trigger syntax checks.
Changes in v11:
- Update testcase to use trace-events-sample kernel module instead of VFS file operations.
--- tools/testing/selftests/ftrace/config | 1 .../test.d/trigger/trigger-wprobe-syntax-errors.tc | 37 +++++++++ .../ftrace/test.d/trigger/trigger-wprobe.tc | 87 ++++++++++++++++++++ 3 files changed, 125 insertions(+) create mode 100644 tools/testing/selftests/ftrace/test.d/trigger/trigger-wprobe-syntax-errors.tc create mode 100644 tools/testing/selftests/ftrace/test.d/trigger/trigger-wprobe.tc
tools/testing/selftests/ftrace/config +1 -0
@@ -28,3 +28,4 @@ CONFIG_TRACER_SNAPSHOT=y CONFIG_UPROBES=y CONFIG_UPROBE_EVENTS=y CONFIG_WPROBE_EVENTS=y +CONFIG_WPROBE_TRIGGERS=y
tools/testing/selftests/ftrace/test.d/trigger/trigger-wprobe-syntax-errors.tc new file +37 -0
@@ -0,0 +1,37 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 +# description: event trigger - test wprobe trigger syntax errors +# requires: dynamic_events error_log "w[:[<group>/][<event>]] [r|w|rw]@<addr>[:<len>]":README "f[:[<group>/][<event>]] <func-name>[%return] [<args>]":README + +check_error() { # command-with-error-pos-by-^ + ftrace_errlog_check "wprobe_trigger" "$1" "events/fprobes/testevent/trigger" +} + +TARGET_FUNC=$(grep -m 1 -E -w "[tT] (vfs_read|do_sys_openat2|do_sys_open)" /proc/kallsyms | awk '{print $3}') +if [ -z "$TARGET_FUNC" ]; then + echo "UNRESOLVED: target function not found" + exit_unresolved +fi + +# Add a dummy fprobe event to attach triggers to +echo "f:fprobes/testevent $TARGET_FUNC dfd=\$arg1" > dynamic_events + +# Add a target wprobe event +echo 'w:watch rw@0:8' >> dynamic_events + +# Test errors on trigger syntax +check_error 'set_wprobe:^non_exist_wprobe:dfd' # WPROBE_NOT_FOUND +check_error 'set_wprobe:^' # WPROBE_NOT_FOUND +check_error 'set_wprobe:watch^' # WPROBE_NEED_FIELD +check_error 'set_wprobe:watch:^non_exist_field' # NO_EVENT_FIELD + +# Enable target wprobe event and test WPROBE_BUSY error +echo 1 > events/wprobes/watch/enable +check_error 'set_wprobe:^watch:dfd' # WPROBE_BUSY +echo 0 > events/wprobes/watch/enable + +# Cleanup +echo '-:watch' >> dynamic_events +echo '-:testevent' >> dynamic_events + +exit 0
tools/testing/selftests/ftrace/test.d/trigger/trigger-wprobe.tc new file +87 -0
87 lines not on this page · raw message
[email protected]
AI reviewRe: [PATCH v13 10/12] selftests: ftrace: Add wprobe trigger testcase
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] The `trigger-wprobe.tc` test has a race condition causing intermittent failures because the…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] The trigger-wprobe.tc test has a race condition causing intermittent failures because the clear_wprobe trigger executes asynchronously and the test does not wait for it to fire before verifying the trace buffer.
- [Medium] The trigger-wprobe.tc test uses timer=t as a fetcharg without enforcing a BTF requirement, which will cause false-positive test failures on kernels without BTF support.
--
--- Patch [10]: [PATCH v13 10/12] selftests: ftrace: Add wprobe trigger testcase --- commit f8200da0c99be51814ee14df413b8fd81995a06f
Author: Masami Hiramatsu (Google) <[email protected]>
selftests: ftrace: Add wprobe trigger testcase
This commit adds a testcase to verify wprobe triggers. It uses set_wprobe and clear_wprobe triggers on fprobe and static trace events to monitor memory accesses within the trace-events-sample kernel module, and tests wprobe trigger syntax error logging.
Quoted 5 lines
tools/testing/selftests/ftrace/test.d/trigger/trigger-wprobe.tc new file +0
-0
[ ... ]
tools/testing/selftests/ftrace/test.d/trigger/trigger-wprobe.tc line 46
Quoted 5 lines
+:;: "Add a wprobe event used by trigger" ;:
tools/testing/selftests/ftrace/test.d/trigger/trigger-wprobe.tc new file +4
-0+echo 'w:watch rw@0:8 address=$addr value=$value' > dynamic_events + +:;: "Add events for triggering wprobe" ;: +echo 'f:fprobes/testevent sample_timer_cb timer=t' >> dynamic_events
[Severity: Medium]
Is it intentional to use a local variable t as a fetcharg here?
This relies on BTF debug info. On kernels without CONFIG_PROBE_EVENTS_BTF_ARGS=y or architectures lacking BTF support, the kernel's tracing subsystem cannot resolve the argument and the fprobe registration will fail.
Should this test either add a requirement for BTF arguments in the requires directive, or use a register argument like $arg1 instead?
Quoted 5 lines
tools/testing/selftests/ftrace/test.d/trigger/trigger-wprobe.tc new file +5
-0+ +:;: "Enable all events before setting triggers" ;: +echo 1 > tracing_on +echo 1 >> events/fprobes/testevent/enable +echo 1 >> events/sample-trace/foo_bar_with_fn/enable
[ ... ]
tools/testing/selftests/ftrace/test.d/trigger/trigger-wprobe.tc line 72
Quoted 10 lines
+:;: "Set
clear_wprobetrigger onfoo_bar_with_fn" ;:tools/testing/selftests/ftrace/test.d/trigger/trigger-wprobe.tc new file +9
-0+echo 'clear_wprobe:watch' >> events/sample-trace/foo_bar_with_fn/trigger +cat events/sample-trace/foo_bar_with_fn/trigger | grep ^clear_wprobe + +# Disable set_wprobe to prevent sample_timer_cb from re-arming the watchpoint +echo '!set_wprobe:watch:timer' >> events/fprobes/testevent/trigger + +# Clear trace and wait to ensure no new watchpoint events are generated +clear_trace +sleep 1
[Severity: Medium]
Can this sequence cause a race condition resulting in intermittent test failures?
If the test clears the trace buffer here without first waiting for foo_bar_with_fn to execute and actually clear the watchpoint, there is a race window.
Since both foo_bar_with_fn and sample_timer_cb run periodically every second, could sample_timer_cb execute after clear_trace but before foo_bar_with_fn has fired?
If so, it would access the still-armed watchpoint and log a watch event in the newly cleared trace buffer, causing the final verification step (! grep -q watch trace) to fail.
Quoted 4 lines
tools/testing/selftests/ftrace/test.d/trigger/trigger-wprobe.tc new file +4
-0+ +:;: "Ensure clear_wprobe trigger deactivated the watchpoint" ;: +! grep -q watch trace +
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/178739053919.1520941.17662338993878200834.stgit@devnote2?part=10
Masami Hiramatsu (Google)
maintainerRe: [PATCH v13 10/12] selftests: ftrace: Add wprobe trigger testcase
Quoted 81 lines · On Sat, 22 Aug 2026 09:39:24 +0000 [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Thetrigger-wprobe.tctest has a race condition causing intermittent failures because theclear_wprobetrigger executes asynchronously and the test does not wait for it to fire before verifying the trace buffer.
- [Medium] Thetrigger-wprobe.tctest usestimer=tas a fetcharg without enforcing a BTF requirement, which will cause false-positive test failures on kernels without BTF support.
----- Patch [10]: [PATCH v13 10/12] selftests: ftrace: Add wprobe trigger testcase --- commit
f8200da0c99be51814ee14df413b8fd81995a06f
Author: Masami Hiramatsu (Google) <[email protected]>selftests: ftrace: Add wprobe trigger testcase
This commit adds a testcase to verify wprobe triggers. It uses
set_wprobeandclear_wprobetriggers on fprobe and static trace events to monitor memory accesses within the trace-events-sample kernel module, and tests wprobe trigger syntax error logging.Quoted 5 lines
tools/testing/selftests/ftrace/test.d/trigger/trigger-wprobe.tc new file +0
-0[ ... ]
tools/testing/selftests/ftrace/test.d/trigger/trigger-wprobe.tc line 46
Quoted 5 lines
+:;: "Add a wprobe event used by trigger" ;:
tools/testing/selftests/ftrace/test.d/trigger/trigger-wprobe.tc new file +4
-0+echo 'w:watch rw@0:8 address=$addr value=$value' > dynamic_events + +:;: "Add events for triggering wprobe" ;: +echo 'f:fprobes/testevent sample_timer_cb timer=t' >> dynamic_events[Severity: Medium]
Is it intentional to use a local variable t as a fetcharg here?This relies on BTF debug info. On kernels without
CONFIG_PROBE_EVENTS_BTF_ARGS=yor architectures lacking BTF support, the kernel's tracing subsystem cannot resolve the argument and the fprobe registration will fail.Should this test either add a requirement for BTF arguments in the requires directive, or use a register argument like $arg1 instead?
Quoted 5 lines
tools/testing/selftests/ftrace/test.d/trigger/trigger-wprobe.tc new file +5
-0+ +:;: "Enable all events before setting triggers" ;: +echo 1 > tracing_on +echo 1 >> events/fprobes/testevent/enable +echo 1 >> events/sample-trace/foo_bar_with_fn/enable[ ... ]
tools/testing/selftests/ftrace/test.d/trigger/trigger-wprobe.tc line 72
Quoted 10 lines
+:;: "Set
clear_wprobetrigger onfoo_bar_with_fn" ;:tools/testing/selftests/ftrace/test.d/trigger/trigger-wprobe.tc new file +9
-0+echo 'clear_wprobe:watch' >> events/sample-trace/foo_bar_with_fn/trigger +cat events/sample-trace/foo_bar_with_fn/trigger | grep ^clear_wprobe + +# Disable set_wprobe to prevent sample_timer_cb from re-arming the watchpoint +echo '!set_wprobe:watch:timer' >> events/fprobes/testevent/trigger + +# Clear trace and wait to ensure no new watchpoint events are generated +clear_trace +sleep 1[Severity: Medium]
Can this sequence cause a race condition resulting in intermittent test failures?If the test clears the trace buffer here without first waiting for
foo_bar_with_fnto execute and actually clear the watchpoint, there is a race window.Since both
foo_bar_with_fnandsample_timer_cbrun periodically every second, couldsample_timer_cbexecute afterclear_tracebut beforefoo_bar_with_fnhas fired?If so, it would access the still-armed watchpoint and log a watch event in the newly cleared trace buffer, causing the final verification step (! grep -q watch trace) to fail.
Quoted 4 lines
tools/testing/selftests/ftrace/test.d/trigger/trigger-wprobe.tc new file +4
-0+ +:;: "Ensure clear_wprobe trigger deactivated the watchpoint" ;: +! grep -q watch trace +Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/178739053919.1520941.17662338993878200834.stgit@devnote2?part=10
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Masami Hiramatsu (Google)
maintainerPATCH v13 11/12 tracing/wprobe: Support BTF typecast in fetchargs
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
Allow BTF typecast syntax (STRUCT)FETCHARG->MEMBER in wprobe event fetchargs. Previously, handle_typecast() rejected any probe context that was not a function entry/return or tracepoint event probe.
Wprobe events use $addr (the accessed address) and $value (the value at that address). By enabling BTF typecast, users can now cast these to a concrete struct type and access its fields directly. For example:
echo 'w:watch rw@0:8 dflag=(dentry)$addr->d_flags' >> dynamic_events
With a set_wprobe trigger pointing the watchpoint at a dentry address, the resulting trace shows d_flags being accessed at that location.
Note that $addr and $value are restricted to kernel-space memory, which is consistent with the existing TPARG_FL_KERNEL flag used when parsing wprobe fetchargs.
---
Changes in v11:
- Update trigger-wprobe-btf-typecast.tc to use trace-events-sample kernel module.
- Fix commit comment.
Changes in v9:
- Newly added.
--- kernel/trace/trace_probe.c | 13 +++- kernel/trace/trace_probe.h | 5 + tools/testing/selftests/ftrace/config | 1 .../test.d/trigger/trigger-wprobe-btf-typecast.tc | 72 ++++++++++++++++++++ 4 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/ftrace/test.d/trigger/trigger-wprobe-btf-typecast.tc
kernel/trace/trace_probe.c +12 -1
@@ -889,6 +889,16 @@ static int query_btf_struct(const char *sname, struct traceprobe_parse_context * ctx->struct_btf =NULL ; } +if (ctx->btf) { + id =btf_find_by_name_kind (ctx->btf, sname,BTF_KIND_STRUCT ); +if (id >0 ) { +btf_get (ctx->btf); + ctx->struct_btf = ctx->btf; + ctx->last_struct =btf_type_by_id (ctx->struct_btf, id); +return 0 ; + } + } + id =bpf_find_btf_id (sname,BTF_KIND_STRUCT , &btf);if (id <0 )return id;
@@ -964,7 +974,8 @@ static int handle_typecast(char *arg, struct traceprobe_parse_context *ctx)if (!(tparg_is_event_probe (ctx->flags) ||tparg_is_function_entry (ctx->flags) || -tparg_is_function_return (ctx->flags))) { +tparg_is_function_return (ctx->flags) || +tparg_is_wprobe (ctx->flags))) {trace_probe_log_err (ctx->offset,NOSUP_BTFARG );return -EOPNOTSUPP ; }
kernel/trace/trace_probe.h +5 -0
@@ -437,6 +437,11 @@ static inline bool tparg_is_event_probe(unsigned int flags)return !!(flags &TPARG_FL_TEVENT ); } +static inline bool tparg_is_wprobe (unsigned int flags) +{ +return !!(flags &TPARG_FL_WPROBE ); +} +/* Each typecast consumes nested level. So the max number of typecast is 8. */ #define TRACEPROBE_MAX_NESTED_LEVEL 8
tools/testing/selftests/ftrace/config +1 -0
@@ -1,5 +1,6 @@ CONFIG_BPF_SYSCALL=y CONFIG_DEBUG_INFO_BTF=y +CONFIG_DEBUG_INFO_BTF_MODULES=y CONFIG_DEBUG_INFO_DWARF4=y CONFIG_EPROBE_EVENTS=y CONFIG_FPROBE=y
tools/testing/selftests/ftrace/test.d/trigger/trigger-wprobe-btf-typecast.tc new file +72 -0
72 lines not on this page · raw message
[email protected]
AI reviewRe: [PATCH v13 11/12] tracing/wprobe: Support BTF typecast in fetchargs
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 15 lines
From: Masami Hiramatsu (Google) <[email protected]>
Allow BTF typecast syntax (STRUCT)FETCHARG->MEMBER in wprobe event fetchargs. Previously,
handle_typecast()rejected any probe context that was not a function entry/return or tracepoint event probe.Wprobe events use $addr (the accessed address) and $value (the value at that address). By enabling BTF typecast, users can now cast these to a concrete struct type and access its fields directly. For example:
echo 'w:watch rw@0:8 dflag=(dentry)$addr->d_flags' >>
dynamic_eventsWith a
set_wprobetrigger pointing the watchpoint at a dentry address, the resulting trace showsd_flagsbeing accessed at that location.
Quoted 6 lines
Note that $addr and $value are restricted to kernel-space memory, which is consistent with the existing
TPARG_FL_KERNELflag used when parsing wprobe fetchargs.Assisted-by Antigravity:gemini-3.5-flashSigned-off-by Masami Hiramatsu (Google) <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/178739053919.1520941.17662338993878200834.stgit@devnote2?part=11
Masami Hiramatsu (Google)
maintainerPATCH v13 12/12 tracing/wprobe: Support BTF struct offset resolution in set_wprobe trigger
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
Allow set_wprobe trigger to use BTF struct offset resolution to specify the target address field.
---
Changes in v13:
- Check for field token before calling wprobe_trigger_field_parse() in clear_wprobe to avoid spurious error log entries on numeric counts.
Changes in v12:
- Refactor field parsing logic into wprobe_trigger_field_parse().
- Remove unused variable count_str in wprobe_trigger_cmd_parse().
--- kernel/trace/trace_wprobe.c | 263 ++++++++++++++------ .../test.d/trigger/trigger-wprobe-btf-offset.tc | 74 ++++++ 2 files changed, 262 insertions(+), 75 deletions(-) create mode 100644 tools/testing/selftests/ftrace/test.d/trigger/trigger-wprobe-btf-offset.tc
kernel/trace/trace_wprobe.c +188 -75
@@ -27,6 +27,7 @@#include <asm/ptrace.h> #include "trace.h" +#include "trace_btf.h" #include "trace_dynevent.h" #include "trace_probe.h" #include "trace_probe_kernel.h"
@@ -950,9 +951,10 @@ static int wprobe_trigger_print(struct seq_file *m,seq_printf (m,":count=%ld" , data->count);if (data->filter_str) -seq_printf (m," if %s\n" , data->filter_str); -else -seq_putc (m,'\n' ); +seq_printf (m," if %s" , data->filter_str); + +seq_printf (m," # offset:%d adjust:%ld\n" , + wprobe_data->offset, wprobe_data->adjust);return 0 ; }
@@ -990,6 +992,181 @@ static void wprobe_trigger_free(struct event_trigger_data *data) } } +#ifdef CONFIG_PROBE_EVENTS_BTF_ARGS + +static int get_offset_of_field (struct btf *btf,const struct btf_type *type,char *field_name) +{ +const struct btf_member *field; +int bitoffs =0 ; +u32 anon_offs; +char *next; + +do { + next =strchr (field_name,'.' ); +if (next) + *next++ ='\0' ; + + field =btf_find_struct_member (btf, type, field_name, &anon_offs); +if (IS_ERR_OR_NULL (field)) +return -ENOENT ; + +if (btf_type_kflag (type)) { +/* Reject bitfield member access */ +if (BTF_MEMBER_BITFIELD_SIZE (field->offset)) +return -EINVAL ; + bitoffs += anon_offs +BTF_MEMBER_BIT_OFFSET (field->offset); + }else { + bitoffs += anon_offs + field->offset; + } + + field_name = next; +if (next) { + type =btf_type_skip_modifiers (btf, field->type,NULL ); +if (!type) +return -ENOENT ; + } + }while (next); +return bitoffs /BITS_PER_BYTE ; +} + +/* btf_put(NULL) is acceptable. */ +DEFINE_FREE(btf_put, struct btf *, btf_put(_T)) + +/* parse typecast: (TYPE[,ASGN])EVENT_FIELD->FIELD[.SUBFIELD...] and set adjust. */ +static int wprobe_trigger_typecast_parse (char **field_str_ptr, +struct trace_event_file *file, +struct wprobe_trigger_data *wprobe_data, +const char *glob) +{ + struct btf *btf __free(btf_put) = NULL; +const struct btf_type *type; +char *assign_field; +char *event_field; +char *type_field; +char *type_name; +char *offs; +long val =0 ; +int id; +int adjust; + + type_name = *field_str_ptr +1 ; + event_field =strchr (type_name,')' ); +if (!event_field) { +wprobe_trigger_log_err (file, glob, type_name - glob,DEREF_OPEN_BRACE ); +return -EINVAL ; + } + *event_field++ ='\0' ; + +/* Check the optional assign field. */ + assign_field =strchr (type_name,',' ); +if (assign_field) + *assign_field++ ='\0' ; + +/* Get the type field name. */ + type_field =strstr (event_field,"->" ); +if (!type_field) { +wprobe_trigger_log_err (file, glob, event_field - glob,TYPECAST_REQ_FIELD ); +return -EINVAL ; + } + *type_field ='\0' ; + type_field +=2 ; + + offs =strpbrk (type_field,"+-" ); +if (offs) { +if (kstrtol (offs,0 , &val) <0 ) { +wprobe_trigger_log_err (file, glob, offs - glob,BAD_DEREF_OFFS ); +return -EINVAL ; + } + *offs ='\0' ; + } + +/* find type from BTF */ + id =bpf_find_btf_id (type_name,BTF_KIND_STRUCT , &btf); +if (id <0 ) { +wprobe_trigger_log_err (file, glob, type_name - glob,BAD_BTF_TID ); +return id; + } + + type =btf_type_by_id (btf, id); +if (!type) { +wprobe_trigger_log_err (file, glob, type_name - glob,BAD_BTF_TID ); +return -EINVAL ; + } + + adjust =get_offset_of_field (btf, type, type_field); +if (adjust <0 ) { +wprobe_trigger_log_err (file, glob, type_field - glob,NO_BTF_FIELD ); +return adjust; + } + wprobe_data->adjust = adjust + val; + +if (assign_field) { +/* assign_field should be a struct field */ + adjust =get_offset_of_field (btf, type, assign_field); +if (adjust <0 ) { +wprobe_trigger_log_err (file, glob, assign_field - glob,NO_BTF_FIELD ); +return adjust; + } + wprobe_data->adjust -= adjust; + } + + *field_str_ptr = event_field; +return 0 ; +} +#else +static int wprobe_trigger_typecast_parse (char **field_str_ptr, +struct trace_event_file *file, +struct wprobe_trigger_data *wprobe_data, +const char *glob) +{ +wprobe_trigger_log_err (file, glob, *field_str_ptr - glob,NOSUP_BTFARG ); +return -EOPNOTSUPP ; +} +#endif /* CONFIG_PROBE_EVENTS_BTF_ARGS */ + +static int wprobe_trigger_field_parse (char *field_str,struct trace_event_file *file, +struct wprobe_trigger_data *wprobe_data, +const char *glob) +{ +struct ftrace_event_field *field; +char *offs; + +if (field_str[0 ] =='(' ) { +int ret =wprobe_trigger_typecast_parse (&field_str, file, wprobe_data, glob); + +if (ret <0 ) +return ret; + }else { + offs =strpbrk (field_str,"+-" ); +if (offs) { +long val; + +if (kstrtol (offs,0 , &val) <0 ) { +wprobe_trigger_log_err (file, glob, offs - glob,BAD_DEREF_OFFS ); +return -EINVAL ; + } + wprobe_data->adjust = val; + *offs ='\0' ; + } + } + + field =trace_find_event_field (file->event_call, field_str); +if (!field) { +wprobe_trigger_log_err (file, glob, field_str - glob,NO_EVENT_FIELD ); +return -ENOENT ; + } +if (field->size !=sizeof (void *)) { +wprobe_trigger_log_err (file, glob, field_str - glob,WPROBE_BAD_FIELD ); +return -ENOEXEC ; + } + wprobe_data->offset = field->offset; + wprobe_data->field =kstrdup (field_str,GFP_KERNEL ); +if (!wprobe_data->field) +return -ENOMEM ; + +return 0 ; +} + static int wprobe_trigger_cmd_parse(struct event_command *cmd_ops, struct trace_event_file *file, char *glob, char *cmd,
@@ -1001,11 +1178,9 @@ static int wprobe_trigger_cmd_parse(struct event_command *cmd_ops,*/ struct wprobe_trigger_data *wprobe_data =NULL ;struct event_trigger_data *trigger_data =NULL ; +char *event_str, *comment;struct trace_event_file *wprobe_file;struct trace_array *tr = file->tr; -char *event_str, *field_str, *comment; -struct ftrace_event_field *field; -struct trace_event_call *event;bool remove, clear = false;struct trace_wprobe *tw;char *param, *filter;
@@ -1064,84 +1239,22 @@ static int wprobe_trigger_cmd_parse(struct event_command *cmd_ops,/* clear_wprobe does not need field, but can have optional field. */ if (!clear) { -char *offs; +char *field_str =strsep (¶m,":" ); -/* Find target field, which must be equivalent to "void *" */ - field_str =strsep (¶m,":" );if (!field_str) {wprobe_trigger_log_err (file, glob,strlen (glob),WPROBE_NEED_FIELD ); ret = -EINVAL ; goto out_free; } - - offs =strpbrk (field_str,"+-" ); -if (offs) { -long val; - -if (kstrtol (offs,0 , &val) <0 ) { -wprobe_trigger_log_err (file, glob, offs - glob,BAD_DEREF_OFFS ); - ret = -EINVAL ; - goto out_free; - } - wprobe_data->adjust = val; - *offs ='\0' ; - } - - event = file->event_call; - field =trace_find_event_field (event, field_str); -if (!field) { -wprobe_trigger_log_err (file, glob, field_str - glob,NO_EVENT_FIELD ); - ret = -ENOENT ; - goto out_free; - } - -if (field->size !=sizeof (void *)) { -wprobe_trigger_log_err (file, glob, field_str - glob,WPROBE_BAD_FIELD ); - ret = -ENOEXEC ; - goto out_free; - } - wprobe_data->offset = field->offset; - wprobe_data->field =kstrdup (field_str,GFP_KERNEL ); -if (!wprobe_data->field) { - ret = -ENOMEM ; - goto out_free; - } - }else if (param && (isalpha (param[0 ]) || param[0 ] =='_' )) { -char *offs; - - field_str =strsep (¶m,":" ); - offs =strpbrk (field_str,"+-" ); -if (offs) { -long val; - -if (kstrtol (offs,0 , &val) <0 ) { -wprobe_trigger_log_err (file, glob, offs - glob,BAD_DEREF_OFFS ); - ret = -EINVAL ; - goto out_free; - } - wprobe_data->adjust = val; - *offs ='\0' ; - } - - event = file->event_call; - field =trace_find_event_field (event, field_str); -if (!field) { -wprobe_trigger_log_err (file, glob, field_str - glob,NO_EVENT_FIELD ); - ret = -ENOENT ; + ret =wprobe_trigger_field_parse (field_str, file, wprobe_data, glob); +if (ret <0 ) goto out_free; - } + }else if (param && (isalpha (param[0 ]) || param[0 ] =='_' || param[0 ] =='(' )) { +char *field_str =strsep (¶m,":" ); -if (field->size !=sizeof (void *)) { -wprobe_trigger_log_err (file, glob, field_str - glob,WPROBE_BAD_FIELD ); - ret = -ENOEXEC ; - goto out_free; - } - wprobe_data->offset = field->offset; - wprobe_data->field =kstrdup (field_str,GFP_KERNEL ); -if (!wprobe_data->field) { - ret = -ENOMEM ; + ret =wprobe_trigger_field_parse (field_str, file, wprobe_data, glob); +if (ret <0 ) goto out_free; - } } trigger_data =trigger_data_alloc (cmd_ops, cmd, param, wprobe_data);
tools/testing/selftests/ftrace/test.d/trigger/trigger-wprobe-btf-offset.tc new file +74 -0
74 lines not on this page · raw message
[email protected]
AI reviewRe: [PATCH v13 12/12] tracing/wprobe: Support BTF struct offset resolution in set_wprobe trigger
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] get_offset_of_field() incorrectly evaluates the BTF kflag of the parent struct instead of the…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] get_offset_of_field() incorrectly evaluates the BTF kflag of the parent struct instead of the nested anonymous struct containing the field, leading to massively inflated offset calculations and bypassing bitfield validation.
- [Low] wprobe_trigger_log_err receives a truncated glob string, causing syntax error logs to place the caret out-of-bounds and hide the user's input.
--
--- Patch [12]: [PATCH v13 12/12] tracing/wprobe: Support BTF struct offset resolution in set_wprobe trigger --- commit 1da976d66d95af47e154412d3c1b912f8a6b4018
Author: Masami Hiramatsu (Google) <[email protected]>
tracing/wprobe: Support BTF struct offset resolution in set_wprobe trigger
Allow set_wprobe trigger to use BTF struct offset resolution to specify the target address field.
Quoted 4 lines
kernel/trace/trace_wprobe.c +0
-0
[ ... ]
Quoted 13 lines
kernel/trace/trace_wprobe.c +9
-0@@ -990,6 +992,181 @@ static void wprobe_trigger_free(struct event_trigger_data *data) } } +#ifdef CONFIG_PROBE_EVENTS_BTF_ARGS + +static int get_offset_of_field (struct btf *btf,const struct btf_type *type,char *field_name) +{ +const struct btf_member *field; +int bitoffs =0 ; +u32 anon_offs; +char *next; +
kernel/trace/trace_wprobe.c static void wprobe_trigger_free(struct event_trigger_data *data) · line 1013
Quoted 10 lines
kernel/trace/trace_wprobe.c +10
-0+ do { + next = strchr(field_name, '.'); + if (next) + *next++ = '\0'; + + field = btf_find_struct_member(btf, type, field_name, &anon_offs); + if (IS_ERR_OR_NULL(field)) + return -ENOENT; + + if (btf_type_kflag(type)) {
[Severity: High]
Does this code check the kflag of the original parent type instead of the anonymous struct that actually contains the field?
btf_find_struct_member() can return a field from a nested anonymous struct, but if the parent struct and the anonymous struct have mismatched BTF kflags (for example, the parent has kflag=0 while the anonymous struct has kflag=1 because it contains bitfields), using the parent's kflag here will cause the bitfield size encoded in the upper 8 bits of the offset to be misinterpreted as part of the byte offset.
This can add a massively inflated value to bitoffs and result in a wild pointer dereference when the wprobe triggers.
Quoted 17 lines
kernel/trace/trace_wprobe.c +17
-0+/* Reject bitfield member access */ +if (BTF_MEMBER_BITFIELD_SIZE (field->offset)) +return -EINVAL ; + bitoffs += anon_offs +BTF_MEMBER_BIT_OFFSET (field->offset); + }else { + bitoffs += anon_offs + field->offset; + } + + field_name = next; +if (next) { + type =btf_type_skip_modifiers (btf, field->type,NULL ); +if (!type) +return -ENOENT ; + } + }while (next); +return bitoffs /BITS_PER_BYTE ; +}
[ ... ]
Quoted 10 lines
kernel/trace/trace_wprobe.c +10
-0+static int wprobe_trigger_typecast_parse (char **field_str_ptr, +struct trace_event_file *file, +struct wprobe_trigger_data *wprobe_data, +const char *glob) +{ + struct btf *btf __free(btf_put) = NULL; +const struct btf_type *type; +char *assign_field; +char *event_field; +char *type_field;
kernel/trace/trace_wprobe.c static void wprobe_trigger_free(struct event_trigger_data *data) · line 1055
Quoted 10 lines
kernel/trace/trace_wprobe.c +10
-0+char *type_name; +char *offs; +long val =0 ; +int id; +int adjust; + + type_name = *field_str_ptr +1 ; + event_field =strchr (type_name,')' ); +if (!event_field) { +wprobe_trigger_log_err (file, glob, type_name - glob,DEREF_OPEN_BRACE );
[Severity: Low]
Will this log message place the caret out of bounds and hide the user's input?
The glob string passed into wprobe_trigger_log_err() seems to be truncated by an earlier strsep() call replacing the delimiter with a null terminator:
kernel/trace/trace_events_trigger.c:trigger_process_regex() {
...
command = strsep(&next, ": \t");
...
}Since glob is truncated to just the command name, calculating the offset as type_name - glob can point far past the end of the printed text, making the syntax error logs illegible.
Quoted 2 lines
kernel/trace/trace_wprobe.c +2
-0+return -EINVAL ; + }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/178739053919.1520941.17662338993878200834.stgit@devnote2?part=12
Masami Hiramatsu (Google)
maintainerRe: [PATCH v13 12/12] tracing/wprobe: Support BTF struct offset resolution in set_wprobe trigger
Quoted 28 lines · On Sat, 22 Aug 2026 09:51:09 +0000 [email protected] wrote:
Quoted 13 lines
kernel/trace/trace_wprobe.c +9
-0@@ -990,6 +992,181 @@ static void wprobe_trigger_free(struct event_trigger_data *data) } } +#ifdef CONFIG_PROBE_EVENTS_BTF_ARGS + +static int get_offset_of_field (struct btf *btf,const struct btf_type *type,char *field_name) +{ +const struct btf_member *field; +int bitoffs =0 ; +u32 anon_offs; +char *next; +kernel/trace/trace_wprobe.c static void wprobe_trigger_free(struct event_trigger_data *data) · line 1013
Quoted 10 lines
kernel/trace/trace_wprobe.c +10
-0+ do { + next = strchr(field_name, '.'); + if (next) + *next++ = '\0'; + + field = btf_find_struct_member(btf, type, field_name, &anon_offs); + if (IS_ERR_OR_NULL(field)) + return -ENOENT; + + if (btf_type_kflag(type)) {[Severity: High]
Does this code check the kflag of the original parent type instead of the anonymous struct that actually contains the field?
Quoted 6 lines
btf_find_struct_member()can return a field from a nested anonymous struct, but if the parent struct and the anonymous struct have mismatched BTF kflags (for example, the parent has kflag=0 while the anonymous struct has kflag=1 because it contains bitfields), using the parent's kflag here will cause the bitfield size encoded in the upper 8 bits of the offset to be misinterpreted as part of the byte offset.
Oops, that's right! btf_find_struct_member() skips anonymous struct and get the field directly. Hmm, it seems the same issue in the trace_probe.c. Let me make a fix patch.
Quoted 49 lines
This can add a massively inflated value to bitoffs and result in a wild pointer dereference when the wprobe triggers.
Quoted 17 lines
kernel/trace/trace_wprobe.c +17
-0+/* Reject bitfield member access */ +if (BTF_MEMBER_BITFIELD_SIZE (field->offset)) +return -EINVAL ; + bitoffs += anon_offs +BTF_MEMBER_BIT_OFFSET (field->offset); + }else { + bitoffs += anon_offs + field->offset; + } + + field_name = next; +if (next) { + type =btf_type_skip_modifiers (btf, field->type,NULL ); +if (!type) +return -ENOENT ; + } + }while (next); +return bitoffs /BITS_PER_BYTE ; +}[ ... ]
Quoted 10 lines
kernel/trace/trace_wprobe.c +10
-0+static int wprobe_trigger_typecast_parse (char **field_str_ptr, +struct trace_event_file *file, +struct wprobe_trigger_data *wprobe_data, +const char *glob) +{ + struct btf *btf __free(btf_put) = NULL; +const struct btf_type *type; +char *assign_field; +char *event_field; +char *type_field;kernel/trace/trace_wprobe.c static void wprobe_trigger_free(struct event_trigger_data *data) · line 1055
Quoted 10 lines
kernel/trace/trace_wprobe.c +10
-0+char *type_name; +char *offs; +long val =0 ; +int id; +int adjust; + + type_name = *field_str_ptr +1 ; + event_field =strchr (type_name,')' ); +if (!event_field) { +wprobe_trigger_log_err (file, glob, type_name - glob,DEREF_OPEN_BRACE );[Severity: Low]
Will this log message place the caret out of bounds and hide the user's input?
Quoted 8 lines
The glob string passed into
wprobe_trigger_log_err()seems to be truncated by an earlierstrsep()call replacing the delimiter with a null terminator:kernel/trace/trace_events_trigger.c:trigger_process_regex() { ... command = strsep(&next, ": \t"); ... }
Oops, strsep modifies the buffer. OK. let me fix it.
Thanks!
Quoted 10 lines
Since glob is truncated to just the command name, calculating the offset as
type_name- glob can point far past the end of the printed text, making the syntax error logs illegible.Quoted 2 lines
(quoted diff) +2
-0+ return -EINVAL; + }Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/178739053919.1520941.17662338993878200834.stgit@devnote2?part=12
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>