[PATCH v21 00/11] tracing: wprobe: x86: Add wprobe for watchpoint

From: Masami Hiramatsu (Google)

Date: Tue Dec 01 2026 - 09:12:02 EST


Hi,

Here is the 21st version of the series for adding new wprobe (watch
probe) which provides memory access tracing events. Moreover, this can
be used via event triggers, so it can trace memory access on dynamically
allocated objects too.
The previous version (v20) is here:

https://lore.kernel.org/all/179480675130.38065.3086970856119381693.stgit@devnote2/

In this version (v21), the series addresses all Sashiko AI review
findings from v20 along with a few additional edge cases:
- Check hardware DR7 against per-CPU cpu_dr7 enable bits in
arch_modify_local_hw_breakpoint_addr() and return -EBUSY when host
breakpoints are temporarily disabled or switched.
- Fix trace_wprobe_match_command_head() locking and module-unload
matching, trace_ctx underflow, stale-hit filtering, TASK_SIZE_MAX
validation, eprobe exclusion, and $addr:symstr support.
- Fix wprobe trigger lifecycle, teardown synchronization, concurrency,
orig_addr preservation, reentrancy, and error_log command truncation.

Note on enable_event/disable_event triggers:
While fixing the trigger teardown race for wprobe, we also noticed that
the existing enable_event/disable_event triggers can hit a similar rare
-EBUSY race when targeting a dynamic event, because
event_enable_trigger_free() defers trace_event_put_ref() to
trigger_kthread via trigger_data_free(). If userspace removes an
enable_event/disable_event trigger on a dynamic event and immediately
removes the event via dynamic_events before trigger_kthread runs,
trace_event_dyn_busy() can transiently return -EBUSY. (Unlike wprobe,
enable_event/disable_event does not have the clear_event_triggers() UAF
since event_enable_trigger() only modifies file->flags, which stays
alive until after tracepoint_synchronize_unregister().) For wprobe
triggers, we avoid both issues without adding per-trigger sync overhead
by holding a single event ref across tw->trigger_ref, batch-freeing
intermediate trigger removals via trigger_data_free(), and running
tracepoint_synchronize_unregister() once on the final trigger removal
before tearing down tw->bp_event and releasing the event ref
synchronously.

Detailed changes are here.

