Skip to content

Commit fb7332a

Browse files
committed
mmu_gather: move minimal range calculations into generic code
On architectures with hardware broadcasting of TLB invalidation messages , it makes sense to reduce the range of the mmu_gather structure when unmapping page ranges based on the dirty address information passed to tlb_remove_tlb_entry. arm64 already does this by directly manipulating the start/end fields of the gather structure, but this confuses the generic code which does not expect these fields to change and can end up calculating invalid, negative ranges when forcing a flush in zap_pte_range. This patch moves the minimal range calculation out of the arm64 code and into the generic implementation, simplifying zap_pte_range in the process (which no longer needs to care about start/end, since they will point to the appropriate ranges already). With the range being tracked by core code, the need_flush flag is dropped in favour of checking that the end of the range has actually been set. Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Russell King - ARM Linux <linux@arm.linux.org.uk> Cc: Michal Simek <monstr@monstr.eu> Acked-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Will Deacon <will.deacon@arm.com>
1 parent 63648dd commit fb7332a

7 files changed

Lines changed: 63 additions & 100 deletions

File tree

‎arch/arm64/include/asm/tlb.h‎

Lines changed: 3 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@
1919
#ifndef __ASM_TLB_H
2020
#define __ASM_TLB_H
2121

22-
#define __tlb_remove_pmd_tlb_entry __tlb_remove_pmd_tlb_entry
23-
24-
#include <asm-generic/tlb.h>
25-
2622
#include <linux/pagemap.h>
2723
#include <linux/swap.h>
2824

@@ -37,79 +33,29 @@ static inline void __tlb_remove_table(void *_table)
3733
#define tlb_remove_entry(tlb, entry) tlb_remove_page(tlb, entry)
3834
#endif /* CONFIG_HAVE_RCU_TABLE_FREE */
3935

40-
/*
41-
* There's three ways the TLB shootdown code is used:
42-
* 1. Unmapping a range of vmas. See zap_page_range(), unmap_region().
43-
* tlb->fullmm = 0, and tlb_start_vma/tlb_end_vma will be called.
44-
* 2. Unmapping all vmas. See exit_mmap().
45-
* tlb->fullmm = 1, and tlb_start_vma/tlb_end_vma will be called.
46-
* Page tables will be freed.
47-
* 3. Unmapping argument pages. See shift_arg_pages().
48-
* tlb->fullmm = 0, but tlb_start_vma/tlb_end_vma will not be called.
49-
*/
36+
#include <asm-generic/tlb.h>
37+
5038
static inline void tlb_flush(struct mmu_gather *tlb)
5139
{
5240
if (tlb->fullmm) {
5341
flush_tlb_mm(tlb->mm);
54-
} else if (tlb->end > 0) {
42+
} else {
5543
struct vm_area_struct vma = { .vm_mm = tlb->mm, };
5644
flush_tlb_range(&vma, tlb->start, tlb->end);
57-
tlb->start = TASK_SIZE;
58-
tlb->end = 0;
59-
}
60-
}
61-
62-
static inline void tlb_add_flush(struct mmu_gather *tlb, unsigned long addr)
63-
{
64-
if (!tlb->fullmm) {
65-
tlb->start = min(tlb->start, addr);
66-
tlb->end = max(tlb->end, addr + PAGE_SIZE);
67-
}
68-
}
69-
70-
/*
71-
* Memorize the range for the TLB flush.
72-
*/
73-
static inline void __tlb_remove_tlb_entry(struct mmu_gather *tlb, pte_t *ptep,
74-
unsigned long addr)
75-
{
76-
tlb_add_flush(tlb, addr);
77-
}
78-
79-
/*
80-
* In the case of tlb vma handling, we can optimise these away in the
81-
* case where we're doing a full MM flush. When we're doing a munmap,
82-
* the vmas are adjusted to only cover the region to be torn down.
83-
*/
84-
static inline void tlb_start_vma(struct mmu_gather *tlb,
85-
struct vm_area_struct *vma)
86-
{
87-
if (!tlb->fullmm) {
88-
tlb->start = TASK_SIZE;
89-
tlb->end = 0;
9045
}
9146
}
9247

93-
static inline void tlb_end_vma(struct mmu_gather *tlb,
94-
struct vm_area_struct *vma)
95-
{
96-
if (!tlb->fullmm)
97-
tlb_flush(tlb);
98-
}
99-
10048
static inline void __pte_free_tlb(struct mmu_gather *tlb, pgtable_t pte,
10149
unsigned long addr)
10250
{
10351
pgtable_page_dtor(pte);
104-
tlb_add_flush(tlb, addr);
10552
tlb_remove_entry(tlb, pte);
10653
}
10754

10855
#if CONFIG_ARM64_PGTABLE_LEVELS > 2
10956
static inline void __pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmdp,
11057
unsigned long addr)
11158
{
112-
tlb_add_flush(tlb, addr);
11359
tlb_remove_entry(tlb, virt_to_page(pmdp));
11460
}
11561
#endif
@@ -118,15 +64,8 @@ static inline void __pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmdp,
11864
static inline void __pud_free_tlb(struct mmu_gather *tlb, pud_t *pudp,
11965
unsigned long addr)
12066
{
121-
tlb_add_flush(tlb, addr);
12267
tlb_remove_entry(tlb, virt_to_page(pudp));
12368
}
12469
#endif
12570

