All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH dovetail 0/5] riscv: dovetail: trap handling fixes
@ 2026-11-17 12:39 Tobias Schaffner
  2026-11-17 12:39 ` [PATCH dovetail 1/5] riscv: dovetail: do not notify traps again from do_trap() Tobias Schaffner
                   ` (6 more replies)
  0 siblings, 7 replies; 14+ messages in thread
From: Tobias Schaffner @ 2026-11-17 12:39 UTC (permalink / raw)
  To: xenomai; +Cc: rpm, jan.kiszka, Tobias Schaffner

Hi all,

this series fixes the riscv trap handling of Dovetail.

Thanks to Jan for sharing his AI reviews on this which made me have
a look at this.

The bugs were verified by running a trap matrix test, which raises
every kind of trap from an out-of-band EVL thread, with a more detailed
oob_trap RV monitor from the Dovetail RV RFC enabled.

Testing was done on v7.3 with EVL under QEMU riscv64: the trap matrix
test, the libevl suite and kprobe tests hitting out-of-band syscalls,
IRQ handlers and nested faults all pass with a clean kernel log and no
monitor reaction. Reverting the fixes one at a time reproduces each
bug except the last one.

Thanks,
Tobias

Tobias Schaffner (5):
  riscv: dovetail: do not notify traps again from do_trap()
  riscv: dovetail: run kernel trap handlers the core did not demote for
  riscv: dovetail: do not nest the unknown trap handler in software
    checks
  riscv: dovetail: keep hard irqs off for page faults handled
    out-of-band
  riscv: dovetail: read the CFI trap value from the saved registers

 arch/riscv/kernel/traps.c | 60 +++++++++++++++++++++------------------
 1 file changed, 32 insertions(+), 28 deletions(-)


base-commit: de6ccdbc1027fda48257c4033c90d57da3e676ea
-- 
2.53.0


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [PATCH dovetail 1/5] riscv: dovetail: do not notify traps again from do_trap()
  2026-11-17 12:39 [PATCH dovetail 0/5] riscv: dovetail: trap handling fixes Tobias Schaffner
@ 2026-11-17 12:39 ` Tobias Schaffner
  2026-11-17 12:39 ` [PATCH dovetail 2/5] riscv: dovetail: run kernel trap handlers the core did not demote for Tobias Schaffner
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 14+ messages in thread
From: Tobias Schaffner @ 2026-11-17 12:39 UTC (permalink / raw)
  To: xenomai; +Cc: rpm, jan.kiszka, Tobias Schaffner

do_trap() is called by trap handlers which already notified the
companion core. The nested mark_trap_exit() unwinds the trap before the
outer handler is done, which then runs with hard irqs off, or over the
out-of-band stage if the trap_exit handler switched back to it.

Drop the nested notification.

Fixes: 1d1fdeccecef ("riscv: dovetail: add core support")
Signed-off-by: Tobias Schaffner <tobias.schaffner@siemens.com>
---
 arch/riscv/kernel/traps.c | 5 -----
 1 file changed, 5 deletions(-)

diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c
index 897fb63ff10b..e2d06cb87c6e 100644
--- a/arch/riscv/kernel/traps.c
+++ b/arch/riscv/kernel/traps.c
@@ -142,9 +142,6 @@ void do_trap(struct pt_regs *regs, int signo, int code, unsigned long addr)
 {
 	struct task_struct *tsk = current;
 
-	if (!mark_trap_entry(regs))
-		return;
-
 	if (show_unhandled_signals && unhandled_signal(tsk, signo)
 	    && printk_ratelimit()) {
 		pr_info("%s[%d]: unhandled signal %d code 0x%x at 0x" REG_FMT,
@@ -156,8 +153,6 @@ void do_trap(struct pt_regs *regs, int signo, int code, unsigned long addr)
 	}
 
 	force_sig_fault(signo, code, (void __user *)addr);
-
-	mark_trap_exit(regs);
 }
 
 static void do_trap_error(struct pt_regs *regs, int signo, int code,
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH dovetail 2/5] riscv: dovetail: run kernel trap handlers the core did not demote for
  2026-11-17 12:39 [PATCH dovetail 0/5] riscv: dovetail: trap handling fixes Tobias Schaffner
  2026-11-17 12:39 ` [PATCH dovetail 1/5] riscv: dovetail: do not notify traps again from do_trap() Tobias Schaffner
@ 2026-11-17 12:39 ` Tobias Schaffner
  2026-11-17 12:39 ` [PATCH dovetail 3/5] riscv: dovetail: do not nest the unknown trap handler in software checks Tobias Schaffner
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 14+ messages in thread
From: Tobias Schaffner @ 2026-11-17 12:39 UTC (permalink / raw)
  To: xenomai; +Cc: rpm, jan.kiszka, Tobias Schaffner

A kernel mode trap is skipped if the companion core leaves the current
context out-of-band. Kernel mode traps are notified from within
irqentry_nmi_enter(), where a core may not demote the current task (EVL
bails out on in_nmi()), so a kprobe, an access fault or a misaligned
access hit out-of-band in kernel mode is never handled and the faulting
instruction traps again forever.

Run the kernel handler regardless, as arm64 does. Only user mode traps
are still skipped, the core having to deal with those itself.

Fixes: 1d1fdeccecef ("riscv: dovetail: add core support")
Signed-off-by: Tobias Schaffner <tobias.schaffner@siemens.com>
---
 arch/riscv/kernel/traps.c | 33 ++++++++++++++++++---------------
 1 file changed, 18 insertions(+), 15 deletions(-)

diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c
index e2d06cb87c6e..a8e37ec27cc2 100644
--- a/arch/riscv/kernel/traps.c
+++ b/arch/riscv/kernel/traps.c
@@ -113,6 +113,11 @@ void die(struct pt_regs *regs, const char *str)
 		make_task_dead(SIGSEGV);
 }
 
+/*
+ * Kernel mode callers ignore the return value: skipping their handler
+ * would trap again on the same instruction, so it runs whether the
+ * core demoted us or not.
+ */
 static __always_inline
 bool mark_trap_entry(struct pt_regs *regs)
 {
@@ -183,10 +188,9 @@ asmlinkage __visible __trap_section void name(struct pt_regs *regs)		\
 		irqentry_exit_to_user_mode(regs);				\
 	} else {								\
 		irqentry_state_t state = irqentry_nmi_enter(regs);		\
-		if (mark_trap_entry(regs)) {					\
-			do_trap_error(regs, signo, code, regs->epc, "Oops - " str); \
-			mark_trap_exit(regs);					\
-		}								\
+		mark_trap_entry(regs);						\
+		do_trap_error(regs, signo, code, regs->epc, "Oops - " str);	\
+		mark_trap_exit(regs);						\
 		irqentry_nmi_exit(regs, state);					\
 	}									\
 }
@@ -221,11 +225,11 @@ asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *re
 		irqentry_exit_to_user_mode(regs);
 	} else {
 		irqentry_state_t state = irqentry_nmi_enter(regs);
-		if (mark_trap_entry(regs)) {
-			do_trap_error(regs, SIGILL, ILL_ILLOPC, regs->epc,
-				      "Oops - illegal instruction");
-			mark_trap_exit(regs);
-		}
+
+		mark_trap_entry(regs);
+		do_trap_error(regs, SIGILL, ILL_ILLOPC, regs->epc,
+			      "Oops - illegal instruction");
+		mark_trap_exit(regs);
 		irqentry_nmi_exit(regs, state);
 	}
 }
@@ -262,8 +266,7 @@ static void do_trap_misaligned(struct pt_regs *regs, enum misaligned_access_type
 		local_irq_enable();
 	} else {
 		state = irqentry_nmi_enter(regs);
-		if (!mark_trap_entry(regs))
-			goto out;
+		mark_trap_entry(regs);
 	}
 
 	if (misaligned_handler[type].handler(regs))
@@ -366,10 +369,10 @@ asmlinkage __visible __trap_section void do_trap_break(struct pt_regs *regs)
 		irqentry_exit_to_user_mode(regs);
 	} else {
 		irqentry_state_t state = irqentry_nmi_enter(regs);
-		if (mark_trap_entry(regs)) {
-			handle_break(regs);
-			mark_trap_exit(regs);
-		}
+
+		mark_trap_entry(regs);
+		handle_break(regs);
+		mark_trap_exit(regs);
 		irqentry_nmi_exit(regs, state);
 	}
 }
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH dovetail 3/5] riscv: dovetail: do not nest the unknown trap handler in software checks
  2026-11-17 12:39 [PATCH dovetail 0/5] riscv: dovetail: trap handling fixes Tobias Schaffner
  2026-11-17 12:39 ` [PATCH dovetail 1/5] riscv: dovetail: do not notify traps again from do_trap() Tobias Schaffner
  2026-11-17 12:39 ` [PATCH dovetail 2/5] riscv: dovetail: run kernel trap handlers the core did not demote for Tobias Schaffner
@ 2026-11-17 12:39 ` Tobias Schaffner
  2026-11-17 20:38   ` Florian Bezdeka
  2026-11-17 12:39 ` [PATCH dovetail 4/5] riscv: dovetail: keep hard irqs off for page faults handled out-of-band Tobias Schaffner
                   ` (3 subsequent siblings)
  6 siblings, 1 reply; 14+ messages in thread
From: Tobias Schaffner @ 2026-11-17 12:39 UTC (permalink / raw)
  To: xenomai; +Cc: rpm, jan.kiszka, Tobias Schaffner

do_trap_software_check() hands a user mode trap which is no CFI
violation to the whole do_trap_unknown() handler, which enters from
user mode and notifies the companion core again on the same frame.
The nested mark_trap_exit() unwinds the trap before the outer handler
is done.

Call do_trap_error() as do_trap_unknown() would.

Fixes: 1d1fdeccecef ("riscv: dovetail: add core support")
Signed-off-by: Tobias Schaffner <tobias.schaffner@siemens.com>
---
 arch/riscv/kernel/traps.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c
index a8e37ec27cc2..dddb30711440 100644
--- a/arch/riscv/kernel/traps.c
+++ b/arch/riscv/kernel/traps.c
@@ -473,7 +473,8 @@ asmlinkage __visible __trap_section void do_trap_software_check(struct pt_regs *
 		if (mark_trap_entry(regs)) {
 			/* not a cfi violation, then merge into flow of unknown trap handler */
 			if (!handle_user_cfi_violation(regs))
-				do_trap_unknown(regs);
+				do_trap_error(regs, SIGILL, ILL_ILLTRP, regs->epc,
+					      "Oops - unknown exception");
 
 			mark_trap_exit(regs);
 		}
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH dovetail 4/5] riscv: dovetail: keep hard irqs off for page faults handled out-of-band
  2026-11-17 12:39 [PATCH dovetail 0/5] riscv: dovetail: trap handling fixes Tobias Schaffner
                   ` (2 preceding siblings ...)
  2026-11-17 12:39 ` [PATCH dovetail 3/5] riscv: dovetail: do not nest the unknown trap handler in software checks Tobias Schaffner
@ 2026-11-17 12:39 ` Tobias Schaffner
  2026-11-17 19:24   ` Florian Bezdeka
  2026-11-17 12:39 ` [PATCH dovetail 5/5] riscv: dovetail: read the CFI trap value from the saved registers Tobias Schaffner
                   ` (2 subsequent siblings)
  6 siblings, 1 reply; 14+ messages in thread
From: Tobias Schaffner @ 2026-11-17 12:39 UTC (permalink / raw)
  To: xenomai; +Cc: rpm, jan.kiszka, Tobias Schaffner

do_page_fault() enables hard irqs for kernel mode faults whether the
companion core demoted the current context or not. A kernel mode fault
the core leaves out-of-band, e.g. a faulting kprobe fetch argument within
a kernel trap, then runs the fault handler over the out-of-band stage
with hard irqs on, where an out-of-band interrupt may reschedule in the
middle of the outer trap. With CONFIG_DEBUG_DOVETAIL, the BUG_ON()
checking for a demotion fires instead.

Toggle hard irqs and the in-band stall only if the core demoted us, and
keep the debug check for user mode faults.

Fixes: 1d1fdeccecef ("riscv: dovetail: add core support")
Signed-off-by: Tobias Schaffner <tobias.schaffner@siemens.com>
---
 arch/riscv/kernel/traps.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c
index dddb30711440..9f8dc6bd7b85 100644
--- a/arch/riscv/kernel/traps.c
+++ b/arch/riscv/kernel/traps.c
@@ -489,21 +489,26 @@ asmlinkage __visible __trap_section void do_trap_software_check(struct pt_regs *
 asmlinkage __visible noinstr void do_page_fault(struct pt_regs *regs)
 {
 	irqentry_state_t state = irqentry_enter(regs);
+	bool inband = mark_trap_entry(regs);
 
-	mark_trap_entry(regs);
-
-	BUG_ON(dovetail_debug() && !running_inband());
+	/*
+	 * Dovetail: a kernel mode fault the core did not demote for, e.g.
+	 * a faulting kprobe fetch within a kernel trap, is handled over
+	 * the out-of-band stage with hard irqs off.
+	 */
+	BUG_ON(dovetail_debug() && !inband && user_mode(regs));
 
-	if (!user_mode(regs))
+	if (!user_mode(regs) && inband)
 		hard_cond_local_irq_enable();
 
 	handle_page_fault(regs, state);
 
-	local_irq_disable();
+	if (inband)
+		local_irq_disable();
 
 	mark_trap_exit(regs);
 
-	if (!user_mode(regs))
+	if (!user_mode(regs) && inband)
 		hard_cond_local_irq_disable();
 
 	irqentry_exit(regs, state);
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH dovetail 5/5] riscv: dovetail: read the CFI trap value from the saved registers
  2026-11-17 12:39 [PATCH dovetail 0/5] riscv: dovetail: trap handling fixes Tobias Schaffner
                   ` (3 preceding siblings ...)
  2026-11-17 12:39 ` [PATCH dovetail 4/5] riscv: dovetail: keep hard irqs off for page faults handled out-of-band Tobias Schaffner
@ 2026-11-17 12:39 ` Tobias Schaffner
  2026-11-17 17:15 ` [PATCH dovetail 0/5] riscv: dovetail: trap handling fixes Jan Kiszka
  2026-11-17 19:16 ` Florian Bezdeka
  6 siblings, 0 replies; 14+ messages in thread
From: Tobias Schaffner @ 2026-11-17 12:39 UTC (permalink / raw)
  To: xenomai; +Cc: rpm, jan.kiszka, Tobias Schaffner

handle_user_cfi_violation() reads CSR_TVAL after mark_trap_entry()
enabled hard irqs for the user mode trap. An interrupt taken in between
overwrites stval, so a CFI violation may be taken for an unknown
exception, or the other way around.

Use the trap value saved on exception entry.

Fixes: 1d1fdeccecef ("riscv: dovetail: add core support")
Signed-off-by: Tobias Schaffner <tobias.schaffner@siemens.com>
---
 arch/riscv/kernel/traps.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c
index 9f8dc6bd7b85..25ce3cc58cc8 100644
--- a/arch/riscv/kernel/traps.c
+++ b/arch/riscv/kernel/traps.c
@@ -433,7 +433,7 @@ void do_trap_ecall_u(struct pt_regs *regs)
 /* handle cfi violations */
 bool handle_user_cfi_violation(struct pt_regs *regs)
 {
-	unsigned long tval = csr_read(CSR_TVAL);
+	unsigned long tval = regs->badaddr;
 	bool is_fcfi = (tval == CFI_TVAL_FCFI_CODE && cpu_supports_indirect_br_lp_instr());
 	bool is_bcfi = (tval == CFI_TVAL_BCFI_CODE && cpu_supports_shadow_stack());
 
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* Re: [PATCH dovetail 0/5] riscv: dovetail: trap handling fixes
  2026-11-17 12:39 [PATCH dovetail 0/5] riscv: dovetail: trap handling fixes Tobias Schaffner
                   ` (4 preceding siblings ...)
  2026-11-17 12:39 ` [PATCH dovetail 5/5] riscv: dovetail: read the CFI trap value from the saved registers Tobias Schaffner
@ 2026-11-17 17:15 ` Jan Kiszka
  2026-11-17 18:15   ` Tobias Schaffner
  2026-11-17 19:16 ` Florian Bezdeka
  6 siblings, 1 reply; 14+ messages in thread
From: Jan Kiszka @ 2026-11-17 17:15 UTC (permalink / raw)
  To: Tobias Schaffner, xenomai; +Cc: rpm

On 17.11.26 13:39, Tobias Schaffner wrote:
> Hi all,
> 
> this series fixes the riscv trap handling of Dovetail.
> 
> Thanks to Jan for sharing his AI reviews on this which made me have
> a look at this.
> 
> The bugs were verified by running a trap matrix test, which raises
> every kind of trap from an out-of-band EVL thread, with a more detailed
> oob_trap RV monitor from the Dovetail RV RFC enabled.
> 
> Testing was done on v7.3 with EVL under QEMU riscv64: the trap matrix
> test, the libevl suite and kprobe tests hitting out-of-band syscalls,
> IRQ handlers and nested faults all pass with a clean kernel log and no
> monitor reaction. Reverting the fixes one at a time reproduces each
> bug except the last one.

Did you also ask the AI to review you patches? I may come back with "all
fine, but I found 5 other bugs" :). Still, this can be worth it.

Jan

-- 
Siemens AG, Foundational Technologies
Linux Expert Center

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH dovetail 0/5] riscv: dovetail: trap handling fixes
  2026-11-17 17:15 ` [PATCH dovetail 0/5] riscv: dovetail: trap handling fixes Jan Kiszka
@ 2026-11-17 18:15   ` Tobias Schaffner
  2026-11-17 18:32     ` Jan Kiszka
  0 siblings, 1 reply; 14+ messages in thread
From: Tobias Schaffner @ 2026-11-17 18:15 UTC (permalink / raw)
  To: Jan Kiszka, xenomai; +Cc: rpm

On 11/17/26 18:15, Jan Kiszka wrote:
> On 17.11.26 13:39, Tobias Schaffner wrote:
>> Hi all,
>>
>> this series fixes the riscv trap handling of Dovetail.
>>
>> Thanks to Jan for sharing his AI reviews on this which made me have
>> a look at this.
>>
>> The bugs were verified by running a trap matrix test, which raises
>> every kind of trap from an out-of-band EVL thread, with a more detailed
>> oob_trap RV monitor from the Dovetail RV RFC enabled.
>>
>> Testing was done on v7.3 with EVL under QEMU riscv64: the trap matrix
>> test, the libevl suite and kprobe tests hitting out-of-band syscalls,
>> IRQ handlers and nested faults all pass with a clean kernel log and no
>> monitor reaction. Reverting the fixes one at a time reproduces each
>> bug except the last one.
> 
> Did you also ask the AI to review you patches? I may come back with "all
> fine, but I found 5 other bugs" :). Still, this can be worth it.

Hi Jan,

yes that's part of my workflow. I am doing some back and forth most of
the time. In this case I had reproducers for all the patches and
reviewed and tested them one by one.

It is not very special but this is the definition of my review agent:

---
name: Review Agent
description: A strict, systems-level code review agent. Use when you 
want blunt, technically rigorous feedback focused on correctness, 
determinism, simplicity, and performance.
argument-hint: The code or diff to review.
tools: ['read', 'search']
---

You are a senior systems engineer performing a strict technical code review.

## Personality and Style

- Direct, precise, and technically grounded.
- Blunt but professional.
- No motivational fluff.
- No emojis.
- No softening language.
- Prefer correctness over style.
- Prefer simplicity over abstraction.
- Prefer determinism over convenience.
- Performance-aware.
- Security-aware.
- Skeptical of overengineering.

You have low tolerance for:

- Unnecessary abstraction
- Hidden control flow
- Magic behavior
- Excessive dependencies
- Premature generalization
- Non-deterministic behavior without justification

---

## Review Rules

1. Identify correctness issues first.
2. Flag undefined behavior, race conditions, edge cases.
3. Call out hidden complexity explicitly.
4. Prefer explicit control flow.
5. Question unnecessary layers of abstraction.
6. Highlight performance and memory implications.
7. Scrutinize concurrency carefully.
8. Reject premature abstraction.
9. Do not rewrite the entire code unless fundamentally broken.
10. Provide concrete, actionable corrections.

---

## Output Format (Always Use This Structure)

### Critical Issues
- Correctness bugs, race conditions, undefined behavior, security risks.

### Design Problems
- Architectural flaws, unnecessary abstraction, layering violations.

### Minor Issues
- Style issues, naming, clarity improvements.

### What Is Actually Good
- Brief acknowledgment of solid engineering decisions.

---

## Tone Examples

Instead of:
"Consider simplifying this abstraction."

Say:
"This abstraction adds no value and hides control flow. Remove it."

Instead of:
"Error handling could be improved."

Say:
"Error handling is incomplete. This will fail silently under load."

---

## Default Assumptions

- Code is production-bound.
- Bugs are expensive.
- Simplicity wins.
- Explicit beats clever.
- Deterministic behavior is required.


> Jan
> 


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH dovetail 0/5] riscv: dovetail: trap handling fixes
  2026-11-17 18:15   ` Tobias Schaffner
@ 2026-11-17 18:32     ` Jan Kiszka
  0 siblings, 0 replies; 14+ messages in thread
From: Jan Kiszka @ 2026-11-17 18:32 UTC (permalink / raw)
  To: Tobias Schaffner, xenomai; +Cc: rpm

On 17.11.26 19:15, Tobias Schaffner wrote:
> On 11/17/26 18:15, Jan Kiszka wrote:
>> On 17.11.26 13:39, Tobias Schaffner wrote:
>>> Hi all,
>>>
>>> this series fixes the riscv trap handling of Dovetail.
>>>
>>> Thanks to Jan for sharing his AI reviews on this which made me have
>>> a look at this.
>>>
>>> The bugs were verified by running a trap matrix test, which raises
>>> every kind of trap from an out-of-band EVL thread, with a more detailed
>>> oob_trap RV monitor from the Dovetail RV RFC enabled.
>>>
>>> Testing was done on v7.3 with EVL under QEMU riscv64: the trap matrix
>>> test, the libevl suite and kprobe tests hitting out-of-band syscalls,
>>> IRQ handlers and nested faults all pass with a clean kernel log and no
>>> monitor reaction. Reverting the fixes one at a time reproduces each
>>> bug except the last one.
>>
>> Did you also ask the AI to review you patches? I may come back with "all
>> fine, but I found 5 other bugs" :). Still, this can be worth it.
> 
> Hi Jan,
> 
> yes that's part of my workflow. I am doing some back and forth most of
> the time. In this case I had reproducers for all the patches and
> reviewed and tested them one by one.
> 
> It is not very special but this is the definition of my review agent:
> 
> ---
> name: Review Agent
> description: A strict, systems-level code review agent. Use when you
> want blunt, technically rigorous feedback focused on correctness,
> determinism, simplicity, and performance.
> argument-hint: The code or diff to review.
> tools: ['read', 'search']
> ---
> 
> You are a senior systems engineer performing a strict technical code
> review.
> 
> ## Personality and Style
> 
> - Direct, precise, and technically grounded.
> - Blunt but professional.
> - No motivational fluff.
> - No emojis.
> - No softening language.
> - Prefer correctness over style.
> - Prefer simplicity over abstraction.
> - Prefer determinism over convenience.
> - Performance-aware.
> - Security-aware.
> - Skeptical of overengineering.
> 
> You have low tolerance for:
> 
> - Unnecessary abstraction
> - Hidden control flow
> - Magic behavior
> - Excessive dependencies
> - Premature generalization
> - Non-deterministic behavior without justification
> 
> ---
> 
> ## Review Rules
> 
> 1. Identify correctness issues first.
> 2. Flag undefined behavior, race conditions, edge cases.
> 3. Call out hidden complexity explicitly.
> 4. Prefer explicit control flow.
> 5. Question unnecessary layers of abstraction.
> 6. Highlight performance and memory implications.
> 7. Scrutinize concurrency carefully.
> 8. Reject premature abstraction.
> 9. Do not rewrite the entire code unless fundamentally broken.
> 10. Provide concrete, actionable corrections.
> 
> ---
> 
> ## Output Format (Always Use This Structure)
> 
> ### Critical Issues
> - Correctness bugs, race conditions, undefined behavior, security risks.
> 
> ### Design Problems
> - Architectural flaws, unnecessary abstraction, layering violations.
> 
> ### Minor Issues
> - Style issues, naming, clarity improvements.
> 
> ### What Is Actually Good
> - Brief acknowledgment of solid engineering decisions.
> 
> ---
> 
> ## Tone Examples
> 
> Instead of:
> "Consider simplifying this abstraction."
> 
> Say:
> "This abstraction adds no value and hides control flow. Remove it."
> 
> Instead of:
> "Error handling could be improved."
> 
> Say:
> "Error handling is incomplete. This will fail silently under load."
> 
> ---
> 
> ## Default Assumptions
> 
> - Code is production-bound.
> - Bugs are expensive.
> - Simplicity wins.
> - Explicit beats clever.
> - Deterministic behavior is required.
> 

Interesting to read - just not needed in my experiments so far. Maybe
OpenCode has something of that built-in, may Opus/Fable are now smart
enough to do The Right Thing automatically, most of the time.

What was a bit annoying so far is that some findings did not come
immediately but only after multiple iterations, once unrelated
improvements should be re-reviewed.

Jan

-- 
Siemens AG, Foundational Technologies
Linux Expert Center

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH dovetail 0/5] riscv: dovetail: trap handling fixes
  2026-11-17 12:39 [PATCH dovetail 0/5] riscv: dovetail: trap handling fixes Tobias Schaffner
                   ` (5 preceding siblings ...)
  2026-11-17 17:15 ` [PATCH dovetail 0/5] riscv: dovetail: trap handling fixes Jan Kiszka
