Skip to content

Commit 9d2edb1

Browse files
Ram Paimpe
authored andcommitted
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 bit numbers are big-endian as defined in the ISA3.0 The patch does the following change to the 4k HTPE backed 64K PTE's format. H_PAGE_BUSY moves from bit 3 to bit 9 (B bit in the figure below) V0 which occupied bit 4 is not used anymore. V1 which occupied bit 5 is not used anymore. V2 which occupied bit 6 is not used anymore. V3 which occupied bit 7 is not used anymore. Before the patch, the 4k backed 64k PTE format was as follows 0 1 2 3 4 5 6 7 8 9 10...........................63 : : : : : : : : : : : : v v v v v v v v v v v v ,-,-,-,-,--,--,--,--,-,-,-,-,-,------------------,-,-,-, |x|x|x|B|V0|V1|V2|V3|x| | |x|x|................|x|x|x|x| <- primary pte '_'_'_'_'__'__'__'__'_'_'_'_'_'________________'_'_'_'_' |S|G|I|X|S |G |I |X |S|G|I|X|..................|S|G|I|X| <- secondary pte '_'_'_'_'__'__'__'__'_'_'_'_'__________________'_'_'_'_' After the patch, the 4k backed 64k PTE format is as follows 0 1 2 3 4 5 6 7 8 9 10...........................63 : : : : : : : : : : : : v v v v v v v v v v v v ,-,-,-,-,--,--,--,--,-,-,-,-,-,------------------,-,-,-, |x|x|x| | | | | |x|B| |x|x|................|.|.|.|.| <- primary pte '_'_'_'_'__'__'__'__'_'_'_'_'_'________________'_'_'_'_' |S|G|I|X|S |G |I |X |S|G|I|X|..................|S|G|I|X| <- secondary pte '_'_'_'_'__'__'__'__'_'_'_'_'__________________'_'_'_'_' the four bits S,G,I,X (one quadruplet per 4k HPTE) that cache the hash-bucket slot value, is initialized to 1,1,1,1 indicating -- an invalid slot. If a HPTE gets cached in a 1111 slot(i.e 7th slot of secondary hash bucket), it is released immediately. In other words, even though 1111 is a valid slot value in the hash bucket, we consider it invalid and release the slot and the HPTE. This gives us the opportunity to determine the validity of S,G,I,X bits based on its contents and not on any of the bits V0,V1,V2 or V3 in the primary PTE When we release a HPTE cached in the 1111 slot we also release a legitimate slot in the primary hash bucket and unmap its corresponding HPTE. This is to ensure that we do get a HPTE cached in a slot of the primary hash bucket, the next time we retry. Though treating 1111 slot as invalid, reduces the number of available slots in the hash bucket and may have an effect on the performance, the probabilty of hitting a 1111 slot is extermely low. Compared to the current scheme, the above scheme reduces the number of false hash table updates significantly and has the added advantage of releasing four valuable PTE bits for other purpose. NOTE:even though bits 3, 4, 5, 6, 7 are not used when the 64K PTE is backed by 4k HPTE, they continue to be used if the PTE gets backed by 64k HPTE. The next patch will decouple that aswell, and truely release the bits. This idea was jointly developed by Paul Mackerras, Aneesh, Michael Ellermen and myself. 4K PTE format remains unchanged currently. The patch does the following code changes a) PTE flags are split between 64k and 4k header files. b) __hash_page_4K() is reimplemented to reflect the above logic. Acked-by: Balbir Singh <[email protected]> Reviewed-by: Aneesh Kumar K.V <[email protected]> Signed-off-by: Ram Pai <[email protected]> Signed-off-by: Michael Ellerman <[email protected]>
1 parent 318995b commit 9d2edb1

File tree

5 files changed

+61
-57
lines changed

5 files changed

+61
-57
lines changed

arch/powerpc/include/asm/book3s/64/hash-4k.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#define H_PUD_TABLE_SIZE (sizeof(pud_t) << H_PUD_INDEX_SIZE)
1818
#define H_PGD_TABLE_SIZE (sizeof(pgd_t) << H_PGD_INDEX_SIZE)
1919

20+
#define H_PAGE_BUSY _RPAGE_RSV1 /* software: PTE & hash are busy */
21+
2022
/* PTE flags to conserve for HPTE identification */
2123
#define _PAGE_HPTEFLAGS (H_PAGE_BUSY | H_PAGE_HASHPTE | \
2224
H_PAGE_F_SECOND | H_PAGE_F_GIX)

arch/powerpc/include/asm/book3s/64/hash-64k.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,14 @@
1313
*/
1414
#define H_PAGE_COMBO _RPAGE_RPN0 /* this is a combo 4k page */
1515
#define H_PAGE_4K_PFN _RPAGE_RPN1 /* PFN is for a single 4k page */
16+
#define H_PAGE_BUSY _RPAGE_RPN42 /* software: PTE & hash are busy */
17+
1618
/*
1719
* We need to differentiate between explicit huge page and THP huge
1820
* page, since THP huge page also need to track real subpage details
1921
*/
2022
#define H_PAGE_THP_HUGE H_PAGE_4K_PFN
2123

