Skip to content

Commit db3d853

Browse files
kvaneeshozbenh
authored andcommitted
powerpc/mm: handle hugepage size correctly when invalidating hpte entries
If a hash bucket gets full, we "evict" a more/less random entry from it. When we do that we don't invalidate the TLB (hpte_remove) because we assume the old translation is still technically "valid". This implies that when we are invalidating or updating pte, even if HPTE entry is not valid we should do a tlb invalidate. With hugepages, we need to pass the correct actual page size value for tlb invalidation. This change update the patch 0608d69 "powerpc/mm: Always invalidate tlb on hpte invalidate and update" to handle transparent hugepages correctly. Signed-off-by: Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com> Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
1 parent 8998897 commit db3d853

9 files changed

Lines changed: 95 additions & 107 deletions

File tree

‎arch/powerpc/include/asm/machdep.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ struct machdep_calls {
3636
#ifdef CONFIG_PPC64
3737
void (*hpte_invalidate)(unsigned long slot,
3838
unsigned long vpn,
39-
int psize, int ssize,
40-
int local);
39+
int bpsize, int apsize,
40+
int ssize, int local);
4141
long (*hpte_updatepp)(unsigned long slot,
4242
unsigned long newpp,
4343
unsigned long vpn,
44-
int psize, int ssize,
45-
int local);
44+
int bpsize, int apsize,
45+
int ssize, int local);
4646
void (*hpte_updateboltedpp)(unsigned long newpp,
4747
unsigned long ea,
4848
int psize, int ssize);

‎arch/powerpc/kvm/book3s_64_mmu_host.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
void kvmppc_mmu_invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte)
3535
{
3636
ppc_md.hpte_invalidate(pte->slot, pte->host_vpn,
37-
MMU_PAGE_4K, MMU_SEGSIZE_256M,
37+
MMU_PAGE_4K, MMU_PAGE_4K, MMU_SEGSIZE_256M,
3838
false);
3939
}
4040

‎arch/powerpc/mm/hash_low_64.S‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,10 @@ htab_modify_pte:
289289

290290
/* Call ppc_md.hpte_updatepp */
291291
mr r5,r29 /* vpn */
292-
li r6,MMU_PAGE_4K /* page size */
293-
ld r7,STK_PARAM(R9)(r1) /* segment size */
294-
ld r8,STK_PARAM(R8)(r1) /* get "local" param */
292+
li r6,MMU_PAGE_4K /* base page size */
293+
li r7,MMU_PAGE_4K /* actual page size */
294+
ld r8,STK_PARAM(R9)(r1) /* segment size */
295+
ld r9,STK_PARAM(R8)(r1) /* get "local" param */
295296
_GLOBAL(htab_call_hpte_updatepp)
296297
bl . /* Patched by htab_finish_init() */
297298

@@ -649,9 +650,10 @@ htab_modify_pte:
649650

650651
/* Call ppc_md.hpte_updatepp */
651652
mr r5,r29 /* vpn */
652-
li r6,MMU_PAGE_4K /* page size */
653-
ld r7,STK_PARAM(R9)(r1) /* segment size */
654-
ld r8,STK_PARAM(R8)(r1) /* get "local" param */
653+
li r6,MMU_PAGE_4K /* base page size */
654+
li r7,MMU_PAGE_4K /* actual page size */
655+
ld r8,STK_PARAM(R9)(r1) /* segment size */
656+
ld r9,STK_PARAM(R8)(r1) /* get "local" param */
655657
_GLOBAL(htab_call_hpte_updatepp)
656658
bl . /* patched by htab_finish_init() */
657659

@@ -937,9 +939,10 @@ ht64_modify_pte:
937939

938940
/* Call ppc_md.hpte_updatepp */
939941
mr r5,r29 /* vpn */
940-
li r6,MMU_PAGE_64K
941-
ld r7,STK_PARAM(R9)(r1) /* segment size */
942-
ld r8,STK_PARAM(R8)(r1) /* get "local" param */
942+
li r6,MMU_PAGE_64K /* base page size */
943+
li r7,MMU_PAGE_64K /* actual page size */
944+
ld r8,STK_PARAM(R9)(r1) /* segment size */
945+
ld r9,STK_PARAM(R8)(r1) /* get "local" param */
943946
_GLOBAL(ht64_call_hpte_updatepp)
944947
bl . /* patched by htab_finish_init() */
945948

‎arch/powerpc/mm/hash_native_64.c‎

