powerpc/pkeys: fixes to pkeys
cover powerpc/pkeys: fixes to pkeys
Assortment of fixes to pkey.
Patch 1 makes pkey consumable in multithreaded applications.
Patch 2 fixes fork behavior to inherit the key attributes.
Patch 3 A off-by-one bug made one key unusable. Fixes it.
Patch 4 Makes pkey-0 less special.
Ram Pai (5):
powerpc/pkeys: Enable all user-allocatable pkeys at init. powerpc/pkeys: Save the pkey registers before fork powerpc/pkeys: fix calculation of total pkeys. powerpc/pkeys: Preallocate execute-only key powerpc/pkeys: make protection key 0 less special
arch/powerpc/include/asm/pkeys.h | 29 ++++++++-- arch/powerpc/kernel/process.c | 1 + arch/powerpc/mm/pkeys.c | 107 ++++++++++++++------------------------ 3 files changed, 64 insertions(+), 73 deletions(-)
PATCH 1/5 powerpc/pkeys: Enable all user-allocatable pkeys at init.
In a multithreaded application, a key allocated by one thread must be activate and usable on all threads.
Currently this is not the case, because the UAMOR bits for all keys are disabled by default. When a new key is allocated in one thread, though the corresponding UAMOR bits for that thread get enabled, the UAMOR bits for all other existing threads continue to have their bits disabled. Other threads have no way to set permissions on the key, effectively making the key useless.
Enable the UAMOR bits for all keys, at process creation. Since the contents of UAMOR are inherited at fork, all threads are capable of modifying the permissions on any key.
BTW: changing the permission on unallocated keys has no effect, till those keys are not associated with any PTEs. The kernel will anyway disallow to association of unallocated keys with PTEs.
--- arch/powerpc/mm/pkeys.c | 47 +++++++++++++++++++++++++++++------------------ 1 files changed, 29 insertions(+), 18 deletions(-)
arch/powerpc/mm/pkeys.c +29 -18
@@ -15,8 +15,9 @@int pkeys_total;/* Total pkeys as per device tree */ bool pkeys_devtree_defined;/* pkey property exported by device tree */ u32 initial_allocation_mask;/* Bits set for reserved keys */ -u64 pkey_amr_uamor_mask;/* Bits in AMR/UMOR not to be touched */ +u64 pkey_amr_mask;/* Bits in AMR not to be touched */ u64 pkey_iamr_mask;/* Bits in AMR not to be touched */ +u64 pkey_uamor_mask;/* Bits in UMOR not to be touched */ #define AMR_BITS_PER_PKEY 2#define AMR_RD_BIT 0x1UL
@@ -119,20 +120,29 @@ int pkey_initialize(void)#else os_reserved =0 ;#endif - initial_allocation_mask = ~0x0 ; - pkey_amr_uamor_mask = ~0x0ul ; + initial_allocation_mask = (0x1 <<0 ) | (0x1 <<1 ); + +/* register mask is in BE format */ + pkey_amr_mask = ~0x0ul ; pkey_iamr_mask = ~0x0ul ; -/* -* key 0, 1 are reserved. -* key 0 is the default key, which allows read/write/execute. -* key 1 is recommended not to be used. PowerISA(3.0) page 1015, -* programming note. -*/ -for (i =2 ; i < (pkeys_total - os_reserved); i++) { - initial_allocation_mask &= ~(0x1 << i); - pkey_amr_uamor_mask &= ~(0x3ul <<pkeyshift (i)); + +for (i =0 ; i < (pkeys_total - os_reserved); i++) { + pkey_amr_mask &= ~(0x3ul <<pkeyshift (i)); pkey_iamr_mask &= ~(0x1ul <<pkeyshift (i)); } + + pkey_uamor_mask = ~0x0ul ; + pkey_uamor_mask &= ~(0x3ul <<pkeyshift (0 )); +/* +* key 1 is recommended not to be used. +* PowerISA(3.0) page 1015, +* @TODO: Revisit this. This is only applicable on +* pseries kernel running on powerVM. +*/ + pkey_uamor_mask &= ~(0x3ul <<pkeyshift (1 )); +for (i = (pkeys_total - os_reserved); i < pkeys_total; i++) + pkey_uamor_mask &= ~(0x3ul <<pkeyshift (i)); +return 0 ; }
@@ -289,9 +299,6 @@ void thread_pkey_regs_restore(struct thread_struct *new_thread,if (static_branch_likely (&pkey_disabled))return ; -/* -* TODO: Just set UAMOR to zero if @new_thread hasn't used any keys yet. -*/ if (old_thread->amr != new_thread->amr)write_amr (new_thread->amr);if (old_thread->iamr != new_thread->iamr)
@@ -305,9 +312,13 @@ void thread_pkey_regs_init(struct thread_struct *thread)if (static_branch_likely (&pkey_disabled))return ; - thread->amr =read_amr () & pkey_amr_uamor_mask; - thread->iamr =read_iamr () & pkey_iamr_mask; - thread->uamor =read_uamor () & pkey_amr_uamor_mask; + thread->amr = pkey_amr_mask; + thread->iamr = pkey_iamr_mask; + thread->uamor = pkey_uamor_mask; + +write_uamor (pkey_uamor_mask); +write_amr (pkey_amr_mask); +write_iamr (pkey_iamr_mask); }static inline bool pkey_allows_readwrite (int pkey)
Signature · 1 line
1.7.1
PATCH 2/5 powerpc/pkeys: Save the pkey registers before fork
In reply to Ram PaiWhen a thread forks the contents of AMR, IAMR, UAMOR registers in the newly forked thread are not inherited.
Save the registers before forking, for content of those registers to be automatically copied into the new thread.
--- arch/powerpc/kernel/process.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
arch/powerpc/kernel/process.c +1 -0
@@ -582,6 +582,7 @@ static void save_all(struct task_struct *tsk)__giveup_spe (tsk);msr_check_and_clear (msr_all_available); +thread_pkey_regs_save (&tsk->thread); }void flush_all_to_thread (struct task_struct *tsk)
Signature · 1 line
1.7.1
PATCH 3/5 powerpc/pkeys: fix calculation of total pkeys.
In reply to Ram PaiTotal number of pkeys calculation is off by 1. Fix it.
--- arch/powerpc/mm/pkeys.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
arch/powerpc/mm/pkeys.c +1 -1
@@ -92,7 +92,7 @@ int pkey_initialize(void)* arch-neutral code. */ pkeys_total =min_t (int, pkeys_total, - (ARCH_VM_PKEY_FLAGS >>VM_PKEY_SHIFT )); + ((ARCH_VM_PKEY_FLAGS >>VM_PKEY_SHIFT )+1 ));if (!pkey_mmu_enabled () ||radix_enabled () || !pkeys_total)static_branch_enable (&pkey_disabled);
Signature · 1 line
1.7.1
PATCH 4/5 powerpc/pkeys: Preallocate execute-only key
In reply to Ram Paiexecute-only key is allocated dynamically. This is a problem. When a thread implicitly creates a execute-only key, and resets UAMOR for that key, the UAMOR value does not percolate to all the other threads. Any other thread may ignorantly change the permissions on the key. This can cause the key to be not execute-only for that thread.
Preallocate the execute-only key and ensure that no thread can change the permission of the key, by resetting the corresponding bit in UAMOR.
--- arch/powerpc/mm/pkeys.c | 53 +++++++--------------------------------------- 1 files changed, 8 insertions(+), 45 deletions(-)
arch/powerpc/mm/pkeys.c +8 -45
@@ -25,6 +25,7 @@#define IAMR_EX_BIT 0x1UL#define PKEY_REG_BITS (sizeof(u64)*8)#define pkeyshift (pkey) (PKEY_REG_BITS - ((pkey+1) * AMR_BITS_PER_PKEY)) +#define EXECUTE_ONLY_KEY 2static void scan_pkey_feature (void ) {
@@ -120,7 +121,8 @@ int pkey_initialize(void)#else os_reserved =0 ;#endif - initial_allocation_mask = (0x1 <<0 ) | (0x1 <<1 ); + initial_allocation_mask = (0x1 <<0 ) | (0x1 <<1 ) | + (0x1 <<EXECUTE_ONLY_KEY );/* register mask is in BE format */ pkey_amr_mask = ~0x0ul ;
@@ -130,6 +132,7 @@ int pkey_initialize(void) pkey_amr_mask &= ~(0x3ul <<pkeyshift (i)); pkey_iamr_mask &= ~(0x1ul <<pkeyshift (i)); } + pkey_amr_mask |= (AMR_RD_BIT |AMR_WR_BIT ) <<pkeyshift (EXECUTE_ONLY_KEY ); pkey_uamor_mask = ~0x0ul ; pkey_uamor_mask &= ~(0x3ul <<pkeyshift (0 ));
@@ -140,6 +143,8 @@ int pkey_initialize(void)* pseries kernel running on powerVM. */ pkey_uamor_mask &= ~(0x3ul <<pkeyshift (1 )); + pkey_uamor_mask &= ~(0x3ul <<pkeyshift (EXECUTE_ONLY_KEY )); +for (i = (pkeys_total - os_reserved); i < pkeys_total; i++) pkey_uamor_mask &= ~(0x3ul <<pkeyshift (i));
@@ -153,8 +158,7 @@ void pkey_mm_init(struct mm_struct *mm)if (static_branch_likely (&pkey_disabled))return ;mm_pkey_allocation_map (mm) = initial_allocation_mask; -/* -1 means unallocated or invalid */ - mm->context.execute_only_pkey =-1 ; + mm->context.execute_only_pkey =EXECUTE_ONLY_KEY ; }static inline u64 read_amr (void )
@@ -333,48 +337,7 @@ static inline bool pkey_allows_readwrite(int pkey)int __execute_only_pkey (struct mm_struct *mm) { -bool need_to_set_mm_pkey = false; -int execute_only_pkey = mm->context.execute_only_pkey; -int ret; - -/* Do we need to assign a pkey for mm's execute-only maps? */ -if (execute_only_pkey ==-1 ) { -/* Go allocate one to use, which might fail */ - execute_only_pkey =mm_pkey_alloc (mm); -if (execute_only_pkey <0 ) -return -1 ; - need_to_set_mm_pkey = true; - } - -/* -* We do not want to go through the relatively costly dance to set AMR -* if we do not need to. Check it first and assume that if the -* execute-only pkey is readwrite-disabled than we do not have to set it -* ourselves. -*/ -if (!need_to_set_mm_pkey && !pkey_allows_readwrite (execute_only_pkey)) -return execute_only_pkey; - -/* -* Set up AMR so that it denies access for everything other than -* execution. -*/ - ret =__arch_set_user_pkey_access (current, execute_only_pkey, -PKEY_DISABLE_ACCESS | -PKEY_DISABLE_WRITE ); -/* -* If the AMR-set operation failed somehow, just return 0 and -* effectively disable execute-only support. -*/ -if (ret) { -mm_pkey_free (mm, execute_only_pkey); -return -1 ; - } - -/* We got one, store it and use it from here on out */ -if (need_to_set_mm_pkey) - mm->context.execute_only_pkey = execute_only_pkey; -return execute_only_pkey; +return mm->context.execute_only_pkey; }static inline bool vma_is_pkey_exec_only (struct vm_area_struct *vma)
Signature · 1 line
1.7.1
PATCH 5/5 powerpc/pkeys: make protection key 0 less special
In reply to Ram PaiApplications need the ability to associate an address-range with some key and latter revert to its initial default key. Pkey-0 comes close to providing this function but falls short, because the current implementation disallows applications to explicitly associate pkey-0 to the address range.
Lets make pkey-0 less special and treat it almost like any other key. Thus it can be explicitly associated with any address range, and can be freed. This gives the application more flexibility and power. The ability to free pkey-0 must be used responsibily, since pkey-0 is associated with almost all address-range by default.
Even with this change pkey-0 continues to be slightly more special from the following point of view.
(a) it is implicitly allocated.
(b) it is the default key assigned to any address-range.
(c) its permissions cannot be modified by userspace.
NOTE: (c) is specific to powerpc only. pkey-0 is associated by default with all pages including kernel pages, and pkeys are also active in kernel mode. If any permission is denied on pkey-0, the kernel running in the context of the application will be unable to operate.
Tested on powerpc.
-----------------------------------------------------------------------
History:
v4: . introduced PKEY_0 macro. No bug fixes. Code
re-arrangement to save a few cycles.
v3: . Corrected a comment in arch_set_user_pkey_access(). . Clarified the header, to capture the notion that pkey-0 permissions cannot be modified by userspace on powerpc.
-- comment from Thiago
v2: . mm_pkey_is_allocated() continued to treat pkey-0 special.
fixed it.
--- arch/powerpc/include/asm/pkeys.h | 29 +++++++++++++++++++++++------ arch/powerpc/mm/pkeys.c | 13 ++++++------- 2 files changed, 29 insertions(+), 13 deletions(-)
arch/powerpc/include/asm/pkeys.h +23 -6
@@ -13,7 +13,10 @@DECLARE_STATIC_KEY_TRUE (pkey_disabled);extern int pkeys_total;/* total pkeys as per device tree */ -extern u32 initial_allocation_mask;/* bits set for reserved keys */ +extern u32 initial_allocation_mask;/* bits set for the initially allocated keys */ +extern u32 reserved_allocation_mask;/* bits set for reserved keys */ + +#define PKEY_0 0/* * Define these here temporarily so we're not dependent on patching linux/mm.h.
@@ -96,15 +99,19 @@ static inline u16 pte_to_pkey_bits(u64 pteflags)#define __mm_pkey_is_allocated (mm, pkey) \ (mm_pkey_allocation_map(mm) & pkey_alloc_mask(pkey)) -#define __mm_pkey_is_reserved (pkey) (initial_allocation_mask & \ +#define __mm_pkey_is_reserved (pkey) (reserved_allocation_mask & \ pkey_alloc_mask(pkey))static inline bool mm_pkey_is_allocated (struct mm_struct *mm,int pkey) { -/* A reserved key is never considered as 'explicitly allocated' */ -return ((pkey <arch_max_pkey ()) && - !__mm_pkey_is_reserved (pkey) && -__mm_pkey_is_allocated (mm, pkey)); +if (pkey <0 || pkey >=arch_max_pkey ()) +return false; + +/* Reserved keys are never allocated. */ +if (__mm_pkey_is_reserved (pkey)) +return false; + +return __mm_pkey_is_allocated (mm, pkey); }extern void __arch_activate_pkey (int pkey);
@@ -200,6 +207,16 @@ static inline int arch_set_user_pkey_access(struct task_struct *tsk, int pkey, {if (static_branch_likely (&pkey_disabled))return -EINVAL ; + +/* +* userspace should not change pkey-0 permissions. +* pkey-0 is associated with every page in the kernel. +* If userspace denies any permission on pkey-0, the +* kernel cannot operate. +*/ +if (pkey ==PKEY_0 ) +return init_val ? -EINVAL :0 ; +return __arch_set_user_pkey_access (tsk, pkey, init_val); }
arch/powerpc/mm/pkeys.c +6 -7
@@ -14,7 +14,8 @@bool pkey_execute_disable_supported;int pkeys_total;/* Total pkeys as per device tree */ bool pkeys_devtree_defined;/* pkey property exported by device tree */ -u32 initial_allocation_mask;/* Bits set for reserved keys */ +u32 initial_allocation_mask;/* Bits set for the initially allocated keys */ +u32 reserved_allocation_mask;/* Bits set for reserved keys */ u64 pkey_amr_mask;/* Bits in AMR not to be touched */ u64 pkey_iamr_mask;/* Bits in AMR not to be touched */ u64 pkey_uamor_mask;/* Bits in UMOR not to be touched */
@@ -121,8 +122,9 @@ int pkey_initialize(void)#else os_reserved =0 ;#endif - initial_allocation_mask = (0x1 <<0 ) | (0x1 <<1 ) | - (0x1 <<EXECUTE_ONLY_KEY ); +/* Bits are in LE format. */ + reserved_allocation_mask = (0x1 <<1 ) | (0x1 <<EXECUTE_ONLY_KEY ); + initial_allocation_mask = reserved_allocation_mask | (0x1 <<PKEY_0 );/* register mask is in BE format */ pkey_amr_mask = ~0x0ul ;
@@ -135,7 +137,7 @@ int pkey_initialize(void) pkey_amr_mask |= (AMR_RD_BIT |AMR_WR_BIT ) <<pkeyshift (EXECUTE_ONLY_KEY ); pkey_uamor_mask = ~0x0ul ; - pkey_uamor_mask &= ~(0x3ul <<pkeyshift (0 )); + pkey_uamor_mask &= ~(0x3ul <<pkeyshift (PKEY_0 ));/* * key 1 is recommended not to be used. * PowerISA(3.0) page 1015,
@@ -381,9 +383,6 @@ static bool pkey_access_permitted(int pkey, bool write, bool execute)int pkey_shift;u64 amr; -if (!pkey) -return true; -if (!is_pkey_enabled (pkey))return true;
Signature · 1 line
1.7.1
Re: [PATCH 5/5] powerpc/pkeys: make protection key 0 less special
Quoted 6 lines · Ram Pai
arch/powerpc/include/asm/pkeys.h +0
-0@@ -13,7 +13,10 @@
arch/powerpc/include/asm/pkeys.h line 22
Quoted 10 lines
arch/powerpc/include/asm/pkeys.h +4
-1DECLARE_STATIC_KEY_TRUE (pkey_disabled);extern int pkeys_total;/* total pkeys as per device tree */ -extern u32 initial_allocation_mask;/* bits set for reserved keys */ +extern u32 initial_allocation_mask;/* bits set for the initially allocated keys */ +extern u32 reserved_allocation_mask;/* bits set for reserved keys */ + +#define PKEY_0 0/* * Define these here temporarily so we're not dependent on patching linux/mm.h.
This part no longer applies cleanly.
Thanks,
Florian