linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] riscv: Enable interrupt during exception handling
@ 2025-06-20 11:43 Nam Cao
  2025-06-23 22:17 ` Palmer Dabbelt
  0 siblings, 1 reply; 13+ messages in thread
From: Nam Cao @ 2025-06-20 11:43 UTC (permalink / raw)
  To: Paul Walmsley, Palmer Dabbelt, Albert Ou, Alexandre Ghiti,
	Sebastian Andrzej Siewior, Clark Williams, Steven Rostedt,
	linux-rt-devel, linux-riscv, linux-kernel
  Cc: Nam Cao

force_sig_fault() takes a spinlock, which is a sleeping lock with
CONFIG_PREEMPT_RT=y. However, exception handling calls force_sig_fault()
with interrupt disabled, causing a sleeping in atomic context warning.

This can be reproduced using userspace programs such as:
    int main() { asm ("ebreak"); }
or
    int main() { asm ("unimp"); }

There is no reason that interrupt must be disabled during exception
handling. Considering the previous struggle with a similar bug [1][2], fix
this problem once for all by enabling interrupt during exception handling
whenever possible:
  - If exception comes from user (interrupt handling was for sure enabled)
  - If exception comes from kernel and interrupt handling was enabled

This also has the added benefit of avoiding unnecessary delays in interrupt
handling.

This patch mimics x86's implementation.

Link: https://lore.kernel.org/linux-riscv/20250411073850.3699180-3-nylon.chen@sifive.com [1]
Link: https://lore.kernel.org/linux-riscv/20250422162324.956065-3-cleger@rivosinc.com [2]
Suggested-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Signed-off-by: Nam Cao <namcao@linutronix.de>
---
 arch/riscv/kernel/traps.c | 36 ++++++++++++++++++++++++++++++------
 arch/riscv/mm/fault.c     |  4 ----
 2 files changed, 30 insertions(+), 10 deletions(-)

diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c
index 9c83848797a78..f7d2372dc0eb6 100644
--- a/arch/riscv/kernel/traps.c
+++ b/arch/riscv/kernel/traps.c
@@ -6,6 +6,7 @@
 #include <linux/cpu.h>
 #include <linux/kernel.h>
 #include <linux/init.h>
+#include <linux/irqflags.h>
 #include <linux/randomize_kstack.h>
 #include <linux/sched.h>
 #include <linux/sched/debug.h>
@@ -72,6 +73,18 @@ static void dump_instr(const char *loglvl, struct pt_regs *regs)
 	printk("%sCode: %s\n", loglvl, str);
 }
 
+static void cond_local_irq_enable(struct pt_regs *regs)
+{
+	if (!regs_irqs_disabled(regs))
+		local_irq_enable();
+}
+
+static void cond_local_irq_disable(struct pt_regs *regs)
+{
+	if (!regs_irqs_disabled(regs))
+		local_irq_disable();
+}
+
 void die(struct pt_regs *regs, const char *str)
 {
 	static int die_counter;
@@ -151,11 +164,15 @@ asmlinkage __visible __trap_section void name(struct pt_regs *regs)		\
 {										\
 	if (user_mode(regs)) {							\
 		irqentry_enter_from_user_mode(regs);				\
+		local_irq_enable();						\
 		do_trap_error(regs, signo, code, regs->epc, "Oops - " str);	\
+		local_irq_disable();						\
 		irqentry_exit_to_user_mode(regs);				\
 	} else {								\
 		irqentry_state_t state = irqentry_nmi_enter(regs);		\
+		cond_local_irq_enable(regs);					\
 		do_trap_error(regs, signo, code, regs->epc, "Oops - " str);	\
+		cond_local_irq_disable(regs);					\
 		irqentry_nmi_exit(regs, state);					\
 	}									\
 }
@@ -173,24 +190,23 @@ asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *re
 
 	if (user_mode(regs)) {
 		irqentry_enter_from_user_mode(regs);
-
 		local_irq_enable();
 
 		handled = riscv_v_first_use_handler(regs);
-
-		local_irq_disable();
-
 		if (!handled)
 			do_trap_error(regs, SIGILL, ILL_ILLOPC, regs->epc,
 				      "Oops - illegal instruction");
 
+		local_irq_disable();
 		irqentry_exit_to_user_mode(regs);
 	} else {
 		irqentry_state_t state = irqentry_nmi_enter(regs);
+		cond_local_irq_enable(regs);
 
 		do_trap_error(regs, SIGILL, ILL_ILLOPC, regs->epc,
 			      "Oops - illegal instruction");
 
+		cond_local_irq_disable(regs);
 		irqentry_nmi_exit(regs, state);
 	}
 }
@@ -225,6 +241,7 @@ static void do_trap_misaligned(struct pt_regs *regs, enum misaligned_access_type
 		local_irq_enable();
 	} else {
 		state = irqentry_nmi_enter(regs);
+		cond_local_irq_enable(regs);
 	}
 
 	if (misaligned_handler[type].handler(regs))
@@ -235,6 +252,7 @@ static void do_trap_misaligned(struct pt_regs *regs, enum misaligned_access_type
 		local_irq_disable();
 		irqentry_exit_to_user_mode(regs);
 	} else {
+		cond_local_irq_disable(regs);
 		irqentry_nmi_exit(regs, state);
 	}
 }
@@ -308,15 +326,19 @@ asmlinkage __visible __trap_section void do_trap_break(struct pt_regs *regs)
 {
 	if (user_mode(regs)) {
 		irqentry_enter_from_user_mode(regs);
+		local_irq_enable();
 
 		handle_break(regs);
 
+		local_irq_disable();
 		irqentry_exit_to_user_mode(regs);
 	} else {
 		irqentry_state_t state = irqentry_nmi_enter(regs);
+		cond_local_irq_enable(regs);
 
 		handle_break(regs);
 
+		cond_local_irq_disable(regs);
 		irqentry_nmi_exit(regs, state);
 	}
 }
@@ -355,10 +377,12 @@ void do_trap_ecall_u(struct pt_regs *regs)
 		syscall_exit_to_user_mode(regs);
 	} else {
 		irqentry_state_t state = irqentry_nmi_enter(regs);
+		cond_local_irq_enable(regs);
 
 		do_trap_error(regs, SIGILL, ILL_ILLTRP, regs->epc,
 			"Oops - environment call from U-mode");
 
+		cond_local_irq_disable(regs);
 		irqentry_nmi_exit(regs, state);
 	}
 
@@ -368,11 +392,11 @@ void do_trap_ecall_u(struct pt_regs *regs)
 asmlinkage __visible noinstr void do_page_fault(struct pt_regs *regs)
 {
 	irqentry_state_t state = irqentry_enter(regs);
+	cond_local_irq_enable(regs);
 
 	handle_page_fault(regs);
 
-	local_irq_disable();
-
+	cond_local_irq_disable(regs);
 	irqentry_exit(regs, state);
 }
 #endif
diff --git a/arch/riscv/mm/fault.c b/arch/riscv/mm/fault.c
index 0194324a0c506..6d23ed0ce8a28 100644
--- a/arch/riscv/mm/fault.c
+++ b/arch/riscv/mm/fault.c
@@ -306,10 +306,6 @@ void handle_page_fault(struct pt_regs *regs)
 		return;
 	}
 
-	/* Enable interrupts if they were enabled in the parent context. */
-	if (!regs_irqs_disabled(regs))
-		local_irq_enable();
-
 	/*
 	 * If we're in an interrupt, have no user context, or are running
 	 * in an atomic region, then we must not take the fault.
-- 
2.39.5


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

* Re: [PATCH] riscv: Enable interrupt during exception handling
  2025-06-20 11:43 [PATCH] riscv: Enable interrupt during exception handling Nam Cao
@ 2025-06-23 22:17 ` Palmer Dabbelt
  2025-06-24  2:09   ` Maciej W. Rozycki
  0 siblings, 1 reply; 13+ messages in thread
From: Palmer Dabbelt @ 2025-06-23 22:17 UTC (permalink / raw)
  To: namcao
  Cc: Paul Walmsley, aou, Alexandre Ghiti, bigeasy, clrkwllms, rostedt,
	linux-rt-devel, linux-riscv, linux-kernel, namcao

On Fri, 20 Jun 2025 04:43:46 PDT (-0700), namcao@linutronix.de wrote:
> force_sig_fault() takes a spinlock, which is a sleeping lock with
> CONFIG_PREEMPT_RT=y. However, exception handling calls force_sig_fault()
> with interrupt disabled, causing a sleeping in atomic context warning.
>
> This can be reproduced using userspace programs such as:
>     int main() { asm ("ebreak"); }
> or
>     int main() { asm ("unimp"); }
>
> There is no reason that interrupt must be disabled during exception
> handling.

Looks like they used to and we lost it as of f0bddf50586d ("riscv: 
entry: Convert to generic entry"), where we'd check the PIE bit and 
quickly re-enable interrupts if they were enabled before.

This way of doing it also seems fine, and it's kind of nice that we can 
just read the code to figure out how the interrupt contexts are managed.  
So I think it's fine to keep it this way, even though it's more code.

I'm kind of split on a Fixes tag here.  One could argue it's a 
regression, as having interrupts disabled during exceptions is going to 
cause all sorts of performance issues for users.  Seems a bit risk to 
just backport, though...

That said, if nobody noticed then it's probably a good sign nobody is 
really paying attention and we should just backport it before anyone 
notices...

> Considering the previous struggle with a similar bug [1][2], fix
> this problem once for all by enabling interrupt during exception handling
> whenever possible:
>   - If exception comes from user (interrupt handling was for sure enabled)
>   - If exception comes from kernel and interrupt handling was enabled
>
> This also has the added benefit of avoiding unnecessary delays in interrupt
> handling.
>
> This patch mimics x86's implementation.
>
> Link: https://lore.kernel.org/linux-riscv/20250411073850.3699180-3-nylon.chen@sifive.com [1]
> Link: https://lore.kernel.org/linux-riscv/20250422162324.956065-3-cleger@rivosinc.com [2]
> Suggested-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
> Signed-off-by: Nam Cao <namcao@linutronix.de>
> ---
>  arch/riscv/kernel/traps.c | 36 ++++++++++++++++++++++++++++++------
>  arch/riscv/mm/fault.c     |  4 ----
>  2 files changed, 30 insertions(+), 10 deletions(-)
>
> diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c
> index 9c83848797a78..f7d2372dc0eb6 100644
> --- a/arch/riscv/kernel/traps.c
> +++ b/arch/riscv/kernel/traps.c
> @@ -6,6 +6,7 @@
>  #include <linux/cpu.h>
>  #include <linux/kernel.h>
>  #include <linux/init.h>
> +#include <linux/irqflags.h>
>  #include <linux/randomize_kstack.h>
>  #include <linux/sched.h>
>  #include <linux/sched/debug.h>
> @@ -72,6 +73,18 @@ static void dump_instr(const char *loglvl, struct pt_regs *regs)
>  	printk("%sCode: %s\n", loglvl, str);
>  }
>  
> +static void cond_local_irq_enable(struct pt_regs *regs)
> +{
> +	if (!regs_irqs_disabled(regs))
> +		local_irq_enable();
> +}
> +
> +static void cond_local_irq_disable(struct pt_regs *regs)
> +{
> +	if (!regs_irqs_disabled(regs))
> +		local_irq_disable();
> +}
> +
>  void die(struct pt_regs *regs, const char *str)
>  {
>  	static int die_counter;
> @@ -151,11 +164,15 @@ asmlinkage __visible __trap_section void name(struct pt_regs *regs)		\
>  {										\
>  	if (user_mode(regs)) {							\
>  		irqentry_enter_from_user_mode(regs);				\
> +		local_irq_enable();						\
>  		do_trap_error(regs, signo, code, regs->epc, "Oops - " str);	\
> +		local_irq_disable();						\
>  		irqentry_exit_to_user_mode(regs);				\
>  	} else {								\
>  		irqentry_state_t state = irqentry_nmi_enter(regs);		\
> +		cond_local_irq_enable(regs);					\
>  		do_trap_error(regs, signo, code, regs->epc, "Oops - " str);	\
> +		cond_local_irq_disable(regs);					\
>  		irqentry_nmi_exit(regs, state);					\
>  	}									\
>  }
> @@ -173,24 +190,23 @@ asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *re
>  
>  	if (user_mode(regs)) {
>  		irqentry_enter_from_user_mode(regs);
> -
>  		local_irq_enable();
>  
>  		handled = riscv_v_first_use_handler(regs);
> -
> -		local_irq_disable();
> -
>  		if (!handled)
>  			do_trap_error(regs, SIGILL, ILL_ILLOPC, regs->epc,
>  				      "Oops - illegal instruction");
>  
> +		local_irq_disable();
>  		irqentry_exit_to_user_mode(regs);
>  	} else {
>  		irqentry_state_t state = irqentry_nmi_enter(regs);
> +		cond_local_irq_enable(regs);
>  
>  		do_trap_error(regs, SIGILL, ILL_ILLOPC, regs->epc,
>  			      "Oops - illegal instruction");
>  
> +		cond_local_irq_disable(regs);
>  		irqentry_nmi_exit(regs, state);
>  	}
>  }
> @@ -225,6 +241,7 @@ static void do_trap_misaligned(struct pt_regs *regs, enum misaligned_access_type
>  		local_irq_enable();
>  	} else {
>  		state = irqentry_nmi_enter(regs);
> +		cond_local_irq_enable(regs);
>  	}
>  
>  	if (misaligned_handler[type].handler(regs))
> @@ -235,6 +252,7 @@ static void do_trap_misaligned(struct pt_regs *regs, enum misaligned_access_type
>  		local_irq_disable();
>  		irqentry_exit_to_user_mode(regs);
>  	} else {
> +		cond_local_irq_disable(regs);
>  		irqentry_nmi_exit(regs, state);
>  	}
>  }
> @@ -308,15 +326,19 @@ asmlinkage __visible __trap_section void do_trap_break(struct pt_regs *regs)
>  {
>  	if (user_mode(regs)) {
>  		irqentry_enter_from_user_mode(regs);
> +		local_irq_enable();
>  
>  		handle_break(regs);
>  
> +		local_irq_disable();
>  		irqentry_exit_to_user_mode(regs);
>  	} else {
>  		irqentry_state_t state = irqentry_nmi_enter(regs);
> +		cond_local_irq_enable(regs);
>  
>  		handle_break(regs);
>  
> +		cond_local_irq_disable(regs);
>  		irqentry_nmi_exit(regs, state);
>  	}
>  }
> @@ -355,10 +377,12 @@ void do_trap_ecall_u(struct pt_regs *regs)
>  		syscall_exit_to_user_mode(regs);
>  	} else {
>  		irqentry_state_t state = irqentry_nmi_enter(regs);
> +		cond_local_irq_enable(regs);
>  
>  		do_trap_error(regs, SIGILL, ILL_ILLTRP, regs->epc,
>  			"Oops - environment call from U-mode");
>  
> +		cond_local_irq_disable(regs);
>  		irqentry_nmi_exit(regs, state);
>  	}
>  
> @@ -368,11 +392,11 @@ void do_trap_ecall_u(struct pt_regs *regs)
>  asmlinkage __visible noinstr void do_page_fault(struct pt_regs *regs)
>  {
>  	irqentry_state_t state = irqentry_enter(regs);
> +	cond_local_irq_enable(regs);
>  
>  	handle_page_fault(regs);
>  
> -	local_irq_disable();
> -
> +	cond_local_irq_disable(regs);
>  	irqentry_exit(regs, state);
>  }
>  #endif
> diff --git a/arch/riscv/mm/fault.c b/arch/riscv/mm/fault.c
> index 0194324a0c506..6d23ed0ce8a28 100644
> --- a/arch/riscv/mm/fault.c
> +++ b/arch/riscv/mm/fault.c
> @@ -306,10 +306,6 @@ void handle_page_fault(struct pt_regs *regs)
>  		return;
>  	}
>  
> -	/* Enable interrupts if they were enabled in the parent context. */
> -	if (!regs_irqs_disabled(regs))
> -		local_irq_enable();
> -
>  	/*
>  	 * If we're in an interrupt, have no user context, or are running
>  	 * in an atomic region, then we must not take the fault.
> -- 
> 2.39.5

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

* Re: [PATCH] riscv: Enable interrupt during exception handling
  2025-06-23 22:17 ` Palmer Dabbelt