22-
/*
23-
* Used to track subpage group valid if H_PAGE_COMBO is set
24-
* This overloads H_PAGE_F_GIX and H_PAGE_F_SECOND
25-
*/
26-
#define H_PAGE_COMBO_VALID (H_PAGE_F_GIX | H_PAGE_F_SECOND)
27-
2824
/* PTE flags to conserve for HPTE identification */
2925
#define _PAGE_HPTEFLAGS (H_PAGE_BUSY | H_PAGE_F_SECOND | \
3026
H_PAGE_F_GIX | H_PAGE_HASHPTE | H_PAGE_COMBO)
@@ -69,6 +65,7 @@ static inline real_pte_t __real_pte(pte_t pte, pte_t *ptep)
6965
}
7066

7167
#define HIDX_BITS(x, index) (x << (index << 2))
68+
#define INVALID_RPTE_HIDX ~(0x0UL)
7269

7370
static inline unsigned long __rpte_to_hidx(real_pte_t rpte, unsigned long index)
7471
{

arch/powerpc/include/asm/book3s/64/hash.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
*/
1111
#define H_PTE_NONE_MASK _PAGE_HPTEFLAGS
1212
#define H_PAGE_F_GIX_SHIFT 56
13-
#define H_PAGE_BUSY _RPAGE_RSV1 /* software: PTE & hash are busy */
1413
#define H_PAGE_F_SECOND _RPAGE_RSV2 /* HPTE is in 2ndary HPTEG */
1514
#define H_PAGE_F_GIX (_RPAGE_RSV3 | _RPAGE_RSV4 | _RPAGE_RPN44)
1615
#define H_PAGE_HASHPTE _RPAGE_RPN43 /* PTE has associated HPTE */

arch/powerpc/mm/hash64_64k.c

Lines changed: 53 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -15,47 +15,34 @@
1515
#include <linux/mm.h>
1616
#include <asm/machdep.h>
1717
#include <asm/mmu.h>
18+
1819
/*
19-
* index from 0 - 15
20+
* Return true, if the entry has a slot value which
21+
* the software considers as invalid.
2022
*/
21-
bool __rpte_sub_valid(real_pte_t rpte, unsigned long index)
23+
static inline bool hpte_soft_invalid(unsigned long hidx)
2224
{
23-
unsigned long g_idx;
24-
unsigned long ptev = pte_val(rpte.pte);
25-
26-
g_idx = (ptev & H_PAGE_COMBO_VALID) >> H_PAGE_F_GIX_SHIFT;
27-
index = index >> 2;
28-
if (g_idx & (0x1 << index))
29-
return true;
30-
else
31-
return false;
25+
return ((hidx & 0xfUL) == 0xfUL);
3226
}
27+
3328
/*
3429
* index from 0 - 15
3530
*/
36-
static unsigned long mark_subptegroup_valid(unsigned long ptev, unsigned long index)
31+
bool __rpte_sub_valid(real_pte_t rpte, unsigned long index)
3732
{
38-
unsigned long g_idx;
39-
40-
if (!(ptev & H_PAGE_COMBO))
41-
return ptev;
42-
index = index >> 2;
43-
g_idx = 0x1 << index;
44-
45-
return ptev | (g_idx << H_PAGE_F_GIX_SHIFT);
33+
return !(hpte_soft_invalid(__rpte_to_hidx(rpte, index)));
4634
}
4735

4836
int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid,
4937
pte_t *ptep, unsigned long trap, unsigned long flags,
5038
int ssize, int subpg_prot)
5139
{
5240
real_pte_t rpte;
53-
unsigned long *hidxp;
5441
unsigned long hpte_group;
5542
unsigned int subpg_index;
56-
unsigned long rflags, pa, hidx;
43+
unsigned long rflags, pa;
5744
unsigned long old_pte, new_pte, subpg_pte;
58-
unsigned long vpn, hash, slot;
45+
unsigned long vpn, hash, slot, gslot;
5946
unsigned long shift = mmu_psize_defs[MMU_PAGE_4K].shift;
6047

6148
/*
@@ -126,18 +113,14 @@ int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid,
126113
if (__rpte_sub_valid(rpte, subpg_index)) {
127114
int ret;
128115

129-
hash = hpt_hash(vpn, shift, ssize);
130-
hidx = __rpte_to_hidx(rpte, subpg_index);
131-
if (hidx & _PTEIDX_SECONDARY)
132-
hash = ~hash;
133-
slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
134-
slot += hidx & _PTEIDX_GROUP_IX;
135-
136-
ret = mmu_hash_ops.hpte_updatepp(slot, rflags, vpn,
116+
gslot = pte_get_hash_gslot(vpn, shift, ssize, rpte,
117+
subpg_index);
118+
ret = mmu_hash_ops.hpte_updatepp(gslot, rflags, vpn,
137119
MMU_PAGE_4K, MMU_PAGE_4K,
138120
ssize, flags);
121+
139122
/*
140-
*if we failed because typically the HPTE wasn't really here
123+
* If we failed because typically the HPTE wasn't really here
141124
* we try an insertion.
142125
*/
143126
if (ret == -1)
@@ -148,6 +131,14 @@ int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid,
148131
}
149132

