kernel: add a simple timer based software watchpoint
PATCH 1/1 kernel: add a simple timer based software watchpoint
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 of this method is it can never capture the exact point like a hardware watchpoint. The idea is trying to approach the point by adjusting the timer interval, hoping the caught context have enough debug info (which did help us in solving quite some 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 vmcore, etc.
One real use case was:
"
On an arm64 platform, some BIOS/HW config caused OS boot easy to stall in systemd init phase, then the reproducing was 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, some static array of 'less' is found corrupted before being written, and one byte in its memory is always '0x33'.At this stage the static array is in bss segment first, and backed by kernel zero page after first read, so it was an obvious memory corruption.
HW engineers tried to capture HW traces after the issue happened, but could not find valuable hints, as the corruption could happen long before 'less' is run, and the trace/context of that time was gone.
As physical address of kernel zero page was known, and offset of the corrupted byte was fixed, the address was A. But HW debugger failed to breakpoint the point that address A was written with '0x33'. Then this method was used to monitor 'writing 0x33 to A' with 30ms interval, and halted the system by 'while (1);' (the system was made to a UP by using 'nr_cpus=1' cmdline parameter) once hit, then HW people collected the HW trace they need and root caused it to be a bad config.
"
The culprits of memory corruption issues we met are mainly:
* broken devices (like ethernet card)
* BIOS runtime service
* silicon bugs
* kernel itself
As kernel already have many useful debug featues like slub_debug, kasan, kfence, kmemleak etc.., this method could be more fit for the upper three types.
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 provides the basic function, and there are more todoes:
* add a ftrace mode to do function level monitoring with ftrace hook, which is more accurate timing wise, as suggested by Steven Rostedt
* 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
* monitor a whole memory region
* currently is monitoring 'changing to a value', add support for 'changing from a value'
---
Changelog:
since RFC:
* move the code to separate kernel/watch_mem.c (Petr)
* add real world use case in commit log (Petr)
* tried several ftrace methods, though not included in V1 (Steven)
* address some comments from Sashiko: https://sashiko.dev/#/patchset/20260527034324.51136-1-feng.tang%40linux.alibaba.com
* simplify ioremap code
RFC: https://lore.kernel.org/lkml/[email protected]/
kernel/Makefile | 1 + kernel/watch_mem.c | 129 +++++++++++++++++++++++++++++++++++++++++++++ lib/Kconfig.debug | 8 +++ 3 files changed, 138 insertions(+) create mode 100644 kernel/watch_mem.c
kernel/Makefile +1 -0
@@ -105,6 +105,7 @@ obj-$(CONFIG_DETECT_HUNG_TASK) += hung_task.o obj-$(CONFIG_LOCKUP_DETECTOR) += watchdog.o obj-$(CONFIG_HARDLOCKUP_DETECTOR_BUDDY) += watchdog_buddy.o obj-$(CONFIG_HARDLOCKUP_DETECTOR_PERF) += watchdog_perf.o +obj-$(CONFIG_SOFTWARE_WATCHPOINT) += watch_mem.o obj-$(CONFIG_SECCOMP) += seccomp.o obj-$(CONFIG_RELAY) += relay.o obj-$(CONFIG_SYSCTL) += utsname_sysctl.o
kernel/watch_mem.c new file +129 -0
129 lines not on this page · raw message
lib/Kconfig.debug +8 -0
@@ -1076,6 +1076,14 @@ config MEM_ALLOC_PROFILING_DEBUG Adds warnings with helpful error messages for memory allocation profiling. +config SOFTWARE_WATCHPOINT + bool "Softwre watchpoint to watch memory corruption" + depends on DEBUG_KERNEL + help + Using periodc timer checking specific physical DRAM/MMIO address + to capture memory corruption triggered by kernel, hardware devices, + BIOS runtime code or silicon. + source "lib/Kconfig.kasan" source "lib/Kconfig.kfence" source "lib/Kconfig.kmsan"
base-commit: ef0c9f75a19532d7675384708fc8621e10850104
Signature · 1 line
2.39.5 (Apple Git-154)
Quoted 68 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).
The biggest shortcoming of this method is it can never capture the exact point like a hardware watchpoint. The idea is trying to approach the point by adjusting the timer interval, hoping the caught context have enough debug info (which did help us in solving quite some 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 vmcore, etc.
One real use case was:
"
On an arm64 platform, some BIOS/HW config caused OS boot easy to stall in systemd init phase, then the reproducing was 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, some static array of 'less' is found corrupted before being written, and one byte in its memory is always '0x33'.At this stage the static array is in bss segment first, and backed by kernel zero page after first read, so it was an obvious memory corruption.
HW engineers tried to capture HW traces after the issue happened, but could not find valuable hints, as the corruption could happen long before 'less' is run, and the trace/context of that time was gone.
As physical address of kernel zero page was known, and offset of the corrupted byte was fixed, the address was A. But HW debugger failed to breakpoint the point that address A was written with '
0x33'. Then this method was used to monitor 'writing0x33to A' with 30ms interval, and halted the system by 'while (1);' (the system was made to a UP by using 'nr_cpus=1' cmdline parameter) once hit, then HW people collected the HW trace they need and root caused it to be a bad config.
"The culprits of memory corruption issues we met are mainly:
* broken devices (like ethernet card)
* BIOS runtime service
* silicon bugs
* kernel itselfAs kernel already have many useful debug featues like
slub_debug, kasan, kfence, kmemleak etc.., this method could be more fit for the upper three types.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 9 lines
This provides the basic function, and there are more todoes:
* add a ftrace mode to do function level monitoring with ftrace hook, which is more accurate timing wise, as suggested by Steven Rostedt
* 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
* monitor a whole memory region
* currently is monitoring 'changing to a value', add support for 'changing from a value'
That really looks more like the kind of thing you would want to carry as a OOT hack for your special debugging needs :)
Signature · 3 lines
Cheers, David
Quoted 6 lines · David Hildenbrand
Quoted 2 lines · Feng Tang
* currently is monitoring 'changing to a value', add support for 'changing from a value'
That really looks more like the kind of thing you would want to carry as a OOT hack for your special debugging needs :)
Indeed.
Hi David,
Quoted 79 lines · David Hildenbrand (Arm)
Quoted 68 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).
The biggest shortcoming of this method is it can never capture the exact point like a hardware watchpoint. The idea is trying to approach the point by adjusting the timer interval, hoping the caught context have enough debug info (which did help us in solving quite some 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 vmcore, etc.
One real use case was:
"
On an arm64 platform, some BIOS/HW config caused OS boot easy to stall in systemd init phase, then the reproducing was 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, some static array of 'less' is found corrupted before being written, and one byte in its memory is always '0x33'.At this stage the static array is in bss segment first, and backed by kernel zero page after first read, so it was an obvious memory corruption.
HW engineers tried to capture HW traces after the issue happened, but could not find valuable hints, as the corruption could happen long before 'less' is run, and the trace/context of that time was gone.
As physical address of kernel zero page was known, and offset of the corrupted byte was fixed, the address was A. But HW debugger failed to breakpoint the point that address A was written with '
0x33'. Then this method was used to monitor 'writing0x33to A' with 30ms interval, and halted the system by 'while (1);' (the system was made to a UP by using 'nr_cpus=1' cmdline parameter) once hit, then HW people collected the HW trace they need and root caused it to be a bad config.
"The culprits of memory corruption issues we met are mainly:
* broken devices (like ethernet card)
* BIOS runtime service
* silicon bugs
* kernel itselfAs kernel already have many useful debug featues like
slub_debug, kasan, kfence, kmemleak etc.., this method could be more fit for the upper three types.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 9 lines
This provides the basic function, and there are more todoes:
* add a ftrace mode to do function level monitoring with ftrace hook, which is more accurate timing wise, as suggested by Steven Rostedt
* 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
* monitor a whole memory region
* currently is monitoring 'changing to a value', add support for 'changing from a value'
Quoted 2 lines
That really looks more like the kind of thing you would want to carry as a OOT hack for your special debugging needs :)
Thanks for the review and bringing up a very good question that whether the patch is worthy!
Personally, I think it is.
Firstly, IMHO, memory corruption is a common issue. Petr asked me to give some real-world examples, and I talked about two cases in
[1]. https://lore.kernel.org/lkml/[email protected]/
I have met more memory corruptions, and I would divide them to 2 types:
kernel-triggered and non-kernel (device/BIOS/silicon) triggered.
For non-kernel triggered: mainly are from devices (ethernet cards, USB controllers etc), many devices may corrupt kernel memory during dma transfer, suspend/resume cycle or warm reboot, like some version of Aquantia NIC , mainstream MLXCX5 NIC, or octeon-hcd, which are general hardware devices:
[2]. https://bugzilla.kernel.org/show_bug.cgi?id=217854
[3]. https://lore.kernel.org/lkml/[email protected]/
For kernel triggered:
* kernel array overflows is common case for memory corruption (which should be easy captured by watchpoint backed by ftrace method)
* kernel zero page case, which I don't know the detail but I guess it comes from some corruption to zero page similar like what I met.
[4]. https://lore.kernel.org/all/[email protected]/
As my own experience is very limited, and I think there should be much more real-world memory corruptions cases than I know.
And IMHO, these non-kernel trigged ones are more difficult to handle than kernel triggered once, as the symptom is more random, and hard to generalize a pattern.
Secondly, it is low cost and could be built into production kernel.
* It is under a "default n" config, so won't bother normal users/OSVs
* The binary is small when built into kernel, and won't cost kernel cycles when not enabled
* For production kernel running on big server fleets, the kasan/slub_debug option usually won't be enabled as affecting performance, so many kernel-triggered memory corruption can't be helped by them.
Thanks,
Feng
Quoted 5 lines
Signature · 4 lines
-- Cheers, David
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
This provides the basic function, and there are more todoes:
* add a ftrace mode to do function level monitoring with ftrace hook, which is more accurate timing wise, as suggested by Steven Rostedt
* 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
* monitor a whole memory region
* currently is monitoring 'changing to a value', add support for 'changing from a value'
Quoted 7 lines
Quoted 2 lines
That really looks more like the kind of thing you would want to carry as a OOT hack for your special debugging needs :)
Thanks for the review and bringing up a very good question that whether the patch is worthy!
Personally, I think it is.
The problem is that the interface is odd (kernel parameters) and the implementation is shaky (what happens if memory does not exist? What happens if we unplug memory?).
I don't think it's a good use of our time to polish all that up and then maintain it.
Signature · 3 lines
Cheers, David
Hi David,
Quoted 14 lines · David Hildenbrand (Arm)
Quoted 14 lines
Quoted 13 lines
Quoted 10 lines
This provides the basic function, and there are more todoes:
* add a ftrace mode to do function level monitoring with ftrace hook, which is more accurate timing wise, as suggested by Steven Rostedt
* 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
* monitor a whole memory region
* currently is monitoring 'changing to a value', add support for 'changing from a value'That really looks more like the kind of thing you would want to carry as a OOT hack for your special debugging needs :)
Quoted 8 lines
Quoted 4 lines
Thanks for the review and bringing up a very good question that whether the patch is worthy!
Personally, I think it is.
The problem is that the interface is odd (kernel parameters) and the implementation is shaky (what happens if memory does not exist? What happens if we unplug memory?).
The concern makes sense to me.
IIUC, the risk of using this patch is similar to the usage of "/dev/mem". Currently this kconfig depends on DEBUG_KERNEL, so how about making it
"depends on DEBUG_KERNEL && IO_STRICT_DEVMEM" (depends on STRICT_DEVMEM)
and warn clearly it is only for kernel issue debugging and "Use with care!" in kconfig help text and code comments?
As discussed in RFC patch review, this debug feature is similar to soft/hard lockup detector and task-hung detector, should I make the control interface to sysctl interface following them, and reduce the control knobs? The dram and mmio interfaces can be merged, and 'check_once', 'panic_on_hit' could be removed if necessary.
How do you think?
Thanks,
Feng
Quoted 7 lines
I don't think it's a good use of our time to polish all that up and then maintain it.
Signature · 3 lines
Cheers, David
Quoted 3 lines · Feng Tang
On Mon, Jun 22, 2026 at 04:13:37PM +0200, David Hildenbrand (Arm) wrote:
As discussed in RFC patch review, this debug feature is similar to soft/hard lockup detector and task-hung detector, should I make the control
How is this very specialized ad hoc debug magic in any way similar to generally useful and just working debug mechanism like the lockup or hung detector? Those are just turned on, do not need a boatload of command line parameters and are generally useful.
Your debug magic is a workaround for a disfunctional hardware debugger, which means it's going to be used by three people twice a year if at all. Seriously?
Thanks,
tglx
Quoted 5 lines · Thomas Gleixner
Quoted 3 lines · Feng Tang
On Mon, Jun 22, 2026 at 04:13:37PM +0200, David Hildenbrand (Arm) wrote:
As discussed in RFC patch review, this debug feature is similar to soft/hard lockup detector and task-hung detector, should I make the control
Quoted 8 lines
How is this very specialized ad hoc debug magic in any way similar to generally useful and just working debug mechanism like the lockup or hung detector? Those are just turned on, do not need a boatload of command line parameters and are generally useful.
Your debug magic is a workaround for a disfunctional hardware debugger, which means it's going to be used by three people twice a year if at all. Seriously?
Jup.
As a side note, I worked on hw/sw breakpoint support for VMs in QEMU in the past.
So if the problem can be reproduced in a VM, there is the possible alternative of attaching gdb to the gdbserver in QEMU and setting hw/sw breakpoints, including watchpoints. It's primarily focused on memory accesses, of course (not mmio).
Signature · 3 lines
Cheers, David
Quoted 15 lines · David Hildenbrand (Arm)
Quoted 5 lines · Thomas Gleixner
Quoted 3 lines · Feng Tang
On Mon, Jun 22, 2026 at 04:13:37PM +0200, David Hildenbrand (Arm) wrote:
As discussed in RFC patch review, this debug feature is similar to soft/hard lockup detector and task-hung detector, should I make the controlQuoted 8 lines
How is this very specialized ad hoc debug magic in any way similar to generally useful and just working debug mechanism like the lockup or hung detector? Those are just turned on, do not need a boatload of command line parameters and are generally useful.
Your debug magic is a workaround for a disfunctional hardware debugger, which means it's going to be used by three people twice a year if at all. Seriously?
Quoted 8 lines
Jup.
As a side note, I worked on hw/sw breakpoint support for VMs in QEMU in the past.
So if the problem can be reproduced in a VM, there is the possible alternative of attaching gdb to the gdbserver in QEMU and setting hw/sw breakpoints, including watchpoints. It's primarily focused on memory accesses, of course (not mmio).
Thanks for the tip! I will keep that in mind.
For the memory corruption issued I listed in the other mail, all of them are from baremetal environment, as many of them are hardware(device/processor related or S3/reboot related).
Thanks,
Feng
Quoted 4 lines
Signature · 3 lines
Cheers, David
Hi Thomas,
Thanks for reviewing this!
Quoted 9 lines · Thomas Gleixner
Quoted 3 lines · Feng Tang
On Mon, Jun 22, 2026 at 04:13:37PM +0200, David Hildenbrand (Arm) wrote:
As discussed in RFC patch review, this debug feature is similar to soft/hard lockup detector and task-hung detector, should I make the controlHow is this very specialized ad hoc debug magic in any way similar to generally useful and just working debug mechanism like the lockup or hung detector? Those are just turned on, do not need a boatload of command line parameters and are generally useful.
That's right. They are very useful and easy to use, as a big part of my time is dealing with all kinds of lockup/task-hung bugs :)
Quoted 3 lines
Your debug magic is a workaround for a disfunctional hardware debugger, which means it's going to be used by three people twice a year if at all. Seriously?
The HW debugger is a Lautebach TRACE32 one, and may not be disfunctional, as it works well for watchpoint virtual address and other daily job. My own guess is that it can only see virtual address and doesn't have the ability to do the virtual to physical address translation instantly to watch a _physical_ address. So I guess, not able to watchpoint a physical address may be common for HW debuggers (I could be very wrong).
As in https://lore.kernel.org/lkml/[email protected]/,
we also used this method to solve one issue that BIOS runtime service corrupting ACPI_ENABLE register issue.
Then I tried to recall some old memory corruption issues I've met before, and think about if there is some that could be captured by this method, one example was a static global array overflow issue, which corrupted some other global variables which was next to it in kernel bss segment.
But yes, as you pointed out, the frequency is low (all of the 3 happened in the past 6 months) for myself. And my wild guess is there could be other developers that meet similar issues :)
Thanks,
Feng
Quoted 5 lines
Thanks,
tglx
Quoted 15 lines · Feng Tang
Quoted 9 lines · Thomas Gleixner
Quoted 3 lines · Feng Tang
On Mon, Jun 22, 2026 at 04:13:37PM +0200, David Hildenbrand (Arm) wrote:
As discussed in RFC patch review, this debug feature is similar to soft/hard lockup detector and task-hung detector, should I make the controlHow is this very specialized ad hoc debug magic in any way similar to generally useful and just working debug mechanism like the lockup or hung detector? Those are just turned on, do not need a boatload of command line parameters and are generally useful.
That's right. They are very useful and easy to use, as a big part of my time is dealing with all kinds of lockup/task-hung bugs :)
Quoted 7 lines
Quoted 3 lines
Your debug magic is a workaround for a disfunctional hardware debugger, which means it's going to be used by three people twice a year if at all. Seriously?
The HW debugger is a Lautebach TRACE32 one, and may not be disfunctional, as it works well for watchpoint virtual address and other daily job. My own guess is that it can only see virtual address and doesn't have the
Guessing is the worst engineering principle as I told you before.
Quoted 3 lines
ability to do the virtual to physical address translation instantly to watch a _physical_ address. So I guess, not able to watchpoint a physical address may be common for HW debuggers (I could be very wrong).
If the hardware debugger and the underlying CPU facility (ETM on ARM64 IIRC) does not support triggers on physical addresses and you already concluded from other information that the problem is in the BIOS, then tracing the kernel with it's virt/phys translation is not going to work. You obviously have to use the BIOS translation which might be very different, no?
Quoted 3 lines
As in https://lore.kernel.org/lkml/[email protected]/,
we also used this method to solve one issue that BIOS runtime service corruptingACPI_ENABLEregister issue.
Again, if the BIOS runtime service changes virt/phys translation the you have to trace the BIOS not the kernel. It's pretty obvious, no?
Quoted 4 lines
Then I tried to recall some old memory corruption issues I've met before, and think about if there is some that could be captured by this method, one example was a static global array overflow issue, which corrupted some other global variables which was next to it in kernel bss segment.
No. This is just all catching the problem after the fact with no trace and conclusive information about the root cause. The tools are there, you just have to use them correctly. But sure creating magic hacks which by chance give you the same information is way better...
Quoted 3 lines
But yes, as you pointed out, the frequency is low (all of the 3 happened in the past 6 months) for myself. And my wild guess is there could be other developers that meet similar issues :)
Can you for once have an informed opinion instead of wild guesses?
Thanks,
tglx
Quoted 20 lines · Thomas Gleixner
Quoted 19 lines · Feng Tang
Quoted 9 lines · Thomas Gleixner
Quoted 3 lines · Feng Tang
On Mon, Jun 22, 2026 at 04:13:37PM +0200, David Hildenbrand (Arm) wrote:
As discussed in RFC patch review, this debug feature is similar to soft/hard lockup detector and task-hung detector, should I make the controlHow is this very specialized ad hoc debug magic in any way similar to generally useful and just working debug mechanism like the lockup or hung detector? Those are just turned on, do not need a boatload of command line parameters and are generally useful.
That's right. They are very useful and easy to use, as a big part of my time is dealing with all kinds of lockup/task-hung bugs :)
Quoted 3 lines
Your debug magic is a workaround for a disfunctional hardware debugger, which means it's going to be used by three people twice a year if at all. Seriously?
Quoted 5 lines
Quoted 3 lines
The HW debugger is a Lautebach TRACE32 one, and may not be disfunctional, as it works well for watchpoint virtual address and other daily job. My own guess is that it can only see virtual address and doesn't have the
Guessing is the worst engineering principle as I told you before.
Aha, indeed. Will reduce similar usage. thanks!
Quoted 10 lines
Quoted 3 lines
ability to do the virtual to physical address translation instantly to watch a _physical_ address. So I guess, not able to watchpoint a physical address may be common for HW debuggers (I could be very wrong).
If the hardware debugger and the underlying CPU facility (ETM on ARM64 IIRC) does not support triggers on physical addresses and you already concluded from other information that the problem is in the BIOS, then tracing the kernel with it's
virt/phystranslation is not going to work. You obviously have to use the BIOS translation which might be very different, no?
I didn't explain the issue clearly. The order for solving this issue was, we first used this method to halt (while (1) dead loop) the system when detecting the memory corruption, silicon engineers gathered hardware traces, then root caused it. Before that, we didn't know it's a BIOS issue, as the initial symptom was random user space "segmentation fault"
Quoted 6 lines
Quoted 3 lines
As in https://lore.kernel.org/lkml/[email protected]/,
we also used this method to solve one issue that BIOS runtime service corruptingACPI_ENABLEregister issue.Again, if the BIOS runtime service changes
virt/phystranslation the you have to trace the BIOS not the kernel. It's pretty obvious, no?
Similarly, I didn't make it clear that the issue was not about address translation.
The bug report I got from test engineers was, the ACPI_ENABLE register has right value from BIOS boot message, and after booting to OS, it was changed to a strange value. So initially the suspect was us OS guy :). And we used the 'approching" policy of the method, checked the kernel logs (we added many debug ones) before the corruption was detected, and found right before the corruption, there was a RTC runtime service calling record, and asked BIOS engineer to check, which root caused it.
So the idea was to find the activites before the happening of "corrution", and check if there was some clues.
Quoted 9 lines
Quoted 4 lines
Then I tried to recall some old memory corruption issues I've met before, and think about if there is some that could be captured by this method, one example was a static global array overflow issue, which corrupted some other global variables which was next to it in kernel bss segment.
No. This is just all catching the problem after the fact with no trace and conclusive information about the root cause. The tools are there, you just have to use them correctly. But sure creating magic hacks which by chance give you the same information is way better...
This issue was interesting. It showed up as a NULL pointer panic, and I found it's a global variable (in bss segment) being corrupted (which shouldn't happen logically). As it didn't happened on normal platforms, but one platform with special config, we think it could be silicon related, and sent it to silicon team, who did root cause it with gathering/analyzing silicon traces to be an array overflow issue, as the special config make that array much longer.
My thought was if I used this method, I could have found the corruption happen right after the initialization of the module which has that array.
Thanks,
Feng
Quoted 9 lines
Quoted 3 lines
But yes, as you pointed out, the frequency is low (all of the 3 happened in the past 6 months) for myself. And my wild guess is there could be other developers that meet similar issues :)
Can you for once have an informed opinion instead of wild guesses?
Thanks,
tglx
Quoted 10 lines · Feng Tang
Quoted 10 lines
Quoted 3 lines
ability to do the virtual to physical address translation instantly to watch a _physical_ address. So I guess, not able to watchpoint a physical address may be common for HW debuggers (I could be very wrong).
If the hardware debugger and the underlying CPU facility (ETM on ARM64 IIRC) does not support triggers on physical addresses and you already concluded from other information that the problem is in the BIOS, then tracing the kernel with it's
virt/phystranslation is not going to work. You obviously have to use the BIOS translation which might be very different, no?
Forgot to metion, the problem of BIOS was actually enabling some experimental silicon feature, which it should not for this hardware, and then the BIOS wasn't involved in the detail context of the issue.
Thanks,
Feng
Quoted 6 lines
I didn't explain the issue clearly. The order for solving this issue was, we first used this method to halt (while (1) dead loop) the system when detecting the memory corruption, silicon engineers gathered hardware traces, then root caused it. Before that, we didn't know it's a BIOS issue, as the initial symptom was random user space "segmentation fault"
Quoted 12 lines · Feng Tang
Quoted 4 lines
No. This is just all catching the problem after the fact with no trace and conclusive information about the root cause. The tools are there, you just have to use them correctly. But sure creating magic hacks which by chance give you the same information is way better...
This issue was interesting. It showed up as a NULL pointer panic, and I found it's a global variable (in bss segment) being corrupted (which shouldn't happen logically). As it didn't happened on normal platforms, but one platform with special config, we think it could be silicon related, and sent it to silicon team, who did root cause it with gathering/analyzing silicon traces to be an array overflow issue, as the special config make that array much longer.
Quoted 2 lines
My thought was if I used this method, I could have found the corruption happen right after the initialization of the module which has that array.
In RFC review, Steven suggested using ftrace hook way instead of timer. If we inject the monitoring into the ftrace hook on function exit path, it should be able to catch exactly which function corrupt this variable.
Thanks,
Feng
Quoted 12 lines · Feng Tang
Quoted 10 lines · Thomas Gleixner
Quoted 3 lines
ability to do the virtual to physical address translation instantly to watch a _physical_ address. So I guess, not able to watchpoint a physical address may be common for HW debuggers (I could be very wrong).
If the hardware debugger and the underlying CPU facility (ETM on ARM64 IIRC) does not support triggers on physical addresses and you already concluded from other information that the problem is in the BIOS, then tracing the kernel with it's
virt/phystranslation is not going to work. You obviously have to use the BIOS translation which might be very different, no?
Quoted 5 lines
I didn't explain the issue clearly. The order for solving this issue was, we first used this method to halt (while (1) dead loop) the system when detecting the memory corruption, silicon engineers gathered hardware traces, then root caused it. Before that, we didn't know it's a BIOS issue, as the initial symptom was random user space "segmentation fault"
Sure the initial symptom was a user space fault and you could not explain it. But you really don't need your magic hack to figure out that it tripped over a corrupted byte in the zero page or wherever.
Once you have that figured out and established that it's reproducible then you add a watchpoint on that address in the kernel which won't trigger. So that excludes the kernel and points to the BIOS, which in turn makes you put a watchpoint on the BIOS translation.
If you need that hack to decode it, then you should rethink your approach to structured problem analysis and deduction.
Quoted 18 lines
Quoted 6 lines
Quoted 3 lines
As in https://lore.kernel.org/lkml/[email protected]/,
we also used this method to solve one issue that BIOS runtime service corruptingACPI_ENABLEregister issue.Again, if the BIOS runtime service changes
virt/phystranslation the you have to trace the BIOS not the kernel. It's pretty obvious, no?Similarly, I didn't make it clear that the issue was not about address translation.
The bug report I got from test engineers was, the
ACPI_ENABLEregister has right value from BIOS boot message, and after booting to OS, it was changed to a strange value. So initially the suspect was us OS guy :). And we used the 'approching" policy of the method, checked the kernel logs (we added many debug ones) before the corruption was detected, and found right before the corruption, there was a RTC runtime service calling record, and asked BIOS engineer to check, which root caused it.
Quoted 2 lines
So the idea was to find the activites before the happening of "corrution", and check if there was some clues.
Again. You failed to structure the problem and use the tools correctly.
Quoted 10 lines
Quoted 9 lines
Quoted 4 lines
Then I tried to recall some old memory corruption issues I've met before, and think about if there is some that could be captured by this method, one example was a static global array overflow issue, which corrupted some other global variables which was next to it in kernel bss segment.
No. This is just all catching the problem after the fact with no trace and conclusive information about the root cause. The tools are there, you just have to use them correctly. But sure creating magic hacks which by chance give you the same information is way better...
Quoted 6 lines
This issue was interesting. It showed up as a NULL pointer panic, and I found it's a global variable (in bss segment) being corrupted (which shouldn't happen logically). As it didn't happened on normal platforms, but one platform with special config, we think it could be silicon related, and sent it to silicon team, who did root cause it with gathering/analyzing silicon traces to be an array overflow issue, as the special config make that array much longer.
Your debug war stories are amazing, but in the wrong way and do not justify to shove a completely ill defined barely usable hack into the kernel to be maintained forever.
Thanks,
tglx
Quoted 11 lines · Thomas Gleixner
Quoted 11 lines
Similarly, I didn't make it clear that the issue was not about address translation.
The bug report I got from test engineers was, the
ACPI_ENABLEregister has right value from BIOS boot message, and after booting to OS, it was changed to a strange value. So initially the suspect was us OS guy :). And we used the 'approching" policy of the method, checked the kernel logs (we added many debug ones) before the corruption was detected, and found right before the corruption, there was a RTC runtime service calling record, and asked BIOS engineer to check, which root caused it.
Quoted 4 lines
Quoted 2 lines
So the idea was to find the activites before the happening of "corrution", and check if there was some clues.
Again. You failed to structure the problem and use the tools correctly.
Yes! For this one, we also concluded it's not OS changing it. As you said, using HW jtag debugger to watchpoint that address for BIOS (usually they use identity mapping, and the virtual address is the same as physical address) should be able to capture the culprit BIOS code precisely.
Quoted 18 lines
Quoted 11 lines
Quoted 10 lines
Quoted 5 lines
Then I tried to recall some old memory corruption issues I've met before, and think about if there is some that could be captured by this method, one example was a static global array overflow issue, which corrupted some other global variables which was next to it in kernel bss segment.
No. This is just all catching the problem after the fact with no trace and conclusive information about the root cause. The tools are there, you just have to use them correctly. But sure creating magic hacks which by chance give you the same information is way better...
Quoted 6 lines
This issue was interesting. It showed up as a NULL pointer panic, and I found it's a global variable (in bss segment) being corrupted (which shouldn't happen logically). As it didn't happened on normal platforms, but one platform with special config, we think it could be silicon related, and sent it to silicon team, who did root cause it with gathering/analyzing silicon traces to be an array overflow issue, as the special config make that array much longer.
Quoted 3 lines
Your debug war stories are amazing, but in the wrong way and do not justify to shove a completely ill defined barely usable hack into the kernel to be maintained forever.
Yes, I agree with you and David that this implementation is hacky and difficult to maintain.
As HW jtag debugger is expensive and not generally available for software developer, also many production hardware don't have their jtag interface open for security reason, I think monitoring the abnormal changes to dram/mmio is convenient for debugging memory corruption issues. Could you help to give some suggestion on how to redesign this? Many thanks!
- Feng
Quoted 3 lines
Thanks,
tglx
Quoted 6 lines · Feng Tang
As HW jtag debugger is expensive and not generally available for software developer, also many production hardware don't have their jtag interface open for security reason, I think monitoring the abnormal changes to dram/mmio is convenient for debugging memory corruption issues. Could you help to give some suggestion on how to redesign this? Many thanks!
If you can reproduce in a VM, you have qemu + gdb, which allows you even to debug the BIOS.
If it's bare metal you can use kgdb or perf events. All of those allow you to set watchpoints, data breakpoints etc.
If you end up with no hit on a watch/breakpoint then you're lost in that case as you can't debug the BIOS w/o a hardware debugger, but that's not any different than with your magic hack.
The tools are all there you just have to use them correctly, no?
Thanks,
tglx
Quoted 10 lines · Thomas Gleixner
Quoted 6 lines · Feng Tang
As HW jtag debugger is expensive and not generally available for software developer, also many production hardware don't have their jtag interface open for security reason, I think monitoring the abnormal changes to dram/mmio is convenient for debugging memory corruption issues. Could you help to give some suggestion on how to redesign this? Many thanks!
If you can reproduce in a VM, you have qemu + gdb, which allows you even to debug the BIOS.
Yes. I use this in normal work, for issues which can be reproduced in QEMU.
Quoted 2 lines
If it's bare metal you can use kgdb or perf events. All of those allow you to set watchpoints, data breakpoints etc.
Thanks for the suggestion! I have never used kgdb before, but rely more on printk/trace_printk :). I went checked it and tried kdb in QEMU, and think it's great and will surely use it.
I read some code about kgdb and HW spec for arm-debug. IIUC, some architectures support watching 'write' to memory, but few supports watchpoint by 'physical address'.
V1 patch's title about 'watchpoint' is misleading, while the RFC's is more explicit about detecting memory corruption (to dram/mmio by physical address, from devices, broken silicon, BIOS and kernel), which has no dependency over other kernel modules or hardware. It works very basically as long as 'timer' works.
Also I failed to explain the issues clearly, let me try again. There are 3 issues discussed:
1. 'zero page' corrupted inside OS due to BIOS enables wrong HW feature, triggering 'segmentation fault' randomly.
2. BIOS runtime service corrupts ACPI_ENABLE register
3. kernel static array overflow issue.
For 2 and 3, they can be solved without this patch, which may accelerate the debug though. Only for 3, the patch is necessary as physical address watchpoint is not supported by hardware.
Anyway I'll explore more in this area. Thanks for your time reviewing!
- Feng
Quoted 13 lines
If you end up with no hit on a watch/breakpoint then you're lost in that case as you can't debug the BIOS w/o a hardware debugger, but that's not any different than with your magic hack.
The tools are all there you just have to use them correctly, no?
Thanks,
tglx
Hi Feng,
lib/Kconfig.debug config MEM_ALLOC_PROFILING_DEBUG · line 1086
Quoted 8 lines · Feng Tang
lib/Kconfig.debug +8
-0+config SOFTWARE_WATCHPOINT + bool "Softwre watchpoint to watch memory corruption" + depends on DEBUG_KERNEL + help + Using periodc timer checking specific physical DRAM/MMIO address + to capture memory corruption triggered by kernel, hardware devices, + BIOS runtime code or silicon. +
You've got some typo issues here in the kconfig:
1. "Softwre" in the prompt,
2. extra space in the 'depends on',
3. 'periodc' in the help text.
Though I guess you have bigger issues to address from other reviewers.
- Julian Braha
Quoted 16 lines · Julian Braha
Hi Feng,
lib/Kconfig.debug config MEM_ALLOC_PROFILING_DEBUG · line 1086
Quoted 8 lines · Feng Tang
lib/Kconfig.debug +8
-0+config SOFTWARE_WATCHPOINT + bool "Softwre watchpoint to watch memory corruption" + depends on DEBUG_KERNEL + help + Using periodc timer checking specific physical DRAM/MMIO address + to capture memory corruption triggered by kernel, hardware devices, + BIOS runtime code or silicon. +You've got some typo issues here in the kconfig:
1. "Softwre" in the prompt,
2. extra space in the 'depends on',
3. 'periodc' in the help text.
Thanks for the catch!
Quoted 1 line
Though I guess you have bigger issues to address from other reviewers.
I agree :)
Quoted 1 line
- Julian Braha