[PATCH v3 1/6] powerpc: Add LKDTM accessor for patching addr

2020-08-26 Thread Christopher M. Riedl
When live patching a STRICT_RWX kernel, a mapping is installed at a
"patching address" with temporary write permissions. Provide a
LKDTM-only accessor function for this address in preparation for a LKDTM
test which attempts to "hijack" this mapping by writing to it from
another CPU.

Signed-off-by: Christopher M. Riedl 
---
 arch/powerpc/include/asm/code-patching.h | 4 
 arch/powerpc/lib/code-patching.c | 7 +++
 2 files changed, 11 insertions(+)

diff --git a/arch/powerpc/include/asm/code-patching.h 
b/arch/powerpc/include/asm/code-patching.h
index eacc9102c251..7216d6a6bb0a 100644
--- a/arch/powerpc/include/asm/code-patching.h
+++ b/arch/powerpc/include/asm/code-patching.h
@@ -187,4 +187,8 @@ static inline unsigned long ppc_kallsyms_lookup_name(const 
char *name)
 ___PPC_RA(__REG_R1) | PPC_LR_STKOFF)
 #endif /* CONFIG_PPC64 */
 
+#if defined(CONFIG_LKDTM) && defined(CONFIG_STRICT_KERNEL_RWX)
+unsigned long read_cpu_patching_addr(unsigned int cpu);
+#endif
+
 #endif /* _ASM_POWERPC_CODE_PATCHING_H */
diff --git a/arch/powerpc/lib/code-patching.c b/arch/powerpc/lib/code-patching.c
index 8c3934ea6220..85d3fdca9452 100644
--- a/arch/powerpc/lib/code-patching.c
+++ b/arch/powerpc/lib/code-patching.c
@@ -46,6 +46,13 @@ int raw_patch_instruction(struct ppc_inst *addr, struct 
ppc_inst instr)
 #ifdef CONFIG_STRICT_KERNEL_RWX
 static DEFINE_PER_CPU(struct vm_struct *, text_poke_area);
 
