Skip to content

Commit c9dbe82

Browse files
howlettakpm00
authored andcommitted
kernel/fork: use maple tree for dup_mmap() during forking
The maple tree was already tracking VMAs in this function by an earlier commit, but the rbtree iterator was being used to iterate the list. Change the iterator to use a maple tree native iterator and switch to the maple tree advanced API to avoid multiple walks of the tree during insert operations. Unexport the now-unused vma_store() function. For performance reasons we bulk allocate the maple tree nodes. The node calculations are done internally to the tree and use the VMA count and assume the worst-case node requirements. The VM_DONT_COPY flag does not allow for the most efficient copy method of the tree and so a bulk loading algorithm is used. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Liam R. Howlett <[email protected]> Signed-off-by: Matthew Wilcox (Oracle) <[email protected]> Acked-by: Vlastimil Babka <[email protected]> Tested-by: Yu Zhao <[email protected]> Cc: Catalin Marinas <[email protected]> Cc: David Hildenbrand <[email protected]> Cc: David Howells <[email protected]> Cc: Davidlohr Bueso <[email protected]> Cc: SeongJae Park <[email protected]> Cc: Sven Schnelle <[email protected]> Cc: Will Deacon <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 3499a13 commit c9dbe82

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

include/linux/mm.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2599,8 +2599,6 @@ extern bool arch_has_descending_max_zone_pfns(void);
25992599
/* nommu.c */
26002600
extern atomic_long_t mmap_pages_allocated;
26012601
extern int nommu_shrink_inode_mappings(struct inode *, size_t, size_t);
2602-
/* mmap.c */
2603-
void vma_mas_store(struct vm_area_struct *vma, struct ma_state *mas);
26042602

26052603
/* interval_tree.c */
26062604
void vma_interval_tree_insert(struct vm_area_struct *node,

kernel/fork.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,8 +583,9 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
583583
struct vm_area_struct *mpnt, *tmp, *prev, **pprev;
584584
struct rb_node **rb_link, *rb_parent;
585585
int retval;
586-
unsigned long charge;
586+
unsigned long charge = 0;
587587
LIST_HEAD(uf);
588+
MA_STATE(old_mas, &oldmm->mm_mt, 0, 0);
588589
MA_STATE(mas, &mm->mm_mt, 0, 0);
589590

590591
uprobe_start_dup_mmap();
@@ -620,7 +621,12 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
620621
goto out;
621622

622623
prev = NULL;
623-
for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) {
624+
625+
retval = mas_expected_entries(&mas, oldmm->map_count);
626+
if (retval)
627+
goto out;
628+
629+
mas_for_each(&old_mas, mpnt, ULONG_MAX) {
624630
struct file *file;
625631

626632
if (mpnt->vm_flags & VM_DONTCOPY) {
@@ -703,6 +709,8 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
703709
mas.index = tmp->vm_start;
704710
mas.last = tmp->vm_end - 1;
705711
mas_store(&mas, tmp);
712+
if (mas_is_err(&mas))
713+
goto fail_nomem_mas_store;
706714

707715
mm->map_count++;
708716
if (!(tmp->vm_flags & VM_WIPEONFORK))
@@ -726,6 +734,9 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
726734
fail_uprobe_end:
727735
uprobe_end_dup_mmap();
728736
return retval;
737+
738+
fail_nomem_mas_store:
739+
unlink_anon_vmas(tmp);
729740
fail_nomem_anon_vma_fork:
730741
mpol_put(vma_policy(tmp));
731742
fail_nomem_policy:

0 commit comments

Comments
 (0)