Skip to content

Commit 97a8941

Browse files
Peter Zijlstratorvalds
authored andcommitted
mm: Remove i_mmap_lock lockbreak
Hugh says: "The only significant loser, I think, would be page reclaim (when concurrent with truncation): could spin for a long time waiting for the i_mmap_mutex it expects would soon be dropped? " Counter points: - cpu contention makes the spin stop (need_resched()) - zap pages should be freeing pages at a higher rate than reclaim ever can I think the simplification of the truncate code is definitely worth it. Effectively reverts: 2aa1589 ("mm: prevent concurrent unmap_mapping_range() on the same inode") and takes out the code that caused its problem. Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl> Reviewed-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com> Cc: Hugh Dickins <hughd@google.com> Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org> Cc: David Miller <davem@davemloft.net> Cc: Martin Schwidefsky <schwidefsky@de.ibm.com> Cc: Russell King <rmk@arm.linux.org.uk> Cc: Paul Mundt <lethal@linux-sh.org> Cc: Jeff Dike <jdike@addtoit.com> Cc: Richard Weinberger <richard@nod.at> Cc: Tony Luck <tony.luck@intel.com> Cc: Mel Gorman <mel@csn.ul.ie> Cc: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com> Cc: Nick Piggin <npiggin@kernel.dk> Cc: Namhyung Kim <namhyung@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent e4c70a6 commit 97a8941

8 files changed

Lines changed: 28 additions & 188 deletions

File tree

‎fs/inode.c‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,6 @@ void address_space_init_once(struct address_space *mapping)
331331
spin_lock_init(&mapping->private_lock);
332332
INIT_RAW_PRIO_TREE_ROOT(&mapping->i_mmap);
333333
INIT_LIST_HEAD(&mapping->i_mmap_nonlinear);
334-
mutex_init(&mapping->unmap_mutex);
335334
}
336335
EXPORT_SYMBOL(address_space_init_once);
337336

‎include/linux/fs.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,6 @@ struct address_space {
635635
struct prio_tree_root i_mmap; /* tree of private and shared mappings */
636636
struct list_head i_mmap_nonlinear;/*list VM_NONLINEAR mappings */
637637
spinlock_t i_mmap_lock; /* protect tree, count, list */
638-
unsigned int truncate_count; /* Cover race condition with truncate */
639638
unsigned long nrpages; /* number of total pages */
640639
pgoff_t writeback_index;/* writeback starts here */
641640
const struct address_space_operations *a_ops; /* methods */
@@ -644,7 +643,6 @@ struct address_space {
644643
spinlock_t private_lock; /* for use by the address_space */
645644
struct list_head private_list; /* ditto */
646645
struct address_space *assoc_mapping; /* ditto */
647-
struct mutex unmap_mutex; /* to protect unmapping */
648646
} __attribute__((aligned(sizeof(long))));
649647
/*
650648
* On most architectures that alignment is already the case; but

‎include/linux/mm.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -895,8 +895,6 @@ struct zap_details {
895895
struct address_space *check_mapping; /* Check page->mapping if set */
896896
pgoff_t first_index; /* Lowest page->index to unmap */
897897
pgoff_t last_index; /* Highest page->index to unmap */
898-
spinlock_t *i_mmap_lock; /* For unmap_mapping_range: */
899-
unsigned long truncate_count; /* Compare vm_truncate_count */
900898
};
901899

902900
struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr,