+#ifdef CONFIG_LKDTM
+unsigned long read_cpu_patching_addr(unsigned int cpu)
+{
+   return (unsigned long)(per_cpu(text_poke_area, cpu))->addr;
+}
+#endif
+
 static int text_area_cpu_up(unsigned int cpu)
 {
struct vm_struct *area;
-- 
2.28.0



Re: [PATCH v2] powerpc: Update documentation of ISA versions for Power10

2020-08-26 Thread Jordan Niethe
On Thu, Aug 27, 2020 at 2:49 PM Christophe Leroy
 wrote:
>
>
>
> Le 27/08/2020 à 06:05, Jordan Niethe a écrit :
> > Update the CPU to ISA Version Mapping document to include Power10 and
> > ISA v3.1.
>
> Maybe Documentation/powerpc/cpu_families.rst should be updated as well.
Good idea it still needs Power9 too.
>
> Christophe
>
>
>
> >
> > Signed-off-by: Jordan Niethe 
> > ---
> > v2: Transactional Memory = No
> > ---
> >   Documentation/powerpc/isa-versions.rst | 4 
> >   1 file changed, 4 insertions(+)
> >
> > diff --git a/Documentation/powerpc/isa-versions.rst 
> > b/Documentation/powerpc/isa-versions.rst
> > index a363d8c1603c..3873bbba183a 100644
> > --- a/Documentation/powerpc/isa-versions.rst
> > +++ b/Documentation/powerpc/isa-versions.rst
> > @@ -7,6 +7,7 @@ Mapping of some CPU versions to relevant ISA versions.
> >   = 
> > 
> >   CPU   Architecture version
> >   = 
> > 
> > +Power10   Power ISA v3.1
> >   Power9Power ISA v3.0B
> >   Power8Power ISA v2.07
> >   Power7Power ISA v2.06
> > @@ -32,6 +33,7 @@ Key Features
> >   == ==
> >   CPUVMX (aka. Altivec)
> >   == ==
> > +Power10Yes
> >   Power9 Yes
> >   Power8 Yes
> >   Power7 Yes
> > @@ -47,6 +49,7 @@ PPC970 Yes
> >   == 
> >   CPUVSX
> >   == 
> > +Power10Yes
> >   Power9 Yes
> >   Power8 Yes
> >   Power7 Yes
> > @@ -62,6 +65,7 @@ PPC970 No
> >   == 
> >   CPUTransactional Memory
> >   == 
> > +Power10No  (* see Power ISA v3.1 Appendix A.)
> >   Power9 Yes (* see transactional_memory.txt)
> >   Power8 Yes
> >   Power7 No
> >


Re: [PATCH v5 08/23] powerpc/book3s64/kuap: Rename MMU_FTR_RADIX_KUAP to MMU_FTR_KUAP

2020-08-26 Thread Christophe Leroy




Le 27/08/2020 à 06:09, Aneesh Kumar K.V a écrit :

This is in preparate to adding support for kuap with hash translation.
In preparation for that rename/move kuap related functions to
non radix names. Also move the feature bit closer to MMU_FTR_KUEP.

Signed-off-by: Aneesh Kumar K.V 
---
  arch/powerpc/include/asm/book3s/64/kup.h | 18 +-
  arch/powerpc/include/asm/mmu.h   | 16 
  arch/powerpc/mm/book3s64/pkeys.c |  2 +-
  3 files changed, 18 insertions(+), 18 deletions(-)

diff --git a/arch/powerpc/include/asm/book3s/64/kup.h 
b/arch/powerpc/include/asm/book3s/64/kup.h
index 918a2fcceee7..5cec202dc42f 100644
--- a/arch/powerpc/include/asm/book3s/64/kup.h
+++ b/arch/powerpc/include/asm/book3s/64/kup.h
@@ -24,7 +24,7 @@
mtspr   SPRN_AMR, \gpr2
/* No isync required, see kuap_restore_amr() */
  998:
-   END_MMU_FTR_SECTION_NESTED_IFSET(MMU_FTR_RADIX_KUAP, 67)
+   END_MMU_FTR_SECTION_NESTED_IFSET(MMU_FTR_KUAP, 67)
  #endif
  .endm
  
@@ -36,7 +36,7 @@

sldi\gpr2, \gpr2, AMR_KUAP_SHIFT
  999:  tdne\gpr1, \gpr2
EMIT_BUG_ENTRY 999b, __FILE__, __LINE__, (BUGFLAG_WARNING | 
BUGFLAG_ONCE)
-   END_MMU_FTR_SECTION_NESTED_IFSET(MMU_FTR_RADIX_KUAP, 67)
+   END_MMU_FTR_SECTION_NESTED_IFSET(MMU_FTR_KUAP, 67)
  #endif
  .endm
  
@@ -56,7 +56,7 @@

mtspr   SPRN_AMR, \gpr2
isync
  99:
-   END_MMU_FTR_SECTION_NESTED_IFSET(MMU_FTR_RADIX_KUAP, 67)
+   END_MMU_FTR_SECTION_NESTED_IFSET(MMU_FTR_KUAP, 67)
  #endif
  .endm
  
@@ -69,7 +69,7 @@
  
  static inline void kuap_restore_amr(struct pt_regs *regs, unsigned long amr)

  {
-   if (mmu_has_feature(MMU_FTR_RADIX_KUAP) && unlikely(regs->kuap != amr)) 
{
+   if (mmu_has_feature(MMU_FTR_KUAP) && unlikely(regs->kuap != amr)) {
isync();
mtspr(SPRN_AMR, regs->kuap);
/*
@@ -82,7 +82,7 @@ static inline void kuap_restore_amr(struct pt_regs *regs, 
unsigned long amr)
  
  static inline unsigned long kuap_get_and_check_amr(void)

  {
-   if (mmu_has_feature(MMU_FTR_RADIX_KUAP)) {
+   if (mmu_has_feature(MMU_FTR_KUAP)) {
unsigned long amr = mfspr(SPRN_AMR);
if (IS_ENABLED(CONFIG_PPC_KUAP_DEBUG)) /* kuap_check_amr() */
WARN_ON_ONCE(amr != AMR_KUAP_BLOCKED);
@@ -93,7 +93,7 @@ static inline unsigned long kuap_get_and_check_amr(void)
  
  static inline void kuap_check_amr(void)

  {
-   if (IS_ENABLED(CONFIG_PPC_KUAP_DEBUG) && 
mmu_has_feature(MMU_FTR_RADIX_KUAP))
+   if (IS_ENABLED(CONFIG_PPC_KUAP_DEBUG) && mmu_has_feature(MMU_FTR_KUAP))
WARN_ON_ONCE(mfspr(SPRN_AMR) != AMR_KUAP_BLOCKED);
  }
  
@@ -122,7 +122,7 @@ static inline unsigned long kuap_get_and_check_amr(void)
  
  static inline unsigned long get_kuap(void)

  {
-   if (!early_mmu_has_feature(MMU_FTR_RADIX_KUAP))
+   if (!early_mmu_has_feature(MMU_FTR_KUAP))
return 0;
  
  	return mfspr(SPRN_AMR);

@@ -130,7 +130,7 @@ static inline unsigned long get_kuap(void)
  
  static inline void set_kuap(unsigned long value)

  {
-   if (!early_mmu_has_feature(MMU_FTR_RADIX_KUAP))
+   if (!early_mmu_has_feature(MMU_FTR_KUAP))
return;
  
  	/*

@@ -180,7 +180,7 @@ static inline void restore_user_access(unsigned long flags)
  static inline bool
  bad_kuap_fault(struct pt_regs *regs, unsigned long address, bool is_write)
  {
-   return WARN(mmu_has_feature(MMU_FTR_RADIX_KUAP) &&
+   return WARN(mmu_has_feature(MMU_FTR_KUAP) &&
(regs->kuap & (is_write ? AMR_KUAP_BLOCK_WRITE : 
AMR_KUAP_BLOCK_READ)),
"Bug: %s fault blocked by AMR!", is_write ? "Write" : 
"Read");
  }
diff --git a/arch/powerpc/include/asm/mmu.h b/arch/powerpc/include/asm/mmu.h
index 255a1837e9f7..04e7a65637fb 100644
--- a/arch/powerpc/include/asm/mmu.h
+++ b/arch/powerpc/include/asm/mmu.h
@@ -29,7 +29,12 @@
   */
  
  /*

- * Support for KUEP feature.
+ * Supports KUAP (key 0 controlling userspace addresses) on radix
+ */
+#define MMU_FTR_KUAP   ASM_CONST(0x0200)
+
+/*
+ * Suppor for KUEP feature.


Unexpected change I guess. Suppor ==> Support

Christophe


   */
  #define MMU_FTR_KUEP  ASM_CONST(0x0400)
  
@@ -120,11 +125,6 @@

   */
  #define MMU_FTR_1T_SEGMENTASM_CONST(0x4000)
  
-/*

- * Supports KUAP (key 0 controlling userspace addresses) on radix
- */
-#define MMU_FTR_RADIX_KUAP ASM_CONST(0x8000)
-
  /* MMU feature bit sets for various CPUs */
  #define MMU_FTRS_DEFAULT_HPTE_ARCH_V2 \
MMU_FTR_HPTE_TABLE | MMU_FTR_PPCAS_ARCH_V2
@@ -187,10 +187,10 @@ enum {
  #ifdef CONFIG_PPC_RADIX_MMU
MMU_FTR_TYPE_RADIX |
MMU_FTR_GTSE |
+#endif /* CONFIG_PPC_RADIX_MMU */
  #ifdef CONFIG_PPC_KUAP
-   MMU_FTR_RADIX_KUAP |
+   MMU_FTR_KUAP |
  #endif /* CONFIG_PPC_KUAP */
-#endif /* CONFIG_PPC_RADIX_MMU */

Re: [PATCH v2] powerpc: Update documentation of ISA versions for Power10

2020-08-26 Thread Christophe Leroy




Le 27/08/2020 à 06:05, Jordan Niethe a écrit :

Update the CPU to ISA Version Mapping document to include Power10 and
ISA v3.1.


Maybe Documentation/powerpc/cpu_families.rst should be updated as well.

Christophe





Signed-off-by: Jordan Niethe 
---
v2: Transactional Memory = No
---
  Documentation/powerpc/isa-versions.rst | 4 
  1 file changed, 4 insertions(+)

diff --git a/Documentation/powerpc/isa-versions.rst 
b/Documentation/powerpc/isa-versions.rst
index a363d8c1603c..3873bbba183a 100644
--- a/Documentation/powerpc/isa-versions.rst
+++ b/Documentation/powerpc/isa-versions.rst
@@ -7,6 +7,7 @@ Mapping of some CPU versions to relevant ISA versions.
  = 
  CPU   Architecture version
  = 
+Power10   Power ISA v3.1
  Power9Power ISA v3.0B
  Power8Power ISA v2.07
  Power7Power ISA v2.06
@@ -32,6 +33,7 @@ Key Features
  == ==
  CPUVMX (aka. Altivec)
  == ==
+Power10Yes
  Power9 Yes
  Power8 Yes
  Power7 Yes
@@ -47,6 +49,7 @@ PPC970 Yes
  == 
  CPUVSX
  == 
+Power10Yes
  Power9 Yes
  Power8 Yes
  Power7 Yes
@@ -62,6 +65,7 @@ PPC970 No
  == 
  CPUTransactional Memory
  == 
+Power10No  (* see Power ISA v3.1 Appendix A.)
  Power9 Yes (* see transactional_memory.txt)
  Power8 Yes
  Power7 No



[PATCH v5 23/23] powerpc/book3s64/pkeys: Optimize FTR_KUAP and FTR_KUEP disabled case

2020-08-26 Thread Aneesh Kumar K.V
If FTR_KUAP is disabled kernel will continue to run with the same AMR
value with which it was entered. Hence there is a high chance that
we can return without restoring the AMR value. This also helps the case
when applications are not using the pkey feature. In this case, different
applications will have the same AMR values and hence we can avoid restoring
AMR in this case too.

Also avoid isync() if not really needed.

Do the same for IAMR.

null-syscall benchmark results:

With smap/smep disabled:
Without patch:
957.95 ns2778.17 cycles
With patch:
858.38 ns2489.30 cycles

With smap/smep enabled:
Without patch:
1017.26 ns2950.36 cycles
With patch:
1021.51 ns2962.44 cycles

Signed-off-by: Aneesh Kumar K.V 
---
 arch/powerpc/include/asm/book3s/64/kup.h | 61 +---
 arch/powerpc/kernel/entry_64.S   |  2 +-
 arch/powerpc/kernel/syscall_64.c | 12 +++--
 3 files changed, 65 insertions(+), 10 deletions(-)

diff --git a/arch/powerpc/include/asm/book3s/64/kup.h 
b/arch/powerpc/include/asm/book3s/64/kup.h
index 34a412d2a65b..d71e9a958eb5 100644
--- a/arch/powerpc/include/asm/book3s/64/kup.h
+++ b/arch/powerpc/include/asm/book3s/64/kup.h
@@ -12,28 +12,54 @@
 
 #ifdef __ASSEMBLY__
 
-.macro kuap_restore_user_amr gpr1
+.macro kuap_restore_user_amr gpr1, gpr2
 #if defined(CONFIG_PPC_PKEY)
BEGIN_MMU_FTR_SECTION_NESTED(67)
+   b   100f  // skip_restore_amr
+   END_MMU_FTR_SECTION_NESTED_IFCLR(MMU_FTR_PKEY, 67)
/*
 * AMR and IAMR are going to be different when
 * returning to userspace.
 */
ld  \gpr1, STACK_REGS_KUAP(r1)
+
+   /*
+* If kuap feature is not enabled, do the mtspr
+* only if AMR value is different.
+*/
+   BEGIN_MMU_FTR_SECTION_NESTED(68)
+   mfspr   \gpr2, SPRN_AMR
+   cmpd\gpr1, \gpr2
+   beq 99f
+   END_MMU_FTR_SECTION_NESTED_IFCLR(MMU_FTR_KUAP, 68)
+
isync
mtspr   SPRN_AMR, \gpr1
+99:
/*
 * Restore IAMR only when returning to userspace
 */
ld  \gpr1, STACK_REGS_KUEP(r1)
+
+   /*
+* If kuep feature is not enabled, do the mtspr
+* only if IAMR value is different.
+*/
+   BEGIN_MMU_FTR_SECTION_NESTED(69)
+   mfspr   \gpr2, SPRN_IAMR
+   cmpd\gpr1, \gpr2
+   beq 100f
+   END_MMU_FTR_SECTION_NESTED_IFCLR(MMU_FTR_KUEP, 69)
+
+   isync
mtspr   SPRN_IAMR, \gpr1
 
+100: //skip_restore_amr
/* No isync required, see kuap_restore_user_amr() */
-   END_MMU_FTR_SECTION_NESTED_IFSET(MMU_FTR_PKEY, 67)
 #endif
 .endm
 
-.macro kuap_restore_kernel_amr gpr1, gpr2
+.macro kuap_restore_kernel_amr gpr1, gpr2
 #if defined(CONFIG_PPC_PKEY)
 
BEGIN_MMU_FTR_SECTION_NESTED(67)
@@ -190,18 +216,41 @@ static inline u64 current_thread_iamr(void)
 
 static inline void kuap_restore_user_amr(struct pt_regs *regs)
 {
+   bool restore_amr = false, restore_iamr = false;
+   unsigned long amr, iamr;
+
if (!mmu_has_feature(MMU_FTR_PKEY))
return;
 
-   isync();
-   mtspr(SPRN_AMR, regs->kuap);
-   mtspr(SPRN_IAMR, regs->kuep);
+   if (!mmu_has_feature(MMU_FTR_KUAP)) {
+   amr = mfspr(SPRN_AMR);
+   if (amr != regs->kuap)
+   restore_amr = true;
+   } else
+   restore_amr = true;
+
+   if (!mmu_has_feature(MMU_FTR_KUEP)) {
+   iamr = mfspr(SPRN_IAMR);
+   if (iamr != regs->kuep)
+   restore_iamr = true;
+   } else
+   restore_iamr = true;
+
+
+   if (restore_amr || restore_iamr) {
+   isync();
+   if (restore_amr)
+   mtspr(SPRN_AMR, regs->kuap);
+   if (restore_iamr)
+   mtspr(SPRN_IAMR, regs->kuep);
+   }
/*
 * No isync required here because we are about to rfi
 * back to previous context before any user accesses
 * would be made, which is a CSI.
 */
 }
+
 static inline void kuap_restore_kernel_amr(struct pt_regs *regs,
   unsigned long amr)
 {
diff --git a/arch/powerpc/kernel/entry_64.S b/arch/powerpc/kernel/entry_64.S
index 68171689db5d..ac6c84a53ff8 100644
--- a/arch/powerpc/kernel/entry_64.S
+++ b/arch/powerpc/kernel/entry_64.S
@@ -667,7 +667,7 @@ _ASM_NOKPROBE_SYMBOL(interrupt_return)
bne-.Lrestore_nvgprs
 
 .Lfast_user_interrupt_return_amr:
-   kuap_restore_user_amr r3
+   kuap_restore_user_amr r3, r4
 .Lfast_user_interrupt_return:
ld  r11,_NIP(r1)
ld  r12,_MSR(r1)
diff --git a/arch/powerpc/kernel/syscall_64.c b/arch/powerpc/kernel/syscall_64.c
index e49d604b811b..945a14e41898 100644
--- a/arch/powerpc/kernel/syscall_64.c
+++ b/arch/powerpc/kernel/syscall_64.c
@@ -38,6 +38,7 @@ notrace long system_call_exception(long r3, long 

[PATCH v5 22/23] powerpc/book3s64/hash/kup: Don't hardcode kup key

2020-08-26 Thread Aneesh Kumar K.V
Make KUAP/KUEP key a variable and also check whether the platform
limit the max key such that we can't use the key for KUAP/KEUP.

Signed-off-by: Aneesh Kumar K.V 
---
 .../powerpc/include/asm/book3s/64/hash-pkey.h | 22 +---
 arch/powerpc/include/asm/book3s/64/pkeys.h|  1 +
 arch/powerpc/mm/book3s64/pkeys.c  | 53 ---
 3 files changed, 49 insertions(+), 27 deletions(-)

diff --git a/arch/powerpc/include/asm/book3s/64/hash-pkey.h 
b/arch/powerpc/include/asm/book3s/64/hash-pkey.h
index 9f44e208f036..ff9907c72ee3 100644
--- a/arch/powerpc/include/asm/book3s/64/hash-pkey.h
+++ b/arch/powerpc/include/asm/book3s/64/hash-pkey.h
@@ -2,9 +2,7 @@
 #ifndef _ASM_POWERPC_BOOK3S_64_HASH_PKEY_H
 #define _ASM_POWERPC_BOOK3S_64_HASH_PKEY_H
 
-/*  We use key 3 for KERNEL */
-#define HASH_DEFAULT_KERNEL_KEY (HPTE_R_KEY_BIT0 | HPTE_R_KEY_BIT1)
-
+u64 pte_to_hpte_pkey_bits(u64 pteflags, unsigned long flags);
 static inline u64 hash__vmflag_to_pte_pkey_bits(u64 vm_flags)
 {
return (((vm_flags & VM_PKEY_BIT0) ? H_PTE_PKEY_BIT0 : 0x0UL) |
@@ -14,24 +12,6 @@ static inline u64 hash__vmflag_to_pte_pkey_bits(u64 vm_flags)
((vm_flags & VM_PKEY_BIT4) ? H_PTE_PKEY_BIT4 : 0x0UL));
 }
 
-static inline u64 pte_to_hpte_pkey_bits(u64 pteflags, unsigned long flags)
-{
-   unsigned long pte_pkey;
-
-   pte_pkey = (((pteflags & H_PTE_PKEY_BIT4) ? HPTE_R_KEY_BIT4 : 0x0UL) |
-   ((pteflags & H_PTE_PKEY_BIT3) ? HPTE_R_KEY_BIT3 : 0x0UL) |
-   ((pteflags & H_PTE_PKEY_BIT2) ? HPTE_R_KEY_BIT2 : 0x0UL) |
-   ((pteflags & H_PTE_PKEY_BIT1) ? HPTE_R_KEY_BIT1 : 0x0UL) |
-   ((pteflags & H_PTE_PKEY_BIT0) ? HPTE_R_KEY_BIT0 : 0x0UL));
-
-   if (mmu_has_feature(MMU_FTR_KUAP) || mmu_has_feature(MMU_FTR_KUEP)) {
-   if ((pte_pkey == 0) && (flags & HPTE_USE_KERNEL_KEY))
-   return HASH_DEFAULT_KERNEL_KEY;
-   }
-
-   return pte_pkey;
-}
-
 static inline u16 hash__pte_to_pkey_bits(u64 pteflags)
 {
return (((pteflags & H_PTE_PKEY_BIT4) ? 0x10 : 0x0UL) |
diff --git a/arch/powerpc/include/asm/book3s/64/pkeys.h 
b/arch/powerpc/include/asm/book3s/64/pkeys.h
index 3b8640498f5b..a2b6c4a7275f 100644
--- a/arch/powerpc/include/asm/book3s/64/pkeys.h
+++ b/arch/powerpc/include/asm/book3s/64/pkeys.h
@@ -8,6 +8,7 @@
 extern u64 __ro_after_init default_uamor;
 extern u64 __ro_after_init default_amr;
 extern u64 __ro_after_init default_iamr;
+extern int kup_key;
 
 static inline u64 vmflag_to_pte_pkey_bits(u64 vm_flags)
 {
diff --git a/arch/powerpc/mm/book3s64/pkeys.c b/arch/powerpc/mm/book3s64/pkeys.c
index b862d5cd78ff..cb1d7d39e801 100644
--- a/arch/powerpc/mm/book3s64/pkeys.c
+++ b/arch/powerpc/mm/book3s64/pkeys.c
@@ -37,7 +37,10 @@ u64 default_uamor __ro_after_init;
  */
 static int execute_only_key = 2;
 static bool pkey_execute_disable_supported;
-
+/*
+ * key used to implement KUAP/KUEP with hash translation.
+ */
+int kup_key = 3;
 
 #define AMR_BITS_PER_PKEY 2
 #define AMR_RD_BIT 0x1UL
@@ -185,6 +188,25 @@ void __init pkey_early_init_devtree(void)
default_uamor &= ~(0x3ul << pkeyshift(execute_only_key));
}
 
+   if (unlikely(num_pkey <= kup_key)) {
+   /*
+* Insufficient number of keys to support
+* KUAP/KUEP feature.
+*/
+   kup_key = -1;
+   } else {
+   /*  handle key which is used by kernel for KAUP */
+   reserved_allocation_mask |= (0x1 << kup_key);
+   /*
+* Mark access for kup_key in default amr so that
+* we continue to operate with that AMR in
+* copy_to/from_user().
+*/
+   default_amr   &= ~(0x3ul << pkeyshift(kup_key));
+   default_iamr  &= ~(0x1ul << pkeyshift(kup_key));
+   default_uamor &= ~(0x3ul << pkeyshift(kup_key));
+   }
+
/*
 * Allow access for only key 0. And prevent any other modification.
 */
@@ -205,9 +227,6 @@ void __init pkey_early_init_devtree(void)
reserved_allocation_mask |= (0x1 << 1);
default_uamor &= ~(0x3ul << pkeyshift(1));
 
-   /*  handle key 3 which is used by kernel for KAUP */
-   reserved_allocation_mask |= (0x1 << 3);
-   default_uamor &= ~(0x3ul << pkeyshift(3));
 
/*
 * Prevent the usage of OS reserved keys. Update UAMOR
@@ -236,7 +255,7 @@ void __init pkey_early_init_devtree(void)
 #ifdef CONFIG_PPC_KUEP
 void __init setup_kuep(bool disabled)
 {
-   if (disabled)
+   if (disabled || kup_key == -1)
return;
/*
 * On hash if PKEY feature is not enabled, disable KUAP too.
@@ -262,7 +281,7 @@ void __init setup_kuep(bool disabled)
 #ifdef CONFIG_PPC_KUAP
 void __init setup_kuap(bool disabled)
 {
-   if (disabled)
+   if (disabled || kup_key == -1)
return;
/*
 * On 

[PATCH v5 21/23] powerpc/book3s64/hash/kuep: Enable KUEP on hash

2020-08-26 Thread Aneesh Kumar K.V
Signed-off-by: Aneesh Kumar K.V 
---
 arch/powerpc/mm/book3s64/pkeys.c | 7 ++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/arch/powerpc/mm/book3s64/pkeys.c b/arch/powerpc/mm/book3s64/pkeys.c
index 16ea0b2f0ea5..b862d5cd78ff 100644
--- a/arch/powerpc/mm/book3s64/pkeys.c
+++ b/arch/powerpc/mm/book3s64/pkeys.c
@@ -236,7 +236,12 @@ void __init pkey_early_init_devtree(void)
 #ifdef CONFIG_PPC_KUEP
 void __init setup_kuep(bool disabled)
 {
-   if (disabled || !early_radix_enabled())
+   if (disabled)
+   return;
+   /*
+* On hash if PKEY feature is not enabled, disable KUAP too.
+*/
+   if (!early_radix_enabled() && !early_mmu_has_feature(MMU_FTR_PKEY))
return;
 
if (smp_processor_id() == boot_cpuid) {
-- 
2.26.2



[PATCH v5 20/23] powerpc/book3s64/hash/kuap: Enable kuap on hash

2020-08-26 Thread Aneesh Kumar K.V
Signed-off-by: Aneesh Kumar K.V 
---
 arch/powerpc/mm/book3s64/pkeys.c | 7 ++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/arch/powerpc/mm/book3s64/pkeys.c b/arch/powerpc/mm/book3s64/pkeys.c
index 391230f93da2..16ea0b2f0ea5 100644
--- a/arch/powerpc/mm/book3s64/pkeys.c
+++ b/arch/powerpc/mm/book3s64/pkeys.c
@@ -257,7 +257,12 @@ void __init setup_kuep(bool disabled)
 #ifdef CONFIG_PPC_KUAP
 void __init setup_kuap(bool disabled)
 {
-   if (disabled || !early_radix_enabled())
+   if (disabled)
+   return;
+   /*
+* On hash if PKEY feature is not enabled, disable KUAP too.
+*/
+   if (!early_radix_enabled() && !early_mmu_has_feature(MMU_FTR_PKEY))
return;
 
if (smp_processor_id() == boot_cpuid) {
-- 
2.26.2



[PATCH v5 19/23] powerpc/book3s64/kuep: Use Key 3 to implement KUEP with hash translation.

2020-08-26 Thread Aneesh Kumar K.V
Radix use IAMR Key 0 and hash translation use IAMR key 3.

Signed-off-by: Aneesh Kumar K.V 
---
 arch/powerpc/include/asm/book3s/64/kup.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/powerpc/include/asm/book3s/64/kup.h 
b/arch/powerpc/include/asm/book3s/64/kup.h
index f326be9e0db7..34a412d2a65b 100644
--- a/arch/powerpc/include/asm/book3s/64/kup.h
+++ b/arch/powerpc/include/asm/book3s/64/kup.h
@@ -7,7 +7,7 @@
 
 #define AMR_KUAP_BLOCK_READUL(0x5455)
 #define AMR_KUAP_BLOCK_WRITE   UL(0xa8aa)
-#define AMR_KUEP_BLOCKED   (1UL << 62)
+#define AMR_KUEP_BLOCKED   UL(0x5455)
 #define AMR_KUAP_BLOCKED   (AMR_KUAP_BLOCK_READ | AMR_KUAP_BLOCK_WRITE)
 
 #ifdef __ASSEMBLY__
-- 
2.26.2



[PATCH v5 18/23] powerpc/book3s64/kuap: Use Key 3 to implement KUAP with hash translation.

2020-08-26 Thread Aneesh Kumar K.V
Radix use AMR Key 0 and hash translation use AMR key 3.

Signed-off-by: Aneesh Kumar K.V 
---
 arch/powerpc/include/asm/book3s/64/kup.h | 9 -
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/arch/powerpc/include/asm/book3s/64/kup.h 
b/arch/powerpc/include/asm/book3s/64/kup.h
index 9c85e4397b2d..f326be9e0db7 100644
--- a/arch/powerpc/include/asm/book3s/64/kup.h
+++ b/arch/powerpc/include/asm/book3s/64/kup.h
@@ -5,11 +5,10 @@
 #include 
 #include 
 
-#define AMR_KUAP_BLOCK_READUL(0x4000)
-#define AMR_KUAP_BLOCK_WRITE   UL(0x8000)
+#define AMR_KUAP_BLOCK_READUL(0x5455)
+#define AMR_KUAP_BLOCK_WRITE   UL(0xa8aa)
 #define AMR_KUEP_BLOCKED   (1UL << 62)
 #define AMR_KUAP_BLOCKED   (AMR_KUAP_BLOCK_READ | AMR_KUAP_BLOCK_WRITE)
-#define AMR_KUAP_SHIFT 62
 
 #ifdef __ASSEMBLY__
 
@@ -61,8 +60,8 @@
 #ifdef CONFIG_PPC_KUAP_DEBUG
BEGIN_MMU_FTR_SECTION_NESTED(67)
mfspr   \gpr1, SPRN_AMR
-   li  \gpr2, (AMR_KUAP_BLOCKED >> AMR_KUAP_SHIFT)
-   sldi\gpr2, \gpr2, AMR_KUAP_SHIFT
+   /* Prevent access to userspace using any key values */
+   LOAD_REG_IMMEDIATE(\gpr2, AMR_KUAP_BLOCKED)
 999:   tdne\gpr1, \gpr2
EMIT_BUG_ENTRY 999b, __FILE__, __LINE__, (BUGFLAG_WARNING | 
BUGFLAG_ONCE)
END_MMU_FTR_SECTION_NESTED_IFSET(MMU_FTR_KUAP, 67)
-- 
2.26.2



[PATCH v5 16/23] powerpc/book3s64/kuap: Restrict access to userspace based on userspace AMR

2020-08-26 Thread Aneesh Kumar K.V
If an application has configured address protection such that read/write is
denied using pkey even the kernel should receive a FAULT on accessing the same.

This patch use user AMR value stored in pt_regs.kuap to achieve the same.

Signed-off-by: Aneesh Kumar K.V 
---
 arch/powerpc/include/asm/book3s/64/kup.h | 12 +---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/arch/powerpc/include/asm/book3s/64/kup.h 
b/arch/powerpc/include/asm/book3s/64/kup.h
index 4e1d666032f6..878cd84922d8 100644
--- a/arch/powerpc/include/asm/book3s/64/kup.h
+++ b/arch/powerpc/include/asm/book3s/64/kup.h
@@ -292,14 +292,20 @@ static inline void set_kuap(unsigned long value)
 static __always_inline void allow_user_access(void __user *to, const void 
__user *from,
  unsigned long size, unsigned long 
dir)
 {
+   unsigned long thread_amr = 0;
+
// This is written so we can resolve to a single case at build time
BUILD_BUG_ON(!__builtin_constant_p(dir));
+
+   if (mmu_has_feature(MMU_FTR_PKEY))
+   thread_amr = current_thread_amr();
+
if (dir == KUAP_READ)
-   set_kuap(AMR_KUAP_BLOCK_WRITE);
+   set_kuap(thread_amr | AMR_KUAP_BLOCK_WRITE);
else if (dir == KUAP_WRITE)
-   set_kuap(AMR_KUAP_BLOCK_READ);
+   set_kuap(thread_amr | AMR_KUAP_BLOCK_READ);
else if (dir == KUAP_READ_WRITE)
-   set_kuap(0);
+   set_kuap(thread_amr);
else
BUILD_BUG();
 }
-- 
2.26.2



[PATCH v5 17/23] powerpc/book3s64/kuap: Improve error reporting with KUAP

2020-08-26 Thread Aneesh Kumar K.V
With hash translation use DSISR_KEYFAULT to identify a wrong access.
With Radix we look at the AMR value and type of fault.

Signed-off-by: Aneesh Kumar K.V 
---
 arch/powerpc/include/asm/book3s/32/kup.h |  4 +--
 arch/powerpc/include/asm/book3s/64/kup.h | 27 
 arch/powerpc/include/asm/kup.h   |  4 +--
 arch/powerpc/include/asm/nohash/32/kup-8xx.h |  4 +--
 arch/powerpc/mm/fault.c  |  2 +-
 5 files changed, 29 insertions(+), 12 deletions(-)

diff --git a/arch/powerpc/include/asm/book3s/32/kup.h 
b/arch/powerpc/include/asm/book3s/32/kup.h
index 32fd4452e960..b18cd931e325 100644
--- a/arch/powerpc/include/asm/book3s/32/kup.h
+++ b/arch/powerpc/include/asm/book3s/32/kup.h
@@ -177,8 +177,8 @@ static inline void restore_user_access(unsigned long flags)
allow_user_access(to, to, end - addr, KUAP_READ_WRITE);
 }
 
-static inline bool
-bad_kuap_fault(struct pt_regs *regs, unsigned long address, bool is_write)
+static inline bool bad_kuap_fault(struct pt_regs *regs, unsigned long address,
+ bool is_write, unsigned long error_code)
 {
unsigned long begin = regs->kuap & 0xf000;
unsigned long end = regs->kuap << 28;
diff --git a/arch/powerpc/include/asm/book3s/64/kup.h 
b/arch/powerpc/include/asm/book3s/64/kup.h
index 878cd84922d8..9c85e4397b2d 100644
--- a/arch/powerpc/include/asm/book3s/64/kup.h
+++ b/arch/powerpc/include/asm/book3s/64/kup.h
@@ -330,12 +330,29 @@ static inline void restore_user_access(unsigned long 
flags)
set_kuap(flags);
 }
 
-static inline bool
-bad_kuap_fault(struct pt_regs *regs, unsigned long address, bool is_write)
+#define RADIX_KUAP_BLOCK_READ  UL(0x4000)
+#define RADIX_KUAP_BLOCK_WRITE UL(0x8000)
+
+static inline bool bad_kuap_fault(struct pt_regs *regs, unsigned long address,
+ bool is_write, unsigned long error_code)
 {
-   return WARN(mmu_has_feature(MMU_FTR_KUAP) &&
-   (regs->kuap & (is_write ? AMR_KUAP_BLOCK_WRITE : 
AMR_KUAP_BLOCK_READ)),
-   "Bug: %s fault blocked by AMR!", is_write ? "Write" : 
"Read");
+   if (!mmu_has_feature(MMU_FTR_KUAP))
+   return false;
+
+   if (radix_enabled()) {
+   /*
+* Will be a storage protection fault.
+* Only check the details of AMR[0]
+*/
+   return WARN((regs->kuap & (is_write ? RADIX_KUAP_BLOCK_WRITE : 
RADIX_KUAP_BLOCK_READ)),
+   "Bug: %s fault blocked by AMR!", is_write ? "Write" 
: "Read");
+   }
+   /*
+* We don't want to WARN here because userspace can setup
+* keys such that a kernel access to user address can cause
+* fault
+*/
+   return !!(error_code & DSISR_KEYFAULT);
 }
 #endif /* CONFIG_PPC_KUAP */
 
diff --git a/arch/powerpc/include/asm/kup.h b/arch/powerpc/include/asm/kup.h
index 6c3ee976ee15..8f5e2d820723 100644
--- a/arch/powerpc/include/asm/kup.h
+++ b/arch/powerpc/include/asm/kup.h
@@ -67,8 +67,8 @@ static inline void prevent_user_access(void __user *to, const 
void __user *from,
   unsigned long size, unsigned long dir) { 
}
 static inline unsigned long prevent_user_access_return(void) { return 0UL; }
 static inline void restore_user_access(unsigned long flags) { }
-static inline bool
-bad_kuap_fault(struct pt_regs *regs, unsigned long address, bool is_write)
+static inline bool bad_kuap_fault(struct pt_regs *regs, unsigned long address,
+ bool is_write, unsigned long error_code)
 {
return false;
 }
diff --git a/arch/powerpc/include/asm/nohash/32/kup-8xx.h 
b/arch/powerpc/include/asm/nohash/32/kup-8xx.h
index 85ed2390fb99..c401e4e404d4 100644
--- a/arch/powerpc/include/asm/nohash/32/kup-8xx.h
+++ b/arch/powerpc/include/asm/nohash/32/kup-8xx.h
@@ -60,8 +60,8 @@ static inline void restore_user_access(unsigned long flags)
mtspr(SPRN_MD_AP, flags);
 }
 
-static inline bool
-bad_kuap_fault(struct pt_regs *regs, unsigned long address, bool is_write)
+static inline bool bad_kuap_fault(struct pt_regs *regs, unsigned long address,
+ bool is_write, unsigned long error_code)
 {
return WARN(!((regs->kuap ^ MD_APG_KUAP) & 0xf000),
"Bug: fault blocked by AP register !");
diff --git a/arch/powerpc/mm/fault.c b/arch/powerpc/mm/fault.c
index 0add963a849b..c91621df0c61 100644
--- a/arch/powerpc/mm/fault.c
+++ b/arch/powerpc/mm/fault.c
@@ -227,7 +227,7 @@ static bool bad_kernel_fault(struct pt_regs *regs, unsigned 
long error_code,
 
// Read/write fault in a valid region (the exception table search passed
// above), but blocked by KUAP is bad, it can never succeed.
-   if (bad_kuap_fault(regs, address, is_write))
+   if (bad_kuap_fault(regs, address, is_write, error_code))

[PATCH v5 15/23] powerpc/book3s64/pkeys: Don't update SPRN_AMR when in kernel mode.

2020-08-26 Thread Aneesh Kumar K.V
Now that kernel correctly store/restore userspace AMR/IAMR values, avoid
manipulating AMR and IAMR from the kernel on behalf of userspace.

Signed-off-by: Aneesh Kumar K.V 
---
 arch/powerpc/include/asm/book3s/64/kup.h | 18 
 arch/powerpc/include/asm/processor.h |  4 --
 arch/powerpc/kernel/process.c|  4 --
 arch/powerpc/kernel/traps.c  |  6 ---
 arch/powerpc/mm/book3s64/pkeys.c | 57 +---
 5 files changed, 28 insertions(+), 61 deletions(-)

diff --git a/arch/powerpc/include/asm/book3s/64/kup.h 
b/arch/powerpc/include/asm/book3s/64/kup.h
index 3f5b97b2a3d8..4e1d666032f6 100644
--- a/arch/powerpc/include/asm/book3s/64/kup.h
+++ b/arch/powerpc/include/asm/book3s/64/kup.h
@@ -171,6 +171,24 @@
 #include 
 #include 
 
+/*
+ * For kernel thread that doesn't have thread.regs return
+ * default AMR/IAMR values.
+ */
+static inline u64 current_thread_amr(void)
+{
+   if (current->thread.regs)
+   return current->thread.regs->kuap;
+   return AMR_KUAP_BLOCKED;
+}
+
+static inline u64 current_thread_iamr(void)
+{
+   if (current->thread.regs)
+   return current->thread.regs->kuep;
+   return AMR_KUEP_BLOCKED;
+}
+
 static inline void kuap_restore_user_amr(struct pt_regs *regs)
 {
if (!mmu_has_feature(MMU_FTR_PKEY))
diff --git a/arch/powerpc/include/asm/processor.h 
b/arch/powerpc/include/asm/processor.h
index ed0d633ab5aa..8adf44a7e54f 100644
--- a/arch/powerpc/include/asm/processor.h
+++ b/arch/powerpc/include/asm/processor.h
@@ -234,10 +234,6 @@ struct thread_struct {
struct thread_vr_state ckvr_state; /* Checkpointed VR state */
unsigned long   ckvrsave; /* Checkpointed VRSAVE */
 #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
-#ifdef CONFIG_PPC_MEM_KEYS
-   unsigned long   amr;
-   unsigned long   iamr;
-#endif
 #ifdef CONFIG_KVM_BOOK3S_32_HANDLER
void*   kvm_shadow_vcpu; /* KVM internal data */
 #endif /* CONFIG_KVM_BOOK3S_32_HANDLER */
diff --git a/arch/powerpc/kernel/process.c b/arch/powerpc/kernel/process.c
index 75fd30e023bd..c8f57afba3a0 100644
--- a/arch/powerpc/kernel/process.c
+++ b/arch/powerpc/kernel/process.c
@@ -603,7 +603,6 @@ static void save_all(struct task_struct *tsk)
__giveup_spe(tsk);
 
msr_check_and_clear(msr_all_available);
-   thread_pkey_regs_save(>thread);
 }
 
 void flush_all_to_thread(struct task_struct *tsk)
@@ -1127,8 +1126,6 @@ static inline void save_sprs(struct thread_struct *t)
t->tar = mfspr(SPRN_TAR);
}
 #endif
-
-   thread_pkey_regs_save(t);
 }
 
 static inline void restore_sprs(struct thread_struct *old_thread,
@@ -1169,7 +1166,6 @@ static inline void restore_sprs(struct thread_struct 
*old_thread,
mtspr(SPRN_TIDR, new_thread->tidr);
 #endif
 
-   thread_pkey_regs_restore(new_thread, old_thread);
 }
 
 struct task_struct *__switch_to(struct task_struct *prev,
diff --git a/arch/powerpc/kernel/traps.c b/arch/powerpc/kernel/traps.c
index d1ebe152f210..5bda54454a2d 100644
--- a/arch/powerpc/kernel/traps.c
+++ b/arch/powerpc/kernel/traps.c
@@ -347,12 +347,6 @@ static bool exception_common(int signr, struct pt_regs 
*regs, int code,
 
current->thread.trap_nr = code;
 
-   /*
-* Save all the pkey registers AMR/IAMR/UAMOR. Eg: Core dumps need
-* to capture the content, if the task gets killed.
-*/
-   thread_pkey_regs_save(>thread);
-
return true;
 }
 
diff --git a/arch/powerpc/mm/book3s64/pkeys.c b/arch/powerpc/mm/book3s64/pkeys.c
index f47d11f2743d..391230f93da2 100644
--- a/arch/powerpc/mm/book3s64/pkeys.c
+++ b/arch/powerpc/mm/book3s64/pkeys.c
@@ -273,30 +273,17 @@ void __init setup_kuap(bool disabled)
 }
 #endif
 
-static inline u64 read_amr(void)
+static inline void update_current_thread_amr(u64 value)
 {
-   return mfspr(SPRN_AMR);
+   current->thread.regs->kuap = value;
 }
 
-static inline void write_amr(u64 value)
-{
-   mtspr(SPRN_AMR, value);
-}
-
-static inline u64 read_iamr(void)
-{
-   if (!likely(pkey_execute_disable_supported))
-   return 0x0UL;
-
-   return mfspr(SPRN_IAMR);
-}
-
-static inline void write_iamr(u64 value)
+static inline void update_current_thread_iamr(u64 value)
 {
if (!likely(pkey_execute_disable_supported))
return;
 
-   mtspr(SPRN_IAMR, value);
+   current->thread.regs->kuep = value;
 }
 
 #ifdef CONFIG_PPC_MEM_KEYS
@@ -311,17 +298,17 @@ void pkey_mm_init(struct mm_struct *mm)
 static inline void init_amr(int pkey, u8 init_bits)
 {
u64 new_amr_bits = (((u64)init_bits & 0x3UL) << pkeyshift(pkey));
-   u64 old_amr = read_amr() & ~((u64)(0x3ul) << pkeyshift(pkey));
+   u64 old_amr = current_thread_amr() & ~((u64)(0x3ul) << pkeyshift(pkey));
 
-   write_amr(old_amr | new_amr_bits);
+   update_current_thread_amr(old_amr | new_amr_bits);
 }
 
 static inline void init_iamr(int pkey, u8 init_bits)
 {
 

[PATCH v5 14/23] powerpc/ptrace-view: Use pt_regs values instead of thread_struct based one.

2020-08-26 Thread Aneesh Kumar K.V
We will remove thread.amr/iamr/uamor in a later patch

Signed-off-by: Aneesh Kumar K.V 
---
 arch/powerpc/kernel/ptrace/ptrace-view.c | 7 ---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/arch/powerpc/kernel/ptrace/ptrace-view.c 
b/arch/powerpc/kernel/ptrace/ptrace-view.c
index 7e6478e7ed07..c719e29aff76 100644
--- a/arch/powerpc/kernel/ptrace/ptrace-view.c
+++ b/arch/powerpc/kernel/ptrace/ptrace-view.c
@@ -470,12 +470,12 @@ static int pkey_active(struct task_struct *target, const 
struct user_regset *reg
 static int pkey_get(struct task_struct *target, const struct user_regset 
*regset,
struct membuf to)
 {
-   BUILD_BUG_ON(TSO(amr) + sizeof(unsigned long) != TSO(iamr));
 
if (!arch_pkeys_enabled())
return -ENODEV;
 
-   membuf_write(, >thread.amr, 2 * sizeof(unsigned long));
+   membuf_store(, target->thread.regs->kuap);
+   membuf_store(, target->thread.regs->kuep);
return membuf_store(, default_uamor);
 }
 
@@ -508,7 +508,8 @@ static int pkey_set(struct task_struct *target, const 
struct user_regset *regset
 * Pick the AMR values for the keys that kernel is using. This
 * will be indicated by the ~default_uamor bits.
 */
-   target->thread.amr = (new_amr & default_uamor) | (target->thread.amr & 
~default_uamor);
+   target->thread.regs->kuap = (new_amr & default_uamor) |
+   (target->thread.regs->kuap & ~default_uamor);
 
return 0;
 }
-- 
2.26.2



[PATCH v5 13/23] powerpc/book3s64/pkeys: Reset userspace AMR correctly on exec

2020-08-26 Thread Aneesh Kumar K.V
On fork, we inherit from the parent and on exec, we should switch to 
default_amr values.

Also, avoid changing the AMR register value within the kernel. The kernel now 
runs with
different AMR values.

Signed-off-by: Aneesh Kumar K.V 
---
 arch/powerpc/include/asm/book3s/64/pkeys.h |  2 ++
 arch/powerpc/kernel/process.c  |  6 +-
 arch/powerpc/mm/book3s64/pkeys.c   | 16 ++--
 3 files changed, 9 insertions(+), 15 deletions(-)

diff --git a/arch/powerpc/include/asm/book3s/64/pkeys.h 
b/arch/powerpc/include/asm/book3s/64/pkeys.h
index b7d9f4267bcd..3b8640498f5b 100644
--- a/arch/powerpc/include/asm/book3s/64/pkeys.h
+++ b/arch/powerpc/include/asm/book3s/64/pkeys.h
@@ -6,6 +6,8 @@
 #include 
 
 extern u64 __ro_after_init default_uamor;
+extern u64 __ro_after_init default_amr;
+extern u64 __ro_after_init default_iamr;
 
 static inline u64 vmflag_to_pte_pkey_bits(u64 vm_flags)
 {
diff --git a/arch/powerpc/kernel/process.c b/arch/powerpc/kernel/process.c
index 725fd1bed2b6..75fd30e023bd 100644
--- a/arch/powerpc/kernel/process.c
+++ b/arch/powerpc/kernel/process.c
@@ -1506,6 +1506,11 @@ void arch_setup_new_exec(void)
current->thread.regs = regs - 1;
}
 
+#ifdef CONFIG_PPC_MEM_KEYS
+   current->thread.regs->kuap  = default_amr;
+   current->thread.regs->kuep  = default_iamr;
+#endif
+
 }
 #else
 void arch_setup_new_exec(void)
@@ -1866,7 +1871,6 @@ void start_thread(struct pt_regs *regs, unsigned long 
start, unsigned long sp)
current->thread.load_tm = 0;
 #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
 
-   thread_pkey_regs_init(>thread);
 }
 EXPORT_SYMBOL(start_thread);
 
diff --git a/arch/powerpc/mm/book3s64/pkeys.c b/arch/powerpc/mm/book3s64/pkeys.c
index 640f090b9f9d..f47d11f2743d 100644
--- a/arch/powerpc/mm/book3s64/pkeys.c
+++ b/arch/powerpc/mm/book3s64/pkeys.c
@@ -28,8 +28,8 @@ static u32 initial_allocation_mask __ro_after_init;
  * Even if we allocate keys with sys_pkey_alloc(), we need to make sure
  * other thread still find the access denied using the same keys.
  */
-static u64 default_amr = ~0x0UL;
-static u64 default_iamr = 0xUL;
+u64 default_amr __ro_after_init  = ~0x0UL;
+u64 default_iamr __ro_after_init = 0xUL;
 u64 default_uamor __ro_after_init;
 /*
  * Key used to implement PROT_EXEC mmap. Denies READ/WRITE
@@ -388,18 +388,6 @@ void thread_pkey_regs_restore(struct thread_struct 
*new_thread,
write_iamr(new_thread->iamr);
 }
 
-void thread_pkey_regs_init(struct thread_struct *thread)
-{
-   if (!mmu_has_feature(MMU_FTR_PKEY))
-   return;
-
-   thread->amr   = default_amr;
-   thread->iamr  = default_iamr;
-
-   write_amr(default_amr);
-   write_iamr(default_iamr);
-}
-
 int execute_only_pkey(struct mm_struct *mm)
 {
return mm->context.execute_only_pkey;
-- 
2.26.2



[PATCH v5 12/23] powerpc/book3s64/pkeys: Inherit correctly on fork.

2020-08-26 Thread Aneesh Kumar K.V
Child thread.kuap value is inherited from the parent in copy_thread_tls. We 
still
need to make sure when the child returns from a fork in the kernel we start 
with the kernel
default AMR value.

Signed-off-by: Aneesh Kumar K.V 
---
 arch/powerpc/kernel/process.c | 9 +
 1 file changed, 9 insertions(+)

diff --git a/arch/powerpc/kernel/process.c b/arch/powerpc/kernel/process.c
index 4633924ea77f..725fd1bed2b6 100644
--- a/arch/powerpc/kernel/process.c
+++ b/arch/powerpc/kernel/process.c
@@ -1732,6 +1732,15 @@ int copy_thread(unsigned long clone_flags, unsigned long 
usp,
childregs->ppr = DEFAULT_PPR;
 
p->thread.tidr = 0;
+#endif
+   /*
+* Run with the current AMR value of the kernel
+*/
+#if defined(CONFIG_PPC_MEM_KEYS)
+   if (mmu_has_feature(MMU_FTR_KUAP))
+   kregs->kuap = AMR_KUAP_BLOCKED;
+   if (mmu_has_feature(MMU_FTR_KUEP))
+   kregs->kuep = AMR_KUEP_BLOCKED;
 #endif
kregs->nip = ppc_function_entry(f);
return 0;
-- 
2.26.2



[PATCH v5 11/23] powerpc/book3s64/pkeys: Store/restore userspace AMR/IAMR correctly on entry and exit from kernel

2020-08-26 Thread Aneesh Kumar K.V
This prepare kernel to operate with a different value than userspace AMR/IAMR.
For this, AMR/IAMR need to be saved and restored on entry and return from the
kernel.

With KUAP we modify kernel AMR when accessing user address from the kernel
via copy_to/from_user interfaces. We don't need to modify IAMR value in
similar fashion.

If MMU_FTR_PKEY is enabled we need to save AMR/IAMR in pt_regs on entering
kernel from userspace. If not we can assume that AMR/IAMR is not modified
from userspace.

We need to save AMR if we have MMU_FTR_KUAP feature enabled and we are
interrupted within kernel. This is required so that if we get interrupted
within copy_to/from_user we continue with the right AMR value.

If we hae MMU_FTR_KUEP enabled we need to restore IAMR on return to userspace
beause kernel will be running with a different IAMR value.

Signed-off-by: Aneesh Kumar K.V 
---
 arch/powerpc/include/asm/book3s/64/kup.h | 177 ---
 arch/powerpc/include/asm/ptrace.h|   4 +-
 arch/powerpc/kernel/asm-offsets.c|   2 +
 arch/powerpc/kernel/entry_64.S   |   6 +-
 arch/powerpc/kernel/exceptions-64s.S |   4 +-
 arch/powerpc/kernel/syscall_64.c |  30 +++-
 6 files changed, 192 insertions(+), 31 deletions(-)

diff --git a/arch/powerpc/include/asm/book3s/64/kup.h 
b/arch/powerpc/include/asm/book3s/64/kup.h
index 5cec202dc42f..3f5b97b2a3d8 100644
--- a/arch/powerpc/include/asm/book3s/64/kup.h
+++ b/arch/powerpc/include/asm/book3s/64/kup.h
@@ -13,17 +13,46 @@
 
 #ifdef __ASSEMBLY__
 
-.macro kuap_restore_amrgpr1, gpr2
-#ifdef CONFIG_PPC_KUAP
+.macro kuap_restore_user_amr gpr1
+#if defined(CONFIG_PPC_PKEY)
BEGIN_MMU_FTR_SECTION_NESTED(67)
-   mfspr   \gpr1, SPRN_AMR
+   /*
+* AMR and IAMR are going to be different when
+* returning to userspace.
+*/
+   ld  \gpr1, STACK_REGS_KUAP(r1)
+   isync
+   mtspr   SPRN_AMR, \gpr1
+   /*
+* Restore IAMR only when returning to userspace
+*/
+   ld  \gpr1, STACK_REGS_KUEP(r1)
+   mtspr   SPRN_IAMR, \gpr1
+
+   /* No isync required, see kuap_restore_user_amr() */
+   END_MMU_FTR_SECTION_NESTED_IFSET(MMU_FTR_PKEY, 67)
+#endif
+.endm
+
+.macro kuap_restore_kernel_amr gpr1, gpr2
+#if defined(CONFIG_PPC_PKEY)
+
+   BEGIN_MMU_FTR_SECTION_NESTED(67)
+   /*
+* AMR is going to be mostly the same since we are
+* returning to the kernel. Compare and do a mtspr.
+*/
ld  \gpr2, STACK_REGS_KUAP(r1)
+   mfspr   \gpr1, SPRN_AMR
cmpd\gpr1, \gpr2
-   beq 998f
+   beq 100f
isync
mtspr   SPRN_AMR, \gpr2
-   /* No isync required, see kuap_restore_amr() */
-998:
+   /*
+* No isync required, see kuap_restore_amr()
+* No need to restore IAMR when returning to kernel space.
+*/
+100:
END_MMU_FTR_SECTION_NESTED_IFSET(MMU_FTR_KUAP, 67)
 #endif
 .endm
@@ -40,23 +69,98 @@
 #endif
 .endm
 
+/*
+ * if (pkey) {
+ *
+ * save AMR -> stack;
+ * if (kuap) {
+ * if (AMR != BLOCKED)
+ * KUAP_BLOCKED -> AMR;
+ * }
+ * if (from_user) {
+ * save IAMR -> stack;
+ * if (kuep) {
+ * KUEP_BLOCKED ->IAMR
+ * }
+ * }
+ * return;
+ * }
+ *
+ * if (kuap) {
+ * if (from_kernel) {
+ * save AMR -> stack;
+ * if (AMR != BLOCKED)
+ * KUAP_BLOCKED -> AMR;
+ * }
+ *
+ * }
+ */
 .macro kuap_save_amr_and_lock gpr1, gpr2, use_cr, msr_pr_cr
-#ifdef CONFIG_PPC_KUAP
+#if defined(CONFIG_PPC_PKEY)
+
+   /*
+* if both pkey and kuap is disabled, nothing to do
+*/
+   BEGIN_MMU_FTR_SECTION_NESTED(68)
+   b   100f  // skip_save_amr
+   END_MMU_FTR_SECTION_NESTED_IFCLR(MMU_FTR_PKEY | MMU_FTR_KUAP, 68)
+
+   /*
+* if pkey is disabled and we are entering from userspace
+* don't do anything.
+*/
BEGIN_MMU_FTR_SECTION_NESTED(67)
.ifnb \msr_pr_cr
-   bne \msr_pr_cr, 99f
+   /*
+* Without pkey we are not changing AMR outside the kernel
+* hence skip this completely.
+*/
+   bne \msr_pr_cr, 100f  // from userspace
.endif
+END_MMU_FTR_SECTION_NESTED_IFCLR(MMU_FTR_PKEY, 67)
+
+   /*
+* pkey is enabled or pkey is disabled but entering from kernel
+*/
mfspr   \gpr1, SPRN_AMR
std \gpr1, STACK_REGS_KUAP(r1)
-   li  \gpr2, (AMR_KUAP_BLOCKED >> AMR_KUAP_SHIFT)
-   sldi\gpr2, \gpr2, AMR_KUAP_SHIFT
+
+   /*
+* update kernel AMR with AMR_KUAP_BLOCKED only
+* if KUAP feature is enabled
+*/
+   BEGIN_MMU_FTR_SECTION_NESTED(69)
+   LOAD_REG_IMMEDIATE(\gpr2, 

[PATCH v5 10/23] powerpc/exec: Set thread.regs early during exec

2020-08-26 Thread Aneesh Kumar K.V
In later patches during exec, we would like to access default regs.kuap to
control access to the user mapping. Having thread.regs set early makes the
code changes simpler.

Signed-off-by: Aneesh Kumar K.V 
---
 arch/powerpc/include/asm/thread_info.h |  2 --
 arch/powerpc/kernel/process.c  | 37 +-
 2 files changed, 25 insertions(+), 14 deletions(-)

diff --git a/arch/powerpc/include/asm/thread_info.h 
b/arch/powerpc/include/asm/thread_info.h
index ca6c97025704..9418dff1cfe1 100644
--- a/arch/powerpc/include/asm/thread_info.h
+++ b/arch/powerpc/include/asm/thread_info.h
@@ -77,10 +77,8 @@ struct thread_info {
 /* how to get the thread information struct from C */
 extern int arch_dup_task_struct(struct task_struct *dst, struct task_struct 
*src);
 
-#ifdef CONFIG_PPC_BOOK3S_64
 void arch_setup_new_exec(void);
 #define arch_setup_new_exec arch_setup_new_exec
-#endif
 
 #endif /* __ASSEMBLY__ */
 
diff --git a/arch/powerpc/kernel/process.c b/arch/powerpc/kernel/process.c
index 016bd831908e..4633924ea77f 100644
--- a/arch/powerpc/kernel/process.c
+++ b/arch/powerpc/kernel/process.c
@@ -1494,10 +1494,32 @@ void flush_thread(void)
 #ifdef CONFIG_PPC_BOOK3S_64
 void arch_setup_new_exec(void)
 {
-   if (radix_enabled())
-   return;
-   hash__setup_new_exec();
+   if (!radix_enabled())
+   hash__setup_new_exec();
+
+   /*
+* If we exec out of a kernel thread then thread.regs will not be
+* set.  Do it now.
+*/
+   if (!current->thread.regs) {
+   struct pt_regs *regs = task_stack_page(current) + THREAD_SIZE;
+   current->thread.regs = regs - 1;
+   }
+
 }
+#else
+void arch_setup_new_exec(void)
+{
+   /*
+* If we exec out of a kernel thread then thread.regs will not be
+* set.  Do it now.
+*/
+   if (!current->thread.regs) {
+   struct pt_regs *regs = task_stack_page(current) + THREAD_SIZE;
+   current->thread.regs = regs - 1;
+   }
+}
+
 #endif
 
 #ifdef CONFIG_PPC64
@@ -1731,15 +1753,6 @@ void start_thread(struct pt_regs *regs, unsigned long 
start, unsigned long sp)
 #endif
 #endif
 
-   /*
-* If we exec out of a kernel thread then thread.regs will not be
-* set.  Do it now.
-*/
-   if (!current->thread.regs) {
-   struct pt_regs *regs = task_stack_page(current) + THREAD_SIZE;
-   current->thread.regs = regs - 1;
-   }
-
 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
/*
 * Clear any transactional state, we're exec()ing. The cause is
-- 
2.26.2



[PATCH v5 08/23] powerpc/book3s64/kuap: Rename MMU_FTR_RADIX_KUAP to MMU_FTR_KUAP

2020-08-26 Thread Aneesh Kumar K.V
This is in preparate to adding support for kuap with hash translation.
In preparation for that rename/move kuap related functions to
non radix names. Also move the feature bit closer to MMU_FTR_KUEP.

Signed-off-by: Aneesh Kumar K.V 
---
 arch/powerpc/include/asm/book3s/64/kup.h | 18 +-
 arch/powerpc/include/asm/mmu.h   | 16 
 arch/powerpc/mm/book3s64/pkeys.c |  2 +-
 3 files changed, 18 insertions(+), 18 deletions(-)

diff --git a/arch/powerpc/include/asm/book3s/64/kup.h 
b/arch/powerpc/include/asm/book3s/64/kup.h
index 918a2fcceee7..5cec202dc42f 100644
--- a/arch/powerpc/include/asm/book3s/64/kup.h
+++ b/arch/powerpc/include/asm/book3s/64/kup.h
@@ -24,7 +24,7 @@
mtspr   SPRN_AMR, \gpr2
/* No isync required, see kuap_restore_amr() */
 998:
-   END_MMU_FTR_SECTION_NESTED_IFSET(MMU_FTR_RADIX_KUAP, 67)
+   END_MMU_FTR_SECTION_NESTED_IFSET(MMU_FTR_KUAP, 67)
 #endif
 .endm
 
@@ -36,7 +36,7 @@
sldi\gpr2, \gpr2, AMR_KUAP_SHIFT
 999:   tdne\gpr1, \gpr2
EMIT_BUG_ENTRY 999b, __FILE__, __LINE__, (BUGFLAG_WARNING | 
BUGFLAG_ONCE)
-   END_MMU_FTR_SECTION_NESTED_IFSET(MMU_FTR_RADIX_KUAP, 67)
+   END_MMU_FTR_SECTION_NESTED_IFSET(MMU_FTR_KUAP, 67)
 #endif
 .endm
 
@@ -56,7 +56,7 @@
mtspr   SPRN_AMR, \gpr2
isync
 99:
-   END_MMU_FTR_SECTION_NESTED_IFSET(MMU_FTR_RADIX_KUAP, 67)
+   END_MMU_FTR_SECTION_NESTED_IFSET(MMU_FTR_KUAP, 67)
 #endif
 .endm
 
@@ -69,7 +69,7 @@
 
 static inline void kuap_restore_amr(struct pt_regs *regs, unsigned long amr)
 {
-   if (mmu_has_feature(MMU_FTR_RADIX_KUAP) && unlikely(regs->kuap != amr)) 
{
+   if (mmu_has_feature(MMU_FTR_KUAP) && unlikely(regs->kuap != amr)) {
isync();
mtspr(SPRN_AMR, regs->kuap);
/*
@@ -82,7 +82,7 @@ static inline void kuap_restore_amr(struct pt_regs *regs, 
unsigned long amr)
 
 static inline unsigned long kuap_get_and_check_amr(void)
 {
-   if (mmu_has_feature(MMU_FTR_RADIX_KUAP)) {
+   if (mmu_has_feature(MMU_FTR_KUAP)) {
unsigned long amr = mfspr(SPRN_AMR);
if (IS_ENABLED(CONFIG_PPC_KUAP_DEBUG)) /* kuap_check_amr() */
WARN_ON_ONCE(amr != AMR_KUAP_BLOCKED);
@@ -93,7 +93,7 @@ static inline unsigned long kuap_get_and_check_amr(void)
 
 static inline void kuap_check_amr(void)
 {
-   if (IS_ENABLED(CONFIG_PPC_KUAP_DEBUG) && 
mmu_has_feature(MMU_FTR_RADIX_KUAP))
+   if (IS_ENABLED(CONFIG_PPC_KUAP_DEBUG) && mmu_has_feature(MMU_FTR_KUAP))
WARN_ON_ONCE(mfspr(SPRN_AMR) != AMR_KUAP_BLOCKED);
 }
 
@@ -122,7 +122,7 @@ static inline unsigned long kuap_get_and_check_amr(void)
 
 static inline unsigned long get_kuap(void)
 {
-   if (!early_mmu_has_feature(MMU_FTR_RADIX_KUAP))
+   if (!early_mmu_has_feature(MMU_FTR_KUAP))
return 0;
 
return mfspr(SPRN_AMR);
@@ -130,7 +130,7 @@ static inline unsigned long get_kuap(void)
 
 static inline void set_kuap(unsigned long value)
 {
-   if (!early_mmu_has_feature(MMU_FTR_RADIX_KUAP))
+   if (!early_mmu_has_feature(MMU_FTR_KUAP))
return;
 
/*
@@ -180,7 +180,7 @@ static inline void restore_user_access(unsigned long flags)
 static inline bool
 bad_kuap_fault(struct pt_regs *regs, unsigned long address, bool is_write)
 {
-   return WARN(mmu_has_feature(MMU_FTR_RADIX_KUAP) &&
+   return WARN(mmu_has_feature(MMU_FTR_KUAP) &&
(regs->kuap & (is_write ? AMR_KUAP_BLOCK_WRITE : 
AMR_KUAP_BLOCK_READ)),
"Bug: %s fault blocked by AMR!", is_write ? "Write" : 
"Read");
 }
diff --git a/arch/powerpc/include/asm/mmu.h b/arch/powerpc/include/asm/mmu.h
index 255a1837e9f7..04e7a65637fb 100644
--- a/arch/powerpc/include/asm/mmu.h
+++ b/arch/powerpc/include/asm/mmu.h
@@ -29,7 +29,12 @@
  */
 
 /*
- * Support for KUEP feature.
+ * Supports KUAP (key 0 controlling userspace addresses) on radix
+ */
+#define MMU_FTR_KUAP   ASM_CONST(0x0200)
+
+/*
+ * Suppor for KUEP feature.
  */
 #define MMU_FTR_KUEP   ASM_CONST(0x0400)
 
@@ -120,11 +125,6 @@
  */
 #define MMU_FTR_1T_SEGMENT ASM_CONST(0x4000)
 
-/*
- * Supports KUAP (key 0 controlling userspace addresses) on radix
- */
-#define MMU_FTR_RADIX_KUAP ASM_CONST(0x8000)
-
 /* MMU feature bit sets for various CPUs */
 #define MMU_FTRS_DEFAULT_HPTE_ARCH_V2  \
MMU_FTR_HPTE_TABLE | MMU_FTR_PPCAS_ARCH_V2
@@ -187,10 +187,10 @@ enum {
 #ifdef CONFIG_PPC_RADIX_MMU
MMU_FTR_TYPE_RADIX |
MMU_FTR_GTSE |
+#endif /* CONFIG_PPC_RADIX_MMU */
 #ifdef CONFIG_PPC_KUAP
-   MMU_FTR_RADIX_KUAP |
+   MMU_FTR_KUAP |
 #endif /* CONFIG_PPC_KUAP */
-#endif /* CONFIG_PPC_RADIX_MMU */
 #ifdef CONFIG_PPC_MEM_KEYS
MMU_FTR_PKEY |
 #endif
diff --git a/arch/powerpc/mm/book3s64/pkeys.c b/arch/powerpc/mm/book3s64/pkeys.c
index 

[PATCH v5 09/23] powerpc/book3s64/kuap: Use Key 3 for kernel mapping with hash translation

2020-08-26 Thread Aneesh Kumar K.V
This patch updates kernel hash page table entries to use storage key 3
for its mapping. This implies all kernel access will now use key 3 to
control READ/WRITE. The patch also prevents the allocation of key 3 from
userspace and UAMOR value is updated such that userspace cannot modify key 3.

Signed-off-by: Aneesh Kumar K.V 
---
 .../powerpc/include/asm/book3s/64/hash-pkey.h | 24 ++-
 arch/powerpc/include/asm/book3s/64/hash.h |  2 +-
 arch/powerpc/include/asm/book3s/64/mmu-hash.h |  1 +
 arch/powerpc/include/asm/mmu_context.h|  2 +-
 arch/powerpc/mm/book3s64/hash_4k.c|  2 +-
 arch/powerpc/mm/book3s64/hash_64k.c   |  4 ++--
 arch/powerpc/mm/book3s64/hash_hugepage.c  |  2 +-
 arch/powerpc/mm/book3s64/hash_hugetlbpage.c   |  2 +-
 arch/powerpc/mm/book3s64/hash_pgtable.c   |  2 +-
 arch/powerpc/mm/book3s64/hash_utils.c | 10 
 arch/powerpc/mm/book3s64/pkeys.c  |  4 
 11 files changed, 37 insertions(+), 18 deletions(-)

diff --git a/arch/powerpc/include/asm/book3s/64/hash-pkey.h 
b/arch/powerpc/include/asm/book3s/64/hash-pkey.h
index 795010897e5d..9f44e208f036 100644
--- a/arch/powerpc/include/asm/book3s/64/hash-pkey.h
+++ b/arch/powerpc/include/asm/book3s/64/hash-pkey.h
@@ -2,6 +2,9 @@
 #ifndef _ASM_POWERPC_BOOK3S_64_HASH_PKEY_H
 #define _ASM_POWERPC_BOOK3S_64_HASH_PKEY_H
 
+/*  We use key 3 for KERNEL */
+#define HASH_DEFAULT_KERNEL_KEY (HPTE_R_KEY_BIT0 | HPTE_R_KEY_BIT1)
+
 static inline u64 hash__vmflag_to_pte_pkey_bits(u64 vm_flags)
 {
return (((vm_flags & VM_PKEY_BIT0) ? H_PTE_PKEY_BIT0 : 0x0UL) |
@@ -11,13 +14,22 @@ static inline u64 hash__vmflag_to_pte_pkey_bits(u64 
vm_flags)
((vm_flags & VM_PKEY_BIT4) ? H_PTE_PKEY_BIT4 : 0x0UL));
 }
 
-static inline u64 pte_to_hpte_pkey_bits(u64 pteflags)
+static inline u64 pte_to_hpte_pkey_bits(u64 pteflags, unsigned long flags)
 {
-   return (((pteflags & H_PTE_PKEY_BIT4) ? HPTE_R_KEY_BIT4 : 0x0UL) |
-   ((pteflags & H_PTE_PKEY_BIT3) ? HPTE_R_KEY_BIT3 : 0x0UL) |
-   ((pteflags & H_PTE_PKEY_BIT2) ? HPTE_R_KEY_BIT2 : 0x0UL) |
-   ((pteflags & H_PTE_PKEY_BIT1) ? HPTE_R_KEY_BIT1 : 0x0UL) |
-   ((pteflags & H_PTE_PKEY_BIT0) ? HPTE_R_KEY_BIT0 : 0x0UL));
+   unsigned long pte_pkey;
+
+   pte_pkey = (((pteflags & H_PTE_PKEY_BIT4) ? HPTE_R_KEY_BIT4 : 0x0UL) |
+   ((pteflags & H_PTE_PKEY_BIT3) ? HPTE_R_KEY_BIT3 : 0x0UL) |
+   ((pteflags & H_PTE_PKEY_BIT2) ? HPTE_R_KEY_BIT2 : 0x0UL) |
+   ((pteflags & H_PTE_PKEY_BIT1) ? HPTE_R_KEY_BIT1 : 0x0UL) |
+   ((pteflags & H_PTE_PKEY_BIT0) ? HPTE_R_KEY_BIT0 : 0x0UL));
+
+   if (mmu_has_feature(MMU_FTR_KUAP) || mmu_has_feature(MMU_FTR_KUEP)) {
+   if ((pte_pkey == 0) && (flags & HPTE_USE_KERNEL_KEY))
+   return HASH_DEFAULT_KERNEL_KEY;
+   }
+
+   return pte_pkey;
 }
 
 static inline u16 hash__pte_to_pkey_bits(u64 pteflags)
diff --git a/arch/powerpc/include/asm/book3s/64/hash.h 
b/arch/powerpc/include/asm/book3s/64/hash.h
index 73ad038ed10b..d959b0195ad9 100644
--- a/arch/powerpc/include/asm/book3s/64/hash.h
+++ b/arch/powerpc/include/asm/book3s/64/hash.h
@@ -145,7 +145,7 @@ extern void hash__mark_initmem_nx(void);
 
 extern void hpte_need_flush(struct mm_struct *mm, unsigned long addr,
pte_t *ptep, unsigned long pte, int huge);
-extern unsigned long htab_convert_pte_flags(unsigned long pteflags);
+unsigned long htab_convert_pte_flags(unsigned long pteflags, unsigned long 
flags);
 /* Atomic PTE updates */
 static inline unsigned long hash__pte_update(struct mm_struct *mm,
 unsigned long addr,
diff --git a/arch/powerpc/include/asm/book3s/64/mmu-hash.h 
b/arch/powerpc/include/asm/book3s/64/mmu-hash.h
index 93d18da5e7ec..fa8a1c51b8f1 100644
--- a/arch/powerpc/include/asm/book3s/64/mmu-hash.h
+++ b/arch/powerpc/include/asm/book3s/64/mmu-hash.h
@@ -452,6 +452,7 @@ static inline unsigned long hpt_hash(unsigned long vpn,
 
 #define HPTE_LOCAL_UPDATE  0x1
 #define HPTE_NOHPTE_UPDATE 0x2
+#define HPTE_USE_KERNEL_KEY0x4
 
 extern int __hash_page_4K(unsigned long ea, unsigned long access,
  unsigned long vsid, pte_t *ptep, unsigned long trap,
diff --git a/arch/powerpc/include/asm/mmu_context.h 
b/arch/powerpc/include/asm/mmu_context.h
index 7f3658a97384..ece806a590d6 100644
--- a/arch/powerpc/include/asm/mmu_context.h
+++ b/arch/powerpc/include/asm/mmu_context.h
@@ -284,7 +284,7 @@ static inline bool arch_vma_access_permitted(struct 
vm_area_struct *vma,
 #define thread_pkey_regs_init(thread)
 #define arch_dup_pkeys(oldmm, mm)
 
-static inline u64 pte_to_hpte_pkey_bits(u64 pteflags)
+static inline u64 pte_to_hpte_pkey_bits(u64 pteflags, unsigned long flags)
 {
return 0x0UL;
 }
diff --git a/arch/powerpc/mm/book3s64/hash_4k.c 
b/arch/powerpc/mm/book3s64/hash_4k.c

[PATCH v5 07/23] powerpc/book3s64/kuep: Move KUEP related function outside radix

2020-08-26 Thread Aneesh Kumar K.V
The next set of patches adds support for kuep with hash translation.
In preparation for that rename/move kuap related functions to
non radix names.

Also set MMU_FTR_KUEP and add the missing isync().

Signed-off-by: Aneesh Kumar K.V 
---
 arch/powerpc/include/asm/book3s/64/kup.h |  1 +
 arch/powerpc/mm/book3s64/pkeys.c | 21 +
 arch/powerpc/mm/book3s64/radix_pgtable.c | 20 
 3 files changed, 22 insertions(+), 20 deletions(-)

diff --git a/arch/powerpc/include/asm/book3s/64/kup.h 
b/arch/powerpc/include/asm/book3s/64/kup.h
index f4008f8be8e3..918a2fcceee7 100644
--- a/arch/powerpc/include/asm/book3s/64/kup.h
+++ b/arch/powerpc/include/asm/book3s/64/kup.h
@@ -7,6 +7,7 @@
 
 #define AMR_KUAP_BLOCK_READUL(0x4000)
 #define AMR_KUAP_BLOCK_WRITE   UL(0x8000)
+#define AMR_KUEP_BLOCKED   (1UL << 62)
 #define AMR_KUAP_BLOCKED   (AMR_KUAP_BLOCK_READ | AMR_KUAP_BLOCK_WRITE)
 #define AMR_KUAP_SHIFT 62
 
diff --git a/arch/powerpc/mm/book3s64/pkeys.c b/arch/powerpc/mm/book3s64/pkeys.c
index c75994cf50a7..82c722fbce52 100644
--- a/arch/powerpc/mm/book3s64/pkeys.c
+++ b/arch/powerpc/mm/book3s64/pkeys.c
@@ -229,6 +229,27 @@ void __init pkey_early_init_devtree(void)
return;
 }
 
+#ifdef CONFIG_PPC_KUEP
+void __init setup_kuep(bool disabled)
+{
+   if (disabled || !early_radix_enabled())
+   return;
+
+   if (smp_processor_id() == boot_cpuid) {
+   pr_info("Activating Kernel Userspace Execution Prevention\n");
+   cur_cpu_spec->mmu_features |= MMU_FTR_KUEP;
+   }
+
+   /*
+* Radix always uses key0 of the IAMR to determine if an access is
+* allowed. We set bit 0 (IBM bit 1) of key0, to prevent instruction
+* fetch.
+*/
+   mtspr(SPRN_IAMR, AMR_KUEP_BLOCKED);
+   isync();
+}
+#endif
+
 #ifdef CONFIG_PPC_KUAP
 void __init setup_kuap(bool disabled)
 {
diff --git a/arch/powerpc/mm/book3s64/radix_pgtable.c 
b/arch/powerpc/mm/book3s64/radix_pgtable.c
index 5c0c74e131ca..ace662231be6 100644
--- a/arch/powerpc/mm/book3s64/radix_pgtable.c
+++ b/arch/powerpc/mm/book3s64/radix_pgtable.c
@@ -587,26 +587,6 @@ static void radix_init_amor(void)
mtspr(SPRN_AMOR, (3ul << 62));
 }
 
-#ifdef CONFIG_PPC_KUEP
-void setup_kuep(bool disabled)
-{
-   if (disabled || !early_radix_enabled())
-   return;
-
-   if (smp_processor_id() == boot_cpuid) {
-   pr_info("Activating Kernel Userspace Execution Prevention\n");
-   cur_cpu_spec->mmu_features |= MMU_FTR_KUEP;
-   }
-
-   /*
-* Radix always uses key0 of the IAMR to determine if an access is
-* allowed. We set bit 0 (IBM bit 1) of key0, to prevent instruction
-* fetch.
-*/
-   mtspr(SPRN_IAMR, (1ul << 62));
-}
-#endif
-
 void __init radix__early_init_mmu(void)
 {
unsigned long lpcr;
-- 
2.26.2



[PATCH v5 06/23] powerpc/book3s64/kup: Use the correct #ifdef when including headers

2020-08-26 Thread Aneesh Kumar K.V
Use CONFIG_PPC_BOOK3S_64 instead of CONFIG_PPC64. This avoid wrong inclusion
with other 64bit platforms. To fix booke 64 build error add macro 
kuap_check_amr.

Signed-off-by: Aneesh Kumar K.V 
---
 arch/powerpc/include/asm/kup.h | 8 
 1 file changed, 8 insertions(+)

diff --git a/arch/powerpc/include/asm/kup.h b/arch/powerpc/include/asm/kup.h
index 1cff92953384..6c3ee976ee15 100644
--- a/arch/powerpc/include/asm/kup.h
+++ b/arch/powerpc/include/asm/kup.h
@@ -15,8 +15,16 @@
 #define KUAP_CURRENT   (KUAP_CURRENT_READ | KUAP_CURRENT_WRITE)
 
 #ifdef CONFIG_PPC64
+#ifdef CONFIG_PPC_BOOK3S_64
 #include 
+#else
+#ifdef __ASSEMBLY__
+.macro kuap_check_amr gpr1, gpr2
+.endm
 #endif
+#endif
+#endif /* CONFIG_PPC_64 */
+
 #ifdef CONFIG_PPC_8xx
 #include 
 #endif
-- 
2.26.2



[PATCH v5 05/23] powerpc/book3s64/kuap: Move KUAP related function outside radix

2020-08-26 Thread Aneesh Kumar K.V
The next set of patches adds support for kuap with hash translation.
In preparation for that rename/move kuap related functions to
non radix names.

Signed-off-by: Aneesh Kumar K.V 
---
 .../asm/book3s/64/{kup-radix.h => kup.h}  |  6 ++---
 arch/powerpc/include/asm/kup.h|  2 +-
 arch/powerpc/kernel/syscall_64.c  |  2 +-
 arch/powerpc/mm/book3s64/pkeys.c  | 22 +++
 arch/powerpc/mm/book3s64/radix_pgtable.c  | 19 
 5 files changed, 27 insertions(+), 24 deletions(-)
 rename arch/powerpc/include/asm/book3s/64/{kup-radix.h => kup.h} (97%)

diff --git a/arch/powerpc/include/asm/book3s/64/kup-radix.h 
b/arch/powerpc/include/asm/book3s/64/kup.h
similarity index 97%
rename from arch/powerpc/include/asm/book3s/64/kup-radix.h
rename to arch/powerpc/include/asm/book3s/64/kup.h
index 19a8e640a4e5..f4008f8be8e3 100644
--- a/arch/powerpc/include/asm/book3s/64/kup-radix.h
+++ b/arch/powerpc/include/asm/book3s/64/kup.h
@@ -1,6 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0 */
-#ifndef _ASM_POWERPC_BOOK3S_64_KUP_RADIX_H
-#define _ASM_POWERPC_BOOK3S_64_KUP_RADIX_H
+#ifndef _ASM_POWERPC_BOOK3S_64_KUP_H
+#define _ASM_POWERPC_BOOK3S_64_KUP_H
 
 #include 
 #include 
@@ -187,4 +187,4 @@ bad_kuap_fault(struct pt_regs *regs, unsigned long address, 
bool is_write)
 
 #endif /* __ASSEMBLY__ */
 
-#endif /* _ASM_POWERPC_BOOK3S_64_KUP_RADIX_H */
+#endif /* _ASM_POWERPC_BOOK3S_64_KUP_H */
diff --git a/arch/powerpc/include/asm/kup.h b/arch/powerpc/include/asm/kup.h
index 1d0f7d838b2e..1cff92953384 100644
--- a/arch/powerpc/include/asm/kup.h
+++ b/arch/powerpc/include/asm/kup.h
@@ -15,7 +15,7 @@
 #define KUAP_CURRENT   (KUAP_CURRENT_READ | KUAP_CURRENT_WRITE)
 
 #ifdef CONFIG_PPC64
-#include 
+#include 
 #endif
 #ifdef CONFIG_PPC_8xx
 #include 
diff --git a/arch/powerpc/kernel/syscall_64.c b/arch/powerpc/kernel/syscall_64.c
index 8e50818aa50b..22a31a988264 100644
--- a/arch/powerpc/kernel/syscall_64.c
+++ b/arch/powerpc/kernel/syscall_64.c
@@ -2,7 +2,7 @@
 
 #include 
 #include 
-#include 
+#include 
 #include 
 #include 
 #include 
diff --git a/arch/powerpc/mm/book3s64/pkeys.c b/arch/powerpc/mm/book3s64/pkeys.c
index 7dc71f85683d..c75994cf50a7 100644
--- a/arch/powerpc/mm/book3s64/pkeys.c
+++ b/arch/powerpc/mm/book3s64/pkeys.c
@@ -9,9 +9,12 @@
 #include 
 #include 
 #include 
+#include 
+
 #include 
 #include 
 
+
 int  num_pkey; /* Max number of pkeys supported */
 /*
  *  Keys marked in the reservation list cannot be allocated by  userspace
@@ -226,6 +229,25 @@ void __init pkey_early_init_devtree(void)
return;
 }
 
+#ifdef CONFIG_PPC_KUAP
+void __init setup_kuap(bool disabled)
+{
+   if (disabled || !early_radix_enabled())
+   return;
+
+   if (smp_processor_id() == boot_cpuid) {
+   pr_info("Activating Kernel Userspace Access Prevention\n");
+   cur_cpu_spec->mmu_features |= MMU_FTR_RADIX_KUAP;
+   }
+
+   /*
+* Set the default kernel AMR values on all cpus.
+*/
+   mtspr(SPRN_AMR, AMR_KUAP_BLOCKED);
+   isync();
+}
+#endif
+
 static inline u64 read_amr(void)
 {
return mfspr(SPRN_AMR);
diff --git a/arch/powerpc/mm/book3s64/radix_pgtable.c 
b/arch/powerpc/mm/book3s64/radix_pgtable.c
index 730e2771a2c8..5c0c74e131ca 100644
--- a/arch/powerpc/mm/book3s64/radix_pgtable.c
+++ b/arch/powerpc/mm/book3s64/radix_pgtable.c
@@ -607,25 +607,6 @@ void setup_kuep(bool disabled)
 }
 #endif
 
-#ifdef CONFIG_PPC_KUAP
-void setup_kuap(bool disabled)
-{
-   if (disabled || !early_radix_enabled())
-   return;
-
-   if (smp_processor_id() == boot_cpuid) {
-   pr_info("Activating Kernel Userspace Access Prevention\n");
-   cur_cpu_spec->mmu_features |= MMU_FTR_RADIX_KUAP;
-   }
-
-   /*
-* Set the default kernel AMR values on all cpus.
-*/
-   mtspr(SPRN_AMR, AMR_KUAP_BLOCKED);
-   isync();
-}
-#endif
-
 void __init radix__early_init_mmu(void)
 {
unsigned long lpcr;
-- 
2.26.2



[PATCH v5 04/23] powerpc/book3s64/kuap/kuep: Move uamor setup to pkey init

2020-08-26 Thread Aneesh Kumar K.V
This patch consolidates UAMOR update across pkey, kuap and kuep features.
The boot cpu initialize UAMOR via pkey init and both radix/hash do the
secondary cpu UAMOR init in early_init_mmu_secondary.

We don't check for mmu_feature in radix secondary init because UAMOR
is a supported SPRN with all CPUs supporting radix translation.
The old code was not updating UAMOR if we had smap disabled and smep enabled.
This change handles that case.

Signed-off-by: Aneesh Kumar K.V 
---
 arch/powerpc/mm/book3s64/radix_pgtable.c | 8 +---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/arch/powerpc/mm/book3s64/radix_pgtable.c 
b/arch/powerpc/mm/book3s64/radix_pgtable.c
index 28c784976bed..730e2771a2c8 100644
--- a/arch/powerpc/mm/book3s64/radix_pgtable.c
+++ b/arch/powerpc/mm/book3s64/radix_pgtable.c
@@ -618,9 +618,6 @@ void setup_kuap(bool disabled)
cur_cpu_spec->mmu_features |= MMU_FTR_RADIX_KUAP;
}
 
-   /* Make sure userspace can't change the AMR */
-   mtspr(SPRN_UAMOR, 0);
-
/*
 * Set the default kernel AMR values on all cpus.
 */
@@ -719,6 +716,11 @@ void radix__early_init_mmu_secondary(void)
 
radix__switch_mmu_context(NULL, _mm);
tlbiel_all();
+
+#ifdef CONFIG_PPC_PKEY
+   /* Make sure userspace can't change the AMR */
+   mtspr(SPRN_UAMOR, 0);
+#endif
 }
 
 void radix__mmu_cleanup_all(void)
-- 
2.26.2



[PATCH v5 03/23] powerpc/book3s64/kuap/kuep: Make KUAP and KUEP a subfeature of PPC_MEM_KEYS

2020-08-26 Thread Aneesh Kumar K.V
The next set of patches adds support for kuap with hash translation.
Hence make KUAP a BOOK3S_64 feature. Also make it a subfeature of
PPC_MEM_KEYS. Hash translation is going to use pkeys to support
KUAP/KUEP. Adding this dependency reduces the code complexity and
enables us to move some of the initialization code to pkeys.c

Signed-off-by: Aneesh Kumar K.V 
---
 .../powerpc/include/asm/book3s/64/kup-radix.h | 33 +++
 arch/powerpc/include/asm/book3s/64/mmu.h  |  2 +-
 arch/powerpc/include/asm/ptrace.h |  2 +-
 arch/powerpc/kernel/asm-offsets.c |  2 +-
 arch/powerpc/mm/book3s64/Makefile |  2 +-
 arch/powerpc/mm/book3s64/pkeys.c  | 24 +-
 arch/powerpc/platforms/Kconfig.cputype|  4 +++
 7 files changed, 42 insertions(+), 27 deletions(-)

diff --git a/arch/powerpc/include/asm/book3s/64/kup-radix.h 
b/arch/powerpc/include/asm/book3s/64/kup-radix.h
index 3ee1ec60be84..19a8e640a4e5 100644
--- a/arch/powerpc/include/asm/book3s/64/kup-radix.h
+++ b/arch/powerpc/include/asm/book3s/64/kup-radix.h
@@ -61,7 +61,7 @@
 
 #else /* !__ASSEMBLY__ */
 
-#ifdef CONFIG_PPC_KUAP
+#ifdef CONFIG_PPC_PKEY
 
 #include 
 #include 
@@ -96,6 +96,24 @@ static inline void kuap_check_amr(void)
WARN_ON_ONCE(mfspr(SPRN_AMR) != AMR_KUAP_BLOCKED);
 }
 
+#else /* CONFIG_PPC_PKEY */
+
+static inline void kuap_restore_amr(struct pt_regs *regs, unsigned long amr)
+{
+}
+
+static inline void kuap_check_amr(void)
+{
+}
+
+static inline unsigned long kuap_get_and_check_amr(void)
+{
+   return 0;
+}
+#endif /* CONFIG_PPC_PKEY */
+
+
+#ifdef CONFIG_PPC_KUAP
 /*
  * We support individually allowing read or write, but we don't support nesting
  * because that would require an expensive read/modify write of the AMR.
@@ -165,19 +183,6 @@ bad_kuap_fault(struct pt_regs *regs, unsigned long 
address, bool is_write)
(regs->kuap & (is_write ? AMR_KUAP_BLOCK_WRITE : 
AMR_KUAP_BLOCK_READ)),
"Bug: %s fault blocked by AMR!", is_write ? "Write" : 
"Read");
 }
-#else /* CONFIG_PPC_KUAP */
-static inline void kuap_restore_amr(struct pt_regs *regs, unsigned long amr)
-{
-}
-
-static inline void kuap_check_amr(void)
-{
-}
-
-static inline unsigned long kuap_get_and_check_amr(void)
-{
-   return 0;
-}
 #endif /* CONFIG_PPC_KUAP */
 
 #endif /* __ASSEMBLY__ */
diff --git a/arch/powerpc/include/asm/book3s/64/mmu.h 
b/arch/powerpc/include/asm/book3s/64/mmu.h
index 55442d45c597..381146afaf80 100644
--- a/arch/powerpc/include/asm/book3s/64/mmu.h
+++ b/arch/powerpc/include/asm/book3s/64/mmu.h
@@ -214,7 +214,7 @@ extern int mmu_io_psize;
 void mmu_early_init_devtree(void);
 void hash__early_init_devtree(void);
 void radix__early_init_devtree(void);
-#ifdef CONFIG_PPC_MEM_KEYS
+#ifdef CONFIG_PPC_PKEY
 void pkey_early_init_devtree(void);
 #else
 static inline void pkey_early_init_devtree(void) {}
diff --git a/arch/powerpc/include/asm/ptrace.h 
b/arch/powerpc/include/asm/ptrace.h
index 155a197c0aa1..5f62ce579a8b 100644
--- a/arch/powerpc/include/asm/ptrace.h
+++ b/arch/powerpc/include/asm/ptrace.h
@@ -53,7 +53,7 @@ struct pt_regs
 #ifdef CONFIG_PPC64
unsigned long ppr;
 #endif
-#ifdef CONFIG_PPC_KUAP
+#ifdef CONFIG_PPC_PKEY
unsigned long kuap;
 #endif
};
diff --git a/arch/powerpc/kernel/asm-offsets.c 
b/arch/powerpc/kernel/asm-offsets.c
index 8711c2164b45..63548992b5ab 100644
--- a/arch/powerpc/kernel/asm-offsets.c
+++ b/arch/powerpc/kernel/asm-offsets.c
@@ -353,7 +353,7 @@ int main(void)
STACK_PT_REGS_OFFSET(_PPR, ppr);
 #endif /* CONFIG_PPC64 */
 
-#ifdef CONFIG_PPC_KUAP
+#ifdef CONFIG_PPC_PKEY
STACK_PT_REGS_OFFSET(STACK_REGS_KUAP, kuap);
 #endif
 
diff --git a/arch/powerpc/mm/book3s64/Makefile 
b/arch/powerpc/mm/book3s64/Makefile
index fd393b8be14f..1b56d3af47d4 100644
--- a/arch/powerpc/mm/book3s64/Makefile
+++ b/arch/powerpc/mm/book3s64/Makefile
@@ -17,7 +17,7 @@ endif
 obj-$(CONFIG_TRANSPARENT_HUGEPAGE) += hash_hugepage.o
 obj-$(CONFIG_PPC_SUBPAGE_PROT) += subpage_prot.o
 obj-$(CONFIG_SPAPR_TCE_IOMMU)  += iommu_api.o
-obj-$(CONFIG_PPC_MEM_KEYS) += pkeys.o
+obj-$(CONFIG_PPC_PKEY) += pkeys.o
 
 # Instrumenting the SLB fault path can lead to duplicate SLB entries
 KCOV_INSTRUMENT_slb.o := n
diff --git a/arch/powerpc/mm/book3s64/pkeys.c b/arch/powerpc/mm/book3s64/pkeys.c
index b1d091a97611..7dc71f85683d 100644
--- a/arch/powerpc/mm/book3s64/pkeys.c
+++ b/arch/powerpc/mm/book3s64/pkeys.c
@@ -89,12 +89,14 @@ static int scan_pkey_feature(void)
}
}
 
+#ifdef CONFIG_PPC_MEM_KEYS
/*
 * Adjust the upper limit, based on the number of bits supported by
 * arch-neutral code.
 */
pkeys_total = min_t(int, pkeys_total,
((ARCH_VM_PKEY_FLAGS >> VM_PKEY_SHIFT) + 1));
+#endif
return pkeys_total;
 }
 
@@ -102,6 +104,7 @@ void __init pkey_early_init_devtree(void)
 

[PATCH v5 02/23] KVM: PPC: BOOK3S: PR: Ignore UAMOR SPR

2020-08-26 Thread Aneesh Kumar K.V
With power7 and above we expect the cpu to support keys. The
number of keys are firmware controlled based on device tree.
PR KVM do not expose key details via device tree. Hence when running with PR KVM
we do run with MMU_FTR_KEY support disabled. But we can still
get updates on UAMOR. Hence ignore access to them and for mfstpr return
0 indicating no AMR/IAMR update is no allowed.

Signed-off-by: Aneesh Kumar K.V 
---
 arch/powerpc/kvm/book3s_emulate.c | 6 ++
 1 file changed, 6 insertions(+)

diff --git a/arch/powerpc/kvm/book3s_emulate.c 
b/arch/powerpc/kvm/book3s_emulate.c
index 0effd48c8f4d..b08cc15f31c7 100644
--- a/arch/powerpc/kvm/book3s_emulate.c
+++ b/arch/powerpc/kvm/book3s_emulate.c
@@ -840,6 +840,9 @@ int kvmppc_core_emulate_mtspr_pr(struct kvm_vcpu *vcpu, int 
sprn, ulong spr_val)
case SPRN_MMCR1:
case SPRN_MMCR2:
case SPRN_UMMCR2:
+   case SPRN_UAMOR:
+   case SPRN_IAMR:
+   case SPRN_AMR:
 #endif
break;
 unprivileged:
@@ -1004,6 +1007,9 @@ int kvmppc_core_emulate_mfspr_pr(struct kvm_vcpu *vcpu, 
int sprn, ulong *spr_val
case SPRN_MMCR2:
case SPRN_UMMCR2:
case SPRN_TIR:
+   case SPRN_UAMOR:
+   case SPRN_IAMR:
+   case SPRN_AMR:
 #endif
*spr_val = 0;
break;
-- 
2.26.2



[PATCH v5 01/23] powerpc: Add new macro to handle NESTED_IFCLR

2020-08-26 Thread Aneesh Kumar K.V
This will be used by the following patches

Signed-off-by: Aneesh Kumar K.V 
---
 arch/powerpc/include/asm/feature-fixups.h | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/arch/powerpc/include/asm/feature-fixups.h 
b/arch/powerpc/include/asm/feature-fixups.h
index b0af97add751..122c22161268 100644
--- a/arch/powerpc/include/asm/feature-fixups.h
+++ b/arch/powerpc/include/asm/feature-fixups.h
@@ -100,6 +100,9 @@ label##5:   
\
 #define END_MMU_FTR_SECTION_NESTED_IFSET(msk, label)   \
END_MMU_FTR_SECTION_NESTED((msk), (msk), label)
 
+#define END_MMU_FTR_SECTION_NESTED_IFCLR(msk, label)   \
+   END_MMU_FTR_SECTION_NESTED((msk), 0, label)
+
 #define END_MMU_FTR_SECTION_IFSET(msk) END_MMU_FTR_SECTION((msk), (msk))
 #define END_MMU_FTR_SECTION_IFCLR(msk) END_MMU_FTR_SECTION((msk), 0)
 
-- 
2.26.2



[PATCH v5 00/23] Kernel userspace access/execution prevention with hash translation

2020-08-26 Thread Aneesh Kumar K.V
This patch series implements KUAP and KUEP with hash translation mode using
memory keys. The kernel now uses memory protection key 3 to control access
to the kernel. Kernel page table entries are now configured with key 3.
Access to locations configured with any other key value is denied when in
kernel mode (MSR_PR=0). This includes userspace which is by default configured
with key 0.

null-syscall benchmark results:

With smap/smep disabled:
Without patch:
845.29 ns2451.44 cycles
With patch series:
858.38 ns2489.30 cycles

With smap/smep enabled:
Without patch:
NA
With patch series:
1021.51 ns2962.44 cycles

Changes from v4:
* Repost with other pkey related changes split out as a separate series.
* Improve null-syscall benchmark by optimizing SPRN save and restore.

Changes from v3:
* Fix build error reported by kernel test robot 

Changes from v2:
* Rebase to the latest kernel.
* Fixed a bug with disabling KUEP/KUAP on kernel command line
* Added a patch to make kup key dynamic.

Changes from V1:
* Rebased on latest kernel

Aneesh Kumar K.V (23):
  powerpc: Add new macro to handle NESTED_IFCLR
  KVM: PPC: BOOK3S: PR: Ignore UAMOR SPR
  powerpc/book3s64/kuap/kuep: Make KUAP and KUEP a subfeature of
PPC_MEM_KEYS
  powerpc/book3s64/kuap/kuep: Move uamor setup to pkey init
  powerpc/book3s64/kuap: Move KUAP related function outside radix
  powerpc/book3s64/kup: Use the correct #ifdef when including headers
  powerpc/book3s64/kuep: Move KUEP related function outside radix
  powerpc/book3s64/kuap: Rename MMU_FTR_RADIX_KUAP to MMU_FTR_KUAP
  powerpc/book3s64/kuap: Use Key 3 for kernel mapping with hash
translation
  powerpc/exec: Set thread.regs early during exec
  powerpc/book3s64/pkeys: Store/restore userspace AMR/IAMR correctly on
entry and exit from kernel
  powerpc/book3s64/pkeys: Inherit correctly on fork.
  powerpc/book3s64/pkeys: Reset userspace AMR correctly on exec
  powerpc/ptrace-view: Use pt_regs values instead of thread_struct based
one.
  powerpc/book3s64/pkeys: Don't update SPRN_AMR when in kernel mode.
  powerpc/book3s64/kuap: Restrict access to userspace based on userspace
AMR
  powerpc/book3s64/kuap: Improve error reporting with KUAP
  powerpc/book3s64/kuap: Use Key 3 to implement KUAP with hash
translation.
  powerpc/book3s64/kuep: Use Key 3 to implement KUEP with hash
translation.
  powerpc/book3s64/hash/kuap: Enable kuap on hash
  powerpc/book3s64/hash/kuep: Enable KUEP on hash
  powerpc/book3s64/hash/kup: Don't hardcode kup key
  powerpc/book3s64/pkeys: Optimize FTR_KUAP and FTR_KUEP disabled case

 arch/powerpc/include/asm/book3s/32/kup.h  |   4 +-
 .../powerpc/include/asm/book3s/64/hash-pkey.h |  10 +-
 arch/powerpc/include/asm/book3s/64/hash.h |   2 +-
 .../powerpc/include/asm/book3s/64/kup-radix.h | 185 
 arch/powerpc/include/asm/book3s/64/kup.h  | 409 ++
 arch/powerpc/include/asm/book3s/64/mmu-hash.h |   1 +
 arch/powerpc/include/asm/book3s/64/mmu.h  |   2 +-
 arch/powerpc/include/asm/book3s/64/pkeys.h|   3 +
 arch/powerpc/include/asm/feature-fixups.h |   3 +
 arch/powerpc/include/asm/kup.h|  14 +-
 arch/powerpc/include/asm/mmu.h|  16 +-
 arch/powerpc/include/asm/mmu_context.h|   2 +-
 arch/powerpc/include/asm/nohash/32/kup-8xx.h  |   4 +-
 arch/powerpc/include/asm/processor.h  |   4 -
 arch/powerpc/include/asm/ptrace.h |   6 +-
 arch/powerpc/include/asm/thread_info.h|   2 -
 arch/powerpc/kernel/asm-offsets.c |   4 +-
 arch/powerpc/kernel/entry_64.S|   6 +-
 arch/powerpc/kernel/exceptions-64s.S  |   4 +-
 arch/powerpc/kernel/process.c |  56 ++-
 arch/powerpc/kernel/ptrace/ptrace-view.c  |   7 +-
 arch/powerpc/kernel/syscall_64.c  |  38 +-
 arch/powerpc/kernel/traps.c   |   6 -
 arch/powerpc/kvm/book3s_emulate.c |   6 +
 arch/powerpc/mm/book3s64/Makefile |   2 +-
 arch/powerpc/mm/book3s64/hash_4k.c|   2 +-
 arch/powerpc/mm/book3s64/hash_64k.c   |   4 +-
 arch/powerpc/mm/book3s64/hash_hugepage.c  |   2 +-
 arch/powerpc/mm/book3s64/hash_hugetlbpage.c   |   2 +-
 arch/powerpc/mm/book3s64/hash_pgtable.c   |   2 +-
 arch/powerpc/mm/book3s64/hash_utils.c |  10 +-
 arch/powerpc/mm/book3s64/pkeys.c  | 177 +---
 arch/powerpc/mm/book3s64/radix_pgtable.c  |  47 +-
 arch/powerpc/mm/fault.c   |   2 +-
 arch/powerpc/platforms/Kconfig.cputype|   4 +
 35 files changed, 679 insertions(+), 369 deletions(-)
 delete mode 100644 arch/powerpc/include/asm/book3s/64/kup-radix.h
 create mode 100644 arch/powerpc/include/asm/book3s/64/kup.h

-- 
2.26.2



[PATCH v2] powerpc: Update documentation of ISA versions for Power10

2020-08-26 Thread Jordan Niethe
Update the CPU to ISA Version Mapping document to include Power10 and
ISA v3.1.

Signed-off-by: Jordan Niethe 
---
v2: Transactional Memory = No
---
 Documentation/powerpc/isa-versions.rst | 4 
 1 file changed, 4 insertions(+)

diff --git a/Documentation/powerpc/isa-versions.rst 
b/Documentation/powerpc/isa-versions.rst
index a363d8c1603c..3873bbba183a 100644
--- a/Documentation/powerpc/isa-versions.rst
+++ b/Documentation/powerpc/isa-versions.rst
@@ -7,6 +7,7 @@ Mapping of some CPU versions to relevant ISA versions.
 = 
 CPU   Architecture version
 = 
+Power10   Power ISA v3.1
 Power9Power ISA v3.0B
 Power8Power ISA v2.07
 Power7Power ISA v2.06
@@ -32,6 +33,7 @@ Key Features
 == ==
 CPUVMX (aka. Altivec)
 == ==
+Power10Yes
 Power9 Yes
 Power8 Yes
 Power7 Yes
@@ -47,6 +49,7 @@ PPC970 Yes
 == 
 CPUVSX
 == 
+Power10Yes
 Power9 Yes
 Power8 Yes
 Power7 Yes
@@ -62,6 +65,7 @@ PPC970 No
 == 
 CPUTransactional Memory
 == 
+Power10No  (* see Power ISA v3.1 Appendix A.)
 Power9 Yes (* see transactional_memory.txt)
 Power8 Yes
 Power7 No
-- 
2.17.1



[PATCH] powerpc/64s: Remove TM from Power10 features

2020-08-26 Thread Jordan Niethe
ISA v3.1 removes transactional memory and hence it should not be present
in cpu_features or cpu_user_features2. Remove CPU_FTR_TM_COMP from
CPU_FTRS_POWER10. Remove PPC_FEATURE2_HTM_COMP and
PPC_FEATURE2_HTM_NOSC_COMP from COMMON_USER2_POWER10.

Fixes: a3ea40d5c736 ("powerpc: Add POWER10 architected mode")
Signed-off-by: Jordan Niethe 
---
 arch/powerpc/include/asm/cputable.h |  2 +-
 arch/powerpc/kernel/cputable.c  | 13 ++---
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/arch/powerpc/include/asm/cputable.h 
b/arch/powerpc/include/asm/cputable.h
index fdddb822d564..5322fec6d413 100644
--- a/arch/powerpc/include/asm/cputable.h
+++ b/arch/powerpc/include/asm/cputable.h
@@ -478,7 +478,7 @@ static inline void cpu_feature_keys_init(void) { }
CPU_FTR_STCX_CHECKS_ADDRESS | CPU_FTR_POPCNTB | CPU_FTR_POPCNTD | \
CPU_FTR_CFAR | CPU_FTR_HVMODE | CPU_FTR_VMX_COPY | \
CPU_FTR_DBELL | CPU_FTR_HAS_PPR | CPU_FTR_ARCH_207S | \
-   CPU_FTR_TM_COMP | CPU_FTR_ARCH_300 | CPU_FTR_ARCH_31 | \
+   CPU_FTR_ARCH_300 | CPU_FTR_ARCH_31 | \
CPU_FTR_DAWR | CPU_FTR_DAWR1)
 #define CPU_FTRS_CELL  (CPU_FTR_LWSYNC | \
CPU_FTR_PPCAS_ARCH_V2 | CPU_FTR_CTRL | \
diff --git a/arch/powerpc/kernel/cputable.c b/arch/powerpc/kernel/cputable.c
index 2f10a874e849..23f9bb51edc3 100644
--- a/arch/powerpc/kernel/cputable.c
+++ b/arch/powerpc/kernel/cputable.c
@@ -123,9 +123,16 @@ extern void __restore_cpu_e6500(void);
 PPC_FEATURE2_DARN | \
 PPC_FEATURE2_SCV)
 #define COMMON_USER_POWER10COMMON_USER_POWER9
-#define COMMON_USER2_POWER10   (COMMON_USER2_POWER9 | \
-PPC_FEATURE2_ARCH_3_1 | \
-PPC_FEATURE2_MMA)
+#define COMMON_USER2_POWER10   (PPC_FEATURE2_ARCH_3_1 | \
+PPC_FEATURE2_MMA | \
+PPC_FEATURE2_ARCH_3_00 | \
+PPC_FEATURE2_HAS_IEEE128 | \
+PPC_FEATURE2_DARN | \
+PPC_FEATURE2_SCV | \
+PPC_FEATURE2_ARCH_2_07 | \
+PPC_FEATURE2_DSCR | \
+PPC_FEATURE2_ISEL | PPC_FEATURE2_TAR | \
+PPC_FEATURE2_VEC_CRYPTO)
 
 #ifdef CONFIG_PPC_BOOK3E_64
 #define COMMON_USER_BOOKE  (COMMON_USER_PPC64 | PPC_FEATURE_BOOKE)
-- 
2.17.1



Re: [PATCH] ASoC: fsl_spdif: Fix unnecessary check infsl_spdif_probe()

2020-08-26 Thread Tang Bin

Hi Mark

在 2020/8/27 0:53, Mark Brown 写道:

On Wed, Aug 26, 2020 at 11:09:18PM +0800, Tang Bin wrote:

The function fsl_spdif_probe() is only called with an openfirmware
platform device. Therefore there is no need to check that the passed
in device is NULL.

Why is this an issue - the check will make things more robust if someone
manages to load the driver on a non-DT system and otherwise costs us a
couple of instructions?

Thanks for your reply.

In this function,  function fsl_spdif_probe() can be triggered only if 
the platform_device and platform_driver matches,


so I think the judgement at the beginning is redundant.

Thanks

Tang Bin





Re: [PATCH v5 4/8] powerpc/watchpoint: Move DAWR detection logic outside of hw_breakpoint.c

2020-08-26 Thread kernel test robot
Hi Ravi,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on powerpc/next]
[also build test ERROR on v5.9-rc2 next-20200826]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:
https://github.com/0day-ci/linux/commits/Ravi-Bangoria/powerpc-watchpoint-Bug-fixes-plus-new-feature-flag/20200825-123831
base:   https://git.kernel.org/pub/scm/linux/kernel/git/powerpc/linux.git next
config: powerpc-randconfig-r024-20200826 (attached as .config)
compiler: powerpc-linux-gcc (GCC) 9.3.0
reproduce (this is a W=1 build):
wget 
https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O 
~/bin/make.cross
chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross 
ARCH=powerpc 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot 

All errors (new ones prefixed by >>):

   arch/powerpc/kernel/hw_breakpoint_constraints.c: In function 
'ea_hw_range_overlaps':
>> arch/powerpc/kernel/hw_breakpoint_constraints.c:42:16: error: 
>> 'HW_BREAKPOINT_SIZE_QUADWORD' undeclared (first use in this function); did 
>> you mean 'HW_BREAKPOINT_SIZE'?
  42 |   align_size = HW_BREAKPOINT_SIZE_QUADWORD;
 |^~~
 |HW_BREAKPOINT_SIZE
   arch/powerpc/kernel/hw_breakpoint_constraints.c:42:16: note: each undeclared 
identifier is reported only once for each function it appears in

# 
https://github.com/0day-ci/linux/commit/4899293e6a722214368fd6b5df8ecda43600ccfb
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review 
Ravi-Bangoria/powerpc-watchpoint-Bug-fixes-plus-new-feature-flag/20200825-123831
git checkout 4899293e6a722214368fd6b5df8ecda43600ccfb
vim +42 arch/powerpc/kernel/hw_breakpoint_constraints.c

30  
31  static bool ea_hw_range_overlaps(unsigned long ea, int size,
32   struct arch_hw_breakpoint *info)
33  {
34  unsigned long hw_start_addr, hw_end_addr;
35  unsigned long align_size = HW_BREAKPOINT_SIZE;
36  
37  /*
38   * On p10 predecessors, quadword is handle differently then
39   * other instructions.
40   */
41  if (!cpu_has_feature(CPU_FTR_ARCH_31) && size == 16)
  > 42  align_size = HW_BREAKPOINT_SIZE_QUADWORD;
43  
44  hw_start_addr = ALIGN_DOWN(info->address, align_size);
45  hw_end_addr = ALIGN(info->address + info->len, align_size);
46  
47  return ((ea < hw_end_addr) && (ea + size > hw_start_addr));
48  }
49  

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-...@lists.01.org


.config.gz
Description: application/gzip


Re: kernel since 5.6 do not boot anymore on Apple PowerBook

2020-08-26 Thread Giuseppe Sacco
Hello Christophe,

Il giorno mer, 26/08/2020 alle 15.53 +0200, Christophe Leroy ha
scritto:
[...]
> If there is no warning, then the issue is something else, bad luck.
> 
> Could you increase the loglevel and try again both with and without 
> VMAP_STACK ? Maybe we'll get more information on where it stops.

The problem is related to the CPU frequency changes. This is where the
system stop: cpufreq get the CPU frequency limits and then start the
default governor (performance) and then calls function
cpufreq_gov_performance_limits() that never returns.

Rebuilding after enabling pr_debug for cpufreq.c, I've got some more
lines of output:

cpufreq: setting new policy for CPU 0: 667000 - 867000 kHz
cpufreq: new min and max freqs are 667000 - 867000 kHz
cpufreq: governor switch
cpufreq: cpufreq_init_governor: for CPU 0
cpufreq: cpufreq_start_governor: for CPU 0
cpufreq: target for CPU 0: 867000 kHz, relation 1, requested 867000 kHz
cpufreq: __target_index: cpu: 0, oldfreq: 667000, new freq: 867000
cpufreq: notification 0 of frequency transition to 867000 kHz
cpufreq: saving 133328 as reference value for loops_per_jiffy; freq is 667000 
kHz

no more lines are printed. I think this output only refers to the
notification sent prior to the frequency change.

I was thinking that selecting a governor that would run at 667mHz would
probably skip the problem. I added cpufreq.default_governor=powersave
to the command line parameters but it did not work: the selected
governor was still performance and the system crashed.

Bye,
Giuseppe



Re: fsl_espi errors on v5.7.15

2020-08-26 Thread Chris Packham
(adding Nicholas)

On 26/08/20 6:38 pm, Heiner Kallweit wrote:
> On 26.08.2020 08:07, Chris Packham wrote:
>> On 26/08/20 1:48 pm, Chris Packham wrote:
>>> On 26/08/20 10:22 am, Chris Packham wrote:
 On 25/08/20 7:22 pm, Heiner Kallweit wrote:

 
> I've been staring at spi-fsl-espi.c for while now and I think I've
>> identified a couple of deficiencies that may or may not be related
>> to my
>> issue.
>>
>> First I think the 'Transfer done but SPIE_DON isn't set' message
>> can be
>> generated spuriously. In fsl_espi_irq() we read the ESPI_SPIE
>> register.
>> We also write back to it to clear the current events. We re-read it in
>> fsl_espi_cpu_irq() and complain when SPIE_DON is not set. But we can
>> naturally end up in that situation if we're doing a large read.
>> Consider
>> the messages for reading a block of data from a spi-nor chip
>>
>>     tx = READ_OP + ADDR
>>     rx = data
>>
>> We setup the transfer and pump out the tx_buf. The first interrupt
>> goes
>> off and ESPI_SPIE has SPIM_DON and SPIM_RXT set. We empty the rx fifo,
>> clear ESPI_SPIE and wait for the next interrupt. The next interrupt
>> fires and this time we have ESPI_SPIE with just SPIM_RXT set. This
>> continues until we've received all the data and we finish with
>> ESPI_SPIE
>> having only SPIM_RXT set. When we re-read it we complain that SPIE_DON
>> isn't set.
>>
>> The other deficiency is that we only get an interrupt when the
>> amount of
>> data in the rx fifo is above FSL_ESPI_RXTHR. If there are fewer than
>> FSL_ESPI_RXTHR left to be received we will never pull them out of
>> the fifo.
>>
> SPIM_DON will trigger an interrupt once the last characters have been
> transferred, and read the remaining characters from the FIFO.
 The T2080RM that I have says the following about the DON bit

 "Last character was transmitted. The last character was transmitted
 and a new command can be written for the next frame."

 That does at least seem to fit with my assertion that it's all about
 the TX direction. But the fact that it doesn't happen all the time
 throws some doubt on it.

> I think the reason I'm seeing some variability is because of how fast
>> (or slow) the interrupts get processed and how fast the spi-nor
>> chip can
>> fill the CPUs rx fifo.
>>
> To rule out timing issues at high bus frequencies I initially asked
> for re-testing at lower frequencies. If you e.g. limit the bus to 1 MHz
> or even less, then timing shouldn't be an issue.
 Yes I've currently got spi-max-frequency = <100>; in my dts. I
 would also expect a slower frequency would fit my "DON is for TX"
 narrative.
> Last relevant functional changes have been done almost 4 years ago.
> And yours is the first such report I see. So question is what could
> be so
> special with your setup that it seems you're the only one being
> affected.
> The scenarios you describe are standard, therefore much more people
> should be affected in case of a driver bug.
 Agreed. But even on my hardware (which may have a latent issue
 despite being in the field for going on 5 years) the issue only
 triggers under some fairly specific circumstances.
> You said that kernel config impacts how frequently the issue happens.
> Therefore question is what's the diff in kernel config, and how could
> the differences be related to SPI.
 It did seem to be somewhat random. Things like CONFIG_PREEMPT have an
 impact but every time I found something that seemed to be having an
 impact I've been able to disprove it. I actually think its about how
 busy the system is which may or may not affect when we get round to
 processing the interrupts.

 I have managed to get the 'Transfer done but SPIE_DON isn't set!' to
 occur on the T2080RDB.

 I've had to add the following to expose the environment as a mtd
 partition

 diff --git a/arch/powerpc/boot/dts/fsl/t208xrdb.dtsi
 b/arch/powerpc/boot/dts/fsl/t208xrdb.dtsi
 index ff87e67c70da..fbf95fc1fd68 100644
 --- a/arch/powerpc/boot/dts/fsl/t208xrdb.dtsi
 +++ b/arch/powerpc/boot/dts/fsl/t208xrdb.dtsi
 @@ -116,6 +116,15 @@ flash@0 {
      compatible = "micron,n25q512ax3",
 "jedec,spi-nor";
      reg = <0>;
      spi-max-frequency = <1000>; /*
 input clock */
 +
 +   partition@u-boot {
 +    reg = <0x 0x0010>;
 +    label = "u-boot";
 +    };
 +    partition@u-boot-env {
 + 

Re: kernel since 5.6 do not boot anymore on Apple PowerBook

2020-08-26 Thread Christophe Leroy

Hello Giuseppe,

Le 24/08/2020 à 22:48, Giuseppe Sacco a écrit :

Hello Christophe,

Il giorno lun, 24/08/2020 alle 07.17 +0200, Christophe Leroy ha
scritto:

Hello Giuseppe,

[...]

The Oopses in the video are fixed in 5.9-rc2, see my response to your
other mail.


Right, I just updated from git and rebuilt the kernel whith
CONFIG_VMAP_STACK not set and the machine boots correctly.


So now we know that your kernel doesn't boot when CONFIG_VMAP_STACK is set.
Can you remind the exact problem ?


latest kernel with CONFIG_VMAP_STACK set stops after writing:
pmac32_cpufreq: registering PowerMac CPU frequency driver
pmac32_cpufreq: Low: 667 MHz, High: 867 Mhz, Boot: 667 MHz


One common problem with CONFIG_VMAP_STACK is when some drivers are
invalidly using buffers in stack for DMA.

Couldn't try with CONFIG_DEBUG_VIRTUAL (without CONFIG_VMAP_STACK) and
see if it triggers some warnings ?


I've just tried: it boots without any special warning. What should I
look for? This is an excerpt of dmesg output about the line it would
otherwise stop:


If there is no warning, then the issue is something else, bad luck.

Could you increase the loglevel and try again both with and without 
VMAP_STACK ? Maybe we'll get more information on where it stops.


Christophe



[...]
[6.566984] PowerMac i2c bus pmu 2 registered
[6.574879] PowerMac i2c bus pmu 1 registered
[6.582634] PowerMac i2c bus mac-io 0 registered
[6.590323] i2c i2c-2: No i2c address for 
/pci@f200/mac-io@17/i2c@18000/i2c-modem
[6.598290] PowerMac i2c bus uni-n 1 registered
[6.606196] i2c i2c-3: i2c-powermac: modalias failure on 
/uni-n@f800/i2c@f8001000/cereal@1c0
[6.614320] PowerMac i2c bus uni-n 0 registered
[6.622501] pmac32_cpufreq: Registering PowerMac CPU frequency driver
[6.630580] pmac32_cpufreq: Low: 667 Mhz, High: 867 Mhz, Boot: 667 Mhz
[6.639518] ledtrig-cpu: registered to indicate activity on CPUs
[6.647894] NET: Registered protocol family 10
[6.656492] Segment Routing with IPv6
[6.664490] mip6: Mobile IPv6
[6.672337] NET: Registered protocol family 17
[6.680213] mpls_gso: MPLS GSO support
[...]

Bye,
Giuseppe



Re: [PATCH] ASoC: fsl_spdif: Fix unnecessary check in fsl_spdif_probe()

2020-08-26 Thread Mark Brown
On Wed, Aug 26, 2020 at 11:09:18PM +0800, Tang Bin wrote:
> The function fsl_spdif_probe() is only called with an openfirmware
> platform device. Therefore there is no need to check that the passed
> in device is NULL.

Why is this an issue - the check will make things more robust if someone
manages to load the driver on a non-DT system and otherwise costs us a
couple of instructions?


signature.asc
Description: PGP signature


Re: [PATCH v1 4/9] powerpc/vdso: Remove unnecessary ifdefs in vdso_pagelist initialization

2020-08-26 Thread Christophe Leroy




Le 26/08/2020 à 16:58, Michael Ellerman a écrit :

Christophe Leroy  writes:

diff --git a/arch/powerpc/kernel/vdso.c b/arch/powerpc/kernel/vdso.c
index daef14a284a3..bbb69832fd46 100644
--- a/arch/powerpc/kernel/vdso.c
+++ b/arch/powerpc/kernel/vdso.c
@@ -718,16 +710,14 @@ static int __init vdso_init(void)

...
  
-

-#ifdef CONFIG_VDSO32
vdso32_kbase = _start;
  
  	/*

@@ -735,8 +725,6 @@ static int __init vdso_init(void)
 */
vdso32_pages = (_end - _start) >> PAGE_SHIFT;
DBG("vdso32_kbase: %p, 0x%x pages\n", vdso32_kbase, vdso32_pages);
-#endif


This didn't build for ppc64le:

   
/opt/cross/gcc-8.20_binutils-2.32/powerpc64-unknown-linux-gnu/bin/powerpc64-unknown-linux-gnu-ld:
 arch/powerpc/kernel/vdso.o:(.toc+0x0): undefined reference to `vdso32_end'
   
/opt/cross/gcc-8.20_binutils-2.32/powerpc64-unknown-linux-gnu/bin/powerpc64-unknown-linux-gnu-ld:
 arch/powerpc/kernel/vdso.o:(.toc+0x8): undefined reference to `vdso32_start'
   make[1]: *** [/scratch/michael/build/maint/Makefile:1166: vmlinux] Error 1
   make: *** [Makefile:185: __sub-make] Error 2

So I just put that ifdef back.



Argh. I guess that's the DBG() that hurts. I'll think about it.

Christophe


[PATCH] ASoC: fsl_spdif: Fix unnecessary check in fsl_spdif_probe()

2020-08-26 Thread Tang Bin
The function fsl_spdif_probe() is only called with an openfirmware
platform device. Therefore there is no need to check that the passed
in device is NULL.

Signed-off-by: Zhang Shengju 
Signed-off-by: Tang Bin 
---
 sound/soc/fsl/fsl_spdif.c | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/sound/soc/fsl/fsl_spdif.c b/sound/soc/fsl/fsl_spdif.c
index 7858a5499..395c50167 100644
--- a/sound/soc/fsl/fsl_spdif.c
+++ b/sound/soc/fsl/fsl_spdif.c
@@ -1221,9 +1221,6 @@ static int fsl_spdif_probe(struct platform_device *pdev)
void __iomem *regs;
int irq, ret, i;
 
-   if (!np)
-   return -ENODEV;
-
spdif_priv = devm_kzalloc(>dev, sizeof(*spdif_priv), GFP_KERNEL);
if (!spdif_priv)
return -ENOMEM;
-- 
2.20.1.windows.1





Re: [PATCH v1 4/9] powerpc/vdso: Remove unnecessary ifdefs in vdso_pagelist initialization

2020-08-26 Thread Michael Ellerman
Christophe Leroy  writes:
> diff --git a/arch/powerpc/kernel/vdso.c b/arch/powerpc/kernel/vdso.c
> index daef14a284a3..bbb69832fd46 100644
> --- a/arch/powerpc/kernel/vdso.c
> +++ b/arch/powerpc/kernel/vdso.c
> @@ -718,16 +710,14 @@ static int __init vdso_init(void)
...
>  
> -
> -#ifdef CONFIG_VDSO32
>   vdso32_kbase = _start;
>  
>   /*
> @@ -735,8 +725,6 @@ static int __init vdso_init(void)
>*/
>   vdso32_pages = (_end - _start) >> PAGE_SHIFT;
>   DBG("vdso32_kbase: %p, 0x%x pages\n", vdso32_kbase, vdso32_pages);
> -#endif

This didn't build for ppc64le:

  
/opt/cross/gcc-8.20_binutils-2.32/powerpc64-unknown-linux-gnu/bin/powerpc64-unknown-linux-gnu-ld:
 arch/powerpc/kernel/vdso.o:(.toc+0x0): undefined reference to `vdso32_end'
  
/opt/cross/gcc-8.20_binutils-2.32/powerpc64-unknown-linux-gnu/bin/powerpc64-unknown-linux-gnu-ld:
 arch/powerpc/kernel/vdso.o:(.toc+0x8): undefined reference to `vdso32_start'
  make[1]: *** [/scratch/michael/build/maint/Makefile:1166: vmlinux] Error 1
  make: *** [Makefile:185: __sub-make] Error 2

So I just put that ifdef back.

cheers


[PATCH v2 16/23] powerpc: use asm-generic/mmu_context.h for no-op implementations

2020-08-26 Thread Nicholas Piggin
Cc: Michael Ellerman 
Cc: Benjamin Herrenschmidt 
Cc: Paul Mackerras 
Cc: linuxppc-dev@lists.ozlabs.org
Signed-off-by: Nicholas Piggin 
---
 arch/powerpc/include/asm/mmu_context.h | 22 +++---
 1 file changed, 7 insertions(+), 15 deletions(-)

diff --git a/arch/powerpc/include/asm/mmu_context.h 
b/arch/powerpc/include/asm/mmu_context.h
index 7f3658a97384..bc22e247ab55 100644
--- a/arch/powerpc/include/asm/mmu_context.h
+++ b/arch/powerpc/include/asm/mmu_context.h
@@ -14,7 +14,9 @@
 /*
  * Most if the context management is out of line
  */
+#define init_new_context init_new_context
 extern int init_new_context(struct task_struct *tsk, struct mm_struct *mm);
+#define destroy_context destroy_context
 extern void destroy_context(struct mm_struct *mm);
 #ifdef CONFIG_SPAPR_TCE_IOMMU
 struct mm_iommu_table_group_mem_t;
@@ -235,27 +237,15 @@ static inline void switch_mm(struct mm_struct *prev, 
struct mm_struct *next,
 }
 #define switch_mm_irqs_off switch_mm_irqs_off
 
-
-#define deactivate_mm(tsk,mm)  do { } while (0)
-
-/*
- * After we have set current->mm to a new value, this activates
- * the context for the new mm so we see the new mappings.
- */
-static inline void activate_mm(struct mm_struct *prev, struct mm_struct *next)
-{
-   switch_mm(prev, next, current);
-}
-
-/* We don't currently use enter_lazy_tlb() for anything */
+#ifdef CONFIG_PPC_BOOK3E_64
+#define enter_lazy_tlb enter_lazy_tlb
 static inline void enter_lazy_tlb(struct mm_struct *mm,
  struct task_struct *tsk)
 {
/* 64-bit Book3E keeps track of current PGD in the PACA */
-#ifdef CONFIG_PPC_BOOK3E_64
get_paca()->pgd = NULL;
-#endif
 }
+#endif
 
 extern void arch_exit_mmap(struct mm_struct *mm);
 
@@ -298,5 +288,7 @@ static inline int arch_dup_mmap(struct mm_struct *oldmm,
return 0;
 }
 
+#include 
+
 #endif /* __KERNEL__ */
 #endif /* __ASM_POWERPC_MMU_CONTEXT_H */
-- 
2.23.0



[Bug 208957] 5.9-rc1 fails to build for a PowerMac G5: .../book3s64/hash_utils.c:1119:21: error: ‘default_uamor’ undeclared (first use in this function) 1119 | mtspr(SPRN_UAMOR, default_uamor);

2020-08-26 Thread bugzilla-daemon
https://bugzilla.kernel.org/show_bug.cgi?id=208957

Michael Ellerman (mich...@ellerman.id.au) changed:

   What|Removed |Added

 Status|RESOLVED|CLOSED

-- 
You are receiving this mail because:
You are watching the assignee of the bug.

[Bug 208957] 5.9-rc1 fails to build for a PowerMac G5: .../book3s64/hash_utils.c:1119:21: error: ‘default_uamor’ undeclared (first use in this function) 1119 | mtspr(SPRN_UAMOR, default_uamor);

2020-08-26 Thread bugzilla-daemon
https://bugzilla.kernel.org/show_bug.cgi?id=208957

Michael Ellerman (mich...@ellerman.id.au) changed:

   What|Removed |Added

 CC||mich...@ellerman.id.au
 Resolution|OBSOLETE|CODE_FIX

--- Comment #2 from Michael Ellerman (mich...@ellerman.id.au) ---
Fixed in 1e4e4bcaf70e ("powerpc/pkeys: Fix build error with PPC_MEM_KEYS
disabled")

-- 
You are receiving this mail because:
You are watching the assignee of the bug.

Re: [PATCH v8 2/8] powerpc/vdso: Remove __kernel_datapage_offset and simplify __get_datapage()

2020-08-26 Thread Michael Ellerman
Christophe Leroy  writes:
> Le 04/08/2020 à 13:17, Christophe Leroy a écrit :
>> On 07/16/2020 02:59 AM, Michael Ellerman wrote:
>>> Christophe Leroy  writes:
 The VDSO datapage and the text pages are always located immediately
 next to each other, so it can be hardcoded without an indirection
 through __kernel_datapage_offset

 In order to ease things, move the data page in front like other
 arches, that way there is no need to know the size of the library
 to locate the data page.

> [...]
>
>>>
>>> I merged this but then realised it breaks the display of the vdso in 
>>> /proc/self/maps.
>>>
>>> ie. the vdso vma gets no name:
>>>
>>>    # cat /proc/self/maps
>
> [...]
>
>>> And it's also going to break the logic in arch_unmap() to detect if
>>> we're unmapping (part of) the VDSO. And it will break arch_remap() too.
>>>
>>> And the logic to recognise the signal trampoline in
>>> arch/powerpc/perf/callchain_*.c as well.
>> 
>> I don't think it breaks that one, because ->vdsobase is still the start 
>> of text.
>> 
>>>
>>> So I'm going to rebase and drop this for now.
>>>
>>> Basically we have a bunch of places that assume that vdso_base is == the
>>> start of the VDSO vma, and also that the code starts there. So that will
>>> need some work to tease out all those assumptions and make them work
>>> with this change.
>> 
>> Ok, one day I need to look at it in more details and see how other 
>> architectures handle it etc ...
>> 
>
> I just sent out a series which switches powerpc to the new 
> _install_special_mapping() API, the one powerpc uses being deprecated 
> since commit a62c34bd2a8a ("x86, mm: Improve _install_special_mapping
> and fix x86 vdso naming")
>
> arch_remap() gets replaced by vdso_remap()
>
> For arch_unmap(), I'm wondering how/what other architectures do, because 
> powerpc seems to be the only one to erase the vdso context pointer when 
> unmapping the vdso.

Yeah. The original unmap/remap stuff was added for CRIU, which I thought
people tested on other architectures (more than powerpc even).

Possibly no one really cares about vdso unmap though, vs just moving the
vdso.

We added a test for vdso unmap recently because it happened to trigger a
KAUP failure, and someone actually hit it & reported it.

Running that test on arm64 segfaults:

  # ./sigreturn_vdso 
  VDSO is at 0x8191f000-0x8191 (4096 bytes)
  Signal delivered OK with VDSO mapped
  VDSO moved to 0x8191a000-0x8191afff (4096 bytes)
  Signal delivered OK with VDSO moved
  Unmapped VDSO
  Remapped the stack executable
  [   48.556191] potentially unexpected fatal signal 11.
  [   48.556752] CPU: 0 PID: 140 Comm: sigreturn_vdso Not tainted 
5.9.0-rc2-00057-g2ac69819ba9e #190
  [   48.556990] Hardware name: linux,dummy-virt (DT)
  [   48.557336] pstate: 60001000 (nZCv daif -PAN -UAO BTYPE=--)
  [   48.557475] pc : 8191a7bc
  [   48.557603] lr : 8191a7bc
  [   48.557697] sp : c13c9e90
  [   48.557873] x29: c13cb0e0 x28:  
  [   48.558201] x27:  x26:  
  [   48.558337] x25:  x24:  
  [   48.558754] x23:  x22:  
  [   48.558893] x21: 004009b0 x20:  
  [   48.559046] x19: 00400ff0 x18:  
  [   48.559180] x17: 817da300 x16: 00412010 
  [   48.559312] x15:  x14: 001c 
  [   48.559443] x13: 656c626174756365 x12: 7865206b63617473 
  [   48.559625] x11: 0003 x10: 0101010101010101 
  [   48.559828] x9 : 818afda8 x8 : 0081 
  [   48.559973] x7 : 6174732065687420 x6 : 64657070616d6552 
  [   48.560115] x5 : 0e0388bd x4 : 0040135d 
  [   48.560270] x3 :  x2 : 0001 
  [   48.560412] x1 : 0003 x0 : 004120b8 
  Segmentation fault
  #

So I think we need to keep the unmap hook. Maybe it should be handled by
the special_mapping stuff generically.

cheers


Re: [PATCH v2 1/4] powerpc/drmem: Make lmb_size 64 bit

2020-08-26 Thread Sasha Levin
Hi

[This is an automated email]

This commit has been processed because it contains a -stable tag.
The stable tag indicates that it's relevant for the following trees: all

The bot has tested the following trees: v5.8.2, v5.7.16, v5.4.59, v4.19.140, 
v4.14.193, v4.9.232, v4.4.232.

v5.8.2: Build OK!
v5.7.16: Build OK!
v5.4.59: Build OK!
v4.19.140: Build OK!
v4.14.193: Failed to apply! Possible dependencies:
22508f3dc985 ("powerpc/numa: Look up device node in of_get_usable_memory()")
2c77721552e5 ("powerpc: Move of_drconf_cell struct to asm/drmem.h")
35f80debaef0 ("powerpc/numa: Look up device node in of_get_assoc_arrays()")
514a9cb3316a ("powerpc/numa: Update numa code use walk_drmem_lmbs")
6195a5001f1d ("powerpc/pseries: Update memory hotplug code to use drmem LMB 
array")
6c6ea53725b3 ("powerpc/mm: Separate ibm, dynamic-memory data from DT 
format")
b6eca183e23e ("powerpc/kernel: Enables memory hot-remove after reboot on 
pseries guests")
b88fc309d6ad ("powerpc/numa: Look up associativity array in 
of_drconf_to_nid_single")

v4.9.232: Failed to apply! Possible dependencies:
3a2df3798d4d ("powerpc/mm: Make switch_mm_irqs_off() out of line")
43ed84a891b7 ("powerpc/mm: Move pgdir setting into a helper")
5a61ef74f269 ("powerpc/64s: Support new device tree binding for discovering 
CPU features")
5d451a87e5eb ("powerpc/64: Retrieve number of L1 cache sets from 
device-tree")
6c6ea53725b3 ("powerpc/mm: Separate ibm, dynamic-memory data from DT 
format")
70cd4c10b290 ("KVM: PPC: Book3S HV: Fix software walk of guest process page 
tables")
9b081e10805c ("powerpc: port 64 bits pgtable_cache to 32 bits")
a25bd72badfa ("powerpc/mm/radix: Workaround prefetch issue with KVM")
bd067f83b084 ("powerpc/64: Fix naming of cache block vs. cache line")
dbcbfee0c81c ("powerpc/64: More definitions for POWER9")
e2827fe5c156 ("powerpc/64: Clean up ppc64_caches using a struct per cache")
f4329f2ecb14 ("powerpc/64s: Reduce exception alignment")

v4.4.232: Failed to apply! Possible dependencies:
11a6f6abd74a ("powerpc/mm: Move radix/hash common data structures to 
book3s64 headers")
26b6a3d9bb48 ("powerpc/mm: move pte headers to book3s directory")
3808a88985b4 ("powerpc: Move FW feature probing out of pseries probe()")
3dfcb315d81e ("powerpc/mm: make a separate copy for book3s")
5a61ef74f269 ("powerpc/64s: Support new device tree binding for discovering 
CPU features")
5d31a96e6c01 ("powerpc/livepatch: Add livepatch stack to struct 
thread_info")
6574ba950bbe ("powerpc/kernel: Convert cpu_has_feature() to returning bool")
6c6ea53725b3 ("powerpc/mm: Separate ibm, dynamic-memory data from DT 
format")
a141cca3892b ("powerpc/mm: Add early_[cpu|mmu]_has_feature()")
a8ed87c92adf ("powerpc/mm/radix: Add MMU_FTR_RADIX")
b92a226e5284 ("powerpc: Move cpu_has_feature() to a separate file")
da6a97bf12d5 ("powerpc: Move epapr_paravirt_early_init() to 
early_init_devtree()")
f63e6d898760 ("powerpc/livepatch: Add livepatch header")


NOTE: The patch will not be queued to stable trees until it is upstream.

How should we proceed with this patch?

-- 
Thanks
Sasha


Re: [PATCH] powerpc/pseries: Do not initiate shutdown when system is running on UPS

2020-08-26 Thread Sasha Levin
Hi

[This is an automated email]

This commit has been processed because it contains a "Fixes:" tag
fixing commit: 79872e35469b ("powerpc/pseries: All events of 
EPOW_SYSTEM_SHUTDOWN must initiate shutdown").

The bot has tested the following trees: v5.8.2, v5.7.16, v5.4.59, v4.19.140, 
v4.14.193, v4.9.232, v4.4.232.

v5.8.2: Build OK!
v5.7.16: Build OK!
v5.4.59: Build OK!
v4.19.140: Build OK!
v4.14.193: Build OK!
v4.9.232: Build OK!
v4.4.232: Failed to apply! Possible dependencies:
b4af279a7cba ("powerpc/pseries: Limit EPOW reset event warnings")


NOTE: The patch will not be queued to stable trees until it is upstream.

How should we proceed with this patch?

-- 
Thanks
Sasha


Re: [PATCH v2 3/4] powerpc/memhotplug: Make lmb size 64bit

2020-08-26 Thread Sasha Levin
Hi

[This is an automated email]

This commit has been processed because it contains a -stable tag.
The stable tag indicates that it's relevant for the following trees: all

The bot has tested the following trees: v5.8.2, v5.7.16, v5.4.59, v4.19.140, 
v4.14.193, v4.9.232, v4.4.232.

v5.8.2: Build OK!
v5.7.16: Build OK!
v5.4.59: Build OK!
v4.19.140: Failed to apply! Possible dependencies:
Unable to calculate

v4.14.193: Failed to apply! Possible dependencies:
Unable to calculate

v4.9.232: Failed to apply! Possible dependencies:
1a367063ca0c ("powerpc/pseries: Check memory device state before 
onlining/offlining")
25b587fba9a4 ("powerpc/pseries: Correct possible read beyond dlpar sysfs 
buffer")
333f7b76865b ("powerpc/pseries: Implement indexed-count hotplug memory add")
753843471cbb ("powerpc/pseries: Implement indexed-count hotplug memory 
remove")
943db62c316c ("powerpc/pseries: Revert 'Auto-online hotplugged memory'")
c21f515c7436 ("powerpc/pseries: Make the acquire/release of the drc for 
memory a seperate step")
e70d59700fc3 ("powerpc/pseries: Introduce memory hotplug READD operation")
f84775c2d5d9 ("powerpc/pseries: Fix build break when MEMORY_HOTREMOVE=n")

v4.4.232: Failed to apply! Possible dependencies:
183deeea5871 ("powerpc/pseries: Consolidate CPU hotplug code to 
hotplug-cpu.c")
1a367063ca0c ("powerpc/pseries: Check memory device state before 
onlining/offlining")
1dc759566636 ("powerpc/pseries: Use kernel hotplug queue for PowerVM 
hotplug events")
1f859adb9253 ("powerpc/pseries: Verify CPU doesn't exist before adding")
25b587fba9a4 ("powerpc/pseries: Correct possible read beyond dlpar sysfs 
buffer")
333f7b76865b ("powerpc/pseries: Implement indexed-count hotplug memory add")
4a4bdfea7cb7 ("powerpc/pseries: Refactor dlpar_add_lmb() code")
753843471cbb ("powerpc/pseries: Implement indexed-count hotplug memory 
remove")
9054619ef54a ("powerpc/pseries: Add pseries hotplug workqueue")
943db62c316c ("powerpc/pseries: Revert 'Auto-online hotplugged memory'")
9dc512819e4b ("powerpc: Fix unused function warning 'lmb_to_memblock'")
bdf5fc633804 ("powerpc/pseries: Update LMB associativity index during DLPAR 
add/remove")
c21f515c7436 ("powerpc/pseries: Make the acquire/release of the drc for 
memory a seperate step")
e70d59700fc3 ("powerpc/pseries: Introduce memory hotplug READD operation")
e9d764f80396 ("powerpc/pseries: Enable kernel CPU dlpar from sysfs")
ec999072442a ("powerpc/pseries: Auto-online hotplugged memory")
f84775c2d5d9 ("powerpc/pseries: Fix build break when MEMORY_HOTREMOVE=n")
fdb4f6e99ffa ("powerpc/pseries: Remove call to memblock_add()")


NOTE: The patch will not be queued to stable trees until it is upstream.

How should we proceed with this patch?

-- 
Thanks
Sasha


Re: [PATCH v2 1/2] powerpc/rtas: Restrict RTAS requests from userspace

2020-08-26 Thread Sasha Levin
Hi

[This is an automated email]

This commit has been processed because it contains a -stable tag.
The stable tag indicates that it's relevant for the following trees: all

The bot has tested the following trees: v5.8.2, v5.7.16, v5.4.59, v4.19.140, 
v4.14.193, v4.9.232, v4.4.232.

v5.8.2: Build OK!
v5.7.16: Build OK!
v5.4.59: Failed to apply! Possible dependencies:
1a8916ee3ac2 ("powerpc: Detect the secure boot mode of the system")
4238fad366a6 ("powerpc/ima: Add support to initialize ima policy rules")
9155e2341aa8 ("powerpc/powernv: Add OPAL API interface to access secure 
variable")
bd5d9c743d38 ("powerpc: expose secure variables to userspace via sysfs")

v4.19.140: Failed to apply! Possible dependencies:
0261a508c9fc ("powerpc/mm: dump segment registers on book3s/32")
136bc0397ae2 ("powerpc/pseries: Introduce option to build secure virtual 
machines")
1a8916ee3ac2 ("powerpc: Detect the secure boot mode of the system")
75d9fc7fd94e ("powerpc/powernv: move OPAL call wrapper tracing and 
interrupt handling to C")
7c91efce1608 ("powerpc/mm: dump block address translation on book3s/32")
97026b5a5ac2 ("powerpc/mm: Split dump_pagelinuxtables flag_array table")
a49dddbdb0cc ("powerpc/kernel: Add ucall_norets() ultravisor call handler")
b2133bd7a553 ("powerpc/book3s/32: do not include pte-common.h")
bd5d9c743d38 ("powerpc: expose secure variables to userspace via sysfs")
cbcbbf4afd6d ("powerpc/mm: Define platform default caches related flags")
d81e6f8b7c66 ("powerpc/mm: don't use _PAGE_EXEC in book3s/32")
d82fd29c5a8c ("powerpc/mm: Distribute platform specific PAGE and PMD flags 
and definitions")
e66c3209c7fd ("powerpc: Move page table dump files in a dedicated 
subdirectory")
fb0b0a73b223 ("powerpc: Enable kcov")
ff00552578ba ("powerpc/8xx: change name of a few page flags to avoid 
confusion")

v4.14.193: Failed to apply! Possible dependencies:
136bc0397ae2 ("powerpc/pseries: Introduce option to build secure virtual 
machines")
1a8916ee3ac2 ("powerpc: Detect the secure boot mode of the system")
4e56207130ed ("kbuild: Cache a few more calls to the compiler")
4fa8bc949de1 ("kbuild: rename *-asn1.[ch] to *.asn1.[ch]")
74ce1896c6c6 ("kbuild: clean up *.dtb and *.dtb.S patterns from top-level 
Makefile")
75d9fc7fd94e ("powerpc/powernv: move OPAL call wrapper tracing and 
interrupt handling to C")
8438ee76b004 ("Makefile: disable PIE before testing asm goto")
8f2133cc0e1f ("powerpc/pseries: hcall_exit tracepoint retval should be 
signed")
92e3da3cf193 ("powerpc: initial pkey plumbing")
9a8dfb394c04 ("kbuild: clean up *.lex.c and *.tab.[ch] patterns from 
top-level Makefile")
9ce285cfe360 (".gitignore: move *-asn1.[ch] patterns to the top-level 
.gitignore")
a49dddbdb0cc ("powerpc/kernel: Add ucall_norets() ultravisor call handler")
bd5d9c743d38 ("powerpc: expose secure variables to userspace via sysfs")
c64ba044ed57 ("kbuild: gcov: enable -fno-tree-loop-im if supported")
d677a4d60193 ("Makefile: support flag -fsanitizer-coverage=trace-cmp")
d682026dd3c5 (".gitignore: ignore ASN.1 auto generated files")
e08d6de4e532 ("kbuild: remove kbuild cache")
e501ce957a78 ("x86: Force asm-goto")
e9666d10a567 ("jump_label: move 'asm goto' support test to Kconfig")
ef46d9b3dc01 ("kbuild: clean up *.i and *.lst patterns by make clean")

v4.9.232: Failed to apply! Possible dependencies:
1515ab932156 ("powerpc/mm: Dump hash table")
1a8916ee3ac2 ("powerpc: Detect the secure boot mode of the system")
6cc89bad60a6 ("powerpc/kprobes: Invoke handlers directly")
7644d5819cf8 ("powerpc: Create asm/debugfs.h and move powerpc_debugfs_root 
there")
7c0f6ba682b9 ("Replace  with  globally")
8eb07b187000 ("powerpc/mm: Dump linux pagetables")
92e3da3cf193 ("powerpc: initial pkey plumbing")
bd5d9c743d38 ("powerpc: expose secure variables to userspace via sysfs")
da6658859b9c ("powerpc: Change places using CONFIG_KEXEC to use 
CONFIG_KEXEC_CORE instead.")
dd5ac03e0955 ("powerpc/mm: Fix page table dump build on non-Book3S")

v4.4.232: Failed to apply! Possible dependencies:
019132ff3daf ("x86/mm/pkeys: Fill in pkey field in siginfo")
0e749e54244e ("dax: increase granularity of dax_clear_blocks() operations")
1a8916ee3ac2 ("powerpc: Detect the secure boot mode of the system")
33a709b25a76 ("mm/gup, x86/mm/pkeys: Check VMAs and PTEs for protection 
keys")
34c0fd540e79 ("mm, dax, pmem: introduce pfn_t")
3565fce3a659 ("mm, x86: get_user_pages() for dax mappings")
52db400fcd50 ("pmem, dax: clean up clear_pmem()")
5c1d90f51027 ("x86/mm/pkeys: Add PTE bits for storing protection key")
63c17fb8e5a4 ("mm/core, x86/mm/pkeys: Store protection bits in high VMA 
flags")
69660fd797c3 ("x86, mm: introduce _PAGE_DEVMAP")
7b2d0dbac489 ("x86/mm/pkeys: Pass VMA down in to fault signal generation 
code")
8f62c883222c 

Re: kernel since 5.6 do not boot anymore on Apple PowerBook

2020-08-26 Thread Christophe Leroy

Hello Giuseppe,

Le 24/08/2020 à 22:48, Giuseppe Sacco a écrit :

Hello Christophe,

Il giorno lun, 24/08/2020 alle 07.17 +0200, Christophe Leroy ha
scritto:

Hello Giuseppe,

[...]

The Oopses in the video are fixed in 5.9-rc2, see my response to your
other mail.


Right, I just updated from git and rebuilt the kernel whith
CONFIG_VMAP_STACK not set and the machine boots correctly.

So now we know that your kernel doesn't boot when CONFIG_VMAP_STACK 
is set.

Can you remind the exact problem ?


latest kernel with CONFIG_VMAP_STACK set stops after writing:
pmac32_cpufreq: registering PowerMac CPU frequency driver
pmac32_cpufreq: Low: 667 MHz, High: 867 Mhz, Boot: 667 MHz


One common problem with CONFIG_VMAP_STACK is when some drivers are
invalidly using buffers in stack for DMA.

Couldn't try with CONFIG_DEBUG_VIRTUAL (without CONFIG_VMAP_STACK) and
see if it triggers some warnings ?


I've just tried: it boots without any special warning. What should I
look for? This is an excerpt of dmesg output about the line it would
otherwise stop:


If there is no warning, then the issue is something else, bad luck.

Could you increase the loglevel and try again both with and without 
VMAP_STACK ? Maybe we'll get more information on where it stops.


Christophe



[...]
[ 6.566984] PowerMac i2c bus pmu 2 registered
[ 6.574879] PowerMac i2c bus pmu 1 registered
[ 6.582634] PowerMac i2c bus mac-io 0 registered
[ 6.590323] i2c i2c-2: No i2c address for 
/pci@f200/mac-io@17/i2c@18000/i2c-modem

[ 6.598290] PowerMac i2c bus uni-n 1 registered
[ 6.606196] i2c i2c-3: i2c-powermac: modalias failure on 
/uni-n@f800/i2c@f8001000/cereal@1c0

[ 6.614320] PowerMac i2c bus uni-n 0 registered
[ 6.622501] pmac32_cpufreq: Registering PowerMac CPU frequency driver
[ 6.630580] pmac32_cpufreq: Low: 667 Mhz, High: 867 Mhz, Boot: 667 Mhz
[ 6.639518] ledtrig-cpu: registered to indicate activity on CPUs
[ 6.647894] NET: Registered protocol family 10
[ 6.656492] Segment Routing with IPv6
[ 6.664490] mip6: Mobile IPv6
[ 6.672337] NET: Registered protocol family 17
[ 6.680213] mpls_gso: MPLS GSO support
[...]

Bye,
Giuseppe



Re: [PATCH v7 06/12] powerpc: inline huge vmap supported functions

2020-08-26 Thread Michael Ellerman
Nicholas Piggin  writes:
> This allows unsupported levels to be constant folded away, and so
> p4d_free_pud_page can be removed because it's no longer linked to.
>
> Cc: linuxppc-dev@lists.ozlabs.org
> Signed-off-by: Nicholas Piggin 
> ---
>
> Ack or objection if this goes via the -mm tree? 

Fine by me if it builds.

Acked-by: Michael Ellerman 

cheers

> diff --git a/arch/powerpc/include/asm/vmalloc.h 
> b/arch/powerpc/include/asm/vmalloc.h
> index 105abb73f075..3f0c153befb0 100644
> --- a/arch/powerpc/include/asm/vmalloc.h
> +++ b/arch/powerpc/include/asm/vmalloc.h
> @@ -1,12 +1,25 @@
>  #ifndef _ASM_POWERPC_VMALLOC_H
>  #define _ASM_POWERPC_VMALLOC_H
>  
> +#include 
>  #include 
>  
>  #ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
> -bool arch_vmap_p4d_supported(pgprot_t prot);
> -bool arch_vmap_pud_supported(pgprot_t prot);
> -bool arch_vmap_pmd_supported(pgprot_t prot);
> +static inline bool arch_vmap_p4d_supported(pgprot_t prot)
> +{
> + return false;
> +}
> +
> +static inline bool arch_vmap_pud_supported(pgprot_t prot)
> +{
> + /* HPT does not cope with large pages in the vmalloc area */
> + return radix_enabled();
> +}
> +
> +static inline bool arch_vmap_pmd_supported(pgprot_t prot)
> +{
> + return radix_enabled();
> +}
>  #endif
>  
>  #endif /* _ASM_POWERPC_VMALLOC_H */
> diff --git a/arch/powerpc/mm/book3s64/radix_pgtable.c 
> b/arch/powerpc/mm/book3s64/radix_pgtable.c
> index eca83a50bf2e..27f5837cf145 100644
> --- a/arch/powerpc/mm/book3s64/radix_pgtable.c
> +++ b/arch/powerpc/mm/book3s64/radix_pgtable.c
> @@ -1134,22 +1134,6 @@ void radix__ptep_modify_prot_commit(struct 
> vm_area_struct *vma,
>   set_pte_at(mm, addr, ptep, pte);
>  }
>  
> -bool arch_vmap_pud_supported(pgprot_t prot)
> -{
> - /* HPT does not cope with large pages in the vmalloc area */
> - return radix_enabled();
> -}
> -
> -bool arch_vmap_pmd_supported(pgprot_t prot)
> -{
> - return radix_enabled();
> -}
> -
> -int p4d_free_pud_page(p4d_t *p4d, unsigned long addr)
> -{
> - return 0;
> -}
> -
>  int pud_set_huge(pud_t *pud, phys_addr_t addr, pgprot_t prot)
>  {
>   pte_t *ptep = (pte_t *)pud;
> @@ -1233,8 +1217,3 @@ int pmd_free_pte_page(pmd_t *pmd, unsigned long addr)
>  
>   return 1;
>  }
> -
> -bool arch_vmap_p4d_supported(pgprot_t prot)
> -{
> - return false;
> -}
> -- 
> 2.23.0


Re: Please apply commit 0828137e8f16 ("powerpc/64s: Don't init FSCR_DSCR in __init_FSCR()") to v4.14.y, v4.19.y, v5.4.y, v5.7.y

2020-08-26 Thread Michael Ellerman
Greg KH  writes:
> On Tue, Aug 25, 2020 at 07:44:08PM -0300, Thadeu Lima de Souza Cascardo wrote:
>> After commit 912c0a7f2b5daa3cbb2bc10f303981e493de73bd ("powerpc/64s: Save 
>> FSCR
>> to init_task.thread.fscr after feature init"), which has been applied to the
>> referred branches, when userspace sets the user DSCR MSR, it won't be 
>> inherited
>> or restored during context switch, because the facility unavailable interrupt
>> won't trigger.
>> 
>> Applying 0828137e8f16721842468e33df0460044a0c588b ("powerpc/64s: Don't init
>> FSCR_DSCR in __init_FSCR()") will fix it.

Oops, thanks for catching it.

Acked-by: Michael Ellerman 


> Now queued up, thanks.

Thanks.

cheers


Re: Please apply commit 0828137e8f16 ("powerpc/64s: Don't init FSCR_DSCR in __init_FSCR()") to v4.14.y, v4.19.y, v5.4.y, v5.7.y

2020-08-26 Thread Greg KH
On Tue, Aug 25, 2020 at 07:44:08PM -0300, Thadeu Lima de Souza Cascardo wrote:
> After commit 912c0a7f2b5daa3cbb2bc10f303981e493de73bd ("powerpc/64s: Save FSCR
> to init_task.thread.fscr after feature init"), which has been applied to the
> referred branches, when userspace sets the user DSCR MSR, it won't be 
> inherited
> or restored during context switch, because the facility unavailable interrupt
> won't trigger.
> 
> Applying 0828137e8f16721842468e33df0460044a0c588b ("powerpc/64s: Don't init
> FSCR_DSCR in __init_FSCR()") will fix it.

Now queued up, thanks.

greg k-h


Re: [PATCH v7 07/12] arm64: inline huge vmap supported functions

2020-08-26 Thread Catalin Marinas
On Wed, Aug 26, 2020 at 12:57:48AM +1000, Nicholas Piggin wrote:
> This allows unsupported levels to be constant folded away, and so
> p4d_free_pud_page can be removed because it's no longer linked to.
> 
> Cc: Catalin Marinas 
> Cc: Will Deacon 
> Cc: linux-arm-ker...@lists.infradead.org
> Signed-off-by: Nicholas Piggin 
> ---
> 
> Ack or objection if this goes via the -mm tree?

No objections.

Acked-by: Catalin Marinas 


Re: [PATCH] Revert "powerpc/powernv/idle: Replace CPU feature check with PVR check"

2020-08-26 Thread Vaidyanathan Srinivasan
* Pratik Rajesh Sampat  [2020-08-26 13:59:18]:

> Cpuidle stop state implementation has minor optimizations for P10
> where hardware preserves more SPR registers compared to P9.
> The current P9 driver works for P10, although does few extra
> save-restores. P9 driver can provide the required power management
> features like SMT thread folding and core level power savings
> on a P10 platform.
> 
> Until the P10 stop driver is available, revert the commit which
> allows for only P9 systems to utilize cpuidle and blocks all
> idle stop states for P10.
> Cpu idle states are enabled and tested on the P10 platform
> with this fix.
> 
> This reverts commit 8747bf36f312356f8a295a0c39ff092d65ce75ae.
> 
> Fixes: 8747bf36f312 ("powerpc/powernv/idle: Replace CPU feature check with 
> PVR check")
> Signed-off-by: Pratik Rajesh Sampat 

Reviewed-by: Vaidyanathan Srinivasan 


> ---
>  @mpe: This revert would resolve a staging issue wherein the P10 stop
>  driver is not yet ready while cpuidle stop states need not be blocked
>  on 5.9 for Power10 systems which could cause SMT folding related
>  performance issues.
> 
>  The P10 stop driver is in the works here:
>  https://lists.ozlabs.org/pipermail/linuxppc-dev/2020-August/216773.html
> 
>  arch/powerpc/platforms/powernv/idle.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/arch/powerpc/platforms/powernv/idle.c 
> b/arch/powerpc/platforms/powernv/idle.c
> index 77513a80cef9..345ab062b21a 100644
> --- a/arch/powerpc/platforms/powernv/idle.c
> +++ b/arch/powerpc/platforms/powernv/idle.c
> @@ -1223,7 +1223,7 @@ static void __init pnv_probe_idle_states(void)
>   return;
>   }
>  
> - if (pvr_version_is(PVR_POWER9))
> + if (cpu_has_feature(CPU_FTR_ARCH_300))
>   pnv_power9_idle_init();
>  
>   for (i = 0; i < nr_pnv_idle_states; i++)


This revert solves the stated problem and makes kernel v5.9 work
reasonable well on P10 with stop states which are required for SMT
mode changes.

Complete P10 driver has been in the works and will build on this fix
and complete the required platform support and optimizations.

--Vaidy



Re: [PATCH] Revert "powerpc/powernv/idle: Replace CPU feature check with PVR check"

2020-08-26 Thread Pratik Sampat




On 26/08/20 2:07 pm, Christophe Leroy wrote:



Le 26/08/2020 à 10:29, Pratik Rajesh Sampat a écrit :

Cpuidle stop state implementation has minor optimizations for P10
where hardware preserves more SPR registers compared to P9.
The current P9 driver works for P10, although does few extra
save-restores. P9 driver can provide the required power management
features like SMT thread folding and core level power savings
on a P10 platform.

Until the P10 stop driver is available, revert the commit which
allows for only P9 systems to utilize cpuidle and blocks all
idle stop states for P10.
Cpu idle states are enabled and tested on the P10 platform
with this fix.

This reverts commit 8747bf36f312356f8a295a0c39ff092d65ce75ae.

Fixes: 8747bf36f312 ("powerpc/powernv/idle: Replace CPU feature check 
with PVR check")

Signed-off-by: Pratik Rajesh Sampat 
---
  @mpe: This revert would resolve a staging issue wherein the P10 stop
  driver is not yet ready while cpuidle stop states need not be blocked
  on 5.9 for Power10 systems which could cause SMT folding related
  performance issues.

  The P10 stop driver is in the works here:
https://lists.ozlabs.org/pipermail/linuxppc-dev/2020-August/216773.html

  arch/powerpc/platforms/powernv/idle.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/powerpc/platforms/powernv/idle.c 
b/arch/powerpc/platforms/powernv/idle.c

index 77513a80cef9..345ab062b21a 100644
--- a/arch/powerpc/platforms/powernv/idle.c
+++ b/arch/powerpc/platforms/powernv/idle.c
@@ -1223,7 +1223,7 @@ static void __init pnv_probe_idle_states(void)
  return;
  }
  -    if (pvr_version_is(PVR_POWER9))
+    if (cpu_has_feature(CPU_FTR_ARCH_300))


Why not something like:

if (pvr_version_is(PVR_POWER9) || pvr_version_is(PVR_POWER10))
    pnv_power9_idle_init(); 


In order to use PVR_POWER10 I would need to define it under
arch/powerpc/include/asm/reg.h, which is not present in 5.9 yet.

However, if it okay with @mpe I could split out Nick's P10 stop driver
(https://lists.ozlabs.org/pipermail/linuxppc-dev/2020-August/216773.html)
into two parts:
1. This could include minimal code to introduce the P10 PVR and the
stop wrappers for it. Although this patch internally still calls into
the P9 path. This should gracefully fix the issue.
2. Then later in this patch we could introduce the p10 callback
methods as they are in Nick's series.

---
Thanks
Pratik




  pnv_power9_idle_init();
    for (i = 0; i < nr_pnv_idle_states; i++)



Christophe




Re: [PATCH] Revert "powerpc/powernv/idle: Replace CPU feature check with PVR check"

2020-08-26 Thread Christophe Leroy




Le 26/08/2020 à 10:29, Pratik Rajesh Sampat a écrit :

Cpuidle stop state implementation has minor optimizations for P10
where hardware preserves more SPR registers compared to P9.
The current P9 driver works for P10, although does few extra
save-restores. P9 driver can provide the required power management
features like SMT thread folding and core level power savings
on a P10 platform.

Until the P10 stop driver is available, revert the commit which
allows for only P9 systems to utilize cpuidle and blocks all
idle stop states for P10.
Cpu idle states are enabled and tested on the P10 platform
with this fix.

This reverts commit 8747bf36f312356f8a295a0c39ff092d65ce75ae.

Fixes: 8747bf36f312 ("powerpc/powernv/idle: Replace CPU feature check with PVR 
check")
Signed-off-by: Pratik Rajesh Sampat 
---
  @mpe: This revert would resolve a staging issue wherein the P10 stop
  driver is not yet ready while cpuidle stop states need not be blocked
  on 5.9 for Power10 systems which could cause SMT folding related
  performance issues.

  The P10 stop driver is in the works here:
  https://lists.ozlabs.org/pipermail/linuxppc-dev/2020-August/216773.html

  arch/powerpc/platforms/powernv/idle.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/powerpc/platforms/powernv/idle.c 
b/arch/powerpc/platforms/powernv/idle.c
index 77513a80cef9..345ab062b21a 100644
--- a/arch/powerpc/platforms/powernv/idle.c
+++ b/arch/powerpc/platforms/powernv/idle.c
@@ -1223,7 +1223,7 @@ static void __init pnv_probe_idle_states(void)
return;
}
  
-	if (pvr_version_is(PVR_POWER9))

+   if (cpu_has_feature(CPU_FTR_ARCH_300))


Why not something like:

if (pvr_version_is(PVR_POWER9) || pvr_version_is(PVR_POWER10))
pnv_power9_idle_init();



pnv_power9_idle_init();
  
  	for (i = 0; i < nr_pnv_idle_states; i++)




Christophe


[PATCH] Revert "powerpc/powernv/idle: Replace CPU feature check with PVR check"

2020-08-26 Thread Pratik Rajesh Sampat
Cpuidle stop state implementation has minor optimizations for P10
where hardware preserves more SPR registers compared to P9.
The current P9 driver works for P10, although does few extra
save-restores. P9 driver can provide the required power management
features like SMT thread folding and core level power savings
on a P10 platform.

Until the P10 stop driver is available, revert the commit which
allows for only P9 systems to utilize cpuidle and blocks all
idle stop states for P10.
Cpu idle states are enabled and tested on the P10 platform
with this fix.

This reverts commit 8747bf36f312356f8a295a0c39ff092d65ce75ae.

Fixes: 8747bf36f312 ("powerpc/powernv/idle: Replace CPU feature check with PVR 
check")
Signed-off-by: Pratik Rajesh Sampat 
---
 @mpe: This revert would resolve a staging issue wherein the P10 stop
 driver is not yet ready while cpuidle stop states need not be blocked
 on 5.9 for Power10 systems which could cause SMT folding related
 performance issues.

 The P10 stop driver is in the works here:
 https://lists.ozlabs.org/pipermail/linuxppc-dev/2020-August/216773.html

 arch/powerpc/platforms/powernv/idle.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/powerpc/platforms/powernv/idle.c 
b/arch/powerpc/platforms/powernv/idle.c
index 77513a80cef9..345ab062b21a 100644
--- a/arch/powerpc/platforms/powernv/idle.c
+++ b/arch/powerpc/platforms/powernv/idle.c
@@ -1223,7 +1223,7 @@ static void __init pnv_probe_idle_states(void)
return;
}
 
-   if (pvr_version_is(PVR_POWER9))
+   if (cpu_has_feature(CPU_FTR_ARCH_300))
pnv_power9_idle_init();
 
for (i = 0; i < nr_pnv_idle_states; i++)
-- 
2.25.4



Re: [PATCH] selftests/powerpc: Fix prefixes in alignment_handler signal handler

2020-08-26 Thread Jordan Niethe
On Mon, Aug 24, 2020 at 11:12 PM Jordan Niethe  wrote:
>
> The signal handler in the alignment handler self test has the ability to
> jump over the instruction that triggered the signal. It does this by
> incrementing the PT_NIP in the user context by 4. If it were a prefixed
> instruction this will mean that the suffix is then executed which is
> incorrect. Instead check if the major opcode indicates a prefixed
> instruction (e.g. it is 1) and if so increment PT_NIP by 8.
>
> If ISA v3.1 is not available treat it as a word instruction even if the
> major opcode is 1.
>
> Fixes: 620a6473df36 ("selftests/powerpc: Add prefixed loads/stores to
> alignment_handler test")
> Signed-off-by: Jordan Niethe 
> ---
>  .../selftests/powerpc/alignment/alignment_handler.c   | 11 ++-
>  1 file changed, 10 insertions(+), 1 deletion(-)
>
> diff --git a/tools/testing/selftests/powerpc/alignment/alignment_handler.c 
> b/tools/testing/selftests/powerpc/alignment/alignment_handler.c
> index 55ef15184057..c197ff828120 100644
> --- a/tools/testing/selftests/powerpc/alignment/alignment_handler.c
> +++ b/tools/testing/selftests/powerpc/alignment/alignment_handler.c
> @@ -64,12 +64,14 @@ int bufsize;
>  int debug;
>  int testing;
>  volatile int gotsig;
> +bool haveprefixes;
>  char *cipath = "/dev/fb0";
>  long cioffset;
>
>  void sighandler(int sig, siginfo_t *info, void *ctx)
>  {
> ucontext_t *ucp = ctx;
> +   u32 inst;
Oh this should be befine __powerpc64__/CONFIG_PPC64 (thank you patchwork).
>
> if (!testing) {
> signal(sig, SIG_DFL);
> @@ -77,7 +79,12 @@ void sighandler(int sig, siginfo_t *info, void *ctx)
> }
> gotsig = sig;
>  #ifdef __powerpc64__
> -   ucp->uc_mcontext.gp_regs[PT_NIP] += 4;
> +   if (haveprefixes) {
> +   inst = *(u32 *)ucp->uc_mcontext.gp_regs[PT_NIP];
> +   ucp->uc_mcontext.gp_regs[PT_NIP] += ((inst >> 26 == 1) ? 8 : 
> 4);
> +   } else {
> +   ucp->uc_mcontext.gp_regs[PT_NIP] += 4;
> +   }
>  #else
> ucp->uc_mcontext.uc_regs->gregs[PT_NIP] += 4;
>  #endif
> @@ -648,6 +655,8 @@ int main(int argc, char *argv[])
> exit(1);
> }
>
> +   haveprefixes = have_hwcap2(PPC_FEATURE2_ARCH_3_1);
> +
> rc |= test_harness(test_alignment_handler_vsx_206,
>"test_alignment_handler_vsx_206");
> rc |= test_harness(test_alignment_handler_vsx_207,
> --
> 2.17.1
>


[PATCH] powerpc/perf: Fix reading of MSR[HV PR] bits in trace-imc

2020-08-26 Thread Athira Rajeev
IMC trace-mode uses MSR[HV PR] bits to set the cpumode
for the instruction pointer captured in each sample.
The bits are fetched from third DW of the trace record.
Reading third DW from IMC trace record should use be64_to_cpu
along with READ_ONCE inorder to fetch correct MSR[HV PR] bits.
Patch addresses this change.

Currently we are using `PERF_RECORD_MISC_HYPERVISOR` as
cpumode if MSR HV is 1 and PR is 0 which means the address is from
host counter. But using `PERF_RECORD_MISC_HYPERVISOR` for host
counter data will fail to resolve the `address -> symbol` during
`perf report` because perf tools side uses `PERF_RECORD_MISC_KERNEL`
to represent the host counter data. Therefore, fix the trace imc
sample data to use `PERF_RECORD_MISC_KERNEL` as cpumode for
host kernel information.

Fixes: 77ca3951cc37 ("powerpc/perf: Add kernel support for new
MSR[HV PR] bits in trace-imc")
Signed-off-by: Athira Rajeev 
---
 arch/powerpc/perf/imc-pmu.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/powerpc/perf/imc-pmu.c b/arch/powerpc/perf/imc-pmu.c
index a45d694..62d0b54 100644
--- a/arch/powerpc/perf/imc-pmu.c
+++ b/arch/powerpc/perf/imc-pmu.c
@@ -1289,7 +1289,7 @@ static int trace_imc_prepare_sample(struct trace_imc_data 
*mem,
header->misc = 0;
 
if (cpu_has_feature(CPU_FTR_ARCH_31)) {
-   switch (IMC_TRACE_RECORD_VAL_HVPR(mem->val)) {
+   switch 
(IMC_TRACE_RECORD_VAL_HVPR(be64_to_cpu(READ_ONCE(mem->val {
case 0:/* when MSR HV and PR not set in the trace-record */
header->misc |= PERF_RECORD_MISC_GUEST_KERNEL;
break;
@@ -1297,7 +1297,7 @@ static int trace_imc_prepare_sample(struct trace_imc_data 
*mem,
header->misc |= PERF_RECORD_MISC_GUEST_USER;
break;
case 2: /* MSR HV is 1 and PR is 0 */
-   header->misc |= PERF_RECORD_MISC_HYPERVISOR;
+   header->misc |= PERF_RECORD_MISC_KERNEL;
break;
case 3: /* MSR HV is 1 and PR is 1 */
header->misc |= PERF_RECORD_MISC_USER;
-- 
1.8.3.1



Re: fsl_espi errors on v5.7.15

2020-08-26 Thread Heiner Kallweit
On 26.08.2020 08:07, Chris Packham wrote:
> 
> On 26/08/20 1:48 pm, Chris Packham wrote:
>>
>> On 26/08/20 10:22 am, Chris Packham wrote:
>>> On 25/08/20 7:22 pm, Heiner Kallweit wrote:
>>>
>>> 
 I've been staring at spi-fsl-espi.c for while now and I think I've
> identified a couple of deficiencies that may or may not be related 
> to my
> issue.
>
> First I think the 'Transfer done but SPIE_DON isn't set' message 
> can be
> generated spuriously. In fsl_espi_irq() we read the ESPI_SPIE 
> register.
> We also write back to it to clear the current events. We re-read it in
> fsl_espi_cpu_irq() and complain when SPIE_DON is not set. But we can
> naturally end up in that situation if we're doing a large read. 
> Consider
> the messages for reading a block of data from a spi-nor chip
>
>    tx = READ_OP + ADDR
>    rx = data
>
> We setup the transfer and pump out the tx_buf. The first interrupt 
> goes
> off and ESPI_SPIE has SPIM_DON and SPIM_RXT set. We empty the rx fifo,
> clear ESPI_SPIE and wait for the next interrupt. The next interrupt
> fires and this time we have ESPI_SPIE with just SPIM_RXT set. This
> continues until we've received all the data and we finish with 
> ESPI_SPIE
> having only SPIM_RXT set. When we re-read it we complain that SPIE_DON
> isn't set.
>
> The other deficiency is that we only get an interrupt when the 
> amount of
> data in the rx fifo is above FSL_ESPI_RXTHR. If there are fewer than
> FSL_ESPI_RXTHR left to be received we will never pull them out of 
> the fifo.
>
 SPIM_DON will trigger an interrupt once the last characters have been
 transferred, and read the remaining characters from the FIFO.
>>>
>>> The T2080RM that I have says the following about the DON bit
>>>
>>> "Last character was transmitted. The last character was transmitted 
>>> and a new command can be written for the next frame."
>>>
>>> That does at least seem to fit with my assertion that it's all about 
>>> the TX direction. But the fact that it doesn't happen all the time 
>>> throws some doubt on it.
>>>
 I think the reason I'm seeing some variability is because of how fast
> (or slow) the interrupts get processed and how fast the spi-nor 
> chip can
> fill the CPUs rx fifo.
>
 To rule out timing issues at high bus frequencies I initially asked
 for re-testing at lower frequencies. If you e.g. limit the bus to 1 MHz
 or even less, then timing shouldn't be an issue.
>>> Yes I've currently got spi-max-frequency = <100>; in my dts. I 
>>> would also expect a slower frequency would fit my "DON is for TX" 
>>> narrative.
 Last relevant functional changes have been done almost 4 years ago.
 And yours is the first such report I see. So question is what could 
 be so
 special with your setup that it seems you're the only one being 
 affected.
 The scenarios you describe are standard, therefore much more people
 should be affected in case of a driver bug.
>>> Agreed. But even on my hardware (which may have a latent issue 
>>> despite being in the field for going on 5 years) the issue only 
>>> triggers under some fairly specific circumstances.
 You said that kernel config impacts how frequently the issue happens.
 Therefore question is what's the diff in kernel config, and how could
 the differences be related to SPI.
>>>
>>> It did seem to be somewhat random. Things like CONFIG_PREEMPT have an 
>>> impact but every time I found something that seemed to be having an 
>>> impact I've been able to disprove it. I actually think its about how 
>>> busy the system is which may or may not affect when we get round to 
>>> processing the interrupts.
>>>
>>> I have managed to get the 'Transfer done but SPIE_DON isn't set!' to 
>>> occur on the T2080RDB.
>>>
>>> I've had to add the following to expose the environment as a mtd 
>>> partition
>>>
>>> diff --git a/arch/powerpc/boot/dts/fsl/t208xrdb.dtsi 
>>> b/arch/powerpc/boot/dts/fsl/t208xrdb.dtsi
>>> index ff87e67c70da..fbf95fc1fd68 100644
>>> --- a/arch/powerpc/boot/dts/fsl/t208xrdb.dtsi
>>> +++ b/arch/powerpc/boot/dts/fsl/t208xrdb.dtsi
>>> @@ -116,6 +116,15 @@ flash@0 {
>>>     compatible = "micron,n25q512ax3", 
>>> "jedec,spi-nor";
>>>     reg = <0>;
>>>     spi-max-frequency = <1000>; /* 
>>> input clock */
>>> +
>>> +   partition@u-boot {
>>> +    reg = <0x 0x0010>;
>>> +    label = "u-boot";
>>> +    };
>>> +    partition@u-boot-env {
>>> +    reg = <0x0010 0x0001>;
>>> +    label = "u-boot-env";
>>> +   

Re: fsl_espi errors on v5.7.15

2020-08-26 Thread Chris Packham

On 26/08/20 1:48 pm, Chris Packham wrote:
>
> On 26/08/20 10:22 am, Chris Packham wrote:
>> On 25/08/20 7:22 pm, Heiner Kallweit wrote:
>>
>> 
>>> I've been staring at spi-fsl-espi.c for while now and I think I've
 identified a couple of deficiencies that may or may not be related 
 to my
 issue.

 First I think the 'Transfer done but SPIE_DON isn't set' message 
 can be
 generated spuriously. In fsl_espi_irq() we read the ESPI_SPIE 
 register.
 We also write back to it to clear the current events. We re-read it in
 fsl_espi_cpu_irq() and complain when SPIE_DON is not set. But we can
 naturally end up in that situation if we're doing a large read. 
 Consider
 the messages for reading a block of data from a spi-nor chip

    tx = READ_OP + ADDR
    rx = data

 We setup the transfer and pump out the tx_buf. The first interrupt 
 goes
 off and ESPI_SPIE has SPIM_DON and SPIM_RXT set. We empty the rx fifo,
 clear ESPI_SPIE and wait for the next interrupt. The next interrupt
 fires and this time we have ESPI_SPIE with just SPIM_RXT set. This
 continues until we've received all the data and we finish with 
 ESPI_SPIE
 having only SPIM_RXT set. When we re-read it we complain that SPIE_DON
 isn't set.

 The other deficiency is that we only get an interrupt when the 
 amount of
 data in the rx fifo is above FSL_ESPI_RXTHR. If there are fewer than
 FSL_ESPI_RXTHR left to be received we will never pull them out of 
 the fifo.

>>> SPIM_DON will trigger an interrupt once the last characters have been
>>> transferred, and read the remaining characters from the FIFO.
>>
>> The T2080RM that I have says the following about the DON bit
>>
>> "Last character was transmitted. The last character was transmitted 
>> and a new command can be written for the next frame."
>>
>> That does at least seem to fit with my assertion that it's all about 
>> the TX direction. But the fact that it doesn't happen all the time 
>> throws some doubt on it.
>>
>>> I think the reason I'm seeing some variability is because of how fast
 (or slow) the interrupts get processed and how fast the spi-nor 
 chip can
 fill the CPUs rx fifo.

>>> To rule out timing issues at high bus frequencies I initially asked
>>> for re-testing at lower frequencies. If you e.g. limit the bus to 1 MHz
>>> or even less, then timing shouldn't be an issue.
>> Yes I've currently got spi-max-frequency = <100>; in my dts. I 
>> would also expect a slower frequency would fit my "DON is for TX" 
>> narrative.
>>> Last relevant functional changes have been done almost 4 years ago.
>>> And yours is the first such report I see. So question is what could 
>>> be so
>>> special with your setup that it seems you're the only one being 
>>> affected.
>>> The scenarios you describe are standard, therefore much more people
>>> should be affected in case of a driver bug.
>> Agreed. But even on my hardware (which may have a latent issue 
>> despite being in the field for going on 5 years) the issue only 
>> triggers under some fairly specific circumstances.
>>> You said that kernel config impacts how frequently the issue happens.
>>> Therefore question is what's the diff in kernel config, and how could
>>> the differences be related to SPI.
>>
>> It did seem to be somewhat random. Things like CONFIG_PREEMPT have an 
>> impact but every time I found something that seemed to be having an 
>> impact I've been able to disprove it. I actually think its about how 
>> busy the system is which may or may not affect when we get round to 
>> processing the interrupts.
>>
>> I have managed to get the 'Transfer done but SPIE_DON isn't set!' to 
>> occur on the T2080RDB.
>>
>> I've had to add the following to expose the environment as a mtd 
>> partition
>>
>> diff --git a/arch/powerpc/boot/dts/fsl/t208xrdb.dtsi 
>> b/arch/powerpc/boot/dts/fsl/t208xrdb.dtsi
>> index ff87e67c70da..fbf95fc1fd68 100644
>> --- a/arch/powerpc/boot/dts/fsl/t208xrdb.dtsi
>> +++ b/arch/powerpc/boot/dts/fsl/t208xrdb.dtsi
>> @@ -116,6 +116,15 @@ flash@0 {
>>     compatible = "micron,n25q512ax3", 
>> "jedec,spi-nor";
>>     reg = <0>;
>>     spi-max-frequency = <1000>; /* 
>> input clock */
>> +
>> +   partition@u-boot {
>> +    reg = <0x 0x0010>;
>> +    label = "u-boot";
>> +    };
>> +    partition@u-boot-env {
>> +    reg = <0x0010 0x0001>;
>> +    label = "u-boot-env";
>> +    };
>>     };
>>     };
>>
>> And I'm using the following script to poke at the environment 
>> (warning if