150133
htab_insert_hpte:
134+
135+
/*
136+
* Initialize all hidx entries to invalid value, the first time
137+
* the PTE is about to allocate a 4K HPTE.
138+
*/
139+
if (!(old_pte & H_PAGE_COMBO))
140+
rpte.hidx = INVALID_RPTE_HIDX;
141+
151142
/*
152143
* handle H_PAGE_4K_PFN case
153144
*/
@@ -172,15 +163,39 @@ int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid,
172163
* Primary is full, try the secondary
173164
*/
174165
if (unlikely(slot == -1)) {
166+
bool soft_invalid;
167+
175168
hpte_group = ((~hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
176169
slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa,
177170
rflags, HPTE_V_SECONDARY,
178171
MMU_PAGE_4K, MMU_PAGE_4K,
179172
ssize);
180-
if (slot == -1) {
181-
if (mftb() & 0x1)
173+
174+
soft_invalid = hpte_soft_invalid(slot);
175+
if (unlikely(soft_invalid)) {
176+
/*
177+
* We got a valid slot from a hardware point of view.
178+
* but we cannot use it, because we use this special
179+
* value; as defined by hpte_soft_invalid(), to track
180+
* invalid slots. We cannot use it. So invalidate it.
181+
*/
182+
gslot = slot & _PTEIDX_GROUP_IX;
183+
mmu_hash_ops.hpte_invalidate(hpte_group + gslot, vpn,
184+
MMU_PAGE_4K, MMU_PAGE_4K,
185+
ssize, 0);
186+
}
187+
188+
if (unlikely(slot == -1 || soft_invalid)) {
189+
/*
190+
* For soft invalid slot, let's ensure that we release a
191+
* slot from the primary, with the hope that we will
192+
* acquire that slot next time we try. This will ensure
193+
* that we do not get the same soft-invalid slot.
194+
*/
195+
if (soft_invalid || (mftb() & 0x1))
182196
hpte_group = ((hash & htab_hash_mask) *
183197
HPTES_PER_GROUP) & ~0x7UL;
198+
184199
mmu_hash_ops.hpte_remove(hpte_group);
185200
/*
186201
* FIXME!! Should be try the group from which we removed ?
@@ -198,21 +213,10 @@ int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid,
198213
MMU_PAGE_4K, MMU_PAGE_4K, old_pte);
199214
return -1;
200215
}
201-
/*
202-
* Insert slot number & secondary bit in PTE second half,
203-
* clear H_PAGE_BUSY and set appropriate HPTE slot bit
204-
* Since we have H_PAGE_BUSY set on ptep, we can be sure
205-
* nobody is undating hidx.
206-
*/
207-
hidxp = (unsigned long *)(ptep + PTRS_PER_PTE);
208-
rpte.hidx &= ~(0xfUL << (subpg_index << 2));
209-
*hidxp = rpte.hidx | (slot << (subpg_index << 2));
210-
new_pte = mark_subptegroup_valid(new_pte, subpg_index);
211-
new_pte |= H_PAGE_HASHPTE;
212-
/*
213-
* check __real_pte for details on matching smp_rmb()
214-
*/
215-
smp_wmb();
216+
217+
new_pte |= pte_set_hidx(ptep, rpte, subpg_index, slot);
218+
new_pte |= H_PAGE_HASHPTE;
219+
216220
*ptep = __pte(new_pte & ~H_PAGE_BUSY);
217221
return 0;
218222
}

arch/powerpc/mm/hash_utils_64.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -979,8 +979,9 @@ void __init hash__early_init_devtree(void)
979979

980980
void __init hash__early_init_mmu(void)
981981
{
982+
#ifndef CONFIG_PPC_64K_PAGES
982983
/*
983-
* We have code in __hash_page_64K() and elsewhere, which assumes it can
984+
* We have code in __hash_page_4K() and elsewhere, which assumes it can
984985
* do the following:
985986
* new_pte |= (slot << H_PAGE_F_GIX_SHIFT) & (H_PAGE_F_SECOND | H_PAGE_F_GIX);
986987
*
@@ -991,6 +992,7 @@ void __init hash__early_init_mmu(void)
991992
* with a BUILD_BUG_ON().
992993
*/
993994
BUILD_BUG_ON(H_PAGE_F_SECOND != (1ul << (H_PAGE_F_GIX_SHIFT + 3)));
995+
#endif /* CONFIG_PPC_64K_PAGES */
994996

995997
htab_init_page_sizes();
996998

0 commit comments

Comments
 (0)