Re: [PATCH] powerpc: Use user_mode() macro when possible

2024-03-13 Thread Michael Ellerman
On Fri, 16 Feb 2024 11:10:36 +0100, Christophe Leroy wrote:
> There is a nice macro to check user mode.
> 
> Use it instead of open coding anding with MSR_PR to increase
> readability and avoid having to comment what that anding is for.
> 
> 

Applied to powerpc/next.

[1/1] powerpc: Use user_mode() macro when possible
  https://git.kernel.org/powerpc/c/d5835fb60bad641dbae64fe30c02f10857bf4647

cheers


[PATCH] powerpc: Use user_mode() macro when possible

2024-02-16 Thread Christophe Leroy
There is a nice macro to check user mode.

Use it instead of open coding anding with MSR_PR to increase
readability and avoid having to comment what that anding is for.

Signed-off-by: Christophe Leroy 
---
 arch/powerpc/include/asm/interrupt.h |  2 +-
 arch/powerpc/kernel/syscall.c|  2 +-
 arch/powerpc/kernel/traps.c  |  4 ++--
 arch/powerpc/lib/sstep.c | 23 +++
 arch/powerpc/perf/core-book3s.c  |  2 +-
 arch/powerpc/xmon/xmon.c |  4 ++--
 6 files changed, 18 insertions(+), 19 deletions(-)

diff --git a/arch/powerpc/include/asm/interrupt.h 
b/arch/powerpc/include/asm/interrupt.h
index a4196ab1d016..7b610864b364 100644
--- a/arch/powerpc/include/asm/interrupt.h
+++ b/arch/powerpc/include/asm/interrupt.h
@@ -97,7 +97,7 @@ DECLARE_STATIC_KEY_FALSE(interrupt_exit_not_reentrant);
 
 static inline bool is_implicit_soft_masked(struct pt_regs *regs)
 {
-   if (regs->msr & MSR_PR)
+   if (user_mode(regs))
return false;
 
if (regs->nip >= (unsigned long)__end_soft_masked)
diff --git a/arch/powerpc/kernel/syscall.c b/arch/powerpc/kernel/syscall.c
index 77fedb190c93..f6f868e817e6 100644
--- a/arch/powerpc/kernel/syscall.c
+++ b/arch/powerpc/kernel/syscall.c
@@ -31,7 +31,7 @@ notrace long system_call_exception(struct pt_regs *regs, 
unsigned long r0)
user_exit_irqoff();
 
BUG_ON(regs_is_unrecoverable(regs));
-   BUG_ON(!(regs->msr & MSR_PR));
+   BUG_ON(!user_mode(regs));
BUG_ON(arch_irq_disabled_regs(regs));
 
 #ifdef CONFIG_PPC_PKEY
diff --git a/arch/powerpc/kernel/traps.c b/arch/powerpc/kernel/traps.c
index 11e062b47d3f..f23430adb68a 100644
--- a/arch/powerpc/kernel/traps.c
+++ b/arch/powerpc/kernel/traps.c
@@ -404,7 +404,7 @@ noinstr void hv_nmi_check_nonrecoverable(struct pt_regs 
*regs)
return;
if (!(regs->msr & MSR_HV))
return;
-   if (regs->msr & MSR_PR)
+   if (user_mode(regs))
return;
 
/*
@@ -1510,7 +1510,7 @@ static void do_program_check(struct pt_regs *regs)
if (!is_kernel_addr(bugaddr) && !(regs->msr & MSR_IR))
bugaddr += PAGE_OFFSET;
 
-   if (!(regs->msr & MSR_PR) &&  /* not user-mode */
+   if (!user_mode(regs) &&
report_bug(bugaddr, regs) == BUG_TRAP_TYPE_WARN) {
regs_add_return_ip(regs, 4);
return;
diff --git a/arch/powerpc/lib/sstep.c b/arch/powerpc/lib/sstep.c
index 5766180f5380..e65f3fb68d06 100644
--- a/arch/powerpc/lib/sstep.c
+++ b/arch/powerpc/lib/sstep.c
@@ -1429,7 +1429,7 @@ int analyse_instr(struct instruction_op *op, const struct 
pt_regs *regs,
return 1;
 
case 18:/* rfid, scary */
-   if (regs->msr & MSR_PR)
+   if (user_mode(regs))
goto priv;
op->type = RFI;
return 0;
@@ -1742,13 +1742,13 @@ int analyse_instr(struct instruction_op *op, const 
struct pt_regs *regs,
return 1;
 #endif
case 83:/* mfmsr */
-   if (regs->msr & MSR_PR)
+   if (user_mode(regs))
goto priv;
op->type = MFMSR;
op->reg = rd;
return 0;
case 146:   /* mtmsr */
-   if (regs->msr & MSR_PR)
+   if (user_mode(regs))
goto priv;
op->type = MTMSR;
op->reg = rd;
@@ -1756,7 +1756,7 @@ int analyse_instr(struct instruction_op *op, const struct 
pt_regs *regs,
return 0;
 #ifdef CONFIG_PPC64
case 178:   /* mtmsrd */
-   if (regs->msr & MSR_PR)
+   if (user_mode(regs))
goto priv;
op->type = MTMSR;
op->reg = rd;
@@ -3437,14 +3437,14 @@ int emulate_loadstore(struct pt_regs *regs, struct 
instruction_op *op)
 * stored in the thread_struct.  If the instruction is in
 * the kernel, we must not touch the state in the thread_struct.
 */
-   if (!(regs->msr & MSR_PR) && !(regs->msr & MSR_FP))
+   if (!user_mode(regs) && !(regs->msr & MSR_FP))
return 0;
err = do_fp_load(op, ea, regs, cross_endian);
break;
 #endif
 #ifdef CONFIG_ALTIVEC
case LOAD_VMX:
-   if (!(regs->msr & MSR_PR) && !(regs->msr & MSR_VEC))
+   if (!user_mode(regs) && !(regs->msr & MSR_VEC))
return 0;
err = do_vec_load(op->reg, ea, size, regs, cross_endian);
break;
@@ -3459,7 +3459,7