@ 2025-06-24  2:09   ` Maciej W. Rozycki
  2025-06-24 11:37     ` Clément Léger
  0 siblings, 1 reply; 13+ messages in thread
From: Maciej W. Rozycki @ 2025-06-24  2:09 UTC (permalink / raw)
  To: Palmer Dabbelt
  Cc: namcao, Paul Walmsley, aou, Alexandre Ghiti, bigeasy, clrkwllms,
	rostedt, linux-rt-devel, linux-riscv, linux-kernel

On Mon, 23 Jun 2025, Palmer Dabbelt wrote:

> I'm kind of split on a Fixes tag here.  One could argue it's a regression, as
> having interrupts disabled during exceptions is going to cause all sorts of
> performance issues for users.  Seems a bit risk to just backport, though...
> 
> That said, if nobody noticed then it's probably a good sign nobody is really
> paying attention and we should just backport it before anyone notices...

 Oh, someone did notice and it's not only performance, cf. 
<https://lore.kernel.org/r/alpine.DEB.2.21.2501070143250.18889@angie.orcam.me.uk/>.

  Maciej

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

* Re: [PATCH] riscv: Enable interrupt during exception handling
  2025-06-24  2:09   ` Maciej W. Rozycki
@ 2025-06-24 11:37     ` Clément Léger
  2025-06-24 13:17       ` Nam Cao
  0 siblings, 1 reply; 13+ messages in thread
From: Clément Léger @ 2025-06-24 11:37 UTC (permalink / raw)
  To: Maciej W. Rozycki, Palmer Dabbelt
  Cc: namcao, Paul Walmsley, aou, Alexandre Ghiti, bigeasy, clrkwllms,
	rostedt, linux-rt-devel, linux-riscv, linux-kernel



On 24/06/2025 04:09, Maciej W. Rozycki wrote:
> On Mon, 23 Jun 2025, Palmer Dabbelt wrote:
> 
>> I'm kind of split on a Fixes tag here.  One could argue it's a regression, as
>> having interrupts disabled during exceptions is going to cause all sorts of
>> performance issues for users.  Seems a bit risk to just backport, though...
>>
>> That said, if nobody noticed then it's probably a good sign nobody is really
>> paying attention and we should just backport it before anyone notices...
> 
>  Oh, someone did notice and it's not only performance, cf. 
> <https://lore.kernel.org/r/alpine.DEB.2.21.2501070143250.18889@angie.orcam.me.uk/>.

I also had a series which was doing so for misaligned accesses handling,
but after discussion, it was not ok to do so.:

https://lore.kernel.org/linux-riscv/20250422094419.GC14170@noisy.programming.kicks-ass.net/

So this series should probably be modified to only reenable interrupts
when explicitly needed, ie page faults and for force_sig_fault().

Thanks,

Clément

> 
>   Maciej
> 
> _______________________________________________
> linux-riscv mailing list
> linux-riscv@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/linux-riscv


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

* Re: [PATCH] riscv: Enable interrupt during exception handling
  2025-06-24 11:37     ` Clément Léger
