Skip to content

Commit a6d6024

Browse files
committed
asm-generic/tlb: Track which levels of the page tables have been cleared
It is common for architectures with hugepage support to require only a single TLB invalidation operation per hugepage during unmap(), rather than iterating through the mapping at a PAGE_SIZE increment. Currently, however, the level in the page table where the unmap() operation occurs is not stored in the mmu_gather structure, therefore forcing architectures to issue additional TLB invalidation operations or to give up and over-invalidate by e.g. invalidating the entire TLB. Ideally, we could add an interval rbtree to the mmu_gather structure, which would allow us to associate the correct mapping granule with the various sub-mappings within the range being invalidated. However, this is costly in terms of book-keeping and memory management, so instead we approximate by keeping track of the page table levels that are cleared and provide a means to query the smallest granule required for invalidation. Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org> Acked-by: Nicholas Piggin <npiggin@gmail.com> Signed-off-by: Will Deacon <will.deacon@arm.com>
1 parent 22a61c3 commit a6d6024

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

include/asm-generic/tlb.h

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,14 @@ struct mmu_gather {
116116
*/
117117
unsigned int freed_tables : 1;
118118

119+
/*
120+
* at which levels have we cleared entries?
121+
*/
122+
unsigned int cleared_ptes : 1;
123+
unsigned int cleared_pmds : 1;
124+
unsigned int cleared_puds : 1;
125+
unsigned int cleared_p4ds : 1;
126+
119127
struct mmu_gather_batch *active;
120128
struct mmu_gather_batch local;
121129
struct page *__pages[MMU_GATHER_BUNDLE];
@@ -150,6 +158,10 @@ static inline void __tlb_reset_range(struct mmu_gather *tlb)
150158
tlb->end = 0;
151159
}
152160
tlb->freed_tables = 0;
161+
tlb->cleared_ptes = 0;
162+
tlb->cleared_pmds = 0;
163+
tlb->cleared_puds = 0;
164+
tlb->cleared_p4ds = 0;
153165
}
154166

155167
static inline void tlb_flush_mmu_tlbonly(struct mmu_gather *tlb)
@@ -199,6 +211,25 @@ static inline void tlb_remove_check_page_size_change(struct mmu_gather *tlb,
199211
}
200212
#endif
201213