‎include/linux/mm_types.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ struct vm_area_struct {
175175
units, *not* PAGE_CACHE_SIZE */
176176
struct file * vm_file; /* File we map to (can be NULL). */
177177
void * vm_private_data; /* was vm_pte (shared mem) */
178-
unsigned long vm_truncate_count;/* truncate_count or restart_addr */
179178

180179
#ifndef CONFIG_MMU
181180
struct vm_region *vm_region; /* NOMMU mapping region */

‎kernel/fork.c‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,6 @@ static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
386386
spin_lock(&mapping->i_mmap_lock);
387387
if (tmp->vm_flags & VM_SHARED)
388388
mapping->i_mmap_writable++;
389-
tmp->vm_truncate_count = mpnt->vm_truncate_count;
390389
flush_dcache_mmap_lock(mapping);
391390
/* insert tmp into the share list, just after mpnt */
392391
vma_prio_tree_add(tmp, mpnt);

‎mm/memory.c‎

Lines changed: 27 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -986,13 +986,13 @@ int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
986986
static unsigned long zap_pte_range(struct mmu_gather *tlb,
987987
struct vm_area_struct *vma, pmd_t *pmd,
988988
unsigned long addr, unsigned long end,
989-
long *zap_work, struct zap_details *details)
989+
struct zap_details *details)
990990
{
991991
struct mm_struct *mm = tlb->mm;
992992
int force_flush = 0;
993-
pte_t *pte;
994-
spinlock_t *ptl;
995993
int rss[NR_MM_COUNTERS];
994+
spinlock_t *ptl;
995+
pte_t *pte;
996996

997997
again:
998998
init_rss_vec(rss);
@@ -1001,12 +1001,9 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb,
10011001
do {
10021002
pte_t ptent = *pte;
10031003
if (pte_none(ptent)) {
1004-
(*zap_work)--;
10051004
continue;
10061005
}
10071006

1008-
(*zap_work) -= PAGE_SIZE;
1009-
10101007
if (pte_present(ptent)) {
10111008
struct page *page;
10121009

@@ -1075,7 +1072,7 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb,
10751072
print_bad_pte(vma, addr, ptent, NULL);
10761073
}
10771074
pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
1078-
} while (pte++, addr += PAGE_SIZE, (addr != end && *zap_work > 0));
1075+
} while (pte++, addr += PAGE_SIZE, addr != end);
10791076

10801077
add_mm_rss_vec(mm, rss);
10811078
arch_leave_lazy_mmu_mode();
@@ -1099,7 +1096,7 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb,
10991096
static inline unsigned long zap_pmd_range(struct mmu_gather *tlb,
11001097
struct vm_area_struct *vma, pud_t *pud,
11011098
unsigned long addr, unsigned long end,
1102-
long *zap_work, struct zap_details *details)
1099+
struct zap_details *details)
11031100
{
11041101
pmd_t *pmd;
11051102
unsigned long next;
@@ -1111,49 +1108,42 @@ static inline unsigned long zap_pmd_range(struct mmu_gather *tlb,
11111108
if (next-addr != HPAGE_PMD_SIZE) {
11121109
VM_BUG_ON(!rwsem_is_locked(&tlb->mm->mmap_sem));
11131110
split_huge_page_pmd(vma->vm_mm, pmd);
1114-
} else if (zap_huge_pmd(tlb, vma, pmd)) {
1115-
(*zap_work)--;
1111+
} else if (zap_huge_pmd(tlb, vma, pmd))
11161112
continue;
1117-
}
11181113
/* fall through */
11191114
}
1120-
if (pmd_none_or_clear_bad(pmd)) {
1121-
(*zap_work)--;
1115+
if (pmd_none_or_clear_bad(pmd))
11221116
continue;
1123-
}
1124-
next = zap_pte_range(tlb, vma, pmd, addr, next,
1125-
zap_work, details);
1126-
} while (pmd++, addr = next, (addr != end && *zap_work > 0));
1117+
next = zap_pte_range(tlb, vma, pmd, addr, next, details);
1118+
cond_resched();
1119+
} while (pmd++, addr = next, addr != end);
11271120

11281121
return addr;
11291122
}
11301123