@ 2025-06-24 13:17       ` Nam Cao
  2025-06-24 13:48         ` Clément Léger
  0 siblings, 1 reply; 13+ messages in thread
From: Nam Cao @ 2025-06-24 13:17 UTC (permalink / raw)
  To: Clément Léger
  Cc: Maciej W. Rozycki, Palmer Dabbelt, Paul Walmsley, aou,
	Alexandre Ghiti, bigeasy, clrkwllms, rostedt, linux-rt-devel,
	linux-riscv, linux-kernel

On Tue, Jun 24, 2025 at 01:37:13PM +0200, Clément Léger wrote:
> On 24/06/2025 04:09, Maciej W. Rozycki wrote:
> > On Mon, 23 Jun 2025, Palmer Dabbelt wrote:
> >> I'm kind of split on a Fixes tag here.  One could argue it's a regression, as
> >> having interrupts disabled during exceptions is going to cause all sorts of
> >> performance issues for users.  Seems a bit risk to just backport, though...
> >>
> >> That said, if nobody noticed then it's probably a good sign nobody is really
> >> paying attention and we should just backport it before anyone notices...
> > 
> >  Oh, someone did notice and it's not only performance, cf. 
> > <https://lore.kernel.org/r/alpine.DEB.2.21.2501070143250.18889@angie.orcam.me.uk/>.
> 
> I also had a series which was doing so for misaligned accesses handling,
> but after discussion, it was not ok to do so.:
> 
> https://lore.kernel.org/linux-riscv/20250422094419.GC14170@noisy.programming.kicks-ass.net/

