timer: tsc_timer: Fix FTRACE support
PATCH 1/2 timer: tsc_timer: Fix FTRACE support
Currently FTRACE aborts on the first function call since getting the timestamp causes a recursion and tracing is immediately disabled.
Fix that by adding 'notrace' to TSC timer functions. TEST=The 'trace' cmd reports that functions have been traced on
qemu-q35 when CONFIG_TRACE is enabled and FTRACE=1.
--- drivers/timer/tsc_timer.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/timer/tsc_timer.c +2 -2
@@ -395,14 +395,14 @@ void __udelay(unsigned long usec)#endif } -static u64 tsc_timer_get_count (struct udevice *dev) +static u64 notrace tsc_timer_get_count(struct udevice *dev) {u64 now_tick =rdtsc ();return now_tick - gd->arch.tsc_base; } -static void tsc_timer_ensure_setup (bool early) +static void notrace tsc_timer_ensure_setup(bool early) {if (gd->arch.tsc_inited)return ;
Signature · 1 line
2.52.0
PATCH 2/2 efi_loader: Fix kernel panic when FTRACE is enabled
When FTRACE is enabled the EFI runtime will call the tracing functions that are not marked as __efi_runtime and cause a kernel panic since the functions have been previously unmapped by the kernel.
Resolve this issue by adding the 'notrace' attribute to '__efi_runtime' attribute and thus disable tracing the EFI runtime. If required this can be worked out latter.
TEST=Can boot a UEFI compatible linux OS on qemu-q35 when U-Boot was
built with FTRACE=1.
--- include/efi_loader.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/efi_loader.h +1 -1
@@ -66,7 +66,7 @@ struct bootflow;* * static __efi_runtime compute_my_table(void); */ -#define __efi_runtime __section(".text.efi_runtime") +#define __efi_runtime __section(".text.efi_runtime") notrace/* * Call this with mmio_ptr as the _pointer_ to a pointer to an MMIO region
Signature · 1 line
2.52.0
Re: [PATCH 2/2] efi_loader: Fix kernel panic when FTRACE is enabled
Thanks Patrick,
Quoted 9 lines · Patrick Rudolph
When FTRACE is enabled the EFI runtime will call the tracing functions that are not marked as
__efi_runtimeand cause a kernel panic since the functions have been previously unmapped by the kernel.Resolve this issue by adding the 'notrace' attribute to '
__efi_runtime' attribute and thus disable tracing the EFI runtime. If required this can be worked out latter.
So I agree with this. Fixing is just a matter of moving the missing functions to the runtime section, but I don;t think it's worth it, unless someone *really* wants to instrument runtime calls. OTOH runtime calls in the OS are rare and very limited
Quoted 24 lines
TEST=Can boot a UEFI compatible linux OS on qemu-q35 when U-Boot was
built with FTRACE=1.Signed-off-by Patrick Rudolph <[email protected]>--- include/efi_loader.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)include/efi_loader.h +1
-2@@ -66,7 +66,7 @@ struct bootflow;* * static __efi_runtime compute_my_table(void); */ -#define __efi_runtime __section(".text.efi_runtime") +#define __efi_runtime __section(".text.efi_runtime") notrace/* * Call this with mmio_ptr as the _pointer_ to a pointer to an MMIO region -- 2.52.0
Re: [PATCH 2/2] efi_loader: Fix kernel panic when FTRACE is enabled
Quoted 12 lines · Ilias Apalodimas
Thanks Patrick,
Quoted 8 lines · Patrick Rudolph
When FTRACE is enabled the EFI runtime will call the tracing functions that are not marked as
__efi_runtimeand cause a kernel panic since the functions have been previously unmapped by the kernel.Resolve this issue by adding the 'notrace' attribute to '
__efi_runtime' attribute and thus disable tracing the EFI runtime. If required this can be worked out latter.
What is the point of actually booting with a U-Boot binary with FTRACE enabled? All your trace data will be lost.
Wouldn't it be preferable to error out at ExitBootServices() and retrieve the trace?
The current implementation of FTRACE looks flawed. The trace buffer should be allocated from LMB so that it cannot overwrite images or EFI data.
Best regards
Heinrich
Quoted 34 lines
Quoted 1 line
So I agree with this. Fixing is just a matter of moving the missing functions to the runtime section, but I don;t think it's worth it, unless someone *really* wants to instrument runtime calls. OTOH runtime calls in the OS are rare and very limited
Reviewed-by Ilias Apalodimas <[email protected]>Quoted 24 lines
TEST=Can boot a UEFI compatible linux OS on qemu-q35 when U-Boot was
built with FTRACE=1.Signed-off-by Patrick Rudolph <[email protected]>--- include/efi_loader.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)include/efi_loader.h +1
-2@@ -66,7 +66,7 @@ struct bootflow;* * static __efi_runtime compute_my_table(void); */ -#define __efi_runtime __section(".text.efi_runtime") +#define __efi_runtime __section(".text.efi_runtime") notrace/* * Call this with mmio_ptr as the _pointer_ to a pointer to an MMIO region -- 2.52.0
Re: [PATCH 2/2] efi_loader: Fix kernel panic when FTRACE is enabled
Quoted 15 lines · Heinrich Schuchardt
Quoted 12 lines · Ilias Apalodimas
Thanks Patrick,
Quoted 8 lines · Patrick Rudolph
When FTRACE is enabled the EFI runtime will call the tracing functions that are not marked as
__efi_runtimeand cause a kernel panic since the functions have been previously unmapped by the kernel.Resolve this issue by adding the 'notrace' attribute to '
__efi_runtime' attribute and thus disable tracing the EFI runtime. If required this can be worked out latter.
Quoted 9 lines
What is the point of actually booting with a U-Boot binary with FTRACE enabled? All your trace data will be lost.
Wouldn't it be preferable to error out at
ExitBootServices()and retrieve the trace?The current implementation of FTRACE looks flawed. The trace buffer should be allocated from LMB so that it cannot overwrite images or EFI data.
Hi Heinrich,
I'm not that familiar with the tracing yet. My expectation was that the trace buffer is marked as reserved
and could be retrieved from /dev/mem, however that's not the case. I updated the CONFIG_BOOTCOMMAND to write the trace file to disk before it runs "bootflow scan"
as described in the documentation. I can then retrieve the trace data from disk. That works for me.
Regards,
Patrick Rudolph
Quoted 39 lines
Best regards
Heinrich
Quoted 34 lines
Quoted 1 line
So I agree with this. Fixing is just a matter of moving the missing functions to the runtime section, but I don;t think it's worth it, unless someone *really* wants to instrument runtime calls. OTOH runtime calls in the OS are rare and very limited
Reviewed-by Ilias Apalodimas <[email protected]>Quoted 24 lines
TEST=Can boot a UEFI compatible linux OS on qemu-q35 when U-Boot was
built with FTRACE=1.Signed-off-by Patrick Rudolph <[email protected]>--- include/efi_loader.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)include/efi_loader.h +1
-2@@ -66,7 +66,7 @@ struct bootflow;* * static __efi_runtime compute_my_table(void); */ -#define __efi_runtime __section(".text.efi_runtime") +#define __efi_runtime __section(".text.efi_runtime") notrace/* * Call this with mmio_ptr as the _pointer_ to a pointer to an MMIO region -- 2.52.0
Re: [PATCH 2/2] efi_loader: Fix kernel panic when FTRACE is enabled
Quoted 34 lines · Patrick Rudolph
Quoted 15 lines · Heinrich Schuchardt
Quoted 12 lines · Ilias Apalodimas
Thanks Patrick,
Quoted 8 lines · Patrick Rudolph
When FTRACE is enabled the EFI runtime will call the tracing functions that are not marked as
__efi_runtimeand cause a kernel panic since the functions have been previously unmapped by the kernel.Resolve this issue by adding the 'notrace' attribute to '
__efi_runtime' attribute and thus disable tracing the EFI runtime. If required this can be worked out latter.Quoted 9 lines
What is the point of actually booting with a U-Boot binary with FTRACE enabled? All your trace data will be lost.
Wouldn't it be preferable to error out at
ExitBootServices()and retrieve the trace?The current implementation of FTRACE looks flawed. The trace buffer should be allocated from LMB so that it cannot overwrite images or EFI data.
Hi Heinrich,
I'm not that familiar with the tracing yet. My expectation was that the trace buffer is marked as reserved
and could be retrieved from/dev/mem, however that's not the case. I updated theCONFIG_BOOTCOMMANDto write the trace file to disk before it runs "bootflow scan"
as described in the documentation. I can then retrieve the trace data from disk. That works for me.
Quoted 2 lines
Regards,
Patrick Rudolph
Trace logic
===========
The logic introduced by commit 852d4dbd70ba ("trace: Detect an infinite loop") just switches of tracing when __cyg_profile_func_enter() is invoked with hdr->trace_locked=true. This lets us end up with having to mark dozens of functions as notrace because they are invoked by add_ftrace().
A better approach than switching of tracing is to simply return without recording the entry by adding
if (!hdr || hdr->trace_locked)
return;
at the start of __cyg_profile_func_enter() and __cyg_profile_func_exit().
Once the outermost __cyg_profile_func_enter() is finished it will unlock the trace.
Missing relocation
==================
Unfortunately there are more bugs that need to be solved to make tracing work properly.
On qemu-riscv64_smode_defconfig I found that there is also an issue in notify_dynamic() which is invoked when searching for the timer driver.
When looping over state->spy_head, list_for_each_entry_safe() does not discover the end of the list and loops forever though the list is empty.
The reason is that the address of state->spy_head has been relocated but state->spy_head.next and state>spy_head.prev still point to the unrelocated address. Therefore list_for_entry_safe() does not discover the end of the list.
We should not start tracing before initr_reloc().
When placing INITCALL(initr_trace) directly after INITCALL(initr_reloc) the timer driver panics with
Could not initialize timer (err -11).
In initcall_run_r() we need to place INITCALL(initr_trace) after INITCALL(initr_dm) to let the timer driver work properly.
Defaults
========
The current defaults for tracing are not helpful. The observed call depth for bootefi hello is 42. The default of 16 MiB as trace buffer is way too low to catch a boot process. Rather think of 1-2 GiB.
Best regards
Heinrich>
Quoted 34 lines
Quoted 34 lines
Quoted 33 lines
So I agree with this. Fixing is just a matter of moving the missing functions to the runtime section, but I don;t think it's worth it, unless someone *really* wants to instrument runtime calls. OTOH runtime calls in the OS are rare and very limited
Reviewed-by Ilias Apalodimas <[email protected]>Quoted 24 lines
TEST=Can boot a UEFI compatible linux OS on qemu-q35 when U-Boot was
built with FTRACE=1.Signed-off-by Patrick Rudolph <[email protected]>--- include/efi_loader.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)include/efi_loader.h +1
-2@@ -66,7 +66,7 @@ struct bootflow;* * static __efi_runtime compute_my_table(void); */ -#define __efi_runtime __section(".text.efi_runtime") +#define __efi_runtime __section(".text.efi_runtime") notrace/* * Call this with mmio_ptr as the _pointer_ to a pointer to an MMIO region -- 2.52.0
Quoted 23 lines · Patrick Rudolph
Currently FTRACE aborts on the first function call since getting the timestamp causes a recursion and tracing is immediately disabled.
Fix that by adding 'notrace' to TSC timer functions. TEST=The 'trace' cmd reports that functions have been traced on
qemu-q35 whenCONFIG_TRACEis enabled and FTRACE=1.Signed-off-by Patrick Rudolph <[email protected]>--- drivers/timer/tsc_timer.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/timer/tsc_timer.c +1
-1@@ -395,14 +395,14 @@ void __udelay(unsigned long usec)#endif } -static u64 tsc_timer_get_count (struct udevice *dev) +static u64 notrace tsc_timer_get_count(struct udevice *dev) {
drivers/timer/tsc_timer.c void __udelay(unsigned long usec) · line 408
Quoted 10 lines
drivers/timer/tsc_timer.c +1
-1u64 now_tick =rdtsc ();return now_tick - gd->arch.tsc_base; } -static void tsc_timer_ensure_setup (bool early) +static void notrace tsc_timer_ensure_setup(bool early) {if (gd->arch.tsc_inited)return ;
Thank you for reporting your issues with the FTRACE functionality and providing a fix for the x86 platform.
In __cyg_profile_func_enter() and __cyg_profile_func_exit() we call add_ftrace().
In function add_ftrace() we call timer_get_us().
In drivers/timer/tsc_timer.c this function is marked notrace but not in arch/arm/cpu/armv7/s5p-common/timer.c and not in arch/xtensa/lib/time.c.
The drivers/timer/tsc_timer.c implementation calls get_ticks() and get_tbclk_mhz().
get_ticks() is marked notrace in lib/time.c but nowhere else.
get_ticks() in lib/time.c calls timer_get_count() implemented in drivers/timer/timer-uclass.c which is marked notrace.
If CONFIG_TIMER_EARLY=y get_ticks() will call timer_early_get_count() when the timer driver is not set-up yet.
Very few driver implementations of timer_get_count() are marked as notrace (e.g. sandbox, risc-v).
Notrace function xtimer_early_get_rate() calls tsc_timer_ensure_setup().
My conclusion is that function tracing will fail on many more platforms than only x86.