tracing: wprobe: x86: Add wprobe for watchpoint
Subsystems (6)
Masami Hiramatsu (Google)
maintainerv11 cover tracing: wprobe: x86: Add wprobe for watchpoint
Hi,
Here is the 11th 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/178476134787.26117.10094977293012760490.stgit@devnote2/
The major changes in this version is to use Jinchao's new modify_local_hw_breakpoint_addr() API, add count option support to wprobe trigger, skip wprobe trigger during another wprobe trigger is working and rewrite selftests for wprobe trigger with trace-events-sample module.
Changes in v11:
- Updated hardware breakpoint modification API:
- Replaced arch_reinstall_hw_breakpoint() with arch_modify_local_hw_breakpoint_addr() and modify_local_hw_breakpoint_addr() API.
- Made DR7 updates NMI safe by using atomic cmpxchg for slot management and per-CPU sequence number (cpu_dr7_seq) to detect concurrent NMI updates.
- Refactored wprobe trigger implementation:
- Updated set_wprobe/clear_wprobe trigger to use modify_local_hw_breakpoint_addr().
- Added tracepoint_synchronize_unregister() in wprobe_unregister_trigger() and parse error path.
- Added NULL check for tw->bp_event in trace_wprobe_update_local() to prevent race conditions.
- Switched trigger data freeing to use event_trigger_data::private_data_free.
- Added count option support (count=N) for wprobe triggers.
- Fixed trigger address calculation (cast rec to char *).
- Added work_pending flag to avoid update conflicts between CPUs.
- Fixed wprobe_trigger_find_same() to check wprobe_data->clear flag.
- Update trigger-wprobe*.tc to use trace-events-sample kernel module.
- Fixed selftest:
- Updated trigger-wprobe.tc assertion logic for clear_wprobe trigger test to check trace log instead of sysfs enable file (which remains 1 under soft-disable).
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 probes/for-next.
Usage
-----
The basic usage of this wprobe is similar to other probes;
w:[GRP/][EVENT] [r|w|rw]@<ADDRESS|SYMBOL[+OFFS]> [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] [if FILTER] clear_wprobe:WPROBE_EVENT[:FIELD[+OFFSET]] [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,
---
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) (9):
HWBP: Add modify_local_hw_breakpoint_addr() API
tracing: wprobe: Add watchpoint probe event based on hardware breakpoint
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 wprobe event trigger
selftests: ftrace: Add wprobe trigger testcase tracing/wprobe: Support BTF typecast in fetchargs tracing/wprobe: Support BTF typecast in wprobe trigger command
Documentation/trace/index.rst | 1 Documentation/trace/wprobetrace.rst | 163 ++ arch/Kconfig | 18 arch/x86/Kconfig | 2 arch/x86/include/asm/debugreg.h | 36 - arch/x86/include/asm/hw_breakpoint.h | 2 arch/x86/kernel/cpu/mce/core.c | 20 arch/x86/kernel/hw_breakpoint.c | 125 +- arch/x86/kernel/nmi.c | 10 arch/x86/kernel/traps.c | 10 include/linux/hw_breakpoint.h | 6 include/linux/trace_events.h | 3 kernel/events/hw_breakpoint.c | 35 + 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 | 34 kernel/trace/trace_probe.h | 15 kernel/trace/trace_wprobe.c | 1392 ++++++++++++++++++++ tools/testing/selftests/ftrace/config | 3 .../ftrace/test.d/dynevent/add_remove_wprobe.tc | 63 + .../test.d/dynevent/wprobes_syntax_errors.tc | 21 .../test.d/trigger/trigger-wprobe-btf-offset.tc | 74 + .../test.d/trigger/trigger-wprobe-btf-typecast.tc | 72 + .../ftrace/test.d/trigger/trigger-wprobe.tc | 85 + 27 files changed, 2156 insertions(+), 76 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.tc
--
Masami Hiramatsu (Google) <[email protected]>
Masami Hiramatsu (Google)
maintainerPATCH v11 1/11 x86/hw_breakpoints: Make DR7 updates NMI safe
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.
--- arch/x86/include/asm/debugreg.h | 36 ++++++++++---- arch/x86/kernel/cpu/mce/core.c | 20 +++++--- arch/x86/kernel/hw_breakpoint.c | 98 +++++++++++++++++++-------------------- arch/x86/kernel/nmi.c | 10 +++- arch/x86/kernel/traps.c | 10 +++- 5 files changed, 101 insertions(+), 73 deletions(-)
arch/x86/include/asm/debugreg.h +26 -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,33 @@ 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 ); + }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 +48 -50
153 lines not on this page · raw message
arch/x86/kernel/nmi.c +8 -2
@@ -532,10 +532,13 @@ 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); +static DEFINE_PER_CPU(unsigned int, nmi_dr7_seq);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 +575,9 @@ DEFINE_IDTENTRY_RAW(exc_nmi)*/ sev_es_ist_enter (regs); -this_cpu_write (nmi_dr7,local_db_save ()); +local_db_save (&dr7, &dr7_seq); +this_cpu_write (nmi_dr7, dr7); +this_cpu_write (nmi_dr7_seq, dr7_seq); irq_state =irqentry_nmi_enter (regs);
@@ -594,7 +599,8 @@ DEFINE_IDTENTRY_RAW(exc_nmi)irqentry_nmi_exit (regs, irq_state); -local_db_restore (this_cpu_read (nmi_dr7)); +local_db_restore (this_cpu_read (nmi_dr7), +this_cpu_read (nmi_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)
Masami Hiramatsu (Google)
maintainerPATCH v11 2/11 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() to find the installed local slot and update only its address shadow and hardware debug address register. Publish the shadow first so hw_breakpoint_restore() observes the new address if an NMI interrupts the update.
The caller must provide an installed local event and a valid address. Slot ownership, breakpoint type, length, mask and DR7 remain unchanged.
---
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 | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 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 +33 -0
@@ -128,6 +128,39 @@ int arch_install_hw_breakpoint(struct perf_event *bp)return 0 ; } +int arch_modify_local_hw_breakpoint_addr (struct perf_event *bp, +unsigned long addr) +{ +struct arch_hw_breakpoint hw; +struct perf_event_attr attr = bp->attr; +unsigned int seq; +int i, ret; + +lockdep_assert_irqs_disabled (); + + attr.bp_addr = addr; + ret =hw_breakpoint_arch_parse (bp, &attr, &hw); +if (ret) +return ret; + +for (i =0 ; i <HBP_NUM ; i++) { +if (this_cpu_read (bp_per_reg[i]) == bp) +break ; + } + +if (WARN_ONCE (i ==HBP_NUM ,"Can't find any breakpoint slot" )) +return -ENOENT ; + +do { + seq =this_cpu_inc_return (cpu_dr7_seq); +this_cpu_write (cpu_debugreg[i], addr); +barrier (); +set_debugreg (addr, i); + }while (seq !=this_cpu_read (cpu_dr7_seq)); + +return 0 ; +} +/* * Uninstall the breakpoint contained in the given counter. *
Masami Hiramatsu (Google)
maintainerPATCH v11 3/11 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 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 | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 50 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 +35 -0
@@ -888,6 +888,41 @@ void unregister_wide_hw_breakpoint(struct perf_event * __percpu *cpu_events) }EXPORT_SYMBOL_GPL (unregister_wide_hw_breakpoint); +/** +* modify_local_hw_breakpoint_addr - update a local breakpoint address +* @bp: the hwbp perf event for this CPU +* @addr: the new address for @bp +* +* Update only the address of an installed breakpoint on the local CPU without +* releasing and reserving its hardware slot. The caller must update other CPUs. +* Return 0, or -EOPNOTSUPP if the architecture does not support this operation. +*/ +#ifdef CONFIG_HAVE_MODIFY_LOCAL_HW_BREAKPOINT_ADDR +int modify_local_hw_breakpoint_addr (struct perf_event *bp, +unsigned long addr) +{ +int ret; + +lockdep_assert_irqs_disabled (); + + ret =arch_modify_local_hw_breakpoint_addr (bp, addr); +if (ret) +return ret; + +counter_arch_bp (bp)->address = addr; + bp->attr.bp_addr = addr; + +return 0 ; +} +#else +int modify_local_hw_breakpoint_addr (struct perf_event *bp, +unsigned long addr) +{ +return -EOPNOTSUPP ; +} +#endif +EXPORT_SYMBOL_GPL (modify_local_hw_breakpoint_addr); +/** * hw_breakpoint_is_used - check if breakpoints are currently used *
Masami Hiramatsu (Google)
maintainerPATCH v11 4/11 tracing: wprobe: Add watchpoint probe event based on hardware breakpoint
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
Add a new probe event for the hardware breakpoint called wprobe-event. This wprobe allows user to trace (watch) the memory access at the specified memory address.
The new syntax is;
w[:[GROUP/]EVENT] [r|w|rw]@[ADDR|SYM][:SIZE] [FETCH_ARGs]
User also can use $addr to fetch the accessed address and $value to fetch the accessed memory value (shorthand for '+0($addr)'). No other variables are supported.
For example, tracing updates of the jiffies;
/sys/kernel/tracing # echo 'w:my_jiffies w@jiffies' >> dynamic_events /sys/kernel/tracing # cat dynamic_events w:wprobes/my_jiffies w@jiffies:4
/sys/kernel/tracing # echo 1 > events/wprobes/my_jiffies/enable /sys/kernel/tracing # head -n 20 trace | tail -n 5
# TASK-PID CPU# ||||| TIMESTAMP FUNCTION
# | | | ||||| | |
<idle>-0 [000] d.Z1. 206.547317: my_jiffies: (tick_do_update_jiffies64+0xbe/0x130)
<idle>-0 [000] d.Z1. 206.548341: my_jiffies: (tick_do_update_jiffies64+0xbe/0x130)
<idle>-0 [000] d.Z1. 206.549346: my_jiffies: (tick_do_update_jiffies64+0xbe/0x130)---
Changes in v11:
- Add WPROBE_NO_MAXACT error log to reject maxactive prefix on wprobe definitions.
Changes in v10:
- Use zalloc_flex() in alloc_trace_wprobe().
- Support matching command arguments in trace_wprobe_match().
Changes in v9:
- Rebased on probes/for-next branch.
- Add WPROBE_NO_SIBLING error log to explicitly reject sibling probes since event triggers identify the target wprobe by event name.
- Use traceprobe_parse_event_name() to properly validate group/event names instead of using the raw command string directly.
- Generate unique event name (w_0x<addr>) for anonymous address-based watchpoints to avoid naming collisions.
- Call traceprobe_update_arg() in __register_trace_wprobe() to resolve @symbol fetch arguments, consistent with kprobe and fprobe.
Changes in v8:
- Include required header files.
- Use READ_ONCE(tw->addr) in trace handler to safely check dynamically updated addresses.
- Prohibit unsafe perf support by returning -EOPNOTSUPP in wprobe_register().
- Add rollback logic to unregister already-enabled sibling probes if registration fails mid-loop.
- Resolve symbol offsets dynamically in trace_wprobe_show() using kallsyms_lookup_name().
- Fix memory leak of parse_address_spec()'s symbol output in
__trace_wprobe_create().
- Print "rw" instead of "x" for read-write type breakpoints in
trace_wprobe_show().
- Document the $value fetcharg in wprobetrace.rst.
Changes in v7:
- Include IS_ERR_PCPU fix.
- use seq_print_ip_sym_offset().
- fix checkpatch warning on DEFINE_FREE()
- Use bp->attr.bp_addr instead of tw->addr because it can be updated from another CPU.
---
Documentation/trace/index.rst | 1
Documentation/trace/wprobetrace.rst | 70 +++
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 | 746 +++++++++++++++++++++++++++++++++++
10 files changed, 874 insertions(+), 4 deletions(-)
create mode 100644 Documentation/trace/wprobetrace.rst
create mode 100644 kernel/trace/trace_wprobe.cDocumentation/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 +70 -0
70 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>]\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 +746 -0
746 lines not on this page · raw message
Masami Hiramatsu (Google)
maintainerPATCH v11 5/11 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
Masami Hiramatsu (Google)
maintainerPATCH v11 6/11 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
Masami Hiramatsu (Google)
maintainerPATCH v11 7/11 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 v11:
- Add WPROBE_NO_MAXACT syntax error case.
--- .../test.d/dynevent/wprobes_syntax_errors.tc | 21 ++++++++++++++++++++ 1 file changed, 21 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 +21 -0
@@ -0,0 +1,21 @@ +#!/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^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
Masami Hiramatsu (Google)
maintainerPATCH v11 8/11 tracing: wprobe: Add wprobe event trigger
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
Add wprobe event trigger to set and clear the watch event dynamically. This allows us to set an watchpoint on a given local variables and a slab object instead of static objects.
The trigger syntax is below:
- set_wprobe:WPROBE:FIELD[+OFFSET][:COUNT] [if FILTER]
- clear_wprobe:WPROBE[:FIELD[+OFFSET]][:COUNT] [if FILTER]
set_wprobe sets the address pointed by FIELD[+offset] to the WPROBE event. The FIELD is the field name of trigger event. clear_wprobe clears the watch address of WPROBE event. If the FIELD option is specified, it clears only if the current watch address is same as the given FIELD[+OFFSET] value.
COUNT is the max number of activating trigger.
The set_wprobe trigger does not change the type and length, these must be set when creating a new wprobe.
Also, 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.
---
Changes in v11:
- Use new modify_local_hw_breakpoint_addr() API.
- Add tracepoint_synchronize_unregister() in wprobe_unregister_trigger() and parse error path.
- Safely check tw->bp_event for NULL in trace_wprobe_update_local() to prevent race conditions.
- Use event_trigger_data::private_data_free.
- Add count option support.
- Fix trigger address calculation. (cast rec to char *)
- Add work_pending flag to avoid update conflict with other CPU.
- Fix to check wprobe_data->clear flag in wprobe_trigger_find_same().
Changes in v9:
- Make event_trigger_free() non-static to solve build dependency.
- Sync irq_work and work inside __unregister_trace_wprobe() before
clearing tw->bp_event to prevent concurrent NULL pointer dereference.
- Introduce private_free destructor in struct event_trigger_data to
safely release wprobe_trigger_data after tracepoint readers exit.
- Fix filter memory leak in wprobe_trigger_cmd_parse() on the error
handling path of trace_event_try_get_ref() failure.
- Avoid overwriting tw->addr on clear_wprobe trigger registration to
prevent active watchpoint corruption and hardware breakpoint leak.
Changes in v8:
- Redesign wprobe_trigger() to be safe in NMI/hardirq contexts by
deferring register updates to a workqueue via irq_work.
- Skip trigger execution and increment an atomic missed count
(tw->missed) if the tracepoint runs in NMI context to prevent
recursive spinlock deadlocks. (this is currently hidden counter)
- Prohibit attaching wprobe triggers to kprobe_events by checking
TRACE_EVENT_FL_KPROBE in wprobe_trigger_cmd_parse().
- Use call_rcu() in trigger deactivation path and add
synchronize_rcu() in parse failure path to ensure safe RCU
lifetime cleanup.
- Acquire the target tracepoint's module reference via
trace_event_try_get_ref() to prevent module refcount underflows.
- Fix event_trigger_data memory leak by properly freeing the
initial refcount in wprobe_trigger_cmd_parse() on success.
- Call on_each_cpu() with wait=true in wprobe_work_func() to
prevent use-after-free during trigger unregistration.
- Synchronize concurrent wprobe triggers on the same event by
using a shared raw spinlock (tw->lock).
- Drop the support of kprobe events (that should be done later).
Changes in v7:
- Use kzalloc_obj().
- Update sample code in document.
Changes in v6:
- Update according to the latest change of trigger ops.
Changes in v5:
- Following the suggestions, the documentation was revised to suit rst.
Changes in v3:
- Add FIELD option support for clear_wprobe and update document.
- Fix to unregister/free event_trigger_data on file correctly.
- Fix syntax comments.
Changes in v2:
- Getting local cpu perf_event from trace_wprobe directly.
- Remove trace_wprobe_local_perf() because it is conditionally unused.
- Make CONFIG_WPROBE_TRIGGERS a hidden config.
---
Documentation/trace/wprobetrace.rst | 93 ++++++
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_wprobe.c | 505 +++++++++++++++++++++++++++++++++++
6 files changed, 611 insertions(+), 1 deletion(-)Documentation/trace/wprobetrace.rst +93 -0
@@ -68,3 +68,96 @@ Here is an example to add a wprobe event on a variable `jiffies`. <idle>-0 [000] d.Z1. 717.026373: my_jiffies: (tick_do_update_jiffies64+0xbe/0x130) You can see the code which writes to `jiffies` is `tick_do_update_jiffies64()`. + +Combination with trigger action +------------------------------- +The event trigger action can extend the utilization of this wprobe. + +- set_wprobe:WPEVENT:FIELD[+|-ADJUST] +- clear_wprobe:WPEVENT[:FIELD[+|-]ADJUST] + +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. + +Notes: +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 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_wprobe.c +505 -0
552 lines not on this page · raw message
Masami Hiramatsu (Google)
maintainerPATCH v11 9/11 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.
---
Changes in v11:
- Update testcase to use trace-events-sample kernel module instead of VFS file operations.
--- tools/testing/selftests/ftrace/config | 1 .../ftrace/test.d/trigger/trigger-wprobe.tc | 85 ++++++++++++++++++++ 2 files changed, 86 insertions(+) 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.tc new file +85 -0
85 lines not on this page · raw message
Masami Hiramatsu (Google)
maintainerPATCH v11 10/11 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
Masami Hiramatsu (Google)
maintainerPATCH v11 11/11 tracing/wprobe: Support BTF typecast in wprobe trigger command
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
Extend the set_wprobe trigger syntax to support automatic BTF-based offset calculation using the form:
set_wprobe:WPEVENT:(TYPE[,ASGN])FIELD->MEMBER[.SUBMEMBER[...]]
Previously, the ADJUST value in FIELD[+/-ADJUST] had to be a numeric literal, requiring the user to know the exact byte offset of the target struct member.
With this change, if the FIELD portion starts with (STRUCTTYPE), the offset of MEMBER within STRUCTTYPE is automatically resolved via BTF. This allows symbolic, kernel-version-independent watchpoint placement at specific struct fields.
For example, to watch when the d_inode pointer inside a dentry is modified (not just when the dentry itself is accessed):
echo 'w:watch rw@0:8 address=$addr value=$value' >> dynamic_events
echo 'f:truncate do_truncate dentry=$arg2' >> dynamic_events
echo 'set_wprobe:watch:(dentry)dentry->d_inode' \
>> events/fprobes/truncate/triggerHere, "dentry" is a field in the fprobe event record. The BTF lookup resolves offsetof(struct dentry, d_inode) at set_wprobe parse time, so no manual numeric offset is needed.
---
Changes in v11:
- Fix BTF member offset calculation and bitfield check in get_offset_of_field().
- Update trigger-wprobe-btf-offset.tc to use trace-events-sample kernel module.
Changes in v10:
- Newly added.
--- kernel/trace/trace_wprobe.c | 201 +++++++++++++++++--- .../test.d/trigger/trigger-wprobe-btf-offset.tc | 74 +++++++ 2 files changed, 245 insertions(+), 30 deletions(-) create mode 100644 tools/testing/selftests/ftrace/test.d/trigger/trigger-wprobe-btf-offset.tc
kernel/trace/trace_wprobe.c +171 -30
@@ -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"
@@ -935,9 +936,10 @@ static int wprobe_trigger_print(struct seq_file *m,seq_printf (m,":count=%ld" , wprobe_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 ; }
@@ -976,6 +978,155 @@ 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 wprobe_trigger_data *wprobe_data) +{ + 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) +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) +return -EINVAL ; + *type_field ='\0' ; + type_field +=2 ; + + offs =strpbrk (type_field,"+-" ); +if (offs) { +if (kstrtol (offs,0 , &val) <0 ) +return -EINVAL ; + *offs ='\0' ; + } + +/* find type from BTF */ + id =bpf_find_btf_id (type_name,BTF_KIND_STRUCT , &btf); +if (id <0 ) +return id; + + type =btf_type_by_id (btf, id); +if (!type) +return -EINVAL ; + + adjust =get_offset_of_field (btf, type, type_field); +if (adjust <0 ) +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 ) +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 wprobe_trigger_data *wprobe_data) +{ +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) +{ +struct ftrace_event_field *field; +char *offs; + +if (field_str[0 ] =='(' ) { +int ret =wprobe_trigger_typecast_parse (&field_str, wprobe_data); + +if (ret <0 ) +return ret; + }else { + offs =strpbrk (field_str,"+-" ); +if (offs) { +long val; + +if (kstrtol (offs,0 , &val) <0 ) +return -EINVAL ; + wprobe_data->adjust = val; + *offs ='\0' ; + } + } + + field =trace_find_event_field (file->event_call, field_str); +if (!field) +return -ENOENT ; +if (field->size !=sizeof (void *)) +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,
@@ -987,11 +1138,9 @@ static int wprobe_trigger_cmd_parse(struct event_command *cmd_ops,*/ struct wprobe_trigger_data *wprobe_data __free(free_wprobe_trigger_data) = NULL; struct event_trigger_data *trigger_data __free(kfree) = NULL; -char *event_str, *field_str, *count_str; -struct ftrace_event_field *field =NULL ; +char *event_str, *count_str, *comment;struct trace_event_file *wprobe_file;struct trace_array *tr = file->tr; -struct trace_event_call *event;bool remove, clear = false;struct trace_wprobe *tw;char *param, *filter;
@@ -1002,6 +1151,10 @@ static int wprobe_trigger_cmd_parse(struct event_command *cmd_ops,if (!strcmp (cmd,CLEAR_WPROBE_STR )) clear = true; + comment =strchr (param_and_filter,'#' ); +if (comment) + *comment ='\0' ; +if (event_trigger_empty_param (param_and_filter))return -EINVAL ;
@@ -1032,34 +1185,22 @@ static int wprobe_trigger_cmd_parse(struct event_command *cmd_ops,/* clear_wprobe does not need field. */ if (!clear) { -char *offs; +char *field_str =strsep (¶m,":" ); -/* Find target field, which must be equivarent to "void *" */ - field_str =strsep (¶m,":" );if (!field_str)return -EINVAL ; - - offs =strpbrk (field_str,"+-" ); -if (offs) { -long val; - -if (kstrtol (offs,0 , &val) <0 ) -return -EINVAL ; - wprobe_data->adjust = val; - *offs ='\0' ; + ret =wprobe_trigger_field_parse (field_str, file, wprobe_data); +if (ret <0 ) +return ret; + }else if (param) { +char *orig_param = param; +char *field_str =strsep (¶m,":" ); + + ret =wprobe_trigger_field_parse (field_str, file, wprobe_data); +if (ret <0 ) { +/* field_str was not a field, so it must be count_str */ + param = orig_param; } - - event = file->event_call; - field =trace_find_event_field (event, field_str); -if (!field) -return -ENOENT ; - -if (field->size !=sizeof (void *)) -return -ENOEXEC ; - wprobe_data->offset = field->offset; - wprobe_data->field =kstrdup (field_str,GFP_KERNEL ); -if (!wprobe_data->field) -return -ENOMEM ; }/* count is optional, "unlimited" by default */
tools/testing/selftests/ftrace/test.d/trigger/trigger-wprobe-btf-offset.tc new file +74 -0
74 lines not on this page · raw message
Masami Hiramatsu (Google)
maintainerRe: [PATCH v11 08/11] tracing: wprobe: Add wprobe event trigger
In reply to Masami Hiramatsu (Google)Quoted 27 lines · On Sun, 2 Aug 2026 17:19:55 +0900 "Masami Hiramatsu (Google)"…
From: Masami Hiramatsu (Google) <[email protected]>
Add wprobe event trigger to set and clear the watch event dynamically. This allows us to set an watchpoint on a given local variables and a slab object instead of static objects.
The trigger syntax is below:
- set_wprobe:WPROBE:FIELD[+OFFSET][:COUNT] [if FILTER]
- clear_wprobe:WPROBE[:FIELD[+OFFSET]][:COUNT] [if FILTER]
set_wprobesets the address pointed by FIELD[+offset] to the WPROBE event. The FIELD is the field name of trigger event.clear_wprobeclears the watch address of WPROBE event. If the FIELD option is specified, it clears only if the current watch address is same as the given FIELD[+OFFSET] value.
COUNT is the max number of activating trigger.The
set_wprobetrigger does not change the type and length, these must be set when creating a new wprobe.Also, 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.
Assisted-by Antigravity:gemini-3.5-flashSigned-off-by Masami Hiramatsu (Google) <[email protected]>
Quoted 9 lines
---
Changes in v11:
- Use newmodify_local_hw_breakpoint_addr()API.
- Addtracepoint_synchronize_unregister()inwprobe_unregister_trigger()and parse error path.
- Safely checktw->bp_eventfor NULL intrace_wprobe_update_local()to prevent race conditions.
- Useevent_trigger_data::private_data_free.
- Add count option support.
Oops, I made a mistake on this count support.
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 1077
Quoted 13 lines
kernel/trace/trace_wprobe.c +13
-0+/* count is optional, "unlimited" by default */ + count_str =strsep (¶m,":" ); +if (count_str) { +long val; + +if (strcmp (count_str,"unlimited" )) { +if (str_has_prefix (count_str,"count=" )) + count_str +=6 ; +if (kstrtol (count_str,0 , &val) <0 ) +return -EINVAL ; + wprobe_data->count = val; + } + }
This handles count option, but
[...]
kernel/trace/trace_wprobe.c static __init int init_wprobe_trace(void) · line 1095
Quoted 3 lines
kernel/trace/trace_wprobe.c +3
-0+ ret =event_trigger_parse_num (param, trigger_data); +if (ret) +return ret;
Here we parse the count again. However, this trigger_data->count is ignored.
I think the new wprobe_trigger_data->count is redundant. it should use trigger_data->count.
Thank you,
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Peter Zijlstra
maintainerRe: [PATCH v11 01/11] x86/hw_breakpoints: Make DR7 updates NMI safe
In reply to Masami Hiramatsu (Google)Quoted 6 lines · 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.
Is KGDB really the only way to trip this? Mostly I think we let KGDB have the pieces if it does something 'funny'.
Quoted 16 lines
arch/x86/kernel/hw_breakpoint.c +2
-0@@ -40,6 +40,9 @@ DEFINE_PER_CPU(unsigned long, cpu_dr7);EXPORT_PER_CPU_SYMBOL (cpu_dr7); +/* Sequence number of the per-CPU DR7 state. */ +DEFINE_PER_CPU(unsigned int, cpu_dr7_seq);arch/x86/kernel/nmi.c +0
-0
arch/x86/kernel/nmi.c enum nmi_states { · line 535
Quoted 4 lines
arch/x86/kernel/nmi.c +1
-0@@ -532,10 +532,13 @@ 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); +static DEFINE_PER_CPU(unsigned int, nmi_dr7_seq);
This is weird, why have two distinct sequence numbers for dr7?
Peter Zijlstra
maintainerRe: [PATCH v11 04/11] tracing: wprobe: Add watchpoint probe event based on hardware breakpoint
In reply to Masami Hiramatsu (Google)Quoted 5 lines · Masami Hiramatsu (Google)
From: Masami Hiramatsu (Google) <[email protected]>
Add a new probe event for the hardware breakpoint called wprobe-event. This wprobe allows user to trace (watch) the memory access at the specified memory address.
s/the memory/the kernel memory/
Quoted 7 lines
The new syntax is;
w[:[GROUP/]EVENT] [r|w|rw]@[ADDR|SYM][:SIZE] [
FETCH_ARGs]User also can use $addr to fetch the accessed address and $value to fetch the accessed memory value (shorthand for '+0($addr)'). No other variables are supported.
This seems limited to kernel addresses only? Should not also userspace addresses be supported?
Masami Hiramatsu (Google)
maintainerQuoted 143 lines · On Sun, 2 Aug 2026 17:18:25 +0900 "Masami Hiramatsu (Google)"…
Hi,
Here is the 11th 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/178476134787.26117.10094977293012760490.stgit@devnote2/
The major changes in this version is to use Jinchao's new
modify_local_hw_breakpoint_addr()API, add count option support to wprobe trigger, skip wprobe trigger during another wprobe trigger is working and rewrite selftests for wprobe trigger with trace-events-sample module.Changes in v11:
- Updated hardware breakpoint modification API:
- Replacedarch_reinstall_hw_breakpoint()witharch_modify_local_hw_breakpoint_addr()andmodify_local_hw_breakpoint_addr()API.
- Made DR7 updates NMI safe by using atomic cmpxchg for slot management and per-CPU sequence number (cpu_dr7_seq) to detect concurrent NMI updates.
- Refactored wprobe trigger implementation:
- Updated set_wprobe/clear_wprobe trigger to usemodify_local_hw_breakpoint_addr().
- Addedtracepoint_synchronize_unregister()inwprobe_unregister_trigger()and parse error path.
- Added NULL check fortw->bp_eventintrace_wprobe_update_local()to prevent race conditions.
- Switched trigger data freeing to useevent_trigger_data::private_data_free.
- Added count option support (count=N) for wprobe triggers.
- Fixed trigger address calculation (cast rec to char *).
- Addedwork_pendingflag to avoid update conflicts between CPUs.
- Fixedwprobe_trigger_find_same()to checkwprobe_data->clearflag.
- Update trigger-wprobe*.tc to use trace-events-sample kernel module.
- Fixed selftest:
- Updated trigger-wprobe.tc assertion logic forclear_wprobetrigger test to check trace log instead of sysfs enable file (which remains 1 under soft-disable).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 probes/for-next.
Usage
-----The basic usage of this wprobe is similar to other probes;
w:[GRP/][EVENT] [r|w|rw]@<ADDRESS|SYMBOL[+OFFS]> [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_eventsecho 1 > events/wprobes/my_jiffies/enableMoreover, 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] [if FILTER] clear_wprobe:WPROBE_EVENT[:FIELD[+OFFSET]] [if FILTER]
set_wprobesets WPROBE_EVENT's watch address on FIELD[+OFFSET].clear_wprobeclears 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 bydentry_kill().
(Note: all tracefs setup uses '>>' so that it does not kickdo_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/hogeThen, 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
Quoted 3 lines
Thank you,
---
Oops, I found I missed base-commit: tag here...
I'll update my tool to add it automatically.
Thanks,
Quoted 55 lines
Jinchao Wang (2):
x86/hw_breakpoints: Make DR7 updates NMI safe x86/hw_breakpoints: Addarch_modify_local_hw_breakpoint_addr()APIMasami Hiramatsu (Google) (9):
HWBP: Addmodify_local_hw_breakpoint_addr()API
tracing: wprobe: Add watchpoint probe event based on hardware breakpoint
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 wprobe event trigger
selftests: ftrace: Add wprobe trigger testcase tracing/wprobe: Support BTF typecast in fetchargs tracing/wprobe: Support BTF typecast in wprobe trigger commandDocumentation/trace/index.rst | 1 Documentation/trace/wprobetrace.rst | 163 ++ arch/Kconfig | 18 arch/x86/Kconfig | 2 arch/x86/include/asm/debugreg.h | 36 - arch/x86/include/asm/hw_breakpoint.h | 2 arch/x86/kernel/cpu/mce/core.c | 20 arch/x86/kernel/hw_breakpoint.c | 125 +- arch/x86/kernel/nmi.c | 10 arch/x86/kernel/traps.c | 10 include/linux/hw_breakpoint.h | 6 include/linux/trace_events.h | 3 kernel/events/hw_breakpoint.c | 35 + 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 | 34 kernel/trace/trace_probe.h | 15 kernel/trace/trace_wprobe.c | 1392 ++++++++++++++++++++ tools/testing/selftests/ftrace/config | 3 .../ftrace/test.d/dynevent/add_remove_wprobe.tc | 63 + .../test.d/dynevent/wprobes_syntax_errors.tc | 21 .../test.d/trigger/trigger-wprobe-btf-offset.tc | 74 + .../test.d/trigger/trigger-wprobe-btf-typecast.tc | 72 + .../ftrace/test.d/trigger/trigger-wprobe.tc | 85 + 27 files changed, 2156 insertions(+), 76 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.tc--
Masami Hiramatsu (Google) <[email protected]>
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Masami Hiramatsu (Google)
maintainerRe: [PATCH v11 01/11] x86/hw_breakpoints: Make DR7 updates NMI safe
In reply to Peter ZijlstraQuoted 10 lines · On Mon, 3 Aug 2026 08:57:44 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 6 lines · 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.
Is KGDB really the only way to trip this? Mostly I think we let KGDB have the pieces if it does something 'funny'.
Hmm, I think even if so, this detection is better to be handled in hw_breakpoint layer. And I plan to use this from kprobe events, which is also a kind of NMI.
Quoted 12 lines
Quoted 11 lines
arch/x86/kernel/hw_breakpoint.c +2
-0@@ -40,6 +40,9 @@ DEFINE_PER_CPU(unsigned long, cpu_dr7);EXPORT_PER_CPU_SYMBOL (cpu_dr7); +/* Sequence number of the per-CPU DR7 state. */ +DEFINE_PER_CPU(unsigned int, cpu_dr7_seq);
Quoted 11 lines
arch/x86/kernel/nmi.c enum nmi_states { · line 535
Quoted 9 lines
arch/x86/kernel/nmi.c +1
-0@@ -532,10 +532,13 @@ 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); +static DEFINE_PER_CPU(unsigned int, nmi_dr7_seq);This is weird, why have two distinct sequence numbers for dr7?
nmi_dr7_seq is for sequence number of operation, which is for detecting dr7 overwrite in NMI.nmi_dr7 is for saving the DR7.
Thanks,
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Masami Hiramatsu (Google)
maintainerRe: [PATCH v11 04/11] tracing: wprobe: Add watchpoint probe event based on hardware breakpoint
In reply to Peter ZijlstraQuoted 8 lines · On Mon, 3 Aug 2026 09:03:56 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 5 lines · Masami Hiramatsu (Google)
From: Masami Hiramatsu (Google) <[email protected]>
Add a new probe event for the hardware breakpoint called wprobe-event. This wprobe allows user to trace (watch) the memory access at the specified memory address.
s/the memory/the kernel memory/
Indeed.
Quoted 11 lines
Quoted 8 lines
The new syntax is;
w[:[GROUP/]EVENT] [r|w|rw]@[ADDR|SYM][:SIZE] [
FETCH_ARGs]User also can use $addr to fetch the accessed address and $value to fetch the accessed memory value (shorthand for '+0($addr)'). No other variables are supported.
This seems limited to kernel addresses only? Should not also userspace addresses be supported?
Hmm, for x86, yes, since it causes a trap after executing (all page fault are handled). But other architecture like arm64 may not be able to support it because it needs to enable watchpoint after major page fault. At this step, I would like to limit it just for kernel memory.
Thank you,
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Peter Zijlstra
maintainerRe: [PATCH v11 01/11] x86/hw_breakpoints: Make DR7 updates NMI safe
In reply to Masami Hiramatsu (Google)Quoted 42 lines · Masami Hiramatsu
Quoted 10 lines · On Mon, 3 Aug 2026 08:57:44 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 6 lines · 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.
Is KGDB really the only way to trip this? Mostly I think we let KGDB have the pieces if it does something 'funny'.
Hmm, I think even if so, this detection is better to be handled in
hw_breakpointlayer. And I plan to use this from kprobe events, which is also a kind of NMI.Quoted 12 lines
Quoted 11 lines
arch/x86/kernel/hw_breakpoint.c +2
-0@@ -40,6 +40,9 @@ DEFINE_PER_CPU(unsigned long, cpu_dr7);EXPORT_PER_CPU_SYMBOL (cpu_dr7); +/* Sequence number of the per-CPU DR7 state. */ +DEFINE_PER_CPU(unsigned int, cpu_dr7_seq);Quoted 11 lines
arch/x86/kernel/nmi.c enum nmi_states { · line 535
Quoted 9 lines
arch/x86/kernel/nmi.c +1
-0@@ -532,10 +532,13 @@ 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); +static DEFINE_PER_CPU(unsigned int, nmi_dr7_seq);This is weird, why have two distinct sequence numbers for dr7?
Quoted 3 lines
nmi_dr7_seqis for sequence number of operation, which is for detecting dr7 overwrite in NMI.nmi_dr7is for saving the DR7.
I mean cpu_dr7_seq and nmi_dr7_seq.
Peter Zijlstra
maintainerRe: [PATCH v11 04/11] tracing: wprobe: Add watchpoint probe event based on hardware breakpoint
In reply to Masami Hiramatsu (Google)Quoted 23 lines · Masami Hiramatsu
Quoted 8 lines · On Mon, 3 Aug 2026 09:03:56 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 5 lines · Masami Hiramatsu (Google)
From: Masami Hiramatsu (Google) <[email protected]>
Add a new probe event for the hardware breakpoint called wprobe-event. This wprobe allows user to trace (watch) the memory access at the specified memory address.
s/the memory/the kernel memory/
Indeed.
Quoted 9 lines
Quoted 8 lines
The new syntax is;
w[:[GROUP/]EVENT] [r|w|rw]@[ADDR|SYM][:SIZE] [
FETCH_ARGs]User also can use $addr to fetch the accessed address and $value to fetch the accessed memory value (shorthand for '+0($addr)'). No other variables are supported.
Quoted 7 lines
Quoted 2 lines
This seems limited to kernel addresses only? Should not also userspace addresses be supported?
Hmm, for x86, yes, since it causes a trap after executing (all page fault are handled). But other architecture like arm64 may not be able to support it because it needs to enable watchpoint after major page fault. At this step, I would like to limit it just for kernel memory.
Sure, but suppose you were to add userspace, what would the ABI be?
That is, don't paint yourself in a corner etc.
Steven Rostedt
maintainerRe: [PATCH v11 04/11] tracing: wprobe: Add watchpoint probe event based on hardware breakpoint
In reply to Peter ZijlstraQuoted 10 lines · On Mon, 3 Aug 2026 09:03:56 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 7 lines
The new syntax is;
w[:[GROUP/]EVENT] [r|w|rw]@[ADDR|SYM][:SIZE] [
FETCH_ARGs]User also can use $addr to fetch the accessed address and $value to fetch the accessed memory value (shorthand for '+0($addr)'). No other variables are supported.
This seems limited to kernel addresses only? Should not also userspace addresses be supported?
Hmm, how would that work? The address is global for all tasks, so if you add a user space address, what task would that be for? The one writing the probe?
-- Steve
Peter Zijlstra
maintainerRe: [PATCH v11 04/11] tracing: wprobe: Add watchpoint probe event based on hardware breakpoint
Quoted 11 lines · Steven Rostedt
Quoted 8 lines · On Mon, 3 Aug 2026 09:03:56 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 7 lines
The new syntax is;
w[:[GROUP/]EVENT] [r|w|rw]@[ADDR|SYM][:SIZE] [
FETCH_ARGs]User also can use $addr to fetch the accessed address and $value to fetch the accessed memory value (shorthand for '+0($addr)'). No other variables are supported.
Quoted 6 lines
Quoted 2 lines
This seems limited to kernel addresses only? Should not also userspace addresses be supported?
Hmm, how would that work? The address is global for all tasks, so if you add a user space address, what task would that be for? The one writing the probe?
Schedule the watchpoint along with the task of course. Given there is breakpoint/perf integration this should be trivial.
Masami Hiramatsu (Google)
maintainerRe: [PATCH v11 04/11] tracing: wprobe: Add watchpoint probe event based on hardware breakpoint
In reply to Peter ZijlstraQuoted 27 lines · On Tue, 4 Aug 2026 01:08:40 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 26 lines · Masami Hiramatsu
Quoted 8 lines · On Mon, 3 Aug 2026 09:03:56 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 5 lines · Masami Hiramatsu (Google)
From: Masami Hiramatsu (Google) <[email protected]>
Add a new probe event for the hardware breakpoint called wprobe-event. This wprobe allows user to trace (watch) the memory access at the specified memory address.
s/the memory/the kernel memory/
Indeed.
Quoted 11 lines
Quoted 8 lines
The new syntax is;
w[:[GROUP/]EVENT] [r|w|rw]@[ADDR|SYM][:SIZE] [
FETCH_ARGs]User also can use $addr to fetch the accessed address and $value to fetch the accessed memory value (shorthand for '+0($addr)'). No other variables are supported.
This seems limited to kernel addresses only? Should not also userspace addresses be supported?
Quoted 6 lines
Quoted 4 lines
Hmm, for x86, yes, since it causes a trap after executing (all page fault are handled). But other architecture like arm64 may not be able to support it because it needs to enable watchpoint after major page fault. At this step, I would like to limit it just for kernel memory.
Sure, but suppose you were to add userspace, what would the ABI be?
Ahh, sorry. It was misreading, I mean it should be only for the kernel address, not for user space. So I don't want to add userspace. (I answered the first one)
Thank you,
Quoted 2 lines
That is, don't paint yourself in a corner etc.
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Masami Hiramatsu (Google)
maintainerRe: [PATCH v11 01/11] x86/hw_breakpoints: Make DR7 updates NMI safe
In reply to Peter ZijlstraQuoted 43 lines · On Tue, 4 Aug 2026 01:07:23 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 42 lines · Masami Hiramatsu
Quoted 10 lines · On Mon, 3 Aug 2026 08:57:44 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 6 lines · 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.
Is KGDB really the only way to trip this? Mostly I think we let KGDB have the pieces if it does something 'funny'.
Hmm, I think even if so, this detection is better to be handled in
hw_breakpointlayer. And I plan to use this from kprobe events, which is also a kind of NMI.Quoted 23 lines
Quoted 17 lines
arch/x86/kernel/hw_breakpoint.c +2
-0@@ -40,6 +40,9 @@ DEFINE_PER_CPU(unsigned long, cpu_dr7);EXPORT_PER_CPU_SYMBOL (cpu_dr7); +/* Sequence number of the per-CPU DR7 state. */ +DEFINE_PER_CPU(unsigned int, cpu_dr7_seq);arch/x86/kernel/nmi.c +0
-0arch/x86/kernel/nmi.c enum nmi_states { · line 535
Quoted 4 lines
arch/x86/kernel/nmi.c +1
-0@@ -532,10 +532,13 @@ 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); +static DEFINE_PER_CPU(unsigned int, nmi_dr7_seq);This is weird, why have two distinct sequence numbers for dr7?
Quoted 5 lines
Quoted 3 lines
nmi_dr7_seqis for sequence number of operation, which is for detecting dr7 overwrite in NMI.nmi_dr7is for saving the DR7.I mean
cpu_dr7_seqandnmi_dr7_seq.
Oops, Indeed. Let me fix it. Thanks!
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Steven Rostedt
maintainerRe: [PATCH v11 04/11] tracing: wprobe: Add watchpoint probe event based on hardware breakpoint
In reply to Peter ZijlstraQuoted 9 lines · On Wed, 5 Aug 2026 21:39:09 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 6 lines
Quoted 2 lines
This seems limited to kernel addresses only? Should not also userspace addresses be supported?
Hmm, how would that work? The address is global for all tasks, so if you add a user space address, what task would that be for? The one writing the probe?
Schedule the watchpoint along with the task of course. Given there is breakpoint/perf integration this should be trivial.
I meant, what task is it attached to? Usually the way a probe works is to create the probe and then enable it.
# cd /sys/kernel/tracing # echo 'w:my_jiffies w@jiffies:8 value=+0($addr)' >> dynamic_events # echo 1 > events/wprobes/my_jiffies/enable
If the address is in user space, which task would that address be for?
What would the API look like? Should we add a pid to that for a specific task?
Maybe use zero for current task?
-- Steve
Re: [PATCH v11 04/11] tracing: wprobe: Add watchpoint probe event based on hardware breakpoint
In reply to Masami Hiramatsu (Google)Quoted 35 lines · Masami Hiramatsu (Google)
Quoted 32 lines · On Tue, 4 Aug 2026 01:08:40 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 30 lines · Masami Hiramatsu
Quoted 8 lines · On Mon, 3 Aug 2026 09:03:56 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 5 lines · Masami Hiramatsu (Google)
From: Masami Hiramatsu (Google) <[email protected]>
Add a new probe event for the hardware breakpoint called wprobe-event. This wprobe allows user to trace (watch) the memory access at the specified memory address.
s/the memory/the kernel memory/
Indeed.
Quoted 11 lines
Quoted 8 lines
The new syntax is;
w[:[GROUP/]EVENT] [r|w|rw]@[ADDR|SYM][:SIZE] [
FETCH_ARGs]User also can use $addr to fetch the accessed address and $value to fetch the accessed memory value (shorthand for '+0($addr)'). No other variables are supported.
This seems limited to kernel addresses only? Should not also userspace addresses be supported?
Hmm, for x86, yes, since it causes a trap after executing (all page fault are handled). But other architecture like arm64 may not be able to support it because it needs to enable watchpoint after major page fault. At this step, I would like to limit it just for kernel memory.
Quoted 7 lines
Quoted 1 line
Sure, but suppose you were to add userspace, what would the ABI be?
Ahh, sorry. It was misreading, I mean it should be only for the kernel address, not for user space. So I don't want to add userspace. (I answered the first one)
Thank you,
Keeping wprobe limited to kernel addresses makes sense to me. Userspace watchpoints are already supported by the perf hardware-breakpoint API, and supporting them in wprobe would add unnecessary complexity.
An explicit address check could also make this boundary clear.
Quoted 4 lines
Quoted 2 lines
That is, don't paint yourself in a corner etc.
Peter Zijlstra
maintainerRe: [PATCH v11 04/11] tracing: wprobe: Add watchpoint probe event based on hardware breakpoint
In reply to Steven RostedtQuoted 16 lines · Steven Rostedt
Quoted 9 lines · On Wed, 5 Aug 2026 21:39:09 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 6 lines
Quoted 2 lines
This seems limited to kernel addresses only? Should not also userspace addresses be supported?
Hmm, how would that work? The address is global for all tasks, so if you add a user space address, what task would that be for? The one writing the probe?
Schedule the watchpoint along with the task of course. Given there is breakpoint/perf integration this should be trivial.
I meant, what task is it attached to? Usually the way a probe works is to create the probe and then enable it.
Quoted 7 lines
# cd /sys/kernel/tracing # echo 'w:my_jiffies w@jiffies:8 value=+0($addr)' >> dynamic_events # echo 1 > events/wprobes/my_jiffies/enableIf the address is in user space, which task would that address be for?
What would the API look like? Should we add a pid to that for a specific task?
Same as uprobes?
Masami Hiramatsu (Google)
maintainerRe: [PATCH v11 01/11] x86/hw_breakpoints: Make DR7 updates NMI safe
In reply to Masami Hiramatsu (Google)Quoted 31 lines · On Thu, 6 Aug 2026 09:07:35 +0900 Masami Hiramatsu (Google)…
Quoted 28 lines · On Tue, 4 Aug 2026 01:07:23 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 27 lines · Masami Hiramatsu
Quoted 10 lines · On Mon, 3 Aug 2026 08:57:44 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 6 lines · 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.
Is KGDB really the only way to trip this? Mostly I think we let KGDB have the pieces if it does something 'funny'.
Hmm, I think even if so, this detection is better to be handled in
hw_breakpointlayer. And I plan to use this from kprobe events, which is also a kind of NMI.Quoted 9 lines
Quoted 9 lines
arch/x86/kernel/hw_breakpoint.c +0
-0@@ -40,6 +40,9 @@ DEFINE_PER_CPU(unsigned long, cpu_dr7);EXPORT_PER_CPU_SYMBOL (cpu_dr7);
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
arch/x86/kernel/hw_breakpoint.c line 44
Quoted 2 lines
arch/x86/kernel/hw_breakpoint.c +2
-0+/* Sequence number of the per-CPU DR7 state. */ +DEFINE_PER_CPU(unsigned int, cpu_dr7_seq);
This is actual sequence number.
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
arch/x86/kernel/nmi.c enum nmi_states { · line 535
Quoted 10 lines
arch/x86/kernel/nmi.c +1
-0@@ -532,10 +532,13 @@ 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); +static DEFINE_PER_CPU(unsigned int, nmi_dr7_seq);
and this is a backup.
Quoted 10 lines
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
This is weird, why have two distinct sequence numbers for dr7?
nmi_dr7_seqis for sequence number of operation, which is for detecting dr7 overwrite in NMI.nmi_dr7is for saving the DR7.I mean
cpu_dr7_seqandnmi_dr7_seq.Oops, Indeed. Let me fix it. Thanks!
BTW, do we really need nmi_dr7 as a per-cpu variable?
Since this patch changes local_db_save()/restore() to handle both DR7 and its sequence number, we need to save/restore both. I thought it is used for handling nested NMI but in exc_nmi, we have this gate check before using the nmi_dr7.
if (this_cpu_read(nmi_state) != NMI_NOT_RUNNING) {
this_cpu_write(nmi_state, NMI_LATCHED);
return;
}Thus the nested NMI is handled serial, not stacked. So we can use a local variable instead of per-cpu variable.
Thank you,
Quoted 5 lines
Signature · 2 lines
-- Masami Hiramatsu (Google) <[email protected]>
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>