If I understand that right, exceptions from kernel should be treated as
NMI, so that lockdep can tell us if exception handlers touch locks.

But (conditionally) enabling interrupts does not lose us that benefit. It
is still considered NMI by lockdep.

Unless I miss something, the patch is fine as is.

Best regards,
Nam

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

* Re: [PATCH] riscv: Enable interrupt during exception handling
  2025-06-24 13:17       ` Nam Cao
@ 2025-06-24 13:48         ` Clément Léger
  2025-06-24 14:08           ` Peter Zijlstra
  0 siblings, 1 reply; 13+ messages in thread
From: Clément Léger @ 2025-06-24 13:48 UTC (permalink / raw)
  To: Nam Cao
  Cc: Maciej W. Rozycki, Palmer Dabbelt, Paul Walmsley, aou,
	Alexandre Ghiti, bigeasy, clrkwllms, rostedt, linux-rt-devel,
	linux-riscv, linux-kernel, Peter Zijlstra



On 24/06/2025 15:17, Nam Cao wrote:
> On Tue, Jun 24, 2025 at 01:37:13PM +0200, Clément Léger wrote:
>> On 24/06/2025 04:09, Maciej W. Rozycki wrote:
>>> On Mon, 23 Jun 2025, Palmer Dabbelt wrote:
>>>> I'm kind of split on a Fixes tag here.  One could argue it's a regression, as
>>>> having interrupts disabled during exceptions is going to cause all sorts of
>>>> performance issues for users.  Seems a bit risk to just backport, though...
>>>>
>>>> That said, if nobody noticed then it's probably a good sign nobody is really
>>>> paying attention and we should just backport it before anyone notices...
>>>
>>>  Oh, someone did notice and it's not only performance, cf. 
>>> <https://lore.kernel.org/r/alpine.DEB.2.21.2501070143250.18889@angie.orcam.me.uk/>.
>>
>> I also had a series which was doing so for misaligned accesses handling,
>> but after discussion, it was not ok to do so.:
>>
>> https://lore.kernel.org/linux-riscv/20250422094419.GC14170@noisy.programming.kicks-ass.net/
> 
> If I understand that right, exceptions from kernel should be treated as
> NMI, so that lockdep can tell us if exception handlers touch locks.
> 
> But (conditionally) enabling interrupts does not lose us that benefit. It
> is still considered NMI by lockdep.
> 
> Unless I miss something, the patch is fine as is.

Hi Nam,

Yeah indeed, providing that all traps handlers really are reentrant, I
think it's fine.

Thanks,

Clément

> 
> Best regards,
> Nam


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

* Re: [PATCH] riscv: Enable interrupt during exception handling
  2025-06-24 13:48         ` Clément Léger
