aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2013-08-15 14:42:25 -0400
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2013-08-20 11:43:05 -0400
commit8e220cfd1a9f6c763a108a3b964a888fe341dabd (patch)
treebb1fc74084c67a972af7a17447b6749f9b17115d
parentf34a4837cd0b28abe7449e591a23cb32ab128520 (diff)
Fix TLB gather virtual address range invalidation corner cases
commit 2b047252d087be7f2ba088b4933cd904f92e6fce upstream. 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 53a59fc67f97 ("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 597e1c3580b7 ("mm/mmu_gather: enable tlb flush range in generic mmu_gather"), and the range handling was already fixed at least once in commit e6c495a96ce0 ("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 <tebulin@googlemail.com> Build-testing-by: Stephen Rothwell <sfr@canb.auug.org.au> Build-testing-by: Richard Weinberger <richard.weinberger@gmail.com> Reviewed-by: Michal Hocko <mhocko@suse.cz> Acked-by: Peter Zijlstra <peterz@infradead.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
-rw-r--r--arch/arm/include/asm/tlb.h7
-rw-r--r--arch/arm64/include/asm/tlb.h7
-rw-r--r--arch/ia64/include/asm/tlb.h9
-rw-r--r--arch/s390/include/asm/tlb.h8
-rw-r--r--arch/sh/include/asm/tlb.h6
-rw-r--r--arch/um/include/asm/tlb.h6
-rw-r--r--fs/exec.c4
-rw-r--r--include/asm-generic/tlb.h2
-rw-r--r--mm/hugetlb.c2
-rw-r--r--mm/memory.c36
-rw-r--r--mm/mmap.c4
11 files changed, 57 insertions, 34 deletions
diff --git a/arch/arm/include/asm/tlb.h b/arch/arm/include/asm/tlb.h
index bdf2b8458ec1..aa9b4ac3fdf6 100644
--- a/arch/arm/include/asm/tlb.h
+++ b/arch/arm/include/asm/tlb.h
@@ -43,6 +43,7 @@ struct mmu_gather {
43 struct mm_struct *mm; 43 struct mm_struct *mm;
44 unsigned int fullmm; 44 unsigned int fullmm;
45 struct vm_area_struct *vma; 45 struct vm_area_struct *vma;
46 unsigned long start, end;
46 unsigned long range_start; 47 unsigned long range_start;
47 unsigned long range_end; 48 unsigned long range_end;
48 unsigned int nr; 49 unsigned int nr;
@@ -107,10 +108,12 @@ static inline void tlb_flush_mmu(struct mmu_gather *tlb)
107} 108}
108 109
109static inline void 110static inline void
110tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned int fullmm) 111tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned long start, unsigned long end)
111{ 112{
112 tlb->mm = mm; 113 tlb->mm = mm;
113 tlb->fullmm = fullmm; 114 tlb->fullmm = !(start | (end+1));
115 tlb->start = start;
116 tlb->end = end;
114 tlb->vma = NULL; 117 tlb->vma = NULL;
115 tlb->max = ARRAY_SIZE(tlb->local); 118 tlb->max = ARRAY_SIZE(tlb->local);
116 tlb->pages = tlb->local; 119 tlb->pages = tlb->local;
diff --git a/arch/arm64/include/asm/tlb.h b/arch/arm64/include/asm/tlb.h
index 654f0968030b..5546653e5cc8 100644
--- a/arch/arm64/include/asm/tlb.h
+++ b/arch/arm64/include/asm/tlb.h
@@ -35,6 +35,7 @@ struct mmu_gather {
35 struct mm_struct *mm; 35 struct mm_struct *mm;
36 unsigned int fullmm; 36 unsigned int fullmm;
37 struct vm_area_struct *vma; 37 struct vm_area_struct *vma;
38 unsigned long start, end;
38 unsigned long range_start; 39 unsigned long range_start;
39 unsigned long range_end; 40 unsigned long range_end;
40 unsigned int nr; 41 unsigned int nr;
@@ -97,10 +98,12 @@ static inline void tlb_flush_mmu(struct mmu_gather *tlb)
97} 98}
98 99
99static inline void 100static inline void
100tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned int fullmm) 101tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned long start, unsigned long end)
101{ 102{
102 tlb->mm = mm; 103 tlb->mm = mm;
103 tlb->fullmm = fullmm; 104 tlb->fullmm = !(start | (end+1));
105 tlb->start = start;
106 tlb->end = end;
104 tlb->vma = NULL; 107 tlb->vma = NULL;
105 tlb->max = ARRAY_SIZE(tlb->local); 108 tlb->max = ARRAY_SIZE(tlb->local);
106 tlb->pages = tlb->local; 109 tlb->pages = tlb->local;
diff --git a/arch/ia64/include/asm/tlb.h b/arch/ia64/include/asm/tlb.h
index ef3a9de01954..bc5efc7c3f3f 100644
--- a/arch/ia64/include/asm/tlb.h
+++ b/arch/ia64/include/asm/tlb.h
@@ -22,7 +22,7 @@
22 * unmapping a portion of the virtual address space, these hooks are called according to 22 * unmapping a portion of the virtual address space, these hooks are called according to
23 * the following template: 23 * the following template:
24 * 24 *
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
26 * { 26 * {
27 * for each vma that needs a shootdown do { 27 * for each vma that needs a shootdown do {
28 * tlb_start_vma(tlb, vma); 28 * tlb_start_vma(tlb, vma);
@@ -58,6 +58,7 @@ struct mmu_gather {
58 unsigned int max; 58 unsigned int max;
59 unsigned char fullmm; /* non-zero means full mm flush */ 59 unsigned char fullmm; /* non-zero means full mm flush */
60 unsigned char need_flush; /* really unmapped some PTEs? */ 60 unsigned char need_flush; /* really unmapped some PTEs? */
61 unsigned long start, end;
61 unsigned long start_addr; 62 unsigned long start_addr;
62 unsigned long end_addr; 63 unsigned long end_addr;
63 struct page **pages; 64 struct page **pages;
@@ -155,13 +156,15 @@ static inline void __tlb_alloc_page(struct mmu_gather *tlb)
155 156
156 157
157static inline void 158static inline void
158tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned int full_mm_flush) 159tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned long start, unsigned long end)
159{ 160{
160 tlb->mm = mm; 161 tlb->mm = mm;
161 tlb->max = ARRAY_SIZE(tlb->local); 162 tlb->max = ARRAY_SIZE(tlb->local);
162 tlb->pages = tlb->local; 163 tlb->pages = tlb->local;
163 tlb->nr = 0; 164 tlb->nr = 0;
164 tlb->fullmm = full_mm_flush; 165 tlb->fullmm = !(start | (end+1));
166 tlb->start = start;
167 tlb->end = end;
165 tlb->start_addr = ~0UL; 168 tlb->start_addr = ~0UL;
166} 169}
167 170
diff --git a/arch/s390/include/asm/tlb.h b/arch/s390/include/asm/tlb.h
index b75d7d686684..23a64d25f2b1 100644
--- a/arch/s390/include/asm/tlb.h
+++ b/arch/s390/include/asm/tlb.h
@@ -32,6 +32,7 @@ struct mmu_gather {
32 struct mm_struct *mm; 32 struct mm_struct *mm;
33 struct mmu_table_batch *batch; 33 struct mmu_table_batch *batch;
34 unsigned int fullmm; 34 unsigned int fullmm;
35 unsigned long start, unsigned long end;
35}; 36};
36 37
37struct mmu_table_batch { 38struct mmu_table_batch {
@@ -48,10 +49,13 @@ extern void tlb_remove_table(struct mmu_gather *tlb, void *table);
48 49
49static inline void tlb_gather_mmu(struct mmu_gather *tlb, 50static inline void tlb_gather_mmu(struct mmu_gather *tlb,
50 struct mm_struct *mm, 51 struct mm_struct *mm,
51 unsigned int full_mm_flush) 52 unsigned long start,
53 unsigned long end)
52{ 54{
53 tlb->mm = mm; 55 tlb->mm = mm;
54 tlb->fullmm = full_mm_flush; 56 tlb->start = start;
57 tlb->end = end;
58 tlb->fullmm = !(start | (end+1));
55 tlb->batch = NULL; 59 tlb->batch = NULL;
56 if (tlb->fullmm) 60 if (tlb->fullmm)
57 __tlb_flush_mm(mm); 61 __tlb_flush_mm(mm);
diff --git a/arch/sh/include/asm/tlb.h b/arch/sh/include/asm/tlb.h
index e61d43d9f689..362192ed12fe 100644
--- a/arch/sh/include/asm/tlb.h
+++ b/arch/sh/include/asm/tlb.h
@@ -36,10 +36,12 @@ static inline void init_tlb_gather(struct mmu_gather *tlb)
36} 36}
37 37
38static inline void 38static inline void
39tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned int full_mm_flush) 39tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned long start, unsigned long end)
40{ 40{
41 tlb->mm = mm; 41 tlb->mm = mm;
42 tlb->fullmm = full_mm_flush; 42 tlb->start = start;
43 tlb->end = end;
44 tlb->fullmm = !(start | (end+1));
43 45
44 init_tlb_gather(tlb); 46 init_tlb_gather(tlb);
45} 47}
diff --git a/arch/um/include/asm/tlb.h b/arch/um/include/asm/tlb.h
index 4febacd1a8a1..29b0301c18aa 100644
--- a/arch/um/include/asm/tlb.h
+++ b/arch/um/include/asm/tlb.h
@@ -45,10 +45,12 @@ static inline void init_tlb_gather(struct mmu_gather *tlb)
45} 45}
46 46
47static inline void 47static inline void
48tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned int full_mm_flush) 48tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned long start, unsigned long end)
49{ 49{
50 tlb->mm = mm; 50 tlb->mm = mm;
51 tlb->fullmm = full_mm_flush; 51 tlb->start = start;
52 tlb->end = end;
53 tlb->fullmm = !(start | (end+1));
52 54
53 init_tlb_gather(tlb); 55 init_tlb_gather(tlb);
54} 56}
diff --git a/fs/exec.c b/fs/exec.c
index ffd7a813ad3d..1f446705636b 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -607,7 +607,7 @@ static int shift_arg_pages(struct vm_area_struct *vma, unsigned long shift)
607 return -ENOMEM; 607 return -ENOMEM;
608 608
609 lru_add_drain(); 609 lru_add_drain();
610 tlb_gather_mmu(&tlb, mm, 0); 610 tlb_gather_mmu(&tlb, mm, old_start, old_end);
611 if (new_end > old_start) { 611 if (new_end > old_start) {
612 /* 612 /*
613 * when the old and new regions overlap clear from new_end. 613 * when the old and new regions overlap clear from new_end.
@@ -624,7 +624,7 @@ static int shift_arg_pages(struct vm_area_struct *vma, unsigned long shift)
624 free_pgd_range(&tlb, old_start, old_end, new_end, 624 free_pgd_range(&tlb, old_start, old_end, new_end,
625 vma->vm_next ? vma->vm_next->vm_start : USER_PGTABLES_CEILING); 625 vma->vm_next ? vma->vm_next->vm_start : USER_PGTABLES_CEILING);
626 } 626 }
627 tlb_finish_mmu(&tlb, new_end, old_end); 627 tlb_finish_mmu(&tlb, old_start, old_end);
628 628
629 /* 629 /*
630 * Shrink the vma to just the new range. Always succeeds. 630 * Shrink the vma to just the new range. Always succeeds.
diff --git a/include/asm-generic/tlb.h b/include/asm-generic/tlb.h
index 13821c339a41..5672d7ea1fa0 100644
--- a/include/asm-generic/tlb.h
+++ b/include/asm-generic/tlb.h
@@ -112,7 +112,7 @@ struct mmu_gather {
112 112
113#define HAVE_GENERIC_MMU_GATHER 113#define HAVE_GENERIC_MMU_GATHER
114 114
115void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, bool fullmm); 115void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned long start, unsigned long end);
116void tlb_flush_mmu(struct mmu_gather *tlb); 116void tlb_flush_mmu(struct mmu_gather *tlb);
117void tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start, 117void tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start,
118 unsigned long end); 118 unsigned long end);
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 5cf99bf8cce2..7c5eb85ec645 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -2490,7 +2490,7 @@ void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
2490 2490
2491 mm = vma->vm_mm; 2491 mm = vma->vm_mm;
2492 2492
2493 tlb_gather_mmu(&tlb, mm, 0); 2493 tlb_gather_mmu(&tlb, mm, start, end);
2494 __unmap_hugepage_range(&tlb, vma, start, end, ref_page); 2494 __unmap_hugepage_range(&tlb, vma, start, end, ref_page);
2495 tlb_finish_mmu(&tlb, start, end); 2495 tlb_finish_mmu(&tlb, start, end);
2496} 2496}
diff --git a/mm/memory.c b/mm/memory.c
index 5e5080005bc4..5a35443c01ad 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -211,14 +211,15 @@ static int tlb_next_batch(struct mmu_gather *tlb)
211 * tear-down from @mm. The @fullmm argument is used when @mm is without 211 * tear-down from @mm. The @fullmm argument is used when @mm is without
212 * users and we're going to destroy the full address space (exit/execve). 212 * users and we're going to destroy the full address space (exit/execve).
213 */ 213 */
214void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, bool fullmm) 214void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, unsigned long start, unsigned long end)
215{ 215{
216 tlb->mm = mm; 216 tlb->mm = mm;
217 217
218 tlb->fullmm = fullmm; 218 /* Is it from 0 to ~0? */
219 tlb->fullmm = !(start | (end+1));
219 tlb->need_flush_all = 0; 220 tlb->need_flush_all = 0;
220 tlb->start = -1UL; 221 tlb->start = start;
221 tlb->end = 0; 222 tlb->end = end;
222 tlb->need_flush = 0; 223 tlb->need_flush = 0;
223 tlb->local.next = NULL; 224 tlb->local.next = NULL;
224 tlb->local.nr = 0; 225 tlb->local.nr = 0;
@@ -258,8 +259,6 @@ void tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long e
258{ 259{
259 struct mmu_gather_batch *batch, *next; 260 struct mmu_gather_batch *batch, *next;
260 261
261 tlb->start = start;
262 tlb->end = end;
263 tlb_flush_mmu(tlb); 262 tlb_flush_mmu(tlb);
264 263
265 /* keep the page table cache within bounds */ 264 /* keep the page table cache within bounds */
@@ -1101,7 +1100,6 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb,
1101 spinlock_t *ptl; 1100 spinlock_t *ptl;
1102 pte_t *start_pte; 1101 pte_t *start_pte;
1103 pte_t *pte; 1102 pte_t *pte;
1104 unsigned long range_start = addr;
1105 1103
1106again: 1104again:
1107 init_rss_vec(rss); 1105 init_rss_vec(rss);
@@ -1204,17 +1202,25 @@ again:
1204 * and page-free while holding it. 1202 * and page-free while holding it.
1205 */ 1203 */
1206 if (force_flush) { 1204 if (force_flush) {
1205 unsigned long old_end;
1206
1207 force_flush = 0; 1207 force_flush = 0;
1208 1208
1209#ifdef HAVE_GENERIC_MMU_GATHER 1209 /*
1210 tlb->start = range_start; 1210 * Flush the TLB just for the previous segment,
1211 * then update the range to be the remaining
1212 * TLB range.
1213 */
1214 old_end = tlb->end;
1211 tlb->end = addr; 1215 tlb->end = addr;
1212#endif 1216
1213 tlb_flush_mmu(tlb); 1217 tlb_flush_mmu(tlb);
1214 if (addr != end) { 1218
1215 range_start = addr; 1219 tlb->start = addr;
1220 tlb->end = old_end;
1221
1222 if (addr != end)
1216 goto again; 1223 goto again;
1217 }
1218 } 1224 }
1219 1225
1220 return addr; 1226 return addr;
@@ -1399,7 +1405,7 @@ void zap_page_range(struct vm_area_struct *vma, unsigned long start,
1399 unsigned long end = start + size; 1405 unsigned long end = start + size;
1400 1406
1401 lru_add_drain(); 1407 lru_add_drain();
1402 tlb_gather_mmu(&tlb, mm, 0); 1408 tlb_gather_mmu(&tlb, mm, start, end);
1403 update_hiwater_rss(mm); 1409 update_hiwater_rss(mm);
1404 mmu_notifier_invalidate_range_start(mm, start, end); 1410 mmu_notifier_invalidate_range_start(mm, start, end);
1405 for ( ; vma && vma->vm_start < end; vma = vma->vm_next) 1411 for ( ; vma && vma->vm_start < end; vma = vma->vm_next)
@@ -1425,7 +1431,7 @@ static void zap_page_range_single(struct vm_area_struct *vma, unsigned long addr
1425 unsigned long end = address + size; 1431 unsigned long end = address + size;
1426 1432
1427 lru_add_drain(); 1433 lru_add_drain();
1428 tlb_gather_mmu(&tlb, mm, 0); 1434 tlb_gather_mmu(&tlb, mm, address, end);
1429 update_hiwater_rss(mm); 1435 update_hiwater_rss(mm);
1430 mmu_notifier_invalidate_range_start(mm, address, end); 1436 mmu_notifier_invalidate_range_start(mm, address, end);
1431 unmap_single_vma(&tlb, vma, address, end, details); 1437 unmap_single_vma(&tlb, vma, address, end, details);
diff --git a/mm/mmap.c b/mm/mmap.c
index 7dbe39745be9..8d25fdc653be 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -2356,7 +2356,7 @@ static void unmap_region(struct mm_struct *mm,
2356 struct mmu_gather tlb; 2356 struct mmu_gather tlb;
2357 2357
2358 lru_add_drain(); 2358 lru_add_drain();
2359 tlb_gather_mmu(&tlb, mm, 0); 2359 tlb_gather_mmu(&tlb, mm, start, end);
2360 update_hiwater_rss(mm); 2360 update_hiwater_rss(mm);
2361 unmap_vmas(&tlb, vma, start, end); 2361 unmap_vmas(&tlb, vma, start, end);
2362 free_pgtables(&tlb, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS, 2362 free_pgtables(&tlb, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS,
@@ -2735,7 +2735,7 @@ void exit_mmap(struct mm_struct *mm)
2735 2735
2736 lru_add_drain(); 2736 lru_add_drain();
2737 flush_cache_mm(mm); 2737 flush_cache_mm(mm);
2738 tlb_gather_mmu(&tlb, mm, 1); 2738 tlb_gather_mmu(&tlb, mm, 0, -1);
2739 /* update_hiwater_rss(mm) here? but nobody should be looking */ 2739 /* update_hiwater_rss(mm) here? but nobody should be looking */
2740 /* Use -1 here to ensure all VMAs in the mm are unmapped */ 2740 /* Use -1 here to ensure all VMAs in the mm are unmapped */
2741 unmap_vmas(&tlb, vma, 0, -1); 2741 unmap_vmas(&tlb, vma, 0, -1);