214+
static inline unsigned long tlb_get_unmap_shift(struct mmu_gather *tlb)
215+
{
216+
if (tlb->cleared_ptes)
217+
return PAGE_SHIFT;
218+
if (tlb->cleared_pmds)
219+
return PMD_SHIFT;
220+
if (tlb->cleared_puds)
221+
return PUD_SHIFT;
222+
if (tlb->cleared_p4ds)
223+
return P4D_SHIFT;
224+
225+
return PAGE_SHIFT;
226+
}
227+
228+
static inline unsigned long tlb_get_unmap_size(struct mmu_gather *tlb)
229+
{
230+
return 1UL << tlb_get_unmap_shift(tlb);
231+
}
232+
202233
/*
203234
* In the case of tlb vma handling, we can optimise these away in the
204235
* case where we're doing a full MM flush. When we're doing a munmap,
@@ -232,13 +263,19 @@ static inline void tlb_remove_check_page_size_change(struct mmu_gather *tlb,
232263
#define tlb_remove_tlb_entry(tlb, ptep, address) \
233264
do { \
234265
__tlb_adjust_range(tlb, address, PAGE_SIZE); \
266+
tlb->cleared_ptes = 1; \
235267
__tlb_remove_tlb_entry(tlb, ptep, address); \
236268
} while (0)
237269

238-
#define tlb_remove_huge_tlb_entry(h, tlb, ptep, address) \
239-
do { \
240-
__tlb_adjust_range(tlb, address, huge_page_size(h)); \
241-
__tlb_remove_tlb_entry(tlb, ptep, address); \
270+
#define tlb_remove_huge_tlb_entry(h, tlb, ptep, address) \
271+
do { \
272+
unsigned long _sz = huge_page_size(h); \
273+
__tlb_adjust_range(tlb, address, _sz); \
274+
if (_sz == PMD_SIZE) \
275+
tlb->cleared_pmds = 1; \
276+
else if (_sz == PUD_SIZE) \
277+
tlb->cleared_puds = 1; \
278+
__tlb_remove_tlb_entry(tlb, ptep, address); \
242279
} while (0)
243280

244281
/**
@@ -252,6 +289,7 @@ static inline void tlb_remove_check_page_size_change(struct mmu_gather *tlb,
252289
#define tlb_remove_pmd_tlb_entry(tlb, pmdp, address) \
253290
do { \
254291
__tlb_adjust_range(tlb, address, HPAGE_PMD_SIZE); \
292+
tlb->cleared_pmds = 1; \
255293
__tlb_remove_pmd_tlb_entry(tlb, pmdp, address); \
256294
} while (0)
257295

@@ -266,6 +304,7 @@ static inline void tlb_remove_check_page_size_change(struct mmu_gather *tlb,
266304
#define tlb_remove_pud_tlb_entry(tlb, pudp, address) \
267305
do { \
268306
__tlb_adjust_range(tlb, address, HPAGE_PUD_SIZE); \
307+
tlb->cleared_puds = 1; \
269308
__tlb_remove_pud_tlb_entry(tlb, pudp, address); \
270309
} while (0)
271310

@@ -291,7 +330,8 @@ static inline void tlb_remove_check_page_size_change(struct mmu_gather *tlb,
291330
#define pte_free_tlb(tlb, ptep, address) \
292331
do { \
293332
__tlb_adjust_range(tlb, address, PAGE_SIZE); \
294-
tlb->freed_tables = 1; \
333+
tlb->freed_tables = 1; \
334+
tlb->cleared_pmds = 1; \
295335
__pte_free_tlb(tlb, ptep, address); \
296336
} while (0)
297337
#endif
@@ -300,7 +340,8 @@ static inline void tlb_remove_check_page_size_change(struct mmu_gather *tlb,
300340
#define pmd_free_tlb(tlb, pmdp, address) \
301341
do { \
302342
__tlb_adjust_range(tlb, address, PAGE_SIZE); \
303-
tlb->freed_tables = 1; \
343+
tlb->freed_tables = 1; \
344+
tlb->cleared_puds = 1; \
304345
__pmd_free_tlb(tlb, pmdp, address); \
305346
} while (0)
306347
#endif
@@ -310,7 +351,8 @@ static inline void tlb_remove_check_page_size_change(struct mmu_gather *tlb,
310351
#define pud_free_tlb(tlb, pudp, address) \
311352
do { \
312353
__tlb_adjust_range(tlb, address, PAGE_SIZE); \
313-
tlb->freed_tables = 1; \
354+
tlb->freed_tables = 1; \
355+
tlb->cleared_p4ds = 1; \
314356
__pud_free_tlb(tlb, pudp, address); \
315357
} while (0)
316358
#endif
@@ -321,7 +363,7 @@ static inline void tlb_remove_check_page_size_change(struct mmu_gather *tlb,
321363
#define p4d_free_tlb(tlb, pudp, address) \
322364
do { \
323365
__tlb_adjust_range(tlb, address, PAGE_SIZE); \
324-
tlb->freed_tables = 1; \
366+
tlb->freed_tables = 1; \
325367
__p4d_free_tlb(tlb, pudp, address); \
326368
} while (0)
327369
#endif

mm/memory.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,10 @@ void arch_tlb_finish_mmu(struct mmu_gather *tlb,
267267
{
268268
struct mmu_gather_batch *batch, *next;
269269

270-
if (force)
270+
if (force) {
271+
__tlb_reset_range(tlb);
271272
__tlb_adjust_range(tlb, start, end - start);
273+
}
272274

273275
tlb_flush_mmu(tlb);
274276

0 commit comments

Comments
 (0)