@ 2025-06-24 14:08           ` Peter Zijlstra
  2025-06-24 14:11             ` Sebastian Andrzej Siewior
  0 siblings, 1 reply; 13+ messages in thread
From: Peter Zijlstra @ 2025-06-24 14:08 UTC (permalink / raw)
  To: Clément Léger
  Cc: Nam Cao, Maciej W. Rozycki, Palmer Dabbelt, Paul Walmsley, aou,
	Alexandre Ghiti, bigeasy, clrkwllms, rostedt, linux-rt-devel,
	linux-riscv, linux-kernel

On Tue, Jun 24, 2025 at 03:48:21PM +0200, Clément Léger wrote:
> 
> 
> On 24/06/2025 15:17, Nam Cao wrote:
> > On Tue, Jun 24, 2025 at 01:37:13PM +0200, Clément Léger wrote:
> >> On 24/06/2025 04:09, Maciej W. Rozycki wrote:
> >>> On Mon, 23 Jun 2025, Palmer Dabbelt wrote:
> >>>> I'm kind of split on a Fixes tag here.  One could argue it's a regression, as
> >>>> having interrupts disabled during exceptions is going to cause all sorts of
> >>>> performance issues for users.  Seems a bit risk to just backport, though...
> >>>>
> >>>> That said, if nobody noticed then it's probably a good sign nobody is really
> >>>> paying attention and we should just backport it before anyone notices...
> >>>
> >>>  Oh, someone did notice and it's not only performance, cf. 
> >>> <https://lore.kernel.org/r/alpine.DEB.2.21.2501070143250.18889@angie.orcam.me.uk/>.
> >>
> >> I also had a series which was doing so for misaligned accesses handling,
> >> but after discussion, it was not ok to do so.:
> >>
> >> https://lore.kernel.org/linux-riscv/20250422094419.GC14170@noisy.programming.kicks-ass.net/
> > 
> > If I understand that right, exceptions from kernel should be treated as
> > NMI, so that lockdep can tell us if exception handlers touch locks.
> > 
> > But (conditionally) enabling interrupts does not lose us that benefit. It
> > is still considered NMI by lockdep.
> > 
> > Unless I miss something, the patch is fine as is.

