Skip to contentSkip to conversation

riscv: vector: only enable interrupts in the first-use trap

PATCH Applied Andy Chiu 5 participants 8 messages Jun 25, 2023 – Jul 9, 2023 RISC-V

lore
Share & tools
lore

Andy Chiu

author
#0 lore raw

PATCH 1/1 riscv: vector: only enable interrupts in the first-use trap

The function irqentry_exit_to_user_mode() must be called with interrupt disabled. The caller of do_trap_insn_illegal() also assumes running without interrupts. So, we should turn off interrupts after riscv_v_first_use_handler() returns.

Fixes cd054837243b ("riscv: Allocate user's vector context in the first-use trap")
---
 arch/riscv/kernel/traps.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
arch/riscv/kernel/traps.c +7 -1
index 05ffdcd1424e..1595e246bda1 100644
@@ -149,12 +149,18 @@ DO_ERROR_INFO(do_trap_insn_fault,
 
 asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *regs)
 {
+	bool handled;
+
 	if (user_mode(regs)) {
 		irqentry_enter_from_user_mode(regs);
 
 		local_irq_enable();
 
-		if (!riscv_v_first_use_handler(regs))
+		handled = riscv_v_first_use_handler(regs);
+
+		local_irq_disable();
+
+		if (!handled)
 			do_trap_error(regs, SIGILL, ILL_ILLOPC, regs->epc,
 				      "Oops - illegal instruction");
 
Signature · 7 lines
2.17.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Andy Chiu

The function irqentry_exit_to_user_mode() must be called with interrupt disabled. The caller of do_trap_insn_illegal() also assumes running without interrupts. So, we should turn off interrupts after riscv_v_first_use_handler() returns.

Fixes cd054837243b ("riscv: Allocate user's vector context in the first-use trap")

Good catch,

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#2 lore raw
In reply to Andy Chiu
Quoted 19 lines · Andy Chiu

The function irqentry_exit_to_user_mode() must be called with interrupt disabled. The caller of do_trap_insn_illegal() also assumes running without interrupts. So, we should turn off interrupts after riscv_v_first_use_handler() returns.

Fixes cd054837243b ("riscv: Allocate user's vector context in the first-use trap")
---
 arch/riscv/kernel/traps.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
arch/riscv/kernel/traps.c +0 -0
index 05ffdcd1424e..1595e246bda1 100644
@@ -149,12 +149,18 @@ DO_ERROR_INFO(do_trap_insn_fault,
 
 asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *regs)

arch/riscv/kernel/traps.c DO_ERROR_INFO(do_trap_insn_fault, · line 159

Quoted 10 lines
arch/riscv/kernel/traps.c +3 -1
 {
+       bool handled;
+
        if (user_mode(regs)) {
                irqentry_enter_from_user_mode(regs);
 
                local_irq_enable();
 
-               if (!riscv_v_first_use_handler(regs))
+               handled = riscv_v_first_use_handler(regs);

How about making riscv_v_first_use_handler irq_disable safe?
--------

arch/riscv/kernel/traps.c +0 -0
index 5158961ea977..545295045705 100644
@@ -153,8 +153,6 @@ 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();
-

                if (!riscv_v_first_use_handler(regs))
                        do_trap_error(regs, SIGILL, ILL_ILLOPC, regs->epc,
                                      "Oops - illegal instruction");
arch/riscv/kernel/vector.c +1 -2
index f9c8e19ab301..7616c027ee64 100644
@@ -84,7 +84,7 @@ static int riscv_v_thread_zalloc(void)
 {
        void *datap;
 
-       datap = kzalloc(riscv_v_vsize, GFP_KERNEL);
+       datap = kzalloc(riscv_v_vsize, GFP_KERNEL | GFP_ATOMIC);
        if (!datap)
                return -ENOMEM;
---------
Quoted 10 lines
arch/riscv/kernel/traps.c +4 -1
+
+               local_irq_disable();
+
+               if (!handled)
                        do_trap_error(regs, SIGILL, ILL_ILLOPC, regs->epc,
                                      "Oops - illegal instruction");
 
--

2.17.1

Signature · 7 lines
Best Regards
 Guo Ren

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#3 lore raw

Another one:

arch/riscv/kernel/vector.c +3 -4
index f9c8e19ab301..764b7c098165 100644
@@ -84,7 +84,7 @@ static int riscv_v_thread_zalloc(void)
 {
        void *datap;
 
        datap = kzalloc(riscv_v_vsize, GFP_KERNEL);
        if (!datap)
                return -ENOMEM;
 
@@ -162,10 +162,9 @@ bool riscv_v_first_use_handler(struct pt_regs *regs)
         * context where VS has been off. So, try to allocate the user's V
         * context and resume execution.
         */
-       if (riscv_v_thread_zalloc()) {
-               force_sig(SIGBUS);
-               return true;
-       }
+       if (riscv_v_thread_zalloc())
+               return false;
+
        riscv_v_vstate_on(regs);
        return true;
 }

--------

Your force_sig throws the debug info away, and the standard one is enough for us.

Quoted 76 lines · Guo Ren
Quoted 19 lines · Andy Chiu

The function irqentry_exit_to_user_mode() must be called with interrupt disabled. The caller of do_trap_insn_illegal() also assumes running without interrupts. So, we should turn off interrupts after riscv_v_first_use_handler() returns.

Fixes cd054837243b ("riscv: Allocate user's vector context in the first-use trap")
---
 arch/riscv/kernel/traps.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
arch/riscv/kernel/traps.c +0 -0
index 05ffdcd1424e..1595e246bda1 100644
@@ -149,12 +149,18 @@ DO_ERROR_INFO(do_trap_insn_fault,
 
 asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *regs)

arch/riscv/kernel/traps.c DO_ERROR_INFO(do_trap_insn_fault, · line 159

Quoted 10 lines
arch/riscv/kernel/traps.c +3 -1
 {
+       bool handled;
+
        if (user_mode(regs)) {
                irqentry_enter_from_user_mode(regs);
 
                local_irq_enable();
 
-               if (!riscv_v_first_use_handler(regs))
+               handled = riscv_v_first_use_handler(regs);

How about making riscv_v_first_use_handler irq_disable safe?
--------

arch/riscv/kernel/traps.c +0 -0
index 5158961ea977..545295045705 100644
@@ -153,8 +153,6 @@ asmlinkage __visible __trap_section void

do_trap_insn_illegal(struct pt_regs *re

        if (user_mode(regs)) {
                irqentry_enter_from_user_mode(regs);
arch/riscv/kernel/traps.c +0 -2
-               local_irq_enable();
-
                if (!riscv_v_first_use_handler(regs))
                        do_trap_error(regs, SIGILL, ILL_ILLOPC, regs->epc,
                                      "Oops - illegal instruction");
arch/riscv/kernel/vector.c +1 -2
index f9c8e19ab301..7616c027ee64 100644
@@ -84,7 +84,7 @@ static int riscv_v_thread_zalloc(void)
 {
        void *datap;
 
-       datap = kzalloc(riscv_v_vsize, GFP_KERNEL);
+       datap = kzalloc(riscv_v_vsize, GFP_KERNEL | GFP_ATOMIC);
        if (!datap)
                return -ENOMEM;
---------
Quoted 11 lines
arch/riscv/kernel/traps.c +4 -1
+
+               local_irq_disable();
+
+               if (!handled)
                        do_trap_error(regs, SIGILL, ILL_ILLOPC, regs->epc,
                                      "Oops - illegal instruction");
 
--

2.17.1

--
Best Regards
Guo Ren

Signature · 7 lines
Best Regards
 Guo Ren

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
author
#4 lore raw

Hi Guo,

Quoted 30 lines · Guo Ren

Another one:

arch/riscv/kernel/vector.c +3 -5
index f9c8e19ab301..764b7c098165 100644
@@ -84,7 +84,7 @@ static int riscv_v_thread_zalloc(void)
 {
        void *datap;
 
        datap = kzalloc(riscv_v_vsize, GFP_KERNEL);
        if (!datap)
                return -ENOMEM;
 
@@ -162,10 +162,9 @@ bool riscv_v_first_use_handler(struct pt_regs *regs)
         * context where VS has been off. So, try to allocate the user's V
         * context and resume execution.
         */
-       if (riscv_v_thread_zalloc()) {
-               force_sig(SIGBUS);
-               return true;
-       }
+       if (riscv_v_thread_zalloc())
+               return false;
+
        riscv_v_vstate_on(regs);
        return true;
 }
--------
Quoted 2 lines

Your force_sig throws the debug info away, and the standard one is enough for us.

Could you help me to understand more on how this throws debug info away? Do you mean the pr_info() print in the do_trap() function? If we'd want to use the standard one then we should instead return a signal number in riscv_v_first_use_handler(). Because we want to send SIGBUS instead of SIGILL.

Quoted 81 lines · Guo Ren
Quoted 68 lines · Andy Chiu
Quoted 19 lines

The function irqentry_exit_to_user_mode() must be called with interrupt disabled. The caller of do_trap_insn_illegal() also assumes running without interrupts. So, we should turn off interrupts after riscv_v_first_use_handler() returns.

Fixes cd054837243b ("riscv: Allocate user's vector context in the first-use trap")
---
 arch/riscv/kernel/traps.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
arch/riscv/kernel/traps.c +0 -0
index 05ffdcd1424e..1595e246bda1 100644
@@ -149,12 +149,18 @@ DO_ERROR_INFO(do_trap_insn_fault,
 
 asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *regs)

arch/riscv/kernel/traps.c DO_ERROR_INFO(do_trap_insn_fault, · line 159

Quoted 10 lines
arch/riscv/kernel/traps.c +3 -1
 {
+       bool handled;
+
        if (user_mode(regs)) {
                irqentry_enter_from_user_mode(regs);
 
                local_irq_enable();
 
-               if (!riscv_v_first_use_handler(regs))
+               handled = riscv_v_first_use_handler(regs);

How about making riscv_v_first_use_handler irq_disable safe?
--------

arch/riscv/kernel/traps.c +0 -0
index 5158961ea977..545295045705 100644
@@ -153,8 +153,6 @@ asmlinkage __visible __trap_section void

do_trap_insn_illegal(struct pt_regs *re

        if (user_mode(regs)) {
                irqentry_enter_from_user_mode(regs);
arch/riscv/kernel/traps.c +0 -2
-               local_irq_enable();
-
                if (!riscv_v_first_use_handler(regs))
                        do_trap_error(regs, SIGILL, ILL_ILLOPC, regs->epc,
                                      "Oops - illegal instruction");
arch/riscv/kernel/vector.c +1 -2
index f9c8e19ab301..7616c027ee64 100644
@@ -84,7 +84,7 @@ static int riscv_v_thread_zalloc(void)
 {
        void *datap;
 
-       datap = kzalloc(riscv_v_vsize, GFP_KERNEL);
+       datap = kzalloc(riscv_v_vsize, GFP_KERNEL | GFP_ATOMIC);
        if (!datap)
                return -ENOMEM;
---------
Quoted 8 lines
arch/riscv/kernel/traps.c +4 -0
+
+               local_irq_disable();
+
+               if (!handled)
                        do_trap_error(regs, SIGILL, ILL_ILLOPC, regs->epc,
                                      "Oops - illegal instruction");
 
Quoted 8 lines
Quoted 3 lines

--
2.17.1

--
Best Regards
Guo Ren

Quoted 3 lines

--
Best Regards
Guo Ren

Thanks,
Andy

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#5 lore raw
Quoted 34 lines · Andy Chiu

Hi Guo,

Quoted 30 lines · Guo Ren

Another one:

arch/riscv/kernel/vector.c +3 -5
index f9c8e19ab301..764b7c098165 100644
@@ -84,7 +84,7 @@ static int riscv_v_thread_zalloc(void)
 {
        void *datap;
 
        datap = kzalloc(riscv_v_vsize, GFP_KERNEL);
        if (!datap)
                return -ENOMEM;
 
@@ -162,10 +162,9 @@ bool riscv_v_first_use_handler(struct pt_regs *regs)
         * context where VS has been off. So, try to allocate the user's V
         * context and resume execution.
         */
-       if (riscv_v_thread_zalloc()) {
-               force_sig(SIGBUS);
-               return true;
-       }
+       if (riscv_v_thread_zalloc())
+               return false;
+
        riscv_v_vstate_on(regs);
        return true;
 }
--------
Quoted 5 lines
Quoted 2 lines

Your force_sig throws the debug info away, and the standard one is enough for us.

Could you help me to understand more on how this throws debug info away? Do you mean the pr_info() print in the do_trap() function? If

Yes, you code just send a sig, but no debug info.

Quoted 3 lines

we'd want to use the standard one then we should instead return a signal number in riscv_v_first_use_handler(). Because we want to send SIGBUS instead of SIGILL.

Do we really need a SIGBUS here?
When we can't handle this instruction, treat it as a invalid one. SIGILL - invalid program image, such as invalid instruction SIGBUS - abbreviation for “Bus Error”.

Quoted 82 lines
Quoted 82 lines · Guo Ren
Quoted 76 lines · Andy Chiu
Quoted 19 lines

The function irqentry_exit_to_user_mode() must be called with interrupt disabled. The caller of do_trap_insn_illegal() also assumes running without interrupts. So, we should turn off interrupts after riscv_v_first_use_handler() returns.

Fixes cd054837243b ("riscv: Allocate user's vector context in the first-use trap")
---
 arch/riscv/kernel/traps.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
arch/riscv/kernel/traps.c +0 -0
index 05ffdcd1424e..1595e246bda1 100644
@@ -149,12 +149,18 @@ DO_ERROR_INFO(do_trap_insn_fault,
 
 asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *regs)

arch/riscv/kernel/traps.c DO_ERROR_INFO(do_trap_insn_fault, · line 159

Quoted 10 lines
arch/riscv/kernel/traps.c +3 -1
 {
+       bool handled;
+
        if (user_mode(regs)) {
                irqentry_enter_from_user_mode(regs);
 
                local_irq_enable();
 
-               if (!riscv_v_first_use_handler(regs))
+               handled = riscv_v_first_use_handler(regs);

How about making riscv_v_first_use_handler irq_disable safe?
--------

arch/riscv/kernel/traps.c +0 -0
index 5158961ea977..545295045705 100644
@@ -153,8 +153,6 @@ asmlinkage __visible __trap_section void

do_trap_insn_illegal(struct pt_regs *re

        if (user_mode(regs)) {
                irqentry_enter_from_user_mode(regs);
arch/riscv/kernel/traps.c +0 -2
-               local_irq_enable();
-
                if (!riscv_v_first_use_handler(regs))
                        do_trap_error(regs, SIGILL, ILL_ILLOPC, regs->epc,
                                      "Oops - illegal instruction");
arch/riscv/kernel/vector.c +1 -2
index f9c8e19ab301..7616c027ee64 100644
@@ -84,7 +84,7 @@ static int riscv_v_thread_zalloc(void)
 {
        void *datap;
 
-       datap = kzalloc(riscv_v_vsize, GFP_KERNEL);
+       datap = kzalloc(riscv_v_vsize, GFP_KERNEL | GFP_ATOMIC);
        if (!datap)
                return -ENOMEM;
---------
Quoted 11 lines
arch/riscv/kernel/traps.c +4 -1
+
+               local_irq_disable();
+
+               if (!handled)
                        do_trap_error(regs, SIGILL, ILL_ILLOPC, regs->epc,
                                      "Oops - illegal instruction");
 
--

2.17.1

--
Best Regards
Guo Ren

Quoted 6 lines
Quoted 3 lines

--
Best Regards
Guo Ren

Thanks,
Andy

--
Best Regards
Guo Ren

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#6 lore raw
In reply to Andy Chiu
Quoted 6 lines · Andy Chiu

The function irqentry_exit_to_user_mode() must be called with interrupt disabled. The caller of do_trap_insn_illegal() also assumes running without interrupts. So, we should turn off interrupts after riscv_v_first_use_handler() returns.

Applied, thanks!

[1/1] riscv: vector: only enable interrupts in the first-use trap

      https://git.kernel.org/palmer/c/26c38cd802c9

Best regards,

Signature · 7 lines
Palmer Dabbelt <[email protected]>


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#7 lore raw
In reply to Andy Chiu
This patch was applied to riscv/linux.git (for-next) by Palmer Dabbelt <[email protected]>:

Hello:

This patch was applied to riscv/linux.git (for-next) by Palmer Dabbelt <[email protected]>:

Quoted 9 lines · On Sun, 25 Jun 2023 15:54:15 +0000 you wrote:

The function irqentry_exit_to_user_mode() must be called with interrupt disabled. The caller of do_trap_insn_illegal() also assumes running without interrupts. So, we should turn off interrupts after riscv_v_first_use_handler() returns.

Fixes cd054837243b ("riscv: Allocate user's vector context in the first-use trap")

[...]

Here is the summary with links:
- riscv: vector: only enable interrupts in the first-use trap

    https://git.kernel.org/riscv/c/26c38cd802c9

You are awesome, thank you!

Signature · 9 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv