Skip to content

Commit 2b04725

Browse files
committed
Fix TLB gather virtual address range invalidation corner cases
Ben Tebulin reported: "Since v3.7.2 on two independent machines a very specific Git repository fails in 9/10 cases on git-fsck due to an SHA1/memory failures. This only occurs on a very specific repository and can be reproduced stably on two independent laptops. Git mailing list ran out of ideas and for me this looks like some very exotic kernel issue" and bisected the failure to the backport of commit 53a59fc ("mm: limit mmu_gather batching to fix soft lockups on !CONFIG_PREEMPT"). That commit itself is not actually buggy, but what it does is to make it much more likely to hit the partial TLB invalidation case, since it introduces a new case in tlb_next_batch() that previously only ever happened when running out of memory. The real bug is that the TLB gather virtual memory range setup is subtly buggered. It was introduced in commit 597e1c3 ("mm/mmu_gather: enable tlb flush range in generic mmu_gather"), and the range handling was already fixed at least once in commit e6c495a ("mm: fix the TLB range flushed when __tlb_remove_page() runs out of slots"), but that fix was not complete. The problem with the TLB gather virtual address range is that it isn't set up by the initial tlb_gather_mmu() initialization (which didn't get the TLB range information), but it is set up ad-hoc later by the functions that actually flush the TLB. And so any such case that forgot to update the TLB range entries would potentially miss TLB invalidates. Rather than try to figure out exactly which particular ad-hoc range setup was missing (I personally suspect it's the hugetlb case in zap_huge_pmd(), which didn't have the same logic as zap_pte_range() did), this patch just gets rid of the problem at the source: make the TLB range information available to tlb_gather_mmu(), and initialize it when initializing all the other tlb gather fields. This makes the patch larger, but conceptually much simpler. And the end result is much more understandable; even if you want to play games with partial ranges when invalidating the TLB contents in chunks, now the range information is always there, and anybody who doesn't want to bother with it won't introduce subtle bugs. Ben verified that this fixes his problem. Reported-bisected-and-tested-by: Ben Tebulin <[email protected]> Build-testing-by: Stephen Rothwell <[email protected]> Build-testing-by: Richard Weinberger <[email protected]> Reviewed-by: Michal Hocko <[email protected]> Acked-by: Peter Zijlstra <[email protected]> Cc: [email protected] Signed-off-by: Linus Torvalds <[email protected]>
1 parent f1d6e17 commit 2b04725

File tree

11 files changed

+57
-34
lines changed

11 files changed

+57
-34
lines changed

arch/arm/include/asm/tlb.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ struct mmu_gather {
4343
struct mm_struct *mm;
4444
unsigned int fullmm;
4545
struct vm_area_struct *vma;
46+
unsigned long start, end;
4647
unsigned long range_start;
4748
unsigned long range_end;
4849
unsigned int nr;
@@ -107,10 +108,12 @@ static inline void tlb_flush_mmu(struct mmu_gather *tlb)
107108
}
108109

109110
static inline void
110-
tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned int fullmm)
111+
tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned long start, unsigned long end)
111112
{
112113
tlb->mm = mm;
113-
tlb->fullmm = fullmm;
114+
tlb->fullmm = !(start | (end+1));
115+
tlb->start = start;
116+
tlb->end = end;
114117
tlb->vma = NULL;
115118
tlb->max = ARRAY_SIZE(tlb->local);
116119
tlb->pages = tlb->local;

arch/arm64/include/asm/tlb.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ struct mmu_gather {
3535
struct mm_struct *mm;
3636
unsigned int fullmm;
3737
struct vm_area_struct *vma;
38+
unsigned long start, end;
3839
unsigned long range_start;
3940
unsigned long range_end;
4041
unsigned int nr;
@@ -97,10 +98,12 @@ static inline void tlb_flush_mmu(struct mmu_gather *tlb)
9798
}
9899

99100
static inline void
100-
tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned int fullmm)
101+
tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned long start, unsigned long end)
101102
{
102103
tlb->mm = mm;
103-
tlb->fullmm = fullmm;
104+
tlb->fullmm = !(start | (end+1));
105+
tlb->start = start;
106+
tlb->end = end;
104107
tlb->vma = NULL;
105108
tlb->max = ARRAY_SIZE(tlb->local);
106109
tlb->pages = tlb->local;

arch/ia64/include/asm/tlb.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* unmapping a portion of the virtual address space, these hooks are called according to
2323
* the following template:
2424
*
25-
* tlb <- tlb_gather_mmu(mm, full_mm_flush); // start unmap for address space MM
25+
* tlb <- tlb_gather_mmu(mm, start, end); // start unmap for address space MM
2626
* {
2727
* for each vma that needs a shootdown do {
2828
* tlb_start_vma(tlb, vma);
@@ -58,6 +58,7 @@ struct mmu_gather {
5858
unsigned int max;
5959
unsigned char fullmm; /* non-zero means full mm flush */
6060
unsigned char need_flush; /* really unmapped some PTEs? */
61+
unsigned long start, end;
6162
unsigned long start_addr;
6263
unsigned long end_addr;
6364
struct page **pages;
@@ -155,13 +156,15 @@ static inline void __tlb_alloc_page(struct mmu_gather *tlb)
155156

156157

157158
static inline void
158-
tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned int full_mm_flush)
159+
tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned long start, unsigned long end)
159160
{
160161
tlb->mm = mm;
161162
tlb->max = ARRAY_SIZE(tlb->local);
162163
tlb->pages = tlb->local;
163164
tlb->nr = 0;
164-
tlb->fullmm = full_mm_flush;
165+
tlb->fullmm = !(start | (end+1));
166+
tlb->start = start;
167+
tlb->end = end;
165168
tlb->start_addr = ~0UL;
166169
}
167170

arch/s390/include/asm/tlb.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ struct mmu_gather {
3232
struct mm_struct *mm;
3333
struct mmu_table_batch *batch;
3434
unsigned int fullmm;
35+
unsigned long start, unsigned long end;
3536
};
3637

3738
struct mmu_table_batch {
@@ -48,10 +49,13 @@ extern void tlb_remove_table(struct mmu_gather *tlb, void *table);
4849

4950
static inline void tlb_gather_mmu(struct mmu_gather *tlb,
5051
struct mm_struct *mm,
51-
unsigned int full_mm_flush)
52+
unsigned long start,
53+
unsigned long end)
5254
{
5355
tlb->mm = mm;
54-
tlb->fullmm = full_mm_flush;
56+
tlb->start = start;
57+
tlb->end = end;
58+
tlb->fullmm = !(start | (end+1));
5559
tlb->batch = NULL;
5660
if (tlb->fullmm)
5761
__tlb_flush_mm(mm);

arch/sh/include/asm/tlb.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ static inline void init_tlb_gather(struct mmu_gather *tlb)
3636
}
3737

3838
static inline void
39-
tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned int full_mm_flush)
39+
tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned long start, unsigned long end)
4040
{
4141
tlb->mm = mm;
42-
tlb->fullmm = full_mm_flush;
42+
tlb->start = start;
43+
tlb->end = end;
44+
tlb->fullmm = !(start | (end+1));
4345

4446
init_tlb_gather(tlb);
4547
}

arch/um/include/asm/tlb.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@ static inline void init_tlb_gather(struct mmu_gather *tlb)
4545
}
4646

4747
static inline void
48-
tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned int full_mm_flush)
48+
tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned long start, unsigned long end)
4949
{
5050
tlb->mm = mm;
51-
tlb->fullmm = full_mm_flush;
51+
tlb->start = start;
52+
tlb->end = end;
53+
tlb->fullmm = !(start | (end+1));
5254

5355
init_tlb_gather(tlb);
5456
}

fs/exec.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ static int shift_arg_pages(struct vm_area_struct *vma, unsigned long shift)
608608
return -ENOMEM;
609609