I'm confused, you're wanting to conditionally enable interrupts from a
kernel exception while its NMI like? *WHY* ?!


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

* Re: [PATCH] riscv: Enable interrupt during exception handling
  2025-06-24 14:08           ` Peter Zijlstra
@ 2025-06-24 14:11             ` Sebastian Andrzej Siewior
  2025-06-24 14:18               ` Peter Zijlstra
  0 siblings, 1 reply; 13+ messages in thread
From: Sebastian Andrzej Siewior @ 2025-06-24 14:11 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: Clément Léger, Nam Cao, Maciej W. Rozycki,
	Palmer Dabbelt, Paul Walmsley, aou, Alexandre Ghiti, clrkwllms,
	rostedt, linux-rt-devel, linux-riscv, linux-kernel

On 2025-06-24 16:08:15 [+0200], Peter Zijlstra wrote:
> > >>
> > >> I also had a series which was doing so for misaligned accesses handling,
> > >> but after discussion, it was not ok to do so.:
> > >>
> > >> https://lore.kernel.org/linux-riscv/20250422094419.GC14170@noisy.programming.kicks-ass.net/
> > > 
> > > If I understand that right, exceptions from kernel should be treated as
> > > NMI, so that lockdep can tell us if exception handlers touch locks.
> > > 
> > > But (conditionally) enabling interrupts does not lose us that benefit. It
> > > is still considered NMI by lockdep.
> > > 
> > > Unless I miss something, the patch is fine as is.
> 
> I'm confused, you're wanting to conditionally enable interrupts from a
> kernel exception while its NMI like? *WHY* ?!

What we want is to enable interrupt handling if it was enabled before
the exception occured. So we can send a proper signal on PREEMPT_RT
without chocking on spinlock_t/ sighand.

Sebastian

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

* Re: [PATCH] riscv: Enable interrupt during exception handling
  2025-06-24 14:11             ` Sebastian Andrzej Siewior