Lines changed: 45 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -273,82 +273,30 @@ static long native_hpte_remove(unsigned long hpte_group)
273273
return i;
274274
}
275275

276-
static inline int __hpte_actual_psize(unsigned int lp, int psize)
277-
{
278-
int i, shift;
279-
unsigned int mask;
280-
281-
/* start from 1 ignoring MMU_PAGE_4K */
282-
for (i = 1; i < MMU_PAGE_COUNT; i++) {
283-
284-
/* invalid penc */
285-
if (mmu_psize_defs[psize].penc[i] == -1)
286-
continue;
287-
/*
288-
* encoding bits per actual page size
289-
* PTE LP actual page size
290-
* rrrr rrrz >=8KB
291-
* rrrr rrzz >=16KB
292-
* rrrr rzzz >=32KB
293-
* rrrr zzzz >=64KB
294-
* .......
295-
*/
296-
shift = mmu_psize_defs[i].shift - LP_SHIFT;
297-
if (shift > LP_BITS)
298-
shift = LP_BITS;
299-
mask = (1 << shift) - 1;
300-
if ((lp & mask) == mmu_psize_defs[psize].penc[i])
301-
return i;
302-
}
303-
return -1;
304-
}
305-
306-
static inline int hpte_actual_psize(struct hash_pte *hptep, int psize)
307-
{
308-
/* Look at the 8 bit LP value */
309-
unsigned int lp = (hptep->r >> LP_SHIFT) & ((1 << LP_BITS) - 1);
310-
311-
if (!(hptep->v & HPTE_V_VALID))
312-
return -1;
313-
314-
/* First check if it is large page */
315-
if (!(hptep->v & HPTE_V_LARGE))
316-
return MMU_PAGE_4K;
317-
318-
return __hpte_actual_psize(lp, psize);
319-
}
320-
321276
static long native_hpte_updatepp(unsigned long slot, unsigned long newpp,
322-
unsigned long vpn, int psize, int ssize,
323-
int local)
277+
unsigned long vpn, int bpsize,
278+
int apsize, int ssize, int local)
324279
{
325280
struct hash_pte *hptep = htab_address + slot;
326281
unsigned long hpte_v, want_v;
327282
int ret = 0;
328-
int actual_psize;
329283

330-
want_v = hpte_encode_avpn(vpn, psize, ssize);
284+
want_v = hpte_encode_avpn(vpn, bpsize, ssize);
331285

332286
DBG_LOW(" update(vpn=%016lx, avpnv=%016lx, group=%lx, newpp=%lx)",
333287
vpn, want_v & HPTE_V_AVPN, slot, newpp);
334288

335289
native_lock_hpte(hptep);
336290

337291
hpte_v = hptep->v;
338-
actual_psize = hpte_actual_psize(hptep, psize);
339292
/*
340293
* We need to invalidate the TLB always because hpte_remove doesn't do
341294
* a tlb invalidate. If a hash bucket gets full, we "evict" a more/less
342295
* random entry from it. When we do that we don't invalidate the TLB
343296
* (hpte_remove) because we assume the old translation is still
344297
* technically "valid".
345298
*/
346-
if (actual_psize < 0) {
347-
actual_psize = psize;
348-
ret = -1;
349-
goto err_out;
350-
}
351-
if (!HPTE_V_COMPARE(hpte_v, want_v)) {
299+
if (!HPTE_V_COMPARE(hpte_v, want_v) || !(hpte_v & HPTE_V_VALID)) {
352300
DBG_LOW(" -> miss\n");
353301
ret = -1;
354302
} else {
@@ -357,11 +305,10 @@ static long native_hpte_updatepp(unsigned long slot, unsigned long newpp,
357305
hptep->r = (hptep->r & ~(HPTE_R_PP | HPTE_R_N)) |
358306
(newpp & (HPTE_R_PP | HPTE_R_N | HPTE_R_C));
359307
}
360-
err_out:
361308
native_unlock_hpte(hptep);
362309

363310
/* Ensure it is out of the tlb too. */
364-
tlbie(vpn, psize, actual_psize, ssize, local);
311+
tlbie(vpn, bpsize, apsize, ssize, local);
365312

366313
return ret;
367314
}
@@ -402,7 +349,6 @@ static long native_hpte_find(unsigned long vpn, int psize, int ssize)
402349
static void native_hpte_updateboltedpp(unsigned long newpp, unsigned long ea,
403350
int psize, int ssize)
404351
{
405-
int actual_psize;
406352
unsigned long vpn;
407353
unsigned long vsid;
408354
long slot;
@@ -415,60 +361,82 @@ static void native_hpte_updateboltedpp(unsigned long newpp, unsigned long ea,
415361
if (slot == -1)
416362
panic("could not find page to bolt\n");
417363
hptep = htab_address + slot;
418-
actual_psize = hpte_actual_psize(hptep, psize);
419-
if (actual_psize < 0)
420-
actual_psize = psize;
421364

422365
/* Update the HPTE */
423366
hptep->r = (hptep->r & ~(HPTE_R_PP | HPTE_R_N)) |
424367
(newpp & (HPTE_R_PP | HPTE_R_N));
425-
426-
/* Ensure it is out of the tlb too. */
427-
tlbie(vpn, psize, actual_psize, ssize, 0);
368+
/*
369+
* Ensure it is out of the tlb too. Bolted entries base and
370+
* actual page size will be same.
371+
*/
372+
tlbie(vpn, psize, psize, ssize, 0);
428373
}
429374

430375
static void native_hpte_invalidate(unsigned long slot, unsigned long vpn,
431-
int psize, int ssize, int local)
376+
int bpsize, int apsize, int ssize, int local)
432377
{
433378
struct hash_pte *hptep = htab_address + slot;
434379
unsigned long hpte_v;
435380
unsigned long want_v;
436381
unsigned long flags;
437-
int actual_psize;
438382

439383
local_irq_save(flags);
440384

441385
DBG_LOW(" invalidate(vpn=%016lx, hash: %lx)\n", vpn, slot);
442386

443-
want_v = hpte_encode_avpn(vpn, psize, ssize);
387+
want_v = hpte_encode_avpn(vpn, bpsize, ssize);
444388
native_lock_hpte(hptep);
445389
hpte_v = hptep->v;
446390

447-
actual_psize = hpte_actual_psize(hptep, psize);
448391
/*
449392
* We need to invalidate the TLB always because hpte_remove doesn't do
450393
* a tlb invalidate. If a hash bucket gets full, we "evict" a more/less
451394
* random entry from it. When we do that we don't invalidate the TLB
452395
* (hpte_remove) because we assume the old translation is still
453396
* technically "valid".
454397
*/
455-
if (actual_psize < 0) {
456-
actual_psize = psize;
457-
native_unlock_hpte(hptep);
458-
goto err_out;
459-
}
460-
if (!HPTE_V_COMPARE(hpte_v, want_v))
398+
if (!HPTE_V_COMPARE(hpte_v, want_v) || !(hpte_v & HPTE_V_VALID))
461399
native_unlock_hpte(hptep);
462400
else
463401
/* Invalidate the hpte. NOTE: this also unlocks it */
464402
hptep->v = 0;
465403

466-
err_out:
467404
/* Invalidate the TLB */
468-
tlbie(vpn, psize, actual_psize, ssize, local);
405+
tlbie(vpn, bpsize, apsize, ssize, local);
406+
469407
local_irq_restore(flags);
470408
}
471409

410+
static inline int __hpte_actual_psize(unsigned int lp, int psize)
411+
{
412+
int i, shift;
413+
unsigned int mask;
414+
415+
/* start from 1 ignoring MMU_PAGE_4K */
416+
for (i = 1; i < MMU_PAGE_COUNT; i++) {
417+
418+
/* invalid penc */
419+
if (mmu_psize_defs[psize].penc[i] == -1)
420+
continue;
421+
/*
422+
* encoding bits per actual page size
423+
* PTE LP actual page size
424+
* rrrr rrrz >=8KB
425+
* rrrr rrzz >=16KB
426+
* rrrr rzzz >=32KB
427+
* rrrr zzzz >=64KB
428+
* .......
429+
*/
430+
shift = mmu_psize_defs[i].shift - LP_SHIFT;
431+
if (shift > LP_BITS)
432+
shift = LP_BITS;
433+
mask = (1 << shift) - 1;
434+
if ((lp & mask) == mmu_psize_defs[psize].penc[i])
435+
return i;
436+
}
437+
return -1;
438+
}
439+
472440
static void hpte_decode(struct hash_pte *hpte, unsigned long slot,
473441
int *psize, int *apsize, int *ssize, unsigned long *vpn)
474442
{

‎arch/powerpc/mm/hash_utils_64.c‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,7 +1232,11 @@ void flush_hash_page(unsigned long vpn, real_pte_t pte, int psize, int ssize,
12321232
slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
12331233
slot += hidx & _PTEIDX_GROUP_IX;
12341234
DBG_LOW(" sub %ld: hash=%lx, hidx=%lx\n", index, slot, hidx);
1235-
ppc_md.hpte_invalidate(slot, vpn, psize, ssize, local);
1235+
/*
1236+
* We use same base page size and actual psize, because we don't
1237+
* use these functions for hugepage
1238+
*/
1239+
ppc_md.hpte_invalidate(slot, vpn, psize, psize, ssize, local);
12361240
} pte_iterate_hashed_end();
12371241

12381242
#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
@@ -1365,7 +1369,8 @@ static void kernel_unmap_linear_page(unsigned long vaddr, unsigned long lmi)
13651369
hash = ~hash;
13661370
slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
13671371
slot += hidx & _PTEIDX_GROUP_IX;
1368-
ppc_md.hpte_invalidate(slot, vpn, mmu_linear_psize, mmu_kernel_ssize, 0);
1372+
ppc_md.hpte_invalidate(slot, vpn, mmu_linear_psize, mmu_linear_psize,
1373+
mmu_kernel_ssize, 0);
13691374
}
13701375

13711376
void kernel_map_pages(struct page *page, int numpages, int enable)

‎arch/powerpc/mm/hugetlbpage-hash64.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ int __hash_page_huge(unsigned long ea, unsigned long access, unsigned long vsid,
8181
slot += (old_pte & _PAGE_F_GIX) >> 12;
8282

8383
if (ppc_md.hpte_updatepp(slot, rflags, vpn, mmu_psize,
84-
ssize, local) == -1)
84+
mmu_psize, ssize, local) == -1)
8585
old_pte &= ~_PAGE_HPTEFLAGS;
8686
}
8787

