Reduce preallocations for maple tree
Subsystems (8)
v3 cover Reduce preallocations for maple tree
Initial work on preallocations showed no regression in performance during testing, but recently some users (both on [1] and off [android] list) have reported that preallocating the worst-case number of nodes has caused some slow down. This patch set addresses the number of allocations in a few ways.
During munmap() most munmap() operations will remove a single VMA, so leverage the fact that the maple tree can place a single pointer at range 0 - 0 without allocating. This is done by changing the index of the VMAs to be indexed by the count, starting at 0.
Re-introduce the entry argument to mas_preallocate() so that a more intelligent guess of the node count can be made.
Implement the more intelligent guess of the node count, although there is more work to be done.
During development of v2 of this patch set, I also noticed that the number of nodes being allocated for a rebalance was beyond what could possibly be needed. This is addressed in patch 0008.
Patches are in the following order:
0001-0002: Testing framework for benchmarking some operations
0003: Reduction of maple node allocation in sidetree
0004: Small cleanup of do_vmi_align_munmap()
0005-0014: mas_preallocate() calculation change
0015: Change the vma iterator order
Changes since v2:
- No longer moving the unmap_vmas() definition - Thanks Mike Kravetz
- Rebase on top of stack changes in do_vmi_align_munmap()
v2: https://lore.kernel.org/linux-mm/[email protected]/
v1: https://lore.kernel.org/lkml/[email protected]/
Liam R. Howlett (15):
maple_tree: Add benchmarking for mas_for_each
maple_tree: Add benchmarking for mas_prev()
mm: Change do_vmi_align_munmap() tracking of VMAs to remove
mm: Remove prev check from do_vmi_align_munmap()
maple_tree: Introduce __mas_set_range()
mm: Remove re-walk from mmap_region()
maple_tree: Re-introduce entry to mas_preallocate() arguments
maple_tree: Adjust node allocation on mas_rebalance()
mm: Use vma_iter_clear_gfp() in nommu
mm: Set up vma iterator for vma_iter_prealloc() calls
maple_tree: Move mas_wr_end_piv() below mas_wr_extend_null()
maple_tree: Update mas_preallocate() testing
maple_tree: Refine mas_preallocate() node calculations
maple_tree: Reduce resets during store setup mm/mmap: Change vma iteration order in do_vmi_align_munmap()
fs/exec.c | 1 + include/linux/maple_tree.h | 23 ++++- include/linux/mm.h | 4 +- lib/maple_tree.c | 117 +++++++++++++++------ lib/test_maple_tree.c | 76 ++++++++++++++ mm/internal.h | 36 +++++-- mm/memory.c | 16 ++- mm/mmap.c | 170 +++++++++++++++++-------------- mm/nommu.c | 45 ++++---- tools/testing/radix-tree/maple.c | 59 ++++++----- 10 files changed, 359 insertions(+), 188 deletions(-)
Signature · 1 line
2.39.2
PATCH v3 1/15 maple_tree: Add benchmarking for mas_for_each
Add a way to test the speed of mas_for_each() to the testing code.
--- lib/test_maple_tree.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+)
lib/test_maple_tree.c +39 -0
@@ -44,6 +44,7 @@ atomic_t maple_tree_tests_passed;/* #define BENCH_WALK */ /* #define BENCH_MT_FOR_EACH */ /* #define BENCH_FORK */ +/* #define BENCH_MAS_FOR_EACH */ #ifdef __KERNEL__#define mt_set_non_kernel (x) do {} while (0)
@@ -1770,6 +1771,37 @@ static noinline void __init bench_mt_for_each(struct maple_tree *mt) }#endif +#if defined(BENCH_MAS_FOR_EACH ) +static noinline void __init bench_mas_for_each(struct maple_tree *mt) +{ +int i, count =1000000 ; +unsigned long max =2500 ; +void *entry; +MA_STATE (mas, mt,0 ,0 ); + +for (i =0 ; i < max; i +=5 ) { +int gap =4 ; + +if (i %30 ==0 ) + gap =3 ; +mtree_store_range (mt, i, i + gap,xa_mk_value (i),GFP_KERNEL ); + } + +rcu_read_lock (); +for (i =0 ; i < count; i++) { +unsigned long j =0 ; + +mas_for_each (&mas, entry, max) { +MT_BUG_ON (mt, entry !=xa_mk_value (j)); + j +=5 ; + } +mas_set (&mas,0 ); + } +rcu_read_unlock (); + +} +#endif +/* check_forking - simulate the kernel forking sequence with the tree. */ static noinline void __init check_forking(struct maple_tree *mt) {
@@ -3498,6 +3530,13 @@ static int __init maple_tree_seed(void)mtree_destroy (&tree); goto skip;#endif +#if defined(BENCH_MAS_FOR_EACH ) +#define BENCH +mt_init_flags (&tree,MT_FLAGS_ALLOC_RANGE ); +bench_mas_for_each (&tree); +mtree_destroy (&tree); + goto skip; +#endif mt_init_flags (&tree,MT_FLAGS_ALLOC_RANGE );check_iteration (&tree);
Signature · 1 line
2.39.2
PATCH v3 2/15 maple_tree: Add benchmarking for mas_prev()
In reply to Liam R. HowlettAdd some benchmarking functions in testing for mas_prev(). This is useful to ensure there are no regressions added during modifications.
--- lib/test_maple_tree.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+)
lib/test_maple_tree.c +37 -0
@@ -45,6 +45,7 @@ atomic_t maple_tree_tests_passed;/* #define BENCH_MT_FOR_EACH */ /* #define BENCH_FORK */ /* #define BENCH_MAS_FOR_EACH */ +/* #define BENCH_MAS_PREV */ #ifdef __KERNEL__#define mt_set_non_kernel (x) do {} while (0)
@@ -1801,7 +1802,36 @@ static noinline void __init bench_mas_for_each(struct maple_tree *mt) }#endif +#if defined(BENCH_MAS_PREV ) +static noinline void __init bench_mas_prev(struct maple_tree *mt) +{ +int i, count =1000000 ; +unsigned long max =2500 ; +void *entry; +MA_STATE (mas, mt,0 ,0 ); + +for (i =0 ; i < max; i +=5 ) { +int gap =4 ; + +if (i %30 ==0 ) + gap =3 ; +mtree_store_range (mt, i, i + gap,xa_mk_value (i),GFP_KERNEL ); + } + +rcu_read_lock (); +for (i =0 ; i < count; i++) { +unsigned long j =2495 ; + +mas_set (&mas,ULONG_MAX ); +while ((entry =mas_prev (&mas,0 )) !=NULL ) { +MT_BUG_ON (mt, entry !=xa_mk_value (j)); + j -=5 ; + } + } +rcu_read_unlock (); +} +#endif /* check_forking - simulate the kernel forking sequence with the tree. */ static noinline void __init check_forking(struct maple_tree *mt) {
@@ -3537,6 +3567,13 @@ static int __init maple_tree_seed(void)mtree_destroy (&tree); goto skip;#endif +#if defined(BENCH_MAS_PREV ) +#define BENCH +mt_init_flags (&tree,MT_FLAGS_ALLOC_RANGE ); +bench_mas_prev (&tree); +mtree_destroy (&tree); + goto skip; +#endif mt_init_flags (&tree,MT_FLAGS_ALLOC_RANGE );check_iteration (&tree);
Signature · 1 line
2.39.2
PATCH v3 3/15 mm: Change do_vmi_align_munmap() tracking of VMAs to remove
In reply to Liam R. HowlettThe majority of the calls to munmap a vm range is within a single vma. The maple tree is able to store a single entry at 0, with a size of 1 as a pointer and avoid any allocations. Change do_vmi_align_munmap() to store the VMAs being munmap()'ed into a tree indexed by the count. This will leverage the ability to store the first entry without a node allocation.
Storing the entries into a tree by the count and not the vma start and end means changing the functions which iterate over the entries. Update unmap_vmas() and free_pgtables() to take a maple state and a tree end address to support this functionality.
Passing through the same maple state to unmap_vmas() and free_pgtables() means the state needs to be reset between calls. This happens in the static unmap_region() and exit_mmap().
--- include/linux/mm.h | 4 ++-- mm/internal.h | 2 +- mm/memory.c | 16 +++++++--------- mm/mmap.c | 41 ++++++++++++++++++++++++----------------- 4 files changed, 34 insertions(+), 29 deletions(-)
include/linux/mm.h +2 -2
@@ -2310,9 +2310,9 @@ static inline void zap_vma_pages(struct vm_area_struct *vma)zap_page_range_single (vma, vma->vm_start, vma->vm_end - vma->vm_start,NULL ); } -void unmap_vmas (struct mmu_gather *tlb,struct maple_tree *mt, +void unmap_vmas (struct mmu_gather *tlb,struct ma_state *mas,struct vm_area_struct *start_vma,unsigned long start, -unsigned long end,bool mm_wr_locked); +unsigned long end,unsigned long tree_end,bool mm_wr_locked);struct mmu_notifier_range ;
mm/internal.h +1 -1
@@ -109,7 +109,7 @@ bool __folio_end_writeback(struct folio *folio);void deactivate_file_folio (struct folio *folio);void folio_activate (struct folio *folio); -void free_pgtables (struct mmu_gather *tlb,struct maple_tree *mt, +void free_pgtables (struct mmu_gather *tlb,struct ma_state *mas,struct vm_area_struct *start_vma,unsigned long floor,unsigned long ceiling,bool mm_wr_locked);void pmd_install (struct mm_struct *mm,pmd_t *pmd,pgtable_t *pte);
mm/memory.c +7 -9
@@ -361,12 +361,10 @@ void free_pgd_range(struct mmu_gather *tlb, }while (pgd++, addr = next, addr != end); } -void free_pgtables (struct mmu_gather *tlb,struct maple_tree *mt, +void free_pgtables (struct mmu_gather *tlb,struct ma_state *mas,struct vm_area_struct *vma,unsigned long floor,unsigned long ceiling,bool mm_wr_locked) { -MA_STATE (mas, mt, vma->vm_end, vma->vm_end); - do { unsigned long addr = vma->vm_start; struct vm_area_struct *next;
@@ -375,7 +373,7 @@ void free_pgtables(struct mmu_gather *tlb, struct maple_tree *mt,* Note: USER_PGTABLES_CEILING may be passed as ceiling and may * be 0. This will underflow and is okay. */ - next =mas_find (&mas, ceiling -1 ); + next =mas_find (mas, ceiling -1 );/* * Hide vma from rmap and truncate_pagecache before freeing
@@ -396,7 +394,7 @@ void free_pgtables(struct mmu_gather *tlb, struct maple_tree *mt,while (next && next->vm_start <= vma->vm_end +PMD_SIZE && !is_vm_hugetlb_page (next)) { vma = next; - next =mas_find (&mas, ceiling -1 ); + next =mas_find (mas, ceiling -1 );if (mm_wr_locked)vma_start_write (vma);unlink_anon_vmas (vma);
@@ -1713,9 +1711,10 @@ static void unmap_single_vma(struct mmu_gather *tlb,* ensure that any thus-far unmapped pages are flushed before unmap_vmas() * drops the lock and schedules. */ -void unmap_vmas (struct mmu_gather *tlb,struct maple_tree *mt, +void unmap_vmas (struct mmu_gather *tlb,struct ma_state *mas,struct vm_area_struct *vma,unsigned long start_addr, -unsigned long end_addr,bool mm_wr_locked) +unsigned long end_addr,unsigned long tree_end, +bool mm_wr_locked) {struct mmu_notifier_range range;struct zap_details details = {
@@ -1723,7 +1722,6 @@ void unmap_vmas(struct mmu_gather *tlb, struct maple_tree *mt,/* Careful - we need to zap private pages too! */ .even_cows = true, }; -MA_STATE (mas, mt, vma->vm_end, vma->vm_end);mmu_notifier_range_init (&range,MMU_NOTIFY_UNMAP ,0 , vma->vm_mm, start_addr, end_addr);
@@ -1731,7 +1729,7 @@ void unmap_vmas(struct mmu_gather *tlb, struct maple_tree *mt,do {unmap_single_vma (tlb, vma, start_addr, end_addr, &details, mm_wr_locked); - }while ((vma =mas_find (&mas, end_addr -1 )) !=NULL ); + }while ((vma =mas_find (mas, tree_end -1 )) !=NULL );mmu_notifier_invalidate_range_end (&range); }
mm/mmap.c +24 -17
114 lines not on this page · raw message
Signature · 1 line
2.39.2
PATCH v3 4/15 mm: Remove prev check from do_vmi_align_munmap()
In reply to Liam R. HowlettIf the prev does not exist, the vma iterator will be set to MAS_NONE, which will be treated as a MAS_START when the mas_next or mas_find is used. In this case, the next caller will be the vma iterator, which uses mas_find() under the hood and will now do what the user expects.
--- mm/mmap.c | 2 -- 1 file changed, 2 deletions(-)
mm/mmap.c +0 -2
@@ -2458,8 +2458,6 @@ do_vmi_align_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma, } prev =vma_prev (vmi); -if (unlikely ((!prev))) -vma_iter_set (vmi, start);/* * Detach a range of VMAs from the mm. Using next as a temp variable as
Signature · 1 line
2.39.2
PATCH v3 5/15 maple_tree: Introduce __mas_set_range()
In reply to Liam R. Howlettmas_set_range() resets the node to MAS_START, which will cause a re-walk of the tree to the range. This is unnecessary when the maple state is already at the correct location of the write. Add a function that only sets the range to avoid unnecessary re-walking of the tree.
--- include/linux/maple_tree.h | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-)
include/linux/maple_tree.h +18 -3
@@ -539,6 +539,22 @@ static inline void mas_reset(struct ma_state *mas)*/ #define mas_for_each (__mas, __entry, __max) \ while (((__entry) = mas_find((__mas), (__max))) != NULL) +/** +* __mas_set_range() - Set up Maple Tree operation state to a sub-range of the +* current location. +* @mas: Maple Tree operation state. +* @start: New start of range in the Maple Tree. +* @last: New end of range in the Maple Tree. +* +* set the internal maple state values to a sub-range. +* Please use mas_set_range() if you do not know where you are in the tree. +*/ +static inline void __mas_set_range (struct ma_state *mas,unsigned long start, +unsigned long last) +{ + mas->index = start; + mas->last = last; +}/** * mas_set_range() - Set up Maple Tree operation state for a different index.
@@ -553,9 +569,8 @@ static inline void mas_reset(struct ma_state *mas)static inline void mas_set_range (struct ma_state *mas,unsigned long start,unsigned long last) { - mas->index = start; - mas->last = last; - mas->node =MAS_START ; +__mas_set_range (mas, start, last); + mas->node =MAS_START ; }/**
Signature · 1 line
2.39.2
PATCH v3 6/15 mm: Remove re-walk from mmap_region()
In reply to Liam R. HowlettUsing vma_iter_set() will reset the tree and cause a re-walk. Use vmi_iter_config() to set the write to a sub-set of the range. Change the file case to also use vmi_iter_config() so that the end is correctly set.
--- mm/internal.h | 8 ++++++++ mm/mmap.c | 15 ++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-)
mm/internal.h +8 -0
@@ -1041,6 +1041,14 @@ static inline bool vma_soft_dirty_enabled(struct vm_area_struct *vma)return !(vma->vm_flags &VM_SOFTDIRTY ); } +static inline void vma_iter_config (struct vma_iterator *vmi, +unsigned long index,unsigned long last) +{ +MAS_BUG_ON (&vmi->mas, vmi->mas.node !=MAS_START && + (vmi->mas.index > index || vmi->mas.last < index)); +__mas_set_range (&vmi->mas, index, last -1 ); +} +/* * VMA Iterator functions shared between nommu and mmap */
mm/mmap.c +10 -5
@@ -2675,8 +2675,11 @@ unsigned long mmap_region(struct file *file, unsigned long addr, next =vma_next (&vmi); prev =vma_prev (&vmi); -if (vm_flags &VM_SPECIAL ) +if (vm_flags &VM_SPECIAL ) { +if (prev) +vma_iter_next_range (&vmi); goto cannot_expand; + }/* Attempt to expand an old mapping */ /* Check next */
@@ -2697,6 +2700,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr, merge_start = prev->vm_start; vma = prev; vm_pgoff = prev->vm_pgoff; + }else if (prev) { +vma_iter_next_range (&vmi); }
@@ -2707,9 +2712,9 @@ unsigned long mmap_region(struct file *file, unsigned long addr, goto expanded; } +if (vma == prev) +vma_iter_set (&vmi, addr); cannot_expand: -if (prev) -vma_iter_next_range (&vmi);/* * Determine the object being mapped and call the appropriate
@@ -2722,7 +2727,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr, goto unacct_error; } -vma_iter_set (&vmi, addr); +vma_iter_config (&vmi, addr, end); vma->vm_start = addr; vma->vm_end = end;vm_flags_init (vma, vm_flags);
@@ -2749,7 +2754,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,if (WARN_ON ((addr != vma->vm_start))) goto close_and_free_vma; -vma_iter_set (&vmi, addr); +vma_iter_config (&vmi, addr, end);/* * If vm_flags changed after call_mmap(), we should try merge * vma again as we may succeed this time.
Signature · 1 line
2.39.2
PATCH v3 7/15 maple_tree: Re-introduce entry to mas_preallocate() arguments
In reply to Liam R. HowlettThe current preallocation strategy is to preallocate the absolute worst-case allocation for a tree modification. The entry (or NULL) is needed to know how many nodes are needed to write to the tree. Start by adding the argument to the mas_preallocate() definition.
--- include/linux/maple_tree.h | 2 +- lib/maple_tree.c | 3 ++- mm/internal.h | 2 +- mm/mmap.c | 4 ++-- tools/testing/radix-tree/maple.c | 32 ++++++++++++++++---------------- 5 files changed, 22 insertions(+), 21 deletions(-)
include/linux/maple_tree.h +1 -1
@@ -466,7 +466,7 @@ void *mas_find(struct ma_state *mas, unsigned long max);void *mas_find_range (struct ma_state *mas,unsigned long max);void *mas_find_rev (struct ma_state *mas,unsigned long min);void *mas_find_range_rev (struct ma_state *mas,unsigned long max); -int mas_preallocate (struct ma_state *mas,gfp_t gfp); +int mas_preallocate (struct ma_state *mas,void *entry,gfp_t gfp);bool mas_is_err (struct ma_state *mas);bool mas_nomem (struct ma_state *mas,gfp_t gfp);
lib/maple_tree.c +2 -1
@@ -5529,11 +5529,12 @@ EXPORT_SYMBOL_GPL(mas_store_prealloc);/** * mas_preallocate() - Preallocate enough nodes for a store operation * @mas: The maple state +* @entry: The entry that will be stored * @gfp: The GFP_FLAGS to use for allocations. * * Return: 0 on success, -ENOMEM if memory could not be allocated. */ -int mas_preallocate (struct ma_state *mas,gfp_t gfp) +int mas_preallocate (struct ma_state *mas,void *entry,gfp_t gfp) {int ret;
mm/internal.h +1 -1
@@ -1054,7 +1054,7 @@ static inline void vma_iter_config(struct vma_iterator *vmi,*/ static inline int vma_iter_prealloc (struct vma_iterator *vmi) { -return mas_preallocate (&vmi->mas,GFP_KERNEL ); +return mas_preallocate (&vmi->mas,NULL ,GFP_KERNEL ); } static inline void vma_iter_clear(struct vma_iterator *vmi,
mm/mmap.c +2 -2
@@ -1959,7 +1959,7 @@ static int expand_upwards(struct vm_area_struct *vma, unsigned long address)/* Check that both stack segments have the same anon_vma? */ } -if (mas_preallocate (&mas,GFP_KERNEL )) +if (mas_preallocate (&mas, vma,GFP_KERNEL ))return -ENOMEM ;/* We must make sure the anon_vma is allocated. */
@@ -2049,7 +2049,7 @@ int expand_downwards(struct vm_area_struct *vma, unsigned long address)return -ENOMEM ; } -if (mas_preallocate (&mas,GFP_KERNEL )) +if (mas_preallocate (&mas, vma,GFP_KERNEL ))return -ENOMEM ;/* We must make sure the anon_vma is allocated. */
tools/testing/radix-tree/maple.c +16 -16
@@ -35458,7 +35458,7 @@ static noinline void __init check_prealloc(struct maple_tree *mt)for (i =0 ; i <= max; i++)mtree_test_store_range (mt, i *10 , i *10 +5 , &i); -MT_BUG_ON (mt,mas_preallocate (&mas,GFP_KERNEL ) !=0 ); +MT_BUG_ON (mt,mas_preallocate (&mas, ptr,GFP_KERNEL ) !=0 ); allocated =mas_allocated (&mas); height =mas_mt_height (&mas);MT_BUG_ON (mt, allocated ==0 );
@@ -35467,18 +35467,18 @@ static noinline void __init check_prealloc(struct maple_tree *mt) allocated =mas_allocated (&mas);MT_BUG_ON (mt, allocated !=0 ); -MT_BUG_ON (mt,mas_preallocate (&mas,GFP_KERNEL ) !=0 ); +MT_BUG_ON (mt,mas_preallocate (&mas, ptr,GFP_KERNEL ) !=0 ); allocated =mas_allocated (&mas); height =mas_mt_height (&mas);MT_BUG_ON (mt, allocated ==0 );MT_BUG_ON (mt, allocated !=1 + height *3 ); -MT_BUG_ON (mt,mas_preallocate (&mas,GFP_KERNEL ) !=0 ); +MT_BUG_ON (mt,mas_preallocate (&mas, ptr,GFP_KERNEL ) !=0 );mas_destroy (&mas); allocated =mas_allocated (&mas);MT_BUG_ON (mt, allocated !=0 ); -MT_BUG_ON (mt,mas_preallocate (&mas,GFP_KERNEL ) !=0 ); +MT_BUG_ON (mt,mas_preallocate (&mas, ptr,GFP_KERNEL ) !=0 ); allocated =mas_allocated (&mas); height =mas_mt_height (&mas);MT_BUG_ON (mt, allocated ==0 );
@@ -35487,26 +35487,26 @@ static noinline void __init check_prealloc(struct maple_tree *mt)MT_BUG_ON (mt,mas_allocated (&mas) != allocated -1 ); mn->parent =ma_parent_ptr (mn);ma_free_rcu (mn); -MT_BUG_ON (mt,mas_preallocate (&mas,GFP_KERNEL ) !=0 ); +MT_BUG_ON (mt,mas_preallocate (&mas, ptr,GFP_KERNEL ) !=0 );mas_destroy (&mas); allocated =mas_allocated (&mas);MT_BUG_ON (mt, allocated !=0 ); -MT_BUG_ON (mt,mas_preallocate (&mas,GFP_KERNEL ) !=0 ); +MT_BUG_ON (mt,mas_preallocate (&mas, ptr,GFP_KERNEL ) !=0 ); allocated =mas_allocated (&mas); height =mas_mt_height (&mas);MT_BUG_ON (mt, allocated ==0 );MT_BUG_ON (mt, allocated !=1 + height *3 ); mn =mas_pop_node (&mas);MT_BUG_ON (mt,mas_allocated (&mas) != allocated -1 ); -MT_BUG_ON (mt,mas_preallocate (&mas,GFP_KERNEL ) !=0 ); +MT_BUG_ON (mt,mas_preallocate (&mas, ptr,GFP_KERNEL ) !=0 );mas_destroy (&mas); allocated =mas_allocated (&mas);MT_BUG_ON (mt, allocated !=0 ); mn->parent =ma_parent_ptr (mn);ma_free_rcu (mn); -MT_BUG_ON (mt,mas_preallocate (&mas,GFP_KERNEL ) !=0 ); +MT_BUG_ON (mt,mas_preallocate (&mas, ptr,GFP_KERNEL ) !=0 ); allocated =mas_allocated (&mas); height =mas_mt_height (&mas);MT_BUG_ON (mt, allocated ==0 );
@@ -35515,12 +35515,12 @@ static noinline void __init check_prealloc(struct maple_tree *mt)MT_BUG_ON (mt,mas_allocated (&mas) != allocated -1 );mas_push_node (&mas, mn);MT_BUG_ON (mt,mas_allocated (&mas) != allocated); -MT_BUG_ON (mt,mas_preallocate (&mas,GFP_KERNEL ) !=0 ); +MT_BUG_ON (mt,mas_preallocate (&mas, ptr,GFP_KERNEL ) !=0 );mas_destroy (&mas); allocated =mas_allocated (&mas);MT_BUG_ON (mt, allocated !=0 ); -MT_BUG_ON (mt,mas_preallocate (&mas,GFP_KERNEL ) !=0 ); +MT_BUG_ON (mt,mas_preallocate (&mas, ptr,GFP_KERNEL ) !=0 ); allocated =mas_allocated (&mas); height =mas_mt_height (&mas);MT_BUG_ON (mt, allocated ==0 );
@@ -35528,21 +35528,21 @@ static noinline void __init check_prealloc(struct maple_tree *mt)mas_store_prealloc (&mas, ptr);MT_BUG_ON (mt,mas_allocated (&mas) !=0 ); -MT_BUG_ON (mt,mas_preallocate (&mas,GFP_KERNEL ) !=0 ); +MT_BUG_ON (mt,mas_preallocate (&mas, ptr,GFP_KERNEL ) !=0 ); allocated =mas_allocated (&mas); height =mas_mt_height (&mas);MT_BUG_ON (mt, allocated ==0 );MT_BUG_ON (mt, allocated !=1 + height *3 );mas_store_prealloc (&mas, ptr);MT_BUG_ON (mt,mas_allocated (&mas) !=0 ); -MT_BUG_ON (mt,mas_preallocate (&mas,GFP_KERNEL ) !=0 ); +MT_BUG_ON (mt,mas_preallocate (&mas, ptr,GFP_KERNEL ) !=0 ); allocated =mas_allocated (&mas); height =mas_mt_height (&mas);MT_BUG_ON (mt, allocated ==0 );MT_BUG_ON (mt, allocated !=1 + height *3 );mas_store_prealloc (&mas, ptr); -MT_BUG_ON (mt,mas_preallocate (&mas,GFP_KERNEL ) !=0 ); +MT_BUG_ON (mt,mas_preallocate (&mas, ptr,GFP_KERNEL ) !=0 ); allocated =mas_allocated (&mas); height =mas_mt_height (&mas);MT_BUG_ON (mt, allocated ==0 );
@@ -35550,14 +35550,14 @@ static noinline void __init check_prealloc(struct maple_tree *mt)mas_store_prealloc (&mas, ptr);MT_BUG_ON (mt,mas_allocated (&mas) !=0 );mt_set_non_kernel (1 ); -MT_BUG_ON (mt,mas_preallocate (&mas,GFP_KERNEL &GFP_NOWAIT ) ==0 ); +MT_BUG_ON (mt,mas_preallocate (&mas, ptr,GFP_KERNEL &GFP_NOWAIT ) ==0 ); allocated =mas_allocated (&mas); height =mas_mt_height (&mas);MT_BUG_ON (mt, allocated !=0 );mas_destroy (&mas); -MT_BUG_ON (mt,mas_preallocate (&mas,GFP_KERNEL ) !=0 ); +MT_BUG_ON (mt,mas_preallocate (&mas, ptr,GFP_KERNEL ) !=0 ); allocated =mas_allocated (&mas); height =mas_mt_height (&mas);MT_BUG_ON (mt, allocated ==0 );
@@ -35565,7 +35565,7 @@ static noinline void __init check_prealloc(struct maple_tree *mt)mas_store_prealloc (&mas, ptr);MT_BUG_ON (mt,mas_allocated (&mas) !=0 );mt_set_non_kernel (1 ); -MT_BUG_ON (mt,mas_preallocate (&mas,GFP_KERNEL &GFP_NOWAIT ) ==0 ); +MT_BUG_ON (mt,mas_preallocate (&mas, ptr,GFP_KERNEL &GFP_NOWAIT ) ==0 ); allocated =mas_allocated (&mas); height =mas_mt_height (&mas);MT_BUG_ON (mt, allocated !=0 );
Signature · 1 line
2.39.2
PATCH v3 8/15 maple_tree: Adjust node allocation on mas_rebalance()
In reply to Liam R. Howlettmas_rebalance() is called to rebalance an insufficient node into a single node or two sufficient nodes. The preallocation estimate is always too many in this case as the height of the tree will never grow and there is no possibility to have a three way split in this case, so revise the node allocation count.
--- lib/maple_tree.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/maple_tree.c +1 -1
@@ -3136,7 +3136,7 @@ static inline int mas_rebalance(struct ma_state *mas,* tries to combine the data in the same way. If one node contains the * entire range of the tree, then that node is used as a new root node. */ -mas_node_count (mas,1 + empty_count *3 ); +mas_node_count (mas, empty_count *2 -1 );if (mas_is_err (mas))return 0 ;
Signature · 1 line
2.39.2
PATCH v3 9/15 mm: Use vma_iter_clear_gfp() in nommu
In reply to Liam R. HowlettMove the definition of vma_iter_clear_gfp() from mmap.c to internal.h so it can be used in the nommu code. This will reduce node preallocations in nommu.
--- mm/internal.h | 12 ++++++++++++ mm/mmap.c | 12 ------------ mm/nommu.c | 12 ++++-------- 3 files changed, 16 insertions(+), 20 deletions(-)
mm/internal.h +12 -0
@@ -1064,6 +1064,18 @@ static inline void vma_iter_clear(struct vma_iterator *vmi,mas_store_prealloc (&vmi->mas,NULL ); } +static inline int vma_iter_clear_gfp (struct vma_iterator *vmi, +unsigned long start,unsigned long end,gfp_t gfp) +{ + vmi->mas.index = start; + vmi->mas.last = end -1 ; +mas_store_gfp (&vmi->mas,NULL , gfp); +if (unlikely (mas_is_err (&vmi->mas))) +return -ENOMEM ; + +return 0 ; +} +static inline struct vm_area_struct *vma_iter_load (struct vma_iterator *vmi) {return mas_walk (&vmi->mas);
mm/mmap.c +0 -12
@@ -154,18 +154,6 @@ static inline struct vm_area_struct *vma_prev_limit(struct vma_iterator *vmi,return mas_prev (&vmi->mas, min); } -static inline int vma_iter_clear_gfp (struct vma_iterator *vmi, -unsigned long start,unsigned long end,gfp_t gfp) -{ - vmi->mas.index = start; - vmi->mas.last = end -1 ; -mas_store_gfp (&vmi->mas,NULL , gfp); -if (unlikely (mas_is_err (&vmi->mas))) -return -ENOMEM ; - -return 0 ; -} -/* * check_brk_limits() - Use platform specific check of range & verify mlock * limits.
mm/nommu.c +4 -8
@@ -1396,17 +1396,13 @@ static int vmi_shrink_vma(struct vma_iterator *vmi,/* adjust the VMA's pointers, which may reposition it in the MM's tree * and list */ -if (vma_iter_prealloc (vmi)) { -pr_warn ("Allocation of vma tree for process %d failed\n" , - current->pid); -return -ENOMEM ; - } -if (from > vma->vm_start) { -vma_iter_clear (vmi, from, vma->vm_end); +if (vma_iter_clear_gfp (vmi, from, vma->vm_end,GFP_KERNEL )) +return -ENOMEM ; vma->vm_end = from; }else { -vma_iter_clear (vmi, vma->vm_start, to); +if (vma_iter_clear_gfp (vmi, vma->vm_start, to,GFP_KERNEL )) +return -ENOMEM ; vma->vm_start = to; }
Signature · 1 line
2.39.2
PATCH v3 10/15 mm: Set up vma iterator for vma_iter_prealloc() calls
In reply to Liam R. HowlettSet the correct limits for vma_iter_prealloc() calls so that the maple tree can be smarter about how many nodes are needed.
--- fs/exec.c | 1 + mm/internal.h | 18 ++++++-------- mm/mmap.c | 69 +++++++++++++++++++++++++++++++-------------------- mm/nommu.c | 33 +++++++++++------------- 4 files changed, 64 insertions(+), 57 deletions(-)
fs/exec.c +1 -0
@@ -701,6 +701,7 @@ static int shift_arg_pages(struct vm_area_struct *vma, unsigned long shift)if (vma !=vma_next (&vmi))return -EFAULT ; +vma_iter_prev_range (&vmi);/* * cover the whole range: [new_start, old_end) */
mm/internal.h +7 -11
@@ -1052,23 +1052,21 @@ static inline void vma_iter_config(struct vma_iterator *vmi,/* * VMA Iterator functions shared between nommu and mmap */ -static inline int vma_iter_prealloc (struct vma_iterator *vmi) +static inline int vma_iter_prealloc (struct vma_iterator *vmi, +struct vm_area_struct *vma) { -return mas_preallocate (&vmi->mas,NULL ,GFP_KERNEL ); +return mas_preallocate (&vmi->mas, vma,GFP_KERNEL ); } -static inline void vma_iter_clear (struct vma_iterator *vmi, -unsigned long start,unsigned long end) +static inline void vma_iter_clear (struct vma_iterator *vmi) { -mas_set_range (&vmi->mas, start, end -1 );mas_store_prealloc (&vmi->mas,NULL ); }static inline int vma_iter_clear_gfp (struct vma_iterator *vmi,unsigned long start,unsigned long end,gfp_t gfp) { - vmi->mas.index = start; - vmi->mas.last = end -1 ; +__mas_set_range (&vmi->mas, start, end -1 );mas_store_gfp (&vmi->mas,NULL , gfp);if (unlikely (mas_is_err (&vmi->mas)))return -ENOMEM ;
@@ -1105,8 +1103,7 @@ static inline void vma_iter_store(struct vma_iterator *vmi, ((vmi->mas.index > vma->vm_start) || (vmi->mas.last < vma->vm_start)))vma_iter_invalidate (vmi); - vmi->mas.index = vma->vm_start; - vmi->mas.last = vma->vm_end -1 ; +__mas_set_range (&vmi->mas, vma->vm_start, vma->vm_end -1 );mas_store_prealloc (&vmi->mas, vma); }
@@ -1117,8 +1114,7 @@ static inline int vma_iter_store_gfp(struct vma_iterator *vmi, ((vmi->mas.index > vma->vm_start) || (vmi->mas.last < vma->vm_start)))vma_iter_invalidate (vmi); - vmi->mas.index = vma->vm_start; - vmi->mas.last = vma->vm_end -1 ; +__mas_set_range (&vmi->mas, vma->vm_start, vma->vm_end -1 );mas_store_gfp (&vmi->mas, vma, gfp);if (unlikely (mas_is_err (&vmi->mas)))return -ENOMEM ;
mm/mmap.c +42 -27
178 lines not on this page · raw message
mm/nommu.c +14 -19
@@ -583,7 +583,8 @@ static int delete_vma_from_mm(struct vm_area_struct *vma) {VMA_ITERATOR (vmi, vma->vm_mm, vma->vm_start); -if (vma_iter_prealloc (&vmi)) { +vma_iter_config (&vmi, vma->vm_start, vma->vm_end); +if (vma_iter_prealloc (&vmi, vma)) {pr_warn ("Allocation of vma tree for process %d failed\n" , current->pid);return -ENOMEM ;
@@ -591,7 +592,7 @@ static int delete_vma_from_mm(struct vm_area_struct *vma)cleanup_vma_from_mm (vma);/* remove from the MM's tree and list */ -vma_iter_clear (&vmi, vma->vm_start, vma->vm_end); +vma_iter_clear (&vmi);return 0 ; }/*
@@ -1054,9 +1055,6 @@ unsigned long do_mmap(struct file *file,if (!vma) goto error_getting_vma; -if (vma_iter_prealloc (&vmi)) - goto error_vma_iter_prealloc; - region->vm_usage =1 ; region->vm_flags = vm_flags; region->vm_pgoff = pgoff;
@@ -1198,6 +1196,10 @@ unsigned long do_mmap(struct file *file, share:BUG_ON (!vma->vm_region); +vma_iter_config (&vmi, vma->vm_start, vma->vm_end); +if (vma_iter_prealloc (&vmi, vma)) + goto error_just_free; +setup_vma_to_mm (vma, current->mm); current->mm->map_count++;/* add the VMA to the tree */
@@ -1244,14 +1246,6 @@ unsigned long do_mmap(struct file *file, len, current->pid);show_mem ();return -ENOMEM ; - -error_vma_iter_prealloc: -kmem_cache_free (vm_region_jar, region); -vm_area_free (vma); -pr_warn ("Allocation of vma tree for process %d failed\n" , current->pid); -show_mem (); -return -ENOMEM ; - } unsigned long ksys_mmap_pgoff(unsigned long addr, unsigned long len,
@@ -1336,12 +1330,6 @@ int split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,if (!new) goto err_vma_dup; -if (vma_iter_prealloc (vmi)) { -pr_warn ("Allocation of vma tree for process %d failed\n" , - current->pid); - goto err_vmi_preallocate; - } -/* most fields are the same, copy all, and then fixup */ *region = *vma->vm_region; new->vm_region = region;
@@ -1355,6 +1343,13 @@ int split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma, region->vm_pgoff = new->vm_pgoff += npages; } +vma_iter_config (vmi, new->vm_start, new->vm_end); +if (vma_iter_prealloc (vmi, vma)) { +pr_warn ("Allocation of vma tree for process %d failed\n" , + current->pid); + goto err_vmi_preallocate; + } +if (new->vm_ops && new->vm_ops->open) new->vm_ops->open (new);
Signature · 1 line
2.39.2
PATCH v3 11/15 maple_tree: Move mas_wr_end_piv() below mas_wr_extend_null()
In reply to Liam R. HowlettRelocate it and call mas_wr_extend_null() from within mas_wr_end_piv(). Extending the NULL may affect the end pivot value so call mas_wr_endtend_null() from within mas_wr_end_piv() to keep it all together.
--- lib/maple_tree.c | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-)
lib/maple_tree.c +15 -16
@@ -4180,18 +4180,6 @@ static inline bool mas_wr_slot_store(struct ma_wr_state *wr_mas)return true; } -static inline void mas_wr_end_piv (struct ma_wr_state *wr_mas) -{ -while ((wr_mas->offset_end < wr_mas->node_end) && - (wr_mas->mas->last > wr_mas->pivots[wr_mas->offset_end])) - wr_mas->offset_end++; - -if (wr_mas->offset_end < wr_mas->node_end) - wr_mas->end_piv = wr_mas->pivots[wr_mas->offset_end]; -else - wr_mas->end_piv = wr_mas->mas->max; -} -static inline void mas_wr_extend_null (struct ma_wr_state *wr_mas) {struct ma_state *mas = wr_mas->mas;
@@ -4228,6 +4216,21 @@ static inline void mas_wr_extend_null(struct ma_wr_state *wr_mas) } } +static inline void mas_wr_end_piv (struct ma_wr_state *wr_mas) +{ +while ((wr_mas->offset_end < wr_mas->node_end) && + (wr_mas->mas->last > wr_mas->pivots[wr_mas->offset_end])) + wr_mas->offset_end++; + +if (wr_mas->offset_end < wr_mas->node_end) + wr_mas->end_piv = wr_mas->pivots[wr_mas->offset_end]; +else + wr_mas->end_piv = wr_mas->mas->max; + +if (!wr_mas->entry) +mas_wr_extend_null (wr_mas); +} +static inline unsigned char mas_wr_new_end (struct ma_wr_state *wr_mas) {struct ma_state *mas = wr_mas->mas;
@@ -4371,10 +4374,6 @@ static inline void *mas_wr_store_entry(struct ma_wr_state *wr_mas)/* At this point, we are at the leaf node that needs to be altered. */ mas_wr_end_piv (wr_mas); - -if (!wr_mas->entry) -mas_wr_extend_null (wr_mas); -/* New root for a single pointer */ if (unlikely (!mas->index && mas->last ==ULONG_MAX )) {mas_new_root (mas, wr_mas->entry);
Signature · 1 line
2.39.2
PATCH v3 12/15 maple_tree: Update mas_preallocate() testing
In reply to Liam R. HowlettSince the mas_preallocate() calculation has been updated to be more precise, the testing must also be updated to check for what is expected.
--- tools/testing/radix-tree/maple.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-)
tools/testing/radix-tree/maple.c +16 -11
@@ -35458,6 +35458,8 @@ static noinline void __init check_prealloc(struct maple_tree *mt)for (i =0 ; i <= max; i++)mtree_test_store_range (mt, i *10 , i *10 +5 , &i); +/* Spanning store */ +mas_set_range (&mas,470 ,500 );MT_BUG_ON (mt,mas_preallocate (&mas, ptr,GFP_KERNEL ) !=0 ); allocated =mas_allocated (&mas); height =mas_mt_height (&mas);
@@ -35481,7 +35483,6 @@ static noinline void __init check_prealloc(struct maple_tree *mt)MT_BUG_ON (mt,mas_preallocate (&mas, ptr,GFP_KERNEL ) !=0 ); allocated =mas_allocated (&mas); height =mas_mt_height (&mas); -MT_BUG_ON (mt, allocated ==0 );MT_BUG_ON (mt, allocated !=1 + height *3 ); mn =mas_pop_node (&mas);MT_BUG_ON (mt,mas_allocated (&mas) != allocated -1 );
@@ -35495,7 +35496,6 @@ static noinline void __init check_prealloc(struct maple_tree *mt)MT_BUG_ON (mt,mas_preallocate (&mas, ptr,GFP_KERNEL ) !=0 ); allocated =mas_allocated (&mas); height =mas_mt_height (&mas); -MT_BUG_ON (mt, allocated ==0 );MT_BUG_ON (mt, allocated !=1 + height *3 ); mn =mas_pop_node (&mas);MT_BUG_ON (mt,mas_allocated (&mas) != allocated -1 );
@@ -35509,7 +35509,6 @@ static noinline void __init check_prealloc(struct maple_tree *mt)MT_BUG_ON (mt,mas_preallocate (&mas, ptr,GFP_KERNEL ) !=0 ); allocated =mas_allocated (&mas); height =mas_mt_height (&mas); -MT_BUG_ON (mt, allocated ==0 );MT_BUG_ON (mt, allocated !=1 + height *3 ); mn =mas_pop_node (&mas);MT_BUG_ON (mt,mas_allocated (&mas) != allocated -1 );
@@ -35523,33 +35522,37 @@ static noinline void __init check_prealloc(struct maple_tree *mt)MT_BUG_ON (mt,mas_preallocate (&mas, ptr,GFP_KERNEL ) !=0 ); allocated =mas_allocated (&mas); height =mas_mt_height (&mas); -MT_BUG_ON (mt, allocated ==0 );MT_BUG_ON (mt, allocated !=1 + height *3 );mas_store_prealloc (&mas, ptr);MT_BUG_ON (mt,mas_allocated (&mas) !=0 ); +/* Slot store does not need allocations */ +mas_set_range (&mas,6 ,9 );MT_BUG_ON (mt,mas_preallocate (&mas, ptr,GFP_KERNEL ) !=0 ); allocated =mas_allocated (&mas); - height =mas_mt_height (&mas); -MT_BUG_ON (mt, allocated ==0 ); -MT_BUG_ON (mt, allocated !=1 + height *3 ); +MT_BUG_ON (mt, allocated !=0 );mas_store_prealloc (&mas, ptr);MT_BUG_ON (mt,mas_allocated (&mas) !=0 ); + +mas_set_range (&mas,6 ,10 );MT_BUG_ON (mt,mas_preallocate (&mas, ptr,GFP_KERNEL ) !=0 ); allocated =mas_allocated (&mas); height =mas_mt_height (&mas); -MT_BUG_ON (mt, allocated ==0 ); -MT_BUG_ON (mt, allocated !=1 + height *3 ); +MT_BUG_ON (mt, allocated !=1 );mas_store_prealloc (&mas, ptr); +MT_BUG_ON (mt,mas_allocated (&mas) !=0 ); +/* Split */ +mas_set_range (&mas,54 ,54 );MT_BUG_ON (mt,mas_preallocate (&mas, ptr,GFP_KERNEL ) !=0 ); allocated =mas_allocated (&mas); height =mas_mt_height (&mas); -MT_BUG_ON (mt, allocated ==0 ); -MT_BUG_ON (mt, allocated !=1 + height *3 ); +MT_BUG_ON (mt, allocated !=1 + height *2 );mas_store_prealloc (&mas, ptr);MT_BUG_ON (mt,mas_allocated (&mas) !=0 );mt_set_non_kernel (1 ); +/* Spanning store */ +mas_set_range (&mas,1 ,100 );MT_BUG_ON (mt,mas_preallocate (&mas, ptr,GFP_KERNEL &GFP_NOWAIT ) ==0 ); allocated =mas_allocated (&mas); height =mas_mt_height (&mas);
@@ -35557,6 +35560,7 @@ static noinline void __init check_prealloc(struct maple_tree *mt)mas_destroy (&mas); +/* Spanning store */ MT_BUG_ON (mt,mas_preallocate (&mas, ptr,GFP_KERNEL ) !=0 ); allocated =mas_allocated (&mas); height =mas_mt_height (&mas);
@@ -35564,6 +35568,7 @@ static noinline void __init check_prealloc(struct maple_tree *mt)MT_BUG_ON (mt, allocated !=1 + height *3 );mas_store_prealloc (&mas, ptr);MT_BUG_ON (mt,mas_allocated (&mas) !=0 ); +mas_set_range (&mas,0 ,200 );mt_set_non_kernel (1 );MT_BUG_ON (mt,mas_preallocate (&mas, ptr,GFP_KERNEL &GFP_NOWAIT ) ==0 ); allocated =mas_allocated (&mas);
Signature · 1 line
2.39.2
PATCH v3 13/15 maple_tree: Refine mas_preallocate() node calculations
In reply to Liam R. HowlettCalculate the number of nodes based on the pending write action instead of assuming the worst case.
This addresses a performance regression introduced in platforms that have longer allocation timing.
--- lib/maple_tree.c | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-)
lib/maple_tree.c +43 -1
@@ -5535,9 +5535,51 @@ EXPORT_SYMBOL_GPL(mas_store_prealloc);*/ int mas_preallocate (struct ma_state *mas,void *entry,gfp_t gfp) { +MA_WR_STATE (wr_mas, mas, entry); +unsigned char node_size; +int request =1 ;int ret; -mas_node_count_gfp (mas,1 +mas_mt_height (mas) *3 , gfp); + +if (unlikely (!mas->index && mas->last ==ULONG_MAX )) + goto ask_now; + +mas_wr_store_setup (&wr_mas); + wr_mas.content =mas_start (mas); +/* Root expand */ +if (unlikely (mas_is_none (mas) ||mas_is_ptr (mas))) + goto ask_now; + +if (unlikely (!mas_wr_walk (&wr_mas))) { +/* Spanning store, use worst case for now */ + request =1 +mas_mt_height (mas) *3 ; + goto ask_now; + } + +/* At this point, we are at the leaf node that needs to be altered. */ +/* Exact fit, no nodes needed. */ +if (wr_mas.r_min == mas->index && wr_mas.r_max == mas->last) +return 0 ; + +mas_wr_end_piv (&wr_mas); + node_size =mas_wr_new_end (&wr_mas); +if (node_size >= mt_slots[wr_mas.type]) { +/* Split, worst case for now. */ + request =1 +mas_mt_height (mas) *2 ; + goto ask_now; + } + +/* New root needs a singe node */ +if (unlikely (mte_is_root (mas->node))) + goto ask_now; + +/* Potential spanning rebalance collapsing a node, use worst-case */ +if (node_size -1 <= mt_min_slots[wr_mas.type]) + request =mas_mt_height (mas) *2 -1 ; + +/* node store, slot store needs one node */ +ask_now: +mas_node_count_gfp (mas, request, gfp); mas->mas_flags |=MA_STATE_PREALLOC ;if (likely (!mas_is_err (mas)))return 0 ;
Signature · 1 line
2.39.2
PATCH v3 14/15 maple_tree: Reduce resets during store setup
In reply to Liam R. Howlettmas_prealloc() may walk partially down the tree before finding that a split or spanning store is needed. When the write occurs, relax the logic on resetting the walk so that partial walks will not restart, but walks that have gone too far (a store that affects beyond the current node) should be restarted.
--- lib/maple_tree.c | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-)
lib/maple_tree.c +26 -11
@@ -5424,19 +5424,34 @@ static inline void mte_destroy_walk(struct maple_enode *enode,static void mas_wr_store_setup (struct ma_wr_state *wr_mas) { +if (mas_is_start (wr_mas->mas)) +return ; +if (unlikely (mas_is_paused (wr_mas->mas))) -mas_reset (wr_mas->mas); + goto reset; -if (!mas_is_start (wr_mas->mas)) { -if (mas_is_none (wr_mas->mas)) { -mas_reset (wr_mas->mas); - }else { - wr_mas->r_max = wr_mas->mas->max; - wr_mas->type =mte_node_type (wr_mas->mas->node); -if (mas_is_span_wr (wr_mas)) -mas_reset (wr_mas->mas); - } - } +if (unlikely (mas_is_none (wr_mas->mas))) + goto reset; + +/* +* A less strict version of mas_is_span_wr() where we allow spanning +* writes within this node. This is to stop partial walks in +* mas_prealloc() from being reset. +*/ +if (wr_mas->mas->last > wr_mas->mas->max) + goto reset; + +if (wr_mas->entry) +return ; + +if (mte_is_leaf (wr_mas->mas->node) && + wr_mas->mas->last == wr_mas->mas->max) + goto reset; + +return ; + +reset: +mas_reset (wr_mas->mas); }/* Interface */
Signature · 1 line
2.39.2
PATCH v3 15/15 mm/mmap: Change vma iteration order in do_vmi_align_munmap()
In reply to Liam R. HowlettBy delaying the setting of prev/next VMA until after the write of NULL, the probability of the prev/next VMA already being in the CPU cache is significantly increased, especially for larger munmap operations. It also means that prev/next will be loaded closer to when they are used.
This requires changing the loop type when gathering the VMAs that will be freed.
Since prev will be set later in the function, it is better to reverse the splitting direction of the start VMA (modify the new_below argument to __split_vma).
Using the vma_iter_prev_range() to walk back to the correct location in the tree will, on the most part, mean walking within the CPU cache. Usually, this is two steps vs a node reset and a tree re-walk.
--- mm/mmap.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-)
mm/mmap.c +13 -14
@@ -2451,20 +2451,17 @@ do_vmi_align_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma,if (end < vma->vm_end && mm->map_count >= sysctl_max_map_count) goto map_count_exceeded; - error =__split_vma (vmi, vma, start,0 ); + error =__split_vma (vmi, vma, start,1 );if (error) goto start_split_failed; - - vma =vma_iter_load (vmi); } - prev =vma_prev (vmi); -/* * Detach a range of VMAs from the mm. Using next as a temp variable as * it is always overwritten. */ -for_each_vma_range (*vmi, next, end) { + next = vma; + do { /* Does it split the end? */ if (next->vm_end > end) { error = __split_vma(vmi, next, end, 0);
@@ -2500,13 +2497,7 @@ do_vmi_align_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma,BUG_ON (next->vm_start < start);BUG_ON (next->vm_start > end);#endif - } - -if (vma_iter_end (vmi) > end) - next =vma_iter_load (vmi); - -if (!next) - next =vma_next (vmi); + }for_each_vma_range (*vmi, next, end);#if defined(CONFIG_DEBUG_VM_MAPLE_TREE )/* Make sure no VMAs are about to be lost. */
@@ -2527,7 +2518,10 @@ do_vmi_align_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma, BUG_ON(count != test_count); }#endif -vma_iter_set (vmi, start); + +while (vma_iter_addr (vmi) > start) +vma_iter_prev_range (vmi); + error =vma_iter_clear_gfp (vmi, start, end,GFP_KERNEL );if (error) goto clear_tree_failed;
@@ -2538,6 +2532,11 @@ do_vmi_align_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma,if (unlock)mmap_write_downgrade (mm); + prev =vma_iter_prev_range (vmi); + next =vma_next (vmi); +if (next) +vma_iter_prev_range (vmi); +/* * We can free page tables without write-locking mmap_lock because VMAs * were isolated before we downgraded mmap_lock.
Signature · 1 line
2.39.2
Jann Horn
reviewerRe: [PATCH v3 15/15] mm/mmap: Change vma iteration order in do_vmi_align_munmap()
@akpm
Quoted 3 lines · Liam R. Howlett
Since prev will be set later in the function, it is better to reverse the splitting direction of the start VMA (modify the
new_belowargument to__split_vma).
It might be a good idea to reorder "mm: always lock new vma before inserting into vma tree" before this patch.
If you apply this patch without "mm: always lock new vma before inserting into vma tree", I think move_vma(), when called with a start address in the middle of a VMA, will behave like this:
- vma_start_write() [lock the VMA to be moved]
- move_page_tables() [moves page table entries]
- do_vmi_munmap()
- do_vmi_align_munmap()
- __split_vma()
- creates a new VMA **covering the moved range** that is **not locked**
- stores the new VMA in the VMA tree **without locking it** [1]
- new VMA is locked and removed again [2]
[...]
So after the page tables in the region have already been moved, I believe there will be a brief window (between [1] and [2]) where page faults in the region can happen again, which could probably cause new page tables and PTEs to be created in the region again in that window. (This can't happen in Linus' current tree because the new VMA created by __split_vma() only covers the range that is not being moved.)
Though I guess that's not going to lead to anything bad, since do_vmi_munmap() anyway cleans up PTEs and page tables in the region? So maybe it's not that important.
Andrew Morton
maintainerRe: [PATCH v3 15/15] mm/mmap: Change vma iteration order in do_vmi_align_munmap()
Quoted 31 lines · On Mon, 14 Aug 2023 17:43:39 +0200 Jann Horn <[email protected]> wrote:
@akpm
Quoted 3 lines · Liam R. Howlett
Since prev will be set later in the function, it is better to reverse the splitting direction of the start VMA (modify the
new_belowargument to__split_vma).It might be a good idea to reorder "mm: always lock new vma before inserting into vma tree" before this patch.
If you apply this patch without "mm: always lock new vma before inserting into vma tree", I think
move_vma(), when called with a start address in the middle of a VMA, will behave like this:-
vma_start_write()[lock the VMA to be moved]
-move_page_tables()[moves page table entries]
-do_vmi_munmap()
-do_vmi_align_munmap()
-__split_vma()
- creates a new VMA **covering the moved range** that is **not locked**
- stores the new VMA in the VMA tree **without locking it** [1]
- new VMA is locked and removed again [2]
[...]So after the page tables in the region have already been moved, I believe there will be a brief window (between [1] and [2]) where page faults in the region can happen again, which could probably cause new page tables and PTEs to be created in the region again in that window. (This can't happen in Linus' current tree because the new VMA created by
__split_vma()only covers the range that is not being moved.)
Quoted 3 lines
Though I guess that's not going to lead to anything bad, since
do_vmi_munmap()anyway cleans up PTEs and page tables in the region? So maybe it's not that important.
Thanks. I'd of course prefer not to rebuild mm-stable. If this ends up being a hard-to-hit issue during git-bisect searches, I think we can live with that.
Re: [PATCH v3 15/15] mm/mmap: Change vma iteration order in do_vmi_align_munmap()
In reply to Jann HornQuoted 24 lines · Jann Horn
@akpm
Quoted 3 lines · Liam R. Howlett
Since prev will be set later in the function, it is better to reverse the splitting direction of the start VMA (modify the
new_belowargument to__split_vma).It might be a good idea to reorder "mm: always lock new vma before inserting into vma tree" before this patch.
If you apply this patch without "mm: always lock new vma before inserting into vma tree", I think
move_vma(), when called with a start address in the middle of a VMA, will behave like this:-
vma_start_write()[lock the VMA to be moved]
-move_page_tables()[moves page table entries]
-do_vmi_munmap()
-do_vmi_align_munmap()
-__split_vma()
- creates a new VMA **covering the moved range** that is **not locked**
- stores the new VMA in the VMA tree **without locking it** [1]
- new VMA is locked and removed again [2]
[...]
Quoted 6 lines
So after the page tables in the region have already been moved, I believe there will be a brief window (between [1] and [2]) where page faults in the region can happen again, which could probably cause new page tables and PTEs to be created in the region again in that window. (This can't happen in Linus' current tree because the new VMA created by
__split_vma()only covers the range that is not being moved.)
Ah, so my reversing of which VMA to keep to the first split call opens a window where the VMA being removed is not locked. Good catch.
Quoted 4 lines
Though I guess that's not going to lead to anything bad, since
do_vmi_munmap()anyway cleans up PTEs and page tables in the region? So maybe it's not that important.
do_vmi_munmap() will clean up PTEs from the end of the previous VMA to the start of the next (even over areas without VMA coverages - because there are platforms what need this, apparently).
I don't have any objections in the ordering or see an issue resulting from having it this way... Except for maybe lockdep, so maybe we should change the ordering of the patch sets just to be safe?
In fact, should we add another check somewhere to ensure we do generate the warning? Perhaps to remove_mt() to avoid the exit path hitting it?
Thanks,
Liam
Jann Horn
reviewerRe: [PATCH v3 15/15] mm/mmap: Change vma iteration order in do_vmi_align_munmap()
Quoted 36 lines · Liam R. Howlett
Quoted 24 lines · Jann Horn
@akpm
Quoted 3 lines · Liam R. Howlett
Since prev will be set later in the function, it is better to reverse the splitting direction of the start VMA (modify the
new_belowargument to__split_vma).It might be a good idea to reorder "mm: always lock new vma before inserting into vma tree" before this patch.
If you apply this patch without "mm: always lock new vma before inserting into vma tree", I think
move_vma(), when called with a start address in the middle of a VMA, will behave like this:-
vma_start_write()[lock the VMA to be moved]
-move_page_tables()[moves page table entries]
-do_vmi_munmap()
-do_vmi_align_munmap()
-__split_vma()
- creates a new VMA **covering the moved range** that is **not locked**
- stores the new VMA in the VMA tree **without locking it** [1]
- new VMA is locked and removed again [2]
[...]Quoted 6 lines
So after the page tables in the region have already been moved, I believe there will be a brief window (between [1] and [2]) where page faults in the region can happen again, which could probably cause new page tables and PTEs to be created in the region again in that window. (This can't happen in Linus' current tree because the new VMA created by
__split_vma()only covers the range that is not being moved.)Ah, so my reversing of which VMA to keep to the first split call opens a window where the VMA being removed is not locked. Good catch.
Quoted 1 line
Quoted 6 lines
Quoted 3 lines
Though I guess that's not going to lead to anything bad, since
do_vmi_munmap()anyway cleans up PTEs and page tables in the region? So maybe it's not that important.
do_vmi_munmap()will clean up PTEs from the end of the previous VMA to the start of the next
Alright, I guess no action is needed here then.
Quoted 6 lines
I don't have any objections in the ordering or see an issue resulting from having it this way... Except for maybe lockdep, so maybe we should change the ordering of the patch sets just to be safe?
In fact, should we add another check somewhere to ensure we do generate the warning? Perhaps to
remove_mt()to avoid the exit path hitting it?
I'm not sure which lockdep check you mean. do_vmi_align_munmap() is going to lock the VMAs again before it operates on them; I guess the only checks that would catch this would be the page table validation logic or the RSS counter checks on exit?
Re: [PATCH v3 15/15] mm/mmap: Change vma iteration order in do_vmi_align_munmap()
Quoted 34 lines · Jann Horn
Quoted 32 lines · Liam R. Howlett
Quoted 30 lines · Jann Horn
@akpm
Quoted 3 lines · Liam R. Howlett
Since prev will be set later in the function, it is better to reverse the splitting direction of the start VMA (modify the
new_belowargument to__split_vma).It might be a good idea to reorder "mm: always lock new vma before inserting into vma tree" before this patch.
If you apply this patch without "mm: always lock new vma before inserting into vma tree", I think
move_vma(), when called with a start address in the middle of a VMA, will behave like this:-
vma_start_write()[lock the VMA to be moved]
-move_page_tables()[moves page table entries]
-do_vmi_munmap()
-do_vmi_align_munmap()
-__split_vma()
- creates a new VMA **covering the moved range** that is **not locked**
- stores the new VMA in the VMA tree **without locking it** [1]
- new VMA is locked and removed again [2]
[...]So after the page tables in the region have already been moved, I believe there will be a brief window (between [1] and [2]) where page faults in the region can happen again, which could probably cause new page tables and PTEs to be created in the region again in that window. (This can't happen in Linus' current tree because the new VMA created by
__split_vma()only covers the range that is not being moved.)
Quoted 2 lines
Quoted 2 lines
Ah, so my reversing of which VMA to keep to the first split call opens a window where the VMA being removed is not locked. Good catch.
Looking at this again, I think it exists in Linus' tree and my change actually removes this window:
- error = __split_vma(vmi, vma, start, 0);
+ error = __split_vma(vmi, vma, start, 1);
if (error)
goto start_split_failed;The last argument is "new_below", which means the new VMA will be at the lower address. I don't love the argument of int or the name, also the documentation is lacking for the split function.
So, once we split at "start", vm_end = "start" in the new VMA while start will be in the old VMA. I then lock the old vma to be removed (again) and add it to the detached maple tree.
Before my patch, we split the VMA and took the new unlocked VMA for removal.. until I locked the new vma to be removed and add it to the detached maple tree. So there is a window that we write the new split VMA into the tree prior to locking the VMA, but it is locked before removal.
This change actually aligns the splitting with the other callers who use the split_vma() wrapper.
Quoted 10 lines
Quoted 8 lines
Quoted 5 lines
Though I guess that's not going to lead to anything bad, since
do_vmi_munmap()anyway cleans up PTEs and page tables in the region? So maybe it's not that important.
do_vmi_munmap()will clean up PTEs from the end of the previous VMA to the start of the nextAlright, I guess no action is needed here then.
I don't see a difference between this and the race that exists after the page fault ends and a task unmaps the area prior to the first task using the faulted areas?
Quoted 5 lines
Quoted 5 lines
I don't have any objections in the ordering or see an issue resulting from having it this way... Except for maybe lockdep, so maybe we should change the ordering of the patch sets just to be safe?
Quoted 8 lines
Quoted 2 lines
In fact, should we add another check somewhere to ensure we do generate the warning? Perhaps to
remove_mt()to avoid the exit path hitting it?I'm not sure which lockdep check you mean.
do_vmi_align_munmap()is going to lock the VMAs again before it operates on them; I guess the only checks that would catch this would be the page table validation logic or the RSS counter checks on exit?
I'm trying to add a lockdep to detect this potential window in the future, but it won't work as you pointed out since it will be locked before removal. I'm not sure it's worth it since Suren added more lockdep checks in his series.
I appreciate you really looking at these changes and thinking them through.
Regards,
Liam
Jann Horn
reviewerRe: [PATCH v3 15/15] mm/mmap: Change vma iteration order in do_vmi_align_munmap()
Quoted 55 lines · Liam R. Howlett
Quoted 34 lines · Jann Horn
Quoted 32 lines · Liam R. Howlett
Quoted 30 lines · Jann Horn
@akpm
Quoted 3 lines · Liam R. Howlett
Since prev will be set later in the function, it is better to reverse the splitting direction of the start VMA (modify the
new_belowargument to__split_vma).It might be a good idea to reorder "mm: always lock new vma before inserting into vma tree" before this patch.
If you apply this patch without "mm: always lock new vma before inserting into vma tree", I think
move_vma(), when called with a start address in the middle of a VMA, will behave like this:-
vma_start_write()[lock the VMA to be moved]
-move_page_tables()[moves page table entries]
-do_vmi_munmap()
-do_vmi_align_munmap()
-__split_vma()
- creates a new VMA **covering the moved range** that is **not locked**
- stores the new VMA in the VMA tree **without locking it** [1]
- new VMA is locked and removed again [2]
[...]So after the page tables in the region have already been moved, I believe there will be a brief window (between [1] and [2]) where page faults in the region can happen again, which could probably cause new page tables and PTEs to be created in the region again in that window. (This can't happen in Linus' current tree because the new VMA created by
__split_vma()only covers the range that is not being moved.)Quoted 2 lines
Quoted 2 lines
Ah, so my reversing of which VMA to keep to the first split call opens a window where the VMA being removed is not locked. Good catch.
Looking at this again, I think it exists in Linus' tree and my change actually removes this window:
mm/mmap.c +1
-1- error =__split_vma (vmi, vma, start,0 ); + error =__split_vma (vmi, vma, start,1 );if (error) goto start_split_failed;The last argument is "
new_below", which means the new VMA will be at the lower address. I don't love the argument of int or the name, also the documentation is lacking for the split function.So, once we split at "start",
vm_end= "start" in the new VMA while start will be in the old VMA. I then lock the old vma to be removed (again) and add it to the detached maple tree.
Quoted 8 lines
Before my patch, we split the VMA and took the new unlocked VMA for removal.. until I locked the new vma to be removed and add it to the detached maple tree. So there is a window that we write the new split VMA into the tree prior to locking the VMA, but it is locked before removal.
This change actually aligns the splitting with the other callers who use the
split_vma()wrapper.
Oooh, you're right. Sorry, I misread that.
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Though I guess that's not going to lead to anything bad, since
do_vmi_munmap()anyway cleans up PTEs and page tables in the region? So maybe it's not that important.
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
do_vmi_munmap()will clean up PTEs from the end of the previous VMA to the start of the nextAlright, I guess no action is needed here then.
I don't see a difference between this and the race that exists after the page fault ends and a task unmaps the area prior to the first task using the faulted areas?
Yeah, you're right. I was thinking about it in terms of "this is a weird situation and it would be dangerous if something relied on there not being any non-empty PTEs in the range", but there's nothing that relies on it, so that's fine.