@ 2025-06-24 14:18               ` Peter Zijlstra
  2025-06-24 14:23                 ` Sebastian Andrzej Siewior
  0 siblings, 1 reply; 13+ messages in thread
From: Peter Zijlstra @ 2025-06-24 14:18 UTC (permalink / raw)
  To: Sebastian Andrzej Siewior
  Cc: Clément Léger, Nam Cao, Maciej W. Rozycki,
	Palmer Dabbelt, Paul Walmsley, aou, Alexandre Ghiti, clrkwllms,
	rostedt, linux-rt-devel, linux-riscv, linux-kernel

On Tue, Jun 24, 2025 at 04:11:30PM +0200, Sebastian Andrzej Siewior wrote:
> On 2025-06-24 16:08:15 [+0200], Peter Zijlstra wrote:
> > > >>
> > > >> I also had a series which was doing so for misaligned accesses handling,
> > > >> but after discussion, it was not ok to do so.:
> > > >>
> > > >> https://lore.kernel.org/linux-riscv/20250422094419.GC14170@noisy.programming.kicks-ass.net/
> > > > 
> > > > If I understand that right, exceptions from kernel should be treated as
> > > > NMI, so that lockdep can tell us if exception handlers touch locks.
> > > > 
> > > > But (conditionally) enabling interrupts does not lose us that benefit. It
> > > > is still considered NMI by lockdep.
> > > > 
> > > > Unless I miss something, the patch is fine as is.
> > 
> > I'm confused, you're wanting to conditionally enable interrupts from a
> > kernel exception while its NMI like? *WHY* ?!
> 
> What we want is to enable interrupt handling if it was enabled before
> the exception occured. So we can send a proper signal on PREEMPT_RT
> without chocking on spinlock_t/ sighand.

I'm confused, sending signals is for exception from userspace. That has
nothing to do with exceptions from kernelspace being NMI like.

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

* Re: [PATCH] riscv: Enable interrupt during exception handling
  2025-06-24 14:18               ` Peter Zijlstra
@ 2025-06-24 14:23                 ` Sebastian Andrzej Siewior
  2025-06-24 14:34                   ` Peter Zijlstra
  0 siblings, 1 reply; 13+ messages in thread
From: Sebastian Andrzej Siewior @ 2025-06-24 14:23 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: Clément Léger, Nam Cao, Maciej W. Rozycki,
	Palmer Dabbelt, Paul Walmsley, aou, Alexandre Ghiti, clrkwllms,
	rostedt, linux-rt-devel, linux-riscv, linux-kernel

On 2025-06-24 16:18:01 [+0200], Peter Zijlstra wrote:
> I'm confused, sending signals is for exception from userspace. That has
> nothing to do with exceptions from kernelspace being NMI like.

Yes. See the original submission
	https://lore.kernel.org/linux-riscv/20250620114346.1740512-1-namcao@linutronix.de/

Sebastian

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

* Re: [PATCH] riscv: Enable interrupt during exception handling
  2025-06-24 14:23                 ` Sebastian Andrzej Siewior
@ 2025-06-24 14:34                   ` Peter Zijlstra
  2025-06-24 15:33                     ` Nam Cao
  0 siblings, 1 reply; 13+ messages in thread
From: Peter Zijlstra @ 2025-06-24 14:34 UTC (permalink / raw)
  To: Sebastian Andrzej Siewior
  Cc: Clément Léger, Nam Cao, Maciej W. Rozycki,
	Palmer Dabbelt, Paul Walmsley, aou, Alexandre Ghiti, clrkwllms,
	rostedt, linux-rt-devel, linux-riscv, linux-kernel

On Tue, Jun 24, 2025 at 04:23:50PM +0200, Sebastian Andrzej Siewior wrote:
> On 2025-06-24 16:18:01 [+0200], Peter Zijlstra wrote:
> > I'm confused, sending signals is for exception from userspace. That has
> > nothing to do with exceptions from kernelspace being NMI like.
> 
> Yes. See the original submission
> 	https://lore.kernel.org/linux-riscv/20250620114346.1740512-1-namcao@linutronix.de/

I'm still confused, that code is trying to enable IRQs in the
from-kernel part. That's insane.

Can some Risc-V person explain why a from-kernel exception would ever
result in a signal?!?!

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