‎arch/powerpc/platforms/cell/beat_htab.c‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ static void beat_lpar_hptab_clear(void)
185185
static long beat_lpar_hpte_updatepp(unsigned long slot,
186186
unsigned long newpp,
187187
unsigned long vpn,
188-
int psize, int ssize, int local)
188+
int psize, int apsize,
189+
int ssize, int local)
189190
{
190191
unsigned long lpar_rc;
191192
u64 dummy0, dummy1;
@@ -274,7 +275,8 @@ static void beat_lpar_hpte_updateboltedpp(unsigned long newpp,
274275
}
275276

276277
static void beat_lpar_hpte_invalidate(unsigned long slot, unsigned long vpn,
277-
int psize, int ssize, int local)
278+
int psize, int apsize,
279+
int ssize, int local)
278280
{
279281
unsigned long want_v;
280282
unsigned long lpar_rc;
@@ -364,9 +366,10 @@ static long beat_lpar_hpte_insert_v3(unsigned long hpte_group,
364366
* already zero. For now I am paranoid.
365367
*/
366368
static long beat_lpar_hpte_updatepp_v3(unsigned long slot,
367-
unsigned long newpp,
368-
unsigned long vpn,
369-
int psize, int ssize, int local)
369+
unsigned long newpp,
370+
unsigned long vpn,
371+
int psize, int apsize,
372+
int ssize, int local)
370373
{
371374
unsigned long lpar_rc;
372375
unsigned long want_v;
@@ -394,7 +397,8 @@ static long beat_lpar_hpte_updatepp_v3(unsigned long slot,
394397
}
395398

396399
static void beat_lpar_hpte_invalidate_v3(unsigned long slot, unsigned long vpn,
397-
int psize, int ssize, int local)
400+
int psize, int apsize,
401+
int ssize, int local)
398402
{
399403
unsigned long want_v;
400404
unsigned long lpar_rc;

‎arch/powerpc/platforms/ps3/htab.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ static long ps3_hpte_remove(unsigned long hpte_group)
109109
}
110110

111111
static long ps3_hpte_updatepp(unsigned long slot, unsigned long newpp,
112-
unsigned long vpn, int psize, int ssize, int local)
112+
unsigned long vpn, int psize, int apsize,
113+
int ssize, int local)
113114
{
114115
int result;
115116
u64 hpte_v, want_v, hpte_rs;
@@ -162,7 +163,7 @@ static void ps3_hpte_updateboltedpp(unsigned long newpp, unsigned long ea,
162163
}
163164

164165
static void ps3_hpte_invalidate(unsigned long slot, unsigned long vpn,
165-
int psize, int ssize, int local)
166+
int psize, int apsize, int ssize, int local)
166167
{
167168
unsigned long flags;
168169
int result;

0 commit comments

Comments
 (0)