126-
static inline void __tlb_remove_pmd_tlb_entry(struct mmu_gather *tlb, pmd_t *pmdp,
127-
unsigned long address)
128-
{
129-
tlb_add_flush(tlb, address);
130-
}
131-
13271
#endif

‎arch/microblaze/include/asm/tlb.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
#define tlb_flush(tlb) flush_tlb_mm((tlb)->mm)
1515

1616
#include <linux/pagemap.h>
17-
#include <asm-generic/tlb.h>
1817

1918
#ifdef CONFIG_MMU
2019
#define tlb_start_vma(tlb, vma) do { } while (0)
2120
#define tlb_end_vma(tlb, vma) do { } while (0)
2221
#define __tlb_remove_tlb_entry(tlb, pte, address) do { } while (0)
2322
#endif
2423

24+
#include <asm-generic/tlb.h>
25+
2526
#endif /* _ASM_MICROBLAZE_TLB_H */

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#ifdef __KERNEL__
44

55
#include <linux/mm.h>
6-
#include <asm-generic/tlb.h>
76

87
#ifdef CONFIG_PPC_BOOK3E
98
extern void tlb_flush_pgtable(struct mmu_gather *tlb, unsigned long address);
@@ -14,6 +13,8 @@ static inline void tlb_flush_pgtable(struct mmu_gather *tlb,
1413
}
1514
#endif /* !CONFIG_PPC_BOOK3E */
1615

16+
extern void tlb_remove_table(struct mmu_gather *tlb, void *table);
17+
1718
#ifdef CONFIG_PPC64
1819
#include <asm/pgalloc-64.h>
1920
#else

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#define tlb_start_vma(tlb, vma) do { } while (0)
2929
#define tlb_end_vma(tlb, vma) do { } while (0)
30+
#define __tlb_remove_tlb_entry __tlb_remove_tlb_entry
3031

3132
extern void tlb_flush(struct mmu_gather *tlb);
3233

‎arch/powerpc/mm/hugetlbpage.c‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,8 +517,6 @@ static void free_hugepd_range(struct mmu_gather *tlb, hugepd_t *hpdp, int pdshif
517517
for (i = 0; i < num_hugepd; i++, hpdp++)
518518
hpdp->pd = 0;
519519

520-
tlb->need_flush = 1;
521-
522520
#ifdef CONFIG_PPC_FSL_BOOK3E
523521
hugepd_free(tlb, hugepte);
524522
#else

‎include/asm-generic/tlb.h‎

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,9 @@ struct mmu_gather {
9696
#endif
9797
unsigned long start;
9898
unsigned long end;
99-
unsigned int need_flush : 1, /* Did free PTEs */
10099
/* we are in the middle of an operation to clear
101100
* a full mm and can make some optimizations */
102-
fullmm : 1,
101+
unsigned int fullmm : 1,
103102
/* we have performed an operation which
104103
* requires a complete flush of the tlb */
105104
need_flush_all : 1;
@@ -128,16 +127,54 @@ static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page)
128127
tlb_flush_mmu(tlb);
129128
}
130129

130+
static inline void __tlb_adjust_range(struct mmu_gather *tlb,
131+
unsigned long address)
132+
{
133+
tlb->start = min(tlb->start, address);
134+
tlb->end = max(tlb->end, address + PAGE_SIZE);
135+
}
136+
137+
static inline void __tlb_reset_range(struct mmu_gather *tlb)
138+
{
139+
tlb->start = TASK_SIZE;
140+
tlb->end = 0;
141+
}
142+
143+
/*
144+
* In the case of tlb vma handling, we can optimise these away in the
145+
* case where we're doing a full MM flush. When we're doing a munmap,
146+
* the vmas are adjusted to only cover the region to be torn down.
147+
*/
148+
#ifndef tlb_start_vma
149+
#define tlb_start_vma(tlb, vma) do { } while (0)
150+
#endif
151+
152+
#define __tlb_end_vma(tlb, vma) \
153+
do { \
154+
if (!tlb->fullmm && tlb->end) { \
155+
tlb_flush(tlb); \
156+
__tlb_reset_range(tlb); \
157+
} \
158+
} while (0)
159+
160+
#ifndef tlb_end_vma
161+
#define tlb_end_vma __tlb_end_vma
162+
#endif
163+
164+
#ifndef __tlb_remove_tlb_entry
165+
#define __tlb_remove_tlb_entry(tlb, ptep, address) do { } while (0)
166+
#endif
167+
131168
/**
132169
* tlb_remove_tlb_entry - remember a pte unmapping for later tlb invalidation.
133170
*
134-
* Record the fact that pte's were really umapped in ->need_flush, so we can
135-
* later optimise away the tlb invalidate. This helps when userspace is
136-
* unmapping already-unmapped pages, which happens quite a lot.
171+
* Record the fact that pte's were really unmapped by updating the range,
172+
* so we can later optimise away the tlb invalidate. This helps when
173+
* userspace is unmapping already-unmapped pages, which happens quite a lot.
137174
*/
138175
#define tlb_remove_tlb_entry(tlb, ptep, address) \
139176
do { \
140-
tlb->need_flush = 1; \
177+
__tlb_adjust_range(tlb, address); \
141178
__tlb_remove_tlb_entry(tlb, ptep, address); \
142179
} while (0)
143180