* Re: [PATCH] riscv: Enable interrupt during exception handling
  2025-06-24 14:34                   ` Peter Zijlstra
@ 2025-06-24 15:33                     ` Nam Cao
  2025-06-24 18:51                       ` Peter Zijlstra
  0 siblings, 1 reply; 13+ messages in thread
From: Nam Cao @ 2025-06-24 15:33 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: Sebastian Andrzej Siewior, Clément Léger,
	Maciej W. Rozycki, Palmer Dabbelt, Paul Walmsley, aou,
	Alexandre Ghiti, clrkwllms, rostedt, linux-rt-devel, linux-riscv,
	linux-kernel

On Tue, Jun 24, 2025 at 04:34:58PM +0200, Peter Zijlstra wrote:
> On Tue, Jun 24, 2025 at 04:23:50PM +0200, Sebastian Andrzej Siewior wrote:
> > On 2025-06-24 16:18:01 [+0200], Peter Zijlstra wrote:
> > > I'm confused, sending signals is for exception from userspace. That has
> > > nothing to do with exceptions from kernelspace being NMI like.
> > 
> > Yes. See the original submission
> > 	https://lore.kernel.org/linux-riscv/20250620114346.1740512-1-namcao@linutronix.de/
> 
> I'm still confused, that code is trying to enable IRQs in the
> from-kernel part. That's insane.
> 
> Can some Risc-V person explain why a from-kernel exception would ever
> result in a signal?!?!

Exceptions from kernel do not raise signals. Enabling irqs there is not
necessary, I can remove that part.

But for my curiousity, do you mind elaborating why it is insane to enable
irqs in from-kernel exception handling?

For "NMI-like" exceptions, (I think) I get it, the context would be messed
up. But what about the others, e.g. kernel page faults?

Best regards,
Nam

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

* Re: [PATCH] riscv: Enable interrupt during exception handling
  2025-06-24 15:33                     ` Nam Cao
@ 2025-06-24 18:51                       ` Peter Zijlstra
  0 siblings, 0 replies; 13+ messages in thread
From: Peter Zijlstra @ 2025-06-24 18:51 UTC (permalink / raw)
  To: Nam Cao
  Cc: Sebastian Andrzej Siewior, Cl??ment L??ger, Maciej W. Rozycki,
	Palmer Dabbelt, Paul Walmsley, aou, Alexandre Ghiti, clrkwllms,
	rostedt, linux-rt-devel, linux-riscv, linux-kernel

On Tue, Jun 24, 2025 at 05:33:14PM +0200, Nam Cao wrote:
> On Tue, Jun 24, 2025 at 04:34:58PM +0200, Peter Zijlstra wrote:
> > On Tue, Jun 24, 2025 at 04:23:50PM +0200, Sebastian Andrzej Siewior wrote:
> > > On 2025-06-24 16:18:01 [+0200], Peter Zijlstra wrote:
> > > > I'm confused, sending signals is for exception from userspace. That has
> > > > nothing to do with exceptions from kernelspace being NMI like.
> > > 
> > > Yes. See the original submission
> > > 	https://lore.kernel.org/linux-riscv/20250620114346.1740512-1-namcao@linutronix.de/
> > 
> > I'm still confused, that code is trying to enable IRQs in the
> > from-kernel part. That's insane.
> > 
> > Can some Risc-V person explain why a from-kernel exception would ever
> > result in a signal?!?!
> 
> Exceptions from kernel do not raise signals. Enabling irqs there is not
> necessary, I can remove that part.
> 
> But for my curiousity, do you mind elaborating why it is insane to enable
> irqs in from-kernel exception handling?
> 
> For "NMI-like" exceptions, (I think) I get it, the context would be messed
> up. But what about the others, e.g. kernel page faults?

The non-NMI exceptions like page-fault are okay. In fact, we hard rely
on it when kernel space accesses user-space data in a preemptible
context, it is expected to handle the fault.

My concern was about the NMI like ones -- enabling IRQs for those is
quite mad.

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

end of thread, other threads:[~2025-06-24 18:51 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-20 11:43 [PATCH] riscv: Enable interrupt during exception handling Nam Cao
2025-06-23 22:17 ` Palmer Dabbelt
2025-06-24  2:09   ` Maciej W. Rozycki
2025-06-24 11:37     ` Clément Léger
2025-06-24 13:17       ` Nam Cao
2025-06-24 13:48         ` Clément Léger
2025-06-24 14:08           ` Peter Zijlstra
2025-06-24 14:11             ` Sebastian Andrzej Siewior
2025-06-24 14:18               ` Peter Zijlstra
2025-06-24 14:23                 ` Sebastian Andrzej Siewior
2025-06-24 14:34                   ` Peter Zijlstra
2025-06-24 15:33                     ` Nam Cao
2025-06-24 18:51                       ` Peter Zijlstra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).