"Aneesh Kumar K.V" writes:
> UAMOR values are not application-specific.
It used to be, that's worth mentioning.
> The kernel initializes its value based on different reserved keys.
> Remove the thread-specific UAMOR value and don't switch the UAMOR on
> context switch.
>
> Move UAMOR initialization to key initialization code. Now that
> KUAP/KUEP feature depends on PPC_MEM_KEYS, we can start to consolidate
> all register initialization to keys init.
>
> Signed-off-by: Aneesh Kumar K.V
> ---
> arch/powerpc/include/asm/book3s/64/kup.h | 2 ++
> arch/powerpc/include/asm/processor.h | 1 -
> arch/powerpc/kernel/ptrace/ptrace-view.c | 17
> arch/powerpc/kernel/smp.c| 5
> arch/powerpc/mm/book3s64/pkeys.c | 35 ++--
> 5 files changed, 39 insertions(+), 21 deletions(-)
>
> diff --git a/arch/powerpc/include/asm/book3s/64/kup.h
> b/arch/powerpc/include/asm/book3s/64/kup.h
> index 3a0e138d2735..942594745dfa 100644
> --- a/arch/powerpc/include/asm/book3s/64/kup.h
> +++ b/arch/powerpc/include/asm/book3s/64/kup.h
> @@ -67,6 +67,8 @@
> #include
> #include
>
> +extern u64 default_uamor;
> +
> static inline void kuap_restore_amr(struct pt_regs *regs, unsigned long amr)
> {
> if (mmu_has_feature(MMU_FTR_KUAP) && unlikely(regs->kuap != amr)) {
> diff --git a/arch/powerpc/include/asm/processor.h
> b/arch/powerpc/include/asm/processor.h
> index 52a67835057a..6ac12168f1fe 100644
> --- a/arch/powerpc/include/asm/processor.h
> +++ b/arch/powerpc/include/asm/processor.h
> @@ -237,7 +237,6 @@ struct thread_struct {
> #ifdef CONFIG_PPC_MEM_KEYS
> unsigned long amr;
> unsigned long iamr;
> - unsigned long uamor;
> #endif
> #ifdef CONFIG_KVM_BOOK3S_32_HANDLER
> void* kvm_shadow_vcpu; /* KVM internal data */
> diff --git a/arch/powerpc/kernel/ptrace/ptrace-view.c
> b/arch/powerpc/kernel/ptrace/ptrace-view.c
> index caeb5822a8f4..689711eb018a 100644
> --- a/arch/powerpc/kernel/ptrace/ptrace-view.c
> +++ b/arch/powerpc/kernel/ptrace/ptrace-view.c
> @@ -488,14 +488,22 @@ 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,
> unsigned int pos, unsigned int count, void *kbuf, void
> __user *ubuf)
> {
> + int ret;
> +
> BUILD_BUG_ON(TSO(amr) + sizeof(unsigned long) != TSO(iamr));
> - BUILD_BUG_ON(TSO(iamr) + sizeof(unsigned long) != TSO(uamor));
>
> if (!arch_pkeys_enabled())
> return -ENODEV;
>
> - return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
> &target->thread.amr,
> -0, ELF_NPKEY * sizeof(unsigned long));
> + ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
> &target->thread.amr,
> + 0, 2 * sizeof(unsigned long));
> + if (ret)
> + goto err_out;
Why not just return?
> +
> + ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &default_uamor,
> + 2 * sizeof(unsigned long), 3 *
> sizeof(unsigned long));
> +err_out:
> + return ret;
> }
>
> static int pkey_set(struct task_struct *target, const struct user_regset
> *regset,
> @@ -518,8 +526,7 @@ static int pkey_set(struct task_struct *target, const
> struct user_regset *regset
> return ret;
>
> /* UAMOR determines which bits of the AMR can be set from userspace. */
> - target->thread.amr = (new_amr & target->thread.uamor) |
> - (target->thread.amr & ~target->thread.uamor);
> + target->thread.amr = (new_amr & default_uamor) | (target->thread.amr &
> ~default_uamor);
That comment could explain better why we are bothering to mask with
~default_uamor.
> return 0;
> }
> diff --git a/arch/powerpc/kernel/smp.c b/arch/powerpc/kernel/smp.c
> index c820c95162ff..eec40082599f 100644
> --- a/arch/powerpc/kernel/smp.c
> +++ b/arch/powerpc/kernel/smp.c
> @@ -59,6 +59,7 @@
> #include
> #include
> #include
> +#include
>
> #ifdef DEBUG
> #include
> @@ -1256,6 +1257,10 @@ void start_secondary(void *unused)
> mmgrab(&init_mm);
> current->active_mm = &init_mm;
>
> +#ifdef CONFIG_PPC_MEM_KEYS
> + mtspr(SPRN_UAMOR, default_uamor);
> +#endif
That's 1) not very pretty and 2) risks blowing up on other CPUs.
It should at least go in early_init_mmu_secondary().
> smp_store_cpu_info(cpu);
> set_dec(tb_ticks_per_jiffy);
> preempt_disable();
> diff --git a/arch/powerpc/mm/book3s64/pkeys.c
> b/arch/powerpc/mm/book3s64/pkeys.c
> index aeecc8b8e11c..3f3593f85358 100644
> --- a/arch/powerpc/mm/book3s64/pkeys.c
> +++ b/arch/powerpc/mm/book3s64/pkeys.c
> @@ -24,7 +24,7 @@ static u32 initial_allocation_mask; /* Bits set for the
> initially allocated k
> static u64 default_amr;
> static u64 default_iamr;
> /* Allow all keys to be modified by default */
> -st