@ 2026-11-17 19:16 ` Florian Bezdeka
  6 siblings, 0 replies; 14+ messages in thread
From: Florian Bezdeka @ 2026-11-17 19:16 UTC (permalink / raw)
  To: Tobias Schaffner, xenomai; +Cc: rpm, jan.kiszka

On Tue, 2026-11-17 at 13:39 +0100, Tobias Schaffner wrote:
> Hi all,
> 
> this series fixes the riscv trap handling of Dovetail.
> 
> Thanks to Jan for sharing his AI reviews on this which made me have
> a look at this.
> 
> The bugs were verified by running a trap matrix test, which raises
> every kind of trap from an out-of-band EVL thread, with a more detailed
> oob_trap RV monitor from the Dovetail RV RFC enabled.

Where are those tests?

The problem with that approach (fixes first, tests later) is quite
simple: Once the fixes are in, interest in automating the necessary
testsuite is gone.

Solution: Tests have to arrive at the same time.

In case they were on the list already: Please add a reference here.

> 
> Testing was done on v7.3 with EVL under QEMU riscv64: the trap matrix
> test, the libevl suite and kprobe tests hitting out-of-band syscalls,
> IRQ handlers and nested faults all pass with a clean kernel log and no
> monitor reaction. Reverting the fixes one at a time reproduces each
> bug except the last one.
> 
> Thanks,
> Tobias
> 
> Tobias Schaffner (5):
>   riscv: dovetail: do not notify traps again from do_trap()
>   riscv: dovetail: run kernel trap handlers the core did not demote for
>   riscv: dovetail: do not nest the unknown trap handler in software
>     checks
>   riscv: dovetail: keep hard irqs off for page faults handled
>     out-of-band
>   riscv: dovetail: read the CFI trap value from the saved registers
> 
>  arch/riscv/kernel/traps.c | 60 +++++++++++++++++++++------------------
>  1 file changed, 32 insertions(+), 28 deletions(-)
> 
> 
> base-commit: de6ccdbc1027fda48257c4033c90d57da3e676ea
> -- 
> 2.53.0

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH dovetail 4/5] riscv: dovetail: keep hard irqs off for page faults handled out-of-band
  2026-11-17 12:39 ` [PATCH dovetail 4/5] riscv: dovetail: keep hard irqs off for page faults handled out-of-band Tobias Schaffner
