x86: dovetail: Permit to declare a trap handled
Subsystems (4)
RFC PATCH 1/1 x86: dovetail: Permit to declare a trap handled
From: Jan Kiszka <[email protected]>
This allows to skip reporting a trap to Linux when the OOB handler resolved it directly. The handler can signal this condition by returning "true". This feature was present in I-pipe as well.
By intention, not all spots of oob_trap_notify are enabled for this. Debug traps are left out as well as asyncpf in kvm - no use cases from OOB-perspective.
NOTE: breaks unprepared users, breaks non-x86.
---
I'm happy to add non-x86 support as well if the direction is fine.
And if we introduce that to dovetail, we will have to add some kernel version checks to Xenomai so that it does the right thing with the right dovetail versions. Feature flags are out of fashion there as far as I understood.
arch/x86/kernel/traps.c | 38 ++++++++++++++++++++++---------------- arch/x86/mm/fault.c | 9 ++++++--- include/linux/dovetail.h | 9 +++++---- kernel/dovetail.c | 9 ++++++--- 4 files changed, 39 insertions(+), 26 deletions(-)
arch/x86/kernel/traps.c +22 -16
@@ -163,10 +163,12 @@ static void show_signal(struct task_struct *tsk, int signr, }static __always_inline -void mark_trap_entry(int trapnr, struct pt_regs *regs) +bool mark_trap_entry(int trapnr, struct pt_regs *regs) { -oob_trap_notify (trapnr, regs); +if (oob_trap_notify (trapnr, regs)) +return false;hard_cond_local_irq_enable (); +return true; } static __always_inline
@@ -211,9 +213,8 @@ static void do_error_trap(struct pt_regs *regs, long error_code, char *str, {RCU_LOCKDEP_WARN (!rcu_is_watching (),"entry code didn't wake RCU" ); -mark_trap_entry (trapnr, regs); - -if (notify_die (DIE_TRAP , str, regs, error_code, trapnr, signr) != +if (mark_trap_entry (trapnr, regs) && +notify_die (DIE_TRAP , str, regs, error_code, trapnr, signr) !=NOTIFY_STOP ) {cond_local_irq_enable (regs);do_trap (trapnr, signr, str, regs, error_code, sicode, addr);
@@ -375,10 +376,12 @@ DEFINE_IDTENTRY_RAW(exc_invalid_op)return ; state =irqentry_enter (regs); -mark_trap_entry (X86_TRAP_UD , regs); +if (!mark_trap_entry (X86_TRAP_UD , regs)) + goto mark_exit;instrumentation_begin ();handle_invalid_op (regs);instrumentation_end (); +mark_exit:mark_trap_exit (X86_TRAP_UD , regs);irqentry_exit (regs, state); }
@@ -411,7 +414,8 @@ DEFINE_IDTENTRY_ERRORCODE(exc_alignment_check) {char *str ="alignment check" ; -mark_trap_entry (X86_TRAP_AC , regs); +if (!mark_trap_entry (X86_TRAP_AC , regs)) + goto mark_exit;if (notify_die (DIE_TRAP , str, regs, error_code,X86_TRAP_AC ,SIGBUS ) ==NOTIFY_STOP ) goto mark_exit;
@@ -595,7 +599,8 @@ DEFINE_IDTENTRY_DF(exc_double_fault)DEFINE_IDTENTRY (exc_bounds) { -mark_trap_entry (X86_TRAP_BR , regs); +if (!mark_trap_entry (X86_TRAP_BR , regs)) + goto out;if (notify_die (DIE_TRAP ,"bounds" , regs,0 ,X86_TRAP_BR ,SIGSEGV ) ==NOTIFY_STOP )
@@ -806,7 +811,8 @@ DEFINE_IDTENTRY_ERRORCODE(exc_general_protection)if (fixup_vdso_exception (regs,X86_TRAP_GP , error_code,0 )) goto exit; -mark_trap_entry (X86_TRAP_GP , regs); +if (!mark_trap_entry (X86_TRAP_GP , regs)) + goto mark_exit;gp_user_force_sig_segv (regs,X86_TRAP_GP , error_code, desc); goto mark_exit; }
@@ -1273,7 +1279,8 @@ static void math_error(struct pt_regs *regs, int trapnr)if (fixup_exception (regs, trapnr,0 ,0 )) goto exit; -mark_trap_entry (trapnr, regs); +if (!mark_trap_entry (trapnr, regs)) + goto mark_exit; task->thread.error_code =0 ; task->thread.trap_nr = trapnr;
@@ -1300,10 +1307,9 @@ static void math_error(struct pt_regs *regs, int trapnr)if (fixup_vdso_exception (regs, trapnr,0 ,0 )) goto exit; -mark_trap_entry (trapnr, regs); - -force_sig_fault (SIGFPE , si_code, - (void __user *)uprobe_get_trap_addr (regs)); +if (mark_trap_entry (trapnr, regs)) +force_sig_fault (SIGFPE , si_code, + (void __user *)uprobe_get_trap_addr (regs)); mark_exit:mark_trap_exit (trapnr, regs); exit:
@@ -1416,8 +1422,8 @@ DEFINE_IDTENTRY(exc_device_not_available)* to kill the task than getting stuck in a never-ending * loop of #NM faults. */ -mark_trap_entry (X86_TRAP_NM , regs); -die ("unexpected #NM exception" , regs,0 ); +if (mark_trap_entry (X86_TRAP_NM , regs)) +die ("unexpected #NM exception" , regs,0 );mark_trap_exit (X86_TRAP_NM , regs); } }
arch/x86/mm/fault.c +6 -3
@@ -650,7 +650,8 @@ page_fault_oops(struct pt_regs *regs, unsigned long error_code,* Do not bother unwinding the notification context on * CPU/firmware/kernel bug. */ -oob_trap_notify (X86_TRAP_PF , regs); +if (oob_trap_notify (X86_TRAP_PF , regs)) +return ;#ifdef CONFIG_VMAP_STACK /*
@@ -890,7 +891,8 @@ __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,if (fixup_vdso_exception (regs,X86_TRAP_PF , error_code, address))return ; -oob_trap_notify (X86_TRAP_PF , regs); +if (oob_trap_notify (X86_TRAP_PF , regs)) +return ;if (likely (show_unhandled_signals))show_signal_msg (regs, error_code, address, tsk);
@@ -1363,7 +1365,8 @@ void do_user_addr_fault(struct pt_regs *regs,* event, so that it might switch current to in-band mode if * need be. */ -oob_trap_notify (X86_TRAP_PF , regs); +if (oob_trap_notify (X86_TRAP_PF , regs)) +return ;perf_sw_event (PERF_COUNT_SW_PAGE_FAULTS ,1 , regs, address);
include/linux/dovetail.h +5 -4
@@ -58,14 +58,15 @@ void inband_task_init(struct task_struct *p);int pipeline_syscall (unsigned int nr,struct pt_regs *regs); -void __oob_trap_notify (unsigned int exception, +bool __oob_trap_notify (unsigned int exception,struct pt_regs *regs); -static __always_inline void oob_trap_notify(unsigned int exception, +static __always_inline bool oob_trap_notify(unsigned int exception,struct pt_regs *regs) {if (running_oob () && !test_thread_local_flags (_TLF_OOBTRAP)) -__oob_trap_notify (exception, regs); +return __oob_trap_notify (exception, regs); +return false; } void __oob_trap_unwind(unsigned int exception,
@@ -248,7 +249,7 @@ static inline void arch_dovetail_exec_prepare(void)* Keep the trap helpers as macros, we might not be able to resolve * trap numbers if CONFIG_DOVETAIL is off. */ -#define oob_trap_notify (__exception, __regs) do { } while (0) +#define oob_trap_notify (__exception, __regs) false#define oob_trap_unwind (__exception, __regs) do { } while (0) static inline
kernel/dovetail.c +6 -3
@@ -254,14 +254,16 @@ int pipeline_syscall(unsigned int nr, struct pt_regs *regs)return 0 ;/* pass syscall down to the in-band dispatcher. */ } -void __weak handle_oob_trap_entry (unsigned int trapnr,struct pt_regs *regs) +bool __weak handle_oob_trap_entry (unsigned int trapnr,struct pt_regs *regs) { +return false; } -noinstr void __oob_trap_notify(unsigned int exception, +noinstr bool __oob_trap_notify(unsigned int exception,struct pt_regs *regs) {unsigned long flags; +bool handled;/* * We send a notification about exceptions raised over a
@@ -273,10 +275,11 @@ noinstr void __oob_trap_notify(unsigned int exception,set_thread_local_flags (_TLF_OOBTRAP); flags =hard_local_irq_save ();instrumentation_begin (); -handle_oob_trap_entry (exception, regs); + handled =handle_oob_trap_entry (exception, regs);instrumentation_end ();hard_local_irq_restore (flags); } +return handled; }void __weak handle_oob_trap_exit (unsigned int trapnr,struct pt_regs *regs)
Signature · 1 line
2.35.3
Quoted 13 lines · Jan Kiszka
From: Jan Kiszka <[email protected]>
This allows to skip reporting a trap to Linux when the OOB handler resolved it directly. The handler can signal this condition by returning "true". This feature was present in I-pipe as well.
By intention, not all spots of
oob_trap_notifyare enabled for this. Debug traps are left out as well as asyncpf in kvm - no use cases from OOB-perspective.NOTE: breaks unprepared users, breaks non-x86.
Signed-off-by Jan Kiszka <[email protected]>
Quoted 9 lines
---
I'm happy to add non-x86 support as well if the direction is fine.
And if we introduce that to dovetail, we will have to add some kernel version checks to Xenomai so that it does the right thing with the right dovetail versions. Feature flags are out of fashion there as far as I understood.
And I forgot to write that this if for v5.19-dovetail-rebase. I have a 5.10 version locally as well, the one we use in a downstream deployment for some months now.
Jan
Signature · 2 lines
Siemens AG, Technology Competence Center Embedded Linux
Hello all,
I just recently found the time to test this patch in the context of our rt_signals. As a matter of fact it works quite well for us.
So is there any intent/possibility to bring this upstream?
I also did some similar changes for arm64. As we also use imx8 boards. If there is interest in those, I could post them.
Best regards
Johannes
Quoted 41 lines
-----Original Message-----
From: Jan Kiszka <[email protected]>
Sent: Freitag, 8. Juli 2022 09:13
To: Xenomai <[email protected]>; Philippe Gerum <[email protected]>; Johannes Kirchmair <[email protected]>; Richard Weinberger <[email protected]>
Subject: Re: [RFC][PATCH] x86: dovetail: Permit to declare a trap handledQuoted 13 lines · Jan Kiszka
From: Jan Kiszka <[email protected]>
This allows to skip reporting a trap to Linux when the OOB handler resolved it directly. The handler can signal this condition by returning "true". This feature was present in I-pipe as well.
By intention, not all spots of
oob_trap_notifyare enabled for this. Debug traps are left out as well as asyncpf in kvm - no use cases from OOB-perspective.NOTE: breaks unprepared users, breaks non-x86.
Signed-off-by Jan Kiszka <[email protected]>Quoted 9 lines
---
I'm happy to add non-x86 support as well if the direction is fine.
And if we introduce that to dovetail, we will have to add some kernel version checks to Xenomai so that it does the right thing with the right dovetail versions. Feature flags are out of fashion there as far as I understood.
And I forgot to write that this if for v5.19-dovetail-rebase. I have a 5.10 version locally as well, the one we use in a downstream deployment for some months now.
Jan
--
Siemens AG, Technology
Competence Center Embedded Linux
Quoted 9 lines · Johannes Kirchmair
Hello all,
I just recently found the time to test this patch in the context of our
rt_signals. As a matter of fact it works quite well for us.So is there any intent/possibility to bring this upstream?
I also did some similar changes for arm64. As we also use imx8 boards. If there is interest in those, I could post them.
Yes, please share. Then we can continue with proposing it for the latest dovetail port (6.1 now).
Jan
Quoted 45 lines
Best regards
JohannesQuoted 41 lines
-----Original Message-----
From: Jan Kiszka <[email protected]>
Sent: Freitag, 8. Juli 2022 09:13
To: Xenomai <[email protected]>; Philippe Gerum <[email protected]>; Johannes Kirchmair <[email protected]>; Richard Weinberger <[email protected]>
Subject: Re: [RFC][PATCH] x86: dovetail: Permit to declare a trap handledQuoted 13 lines · Jan Kiszka
From: Jan Kiszka <[email protected]>
This allows to skip reporting a trap to Linux when the OOB handler resolved it directly. The handler can signal this condition by returning "true". This feature was present in I-pipe as well.
By intention, not all spots of
oob_trap_notifyare enabled for this. Debug traps are left out as well as asyncpf in kvm - no use cases from OOB-perspective.NOTE: breaks unprepared users, breaks non-x86.
Signed-off-by Jan Kiszka <[email protected]>Quoted 9 lines
---
I'm happy to add non-x86 support as well if the direction is fine.
And if we introduce that to dovetail, we will have to add some kernel version checks to Xenomai so that it does the right thing with the right dovetail versions. Feature flags are out of fashion there as far as I understood.
And I forgot to write that this if for v5.19-dovetail-rebase. I have a 5.10 version locally as well, the one we use in a downstream deployment for some months now.
Jan
--
Siemens AG, Technology
Competence Center Embedded Linux
Signature · 2 lines
Siemens AG, Technology Competence Center Embedded Linux
Quoted 6 lines · Jan Kiszka
Quoted 5 lines · Johannes Kirchmair
Hello all,
I just recently found the time to test this patch in the context of our
rt_signals. As a matter of fact it works quite well for us.
Quoted 8 lines
Quoted 4 lines
So is there any intent/possibility to bring this upstream?
I also did some similar changes for arm64. As we also use imx8 boards. If there is interest in those, I could post them.
Yes, please share. Then we can continue with proposing it for the latest dovetail port (6.1 now).
There is a slightly different approach which expresses the intent of that notification hook Dovetail-wise:
- if the real-time core switches in-band upon notification, then the in-band kernel code may assume that all regular fixups need to be applied next. This is the common case for most traps.
- if the real-time core stays on the out-of-band stage upon notification, then the in-band kernel code should assume that all required fixups were done from that stage already, returning asap from the outer trap handler.
This gives the same flexibility than by expecting the hook to return a continuation status, but makes sure that regular fixups cannot be bypassed when/if switching to in-band mode is required to handle the trap. Rationale: the regular in-band trap handlers know better about the way to deal with those events from in-band context.
e.g.
arch/x86/kernel/traps.c +42 -21
@@ -161,10 +161,16 @@ static void show_signal(struct task_struct *tsk, int signr, }static __always_inline -void mark_trap_entry(int trapnr, struct pt_regs *regs) +bool mark_trap_entry(int trapnr, struct pt_regs *regs) {oob_trap_notify (trapnr, regs); -hard_cond_local_irq_enable (); + +if (likely (running_inband ())) { +hard_cond_local_irq_enable (); +return true; + } + +return false; } static __always_inline
@@ -175,9 +181,10 @@ void mark_trap_exit(int trapnr, struct pt_regs *regs) }static __always_inline -void mark_trap_entry_raw(int trapnr, struct pt_regs *regs) +bool mark_trap_entry_raw(int trapnr, struct pt_regs *regs) {oob_trap_notify (trapnr, regs); +return running_inband (); } static __always_inline
@@ -209,7 +216,8 @@ static void do_error_trap(struct pt_regs *regs, long error_code, char *str, {RCU_LOCKDEP_WARN (!rcu_is_watching (),"entry code didn't wake RCU" ); -mark_trap_entry (trapnr, regs); +if (!mark_trap_entry (trapnr, regs)) +return ;if (notify_die (DIE_TRAP , str, regs, error_code, trapnr, signr) !=NOTIFY_STOP ) {
@@ -289,7 +297,8 @@ DEFINE_IDTENTRY_RAW(exc_invalid_op) {irqentry_state_t state; -mark_trap_entry_raw (X86_TRAP_UD , regs); +if (!mark_trap_entry_raw (X86_TRAP_UD , regs)) +return ;/* * We use UD2 as a short encoding for 'CALL __WARN', as such
@@ -336,7 +345,8 @@ DEFINE_IDTENTRY_ERRORCODE(exc_alignment_check) {char *str ="alignment check" ; -mark_trap_entry (X86_TRAP_AC , regs); +if (!mark_trap_entry (X86_TRAP_AC , regs)) +return ;if (notify_die (DIE_TRAP , str, regs, error_code,X86_TRAP_AC ,SIGBUS ) ==NOTIFY_STOP ) goto mark_exit;
@@ -520,7 +530,8 @@ DEFINE_IDTENTRY_DF(exc_double_fault)DEFINE_IDTENTRY (exc_bounds) { -mark_trap_entry (X86_TRAP_BR , regs); +if (!mark_trap_entry (X86_TRAP_BR , regs)) +return ;if (notify_die (DIE_TRAP ,"bounds" , regs,0 ,X86_TRAP_BR ,SIGSEGV ) ==NOTIFY_STOP )
@@ -641,13 +652,15 @@ DEFINE_IDTENTRY_ERRORCODE(exc_general_protection)if (fixup_iopl_exception (regs)) goto exit; -mark_trap_entry (X86_TRAP_GP , regs); tsk->thread.error_code = error_code; tsk->thread.trap_nr =X86_TRAP_GP ;if (fixup_vdso_exception (regs,X86_TRAP_GP , error_code,0 )) goto exit; +if (!mark_trap_entry (X86_TRAP_GP , regs)) + goto exit; +show_signal (tsk,SIGSEGV ,"" , desc, regs, error_code);force_sig (SIGSEGV ); goto mark_exit;
@@ -668,7 +681,8 @@ DEFINE_IDTENTRY_ERRORCODE(exc_general_protection) kprobe_fault_handler(regs, X86_TRAP_GP)) goto exit; -mark_trap_entry (X86_TRAP_GP , regs); +if (!mark_trap_entry (X86_TRAP_GP , regs)) + goto exit; ret =notify_die (DIE_GPF , desc, regs, error_code,X86_TRAP_GP ,SIGSEGV );if (ret ==NOTIFY_STOP )
@@ -740,7 +754,8 @@ DEFINE_IDTENTRY_RAW(exc_int3)if (poke_int3_handler (regs))return ; -mark_trap_entry_raw (X86_TRAP_BP , regs); +if (!mark_trap_entry_raw (X86_TRAP_BP , regs)) +return ;/* * irqentry_enter_from_user_mode() uses static_branch_{,un}likely()
@@ -1089,17 +1104,19 @@ static __always_inline void exc_debug_user(struct pt_regs *regs,/* IST stack entry */ DEFINE_IDTENTRY_DEBUG (exc_debug) { -mark_trap_entry_raw (X86_TRAP_DB , regs); -exc_debug_kernel (regs,debug_read_clear_dr6 ()); -mark_trap_exit_raw (X86_TRAP_DB , regs); +if (mark_trap_entry_raw (X86_TRAP_DB , regs)) { +exc_debug_kernel (regs,debug_read_clear_dr6 ()); +mark_trap_exit_raw (X86_TRAP_DB , regs); + } }/* User entry, runs on regular task stack */ DEFINE_IDTENTRY_DEBUG_USER (exc_debug) { -mark_trap_entry_raw (X86_TRAP_DB , regs); -exc_debug_user (regs,debug_read_clear_dr6 ()); -mark_trap_exit_raw (X86_TRAP_DB , regs); +if (mark_trap_entry_raw (X86_TRAP_DB , regs)) { +exc_debug_user (regs,debug_read_clear_dr6 ()); +mark_trap_exit_raw (X86_TRAP_DB , regs); + } }#else /* 32 bit does not have separate entry points. */
@@ -1133,7 +1150,9 @@ static void math_error(struct pt_regs *regs, int trapnr)if (fixup_exception (regs, trapnr,0 ,0 )) goto exit; -mark_trap_entry (trapnr, regs); +if (!mark_trap_entry (trapnr, regs)) + goto exit; + task->thread.error_code =0 ; task->thread.trap_nr = trapnr;
@@ -1160,7 +1179,8 @@ static void math_error(struct pt_regs *regs, int trapnr)if (fixup_vdso_exception (regs, trapnr,0 ,0 )) goto exit; -mark_trap_entry (trapnr, regs); +if (!mark_trap_entry (trapnr, regs)) + goto exit;force_sig_fault (SIGFPE , si_code, (void __user *)uprobe_get_trap_addr (regs));
@@ -1238,9 +1258,10 @@ DEFINE_IDTENTRY(exc_device_not_available)* to kill the task than getting stuck in a never-ending * loop of #NM faults. */ -mark_trap_entry (X86_TRAP_NM , regs); -die ("unexpected #NM exception" , regs,0 ); -mark_trap_exit (X86_TRAP_NM , regs); +if (mark_trap_entry (X86_TRAP_NM , regs)) { +die ("unexpected #NM exception" , regs,0 ); +mark_trap_exit (X86_TRAP_NM , regs); + } } }
arch/x86/mm/fault.c +8 -1
@@ -891,6 +891,10 @@ __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,return ;oob_trap_notify (X86_TRAP_PF , regs); +if (!running_inband ()) { +local_irq_disable_full (); +return ; + }if (likely (show_unhandled_signals))show_signal_msg (regs, error_code, address, tsk);
@@ -1361,9 +1365,12 @@ void do_user_addr_fault(struct pt_regs *regs,* At this point, we would have to stop running * out-of-band. Tell the companion core about the page fault * event, so that it might switch current to in-band mode if -* need be. +* need be. If it does not, then we may assume that it would +* also handle the fixups. */ oob_trap_notify (X86_TRAP_PF , regs); +if (!running_inband ()) +return ;perf_sw_event (PERF_COUNT_SW_PAGE_FAULTS ,1 , regs, address);
Signature · 1 line
Philippe.
Quoted 30 lines · Philippe Gerum
Quoted 6 lines · Jan Kiszka
Quoted 5 lines · Johannes Kirchmair
Hello all,
I just recently found the time to test this patch in the context of our
rt_signals. As a matter of fact it works quite well for us.Quoted 8 lines
Quoted 4 lines
So is there any intent/possibility to bring this upstream?
I also did some similar changes for arm64. As we also use imx8 boards. If there is interest in those, I could post them.
Yes, please share. Then we can continue with proposing it for the latest dovetail port (6.1 now).
There is a slightly different approach which expresses the intent of that notification hook Dovetail-wise:
- if the real-time core switches in-band upon notification, then the in-band kernel code may assume that all regular fixups need to be applied next. This is the common case for most traps.
- if the real-time core stays on the out-of-band stage upon notification, then the in-band kernel code should assume that all required fixups were done from that stage already, returning asap from the outer trap handler.
Quoted 6 lines
This gives the same flexibility than by expecting the hook to return a continuation status, but makes sure that regular fixups cannot be bypassed when/if switching to in-band mode is required to handle the trap. Rationale: the regular in-band trap handlers know better about the way to deal with those events from in-band context.
Sounds interesting, indeed. Would also obsolete the need to adjust users to a new calling convention.
Jan
Signature · 2 lines
Siemens AG, Technology Competence Center Embedded Linux
Hello,
Quoted 12 lines
-----Original Message-----
From: Jan Kiszka <[email protected]>
Sent: Mittwoch, 9. November 2022 11:39
To: Philippe Gerum <[email protected]>
Cc: Johannes Kirchmair <[email protected]>; Xenomai <[email protected]>; Richard Weinberger <[email protected]>
Subject: Re: [RFC][PATCH] x86: dovetail: Permit to declare a trap handledCAUTION: External E-Mail !
Quoted 1 line · Philippe Gerum
Quoted 36 lines
Quoted 6 lines
Quoted 4 lines · Jan Kiszka
Quoted 3 lines · Johannes Kirchmair
Hello all,
I just recently found the time to test this patch in the context of our
rt_signals.Quoted 29 lines
Quoted 10 lines
Quoted 6 lines
As a matter of fact it works quite well for us.
So is there any intent/possibility to bring this upstream?
I also did some similar changes for arm64. As we also use imx8 boards. If there is interest in those, I could post them.
Yes, please share. Then we can continue with proposing it for the latest dovetail port (6.1 now).
There is a slightly different approach which expresses the intent of that notification hook Dovetail-wise:
- if the real-time core switches in-band upon notification, then the in-band kernel code may assume that all regular fixups need to be applied next. This is the common case for most traps.
- if the real-time core stays on the out-of-band stage upon notification, then the in-band kernel code should assume that all required fixups were done from that stage already, returning asap from the outer trap handler.
This gives the same flexibility than by expecting the hook to return a continuation status, but makes sure that regular fixups cannot be bypassed when/if switching to in-band mode is required to handle the trap. Rationale: the regular in-band trap handlers know better about the way to deal with those events from in-band context.
I like that idea because as I was looking at the arm code, I was not sure what would happen if we are out of band but indicate that the kernel should handle the exception. With your idea that’s not possible.
Will have a look at my arm64 patch and try to adapt it.
Best regards
Johannes
Quoted 9 lines
Sounds interesting, indeed. Would also obsolete the need to adjust users to a new calling convention.
Jan
--
Siemens AG, Technology
Competence Center Embedded Linux
Quoted 51 lines · Johannes Kirchmair
Hello,
Quoted 12 lines
-----Original Message-----
From: Jan Kiszka <[email protected]>
Sent: Mittwoch, 9. November 2022 11:39
To: Philippe Gerum <[email protected]>
Cc: Johannes Kirchmair <[email protected]>; Xenomai <[email protected]>; Richard Weinberger <[email protected]>
Subject: Re: [RFC][PATCH] x86: dovetail: Permit to declare a trap handledCAUTION: External E-Mail !
Quoted 1 line · Philippe Gerum
Quoted 36 lines
Quoted 6 lines
Quoted 4 lines · Jan Kiszka
Quoted 3 lines · Johannes Kirchmair
Hello all,
I just recently found the time to test this patch in the context of our
rt_signals.Quoted 29 lines
Quoted 10 lines
Quoted 6 lines
As a matter of fact it works quite well for us.
So is there any intent/possibility to bring this upstream?
I also did some similar changes for arm64. As we also use imx8 boards. If there is interest in those, I could post them.
Yes, please share. Then we can continue with proposing it for the latest dovetail port (6.1 now).
There is a slightly different approach which expresses the intent of that notification hook Dovetail-wise:
- if the real-time core switches in-band upon notification, then the in-band kernel code may assume that all regular fixups need to be applied next. This is the common case for most traps.
- if the real-time core stays on the out-of-band stage upon notification, then the in-band kernel code should assume that all required fixups were done from that stage already, returning asap from the outer trap handler.
This gives the same flexibility than by expecting the hook to return a continuation status, but makes sure that regular fixups cannot be bypassed when/if switching to in-band mode is required to handle the trap. Rationale: the regular in-band trap handlers know better about the way to deal with those events from in-band context.
Quoted 5 lines
I like that idea because as I was looking at the arm code, I was not sure what would happen if we are out of band but indicate that the kernel should handle the exception. With your idea that’s not possible.
Will have a look at my arm64 patch and try to adapt it.
Ok, so since the idea is generally agreed, I'll push the x86 and arm support for this in a couple of days.
Signature · 1 line
Philippe.
Hi!
Sorry for exhuming this old mail thread.
Am Donnerstag, 10. November 2022, 10:00:33 CET schrieb Philippe Gerum:
Quoted 26 lines
Quoted 26 lines
Quoted 25 lines
Quoted 25 lines
Quoted 6 lines
Quoted 2 lines
I also did some similar changes for arm64. As we also use imx8 boards. If there is interest in those, I could post them.
Yes, please share. Then we can continue with proposing it for the latest dovetail port (6.1 now).
There is a slightly different approach which expresses the intent of that notification hook Dovetail-wise:
- if the real-time core switches in-band upon notification, then the in-band kernel code may assume that all regular fixups need to be applied next. This is the common case for most traps.
- if the real-time core stays on the out-of-band stage upon notification, then the in-band kernel code should assume that all required fixups were done from that stage already, returning asap from the outer trap handler.
This gives the same flexibility than by expecting the hook to return a continuation status, but makes sure that regular fixups cannot be bypassed when/if switching to in-band mode is required to handle the trap. Rationale: the regular in-band trap handlers know better about the way to deal with those events from in-band context.
Quoted 8 lines
Quoted 5 lines
I like that idea because as I was looking at the arm code, I was not sure what would happen if we are out of band but indicate that the kernel should handle the exception. With your idea that’s not possible.
Will have a look at my arm64 patch and try to adapt it.
Ok, so since the idea is generally agreed, I'll push the x86 and arm support for this in a couple of days.
Currently, I'm looking into implementing Johannes's RT-Signals for ARM64,
but from what I see on the dovetail side, only x86 has gained support to declare traps handled.
At least on x86, I see that a trap is considered handled if no switch to in-band has occurred. Did I miss something?
Thanks,
//richard
Signature · 2 lines
sigma star gmbh | Eduard-Bodem-Gasse 6, 6020 Innsbruck, AUT UID/VAT Nr: ATU 66964118 | FN: 374287y
Quoted 37 lines · Richard Weinberger
Hi!
Sorry for exhuming this old mail thread.
Am Donnerstag, 10. November 2022, 10:00:33 CET schrieb Philippe Gerum:
Quoted 32 lines
Quoted 31 lines
Quoted 19 lines
Quoted 19 lines
Quoted 6 lines
Quoted 2 lines
I also did some similar changes for arm64. As we also use imx8 boards. If there is interest in those, I could post them.
Yes, please share. Then we can continue with proposing it for the latest dovetail port (6.1 now).
There is a slightly different approach which expresses the intent of that notification hook Dovetail-wise:
- if the real-time core switches in-band upon notification, then the in-band kernel code may assume that all regular fixups need to be applied next. This is the common case for most traps.
- if the real-time core stays on the out-of-band stage upon notification, then the in-band kernel code should assume that all required fixups were done from that stage already, returning asap from the outer trap handler.
Quoted 6 lines
Quoted 6 lines
This gives the same flexibility than by expecting the hook to return a continuation status, but makes sure that regular fixups cannot be bypassed when/if switching to in-band mode is required to handle the trap. Rationale: the regular in-band trap handlers know better about the way to deal with those events from in-band context.
I like that idea because as I was looking at the arm code, I was not sure what would happen if we are out of band but indicate that the kernel should handle the exception. With your idea that’s not possible.
Will have a look at my arm64 patch and try to adapt it.
Quoted 8 lines
Quoted 2 lines
Ok, so since the idea is generally agreed, I'll push the x86 and arm support for this in a couple of days.
Currently, I'm looking into implementing Johannes's RT-Signals for ARM64,
but from what I see on the dovetail side, only x86 has gained support to declare traps handled.At least on x86, I see that a trap is considered handled if no switch to in-band has occurred. Did I miss something?
Yes, I do have some first hacks here as well. ARM and ARM64 is not yet ready for this, unfortunately.
Jan
Signature · 2 lines
Siemens AG, Technology Linux Expert Center
Quoted 11 lines · Jan Kiszka
Quoted 8 lines
Quoted 2 lines
Ok, so since the idea is generally agreed, I'll push the x86 and arm support for this in a couple of days.
Currently, I'm looking into implementing Johannes's RT-Signals for ARM64,
but from what I see on the dovetail side, only x86 has gained support to declare traps handled.At least on x86, I see that a trap is considered handled if no switch to in-band has occurred. Did I miss something?
Yes, I do have some first hacks here as well. ARM and ARM64 is not yet ready for this, unfortunately.
Can you please share your hacks?
What is missing? I'll happily continue your work.
Signature · 2 lines
Thanks, //richard
Quoted 7 lines · Richard Weinberger
Quoted 6 lines · Jan Kiszka
Quoted 6 lines
Quoted 2 lines
Ok, so since the idea is generally agreed, I'll push the x86 and arm support for this in a couple of days.
Currently, I'm looking into implementing Johannes's RT-Signals for ARM64,
but from what I see on the dovetail side, only x86 has gained support to declare traps handled.
Quoted 9 lines
Quoted 5 lines
Quoted 2 lines
At least on x86, I see that a trap is considered handled if no switch to in-band has occurred. Did I miss something?
Yes, I do have some first hacks here as well. ARM and ARM64 is not yet ready for this, unfortunately.
Can you please share your hacks?
What is missing? I'll happily continue your work.
Just refreshed [1], and the kernel side hacks are attached (mixture of Johannes and my WIP changes). Blocker is really the level of kernel enabling we need/want for non-x86. Those changes can be many. Also a problem is the information forwarding from the exception handler to the oob callback - missing in many cases, see also the dances needed in xnarch_setup_trap_info().
Jan
[1]
https://source.denx.de/Xenomai/xenomai/-/commits/wip/fault-signals/?ref_type=heads
Signature · 2 lines
Siemens AG, Technology Linux Expert Center
From 4873c7f02d5364646ed4fca272b6336aeb3a5cde Mon Sep 17 00:00:00 2001
From: Johannes Kirchmair <[email protected]>
Date: Tue, 9 May 2023 15:11:51 +0200
Subject: [PATCH 1/3] add dovtail signal setup function
add an interface to dovetail for signal frame setup and restoring.
This is useful for implementing rt signals within Xenomai.
The interface makes it possible to use the setup use functions provided by Linux.
This is just a proof of concept using a very naive approach using most of the code provided by Linux to setup signals.
The patch should be discussed, especially considering if using the Linux functions is viable in a rt context.
Also to discuss would be the interface itself,
for convenience I used the kernels struct ksignal for testing. But maybe we should maintain our own struct, that is not that feature rich.
--- arch/x86/include/asm/sighandling.h | 3 ++ arch/x86/kernel/signal.c | 71 ++++++++++++++++++++++++++++++ arch/x86/kernel/signal_32.c | 2 +- arch/x86/kernel/signal_64.c | 2 +- include/linux/dovetail.h | 4 ++ 5 files changed, 80 insertions(+), 2 deletions(-)
arch/x86/include/asm/sighandling.h +3 -0
@@ -24,4 +24,7 @@ int ia32_setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs);int x64_setup_rt_frame (struct ksignal *ksig,struct pt_regs *regs);int x32_setup_rt_frame (struct ksignal *ksig,struct pt_regs *regs); +bool ia32_restore_sigcontext (struct pt_regs *regs,struct sigcontext_32 __user *usc); +bool restore_sigcontext (struct pt_regs *regs,struct sigcontext __user *usc,unsigned long uc_flags); +#endif /* _ASM_X86_SIGHANDLING_H */
arch/x86/kernel/signal.c +71 -0
74 lines not on this page · raw message
arch/x86/kernel/signal_32.c +1 -1
@@ -74,7 +74,7 @@ static inline void reload_segments(struct sigcontext_32 *sc)/* * Do a signal return; undo the signal stack. */ -static bool ia32_restore_sigcontext (struct pt_regs *regs, +bool ia32_restore_sigcontext (struct pt_regs *regs,struct sigcontext_32 __user *usc) {struct sigcontext_32 sc;
arch/x86/kernel/signal_64.c +1 -1
@@ -47,7 +47,7 @@ static void force_valid_ss(struct pt_regs *regs) regs->ss = __USER_DS; } -static bool restore_sigcontext (struct pt_regs *regs, +bool restore_sigcontext (struct pt_regs *regs,struct sigcontext __user *usc,unsigned long uc_flags) {
include/linux/dovetail.h +4 -0
@@ -151,6 +151,10 @@ void oob_trampoline(void);void arch_inband_task_init (struct task_struct *p); +int dovetail_setup_rt_signal_frame (struct ksignal *ksig,struct pt_regs *regs); + +int dovetail_restore_rt_signal_frame (struct pt_regs *regs); +int dovetail_start (void );void dovetail_stop (void );
Signature · 1 line
2.35.3
From 7bfdc9830fb38adadb849b16383cb1da4b344178 Mon Sep 17 00:00:00 2001
From: Jan Kiszka <[email protected]>
Date: Tue, 17 Oct 2023 14:36:57 +0200
Subject: [PATCH 2/3] arm: signal frames
--- arch/arm/kernel/signal.c | 39 +++++++++++++++++++++++++++++++++++++ arch/arm/mm/alignment.c | 3 ++- arch/arm/mm/fault.c | 42 ++++++++++++++++++++++++---------------- 3 files changed, 66 insertions(+), 18 deletions(-)
arch/arm/kernel/signal.c +39 -0
@@ -599,6 +599,45 @@ static int do_signal(struct pt_regs *regs, int syscall)return 0 ; } +#ifdef CONFIG_DOVETAIL +int dovetail_setup_rt_signal_frame (struct ksignal *ksig,struct pt_regs *regs) +{ +sigset_t *oldset =sigmask_to_save (); +int ret; + +pagefault_disable (); + + ret =setup_rt_frame (ksig, oldset, regs); + ret |= !valid_user_regs (regs); + +pagefault_enable (); + +return ret; +} + +int dovetail_restore_rt_signal_frame (struct pt_regs *regs) +{ +struct rt_sigframe __user *frame; +int ret =0 ; + +if (regs->ARM_sp &7 ) +return -EFAULT ; + + frame = (struct rt_sigframe __user *)regs->ARM_sp; + +pagefault_disable (); + +if (!access_ok (frame,sizeof (*frame)) || +restore_sigframe (regs, &frame->sig) || +restore_altstack (&frame->sig.uc.uc_stack)) + ret = -EFAULT ; + +pagefault_enable (); + +return ret; +} +#endif +static inline void do_retuser (void ) {unsigned int thread_flags;
arch/arm/mm/alignment.c +2 -1
@@ -813,7 +813,8 @@ do_alignment(unsigned long addr, unsigned int fsr, struct pt_regs *regs)if (interrupts_enabled (regs))hard_local_irq_enable (); -mark_trap_entry (ARM_TRAP_ALIGNMENT , regs); +if (!mark_cond_trap_entry (ARM_TRAP_ALIGNMENT , regs)) +return 0 ; instrptr =instruction_pointer (regs);
arch/arm/mm/fault.c +25 -17
98 lines not on this page · raw message
Signature · 1 line
2.35.3
From 9cb4078aac4e42e6d54d77c9c97140dc7055474e Mon Sep 17 00:00:00 2001
From: Jan Kiszka <[email protected]>
Date: Thu, 11 Jan 2024 11:02:34 +0100
Subject: [PATCH 3/3] arm64 rt signals
--- arch/arm64/kernel/signal.c | 39 ++++++++++++++++++++++++++++++++++++++ arch/arm64/kernel/traps.c | 22 ++++++++++----------- arch/arm64/mm/fault.c | 20 +++++++++++++------ 3 files changed, 63 insertions(+), 18 deletions(-)
arch/arm64/kernel/signal.c +39 -0
@@ -1279,6 +1279,45 @@ static void do_signal(struct pt_regs *regs)restore_saved_sigmask (); } +#ifdef CONFIG_DOVETAIL +int dovetail_setup_rt_signal_frame (struct ksignal *ksig,struct pt_regs *regs) +{ +sigset_t *oldset =sigmask_to_save (); +int ret; + +pagefault_disable (); + + ret =setup_rt_frame (ksig->sig, ksig, oldset, regs); + ret |= !valid_user_regs (®s->user_regs, current); + +pagefault_enable (); + +return ret; +} + +int dovetail_restore_rt_signal_frame (struct pt_regs *regs) +{ +struct rt_sigframe __user *frame; +int ret =0 ; + +if (regs->sp &15 ) +return -EFAULT ; + + frame = (struct rt_sigframe __user *)regs->sp; + +pagefault_disable (); + +if (!access_ok (frame,sizeof (*frame)) || +restore_sigframe (regs, frame) || +restore_altstack (&frame->uc.uc_stack)) + ret = -EFAULT ; + +pagefault_enable (); + +return ret; +} +#endif +static inline void do_retuser (void ) {unsigned long thread_flags;
arch/arm64/kernel/traps.c +10 -12
@@ -456,14 +456,8 @@ void do_el0_undef(struct pt_regs *regs, unsigned long esr) {u32 insn; -mark_trap_entry (ARM64_TRAP_UNDI , regs); - -/* -* If the companion core did not switched us to in-band -* context, we may assume that it has handled the trap. -*/ -if (running_oob ()) - goto out_exit; +if (!mark_cond_trap_entry (ARM64_TRAP_UNDI , regs)) +return ;/* check for AArch32 breakpoint instructions */ if (!aarch32_break_handler (regs))
@@ -501,7 +495,8 @@ void do_el1_undef(struct pt_regs *regs, unsigned long esr)void do_el0_bti (struct pt_regs *regs) { -mark_trap_entry (ARM64_TRAP_BTI , regs); +if (!mark_cond_trap_entry (ARM64_TRAP_BTI , regs)) +return ;force_signal_inject (SIGILL ,ILL_ILLOPC , regs->pc,0 );mark_trap_exit (ARM64_TRAP_BTI , regs); }
@@ -590,7 +585,8 @@ static void user_cache_maint_handler(unsigned long esr, struct pt_regs *regs) }if (ret) { -mark_trap_entry (ARM64_TRAP_ACCESS , regs); +if (!mark_cond_trap_entry (ARM64_TRAP_ACCESS , regs)) +return ;arm64_notify_segfault (tagged_address);mark_trap_exit (ARM64_TRAP_ACCESS , regs); }else {
@@ -641,7 +637,8 @@ static void mrs_handler(unsigned long esr, struct pt_regs *regs) sysreg =esr_sys64_to_sysreg (esr);if (do_emulate_mrs (regs, sysreg, rt) !=0 ) { -mark_trap_entry (ARM64_TRAP_ACCESS , regs); +if (!mark_cond_trap_entry (ARM64_TRAP_ACCESS , regs)) +return ;force_signal_inject (SIGILL ,ILL_ILLOPC , regs->pc,0 );mark_trap_exit (ARM64_TRAP_ACCESS , regs); }
@@ -884,7 +881,8 @@ void bad_el0_sync(struct pt_regs *regs, int reason, unsigned long esr) {unsigned long pc =instruction_pointer (regs); -mark_trap_entry (ARM64_TRAP_ACCESS , regs); +if (!mark_cond_trap_entry (ARM64_TRAP_ACCESS , regs)) +return ; current->thread.fault_address =0 ; current->thread.fault_code = esr;
arch/arm64/mm/fault.c +14 -6
@@ -493,7 +493,8 @@ static void do_bad_area(unsigned long far, unsigned long esr,if (user_mode (regs)) {const struct fault_info *inf =esr_to_fault_info (esr); -mark_trap_entry (ARM64_TRAP_ACCESS , regs); +if (!mark_cond_trap_entry (ARM64_TRAP_ACCESS , regs)) +return ;set_thread_esr (addr, esr);arm64_force_sig_fault (inf->sig, inf->code, far, inf->name);mark_trap_exit (ARM64_TRAP_ACCESS , regs);
@@ -549,7 +550,8 @@ static int __kprobes do_page_fault(unsigned long far, unsigned long esr,if (kprobe_page_fault (regs, esr))return 0 ; -mark_trap_entry (ARM64_TRAP_ACCESS , regs); +if (!mark_cond_trap_entry (ARM64_TRAP_ACCESS , regs)) +return 0 ;/* * If we're in an interrupt or have no user context, we must not take
@@ -624,6 +626,7 @@ static int __kprobes do_page_fault(unsigned long far, unsigned long esr,if (fault_signal_pending (fault, regs)) {if (!user_mode (regs)) goto no_context; +mark_trap_exit (ARM64_TRAP_ACCESS , regs);return 0 ; } lock_mmap:
@@ -645,8 +648,10 @@ static int __kprobes do_page_fault(unsigned long far, unsigned long esr, }/* The fault is fully completed (including releasing mmap lock) */ -if (fault &VM_FAULT_COMPLETED ) +if (fault &VM_FAULT_COMPLETED ) { +mark_trap_exit (ARM64_TRAP_ACCESS , regs);return 0 ; + }if (fault &VM_FAULT_RETRY ) { mm_flags |=FAULT_FLAG_TRIED ;
@@ -747,7 +752,8 @@ static int do_sea(unsigned long far, unsigned long esr, struct pt_regs *regs)const struct fault_info *inf;unsigned long siaddr; -mark_trap_entry (ARM64_TRAP_SEA , regs); +if (!mark_cond_trap_entry (ARM64_TRAP_SEA , regs)) +return 0 ; inf =esr_to_fault_info (esr);
@@ -864,7 +870,8 @@ void do_mem_abort(unsigned long far, unsigned long esr, struct pt_regs *regs)if (!inf->fn (far, esr, regs))return ; -mark_trap_entry (ARM64_TRAP_ACCESS , regs); +if (!mark_cond_trap_entry (ARM64_TRAP_ACCESS , regs)) +return ;if (!user_mode (regs))die_kernel_fault (inf->name, addr, esr, regs);
@@ -882,7 +889,8 @@ NOKPROBE_SYMBOL(do_mem_abort);void do_sp_pc_abort (unsigned long addr,unsigned long esr,struct pt_regs *regs) { -mark_trap_entry (ARM64_TRAP_ALIGN , regs); +if (!mark_cond_trap_entry (ARM64_TRAP_ALIGN , regs)) +return ;arm64_notify_die ("SP/PC alignment exception" , regs,SIGBUS ,BUS_ADRALN , addr, esr);
Signature · 1 line
2.35.3
Jan,
Quoted 7 lines · Jan Kiszka
Quoted 6 lines
Quoted 2 lines
Yes, I do have some first hacks here as well. ARM and ARM64 is not yet ready for this, unfortunately.
Can you please share your hacks?
What is missing? I'll happily continue your work.
Quoted 6 lines
Just refreshed [1], and the kernel side hacks are attached (mixture of Johannes and my WIP changes). Blocker is really the level of kernel enabling we need/want for non-x86. Those changes can be many. Also a problem is the information forwarding from the exception handler to the oob callback - missing in many cases, see also the dances needed in
xnarch_setup_trap_info().
In commit 7e13c6997 ("rt-signal: kernel refactorings") I see the following change:
kernel/cobalt/dovetail/kevents.c +0 -0
@@ -56,7 +56,8 @@ void handle_oob_trap_entry(unsigned int trapnr,
struct pt_regs *regs)
xnsched_run();
}- if (xnthread_handle_rt_signals(trapnr, regs) == 0)
+ if (xnthread_test_state(thread, XNUSER) &&
+ xnthread_handle_rt_signals(trapnr, regs))
return;
/*I guess the idea behind is making sure we handle only faults from user mode. But isn't XNUSER also present if a thread is executing a cobalt syscall? So, I think instead of just testing for XNUSER we need to check using user_mode(regs) too.
What do you think?
Signature · 2 lines
Thanks, //richard
Quoted 36 lines · Richard Weinberger
Jan,
Quoted 7 lines · Jan Kiszka
Quoted 6 lines
Quoted 2 lines
Yes, I do have some first hacks here as well. ARM and ARM64 is not yet ready for this, unfortunately.
Can you please share your hacks?
What is missing? I'll happily continue your work.Quoted 6 lines
Just refreshed [1], and the kernel side hacks are attached (mixture of Johannes and my WIP changes). Blocker is really the level of kernel enabling we need/want for non-x86. Those changes can be many. Also a problem is the information forwarding from the exception handler to the oob callback - missing in many cases, see also the dances needed in
xnarch_setup_trap_info().In commit
7e13c6997("rt-signal: kernel refactorings") I see the following change:kernel/cobalt/dovetail/kevents.c +2
-1@@ -56,7 +56,8 @@ void handle_oob_trap_entry(unsigned int trapnr, struct pt_regs *regs)xnsched_run (); } -if (xnthread_handle_rt_signals (trapnr, regs) ==0 ) +if (xnthread_test_state (thread,XNUSER ) && +xnthread_handle_rt_signals (trapnr, regs))return ;/*
Quoted 7 lines
I guess the idea behind is making sure we handle only faults from user mode. But isn't XNUSER also present if a thread is executing a cobalt syscall? So, I think instead of just testing for XNUSER we need to check using
user_mode(regs)too.What do you think?
I think the idea was to exclude kernel threads, but I'm not 100% sure anymore. I agree that testing for user_mode should have that effect as well and exclude kernel oopses - if we don't filter them elsewhere already.
Jan
Signature · 2 lines
Siemens AG, Technology Linux Expert Center
Dear Jan
Quoted 228 lines
-----Original Message-----
From: Jan Kiszka <[email protected]>
Sent: Freitag, 8. Juli 2022 09:11
To: Xenomai <[email protected]>; Philippe Gerum <[email protected]>; Johannes Kirchmair <[email protected]>; Richard Weinberger <[email protected]>
Subject: [RFC][PATCH] x86: dovetail: Permit to declare a trap handledFrom: Jan Kiszka <[email protected]>
This allows to skip reporting a trap to Linux when the OOB handler resolved it directly. The handler can signal this condition by returning "true". This feature was present in I-pipe as well.
By intention, not all spots of
oob_trap_notifyare enabled for this. Debug traps are left out as well as asyncpf in kvm - no use cases from OOB-perspective.NOTE: breaks unprepared users, breaks non-x86.
Signed-off-by Jan Kiszka <[email protected]>---
I'm happy to add non-x86 support as well if the direction is fine.
And if we introduce that to dovetail, we will have to add some kernel version checks to Xenomai so that it does the right thing with the right dovetail versions. Feature flags are out of fashion there as far as I understood.
arch/x86/kernel/traps.c | 38 ++++++++++++++++++++++---------------- arch/x86/mm/fault.c | 9 ++++++--- include/linux/dovetail.h | 9 +++++---- kernel/dovetail.c | 9 ++++++--- 4 files changed, 39 insertions(+), 26 deletions(-)arch/x86/kernel/traps.c +6
-5@@ -163,10 +163,12 @@ static void show_signal(struct task_struct *tsk, int signr, }static __always_inline -void mark_trap_entry(int trapnr, struct pt_regs *regs) +bool mark_trap_entry(int trapnr, struct pt_regs *regs) { -oob_trap_notify (trapnr, regs); +if (oob_trap_notify (trapnr, regs)) +return false;hard_cond_local_irq_enable (); +return true; } static __always_inline@@ -211,9 +213,8 @@ static void do_error_trap(struct pt_regs *regs, long error_code, char *str, {RCU_LOCKDEP_WARN (!rcu_is_watching (),"entry code didn't wake RCU"); -mark_trap_entry (trapnr, regs); - -if (notify_die (DIE_TRAP , str, regs, error_code, trapnr, signr) != +if (mark_trap_entry (trapnr, regs) && +notify_die (DIE_TRAP , str, regs, error_code, trapnr, signr) !=NOTIFY_STOP ) {arch/x86/kernel/traps.c +5
-2cond_local_irq_enable (regs);do_trap (trapnr, signr, str, regs, error_code, sicode, addr);@@ -375,10 +376,12 @@ DEFINE_IDTENTRY_RAW(exc_invalid_op)return ; state =irqentry_enter (regs); -mark_trap_entry (X86_TRAP_UD , regs); +if (!mark_trap_entry (X86_TRAP_UD , regs)) + goto mark_exit;instrumentation_begin ();handle_invalid_op (regs);instrumentation_end (); +mark_exit:mark_trap_exit (X86_TRAP_UD , regs);irqentry_exit (regs, state); }@@ -411,7 +414,8 @@ DEFINE_IDTENTRY_ERRORCODE(exc_alignment_check) {char *str ="alignment check" ; -mark_trap_entry (X86_TRAP_AC , regs); +if (!mark_trap_entry (X86_TRAP_AC , regs)) + goto mark_exit;if (notify_die (DIE_TRAP , str, regs, error_code,X86_TRAP_AC ,SIGBUS ) ==NOTIFY_STOP )arch/x86/kernel/traps.c +11
-9goto mark_exit;@@ -595,7 +599,8 @@ DEFINE_IDTENTRY_DF(exc_double_fault)DEFINE_IDTENTRY (exc_bounds) { -mark_trap_entry (X86_TRAP_BR , regs); +if (!mark_trap_entry (X86_TRAP_BR , regs)) + goto out;if (notify_die (DIE_TRAP ,"bounds" , regs,0 ,X86_TRAP_BR ,SIGSEGV ) ==NOTIFY_STOP )@@ -806,7 +811,8 @@ DEFINE_IDTENTRY_ERRORCODE(exc_general_protection)if (fixup_vdso_exception (regs,X86_TRAP_GP , error_code,0 )) goto exit; -mark_trap_entry (X86_TRAP_GP , regs); +if (!mark_trap_entry (X86_TRAP_GP , regs)) + goto mark_exit;gp_user_force_sig_segv (regs,X86_TRAP_GP , error_code, desc); goto mark_exit; }@@ -1273,7 +1279,8 @@ static void math_error(struct pt_regs *regs, int trapnr)if (fixup_exception (regs, trapnr,0 ,0 )) goto exit; -mark_trap_entry (trapnr, regs); +if (!mark_trap_entry (trapnr, regs)) + goto mark_exit; task->thread.error_code =0 ; task->thread.trap_nr = trapnr;@@ -1300,10 +1307,9 @@ static void math_error(struct pt_regs *regs, int trapnr)if (fixup_vdso_exception (regs, trapnr,0 ,0 )) goto exit; -mark_trap_entry (trapnr, regs); - -force_sig_fault (SIGFPE , si_code, - (void __user *)uprobe_get_trap_addr (regs)); +if (mark_trap_entry (trapnr, regs)) +force_sig_fault (SIGFPE , si_code, + (void __user *)uprobe_get_trap_addr (regs)); mark_exit:mark_trap_exit (trapnr, regs); exit:@@ -1416,8 +1422,8 @@ DEFINE_IDTENTRY(exc_device_not_available)* to kill the task than getting stuck in a never-ending * loop of #NM faults. */ -mark_trap_entry (X86_TRAP_NM , regs); -die ("unexpected #NM exception" , regs,0 ); +if (mark_trap_entry (X86_TRAP_NM , regs)) +die ("unexpected #NM exception" , regs,0 );mark_trap_exit (X86_TRAP_NM , regs); } }arch/x86/mm/fault.c +2
-1@@ -650,7 +650,8 @@ page_fault_oops(struct pt_regs *regs, unsigned longerror_code, * Do not bother unwinding the notification context on * CPU/firmware/kernel bug. */ -oob_trap_notify (X86_TRAP_PF , regs); +if (oob_trap_notify (X86_TRAP_PF , regs)) +return ;#ifdef CONFIG_VMAP_STACK arch/x86/mm/fault.c +2
-1/* @@ -890,7 +891,8 @@ __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code, if (fixup_vdso_exception(regs, X86_TRAP_PF, error_code, address)) return; -oob_trap_notify (X86_TRAP_PF ,regs ); +if (oob_trap_notify (X86_TRAP_PF , regs)) +return ;if (likely (show_unhandled_signals))arch/x86/mm/fault.c +2
-1show_signal_msg (regs, error_code, address, tsk);@@ -1363,7 +1365,8 @@ void do_user_addr_fault(struct pt_regs *regs,* event, so that it might switch current to in-band mode if * need be. */ -oob_trap_notify (X86_TRAP_PF , regs); +if (oob_trap_notify (X86_TRAP_PF , regs)) +return ;perf_sw_event (PERF_COUNT_SW_PAGE_FAULTS ,1 , regs, address);include/linux/dovetail.h +5
-4@@ -58,14 +58,15 @@ void inband_task_init(struct task_struct *p);int pipeline_syscall (unsigned int nr,struct pt_regs *regs); -void __oob_trap_notify (unsigned int exception, +bool __oob_trap_notify (unsigned int exception,struct pt_regs *regs); -static __always_inline void oob_trap_notify(unsigned int exception, +static __always_inline bool oob_trap_notify(unsigned int exception,struct pt_regs *regs) {if (running_oob () && !test_thread_local_flags (_TLF_OOBTRAP)) -__oob_trap_notify (exception, regs); +return __oob_trap_notify (exception, regs); +return false; } void __oob_trap_unwind(unsigned int exception,@@ -248,7 +249,7 @@ static inline void arch_dovetail_exec_prepare(void)* Keep the trap helpers as macros, we might not be able to resolve * trap numbers if CONFIG_DOVETAIL is off. */ -#define oob_trap_notify (__exception, __regs) do { } while (0) +#define oob_trap_notify (__exception, __regs) false#define oob_trap_unwind (__exception, __regs) do { } while (0) static inlinekernel/dovetail.c +1
-1@@ -254,14 +254,16 @@ int pipeline_syscall(unsigned int nr, struct pt_regs *regs)return 0 ;/* pass syscall down to the in-band dispatcher. */ } -void __weak handle_oob_trap_entry (unsigned int trapnr,struct pt_regs *regs) +bool __weak handle_oob_trap_entry (unsigned int trapnr,struct pt_regs *regs)
kernel/dovetail.c int pipeline_syscall(unsigned int nr, struct pt_regs · line 267
Quoted 10 lines
kernel/dovetail.c +3
-1{ +return false; } -noinstr void __oob_trap_notify(unsigned int exception, +noinstr bool __oob_trap_notify(unsigned int exception,struct pt_regs *regs) {unsigned long flags; +bool handled;
Wouldn’t it be better to have this initialized to false?
So if dovetail_enabled is false, we don’t return a not well defined value? Or do I miss something.
Best regards Johannes
Quoted 18 lines
kernel/dovetail.c +2
-2/* * We send a notification about exceptions raised over a @@ -273,10 +275,11 @@ noinstr void __oob_trap_notify(unsigned int exception,set_thread_local_flags (_TLF_OOBTRAP); flags =hard_local_irq_save ();instrumentation_begin (); -handle_oob_trap_entry (exception, regs); + handled =handle_oob_trap_entry (exception, regs);instrumentation_end ();hard_local_irq_restore (flags); } +return handled; }void __weak handle_oob_trap_exit (unsigned int trapnr,struct pt_regs *regs) --2.35.3
Quoted 234 lines · Johannes Kirchmair
Dear Jan
Quoted 232 lines
-----Original Message-----
From: Jan Kiszka <[email protected]>
Sent: Freitag, 8. Juli 2022 09:11
To: Xenomai <[email protected]>; Philippe Gerum <[email protected]>; Johannes Kirchmair <[email protected]>; Richard Weinberger <[email protected]>
Subject: [RFC][PATCH] x86: dovetail: Permit to declare a trap handledFrom: Jan Kiszka <[email protected]>
This allows to skip reporting a trap to Linux when the OOB handler resolved it directly. The handler can signal this condition by returning "true". This feature was present in I-pipe as well.
By intention, not all spots of
oob_trap_notifyare enabled for this. Debug traps are left out as well as asyncpf in kvm - no use cases from OOB-perspective.NOTE: breaks unprepared users, breaks non-x86.
Signed-off-by Jan Kiszka <[email protected]>---
I'm happy to add non-x86 support as well if the direction is fine.
And if we introduce that to dovetail, we will have to add some kernel version checks to Xenomai so that it does the right thing with the right dovetail versions. Feature flags are out of fashion there as far as I understood.
arch/x86/kernel/traps.c | 38 ++++++++++++++++++++++---------------- arch/x86/mm/fault.c | 9 ++++++--- include/linux/dovetail.h | 9 +++++---- kernel/dovetail.c | 9 ++++++--- 4 files changed, 39 insertions(+), 26 deletions(-)arch/x86/kernel/traps.c +6
-5@@ -163,10 +163,12 @@ static void show_signal(struct task_struct *tsk, int signr, }static __always_inline -void mark_trap_entry(int trapnr, struct pt_regs *regs) +bool mark_trap_entry(int trapnr, struct pt_regs *regs) { -oob_trap_notify (trapnr, regs); +if (oob_trap_notify (trapnr, regs)) +return false;hard_cond_local_irq_enable (); +return true; } static __always_inline@@ -211,9 +213,8 @@ static void do_error_trap(struct pt_regs *regs, long error_code, char *str, {RCU_LOCKDEP_WARN (!rcu_is_watching (),"entry code didn't wake RCU"); -mark_trap_entry (trapnr, regs); - -if (notify_die (DIE_TRAP , str, regs, error_code, trapnr, signr) != +if (mark_trap_entry (trapnr, regs) && +notify_die (DIE_TRAP , str, regs, error_code, trapnr, signr) !=NOTIFY_STOP ) {arch/x86/kernel/traps.c +5
-2cond_local_irq_enable (regs);do_trap (trapnr, signr, str, regs, error_code, sicode, addr);@@ -375,10 +376,12 @@ DEFINE_IDTENTRY_RAW(exc_invalid_op)return ; state =irqentry_enter (regs); -mark_trap_entry (X86_TRAP_UD , regs); +if (!mark_trap_entry (X86_TRAP_UD , regs)) + goto mark_exit;instrumentation_begin ();handle_invalid_op (regs);instrumentation_end (); +mark_exit:mark_trap_exit (X86_TRAP_UD , regs);irqentry_exit (regs, state); }@@ -411,7 +414,8 @@ DEFINE_IDTENTRY_ERRORCODE(exc_alignment_check) {char *str ="alignment check" ; -mark_trap_entry (X86_TRAP_AC , regs); +if (!mark_trap_entry (X86_TRAP_AC , regs)) + goto mark_exit;if (notify_die (DIE_TRAP , str, regs, error_code,X86_TRAP_AC ,SIGBUS ) ==NOTIFY_STOP )arch/x86/kernel/traps.c +11
-9goto mark_exit;@@ -595,7 +599,8 @@ DEFINE_IDTENTRY_DF(exc_double_fault)DEFINE_IDTENTRY (exc_bounds) { -mark_trap_entry (X86_TRAP_BR , regs); +if (!mark_trap_entry (X86_TRAP_BR , regs)) + goto out;if (notify_die (DIE_TRAP ,"bounds" , regs,0 ,X86_TRAP_BR ,SIGSEGV ) ==NOTIFY_STOP )@@ -806,7 +811,8 @@ DEFINE_IDTENTRY_ERRORCODE(exc_general_protection)if (fixup_vdso_exception (regs,X86_TRAP_GP , error_code,0 )) goto exit; -mark_trap_entry (X86_TRAP_GP , regs); +if (!mark_trap_entry (X86_TRAP_GP , regs)) + goto mark_exit;gp_user_force_sig_segv (regs,X86_TRAP_GP , error_code, desc); goto mark_exit; }@@ -1273,7 +1279,8 @@ static void math_error(struct pt_regs *regs, int trapnr)if (fixup_exception (regs, trapnr,0 ,0 )) goto exit; -mark_trap_entry (trapnr, regs); +if (!mark_trap_entry (trapnr, regs)) + goto mark_exit; task->thread.error_code =0 ; task->thread.trap_nr = trapnr;@@ -1300,10 +1307,9 @@ static void math_error(struct pt_regs *regs, int trapnr)if (fixup_vdso_exception (regs, trapnr,0 ,0 )) goto exit; -mark_trap_entry (trapnr, regs); - -force_sig_fault (SIGFPE , si_code, - (void __user *)uprobe_get_trap_addr (regs)); +if (mark_trap_entry (trapnr, regs)) +force_sig_fault (SIGFPE , si_code, + (void __user *)uprobe_get_trap_addr (regs)); mark_exit:mark_trap_exit (trapnr, regs); exit:@@ -1416,8 +1422,8 @@ DEFINE_IDTENTRY(exc_device_not_available)* to kill the task than getting stuck in a never-ending * loop of #NM faults. */ -mark_trap_entry (X86_TRAP_NM , regs); -die ("unexpected #NM exception" , regs,0 ); +if (mark_trap_entry (X86_TRAP_NM , regs)) +die ("unexpected #NM exception" , regs,0 );mark_trap_exit (X86_TRAP_NM , regs); } }arch/x86/mm/fault.c +2
-1@@ -650,7 +650,8 @@ page_fault_oops(struct pt_regs *regs, unsigned longerror_code, * Do not bother unwinding the notification context on * CPU/firmware/kernel bug. */ -oob_trap_notify (X86_TRAP_PF , regs); +if (oob_trap_notify (X86_TRAP_PF , regs)) +return ;#ifdef CONFIG_VMAP_STACK arch/x86/mm/fault.c +2
-1/* @@ -890,7 +891,8 @@ __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code, if (fixup_vdso_exception(regs, X86_TRAP_PF, error_code, address)) return; -oob_trap_notify (X86_TRAP_PF ,regs ); +if (oob_trap_notify (X86_TRAP_PF , regs)) +return ;if (likely (show_unhandled_signals))arch/x86/mm/fault.c +2
-1show_signal_msg (regs, error_code, address, tsk);@@ -1363,7 +1365,8 @@ void do_user_addr_fault(struct pt_regs *regs,* event, so that it might switch current to in-band mode if * need be. */ -oob_trap_notify (X86_TRAP_PF , regs); +if (oob_trap_notify (X86_TRAP_PF , regs)) +return ;perf_sw_event (PERF_COUNT_SW_PAGE_FAULTS ,1 , regs, address);include/linux/dovetail.h +5
-4@@ -58,14 +58,15 @@ void inband_task_init(struct task_struct *p);int pipeline_syscall (unsigned int nr,struct pt_regs *regs); -void __oob_trap_notify (unsigned int exception, +bool __oob_trap_notify (unsigned int exception,struct pt_regs *regs); -static __always_inline void oob_trap_notify(unsigned int exception, +static __always_inline bool oob_trap_notify(unsigned int exception,struct pt_regs *regs) {if (running_oob () && !test_thread_local_flags (_TLF_OOBTRAP)) -__oob_trap_notify (exception, regs); +return __oob_trap_notify (exception, regs); +return false; } void __oob_trap_unwind(unsigned int exception,@@ -248,7 +249,7 @@ static inline void arch_dovetail_exec_prepare(void)* Keep the trap helpers as macros, we might not be able to resolve * trap numbers if CONFIG_DOVETAIL is off. */ -#define oob_trap_notify (__exception, __regs) do { } while (0) +#define oob_trap_notify (__exception, __regs) false#define oob_trap_unwind (__exception, __regs) do { } while (0) static inlinekernel/dovetail.c +2
-1@@ -254,14 +254,16 @@ int pipeline_syscall(unsigned int nr, struct pt_regs *regs)return 0 ;/* pass syscall down to the in-band dispatcher. */ } -void __weak handle_oob_trap_entry (unsigned int trapnr,struct pt_regs *regs) +bool __weak handle_oob_trap_entry (unsigned int trapnr,struct pt_regs *regs) { +return false; }
Quoted 10 lines
kernel/dovetail.c int pipeline_syscall(unsigned int nr, struct pt_regs *regs) · line 266
Quoted 6 lines
kernel/dovetail.c +2
-1-noinstr void __oob_trap_notify(unsigned int exception, +noinstr bool __oob_trap_notify(unsigned int exception,struct pt_regs *regs) {unsigned long flags; +bool handled;Wouldn’t it be better to have this initialized to false?
So ifdovetail_enabledis false, we don’t return a not well defined value? Or do I miss something.
Yes, this is a bug.
Jan
Quoted 21 lines
Best regards Johannes
Quoted 18 lines
kernel/dovetail.c +2
-2/* * We send a notification about exceptions raised over a @@ -273,10 +275,11 @@ noinstr void __oob_trap_notify(unsigned int exception,set_thread_local_flags (_TLF_OOBTRAP); flags =hard_local_irq_save ();instrumentation_begin (); -handle_oob_trap_entry (exception, regs); + handled =handle_oob_trap_entry (exception, regs);instrumentation_end ();hard_local_irq_restore (flags); } +return handled; }void __weak handle_oob_trap_exit (unsigned int trapnr,struct pt_regs *regs) --2.35.3
Signature · 2 lines
Siemens AG, Technology Competence Center Embedded Linux