610610
lru_add_drain();
611-
tlb_gather_mmu(&tlb, mm, 0);
611+
tlb_gather_mmu(&tlb, mm, old_start, old_end);
612612
if (new_end > old_start) {
613613
/*
614614
* when the old and new regions overlap clear from new_end.
@@ -625,7 +625,7 @@ static int shift_arg_pages(struct vm_area_struct *vma, unsigned long shift)
625625
free_pgd_range(&tlb, old_start, old_end, new_end,
626626
vma->vm_next ? vma->vm_next->vm_start : USER_PGTABLES_CEILING);
627627
}
628-
tlb_finish_mmu(&tlb, new_end, old_end);
628+
tlb_finish_mmu(&tlb, old_start, old_end);
629629

630630
/*
631631
* Shrink the vma to just the new range. Always succeeds.

include/asm-generic/tlb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ struct mmu_gather {
112112

113113
#define HAVE_GENERIC_MMU_GATHER
114114

115-
void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, bool fullmm);
115+
void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned long start, unsigned long end);
116116
void tlb_flush_mmu(struct mmu_gather *tlb);
117117
void tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start,
118118
unsigned long end);

mm/hugetlb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2490,7 +2490,7 @@ void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
24902490

24912491
mm = vma->vm_mm;
24922492

2493-
tlb_gather_mmu(&tlb, mm, 0);
2493+
tlb_gather_mmu(&tlb, mm, start, end);
24942494
__unmap_hugepage_range(&tlb, vma, start, end, ref_page);
24952495
tlb_finish_mmu(&tlb, start, end);
24962496
}

mm/memory.c

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,15 @@ static int tlb_next_batch(struct mmu_gather *tlb)
209209
* tear-down from @mm. The @fullmm argument is used when @mm is without
210210
* users and we're going to destroy the full address space (exit/execve).
211211
*/
212-
void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, bool fullmm)
212+
void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned long start, unsigned long end)
213213
{
214214
tlb->mm = mm;
215215

216-
tlb->fullmm = fullmm;
216+
/* Is it from 0 to ~0? */
217+
tlb->fullmm = !(start | (end+1));
217218
tlb->need_flush_all = 0;
218-
tlb->start = -1UL;
219-
tlb->end = 0;
219+
tlb->start = start;
220+
tlb->end = end;
220221
tlb->need_flush = 0;
221222
tlb->local.next = NULL;
222223
tlb->local.nr = 0;
@@ -256,8 +257,6 @@ void tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long e
256257
{
257258
struct mmu_gather_batch *batch, *next;
258259

259-
tlb->start = start;
260-
tlb->end = end;
261260
tlb_flush_mmu(tlb);
262261

263262
/* keep the page table cache within bounds */
@@ -1099,7 +1098,6 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb,
10991098
spinlock_t *ptl;
11001099
pte_t *start_pte;
11011100
pte_t *pte;
1102-
unsigned long range_start = addr;
11031101

11041102
again:
11051103
init_rss_vec(rss);
@@ -1205,17 +1203,25 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb,
12051203
* and page-free while holding it.
12061204
*/
12071205
if (force_flush) {
1206+
unsigned long old_end;
1207+
12081208
force_flush = 0;
12091209

1210-
#ifdef HAVE_GENERIC_MMU_GATHER
1211-
tlb->start = range_start;
1210+
/*
1211+
* Flush the TLB just for the previous segment,
1212+
* then update the range to be the remaining
1213+
* TLB range.
1214+
*/
1215+
old_end = tlb->end;
12121216
tlb->end = addr;
1213-
#endif
1217+
12141218
tlb_flush_mmu(tlb);
1215-
if (addr != end) {
1216-
range_start = addr;
1219+
1220+
tlb->start = addr;
1221+
tlb->end = old_end;
1222+
1223+
if (addr != end)
12171224
goto again;
1218-
}
12191225
}
12201226

12211227
return addr;
@@ -1400,7 +1406,7 @@ void zap_page_range(struct vm_area_struct *vma, unsigned long start,
14001406
unsigned long end = start + size;
14011407

14021408
lru_add_drain();
1403-
tlb_gather_mmu(&tlb, mm, 0);
1409+
tlb_gather_mmu(&tlb, mm, start, end);
14041410
update_hiwater_rss(mm);
14051411
mmu_notifier_invalidate_range_start(mm, start, end);
14061412
for ( ; vma && vma->vm_start < end; vma = vma->vm_next)
@@ -1426,7 +1432,7 @@ static void zap_page_range_single(struct vm_area_struct *vma, unsigned long addr
14261432
unsigned long end = address + size;
14271433

14281434
lru_add_drain();
1429-
tlb_gather_mmu(&tlb, mm, 0);
1435+
tlb_gather_mmu(&tlb, mm, address, end);
14301436
update_hiwater_rss(mm);
14311437
mmu_notifier_invalidate_range_start(mm, address, end);
14321438
unmap_single_vma(&tlb, vma, address, end, details);

mm/mmap.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2336,7 +2336,7 @@ static void unmap_region(struct mm_struct *mm,
23362336
struct mmu_gather tlb;
23372337

23382338
lru_add_drain();
2339-
tlb_gather_mmu(&tlb, mm, 0);
2339+
tlb_gather_mmu(&tlb, mm, start, end);
23402340
update_hiwater_rss(mm);
23412341
unmap_vmas(&tlb, vma, start, end);
23422342
free_pgtables(&tlb, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS,
@@ -2709,7 +2709,7 @@ void exit_mmap(struct mm_struct *mm)
27092709

27102710
lru_add_drain();
27112711
flush_cache_mm(mm);
2712-
tlb_gather_mmu(&tlb, mm, 1);
2712+
tlb_gather_mmu(&tlb, mm, 0, -1);
27132713
/* update_hiwater_rss(mm) here? but nobody should be looking */
27142714
/* Use -1 here to ensure all VMAs in the mm are unmapped */
27152715
unmap_vmas(&tlb, vma, 0, -1);

0 commit comments

Comments
 (0)