@ 2026-11-17 19:24   ` Florian Bezdeka
  2026-11-17 21:12     ` Bezdeka, Florian
  0 siblings, 1 reply; 14+ messages in thread
From: Florian Bezdeka @ 2026-11-17 19:24 UTC (permalink / raw)
  To: Tobias Schaffner, xenomai; +Cc: rpm, jan.kiszka

On Tue, 2026-11-17 at 13:39 +0100, Tobias Schaffner wrote:
> do_page_fault() enables hard irqs for kernel mode faults whether the
> companion core demoted the current context or not. A kernel mode fault
> the core leaves out-of-band, e.g. a faulting kprobe fetch argument within
> a kernel trap, then runs the fault handler over the out-of-band stage
> with hard irqs on, where an out-of-band interrupt may reschedule in the
> middle of the outer trap. With CONFIG_DEBUG_DOVETAIL, the BUG_ON()
> checking for a demotion fires instead.
> 
> Toggle hard irqs and the in-band stall only if the core demoted us, and
> keep the debug check for user mode faults.
> 
> Fixes: 1d1fdeccecef ("riscv: dovetail: add core support")
> Signed-off-by: Tobias Schaffner <tobias.schaffner@siemens.com>
> ---
>  arch/riscv/kernel/traps.c | 17 +++++++++++------
>  1 file changed, 11 insertions(+), 6 deletions(-)
> 
> diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c
> index dddb30711440..9f8dc6bd7b85 100644
> --- a/arch/riscv/kernel/traps.c
> +++ b/arch/riscv/kernel/traps.c
> @@ -489,21 +489,26 @@ asmlinkage __visible __trap_section void do_trap_software_check(struct pt_regs *
>  asmlinkage __visible noinstr void do_page_fault(struct pt_regs *regs)
>  {
>  	irqentry_state_t state = irqentry_enter(regs);
> +	bool inband = mark_trap_entry(regs);
>  
> -	mark_trap_entry(regs);
> -
> -	BUG_ON(dovetail_debug() && !running_inband());
> +	/*
> +	 * Dovetail: a kernel mode fault the core did not demote for, e.g.
> +	 * a faulting kprobe fetch within a kernel trap, is handled over
> +	 * the out-of-band stage with hard irqs off.
> +	 */
> +	BUG_ON(dovetail_debug() && !inband && user_mode(regs));
>  
> -	if (!user_mode(regs))
> +	if (!user_mode(regs) && inband)
>  		hard_cond_local_irq_enable();

Handle with care, just a 30 sec scan:

That one looks strange. !user_mode() means fault over kernel code. But
what if the hard IRQs are disabled in the parent context?

We can only (unconditionally) enable/disable (hard) IRQs over user
context.

Seems that was already wrong during first riscv integration. Or: Wrong
API? _save() _restore()?

>  
>  	handle_page_fault(regs, state);
>  
> -	local_irq_disable();
> +	if (inband)
> +		local_irq_disable();

That was clearly a bug.

>  
>  	mark_trap_exit(regs);
>  
> -	if (!user_mode(regs))
> +	if (!user_mode(regs) && inband)
>  		hard_cond_local_irq_disable();

Most likely the same follow up problem.

>  
>  	irqentry_exit(regs, state);
> -- 
> 2.53.0

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH dovetail 3/5] riscv: dovetail: do not nest the unknown trap handler in software checks
  2026-11-17 12:39 ` [PATCH dovetail 3/5] riscv: dovetail: do not nest the unknown trap handler in software checks Tobias Schaffner
@ 2026-11-17 20:38   ` Florian Bezdeka
  2026-11-18  6:11     ` Jan Kiszka
  0 siblings, 1 reply; 14+ messages in thread
From: Florian Bezdeka @ 2026-11-17 20:38 UTC (permalink / raw)
  To: Tobias Schaffner, xenomai; +Cc: rpm, jan.kiszka

On Tue, 2026-11-17 at 13:39 +0100, Tobias Schaffner wrote:
> do_trap_software_check() hands a user mode trap which is no CFI
> violation to the whole do_trap_unknown() handler, which enters from
> user mode and notifies the companion core again on the same frame.
> The nested mark_trap_exit() unwinds the trap before the outer handler
> is done.

If I get this right it's about nesting of those traps, or
trap_entry/exit() calls.

That problem should be solved by the series posted by Jan.

But: We shouldn't inform the co-kernel twice for the same problem. I
guess we don't once the nesting is probably solved. Isn't it?

> 
> Call do_trap_error() as do_trap_unknown() would.
> 
> Fixes: 1d1fdeccecef ("riscv: dovetail: add core support")
> Signed-off-by: Tobias Schaffner <tobias.schaffner@siemens.com>
> ---
>  arch/riscv/kernel/traps.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c
> index a8e37ec27cc2..dddb30711440 100644
> --- a/arch/riscv/kernel/traps.c
> +++ b/arch/riscv/kernel/traps.c
> @@ -473,7 +473,8 @@ asmlinkage __visible __trap_section void do_trap_software_check(struct pt_regs *
>  		if (mark_trap_entry(regs)) {
>  			/* not a cfi violation, then merge into flow of unknown trap handler */
>  			if (!handle_user_cfi_violation(regs))
> -				do_trap_unknown(regs);
> +				do_trap_error(regs, SIGILL, ILL_ILLTRP, regs->epc,
> +					      "Oops - unknown exception");
>  
>  			mark_trap_exit(regs);
>  		}
> -- 
> 2.53.0

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH dovetail 4/5] riscv: dovetail: keep hard irqs off for page faults handled out-of-band
  2026-11-17 19:24   ` Florian Bezdeka
@ 2026-11-17 21:12     ` Bezdeka, Florian
  0 siblings, 0 replies; 14+ messages in thread
From: Bezdeka, Florian @ 2026-11-17 21:12 UTC (permalink / raw)
  To: Schaffner, Tobias, xenomai@lists.linux.dev; +Cc: rpm@xenomai.org, Kiszka, Jan

On Tue, 2026-11-17 at 20:24 +0100, Florian Bezdeka wrote:
> On Tue, 2026-11-17 at 13:39 +0100, Tobias Schaffner wrote:
> > do_page_fault() enables hard irqs for kernel mode faults whether the
> > companion core demoted the current context or not. A kernel mode fault
> > the core leaves out-of-band, e.g. a faulting kprobe fetch argument within
> > a kernel trap, then runs the fault handler over the out-of-band stage
> > with hard irqs on, where an out-of-band interrupt may reschedule in the
> > middle of the outer trap. With CONFIG_DEBUG_DOVETAIL, the BUG_ON()
> > checking for a demotion fires instead.
> > 
> > Toggle hard irqs and the in-band stall only if the core demoted us, and
> > keep the debug check for user mode faults.
> > 
> > Fixes: 1d1fdeccecef ("riscv: dovetail: add core support")
> > Signed-off-by: Tobias Schaffner <tobias.schaffner@siemens.com>
> > ---
> >  arch/riscv/kernel/traps.c | 17 +++++++++++------
> >  1 file changed, 11 insertions(+), 6 deletions(-)
> > 
> > diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c
> > index dddb30711440..9f8dc6bd7b85 100644
> > --- a/arch/riscv/kernel/traps.c
> > +++ b/arch/riscv/kernel/traps.c
> > @@ -489,21 +489,26 @@ asmlinkage __visible __trap_section void do_trap_software_check(struct pt_regs *
> >  asmlinkage __visible noinstr void do_page_fault(struct pt_regs *regs)
> >  {
> >  	irqentry_state_t state = irqentry_enter(regs);
> > +	bool inband = mark_trap_entry(regs);
> >  
> > -	mark_trap_entry(regs);
> > -
> > -	BUG_ON(dovetail_debug() && !running_inband());
> > +	/*
> > +	 * Dovetail: a kernel mode fault the core did not demote for, e.g.
> > +	 * a faulting kprobe fetch within a kernel trap, is handled over
> > +	 * the out-of-band stage with hard irqs off.
> > +	 */
> > +	BUG_ON(dovetail_debug() && !inband && user_mode(regs));
> >  
> > -	if (!user_mode(regs))
> > +	if (!user_mode(regs) && inband)
> >  		hard_cond_local_irq_enable();
> 
> Handle with care, just a 30 sec scan:
> 
> That one looks strange. !user_mode() means fault over kernel code. But
> what if the hard IRQs are disabled in the parent context?
> 
> We can only (unconditionally) enable/disable (hard) IRQs over user
> context.
> 
> Seems that was already wrong during first riscv integration. Or: Wrong
> API? _save() _restore()?

Even more confused on a second glance: That is already handled inside
mark_trap_entry(), just without the !user_mode() check, which makes this
a duplicate - for the !user_mode() case. 

> 
> >  
> >  	handle_page_fault(regs, state);
> >  
> > -	local_irq_disable();
> > +	if (inband)
> > +		local_irq_disable();
> 
> That was clearly a bug.
> 
> >  
> >  	mark_trap_exit(regs);
> >  
> > -	if (!user_mode(regs))
> > +	if (!user_mode(regs) && inband)
> >  		hard_cond_local_irq_disable();
> 
> Most likely the same follow up problem.
> 
> >  
> >  	irqentry_exit(regs, state);
> > -- 
> > 2.53.0

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH dovetail 3/5] riscv: dovetail: do not nest the unknown trap handler in software checks
  2026-11-17 20:38   ` Florian Bezdeka
@ 2026-11-18  6:11     ` Jan Kiszka
  0 siblings, 0 replies; 14+ messages in thread
From: Jan Kiszka @ 2026-11-18  6:11 UTC (permalink / raw)
  To: Florian Bezdeka, Tobias Schaffner, xenomai; +Cc: rpm

On 17.11.26 21:38, Florian Bezdeka wrote:
> On Tue, 2026-11-17 at 13:39 +0100, Tobias Schaffner wrote:
>> do_trap_software_check() hands a user mode trap which is no CFI
>> violation to the whole do_trap_unknown() handler, which enters from
>> user mode and notifies the companion core again on the same frame.
>> The nested mark_trap_exit() unwinds the trap before the outer handler
>> is done.
> 
> If I get this right it's about nesting of those traps, or
> trap_entry/exit() calls.
> 
> That problem should be solved by the series posted by Jan.

Exactly, no need to address that at arch-level:
https://lore.kernel.org/xenomai/d40c9a22e1e450a83533ef7d781f88010c2aacc6.1794399047.git.jan.kiszka@siemens.com/

Jan

> 
> But: We shouldn't inform the co-kernel twice for the same problem. I
> guess we don't once the nesting is probably solved. Isn't it?
> 
>>
>> Call do_trap_error() as do_trap_unknown() would.
>>
>> Fixes: 1d1fdeccecef ("riscv: dovetail: add core support")
>> Signed-off-by: Tobias Schaffner <tobias.schaffner@siemens.com>
>> ---
>>  arch/riscv/kernel/traps.c | 3 ++-
>>  1 file changed, 2 insertions(+), 1 deletion(-)
>>
>> diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c
>> index a8e37ec27cc2..dddb30711440 100644
>> --- a/arch/riscv/kernel/traps.c
>> +++ b/arch/riscv/kernel/traps.c
>> @@ -473,7 +473,8 @@ asmlinkage __visible __trap_section void do_trap_software_check(struct pt_regs *
>>  		if (mark_trap_entry(regs)) {
>>  			/* not a cfi violation, then merge into flow of unknown trap handler */
>>  			if (!handle_user_cfi_violation(regs))
>> -				do_trap_unknown(regs);
>> +				do_trap_error(regs, SIGILL, ILL_ILLTRP, regs->epc,
>> +					      "Oops - unknown exception");
>>  
>>  			mark_trap_exit(regs);
>>  		}
>> -- 
>> 2.53.0

-- 
Siemens AG, Foundational Technologies
Linux Expert Center

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2026-11-18  6:11 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17 12:39 [PATCH dovetail 0/5] riscv: dovetail: trap handling fixes Tobias Schaffner
2026-11-17 12:39 ` [PATCH dovetail 1/5] riscv: dovetail: do not notify traps again from do_trap() Tobias Schaffner
2026-11-17 12:39 ` [PATCH dovetail 2/5] riscv: dovetail: run kernel trap handlers the core did not demote for Tobias Schaffner
2026-11-17 12:39 ` [PATCH dovetail 3/5] riscv: dovetail: do not nest the unknown trap handler in software checks Tobias Schaffner
2026-11-17 20:38   ` Florian Bezdeka
2026-11-18  6:11     ` Jan Kiszka
2026-11-17 12:39 ` [PATCH dovetail 4/5] riscv: dovetail: keep hard irqs off for page faults handled out-of-band Tobias Schaffner
2026-11-17 19:24   ` Florian Bezdeka
2026-11-17 21:12     ` Bezdeka, Florian
2026-11-17 12:39 ` [PATCH dovetail 5/5] riscv: dovetail: read the CFI trap value from the saved registers Tobias Schaffner
2026-11-17 17:15 ` [PATCH dovetail 0/5] riscv: dovetail: trap handling fixes Jan Kiszka
2026-11-17 18:15   ` Tobias Schaffner
2026-11-17 18:32     ` Jan Kiszka
2026-11-17 19:16 ` Florian Bezdeka

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.