11311124
static inline unsigned long zap_pud_range(struct mmu_gather *tlb,
11321125
struct vm_area_struct *vma, pgd_t *pgd,
11331126
unsigned long addr, unsigned long end,
1134-
long *zap_work, struct zap_details *details)
1127+
struct zap_details *details)
11351128
{
11361129
pud_t *pud;
11371130
unsigned long next;
11381131

11391132
pud = pud_offset(pgd, addr);
11401133
do {
11411134
next = pud_addr_end(addr, end);
1142-
if (pud_none_or_clear_bad(pud)) {
1143-
(*zap_work)--;
1135+
if (pud_none_or_clear_bad(pud))
11441136
continue;
1145-
}
1146-
next = zap_pmd_range(tlb, vma, pud, addr, next,
1147-
zap_work, details);
1148-
} while (pud++, addr = next, (addr != end && *zap_work > 0));
1137+
next = zap_pmd_range(tlb, vma, pud, addr, next, details);
1138+
} while (pud++, addr = next, addr != end);
11491139

11501140
return addr;
11511141
}
11521142

11531143
static unsigned long unmap_page_range(struct mmu_gather *tlb,
11541144
struct vm_area_struct *vma,
11551145
unsigned long addr, unsigned long end,
1156-
long *zap_work, struct zap_details *details)
1146+
struct zap_details *details)
11571147
{
11581148
pgd_t *pgd;
11591149
unsigned long next;
@@ -1167,13 +1157,10 @@ static unsigned long unmap_page_range(struct mmu_gather *tlb,
11671157
pgd = pgd_offset(vma->vm_mm, addr);
11681158
do {
11691159
next = pgd_addr_end(addr, end);
1170-
if (pgd_none_or_clear_bad(pgd)) {
1171-
(*zap_work)--;
1160+
if (pgd_none_or_clear_bad(pgd))
11721161
continue;
1173-
}
1174-
next = zap_pud_range(tlb, vma, pgd, addr, next,
1175-
zap_work, details);
1176-
} while (pgd++, addr = next, (addr != end && *zap_work > 0));
1162+
next = zap_pud_range(tlb, vma, pgd, addr, next, details);
1163+
} while (pgd++, addr = next, addr != end);
11771164
tlb_end_vma(tlb, vma);
11781165
mem_cgroup_uncharge_end();
11791166

@@ -1218,9 +1205,7 @@ unsigned long unmap_vmas(struct mmu_gather *tlb,
12181205
unsigned long end_addr, unsigned long *nr_accounted,
12191206
struct zap_details *details)
12201207
{
1221-
long zap_work = ZAP_BLOCK_SIZE;
12221208
unsigned long start = start_addr;
1223-
spinlock_t *i_mmap_lock = details? details->i_mmap_lock: NULL;
12241209
struct mm_struct *mm = vma->vm_mm;
12251210

12261211
mmu_notifier_invalidate_range_start(mm, start_addr, end_addr);
@@ -1253,33 +1238,15 @@ unsigned long unmap_vmas(struct mmu_gather *tlb,
12531238
* Since no pte has actually been setup, it is
12541239
* safe to do nothing in this case.
12551240
*/
1256-
if (vma->vm_file) {
1241+
if (vma->vm_file)
12571242
unmap_hugepage_range(vma, start, end, NULL);
1258-
zap_work -= (end - start) /
1259-
pages_per_huge_page(hstate_vma(vma));
1260-
}
12611243

12621244
start = end;
12631245
} else
1264-
start = unmap_page_range(tlb, vma,
1265-
start, end, &zap_work, details);
1266-
1267-
if (zap_work > 0) {
1268-
BUG_ON(start != end);
1269-
break;
1270-
}
1271-
1272-
if (need_resched() ||
1273-
(i_mmap_lock && spin_needbreak(i_mmap_lock))) {
1274-
if (i_mmap_lock)
1275-
goto out;
1276-
cond_resched();
1277-
}
1278-
1279-
zap_work = ZAP_BLOCK_SIZE;
1246+
start = unmap_page_range(tlb, vma, start, end, details);
12801247
}
12811248
}
1282-
out:
1249+
12831250
mmu_notifier_invalidate_range_end(mm, start_addr, end_addr);
12841251
return start; /* which is now the end (or restart) address */
12851252
}
@@ -2612,96 +2579,11 @@ static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
26122579
return ret;
26132580
}
26142581

2615-
/*
2616-
* Helper functions for unmap_mapping_range().
2617-
*
2618-
* __ Notes on dropping i_mmap_lock to reduce latency while unmapping __
2619-
*
2620-
* We have to restart searching the prio_tree whenever we drop the lock,
2621-
* since the iterator is only valid while the lock is held, and anyway
2622-
* a later vma might be split and reinserted earlier while lock dropped.
2623-
*
2624-
* The list of nonlinear vmas could be handled more efficiently, using
2625-
* a placeholder, but handle it in the same way until a need is shown.
2626-
* It is important to search the prio_tree before nonlinear list: a vma
2627-
* may become nonlinear and be shifted from prio_tree to nonlinear list
2628-
* while the lock is dropped; but never shifted from list to prio_tree.
2629-
*
2630-
* In order to make forward progress despite restarting the search,
2631-
* vm_truncate_count is used to mark a vma as now dealt with, so we can
2632-
* quickly skip it next time around. Since the prio_tree search only
2633-
* shows us those vmas affected by unmapping the range in question, we
2634-
* can't efficiently keep all vmas in step with mapping->truncate_count:
2635-
* so instead reset them all whenever it wraps back to 0 (then go to 1).
2636-
* mapping->truncate_count and vma->vm_truncate_count are protected by
2637-
* i_mmap_lock.
2638-
*
2639-
* In order to make forward progress despite repeatedly restarting some
2640-
* large vma, note the restart_addr from unmap_vmas when it breaks out:
2641-
* and restart from that address when we reach that vma again. It might
2642-
* have been split or merged, shrunk or extended, but never shifted: so
2643-
* restart_addr remains valid so long as it remains in the vma's range.
2644-
* unmap_mapping_range forces truncate_count to leap over page-aligned
2645-
* values so we can save vma's restart_addr in its truncate_count field.
2646-
*/
2647-
#define is_restart_addr(truncate_count) (!((truncate_count) & ~PAGE_MASK))
2648-
2649-
static void reset_vma_truncate_counts(struct address_space *mapping)
2650-
{
2651-
struct vm_area_struct *vma;
2652-
struct prio_tree_iter iter;
2653-
2654-
vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, ULONG_MAX)
2655-
vma->vm_truncate_count = 0;
2656-
list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list)
2657-
vma->vm_truncate_count = 0;
2658-
}
2659-
2660-
static int unmap_mapping_range_vma(struct vm_area_struct *vma,
2582+
static void unmap_mapping_range_vma(struct vm_area_struct *vma,
26612583
unsigned long start_addr, unsigned long end_addr,
26622584
struct zap_details *details)
26632585
{
2664-
unsigned long restart_addr;
2665-
int need_break;
2666-
2667-
/*
2668-
* files that support invalidating or truncating portions of the
2669-
* file from under mmaped areas must have their ->fault function
2670-
* return a locked page (and set VM_FAULT_LOCKED in the return).
2671-
* This provides synchronisation against concurrent unmapping here.
2672-
*/
2673-
2674-
again:
2675-
restart_addr = vma->vm_truncate_count;
2676-
if (is_restart_addr(restart_addr) && start_addr < restart_addr) {
2677-
start_addr = restart_addr;
2678-
if (start_addr >= end_addr) {
2679-
/* Top of vma has been split off since last time */
2680-
vma->vm_truncate_count = details->truncate_count;
2681-
return 0;
2682-
}
2683-
}
2684-
2685-
restart_addr = zap_page_range(vma, start_addr,
2686-
end_addr - start_addr, details);
2687-
need_break = need_resched() || spin_needbreak(details->i_mmap_lock);
2688-
2689-
if (restart_addr >= end_addr) {
2690-
/* We have now completed this vma: mark it so */
2691-
vma->vm_truncate_count = details->truncate_count;
2692-
if (!need_break)
2693-
return 0;
2694-
} else {
2695-
/* Note restart_addr in vma's truncate_count field */
2696-
vma->vm_truncate_count = restart_addr;
2697-
if (!need_break)
2698-
goto again;
2699-
}
2700-
2701-
spin_unlock(details->i_mmap_lock);
2702-
cond_resched();
2703-
spin_lock(details->i_mmap_lock);
2704-
return -EINTR;
2586+
zap_page_range(vma, start_addr, end_addr - start_addr, details);
27052587
}
27062588

27072589
static inline void unmap_mapping_range_tree(struct prio_tree_root *root,
@@ -2711,12 +2593,8 @@ static inline void unmap_mapping_range_tree(struct prio_tree_root *root,
27112593
struct prio_tree_iter iter;
27122594
pgoff_t vba, vea, zba, zea;
27132595

2714-
restart:
27152596
vma_prio_tree_foreach(vma, &iter, root,
27162597
details->first_index, details->last_index) {
2717-
/* Skip quickly over those we have already dealt with */
2718-
if (vma->vm_truncate_count == details->truncate_count)
2719-
continue;
27202598

27212599
vba = vma->vm_pgoff;
27222600
vea = vba + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) - 1;
@@ -2728,11 +2606,10 @@ static inline void unmap_mapping_range_tree(struct prio_tree_root *root,
27282606
if (zea > vea)
27292607
zea = vea;
27302608

2731-
if (unmap_mapping_range_vma(vma,
2609+
unmap_mapping_range_vma(vma,
27322610
((zba - vba) << PAGE_SHIFT) + vma->vm_start,
27332611
((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start,
2734-
details) < 0)
2735-
goto restart;
2612+
details);
27362613
}
27372614
}
27382615

@@ -2747,15 +2624,9 @@ static inline void unmap_mapping_range_list(struct list_head *head,
27472624
* across *all* the pages in each nonlinear VMA, not just the pages
27482625
* whose virtual address lies outside the file truncation point.
27492626
*/
2750-
restart:
27512627
list_for_each_entry(vma, head, shared.vm_set.list) {
2752-
/* Skip quickly over those we have already dealt with */
2753-
if (vma->vm_truncate_count == details->truncate_count)
2754-
continue;
27552628
details->nonlinear_vma = vma;
2756-
if (unmap_mapping_range_vma(vma, vma->vm_start,
2757-
vma->vm_end, details) < 0)
2758-
goto restart;
2629+
unmap_mapping_range_vma(vma, vma->vm_start, vma->vm_end, details);
27592630
}
27602631
}
27612632

@@ -2794,26 +2665,14 @@ void unmap_mapping_range(struct address_space *mapping,
27942665
details.last_index = hba + hlen - 1;
27952666
if (details.last_index < details.first_index)
27962667
details.last_index = ULONG_MAX;
2797-
details.i_mmap_lock = &mapping->i_mmap_lock;
27982668

2799-
mutex_lock(&mapping->unmap_mutex);
2800-
spin_lock(&mapping->i_mmap_lock);
2801-
2802-
/* Protect against endless unmapping loops */
2803-
mapping->truncate_count++;
2804-
if (unlikely(is_restart_addr(mapping->truncate_count))) {
2805-
if (mapping->truncate_count == 0)
2806-
reset_vma_truncate_counts(mapping);
2807-
mapping->truncate_count++;
2808-
}
2809-
details.truncate_count = mapping->truncate_count;
28102669

2670+
spin_lock(&mapping->i_mmap_lock);
28112671
if (unlikely(!prio_tree_empty(&mapping->i_mmap)))
28122672
unmap_mapping_range_tree(&mapping->i_mmap, &details);
28132673
if (unlikely(!list_empty(&mapping->i_mmap_nonlinear)))
28142674
unmap_mapping_range_list(&mapping->i_mmap_nonlinear, &details);
28152675
spin_unlock(&mapping->i_mmap_lock);
2816-
mutex_unlock(&mapping->unmap_mutex);
28172676
}
28182677
EXPORT_SYMBOL(unmap_mapping_range);
28192678

0 commit comments

Comments
 (0)