@@ -151,27 +188,27 @@ static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page)
151188

152189
#define tlb_remove_pmd_tlb_entry(tlb, pmdp, address) \
153190
do { \
154-
tlb->need_flush = 1; \
191+
__tlb_adjust_range(tlb, address); \
155192
__tlb_remove_pmd_tlb_entry(tlb, pmdp, address); \
156193
} while (0)
157194

158195
#define pte_free_tlb(tlb, ptep, address) \
159196
do { \
160-
tlb->need_flush = 1; \
197+
__tlb_adjust_range(tlb, address); \
161198
__pte_free_tlb(tlb, ptep, address); \
162199
} while (0)
163200

164201
#ifndef __ARCH_HAS_4LEVEL_HACK
165202
#define pud_free_tlb(tlb, pudp, address) \
166203
do { \
167-
tlb->need_flush = 1; \
204+
__tlb_adjust_range(tlb, address); \
168205
__pud_free_tlb(tlb, pudp, address); \
169206
} while (0)
170207
#endif
171208

172209
#define pmd_free_tlb(tlb, pmdp, address) \
173210
do { \
174-
tlb->need_flush = 1; \
211+
__tlb_adjust_range(tlb, address); \
175212
__pmd_free_tlb(tlb, pmdp, address); \
176213
} while (0)
177214

‎mm/memory.c‎

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,6 @@ void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned long
220220
/* Is it from 0 to ~0? */
221221
tlb->fullmm = !(start | (end+1));
222222
tlb->need_flush_all = 0;
223-
tlb->start = start;
224-
tlb->end = end;
225-
tlb->need_flush = 0;
226223
tlb->local.next = NULL;
227224
tlb->local.nr = 0;
228225
tlb->local.max = ARRAY_SIZE(tlb->__pages);
@@ -232,15 +229,20 @@ void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned long
232229
#ifdef CONFIG_HAVE_RCU_TABLE_FREE
233230
tlb->batch = NULL;
234231
#endif
232+
233+
__tlb_reset_range(tlb);
235234
}
236235

237236
static void tlb_flush_mmu_tlbonly(struct mmu_gather *tlb)
238237
{
239-
tlb->need_flush = 0;
238+
if (!tlb->end)
239+
return;
240+
240241
tlb_flush(tlb);
241242
#ifdef CONFIG_HAVE_RCU_TABLE_FREE
242243
tlb_table_flush(tlb);
243244
#endif
245+
__tlb_reset_range(tlb);
244246
}
245247

246248
static void tlb_flush_mmu_free(struct mmu_gather *tlb)
@@ -256,8 +258,6 @@ static void tlb_flush_mmu_free(struct mmu_gather *tlb)
256258

257259
void tlb_flush_mmu(struct mmu_gather *tlb)
258260
{
259-
if (!tlb->need_flush)
260-
return;
261261
tlb_flush_mmu_tlbonly(tlb);
262262
tlb_flush_mmu_free(tlb);
263263
}
@@ -292,7 +292,7 @@ int __tlb_remove_page(struct mmu_gather *tlb, struct page *page)
292292
{
293293
struct mmu_gather_batch *batch;
294294

295-
VM_BUG_ON(!tlb->need_flush);
295+
VM_BUG_ON(!tlb->end);
296296

297297
batch = tlb->active;
298298
batch->pages[batch->nr++] = page;
@@ -359,8 +359,6 @@ void tlb_remove_table(struct mmu_gather *tlb, void *table)
359359
{
360360
struct mmu_table_batch **batch = &tlb->batch;
361361

362-
tlb->need_flush = 1;
363-
364362
/*
365363
* When there's less then two users of this mm there cannot be a
366364
* concurrent page-table walk.
@@ -1185,20 +1183,8 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb,
11851183
arch_leave_lazy_mmu_mode();
11861184

11871185
/* Do the actual TLB flush before dropping ptl */
1188-
if (force_flush) {
1189-
unsigned long old_end;
1190-
1191-
/*
1192-
* Flush the TLB just for the previous segment,
1193-
* then update the range to be the remaining
1194-
* TLB range.
1195-
*/
1196-
old_end = tlb->end;
1197-
tlb->end = addr;
1186+
if (force_flush)
11981187
tlb_flush_mmu_tlbonly(tlb);
1199-
tlb->start = addr;
1200-
tlb->end = old_end;
1201-
}
12021188
pte_unmap_unlock(start_pte, ptl);
12031189

12041190
/*

0 commit comments

Comments
 (0)