powerpc: Memory Protection Keys
Subsystems (7)
v2 cover powerpc: Memory Protection Keys
Memory protection keys enable applications to protect its address space from inadvertent access or corruption from itself.
The overall idea:
A process allocates a key and associates it with
a address range within its address space.
The process than can dynamically set read/write
permissions on the key without involving the
kernel. Any code that violates the permissions
off the address space; as defined by its associated key, will receive a segmentation fault.
This patch series enables the feature on PPC64.
It is enabled on HPTE 64K-page platform.
ISA3.0 section 5.7.13 describes the detailed specifications.
Testing:
This patch series has passed all the protection key tests available in the selftests directory. The tests are updated to work on both x86 and powerpc.
version v2:
(1) documentation and selftest added
(2) fixed a bug in 4k hpte backed 64k pte where page invalidation was not done correctly, and initialization of second-part-of-the-pte was not done correctly if the pte was not yet Hashed with a hpte. Reported by Aneesh.
(3) Fixed ABI breakage caused in siginfo structure. Reported by Anshuman.
Outstanding known issue:
Calls to sys_swapcontext with a made-up context will end up with a crap AMR if done by code who didn't know about that register. -- Reported by Ben.
version v1: Initial version
Thanks-to: Dave Hansen, Aneesh, Paul Mackerras,
Michael Ellermen
Ram Pai (12):
Free up four 64K PTE bits in 4K backed hpte pages. Free up four 64K PTE bits in 64K backed hpte pages. Implement sys_pkey_alloc and sys_pkey_free system call. store and restore the pkey state across context switches. Implementation for sys_mprotect_pkey() system call. Program HPTE key protection bits.
Macro the mask used for checking DSI exception Handle exceptions caused by violation of pkey protection. Deliver SEGV signal on pkey violation.
Read AMR only if pkey-violation caused the exception. Documentation updates.
Updated protection key selftest
Documentation/vm/protection-keys.txt | 110 ++ Documentation/x86/protection-keys.txt | 85 -- arch/powerpc/Kconfig | 15 + arch/powerpc/include/asm/book3s/64/hash-4k.h | 20 + arch/powerpc/include/asm/book3s/64/hash-64k.h | 48 +- arch/powerpc/include/asm/book3s/64/hash.h | 15 +- arch/powerpc/include/asm/book3s/64/mmu-hash.h | 10 + arch/powerpc/include/asm/book3s/64/mmu.h | 10 + arch/powerpc/include/asm/book3s/64/pgtable.h | 84 +- arch/powerpc/include/asm/mman.h | 29 +- arch/powerpc/include/asm/mmu_context.h | 12 + arch/powerpc/include/asm/paca.h | 1 + arch/powerpc/include/asm/pkeys.h | 159 +++ arch/powerpc/include/asm/processor.h | 5 + arch/powerpc/include/asm/reg.h | 10 +- arch/powerpc/include/asm/systbl.h | 3 + arch/powerpc/include/asm/unistd.h | 6 +- arch/powerpc/include/uapi/asm/ptrace.h | 3 +- arch/powerpc/include/uapi/asm/unistd.h | 3 + arch/powerpc/kernel/asm-offsets.c | 5 + arch/powerpc/kernel/exceptions-64s.S | 18 +- arch/powerpc/kernel/process.c | 18 + arch/powerpc/kernel/signal_32.c | 14 + arch/powerpc/kernel/signal_64.c | 14 + arch/powerpc/kernel/traps.c | 49 + arch/powerpc/mm/Makefile | 1 + arch/powerpc/mm/dump_linuxpagetables.c | 3 +- arch/powerpc/mm/fault.c | 25 +- arch/powerpc/mm/hash64_4k.c | 14 +- arch/powerpc/mm/hash64_64k.c | 93 +- arch/powerpc/mm/hash_utils_64.c | 35 +- arch/powerpc/mm/hugetlbpage-hash64.c | 16 +- arch/powerpc/mm/mmu_context_book3s64.c | 5 + arch/powerpc/mm/pkeys.c | 267 +++++ include/linux/mm.h | 32 +- include/uapi/asm-generic/mman-common.h | 2 +- tools/testing/selftests/vm/Makefile | 1 + tools/testing/selftests/vm/pkey-helpers.h | 365 +++++++ tools/testing/selftests/vm/protection_keys.c | 1451 +++++++++++++++++++++++++ tools/testing/selftests/x86/Makefile | 2 +- tools/testing/selftests/x86/pkey-helpers.h | 219 ---- tools/testing/selftests/x86/protection_keys.c | 1395 ------------------------ 42 files changed, 2828 insertions(+), 1844 deletions(-) create mode 100644 Documentation/vm/protection-keys.txt delete mode 100644 Documentation/x86/protection-keys.txt create mode 100644 arch/powerpc/include/asm/pkeys.h create mode 100644 arch/powerpc/mm/pkeys.c create mode 100644 tools/testing/selftests/vm/pkey-helpers.h create mode 100644 tools/testing/selftests/vm/protection_keys.c delete mode 100644 tools/testing/selftests/x86/pkey-helpers.h delete mode 100644 tools/testing/selftests/x86/protection_keys.c
Signature · 1 line
1.8.3.1
RFC v2 1/12 powerpc: Free up four 64K PTE bits in 4K backed hpte pages.
Rearrange 64K PTE bits to free up bits 3, 4, 5 and 6 in the 4K backed hpte pages. These bits continue to be used for 64K backed hpte pages in this patch, but will be freed up in the next patch.
The patch does the following change to the 64K PTE format
H_PAGE_BUSY moves from bit 3 to bit 9 H_PAGE_F_SECOND which occupied bit 4 moves to the second part
of the pte.H_PAGE_F_GIX which occupied bit 5, 6 and 7 also moves to the
second part of the pte.
the four bits((H_PAGE_F_SECOND|H_PAGE_F_GIX) that represent a slot is initialized to 0xF indicating an invalid slot. If a hpte
gets cached in a 0xF slot(i.e 7th slot of secondary), it is
released immediately. In other words, even though 0xF is a
valid slot we discard and consider it as an invalid slot;i.e hpte_soft_invalid(). This gives us an opportunity to not depend on a bit in the primary PTE in order to determine the validity of a slot.
When we release a hpte in the 0xF slot we also release a
legitimate primary slot and unmap that entry. This is to
ensure that we do get a legimate non-0xF slot the next time we
retry for a slot.
Though treating 0xF slot as invalid reduces the number of available slots and may have an effect on the performance, the probabilty of hitting a 0xF is extermely low.
Compared to the current scheme, the above described scheme reduces the number of false hash table updates significantly and has the added advantage of releasing four valuable PTE bits for other purpose.
This idea was jointly developed by Paul Mackerras, Aneesh, Michael Ellermen and myself.
4K PTE format remain unchanged currently.
--- arch/powerpc/include/asm/book3s/64/hash-4k.h | 20 +++++++ arch/powerpc/include/asm/book3s/64/hash-64k.h | 32 +++++++---- arch/powerpc/include/asm/book3s/64/hash.h | 15 +++-- arch/powerpc/include/asm/book3s/64/mmu-hash.h | 5 ++ arch/powerpc/mm/dump_linuxpagetables.c | 3 +- arch/powerpc/mm/hash64_4k.c | 14 ++--- arch/powerpc/mm/hash64_64k.c | 81 ++++++++++++--------------- arch/powerpc/mm/hash_utils_64.c | 30 +++++++--- 8 files changed, 122 insertions(+), 78 deletions(-)
arch/powerpc/include/asm/book3s/64/hash-4k.h +20 -0
@@ -16,6 +16,18 @@#define H_PUD_TABLE_SIZE (sizeof(pud_t) << H_PUD_INDEX_SIZE)#define H_PGD_TABLE_SIZE (sizeof(pgd_t) << H_PGD_INDEX_SIZE) + +/* +* Only supported by 4k linux page size +*/ +#define H_PAGE_F_SECOND _RPAGE_RSV2/* HPTE is in 2ndary HPTEG */ +#define H_PAGE_F_GIX (_RPAGE_RSV3 | _RPAGE_RSV4 | _RPAGE_RPN44) +#define H_PAGE_F_GIX_SHIFT 56 + +#define H_PAGE_BUSY _RPAGE_RSV1/* software: PTE & hash are busy */ +#define H_PAGE_HASHPTE _RPAGE_RPN43/* PTE has associated HPTE */ + +/* PTE flags to conserve for HPTE identification */ #define _PAGE_HPTEFLAGS (H_PAGE_BUSY | H_PAGE_HASHPTE | \ H_PAGE_F_SECOND | H_PAGE_F_GIX)
@@ -48,6 +60,14 @@ static inline int hash__hugepd_ok(hugepd_t hpd) }#endif +static inline unsigned long set_hidx_slot (pte_t *ptep,real_pte_t rpte, +unsigned int subpg_index,unsigned long slot) +{ +return (slot <<H_PAGE_F_GIX_SHIFT ) & + (H_PAGE_F_SECOND |H_PAGE_F_GIX ); +} + +#ifdef CONFIG_TRANSPARENT_HUGEPAGE static inline char *get_hpte_slot_array (pmd_t *pmdp)
arch/powerpc/include/asm/book3s/64/hash-64k.h +22 -10
@@ -10,23 +10,25 @@* 64k aligned address free up few of the lower bits of RPN for us * We steal that here. For more deatils look at pte_pfn/pfn_pte() */ -#define H_PAGE_COMBO _RPAGE_RPN0/* this is a combo 4k page */ -#define H_PAGE_4K_PFN _RPAGE_RPN1/* PFN is for a single 4k page */ +#define H_PAGE_COMBO _RPAGE_RPN0/* this is a combo 4k page */ +#define H_PAGE_4K_PFN _RPAGE_RPN1/* PFN is for a single 4k page */ +#define H_PAGE_F_SECOND _RPAGE_RSV2/* HPTE is in 2ndary HPTEG */ +#define H_PAGE_F_GIX (_RPAGE_RSV3 | _RPAGE_RSV4 | _RPAGE_RPN44) +#define H_PAGE_F_GIX_SHIFT 56 + + +#define H_PAGE_BUSY _RPAGE_RPN42/* software: PTE & hash are busy */ +#define H_PAGE_HASHPTE _RPAGE_RPN43/* PTE has associated HPTE */ +/* * We need to differentiate between explicit huge page and THP huge * page, since THP huge page also need to track real subpage details */ #define H_PAGE_THP_HUGE H_PAGE_4K_PFN -/* -* Used to track subpage group valid if H_PAGE_COMBO is set -* This overloads H_PAGE_F_GIX and H_PAGE_F_SECOND -*/ -#define H_PAGE_COMBO_VALID (H_PAGE_F_GIX | H_PAGE_F_SECOND) -/* PTE flags to conserve for HPTE identification */ -#define _PAGE_HPTEFLAGS (H_PAGE_BUSY | H_PAGE_F_SECOND | \ - H_PAGE_F_GIX | H_PAGE_HASHPTE | H_PAGE_COMBO) +#define _PAGE_HPTEFLAGS (H_PAGE_BUSY | H_PAGE_HASHPTE | H_PAGE_COMBO) +/* * we support 16 fragments per PTE page of 64K size. */
@@ -74,6 +76,16 @@ static inline unsigned long __rpte_to_hidx(real_pte_t rpte, unsigned long index)return (pte_val (rpte.pte) >>H_PAGE_F_GIX_SHIFT ) &0xf ; } +static inline unsigned long set_hidx_slot (pte_t *ptep,real_pte_t rpte, +unsigned int subpg_index,unsigned long slot) +{ +unsigned long *hidxp = (unsigned long *)(ptep +PTRS_PER_PTE ); + + rpte.hidx &= ~(0xfUL << (subpg_index <<2 )); + *hidxp = rpte.hidx | (slot << (subpg_index <<2 )); +return 0x0UL ; +} +#define __rpte_to_pte (r) ((r).pte)extern bool __rpte_sub_valid (real_pte_t rpte,unsigned long index);/*
arch/powerpc/include/asm/book3s/64/hash.h +10 -5
@@ -8,11 +8,8 @@* */ #define H_PTE_NONE_MASK _PAGE_HPTEFLAGS -#define H_PAGE_F_GIX_SHIFT 56 -#define H_PAGE_BUSY _RPAGE_RSV1/* software: PTE & hash are busy */ -#define H_PAGE_F_SECOND _RPAGE_RSV2/* HPTE is in 2ndary HPTEG */ -#define H_PAGE_F_GIX (_RPAGE_RSV3 | _RPAGE_RSV4 | _RPAGE_RPN44) -#define H_PAGE_HASHPTE _RPAGE_RPN43/* PTE has associated HPTE */ + +#define INIT_HIDX (~0x0UL)#ifdef CONFIG_PPC_64K_PAGES #include <asm/book3s/64/hash-64k.h>
@@ -160,6 +157,14 @@ static inline int hash__pte_none(pte_t pte)return (pte_val (pte) & ~H_PTE_NONE_MASK ) ==0 ; } +static inline bool hpte_soft_invalid (unsigned long slot) +{ +return ((slot &0xfUL ) ==0xfUL ); +} + +unsigned long get_hidx_gslot (unsigned long vpn,unsigned long shift, +int ssize,real_pte_t rpte,unsigned int subpg_index); +/* This low level function performs the actual PTE insertion * Setting the PTE depends on the MMU type and other factors. It's * an horrible mess that I'm not going to try to clean up now but
arch/powerpc/include/asm/book3s/64/mmu-hash.h +5 -0
@@ -435,6 +435,11 @@ extern int __hash_page_4K(unsigned long ea, unsigned long access,extern int __hash_page_64K (unsigned long ea,unsigned long access,unsigned long vsid,pte_t *ptep,unsigned long trap,unsigned long flags,int ssize); +extern unsigned long set_hidx_slot (pte_t *ptep,real_pte_t rpte, +unsigned int subpg_index,unsigned long slot); +extern unsigned long get_hidx_slot (unsigned long vpn,unsigned long shift, +int ssize,real_pte_t rpte,unsigned int subpg_index); +struct mm_struct ;unsigned int hash_page_do_lazy_icache (unsigned int pp,pte_t pte,int trap); extern int hash_page_mm(struct mm_struct *mm, unsigned long ea,
arch/powerpc/mm/dump_linuxpagetables.c +2 -1
@@ -213,7 +213,7 @@ struct flag_info { .val = H_PAGE_4K_PFN, .set = "4K_pfn", }, { -#endif +#else .mask = H_PAGE_F_GIX, .val = H_PAGE_F_GIX, .set = "f_gix",
@@ -224,6 +224,7 @@ struct flag_info { .val = H_PAGE_F_SECOND, .set = "f_second", }, { +#endif /* CONFIG_PPC_64K_PAGES */ #endif .mask = _PAGE_SPECIAL, .val = _PAGE_SPECIAL,
arch/powerpc/mm/hash64_4k.c +6 -8
@@ -20,6 +20,7 @@ int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid, pte_t *ptep, unsigned long trap, unsigned long flags, int ssize, int subpg_prot) { + real_pte_t rpte;unsigned long hpte_group;unsigned long rflags, pa;unsigned long old_pte, new_pte;
@@ -54,6 +55,7 @@ int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid,* need to add in 0x1 if it's a read-only user page */ rflags =htab_convert_pte_flags (new_pte); + rpte =__real_pte (__pte (old_pte), ptep);if (cpu_has_feature (CPU_FTR_NOEXECUTE ) && !cpu_has_feature (CPU_FTR_COHERENT_ICACHE ))
@@ -64,13 +66,10 @@ int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid,/* * There MIGHT be an HPTE for this pte */ - hash =hpt_hash (vpn, shift, ssize); -if (old_pte &H_PAGE_F_SECOND ) - hash = ~hash; - slot = (hash & htab_hash_mask) *HPTES_PER_GROUP ; - slot += (old_pte &H_PAGE_F_GIX ) >>H_PAGE_F_GIX_SHIFT ; +unsigned long gslot =get_hidx_gslot (vpn, shift, + ssize, rpte,0 ); -if (mmu_hash_ops.hpte_updatepp (slot, rflags, vpn,MMU_PAGE_4K , +if (mmu_hash_ops.hpte_updatepp (gslot, rflags, vpn,MMU_PAGE_4K ,MMU_PAGE_4K , ssize, flags) ==-1 ) old_pte &= ~_PAGE_HPTEFLAGS; }
@@ -118,8 +117,7 @@ int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid,return -1 ; } new_pte = (new_pte & ~_PAGE_HPTEFLAGS) |H_PAGE_HASHPTE ; - new_pte |= (slot <<H_PAGE_F_GIX_SHIFT ) & - (H_PAGE_F_SECOND |H_PAGE_F_GIX ); + new_pte |=set_hidx_slot (ptep, rpte,0 , slot); } *ptep =__pte (new_pte & ~H_PAGE_BUSY );return 0 ;
arch/powerpc/mm/hash64_64k.c +36 -45
144 lines not on this page · raw message
arch/powerpc/mm/hash_utils_64.c +21 -9
@@ -975,8 +975,9 @@ void __init hash__early_init_devtree(void)void __init hash__early_init_mmu (void ) { +#ifndef CONFIG_PPC_64K_PAGES /* -* We have code in __hash_page_64K() and elsewhere, which assumes it can +* We have code in __hash_page_4K() and elsewhere, which assumes it can * do the following: * new_pte |= (slot << H_PAGE_F_GIX_SHIFT) & (H_PAGE_F_SECOND | H_PAGE_F_GIX); *
@@ -987,6 +988,7 @@ void __init hash__early_init_mmu(void)* with a BUILD_BUG_ON(). */ BUILD_BUG_ON (H_PAGE_F_SECOND != (1ul << (H_PAGE_F_GIX_SHIFT +3 ))); +#endif /* CONFIG_PPC_64K_PAGES */ htab_init_page_sizes ();
@@ -1589,29 +1591,39 @@ static inline void tm_flush_hash_page(int local) }#endif +unsigned long get_hidx_gslot (unsigned long vpn,unsigned long shift, +int ssize,real_pte_t rpte,unsigned int subpg_index) +{ +unsigned long hash, slot, hidx; + + hash =hpt_hash (vpn, shift, ssize); + hidx =__rpte_to_hidx (rpte, subpg_index); +if (hidx & _PTEIDX_SECONDARY) + hash = ~hash; + slot = (hash & htab_hash_mask) *HPTES_PER_GROUP ; + slot += hidx & _PTEIDX_GROUP_IX; +return slot; +} + +/* WARNING: This is called from hash_low_64.S, if you change this prototype, * do not forget to update the assembly call site ! */ void flush_hash_page (unsigned long vpn,real_pte_t pte,int psize,int ssize,unsigned long flags) { -unsigned long hash, index, shift, hidx, slot; +unsigned long hash, index, shift, hidx, gslot;int local = flags &HPTE_LOCAL_UPDATE ;DBG_LOW ("flush_hash_page(vpn=%016lx)\n" , vpn); pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) { - hash =hpt_hash (vpn, shift, ssize); - hidx =__rpte_to_hidx (pte, index); -if (hidx & _PTEIDX_SECONDARY) - hash = ~hash; - slot = (hash & htab_hash_mask) *HPTES_PER_GROUP ; - slot += hidx & _PTEIDX_GROUP_IX; + gslot =get_hidx_gslot (vpn, shift, ssize, pte, index);DBG_LOW (" sub %ld: hash=%lx, hidx=%lx\n" , index, slot, hidx);/* * We use same base page size and actual psize, because we don't * use these functions for hugepage */ - mmu_hash_ops.hpte_invalidate (slot, vpn, psize, psize, + mmu_hash_ops.hpte_invalidate (gslot, vpn, psize, psize, ssize, local); }pte_iterate_hashed_end ();
Signature · 1 line
1.8.3.1
Re: [RFC v2 01/12] powerpc: Free up four 64K PTE bits in 4K backed hpte pages.
Quoted 4 lines · Ram Pai
Rearrange 64K PTE bits to free up bits 3, 4, 5 and 6 in the 4K backed hpte pages. These bits continue to be used for 64K backed hpte pages in this patch, but will be freed up in the next patch.
The counting 3, 4, 5 and 6 are in BE format I believe, I was initially trying to see that from right to left as we normally do in the kernel and was getting confused. So basically these bits (which are only applicable for 64K mapping IIUC) are going to be freed up from the PTE format.
#define _RPAGE_RSV1 0x1000000000000000UL
#define _RPAGE_RSV2 0x0800000000000000UL
#define _RPAGE_RSV3 0x0400000000000000UL
#define _RPAGE_RSV4 0x0200000000000000UL
As you have mentioned before this feature is available for 64K page size only and not for 4K mappings. So I assume we support both the combinations.
* 64K mapping on 64K
* 64K mapping on 4K
These are the current users of the above bits
#define H_PAGE_BUSY _RPAGE_RSV1 /* software: PTE & hash are busy */
#define H_PAGE_F_SECOND _RPAGE_RSV2 /* HPTE is in 2ndary HPTEG */
#define H_PAGE_F_GIX (_RPAGE_RSV3 | _RPAGE_RSV4 | _RPAGE_RPN44)
#define H_PAGE_HASHPTE _RPAGE_RPN43 /* PTE has associated HPTE */
Quoted 4 lines
The patch does the following change to the 64K PTE format
H_PAGE_BUSYmoves from bit 3 to bit 9
and what is in there on bit 9 now ? This ?
#define _RPAGE_SW2 0x00400
which is used as
#define _PAGE_SPECIAL _RPAGE_SW2 /* software: special page */
which will not be required any more ?
Quoted 9 lines
H_PAGE_F_SECONDwhich occupied bit 4 moves to the second part
of the pte.H_PAGE_F_GIXwhich occupied bit 5, 6 and 7 also moves to the
second part of the pte.the four bits((H_PAGE_F_SECOND|H_PAGE_F_GIX) that represent a slot is initialized to
0xFindicating an invalid slot. If a hpte
gets cached in a0xFslot(i.e 7th slot of secondary), it is
released immediately. In other words, even though0xFis a
Release immediately means we attempt again for a new hash slot ?
Quoted 4 lines
valid slot we discard and consider it as an invalid slot;i.e
hpte_soft_invalid(). This gives us an opportunity to not depend on a bit in the primary PTE in order to determine the validity of a slot.
So we have to see the slot number in the second half for each PTE to figure out if it has got a valid slot in the hash page table.
Quoted 5 lines
When we release a hpte in the
0xFslot we also release a
legitimate primary slot and unmap that entry. This is to
ensure that we do get a legimate non-0xF slot the next time we
retry for a slot.
Okay.
Quoted 4 lines
Though treating
0xFslot as invalid reduces the number of available slots and may have an effect on the performance, the probabilty of hitting a0xFis extermely low.
Why you say that ? I thought every slot number has the same probability of hit from the hash function.
Quoted 3 lines
Compared to the current scheme, the above described scheme reduces the number of false hash table updates significantly and has the
How it reduces false hash table updates ?
Quoted 30 lines
added advantage of releasing four valuable PTE bits for other purpose.
This idea was jointly developed by Paul Mackerras, Aneesh, Michael Ellermen and myself.
4K PTE format remain unchanged currently.
Signed-off-by Ram Pai <[email protected]>--- arch/powerpc/include/asm/book3s/64/hash-4k.h | 20 +++++++ arch/powerpc/include/asm/book3s/64/hash-64k.h | 32 +++++++---- arch/powerpc/include/asm/book3s/64/hash.h | 15 +++-- arch/powerpc/include/asm/book3s/64/mmu-hash.h | 5 ++ arch/powerpc/mm/dump_linuxpagetables.c | 3 +- arch/powerpc/mm/hash64_4k.c | 14 ++--- arch/powerpc/mm/hash64_64k.c | 81 ++++++++++++--------------- arch/powerpc/mm/hash_utils_64.c | 30 +++++++--- 8 files changed, 122 insertions(+), 78 deletions(-)arch/powerpc/include/asm/book3s/64/hash-4k.h +2
-0@@ -16,6 +16,18 @@#define H_PUD_TABLE_SIZE (sizeof(pud_t) << H_PUD_INDEX_SIZE)#define H_PGD_TABLE_SIZE (sizeof(pgd_t) << H_PGD_INDEX_SIZE) + +/*
arch/powerpc/include/asm/book3s/64/hash-4k.h line 30
Quoted 10 lines
arch/powerpc/include/asm/book3s/64/hash-4k.h +10
-0+* Only supported by 4k linux page size +*/ +#define H_PAGE_F_SECOND _RPAGE_RSV2/* HPTE is in 2ndary HPTEG */ +#define H_PAGE_F_GIX (_RPAGE_RSV3 | _RPAGE_RSV4 | _RPAGE_RPN44) +#define H_PAGE_F_GIX_SHIFT 56 + +#define H_PAGE_BUSY _RPAGE_RSV1/* software: PTE & hash are busy */ +#define H_PAGE_HASHPTE _RPAGE_RPN43/* PTE has associated HPTE */ + +
So we moved the common 64K definitions here.
arch/powerpc/include/asm/book3s/64/hash-4k.h static inline int hash__hugepd_ok(hugepd_t hpd) · line 68
Quoted 13 lines
arch/powerpc/include/asm/book3s/64/hash-4k.h +6
-0/* PTE flags to conserve for HPTE identification */ #define _PAGE_HPTEFLAGS (H_PAGE_BUSY | H_PAGE_HASHPTE | \ H_PAGE_F_SECOND | H_PAGE_F_GIX)@@ -48,6 +60,14 @@ static inline int hash__hugepd_ok(hugepd_t hpd) }#endif +static inline unsigned long set_hidx_slot (pte_t *ptep,real_pte_t rpte, +unsigned int subpg_index,unsigned long slot) +{ +return (slot <<H_PAGE_F_GIX_SHIFT ) & + (H_PAGE_F_SECOND |H_PAGE_F_GIX ); +}
Why we are passing the first 3 arguments of the function if we never use it inside. Is the caller expected to take care of it ?
Quoted 10 lines
arch/powerpc/include/asm/book3s/64/hash-4k.h +2
-0+ +#ifdef CONFIG_TRANSPARENT_HUGEPAGE static inline char *get_hpte_slot_array (pmd_t *pmdp)arch/powerpc/include/asm/book3s/64/hash-64k.h +0
-0
arch/powerpc/include/asm/book3s/64/hash-64k.h line 14
Quoted 7 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h +2
-2@@ -10,23 +10,25 @@* 64k aligned address free up few of the lower bits of RPN for us * We steal that here. For more deatils look at pte_pfn/pfn_pte() */ -#define H_PAGE_COMBO _RPAGE_RPN0/* this is a combo 4k page */ -#define H_PAGE_4K_PFN _RPAGE_RPN1/* PFN is for a single 4k page */ +#define H_PAGE_COMBO _RPAGE_RPN0/* this is a combo 4k page */ +#define H_PAGE_4K_PFN _RPAGE_RPN1/* PFN is for a single 4k page */
Its the same thing, changes nothing.
arch/powerpc/include/asm/book3s/64/hash-64k.h line 21
Quoted 7 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h +7
-0+#define H_PAGE_F_SECOND _RPAGE_RSV2/* HPTE is in 2ndary HPTEG */ +#define H_PAGE_F_GIX (_RPAGE_RSV3 | _RPAGE_RSV4 | _RPAGE_RPN44) +#define H_PAGE_F_GIX_SHIFT 56 + + +#define H_PAGE_BUSY _RPAGE_RPN42/* software: PTE & hash are busy */ +#define H_PAGE_HASHPTE _RPAGE_RPN43/* PTE has associated HPTE */
H_PAGE_BUSY seems to be differently defined here.
arch/powerpc/include/asm/book3s/64/hash-64k.h line 25
Quoted 12 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h +1
-5+/* * We need to differentiate between explicit huge page and THP huge * page, since THP huge page also need to track real subpage details */ #define H_PAGE_THP_HUGE H_PAGE_4K_PFN -/* -* Used to track subpage group valid if H_PAGE_COMBO is set -* This overloads H_PAGE_F_GIX and H_PAGE_F_SECOND -*/ -#define H_PAGE_COMBO_VALID (H_PAGE_F_GIX | H_PAGE_F_SECOND)
H_PAGE_COMBO_VALID is not defined alternately ?
arch/powerpc/include/asm/book3s/64/hash-64k.h line 31
Quoted 6 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h +2
-3-/* PTE flags to conserve for HPTE identification */ -#define _PAGE_HPTEFLAGS (H_PAGE_BUSY | H_PAGE_F_SECOND | \ - H_PAGE_F_GIX | H_PAGE_HASHPTE | H_PAGE_COMBO) +#define _PAGE_HPTEFLAGS (H_PAGE_BUSY | H_PAGE_HASHPTE | H_PAGE_COMBO) +
Slot information has moved to the second half, hence _PAGE_HPTEFLAGS need not carry that.
Quoted 6 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h +0
-0/* * we support 16 fragments per PTE page of 64K size. */ @@ -74,6 +76,16 @@ static inline unsigned long __rpte_to_hidx(real_pte_t rpte, unsigned long index)return (pte_val (rpte.pte) >>H_PAGE_F_GIX_SHIFT ) &0xf ; }
arch/powerpc/include/asm/book3s/64/hash-64k.h static inline unsigned long __rpte_to_hidx(real_pte_t rpte, unsigned long index) · line 87
Quoted 10 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h +9
-0+static inline unsigned long set_hidx_slot (pte_t *ptep,real_pte_t rpte, +unsigned int subpg_index,unsigned long slot) +{ +unsigned long *hidxp = (unsigned long *)(ptep +PTRS_PER_PTE ); + + rpte.hidx &= ~(0xfUL << (subpg_index <<2 )); + *hidxp = rpte.hidx | (slot << (subpg_index <<2 )); +return 0x0UL ; +}
New method to insert the slot information in the second half.
Quoted 9 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h +1
-0+#define __rpte_to_pte (r) ((r).pte)extern bool __rpte_sub_valid (real_pte_t rpte,unsigned long index);/* arch/powerpc/include/asm/book3s/64/hash.h +0
-0
arch/powerpc/include/asm/book3s/64/hash.h line 15
Quoted 8 lines
arch/powerpc/include/asm/book3s/64/hash.h +0
-5@@ -8,11 +8,8 @@* */ #define H_PTE_NONE_MASK _PAGE_HPTEFLAGS -#define H_PAGE_F_GIX_SHIFT 56 -#define H_PAGE_BUSY _RPAGE_RSV1/* software: PTE & hash are busy */ -#define H_PAGE_F_SECOND _RPAGE_RSV2/* HPTE is in 2ndary HPTEG */ -#define H_PAGE_F_GIX (_RPAGE_RSV3 | _RPAGE_RSV4 | _RPAGE_RPN44) -#define H_PAGE_HASHPTE _RPAGE_RPN43/* PTE has associated HPTE */
Removing the common definitions.
Quoted 25 lines
arch/powerpc/include/asm/book3s/64/hash.h +10
-0+ +#define INIT_HIDX (~0x0UL)#ifdef CONFIG_PPC_64K_PAGES #include <asm/book3s/64/hash-64k.h> @@ -160,6 +157,14 @@ static inline int hash__pte_none(pte_t pte)return (pte_val (pte) & ~H_PTE_NONE_MASK ) ==0 ; } +static inline bool hpte_soft_invalid (unsigned long slot) +{ +return ((slot &0xfUL ) ==0xfUL ); +} + +unsigned long get_hidx_gslot (unsigned long vpn,unsigned long shift, +int ssize,real_pte_t rpte,unsigned int subpg_index); +/* This low level function performs the actual PTE insertion * Setting the PTE depends on the MMU type and other factors. It's * an horrible mess that I'm not going to try to clean up now but arch/powerpc/include/asm/book3s/64/mmu-hash.h +0
-0
arch/powerpc/include/asm/book3s/64/mmu-hash.h extern int __hash_page_4K(unsigned long ea, unsigned long access, · line 441
Quoted 7 lines
arch/powerpc/include/asm/book3s/64/mmu-hash.h +4
-0@@ -435,6 +435,11 @@ extern int __hash_page_4K(unsigned long ea, unsigned long access,extern int __hash_page_64K (unsigned long ea,unsigned long access,unsigned long vsid,pte_t *ptep,unsigned long trap,unsigned long flags,int ssize); +extern unsigned long set_hidx_slot (pte_t *ptep,real_pte_t rpte, +unsigned int subpg_index,unsigned long slot); +extern unsigned long get_hidx_slot (unsigned long vpn,unsigned long shift, +int ssize,real_pte_t rpte,unsigned int subpg_index);
I wonder what purpose set_hidx_slot() defined previously, served.
Quoted 18 lines
arch/powerpc/include/asm/book3s/64/mmu-hash.h +1
-0+struct mm_struct ;unsigned int hash_page_do_lazy_icache (unsigned int pp,pte_t pte,int trap); extern int hash_page_mm(struct mm_struct *mm, unsigned long ea,arch/powerpc/mm/dump_linuxpagetables.c +1
-1@@ -213,7 +213,7 @@ struct flag_info { .val = H_PAGE_4K_PFN, .set = "4K_pfn", }, { -#endif +#else .mask = H_PAGE_F_GIX, .val = H_PAGE_F_GIX, .set = "f_gix",
arch/powerpc/mm/dump_linuxpagetables.c struct flag_info { · line 227
Quoted 4 lines
arch/powerpc/mm/dump_linuxpagetables.c +1
-0@@ -224,6 +224,7 @@ struct flag_info { .val = H_PAGE_F_SECOND, .set = "f_second", }, { +#endif /* CONFIG_PPC_64K_PAGES */
Are we adding H_PAGE_F_GIX as an element for 4K mapping ?
Quoted 24 lines
arch/powerpc/mm/dump_linuxpagetables.c +0
-0#endif .mask = _PAGE_SPECIAL, .val = _PAGE_SPECIAL,arch/powerpc/mm/hash64_4k.c +2
-0@@ -20,6 +20,7 @@ int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid, pte_t *ptep, unsigned long trap, unsigned long flags, int ssize, int subpg_prot) { + real_pte_t rpte;unsigned long hpte_group;unsigned long rflags, pa;unsigned long old_pte, new_pte;@@ -54,6 +55,7 @@ int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid,* need to add in 0x1 if it's a read-only user page */ rflags =htab_convert_pte_flags (new_pte); + rpte =__real_pte (__pte (old_pte), ptep);if (cpu_has_feature (CPU_FTR_NOEXECUTE ) && !cpu_has_feature (CPU_FTR_COHERENT_ICACHE ))
arch/powerpc/mm/hash64_4k.c int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid, · line 70
Quoted 10 lines
arch/powerpc/mm/hash64_4k.c +2
-5@@ -64,13 +66,10 @@ int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid,/* * There MIGHT be an HPTE for this pte */ - hash =hpt_hash (vpn, shift, ssize); -if (old_pte &H_PAGE_F_SECOND ) - hash = ~hash; - slot = (hash & htab_hash_mask) *HPTES_PER_GROUP ; - slot += (old_pte &H_PAGE_F_GIX ) >>H_PAGE_F_GIX_SHIFT ; +unsigned long gslot =get_hidx_gslot (vpn, shift, + ssize, rpte,0 );
I am wondering why there is a 'g' before the slot in all these functions.
Its already too much of changes in a single patch. Being a single logical change it needs to be inside a single change but then we need much more description in the commit message for some one to understand what all changed and how.
Re: [RFC v2 01/12] powerpc: Free up four 64K PTE bits in 4K backed hpte pages.
Quoted 17 lines · Anshuman Khandual
Quoted 4 lines · Ram Pai
Rearrange 64K PTE bits to free up bits 3, 4, 5 and 6 in the 4K backed hpte pages. These bits continue to be used for 64K backed hpte pages in this patch, but will be freed up in the next patch.
The counting 3, 4, 5 and 6 are in BE format I believe, I was initially trying to see that from right to left as we normally do in the kernel and was getting confused. So basically these bits (which are only applicable for 64K mapping IIUC) are going to be freed up from the PTE format.
#define
_RPAGE_RSV10x1000000000000000UL
#define_RPAGE_RSV20x0800000000000000UL
#define_RPAGE_RSV30x0400000000000000UL
#define_RPAGE_RSV40x0200000000000000UL
Quoted 6 lines
As you have mentioned before this feature is available for 64K page size only and not for 4K mappings. So I assume we support both the combinations.
* 64K mapping on 64K
* 64K mapping on 4K
yes.
Quoted 15 lines
These are the current users of the above bits
#define
H_PAGE_BUSY_RPAGE_RSV1/* software: PTE & hash are busy */
#defineH_PAGE_F_SECOND_RPAGE_RSV2/* HPTE is in 2ndary HPTEG */
#defineH_PAGE_F_GIX(_RPAGE_RSV3|_RPAGE_RSV4|_RPAGE_RPN44)
#defineH_PAGE_HASHPTE_RPAGE_RPN43/* PTE has associated HPTE */Quoted 4 lines
The patch does the following change to the 64K PTE format
H_PAGE_BUSYmoves from bit 3 to bit 9and what is in there on bit 9 now ? This ?
Quoted 7 lines
#define
_RPAGE_SW20x00400which is used as
#define
_PAGE_SPECIAL_RPAGE_SW2/* software: special page */which will not be required any more ?
i think you are reading bit 9 from right to left. the bit 9 i refer to is from left to right. Using the same numbering convention the ISA3.0 uses. I know it is confusing, will make a mention in the comment of this patch, to read it the big-endian way.
BTW: Bit 9 is not used currently. so using it in this patch. But this is a temporary move. the H_PAGE_BUSY will move to bit 7 in the next patch.
Had to keep at bit 9, because bit 7 is not yet entirely freed up. it is used by 64K PTE backed by 64k htpe.
Quoted 6 lines
Quoted 6 lines
H_PAGE_F_SECONDwhich occupied bit 4 moves to the second part
of the pte.H_PAGE_F_GIXwhich occupied bit 5, 6 and 7 also moves to the
second part of the pte.
Quoted 6 lines
Quoted 4 lines
the four bits((H_PAGE_F_SECOND|H_PAGE_F_GIX) that represent a slot is initialized to
0xFindicating an invalid slot. If a hpte
gets cached in a0xFslot(i.e 7th slot of secondary), it is
released immediately. In other words, even though0xFis aRelease immediately means we attempt again for a new hash slot ?
yes.
Quoted 8 lines
Quoted 5 lines
valid slot we discard and consider it as an invalid slot;i.e
hpte_soft_invalid(). This gives us an opportunity to not depend on a bit in the primary PTE in order to determine the validity of a slot.So we have to see the slot number in the second half for each PTE to figure out if it has got a valid slot in the hash page table.
yes.
Quoted 10 lines
Quoted 6 lines
When we release a hpte in the
0xFslot we also release a
legitimate primary slot and unmap that entry. This is to
ensure that we do get a legimate non-0xF slot the next time we
retry for a slot.Okay.
Quoted 1 line
Quoted 6 lines
Quoted 3 lines
Though treating
0xFslot as invalid reduces the number of available slots and may have an effect on the performance, the probabilty of hitting a0xFis extermely low.Why you say that ? I thought every slot number has the same probability of hit from the hash function.
Every hash bucket has the same probability. But every slot within the hash bucket is filled in sequentially. so it takes 15 hptes to hash to the same bucket before we get to the 15th slot in the secondary.
Quoted 6 lines
Quoted 4 lines
Compared to the current scheme, the above described scheme reduces the number of false hash table updates significantly and has the
How it reduces false hash table updates ?
earlier, we had 1 bit allocated in the first-part-of-the 64K-PTE for four consecutive 4K hptes. If any one 4k hpte got hashed-in, the bit got set. Which means anytime it faulted on the remaining three 4k hpte, we saw the bit already set and tried to erroneously update that hpte. So we had a 75% update error rate. Funcationally not bad, but bad from a performance point of view.
With the current scheme, we decide if a 4k slot is valid by looking at its value rather than depending on a bit in the main-pte. So there is no chance of getting mislead. And hence no chance of trying to update a invalid hpte. Should improve performance and at the same time give us four valuable PTE bits.
Quoted 29 lines
Quoted 29 lines
added advantage of releasing four valuable PTE bits for other purpose.
This idea was jointly developed by Paul Mackerras, Aneesh, Michael Ellermen and myself.
4K PTE format remain unchanged currently.
Signed-off-by Ram Pai <[email protected]>--- arch/powerpc/include/asm/book3s/64/hash-4k.h | 20 +++++++ arch/powerpc/include/asm/book3s/64/hash-64k.h | 32 +++++++---- arch/powerpc/include/asm/book3s/64/hash.h | 15 +++-- arch/powerpc/include/asm/book3s/64/mmu-hash.h | 5 ++ arch/powerpc/mm/dump_linuxpagetables.c | 3 +- arch/powerpc/mm/hash64_4k.c | 14 ++--- arch/powerpc/mm/hash64_64k.c | 81 ++++++++++++--------------- arch/powerpc/mm/hash_utils_64.c | 30 +++++++--- 8 files changed, 122 insertions(+), 78 deletions(-)arch/powerpc/include/asm/book3s/64/hash-4k.h +0
-0@@ -16,6 +16,18 @@#define H_PUD_TABLE_SIZE (sizeof(pud_t) << H_PUD_INDEX_SIZE)#define H_PGD_TABLE_SIZE (sizeof(pgd_t) << H_PGD_INDEX_SIZE)
Quoted 14 lines
arch/powerpc/include/asm/book3s/64/hash-4k.h line 30
Quoted 12 lines
arch/powerpc/include/asm/book3s/64/hash-4k.h +12
-0+ +/* +* Only supported by 4k linux page size +*/ +#define H_PAGE_F_SECOND _RPAGE_RSV2/* HPTE is in 2ndary HPTEG */ +#define H_PAGE_F_GIX (_RPAGE_RSV3 | _RPAGE_RSV4 | _RPAGE_RPN44) +#define H_PAGE_F_GIX_SHIFT 56 + +#define H_PAGE_BUSY _RPAGE_RSV1/* software: PTE & hash are busy */ +#define H_PAGE_HASHPTE _RPAGE_RPN43/* PTE has associated HPTE */ + +So we moved the common 64K definitions here.
yes.
Quoted 9 lines
Quoted 9 lines
arch/powerpc/include/asm/book3s/64/hash-4k.h +0
-0/* PTE flags to conserve for HPTE identification */ #define _PAGE_HPTEFLAGS (H_PAGE_BUSY | H_PAGE_HASHPTE | \ H_PAGE_F_SECOND | H_PAGE_F_GIX)@@ -48,6 +60,14 @@ static inline int hash__hugepd_ok(hugepd_t hpd) }#endif
Quoted 9 lines
arch/powerpc/include/asm/book3s/64/hash-4k.h static inline int hash__hugepd_ok(hugepd_t hpd) · line 68
Quoted 6 lines
arch/powerpc/include/asm/book3s/64/hash-4k.h +6
-0+static inline unsigned long set_hidx_slot (pte_t *ptep,real_pte_t rpte, +unsigned int subpg_index,unsigned long slot) +{ +return (slot <<H_PAGE_F_GIX_SHIFT ) & + (H_PAGE_F_SECOND |H_PAGE_F_GIX ); +}Why we are passing the first 3 arguments of the function if we never use it inside. Is the caller expected to take care of it ?
trying to keep the same prototype for the 4K-pte and 64K-pte cases. Otherwise the caller has to wonder which parameter scheme to use.
Quoted 5 lines
Quoted 5 lines
arch/powerpc/include/asm/book3s/64/hash-4k.h +2
-0+ +#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Quoted 15 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h line 14
Quoted 13 lines
arch/powerpc/include/asm/book3s/64/hash-4k.h +0
-0static inline char *get_hpte_slot_array (pmd_t *pmdp)arch/powerpc/include/asm/book3s/64/hash-64k.h +2
-2@@ -10,23 +10,25 @@* 64k aligned address free up few of the lower bits of RPN for us * We steal that here. For more deatils look at pte_pfn/pfn_pte() */ -#define H_PAGE_COMBO _RPAGE_RPN0/* this is a combo 4k page */ -#define H_PAGE_4K_PFN _RPAGE_RPN1/* PFN is for a single 4k page */ +#define H_PAGE_COMBO _RPAGE_RPN0/* this is a combo 4k page */ +#define H_PAGE_4K_PFN _RPAGE_RPN1/* PFN is for a single 4k page */ Its the same thing, changes nothing.
it fixes some space/tab problem.
Quoted 10 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h line 21
Quoted 8 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h +7
-0+#define H_PAGE_F_SECOND _RPAGE_RSV2/* HPTE is in 2ndary HPTEG */ +#define H_PAGE_F_GIX (_RPAGE_RSV3 | _RPAGE_RSV4 | _RPAGE_RPN44) +#define H_PAGE_F_GIX_SHIFT 56 + + +#define H_PAGE_BUSY _RPAGE_RPN42/* software: PTE & hash are busy */ +#define H_PAGE_HASHPTE _RPAGE_RPN43/* PTE has associated HPTE */
H_PAGE_BUSYseems to be differently defined here.
Yes. it is using two different bits depending on 4K hpte v/s 64k hpte case. But in the next patch all will be same and consistent.
Quoted 8 lines
Quoted 8 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h +1
-0+/* * We need to differentiate between explicit huge page and THP huge * page, since THP huge page also need to track real subpage details */ #define H_PAGE_THP_HUGE H_PAGE_4K_PFN
Quoted 7 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h line 25
Quoted 5 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h +0
-5-/* -* Used to track subpage group valid if H_PAGE_COMBO is set -* This overloads H_PAGE_F_GIX and H_PAGE_F_SECOND -*/ -#define H_PAGE_COMBO_VALID (H_PAGE_F_GIX | H_PAGE_F_SECOND)
H_PAGE_COMBO_VALIDis not defined alternately ?
it is not needed anymore.
Quoted 10 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h line 31
Quoted 7 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h +2
-3-/* PTE flags to conserve for HPTE identification */ -#define _PAGE_HPTEFLAGS (H_PAGE_BUSY | H_PAGE_F_SECOND | \ - H_PAGE_F_GIX | H_PAGE_HASHPTE | H_PAGE_COMBO) +#define _PAGE_HPTEFLAGS (H_PAGE_BUSY | H_PAGE_HASHPTE | H_PAGE_COMBO) +Slot information has moved to the second half, hence
_PAGE_HPTEFLAGSneed not carry that.
yes.
Quoted 8 lines
Quoted 8 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h +0
-0/* * we support 16 fragments per PTE page of 64K size. */ @@ -74,6 +76,16 @@ static inline unsigned long __rpte_to_hidx(real_pte_t rpte, unsigned long index)return (pte_val (rpte.pte) >>H_PAGE_F_GIX_SHIFT ) &0xf ; }
Quoted 11 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h static inline unsigned long __rpte_to_hidx(real_pte_t rpte, unsigned long index) · line 87
Quoted 9 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h +9
-0+static inline unsigned long set_hidx_slot (pte_t *ptep,real_pte_t rpte, +unsigned int subpg_index,unsigned long slot) +{ +unsigned long *hidxp = (unsigned long *)(ptep +PTRS_PER_PTE ); + + rpte.hidx &= ~(0xfUL << (subpg_index <<2 )); + *hidxp = rpte.hidx | (slot << (subpg_index <<2 )); +return 0x0UL ; +}New method to insert the slot information in the second half.
yes. well it basically trying to reduce code redundancy. Too many places using exactly the same code to accomplish the same thing. Makes sense to bring it all in one place.
Quoted 72 lines
Quoted 10 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h +1
-0+#define __rpte_to_pte (r) ((r).pte)extern bool __rpte_sub_valid (real_pte_t rpte,unsigned long index);/* arch/powerpc/include/asm/book3s/64/hash.h +0
-0arch/powerpc/include/asm/book3s/64/hash.h line 15
Quoted 8 lines
arch/powerpc/include/asm/book3s/64/hash.h +0
-5@@ -8,11 +8,8 @@* */ #define H_PTE_NONE_MASK _PAGE_HPTEFLAGS -#define H_PAGE_F_GIX_SHIFT 56 -#define H_PAGE_BUSY _RPAGE_RSV1/* software: PTE & hash are busy */ -#define H_PAGE_F_SECOND _RPAGE_RSV2/* HPTE is in 2ndary HPTEG */ -#define H_PAGE_F_GIX (_RPAGE_RSV3 | _RPAGE_RSV4 | _RPAGE_RPN44) -#define H_PAGE_HASHPTE _RPAGE_RPN43/* PTE has associated HPTE */ Removing the common definitions.
Quoted 25 lines
arch/powerpc/include/asm/book3s/64/hash.h +10
-0+ +#define INIT_HIDX (~0x0UL)#ifdef CONFIG_PPC_64K_PAGES #include <asm/book3s/64/hash-64k.h> @@ -160,6 +157,14 @@ static inline int hash__pte_none(pte_t pte)return (pte_val (pte) & ~H_PTE_NONE_MASK ) ==0 ; } +static inline bool hpte_soft_invalid (unsigned long slot) +{ +return ((slot &0xfUL ) ==0xfUL ); +} + +unsigned long get_hidx_gslot (unsigned long vpn,unsigned long shift, +int ssize,real_pte_t rpte,unsigned int subpg_index); +/* This low level function performs the actual PTE insertion * Setting the PTE depends on the MMU type and other factors. It's * an horrible mess that I'm not going to try to clean up now but arch/powerpc/include/asm/book3s/64/mmu-hash.h +0
-0arch/powerpc/include/asm/book3s/64/mmu-hash.h extern int __hash_page_4K(unsigned long ea, unsigned long access, · line 441
Quoted 7 lines
arch/powerpc/include/asm/book3s/64/mmu-hash.h +4
-0@@ -435,6 +435,11 @@ extern int __hash_page_4K(unsigned long ea, unsigned long access,extern int __hash_page_64K (unsigned long ea,unsigned long access,unsigned long vsid,pte_t *ptep,unsigned long trap,unsigned long flags,int ssize); +extern unsigned long set_hidx_slot (pte_t *ptep,real_pte_t rpte, +unsigned int subpg_index,unsigned long slot); +extern unsigned long get_hidx_slot (unsigned long vpn,unsigned long shift, +int ssize,real_pte_t rpte,unsigned int subpg_index);I wonder what purpose
set_hidx_slot()defined previously, served.Quoted 16 lines
arch/powerpc/include/asm/book3s/64/mmu-hash.h +1
-0+struct mm_struct ;unsigned int hash_page_do_lazy_icache (unsigned int pp,pte_t pte,int trap); extern int hash_page_mm(struct mm_struct *mm, unsigned long ea,arch/powerpc/mm/dump_linuxpagetables.c +1
-1@@ -213,7 +213,7 @@ struct flag_info { .val = H_PAGE_4K_PFN, .set = "4K_pfn", }, { -#endif +#else .mask = H_PAGE_F_GIX, .val = H_PAGE_F_GIX,
Quoted 8 lines
arch/powerpc/mm/dump_linuxpagetables.c struct flag_info { · line 227
Quoted 6 lines
arch/powerpc/mm/dump_linuxpagetables.c +1
-0.set = "f_gix",@@ -224,6 +224,7 @@ struct flag_info { .val = H_PAGE_F_SECOND, .set = "f_second", }, { +#endif /* CONFIG_PPC_64K_PAGES */ Are we adding
H_PAGE_F_GIXas an element for 4K mapping ?
I think there is mistake here.
In the next patch when these bits are divorsed from 64K ptes entirely, we will not need the above code for 64K ptes. But good catch. Will fix the error in this patch.
Quoted 22 lines
Quoted 22 lines
arch/powerpc/mm/dump_linuxpagetables.c +0
-0#endif .mask = _PAGE_SPECIAL, .val = _PAGE_SPECIAL,arch/powerpc/mm/hash64_4k.c +2
-0@@ -20,6 +20,7 @@ int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid, pte_t *ptep, unsigned long trap, unsigned long flags, int ssize, int subpg_prot) { + real_pte_t rpte;unsigned long hpte_group;unsigned long rflags, pa;unsigned long old_pte, new_pte;@@ -54,6 +55,7 @@ int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid,* need to add in 0x1 if it's a read-only user page */ rflags =htab_convert_pte_flags (new_pte); + rpte =__real_pte (__pte (old_pte), ptep);
Quoted 16 lines
arch/powerpc/mm/hash64_4k.c int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid, · line 70
Quoted 13 lines
arch/powerpc/mm/hash64_4k.c +2
-5if (cpu_has_feature (CPU_FTR_NOEXECUTE ) && !cpu_has_feature (CPU_FTR_COHERENT_ICACHE ))@@ -64,13 +66,10 @@ int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid,/* * There MIGHT be an HPTE for this pte */ - hash =hpt_hash (vpn, shift, ssize); -if (old_pte &H_PAGE_F_SECOND ) - hash = ~hash; - slot = (hash & htab_hash_mask) *HPTES_PER_GROUP ; - slot += (old_pte &H_PAGE_F_GIX ) >>H_PAGE_F_GIX_SHIFT ; +unsigned long gslot =get_hidx_gslot (vpn, shift, + ssize, rpte,0 );I am wondering why there is a 'g' before the slot in all these functions.
Right. even i was confused initially. :)
hash table slots are originized as one big table. 8 consecutive entires in that table form a bucket. the term slot is used to refer to the slot within the bucket. the term gslot is used to refer to an entry in the table. roughly speaking slot 2 in bucket 2, will be gslot 2*8+2=18.
Quoted 5 lines
Its already too much of changes in a single patch. Being a single logical change it needs to be inside a single change but then we need much more description in the commit message for some one to understand what all changed and how.
I have further broken down this patch, one to introduce get_hidx_gslot() one to introduce set_hidx_slot() . Hopefully that will reduce the size of the patch to graspable level. let me know,
Thanks for your valuable comments,
RP
Signature · 1 line
Ram Pai
Re: [RFC v2 01/12] powerpc: Free up four 64K PTE bits in 4K backed hpte pages.
Quoted 44 lines · Ram Pai
Quoted 17 lines · Anshuman Khandual
Quoted 4 lines · Ram Pai
Rearrange 64K PTE bits to free up bits 3, 4, 5 and 6 in the 4K backed hpte pages. These bits continue to be used for 64K backed hpte pages in this patch, but will be freed up in the next patch.
The counting 3, 4, 5 and 6 are in BE format I believe, I was initially trying to see that from right to left as we normally do in the kernel and was getting confused. So basically these bits (which are only applicable for 64K mapping IIUC) are going to be freed up from the PTE format.
#define
_RPAGE_RSV10x1000000000000000UL
#define_RPAGE_RSV20x0800000000000000UL
#define_RPAGE_RSV30x0400000000000000UL
#define_RPAGE_RSV40x0200000000000000ULQuoted 6 lines
As you have mentioned before this feature is available for 64K page size only and not for 4K mappings. So I assume we support both the combinations.
* 64K mapping on 64K
* 64K mapping on 4Kyes.
Quoted 17 lines
These are the current users of the above bits
#define
H_PAGE_BUSY_RPAGE_RSV1/* software: PTE & hash are busy */
#defineH_PAGE_F_SECOND_RPAGE_RSV2/* HPTE is in 2ndary HPTEG */
#defineH_PAGE_F_GIX(_RPAGE_RSV3|_RPAGE_RSV4|_RPAGE_RPN44)
#defineH_PAGE_HASHPTE_RPAGE_RPN43/* PTE has associated HPTE */Quoted 4 lines
The patch does the following change to the 64K PTE format
H_PAGE_BUSYmoves from bit 3 to bit 9and what is in there on bit 9 now ? This ?
#define
_RPAGE_SW20x00400
Quoted 8 lines
Quoted 5 lines
which is used as
#define
_PAGE_SPECIAL_RPAGE_SW2/* software: special page */which will not be required any more ?
i think you are reading bit 9 from right to left. the bit 9 i refer to is from left to right. Using the same numbering convention the ISA3.0 uses.
Right, my bad. Then it would be this one.
'#define _RPAGE_RPN42 0x0040000000000000UL'
Quoted 2 lines
I know it is confusing, will make a mention in the comment of this patch, to read it the big-endian way.
Right.
Quoted 6 lines
BTW: Bit 9 is not used currently. so using it in this patch. But this is a temporary move. the
H_PAGE_BUSYwill move to bit 7 in the next patch.Had to keep at bit 9, because bit 7 is not yet entirely freed up. it is used by 64K PTE backed by 64k htpe.
Got it.
Quoted 41 lines
Quoted 7 lines
Quoted 7 lines
H_PAGE_F_SECONDwhich occupied bit 4 moves to the second part
of the pte.H_PAGE_F_GIXwhich occupied bit 5, 6 and 7 also moves to the
second part of the pte.Quoted 6 lines
Quoted 4 lines
the four bits((H_PAGE_F_SECOND|H_PAGE_F_GIX) that represent a slot is initialized to
0xFindicating an invalid slot. If a hpte
gets cached in a0xFslot(i.e 7th slot of secondary), it is
released immediately. In other words, even though0xFis aRelease immediately means we attempt again for a new hash slot ?
yes.
Quoted 8 lines
Quoted 5 lines
valid slot we discard and consider it as an invalid slot;i.e
hpte_soft_invalid(). This gives us an opportunity to not depend on a bit in the primary PTE in order to determine the validity of a slot.So we have to see the slot number in the second half for each PTE to figure out if it has got a valid slot in the hash page table.
yes.
Quoted 14 lines
Quoted 6 lines
When we release a hpte in the
0xFslot we also release a
legitimate primary slot and unmap that entry. This is to
ensure that we do get a legimate non-0xF slot the next time we
retry for a slot.Okay.
Quoted 4 lines
Though treating
0xFslot as invalid reduces the number of available slots and may have an effect on the performance, the probabilty of hitting a0xFis extermely low.
Quoted 6 lines
Quoted 2 lines
Why you say that ? I thought every slot number has the same probability of hit from the hash function.
Every hash bucket has the same probability. But every slot within the hash bucket is filled in sequentially. so it takes 15 hptes to hash to the same bucket before we get to the 15th slot in the secondary.
Okay, would the last one be 16th instead ?
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Compared to the current scheme, the above described scheme reduces the number of false hash table updates significantly and has the
Quoted 8 lines
Quoted 1 line
How it reduces false hash table updates ?
earlier, we had 1 bit allocated in the first-part-of-the 64K-PTE for four consecutive 4K hptes. If any one 4k hpte got hashed-in, the bit got set. Which means anytime it faulted on the remaining three 4k hpte, we saw the bit already set and tried to erroneously update that hpte. So we had a 75% update error rate. Funcationally not bad, but bad from a performance point of view.
I am bit out of sync regarding these PTE bits, after Aneesh's radix changes went in :) Will look into this bit closer.
Quoted 6 lines
With the current scheme, we decide if a 4k slot is valid by looking at its value rather than depending on a bit in the main-pte. So there is no chance of getting mislead. And hence no chance of trying to update a invalid hpte. Should improve performance and at the same time give us four valuable PTE bits.
I am not sure why you say 'invalid hpte'. IIUC
* We will require 16 '64K on 4K' mappings to actually cover 64K on 64K
* A single (64K on 4K)'s TLB can cover 64K on 64K as long as the TLB is present and not flushed. That gets us performance. Once flushed, a new HPTE entry covering new (64K on 4K) is inserted. As long as the PFN for the 4K is different HPTE will be different and it cannot collide with any existing ones and create problems (ERAT error ?)
As you are pointing out, I am not sure whether the existing design had more probability for an invalid HPTE insert. Will look into this in detail.
Quoted 157 lines
Quoted 31 lines
Quoted 31 lines
added advantage of releasing four valuable PTE bits for other purpose.
This idea was jointly developed by Paul Mackerras, Aneesh, Michael Ellermen and myself.
4K PTE format remain unchanged currently.
Signed-off-by Ram Pai <[email protected]>--- arch/powerpc/include/asm/book3s/64/hash-4k.h | 20 +++++++ arch/powerpc/include/asm/book3s/64/hash-64k.h | 32 +++++++---- arch/powerpc/include/asm/book3s/64/hash.h | 15 +++-- arch/powerpc/include/asm/book3s/64/mmu-hash.h | 5 ++ arch/powerpc/mm/dump_linuxpagetables.c | 3 +- arch/powerpc/mm/hash64_4k.c | 14 ++--- arch/powerpc/mm/hash64_64k.c | 81 ++++++++++++--------------- arch/powerpc/mm/hash_utils_64.c | 30 +++++++--- 8 files changed, 122 insertions(+), 78 deletions(-)arch/powerpc/include/asm/book3s/64/hash-4k.h +0
-0@@ -16,6 +16,18 @@#define H_PUD_TABLE_SIZE (sizeof(pud_t) << H_PUD_INDEX_SIZE)#define H_PGD_TABLE_SIZE (sizeof(pgd_t) << H_PGD_INDEX_SIZE)Quoted 14 lines
arch/powerpc/include/asm/book3s/64/hash-4k.h line 30
Quoted 12 lines
arch/powerpc/include/asm/book3s/64/hash-4k.h +12
-0+ +/* +* Only supported by 4k linux page size +*/ +#define H_PAGE_F_SECOND _RPAGE_RSV2/* HPTE is in 2ndary HPTEG */ +#define H_PAGE_F_GIX (_RPAGE_RSV3 | _RPAGE_RSV4 | _RPAGE_RPN44) +#define H_PAGE_F_GIX_SHIFT 56 + +#define H_PAGE_BUSY _RPAGE_RSV1/* software: PTE & hash are busy */ +#define H_PAGE_HASHPTE _RPAGE_RPN43/* PTE has associated HPTE */ + +So we moved the common 64K definitions here.
yes.
Quoted 9 lines
Quoted 9 lines
arch/powerpc/include/asm/book3s/64/hash-4k.h +0
-0/* PTE flags to conserve for HPTE identification */ #define _PAGE_HPTEFLAGS (H_PAGE_BUSY | H_PAGE_HASHPTE | \ H_PAGE_F_SECOND | H_PAGE_F_GIX)@@ -48,6 +60,14 @@ static inline int hash__hugepd_ok(hugepd_t hpd) }#endif Quoted 9 lines
arch/powerpc/include/asm/book3s/64/hash-4k.h static inline int hash__hugepd_ok(hugepd_t hpd) · line 68
Quoted 6 lines
arch/powerpc/include/asm/book3s/64/hash-4k.h +6
-0+static inline unsigned long set_hidx_slot (pte_t *ptep,real_pte_t rpte, +unsigned int subpg_index,unsigned long slot) +{ +return (slot <<H_PAGE_F_GIX_SHIFT ) & + (H_PAGE_F_SECOND |H_PAGE_F_GIX ); +}Why we are passing the first 3 arguments of the function if we never use it inside. Is the caller expected to take care of it ?
trying to keep the same prototype for the 4K-pte and 64K-pte cases. Otherwise the caller has to wonder which parameter scheme to use.
Quoted 5 lines
Quoted 5 lines
arch/powerpc/include/asm/book3s/64/hash-4k.h +2
-0+ +#ifdef CONFIG_TRANSPARENT_HUGEPAGE Quoted 15 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h line 14
Quoted 13 lines
arch/powerpc/include/asm/book3s/64/hash-4k.h +0
-0static inline char *get_hpte_slot_array (pmd_t *pmdp)arch/powerpc/include/asm/book3s/64/hash-64k.h +2
-2@@ -10,23 +10,25 @@* 64k aligned address free up few of the lower bits of RPN for us * We steal that here. For more deatils look at pte_pfn/pfn_pte() */ -#define H_PAGE_COMBO _RPAGE_RPN0/* this is a combo 4k page */ -#define H_PAGE_4K_PFN _RPAGE_RPN1/* PFN is for a single 4k page */ +#define H_PAGE_COMBO _RPAGE_RPN0/* this is a combo 4k page */ +#define H_PAGE_4K_PFN _RPAGE_RPN1/* PFN is for a single 4k page */ Its the same thing, changes nothing.
it fixes some space/tab problem.
Quoted 10 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h line 21
Quoted 8 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h +7
-0+#define H_PAGE_F_SECOND _RPAGE_RSV2/* HPTE is in 2ndary HPTEG */ +#define H_PAGE_F_GIX (_RPAGE_RSV3 | _RPAGE_RSV4 | _RPAGE_RPN44) +#define H_PAGE_F_GIX_SHIFT 56 + + +#define H_PAGE_BUSY _RPAGE_RPN42/* software: PTE & hash are busy */ +#define H_PAGE_HASHPTE _RPAGE_RPN43/* PTE has associated HPTE */
H_PAGE_BUSYseems to be differently defined here.Yes. it is using two different bits depending on 4K hpte v/s 64k hpte case. But in the next patch all will be same and consistent.
Quoted 8 lines
Quoted 8 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h +1
-0+/* * We need to differentiate between explicit huge page and THP huge * page, since THP huge page also need to track real subpage details */ #define H_PAGE_THP_HUGE H_PAGE_4K_PFNQuoted 7 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h line 25
Quoted 5 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h +0
-5-/* -* Used to track subpage group valid if H_PAGE_COMBO is set -* This overloads H_PAGE_F_GIX and H_PAGE_F_SECOND -*/ -#define H_PAGE_COMBO_VALID (H_PAGE_F_GIX | H_PAGE_F_SECOND)
H_PAGE_COMBO_VALIDis not defined alternately ?it is not needed anymore.
Quoted 10 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h line 31
Quoted 7 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h +2
-3-/* PTE flags to conserve for HPTE identification */ -#define _PAGE_HPTEFLAGS (H_PAGE_BUSY | H_PAGE_F_SECOND | \ - H_PAGE_F_GIX | H_PAGE_HASHPTE | H_PAGE_COMBO) +#define _PAGE_HPTEFLAGS (H_PAGE_BUSY | H_PAGE_HASHPTE | H_PAGE_COMBO) +Slot information has moved to the second half, hence
_PAGE_HPTEFLAGSneed not carry that.yes.
Quoted 8 lines
Quoted 8 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h +0
-0/* * we support 16 fragments per PTE page of 64K size. */ @@ -74,6 +76,16 @@ static inline unsigned long __rpte_to_hidx(real_pte_t rpte, unsigned long index)return (pte_val (rpte.pte) >>H_PAGE_F_GIX_SHIFT ) &0xf ; }Quoted 11 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h static inline unsigned long __rpte_to_hidx(real_pte_t rpte, unsigned long index) · line 87
Quoted 9 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h +9
-0+static inline unsigned long set_hidx_slot (pte_t *ptep,real_pte_t rpte, +unsigned int subpg_index,unsigned long slot) +{ +unsigned long *hidxp = (unsigned long *)(ptep +PTRS_PER_PTE ); + + rpte.hidx &= ~(0xfUL << (subpg_index <<2 )); + *hidxp = rpte.hidx | (slot << (subpg_index <<2 )); +return 0x0UL ; +}New method to insert the slot information in the second half.
Quoted 3 lines
yes. well it basically trying to reduce code redundancy. Too many places using exactly the same code to accomplish the same thing. Makes sense to bring it all in one place.
Right.
Quoted 126 lines
Quoted 73 lines
Quoted 11 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h +1
-0+#define __rpte_to_pte (r) ((r).pte)extern bool __rpte_sub_valid (real_pte_t rpte,unsigned long index);/* arch/powerpc/include/asm/book3s/64/hash.h +0
-0arch/powerpc/include/asm/book3s/64/hash.h line 15
Quoted 8 lines
arch/powerpc/include/asm/book3s/64/hash.h +0
-5@@ -8,11 +8,8 @@* */ #define H_PTE_NONE_MASK _PAGE_HPTEFLAGS -#define H_PAGE_F_GIX_SHIFT 56 -#define H_PAGE_BUSY _RPAGE_RSV1/* software: PTE & hash are busy */ -#define H_PAGE_F_SECOND _RPAGE_RSV2/* HPTE is in 2ndary HPTEG */ -#define H_PAGE_F_GIX (_RPAGE_RSV3 | _RPAGE_RSV4 | _RPAGE_RPN44) -#define H_PAGE_HASHPTE _RPAGE_RPN43/* PTE has associated HPTE */ Removing the common definitions.
Quoted 25 lines
arch/powerpc/include/asm/book3s/64/hash.h +10
-0+ +#define INIT_HIDX (~0x0UL)#ifdef CONFIG_PPC_64K_PAGES #include <asm/book3s/64/hash-64k.h> @@ -160,6 +157,14 @@ static inline int hash__pte_none(pte_t pte)return (pte_val (pte) & ~H_PTE_NONE_MASK ) ==0 ; } +static inline bool hpte_soft_invalid (unsigned long slot) +{ +return ((slot &0xfUL ) ==0xfUL ); +} + +unsigned long get_hidx_gslot (unsigned long vpn,unsigned long shift, +int ssize,real_pte_t rpte,unsigned int subpg_index); +/* This low level function performs the actual PTE insertion * Setting the PTE depends on the MMU type and other factors. It's * an horrible mess that I'm not going to try to clean up now but arch/powerpc/include/asm/book3s/64/mmu-hash.h +0
-0arch/powerpc/include/asm/book3s/64/mmu-hash.h extern int __hash_page_4K(unsigned long ea, unsigned long access, · line 441
Quoted 7 lines
arch/powerpc/include/asm/book3s/64/mmu-hash.h +4
-0@@ -435,6 +435,11 @@ extern int __hash_page_4K(unsigned long ea, unsigned long access,extern int __hash_page_64K (unsigned long ea,unsigned long access,unsigned long vsid,pte_t *ptep,unsigned long trap,unsigned long flags,int ssize); +extern unsigned long set_hidx_slot (pte_t *ptep,real_pte_t rpte, +unsigned int subpg_index,unsigned long slot); +extern unsigned long get_hidx_slot (unsigned long vpn,unsigned long shift, +int ssize,real_pte_t rpte,unsigned int subpg_index);I wonder what purpose
set_hidx_slot()defined previously, served.Quoted 16 lines
arch/powerpc/include/asm/book3s/64/mmu-hash.h +1
-0+struct mm_struct ;unsigned int hash_page_do_lazy_icache (unsigned int pp,pte_t pte,int trap); extern int hash_page_mm(struct mm_struct *mm, unsigned long ea,arch/powerpc/mm/dump_linuxpagetables.c +1
-1@@ -213,7 +213,7 @@ struct flag_info { .val = H_PAGE_4K_PFN, .set = "4K_pfn", }, { -#endif +#else .mask = H_PAGE_F_GIX, .val = H_PAGE_F_GIX,Quoted 8 lines
arch/powerpc/mm/dump_linuxpagetables.c struct flag_info { · line 227
Quoted 6 lines
arch/powerpc/mm/dump_linuxpagetables.c +1
-0.set = "f_gix",@@ -224,6 +224,7 @@ struct flag_info { .val = H_PAGE_F_SECOND, .set = "f_second", }, { +#endif /* CONFIG_PPC_64K_PAGES */ Are we adding
H_PAGE_F_GIXas an element for 4K mapping ?I think there is mistake here.
In the next patch when these bits are divorsed from 64K ptes entirely, we will not need the above code for 64K ptes. But good catch. Will fix the error in this patch.Quoted 22 lines
Quoted 22 lines
arch/powerpc/mm/dump_linuxpagetables.c +0
-0#endif .mask = _PAGE_SPECIAL, .val = _PAGE_SPECIAL,arch/powerpc/mm/hash64_4k.c +2
-0@@ -20,6 +20,7 @@ int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid, pte_t *ptep, unsigned long trap, unsigned long flags, int ssize, int subpg_prot) { + real_pte_t rpte;unsigned long hpte_group;unsigned long rflags, pa;unsigned long old_pte, new_pte;@@ -54,6 +55,7 @@ int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid,* need to add in 0x1 if it's a read-only user page */ rflags =htab_convert_pte_flags (new_pte); + rpte =__real_pte (__pte (old_pte), ptep);Quoted 16 lines
arch/powerpc/mm/hash64_4k.c int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid, · line 70
Quoted 13 lines
arch/powerpc/mm/hash64_4k.c +2
-5if (cpu_has_feature (CPU_FTR_NOEXECUTE ) && !cpu_has_feature (CPU_FTR_COHERENT_ICACHE ))@@ -64,13 +66,10 @@ int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid,/* * There MIGHT be an HPTE for this pte */ - hash =hpt_hash (vpn, shift, ssize); -if (old_pte &H_PAGE_F_SECOND ) - hash = ~hash; - slot = (hash & htab_hash_mask) *HPTES_PER_GROUP ; - slot += (old_pte &H_PAGE_F_GIX ) >>H_PAGE_F_GIX_SHIFT ; +unsigned long gslot =get_hidx_gslot (vpn, shift, + ssize, rpte,0 );I am wondering why there is a 'g' before the slot in all these functions.
Quoted 6 lines
Right. even i was confused initially. :)
hash table slots are originized as one big table. 8 consecutive entires in that table form a bucket. the term slot is used to refer to the slot within the bucket. the term gslot is used to refer to an entry in the table. roughly speaking slot 2 in bucket 2, will be gslot 2*8+2=18.
Global slot as it can point any where on that two dimensional table ?
Quoted 10 lines
Quoted 6 lines
Its already too much of changes in a single patch. Being a single logical change it needs to be inside a single change but then we need much more description in the commit message for some one to understand what all changed and how.
I have further broken down this patch, one to introduce
get_hidx_gslot()one to introduceset_hidx_slot(). Hopefully that will reduce the size of the patch to graspable level. let me know,
I did some experiments with the first two patches.
* First of all the first patch does not compile without this.
arch/powerpc/mm/hash_utils_64.c +1 -1
@@ -1612,7 +1612,7 @@ unsigned long get_hidx_gslot(unsigned long vpn, unsigned long shift,void flush_hash_page (unsigned long vpn,real_pte_t pte,int psize,int ssize,unsigned long flags) { -unsigned long hash, index, shift, hidx, gslot; +unsigned long index, shift, gslot;int local = flags &HPTE_LOCAL_UPDATE ;DBG_LOW ("flush_hash_page(vpn=%016lx)\n" , vpn);
* Though it boots the kernel, system is kind of unresponsive while attempting to compile a kernel. Though I did not dig further on this, seems like the first patch is not self sufficient yet.
* With both first and second patch, the kernel boots fine and compiles a kernel.
We need to sort out issues in the first two patches before looking into the rest of the patch series.
Re: [RFC v2 01/12] powerpc: Free up four 64K PTE bits in 4K backed hpte pages.
Quoted 141 lines · Anshuman Khandual
Quoted 44 lines · Ram Pai
Quoted 17 lines · Anshuman Khandual
Quoted 4 lines · Ram Pai
Rearrange 64K PTE bits to free up bits 3, 4, 5 and 6 in the 4K backed hpte pages. These bits continue to be used for 64K backed hpte pages in this patch, but will be freed up in the next patch.
The counting 3, 4, 5 and 6 are in BE format I believe, I was initially trying to see that from right to left as we normally do in the kernel and was getting confused. So basically these bits (which are only applicable for 64K mapping IIUC) are going to be freed up from the PTE format.
#define
_RPAGE_RSV10x1000000000000000UL
#define_RPAGE_RSV20x0800000000000000UL
#define_RPAGE_RSV30x0400000000000000UL
#define_RPAGE_RSV40x0200000000000000ULQuoted 6 lines
As you have mentioned before this feature is available for 64K page size only and not for 4K mappings. So I assume we support both the combinations.
* 64K mapping on 64K
* 64K mapping on 4Kyes.
Quoted 17 lines
These are the current users of the above bits
#define
H_PAGE_BUSY_RPAGE_RSV1/* software: PTE & hash are busy */
#defineH_PAGE_F_SECOND_RPAGE_RSV2/* HPTE is in 2ndary HPTEG */
#defineH_PAGE_F_GIX(_RPAGE_RSV3|_RPAGE_RSV4|_RPAGE_RPN44)
#defineH_PAGE_HASHPTE_RPAGE_RPN43/* PTE has associated HPTE */Quoted 4 lines
The patch does the following change to the 64K PTE format
H_PAGE_BUSYmoves from bit 3 to bit 9and what is in there on bit 9 now ? This ?
#define
_RPAGE_SW20x00400Quoted 8 lines
Quoted 5 lines
which is used as
#define
_PAGE_SPECIAL_RPAGE_SW2/* software: special page */which will not be required any more ?
i think you are reading bit 9 from right to left. the bit 9 i refer to is from left to right. Using the same numbering convention the ISA3.0 uses.
Right, my bad. Then it would be this one.
'#define
_RPAGE_RPN420x0040000000000000UL'Quoted 2 lines
I know it is confusing, will make a mention in the comment of this patch, to read it the big-endian way.
Right.
Quoted 6 lines
BTW: Bit 9 is not used currently. so using it in this patch. But this is a temporary move. the
H_PAGE_BUSYwill move to bit 7 in the next patch.Had to keep at bit 9, because bit 7 is not yet entirely freed up. it is used by 64K PTE backed by 64k htpe.
Got it.
Quoted 41 lines
Quoted 7 lines
Quoted 7 lines
H_PAGE_F_SECONDwhich occupied bit 4 moves to the second part
of the pte.H_PAGE_F_GIXwhich occupied bit 5, 6 and 7 also moves to the
second part of the pte.Quoted 6 lines
Quoted 4 lines
the four bits((H_PAGE_F_SECOND|H_PAGE_F_GIX) that represent a slot is initialized to
0xFindicating an invalid slot. If a hpte
gets cached in a0xFslot(i.e 7th slot of secondary), it is
released immediately. In other words, even though0xFis aRelease immediately means we attempt again for a new hash slot ?
yes.
Quoted 8 lines
Quoted 5 lines
valid slot we discard and consider it as an invalid slot;i.e
hpte_soft_invalid(). This gives us an opportunity to not depend on a bit in the primary PTE in order to determine the validity of a slot.So we have to see the slot number in the second half for each PTE to figure out if it has got a valid slot in the hash page table.
yes.
Quoted 14 lines
Quoted 6 lines
When we release a hpte in the
0xFslot we also release a
legitimate primary slot and unmap that entry. This is to
ensure that we do get a legimate non-0xF slot the next time we
retry for a slot.Okay.
Quoted 4 lines
Though treating
0xFslot as invalid reduces the number of available slots and may have an effect on the performance, the probabilty of hitting a0xFis extermely low.Quoted 6 lines
Quoted 2 lines
Why you say that ? I thought every slot number has the same probability of hit from the hash function.
Every hash bucket has the same probability. But every slot within the hash bucket is filled in sequentially. so it takes 15 hptes to hash to the same bucket before we get to the 15th slot in the secondary.
Okay, would the last one be 16th instead ?
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Compared to the current scheme, the above described scheme reduces the number of false hash table updates significantly and has the
Quoted 8 lines
Quoted 1 line
How it reduces false hash table updates ?
earlier, we had 1 bit allocated in the first-part-of-the 64K-PTE for four consecutive 4K hptes. If any one 4k hpte got hashed-in, the bit got set. Which means anytime it faulted on the remaining three 4k hpte, we saw the bit already set and tried to erroneously update that hpte. So we had a 75% update error rate. Funcationally not bad, but bad from a performance point of view.
I am bit out of sync regarding these PTE bits, after Aneesh's radix changes went in :) Will look into this bit closer.
Quoted 1 line
Quoted 7 lines
Quoted 5 lines
With the current scheme, we decide if a 4k slot is valid by looking at its value rather than depending on a bit in the main-pte. So there is no chance of getting mislead. And hence no chance of trying to update a invalid hpte. Should improve performance and at the same time give us four valuable PTE bits.
I am not sure why you say 'invalid hpte'. IIUC
I mean to say a entry which does not yet have a mapped hpte.
Quoted 318 lines
* We will require 16 '64K on 4K' mappings to actually cover 64K on 64K
* A single (64K on 4K)'s TLB can cover 64K on 64K as long as the TLB is present and not flushed. That gets us performance. Once flushed, a new HPTE entry covering new (64K on 4K) is inserted. As long as the PFN for the 4K is different HPTE will be different and it cannot collide with any existing ones and create problems (ERAT error ?)
As you are pointing out, I am not sure whether the existing design had more probability for an invalid HPTE insert. Will look into this in detail.
Quoted 157 lines
Quoted 31 lines
Quoted 31 lines
added advantage of releasing four valuable PTE bits for other purpose.
This idea was jointly developed by Paul Mackerras, Aneesh, Michael Ellermen and myself.
4K PTE format remain unchanged currently.
Signed-off-by Ram Pai <[email protected]>--- arch/powerpc/include/asm/book3s/64/hash-4k.h | 20 +++++++ arch/powerpc/include/asm/book3s/64/hash-64k.h | 32 +++++++---- arch/powerpc/include/asm/book3s/64/hash.h | 15 +++-- arch/powerpc/include/asm/book3s/64/mmu-hash.h | 5 ++ arch/powerpc/mm/dump_linuxpagetables.c | 3 +- arch/powerpc/mm/hash64_4k.c | 14 ++--- arch/powerpc/mm/hash64_64k.c | 81 ++++++++++++--------------- arch/powerpc/mm/hash_utils_64.c | 30 +++++++--- 8 files changed, 122 insertions(+), 78 deletions(-)arch/powerpc/include/asm/book3s/64/hash-4k.h +0
-0@@ -16,6 +16,18 @@#define H_PUD_TABLE_SIZE (sizeof(pud_t) << H_PUD_INDEX_SIZE)#define H_PGD_TABLE_SIZE (sizeof(pgd_t) << H_PGD_INDEX_SIZE)Quoted 14 lines
arch/powerpc/include/asm/book3s/64/hash-4k.h line 30
Quoted 12 lines
arch/powerpc/include/asm/book3s/64/hash-4k.h +12
-0+ +/* +* Only supported by 4k linux page size +*/ +#define H_PAGE_F_SECOND _RPAGE_RSV2/* HPTE is in 2ndary HPTEG */ +#define H_PAGE_F_GIX (_RPAGE_RSV3 | _RPAGE_RSV4 | _RPAGE_RPN44) +#define H_PAGE_F_GIX_SHIFT 56 + +#define H_PAGE_BUSY _RPAGE_RSV1/* software: PTE & hash are busy */ +#define H_PAGE_HASHPTE _RPAGE_RPN43/* PTE has associated HPTE */ + +So we moved the common 64K definitions here.
yes.
Quoted 9 lines
Quoted 9 lines
arch/powerpc/include/asm/book3s/64/hash-4k.h +0
-0/* PTE flags to conserve for HPTE identification */ #define _PAGE_HPTEFLAGS (H_PAGE_BUSY | H_PAGE_HASHPTE | \ H_PAGE_F_SECOND | H_PAGE_F_GIX)@@ -48,6 +60,14 @@ static inline int hash__hugepd_ok(hugepd_t hpd) }#endif Quoted 9 lines
arch/powerpc/include/asm/book3s/64/hash-4k.h static inline int hash__hugepd_ok(hugepd_t hpd) · line 68
Quoted 6 lines
arch/powerpc/include/asm/book3s/64/hash-4k.h +6
-0+static inline unsigned long set_hidx_slot (pte_t *ptep,real_pte_t rpte, +unsigned int subpg_index,unsigned long slot) +{ +return (slot <<H_PAGE_F_GIX_SHIFT ) & + (H_PAGE_F_SECOND |H_PAGE_F_GIX ); +}Why we are passing the first 3 arguments of the function if we never use it inside. Is the caller expected to take care of it ?
trying to keep the same prototype for the 4K-pte and 64K-pte cases. Otherwise the caller has to wonder which parameter scheme to use.
Quoted 5 lines
Quoted 5 lines
arch/powerpc/include/asm/book3s/64/hash-4k.h +2
-0+ +#ifdef CONFIG_TRANSPARENT_HUGEPAGE Quoted 15 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h line 14
Quoted 13 lines
arch/powerpc/include/asm/book3s/64/hash-4k.h +0
-0static inline char *get_hpte_slot_array (pmd_t *pmdp)arch/powerpc/include/asm/book3s/64/hash-64k.h +2
-2@@ -10,23 +10,25 @@* 64k aligned address free up few of the lower bits of RPN for us * We steal that here. For more deatils look at pte_pfn/pfn_pte() */ -#define H_PAGE_COMBO _RPAGE_RPN0/* this is a combo 4k page */ -#define H_PAGE_4K_PFN _RPAGE_RPN1/* PFN is for a single 4k page */ +#define H_PAGE_COMBO _RPAGE_RPN0/* this is a combo 4k page */ +#define H_PAGE_4K_PFN _RPAGE_RPN1/* PFN is for a single 4k page */ Its the same thing, changes nothing.
it fixes some space/tab problem.
Quoted 10 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h line 21
Quoted 8 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h +7
-0+#define H_PAGE_F_SECOND _RPAGE_RSV2/* HPTE is in 2ndary HPTEG */ +#define H_PAGE_F_GIX (_RPAGE_RSV3 | _RPAGE_RSV4 | _RPAGE_RPN44) +#define H_PAGE_F_GIX_SHIFT 56 + + +#define H_PAGE_BUSY _RPAGE_RPN42/* software: PTE & hash are busy */ +#define H_PAGE_HASHPTE _RPAGE_RPN43/* PTE has associated HPTE */
H_PAGE_BUSYseems to be differently defined here.Yes. it is using two different bits depending on 4K hpte v/s 64k hpte case. But in the next patch all will be same and consistent.
Quoted 8 lines
Quoted 8 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h +1
-0+/* * We need to differentiate between explicit huge page and THP huge * page, since THP huge page also need to track real subpage details */ #define H_PAGE_THP_HUGE H_PAGE_4K_PFNQuoted 7 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h line 25
Quoted 5 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h +0
-5-/* -* Used to track subpage group valid if H_PAGE_COMBO is set -* This overloads H_PAGE_F_GIX and H_PAGE_F_SECOND -*/ -#define H_PAGE_COMBO_VALID (H_PAGE_F_GIX | H_PAGE_F_SECOND)
H_PAGE_COMBO_VALIDis not defined alternately ?it is not needed anymore.
Quoted 10 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h line 31
Quoted 7 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h +2
-3-/* PTE flags to conserve for HPTE identification */ -#define _PAGE_HPTEFLAGS (H_PAGE_BUSY | H_PAGE_F_SECOND | \ - H_PAGE_F_GIX | H_PAGE_HASHPTE | H_PAGE_COMBO) +#define _PAGE_HPTEFLAGS (H_PAGE_BUSY | H_PAGE_HASHPTE | H_PAGE_COMBO) +Slot information has moved to the second half, hence
_PAGE_HPTEFLAGSneed not carry that.yes.
Quoted 8 lines
Quoted 8 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h +0
-0/* * we support 16 fragments per PTE page of 64K size. */ @@ -74,6 +76,16 @@ static inline unsigned long __rpte_to_hidx(real_pte_t rpte, unsigned long index)return (pte_val (rpte.pte) >>H_PAGE_F_GIX_SHIFT ) &0xf ; }Quoted 11 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h static inline unsigned long __rpte_to_hidx(real_pte_t rpte, unsigned long index) · line 87
Quoted 9 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h +9
-0+static inline unsigned long set_hidx_slot (pte_t *ptep,real_pte_t rpte, +unsigned int subpg_index,unsigned long slot) +{ +unsigned long *hidxp = (unsigned long *)(ptep +PTRS_PER_PTE ); + + rpte.hidx &= ~(0xfUL << (subpg_index <<2 )); + *hidxp = rpte.hidx | (slot << (subpg_index <<2 )); +return 0x0UL ; +}New method to insert the slot information in the second half.
Quoted 3 lines
yes. well it basically trying to reduce code redundancy. Too many places using exactly the same code to accomplish the same thing. Makes sense to bring it all in one place.
Right.
Quoted 126 lines
Quoted 73 lines
Quoted 11 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h +1
-0+#define __rpte_to_pte (r) ((r).pte)extern bool __rpte_sub_valid (real_pte_t rpte,unsigned long index);/* arch/powerpc/include/asm/book3s/64/hash.h +0
-0arch/powerpc/include/asm/book3s/64/hash.h line 15
Quoted 8 lines
arch/powerpc/include/asm/book3s/64/hash.h +0
-5@@ -8,11 +8,8 @@* */ #define H_PTE_NONE_MASK _PAGE_HPTEFLAGS -#define H_PAGE_F_GIX_SHIFT 56 -#define H_PAGE_BUSY _RPAGE_RSV1/* software: PTE & hash are busy */ -#define H_PAGE_F_SECOND _RPAGE_RSV2/* HPTE is in 2ndary HPTEG */ -#define H_PAGE_F_GIX (_RPAGE_RSV3 | _RPAGE_RSV4 | _RPAGE_RPN44) -#define H_PAGE_HASHPTE _RPAGE_RPN43/* PTE has associated HPTE */ Removing the common definitions.
Quoted 25 lines
arch/powerpc/include/asm/book3s/64/hash.h +10
-0+ +#define INIT_HIDX (~0x0UL)#ifdef CONFIG_PPC_64K_PAGES #include <asm/book3s/64/hash-64k.h> @@ -160,6 +157,14 @@ static inline int hash__pte_none(pte_t pte)return (pte_val (pte) & ~H_PTE_NONE_MASK ) ==0 ; } +static inline bool hpte_soft_invalid (unsigned long slot) +{ +return ((slot &0xfUL ) ==0xfUL ); +} + +unsigned long get_hidx_gslot (unsigned long vpn,unsigned long shift, +int ssize,real_pte_t rpte,unsigned int subpg_index); +/* This low level function performs the actual PTE insertion * Setting the PTE depends on the MMU type and other factors. It's * an horrible mess that I'm not going to try to clean up now but arch/powerpc/include/asm/book3s/64/mmu-hash.h +0
-0arch/powerpc/include/asm/book3s/64/mmu-hash.h extern int __hash_page_4K(unsigned long ea, unsigned long access, · line 441
Quoted 7 lines
arch/powerpc/include/asm/book3s/64/mmu-hash.h +4
-0@@ -435,6 +435,11 @@ extern int __hash_page_4K(unsigned long ea, unsigned long access,extern int __hash_page_64K (unsigned long ea,unsigned long access,unsigned long vsid,pte_t *ptep,unsigned long trap,unsigned long flags,int ssize); +extern unsigned long set_hidx_slot (pte_t *ptep,real_pte_t rpte, +unsigned int subpg_index,unsigned long slot); +extern unsigned long get_hidx_slot (unsigned long vpn,unsigned long shift, +int ssize,real_pte_t rpte,unsigned int subpg_index);I wonder what purpose
set_hidx_slot()defined previously, served.Quoted 16 lines
arch/powerpc/include/asm/book3s/64/mmu-hash.h +1
-0+struct mm_struct ;unsigned int hash_page_do_lazy_icache (unsigned int pp,pte_t pte,int trap); extern int hash_page_mm(struct mm_struct *mm, unsigned long ea,arch/powerpc/mm/dump_linuxpagetables.c +1
-1@@ -213,7 +213,7 @@ struct flag_info { .val = H_PAGE_4K_PFN, .set = "4K_pfn", }, { -#endif +#else .mask = H_PAGE_F_GIX, .val = H_PAGE_F_GIX,Quoted 8 lines
arch/powerpc/mm/dump_linuxpagetables.c struct flag_info { · line 227
Quoted 6 lines
arch/powerpc/mm/dump_linuxpagetables.c +1
-0.set = "f_gix",@@ -224,6 +224,7 @@ struct flag_info { .val = H_PAGE_F_SECOND, .set = "f_second", }, { +#endif /* CONFIG_PPC_64K_PAGES */ Are we adding
H_PAGE_F_GIXas an element for 4K mapping ?I think there is mistake here.
In the next patch when these bits are divorsed from 64K ptes entirely, we will not need the above code for 64K ptes. But good catch. Will fix the error in this patch.Quoted 22 lines
Quoted 22 lines
arch/powerpc/mm/dump_linuxpagetables.c +0
-0#endif .mask = _PAGE_SPECIAL, .val = _PAGE_SPECIAL,arch/powerpc/mm/hash64_4k.c +2
-0@@ -20,6 +20,7 @@ int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid, pte_t *ptep, unsigned long trap, unsigned long flags, int ssize, int subpg_prot) { + real_pte_t rpte;unsigned long hpte_group;unsigned long rflags, pa;unsigned long old_pte, new_pte;@@ -54,6 +55,7 @@ int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid,* need to add in 0x1 if it's a read-only user page */ rflags =htab_convert_pte_flags (new_pte); + rpte =__real_pte (__pte (old_pte), ptep);Quoted 16 lines
arch/powerpc/mm/hash64_4k.c int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid, · line 70
Quoted 13 lines
arch/powerpc/mm/hash64_4k.c +2
-5if (cpu_has_feature (CPU_FTR_NOEXECUTE ) && !cpu_has_feature (CPU_FTR_COHERENT_ICACHE ))@@ -64,13 +66,10 @@ int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid,/* * There MIGHT be an HPTE for this pte */ - hash =hpt_hash (vpn, shift, ssize); -if (old_pte &H_PAGE_F_SECOND ) - hash = ~hash; - slot = (hash & htab_hash_mask) *HPTES_PER_GROUP ; - slot += (old_pte &H_PAGE_F_GIX ) >>H_PAGE_F_GIX_SHIFT ; +unsigned long gslot =get_hidx_gslot (vpn, shift, + ssize, rpte,0 );I am wondering why there is a 'g' before the slot in all these functions.
Quoted 6 lines
Right. even i was confused initially. :)
hash table slots are originized as one big table. 8 consecutive entires in that table form a bucket. the term slot is used to refer to the slot within the bucket. the term gslot is used to refer to an entry in the table. roughly speaking slot 2 in bucket 2, will be gslot 2*8+2=18.
Global slot as it can point any where on that two dimensional table ?
Quoted 7 lines
Quoted 6 lines
Its already too much of changes in a single patch. Being a single logical change it needs to be inside a single change but then we need much more description in the commit message for some one to understand what all changed and how.
Quoted 7 lines
Quoted 3 lines
I have further broken down this patch, one to introduce
get_hidx_gslot()one to introduceset_hidx_slot(). Hopefully that will reduce the size of the patch to graspable level. let me know,I did some experiments with the first two patches.
* First of all the first patch does not compile without this.
its a warning that a variable is defined but not used. I have fixed it in my new patch series; about to be launched soon.
Quoted 13 lines
arch/powerpc/mm/hash_utils_64.c +1
-1@@ -1612,7 +1612,7 @@ unsigned long get_hidx_gslot(unsigned long vpn, unsigned long shift,void flush_hash_page (unsigned long vpn,real_pte_t pte,int psize,int ssize,unsigned long flags) { -unsigned long hash, index, shift, hidx, gslot; +unsigned long index, shift, gslot;int local = flags &HPTE_LOCAL_UPDATE ;DBG_LOW ("flush_hash_page(vpn=%016lx)\n" , vpn);
Quoted 3 lines
* Though it boots the kernel, system is kind of unresponsive while attempting to compile a kernel. Though I did not dig further on this, seems like the first patch is not self sufficient yet.
I wouldn't have broken the the patch into two, because there is too much coupling between the two. But Aneesh wanted it that way. And it makes sense to break it from a review point of view.
Quoted 2 lines
* With both first and second patch, the kernel boots fine and compiles a kernel.
Yes. that meets my expectation.
Quoted 3 lines
We need to sort out issues in the first two patches before looking into the rest of the patch series.
I am not aware of any issues in the first two patches though. Do you see any?
RP
Signature · 1 line
Ram Pai
Re: [RFC v2 01/12] powerpc: Free up four 64K PTE bits in 4K backed hpte pages.
In reply to Ram PaiQuoted 117 lines · Ram Pai
Rearrange 64K PTE bits to free up bits 3, 4, 5 and 6 in the 4K backed hpte pages. These bits continue to be used for 64K backed hpte pages in this patch, but will be freed up in the next patch.
The patch does the following change to the 64K PTE format
H_PAGE_BUSYmoves from bit 3 to bit 9H_PAGE_F_SECONDwhich occupied bit 4 moves to the second part
of the pte.H_PAGE_F_GIXwhich occupied bit 5, 6 and 7 also moves to the
second part of the pte.the four bits((H_PAGE_F_SECOND|H_PAGE_F_GIX) that represent a slot is initialized to
0xFindicating an invalid slot. If a hpte
gets cached in a0xFslot(i.e 7th slot of secondary), it is
released immediately. In other words, even though0xFis a
valid slot we discard and consider it as an invalid slot;i.ehpte_soft_invalid(). This gives us an opportunity to not depend on a bit in the primary PTE in order to determine the validity of a slot.When we release a hpte in the
0xFslot we also release a
legitimate primary slot and unmap that entry. This is to
ensure that we do get a legimate non-0xF slot the next time we
retry for a slot.Though treating
0xFslot as invalid reduces the number of available slots and may have an effect on the performance, the probabilty of hitting a0xFis extermely low.Compared to the current scheme, the above described scheme reduces the number of false hash table updates significantly and has the added advantage of releasing four valuable PTE bits for other purpose.
This idea was jointly developed by Paul Mackerras, Aneesh, Michael Ellermen and myself.
4K PTE format remain unchanged currently.
Signed-off-by Ram Pai <[email protected]>--- arch/powerpc/include/asm/book3s/64/hash-4k.h | 20 +++++++ arch/powerpc/include/asm/book3s/64/hash-64k.h | 32 +++++++---- arch/powerpc/include/asm/book3s/64/hash.h | 15 +++-- arch/powerpc/include/asm/book3s/64/mmu-hash.h | 5 ++ arch/powerpc/mm/dump_linuxpagetables.c | 3 +- arch/powerpc/mm/hash64_4k.c | 14 ++--- arch/powerpc/mm/hash64_64k.c | 81 ++++++++++++--------------- arch/powerpc/mm/hash_utils_64.c | 30 +++++++--- 8 files changed, 122 insertions(+), 78 deletions(-)arch/powerpc/include/asm/book3s/64/hash-4k.h +20
-0@@ -16,6 +16,18 @@#define H_PUD_TABLE_SIZE (sizeof(pud_t) << H_PUD_INDEX_SIZE)#define H_PGD_TABLE_SIZE (sizeof(pgd_t) << H_PGD_INDEX_SIZE) + +/* +* Only supported by 4k linux page size +*/ +#define H_PAGE_F_SECOND _RPAGE_RSV2/* HPTE is in 2ndary HPTEG */ +#define H_PAGE_F_GIX (_RPAGE_RSV3 | _RPAGE_RSV4 | _RPAGE_RPN44) +#define H_PAGE_F_GIX_SHIFT 56 + +#define H_PAGE_BUSY _RPAGE_RSV1/* software: PTE & hash are busy */ +#define H_PAGE_HASHPTE _RPAGE_RPN43/* PTE has associated HPTE */ + +/* PTE flags to conserve for HPTE identification */ #define _PAGE_HPTEFLAGS (H_PAGE_BUSY | H_PAGE_HASHPTE | \ H_PAGE_F_SECOND | H_PAGE_F_GIX)@@ -48,6 +60,14 @@ static inline int hash__hugepd_ok(hugepd_t hpd) }#endif +static inline unsigned long set_hidx_slot (pte_t *ptep,real_pte_t rpte, +unsigned int subpg_index,unsigned long slot) +{ +return (slot <<H_PAGE_F_GIX_SHIFT ) & + (H_PAGE_F_SECOND |H_PAGE_F_GIX ); +} + +#ifdef CONFIG_TRANSPARENT_HUGEPAGE static inline char *get_hpte_slot_array (pmd_t *pmdp)arch/powerpc/include/asm/book3s/64/hash-64k.h +10
-2@@ -10,23 +10,25 @@* 64k aligned address free up few of the lower bits of RPN for us * We steal that here. For more deatils look at pte_pfn/pfn_pte() */ -#define H_PAGE_COMBO _RPAGE_RPN0/* this is a combo 4k page */ -#define H_PAGE_4K_PFN _RPAGE_RPN1/* PFN is for a single 4k page */ +#define H_PAGE_COMBO _RPAGE_RPN0/* this is a combo 4k page */ +#define H_PAGE_4K_PFN _RPAGE_RPN1/* PFN is for a single 4k page */ +#define H_PAGE_F_SECOND _RPAGE_RSV2/* HPTE is in 2ndary HPTEG */ +#define H_PAGE_F_GIX (_RPAGE_RSV3 | _RPAGE_RSV4 | _RPAGE_RPN44) +#define H_PAGE_F_GIX_SHIFT 56 + + +#define H_PAGE_BUSY _RPAGE_RPN42/* software: PTE & hash are busy */ +#define H_PAGE_HASHPTE _RPAGE_RPN43/* PTE has associated HPTE */ +/* * We need to differentiate between explicit huge page and THP huge * page, since THP huge page also need to track real subpage details */ #define H_PAGE_THP_HUGE H_PAGE_4K_PFN
arch/powerpc/include/asm/book3s/64/hash-64k.h line 30
Quoted 10 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h +1
-8-/* -* Used to track subpage group valid if H_PAGE_COMBO is set -* This overloads H_PAGE_F_GIX and H_PAGE_F_SECOND -*/ -#define H_PAGE_COMBO_VALID (H_PAGE_F_GIX | H_PAGE_F_SECOND) -/* PTE flags to conserve for HPTE identification */ -#define _PAGE_HPTEFLAGS (H_PAGE_BUSY | H_PAGE_F_SECOND | \ - H_PAGE_F_GIX | H_PAGE_HASHPTE | H_PAGE_COMBO) +#define _PAGE_HPTEFLAGS (H_PAGE_BUSY | H_PAGE_HASHPTE | H_PAGE_COMBO)
Why in this patch ? This is related to 64K pte
Quoted 129 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h +11
-0+/* * we support 16 fragments per PTE page of 64K size. */ @@ -74,6 +76,16 @@ static inline unsigned long __rpte_to_hidx(real_pte_t rpte, unsigned long index)return (pte_val (rpte.pte) >>H_PAGE_F_GIX_SHIFT ) &0xf ; } +static inline unsigned long set_hidx_slot (pte_t *ptep,real_pte_t rpte, +unsigned int subpg_index,unsigned long slot) +{ +unsigned long *hidxp = (unsigned long *)(ptep +PTRS_PER_PTE ); + + rpte.hidx &= ~(0xfUL << (subpg_index <<2 )); + *hidxp = rpte.hidx | (slot << (subpg_index <<2 )); +return 0x0UL ; +} +#define __rpte_to_pte (r) ((r).pte)extern bool __rpte_sub_valid (real_pte_t rpte,unsigned long index);/* arch/powerpc/include/asm/book3s/64/hash.h +10
-5@@ -8,11 +8,8 @@* */ #define H_PTE_NONE_MASK _PAGE_HPTEFLAGS -#define H_PAGE_F_GIX_SHIFT 56 -#define H_PAGE_BUSY _RPAGE_RSV1/* software: PTE & hash are busy */ -#define H_PAGE_F_SECOND _RPAGE_RSV2/* HPTE is in 2ndary HPTEG */ -#define H_PAGE_F_GIX (_RPAGE_RSV3 | _RPAGE_RSV4 | _RPAGE_RPN44) -#define H_PAGE_HASHPTE _RPAGE_RPN43/* PTE has associated HPTE */ + +#define INIT_HIDX (~0x0UL)#ifdef CONFIG_PPC_64K_PAGES #include <asm/book3s/64/hash-64k.h> @@ -160,6 +157,14 @@ static inline int hash__pte_none(pte_t pte)return (pte_val (pte) & ~H_PTE_NONE_MASK ) ==0 ; } +static inline bool hpte_soft_invalid (unsigned long slot) +{ +return ((slot &0xfUL ) ==0xfUL ); +} + +unsigned long get_hidx_gslot (unsigned long vpn,unsigned long shift, +int ssize,real_pte_t rpte,unsigned int subpg_index); +/* This low level function performs the actual PTE insertion * Setting the PTE depends on the MMU type and other factors. It's * an horrible mess that I'm not going to try to clean up now but arch/powerpc/include/asm/book3s/64/mmu-hash.h +5
-0@@ -435,6 +435,11 @@ extern int __hash_page_4K(unsigned long ea, unsigned long access,extern int __hash_page_64K (unsigned long ea,unsigned long access,unsigned long vsid,pte_t *ptep,unsigned long trap,unsigned long flags,int ssize); +extern unsigned long set_hidx_slot (pte_t *ptep,real_pte_t rpte, +unsigned int subpg_index,unsigned long slot); +extern unsigned long get_hidx_slot (unsigned long vpn,unsigned long shift, +int ssize,real_pte_t rpte,unsigned int subpg_index); +struct mm_struct ;unsigned int hash_page_do_lazy_icache (unsigned int pp,pte_t pte,int trap); extern int hash_page_mm(struct mm_struct *mm, unsigned long ea,arch/powerpc/mm/dump_linuxpagetables.c +2
-1@@ -213,7 +213,7 @@ struct flag_info { .val = H_PAGE_4K_PFN, .set = "4K_pfn", }, { -#endif +#else .mask = H_PAGE_F_GIX, .val = H_PAGE_F_GIX, .set = "f_gix",@@ -224,6 +224,7 @@ struct flag_info { .val = H_PAGE_F_SECOND, .set = "f_second", }, { +#endif /* CONFIG_PPC_64K_PAGES */ #endif .mask = _PAGE_SPECIAL, .val = _PAGE_SPECIAL,arch/powerpc/mm/hash64_4k.c +5
-6@@ -20,6 +20,7 @@ int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid, pte_t *ptep, unsigned long trap, unsigned long flags, int ssize, int subpg_prot) { + real_pte_t rpte;unsigned long hpte_group;unsigned long rflags, pa;unsigned long old_pte, new_pte;@@ -54,6 +55,7 @@ int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid,* need to add in 0x1 if it's a read-only user page */ rflags =htab_convert_pte_flags (new_pte); + rpte =__real_pte (__pte (old_pte), ptep);if (cpu_has_feature (CPU_FTR_NOEXECUTE ) && !cpu_has_feature (CPU_FTR_COHERENT_ICACHE ))@@ -64,13 +66,10 @@ int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid,/* * There MIGHT be an HPTE for this pte */ - hash =hpt_hash (vpn, shift, ssize); -if (old_pte &H_PAGE_F_SECOND ) - hash = ~hash; - slot = (hash & htab_hash_mask) *HPTES_PER_GROUP ; - slot += (old_pte &H_PAGE_F_GIX ) >>H_PAGE_F_GIX_SHIFT ; +unsigned long gslot =get_hidx_gslot (vpn, shift, + ssize, rpte,0 ); -if (mmu_hash_ops.hpte_updatepp (slot, rflags, vpn,MMU_PAGE_4K , +if (mmu_hash_ops.hpte_updatepp (gslot, rflags, vpn,MMU_PAGE_4K ,MMU_PAGE_4K , ssize, flags) ==-1 ) old_pte &= ~_PAGE_HPTEFLAGS; }
arch/powerpc/mm/hash64_4k.c int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid, · line 123
Quoted 9 lines
arch/powerpc/mm/hash64_4k.c +1
-2@@ -118,8 +117,7 @@ int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid,return -1 ; } new_pte = (new_pte & ~_PAGE_HPTEFLAGS) |H_PAGE_HASHPTE ; - new_pte |= (slot <<H_PAGE_F_GIX_SHIFT ) & - (H_PAGE_F_SECOND |H_PAGE_F_GIX ); + new_pte |=set_hidx_slot (ptep, rpte,0 , slot); } *ptep =__pte (new_pte & ~H_PAGE_BUSY );return 0 ;
None of the above changes are needed. We are not changing anything w.r.t 4k linux page table yet. So we can drop this.
Quoted 67 lines
arch/powerpc/mm/hash64_64k.c +5
-27@@ -15,34 +15,13 @@#include <linux/mm.h> #include <asm/machdep.h> #include <asm/mmu.h> +/* * index from 0 - 15 */ bool __rpte_sub_valid (real_pte_t rpte,unsigned long index) { -unsigned long g_idx; -unsigned long ptev =pte_val (rpte.pte); - - g_idx = (ptev &H_PAGE_COMBO_VALID ) >>H_PAGE_F_GIX_SHIFT ; - index = index >>2 ; -if (g_idx & (0x1 << index)) -return true; -else -return false; -} -/* -* index from 0 - 15 -*/ -static unsigned long mark_subptegroup_valid (unsigned long ptev,unsigned long index) -{ -unsigned long g_idx; - -if (!(ptev &H_PAGE_COMBO )) -return ptev; - index = index >>2 ; - g_idx =0x1 << index; - -return ptev | (g_idx <<H_PAGE_F_GIX_SHIFT ); +return !(hpte_soft_invalid (rpte.hidx >> (index <<2 ))); } int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid,@@ -50,10 +29,9 @@ int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid, int ssize, int subpg_prot) {real_pte_t rpte; -unsigned long *hidxp;unsigned long hpte_group;unsigned int subpg_index; -unsigned long rflags, pa, hidx; +unsigned long rflags, pa;unsigned long old_pte, new_pte, subpg_pte;unsigned long vpn, hash, slot;unsigned long shift = mmu_psize_defs[MMU_PAGE_4K ].shift;@@ -116,28 +94,23 @@ int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid,* On hash insert failure we use old pte value and we don't * want slot information there if we have a insert failure. */ - old_pte &= ~(H_PAGE_HASHPTE |H_PAGE_F_GIX |H_PAGE_F_SECOND ); - new_pte &= ~(H_PAGE_HASHPTE |H_PAGE_F_GIX |H_PAGE_F_SECOND ); + old_pte &= ~(H_PAGE_HASHPTE ); + new_pte &= ~(H_PAGE_HASHPTE ); goto htab_insert_hpte; }/* * Check for sub page valid and update */ if (__rpte_sub_valid (rpte, subpg_index)) {
arch/powerpc/mm/hash64_64k.c int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid, · line 107
Quoted 10 lines
arch/powerpc/mm/hash64_64k.c +2
-7-int ret; - hash =hpt_hash (vpn, shift, ssize); - hidx =__rpte_to_hidx (rpte, subpg_index); -if (hidx & _PTEIDX_SECONDARY) - hash = ~hash; - slot = (hash & htab_hash_mask) *HPTES_PER_GROUP ; - slot += hidx & _PTEIDX_GROUP_IX; +unsigned long gslot =get_hidx_gslot (vpn, shift, + ssize, rpte, subpg_index);
Converting that to helper is also not needed in this patch. Leave it as it is. It is much easier to review.
Quoted 28 lines
arch/powerpc/mm/hash64_64k.c +11
-2- ret = mmu_hash_ops.hpte_updatepp (slot, rflags, vpn, +int ret = mmu_hash_ops.hpte_updatepp (gslot, rflags, vpn,MMU_PAGE_4K ,MMU_PAGE_4K , ssize, flags);/* -*if we failed because typically the HPTE wasn't really here +* if we failed because typically the HPTE wasn't really here * we try an insertion. */ if (ret ==-1 )@@ -148,6 +121,15 @@ int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid, } htab_insert_hpte: + +/* +* initialize all hidx entries to a invalid value, +* the first time the PTE is about to allocate +* a 4K hpte +*/ +if (!(old_pte &H_PAGE_COMBO )) + rpte.hidx =INIT_HIDX ; +/* * handle H_PAGE_4K_PFN case */
arch/powerpc/mm/hash64_64k.c int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid, · line 163
Quoted 7 lines
arch/powerpc/mm/hash64_64k.c +2
-2@@ -177,10 +159,20 @@ int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid, rflags,HPTE_V_SECONDARY ,MMU_PAGE_4K ,MMU_PAGE_4K , ssize); -if (slot ==-1 ) { -if (mftb () &0x1 ) + +if (unlikely (hpte_soft_invalid (slot))) {
Should we name that hpte_slot_invalid() ? ie. s/soft/slot/ ?
arch/powerpc/mm/hash64_64k.c int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid, · line 167
Quoted 4 lines
arch/powerpc/mm/hash64_64k.c +4
-0+ slot = slot & _PTEIDX_GROUP_IX; + mmu_hash_ops.hpte_invalidate (hpte_group+slot, vpn, +MMU_PAGE_4K ,MMU_PAGE_4K , + ssize, flags);
What is the last arg flags here ? I guess we need to pass 0 there ? We can't do a local = 1 invalidate, because we don't know whether anybody did really access this address in between and has got the entry in TLB.
arch/powerpc/mm/hash64_64k.c int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid, · line 171
Quoted 4 lines
arch/powerpc/mm/hash64_64k.c +4
-0+ } + +if (unlikely (slot ==-1 ||hpte_soft_invalid (slot))) { +
Can you add a comment around explaining invalid slot always result in removing from primary ? Also do we want to store that invalid slot details in a variable ? instead of doing that conditional again and again ? This is hotpath.
Quoted 23 lines
arch/powerpc/mm/hash64_64k.c +5
-5+if (hpte_soft_invalid (slot) || (mftb () &0x1 )) hpte_group = ((hash & htab_hash_mask) *HPTES_PER_GROUP ) & ~0x7UL ; + mmu_hash_ops.hpte_remove (hpte_group);/* * FIXME!! Should be try the group from which we removed ? @@ -204,11 +196,9 @@ int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid,* Since we have H_PAGE_BUSY set on ptep, we can be sure * nobody is undating hidx. */ - hidxp = (unsigned long *)(ptep +PTRS_PER_PTE ); - rpte.hidx &= ~(0xfUL << (subpg_index <<2 )); - *hidxp = rpte.hidx | (slot << (subpg_index <<2 )); - new_pte =mark_subptegroup_valid (new_pte, subpg_index); - new_pte |=H_PAGE_HASHPTE ; + new_pte |=set_hidx_slot (ptep, rpte, subpg_index, slot); + new_pte |=H_PAGE_HASHPTE ; +/* * check __real_pte for details on matching smp_rmb() */
arch/powerpc/mm/hash64_64k.c int __hash_page_64K(unsigned long ea, unsigned long access, · line 318
Quoted 9 lines
arch/powerpc/mm/hash64_64k.c +3
-2@@ -322,9 +312,10 @@ int __hash_page_64K(unsigned long ea, unsigned long access, MMU_PAGE_64K, MMU_PAGE_64K, old_pte);return -1 ; } - new_pte = (new_pte & ~_PAGE_HPTEFLAGS) |H_PAGE_HASHPTE ; + new_pte |= (slot <<H_PAGE_F_GIX_SHIFT ) & - (H_PAGE_F_SECOND |H_PAGE_F_GIX ); + (H_PAGE_F_SECOND |H_PAGE_F_GIX ); + new_pte = (new_pte & ~_PAGE_HPTEFLAGS) |H_PAGE_HASHPTE ;
What is this change ? i guess we want this in second patch ?
Quoted 33 lines
arch/powerpc/mm/hash64_64k.c +0
-0} *ptep =__pte (new_pte & ~H_PAGE_BUSY );return 0 ;arch/powerpc/mm/hash_utils_64.c +6
-1@@ -975,8 +975,9 @@ void __init hash__early_init_devtree(void)void __init hash__early_init_mmu (void ) { +#ifndef CONFIG_PPC_64K_PAGES /* -* We have code in __hash_page_64K() and elsewhere, which assumes it can +* We have code in __hash_page_4K() and elsewhere, which assumes it can * do the following: * new_pte |= (slot << H_PAGE_F_GIX_SHIFT) & (H_PAGE_F_SECOND | H_PAGE_F_GIX); * @@ -987,6 +988,7 @@ void __init hash__early_init_mmu(void)* with a BUILD_BUG_ON(). */ BUILD_BUG_ON (H_PAGE_F_SECOND != (1ul << (H_PAGE_F_GIX_SHIFT +3 ))); +#endif /* CONFIG_PPC_64K_PAGES */ htab_init_page_sizes ();@@ -1589,29 +1591,39 @@ static inline void tm_flush_hash_page(int local) }#endif +unsigned long get_hidx_gslot (unsigned long vpn,unsigned long shift, +int ssize,real_pte_t rpte,unsigned int subpg_index) +{
arch/powerpc/mm/hash_utils_64.c static inline void tm_flush_hash_page(int local) · line 1606
Quoted 10 lines
arch/powerpc/mm/hash_utils_64.c +10
-0+unsigned long hash, slot, hidx; + + hash =hpt_hash (vpn, shift, ssize); + hidx =__rpte_to_hidx (rpte, subpg_index); +if (hidx & _PTEIDX_SECONDARY) + hash = ~hash; + slot = (hash & htab_hash_mask) *HPTES_PER_GROUP ; + slot += hidx & _PTEIDX_GROUP_IX; +return slot; +}
We don't need this helper for this patch series ?
Quoted 21 lines
arch/powerpc/mm/hash_utils_64.c +4
-7+ +/* WARNING: This is called from hash_low_64.S, if you change this prototype, * do not forget to update the assembly call site ! */ void flush_hash_page (unsigned long vpn,real_pte_t pte,int psize,int ssize,unsigned long flags) { -unsigned long hash, index, shift, hidx, slot; +unsigned long hash, index, shift, hidx, gslot;int local = flags &HPTE_LOCAL_UPDATE ;DBG_LOW ("flush_hash_page(vpn=%016lx)\n" , vpn); pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) { - hash =hpt_hash (vpn, shift, ssize); - hidx =__rpte_to_hidx (pte, index); -if (hidx & _PTEIDX_SECONDARY) - hash = ~hash; - slot = (hash & htab_hash_mask) *HPTES_PER_GROUP ; - slot += hidx & _PTEIDX_GROUP_IX; + gslot =get_hidx_gslot (vpn, shift, ssize, pte, index);
arch/powerpc/mm/hash_utils_64.c static inline void tm_flush_hash_page(int local) · line 1629
Quoted 10 lines
arch/powerpc/mm/hash_utils_64.c +1
-1DBG_LOW (" sub %ld: hash=%lx, hidx=%lx\n" , index, slot, hidx);/* * We use same base page size and actual psize, because we don't * use these functions for hugepage */ - mmu_hash_ops.hpte_invalidate (slot, vpn, psize, psize, + mmu_hash_ops.hpte_invalidate (gslot, vpn, psize, psize, ssize, local); }pte_iterate_hashed_end ();
And if we avoid adding that helper, changes like this can be avoided in the patch.
-aneesh
Re: [RFC v2 01/12] powerpc: Free up four 64K PTE bits in 4K backed hpte pages.
Quoted 119 lines · Aneesh Kumar K.V
Quoted 117 lines · Ram Pai
Rearrange 64K PTE bits to free up bits 3, 4, 5 and 6 in the 4K backed hpte pages. These bits continue to be used for 64K backed hpte pages in this patch, but will be freed up in the next patch.
The patch does the following change to the 64K PTE format
H_PAGE_BUSYmoves from bit 3 to bit 9H_PAGE_F_SECONDwhich occupied bit 4 moves to the second part
of the pte.H_PAGE_F_GIXwhich occupied bit 5, 6 and 7 also moves to the
second part of the pte.the four bits((H_PAGE_F_SECOND|H_PAGE_F_GIX) that represent a slot is initialized to
0xFindicating an invalid slot. If a hpte
gets cached in a0xFslot(i.e 7th slot of secondary), it is
released immediately. In other words, even though0xFis a
valid slot we discard and consider it as an invalid slot;i.ehpte_soft_invalid(). This gives us an opportunity to not depend on a bit in the primary PTE in order to determine the validity of a slot.When we release a hpte in the
0xFslot we also release a
legitimate primary slot and unmap that entry. This is to
ensure that we do get a legimate non-0xF slot the next time we
retry for a slot.Though treating
0xFslot as invalid reduces the number of available slots and may have an effect on the performance, the probabilty of hitting a0xFis extermely low.Compared to the current scheme, the above described scheme reduces the number of false hash table updates significantly and has the added advantage of releasing four valuable PTE bits for other purpose.
This idea was jointly developed by Paul Mackerras, Aneesh, Michael Ellermen and myself.
4K PTE format remain unchanged currently.
Signed-off-by Ram Pai <[email protected]>--- arch/powerpc/include/asm/book3s/64/hash-4k.h | 20 +++++++ arch/powerpc/include/asm/book3s/64/hash-64k.h | 32 +++++++---- arch/powerpc/include/asm/book3s/64/hash.h | 15 +++-- arch/powerpc/include/asm/book3s/64/mmu-hash.h | 5 ++ arch/powerpc/mm/dump_linuxpagetables.c | 3 +- arch/powerpc/mm/hash64_4k.c | 14 ++--- arch/powerpc/mm/hash64_64k.c | 81 ++++++++++++--------------- arch/powerpc/mm/hash_utils_64.c | 30 +++++++--- 8 files changed, 122 insertions(+), 78 deletions(-)arch/powerpc/include/asm/book3s/64/hash-4k.h +20
-0@@ -16,6 +16,18 @@#define H_PUD_TABLE_SIZE (sizeof(pud_t) << H_PUD_INDEX_SIZE)#define H_PGD_TABLE_SIZE (sizeof(pgd_t) << H_PGD_INDEX_SIZE) + +/* +* Only supported by 4k linux page size +*/ +#define H_PAGE_F_SECOND _RPAGE_RSV2/* HPTE is in 2ndary HPTEG */ +#define H_PAGE_F_GIX (_RPAGE_RSV3 | _RPAGE_RSV4 | _RPAGE_RPN44) +#define H_PAGE_F_GIX_SHIFT 56 + +#define H_PAGE_BUSY _RPAGE_RSV1/* software: PTE & hash are busy */ +#define H_PAGE_HASHPTE _RPAGE_RPN43/* PTE has associated HPTE */ + +/* PTE flags to conserve for HPTE identification */ #define _PAGE_HPTEFLAGS (H_PAGE_BUSY | H_PAGE_HASHPTE | \ H_PAGE_F_SECOND | H_PAGE_F_GIX)@@ -48,6 +60,14 @@ static inline int hash__hugepd_ok(hugepd_t hpd) }#endif +static inline unsigned long set_hidx_slot (pte_t *ptep,real_pte_t rpte, +unsigned int subpg_index,unsigned long slot) +{ +return (slot <<H_PAGE_F_GIX_SHIFT ) & + (H_PAGE_F_SECOND |H_PAGE_F_GIX ); +} + +#ifdef CONFIG_TRANSPARENT_HUGEPAGE static inline char *get_hpte_slot_array (pmd_t *pmdp)arch/powerpc/include/asm/book3s/64/hash-64k.h +10
-2@@ -10,23 +10,25 @@* 64k aligned address free up few of the lower bits of RPN for us * We steal that here. For more deatils look at pte_pfn/pfn_pte() */ -#define H_PAGE_COMBO _RPAGE_RPN0/* this is a combo 4k page */ -#define H_PAGE_4K_PFN _RPAGE_RPN1/* PFN is for a single 4k page */ +#define H_PAGE_COMBO _RPAGE_RPN0/* this is a combo 4k page */ +#define H_PAGE_4K_PFN _RPAGE_RPN1/* PFN is for a single 4k page */ +#define H_PAGE_F_SECOND _RPAGE_RSV2/* HPTE is in 2ndary HPTEG */ +#define H_PAGE_F_GIX (_RPAGE_RSV3 | _RPAGE_RSV4 | _RPAGE_RPN44) +#define H_PAGE_F_GIX_SHIFT 56 + + +#define H_PAGE_BUSY _RPAGE_RPN42/* software: PTE & hash are busy */ +#define H_PAGE_HASHPTE _RPAGE_RPN43/* PTE has associated HPTE */ +/* * We need to differentiate between explicit huge page and THP huge * page, since THP huge page also need to track real subpage details */ #define H_PAGE_THP_HUGE H_PAGE_4K_PFN
Quoted 13 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h line 30
Quoted 10 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h +1
-8-/* -* Used to track subpage group valid if H_PAGE_COMBO is set -* This overloads H_PAGE_F_GIX and H_PAGE_F_SECOND -*/ -#define H_PAGE_COMBO_VALID (H_PAGE_F_GIX | H_PAGE_F_SECOND) -/* PTE flags to conserve for HPTE identification */ -#define _PAGE_HPTEFLAGS (H_PAGE_BUSY | H_PAGE_F_SECOND | \ - H_PAGE_F_GIX | H_PAGE_HASHPTE | H_PAGE_COMBO) +#define _PAGE_HPTEFLAGS (H_PAGE_BUSY | H_PAGE_HASHPTE | H_PAGE_COMBO)Why in this patch ? This is related to 64K pte
Yes its in the wrong patch. Have fixed it in my new series.
Quoted 136 lines
Quoted 136 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h +11
-0+/* * we support 16 fragments per PTE page of 64K size. */ @@ -74,6 +76,16 @@ static inline unsigned long __rpte_to_hidx(real_pte_t rpte, unsigned long index)return (pte_val (rpte.pte) >>H_PAGE_F_GIX_SHIFT ) &0xf ; } +static inline unsigned long set_hidx_slot (pte_t *ptep,real_pte_t rpte, +unsigned int subpg_index,unsigned long slot) +{ +unsigned long *hidxp = (unsigned long *)(ptep +PTRS_PER_PTE ); + + rpte.hidx &= ~(0xfUL << (subpg_index <<2 )); + *hidxp = rpte.hidx | (slot << (subpg_index <<2 )); +return 0x0UL ; +} +#define __rpte_to_pte (r) ((r).pte)extern bool __rpte_sub_valid (real_pte_t rpte,unsigned long index);/* arch/powerpc/include/asm/book3s/64/hash.h +10
-5@@ -8,11 +8,8 @@* */ #define H_PTE_NONE_MASK _PAGE_HPTEFLAGS -#define H_PAGE_F_GIX_SHIFT 56 -#define H_PAGE_BUSY _RPAGE_RSV1/* software: PTE & hash are busy */ -#define H_PAGE_F_SECOND _RPAGE_RSV2/* HPTE is in 2ndary HPTEG */ -#define H_PAGE_F_GIX (_RPAGE_RSV3 | _RPAGE_RSV4 | _RPAGE_RPN44) -#define H_PAGE_HASHPTE _RPAGE_RPN43/* PTE has associated HPTE */ + +#define INIT_HIDX (~0x0UL)#ifdef CONFIG_PPC_64K_PAGES #include <asm/book3s/64/hash-64k.h> @@ -160,6 +157,14 @@ static inline int hash__pte_none(pte_t pte)return (pte_val (pte) & ~H_PTE_NONE_MASK ) ==0 ; } +static inline bool hpte_soft_invalid (unsigned long slot) +{ +return ((slot &0xfUL ) ==0xfUL ); +} + +unsigned long get_hidx_gslot (unsigned long vpn,unsigned long shift, +int ssize,real_pte_t rpte,unsigned int subpg_index); +/* This low level function performs the actual PTE insertion * Setting the PTE depends on the MMU type and other factors. It's * an horrible mess that I'm not going to try to clean up now but arch/powerpc/include/asm/book3s/64/mmu-hash.h +5
-0@@ -435,6 +435,11 @@ extern int __hash_page_4K(unsigned long ea, unsigned long access,extern int __hash_page_64K (unsigned long ea,unsigned long access,unsigned long vsid,pte_t *ptep,unsigned long trap,unsigned long flags,int ssize); +extern unsigned long set_hidx_slot (pte_t *ptep,real_pte_t rpte, +unsigned int subpg_index,unsigned long slot); +extern unsigned long get_hidx_slot (unsigned long vpn,unsigned long shift, +int ssize,real_pte_t rpte,unsigned int subpg_index); +struct mm_struct ;unsigned int hash_page_do_lazy_icache (unsigned int pp,pte_t pte,int trap); extern int hash_page_mm(struct mm_struct *mm, unsigned long ea,arch/powerpc/mm/dump_linuxpagetables.c +2
-1@@ -213,7 +213,7 @@ struct flag_info { .val = H_PAGE_4K_PFN, .set = "4K_pfn", }, { -#endif +#else .mask = H_PAGE_F_GIX, .val = H_PAGE_F_GIX, .set = "f_gix",@@ -224,6 +224,7 @@ struct flag_info { .val = H_PAGE_F_SECOND, .set = "f_second", }, { +#endif /* CONFIG_PPC_64K_PAGES */ #endif .mask = _PAGE_SPECIAL, .val = _PAGE_SPECIAL,arch/powerpc/mm/hash64_4k.c +6
-8@@ -20,6 +20,7 @@ int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid, pte_t *ptep, unsigned long trap, unsigned long flags, int ssize, int subpg_prot) { + real_pte_t rpte;unsigned long hpte_group;unsigned long rflags, pa;unsigned long old_pte, new_pte;@@ -54,6 +55,7 @@ int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid,* need to add in 0x1 if it's a read-only user page */ rflags =htab_convert_pte_flags (new_pte); + rpte =__real_pte (__pte (old_pte), ptep);if (cpu_has_feature (CPU_FTR_NOEXECUTE ) && !cpu_has_feature (CPU_FTR_COHERENT_ICACHE ))@@ -64,13 +66,10 @@ int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid,/* * There MIGHT be an HPTE for this pte */ - hash =hpt_hash (vpn, shift, ssize); -if (old_pte &H_PAGE_F_SECOND ) - hash = ~hash; - slot = (hash & htab_hash_mask) *HPTES_PER_GROUP ; - slot += (old_pte &H_PAGE_F_GIX ) >>H_PAGE_F_GIX_SHIFT ; +unsigned long gslot =get_hidx_gslot (vpn, shift, + ssize, rpte,0 ); -if (mmu_hash_ops.hpte_updatepp (slot, rflags, vpn,MMU_PAGE_4K , +if (mmu_hash_ops.hpte_updatepp (gslot, rflags, vpn,MMU_PAGE_4K ,MMU_PAGE_4K , ssize, flags) ==-1 ) old_pte &= ~_PAGE_HPTEFLAGS; }@@ -118,8 +117,7 @@ int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid,return -1 ; } new_pte = (new_pte & ~_PAGE_HPTEFLAGS) |H_PAGE_HASHPTE ; - new_pte |= (slot <<H_PAGE_F_GIX_SHIFT ) & - (H_PAGE_F_SECOND |H_PAGE_F_GIX ); + new_pte |=set_hidx_slot (ptep, rpte,0 , slot);
Quoted 9 lines
arch/powerpc/mm/hash64_4k.c int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid, · line 123
Quoted 3 lines
arch/powerpc/mm/hash64_4k.c +0
-0} *ptep =__pte (new_pte & ~H_PAGE_BUSY );return 0 ;None of the above changes are needed. We are not changing anything w.r.t 4k linux page table yet. So we can drop this.
I have put these changes in a separate patch. If needed it can be pulled in. But its not mandatory. Would be nice to have though, since it reduces a bunch of lines.
Quoted 70 lines
Quoted 70 lines
arch/powerpc/mm/hash64_64k.c +5
-28@@ -15,34 +15,13 @@#include <linux/mm.h> #include <asm/machdep.h> #include <asm/mmu.h> +/* * index from 0 - 15 */ bool __rpte_sub_valid (real_pte_t rpte,unsigned long index) { -unsigned long g_idx; -unsigned long ptev =pte_val (rpte.pte); - - g_idx = (ptev &H_PAGE_COMBO_VALID ) >>H_PAGE_F_GIX_SHIFT ; - index = index >>2 ; -if (g_idx & (0x1 << index)) -return true; -else -return false; -} -/* -* index from 0 - 15 -*/ -static unsigned long mark_subptegroup_valid (unsigned long ptev,unsigned long index) -{ -unsigned long g_idx; - -if (!(ptev &H_PAGE_COMBO )) -return ptev; - index = index >>2 ; - g_idx =0x1 << index; - -return ptev | (g_idx <<H_PAGE_F_GIX_SHIFT ); +return !(hpte_soft_invalid (rpte.hidx >> (index <<2 ))); } int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid,@@ -50,10 +29,9 @@ int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid, int ssize, int subpg_prot) {real_pte_t rpte; -unsigned long *hidxp;unsigned long hpte_group;unsigned int subpg_index; -unsigned long rflags, pa, hidx; +unsigned long rflags, pa;unsigned long old_pte, new_pte, subpg_pte;unsigned long vpn, hash, slot;unsigned long shift = mmu_psize_defs[MMU_PAGE_4K ].shift;@@ -116,28 +94,23 @@ int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid,* On hash insert failure we use old pte value and we don't * want slot information there if we have a insert failure. */ - old_pte &= ~(H_PAGE_HASHPTE |H_PAGE_F_GIX |H_PAGE_F_SECOND ); - new_pte &= ~(H_PAGE_HASHPTE |H_PAGE_F_GIX |H_PAGE_F_SECOND ); + old_pte &= ~(H_PAGE_HASHPTE ); + new_pte &= ~(H_PAGE_HASHPTE ); goto htab_insert_hpte; }/* * Check for sub page valid and update */ if (__rpte_sub_valid (rpte, subpg_index)) { -int ret;
Quoted 13 lines
arch/powerpc/mm/hash64_64k.c int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid, · line 107
Quoted 8 lines
arch/powerpc/mm/hash64_64k.c +2
-6- hash =hpt_hash (vpn, shift, ssize); - hidx =__rpte_to_hidx (rpte, subpg_index); -if (hidx & _PTEIDX_SECONDARY) - hash = ~hash; - slot = (hash & htab_hash_mask) *HPTES_PER_GROUP ; - slot += hidx & _PTEIDX_GROUP_IX; +unsigned long gslot =get_hidx_gslot (vpn, shift, + ssize, rpte, subpg_index);Converting that to helper is also not needed in this patch. Leave it as it is. It is much easier to review.
ok. But dont want to reduce a bunch of lines?
Quoted 30 lines
Quoted 30 lines
arch/powerpc/mm/hash64_64k.c +11
-2- ret = mmu_hash_ops.hpte_updatepp (slot, rflags, vpn, +int ret = mmu_hash_ops.hpte_updatepp (gslot, rflags, vpn,MMU_PAGE_4K ,MMU_PAGE_4K , ssize, flags);/* -*if we failed because typically the HPTE wasn't really here +* if we failed because typically the HPTE wasn't really here * we try an insertion. */ if (ret ==-1 )@@ -148,6 +121,15 @@ int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid, } htab_insert_hpte: + +/* +* initialize all hidx entries to a invalid value, +* the first time the PTE is about to allocate +* a 4K hpte +*/ +if (!(old_pte &H_PAGE_COMBO )) + rpte.hidx =INIT_HIDX ; +/* * handle H_PAGE_4K_PFN case */ @@ -177,10 +159,20 @@ int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid, rflags, HPTE_V_SECONDARY,
Quoted 8 lines
arch/powerpc/mm/hash64_64k.c int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid, · line 163
Quoted 6 lines
arch/powerpc/mm/hash64_64k.c +2
-2MMU_PAGE_4K ,MMU_PAGE_4K , ssize); -if (slot ==-1 ) { -if (mftb () &0x1 ) + +if (unlikely (hpte_soft_invalid (slot))) {Should we name that
hpte_slot_invalid()? ie. s/soft/slot/ ?
I intentionally used the word soft, since for the hardware it is a valid slot. The *soft*ware is considering it invalid. Hence the word *soft*.
Quoted 11 lines
arch/powerpc/mm/hash64_64k.c int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid, · line 167
Quoted 6 lines
arch/powerpc/mm/hash64_64k.c +4
-0+ slot = slot & _PTEIDX_GROUP_IX; + mmu_hash_ops.hpte_invalidate (hpte_group+slot, vpn, +MMU_PAGE_4K ,MMU_PAGE_4K , + ssize, flags);What is the last arg flags here ? I guess we need to pass 0 there ? We can't do a local = 1 invalidate, because we don't know whether anybody did really access this address in between and has got the entry in TLB.
ok. I think you are right. it should be 0.
Quoted 12 lines
arch/powerpc/mm/hash64_64k.c int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid, · line 171
Quoted 6 lines
arch/powerpc/mm/hash64_64k.c +4
-0+ } + +if (unlikely (slot ==-1 ||hpte_soft_invalid (slot))) { +Can you add a comment around explaining invalid slot always result in removing from primary ? Also do we want to store that invalid slot details in a variable ? instead of doing that conditional again and again ? This is hotpath.
will do.
Quoted 26 lines
Quoted 26 lines
arch/powerpc/mm/hash64_64k.c +5
-5+if (hpte_soft_invalid (slot) || (mftb () &0x1 )) hpte_group = ((hash & htab_hash_mask) *HPTES_PER_GROUP ) & ~0x7UL ; + mmu_hash_ops.hpte_remove (hpte_group);/* * FIXME!! Should be try the group from which we removed ? @@ -204,11 +196,9 @@ int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid,* Since we have H_PAGE_BUSY set on ptep, we can be sure * nobody is undating hidx. */ - hidxp = (unsigned long *)(ptep +PTRS_PER_PTE ); - rpte.hidx &= ~(0xfUL << (subpg_index <<2 )); - *hidxp = rpte.hidx | (slot << (subpg_index <<2 )); - new_pte =mark_subptegroup_valid (new_pte, subpg_index); - new_pte |=H_PAGE_HASHPTE ; + new_pte |=set_hidx_slot (ptep, rpte, subpg_index, slot); + new_pte |=H_PAGE_HASHPTE ; +/* * check __real_pte for details on matching smp_rmb() */ @@ -322,9 +312,10 @@ int __hash_page_64K(unsigned long ea, unsigned long access, MMU_PAGE_64K, MMU_PAGE_64K, old_pte);return -1 ; }
Quoted 8 lines
arch/powerpc/mm/hash64_64k.c int __hash_page_64K(unsigned long ea, unsigned long access, · line 318
Quoted 6 lines
arch/powerpc/mm/hash64_64k.c +3
-2- new_pte = (new_pte & ~_PAGE_HPTEFLAGS) |H_PAGE_HASHPTE ; + new_pte |= (slot <<H_PAGE_F_GIX_SHIFT ) & - (H_PAGE_F_SECOND |H_PAGE_F_GIX ); + (H_PAGE_F_SECOND |H_PAGE_F_GIX ); + new_pte = (new_pte & ~_PAGE_HPTEFLAGS) |H_PAGE_HASHPTE ;What is this change ? i guess we want this in second patch ?
yes. have moved it to the second patch.
Quoted 32 lines
Quoted 32 lines
arch/powerpc/mm/hash64_64k.c +0
-0} *ptep =__pte (new_pte & ~H_PAGE_BUSY );return 0 ;arch/powerpc/mm/hash_utils_64.c +3
-1@@ -975,8 +975,9 @@ void __init hash__early_init_devtree(void)void __init hash__early_init_mmu (void ) { +#ifndef CONFIG_PPC_64K_PAGES /* -* We have code in __hash_page_64K() and elsewhere, which assumes it can +* We have code in __hash_page_4K() and elsewhere, which assumes it can * do the following: * new_pte |= (slot << H_PAGE_F_GIX_SHIFT) & (H_PAGE_F_SECOND | H_PAGE_F_GIX); * @@ -987,6 +988,7 @@ void __init hash__early_init_mmu(void)* with a BUILD_BUG_ON(). */ BUILD_BUG_ON (H_PAGE_F_SECOND != (1ul << (H_PAGE_F_GIX_SHIFT +3 ))); +#endif /* CONFIG_PPC_64K_PAGES */ htab_init_page_sizes ();@@ -1589,29 +1591,39 @@ static inline void tm_flush_hash_page(int local) }#endif
Quoted 17 lines
arch/powerpc/mm/hash_utils_64.c static inline void tm_flush_hash_page(int local) · line 1606
Quoted 13 lines
arch/powerpc/mm/hash_utils_64.c +13
-0+unsigned long get_hidx_gslot (unsigned long vpn,unsigned long shift, +int ssize,real_pte_t rpte,unsigned int subpg_index) +{ +unsigned long hash, slot, hidx; + + hash =hpt_hash (vpn, shift, ssize); + hidx =__rpte_to_hidx (rpte, subpg_index); +if (hidx & _PTEIDX_SECONDARY) + hash = ~hash; + slot = (hash & htab_hash_mask) *HPTES_PER_GROUP ; + slot += hidx & _PTEIDX_GROUP_IX; +return slot; +}We don't need this helper for this patch series ?
the helpers will now be moved into independent patches. Can be applied if needed.
Quoted 36 lines
Quoted 21 lines
arch/powerpc/mm/hash_utils_64.c +4
-7+ +/* WARNING: This is called from hash_low_64.S, if you change this prototype, * do not forget to update the assembly call site ! */ void flush_hash_page (unsigned long vpn,real_pte_t pte,int psize,int ssize,unsigned long flags) { -unsigned long hash, index, shift, hidx, slot; +unsigned long hash, index, shift, hidx, gslot;int local = flags &HPTE_LOCAL_UPDATE ;DBG_LOW ("flush_hash_page(vpn=%016lx)\n" , vpn); pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) { - hash =hpt_hash (vpn, shift, ssize); - hidx =__rpte_to_hidx (pte, index); -if (hidx & _PTEIDX_SECONDARY) - hash = ~hash; - slot = (hash & htab_hash_mask) *HPTES_PER_GROUP ; - slot += hidx & _PTEIDX_GROUP_IX; + gslot =get_hidx_gslot (vpn, shift, ssize, pte, index);arch/powerpc/mm/hash_utils_64.c static inline void tm_flush_hash_page(int local) · line 1629
Quoted 10 lines
arch/powerpc/mm/hash_utils_64.c +1
-1DBG_LOW (" sub %ld: hash=%lx, hidx=%lx\n" , index, slot, hidx);/* * We use same base page size and actual psize, because we don't * use these functions for hugepage */ - mmu_hash_ops.hpte_invalidate (slot, vpn, psize, psize, + mmu_hash_ops.hpte_invalidate (gslot, vpn, psize, psize, ssize, local); }pte_iterate_hashed_end ();And if we avoid adding that helper, changes like this can be avoided in the patch.
-aneesh
Re: [RFC v2 01/12] powerpc: Free up four 64K PTE bits in 4K backed hpte pages.
In reply to Ram PaiQuoted 36 lines · Ram Pai
Rearrange 64K PTE bits to free up bits 3, 4, 5 and 6 in the 4K backed hpte pages. These bits continue to be used for 64K backed hpte pages in this patch, but will be freed up in the next patch.
The patch does the following change to the 64K PTE format
H_PAGE_BUSYmoves from bit 3 to bit 9H_PAGE_F_SECONDwhich occupied bit 4 moves to the second part
of the pte.H_PAGE_F_GIXwhich occupied bit 5, 6 and 7 also moves to the
second part of the pte.the four bits((H_PAGE_F_SECOND|H_PAGE_F_GIX) that represent a slot is initialized to
0xFindicating an invalid slot. If a hpte
gets cached in a0xFslot(i.e 7th slot of secondary), it is
released immediately. In other words, even though0xFis a
valid slot we discard and consider it as an invalid slot;i.ehpte_soft_invalid(). This gives us an opportunity to not depend on a bit in the primary PTE in order to determine the validity of a slot.When we release a hpte in the
0xFslot we also release a
legitimate primary slot and unmap that entry. This is to
ensure that we do get a legimate non-0xF slot the next time we
retry for a slot.Though treating
0xFslot as invalid reduces the number of available slots and may have an effect on the performance, the probabilty of hitting a0xFis extermely low.Compared to the current scheme, the above described scheme reduces the number of false hash table updates significantly and has the added advantage of releasing four valuable PTE bits for other purpose.
Quoted 4 lines
This idea was jointly developed by Paul Mackerras, Aneesh, Michael Ellermen and myself.
4K PTE format remain unchanged currently.
Scanned through the PTE format again for hash 64K and 4K. It seems to me that there might be 5 free bits already present on the PTE format. I might have seriously mistaken something here :) Please correct me if that is not the case. _RPAGE_RPN* I think is applicable only for hash page table format and will not be available for radix later.
+#define _PAGE_FREE_1 0x0000000000000040UL /* Not used */ +#define _RPAGE_SW0 0x2000000000000000UL /* Not used */ +#define _RPAGE_SW1 0x0000000000000800UL /* Not used */ +#define _RPAGE_RPN42 0x0040000000000000UL /* Not used */ +#define _RPAGE_RPN41 0x0020000000000000UL /* Not used */
Re: [RFC v2 01/12] powerpc: Free up four 64K PTE bits in 4K backed hpte pages.
Quoted 50 lines · Anshuman Khandual
Quoted 36 lines · Ram Pai
Rearrange 64K PTE bits to free up bits 3, 4, 5 and 6 in the 4K backed hpte pages. These bits continue to be used for 64K backed hpte pages in this patch, but will be freed up in the next patch.
The patch does the following change to the 64K PTE format
H_PAGE_BUSYmoves from bit 3 to bit 9H_PAGE_F_SECONDwhich occupied bit 4 moves to the second part
of the pte.H_PAGE_F_GIXwhich occupied bit 5, 6 and 7 also moves to the
second part of the pte.the four bits((H_PAGE_F_SECOND|H_PAGE_F_GIX) that represent a slot is initialized to
0xFindicating an invalid slot. If a hpte
gets cached in a0xFslot(i.e 7th slot of secondary), it is
released immediately. In other words, even though0xFis a
valid slot we discard and consider it as an invalid slot;i.ehpte_soft_invalid(). This gives us an opportunity to not depend on a bit in the primary PTE in order to determine the validity of a slot.When we release a hpte in the
0xFslot we also release a
legitimate primary slot and unmap that entry. This is to
ensure that we do get a legimate non-0xF slot the next time we
retry for a slot.Though treating
0xFslot as invalid reduces the number of available slots and may have an effect on the performance, the probabilty of hitting a0xFis extermely low.Compared to the current scheme, the above described scheme reduces the number of false hash table updates significantly and has the added advantage of releasing four valuable PTE bits for other purpose.
Quoted 4 lines
This idea was jointly developed by Paul Mackerras, Aneesh, Michael Ellermen and myself.
4K PTE format remain unchanged currently.
Scanned through the PTE format again for hash 64K and 4K. It seems to me that there might be 5 free bits already present on the PTE format. I might have seriously mistaken something here :) Please correct me if that is not the case. _RPAGE_RPN* I think is applicable only for hash page table format and will not be available for radix later.
Review
Quoted 5 lines
(quoted diff) +5
-0+#define _PAGE_FREE_1 0x0000000000000040UL /* Not used */ +#define _RPAGE_SW0 0x2000000000000000UL /* Not used */ +#define _RPAGE_SW1 0x0000000000000800UL /* Not used */ +#define _RPAGE_RPN42 0x0040000000000000UL /* Not used */ +#define _RPAGE_RPN41 0x0020000000000000UL /* Not used */
The bits are chosen to future proof for radix implementation. _RPAGE_SW* will eat into what is available for software in the future, and these key-bits will certainly be something that the radix hardware will read, in the future.
The _RPAGE_RPN* bits cannot be relied on for radix.
But finally the bits that we chose (H_PAGE_F_SECOND|H_PAGE_F_GIX) had the best potential for giving us the highest number of free bits with relatively less effort.
RP
RFC v2 2/12 powerpc: Free up four 64K PTE bits in 64K backed hpte pages.
In reply to Ram PaiRearrange 64K PTE bits to free up bits 3, 4, 5 and 6 in the 64K backed hpte pages. This along with the earlier patch will entirely free up the four bits from 64K PTE.
This patch does the following change to 64K PTE that is backed by 64K hpte.
H_PAGE_F_SECOND which occupied bit 4 moves to the second part
of the pte.H_PAGE_F_GIX which occupied bit 5, 6 and 7 also moves to the
second part of the pte.
since bit 7 is now freed up, we move H_PAGE_BUSY from bit 9 to bit 7. Trying to minimize gaps so that contiguous bits can be allocated if needed in the future.
The second part of the PTE will hold (H_PAGE_F_SECOND|H_PAGE_F_GIX) at bit 60,61,62,63.
--- arch/powerpc/include/asm/book3s/64/hash-64k.h | 26 ++++++++------------------ arch/powerpc/mm/hash64_64k.c | 16 +++++++--------- arch/powerpc/mm/hugetlbpage-hash64.c | 16 ++++++---------- 3 files changed, 21 insertions(+), 37 deletions(-)
arch/powerpc/include/asm/book3s/64/hash-64k.h +8 -18
@@ -12,12 +12,8 @@*/ #define H_PAGE_COMBO _RPAGE_RPN0/* this is a combo 4k page */ #define H_PAGE_4K_PFN _RPAGE_RPN1/* PFN is for a single 4k page */ -#define H_PAGE_F_SECOND _RPAGE_RSV2/* HPTE is in 2ndary HPTEG */ -#define H_PAGE_F_GIX (_RPAGE_RSV3 | _RPAGE_RSV4 | _RPAGE_RPN44) -#define H_PAGE_F_GIX_SHIFT 56 - -#define H_PAGE_BUSY _RPAGE_RPN42/* software: PTE & hash are busy */ +#define H_PAGE_BUSY _RPAGE_RPN44/* software: PTE & hash are busy */ #define H_PAGE_HASHPTE _RPAGE_RPN43/* PTE has associated HPTE */ /*
@@ -56,24 +52,18 @@ static inline real_pte_t __real_pte(pte_t pte, pte_t *ptep)unsigned long *hidxp; rpte.pte = pte; - rpte.hidx =0 ; -if (pte_val (pte) &H_PAGE_COMBO ) { -/* -* Make sure we order the hidx load against the H_PAGE_COMBO -* check. The store side ordering is done in __hash_page_4K -*/ -smp_rmb (); - hidxp = (unsigned long *)(ptep +PTRS_PER_PTE ); - rpte.hidx = *hidxp; - } +/* +* The store side ordering is done in __hash_page_4K +*/ +smp_rmb (); + hidxp = (unsigned long *)(ptep +PTRS_PER_PTE ); + rpte.hidx = *hidxp;return rpte; }static inline unsigned long __rpte_to_hidx (real_pte_t rpte,unsigned long index) { -if ((pte_val (rpte.pte) &H_PAGE_COMBO )) -return (rpte.hidx >> (index<<2 )) &0xf ; -return (pte_val (rpte.pte) >>H_PAGE_F_GIX_SHIFT ) &0xf ; +return ((rpte.hidx >> (index<<2 )) &0xfUL ); } static inline unsigned long set_hidx_slot(pte_t *ptep, real_pte_t rpte,
arch/powerpc/mm/hash64_64k.c +7 -9
@@ -211,6 +211,7 @@ int __hash_page_64K(unsigned long ea, unsigned long access, unsigned long vsid, pte_t *ptep, unsigned long trap, unsigned long flags, int ssize) { + real_pte_t rpte;unsigned long hpte_group;unsigned long rflags, pa;unsigned long old_pte, new_pte;
@@ -247,6 +248,7 @@ int __hash_page_64K(unsigned long ea, unsigned long access, }while (!pte_xchg (ptep,__pte (old_pte),__pte (new_pte))); rflags =htab_convert_pte_flags (new_pte); + rpte =__real_pte (__pte (old_pte), ptep);if (cpu_has_feature (CPU_FTR_NOEXECUTE ) && !cpu_has_feature (CPU_FTR_COHERENT_ICACHE ))
@@ -254,16 +256,13 @@ int __hash_page_64K(unsigned long ea, unsigned long access, vpn =hpt_vpn (ea, vsid, ssize);if (unlikely (old_pte &H_PAGE_HASHPTE )) { +unsigned long gslot; +/* * There MIGHT be an HPTE for this pte */ - hash =hpt_hash (vpn, shift, ssize); -if (old_pte &H_PAGE_F_SECOND ) - hash = ~hash; - slot = (hash & htab_hash_mask) *HPTES_PER_GROUP ; - slot += (old_pte &H_PAGE_F_GIX ) >>H_PAGE_F_GIX_SHIFT ; - -if (mmu_hash_ops.hpte_updatepp (slot, rflags, vpn,MMU_PAGE_64K , + gslot =get_hidx_gslot (vpn, shift, ssize, rpte,0 ); +if (mmu_hash_ops.hpte_updatepp (gslot, rflags, vpn,MMU_PAGE_64K ,MMU_PAGE_64K , ssize, flags) ==-1 ) old_pte &= ~_PAGE_HPTEFLAGS;
@@ -313,8 +312,7 @@ int __hash_page_64K(unsigned long ea, unsigned long access,return -1 ; } - new_pte |= (slot <<H_PAGE_F_GIX_SHIFT ) & - (H_PAGE_F_SECOND |H_PAGE_F_GIX ); +set_hidx_slot (ptep, rpte,0 , slot); new_pte = (new_pte & ~_PAGE_HPTEFLAGS) |H_PAGE_HASHPTE ; } *ptep =__pte (new_pte & ~H_PAGE_BUSY );
arch/powerpc/mm/hugetlbpage-hash64.c +6 -10
@@ -22,6 +22,7 @@ int __hash_page_huge(unsigned long ea, unsigned long access, unsigned long vsid, pte_t *ptep, unsigned long trap, unsigned long flags, int ssize, unsigned int shift, unsigned int mmu_psize) { + real_pte_t rpte;unsigned long vpn;unsigned long old_pte, new_pte;unsigned long rflags, pa, sz;
@@ -61,6 +62,7 @@ int __hash_page_huge(unsigned long ea, unsigned long access, unsigned long vsid, }while (!pte_xchg (ptep,__pte (old_pte),__pte (new_pte))); rflags =htab_convert_pte_flags (new_pte); + rpte =__real_pte (__pte (old_pte), ptep); sz = ((1UL ) << shift);if (!cpu_has_feature (CPU_FTR_COHERENT_ICACHE ))
@@ -71,15 +73,10 @@ int __hash_page_huge(unsigned long ea, unsigned long access, unsigned long vsid,/* Check if pte already has an hpte (case 2) */ if (unlikely (old_pte &H_PAGE_HASHPTE )) {/* There MIGHT be an HPTE for this pte */ -unsigned long hash, slot; +unsigned long gslot; - hash =hpt_hash (vpn, shift, ssize); -if (old_pte &H_PAGE_F_SECOND ) - hash = ~hash; - slot = (hash & htab_hash_mask) *HPTES_PER_GROUP ; - slot += (old_pte &H_PAGE_F_GIX ) >>H_PAGE_F_GIX_SHIFT ; - -if (mmu_hash_ops.hpte_updatepp (slot, rflags, vpn, mmu_psize, + gslot =get_hidx_gslot (vpn, shift, ssize, rpte,0 ); +if (mmu_hash_ops.hpte_updatepp (gslot, rflags, vpn, mmu_psize, mmu_psize, ssize, flags) ==-1 ) old_pte &= ~_PAGE_HPTEFLAGS; }
@@ -106,8 +103,7 @@ int __hash_page_huge(unsigned long ea, unsigned long access, unsigned long vsid,return -1 ; } - new_pte |= (slot <<H_PAGE_F_GIX_SHIFT ) & - (H_PAGE_F_SECOND |H_PAGE_F_GIX ); + new_pte |=set_hidx_slot (ptep, rpte,0 , slot); }/*
Signature · 1 line
1.8.3.1
Re: [RFC v2 02/12] powerpc: Free up four 64K PTE bits in 64K backed hpte pages.
Quoted 12 lines · Ram Pai
Rearrange 64K PTE bits to free up bits 3, 4, 5 and 6 in the 64K backed hpte pages. This along with the earlier patch will entirely free up the four bits from 64K PTE.
This patch does the following change to 64K PTE that is backed by 64K hpte.
H_PAGE_F_SECONDwhich occupied bit 4 moves to the second part
of the pte.H_PAGE_F_GIXwhich occupied bit 5, 6 and 7 also moves to the
second part of the pte.
Quoted 6 lines
since bit 7 is now freed up, we move
H_PAGE_BUSYfrom bit 9 to bit 7. Trying to minimize gaps so that contiguous bits can be allocated if needed in the future.The second part of the PTE will hold (H_PAGE_F_SECOND|H_PAGE_F_GIX) at bit 60,61,62,63.
I still dont understand how we freed up the 5th bit which is used in the 5th patch. Was that bit never used for any thing on 64K page size (64K and 4K mappings) ?
+#define _RPAGE_RSV5 0x00040UL
+#define H_PAGE_PKEY_BIT0 _RPAGE_RSV1
+#define H_PAGE_PKEY_BIT1 _RPAGE_RSV2
+#define H_PAGE_PKEY_BIT2 _RPAGE_RSV3
+#define H_PAGE_PKEY_BIT3 _RPAGE_RSV4
+#define H_PAGE_PKEY_BIT4 _RPAGE_RSV5
Re: [RFC v2 02/12] powerpc: Free up four 64K PTE bits in 64K backed hpte pages.
Quoted 17 lines · Anshuman Khandual
Quoted 16 lines · Ram Pai
Rearrange 64K PTE bits to free up bits 3, 4, 5 and 6 in the 64K backed hpte pages. This along with the earlier patch will entirely free up the four bits from 64K PTE.
This patch does the following change to 64K PTE that is backed by 64K hpte.
H_PAGE_F_SECONDwhich occupied bit 4 moves to the second part
of the pte.H_PAGE_F_GIXwhich occupied bit 5, 6 and 7 also moves to the
second part of the pte.since bit 7 is now freed up, we move
H_PAGE_BUSYfrom bit 9 to bit 7. Trying to minimize gaps so that contiguous bits can be allocated if needed in the future.
Quoted 6 lines
Quoted 2 lines
The second part of the PTE will hold (H_PAGE_F_SECOND|H_PAGE_F_GIX) at bit 60,61,62,63.
I still dont understand how we freed up the 5th bit which is used in the 5th patch. Was that bit never used for any thing on 64K page size (64K and 4K mappings) ?
yes. it was not used. So I gladly used it :-)
RP
Re: [RFC v2 02/12] powerpc: Free up four 64K PTE bits in 64K backed hpte pages.
In reply to Ram PaiQuoted 12 lines · Ram Pai
Rearrange 64K PTE bits to free up bits 3, 4, 5 and 6 in the 64K backed hpte pages. This along with the earlier patch will entirely free up the four bits from 64K PTE.
This patch does the following change to 64K PTE that is backed by 64K hpte.
H_PAGE_F_SECONDwhich occupied bit 4 moves to the second part
of the pte.H_PAGE_F_GIXwhich occupied bit 5, 6 and 7 also moves to the
second part of the pte.
Quoted 6 lines
since bit 7 is now freed up, we move
H_PAGE_BUSYfrom bit 9 to bit 7. Trying to minimize gaps so that contiguous bits can be allocated if needed in the future.The second part of the PTE will hold (H_PAGE_F_SECOND|H_PAGE_F_GIX) at bit 60,61,62,63.
This patch will be really simple, if you don't use the get_hidx_gslot() helper
Quoted 33 lines
Signed-off-by Ram Pai <[email protected]>--- arch/powerpc/include/asm/book3s/64/hash-64k.h | 26 ++++++++------------------ arch/powerpc/mm/hash64_64k.c | 16 +++++++--------- arch/powerpc/mm/hugetlbpage-hash64.c | 16 ++++++---------- 3 files changed, 21 insertions(+), 37 deletions(-)arch/powerpc/include/asm/book3s/64/hash-64k.h +1
-8@@ -12,12 +12,8 @@*/ #define H_PAGE_COMBO _RPAGE_RPN0/* this is a combo 4k page */ #define H_PAGE_4K_PFN _RPAGE_RPN1/* PFN is for a single 4k page */ -#define H_PAGE_F_SECOND _RPAGE_RSV2/* HPTE is in 2ndary HPTEG */ -#define H_PAGE_F_GIX (_RPAGE_RSV3 | _RPAGE_RSV4 | _RPAGE_RPN44) -#define H_PAGE_F_GIX_SHIFT 56 - -#define H_PAGE_BUSY _RPAGE_RPN42/* software: PTE & hash are busy */ +#define H_PAGE_BUSY _RPAGE_RPN44/* software: PTE & hash are busy */ #define H_PAGE_HASHPTE _RPAGE_RPN43/* PTE has associated HPTE */ /* @@ -56,24 +52,18 @@ static inline real_pte_t __real_pte(pte_t pte, pte_t *ptep)unsigned long *hidxp; rpte.pte = pte; - rpte.hidx =0 ; -if (pte_val (pte) &H_PAGE_COMBO ) { -/*
arch/powerpc/include/asm/book3s/64/hash-64k.h static inline real_pte_t __real_pte(pte_t pte, pte_t *ptep) · line 57
Quoted 10 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h +3
-7-* Make sure we order the hidx load against the H_PAGE_COMBO -* check. The store side ordering is done in __hash_page_4K -*/ -smp_rmb (); - hidxp = (unsigned long *)(ptep +PTRS_PER_PTE ); - rpte.hidx = *hidxp; - } +/* +* The store side ordering is done in __hash_page_4K +*/
This is not just __hash_page_4k related now and you need to explain the stoer side ordering more. Are we doing this correctly now ?
Quoted 117 lines
arch/powerpc/include/asm/book3s/64/hash-64k.h +4
-3+smp_rmb (); + hidxp = (unsigned long *)(ptep +PTRS_PER_PTE ); + rpte.hidx = *hidxp;return rpte; }static inline unsigned long __rpte_to_hidx (real_pte_t rpte,unsigned long index) { -if ((pte_val (rpte.pte) &H_PAGE_COMBO )) -return (rpte.hidx >> (index<<2 )) &0xf ; -return (pte_val (rpte.pte) >>H_PAGE_F_GIX_SHIFT ) &0xf ; +return ((rpte.hidx >> (index<<2 )) &0xfUL ); } static inline unsigned long set_hidx_slot(pte_t *ptep, real_pte_t rpte,arch/powerpc/mm/hash64_64k.c +7
-9@@ -211,6 +211,7 @@ int __hash_page_64K(unsigned long ea, unsigned long access, unsigned long vsid, pte_t *ptep, unsigned long trap, unsigned long flags, int ssize) { + real_pte_t rpte;unsigned long hpte_group;unsigned long rflags, pa;unsigned long old_pte, new_pte;@@ -247,6 +248,7 @@ int __hash_page_64K(unsigned long ea, unsigned long access, }while (!pte_xchg (ptep,__pte (old_pte),__pte (new_pte))); rflags =htab_convert_pte_flags (new_pte); + rpte =__real_pte (__pte (old_pte), ptep);if (cpu_has_feature (CPU_FTR_NOEXECUTE ) && !cpu_has_feature (CPU_FTR_COHERENT_ICACHE ))@@ -254,16 +256,13 @@ int __hash_page_64K(unsigned long ea, unsigned long access, vpn =hpt_vpn (ea, vsid, ssize);if (unlikely (old_pte &H_PAGE_HASHPTE )) { +unsigned long gslot; +/* * There MIGHT be an HPTE for this pte */ - hash =hpt_hash (vpn, shift, ssize); -if (old_pte &H_PAGE_F_SECOND ) - hash = ~hash; - slot = (hash & htab_hash_mask) *HPTES_PER_GROUP ; - slot += (old_pte &H_PAGE_F_GIX ) >>H_PAGE_F_GIX_SHIFT ; - -if (mmu_hash_ops.hpte_updatepp (slot, rflags, vpn,MMU_PAGE_64K , + gslot =get_hidx_gslot (vpn, shift, ssize, rpte,0 ); +if (mmu_hash_ops.hpte_updatepp (gslot, rflags, vpn,MMU_PAGE_64K ,MMU_PAGE_64K , ssize, flags) ==-1 ) old_pte &= ~_PAGE_HPTEFLAGS;@@ -313,8 +312,7 @@ int __hash_page_64K(unsigned long ea, unsigned long access,return -1 ; } - new_pte |= (slot <<H_PAGE_F_GIX_SHIFT ) & - (H_PAGE_F_SECOND |H_PAGE_F_GIX ); +set_hidx_slot (ptep, rpte,0 , slot); new_pte = (new_pte & ~_PAGE_HPTEFLAGS) |H_PAGE_HASHPTE ; } *ptep =__pte (new_pte & ~H_PAGE_BUSY );arch/powerpc/mm/hugetlbpage-hash64.c +6
-10@@ -22,6 +22,7 @@ int __hash_page_huge(unsigned long ea, unsigned long access, unsigned long vsid, pte_t *ptep, unsigned long trap, unsigned long flags, int ssize, unsigned int shift, unsigned int mmu_psize) { + real_pte_t rpte;unsigned long vpn;unsigned long old_pte, new_pte;unsigned long rflags, pa, sz;@@ -61,6 +62,7 @@ int __hash_page_huge(unsigned long ea, unsigned long access, unsigned long vsid, }while (!pte_xchg (ptep,__pte (old_pte),__pte (new_pte))); rflags =htab_convert_pte_flags (new_pte); + rpte =__real_pte (__pte (old_pte), ptep); sz = ((1UL ) << shift);if (!cpu_has_feature (CPU_FTR_COHERENT_ICACHE ))@@ -71,15 +73,10 @@ int __hash_page_huge(unsigned long ea, unsigned long access, unsigned long vsid,/* Check if pte already has an hpte (case 2) */ if (unlikely (old_pte &H_PAGE_HASHPTE )) {/* There MIGHT be an HPTE for this pte */ -unsigned long hash, slot; +unsigned long gslot; - hash =hpt_hash (vpn, shift, ssize); -if (old_pte &H_PAGE_F_SECOND ) - hash = ~hash; - slot = (hash & htab_hash_mask) *HPTES_PER_GROUP ; - slot += (old_pte &H_PAGE_F_GIX ) >>H_PAGE_F_GIX_SHIFT ; - -if (mmu_hash_ops.hpte_updatepp (slot, rflags, vpn, mmu_psize, + gslot =get_hidx_gslot (vpn, shift, ssize, rpte,0 ); +if (mmu_hash_ops.hpte_updatepp (gslot, rflags, vpn, mmu_psize, mmu_psize, ssize, flags) ==-1 ) old_pte &= ~_PAGE_HPTEFLAGS; }@@ -106,8 +103,7 @@ int __hash_page_huge(unsigned long ea, unsigned long access, unsigned long vsid,return -1 ; } - new_pte |= (slot <<H_PAGE_F_GIX_SHIFT ) & - (H_PAGE_F_SECOND |H_PAGE_F_GIX ); + new_pte |=set_hidx_slot (ptep, rpte,0 , slot); }/* Signature · 1 line
1.8.3.1
Re: [RFC v2 02/12] powerpc: Free up four 64K PTE bits in 64K backed hpte pages.
In reply to Ram PaiRam Pai <[email protected]> writes:
....
Quoted 40 lines
arch/powerpc/mm/hugetlbpage-hash64.c +5
-8@@ -22,6 +22,7 @@ int __hash_page_huge(unsigned long ea, unsigned long access, unsigned long vsid, pte_t *ptep, unsigned long trap, unsigned long flags, int ssize, unsigned int shift, unsigned int mmu_psize) { + real_pte_t rpte;unsigned long vpn;unsigned long old_pte, new_pte;unsigned long rflags, pa, sz;@@ -61,6 +62,7 @@ int __hash_page_huge(unsigned long ea, unsigned long access, unsigned long vsid, }while (!pte_xchg (ptep,__pte (old_pte),__pte (new_pte))); rflags =htab_convert_pte_flags (new_pte); + rpte =__real_pte (__pte (old_pte), ptep); sz = ((1UL ) << shift);if (!cpu_has_feature (CPU_FTR_COHERENT_ICACHE ))@@ -71,15 +73,10 @@ int __hash_page_huge(unsigned long ea, unsigned long access, unsigned long vsid,/* Check if pte already has an hpte (case 2) */ if (unlikely (old_pte &H_PAGE_HASHPTE )) {/* There MIGHT be an HPTE for this pte */ -unsigned long hash, slot; +unsigned long gslot; - hash =hpt_hash (vpn, shift, ssize); -if (old_pte &H_PAGE_F_SECOND ) - hash = ~hash; - slot = (hash & htab_hash_mask) *HPTES_PER_GROUP ; - slot += (old_pte &H_PAGE_F_GIX ) >>H_PAGE_F_GIX_SHIFT ; - -if (mmu_hash_ops.hpte_updatepp (slot, rflags, vpn, mmu_psize, + gslot =get_hidx_gslot (vpn, shift, ssize, rpte,0 ); +if (mmu_hash_ops.hpte_updatepp (gslot, rflags, vpn, mmu_psize, mmu_psize, ssize, flags) ==-1 ) old_pte &= ~_PAGE_HPTEFLAGS; }
arch/powerpc/mm/hugetlbpage-hash64.c int __hash_page_huge(unsigned long ea, unsigned long access, unsigned long vsid, · line 106
Quoted 6 lines
arch/powerpc/mm/hugetlbpage-hash64.c +1
-2@@ -106,8 +103,7 @@ int __hash_page_huge(unsigned long ea, unsigned long access, unsigned long vsid,return -1 ; } - new_pte |= (slot <<H_PAGE_F_GIX_SHIFT ) & - (H_PAGE_F_SECOND |H_PAGE_F_GIX ); + new_pte |=set_hidx_slot (ptep, rpte,0 , slot);
We don't really need rpte here. We are just need to track one entry here. May be it becomes simpler if use different helpers for 4k hpte and others ?
-aneesh
Re: [RFC v2 02/12] powerpc: Free up four 64K PTE bits in 64K backed hpte pages.
Quoted 47 lines · Aneesh Kumar K.V
Ram Pai <[email protected]> writes:
....
Quoted 43 lines
arch/powerpc/mm/hugetlbpage-hash64.c +5
-8@@ -22,6 +22,7 @@ int __hash_page_huge(unsigned long ea, unsigned long access, unsigned long vsid, pte_t *ptep, unsigned long trap, unsigned long flags, int ssize, unsigned int shift, unsigned int mmu_psize) { + real_pte_t rpte;unsigned long vpn;unsigned long old_pte, new_pte;unsigned long rflags, pa, sz;@@ -61,6 +62,7 @@ int __hash_page_huge(unsigned long ea, unsigned long access, unsigned long vsid, }while (!pte_xchg (ptep,__pte (old_pte),__pte (new_pte))); rflags =htab_convert_pte_flags (new_pte); + rpte =__real_pte (__pte (old_pte), ptep); sz = ((1UL ) << shift);if (!cpu_has_feature (CPU_FTR_COHERENT_ICACHE ))@@ -71,15 +73,10 @@ int __hash_page_huge(unsigned long ea, unsigned long access, unsigned long vsid,/* Check if pte already has an hpte (case 2) */ if (unlikely (old_pte &H_PAGE_HASHPTE )) {/* There MIGHT be an HPTE for this pte */ -unsigned long hash, slot; +unsigned long gslot; - hash =hpt_hash (vpn, shift, ssize); -if (old_pte &H_PAGE_F_SECOND ) - hash = ~hash; - slot = (hash & htab_hash_mask) *HPTES_PER_GROUP ; - slot += (old_pte &H_PAGE_F_GIX ) >>H_PAGE_F_GIX_SHIFT ; - -if (mmu_hash_ops.hpte_updatepp (slot, rflags, vpn, mmu_psize, + gslot =get_hidx_gslot (vpn, shift, ssize, rpte,0 ); +if (mmu_hash_ops.hpte_updatepp (gslot, rflags, vpn, mmu_psize, mmu_psize, ssize, flags) ==-1 ) old_pte &= ~_PAGE_HPTEFLAGS; }@@ -106,8 +103,7 @@ int __hash_page_huge(unsigned long ea, unsigned long access, unsigned long vsid,return -1 ; }
Quoted 7 lines
arch/powerpc/mm/hugetlbpage-hash64.c int __hash_page_huge(unsigned long ea, unsigned long access, unsigned long vsid, · line 106
Quoted 3 lines
arch/powerpc/mm/hugetlbpage-hash64.c +1
-2- new_pte |= (slot <<H_PAGE_F_GIX_SHIFT ) & - (H_PAGE_F_SECOND |H_PAGE_F_GIX ); + new_pte |=set_hidx_slot (ptep, rpte,0 , slot);We don't really need rpte here. We are just need to track one entry here. May be it becomes simpler if use different helpers for 4k hpte and others ?
actually we need rpte here. the hidx for these 64K-hpte backed PTEs are now stored in the second half of the pte.
I have abstracted the helpers, so that the caller need not know the location of the hidx. It comes in really handy.
RP
RFC v2 3/12 powerpc: Implement sys_pkey_alloc and sys_pkey_free system call.
In reply to Ram PaiSys_pkey_alloc() allocates and returns available pkey Sys_pkey_free() frees up the pkey.
Total 32 keys are supported on powerpc. However pkey 0,1 and 31 are reserved. So effectively we have 29 pkeys.
--- arch/powerpc/Kconfig | 15 ++++ arch/powerpc/include/asm/book3s/64/mmu.h | 10 +++ arch/powerpc/include/asm/book3s/64/pgtable.h | 62 ++++++++++++++ arch/powerpc/include/asm/pkeys.h | 124 +++++++++++++++++++++++++++ arch/powerpc/include/asm/systbl.h | 2 + arch/powerpc/include/asm/unistd.h | 4 +- arch/powerpc/include/uapi/asm/unistd.h | 2 + arch/powerpc/mm/Makefile | 1 + arch/powerpc/mm/mmu_context_book3s64.c | 5 ++ arch/powerpc/mm/pkeys.c | 88 +++++++++++++++++++ include/linux/mm.h | 31 ++++--- include/uapi/asm-generic/mman-common.h | 2 +- 12 files changed, 331 insertions(+), 15 deletions(-) create mode 100644 arch/powerpc/include/asm/pkeys.h create mode 100644 arch/powerpc/mm/pkeys.c
arch/powerpc/Kconfig +15 -0
@@ -871,6 +871,21 @@ config SECCOMP If unsure, say Y. Only embedded should say N here. +config PPC64_MEMORY_PROTECTION_KEYS + prompt "PowerPC Memory Protection Keys" + def_bool y + # Note: only available in 64-bit mode + depends on PPC64 && PPC_64K_PAGES + select ARCH_USES_HIGH_VMA_FLAGS + select ARCH_HAS_PKEYS + ---help--- + Memory Protection Keys provides a mechanism for enforcing + page-based protections, but without requiring modification of the + page tables when an application changes protection domains. + + For details, see Documentation/powerpc/protection-keys.txt + + If unsure, say y. endmenu config ISA_DMA_API
arch/powerpc/include/asm/book3s/64/mmu.h +10 -0
@@ -108,6 +108,16 @@ struct patb_entry {#ifdef CONFIG_SPAPR_TCE_IOMMU struct list_head iommu_group_mem_list;#endif + +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +/* +* Each bit represents one protection key. +* bit set -> key allocated +* bit unset -> key available for allocation +*/ +u32 pkey_allocation_map; +s16 execute_only_pkey;/* key holding execute-only protection */ +#endif } mm_context_t;/*
arch/powerpc/include/asm/book3s/64/pgtable.h +62 -0
68 lines not on this page · raw message
arch/powerpc/include/asm/pkeys.h new file +124 -0
124 lines not on this page · raw message
arch/powerpc/include/asm/systbl.h +2 -0
@@ -388,3 +388,5 @@ COMPAT_SYS_SPU(pwritev2) SYSCALL(kexec_file_load) SYSCALL(statx) +SYSCALL(pkey_alloc) +SYSCALL(pkey_free)
arch/powerpc/include/asm/unistd.h +1 -3
@@ -12,13 +12,11 @@#include <uapi/asm/unistd.h> -#define NR_syscalls 384 +#define NR_syscalls 386#define __NR__exit __NR_exit#define __IGNORE_pkey_mprotect -#define __IGNORE_pkey_alloc -#define __IGNORE_pkey_free#ifndef __ASSEMBLY__
arch/powerpc/include/uapi/asm/unistd.h +2 -0
@@ -394,5 +394,7 @@#define __NR_pwritev2 381#define __NR_kexec_file_load 382#define __NR_statx 383 +#define __NR_pkey_alloc 384 +#define __NR_pkey_free 385#endif /* _UAPI_ASM_POWERPC_UNISTD_H_ */
arch/powerpc/mm/Makefile +1 -0
@@ -45,3 +45,4 @@ obj-$(CONFIG_PPC_COPRO_BASE) += copro_fault.o obj-$(CONFIG_SPAPR_TCE_IOMMU) += mmu_context_iommu.o obj-$(CONFIG_PPC_PTDUMP) += dump_linuxpagetables.o obj-$(CONFIG_PPC_HTDUMP) += dump_hashpagetable.o +obj-$(CONFIG_PPC64_MEMORY_PROTECTION_KEYS) += pkeys.o
arch/powerpc/mm/mmu_context_book3s64.c +5 -0
@@ -16,6 +16,7 @@#include <linux/string.h> #include <linux/types.h> #include <linux/mm.h> +#include <linux/pkeys.h> #include <linux/spinlock.h> #include <linux/idr.h> #include <linux/export.h>
@@ -120,6 +121,10 @@ static int hash__init_new_context(struct mm_struct *mm)subpage_prot_init_new_context (mm); +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +pkey_mm_init (mm); +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +return index; }
arch/powerpc/mm/pkeys.c new file +88 -0
88 lines not on this page · raw message
include/linux/mm.h +20 -11
@@ -204,26 +204,35 @@ extern int overcommit_kbytes_handler(struct ctl_table *, int, void __user *,#define VM_MERGEABLE 0x80000000/* KSM may merge identical pages */ #ifdef CONFIG_ARCH_USES_HIGH_VMA_FLAGS -#define VM_HIGH_ARCH_BIT_0 32/* bit only usable on 64-bit architectures */ -#define VM_HIGH_ARCH_BIT_1 33/* bit only usable on 64-bit architectures */ -#define VM_HIGH_ARCH_BIT_2 34/* bit only usable on 64-bit architectures */ -#define VM_HIGH_ARCH_BIT_3 35/* bit only usable on 64-bit architectures */ +#define VM_HIGH_ARCH_BIT_0 32/* bit only usable on 64-bit arch */ +#define VM_HIGH_ARCH_BIT_1 33/* bit only usable on 64-bit arch */ +#define VM_HIGH_ARCH_BIT_2 34/* bit only usable on 64-bit arch */ +#define VM_HIGH_ARCH_BIT_3 35/* bit only usable on 64-bit arch */ +#define VM_HIGH_ARCH_BIT_4 36/* bit only usable on 64-bit arch */ #define VM_HIGH_ARCH_0 BIT(VM_HIGH_ARCH_BIT_0)#define VM_HIGH_ARCH_1 BIT(VM_HIGH_ARCH_BIT_1)#define VM_HIGH_ARCH_2 BIT(VM_HIGH_ARCH_BIT_2)#define VM_HIGH_ARCH_3 BIT(VM_HIGH_ARCH_BIT_3) +#define VM_HIGH_ARCH_4 BIT(VM_HIGH_ARCH_BIT_4)#endif /* CONFIG_ARCH_USES_HIGH_VMA_FLAGS */ #if defined(CONFIG_X86 )# define VM_PAT VM_ARCH_1/* PAT reserves whole VMA at once (x86) */ -#if defined (CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS ) -# define VM_PKEY_SHIFT VM_HIGH_ARCH_BIT_0 -# define VM_PKEY_BIT0 VM_HIGH_ARCH_0/* A protection key is a 4-bit value */ -# define VM_PKEY_BIT1 VM_HIGH_ARCH_1 -# define VM_PKEY_BIT2 VM_HIGH_ARCH_2 -# define VM_PKEY_BIT3 VM_HIGH_ARCH_3 -#endif +#if defined(CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS ) \ + || defined(CONFIG_PPC64_MEMORY_PROTECTION_KEYS ) +#define VM_PKEY_SHIFT VM_HIGH_ARCH_BIT_0 +#define VM_PKEY_BIT0 VM_HIGH_ARCH_0/* A protection key is a 5-bit value */ +#define VM_PKEY_BIT1 VM_HIGH_ARCH_1 +#define VM_PKEY_BIT2 VM_HIGH_ARCH_2 +#define VM_PKEY_BIT3 VM_HIGH_ARCH_3 +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ #elif defined(CONFIG_PPC ) +#define VM_PKEY_BIT0 VM_HIGH_ARCH_0/* A protection key is a 5-bit value */ +#define VM_PKEY_BIT1 VM_HIGH_ARCH_1 +#define VM_PKEY_BIT2 VM_HIGH_ARCH_2 +#define VM_PKEY_BIT3 VM_HIGH_ARCH_3 +#define VM_PKEY_BIT4 VM_HIGH_ARCH_4/* intel does not use this bit */ +/* but reserved for future expansion */ # define VM_SAO VM_ARCH_1/* Strong Access Ordering (powerpc) */ #elif defined(CONFIG_PARISC )# define VM_GROWSUP VM_ARCH_1
include/uapi/asm-generic/mman-common.h +1 -1
@@ -76,5 +76,5 @@#define PKEY_DISABLE_WRITE 0x2#define PKEY_ACCESS_MASK (PKEY_DISABLE_ACCESS |\ PKEY_DISABLE_WRITE) - +#define PKEY_DISABLE_EXECUTE 0x4#endif /* __ASM_GENERIC_MMAN_COMMON_H */
Signature · 1 line
1.8.3.1
Michael Ellerman
reviewerRe: [RFC v2 03/12] powerpc: Implement sys_pkey_alloc and sys_pkey_free system call.
Hi Ram,
Quoted 10 lines · Ram Pai
Sys_pkey_alloc()allocates and returns available pkeySys_pkey_free()frees up the pkey.Total 32 keys are supported on powerpc. However pkey 0,1 and 31 are reserved. So effectively we have 29 pkeys.
Signed-off-by Ram Pai <[email protected]>--- include/linux/mm.h | 31 ++++--- include/uapi/asm-generic/mman-common.h | 2 +-
Those changes need to be split out and acked by mm folks.
Quoted 6 lines
include/linux/mm.h +0
-0@@ -204,26 +204,35 @@ extern int overcommit_kbytes_handler(struct ctl_table *, int, void __user *,#define VM_MERGEABLE 0x80000000/* KSM may merge identical pages */
include/linux/mm.h extern int overcommit_kbytes_handler(struct ctl_table *, int, void __user *, · line 210
Quoted 10 lines
include/linux/mm.h +4
-4#ifdef CONFIG_ARCH_USES_HIGH_VMA_FLAGS -#define VM_HIGH_ARCH_BIT_0 32/* bit only usable on 64-bit architectures */ -#define VM_HIGH_ARCH_BIT_1 33/* bit only usable on 64-bit architectures */ -#define VM_HIGH_ARCH_BIT_2 34/* bit only usable on 64-bit architectures */ -#define VM_HIGH_ARCH_BIT_3 35/* bit only usable on 64-bit architectures */ +#define VM_HIGH_ARCH_BIT_0 32/* bit only usable on 64-bit arch */ +#define VM_HIGH_ARCH_BIT_1 33/* bit only usable on 64-bit arch */ +#define VM_HIGH_ARCH_BIT_2 34/* bit only usable on 64-bit arch */ +#define VM_HIGH_ARCH_BIT_3 35/* bit only usable on 64-bit arch */
Please don't change the comments, it makes the diff harder to read.
You're actually just adding this AFAICS:
include/linux/mm.h extern int overcommit_kbytes_handler(struct ctl_table *, int, void __user *, · line 219
Quoted 10 lines
include/linux/mm.h +2
-0+#define VM_HIGH_ARCH_BIT_4 36/* bit only usable on 64-bit arch */ #define VM_HIGH_ARCH_0 BIT(VM_HIGH_ARCH_BIT_0)#define VM_HIGH_ARCH_1 BIT(VM_HIGH_ARCH_BIT_1)#define VM_HIGH_ARCH_2 BIT(VM_HIGH_ARCH_BIT_2)#define VM_HIGH_ARCH_3 BIT(VM_HIGH_ARCH_BIT_3) +#define VM_HIGH_ARCH_4 BIT(VM_HIGH_ARCH_BIT_4)#endif /* CONFIG_ARCH_USES_HIGH_VMA_FLAGS */ #if defined(CONFIG_X86 )
^
include/linux/mm.h extern int overcommit_kbytes_handler(struct ctl_table *, int, void __user *, · line 224
Quoted 12 lines
include/linux/mm.h +4
-7# define VM_PAT VM_ARCH_1/* PAT reserves whole VMA at once (x86) */ -#if defined (CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS ) -# define VM_PKEY_SHIFT VM_HIGH_ARCH_BIT_0 -# define VM_PKEY_BIT0 VM_HIGH_ARCH_0/* A protection key is a 4-bit value */ -# define VM_PKEY_BIT1 VM_HIGH_ARCH_1 -# define VM_PKEY_BIT2 VM_HIGH_ARCH_2 -# define VM_PKEY_BIT3 VM_HIGH_ARCH_3 -#endif +#if defined(CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS ) \ + || defined(CONFIG_PPC64_MEMORY_PROTECTION_KEYS ) +#define VM_PKEY_SHIFT VM_HIGH_ARCH_BIT_0 +#define VM_PKEY_BIT0 VM_HIGH_ARCH_0/* A protection key is a 5-bit value */
^ 4?
include/linux/mm.h extern int overcommit_kbytes_handler(struct ctl_table *, int, void __user *, · line 228
Quoted 4 lines
include/linux/mm.h +4
-0+#define VM_PKEY_BIT1 VM_HIGH_ARCH_1 +#define VM_PKEY_BIT2 VM_HIGH_ARCH_2 +#define VM_PKEY_BIT3 VM_HIGH_ARCH_3 +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */
That appears to be inside an #if defined(CONFIG_X86) ?
include/linux/mm.h extern int overcommit_kbytes_handler(struct ctl_table *, int, void __user *, · line 229
Quoted 1 line
include/linux/mm.h +0
-0#elif defined(CONFIG_PPC )
^
Should be CONFIG_PPC64_MEMORY_PROTECTION_KEYS no?
include/linux/mm.h extern int overcommit_kbytes_handler(struct ctl_table *, int, void __user *, · line 235
Quoted 6 lines
include/linux/mm.h +6
-0+#define VM_PKEY_BIT0 VM_HIGH_ARCH_0/* A protection key is a 5-bit value */ +#define VM_PKEY_BIT1 VM_HIGH_ARCH_1 +#define VM_PKEY_BIT2 VM_HIGH_ARCH_2 +#define VM_PKEY_BIT3 VM_HIGH_ARCH_3 +#define VM_PKEY_BIT4 VM_HIGH_ARCH_4/* intel does not use this bit */ +/* but reserved for future expansion */
But this hunk is for PPC ?
Is it OK for the other arches & generic code to add another VM_PKEY_BIT4 ?
Do you need to update show_smap_vma_flags() ?
Quoted 9 lines
include/linux/mm.h +0
-0# define VM_SAO VM_ARCH_1/* Strong Access Ordering (powerpc) */ #elif defined(CONFIG_PARISC )# define VM_GROWSUP VM_ARCH_1include/uapi/asm-generic/mman-common.h +0
-0
include/uapi/asm-generic/mman-common.h line 79
Quoted 5 lines
include/uapi/asm-generic/mman-common.h +1
-1@@ -76,5 +76,5 @@#define PKEY_DISABLE_WRITE 0x2#define PKEY_ACCESS_MASK (PKEY_DISABLE_ACCESS |\ PKEY_DISABLE_WRITE) - +#define PKEY_DISABLE_EXECUTE 0x4
How you can set that if it's not in PKEY_ACCESS_MASK?
See:
SYSCALL_DEFINE2(pkey_alloc, unsigned long, flags, unsigned long, init_val)
{
int pkey;
int ret;
/* No flags supported yet. */
if (flags)
return -EINVAL;
/* check for unsupported init values */
if (init_val & ~PKEY_ACCESS_MASK)
return -EINVAL;cheers
Re: [RFC v2 03/12] powerpc: Implement sys_pkey_alloc and sys_pkey_free system call.
Quoted 23 lines · Michael Ellerman
Hi Ram,
Quoted 10 lines · Ram Pai
Sys_pkey_alloc()allocates and returns available pkeySys_pkey_free()frees up the pkey.Total 32 keys are supported on powerpc. However pkey 0,1 and 31 are reserved. So effectively we have 29 pkeys.
Signed-off-by Ram Pai <[email protected]>--- include/linux/mm.h | 31 ++++--- include/uapi/asm-generic/mman-common.h | 2 +-Those changes need to be split out and acked by mm folks.
Quoted 7 lines
include/linux/mm.h +0
-0@@ -204,26 +204,35 @@ extern int overcommit_kbytes_handler(struct ctl_table *, int, void __user *,#define VM_MERGEABLE 0x80000000/* KSM may merge identical pages */
Quoted 11 lines
include/linux/mm.h extern int overcommit_kbytes_handler(struct ctl_table *, int, void __user *, · line 210
Quoted 9 lines
include/linux/mm.h +4
-4#ifdef CONFIG_ARCH_USES_HIGH_VMA_FLAGS -#define VM_HIGH_ARCH_BIT_0 32/* bit only usable on 64-bit architectures */ -#define VM_HIGH_ARCH_BIT_1 33/* bit only usable on 64-bit architectures */ -#define VM_HIGH_ARCH_BIT_2 34/* bit only usable on 64-bit architectures */ -#define VM_HIGH_ARCH_BIT_3 35/* bit only usable on 64-bit architectures */ +#define VM_HIGH_ARCH_BIT_0 32/* bit only usable on 64-bit arch */ +#define VM_HIGH_ARCH_BIT_1 33/* bit only usable on 64-bit arch */ +#define VM_HIGH_ARCH_BIT_2 34/* bit only usable on 64-bit arch */ +#define VM_HIGH_ARCH_BIT_3 35/* bit only usable on 64-bit arch */ Please don't change the comments, it makes the diff harder to read.
The lines were surpassing 80 columns. tried to compress the comments without loosing meaning. will restore.
Quoted 34 lines
You're actually just adding this AFAICS:
include/linux/mm.h extern int overcommit_kbytes_handler(struct ctl_table *, int, void __user *, · line 219
Quoted 10 lines
include/linux/mm.h +2
-0+#define VM_HIGH_ARCH_BIT_4 36/* bit only usable on 64-bit arch */ #define VM_HIGH_ARCH_0 BIT(VM_HIGH_ARCH_BIT_0)#define VM_HIGH_ARCH_1 BIT(VM_HIGH_ARCH_BIT_1)#define VM_HIGH_ARCH_2 BIT(VM_HIGH_ARCH_BIT_2)#define VM_HIGH_ARCH_3 BIT(VM_HIGH_ARCH_BIT_3) +#define VM_HIGH_ARCH_4 BIT(VM_HIGH_ARCH_BIT_4)#endif /* CONFIG_ARCH_USES_HIGH_VMA_FLAGS */ #if defined(CONFIG_X86 )^include/linux/mm.h extern int overcommit_kbytes_handler(struct ctl_table *, int, void __user *, · line 224
Quoted 12 lines
include/linux/mm.h +4
-7# define VM_PAT VM_ARCH_1/* PAT reserves whole VMA at once (x86) */ -#if defined (CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS ) -# define VM_PKEY_SHIFT VM_HIGH_ARCH_BIT_0 -# define VM_PKEY_BIT0 VM_HIGH_ARCH_0/* A protection key is a 4-bit value */ -# define VM_PKEY_BIT1 VM_HIGH_ARCH_1 -# define VM_PKEY_BIT2 VM_HIGH_ARCH_2 -# define VM_PKEY_BIT3 VM_HIGH_ARCH_3 -#endif +#if defined(CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS ) \ + || defined(CONFIG_PPC64_MEMORY_PROTECTION_KEYS ) +#define VM_PKEY_SHIFT VM_HIGH_ARCH_BIT_0 +#define VM_PKEY_BIT0 VM_HIGH_ARCH_0/* A protection key is a 5-bit value */ ^ 4?include/linux/mm.h extern int overcommit_kbytes_handler(struct ctl_table *, int, void __user *, · line 228
Quoted 4 lines
include/linux/mm.h +4
-0+#define VM_PKEY_BIT1 VM_HIGH_ARCH_1 +#define VM_PKEY_BIT2 VM_HIGH_ARCH_2 +#define VM_PKEY_BIT3 VM_HIGH_ARCH_3 +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ That appears to be inside an #if
defined(CONFIG_X86)?
Quoted 3 lines
include/linux/mm.h extern int overcommit_kbytes_handler(struct ctl_table *, int, void __user *, · line 229
Quoted 1 line
include/linux/mm.h +0
-0#elif defined(CONFIG_PPC )^Should be
CONFIG_PPC64_MEMORY_PROTECTION_KEYSno?
Its a little garbled. Will fix it.
Quoted 11 lines
include/linux/mm.h extern int overcommit_kbytes_handler(struct ctl_table *, int, void __user *, · line 235
Quoted 7 lines
include/linux/mm.h +6
-0+#define VM_PKEY_BIT0 VM_HIGH_ARCH_0/* A protection key is a 5-bit value */ +#define VM_PKEY_BIT1 VM_HIGH_ARCH_1 +#define VM_PKEY_BIT2 VM_HIGH_ARCH_2 +#define VM_PKEY_BIT3 VM_HIGH_ARCH_3 +#define VM_PKEY_BIT4 VM_HIGH_ARCH_4/* intel does not use this bit */ +/* but reserved for future expansion */ But this hunk is for PPC ?
Is it OK for the other arches & generic code to add another
VM_PKEY_BIT4?
No. it has to be PPC specific.
Quoted 7 lines
Do you need to update
show_smap_vma_flags()?Quoted 3 lines
include/linux/mm.h +0
-0# define VM_SAO VM_ARCH_1/* Strong Access Ordering (powerpc) */ #elif defined(CONFIG_PARISC )# define VM_GROWSUP VM_ARCH_1
Quoted 12 lines
include/uapi/asm-generic/mman-common.h line 79
Quoted 10 lines
include/uapi/asm-generic/mman-common.h +1
-1@@ -76,5 +76,5 @@#define PKEY_DISABLE_WRITE 0x2#define PKEY_ACCESS_MASK (PKEY_DISABLE_ACCESS |\ PKEY_DISABLE_WRITE) - +#define PKEY_DISABLE_EXECUTE 0x4How you can set that if it's not in
PKEY_ACCESS_MASK?
I was wondering how to handle this. x86 does not support this flag. However powerpc has the ability to enable/disable execute permission on a key. It cannot be done from userspace, but can be done through the sys_mprotect_pkey() sys call. Initially I was thinking of not enabling it in powerpc aswell, but than i think we should be not block the hardware feature from being used. I will make PKEY_DISABLE_EXECUTE as part of the PKEY_ACCESS_MASK, and have powerpc handle it. Also will x86 patch that return error if the flag is provided.
makes sense?
Thanks for your comments,
RP
Quoted 17 lines
See:
SYSCALL_DEFINE2(pkey_alloc, unsigned long, flags, unsigned long, init_val){ int pkey; int ret; /* No flags supported yet. */ if (flags) return -EINVAL; /* check for unsupported init values */ if (init_val & ~PKEY_ACCESS_MASK) return -EINVAL;cheers
Signature · 1 line
Ram Pai
RFC v2 4/12 powerpc: store and restore the pkey state across context switches.
In reply to Ram Pai--- arch/powerpc/include/asm/processor.h | 5 +++++ arch/powerpc/kernel/process.c | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+)
arch/powerpc/include/asm/processor.h +5 -0
@@ -310,6 +310,11 @@ struct thread_struct {struct thread_vr_state ckvr_state;/* Checkpointed VR state */ unsigned long ckvrsave;/* Checkpointed VRSAVE */ #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */ +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +unsigned long amr; +unsigned long iamr; +unsigned long uamor; +#endif #ifdef CONFIG_KVM_BOOK3S_32_HANDLER void * kvm_shadow_vcpu;/* KVM internal data */ #endif /* CONFIG_KVM_BOOK3S_32_HANDLER */
arch/powerpc/kernel/process.c +18 -0
@@ -1096,6 +1096,11 @@ static inline void save_sprs(struct thread_struct *t) t->tar =mfspr (SPRN_TAR ); }#endif +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS + t->amr =mfspr (SPRN_AMR ); + t->iamr =mfspr (SPRN_IAMR ); + t->uamor =mfspr (SPRN_UAMOR ); +#endif } static inline void restore_sprs(struct thread_struct *old_thread,
@@ -1131,6 +1136,14 @@ static inline void restore_sprs(struct thread_struct *old_thread,mtspr (SPRN_TAR , new_thread->tar); }#endif +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +if (old_thread->amr != new_thread->amr) +mtspr (SPRN_AMR , new_thread->amr); +if (old_thread->iamr != new_thread->iamr) +mtspr (SPRN_IAMR , new_thread->iamr); +if (old_thread->uamor != new_thread->uamor) +mtspr (SPRN_UAMOR , new_thread->uamor); +#endif } struct task_struct *__switch_to(struct task_struct *prev,
@@ -1686,6 +1699,11 @@ void start_thread(struct pt_regs *regs, unsigned long start, unsigned long sp) current->thread.tm_texasr =0 ; current->thread.tm_tfiar =0 ;#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */ +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS + current->thread.amr =0x0ul ; + current->thread.iamr =0x0ul ; + current->thread.uamor =0x0ul ; +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ }EXPORT_SYMBOL (start_thread);
Signature · 1 line
1.8.3.1
RFC v2 5/12 powerpc: Implementation for sys_mprotect_pkey() system call.
In reply to Ram PaiThis system call, associates the pkey with PTE of all pages corresponding to the given address range.
--- arch/powerpc/include/asm/book3s/64/pgtable.h | 22 ++++++- arch/powerpc/include/asm/mman.h | 29 +++++---- arch/powerpc/include/asm/pkeys.h | 21 ++++++- arch/powerpc/include/asm/systbl.h | 1 + arch/powerpc/include/asm/unistd.h | 4 +- arch/powerpc/include/uapi/asm/unistd.h | 1 + arch/powerpc/mm/pkeys.c | 93 +++++++++++++++++++++++++++- include/linux/mm.h | 1 + 8 files changed, 154 insertions(+), 18 deletions(-)
arch/powerpc/include/asm/book3s/64/pgtable.h +21 -1
@@ -37,6 +37,7 @@#define _RPAGE_RSV2 0x0800000000000000UL#define _RPAGE_RSV3 0x0400000000000000UL#define _RPAGE_RSV4 0x0200000000000000UL +#define _RPAGE_RSV5 0x00040UL#define _PAGE_PTE 0x4000000000000000UL/* distinguishes PTEs from pointers */ #define _PAGE_PRESENT 0x8000000000000000UL/* pte contains a translation */
@@ -56,6 +57,20 @@/* Max physical address bit as per radix table */ #define _RPAGE_PA_MAX 57 +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +#define H_PAGE_PKEY_BIT0 _RPAGE_RSV1 +#define H_PAGE_PKEY_BIT1 _RPAGE_RSV2 +#define H_PAGE_PKEY_BIT2 _RPAGE_RSV3 +#define H_PAGE_PKEY_BIT3 _RPAGE_RSV4 +#define H_PAGE_PKEY_BIT4 _RPAGE_RSV5 +#else /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +#define H_PAGE_PKEY_BIT0 0 +#define H_PAGE_PKEY_BIT1 0 +#define H_PAGE_PKEY_BIT2 0 +#define H_PAGE_PKEY_BIT3 0 +#define H_PAGE_PKEY_BIT4 0 +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +/* * Max physical address bit we will use for now. *
@@ -122,7 +137,12 @@#define PAGE_PROT_BITS (_PAGE_SAO | _PAGE_NON_IDEMPOTENT | _PAGE_TOLERANT | \ H_PAGE_4K_PFN | _PAGE_PRIVILEGED | _PAGE_ACCESSED | \ _PAGE_READ | _PAGE_WRITE | _PAGE_DIRTY | _PAGE_EXEC | \ - _PAGE_SOFT_DIRTY) + _PAGE_SOFT_DIRTY | \ + H_PAGE_PKEY_BIT0 | \ + H_PAGE_PKEY_BIT1 | \ + H_PAGE_PKEY_BIT2 | \ + H_PAGE_PKEY_BIT3 | \ + H_PAGE_PKEY_BIT4)/* * We define 2 sets of base prot bits, one for basic pages (ie, * cacheable kernel and user pages) and one for non cacheable
arch/powerpc/include/asm/mman.h +18 -11
@@ -13,24 +13,31 @@#include <asm/cputable.h> #include <linux/mm.h> +#include <linux/pkeys.h> #include <asm/cpu_has_feature.h> +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +/* * This file is included by linux/mman.h, so we can't use cacl_vm_prot_bits() * here. How important is the optimization? */ -static inline unsigned long arch_calc_vm_prot_bits (unsigned long prot, -unsigned long pkey) -{ -return (prot &PROT_SAO ) ?VM_SAO :0 ; -} -#define arch_calc_vm_prot_bits (prot, pkey) arch_calc_vm_prot_bits(prot, pkey) +#define arch_calc_vm_prot_bits (prot, key) ( \ + ((prot) & PROT_SAO ? VM_SAO : 0) | \ + pkey_to_vmflag_bits(key)) +#define arch_vm_get_page_prot (vm_flags) __pgprot( \ + ((vm_flags) & VM_SAO ? _PAGE_SAO : 0) | \ + vmflag_to_page_pkey_bits(vm_flags)) + +#else /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ + +#define arch_calc_vm_prot_bits (prot, key) ( \ + ((prot) & PROT_SAO ? VM_SAO : 0)) +#define arch_vm_get_page_prot (vm_flags) __pgprot( \ + ((vm_flags) & VM_SAO ? _PAGE_SAO : 0)) + +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ -static inline pgprot_t arch_vm_get_page_prot (unsigned long vm_flags) -{ -return (vm_flags &VM_SAO ) ?__pgprot (_PAGE_SAO) :__pgprot (0 ); -} -#define arch_vm_get_page_prot (vm_flags) arch_vm_get_page_prot(vm_flags)static inline bool arch_validate_prot (unsigned long prot) {
arch/powerpc/include/asm/pkeys.h +20 -1
@@ -14,6 +14,19 @@ VM_PKEY_BIT3 | \ VM_PKEY_BIT4) +#define pkey_to_vmflag_bits (key) (((key & 0x1UL) ? VM_PKEY_BIT0 : 0x0UL) | \ + ((key & 0x2UL) ? VM_PKEY_BIT1 : 0x0UL) | \ + ((key & 0x4UL) ? VM_PKEY_BIT2 : 0x0UL) | \ + ((key & 0x8UL) ? VM_PKEY_BIT3 : 0x0UL) | \ + ((key & 0x10UL) ? VM_PKEY_BIT4 : 0x0UL)) + +#define vmflag_to_page_pkey_bits (vm_flags) \ + (((vm_flags & VM_PKEY_BIT0) ? H_PAGE_PKEY_BIT4 : 0x0UL)| \ + ((vm_flags & VM_PKEY_BIT1) ? H_PAGE_PKEY_BIT3 : 0x0UL) | \ + ((vm_flags & VM_PKEY_BIT2) ? H_PAGE_PKEY_BIT2 : 0x0UL) | \ + ((vm_flags & VM_PKEY_BIT3) ? H_PAGE_PKEY_BIT1 : 0x0UL) | \ + ((vm_flags & VM_PKEY_BIT4) ? H_PAGE_PKEY_BIT0 : 0x0UL)) +/* * Bits are in BE format. * NOTE: key 31, 1, 0 are not used.
@@ -42,6 +55,12 @@#define mm_set_pkey_is_reserved (mm, pkey) (PKEY_INITIAL_ALLOCAION & \ pkeybit_mask(pkey)) + +static inline int vma_pkey (struct vm_area_struct *vma) +{ +return (vma->vm_flags &ARCH_VM_PKEY_FLAGS ) >>VM_PKEY_SHIFT ; +} +static inline bool mm_pkey_is_allocated (struct mm_struct *mm,int pkey) {/* a reserved key is never considered as 'explicitly allocated' */
@@ -114,7 +133,7 @@ static inline int arch_set_user_pkey_access(struct task_struct *tsk, int pkey,return __arch_set_user_pkey_access (tsk, pkey, init_val); } -static inline pkey_mm_init(struct mm_struct *mm) +static inline void pkey_mm_init (struct mm_struct *mm) {mm_pkey_allocation_map (mm) =PKEY_INITIAL_ALLOCAION ;/* -1 means unallocated or invalid */
arch/powerpc/include/asm/systbl.h +1 -0
@@ -390,3 +390,4 @@ SYSCALL(statx) SYSCALL(pkey_alloc) SYSCALL(pkey_free) +SYSCALL(pkey_mprotect)
arch/powerpc/include/asm/unistd.h +1 -3
@@ -12,12 +12,10 @@#include <uapi/asm/unistd.h> -#define NR_syscalls 386 +#define NR_syscalls 387#define __NR__exit __NR_exit -#define __IGNORE_pkey_mprotect -#ifndef __ASSEMBLY__#include <linux/types.h>
arch/powerpc/include/uapi/asm/unistd.h +1 -0
@@ -396,5 +396,6 @@#define __NR_statx 383#define __NR_pkey_alloc 384#define __NR_pkey_free 385 +#define __NR_pkey_mprotect 386#endif /* _UAPI_ASM_POWERPC_UNISTD_H_ */
arch/powerpc/mm/pkeys.c +91 -2
109 lines not on this page · raw message
include/linux/mm.h +1 -0
@@ -227,6 +227,7 @@ extern int overcommit_kbytes_handler(struct ctl_table *, int, void __user *,#define VM_PKEY_BIT3 VM_HIGH_ARCH_3#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ #elif defined(CONFIG_PPC) +#define VM_PKEY_SHIFT VM_HIGH_ARCH_BIT_0#define VM_PKEY_BIT0 VM_HIGH_ARCH_0/* A protection key is a 5-bit value */ #define VM_PKEY_BIT1 VM_HIGH_ARCH_1#define VM_PKEY_BIT2 VM_HIGH_ARCH_2
Signature · 1 line
1.8.3.1
Re: [RFC v2 05/12] powerpc: Implementation for sys_mprotect_pkey() system call.
Ram Pai <[email protected]> writes:
....
Quoted 9 lines
arch/powerpc/include/asm/mman.h +2
-2+#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +/* * This file is included by linux/mman.h, so we can't use cacl_vm_prot_bits() * here. How important is the optimization? */ -static inline unsigned long arch_calc_vm_prot_bits (unsigned long prot, -unsigned long pkey)
arch/powerpc/include/asm/mman.h line 30
Quoted 10 lines
arch/powerpc/include/asm/mman.h +6
-4-{ -return (prot &PROT_SAO ) ?VM_SAO :0 ; -} -#define arch_calc_vm_prot_bits (prot, pkey) arch_calc_vm_prot_bits(prot, pkey) +#define arch_calc_vm_prot_bits (prot, key) ( \ + ((prot) & PROT_SAO ? VM_SAO : 0) | \ + pkey_to_vmflag_bits(key)) +#define arch_vm_get_page_prot (vm_flags) __pgprot( \ + ((vm_flags) & VM_SAO ? _PAGE_SAO : 0) | \ + vmflag_to_page_pkey_bits(vm_flags))
Can we avoid converting static inline back to macors ? They loose type checking.
Quoted 8 lines
arch/powerpc/include/asm/mman.h +8
-0+ +#else /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ + +#define arch_calc_vm_prot_bits (prot, key) ( \ + ((prot) & PROT_SAO ? VM_SAO : 0)) +#define arch_vm_get_page_prot (vm_flags) __pgprot( \ + ((vm_flags) & VM_SAO ? _PAGE_SAO : 0)) +
arch/powerpc/include/asm/mman.h line 43
Quoted 10 lines
arch/powerpc/include/asm/mman.h +1
-5+#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ -static inline pgprot_t arch_vm_get_page_prot (unsigned long vm_flags) -{ -return (vm_flags &VM_SAO ) ?__pgprot (_PAGE_SAO) :__pgprot (0 ); -} -#define arch_vm_get_page_prot (vm_flags) arch_vm_get_page_prot(vm_flags)static inline bool arch_validate_prot (unsigned long prot) {
-aneesh
RFC v2 6/12 powerpc: Program HPTE key protection bits.
In reply to Ram PaiMap the PTE protection key bits to the HPTE key protection bits, while creatiing HPTE entries.
--- arch/powerpc/include/asm/book3s/64/mmu-hash.h | 5 +++++ arch/powerpc/include/asm/pkeys.h | 7 +++++++ arch/powerpc/mm/hash_utils_64.c | 5 +++++ 3 files changed, 17 insertions(+)
arch/powerpc/include/asm/book3s/64/mmu-hash.h +5 -0
@@ -90,6 +90,8 @@#define HPTE_R_PP0 ASM_CONST(0x8000000000000000)#define HPTE_R_TS ASM_CONST(0x4000000000000000)#define HPTE_R_KEY_HI ASM_CONST(0x3000000000000000) +#define HPTE_R_KEY_BIT0 ASM_CONST(0x2000000000000000) +#define HPTE_R_KEY_BIT1 ASM_CONST(0x1000000000000000)#define HPTE_R_RPN_SHIFT 12#define HPTE_R_RPN ASM_CONST(0x0ffffffffffff000)#define HPTE_R_RPN_3_0 ASM_CONST(0x01fffffffffff000)
@@ -104,6 +106,9 @@#define HPTE_R_C ASM_CONST(0x0000000000000080)#define HPTE_R_R ASM_CONST(0x0000000000000100)#define HPTE_R_KEY_LO ASM_CONST(0x0000000000000e00) +#define HPTE_R_KEY_BIT2 ASM_CONST(0x0000000000000800) +#define HPTE_R_KEY_BIT3 ASM_CONST(0x0000000000000400) +#define HPTE_R_KEY_BIT4 ASM_CONST(0x0000000000000200)#define HPTE_V_1TB_SEG ASM_CONST(0x4000000000000000)#define HPTE_V_VRMA_MASK ASM_CONST(0x4001ffffff000000)
arch/powerpc/include/asm/pkeys.h +7 -0
@@ -27,6 +27,13 @@ ((vm_flags & VM_PKEY_BIT3) ? H_PAGE_PKEY_BIT1 : 0x0UL) | \ ((vm_flags & VM_PKEY_BIT4) ? H_PAGE_PKEY_BIT0 : 0x0UL)) +#define calc_pte_to_hpte_pkey_bits (pteflags) \ + (((pteflags & H_PAGE_PKEY_BIT0) ? HPTE_R_KEY_BIT0 : 0x0UL) | \ + ((pteflags & H_PAGE_PKEY_BIT1) ? HPTE_R_KEY_BIT1 : 0x0UL) | \ + ((pteflags & H_PAGE_PKEY_BIT2) ? HPTE_R_KEY_BIT2 : 0x0UL) | \ + ((pteflags & H_PAGE_PKEY_BIT3) ? HPTE_R_KEY_BIT3 : 0x0UL) | \ + ((pteflags & H_PAGE_PKEY_BIT4) ? HPTE_R_KEY_BIT4 : 0x0UL)) +/* * Bits are in BE format. * NOTE: key 31, 1, 0 are not used.
arch/powerpc/mm/hash_utils_64.c +5 -0
@@ -35,6 +35,7 @@#include <linux/memblock.h> #include <linux/context_tracking.h> #include <linux/libfdt.h> +#include <linux/pkeys.h> #include <asm/debugfs.h> #include <asm/processor.h>
@@ -230,6 +231,10 @@ unsigned long htab_convert_pte_flags(unsigned long pteflags)*/ rflags |=HPTE_R_M ; +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS + rflags |=calc_pte_to_hpte_pkey_bits (pteflags); +#endif +return rflags; }
Signature · 1 line
1.8.3.1
Re: [RFC v2 06/12] powerpc: Program HPTE key protection bits.
Quoted 24 lines · Ram Pai
Map the PTE protection key bits to the HPTE key protection bits, while creatiing HPTE entries.
Signed-off-by Ram Pai <[email protected]>--- arch/powerpc/include/asm/book3s/64/mmu-hash.h | 5 +++++ arch/powerpc/include/asm/pkeys.h | 7 +++++++ arch/powerpc/mm/hash_utils_64.c | 5 +++++ 3 files changed, 17 insertions(+)arch/powerpc/include/asm/book3s/64/mmu-hash.h +2
-0@@ -90,6 +90,8 @@#define HPTE_R_PP0 ASM_CONST(0x8000000000000000)#define HPTE_R_TS ASM_CONST(0x4000000000000000)#define HPTE_R_KEY_HI ASM_CONST(0x3000000000000000) +#define HPTE_R_KEY_BIT0 ASM_CONST(0x2000000000000000) +#define HPTE_R_KEY_BIT1 ASM_CONST(0x1000000000000000)#define HPTE_R_RPN_SHIFT 12#define HPTE_R_RPN ASM_CONST(0x0ffffffffffff000)#define HPTE_R_RPN_3_0 ASM_CONST(0x01fffffffffff000)
arch/powerpc/include/asm/book3s/64/mmu-hash.h line 112
Quoted 7 lines
arch/powerpc/include/asm/book3s/64/mmu-hash.h +3
-0@@ -104,6 +106,9 @@#define HPTE_R_C ASM_CONST(0x0000000000000080)#define HPTE_R_R ASM_CONST(0x0000000000000100)#define HPTE_R_KEY_LO ASM_CONST(0x0000000000000e00) +#define HPTE_R_KEY_BIT2 ASM_CONST(0x0000000000000800) +#define HPTE_R_KEY_BIT3 ASM_CONST(0x0000000000000400) +#define HPTE_R_KEY_BIT4 ASM_CONST(0x0000000000000200)
Should we indicate/document how these 5 bits are not contiguous in the HPTE format for any given real page ?
Quoted 7 lines
arch/powerpc/include/asm/book3s/64/mmu-hash.h +0
-0#define HPTE_V_1TB_SEG ASM_CONST(0x4000000000000000)#define HPTE_V_VRMA_MASK ASM_CONST(0x4001ffffff000000)arch/powerpc/include/asm/pkeys.h +0
-0
arch/powerpc/include/asm/pkeys.h line 36
Quoted 10 lines
arch/powerpc/include/asm/pkeys.h +7
-0@@ -27,6 +27,13 @@ ((vm_flags & VM_PKEY_BIT3) ? H_PAGE_PKEY_BIT1 : 0x0UL) | \ ((vm_flags & VM_PKEY_BIT4) ? H_PAGE_PKEY_BIT0 : 0x0UL)) +#define calc_pte_to_hpte_pkey_bits (pteflags) \ + (((pteflags & H_PAGE_PKEY_BIT0) ? HPTE_R_KEY_BIT0 : 0x0UL) | \ + ((pteflags & H_PAGE_PKEY_BIT1) ? HPTE_R_KEY_BIT1 : 0x0UL) | \ + ((pteflags & H_PAGE_PKEY_BIT2) ? HPTE_R_KEY_BIT2 : 0x0UL) | \ + ((pteflags & H_PAGE_PKEY_BIT3) ? HPTE_R_KEY_BIT3 : 0x0UL) | \ + ((pteflags & H_PAGE_PKEY_BIT4) ? HPTE_R_KEY_BIT4 : 0x0UL)) +
We can drop calc_ in here. pte_to_hpte_pkey_bits should be sufficient.
Re: [RFC v2 06/12] powerpc: Program HPTE key protection bits.
Quoted 27 lines · Anshuman Khandual
Quoted 26 lines · Ram Pai
Map the PTE protection key bits to the HPTE key protection bits, while creatiing HPTE entries.
Signed-off-by Ram Pai <[email protected]>--- arch/powerpc/include/asm/book3s/64/mmu-hash.h | 5 +++++ arch/powerpc/include/asm/pkeys.h | 7 +++++++ arch/powerpc/mm/hash_utils_64.c | 5 +++++ 3 files changed, 17 insertions(+)arch/powerpc/include/asm/book3s/64/mmu-hash.h +2
-0@@ -90,6 +90,8 @@#define HPTE_R_PP0 ASM_CONST(0x8000000000000000)#define HPTE_R_TS ASM_CONST(0x4000000000000000)#define HPTE_R_KEY_HI ASM_CONST(0x3000000000000000) +#define HPTE_R_KEY_BIT0 ASM_CONST(0x2000000000000000) +#define HPTE_R_KEY_BIT1 ASM_CONST(0x1000000000000000)#define HPTE_R_RPN_SHIFT 12#define HPTE_R_RPN ASM_CONST(0x0ffffffffffff000)#define HPTE_R_RPN_3_0 ASM_CONST(0x01fffffffffff000)@@ -104,6 +106,9 @@#define HPTE_R_C ASM_CONST(0x0000000000000080)#define HPTE_R_R ASM_CONST(0x0000000000000100)
Quoted 8 lines
arch/powerpc/include/asm/book3s/64/mmu-hash.h line 112
Quoted 5 lines
arch/powerpc/include/asm/book3s/64/mmu-hash.h +3
-0#define HPTE_R_KEY_LO ASM_CONST(0x0000000000000e00) +#define HPTE_R_KEY_BIT2 ASM_CONST(0x0000000000000800) +#define HPTE_R_KEY_BIT3 ASM_CONST(0x0000000000000400) +#define HPTE_R_KEY_BIT4 ASM_CONST(0x0000000000000200)Should we indicate/document how these 5 bits are not contiguous in the HPTE format for any given real page ?
I can, but its all well documented in the ISA. Infact all the bits and the macros are one to one translation from the ISA.
Quoted 11 lines
Quoted 11 lines
arch/powerpc/include/asm/book3s/64/mmu-hash.h +0
-0#define HPTE_V_1TB_SEG ASM_CONST(0x4000000000000000)#define HPTE_V_VRMA_MASK ASM_CONST(0x4001ffffff000000)arch/powerpc/include/asm/pkeys.h +0
-0@@ -27,6 +27,13 @@ ((vm_flags &VM_PKEY_BIT3 ) ?H_PAGE_PKEY_BIT1 :0x0UL ) | \ ((vm_flags & VM_PKEY_BIT4) ? H_PAGE_PKEY_BIT0 : 0x0UL))
Quoted 10 lines
arch/powerpc/include/asm/pkeys.h line 36
Quoted 7 lines
arch/powerpc/include/asm/pkeys.h +7
-0+#define calc_pte_to_hpte_pkey_bits (pteflags) \ + (((pteflags & H_PAGE_PKEY_BIT0) ? HPTE_R_KEY_BIT0 : 0x0UL) | \ + ((pteflags & H_PAGE_PKEY_BIT1) ? HPTE_R_KEY_BIT1 : 0x0UL) | \ + ((pteflags & H_PAGE_PKEY_BIT2) ? HPTE_R_KEY_BIT2 : 0x0UL) | \ + ((pteflags & H_PAGE_PKEY_BIT3) ? HPTE_R_KEY_BIT3 : 0x0UL) | \ + ((pteflags & H_PAGE_PKEY_BIT4) ? HPTE_R_KEY_BIT4 : 0x0UL)) +We can drop
calc_in here.pte_to_hpte_pkey_bitsshould be sufficient.
ok. will do.
thanks for your comments,
RP
RFC v2 7/12 powerpc: Macro the mask used for checking DSI exception
In reply to Ram PaiReplace the magic number used to check for DSI exception with a meaningful value.
--- arch/powerpc/include/asm/reg.h | 9 ++++++++- arch/powerpc/kernel/exceptions-64s.S | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-)
arch/powerpc/include/asm/reg.h +8 -1
@@ -272,16 +272,23 @@#define SPRN_DAR 0x013/* Data Address Register */ #define SPRN_DBCR 0x136/* e300 Data Breakpoint Control Reg */ #define SPRN_DSISR 0x012/* Data Storage Interrupt Status Register */ +#define DSISR_BIT32 0x80000000/* not defined */ #define DSISR_NOHPTE 0x40000000/* no translation found */ +#define DSISR_PAGEATTR_CONFLT 0x20000000/* page attribute conflict */ +#define DSISR_BIT35 0x10000000/* not defined */ #define DSISR_PROTFAULT 0x08000000/* protection fault */ #define DSISR_BADACCESS 0x04000000/* bad access to CI or G */ #define DSISR_ISSTORE 0x02000000/* access was a store */ #define DSISR_DABRMATCH 0x00400000/* hit data breakpoint */ -#define DSISR_NOSEGMENT 0x00200000/* SLB miss */ #define DSISR_KEYFAULT 0x00200000/* Key fault */ +#define DSISR_BIT43 0x00100000/* not defined */ #define DSISR_UNSUPP_MMU 0x00080000/* Unsupported MMU config */ #define DSISR_SET_RC 0x00040000/* Failed setting of R/C bits */ #define DSISR_PGDIRFAULT 0x00020000/* Fault on page directory */ +#define DSISR_PAGE_FAULT_MASK (DSISR_BIT32 | \ + DSISR_PAGEATTR_CONFLT | \ + DSISR_BADACCESS | \ + DSISR_BIT43)#define SPRN_TBRL 0x10C/* Time Base Read Lower Register (user, R/O) */ #define SPRN_TBRU 0x10D/* Time Base Read Upper Register (user, R/O) */ #define SPRN_CIR 0x11B/* Chip Information Register (hyper, R/0) */
arch/powerpc/kernel/exceptions-64s.S +1 -1
@@ -1411,7 +1411,7 @@ USE_TEXT_SECTION() .balign IFETCH_ALIGN_BYTES do_hash_page: #ifdef CONFIG_PPC_STD_MMU_64 - andis. r0,r4,0xa410 /* weird error? */ + andis. r0,r4,DSISR_PAGE_FAULT_MASK@h bne- handle_page_fault /* if not, try to insert a HPTE */ andis. r0,r4,DSISR_DABRMATCH@h bne- handle_dabr_fault
Signature · 1 line
1.8.3.1
Re: [RFC v2 07/12] powerpc: Macro the mask used for checking DSI exception
Quoted 25 lines · Ram Pai
Replace the magic number used to check for DSI exception with a meaningful value.
Signed-off-by Ram Pai <[email protected]>--- arch/powerpc/include/asm/reg.h | 9 ++++++++- arch/powerpc/kernel/exceptions-64s.S | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-)arch/powerpc/include/asm/reg.h +3
-0@@ -272,16 +272,23 @@#define SPRN_DAR 0x013/* Data Address Register */ #define SPRN_DBCR 0x136/* e300 Data Breakpoint Control Reg */ #define SPRN_DSISR 0x012/* Data Storage Interrupt Status Register */ +#define DSISR_BIT32 0x80000000/* not defined */ #define DSISR_NOHPTE 0x40000000/* no translation found */ +#define DSISR_PAGEATTR_CONFLT 0x20000000/* page attribute conflict */ +#define DSISR_BIT35 0x10000000/* not defined */ #define DSISR_PROTFAULT 0x08000000/* protection fault */ #define DSISR_BADACCESS 0x04000000/* bad access to CI or G */ #define DSISR_ISSTORE 0x02000000/* access was a store */ #define DSISR_DABRMATCH 0x00400000/* hit data breakpoint */
arch/powerpc/include/asm/reg.h line 291
Quoted 10 lines
arch/powerpc/include/asm/reg.h +5
-1-#define DSISR_NOSEGMENT 0x00200000/* SLB miss */ #define DSISR_KEYFAULT 0x00200000/* Key fault */ +#define DSISR_BIT43 0x00100000/* not defined */ #define DSISR_UNSUPP_MMU 0x00080000/* Unsupported MMU config */ #define DSISR_SET_RC 0x00040000/* Failed setting of R/C bits */ #define DSISR_PGDIRFAULT 0x00020000/* Fault on page directory */ +#define DSISR_PAGE_FAULT_MASK (DSISR_BIT32 | \ + DSISR_PAGEATTR_CONFLT | \ + DSISR_BADACCESS | \ + DSISR_BIT43)
Sorry missed this one. Seems like there are couple of unnecessary line additions in the subsequent patch which adds the new PKEY reason code.
-#define DSISR_PAGE_FAULT_MASK (DSISR_BIT32 | \
- DSISR_PAGEATTR_CONFLT | \
- DSISR_BADACCESS | \
+#define DSISR_PAGE_FAULT_MASK (DSISR_BIT32 | \
+ DSISR_PAGEATTR_CONFLT | \
+ DSISR_BADACCESS | \
+ DSISR_KEYFAULT | \
DSISR_BIT43)
Re: [RFC v2 07/12] powerpc: Macro the mask used for checking DSI exception
Quoted 41 lines · Anshuman Khandual
Quoted 25 lines · Ram Pai
Replace the magic number used to check for DSI exception with a meaningful value.
Signed-off-by Ram Pai <[email protected]>--- arch/powerpc/include/asm/reg.h | 9 ++++++++- arch/powerpc/kernel/exceptions-64s.S | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-)arch/powerpc/include/asm/reg.h +3
-0@@ -272,16 +272,23 @@#define SPRN_DAR 0x013/* Data Address Register */ #define SPRN_DBCR 0x136/* e300 Data Breakpoint Control Reg */ #define SPRN_DSISR 0x012/* Data Storage Interrupt Status Register */ +#define DSISR_BIT32 0x80000000/* not defined */ #define DSISR_NOHPTE 0x40000000/* no translation found */ +#define DSISR_PAGEATTR_CONFLT 0x20000000/* page attribute conflict */ +#define DSISR_BIT35 0x10000000/* not defined */ #define DSISR_PROTFAULT 0x08000000/* protection fault */ #define DSISR_BADACCESS 0x04000000/* bad access to CI or G */ #define DSISR_ISSTORE 0x02000000/* access was a store */ #define DSISR_DABRMATCH 0x00400000/* hit data breakpoint */ arch/powerpc/include/asm/reg.h line 291
Quoted 10 lines
arch/powerpc/include/asm/reg.h +5
-1-#define DSISR_NOSEGMENT 0x00200000/* SLB miss */ #define DSISR_KEYFAULT 0x00200000/* Key fault */ +#define DSISR_BIT43 0x00100000/* not defined */ #define DSISR_UNSUPP_MMU 0x00080000/* Unsupported MMU config */ #define DSISR_SET_RC 0x00040000/* Failed setting of R/C bits */ #define DSISR_PGDIRFAULT 0x00020000/* Fault on page directory */ +#define DSISR_PAGE_FAULT_MASK (DSISR_BIT32 | \ + DSISR_PAGEATTR_CONFLT | \ + DSISR_BADACCESS | \ + DSISR_BIT43)Sorry missed this one. Seems like there are couple of unnecessary line additions in the subsequent patch which adds the new PKEY reason code.
Review
Quoted 8 lines
(quoted diff) +4
-3-#define DSISR_PAGE_FAULT_MASK (DSISR_BIT32 | \ - DSISR_PAGEATTR_CONFLT | \ - DSISR_BADACCESS | \ +#define DSISR_PAGE_FAULT_MASK (DSISR_BIT32 | \ + DSISR_PAGEATTR_CONFLT | \ + DSISR_BADACCESS | \ + DSISR_KEYFAULT | \ DSISR_BIT43)
i like to see them separately, one per line. But than you are right. that is not the convention in this file. So will change it accordingly.
thanks,
RP
Quoted 2 lines
Signature · 1 line
Ram Pai
Re: [RFC v2 07/12] powerpc: Macro the mask used for checking DSI exception
In reply to Ram PaiQuoted 43 lines · Ram Pai
Replace the magic number used to check for DSI exception with a meaningful value.
Signed-off-by Ram Pai <[email protected]>--- arch/powerpc/include/asm/reg.h | 9 ++++++++- arch/powerpc/kernel/exceptions-64s.S | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-)arch/powerpc/include/asm/reg.h +8
-1@@ -272,16 +272,23 @@#define SPRN_DAR 0x013/* Data Address Register */ #define SPRN_DBCR 0x136/* e300 Data Breakpoint Control Reg */ #define SPRN_DSISR 0x012/* Data Storage Interrupt Status Register */ +#define DSISR_BIT32 0x80000000/* not defined */ #define DSISR_NOHPTE 0x40000000/* no translation found */ +#define DSISR_PAGEATTR_CONFLT 0x20000000/* page attribute conflict */ +#define DSISR_BIT35 0x10000000/* not defined */ #define DSISR_PROTFAULT 0x08000000/* protection fault */ #define DSISR_BADACCESS 0x04000000/* bad access to CI or G */ #define DSISR_ISSTORE 0x02000000/* access was a store */ #define DSISR_DABRMATCH 0x00400000/* hit data breakpoint */ -#define DSISR_NOSEGMENT 0x00200000/* SLB miss */ #define DSISR_KEYFAULT 0x00200000/* Key fault */ +#define DSISR_BIT43 0x00100000/* not defined */ #define DSISR_UNSUPP_MMU 0x00080000/* Unsupported MMU config */ #define DSISR_SET_RC 0x00040000/* Failed setting of R/C bits */ #define DSISR_PGDIRFAULT 0x00020000/* Fault on page directory */ +#define DSISR_PAGE_FAULT_MASK (DSISR_BIT32 | \ + DSISR_PAGEATTR_CONFLT | \ + DSISR_BADACCESS | \ + DSISR_BIT43)#define SPRN_TBRL 0x10C/* Time Base Read Lower Register (user, R/O) */ #define SPRN_TBRU 0x10D/* Time Base Read Upper Register (user, R/O) */ #define SPRN_CIR 0x11B/* Chip Information Register (hyper, R/0) */ arch/powerpc/kernel/exceptions-64s.S +0
-0
arch/powerpc/kernel/exceptions-64s.S USE_TEXT_SECTION() · line 1417
Quoted 8 lines
arch/powerpc/kernel/exceptions-64s.S +1
-1@@ -1411,7 +1411,7 @@ USE_TEXT_SECTION() .balign IFETCH_ALIGN_BYTES do_hash_page: #ifdef CONFIG_PPC_STD_MMU_64 - andis. r0,r4,0xa410 /* weird error? */ + andis. r0,r4,DSISR_PAGE_FAULT_MASK@h bne- handle_page_fault /* if not, try to insert a HPTE */ andis. r0,r4,DSISR_DABRMATCH@h bne- handle_dabr_fault
Thanks for doing this. I always wondered what that 0xa410 indicates. Now tha it is documented, I am wondering are those the only DSISR values that we want to check early ? You also added few bit positions that is expected to carry value 0 ? But then excluded BIT35. Any reason ?
-aneesh
Re: [RFC v2 07/12] powerpc: Macro the mask used for checking DSI exception
Quoted 51 lines · Aneesh Kumar K.V
Quoted 49 lines · Ram Pai
Replace the magic number used to check for DSI exception with a meaningful value.
Signed-off-by Ram Pai <[email protected]>--- arch/powerpc/include/asm/reg.h | 9 ++++++++- arch/powerpc/kernel/exceptions-64s.S | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-)arch/powerpc/include/asm/reg.h +8
-1@@ -272,16 +272,23 @@#define SPRN_DAR 0x013/* Data Address Register */ #define SPRN_DBCR 0x136/* e300 Data Breakpoint Control Reg */ #define SPRN_DSISR 0x012/* Data Storage Interrupt Status Register */ +#define DSISR_BIT32 0x80000000/* not defined */ #define DSISR_NOHPTE 0x40000000/* no translation found */ +#define DSISR_PAGEATTR_CONFLT 0x20000000/* page attribute conflict */ +#define DSISR_BIT35 0x10000000/* not defined */ #define DSISR_PROTFAULT 0x08000000/* protection fault */ #define DSISR_BADACCESS 0x04000000/* bad access to CI or G */ #define DSISR_ISSTORE 0x02000000/* access was a store */ #define DSISR_DABRMATCH 0x00400000/* hit data breakpoint */ -#define DSISR_NOSEGMENT 0x00200000/* SLB miss */ #define DSISR_KEYFAULT 0x00200000/* Key fault */ +#define DSISR_BIT43 0x00100000/* not defined */ #define DSISR_UNSUPP_MMU 0x00080000/* Unsupported MMU config */ #define DSISR_SET_RC 0x00040000/* Failed setting of R/C bits */ #define DSISR_PGDIRFAULT 0x00020000/* Fault on page directory */ +#define DSISR_PAGE_FAULT_MASK (DSISR_BIT32 | \ + DSISR_PAGEATTR_CONFLT | \ + DSISR_BADACCESS | \ + DSISR_BIT43)#define SPRN_TBRL 0x10C/* Time Base Read Lower Register (user, R/O) */ #define SPRN_TBRU 0x10D/* Time Base Read Upper Register (user, R/O) */ #define SPRN_CIR 0x11B/* Chip Information Register (hyper, R/0) */ arch/powerpc/kernel/exceptions-64s.S +1
-1@@ -1411,7 +1411,7 @@ USE_TEXT_SECTION() .balign IFETCH_ALIGN_BYTES do_hash_page: #ifdef CONFIG_PPC_STD_MMU_64 - andis. r0,r4,0xa410 /* weird error? */ + andis. r0,r4,DSISR_PAGE_FAULT_MASK@h bne- handle_page_fault /* if not, try to insert a HPTE */
Quoted 8 lines
arch/powerpc/kernel/exceptions-64s.S USE_TEXT_SECTION() · line 1417
Quoted 2 lines
arch/powerpc/kernel/exceptions-64s.S +0
-0andis. r0,r4,DSISR_DABRMATCH@h bne- handle_dabr_faultThanks for doing this. I always wondered what that
0xa410indicates. Now tha it is documented, I am wondering are those the only DSISR values that we want to check early ? You also added few bit positions that is expected to carry value 0 ? But then excluded BIT35. Any reason ?
I did not look deeply into why the exact number 0xa410 was used in the past. I built the macro DSISR_PAGE_FAULT_MASK using whatever bits make up 0xa410. BIT35 if added to the DSISR_PAGE_FAULT_MASK would make it 0xb410. So I did not consider it.
However the macro for BIT35 is already defined in this patch, if that is what you were looking for.
+#define DSISR_BIT35 0x10000000 /* not defined */
RP
RFC v2 8/12 powerpc: Handle exceptions caused by violation of pkey protection.
In reply to Ram PaiHandle Data and Instruction exceptions caused by memory protection-key.
Conflicts:
arch/powerpc/include/asm/reg.h
arch/powerpc/kernel/exceptions-64s.S
--- arch/powerpc/include/asm/mmu_context.h | 12 +++++ arch/powerpc/include/asm/pkeys.h | 9 ++++ arch/powerpc/include/asm/reg.h | 7 +-- arch/powerpc/mm/fault.c | 21 +++++++- arch/powerpc/mm/pkeys.c | 90 ++++++++++++++++++++++++++++++++++ 5 files changed, 134 insertions(+), 5 deletions(-)
arch/powerpc/include/asm/mmu_context.h +12 -0
@@ -175,11 +175,23 @@ static inline void arch_bprm_mm_init(struct mm_struct *mm, { } +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +bool arch_pte_access_permitted (pte_t pte,bool write); +bool arch_vma_access_permitted (struct vm_area_struct *vma, +bool write,bool execute,bool foreign); +#else /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +static inline bool arch_pte_access_permitted (pte_t pte,bool write) +{ +/* by default, allow everything */ +return true; +}static inline bool arch_vma_access_permitted (struct vm_area_struct *vma,bool write,bool execute,bool foreign) {/* by default, allow everything */ return true; } +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +#endif /* __KERNEL__ */ #endif /* __ASM_POWERPC_MMU_CONTEXT_H */
arch/powerpc/include/asm/pkeys.h +9 -0
@@ -14,6 +14,15 @@ VM_PKEY_BIT3 | \ VM_PKEY_BIT4) +static inline u16 pte_flags_to_pkey (unsigned long pte_flags) +{ +return ((pte_flags &H_PAGE_PKEY_BIT4 ) ?0x1 :0x0 ) | + ((pte_flags &H_PAGE_PKEY_BIT3 ) ?0x2 :0x0 ) | + ((pte_flags &H_PAGE_PKEY_BIT2 ) ?0x4 :0x0 ) | + ((pte_flags &H_PAGE_PKEY_BIT1 ) ?0x8 :0x0 ) | + ((pte_flags &H_PAGE_PKEY_BIT0 ) ?0x10 :0x0 ); +} +#define pkey_to_vmflag_bits (key) (((key & 0x1UL) ? VM_PKEY_BIT0 : 0x0UL) | \ ((key & 0x2UL) ? VM_PKEY_BIT1 : 0x0UL) | \ ((key & 0x4UL) ? VM_PKEY_BIT2 : 0x0UL) | \
arch/powerpc/include/asm/reg.h +4 -3
@@ -285,9 +285,10 @@#define DSISR_UNSUPP_MMU 0x00080000/* Unsupported MMU config */ #define DSISR_SET_RC 0x00040000/* Failed setting of R/C bits */ #define DSISR_PGDIRFAULT 0x00020000/* Fault on page directory */ -#define DSISR_PAGE_FAULT_MASK (DSISR_BIT32 | \ - DSISR_PAGEATTR_CONFLT | \ - DSISR_BADACCESS | \ +#define DSISR_PAGE_FAULT_MASK (DSISR_BIT32 | \ + DSISR_PAGEATTR_CONFLT | \ + DSISR_BADACCESS | \ + DSISR_KEYFAULT | \ DSISR_BIT43)#define SPRN_TBRL 0x10C/* Time Base Read Lower Register (user, R/O) */ #define SPRN_TBRU 0x10D/* Time Base Read Upper Register (user, R/O) */
arch/powerpc/mm/fault.c +19 -2
@@ -216,9 +216,10 @@ int do_page_fault(struct pt_regs *regs, unsigned long address,* bits we are interested in. But there are some bits which * indicate errors in DSISR but can validly be set in SRR1. */ -if (trap ==0x400 ) +if (trap ==0x400 ) { error_code &=0x48200000 ; -else + flags |=FAULT_FLAG_INSTRUCTION ; + }else is_write = error_code &DSISR_ISSTORE ;#else is_write = error_code &ESR_DST ;
@@ -261,6 +262,13 @@ int do_page_fault(struct pt_regs *regs, unsigned long address, }#endif +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +if (error_code &DSISR_KEYFAULT ) { + code =SEGV_PKUERR ; + goto bad_area_nosemaphore; + } +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +/* We restore the interrupt state now */ if (!arch_irq_disabled_regs (regs))local_irq_enable ();
@@ -441,6 +449,15 @@ int do_page_fault(struct pt_regs *regs, unsigned long address,WARN_ON_ONCE (error_code &DSISR_PROTFAULT );#endif /* CONFIG_PPC_STD_MMU */ +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +if (!arch_vma_access_permitted (vma, flags &FAULT_FLAG_WRITE , + flags &FAULT_FLAG_INSTRUCTION , +0 )) { + code =SEGV_PKUERR ; + goto bad_area; + } +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +/* * If for any reason at all we couldn't handle the fault, * make sure we exit gracefully rather than endlessly redo
arch/powerpc/mm/pkeys.c +90 -0
99 lines not on this page · raw message
Signature · 1 line
1.8.3.1
Re: [RFC v2 08/12] powerpc: Handle exceptions caused by violation of pkey protection.
Quoted 5 lines · Ram Pai
Handle Data and Instruction exceptions caused by memory protection-key.
Signed-off-by Ram Pai <[email protected]>(cherry picked from commit a5e5217619a0c475fe0cacc3b0cf1d3d33c79a09)
To which tree this commit belongs to ?
Quoted 26 lines
Conflicts:
arch/powerpc/include/asm/reg.h
arch/powerpc/kernel/exceptions-64s.S--- arch/powerpc/include/asm/mmu_context.h | 12 +++++ arch/powerpc/include/asm/pkeys.h | 9 ++++ arch/powerpc/include/asm/reg.h | 7 +-- arch/powerpc/mm/fault.c | 21 +++++++- arch/powerpc/mm/pkeys.c | 90 ++++++++++++++++++++++++++++++++++ 5 files changed, 134 insertions(+), 5 deletions(-)arch/powerpc/include/asm/mmu_context.h +6
-0@@ -175,11 +175,23 @@ static inline void arch_bprm_mm_init(struct mm_struct *mm, { } +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +bool arch_pte_access_permitted (pte_t pte,bool write); +bool arch_vma_access_permitted (struct vm_area_struct *vma, +bool write,bool execute,bool foreign); +#else /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +static inline bool arch_pte_access_permitted (pte_t pte,bool write)
arch/powerpc/include/asm/mmu_context.h static inline void arch_bprm_mm_init(struct mm_struct *mm, · line 193
Quoted 10 lines
arch/powerpc/include/asm/mmu_context.h +4
-0+{ +/* by default, allow everything */ +return true; +}static inline bool arch_vma_access_permitted (struct vm_area_struct *vma,bool write,bool execute,bool foreign) {/* by default, allow everything */ return true; }
Right, these are the two functions the core VM expects the arch to provide.
Quoted 10 lines
arch/powerpc/include/asm/mmu_context.h +2
-0+#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +#endif /* __KERNEL__ */ #endif /* __ASM_POWERPC_MMU_CONTEXT_H */ arch/powerpc/include/asm/pkeys.h +0
-0@@ -14,6 +14,15 @@ VM_PKEY_BIT3 | \
arch/powerpc/include/asm/pkeys.h line 24
Quoted 10 lines
arch/powerpc/include/asm/pkeys.h +8
-0VM_PKEY_BIT4) +static inline u16 pte_flags_to_pkey(unsigned long pte_flags) +{ +return ((pte_flags &H_PAGE_PKEY_BIT4 ) ?0x1 :0x0 ) | + ((pte_flags &H_PAGE_PKEY_BIT3 ) ?0x2 :0x0 ) | + ((pte_flags &H_PAGE_PKEY_BIT2 ) ?0x4 :0x0 ) | + ((pte_flags &H_PAGE_PKEY_BIT1 ) ?0x8 :0x0 ) | + ((pte_flags &H_PAGE_PKEY_BIT0 ) ?0x10 :0x0 ); +}
Add defines for the above 0x1, 0x2, 0x4, 0x8 etc ?
Quoted 10 lines
arch/powerpc/include/asm/pkeys.h +1
-0+#define pkey_to_vmflag_bits (key) (((key & 0x1UL) ? VM_PKEY_BIT0 : 0x0UL) | \ ((key & 0x2UL) ? VM_PKEY_BIT1 : 0x0UL) | \ ((key & 0x4UL) ? VM_PKEY_BIT2 : 0x0UL) | \arch/powerpc/include/asm/reg.h +0
-0@@ -285,9 +285,10 @@#define DSISR_UNSUPP_MMU 0x00080000/* Unsupported MMU config */
arch/powerpc/include/asm/reg.h line 292
Quoted 10 lines
arch/powerpc/include/asm/reg.h +4
-3#define DSISR_SET_RC 0x00040000/* Failed setting of R/C bits */ #define DSISR_PGDIRFAULT 0x00020000/* Fault on page directory */ -#define DSISR_PAGE_FAULT_MASK (DSISR_BIT32 | \ - DSISR_PAGEATTR_CONFLT | \ - DSISR_BADACCESS | \ +#define DSISR_PAGE_FAULT_MASK (DSISR_BIT32 | \ + DSISR_PAGEATTR_CONFLT | \ + DSISR_BADACCESS | \ + DSISR_KEYFAULT | \ DSISR_BIT43)
This should have been cleaned up before adding new DSISR_KEYFAULT reason code into it. But I guess its okay.
Quoted 8 lines
arch/powerpc/include/asm/reg.h +0
-0#define SPRN_TBRL 0x10C/* Time Base Read Lower Register (user, R/O) */ #define SPRN_TBRU 0x10D/* Time Base Read Upper Register (user, R/O) */ arch/powerpc/mm/fault.c +0
-0@@ -216,9 +216,10 @@ int do_page_fault(struct pt_regs *regs, unsigned long address,* bits we are interested in. But there are some bits which
arch/powerpc/mm/fault.c int do_page_fault(struct pt_regs *regs, unsigned long address, · line 224
Quoted 10 lines
arch/powerpc/mm/fault.c +3
-2* indicate errors in DSISR but can validly be set in SRR1. */ -if (trap ==0x400 ) +if (trap ==0x400 ) { error_code &=0x48200000 ; -else + flags |=FAULT_FLAG_INSTRUCTION ; + }else is_write = error_code &DSISR_ISSTORE ;#else
Why adding the FAULT_FLAG_INSTRUCTION here ?
Quoted 18 lines
arch/powerpc/mm/fault.c +7
-0is_write = error_code &ESR_DST ;@@ -261,6 +262,13 @@ int do_page_fault(struct pt_regs *regs, unsigned long address, }#endif +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +if (error_code &DSISR_KEYFAULT ) { + code =SEGV_PKUERR ; + goto bad_area_nosemaphore; + } +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +/* We restore the interrupt state now */ if (!arch_irq_disabled_regs (regs))local_irq_enable ();@@ -441,6 +449,15 @@ int do_page_fault(struct pt_regs *regs, unsigned long address,WARN_ON_ONCE (error_code &DSISR_PROTFAULT );#endif /* CONFIG_PPC_STD_MMU */
arch/powerpc/mm/fault.c int do_page_fault(struct pt_regs *regs, unsigned long address, · line 460
Quoted 10 lines
arch/powerpc/mm/fault.c +9
-0+#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +if (!arch_vma_access_permitted (vma, flags &FAULT_FLAG_WRITE , + flags &FAULT_FLAG_INSTRUCTION , +0 )) { + code =SEGV_PKUERR ; + goto bad_area; + } +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +
I am wondering why both the above checks are required ?
* DSISR should contains DSISR_KEYFAULT
* VMA pkey values whether they matched the fault cause
Quoted 10 lines
arch/powerpc/mm/fault.c +0
-0/* * If for any reason at all we couldn't handle the fault, * make sure we exit gracefully rather than endlessly redo arch/powerpc/mm/pkeys.c +0
-0@@ -27,6 +27,37 @@ static inline bool pkey_allows_readwrite(int pkey)return !(read_amr () & ((AMR_AD_BIT |AMR_WD_BIT ) << pkey_shift)); }
arch/powerpc/mm/pkeys.c static inline bool pkey_allows_readwrite(int pkey) · line 38
Quoted 10 lines
arch/powerpc/mm/pkeys.c +9
-0+static inline bool pkey_allows_read (int pkey) +{ +int pkey_shift = (arch_max_pkey ()-pkey-1 ) *AMR_BITS_PER_PKEY ; + +if (!(read_uamor () & (0x3ul << pkey_shift))) +return true; + +return !(read_amr () & (AMR_AD_BIT << pkey_shift)); +}
Get read_amr() into a local variable and save some cycles if we have to do it again.
arch/powerpc/mm/pkeys.c static inline bool pkey_allows_readwrite(int pkey) · line 49
Quoted 11 lines
arch/powerpc/mm/pkeys.c +11
-0+ +static inline bool pkey_allows_write (int pkey) +{ +int pkey_shift = (arch_max_pkey ()-pkey-1 ) *AMR_BITS_PER_PKEY ; + +if (!(read_uamor () & (0x3ul << pkey_shift))) +return true; + +return !(read_amr () & (AMR_WD_BIT << pkey_shift)); +} +
Ditto
arch/powerpc/mm/pkeys.c static inline bool pkey_allows_readwrite(int pkey) · line 58
Quoted 9 lines
arch/powerpc/mm/pkeys.c +9
-0+static inline bool pkey_allows_execute (int pkey) +{ +int pkey_shift = (arch_max_pkey ()-pkey-1 ) *AMR_BITS_PER_PKEY ; + +if (!(read_uamor () & (0x3ul << pkey_shift))) +return true; + +return !(read_iamr () & (IAMR_EX_BIT << pkey_shift)); +}
Ditto
Quoted 34 lines
arch/powerpc/mm/pkeys.c +27
-0+ +/* * set the access right in AMR IAMR and UAMOR register * for @pkey to that specified in @init_val. @@ -175,3 +206,62 @@ int __arch_override_mprotect_pkey(struct vm_area_struct *vma, int prot,*/ return vma_pkey (vma); } + +bool arch_pte_access_permitted (pte_t pte,bool write) +{ +int pkey =pte_flags_to_pkey (pte_val (pte)); + +if (!pkey_allows_read (pkey)) +return false; +if (write && !pkey_allows_write (pkey)) +return false; +return true; +} + +/* +* We only want to enforce protection keys on the current process +* because we effectively have no access to AMR/IAMR for other +* processes or any way to tell *which * AMR/IAMR in a threaded +* process we could use. +* +* So do not enforce things if the VMA is not from the current +* mm, or if we are in a kernel thread. +*/ +static inline bool vma_is_foreign (struct vm_area_struct *vma) +{ +if (!current->mm) +return true;
arch/powerpc/mm/pkeys.c int __arch_override_mprotect_pkey(struct vm_area_struct *vma, int prot, · line 243
Quoted 10 lines
arch/powerpc/mm/pkeys.c +10
-0+/* +* if the VMA is from another process, then AMR/IAMR has no +* relevance and should not be enforced. +*/ +if (current->mm != vma->vm_mm) +return true; + +return false; +} +
This seems pretty generic, should not be moved to core MM ?
Re: [RFC v2 08/12] powerpc: Handle exceptions caused by violation of pkey protection.
Quoted 6 lines · Anshuman Khandual
Quoted 5 lines · Ram Pai
Handle Data and Instruction exceptions caused by memory protection-key.
Signed-off-by Ram Pai <[email protected]>(cherry picked from commit a5e5217619a0c475fe0cacc3b0cf1d3d33c79a09)
Sorry. it was residue of a bad cleanup. It got cherry-picked from my own internal branch, but than i forgot to delete that line.
Quoted 7 lines
To which tree this commit belongs to ?
Quoted 4 lines
Conflicts:
arch/powerpc/include/asm/reg.h
arch/powerpc/kernel/exceptions-64s.S
same here. these two line are some residues of patching-up my tree with commits from other internal branches.
Quoted 48 lines
Quoted 22 lines
--- arch/powerpc/include/asm/mmu_context.h | 12 +++++ arch/powerpc/include/asm/pkeys.h | 9 ++++ arch/powerpc/include/asm/reg.h | 7 +-- arch/powerpc/mm/fault.c | 21 +++++++- arch/powerpc/mm/pkeys.c | 90 ++++++++++++++++++++++++++++++++++ 5 files changed, 134 insertions(+), 5 deletions(-)arch/powerpc/include/asm/mmu_context.h +6
-0@@ -175,11 +175,23 @@ static inline void arch_bprm_mm_init(struct mm_struct *mm, { } +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +bool arch_pte_access_permitted (pte_t pte,bool write); +bool arch_vma_access_permitted (struct vm_area_struct *vma, +bool write,bool execute,bool foreign); +#else /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +static inline bool arch_pte_access_permitted (pte_t pte,bool write)arch/powerpc/include/asm/mmu_context.h static inline void arch_bprm_mm_init(struct mm_struct *mm, · line 193
Quoted 10 lines
arch/powerpc/include/asm/mmu_context.h +4
-0+{ +/* by default, allow everything */ +return true; +}static inline bool arch_vma_access_permitted (struct vm_area_struct *vma,bool write,bool execute,bool foreign) {/* by default, allow everything */ return true; }Right, these are the two functions the core VM expects the arch to provide.
Quoted 12 lines
arch/powerpc/include/asm/mmu_context.h +2
-0+#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +#endif /* __KERNEL__ */ #endif /* __ASM_POWERPC_MMU_CONTEXT_H */ arch/powerpc/include/asm/pkeys.h +0
-0@@ -14,6 +14,15 @@VM_PKEY_BIT3 | \ VM_PKEY_BIT4)
Quoted 10 lines
arch/powerpc/include/asm/pkeys.h line 24
Quoted 8 lines
arch/powerpc/include/asm/pkeys.h +8
-0+static inline u16 pte_flags_to_pkey (unsigned long pte_flags) +{ +return ((pte_flags &H_PAGE_PKEY_BIT4 ) ?0x1 :0x0 ) | + ((pte_flags &H_PAGE_PKEY_BIT3 ) ?0x2 :0x0 ) | + ((pte_flags &H_PAGE_PKEY_BIT2 ) ?0x4 :0x0 ) | + ((pte_flags &H_PAGE_PKEY_BIT1 ) ?0x8 :0x0 ) | + ((pte_flags &H_PAGE_PKEY_BIT0 ) ?0x10 :0x0 ); +}Add defines for the above
0x1,0x2,0x4,0x8etc ?
hmm...not sure if it will make the code any better.
Quoted 38 lines
Quoted 11 lines
arch/powerpc/include/asm/pkeys.h +1
-0+#define pkey_to_vmflag_bits (key) (((key & 0x1UL) ? VM_PKEY_BIT0 : 0x0UL) | \ ((key & 0x2UL) ? VM_PKEY_BIT1 : 0x0UL) | \ ((key & 0x4UL) ? VM_PKEY_BIT2 : 0x0UL) | \arch/powerpc/include/asm/reg.h +0
-0@@ -285,9 +285,10 @@#define DSISR_UNSUPP_MMU 0x00080000/* Unsupported MMU config */ arch/powerpc/include/asm/reg.h line 292
Quoted 10 lines
arch/powerpc/include/asm/reg.h +4
-3#define DSISR_SET_RC 0x00040000/* Failed setting of R/C bits */ #define DSISR_PGDIRFAULT 0x00020000/* Fault on page directory */ -#define DSISR_PAGE_FAULT_MASK (DSISR_BIT32 | \ - DSISR_PAGEATTR_CONFLT | \ - DSISR_BADACCESS | \ +#define DSISR_PAGE_FAULT_MASK (DSISR_BIT32 | \ + DSISR_PAGEATTR_CONFLT | \ + DSISR_BADACCESS | \ + DSISR_KEYFAULT | \ DSISR_BIT43)This should have been cleaned up before adding new
DSISR_KEYFAULTreason code into it. But I guess its okay.Quoted 12 lines
arch/powerpc/include/asm/reg.h +0
-0#define SPRN_TBRL 0x10C/* Time Base Read Lower Register (user, R/O) */ #define SPRN_TBRU 0x10D/* Time Base Read Upper Register (user, R/O) */ arch/powerpc/mm/fault.c +1
-1@@ -216,9 +216,10 @@ int do_page_fault(struct pt_regs *regs, unsigned long address,* bits we are interested in. But there are some bits which * indicate errors in DSISR but can validly be set in SRR1. */ -if (trap ==0x400 ) +if (trap ==0x400 ) {
Quoted 8 lines
arch/powerpc/mm/fault.c int do_page_fault(struct pt_regs *regs, unsigned long address, · line 224
Quoted 6 lines
arch/powerpc/mm/fault.c +2
-1error_code &=0x48200000 ; - else + flags |= FAULT_FLAG_INSTRUCTION; + }else is_write = error_code &DSISR_ISSTORE ;#else Why adding the
FAULT_FLAG_INSTRUCTIONhere ?
later in this code, this flag is checked to see if execute-protection was violated.
Quoted 20 lines
Quoted 20 lines
arch/powerpc/mm/fault.c +7
-0is_write = error_code &ESR_DST ;@@ -261,6 +262,13 @@ int do_page_fault(struct pt_regs *regs, unsigned long address, }#endif +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +if (error_code &DSISR_KEYFAULT ) { + code =SEGV_PKUERR ; + goto bad_area_nosemaphore; + } +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +/* We restore the interrupt state now */ if (!arch_irq_disabled_regs (regs))local_irq_enable ();@@ -441,6 +449,15 @@ int do_page_fault(struct pt_regs *regs, unsigned long address,WARN_ON_ONCE (error_code &DSISR_PROTFAULT );#endif /* CONFIG_PPC_STD_MMU */
Quoted 11 lines
arch/powerpc/mm/fault.c int do_page_fault(struct pt_regs *regs, unsigned long address, · line 460
Quoted 9 lines
arch/powerpc/mm/fault.c +9
-0+#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +if (!arch_vma_access_permitted (vma, flags &FAULT_FLAG_WRITE , + flags &FAULT_FLAG_INSTRUCTION , +0 )) { + code =SEGV_PKUERR ; + goto bad_area; + } +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +I am wondering why both the above checks are required ?
Yes good question. there are two cases here.
a) when a hpte is not yet hashed to pte.
in this case the fault is because the hpte is not yet mapped. However the access may have also violated the protection permissions of the key associated with that address. So we need to a software check to determine if a key was violated.
if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE,...
handles this case.
b) when the hpte is hashed to the pte and keys are programmed into the hpte.
in this case the hardware senses the key protection fault and we just have to check if that is the case.
if (error_code & DSISR_KEYFAULT) {....
handles this case.
Quoted 17 lines
* DSISR should contains
DSISR_KEYFAULT* VMA pkey values whether they matched the fault cause
Quoted 11 lines
arch/powerpc/mm/fault.c +0
-0/* * If for any reason at all we couldn't handle the fault, * make sure we exit gracefully rather than endlessly redo arch/powerpc/mm/pkeys.c +0
-0@@ -27,6 +27,37 @@ static inline bool pkey_allows_readwrite(int pkey)return !(read_amr () & ((AMR_AD_BIT |AMR_WD_BIT ) << pkey_shift)); }
Quoted 12 lines
arch/powerpc/mm/pkeys.c static inline bool pkey_allows_readwrite(int pkey) · line 38
Quoted 9 lines
arch/powerpc/mm/pkeys.c +9
-0+static inline bool pkey_allows_read (int pkey) +{ +int pkey_shift = (arch_max_pkey ()-pkey-1 ) *AMR_BITS_PER_PKEY ; + +if (!(read_uamor () & (0x3ul << pkey_shift))) +return true; + +return !(read_amr () & (AMR_AD_BIT << pkey_shift)); +}Get
read_amr()into a local variable and save some cycles if we have to do it again.
No. not really. the AMR can be changed by the process in userspace. So anything that we cache can go stale.
Or maybe i do not understand your comment.
RP.
Re: [RFC v2 08/12] powerpc: Handle exceptions caused by violation of pkey protection.
Quoted 130 lines · Ram Pai
Quoted 6 lines · Anshuman Khandual
Quoted 5 lines · Ram Pai
Handle Data and Instruction exceptions caused by memory protection-key.
Signed-off-by Ram Pai <[email protected]>(cherry picked from commit a5e5217619a0c475fe0cacc3b0cf1d3d33c79a09)Sorry. it was residue of a bad cleanup. It got cherry-picked from my own internal branch, but than i forgot to delete that line.
Quoted 7 lines
To which tree this commit belongs to ?
Quoted 4 lines
Conflicts:
arch/powerpc/include/asm/reg.h
arch/powerpc/kernel/exceptions-64s.Ssame here. these two line are some residues of patching-up my tree with commits from other internal branches.
Quoted 48 lines
Quoted 22 lines
--- arch/powerpc/include/asm/mmu_context.h | 12 +++++ arch/powerpc/include/asm/pkeys.h | 9 ++++ arch/powerpc/include/asm/reg.h | 7 +-- arch/powerpc/mm/fault.c | 21 +++++++- arch/powerpc/mm/pkeys.c | 90 ++++++++++++++++++++++++++++++++++ 5 files changed, 134 insertions(+), 5 deletions(-)arch/powerpc/include/asm/mmu_context.h +6
-0@@ -175,11 +175,23 @@ static inline void arch_bprm_mm_init(struct mm_struct *mm, { } +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +bool arch_pte_access_permitted (pte_t pte,bool write); +bool arch_vma_access_permitted (struct vm_area_struct *vma, +bool write,bool execute,bool foreign); +#else /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +static inline bool arch_pte_access_permitted (pte_t pte,bool write)arch/powerpc/include/asm/mmu_context.h static inline void arch_bprm_mm_init(struct mm_struct *mm, · line 193
Quoted 10 lines
arch/powerpc/include/asm/mmu_context.h +4
-0+{ +/* by default, allow everything */ +return true; +}static inline bool arch_vma_access_permitted (struct vm_area_struct *vma,bool write,bool execute,bool foreign) {/* by default, allow everything */ return true; }Right, these are the two functions the core VM expects the arch to provide.
Quoted 12 lines
arch/powerpc/include/asm/mmu_context.h +2
-0+#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +#endif /* __KERNEL__ */ #endif /* __ASM_POWERPC_MMU_CONTEXT_H */ arch/powerpc/include/asm/pkeys.h +0
-0@@ -14,6 +14,15 @@VM_PKEY_BIT3 | \ VM_PKEY_BIT4)Quoted 10 lines
arch/powerpc/include/asm/pkeys.h line 24
Quoted 8 lines
arch/powerpc/include/asm/pkeys.h +8
-0+static inline u16 pte_flags_to_pkey (unsigned long pte_flags) +{ +return ((pte_flags &H_PAGE_PKEY_BIT4 ) ?0x1 :0x0 ) | + ((pte_flags &H_PAGE_PKEY_BIT3 ) ?0x2 :0x0 ) | + ((pte_flags &H_PAGE_PKEY_BIT2 ) ?0x4 :0x0 ) | + ((pte_flags &H_PAGE_PKEY_BIT1 ) ?0x8 :0x0 ) | + ((pte_flags &H_PAGE_PKEY_BIT0 ) ?0x10 :0x0 ); +}Add defines for the above
0x1,0x2,0x4,0x8etc ?hmm...not sure if it will make the code any better.
Quoted 38 lines
Quoted 11 lines
arch/powerpc/include/asm/pkeys.h +1
-0+#define pkey_to_vmflag_bits (key) (((key & 0x1UL) ? VM_PKEY_BIT0 : 0x0UL) | \ ((key & 0x2UL) ? VM_PKEY_BIT1 : 0x0UL) | \ ((key & 0x4UL) ? VM_PKEY_BIT2 : 0x0UL) | \arch/powerpc/include/asm/reg.h +0
-0@@ -285,9 +285,10 @@#define DSISR_UNSUPP_MMU 0x00080000/* Unsupported MMU config */ arch/powerpc/include/asm/reg.h line 292
Quoted 10 lines
arch/powerpc/include/asm/reg.h +4
-3#define DSISR_SET_RC 0x00040000/* Failed setting of R/C bits */ #define DSISR_PGDIRFAULT 0x00020000/* Fault on page directory */ -#define DSISR_PAGE_FAULT_MASK (DSISR_BIT32 | \ - DSISR_PAGEATTR_CONFLT | \ - DSISR_BADACCESS | \ +#define DSISR_PAGE_FAULT_MASK (DSISR_BIT32 | \ + DSISR_PAGEATTR_CONFLT | \ + DSISR_BADACCESS | \ + DSISR_KEYFAULT | \ DSISR_BIT43)This should have been cleaned up before adding new
DSISR_KEYFAULTreason code into it. But I guess its okay.Quoted 12 lines
arch/powerpc/include/asm/reg.h +0
-0#define SPRN_TBRL 0x10C/* Time Base Read Lower Register (user, R/O) */ #define SPRN_TBRU 0x10D/* Time Base Read Upper Register (user, R/O) */ arch/powerpc/mm/fault.c +1
-1@@ -216,9 +216,10 @@ int do_page_fault(struct pt_regs *regs, unsigned long address,* bits we are interested in. But there are some bits which * indicate errors in DSISR but can validly be set in SRR1. */ -if (trap ==0x400 ) +if (trap ==0x400 ) {Quoted 8 lines
arch/powerpc/mm/fault.c int do_page_fault(struct pt_regs *regs, unsigned long address, · line 224
Quoted 6 lines
arch/powerpc/mm/fault.c +2
-1error_code &=0x48200000 ; - else + flags |= FAULT_FLAG_INSTRUCTION; + }else is_write = error_code &DSISR_ISSTORE ;#else Why adding the
FAULT_FLAG_INSTRUCTIONhere ?
Quoted 2 lines
later in this code, this flag is checked to see if execute-protection was violated.
'is_exec' which is set for every 400 interrupt can be used for that purpose ? I guess thats how we have been dealing with generic PROT_EXEC based faults.
Quoted 32 lines
Quoted 20 lines
Quoted 20 lines
arch/powerpc/mm/fault.c +7
-0is_write = error_code &ESR_DST ;@@ -261,6 +262,13 @@ int do_page_fault(struct pt_regs *regs, unsigned long address, }#endif +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +if (error_code &DSISR_KEYFAULT ) { + code =SEGV_PKUERR ; + goto bad_area_nosemaphore; + } +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +/* We restore the interrupt state now */ if (!arch_irq_disabled_regs (regs))local_irq_enable ();@@ -441,6 +449,15 @@ int do_page_fault(struct pt_regs *regs, unsigned long address,WARN_ON_ONCE (error_code &DSISR_PROTFAULT );#endif /* CONFIG_PPC_STD_MMU */ Quoted 11 lines
arch/powerpc/mm/fault.c int do_page_fault(struct pt_regs *regs, unsigned long address, · line 460
Quoted 9 lines
arch/powerpc/mm/fault.c +9
-0+#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +if (!arch_vma_access_permitted (vma, flags &FAULT_FLAG_WRITE , + flags &FAULT_FLAG_INSTRUCTION , +0 )) { + code =SEGV_PKUERR ; + goto bad_area; + } +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +I am wondering why both the above checks are required ?
Quoted 7 lines
Yes good question. there are two cases here.
a) when a hpte is not yet hashed to pte.
in this case the fault is because the hpte is not yet mapped. However the access may have also violated the protection permissions of the key associated with that address. So we need
Both of these cannot be possible simultaneously. In this case MMU will take a fault because of no HPTE is found for the access not for the protection key irrespective of the pkey value and type of the access. Are you saying that DSISR might have both DSISR_NOHPTE and DSISR_KEYFAULT set for this case ? If not its not a good idea to present SEGV_PKUERR as reason code during signal delivery.
Quoted 10 lines
to a software check to determine if a key was violated.
if (!arch_vma_access_permitted(vma, flags &
FAULT_FLAG_WRITE,...handles this case.
b) when the hpte is hashed to the pte and keys are programmed into the hpte.
Quoted 6 lines
in this case the hardware senses the key protection fault and we just have to check if that is the case.
if (
error_code&DSISR_KEYFAULT) {....handles this case.
This is correct.
Quoted 32 lines
Quoted 19 lines
* DSISR should contains
DSISR_KEYFAULT* VMA pkey values whether they matched the fault cause
Quoted 11 lines
arch/powerpc/mm/fault.c +0
-0/* * If for any reason at all we couldn't handle the fault, * make sure we exit gracefully rather than endlessly redo arch/powerpc/mm/pkeys.c +0
-0@@ -27,6 +27,37 @@ static inline bool pkey_allows_readwrite(int pkey)return !(read_amr () & ((AMR_AD_BIT |AMR_WD_BIT ) << pkey_shift)); }Quoted 12 lines
arch/powerpc/mm/pkeys.c static inline bool pkey_allows_readwrite(int pkey) · line 38
Quoted 9 lines
arch/powerpc/mm/pkeys.c +9
-0+static inline bool pkey_allows_read (int pkey) +{ +int pkey_shift = (arch_max_pkey ()-pkey-1 ) *AMR_BITS_PER_PKEY ; + +if (!(read_uamor () & (0x3ul << pkey_shift))) +return true; + +return !(read_amr () & (AMR_AD_BIT << pkey_shift)); +}Get
read_amr()into a local variable and save some cycles if we have to do it again.
Quoted 3 lines
No. not really. the AMR can be changed by the process in userspace. So anything that we cache can go stale.
Or maybe i do not understand your comment.
I am not saying to cache the value. Just inside the function, if we have a local variable holding read_amr() once, it can be used twice without reading the register again. Just inside the function.
Re: [RFC v2 08/12] powerpc: Handle exceptions caused by violation of pkey protection.
Quoted 131 lines · Anshuman Khandual
Quoted 130 lines · Ram Pai
Quoted 6 lines · Anshuman Khandual
Quoted 5 lines · Ram Pai
Handle Data and Instruction exceptions caused by memory protection-key.
Signed-off-by Ram Pai <[email protected]>(cherry picked from commit a5e5217619a0c475fe0cacc3b0cf1d3d33c79a09)Sorry. it was residue of a bad cleanup. It got cherry-picked from my own internal branch, but than i forgot to delete that line.
Quoted 7 lines
To which tree this commit belongs to ?
Quoted 4 lines
Conflicts:
arch/powerpc/include/asm/reg.h
arch/powerpc/kernel/exceptions-64s.Ssame here. these two line are some residues of patching-up my tree with commits from other internal branches.
Quoted 48 lines
Quoted 22 lines
--- arch/powerpc/include/asm/mmu_context.h | 12 +++++ arch/powerpc/include/asm/pkeys.h | 9 ++++ arch/powerpc/include/asm/reg.h | 7 +-- arch/powerpc/mm/fault.c | 21 +++++++- arch/powerpc/mm/pkeys.c | 90 ++++++++++++++++++++++++++++++++++ 5 files changed, 134 insertions(+), 5 deletions(-)arch/powerpc/include/asm/mmu_context.h +6
-0@@ -175,11 +175,23 @@ static inline void arch_bprm_mm_init(struct mm_struct *mm, { } +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +bool arch_pte_access_permitted (pte_t pte,bool write); +bool arch_vma_access_permitted (struct vm_area_struct *vma, +bool write,bool execute,bool foreign); +#else /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +static inline bool arch_pte_access_permitted (pte_t pte,bool write)arch/powerpc/include/asm/mmu_context.h static inline void arch_bprm_mm_init(struct mm_struct *mm, · line 193
Quoted 10 lines
arch/powerpc/include/asm/mmu_context.h +4
-0+{ +/* by default, allow everything */ +return true; +}static inline bool arch_vma_access_permitted (struct vm_area_struct *vma,bool write,bool execute,bool foreign) {/* by default, allow everything */ return true; }Right, these are the two functions the core VM expects the arch to provide.
Quoted 12 lines
arch/powerpc/include/asm/mmu_context.h +2
-0+#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +#endif /* __KERNEL__ */ #endif /* __ASM_POWERPC_MMU_CONTEXT_H */ arch/powerpc/include/asm/pkeys.h +0
-0@@ -14,6 +14,15 @@VM_PKEY_BIT3 | \ VM_PKEY_BIT4)Quoted 10 lines
arch/powerpc/include/asm/pkeys.h line 24
Quoted 8 lines
arch/powerpc/include/asm/pkeys.h +8
-0+static inline u16 pte_flags_to_pkey (unsigned long pte_flags) +{ +return ((pte_flags &H_PAGE_PKEY_BIT4 ) ?0x1 :0x0 ) | + ((pte_flags &H_PAGE_PKEY_BIT3 ) ?0x2 :0x0 ) | + ((pte_flags &H_PAGE_PKEY_BIT2 ) ?0x4 :0x0 ) | + ((pte_flags &H_PAGE_PKEY_BIT1 ) ?0x8 :0x0 ) | + ((pte_flags &H_PAGE_PKEY_BIT0 ) ?0x10 :0x0 ); +}Add defines for the above
0x1,0x2,0x4,0x8etc ?hmm...not sure if it will make the code any better.
Quoted 46 lines
Quoted 11 lines
arch/powerpc/include/asm/pkeys.h +1
-0+#define pkey_to_vmflag_bits (key) (((key & 0x1UL) ? VM_PKEY_BIT0 : 0x0UL) | \ ((key & 0x2UL) ? VM_PKEY_BIT1 : 0x0UL) | \ ((key & 0x4UL) ? VM_PKEY_BIT2 : 0x0UL) | \arch/powerpc/include/asm/reg.h +0
-0@@ -285,9 +285,10 @@#define DSISR_UNSUPP_MMU 0x00080000/* Unsupported MMU config */ arch/powerpc/include/asm/reg.h line 292
Quoted 10 lines
arch/powerpc/include/asm/reg.h +4
-3#define DSISR_SET_RC 0x00040000/* Failed setting of R/C bits */ #define DSISR_PGDIRFAULT 0x00020000/* Fault on page directory */ -#define DSISR_PAGE_FAULT_MASK (DSISR_BIT32 | \ - DSISR_PAGEATTR_CONFLT | \ - DSISR_BADACCESS | \ +#define DSISR_PAGE_FAULT_MASK (DSISR_BIT32 | \ + DSISR_PAGEATTR_CONFLT | \ + DSISR_BADACCESS | \ + DSISR_KEYFAULT | \ DSISR_BIT43)This should have been cleaned up before adding new
DSISR_KEYFAULTreason code into it. But I guess its okay.Quoted 8 lines
arch/powerpc/include/asm/reg.h +0
-0#define SPRN_TBRL 0x10C/* Time Base Read Lower Register (user, R/O) */ #define SPRN_TBRU 0x10D/* Time Base Read Upper Register (user, R/O) */ arch/powerpc/mm/fault.c +0
-0@@ -216,9 +216,10 @@ int do_page_fault(struct pt_regs *regs, unsigned long address,* bits we are interested in. But there are some bits which arch/powerpc/mm/fault.c int do_page_fault(struct pt_regs *regs, unsigned long address, · line 224
Quoted 10 lines
arch/powerpc/mm/fault.c +3
-2* indicate errors in DSISR but can validly be set in SRR1. */ -if (trap ==0x400 ) +if (trap ==0x400 ) { error_code &=0x48200000 ; -else + flags |=FAULT_FLAG_INSTRUCTION ; + }else is_write = error_code &DSISR_ISSTORE ;#else Why adding the
FAULT_FLAG_INSTRUCTIONhere ?
Quoted 7 lines
Quoted 2 lines
later in this code, this flag is checked to see if execute-protection was violated.
'
is_exec' which is set for every 400 interrupt can be used for that purpose ? I guess thats how we have been dealing with genericPROT_EXECbased faults.
This is right. Thanks for pointing it out. Yes 'is_exec' is sufficient to achieve the purpose.
Quoted 41 lines
Quoted 33 lines
Quoted 21 lines
Quoted 21 lines
arch/powerpc/mm/fault.c +7
-0is_write = error_code &ESR_DST ;@@ -261,6 +262,13 @@ int do_page_fault(struct pt_regs *regs, unsigned long address, }#endif +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +if (error_code &DSISR_KEYFAULT ) { + code =SEGV_PKUERR ; + goto bad_area_nosemaphore; + } +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +/* We restore the interrupt state now */ if (!arch_irq_disabled_regs (regs))arch/powerpc/mm/fault.c +0
-0local_irq_enable ();@@ -441,6 +449,15 @@ int do_page_fault(struct pt_regs *regs, unsigned long address,WARN_ON_ONCE (error_code &DSISR_PROTFAULT );#endif /* CONFIG_PPC_STD_MMU */ Quoted 11 lines
arch/powerpc/mm/fault.c int do_page_fault(struct pt_regs *regs, unsigned long address, · line 460
Quoted 9 lines
arch/powerpc/mm/fault.c +9
-0+#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +if (!arch_vma_access_permitted (vma, flags &FAULT_FLAG_WRITE , + flags &FAULT_FLAG_INSTRUCTION , +0 )) { + code =SEGV_PKUERR ; + goto bad_area; + } +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +I am wondering why both the above checks are required ?
Quoted 7 lines
Yes good question. there are two cases here.
a) when a hpte is not yet hashed to pte.
in this case the fault is because the hpte is not yet mapped. However the access may have also violated the protection permissions of the key associated with that address. So we need
Quoted 6 lines
Both of these cannot be possible simultaneously. In this case MMU will take a fault because of no HPTE is found for the access not for the protection key irrespective of the pkey value and type of the access. Are you saying that DSISR might have both
DSISR_NOHPTEandDSISR_KEYFAULTset for this case ? If not its not a good idea to presentSEGV_PKUERRas reason code during signal delivery.
Both DSISR_NOHPTE and DSISR_KEYFAULT may not be set simultaneously. A HPTE needs to exist before a key can be programmed into it.
However its still a key violation, if the fault was a DSISR_NOHPTE, and the faulting address has a key in the vma that is violated. There is a violation, it needs to be reported as SEG_PKUERR. The hardware may not have detected it, but software is still responsible for detecting and reporting it.
Quoted 52 lines
Quoted 11 lines
to a software check to determine if a key was violated.
if (!arch_vma_access_permitted(vma, flags &
FAULT_FLAG_WRITE,...handles this case.
b) when the hpte is hashed to the pte and keys are programmed into the hpte.
Quoted 6 lines
in this case the hardware senses the key protection fault and we just have to check if that is the case.
if (
error_code&DSISR_KEYFAULT) {....handles this case.
This is correct.
Quoted 32 lines
Quoted 31 lines
* DSISR should contains
DSISR_KEYFAULT* VMA pkey values whether they matched the fault cause
Quoted 10 lines
arch/powerpc/mm/fault.c +0
-0/* * If for any reason at all we couldn't handle the fault, * make sure we exit gracefully rather than endlessly redo arch/powerpc/mm/pkeys.c +0
-0@@ -27,6 +27,37 @@ static inline bool pkey_allows_readwrite(int pkey)return !(read_amr () & ((AMR_AD_BIT |AMR_WD_BIT ) << pkey_shift)); }arch/powerpc/mm/pkeys.c static inline bool pkey_allows_readwrite(int pkey) · line 38
Quoted 10 lines
arch/powerpc/mm/pkeys.c +9
-0+static inline bool pkey_allows_read (int pkey) +{ +int pkey_shift = (arch_max_pkey ()-pkey-1 ) *AMR_BITS_PER_PKEY ; + +if (!(read_uamor () & (0x3ul << pkey_shift))) +return true; + +return !(read_amr () & (AMR_AD_BIT << pkey_shift)); +}Get
read_amr()into a local variable and save some cycles if we have to do it again.
Quoted 7 lines
Quoted 3 lines
No. not really. the AMR can be changed by the process in userspace. So anything that we cache can go stale.
Or maybe i do not understand your comment.I am not saying to cache the value. Just inside the function, if we have a local variable holding
read_amr()once, it can be used twice without reading the register again. Just inside the function.
O!. May be you are reading read_uamor() and read_amr() as the same thing? They are two different registers.
RP
Signature · 1 line
Ram Pai
RFC v2 9/12 powerpc: Deliver SEGV signal on pkey violation.
In reply to Ram PaiThe value of the AMR register at the time of exception is made available in gp_regs[PT_AMR] of the siginfo.
This field can be used to reprogram the permission bits of any valid pkey.
Similarly the value of the pkey, whose protection got violated, is made available at si_pkey field of the siginfo structure.
--- arch/powerpc/include/asm/paca.h | 1 + arch/powerpc/include/uapi/asm/ptrace.h | 3 ++- arch/powerpc/kernel/asm-offsets.c | 5 ++++ arch/powerpc/kernel/exceptions-64s.S | 8 ++++++ arch/powerpc/kernel/signal_32.c | 14 ++++++++++ arch/powerpc/kernel/signal_64.c | 14 ++++++++++ arch/powerpc/kernel/traps.c | 49 ++++++++++++++++++++++++++++++++++ arch/powerpc/mm/fault.c | 4 +++ 8 files changed, 97 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/paca.h +1 -0
@@ -92,6 +92,7 @@ struct paca_struct {struct dtl_entry *dispatch_log_end;#endif /* CONFIG_PPC_STD_MMU_64 */ u64 dscr_default;/* per-CPU default DSCR */ +u64 paca_amr;/* value of amr at exception */ #ifdef CONFIG_PPC_STD_MMU_64 /*
arch/powerpc/include/uapi/asm/ptrace.h +2 -1
@@ -108,8 +108,9 @@ struct pt_regs {#define PT_DAR 41#define PT_DSISR 42#define PT_RESULT 43 -#define PT_DSCR 44#define PT_REGS_COUNT 44 +#define PT_DSCR 44 +#define PT_AMR 45#define PT_FPR0 48/* each FP reg occupies 2 slots in this space */
arch/powerpc/kernel/asm-offsets.c +5 -0
@@ -241,6 +241,11 @@ int main(void)OFFSET (PACAHWCPUID , paca_struct, hw_cpu_id);OFFSET (PACAKEXECSTATE , paca_struct, kexec_state);OFFSET (PACA_DSCR_DEFAULT , paca_struct, dscr_default); + +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +OFFSET (PACA_AMR , paca_struct, paca_amr); +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +OFFSET (ACCOUNT_STARTTIME , paca_struct, accounting.starttime);OFFSET (ACCOUNT_STARTTIME_USER , paca_struct, accounting.starttime_user);OFFSET (ACCOUNT_USER_TIME , paca_struct, accounting.utime);
arch/powerpc/kernel/exceptions-64s.S +8 -0
@@ -493,6 +493,10 @@ EXC_COMMON_BEGIN(data_access_common) ld r12,_MSR(r1) ld r3,PACA_EXGEN+EX_DAR(r13) lwz r4,PACA_EXGEN+EX_DSISR(r13) +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS + mfspr r5,SPRN_AMR + std r5,PACA_AMR(r13) +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ li r5,0x300 std r3,_DAR(r1) std r4,_DSISR(r1)
@@ -561,6 +565,10 @@ EXC_COMMON_BEGIN(instruction_access_common) ld r12,_MSR(r1) ld r3,_NIP(r1) andis. r4,r12,0x5820 +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS + mfspr r5,SPRN_AMR + std r5,PACA_AMR(r13) +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ li r5,0x400 std r3,_DAR(r1) std r4,_DSISR(r1)
arch/powerpc/kernel/signal_32.c +14 -0
@@ -500,6 +500,11 @@ static int save_user_regs(struct pt_regs *regs, struct mcontext __user *frame, (unsigned long) &frame->tramp[2]); } +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +if (__put_user (get_paca ()->paca_amr, &frame->mc_gregs[PT_AMR ])) +return 1 ; +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +return 0 ; }
@@ -661,6 +666,9 @@ static long restore_user_regs(struct pt_regs *regs,long err;unsigned int save_r2 =0 ;unsigned long msr; +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +unsigned long amr; +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ #ifdef CONFIG_VSX int i;#endif
@@ -750,6 +758,12 @@ static long restore_user_regs(struct pt_regs *regs,return 1 ;#endif /* CONFIG_SPE */ +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS + err |=__get_user (amr, &sr->mc_gregs[PT_AMR ]); +if (!err && amr !=get_paca ()->paca_amr) +write_amr (amr); +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +return 0 ; }
arch/powerpc/kernel/signal_64.c +14 -0
@@ -174,6 +174,10 @@ static long setup_sigcontext(struct sigcontext __user *sc,if (set !=NULL ) err |=__put_user (set->sig[0 ], &sc->oldmask); +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS + err |=__put_user (get_paca ()->paca_amr, &sc->gp_regs[PT_AMR ]); +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +return err; }
@@ -327,6 +331,9 @@ static long restore_sigcontext(struct task_struct *tsk, sigset_t *set, int sig,unsigned long save_r13 =0 ;unsigned long msr;struct pt_regs *regs = tsk->thread.regs; +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +unsigned long amr; +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ #ifdef CONFIG_VSX int i;#endif
@@ -406,6 +413,13 @@ static long restore_sigcontext(struct task_struct *tsk, sigset_t *set, int sig, tsk->thread.fp_state.fpr[i][TS_VSRLOWOFFSET] = 0; }#endif + +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS + err |=__get_user (amr, &sc->gp_regs[PT_AMR ]); +if (!err && amr !=get_paca ()->paca_amr) +write_amr (amr); +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +return err; }
arch/powerpc/kernel/traps.c +49 -0
67 lines not on this page · raw message
arch/powerpc/mm/fault.c +4 -0
@@ -453,6 +453,10 @@ int do_page_fault(struct pt_regs *regs, unsigned long address,if (!arch_vma_access_permitted (vma, flags &FAULT_FLAG_WRITE , flags &FAULT_FLAG_INSTRUCTION ,0 )) { + +/* our caller may not have saved the amr. Lets save it */ +get_paca ()->paca_amr =read_amr (); + code =SEGV_PKUERR ; goto bad_area; }
Signature · 1 line
1.8.3.1
Re: [RFC v2 09/12] powerpc: Deliver SEGV signal on pkey violation.
Quoted 38 lines · Ram Pai
The value of the AMR register at the time of exception is made available in gp_regs[PT_AMR] of the siginfo.
This field can be used to reprogram the permission bits of any valid pkey.
Similarly the value of the pkey, whose protection got violated, is made available at
si_pkeyfield of the siginfo structure.Signed-off-by Ram Pai <[email protected]>--- arch/powerpc/include/asm/paca.h | 1 + arch/powerpc/include/uapi/asm/ptrace.h | 3 ++- arch/powerpc/kernel/asm-offsets.c | 5 ++++ arch/powerpc/kernel/exceptions-64s.S | 8 ++++++ arch/powerpc/kernel/signal_32.c | 14 ++++++++++ arch/powerpc/kernel/signal_64.c | 14 ++++++++++ arch/powerpc/kernel/traps.c | 49 ++++++++++++++++++++++++++++++++++ arch/powerpc/mm/fault.c | 4 +++ 8 files changed, 97 insertions(+), 1 deletion(-)arch/powerpc/include/asm/paca.h +1
-0@@ -92,6 +92,7 @@ struct paca_struct {struct dtl_entry *dispatch_log_end;#endif /* CONFIG_PPC_STD_MMU_64 */ u64 dscr_default;/* per-CPU default DSCR */ +u64 paca_amr;/* value of amr at exception */ #ifdef CONFIG_PPC_STD_MMU_64 /* arch/powerpc/include/uapi/asm/ptrace.h +0
-0
arch/powerpc/include/uapi/asm/ptrace.h struct pt_regs { · line 113
Quoted 7 lines
arch/powerpc/include/uapi/asm/ptrace.h +2
-1@@ -108,8 +108,9 @@ struct pt_regs {#define PT_DAR 41#define PT_DSISR 42#define PT_RESULT 43 -#define PT_DSCR 44#define PT_REGS_COUNT 44 +#define PT_DSCR 44 +#define PT_AMR 45
PT_REGS_COUNT is not getting incremented even after adding one more element into the pack ?
Quoted 8 lines
arch/powerpc/include/uapi/asm/ptrace.h +0
-0#define PT_FPR0 48/* each FP reg occupies 2 slots in this space */ arch/powerpc/kernel/asm-offsets.c +0
-0
arch/powerpc/kernel/asm-offsets.c int main(void) · line 248
Quoted 8 lines
arch/powerpc/kernel/asm-offsets.c +5
-0@@ -241,6 +241,11 @@ int main(void)OFFSET (PACAHWCPUID , paca_struct, hw_cpu_id);OFFSET (PACAKEXECSTATE , paca_struct, kexec_state);OFFSET (PACA_DSCR_DEFAULT , paca_struct, dscr_default); + +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +OFFSET (PACA_AMR , paca_struct, paca_amr); +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +
So we now have a place in PACA for AMR.
Quoted 19 lines
arch/powerpc/kernel/asm-offsets.c +0
-0OFFSET (ACCOUNT_STARTTIME , paca_struct, accounting.starttime);OFFSET (ACCOUNT_STARTTIME_USER , paca_struct, accounting.starttime_user);OFFSET (ACCOUNT_USER_TIME , paca_struct, accounting.utime);arch/powerpc/kernel/exceptions-64s.S +4
-0@@ -493,6 +493,10 @@ EXC_COMMON_BEGIN(data_access_common) ld r12,_MSR(r1) ld r3,PACA_EXGEN+EX_DAR(r13) lwz r4,PACA_EXGEN+EX_DSISR(r13) +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS + mfspr r5,SPRN_AMR + std r5,PACA_AMR(r13) +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ li r5,0x300 std r3,_DAR(r1) std r4,_DSISR(r1)
arch/powerpc/kernel/exceptions-64s.S EXC_COMMON_BEGIN(instruction_access_common) · line 571
Quoted 7 lines
arch/powerpc/kernel/exceptions-64s.S +4
-0@@ -561,6 +565,10 @@ EXC_COMMON_BEGIN(instruction_access_common) ld r12,_MSR(r1) ld r3,_NIP(r1) andis. r4,r12,0x5820 +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS + mfspr r5,SPRN_AMR + std r5,PACA_AMR(r13) +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */
Saving the AMR context on page faults, this seems to be changing in the next patch again based on whether any key was active at that point and fault happened for the key enforcement ?
Quoted 95 lines
arch/powerpc/kernel/exceptions-64s.S +0
-0li r5,0x400 std r3,_DAR(r1) std r4,_DSISR(r1)arch/powerpc/kernel/signal_32.c +14
-0@@ -500,6 +500,11 @@ static int save_user_regs(struct pt_regs *regs, struct mcontext __user *frame, (unsigned long) &frame->tramp[2]); } +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +if (__put_user (get_paca ()->paca_amr, &frame->mc_gregs[PT_AMR ])) +return 1 ; +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +return 0 ; }@@ -661,6 +666,9 @@ static long restore_user_regs(struct pt_regs *regs,long err;unsigned int save_r2 =0 ;unsigned long msr; +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +unsigned long amr; +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ #ifdef CONFIG_VSX int i;#endif @@ -750,6 +758,12 @@ static long restore_user_regs(struct pt_regs *regs,return 1 ;#endif /* CONFIG_SPE */ +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS + err |=__get_user (amr, &sr->mc_gregs[PT_AMR ]); +if (!err && amr !=get_paca ()->paca_amr) +write_amr (amr); +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +return 0 ; }arch/powerpc/kernel/signal_64.c +14
-0@@ -174,6 +174,10 @@ static long setup_sigcontext(struct sigcontext __user *sc,if (set !=NULL ) err |=__put_user (set->sig[0 ], &sc->oldmask); +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS + err |=__put_user (get_paca ()->paca_amr, &sc->gp_regs[PT_AMR ]); +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +return err; }@@ -327,6 +331,9 @@ static long restore_sigcontext(struct task_struct *tsk, sigset_t *set, int sig,unsigned long save_r13 =0 ;unsigned long msr;struct pt_regs *regs = tsk->thread.regs; +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +unsigned long amr; +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ #ifdef CONFIG_VSX int i;#endif @@ -406,6 +413,13 @@ static long restore_sigcontext(struct task_struct *tsk, sigset_t *set, int sig, tsk->thread.fp_state.fpr[i][TS_VSRLOWOFFSET] = 0; }#endif + +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS + err |=__get_user (amr, &sc->gp_regs[PT_AMR ]); +if (!err && amr !=get_paca ()->paca_amr) +write_amr (amr); +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +return err; }arch/powerpc/kernel/traps.c +1
-0@@ -20,6 +20,7 @@#include <linux/sched/debug.h> #include <linux/kernel.h> #include <linux/mm.h> +#include <linux/pkeys.h> #include <linux/stddef.h> #include <linux/unistd.h> #include <linux/ptrace.h> @@ -247,6 +248,49 @@ void user_single_step_siginfo(struct task_struct *tsk, info->si_addr = (void __user *)regs->nip;
arch/powerpc/kernel/traps.c void user_single_step_siginfo(struct task_struct *tsk, · line 258
Quoted 10 lines
arch/powerpc/kernel/traps.c +8
-0} +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +static void fill_sig_info_pkey (int si_code,siginfo_t *info,unsigned long addr) +{ +struct vm_area_struct *vma; + +/* Fault not from Protection Keys: nothing to do */ +if (si_code !=SEGV_PKUERR ) +return ;
Should have checked this in the caller ?
Quoted 7 lines
arch/powerpc/kernel/traps.c +7
-0+ +down_read (¤t->mm->mmap_sem); +/* +* we could be racing with pkey_mprotect(). +* If pkey_mprotect() wins the key value could +* get modified...xxx +*/
arch/powerpc/kernel/traps.c void user_single_step_siginfo(struct task_struct *tsk, · line 275
Quoted 10 lines
arch/powerpc/kernel/traps.c +10
-0+ vma =find_vma (current->mm, addr); +up_read (¤t->mm->mmap_sem); + +/* +* force_sig_info_fault() is called from a number of +* contexts, some of which have a VMA and some of which +* do not. The Pkey-fault handing happens after we have a +* valid VMA, so we should never reach this without a +* valid VMA. +*/
Also because pkey can only be used from user space when we will definitely have a VMA associated with it.
arch/powerpc/kernel/traps.c void user_single_step_siginfo(struct task_struct *tsk, · line 284
Quoted 9 lines
arch/powerpc/kernel/traps.c +9
-0+if (!vma) { +WARN_ONCE (1 ,"Pkey fault with no VMA passed in" ); + info->si_pkey =0 ; +return ; + } + +/* +* We could report the incorrect key because of the reason +* explained above.
What if we hold mm->mmap_sem for some more time till we update info->si_pkey ? Is there still a chance that pkey would have changed by the time siginfo returns to user space ? I am still wondering is there way to hold up VMA changes to be on safer side. Is the race conditions exists on x86 as well ?
Quoted 29 lines
arch/powerpc/kernel/traps.c +14
-0+* +* si_pkey should be thought off as a strong hint, but not +* an absolutely guarantee because of the race explained +* above. +*/ + info->si_pkey = vma_pkey(vma); +} +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +void _exception (int signr,struct pt_regs *regs,int code,unsigned long addr) {siginfo_t info;@@ -274,6 +318,11 @@ void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr) info.si_signo = signr; info.si_code = code; info.si_addr = (void __user *) addr; + +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +fill_sig_info_pkey (code, &info, addr); +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +force_sig_info (signr, &info, current); }arch/powerpc/mm/fault.c +0
-0
arch/powerpc/mm/fault.c int do_page_fault(struct pt_regs *regs, unsigned long address, · line 459
Quoted 7 lines
arch/powerpc/mm/fault.c +4
-0@@ -453,6 +453,10 @@ int do_page_fault(struct pt_regs *regs, unsigned long address,if (!arch_vma_access_permitted (vma, flags &FAULT_FLAG_WRITE , flags &FAULT_FLAG_INSTRUCTION ,0 )) { + +/* our caller may not have saved the amr. Lets save it */ +get_paca ()->paca_amr =read_amr (); +
Something is not right here. PACA save should have happened before we come here. Why say the caller might not have saved the AMR ? Is there a path when its possible ?
Re: [RFC v2 09/12] powerpc: Deliver SEGV signal on pkey violation.
Quoted 41 lines · Anshuman Khandual
Quoted 40 lines · Ram Pai
The value of the AMR register at the time of exception is made available in gp_regs[PT_AMR] of the siginfo.
This field can be used to reprogram the permission bits of any valid pkey.
Similarly the value of the pkey, whose protection got violated, is made available at
si_pkeyfield of the siginfo structure.Signed-off-by Ram Pai <[email protected]>--- arch/powerpc/include/asm/paca.h | 1 + arch/powerpc/include/uapi/asm/ptrace.h | 3 ++- arch/powerpc/kernel/asm-offsets.c | 5 ++++ arch/powerpc/kernel/exceptions-64s.S | 8 ++++++ arch/powerpc/kernel/signal_32.c | 14 ++++++++++ arch/powerpc/kernel/signal_64.c | 14 ++++++++++ arch/powerpc/kernel/traps.c | 49 ++++++++++++++++++++++++++++++++++ arch/powerpc/mm/fault.c | 4 +++ 8 files changed, 97 insertions(+), 1 deletion(-)arch/powerpc/include/asm/paca.h +1
-0@@ -92,6 +92,7 @@ struct paca_struct {struct dtl_entry *dispatch_log_end;#endif /* CONFIG_PPC_STD_MMU_64 */ u64 dscr_default;/* per-CPU default DSCR */ +u64 paca_amr;/* value of amr at exception */ #ifdef CONFIG_PPC_STD_MMU_64 /* arch/powerpc/include/uapi/asm/ptrace.h +0
-0@@ -108,8 +108,9 @@ struct pt_regs {#define PT_DAR 41#define PT_DSISR 42
Quoted 8 lines
arch/powerpc/include/uapi/asm/ptrace.h struct pt_regs { · line 113
Quoted 5 lines
arch/powerpc/include/uapi/asm/ptrace.h +2
-1#define PT_RESULT 43 -#define PT_DSCR 44#define PT_REGS_COUNT 44 +#define PT_DSCR 44 +#define PT_AMR 45
PT_REGS_COUNTis not getting incremented even after adding one more element into the pack ?
Correct. there are 48 entires in gp_regs table AFAICT, only the first 45 are exposed through pt_regs and through gp_regs. the remaining are exposed through gp_regs only.
Quoted 4 lines
Quoted 4 lines
arch/powerpc/include/uapi/asm/ptrace.h +0
-0#define PT_FPR0 48/* each FP reg occupies 2 slots in this space */
Quoted 15 lines
arch/powerpc/kernel/asm-offsets.c int main(void) · line 248
Quoted 13 lines
arch/powerpc/kernel/asm-offsets.c +5
-0@@ -241,6 +241,11 @@ int main(void)OFFSET (PACAHWCPUID , paca_struct, hw_cpu_id);OFFSET (PACAKEXECSTATE , paca_struct, kexec_state);OFFSET (PACA_DSCR_DEFAULT , paca_struct, dscr_default); + +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +OFFSET (PACA_AMR , paca_struct, paca_amr); +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +So we now have a place in PACA for AMR.
yes.
Quoted 24 lines
Quoted 24 lines
arch/powerpc/kernel/asm-offsets.c +0
-0OFFSET (ACCOUNT_STARTTIME , paca_struct, accounting.starttime);OFFSET (ACCOUNT_STARTTIME_USER , paca_struct, accounting.starttime_user);OFFSET (ACCOUNT_USER_TIME , paca_struct, accounting.utime);arch/powerpc/kernel/exceptions-64s.S +5
-0@@ -493,6 +493,10 @@ EXC_COMMON_BEGIN(data_access_common) ld r12,_MSR(r1) ld r3,PACA_EXGEN+EX_DAR(r13) lwz r4,PACA_EXGEN+EX_DSISR(r13) +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS + mfspr r5,SPRN_AMR + std r5,PACA_AMR(r13) +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ li r5,0x300 std r3,_DAR(r1) std r4,_DSISR(r1)@@ -561,6 +565,10 @@ EXC_COMMON_BEGIN(instruction_access_common) ld r12,_MSR(r1) ld r3,_NIP(r1) andis. r4,r12,0x5820 +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS
Quoted 8 lines
arch/powerpc/kernel/exceptions-64s.S EXC_COMMON_BEGIN(data_access_common) · line 499
Quoted 3 lines
arch/powerpc/kernel/exceptions-64s.S +3
-0+ mfspr r5,SPRN_AMR + std r5,PACA_AMR(r13) +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */Saving the AMR context on page faults, this seems to be changing in the next patch again based on whether any key was active at that point and fault happened for the key enforcement ?
yes. i am going to merge the next patch with this patch.
Quoted 98 lines
Quoted 98 lines
arch/powerpc/kernel/exceptions-64s.S +0
-0li r5,0x400 std r3,_DAR(r1) std r4,_DSISR(r1)arch/powerpc/kernel/signal_32.c +14
-0@@ -500,6 +500,11 @@ static int save_user_regs(struct pt_regs *regs, struct mcontext __user *frame, (unsigned long) &frame->tramp[2]); } +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +if (__put_user (get_paca ()->paca_amr, &frame->mc_gregs[PT_AMR ])) +return 1 ; +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +return 0 ; }@@ -661,6 +666,9 @@ static long restore_user_regs(struct pt_regs *regs,long err;unsigned int save_r2 =0 ;unsigned long msr; +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +unsigned long amr; +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ #ifdef CONFIG_VSX int i;#endif @@ -750,6 +758,12 @@ static long restore_user_regs(struct pt_regs *regs,return 1 ;#endif /* CONFIG_SPE */ +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS + err |=__get_user (amr, &sr->mc_gregs[PT_AMR ]); +if (!err && amr !=get_paca ()->paca_amr) +write_amr (amr); +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +return 0 ; }arch/powerpc/kernel/signal_64.c +14
-0@@ -174,6 +174,10 @@ static long setup_sigcontext(struct sigcontext __user *sc,if (set !=NULL ) err |=__put_user (set->sig[0 ], &sc->oldmask); +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS + err |=__put_user (get_paca ()->paca_amr, &sc->gp_regs[PT_AMR ]); +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +return err; }@@ -327,6 +331,9 @@ static long restore_sigcontext(struct task_struct *tsk, sigset_t *set, int sig,unsigned long save_r13 =0 ;unsigned long msr;struct pt_regs *regs = tsk->thread.regs; +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +unsigned long amr; +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ #ifdef CONFIG_VSX int i;#endif @@ -406,6 +413,13 @@ static long restore_sigcontext(struct task_struct *tsk, sigset_t *set, int sig, tsk->thread.fp_state.fpr[i][TS_VSRLOWOFFSET] = 0; }#endif + +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS + err |=__get_user (amr, &sc->gp_regs[PT_AMR ]); +if (!err && amr !=get_paca ()->paca_amr) +write_amr (amr); +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +return err; }arch/powerpc/kernel/traps.c +1
-0@@ -20,6 +20,7 @@#include <linux/sched/debug.h> #include <linux/kernel.h> #include <linux/mm.h> +#include <linux/pkeys.h> #include <linux/stddef.h> #include <linux/unistd.h> #include <linux/ptrace.h> @@ -247,6 +248,49 @@ void user_single_step_siginfo(struct task_struct *tsk, info->si_addr = (void __user *)regs->nip; }
Quoted 10 lines
arch/powerpc/kernel/traps.c void user_single_step_siginfo(struct task_struct *tsk, · line 258
Quoted 8 lines
arch/powerpc/kernel/traps.c +8
-0+#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +static void fill_sig_info_pkey (int si_code,siginfo_t *info,unsigned long addr) +{ +struct vm_area_struct *vma; + +/* Fault not from Protection Keys: nothing to do */ +if (si_code !=SEGV_PKUERR ) +return ;Should have checked this in the caller ?
maybe. currently there is only one caller to this function. so either way is ok. But if more than one callers show up later having the check here reduces the burden on the caller.
Quoted 62 lines
Quoted 8 lines
arch/powerpc/kernel/traps.c +7
-0+ +down_read (¤t->mm->mmap_sem); +/* +* we could be racing with pkey_mprotect(). +* If pkey_mprotect() wins the key value could +* get modified...xxx +*/ arch/powerpc/kernel/traps.c void user_single_step_siginfo(struct task_struct *tsk, · line 275
Quoted 10 lines
arch/powerpc/kernel/traps.c +10
-0+ vma =find_vma (current->mm, addr); +up_read (¤t->mm->mmap_sem); + +/* +* force_sig_info_fault() is called from a number of +* contexts, some of which have a VMA and some of which +* do not. The Pkey-fault handing happens after we have a +* valid VMA, so we should never reach this without a +* valid VMA. +*/ Also because pkey can only be used from user space when we will definitely have a VMA associated with it.
arch/powerpc/kernel/traps.c void user_single_step_siginfo(struct task_struct *tsk, · line 284
Quoted 9 lines
arch/powerpc/kernel/traps.c +9
-0+if (!vma) { +WARN_ONCE (1 ,"Pkey fault with no VMA passed in" ); + info->si_pkey =0 ; +return ; + } + +/* +* We could report the incorrect key because of the reason +* explained above. What if we hold
mm->mmap_semfor some more time till we updateinfo->si_pkey? Is there still a chance that pkey would have changed by the time siginfo returns to user space ? I am still wondering is there way to hold up VMA changes to be on safer side. Is the race conditions exists on x86 as well ?Quoted 24 lines
arch/powerpc/kernel/traps.c +14
-0+* +* si_pkey should be thought off as a strong hint, but not +* an absolutely guarantee because of the race explained +* above. +*/ + info->si_pkey = vma_pkey(vma); +} +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +void _exception (int signr,struct pt_regs *regs,int code,unsigned long addr) {siginfo_t info;@@ -274,6 +318,11 @@ void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr) info.si_signo = signr; info.si_code = code; info.si_addr = (void __user *) addr; + +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +fill_sig_info_pkey (code, &info, addr); +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +force_sig_info (signr, &info, current); }
Quoted 16 lines
arch/powerpc/mm/fault.c int do_page_fault(struct pt_regs *regs, unsigned long address, · line 459
Quoted 12 lines
arch/powerpc/mm/fault.c +4
-0@@ -453,6 +453,10 @@ int do_page_fault(struct pt_regs *regs, unsigned long address,if (!arch_vma_access_permitted (vma, flags &FAULT_FLAG_WRITE , flags &FAULT_FLAG_INSTRUCTION ,0 )) { + +/* our caller may not have saved the amr. Lets save it */ +get_paca ()->paca_amr =read_amr (); +Something is not right here. PACA save should have happened before we come here. Why say the caller might not have saved the AMR ? Is there a path when its possible ?
This is a case, where the fault is cause because of page not yet being hashed, but at the same time could have violated a protection key. Since the page is not hashed yet, there is no protection-key fault and hence the caller would have not saved the AMR. We do it here to catch that case.
RP
Signature · 1 line
Ram Pai
Re: [RFC v2 09/12] powerpc: Deliver SEGV signal on pkey violation.
Quoted 222 lines · Ram Pai
Quoted 41 lines · Anshuman Khandual
Quoted 40 lines · Ram Pai
The value of the AMR register at the time of exception is made available in gp_regs[PT_AMR] of the siginfo.
This field can be used to reprogram the permission bits of any valid pkey.
Similarly the value of the pkey, whose protection got violated, is made available at
si_pkeyfield of the siginfo structure.Signed-off-by Ram Pai <[email protected]>--- arch/powerpc/include/asm/paca.h | 1 + arch/powerpc/include/uapi/asm/ptrace.h | 3 ++- arch/powerpc/kernel/asm-offsets.c | 5 ++++ arch/powerpc/kernel/exceptions-64s.S | 8 ++++++ arch/powerpc/kernel/signal_32.c | 14 ++++++++++ arch/powerpc/kernel/signal_64.c | 14 ++++++++++ arch/powerpc/kernel/traps.c | 49 ++++++++++++++++++++++++++++++++++ arch/powerpc/mm/fault.c | 4 +++ 8 files changed, 97 insertions(+), 1 deletion(-)arch/powerpc/include/asm/paca.h +1
-0@@ -92,6 +92,7 @@ struct paca_struct {struct dtl_entry *dispatch_log_end;#endif /* CONFIG_PPC_STD_MMU_64 */ u64 dscr_default;/* per-CPU default DSCR */ +u64 paca_amr;/* value of amr at exception */ #ifdef CONFIG_PPC_STD_MMU_64 /* arch/powerpc/include/uapi/asm/ptrace.h +0
-0@@ -108,8 +108,9 @@ struct pt_regs {#define PT_DAR 41#define PT_DSISR 42Quoted 8 lines
arch/powerpc/include/uapi/asm/ptrace.h struct pt_regs { · line 113
Quoted 5 lines
arch/powerpc/include/uapi/asm/ptrace.h +2
-1#define PT_RESULT 43 -#define PT_DSCR 44#define PT_REGS_COUNT 44 +#define PT_DSCR 44 +#define PT_AMR 45
PT_REGS_COUNTis not getting incremented even after adding one more element into the pack ?Correct. there are 48 entires in
gp_regstable AFAICT, only the first 45 are exposed throughpt_regsand throughgp_regs. the remaining are exposed throughgp_regsonly.Quoted 4 lines
Quoted 4 lines
arch/powerpc/include/uapi/asm/ptrace.h +0
-0#define PT_FPR0 48/* each FP reg occupies 2 slots in this space */ Quoted 15 lines
arch/powerpc/kernel/asm-offsets.c int main(void) · line 248
Quoted 13 lines
arch/powerpc/kernel/asm-offsets.c +5
-0@@ -241,6 +241,11 @@ int main(void)OFFSET (PACAHWCPUID , paca_struct, hw_cpu_id);OFFSET (PACAKEXECSTATE , paca_struct, kexec_state);OFFSET (PACA_DSCR_DEFAULT , paca_struct, dscr_default); + +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +OFFSET (PACA_AMR , paca_struct, paca_amr); +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +So we now have a place in PACA for AMR.
yes.
Quoted 24 lines
Quoted 24 lines
arch/powerpc/kernel/asm-offsets.c +0
-0OFFSET (ACCOUNT_STARTTIME , paca_struct, accounting.starttime);OFFSET (ACCOUNT_STARTTIME_USER , paca_struct, accounting.starttime_user);OFFSET (ACCOUNT_USER_TIME , paca_struct, accounting.utime);arch/powerpc/kernel/exceptions-64s.S +5
-0@@ -493,6 +493,10 @@ EXC_COMMON_BEGIN(data_access_common) ld r12,_MSR(r1) ld r3,PACA_EXGEN+EX_DAR(r13) lwz r4,PACA_EXGEN+EX_DSISR(r13) +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS + mfspr r5,SPRN_AMR + std r5,PACA_AMR(r13) +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ li r5,0x300 std r3,_DAR(r1) std r4,_DSISR(r1)@@ -561,6 +565,10 @@ EXC_COMMON_BEGIN(instruction_access_common) ld r12,_MSR(r1) ld r3,_NIP(r1) andis. r4,r12,0x5820 +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYSQuoted 8 lines
arch/powerpc/kernel/exceptions-64s.S EXC_COMMON_BEGIN(data_access_common) · line 499
Quoted 3 lines
arch/powerpc/kernel/exceptions-64s.S +3
-0+ mfspr r5,SPRN_AMR + std r5,PACA_AMR(r13) +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */Saving the AMR context on page faults, this seems to be changing in the next patch again based on whether any key was active at that point and fault happened for the key enforcement ?
yes. i am going to merge the next patch with this patch.
Quoted 98 lines
Quoted 98 lines
arch/powerpc/kernel/exceptions-64s.S +0
-0li r5,0x400 std r3,_DAR(r1) std r4,_DSISR(r1)arch/powerpc/kernel/signal_32.c +14
-0@@ -500,6 +500,11 @@ static int save_user_regs(struct pt_regs *regs, struct mcontext __user *frame, (unsigned long) &frame->tramp[2]); } +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +if (__put_user (get_paca ()->paca_amr, &frame->mc_gregs[PT_AMR ])) +return 1 ; +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +return 0 ; }@@ -661,6 +666,9 @@ static long restore_user_regs(struct pt_regs *regs,long err;unsigned int save_r2 =0 ;unsigned long msr; +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +unsigned long amr; +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ #ifdef CONFIG_VSX int i;#endif @@ -750,6 +758,12 @@ static long restore_user_regs(struct pt_regs *regs,return 1 ;#endif /* CONFIG_SPE */ +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS + err |=__get_user (amr, &sr->mc_gregs[PT_AMR ]); +if (!err && amr !=get_paca ()->paca_amr) +write_amr (amr); +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +return 0 ; }arch/powerpc/kernel/signal_64.c +14
-0@@ -174,6 +174,10 @@ static long setup_sigcontext(struct sigcontext __user *sc,if (set !=NULL ) err |=__put_user (set->sig[0 ], &sc->oldmask); +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS + err |=__put_user (get_paca ()->paca_amr, &sc->gp_regs[PT_AMR ]); +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +return err; }@@ -327,6 +331,9 @@ static long restore_sigcontext(struct task_struct *tsk, sigset_t *set, int sig,unsigned long save_r13 =0 ;unsigned long msr;struct pt_regs *regs = tsk->thread.regs; +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +unsigned long amr; +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ #ifdef CONFIG_VSX int i;#endif @@ -406,6 +413,13 @@ static long restore_sigcontext(struct task_struct *tsk, sigset_t *set, int sig, tsk->thread.fp_state.fpr[i][TS_VSRLOWOFFSET] = 0; }#endif + +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS + err |=__get_user (amr, &sc->gp_regs[PT_AMR ]); +if (!err && amr !=get_paca ()->paca_amr) +write_amr (amr); +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +return err; }arch/powerpc/kernel/traps.c +1
-0@@ -20,6 +20,7 @@#include <linux/sched/debug.h> #include <linux/kernel.h> #include <linux/mm.h> +#include <linux/pkeys.h> #include <linux/stddef.h> #include <linux/unistd.h> #include <linux/ptrace.h> @@ -247,6 +248,49 @@ void user_single_step_siginfo(struct task_struct *tsk, info->si_addr = (void __user *)regs->nip; }Quoted 10 lines
arch/powerpc/kernel/traps.c void user_single_step_siginfo(struct task_struct *tsk, · line 258
Quoted 8 lines
arch/powerpc/kernel/traps.c +8
-0+#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +static void fill_sig_info_pkey (int si_code,siginfo_t *info,unsigned long addr) +{ +struct vm_area_struct *vma; + +/* Fault not from Protection Keys: nothing to do */ +if (si_code !=SEGV_PKUERR ) +return ;Should have checked this in the caller ?
Quoted 42 lines
maybe. currently there is only one caller to this function. so either way is ok. But if more than one callers show up later having the check here reduces the burden on the caller.
Quoted 37 lines
Quoted 8 lines
arch/powerpc/kernel/traps.c +7
-0+ +down_read (¤t->mm->mmap_sem); +/* +* we could be racing with pkey_mprotect(). +* If pkey_mprotect() wins the key value could +* get modified...xxx +*/ arch/powerpc/kernel/traps.c void user_single_step_siginfo(struct task_struct *tsk, · line 275
Quoted 10 lines
arch/powerpc/kernel/traps.c +10
-0+ vma =find_vma (current->mm, addr); +up_read (¤t->mm->mmap_sem); + +/* +* force_sig_info_fault() is called from a number of +* contexts, some of which have a VMA and some of which +* do not. The Pkey-fault handing happens after we have a +* valid VMA, so we should never reach this without a +* valid VMA. +*/ Also because pkey can only be used from user space when we will definitely have a VMA associated with it.
arch/powerpc/kernel/traps.c void user_single_step_siginfo(struct task_struct *tsk, · line 284
Quoted 9 lines
arch/powerpc/kernel/traps.c +9
-0+if (!vma) { +WARN_ONCE (1 ,"Pkey fault with no VMA passed in" ); + info->si_pkey =0 ; +return ; + } + +/* +* We could report the incorrect key because of the reason +* explained above. What if we hold
mm->mmap_semfor some more time till we updateinfo->si_pkey? Is there still a chance that pkey would have changed by the time siginfo returns to user space ? I am still wondering is there way to hold up VMA changes to be on safer side. Is the race conditions exists on x86 as well ?
Is the race condition exists on x86 system as well ? Can we hold up little more the mmap_sem to improve our chances ?
Quoted 42 lines
Quoted 25 lines
Quoted 25 lines
arch/powerpc/kernel/traps.c +14
-0+* +* si_pkey should be thought off as trong hint, but not +* an absolutely guarantee because of the race explained +* above. +*/ + info->si_pkey = vma_pkey(vma); +} +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +void _exception (int signr,struct pt_regs *regs,int code,unsigned long addr) {siginfo_t info;@@ -274,6 +318,11 @@ void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr) info.si_signo = signr; info.si_code = code; info.si_addr = (void __user *) addr; + +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +fill_sig_info_pkey (code, &info, addr); +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +force_sig_info (signr, &info, current); }Quoted 16 lines
arch/powerpc/mm/fault.c int do_page_fault(struct pt_regs *regs, unsigned long address, · line 459
Quoted 12 lines
arch/powerpc/mm/fault.c +4
-0@@ -453,6 +453,10 @@ int do_page_fault(struct pt_regs *regs, unsigned long address,if (!arch_vma_access_permitted (vma, flags &FAULT_FLAG_WRITE , flags &FAULT_FLAG_INSTRUCTION ,0 )) { + +/* our caller may not have saved the amr. Lets save it */ +get_paca ()->paca_amr =read_amr (); +Something is not right here. PACA save should have happened before we come here. Why say the caller might not have saved the AMR ? Is there a path when its possible ?
Quoted 5 lines
This is a case, where the fault is cause because of page not yet being hashed, but at the same time could have violated a protection key. Since the page is not hashed yet, there is no protection-key fault and hence the caller would have not saved the AMR. We do it here to catch that case.
Is that because of the above optimization you have added in the page fault path ?
Re: [RFC v2 09/12] powerpc: Deliver SEGV signal on pkey violation.
Quoted 266 lines · Anshuman Khandual
Quoted 222 lines · Ram Pai
Quoted 41 lines · Anshuman Khandual
Quoted 40 lines · Ram Pai
The value of the AMR register at the time of exception is made available in gp_regs[PT_AMR] of the siginfo.
This field can be used to reprogram the permission bits of any valid pkey.
Similarly the value of the pkey, whose protection got violated, is made available at
si_pkeyfield of the siginfo structure.Signed-off-by Ram Pai <[email protected]>--- arch/powerpc/include/asm/paca.h | 1 + arch/powerpc/include/uapi/asm/ptrace.h | 3 ++- arch/powerpc/kernel/asm-offsets.c | 5 ++++ arch/powerpc/kernel/exceptions-64s.S | 8 ++++++ arch/powerpc/kernel/signal_32.c | 14 ++++++++++ arch/powerpc/kernel/signal_64.c | 14 ++++++++++ arch/powerpc/kernel/traps.c | 49 ++++++++++++++++++++++++++++++++++ arch/powerpc/mm/fault.c | 4 +++ 8 files changed, 97 insertions(+), 1 deletion(-)arch/powerpc/include/asm/paca.h +1
-0@@ -92,6 +92,7 @@ struct paca_struct {struct dtl_entry *dispatch_log_end;#endif /* CONFIG_PPC_STD_MMU_64 */ u64 dscr_default;/* per-CPU default DSCR */ +u64 paca_amr;/* value of amr at exception */ #ifdef CONFIG_PPC_STD_MMU_64 /* arch/powerpc/include/uapi/asm/ptrace.h +0
-0@@ -108,8 +108,9 @@ struct pt_regs {#define PT_DAR 41#define PT_DSISR 42Quoted 8 lines
arch/powerpc/include/uapi/asm/ptrace.h struct pt_regs { · line 113
Quoted 5 lines
arch/powerpc/include/uapi/asm/ptrace.h +2
-1#define PT_RESULT 43 -#define PT_DSCR 44#define PT_REGS_COUNT 44 +#define PT_DSCR 44 +#define PT_AMR 45
PT_REGS_COUNTis not getting incremented even after adding one more element into the pack ?Correct. there are 48 entires in
gp_regstable AFAICT, only the first 45 are exposed throughpt_regsand throughgp_regs. the remaining are exposed throughgp_regsonly.Quoted 4 lines
Quoted 4 lines
arch/powerpc/include/uapi/asm/ptrace.h +0
-0#define PT_FPR0 48/* each FP reg occupies 2 slots in this space */ Quoted 15 lines
arch/powerpc/kernel/asm-offsets.c int main(void) · line 248
Quoted 13 lines
arch/powerpc/kernel/asm-offsets.c +5
-0@@ -241,6 +241,11 @@ int main(void)OFFSET (PACAHWCPUID , paca_struct, hw_cpu_id);OFFSET (PACAKEXECSTATE , paca_struct, kexec_state);OFFSET (PACA_DSCR_DEFAULT , paca_struct, dscr_default); + +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +OFFSET (PACA_AMR , paca_struct, paca_amr); +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +So we now have a place in PACA for AMR.
yes.
Quoted 24 lines
Quoted 24 lines
arch/powerpc/kernel/asm-offsets.c +0
-0OFFSET (ACCOUNT_STARTTIME , paca_struct, accounting.starttime);OFFSET (ACCOUNT_STARTTIME_USER , paca_struct, accounting.starttime_user);OFFSET (ACCOUNT_USER_TIME , paca_struct, accounting.utime);arch/powerpc/kernel/exceptions-64s.S +5
-0@@ -493,6 +493,10 @@ EXC_COMMON_BEGIN(data_access_common) ld r12,_MSR(r1) ld r3,PACA_EXGEN+EX_DAR(r13) lwz r4,PACA_EXGEN+EX_DSISR(r13) +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS + mfspr r5,SPRN_AMR + std r5,PACA_AMR(r13) +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ li r5,0x300 std r3,_DAR(r1) std r4,_DSISR(r1)@@ -561,6 +565,10 @@ EXC_COMMON_BEGIN(instruction_access_common) ld r12,_MSR(r1) ld r3,_NIP(r1) andis. r4,r12,0x5820 +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYSQuoted 8 lines
arch/powerpc/kernel/exceptions-64s.S EXC_COMMON_BEGIN(data_access_common) · line 499
Quoted 3 lines
arch/powerpc/kernel/exceptions-64s.S +3
-0+ mfspr r5,SPRN_AMR + std r5,PACA_AMR(r13) +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */Saving the AMR context on page faults, this seems to be changing in the next patch again based on whether any key was active at that point and fault happened for the key enforcement ?
yes. i am going to merge the next patch with this patch.
Quoted 98 lines
Quoted 98 lines
arch/powerpc/kernel/exceptions-64s.S +0
-0li r5,0x400 std r3,_DAR(r1) std r4,_DSISR(r1)arch/powerpc/kernel/signal_32.c +14
-0@@ -500,6 +500,11 @@ static int save_user_regs(struct pt_regs *regs, struct mcontext __user *frame, (unsigned long) &frame->tramp[2]); } +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +if (__put_user (get_paca ()->paca_amr, &frame->mc_gregs[PT_AMR ])) +return 1 ; +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +return 0 ; }@@ -661,6 +666,9 @@ static long restore_user_regs(struct pt_regs *regs,long err;unsigned int save_r2 =0 ;unsigned long msr; +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +unsigned long amr; +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ #ifdef CONFIG_VSX int i;#endif @@ -750,6 +758,12 @@ static long restore_user_regs(struct pt_regs *regs,return 1 ;#endif /* CONFIG_SPE */ +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS + err |=__get_user (amr, &sr->mc_gregs[PT_AMR ]); +if (!err && amr !=get_paca ()->paca_amr) +write_amr (amr); +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +return 0 ; }arch/powerpc/kernel/signal_64.c +14
-0@@ -174,6 +174,10 @@ static long setup_sigcontext(struct sigcontext __user *sc,if (set !=NULL ) err |=__put_user (set->sig[0 ], &sc->oldmask); +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS + err |=__put_user (get_paca ()->paca_amr, &sc->gp_regs[PT_AMR ]); +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +return err; }@@ -327,6 +331,9 @@ static long restore_sigcontext(struct task_struct *tsk, sigset_t *set, int sig,unsigned long save_r13 =0 ;unsigned long msr;struct pt_regs *regs = tsk->thread.regs; +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +unsigned long amr; +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ #ifdef CONFIG_VSX int i;#endif @@ -406,6 +413,13 @@ static long restore_sigcontext(struct task_struct *tsk, sigset_t *set, int sig, tsk->thread.fp_state.fpr[i][TS_VSRLOWOFFSET] = 0; }#endif + +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS + err |=__get_user (amr, &sc->gp_regs[PT_AMR ]); +if (!err && amr !=get_paca ()->paca_amr) +write_amr (amr); +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +return err; }arch/powerpc/kernel/traps.c +1
-0@@ -20,6 +20,7 @@#include <linux/sched/debug.h> #include <linux/kernel.h> #include <linux/mm.h> +#include <linux/pkeys.h> #include <linux/stddef.h> #include <linux/unistd.h> #include <linux/ptrace.h> @@ -247,6 +248,49 @@ void user_single_step_siginfo(struct task_struct *tsk, info->si_addr = (void __user *)regs->nip; }Quoted 10 lines
arch/powerpc/kernel/traps.c void user_single_step_siginfo(struct task_struct *tsk, · line 258
Quoted 8 lines
arch/powerpc/kernel/traps.c +8
-0+#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +static void fill_sig_info_pkey (int si_code,siginfo_t *info,unsigned long addr) +{ +struct vm_area_struct *vma; + +/* Fault not from Protection Keys: nothing to do */ +if (si_code !=SEGV_PKUERR ) +return ;Should have checked this in the caller ?
Quoted 42 lines
maybe. currently there is only one caller to this function. so either way is ok. But if more than one callers show up later having the check here reduces the burden on the caller.
Quoted 37 lines
Quoted 8 lines
arch/powerpc/kernel/traps.c +7
-0+ +down_read (¤t->mm->mmap_sem); +/* +* we could be racing with pkey_mprotect(). +* If pkey_mprotect() wins the key value could +* get modified...xxx +*/ arch/powerpc/kernel/traps.c void user_single_step_siginfo(struct task_struct *tsk, · line 275
Quoted 10 lines
arch/powerpc/kernel/traps.c +10
-0+ vma =find_vma (current->mm, addr); +up_read (¤t->mm->mmap_sem); + +/* +* force_sig_info_fault() is called from a number of +* contexts, some of which have a VMA and some of which +* do not. The Pkey-fault handing happens after we have a +* valid VMA, so we should never reach this without a +* valid VMA. +*/ Also because pkey can only be used from user space when we will definitely have a VMA associated with it.
arch/powerpc/kernel/traps.c void user_single_step_siginfo(struct task_struct *tsk, · line 284
Quoted 9 lines
arch/powerpc/kernel/traps.c +9
-0+if (!vma) { +WARN_ONCE (1 ,"Pkey fault with no VMA passed in" ); + info->si_pkey =0 ; +return ; + } + +/* +* We could report the incorrect key because of the reason +* explained above. What if we hold
mm->mmap_semfor some more time till we updateinfo->si_pkey? Is there still a chance that pkey would have changed by the time siginfo returns to user space ? I am still wondering is there way to hold up VMA changes to be on safer side. Is the race conditions exists on x86 as well ?
Quoted 3 lines
Is the race condition exists on x86 system as well ? Can we hold up little more the
mmap_semto improve our chances ?
Yes it exists on x86 as well. We could hold the sem longer, but that does not entirely solve the problem, at the same time we will slow down the process. The fool-proof way of solving the problem is if the hardware told us which key got violated, instead of just telling us that there was key violation. Depending on the value in the vma, is always going to be racy.
Quoted 42 lines
Quoted 42 lines
Quoted 41 lines
Quoted 30 lines
arch/powerpc/kernel/traps.c +14
-0+* +* si_pkey should be thought off as trong hint, but not +* an absolutely guarantee because of the race explained +* above. +*/ + info->si_pkey = vma_pkey(vma); +} +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +void _exception (int signr,struct pt_regs *regs,int code,unsigned long addr) {siginfo_t info;@@ -274,6 +318,11 @@ void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr) info.si_signo = signr; info.si_code = code; info.si_addr = (void __user *) addr; + +#ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS +fill_sig_info_pkey (code, &info, addr); +#endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ +force_sig_info (signr, &info, current); }arch/powerpc/mm/fault.c +0
-0arch/powerpc/mm/fault.c int do_page_fault(struct pt_regs *regs, unsigned long address, · line 459
Quoted 7 lines
arch/powerpc/mm/fault.c +4
-0@@ -453,6 +453,10 @@ int do_page_fault(struct pt_regs *regs, unsigned long address,if (!arch_vma_access_permitted (vma, flags &FAULT_FLAG_WRITE , flags &FAULT_FLAG_INSTRUCTION ,0 )) { + +/* our caller may not have saved the amr. Lets save it */ +get_paca ()->paca_amr =read_amr (); +Something is not right here. PACA save should have happened before we come here. Why say the caller might not have saved the AMR ? Is there a path when its possible ?
Quoted 8 lines
Quoted 5 lines
This is a case, where the fault is cause because of page not yet being hashed, but at the same time could have violated a protection key. Since the page is not hashed yet, there is no protection-key fault and hence the caller would have not saved the AMR. We do it here to catch that case.
Is that because of the above optimization you have added in the page fault path ?
yes. exactly.
Signature · 1 line
Ram Pai
RFC v2 10/12 powerpc: Read AMR only if pkey-violation caused the exception.
In reply to Ram Pai--- arch/powerpc/kernel/exceptions-64s.S | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)
arch/powerpc/kernel/exceptions-64s.S +10 -6
@@ -493,13 +493,15 @@ EXC_COMMON_BEGIN(data_access_common) ld r12,_MSR(r1) ld r3,PACA_EXGEN+EX_DAR(r13) lwz r4,PACA_EXGEN+EX_DSISR(r13) + std r3,_DAR(r1) + std r4,_DSISR(r1) #ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS + andis. r0,r4,DSISR_KEYFAULT@h /* save AMR only if its a key fault */ + beq+ 1f mfspr r5,SPRN_AMR std r5,PACA_AMR(r13) #endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ - li r5,0x300 - std r3,_DAR(r1) - std r4,_DSISR(r1) +1: li r5,0x300 BEGIN_MMU_FTR_SECTION b do_hash_page /* Try to handle as hpte fault */ MMU_FTR_SECTION_ELSE
@@ -565,13 +567,15 @@ EXC_COMMON_BEGIN(instruction_access_common) ld r12,_MSR(r1) ld r3,_NIP(r1) andis. r4,r12,0x5820 + std r3,_DAR(r1) + std r4,_DSISR(r1) #ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS + andis. r0,r4,DSISR_KEYFAULT@h /* save AMR only if its a key fault */ + beq+ 1f mfspr r5,SPRN_AMR std r5,PACA_AMR(r13) #endif /* CONFIG_PPC64_MEMORY_PROTECTION_KEYS */ - li r5,0x400 - std r3,_DAR(r1) - std r4,_DSISR(r1) +1: li r5,0x400 BEGIN_MMU_FTR_SECTION b do_hash_page /* Try to handle as hpte fault */ MMU_FTR_SECTION_ELSE
Signature · 1 line
1.8.3.1
Michael Ellerman
reviewerRe: [RFC v2 10/12] powerpc: Read AMR only if pkey-violation caused the exception.
Quoted 10 lines · Ram Pai
Signed-off-by Ram Pai <[email protected]>--- arch/powerpc/kernel/exceptions-64s.S | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)arch/powerpc/kernel/exceptions-64s.S +0
-0
arch/powerpc/kernel/exceptions-64s.S EXC_COMMON_BEGIN(data_access_common) · line 500
Quoted 8 lines
arch/powerpc/kernel/exceptions-64s.S +4
-0@@ -493,13 +493,15 @@ EXC_COMMON_BEGIN(data_access_common) ld r12,_MSR(r1) ld r3,PACA_EXGEN+EX_DAR(r13) lwz r4,PACA_EXGEN+EX_DSISR(r13) + std r3,_DAR(r1) + std r4,_DSISR(r1) #ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS + andis. r0,r4,DSISR_KEYFAULT@h /* save AMR only if its a key fault */ + beq+ 1f
This seems to be incremental on top of one of your other patches.
But I don't see why, can you please just squash this into whatever patch adds this code in the first place.
cheers
Re: [RFC v2 10/12] powerpc: Read AMR only if pkey-violation caused the exception.
Quoted 23 lines · Michael Ellerman
Quoted 10 lines · Ram Pai
Signed-off-by Ram Pai <[email protected]>--- arch/powerpc/kernel/exceptions-64s.S | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)arch/powerpc/kernel/exceptions-64s.S +0
-0arch/powerpc/kernel/exceptions-64s.S EXC_COMMON_BEGIN(data_access_common) · line 500
Quoted 8 lines
arch/powerpc/kernel/exceptions-64s.S +4
-0@@ -493,13 +493,15 @@ EXC_COMMON_BEGIN(data_access_common) ld r12,_MSR(r1) ld r3,PACA_EXGEN+EX_DAR(r13) lwz r4,PACA_EXGEN+EX_DSISR(r13) + std r3,_DAR(r1) + std r4,_DSISR(r1) #ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS + andis. r0,r4,DSISR_KEYFAULT@h /* save AMR only if its a key fault */ + beq+ 1fThis seems to be incremental on top of one of your other patches.
Quoted 2 lines
But I don't see why, can you please just squash this into whatever patch adds this code in the first place.
It was an optimization added later. But yes it can be squashed into an earlier patch.
RP
Re: [RFC v2 10/12] powerpc: Read AMR only if pkey-violation caused the exception.
Quoted 24 lines · Ram Pai
Quoted 23 lines · Michael Ellerman
Quoted 10 lines · Ram Pai
Signed-off-by Ram Pai <[email protected]>--- arch/powerpc/kernel/exceptions-64s.S | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)arch/powerpc/kernel/exceptions-64s.S +0
-0arch/powerpc/kernel/exceptions-64s.S EXC_COMMON_BEGIN(data_access_common) · line 500
Quoted 8 lines
arch/powerpc/kernel/exceptions-64s.S +4
-0@@ -493,13 +493,15 @@ EXC_COMMON_BEGIN(data_access_common) ld r12,_MSR(r1) ld r3,PACA_EXGEN+EX_DAR(r13) lwz r4,PACA_EXGEN+EX_DSISR(r13) + std r3,_DAR(r1) + std r4,_DSISR(r1) #ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS + andis. r0,r4,DSISR_KEYFAULT@h /* save AMR only if its a key fault */ + beq+ 1fThis seems to be incremental on top of one of your other patches.
Quoted 5 lines
Quoted 2 lines
But I don't see why, can you please just squash this into whatever patch adds this code in the first place.
It was an optimization added later. But yes it can be squashed into an earlier patch.
Could you please explain what is the optimization this achieves ?
Re: [RFC v2 10/12] powerpc: Read AMR only if pkey-violation caused the exception.
Quoted 25 lines · Anshuman Khandual
Quoted 24 lines · Ram Pai
Quoted 23 lines · Michael Ellerman
Quoted 10 lines · Ram Pai
Signed-off-by Ram Pai <[email protected]>--- arch/powerpc/kernel/exceptions-64s.S | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)arch/powerpc/kernel/exceptions-64s.S +0
-0arch/powerpc/kernel/exceptions-64s.S EXC_COMMON_BEGIN(data_access_common) · line 500
Quoted 8 lines
arch/powerpc/kernel/exceptions-64s.S +4
-0@@ -493,13 +493,15 @@ EXC_COMMON_BEGIN(data_access_common) ld r12,_MSR(r1) ld r3,PACA_EXGEN+EX_DAR(r13) lwz r4,PACA_EXGEN+EX_DSISR(r13) + std r3,_DAR(r1) + std r4,_DSISR(r1) #ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS + andis. r0,r4,DSISR_KEYFAULT@h /* save AMR only if its a key fault */ + beq+ 1fThis seems to be incremental on top of one of your other patches.
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
But I don't see why, can you please just squash this into whatever patch adds this code in the first place.
It was an optimization added later. But yes it can be squashed into an earlier patch.
Could you please explain what is the optimization this achieves ?
Don't want to read the AMR if it is not a key protection fault. This is a hot-hot-path. A few cycles saved can accumulate into signficant savings overall.
RP
Re: [RFC v2 10/12] powerpc: Read AMR only if pkey-violation caused the exception.
In reply to Michael EllermanQuoted 23 lines · Michael Ellerman
Quoted 10 lines · Ram Pai
Signed-off-by Ram Pai <[email protected]>--- arch/powerpc/kernel/exceptions-64s.S | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)arch/powerpc/kernel/exceptions-64s.S +0
-0arch/powerpc/kernel/exceptions-64s.S EXC_COMMON_BEGIN(data_access_common) · line 500
Quoted 8 lines
arch/powerpc/kernel/exceptions-64s.S +4
-0@@ -493,13 +493,15 @@ EXC_COMMON_BEGIN(data_access_common) ld r12,_MSR(r1) ld r3,PACA_EXGEN+EX_DAR(r13) lwz r4,PACA_EXGEN+EX_DSISR(r13) + std r3,_DAR(r1) + std r4,_DSISR(r1) #ifdef CONFIG_PPC64_MEMORY_PROTECTION_KEYS + andis. r0,r4,DSISR_KEYFAULT@h /* save AMR only if its a key fault */ + beq+ 1fThis seems to be incremental on top of one of your other patches.
Quoted 2 lines
But I don't see why, can you please just squash this into whatever patch adds this code in the first place.
Yes. squash it is.
next version of my patch will have it squashed.
RP
RFC v2 11/12 Documentation: Documentation updates.
In reply to Ram PaiThe Documentaton file is moved from x86 into the generic area, since this feature is now supported by more than one archs.
--- Documentation/vm/protection-keys.txt | 110 ++++++++++++++++++++++++++++++++++ Documentation/x86/protection-keys.txt | 85 -------------------------- 2 files changed, 110 insertions(+), 85 deletions(-) create mode 100644 Documentation/vm/protection-keys.txt delete mode 100644 Documentation/x86/protection-keys.txt
Documentation/vm/protection-keys.txt new file +110 -0
@@ -0,0 +1,110 @@ +Memory Protection Keys for Userspace (PKU aka PKEYs) is a CPU feature +found in new generation of intel CPUs on PowerPC CPUs. + +Memory Protection Keys provides a mechanism for enforcing page-based +protections, but without requiring modification of the page tables +when an application changes protection domains. + + +On Intel: + +It works by dedicating 4 previously ignored bits in each page table +entry to a "protection key", giving 16 possible keys. + +There is also a new user-accessible register (PKRU) with two separate +bits (Access Disable and Write Disable) for each key. Being a CPU +register, PKRU is inherently thread-local, potentially giving each +thread a different set of protections from every other thread. + +There are two new instructions (RDPKRU/WRPKRU) for reading and writing +to the new register. The feature is only available in 64-bit mode, +even though there is theoretically space in the PAE PTEs. These +permissions are enforced on data access only and have no effect on +instruction fetches. + + +On PowerPC: + +It works by dedicating 5 page table entry to a "protection key", +giving 32 possible keys. + +There is a user-accessible register (AMR) with two separate bits +(Access Disable and Write Disable) for each key. Being a CPU +register, AMR is inherently thread-local, potentially giving each +thread a different set of protections from every other thread. +NOTE: Disabling read permission does not disable +write and vice-versa. + +The feature is available on 64-bit HPTE mode only. + +'mtspr 0xd, mem' reads the AMR register +'mfspr mem, 0xd' writes into the AMR register. + +Permissions are enforced on data access only and have no effect on +instruction fetches. + +=========================== Syscalls =========================== + +There are 3 system calls which directly interact with pkeys: + + int pkey_alloc(unsigned long flags, unsigned long init_access_rights) + int pkey_free(int pkey); + int pkey_mprotect(unsigned long start, size_t len, + unsigned long prot, int pkey); + +Before a pkey can be used, it must first be allocated with +pkey_alloc(). An application calls the WRPKRU instruction +directly in order to change access permissions to memory covered +with a key. In this example WRPKRU is wrapped by a C function +called pkey_set(). + + int real_prot = PROT_READ|PROT_WRITE; + pkey = pkey_alloc(0, PKEY_DENY_WRITE); + ptr = mmap(NULL, PAGE_SIZE, PROT_NONE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); + ret = pkey_mprotect(ptr, PAGE_SIZE, real_prot, pkey); + ... application runs here + +Now, if the application needs to update the data at 'ptr', it can +gain access, do the update, then remove its write access: + + pkey_set(pkey, 0); // clear PKEY_DENY_WRITE + *ptr = foo; // assign something + pkey_set(pkey, PKEY_DENY_WRITE); // set PKEY_DENY_WRITE again + +Now when it frees the memory, it will also free the pkey since it +is no longer in use: + + munmap(ptr, PAGE_SIZE); + pkey_free(pkey); + +(Note: pkey_set() is a wrapper for the RDPKRU and WRPKRU instructions. + An example implementation can be found in + tools/testing/selftests/x86/protection_keys.c) + +=========================== Behavior =========================== + +The kernel attempts to make protection keys consistent with the +behavior of a plain mprotect(). For instance if you do this: + + mprotect(ptr, size, PROT_NONE); + something(ptr); + +you can expect the same effects with protection keys when doing this: + + pkey = pkey_alloc(0, PKEY_DISABLE_WRITE | PKEY_DISABLE_READ); + pkey_mprotect(ptr, size, PROT_READ|PROT_WRITE, pkey); + something(ptr); + +That should be true whether something() is a direct access to 'ptr' +like: + + *ptr = foo; + +or when the kernel does the access on the application's behalf like +with a read(): + + read(fd, ptr, 1); + +The kernel will send a SIGSEGV in both cases, but si_code will be set +to SEGV_PKERR when violating protection keys versus SEGV_ACCERR when +the plain mprotect() permissions are violated.
Documentation/x86/protection-keys.txt deleted +0 -85
85 lines not on this page · raw message
Signature · 1 line
1.8.3.1
Re: [RFC v2 11/12]Documentation: Documentation updates.
Quoted 7 lines · Ram Pai
The Documentaton file is moved from x86 into the generic area, since this feature is now supported by more than one archs.
Signed-off-by Ram Pai <[email protected]>--- Documentation/vm/protection-keys.txt | 110 ++++++++++++++++++++++++++++++++++ Documentation/x86/protection-keys.txt | 85 --------------------------
I am not sure whether this is a good idea. There might be specifics for each architecture which need to be detailed again in this new generic one.
Quoted 10 lines
2 files changed, 110 insertions(+), 85 deletions(-) create mode 100644 Documentation/vm/protection-keys.txt delete mode 100644 Documentation/x86/protection-keys.txtDocumentation/vm/protection-keys.txt new file +0
-0
Documentation/vm/protection-keys.txt line 6
Quoted 6 lines
Documentation/vm/protection-keys.txt new file +6
-0@@ -0,0 +1,110 @@ +Memory Protection Keys for Userspace (PKU aka PKEYs) is a CPU feature +found in new generation of intel CPUs on PowerPC CPUs. + +Memory Protection Keys provides a mechanism for enforcing page-based +protections, but without requiring modification of the page tables +when an application changes protection domains.
Does resultant access through protection keys should be a subset of the protection bits enabled through original PTE PROT format ? Does the semantics exactly the same on x86 and powerpc ?
Quoted 18 lines
Documentation/vm/protection-keys.txt new file +18
-0+ + +On Intel: + +It works by dedicating 4 previously ignored bits in each page table +entry to a "protection key", giving 16 possible keys. + +There is also a new user-accessible register (PKRU) with two separate +bits (Access Disable and Write Disable) for each key. Being a CPU +register, PKRU is inherently thread-local, potentially giving each +thread a different set of protections from every other thread. + +There are two new instructions (RDPKRU/WRPKRU) for reading and writing +to the new register. The feature is only available in 64-bit mode, +even though there is theoretically space in the PAE PTEs. These +permissions are enforced on data access only and have no effect on +instruction fetches. +
Documentation/vm/protection-keys.txt line 34
Quoted 10 lines
Documentation/vm/protection-keys.txt new file +10
-0+ +On PowerPC: + +It works by dedicating 5 page table entry to a "protection key", +giving 32 possible keys. + +There is a user-accessible register (AMR) with two separate bits +(Access Disable and Write Disable) for each key. Being a CPU +register, AMR is inherently thread-local, potentially giving each +thread a different set of protections from every other thread.
Small nit. Space needed here.
Quoted 66 lines
Documentation/vm/protection-keys.txt new file +66
-0+NOTE: Disabling read permission does not disable +write and vice-versa. + +The feature is available on 64-bit HPTE mode only. + +'mtspr 0xd, mem' reads the AMR register +'mfspr mem, 0xd' writes into the AMR register. + +Permissions are enforced on data access only and have no effect on +instruction fetches. + +=========================== Syscalls =========================== + +There are 3 system calls which directly interact with pkeys: + + int pkey_alloc(unsigned long flags, unsigned long init_access_rights) + int pkey_free(int pkey); + int pkey_mprotect(unsigned long start, size_t len, + unsigned long prot, int pkey); + +Before a pkey can be used, it must first be allocated with +pkey_alloc(). An application calls the WRPKRU instruction +directly in order to change access permissions to memory covered +with a key. In this example WRPKRU is wrapped by a C function +called pkey_set(). + + int real_prot = PROT_READ|PROT_WRITE; + pkey = pkey_alloc(0, PKEY_DENY_WRITE); + ptr = mmap(NULL, PAGE_SIZE, PROT_NONE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); + ret = pkey_mprotect(ptr, PAGE_SIZE, real_prot, pkey); + ... application runs here + +Now, if the application needs to update the data at 'ptr', it can +gain access, do the update, then remove its write access: + + pkey_set(pkey, 0); // clear PKEY_DENY_WRITE + *ptr = foo; // assign something + pkey_set(pkey, PKEY_DENY_WRITE); // set PKEY_DENY_WRITE again + +Now when it frees the memory, it will also free the pkey since it +is no longer in use: + + munmap(ptr, PAGE_SIZE); + pkey_free(pkey); + +(Note: pkey_set() is a wrapper for the RDPKRU and WRPKRU instructions. + An example implementation can be found in + tools/testing/selftests/x86/protection_keys.c) + +=========================== Behavior =========================== + +The kernel attempts to make protection keys consistent with the +behavior of a plain mprotect(). For instance if you do this: + + mprotect(ptr, size, PROT_NONE); + something(ptr); + +you can expect the same effects with protection keys when doing this: + + pkey = pkey_alloc(0, PKEY_DISABLE_WRITE | PKEY_DISABLE_READ); + pkey_mprotect(ptr, size, PROT_READ|PROT_WRITE, pkey); + something(ptr); + +That should be true whether something() is a direct access to 'ptr' +like: +
Documentation/vm/protection-keys.txt line 110
Quoted 10 lines
Documentation/vm/protection-keys.txt new file +10
-0+ *ptr = foo; + +or when the kernel does the access on the application's behalf like +with a read(): + + read(fd, ptr, 1); + +The kernel will send a SIGSEGV in both cases, but si_code will be set +to SEGV_PKERR when violating protection keys versus SEGV_ACCERR when +the plain mprotect() permissions are violated.
I guess the right thing would be to have three files
* Documentation/vm/protection-keys.txt
- Generic interface, system calls
- Signal handling, error codes
- Semantics of programming with an example
* Documentation/x86/protection-keys.txt
- Number of active protections keys inside an address space
- X86 protection key instruction details
- PTE protection bits placement details
- Page fault handling
- Implementation details a bit ?
* Documentation/powerpc/protection-keys.txt
- Number of active protections keys inside an address space
- Powerpc instructions details
- PTE protection bits placement details
- Page fault handling
- Implementation details a bit ?
Re: [RFC v2 11/12]Documentation: Documentation updates.
Quoted 26 lines · Anshuman Khandual
Quoted 7 lines · Ram Pai
The Documentaton file is moved from x86 into the generic area, since this feature is now supported by more than one archs.
Signed-off-by Ram Pai <[email protected]>--- Documentation/vm/protection-keys.txt | 110 ++++++++++++++++++++++++++++++++++ Documentation/x86/protection-keys.txt | 85 --------------------------I am not sure whether this is a good idea. There might be specifics for each architecture which need to be detailed again in this new generic one.
Quoted 13 lines
2 files changed, 110 insertions(+), 85 deletions(-) create mode 100644 Documentation/vm/protection-keys.txt delete mode 100644 Documentation/x86/protection-keys.txtDocumentation/vm/protection-keys.txt new file +3
-0@@ -0,0 +1,110 @@ +Memory Protection Keys for Userspace (PKU aka PKEYs) is a CPU feature +found in new generation of intel CPUs on PowerPC CPUs. +
Quoted 8 lines
Documentation/vm/protection-keys.txt line 6
Quoted 3 lines
Documentation/vm/protection-keys.txt new file +3
-0+Memory Protection Keys provides a mechanism for enforcing page-based +protections, but without requiring modification of the page tables +when an application changes protection domains.Does resultant access through protection keys should be a subset of the protection bits enabled through original PTE PROT format ? Does the semantics exactly the same on x86 and powerpc ?
The protection key takes precedence over protection done through mprotect.
Yes both on x86 and powerpc we maintain the same semantics.
Quoted 125 lines
Quoted 19 lines
Documentation/vm/protection-keys.txt new file +18
-0+ + +On Intel: + +It works by dedicating 4 previously ignored bits in each page table +entry to a "protection key", giving 16 possible keys. + +There is also a new user-accessible register (PKRU) with two separate +bits (Access Disable and Write Disable) for each key. Being a CPU +register, PKRU is inherently thread-local, potentially giving each +thread a different set of protections from every other thread. + +There are two new instructions (RDPKRU/WRPKRU) for reading and writing +to the new register. The feature is only available in 64-bit mode, +even though there is theoretically space in the PAE PTEs. These +permissions are enforced on data access only and have no effect on +instruction fetches. +Documentation/vm/protection-keys.txt line 34
Quoted 10 lines
Documentation/vm/protection-keys.txt new file +10
-0+ +On PowerPC: + +It works by dedicating 5 page table entry to a "protection key", +giving 32 possible keys. + +There is a user-accessible register (AMR) with two separate bits +(Access Disable and Write Disable) for each key. Being a CPU +register, AMR is inherently thread-local, potentially giving each +thread a different set of protections from every other thread.Small nit. Space needed here.
Quoted 66 lines
Documentation/vm/protection-keys.txt new file +66
-0+NOTE: Disabling read permission does not disable +write and vice-versa. + +The feature is available on 64-bit HPTE mode only. + +'mtspr 0xd, mem' reads the AMR register +'mfspr mem, 0xd' writes into the AMR register. + +Permissions are enforced on data access only and have no effect on +instruction fetches. + +=========================== Syscalls =========================== + +There are 3 system calls which directly interact with pkeys: + + int pkey_alloc(unsigned long flags, unsigned long init_access_rights) + int pkey_free(int pkey); + int pkey_mprotect(unsigned long start, size_t len, + unsigned long prot, int pkey); + +Before a pkey can be used, it must first be allocated with +pkey_alloc(). An application calls the WRPKRU instruction +directly in order to change access permissions to memory covered +with a key. In this example WRPKRU is wrapped by a C function +called pkey_set(). + + int real_prot = PROT_READ|PROT_WRITE; + pkey = pkey_alloc(0, PKEY_DENY_WRITE); + ptr = mmap(NULL, PAGE_SIZE, PROT_NONE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); + ret = pkey_mprotect(ptr, PAGE_SIZE, real_prot, pkey); + ... application runs here + +Now, if the application needs to update the data at 'ptr', it can +gain access, do the update, then remove its write access: + + pkey_set(pkey, 0); // clear PKEY_DENY_WRITE + *ptr = foo; // assign something + pkey_set(pkey, PKEY_DENY_WRITE); // set PKEY_DENY_WRITE again + +Now when it frees the memory, it will also free the pkey since it +is no longer in use: + + munmap(ptr, PAGE_SIZE); + pkey_free(pkey); + +(Note: pkey_set() is a wrapper for the RDPKRU and WRPKRU instructions. + An example implementation can be found in + tools/testing/selftests/x86/protection_keys.c) + +=========================== Behavior =========================== + +The kernel attempts to make protection keys consistent with the +behavior of a plain mprotect(). For instance if you do this: + + mprotect(ptr, size, PROT_NONE); + something(ptr); + +you can expect the same effects with protection keys when doing this: + + pkey = pkey_alloc(0, PKEY_DISABLE_WRITE | PKEY_DISABLE_READ); + pkey_mprotect(ptr, size, PROT_READ|PROT_WRITE, pkey); + something(ptr); + +That should be true whether something() is a direct access to 'ptr' +like: +Documentation/vm/protection-keys.txt line 110
Quoted 10 lines
Documentation/vm/protection-keys.txt new file +10
-0+ *ptr = foo; + +or when the kernel does the access on the application's behalf like +with a read(): + + read(fd, ptr, 1); + +The kernel will send a SIGSEGV in both cases, but si_code will be set +to SEGV_PKERR when violating protection keys versus SEGV_ACCERR when +the plain mprotect() permissions are violated.I guess the right thing would be to have three files
*
Documentation/vm/protection-keys.txt- Generic interface, system calls
- Signal handling, error codes
- Semantics of programming with an example*
Documentation/x86/protection-keys.txt- Number of active protections keys inside an address space
- X86 protection key instruction details
- PTE protection bits placement details
- Page fault handling
- Implementation details a bit ?
Quoted 7 lines
*
Documentation/powerpc/protection-keys.txt- Number of active protections keys inside an address space
- Powerpc instructions details
- PTE protection bits placement details
- Page fault handling
- Implementation details a bit ?
I see the value of your suggestion. This is something that will touch atleast two architectures. Want to hear some more inputs before I do the changes.
Dave Hansen: would like to hear your ideas.
RP
RFC v2 12/12 selftest: Updated protection key selftest
In reply to Ram PaiAdded test support for PowerPC implementation off protection keys.
--- tools/testing/selftests/vm/Makefile | 1 + tools/testing/selftests/vm/pkey-helpers.h | 365 +++++++ tools/testing/selftests/vm/protection_keys.c | 1451 +++++++++++++++++++++++++ tools/testing/selftests/x86/Makefile | 2 +- tools/testing/selftests/x86/pkey-helpers.h | 219 ---- tools/testing/selftests/x86/protection_keys.c | 1395 ------------------------ 6 files changed, 1818 insertions(+), 1615 deletions(-) create mode 100644 tools/testing/selftests/vm/pkey-helpers.h create mode 100644 tools/testing/selftests/vm/protection_keys.c delete mode 100644 tools/testing/selftests/x86/pkey-helpers.h delete mode 100644 tools/testing/selftests/x86/protection_keys.c
tools/testing/selftests/vm/Makefile +1 -0
@@ -17,6 +17,7 @@ TEST_GEN_FILES += transhuge-stress TEST_GEN_FILES += userfaultfd TEST_GEN_FILES += mlock-random-test TEST_GEN_FILES += virtual_address_range +TEST_GEN_FILES += protection_keys TEST_PROGS := run_vmtests
tools/testing/selftests/vm/pkey-helpers.h new file +365 -0
365 lines not on this page · raw message
tools/testing/selftests/vm/protection_keys.c new file +1451 -0
1,451 lines not on this page · raw message
tools/testing/selftests/x86/Makefile +1 -1
@@ -6,7 +6,7 @@ include ../lib.mk TARGETS_C_BOTHBITS := single_step_syscall sysret_ss_attrs syscall_nt ptrace_syscall test_mremap_vdso \ check_initial_reg_state sigreturn ldt_gdt iopl mpx-mini-test ioperm \ - protection_keys test_vdso + test_vdso TARGETS_C_32BIT_ONLY := entry_from_vm86 syscall_arg_fault test_syscall_vdso unwind_vdso \ test_FCMOV test_FCOMI test_FISTTP \ vdso_restorer
tools/testing/selftests/x86/pkey-helpers.h deleted +0 -219
219 lines not on this page · raw message
tools/testing/selftests/x86/protection_keys.c deleted +0 -1395
950 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
1.8.3.1
Part of the diff is not shown here. View the raw message.
Michael Ellerman
reviewerRe: [RFC v2 12/12]selftest: Updated protection key selftest
Quoted 10 lines · Ram Pai
Added test support for PowerPC implementation off protection keys.
Signed-off-by Ram Pai <[email protected]>--- tools/testing/selftests/vm/Makefile | 1 + tools/testing/selftests/vm/pkey-helpers.h | 365 +++++++ tools/testing/selftests/vm/protection_keys.c | 1451 +++++++++++++++++++++++++ tools/testing/selftests/x86/Makefile | 2 +- tools/testing/selftests/x86/pkey-helpers.h | 219 ---- tools/testing/selftests/x86/protection_keys.c | 1395 ------------------------
Please split the move and the addition of the powerpc code into two separate patches (move first). That way we can actually see what you're doing to add powerpc support.
cheers
Re: [RFC v2 12/12]selftest: Updated protection key selftest
In reply to Ram PaiQuoted 3 lines · Ram Pai
Added test support for PowerPC implementation off protection keys.
Signed-off-by Ram Pai <[email protected]>
First of all, there are a lot of instances where we use *pkru* named functions on power even the real implementations have taken care of doing appropriate things. That looks pretty hacky. We need to change them to generic names first before adding both x86 and powerpc procedures inside it.
Re: [RFC v2 12/12]selftest: Updated protection key selftest
Quoted 10 lines · Anshuman Khandual
Quoted 3 lines · Ram Pai
Added test support for PowerPC implementation off protection keys.
Signed-off-by Ram Pai <[email protected]>First of all, there are a lot of instances where we use *pkru* named functions on power even the real implementations have taken care of doing appropriate things. That looks pretty hacky. We need to change them to generic names first before adding both x86 and powerpc procedures inside it.
I have abstracted out the arch-specific code. References to pkru should now be constricted to x86 code only.
The patch, i acknowledge, is not easily reviewable.
As Michael Ellermen mentioned I will break them into two patches. One moves the file and the second does the code changes. That way it will be easy to review.
RP
Quoted 3 lines · Ram Pai
Memory protection keys enable applications to protect its address space from inadvertent access or corruption from itself.
I presume by itself you mean protection between threads?
Quoted 5 lines
The overall idea:
A process allocates a key and associates it with
a address range within its address space.
OK, so this is per VMA?
Quoted 3 lines
The process than can dynamically set read/write
permissions on the key without involving the
kernel.
This bit is not clear, how can the key be set without involving the kernel? I presume you mean the key is set in the PTE's and the access protection values can be set without involving the kernel?
Any code that violates the permissions
Quoted 6 lines
off the address space; as defined by its associated key, will receive a segmentation fault.
This patch series enables the feature on PPC64.
It is enabled on HPTE 64K-page platform.
Quoted 7 lines
ISA3.0 section 5.7.13 describes the detailed specifications.
Testing:
This patch series has passed all the protection key tests available in the selftests directory. The tests are updated to work on both x86 and powerpc.
Balbir
Quoted 6 lines · Balbir Singh
Quoted 3 lines · Ram Pai
Memory protection keys enable applications to protect its address space from inadvertent access or corruption from itself.
I presume by itself you mean protection between threads?
Between threads due to race conditions or from the same thread because of programming error.
Quoted 8 lines
Quoted 6 lines
The overall idea:
A process allocates a key and associates it with
a address range within its address space.OK, so this is per VMA?
Yeah but the same key can be given to multiple VMAs. Any change will effect every VMA who got tagged by it.
Quoted 7 lines
Quoted 4 lines
The process than can dynamically set read/write
permissions on the key without involving the
kernel.This bit is not clear, how can the key be set without involving the kernel? I presume you mean the key is set
With pkey_mprotect() system call, all the effected PTEs get tagged for once. Switching the permission happens just by writing into the register on the fly.
Quoted 2 lines
in the PTE's and the access protection values can be set without involving the kernel?
PTE setting happens once, access protection values can be changed on the fly through register.
Quoted 6 lines · Balbir Singh
Quoted 3 lines · Ram Pai
Memory protection keys enable applications to protect its address space from inadvertent access or corruption from itself.
I presume by itself you mean protection between threads?
Not necessarily. You could have for example a JIT that when it runs the JITed code, only "opens" the keys for the VM itself, preventing the JITed code from "leaking out"
There are plenty of other usages...
Quoted 33 lines
Quoted 5 lines
The overall idea:
A process allocates a key and associates it with
a address range within its address space.OK, so this is per VMA?
Quoted 3 lines
The process than can dynamically set read/write
permissions on the key without involving the
kernel.This bit is not clear, how can the key be set without involving the kernel? I presume you mean the key is set in the PTE's and the access protection values can be set without involving the kernel?
Any code that violates the permissions
Quoted 6 lines
off the address space; as defined by its associated key, will receive a segmentation fault.
This patch series enables the feature on PPC64.
It is enabled on HPTE 64K-page platform.Quoted 7 lines
ISA3.0 section 5.7.13 describes the detailed specifications.
Testing:
This patch series has passed all the protection key tests available in the selftests directory. The tests are updated to work on both x86 and powerpc.Balbir