Changes in v21:
[01/11]
- Check hardware DR7 against cpu_dr7 enable bits and return -EBUSY
when host breakpoints are temporarily disabled or switched.
[04/11]
- Skip trace_probe_log_err() and kallsyms_lookup_name() in
trace_wprobe_match_command_head() via __parse_address_spec().
- Check READ_ONCE(tw->addr) != addr and acquire guard(preempt_notrace)()
in wprobe_perf_handler() before calling wprobe_trace_handler().
- Check TASK_SIZE_MAX instead of TASK_SIZE in
trace_wprobe_is_valid_addr().
- Exclude TRACE_EVENT_FL_WPROBE in trace_eprobe.c:find_and_get_event().
- Allow FETCH_OP_BADDR ($addr) with symstr in finalize_fetch_insn().
- Fix synopsis brackets and default length example in wprobetrace.rst.
[07/11]
- Fix test filename typo in commit message (wprobes_syntax_errors.tc).
[08/11]
- Reset tw->work_pending to 0 after cancel_work_sync() in
__unregister_trace_wprobe().
- Track per-wprobe trigger_ref/trigger_file with one event ref;
batch-free intermediate removals and sync once on final removal.
- Clear tw->bp_event under tw->lock before
unregister_wide_hw_breakpoint().
- Allow clear_wprobe-first and multi-event triggers in one instance;
reject manually enabled or cross-instance wprobes with -EBUSY.
- Keep immutable tw->orig_addr for show/match and restore tw->addr on
wprobe unregistration.
- Add per-CPU wprobe_trigger_busy reentrancy guard and move
irq_work_queue() outside tw->lock.
- Snapshot orig_cmd before tokenization, fix empty-param error offset
to strlen(glob), update wprobetrace.rst sample, and drop unused fields.
[09/11]
- Match count=[01] after registering set_wprobe:watch:timer+16:1 in
trigger-wprobe.tc before the post-sleep count=0 check.
- Use u16_arg and rw@-1:4 in trigger-wprobe-syntax-errors.tc for
32-bit x86 compatibility.
- Use clear_dynamic_events in cleanup_wprobe_triggers() in
trigger-wprobe.tc and trigger-wprobe-hotplug.tc.
- Add regression checks for clear_wprobe-first, -1 spec preservation,
clear_wprobe WPROBE_BUSY, and untruncated error_log output.
[10/11]
- Use clear_dynamic_events in cleanup_wprobe_triggers() in
trigger-wprobe-btf-typecast.tc.
[11/11]
- Pass orig_cmd and base_offset into wprobe_trigger_field_parse() and
wprobe_trigger_typecast_parse() for untruncated error logging.
- Use clear_dynamic_events in cleanup_wprobe_triggers() in
trigger-wprobe-btf-offset.tc.

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/probes/core.

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 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@-1: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: 20/20 #P:4
#
# _-----=> irqs-off/BH-disabled
# / _----=> need-resched
# | / _---=> hardirq/softirq
# || / _--=> preempt-depth
# ||| / _-=> migrate-disable
# |||| / delay
# TASK-PID CPU# ||||| TIMESTAMP FUNCTION
# | | | ||||| | |
run_target.sh-90 [002] ...1. 47.475343: truncate: (do_truncate+0x4/0xf0) dentry=0xffff88800500ef20
run_target.sh-90 [002] d.Z.. 47.475539: watch: (lookup_fast+0x48/0x110) address=0xffff88800500ef20 value=0x8200080
run_target.sh-90 [002] d.Z.. 47.475545: watch: (path_openat+0x118/0xaa0) address=0xffff88800500ef20 value=0x8200080
run_target.sh-90 [002] d.Z.. 47.475549: watch: (path_openat+0x137/0xaa0) address=0xffff88800500ef20 value=0x8200080
run_target.sh-90 [002] d.Z.. 47.475554: watch: (path_openat+0x1ce/0xaa0) address=0xffff88800500ef20 value=0x8200080
run_target.sh-90 [002] d.Z.. 47.475558: watch: (path_openat+0x94c/0xaa0) address=0xffff88800500ef20 value=0x8200080
run_target.sh-90 [002] ...1. 47.475562: truncate: (do_truncate+0x4/0xf0) dentry=0xffff88800500ef20
rm-869 [003] d.Z.. 47.475962: watch: (lookup_fast+0x48/0x110) address=0xffff88800500ef20 value=0x8200080
rm-869 [003] d.Z.. 47.475966: watch: (path_lookupat+0x11d/0x2a0) address=0xffff88800500ef20 value=0x8200080
rm-869 [003] d.Z.. 47.475975: watch: (lookup_fast+0x48/0x110) address=0xffff88800500ef20 value=0x8200080
rm-869 [003] d.Z.. 47.475979: watch: (path_lookupat+0x11d/0x2a0) address=0xffff88800500ef20 value=0x8200080
rm-869 [003] d.Z.. 47.475989: watch: (lookup_dcache+0x2f/0x80) address=0xffff88800500ef20 value=0x8200080
rm-869 [003] d.Z.. 47.475992: watch: (lookup_one_qstr_excl+0x3b/0xf0) address=0xffff88800500ef20 value=0x8200080
rm-869 [003] d.Z.. 47.475996: watch: (may_delete_dentry+0x27/0x1e0) address=0xffff88800500ef20 value=0x8200080
rm-869 [003] d.Z.. 47.476000: watch: (may_delete_dentry+0x124/0x1e0) address=0xffff88800500ef20 value=0x8200080
rm-869 [003] d.Z.. 47.476012: watch: (vfs_unlink+0x81/0x3b0) address=0xffff88800500ef20 value=0x8200080
rm-869 [003] d.Z1. 47.476050: watch: (d_make_discardable+0x21/0xc0) address=0xffff88800500ef20 value=0x8200080
rm-869 [003] d.Z1. 47.476054: watch: (d_make_discardable+0x3a/0xc0) address=0xffff88800500ef20 value=0x200080
rm-869 [003] ...2. 47.476056: dentry_kill: (dentry_kill+0x0/0x410) dentry=0xffff88800500ef20
rm-869 [003] .l.2. 47.476102: dentry_kill: (dentry_kill+0x0/0x410) dentry=0xffff88800500ef20

Thank you,

Jinchao Wang (1):
x86/hw_breakpoints: Add arch_modify_local_hw_breakpoint_addr() API

Masami Hiramatsu (Google) (10):
HWBP: Add modify_local_hw_breakpoint_addr() API
perf/hw_breakpoint: Add register_wide_hw_breakpoint_cpuslocked() 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: tracing: Add wprobe trigger testcases
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 | 194 ++
arch/Kconfig | 18 +
arch/x86/Kconfig | 2 +
arch/x86/include/asm/hw_breakpoint.h | 2 +
arch/x86/kernel/hw_breakpoint.c | 68 +
include/linux/hw_breakpoint.h | 15 +
include/linux/trace_events.h | 3 +
kernel/events/hw_breakpoint.c | 86 +-
kernel/trace/Kconfig | 24 +
kernel/trace/Makefile | 1 +
kernel/trace/trace.c | 9 +-
kernel/trace/trace.h | 6 +
kernel/trace/trace_eprobe.c | 3 +-
kernel/trace/trace_events_trigger.c | 2 +-
kernel/trace/trace_probe.c | 38 +-
kernel/trace/trace_probe.h | 22 +-
kernel/trace/trace_wprobe.c | 1882 +++++++++++++++++
tools/testing/selftests/ftrace/config | 3 +
.../test.d/dynevent/add_remove_wprobe.tc | 81 +
.../test.d/dynevent/wprobes_syntax_errors.tc | 28 +
.../trigger/trigger-wprobe-btf-offset.tc | 94 +
.../trigger/trigger-wprobe-btf-typecast.tc | 85 +
.../test.d/trigger/trigger-wprobe-hotplug.tc | 150 ++
.../trigger/trigger-wprobe-syntax-errors.tc | 42 +
.../ftrace/test.d/trigger/trigger-wprobe.tc | 157 ++
26 files changed, 3001 insertions(+), 15 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-hotplug.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


base-commit: 5239fd583f19bd54d0ee3776547904b7d4211964
--
Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx>