lib/sys_info: add a simple timer based memory corruption detector
PATCH 1/1 lib/sys_info: add a simple timer based memory corruption detector
During debugging some bios/hardware related nasty memory corruption issues, we found using periodic timer to monitor specific dram/mmio physical address is very useful for debugging, which acts like a basic software watchpoint.
For those bugs, who (and when) change(corrupt) those dram or mmio register is hard to trace, and sometimes even hardware jtag debugger can't help (say the physical address watchpoint doesn't work).
The biggest shortcoming is it can never capture the exact point like a hardware watchpoint, no matter how small the timer interval is set, the idea is trying to approach the point, hoping the caught context have enough debug info (which did help us in solving bios/hardware bugs)
The working flow is simple: after suspected address is identified, start periodic timer polling it to catch if its value is changed to target 'magic' value, then halt the cpu (better limit to have only one cpu online), or panic, or print out system information, so that the error environment is frozen for further check , or let kexec/kdump to record the vmore, etc.
All the settings are module parameters:
watch_interval_ms: SW watchpoint check interval in ms
paddr_dram_to_watch: Physical dram address to monitor.
target_dram_val: Expected value at the dram address that triggers the watchpoint.
paddr_mmio_to_watch: Physical mmio address to monitor. Must be 32-bit aligned.
target_mmio_val: Expected value at the mmio address that triggers the watchpoint.
panic_on_hit: Trigger kernel panic when watchpoint condition hits.
hang_on_hit: halt the CPU (wait for HW debugger)
This RFC is trying to show the idea and get feedback, and there are some todos:
* merge the dram/mmio interface to auto detect it's dram or mmio
* support runtime changing the address
* move the starting point earlier in boot phase
* currently is monitoring 'changing to a value', add support for 'changing from a value'
--- lib/sys_info.c | 104 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+)
lib/sys_info.c +104 -0
@@ -164,3 +164,107 @@ void sys_info(unsigned long si_mask) {__sys_info (si_mask ? : kernel_si_mask); } + +#ifdef CONFIG_SW_WATCHPOINT + +/* default 100 ms interval */ +static unsigned long watch_interval_ms =100 ; +module_param (watch_interval_ms, ulong,0644 ); +MODULE_PARM_DESC (watch_interval_ms,"SW watchpoint check interval in ms" ); + +static unsigned long paddr_dram_to_watch; +module_param (paddr_dram_to_watch, ulong,0644 ); +MODULE_PARM_DESC (paddr_dram_to_watch,"Physical DRAM address to watch" ); + +static unsigned long *vaddr_dram; + +static unsigned long target_dram_val; +module_param (target_dram_val, ulong,0644 ); +MODULE_PARM_DESC (target_dram_val,"Target DRAM value to trigger watchpoint" ); + +/* The MMIO address should be 32b aligned */ +static unsigned long paddr_mmio_to_watch; +module_param (paddr_mmio_to_watch, ulong,0644 ); +MODULE_PARM_DESC (paddr_mmio_to_watch,"Physical MMIO address to watch (32bit aligned)" ); + +static unsigned int *vaddr_mmio; + +static unsigned int target_mmio_val; +module_param (target_mmio_val, uint,0644 ); +MODULE_PARM_DESC (target_mmio_val,"Target MMIO value to trigger watchpoint" ); + +static bool panic_on_hit; +module_param (panic_on_hit, bool,0644 ); +MODULE_PARM_DESC (panic_on_hit,"Panic when watchpoint hits" ); + +static bool hang_on_hit; +module_param (hang_on_hit, bool,0644 ); +MODULE_PARM_DESC (hang_on_hit,"Hang when watchpoint hits" ); + +/* Stop the watchpoint timer after first hit */ +static bool check_once = true; +module_param (check_once, bool,0644 ); +MODULE_PARM_DESC (check_once,"Stop watching after first hit" ); + +static struct timer_list sw_watchpoint_timer; + +static void sw_watchpoint_timer_fn (struct timer_list *unused) +{ +bool hit = false; + +if (vaddr_mmio && (*vaddr_mmio == target_mmio_val)) { +pr_info ("MMIO [@0x%lx] hit the target value [0x%x]!\n" , + paddr_mmio_to_watch, target_mmio_val); + hit = true; + } + +if (vaddr_dram && (*vaddr_dram == target_dram_val)) { +pr_info ("DRAM [@0x%lx] hit the target value [0x%lx]!\n" , + paddr_dram_to_watch, target_dram_val); + hit = true; + } + +if (hit) { +sys_info (0 ); + +/* Useful for attaching HW debugger */ +if (hang_on_hit) { +pr_warn ("Will dead loop on this CPU\n" ); +while (1 ); + } + +/* Could be used to trigger kexec/kdump */ +if (panic_on_hit) +panic ("SW watchpoint hit!" ); + +if (check_once) +return ; + } + +mod_timer (&sw_watchpoint_timer, jiffies +msecs_to_jiffies (watch_interval_ms)); +} + +static int __init sw_watchpoint_timer_init (void ) +{ +if (paddr_mmio_to_watch) { + vaddr_mmio =ioremap (paddr_mmio_to_watch &PAGE_MASK ,PAGE_SIZE ); +if (!vaddr_mmio) +return -ENOMEM ; + + vaddr_mmio += (paddr_mmio_to_watch %PAGE_SIZE ) /4 ; + } + +if (paddr_dram_to_watch) { + vaddr_dram =phys_to_virt (paddr_dram_to_watch); +if (!vaddr_dram) +return -ENOMEM ; + } + +timer_setup (&sw_watchpoint_timer, sw_watchpoint_timer_fn,0 ); + sw_watchpoint_timer.expires = jiffies +msecs_to_jiffies (watch_interval_ms); +add_timer (&sw_watchpoint_timer); + +return 0 ; +} +core_initcall (sw_watchpoint_timer_init); +#endif
base-commit: e7ae89a0c97ce2b68b0983cd01eda67cf373517d
Signature · 1 line
2.39.5 (Apple Git-154)
Added few more people into Cc.
Quoted 8 lines · Feng Tang
During debugging some bios/hardware related nasty memory corruption issues, we found using periodic timer to monitor specific dram/mmio physical address is very useful for debugging, which acts like a basic software watchpoint.
For those bugs, who (and when) change(corrupt) those dram or mmio register is hard to trace, and sometimes even hardware jtag debugger can't help (say the physical address watchpoint doesn't work).
It seems that this approach helped you to debug a nasty problem. I am not sure why the other ways did not work.
Could you please provide some more information about the use case?
Ideally, please describe one particular situation where it helped. What was the bug, how it manifested, and how the crash dump helped to analyze it. Feel free to use generic names, like graphics card, or ssd disk, instead of exact producer names, ...
Quoted 23 lines
The biggest shortcoming is it can never capture the exact point like a hardware watchpoint, no matter how small the timer interval is set, the idea is trying to approach the point, hoping the caught context have enough debug info (which did help us in solving bios/hardware bugs)
The working flow is simple: after suspected address is identified, start periodic timer polling it to catch if its value is changed to target 'magic' value, then halt the cpu (better limit to have only one cpu online), or panic, or print out system information, so that the error environment is frozen for further check , or let kexec/kdump to record the vmore, etc.
All the settings are module parameters:
watch_interval_ms: SW watchpoint check interval in ms
paddr_dram_to_watch: Physical dram address to monitor.
target_dram_val: Expected value at the dram address that triggers the watchpoint.
paddr_mmio_to_watch: Physical mmio address to monitor. Must be 32-bit aligned.
target_mmio_val: Expected value at the mmio address that triggers the watchpoint.
panic_on_hit: Trigger kernel panic when watchpoint condition hits.
hang_on_hit: halt the CPU (wait for HW debugger)
Quoted 7 lines
This RFC is trying to show the idea and get feedback, and there are some todos:
* merge the dram/mmio interface to auto detect it's dram or mmio
* support runtime changing the address
* move the starting point earlier in boot phase
* currently is monitoring 'changing to a value', add support for 'changing from a value'
Sashiko AI has pointed out several possible problems, see
https://sashiko.dev/#/patchset/20260527034324.51136-1-feng.tang%40linux.alibaba.com
lib/sys_info.c
Quoted 2 lines
lib/sys_info.c +0
-0
I we agreed that this feature would be useful then it would deserve its own source file.
IMHO, it fits into the watchdog category. I would put it into kernel/watch_mem or so.
Best Regards,
Petr
Quoted 112 lines
lib/sys_info.c +104
-0@@ -164,3 +164,107 @@ void sys_info(unsigned long si_mask) {__sys_info (si_mask ? : kernel_si_mask); } + +#ifdef CONFIG_SW_WATCHPOINT + +/* default 100 ms interval */ +static unsigned long watch_interval_ms =100 ; +module_param (watch_interval_ms, ulong,0644 ); +MODULE_PARM_DESC (watch_interval_ms,"SW watchpoint check interval in ms" ); + +static unsigned long paddr_dram_to_watch; +module_param (paddr_dram_to_watch, ulong,0644 ); +MODULE_PARM_DESC (paddr_dram_to_watch,"Physical DRAM address to watch" ); + +static unsigned long *vaddr_dram; + +static unsigned long target_dram_val; +module_param (target_dram_val, ulong,0644 ); +MODULE_PARM_DESC (target_dram_val,"Target DRAM value to trigger watchpoint" ); + +/* The MMIO address should be 32b aligned */ +static unsigned long paddr_mmio_to_watch; +module_param (paddr_mmio_to_watch, ulong,0644 ); +MODULE_PARM_DESC (paddr_mmio_to_watch,"Physical MMIO address to watch (32bit aligned)" ); + +static unsigned int *vaddr_mmio; + +static unsigned int target_mmio_val; +module_param (target_mmio_val, uint,0644 ); +MODULE_PARM_DESC (target_mmio_val,"Target MMIO value to trigger watchpoint" ); + +static bool panic_on_hit; +module_param (panic_on_hit, bool,0644 ); +MODULE_PARM_DESC (panic_on_hit,"Panic when watchpoint hits" ); + +static bool hang_on_hit; +module_param (hang_on_hit, bool,0644 ); +MODULE_PARM_DESC (hang_on_hit,"Hang when watchpoint hits" ); + +/* Stop the watchpoint timer after first hit */ +static bool check_once = true; +module_param (check_once, bool,0644 ); +MODULE_PARM_DESC (check_once,"Stop watching after first hit" ); + +static struct timer_list sw_watchpoint_timer; + +static void sw_watchpoint_timer_fn (struct timer_list *unused) +{ +bool hit = false; + +if (vaddr_mmio && (*vaddr_mmio == target_mmio_val)) { +pr_info ("MMIO [@0x%lx] hit the target value [0x%x]!\n" , + paddr_mmio_to_watch, target_mmio_val); + hit = true; + } + +if (vaddr_dram && (*vaddr_dram == target_dram_val)) { +pr_info ("DRAM [@0x%lx] hit the target value [0x%lx]!\n" , + paddr_dram_to_watch, target_dram_val); + hit = true; + } + +if (hit) { +sys_info (0 ); + +/* Useful for attaching HW debugger */ +if (hang_on_hit) { +pr_warn ("Will dead loop on this CPU\n" ); +while (1 ); + } + +/* Could be used to trigger kexec/kdump */ +if (panic_on_hit) +panic ("SW watchpoint hit!" ); + +if (check_once) +return ; + } + +mod_timer (&sw_watchpoint_timer, jiffies +msecs_to_jiffies (watch_interval_ms)); +} + +static int __init sw_watchpoint_timer_init (void ) +{ +if (paddr_mmio_to_watch) { + vaddr_mmio =ioremap (paddr_mmio_to_watch &PAGE_MASK ,PAGE_SIZE ); +if (!vaddr_mmio) +return -ENOMEM ; + + vaddr_mmio += (paddr_mmio_to_watch %PAGE_SIZE ) /4 ; + } + +if (paddr_dram_to_watch) { + vaddr_dram =phys_to_virt (paddr_dram_to_watch); +if (!vaddr_dram) +return -ENOMEM ; + } + +timer_setup (&sw_watchpoint_timer, sw_watchpoint_timer_fn,0 ); + sw_watchpoint_timer.expires = jiffies +msecs_to_jiffies (watch_interval_ms); +add_timer (&sw_watchpoint_timer); + +return 0 ; +} +core_initcall (sw_watchpoint_timer_init); +#endif base-commit:
e7ae89a0c97ce2b68b0983cd01eda67cf373517dSignature · 1 line
2.39.5 (Apple Git-154)
Quoted 1 line · Petr Mladek
Added few more people into Cc.
Thanks!
Quoted 13 lines · Feng Tang
Quoted 8 lines
During debugging some bios/hardware related nasty memory corruption issues, we found using periodic timer to monitor specific dram/mmio physical address is very useful for debugging, which acts like a basic software watchpoint.
For those bugs, who (and when) change(corrupt) those dram or mmio register is hard to trace, and sometimes even hardware jtag debugger can't help (say the physical address watchpoint doesn't work).
It seems that this approach helped you to debug a nasty problem. I am not sure why the other ways did not work.
Quoted 6 lines
Could you please provide some more information about the use case?
Ideally, please describe one particular situation where it helped. What was the bug, how it manifested, and how the crash dump helped to analyze it. Feel free to use generic names, like graphics card, or ssd disk, instead of exact producer names, ...
One bug was some BIOS/HW config causing the OS boot easy to stall during systemd init phase, and then we simplified it by making it boot to console with a function-reduced rootfs, and always triggering 'segmentation fault' when running 'less' command.
By using GDB we found some static array of 'less' is corrupted before being initialized, and one byte in its memory is always '0x33' (checked by gdb), and at this stage the static array is in bss segment, and backed actually by kernel zero page, so it was an obvious memory corruption issue.
HW engineers tried to capture HW traces after the issue happened, but could not find valuable hints, as the corruption could happen long before running the 'less', and the trace of that time was gone.
Good thing is we have the physical address of kernel zero page, and the offset of the corrupted byte is fixed, say the address was A. But HW debugger failed to breakpoint the point that address A was written with '0x33', so we need some software watchpoint thing here.
We used this code to monitor/watchpoint 'writing 0x33 to A', and when it hit, we halted the system by 'while (1);' (we already make it a UP by adding 'nr_cpus=1' cmdline param), then HW people collected the HW trace they need and root caused it to be a bad config.
Another bug we met is, the 'ACPI_ENABLE" register was written with a strange value, while we are sure the kernel won't do it. Then we maximized kernel's own debug message and added more, with short time interval of this patch, we found the nearest action before the 'write' is kernel calling EFI time service, then reported it to firmware team, which turned out to be a firmware issue.
These 2 bugs both happened on an arm64 server.
We haven't used 'crash dump' yet in chasing real world issues, and I added the panic (crash dump) for potential usage.
Quoted 31 lines
Quoted 23 lines
The biggest shortcoming is it can never capture the exact point like a hardware watchpoint, no matter how small the timer interval is set, the idea is trying to approach the point, hoping the caught context have enough debug info (which did help us in solving bios/hardware bugs)
The working flow is simple: after suspected address is identified, start periodic timer polling it to catch if its value is changed to target 'magic' value, then halt the cpu (better limit to have only one cpu online), or panic, or print out system information, so that the error environment is frozen for further check , or let kexec/kdump to record the vmore, etc.
All the settings are module parameters:
watch_interval_ms: SW watchpoint check interval in ms
paddr_dram_to_watch: Physical dram address to monitor.
target_dram_val: Expected value at the dram address that triggers the watchpoint.
paddr_mmio_to_watch: Physical mmio address to monitor. Must be 32-bit aligned.
target_mmio_val: Expected value at the mmio address that triggers the watchpoint.
panic_on_hit: Trigger kernel panic when watchpoint condition hits.
hang_on_hit: halt the CPU (wait for HW debugger)Quoted 7 lines
This RFC is trying to show the idea and get feedback, and there are some todos:
* merge the dram/mmio interface to auto detect it's dram or mmio
* support runtime changing the address
* move the starting point earlier in boot phase
* currently is monitoring 'changing to a value', add support for 'changing from a value'
Quoted 2 lines
Sashiko AI has pointed out several possible problems, see
https://sashiko.dev/#/patchset/20260527034324.51136-1-feng.tang%40linux.alibaba.com
Thanks for the pointer, will handle them in next version.
Quoted 8 lines
lib/sys_info.c
Quoted 2 lines
lib/sys_info.c +0
-0I we agreed that this feature would be useful then it would deserve its own source file.
IMHO, it fits into the watchdog category. I would put it into
kernel/watch_memor so.
Great! This is exactly one suggestion I could get for this RFC.
Thanks,
Feng
Quoted 115 lines
Best Regards,
PetrQuoted 112 lines
lib/sys_info.c +104
-0@@ -164,3 +164,107 @@ void sys_info(unsigned long si_mask) {__sys_info (si_mask ? : kernel_si_mask); } + +#ifdef CONFIG_SW_WATCHPOINT + +/* default 100 ms interval */ +static unsigned long watch_interval_ms =100 ; +module_param (watch_interval_ms, ulong,0644 ); +MODULE_PARM_DESC (watch_interval_ms,"SW watchpoint check interval in ms" ); + +static unsigned long paddr_dram_to_watch; +module_param (paddr_dram_to_watch, ulong,0644 ); +MODULE_PARM_DESC (paddr_dram_to_watch,"Physical DRAM address to watch" ); + +static unsigned long *vaddr_dram; + +static unsigned long target_dram_val; +module_param (target_dram_val, ulong,0644 ); +MODULE_PARM_DESC (target_dram_val,"Target DRAM value to trigger watchpoint" ); + +/* The MMIO address should be 32b aligned */ +static unsigned long paddr_mmio_to_watch; +module_param (paddr_mmio_to_watch, ulong,0644 ); +MODULE_PARM_DESC (paddr_mmio_to_watch,"Physical MMIO address to watch (32bit aligned)" ); + +static unsigned int *vaddr_mmio; + +static unsigned int target_mmio_val; +module_param (target_mmio_val, uint,0644 ); +MODULE_PARM_DESC (target_mmio_val,"Target MMIO value to trigger watchpoint" ); + +static bool panic_on_hit; +module_param (panic_on_hit, bool,0644 ); +MODULE_PARM_DESC (panic_on_hit,"Panic when watchpoint hits" ); + +static bool hang_on_hit; +module_param (hang_on_hit, bool,0644 ); +MODULE_PARM_DESC (hang_on_hit,"Hang when watchpoint hits" ); + +/* Stop the watchpoint timer after first hit */ +static bool check_once = true; +module_param (check_once, bool,0644 ); +MODULE_PARM_DESC (check_once,"Stop watching after first hit" ); + +static struct timer_list sw_watchpoint_timer; + +static void sw_watchpoint_timer_fn (struct timer_list *unused) +{ +bool hit = false; + +if (vaddr_mmio && (*vaddr_mmio == target_mmio_val)) { +pr_info ("MMIO [@0x%lx] hit the target value [0x%x]!\n" , + paddr_mmio_to_watch, target_mmio_val); + hit = true; + } + +if (vaddr_dram && (*vaddr_dram == target_dram_val)) { +pr_info ("DRAM [@0x%lx] hit the target value [0x%lx]!\n" , + paddr_dram_to_watch, target_dram_val); + hit = true; + } + +if (hit) { +sys_info (0 ); + +/* Useful for attaching HW debugger */ +if (hang_on_hit) { +pr_warn ("Will dead loop on this CPU\n" ); +while (1 ); + } + +/* Could be used to trigger kexec/kdump */ +if (panic_on_hit) +panic ("SW watchpoint hit!" ); + +if (check_once) +return ; + } + +mod_timer (&sw_watchpoint_timer, jiffies +msecs_to_jiffies (watch_interval_ms)); +} + +static int __init sw_watchpoint_timer_init (void ) +{ +if (paddr_mmio_to_watch) { + vaddr_mmio =ioremap (paddr_mmio_to_watch &PAGE_MASK ,PAGE_SIZE ); +if (!vaddr_mmio) +return -ENOMEM ; + + vaddr_mmio += (paddr_mmio_to_watch %PAGE_SIZE ) /4 ; + } + +if (paddr_dram_to_watch) { + vaddr_dram =phys_to_virt (paddr_dram_to_watch); +if (!vaddr_dram) +return -ENOMEM ; + } + +timer_setup (&sw_watchpoint_timer, sw_watchpoint_timer_fn,0 ); + sw_watchpoint_timer.expires = jiffies +msecs_to_jiffies (watch_interval_ms); +add_timer (&sw_watchpoint_timer); + +return 0 ; +} +core_initcall (sw_watchpoint_timer_init); +#endif base-commit:
e7ae89a0c97ce2b68b0983cd01eda67cf373517dSignature · 1 line
2.39.5 (Apple Git-154)
Quoted 9 lines · On Wed, 27 May 2026 11:43:24 +0800 Feng Tang <[email protected]>…
During debugging some bios/hardware related nasty memory corruption issues, we found using periodic timer to monitor specific dram/mmio physical address is very useful for debugging, which acts like a basic software watchpoint.
For those bugs, who (and when) change(corrupt) those dram or mmio register is hard to trace, and sometimes even hardware jtag debugger can't help (say the physical address watchpoint doesn't work).
Quoted 5 lines
The biggest shortcoming is it can never capture the exact point like a hardware watchpoint, no matter how small the timer interval is set, the idea is trying to approach the point, hoping the caught context have enough debug info (which did help us in solving bios/hardware bugs)
Instead of using a timer, can't you use the function tracer? That is, have the value checked at *every* function call. You can easily add a custom callback that gets called when every function is executed.
See https://docs.kernel.org/trace/ftrace-uses.html
Quoted 51 lines
The working flow is simple: after suspected address is identified, start periodic timer polling it to catch if its value is changed to target 'magic' value, then halt the cpu (better limit to have only one cpu online), or panic, or print out system information, so that the error environment is frozen for further check , or let kexec/kdump to record the vmore, etc.
All the settings are module parameters:
watch_interval_ms: SW watchpoint check interval in ms
paddr_dram_to_watch: Physical dram address to monitor.
target_dram_val: Expected value at the dram address that triggers the watchpoint.
paddr_mmio_to_watch: Physical mmio address to monitor. Must be 32-bit aligned.
target_mmio_val: Expected value at the mmio address that triggers the watchpoint.
panic_on_hit: Trigger kernel panic when watchpoint condition hits.
hang_on_hit: halt the CPU (wait for HW debugger)This RFC is trying to show the idea and get feedback, and there are some todos:
* merge the dram/mmio interface to auto detect it's dram or mmio
* support runtime changing the address
* move the starting point earlier in boot phase
* currently is monitoring 'changing to a value', add support for 'changing from a value'Signed-off-by Feng Tang <[email protected]>lib/sys_info.c +22
-0+static void sw_watchpoint_timer_fn (struct timer_list *unused) +{ +bool hit = false; + +if (vaddr_mmio && (*vaddr_mmio == target_mmio_val)) { +pr_info ("MMIO [@0x%lx] hit the target value [0x%x]!\n" , + paddr_mmio_to_watch, target_mmio_val); + hit = true; + } + +if (vaddr_dram && (*vaddr_dram == target_dram_val)) { +pr_info ("DRAM [@0x%lx] hit the target value [0x%lx]!\n" , + paddr_dram_to_watch, target_dram_val); + hit = true; + } + +if (hit) { +sys_info (0 ); + +/* Useful for attaching HW debugger */ +if (hang_on_hit) { +pr_warn ("Will dead loop on this CPU\n" );
lib/sys_info.c void sys_info(unsigned long si_mask) · line 242
Quoted 10 lines
lib/sys_info.c +10
-0+while (1 ); + } + +/* Could be used to trigger kexec/kdump */ +if (panic_on_hit) +panic ("SW watchpoint hit!" ); + +if (check_once) +return ; + }
The above function would be:
static bool no_check;
static void sw_watchpoint_fn(unsigned long ip, unsigned long pip,
struct ftrace_ops *op, struct ftrace_regs *fregs)
{
bool hit = false;
int bit;
if (no_check)
return;
bit = ftrace_test_recursion_trylock(ip, parent_ip);
if (bit < 0)
return;
[ do the above code ]
if (check_once) {
// possibly call irq_work to unregister ftrace
no_check = true;
}
ftrace_test_recursion_unlock(bit);
}static struct ftrace_ops sw_watchpoint_fops = {
.func = sw_watchpoint_fn;
};[..]
Quoted 13 lines
lib/sys_info.c +13
-0+ +mod_timer (&sw_watchpoint_timer, jiffies +msecs_to_jiffies (watch_interval_ms)); +} + +static int __init sw_watchpoint_timer_init (void ) +{ +if (paddr_mmio_to_watch) { + vaddr_mmio =ioremap (paddr_mmio_to_watch &PAGE_MASK ,PAGE_SIZE ); +if (!vaddr_mmio) +return -ENOMEM ; + + vaddr_mmio += (paddr_mmio_to_watch %PAGE_SIZE ) /4 ; + }
lib/sys_info.c void sys_info(unsigned long si_mask) · line 265
Quoted 10 lines
lib/sys_info.c +10
-0+ +if (paddr_dram_to_watch) { + vaddr_dram =phys_to_virt (paddr_dram_to_watch); +if (!vaddr_dram) +return -ENOMEM ; + } + +timer_setup (&sw_watchpoint_timer, sw_watchpoint_timer_fn,0 ); + sw_watchpoint_timer.expires = jiffies +msecs_to_jiffies (watch_interval_ms); +add_timer (&sw_watchpoint_timer);
Instead of the above, have:
register_ftrace_function(&sw_watchpoint_fops);
-- Steve
Quoted 7 lines
lib/sys_info.c +5
-0+ + return 0; +} +core_initcall (sw_watchpoint_timer_init); +#endif base-commit:
e7ae89a0c97ce2b68b0983cd01eda67cf373517d
Add more reviewers as Petr suggested.
Quoted 18 lines · Steven Rostedt
Quoted 9 lines · On Wed, 27 May 2026 11:43:24 +0800 Feng Tang <[email protected]>…
During debugging some bios/hardware related nasty memory corruption issues, we found using periodic timer to monitor specific dram/mmio physical address is very useful for debugging, which acts like a basic software watchpoint.
For those bugs, who (and when) change(corrupt) those dram or mmio register is hard to trace, and sometimes even hardware jtag debugger can't help (say the physical address watchpoint doesn't work).
Quoted 5 lines
The biggest shortcoming is it can never capture the exact point like a hardware watchpoint, no matter how small the timer interval is set, the idea is trying to approach the point, hoping the caught context have enough debug info (which did help us in solving bios/hardware bugs)
Quoted 5 lines
Instead of using a timer, can't you use the function tracer? That is, have the value checked at *every* function call. You can easily add a custom callback that gets called when every function is executed.
Many thanks for the suggestion and detailed sample codes !
For the 2 bugs I mentioned in https://lore.kernel.org/lkml/[email protected]/,
this approach should make the software watchpoint hit much more
precisely, though the culprits were not kernel itself.
One thought is, adding this software hook to every function call could be a high cost for production kernel. For the kernel zero page corruption case, we already added a 3-seconds interval timer to detect similar issue, and also ftrace could be disabled for some kernel.
So I'm thinking to use your ftrace method as the base solution in next version, and in future provide an possible light weight option of using timer method. How do you think?
I will go test this method and sent the v1 soon, thanks
- Feng
Quoted 132 lines
Quoted 51 lines
The working flow is simple: after suspected address is identified, start periodic timer polling it to catch if its value is changed to target 'magic' value, then halt the cpu (better limit to have only one cpu online), or panic, or print out system information, so that the error environment is frozen for further check , or let kexec/kdump to record the vmore, etc.
All the settings are module parameters:
watch_interval_ms: SW watchpoint check interval in ms
paddr_dram_to_watch: Physical dram address to monitor.
target_dram_val: Expected value at the dram address that triggers the watchpoint.
paddr_mmio_to_watch: Physical mmio address to monitor. Must be 32-bit aligned.
target_mmio_val: Expected value at the mmio address that triggers the watchpoint.
panic_on_hit: Trigger kernel panic when watchpoint condition hits.
hang_on_hit: halt the CPU (wait for HW debugger)This RFC is trying to show the idea and get feedback, and there are some todos:
* merge the dram/mmio interface to auto detect it's dram or mmio
* support runtime changing the address
* move the starting point earlier in boot phase
* currently is monitoring 'changing to a value', add support for 'changing from a value'Signed-off-by Feng Tang <[email protected]>lib/sys_info.c +22
-0+static void sw_watchpoint_timer_fn (struct timer_list *unused) +{ +bool hit = false; + +if (vaddr_mmio && (*vaddr_mmio == target_mmio_val)) { +pr_info ("MMIO [@0x%lx] hit the target value [0x%x]!\n" , + paddr_mmio_to_watch, target_mmio_val); + hit = true; + } + +if (vaddr_dram && (*vaddr_dram == target_dram_val)) { +pr_info ("DRAM [@0x%lx] hit the target value [0x%lx]!\n" , + paddr_dram_to_watch, target_dram_val); + hit = true; + } + +if (hit) { +sys_info (0 ); + +/* Useful for attaching HW debugger */ +if (hang_on_hit) { +pr_warn ("Will dead loop on this CPU\n" );lib/sys_info.c void sys_info(unsigned long si_mask) · line 242
Quoted 10 lines
lib/sys_info.c +10
-0+while (1 ); + } + +/* Could be used to trigger kexec/kdump */ +if (panic_on_hit) +panic ("SW watchpoint hit!" ); + +if (check_once) +return ; + }The above function would be:
static bool
no_check;static void sw_watchpoint_fn(unsigned long ip, unsigned long pip,
structftrace_ops*op, structftrace_regs*fregs){ bool hit = false; int bit; if (no_check) return; bit = ftrace_test_recursion_trylock(ip, parent_ip); if (bit < 0) return; [ do the above code ] if (check_once) { // possibly call irq_work to unregister ftrace no_check = true; } ftrace_test_recursion_unlock(bit); }static struct ftrace_ops sw_watchpoint_fops = { .func = sw_watchpoint_fn; };[..]
Quoted 13 lines
lib/sys_info.c +13
-0+ +mod_timer (&sw_watchpoint_timer, jiffies +msecs_to_jiffies (watch_interval_ms)); +} + +static int __init sw_watchpoint_timer_init (void ) +{ +if (paddr_mmio_to_watch) { + vaddr_mmio =ioremap (paddr_mmio_to_watch &PAGE_MASK ,PAGE_SIZE ); +if (!vaddr_mmio) +return -ENOMEM ; + + vaddr_mmio += (paddr_mmio_to_watch %PAGE_SIZE ) /4 ; + }lib/sys_info.c void sys_info(unsigned long si_mask) · line 265
Quoted 10 lines
lib/sys_info.c +10
-0+ +if (paddr_dram_to_watch) { + vaddr_dram =phys_to_virt (paddr_dram_to_watch); +if (!vaddr_dram) +return -ENOMEM ; + } + +timer_setup (&sw_watchpoint_timer, sw_watchpoint_timer_fn,0 ); + sw_watchpoint_timer.expires = jiffies +msecs_to_jiffies (watch_interval_ms); +add_timer (&sw_watchpoint_timer);Instead of the above, have:
register_ftrace_function(&sw_watchpoint_fops);-- Steve
Quoted 7 lines
lib/sys_info.c +5
-0+ + return 0; +} +core_initcall (sw_watchpoint_timer_init); +#endif base-commit:
e7ae89a0c97ce2b68b0983cd01eda67cf373517d
Hi Steven,
Quoted 38 lines · Feng Tang
Add more reviewers as Petr suggested.
Quoted 18 lines · Steven Rostedt
Quoted 9 lines · On Wed, 27 May 2026 11:43:24 +0800 Feng Tang <[email protected]>…
During debugging some bios/hardware related nasty memory corruption issues, we found using periodic timer to monitor specific dram/mmio physical address is very useful for debugging, which acts like a basic software watchpoint.
For those bugs, who (and when) change(corrupt) those dram or mmio register is hard to trace, and sometimes even hardware jtag debugger can't help (say the physical address watchpoint doesn't work).
Quoted 5 lines
The biggest shortcoming is it can never capture the exact point like a hardware watchpoint, no matter how small the timer interval is set, the idea is trying to approach the point, hoping the caught context have enough debug info (which did help us in solving bios/hardware bugs)
Quoted 5 lines
Instead of using a timer, can't you use the function tracer? That is, have the value checked at *every* function call. You can easily add a custom callback that gets called when every function is executed.
Many thanks for the suggestion and detailed sample codes !
For the 2 bugs I mentioned in https://lore.kernel.org/lkml/[email protected]/,
this approach should make the software watchpoint hit much more
precisely, though the culprits were not kernel itself.One thought is, adding this software hook to every function call could be a high cost for production kernel. For the kernel zero page corruption case, we already added a 3-seconds interval timer to detect similar issue, and also ftrace could be disabled for some kernel.
Quoted 5 lines
So I'm thinking to use your ftrace method as the base solution in next version, and in future provide an possible light weight option of using timer method. How do you think?
I will go test this method and sent the v1 soon, thanks
I tried the ftrace_ops way, and it worked well also to hit the watchpoint. But the system is obviously slower comparing to the 100ms periodic timer way, for the same QEMU test setup, the 'systemd-analyze' cmd show the difference of boot time:
17.555s (kernel) + 3min 7.559s (userspace) vs 350ms (kernel) + 3.646s (userspace)
So I tend to still use the 'timer' way as the base and add an option to switch to ftrace_ops way.
For the nasty memory corruption issues I've worked on, many of them are triggered by device (like NIC), BIOS runtime service or silicon, and not by kernel itself.
And for kernel triggered ones, as inspired by your ftrace idea, I tried the 'fgraph_ops' in a hacked way, which has the 'retfunc' feature to insert the watchpoint check before exitting the function, and did help on catching the exact culprit function.
Thanks,
Feng