mm: split the file's i_mmap tree for NUMA
Subsystems (19)
v2 cover mm: split the file's i_mmap tree for NUMA
In NUMA, there are maybe many NUMA nodes and many CPUs.
For example, a Hygon's server has 12 NUMA nodes, and 384 CPUs. In the UnixBench tests, there is a test "execl" which tests the execve system call.
When we test our server with "./Run -c 384 execl",
the test result is not good enough. The i_mmap locks contended heavily on "libc.so" and "ld.so". For example, the i_mmap tree for "libc.so" can have over 6000 VMAs, all the VMAs can be in different NUMA mode. The insert/remove operations do not run quickly enough.
patch 1 & patch 2 are try to hide the direct access of i_mmap. patch 3 splits the i_mmap into sibling trees, each tree has separate lock, and we can get better performance with this patch set in our NUMA server:
we can get over 400% performance improvement.
I did not test the non-NUMA case, since I do not have such server.
v1 --> v2:
Not only split the immap tree, but also split the lock. v1 : https://lkml.org/lkml/2026/4/13/199
Huang Shijie (4):
mm: use mapping_mapped to simplify the code
mm: use get_i_mmap_root to access the file's i_mmap mm/fs: split the file's i_mmap tree docs/mm: update document for split i_mmap tree
Documentation/mm/process_addrs.rst | 63 +++++++--- arch/arm/mm/fault-armv.c | 3 +- arch/arm/mm/flush.c | 3 +- arch/nios2/mm/cacheflush.c | 3 +- arch/parisc/kernel/cache.c | 4 +- fs/Kconfig | 8 ++ fs/dax.c | 3 +- fs/hugetlbfs/inode.c | 30 +++-- fs/inode.c | 75 +++++++++++- include/linux/fs.h | 179 ++++++++++++++++++++++++++++- include/linux/mm.h | 81 +++++++++++++ include/linux/mm_types.h | 3 + kernel/events/uprobes.c | 3 +- mm/hugetlb.c | 7 +- mm/internal.h | 3 +- mm/khugepaged.c | 6 +- mm/memory-failure.c | 8 +- mm/memory.c | 8 +- mm/mmap.c | 11 +- mm/nommu.c | 28 +++-- mm/pagewalk.c | 4 +- mm/rmap.c | 2 +- mm/vma.c | 74 +++++++++--- mm/vma_init.c | 3 + 24 files changed, 534 insertions(+), 78 deletions(-)
Signature · 1 line
2.53.0
PATCH v2 1/4 mm: use mapping_mapped to simplify the code
Use mapping_mapped() to simplify the code, make
the code tidy and clean.
--- fs/hugetlbfs/inode.c | 4 ++-- mm/memory.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)
fs/hugetlbfs/inode.c +2 -2
@@ -614,7 +614,7 @@ static void hugetlb_vmtruncate(struct inode *inode, loff_t offset)i_size_write (inode, offset);i_mmap_lock_write (mapping); -if (!RB_EMPTY_ROOT (&mapping->i_mmap.rb_root)) +if (mapping_mapped (mapping))hugetlb_vmdelete_list (&mapping->i_mmap, pgoff,0 ,ZAP_FLAG_DROP_MARKER );i_mmap_unlock_write (mapping);
@@ -675,7 +675,7 @@ static long hugetlbfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)/* Unmap users of full pages in the hole. */ if (hole_end > hole_start) { -if (!RB_EMPTY_ROOT (&mapping->i_mmap.rb_root)) +if (mapping_mapped (mapping))hugetlb_vmdelete_list (&mapping->i_mmap, hole_start >>PAGE_SHIFT , hole_end >>PAGE_SHIFT ,0 );
mm/memory.c +2 -2
@@ -4386,7 +4386,7 @@ void unmap_mapping_folio(struct folio *folio) details.zap_flags =ZAP_FLAG_DROP_MARKER ;i_mmap_lock_read (mapping); -if (unlikely (!RB_EMPTY_ROOT (&mapping->i_mmap.rb_root))) +if (unlikely (mapping_mapped (mapping)))unmap_mapping_range_tree (&mapping->i_mmap, first_index, last_index, &details);i_mmap_unlock_read (mapping);
@@ -4416,7 +4416,7 @@ void unmap_mapping_pages(struct address_space *mapping, pgoff_t start, last_index =ULONG_MAX ;i_mmap_lock_read (mapping); -if (unlikely (!RB_EMPTY_ROOT (&mapping->i_mmap.rb_root))) +if (unlikely (mapping_mapped (mapping)))unmap_mapping_range_tree (&mapping->i_mmap, first_index, last_index, &details);i_mmap_unlock_read (mapping);
Signature · 1 line
2.53.0
Re: [PATCH v2 1/4] mm: use mapping_mapped to simplify the code
Quoted 4 lines · Huang Shijie
Use
mapping_mapped()to simplify the code, make
the code tidy and clean.Signed-off-by Huang Shijie <[email protected]>
LGTM, thanks! Super uncontroversial so perhaps
could be picked up separately.
Signature · 1 line
Pedro
Re: [PATCH v2 1/4] mm: use mapping_mapped to simplify the code
In reply to Huang ShijieQuoted 4 lines · Huang Shijie
Use
mapping_mapped()to simplify the code, make
the code tidy and clean.Signed-off-by Huang Shijie <[email protected]>
Yeah as Pedro said this one could just be sent separately, and I in fact suggest you do that :) So:
Cheers, Lorenzo
Quoted 53 lines
--- fs/hugetlbfs/inode.c | 4 ++-- mm/memory.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)fs/hugetlbfs/inode.c +2
-2@@ -614,7 +614,7 @@ static void hugetlb_vmtruncate(struct inode *inode, loff_t offset)i_size_write (inode, offset);i_mmap_lock_write (mapping); -if (!RB_EMPTY_ROOT (&mapping->i_mmap.rb_root)) +if (mapping_mapped (mapping))hugetlb_vmdelete_list (&mapping->i_mmap, pgoff,0 ,ZAP_FLAG_DROP_MARKER );i_mmap_unlock_write (mapping);@@ -675,7 +675,7 @@ static long hugetlbfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)/* Unmap users of full pages in the hole. */ if (hole_end > hole_start) { -if (!RB_EMPTY_ROOT (&mapping->i_mmap.rb_root)) +if (mapping_mapped (mapping))hugetlb_vmdelete_list (&mapping->i_mmap, hole_start >>PAGE_SHIFT , hole_end >>PAGE_SHIFT ,0 );mm/memory.c +2
-3@@ -4386,7 +4386,7 @@ void unmap_mapping_folio(struct folio *folio) details.zap_flags =ZAP_FLAG_DROP_MARKER ;i_mmap_lock_read (mapping); -if (unlikely (!RB_EMPTY_ROOT (&mapping->i_mmap.rb_root))) +if (unlikely (mapping_mapped (mapping)))unmap_mapping_range_tree (&mapping->i_mmap, first_index, last_index, &details);i_mmap_unlock_read (mapping);@@ -4416,7 +4416,7 @@ void unmap_mapping_pages(struct address_space *mapping, pgoff_t start, last_index =ULONG_MAX ;i_mmap_lock_read (mapping); -if (unlikely (!RB_EMPTY_ROOT (&mapping->i_mmap.rb_root))) +if (unlikely (mapping_mapped (mapping)))unmap_mapping_range_tree (&mapping->i_mmap, first_index, last_index, &details);i_mmap_unlock_read (mapping); --2.53.0
Re: [PATCH v2 1/4] mm: use mapping_mapped to simplify the code
Hi Lorenzo & Pedro,
Quoted 9 lines · Lorenzo Stoakes
Quoted 4 lines · Huang Shijie
Use
mapping_mapped()to simplify the code, make
the code tidy and clean.Signed-off-by Huang Shijie <[email protected]>Yeah as Pedro said this one could just be sent separately, and I in fact suggest you do that :) So:
Thank you Pedro and Lorenzo.
I can send a separate patch later.
Thanks
Huang Shijie
PATCH v2 2/4 mm: use get_i_mmap_root to access the file's i_mmap
In reply to Huang ShijieDo not access the file's i_mmap directly, use get_i_mmap_root() to access it. This patch makes preparations for later patch.
--- arch/arm/mm/fault-armv.c | 3 ++- arch/arm/mm/flush.c | 3 ++- arch/nios2/mm/cacheflush.c | 3 ++- arch/parisc/kernel/cache.c | 4 +++- fs/dax.c | 3 ++- fs/hugetlbfs/inode.c | 6 +++--- include/linux/fs.h | 5 +++++ include/linux/mm.h | 1 + kernel/events/uprobes.c | 3 ++- mm/hugetlb.c | 7 +++++-- mm/khugepaged.c | 6 ++++-- mm/memory-failure.c | 8 +++++--- mm/memory.c | 4 ++-- mm/mmap.c | 2 +- mm/nommu.c | 9 +++++---- mm/pagewalk.c | 2 +- mm/rmap.c | 2 +- mm/vma.c | 14 ++++++++------ 18 files changed, 54 insertions(+), 31 deletions(-)
arch/arm/mm/fault-armv.c +2 -1
@@ -126,6 +126,7 @@ make_coherent(struct address_space *mapping, struct vm_area_struct *vma, {const unsigned long pmd_start_addr =ALIGN_DOWN (addr,PMD_SIZE );const unsigned long pmd_end_addr = pmd_start_addr +PMD_SIZE ; +struct rb_root_cached *root =get_i_mmap_root (mapping);struct mm_struct *mm = vma->vm_mm;struct vm_area_struct *mpnt;unsigned long offset;
@@ -140,7 +141,7 @@ make_coherent(struct address_space *mapping, struct vm_area_struct *vma,* cache coherency. */ flush_dcache_mmap_lock (mapping); - vma_interval_tree_foreach(mpnt, &mapping->i_mmap, pgoff, pgoff) { + vma_interval_tree_foreach(mpnt, root, pgoff, pgoff) {/* * If we are using split PTE locks, then we need to take the pte * lock. Otherwise we are using shared mm->page_table_lock which
arch/arm/mm/flush.c +2 -1
@@ -238,6 +238,7 @@ void __flush_dcache_folio(struct address_space *mapping, struct folio *folio)static void __flush_dcache_aliases (struct address_space *mapping,struct folio *folio) {struct mm_struct *mm = current->active_mm; +struct rb_root_cached *root =get_i_mmap_root (mapping);struct vm_area_struct *vma;pgoff_t pgoff, pgoff_end;
@@ -251,7 +252,7 @@ static void __flush_dcache_aliases(struct address_space *mapping, struct folio * pgoff_end = pgoff +folio_nr_pages (folio) -1 ;flush_dcache_mmap_lock (mapping); - vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff_end) { + vma_interval_tree_foreach(vma, root, pgoff, pgoff_end) {unsigned long start, offset, pfn;unsigned int nr;
arch/nios2/mm/cacheflush.c +2 -1
@@ -78,11 +78,12 @@ static void flush_aliases(struct address_space *mapping, struct folio *folio)unsigned long flags;pgoff_t pgoff;unsigned long nr =folio_nr_pages (folio); +struct rb_root_cached *root =get_i_mmap_root (mapping); pgoff = folio->index;flush_dcache_mmap_lock_irqsave (mapping, flags); - vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff + nr - 1) { + vma_interval_tree_foreach(vma, root, pgoff, pgoff + nr - 1) {unsigned long start;if (vma->vm_mm != mm)
arch/parisc/kernel/cache.c +3 -1
@@ -473,6 +473,7 @@ static inline unsigned long get_upa(struct mm_struct *mm, unsigned long addr)void flush_dcache_folio (struct folio *folio) {struct address_space *mapping =folio_flush_mapping (folio); +struct rb_root_cached *root;struct vm_area_struct *vma;unsigned long addr, old_addr =0 ;void *kaddr;
@@ -494,6 +495,7 @@ void flush_dcache_folio(struct folio *folio)return ; pgoff = folio->index; + root =get_i_mmap_root (mapping);/* * We have carefully arranged in arch_get_unmapped_area() that
@@ -503,7 +505,7 @@ void flush_dcache_folio(struct folio *folio)* on machines that support equivalent aliasing */ flush_dcache_mmap_lock_irqsave (mapping, flags); - vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff + nr - 1) { + vma_interval_tree_foreach(vma, root, pgoff, pgoff + nr - 1) {unsigned long offset = pgoff - vma->vm_pgoff;unsigned long pfn =folio_pfn (folio);
fs/dax.c +2 -1
@@ -1138,6 +1138,7 @@ static int dax_writeback_one(struct xa_state *xas, struct dax_device *dax_dev, struct address_space *mapping, void *entry) { unsigned long pfn, index, count, end; +struct rb_root_cached *root =get_i_mmap_root (mapping);long ret =0 ;struct vm_area_struct *vma;
@@ -1201,7 +1202,7 @@ static int dax_writeback_one(struct xa_state *xas, struct dax_device *dax_dev,/* Walk all mappings of a given index of a file and writeprotect them */ i_mmap_lock_read (mapping); - vma_interval_tree_foreach(vma, &mapping->i_mmap, index, end) { + vma_interval_tree_foreach(vma, root, index, end) {pfn_mkclean_range (pfn, count, index, vma);cond_resched (); }
fs/hugetlbfs/inode.c +3 -3
@@ -380,7 +380,7 @@ static void hugetlb_unmap_file_folio(struct hstate *h,struct address_space *mapping, struct folio *folio, pgoff_t index) { - struct rb_root_cached *root = &mapping->i_mmap; + struct rb_root_cached *root = get_i_mmap_root(mapping);struct hugetlb_vma_lock *vma_lock;unsigned long pfn =folio_pfn (folio);struct vm_area_struct *vma;
@@ -615,7 +615,7 @@ static void hugetlb_vmtruncate(struct inode *inode, loff_t offset)i_size_write (inode, offset);i_mmap_lock_write (mapping);if (mapping_mapped (mapping)) -hugetlb_vmdelete_list (&mapping->i_mmap, pgoff,0 , +hugetlb_vmdelete_list (get_i_mmap_root (mapping), pgoff,0 ,ZAP_FLAG_DROP_MARKER );i_mmap_unlock_write (mapping);remove_inode_hugepages (inode, offset,LLONG_MAX );
@@ -676,7 +676,7 @@ static long hugetlbfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)/* Unmap users of full pages in the hole. */ if (hole_end > hole_start) {if (mapping_mapped (mapping)) -hugetlb_vmdelete_list (&mapping->i_mmap, +hugetlb_vmdelete_list (get_i_mmap_root (mapping), hole_start >>PAGE_SHIFT , hole_end >>PAGE_SHIFT ,0 ); }
include/linux/fs.h +5 -0
@@ -556,6 +556,11 @@ static inline int mapping_mapped(const struct address_space *mapping)return !RB_EMPTY_ROOT (&mapping->i_mmap.rb_root); } +static inline struct rb_root_cached *get_i_mmap_root (struct address_space *mapping) +{ +return &mapping->i_mmap; +} +/* * Might pages of this file have been modified in userspace? * Note that i_mmap_writable counts all VM_SHARED, VM_MAYWRITE vmas: do_mmap
include/linux/mm.h +1 -0
@@ -4041,6 +4041,7 @@ struct vm_area_struct *vma_interval_tree_iter_first(struct rb_root_cached *root,struct vm_area_struct *vma_interval_tree_iter_next (struct vm_area_struct *node,unsigned long start,unsigned long last); +/* Please use get_i_mmap_root() to get the @root */ #define vma_interval_tree_foreach (vma, root, start, last) \ for (vma = vma_interval_tree_iter_first(root, start, last); \ vma; vma = vma_interval_tree_iter_next(vma, start, last))
kernel/events/uprobes.c +2 -1
@@ -1201,6 +1201,7 @@ static inline struct map_info *free_map_info(struct map_info *info)static struct map_info *build_map_info (struct address_space *mapping,loff_t offset,bool is_register) { +struct rb_root_cached *root =get_i_mmap_root (mapping);unsigned long pgoff = offset >>PAGE_SHIFT ;struct vm_area_struct *vma;struct map_info *curr =NULL ;
@@ -1210,7 +1211,7 @@ build_map_info(struct address_space *mapping, loff_t offset, bool is_register) again:i_mmap_lock_read (mapping); - vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) { + vma_interval_tree_foreach(vma, root, pgoff, pgoff) {if (!valid_vma (vma, is_register))continue ;
mm/hugetlb.c +5 -2
@@ -5360,6 +5360,7 @@ static void unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma,struct hstate *h =hstate_vma (vma);struct vm_area_struct *iter_vma;struct address_space *mapping; +struct rb_root_cached *root;pgoff_t pgoff;/*
@@ -5370,6 +5371,7 @@ static void unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma, pgoff = ((address - vma->vm_start) >>PAGE_SHIFT ) + vma->vm_pgoff; mapping = vma->vm_file->f_mapping; + root =get_i_mmap_root (mapping);/* * Take the mapping lock for the duration of the table walk. As
@@ -5377,7 +5379,7 @@ static void unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma,* __unmap_hugepage_range() is called as the lock is already held */ i_mmap_lock_write (mapping); - vma_interval_tree_foreach(iter_vma, &mapping->i_mmap, pgoff, pgoff) { + vma_interval_tree_foreach(iter_vma, root, pgoff, pgoff) {/* Do not unmap the current VMA */ if (iter_vma == vma)continue ;
@@ -6850,6 +6852,7 @@ pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma, unsigned long addr, pud_t *pud) { struct address_space *mapping = vma->vm_file->f_mapping; +struct rb_root_cached *root =get_i_mmap_root (mapping);pgoff_t idx = ((addr - vma->vm_start) >>PAGE_SHIFT ) + vma->vm_pgoff;struct vm_area_struct *svma;
@@ -6858,7 +6861,7 @@ pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma,pte_t *pte;i_mmap_lock_read (mapping); - vma_interval_tree_foreach(svma, &mapping->i_mmap, idx, idx) { + vma_interval_tree_foreach(svma, root, idx, idx) {if (svma == vma)continue ;
mm/khugepaged.c +4 -2
@@ -1773,10 +1773,11 @@ static bool file_backed_vma_is_retractable(struct vm_area_struct *vma)static void retract_page_tables (struct address_space *mapping,pgoff_t pgoff) { +struct rb_root_cached *root =get_i_mmap_root (mapping);struct vm_area_struct *vma;i_mmap_lock_read (mapping); - vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) { + vma_interval_tree_foreach(vma, root, pgoff, pgoff) {struct mmu_notifier_range range;struct mm_struct *mm;unsigned long addr;
@@ -2194,7 +2195,8 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,* not be able to observe any missing pages due to the * previously inserted retry entries. */ - vma_interval_tree_foreach(vma, &mapping->i_mmap, start, end) { +vma_interval_tree_foreach (vma,get_i_mmap_root (mapping), + start, end) {if (userfaultfd_missing (vma)) { result =SCAN_EXCEED_NONE_PTE ; goto immap_locked;
mm/memory-failure.c +5 -3
@@ -598,7 +598,7 @@ static void collect_procs_file(const struct folio *folio,if (!t)continue ; -vma_interval_tree_foreach (vma, &mapping->i_mmap, pgoff, +vma_interval_tree_foreach (vma,get_i_mmap_root (mapping), pgoff, pgoff) {/* * Send early kill signal to tasks where a vma covers
@@ -650,7 +650,8 @@ static void collect_procs_fsdax(const struct page *page, t =task_early_kill (tsk, true);if (!t)continue ; - vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) { +vma_interval_tree_foreach (vma,get_i_mmap_root (mapping), pgoff, + pgoff) {if (vma->vm_mm == t->mm)add_to_kill_fsdax (t, page, vma, to_kill, pgoff); }
@@ -2251,7 +2252,8 @@ static void collect_procs_pfn(struct pfn_address_space *pfn_space, t =task_early_kill (tsk, true);if (!t)continue ; - vma_interval_tree_foreach(vma, &mapping->i_mmap, 0, ULONG_MAX) { +vma_interval_tree_foreach (vma,get_i_mmap_root (mapping), + 0, ULONG_MAX) {pgoff_t pgoff; if (vma->vm_mm == t->mm &&
mm/memory.c +2 -2
@@ -4387,7 +4387,7 @@ void unmap_mapping_folio(struct folio *folio)i_mmap_lock_read (mapping);if (unlikely (mapping_mapped (mapping))) -unmap_mapping_range_tree (&mapping->i_mmap, first_index, +unmap_mapping_range_tree (get_i_mmap_root (mapping), first_index, last_index, &details);i_mmap_unlock_read (mapping); }
@@ -4417,7 +4417,7 @@ void unmap_mapping_pages(struct address_space *mapping, pgoff_t start,i_mmap_lock_read (mapping);if (unlikely (mapping_mapped (mapping))) -unmap_mapping_range_tree (&mapping->i_mmap, first_index, +unmap_mapping_range_tree (get_i_mmap_root (mapping), first_index, last_index, &details);i_mmap_unlock_read (mapping); }
mm/mmap.c +1 -1
@@ -1831,7 +1831,7 @@ __latent_entropy int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)flush_dcache_mmap_lock (mapping);/* insert tmp into the share list, just after mpnt */ vma_interval_tree_insert_after (tmp, mpnt, - &mapping->i_mmap); +get_i_mmap_root (mapping));flush_dcache_mmap_unlock (mapping);i_mmap_unlock_write (mapping); }
mm/nommu.c +5 -4
@@ -569,7 +569,7 @@ static void setup_vma_to_mm(struct vm_area_struct *vma, struct mm_struct *mm)i_mmap_lock_write (mapping);flush_dcache_mmap_lock (mapping); -vma_interval_tree_insert (vma, &mapping->i_mmap); +vma_interval_tree_insert (vma,get_i_mmap_root (mapping));flush_dcache_mmap_unlock (mapping);i_mmap_unlock_write (mapping); }
@@ -585,7 +585,7 @@ static void cleanup_vma_from_mm(struct vm_area_struct *vma)i_mmap_lock_write (mapping);flush_dcache_mmap_lock (mapping); -vma_interval_tree_remove (vma, &mapping->i_mmap); +vma_interval_tree_remove (vma,get_i_mmap_root (mapping));flush_dcache_mmap_unlock (mapping);i_mmap_unlock_write (mapping); }
@@ -1804,6 +1804,7 @@ EXPORT_SYMBOL_GPL(copy_remote_vm_str);int nommu_shrink_inode_mappings (struct inode *inode,size_t size,size_t newsize) { +struct rb_root_cached *root =get_i_mmap_root (&inode->i_mapping);struct vm_area_struct *vma;struct vm_region *region;pgoff_t low, high;
@@ -1816,7 +1817,7 @@ int nommu_shrink_inode_mappings(struct inode *inode, size_t size,i_mmap_lock_read (inode->i_mapping);/* search for VMAs that fall within the dead zone */ - vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, low, high) { + vma_interval_tree_foreach(vma, root, low, high) {/* found one - only interested if it's shared out of the page * cache */ if (vma->vm_flags &VM_SHARED ) {
@@ -1832,7 +1833,7 @@ int nommu_shrink_inode_mappings(struct inode *inode, size_t size,* we don't check for any regions that start beyond the EOF as there * shouldn't be any */ - vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, 0, ULONG_MAX) { + vma_interval_tree_foreach(vma, root, 0, ULONG_MAX) {if (!(vma->vm_flags &VM_SHARED ))continue ;
mm/pagewalk.c +1 -1
@@ -810,7 +810,7 @@ int walk_page_mapping(struct address_space *mapping, pgoff_t first_index,return -EINVAL ;lockdep_assert_held (&mapping->i_mmap_rwsem); -vma_interval_tree_foreach (vma, &mapping->i_mmap, first_index, +vma_interval_tree_foreach (vma,get_i_mmap_root (mapping), first_index, first_index + nr - 1) {/* Clip to the vma */ vba = vma->vm_pgoff;
mm/rmap.c +1 -1
@@ -3051,7 +3051,7 @@ static void __rmap_walk_file(struct folio *folio, struct address_space *mapping,i_mmap_lock_read (mapping); } lookup: -vma_interval_tree_foreach (vma, &mapping->i_mmap, +vma_interval_tree_foreach (vma,get_i_mmap_root (mapping), pgoff_start, pgoff_end) {unsigned long address =vma_address (vma, pgoff_start, nr_pages);
mm/vma.c +8 -6
@@ -234,7 +234,7 @@ static void __vma_link_file(struct vm_area_struct *vma,mapping_allow_writable (mapping);flush_dcache_mmap_lock (mapping); -vma_interval_tree_insert (vma, &mapping->i_mmap); +vma_interval_tree_insert (vma,get_i_mmap_root (mapping));flush_dcache_mmap_unlock (mapping); }
@@ -248,7 +248,7 @@ static void __remove_shared_vm_struct(struct vm_area_struct *vma,mapping_unmap_writable (mapping);flush_dcache_mmap_lock (mapping); -vma_interval_tree_remove (vma, &mapping->i_mmap); +vma_interval_tree_remove (vma,get_i_mmap_root (mapping));flush_dcache_mmap_unlock (mapping); }
@@ -319,10 +319,11 @@ static void vma_prepare(struct vma_prepare *vp)if (vp->file) {flush_dcache_mmap_lock (vp->mapping); -vma_interval_tree_remove (vp->vma, &vp->mapping->i_mmap); +vma_interval_tree_remove (vp->vma, +get_i_mmap_root (vp->mapping));if (vp->adj_next)vma_interval_tree_remove (vp->adj_next, - &vp->mapping->i_mmap); +get_i_mmap_root (vp->mapping)); } }
@@ -341,8 +342,9 @@ static void vma_complete(struct vma_prepare *vp, struct vma_iterator *vmi,if (vp->file) {if (vp->adj_next)vma_interval_tree_insert (vp->adj_next, - &vp->mapping->i_mmap); -vma_interval_tree_insert (vp->vma, &vp->mapping->i_mmap); +get_i_mmap_root (vp->mapping)); +vma_interval_tree_insert (vp->vma, +get_i_mmap_root (vp->mapping));flush_dcache_mmap_unlock (vp->mapping); }
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v2 2/4] mm: use get_i_mmap_root to access the file's i_mmap
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] In `nommu_shrink_inode_mappings`, `get_i_mmap_root()` is incorrectly called with…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] In nommu_shrink_inode_mappings, get_i_mmap_root() is incorrectly called with &inode->i_mapping instead of inode->i_mapping, causing an invalid pointer calculation and subsequent memory corruption. --
commit af3e57c8cfc7072a4088af8cf0bf41f373c9261b
Author: Huang Shijie <[email protected]>
mm: use get_i_mmap_root to access the file's i_mmap
This patch introduces the get_i_mmap_root() helper to encapsulate direct accesses to the i_mmap interval tree root within struct address_space. It updates various architectures and core memory management subsystems to use the new helper.
Quoted 4 lines
mm/nommu.c +0
-0
[ ... ]
mm/nommu.c EXPORT_SYMBOL_GPL(copy_remote_vm_str); · line 1807
Quoted 5 lines
mm/nommu.c +1
-0@@ -1804,6 +1804,7 @@ EXPORT_SYMBOL_GPL(copy_remote_vm_str);int nommu_shrink_inode_mappings (struct inode *inode,size_t size,size_t newsize) { +struct rb_root_cached *root =get_i_mmap_root (&inode->i_mapping);
[Severity: High]
Does this code pass a double pointer to get_i_mmap_root()?
Since inode->i_mapping is already a pointer to struct address_space, passing &inode->i_mapping provides a struct address_space ** instead of the expected struct address_space *.
This will cause get_i_mmap_root() to calculate an invalid address by adding the structure offset to the address of the i_mapping pointer itself. The subsequent call to vma_interval_tree_foreach() will interpret garbage memory as an rb_root_cached structure, leading to a panic when it attempts to dereference the fake rb_node pointers. Should this be called with inode->i_mapping instead?
Quoted 3 lines
mm/nommu.c +0
-0struct vm_area_struct *vma;struct vm_region *region;pgoff_t low, high;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH v2 3/4 mm/fs: split the file's i_mmap tree
In reply to Huang ShijieIn the UnixBench tests, there is a test "execl" which tests
the execve system call.
For example, a Hygon's server has 12 NUMA nodes, and 384 CPUs.
When we test our server with "./Run -c 384 execl",
the test result is not good enough. The i_mmap locks contended heavily on "libc.so" and "ld.so". The i_mmap tree for "libc.so" can be
over 6000 VMAs, all the VMAs can be in different NUMA mode. The insert/remove operations do not run quickly enough.
In order to reduce the competition of the i_mmap lock, this patch does
following:
1.) Split the single i_mmap tree into several sibling trees:
Each tree has a lock. The CONFIG_SPLIT_I_MMAP is used to turn on/off this feature.
2.) Introduce a new field "tree_idx" for vm_area_struct to save the
sibling tree index for this VMA.
3.) Introduce a new field "vma_count" for address_space.
The new mapping_mapped() will use it.
4.) Rewrite the vma_interval_tree_foreach()
5.) Rewrite the lock functions.
After this patch, the VMA insert/remove operations will work faster,
and we can get over 400% performance improvement with the above test.
--- fs/Kconfig | 8 ++ fs/hugetlbfs/inode.c | 20 ++++- fs/inode.c | 75 ++++++++++++++++- include/linux/fs.h | 174 ++++++++++++++++++++++++++++++++++++++- include/linux/mm.h | 80 ++++++++++++++++++ include/linux/mm_types.h | 3 + mm/internal.h | 3 +- mm/mmap.c | 11 ++- mm/nommu.c | 23 ++++-- mm/pagewalk.c | 2 +- mm/vma.c | 72 +++++++++++----- mm/vma_init.c | 3 + 12 files changed, 436 insertions(+), 38 deletions(-)
fs/Kconfig +8 -0
@@ -9,6 +9,14 @@ menu "File systems" config DCACHE_WORD_ACCESS bool +config SPLIT_I_MMAP + bool "Split the file's i_mmap to several trees" + default n + help + Split the file's i_mmap to several trees, each tree has a separate + lock. This will reduce the lock contention of file's i_mmap tree, + but it will cost more memory for per inode. + config VALIDATE_FS_PARSER bool "Validate filesystem parameter description" help
fs/hugetlbfs/inode.c +18 -2
@@ -891,6 +891,23 @@ static struct inode *hugetlbfs_get_root(struct super_block *sb,*/ static struct lock_class_key hugetlbfs_i_mmap_rwsem_key; +#ifdef CONFIG_SPLIT_I_MMAP +static void hugetlbfs_lockdep_set_class (struct address_space *mapping) +{ +int i; + +for (i =0 ; i < split_tree_num; i++) { +lockdep_set_class (&mapping->i_mmap[i].rwsem, + &hugetlbfs_i_mmap_rwsem_key); + } +} +#else +static void hugetlbfs_lockdep_set_class (struct address_space *mapping) +{ +lockdep_set_class (&mapping->i_mmap_rwsem, &hugetlbfs_i_mmap_rwsem_key); +} +#endif + static struct inode *hugetlbfs_get_inode(struct super_block *sb, struct mnt_idmap *idmap, struct inode *dir,
@@ -915,8 +932,7 @@ static struct inode *hugetlbfs_get_inode(struct super_block *sb, inode->i_ino =get_next_ino ();inode_init_owner (idmap, inode, dir, mode); -lockdep_set_class (&inode->i_mapping->i_mmap_rwsem, - &hugetlbfs_i_mmap_rwsem_key); +hugetlbfs_lockdep_set_class (inode->i_mapping); inode->i_mapping->a_ops = &hugetlbfs_aops;simple_inode_init_ts (inode); info->resv_map = resv_map;
fs/inode.c +72 -3
108 lines not on this page · raw message
include/linux/fs.h +171 -3
211 lines not on this page · raw message
include/linux/mm.h +80 -0
91 lines not on this page · raw message
include/linux/mm_types.h +3 -0
@@ -1072,6 +1072,9 @@ struct vm_area_struct {#ifdef __HAVE_PFNMAP_TRACKINGstruct pfnmap_track_ctx *pfnmap_track_ctx;#endif +#ifdef CONFIG_SPLIT_I_MMAP +int tree_idx;/* The sibling tree index for the VMA */ +#endif } __randomize_layout;/* Clears all bits in the VMA flags bitmap, non-atomically. */
mm/internal.h +2 -1
@@ -1888,7 +1888,8 @@ static inline void maybe_rmap_unlock_action(struct vm_area_struct *vma,VM_WARN_ON_ONCE (vma_is_anonymous (vma)); file = vma->vm_file; -i_mmap_unlock_write (file->f_mapping); +i_mmap_tree_unlock_write (file->f_mapping, vma); +i_mmap_unlock_write_complete (file->f_mapping); action->hide_from_rmap_until_complete = false; }
mm/mmap.c +8 -3
@@ -1825,15 +1825,20 @@ __latent_entropy int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)struct address_space *mapping = file->f_mapping;get_file (file); -i_mmap_lock_write (mapping); +i_mmap_lock_write_prepare (mapping); +i_mmap_tree_lock_write (mapping, mpnt); +if (vma_is_shared_maywrite (tmp))mapping_allow_writable (mapping);flush_dcache_mmap_lock (mapping);/* insert tmp into the share list, just after mpnt */ vma_interval_tree_insert_after (tmp, mpnt, -get_i_mmap_root (mapping)); +get_rb_root (mpnt, mapping)); +inc_mapping_vma (mapping, tmp);flush_dcache_mmap_unlock (mapping); -i_mmap_unlock_write (mapping); + +i_mmap_tree_unlock_write (mapping, mpnt); +i_mmap_unlock_write_complete (mapping); }if (!(tmp->vm_flags &VM_WIPEONFORK ))
mm/nommu.c +17 -6
@@ -567,11 +567,16 @@ static void setup_vma_to_mm(struct vm_area_struct *vma, struct mm_struct *mm)if (vma->vm_file) {struct address_space *mapping = vma->vm_file->f_mapping; -i_mmap_lock_write (mapping); +i_mmap_lock_write_prepare (mapping); +i_mmap_tree_lock_write (mapping, vma); +flush_dcache_mmap_lock (mapping); -vma_interval_tree_insert (vma,get_i_mmap_root (mapping)); +vma_interval_tree_insert (vma,get_rb_root (vma, mapping)); +inc_mapping_vma (mapping, vma);flush_dcache_mmap_unlock (mapping); -i_mmap_unlock_write (mapping); + +i_mmap_tree_unlock_write (mapping, vma); +i_mmap_unlock_write_complete (mapping); } }
@@ -583,11 +588,16 @@ static void cleanup_vma_from_mm(struct vm_area_struct *vma)struct address_space *mapping; mapping = vma->vm_file->f_mapping; -i_mmap_lock_write (mapping); +i_mmap_lock_write_prepare (mapping); +i_mmap_tree_lock_write (mapping, vma); +flush_dcache_mmap_lock (mapping); -vma_interval_tree_remove (vma,get_i_mmap_root (mapping)); +vma_interval_tree_remove (vma,get_rb_root (vma, mapping)); +dec_mapping_vma (mapping, vma);flush_dcache_mmap_unlock (mapping); -i_mmap_unlock_write (mapping); + +i_mmap_tree_unlock_write (mapping, vma); +i_mmap_unlock_write_complete (mapping); } }
@@ -1063,6 +1073,7 @@ unsigned long do_mmap(struct file *file,if (file) { region->vm_file =get_file (file); vma->vm_file =get_file (file); +vma_set_tree_idx (vma); }down_write (&nommu_region_sem);
mm/pagewalk.c +1 -1
@@ -809,7 +809,7 @@ int walk_page_mapping(struct address_space *mapping, pgoff_t first_index,if (!check_ops_safe (ops))return -EINVAL ; -lockdep_assert_held (&mapping->i_mmap_rwsem); +i_mmap_assert_locked (mapping);vma_interval_tree_foreach (vma,get_i_mmap_root (mapping), first_index, first_index + nr - 1) {/* Clip to the vma */
mm/vma.c +53 -19
161 lines not on this page · raw message
mm/vma_init.c +3 -0
@@ -72,6 +72,9 @@ static void vm_area_init_from(const struct vm_area_struct *src,#ifdef CONFIG_NUMA dest->vm_policy = src->vm_policy;#endif +#ifdef CONFIG_SPLIT_I_MMAP + dest->tree_idx = src->tree_idx; +#endif #ifdef __HAVE_PFNMAP_TRACKING dest->pfnmap_track_ctx =NULL ;#endif
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v2 3/4] mm/fs: split the file's i_mmap tree
Thank you for your contribution! Sashiko AI review found 8 potential issue(s) to consider: - [Critical] Immediate self-deadlock in `vma_prepare()` when adjacent VMAs share the same `tree_idx`.…
Thank you for your contribution! Sashiko AI review found 8 potential issue(s) to consider:
- [Critical] Immediate self-deadlock in vma_prepare() when adjacent VMAs share the same tree_idx.
- [Critical] vma_complete() blindly unlocks both vp->vma and vp->adj_next trees, risking a double unlock.
- [High] Lock inversion deadlock between vma_prepare() and global i_mmap tree locking functions.
- [High] Massive per-inode memory bloat due to unconditional split tree allocation.
- [High] address_space_init_once() no longer initializes i_mmap fields.
- [High] Lock ordering inversion between anon_vma->rwsem and mapping->i_mmap sibling tree locks.
- [Medium] Iterating over all i_mmap sibling trees triggers lockdep recursive locking splats.
- [Low] Syntax error in hugetlbfs_lockdep_set_class() blocks compilation.
--
commit f5f0f658ce23acc54c74ca009d1b5fa489c2d03a
Author: Huang Shijie <[email protected]>
Subject: mm/fs: split the file's i_mmap tree
This commit splits the single i_mmap tree per file into multiple sibling trees to reduce lock contention during VMA operations. It introduces new fields and updates lock handling and interval tree iterators across the memory management subsystem.
Quoted 3 lines
fs/hugetlbfs/inode.c +0
-0
[ ... ]
fs/hugetlbfs/inode.c static struct inode *hugetlbfs_get_root(struct super_block *sb, · line 900
Quoted 11 lines
fs/hugetlbfs/inode.c +7
-0@@ -891,6 +891,23 @@ static struct inode *hugetlbfs_get_root(struct super_block *sb,*/ static struct lock_class_key hugetlbfs_i_mmap_rwsem_key; +#ifdef CONFIG_SPLIT_I_MMAP +static void hugetlbfs_lockdep_set_class (struct address_space *mapping) +{ +int i; + +for (i =0 ; i < split_tree_num; i++) { + lockdep_set_class(&mapping->i_mmap[i].rwsem,
[Severity: Low]
Does this cause a compilation error? Since mapping->i_mmap is an array of pointers to struct i_mmap_tree, using the dot operator to access rwsem will fail. Should this be mapping->i_mmap[i]->rwsem instead?
Quoted 3 lines
fs/hugetlbfs/inode.c +3
-0+ &hugetlbfs_i_mmap_rwsem_key); + } +}
[ ... ]
Quoted 3 lines
fs/inode.c +0
-0
[ ... ]
Quoted 5 lines
fs/inode.c +1
-0@@ -214,6 +214,70 @@ static int no_open(struct inode *inode, struct file *file)return -ENXIO ; } +#ifdef CONFIG_SPLIT_I_MMAP
[ ... ]
fs/inode.c static int no_open(struct inode *inode, struct file *file) · line 255
Quoted 12 lines
fs/inode.c +12
-0+static int init_mapping_i_mmap (struct address_space *mapping,gfp_t gfp) +{ +struct i_mmap_tree *tree; +int i; + +/* The extra one is used as terminator in vma_interval_tree_foreach() */ + mapping->i_mmap =kzalloc (sizeof (tree) * (split_tree_num +1 ), gfp); +if (!mapping->i_mmap) +return -ENOMEM ; + +for (i =0 ; i < split_tree_num; i++) { + tree =kzalloc_node (sizeof (*tree), gfp, i);
[Severity: High]
Could this lead to significant memory bloat? By allocating the array and multiple trees unconditionally for every instantiated inode, high-core-count systems might consume massive amounts of memory for millions of cached inodes, many of which may never use the i_mmap tree.
Quoted 10 lines
fs/inode.c +10
-0+if (!tree) + goto nomem; + + tree->root =RB_ROOT_CACHED ; +init_rwsem (&tree->rwsem); + + mapping->i_mmap[i] = tree; + } +return 0 ; +nomem:
[ ... ]
fs/inode.c EXPORT_SYMBOL(inc_nlink); · line 554
Quoted 8 lines
fs/inode.c +0
-2@@ -480,9 +550,7 @@ EXPORT_SYMBOL(inc_nlink);static void __address_space_init_once (struct address_space *mapping) {xa_init_flags (&mapping->i_pages,XA_FLAGS_LOCK_IRQ |XA_FLAGS_ACCOUNT ); -init_rwsem (&mapping->i_mmap_rwsem);spin_lock_init (&mapping->i_private_lock); - mapping->i_mmap =RB_ROOT_CACHED ; }
[Severity: High]
Will this cause issues for non-inode users of address_space? Since address_space_init_once() no longer initializes the i_mmap fields, standalone mappings could trigger null pointer dereferences when their i_mmap locks are acquired.
Quoted 3 lines
include/linux/fs.h +0
-0
[ ... ]
include/linux/fs.h static inline bool mapping_tagged(const struct address_space *mapping, xa_mark_t · line 595
Quoted 7 lines
include/linux/fs.h +7
-0+static inline void i_mmap_lock_write (struct address_space *mapping) +{ +int i; + +for (i =0 ; i < split_tree_num; i++) +down_write (&mapping->i_mmap[i]->rwsem); +}
[Severity: Medium]
Will this trigger lockdep splats for recursive locking? Because all sibling tree semaphores share the same static lock class, acquiring them sequentially inside this loop might cause lockdep to detect a false recursion and exceed its maximum depth limits.
Quoted 3 lines
mm/vma.c +0
-0
[ ... ]
mm/vma.c static void vma_prepare(struct vma_prepare *vp) · line 324
Quoted 5 lines
mm/vma.c +1
-0@@ -318,12 +321,17 @@ static void vma_prepare(struct vma_prepare *vp) }if (vp->file) { +i_mmap_tree_lock_write (vp->mapping, vp->vma);
[Severity: High]
Does this sequence invert the standard locking hierarchy? This path inside vma_prepare() now acquires the i_mmap tree lock after acquiring the anon_vma lock (acquired earlier in the function), whereas previously the full i_mmap lock was acquired before locking the anon_vma.
mm/vma.c static void vma_prepare(struct vma_prepare *vp) · line 330
Quoted 8 lines
mm/vma.c +4
-2flush_dcache_mmap_lock (vp->mapping);vma_interval_tree_remove (vp->vma, -get_i_mmap_root (vp->mapping)); -if (vp->adj_next) +get_rb_root (vp->vma, vp->mapping)); +dec_mapping_vma (vp->mapping, vp->vma); +if (vp->adj_next) { +i_mmap_tree_lock_write (vp->mapping, vp->adj_next);
[Severity: Critical]
Is there a risk of an immediate self-deadlock here? If vp->vma and vp->adj_next share the same tree index, this thread will attempt to acquire the exact same semaphore twice in a row.
[Severity: High]
Could this also cause a lock inversion against the global tree traversal functions? The global functions lock all trees in ascending index order, but this path locks based on adjacent vma tree indexes which could be in descending order.
Quoted 6 lines
mm/vma.c +3
-1vma_interval_tree_remove (vp->adj_next, -get_i_mmap_root (vp->mapping)); +get_rb_root (vp->adj_next, vp->mapping)); +dec_mapping_vma (vp->mapping, vp->adj_next); + } }
[ ... ]
Quoted 9 lines
mm/vma.c +2
-2@@ -340,12 +348,17 @@ static void vma_complete(struct vma_prepare *vp, struct vma_iterator *vmi, struct mm_struct *mm) {if (vp->file) { -if (vp->adj_next) +if (vp->adj_next) {vma_interval_tree_insert (vp->adj_next, -get_i_mmap_root (vp->mapping)); +get_rb_root (vp->adj_next, vp->mapping));
mm/vma.c static void vma_complete(struct vma_prepare *vp, struct vma_iterator *vmi, · line 362
Quoted 10 lines
mm/vma.c +6
-1+inc_mapping_vma (vp->mapping, vp->adj_next); +i_mmap_tree_unlock_write (vp->mapping, vp->adj_next); + }vma_interval_tree_insert (vp->vma, -get_i_mmap_root (vp->mapping)); +get_rb_root (vp->vma, vp->mapping)); +inc_mapping_vma (vp->mapping, vp->vma);flush_dcache_mmap_unlock (vp->mapping); +i_mmap_tree_unlock_write (vp->mapping, vp->vma); }
[Severity: Critical]
If vp->vma and vp->adj_next both map to the same sibling tree index, could this cause a double unlock? This sequence releases the tree for vp->adj_next and then for vp->vma, which would unlock the same semaphore twice and corrupt its state.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Pedro Falcato
reviewerRe: [PATCH v2 3/4] mm/fs: split the file's i_mmap tree
In reply to Huang ShijieHi,
Quoted 8 lines · Huang Shijie
In the UnixBench tests, there is a test "execl" which tests
the execve system call.
For example, a Hygon's server has 12 NUMA nodes, and 384 CPUs.
When we test our server with "./Run -c 384 execl",
the test result is not good enough. Thei_mmaplocks contended heavily on "libc.so" and "ld.so". Thei_mmaptree for "libc.so" can be
over 6000 VMAs, all the VMAs can be in different NUMA mode. The insert/remove operations do not run quickly enough.
I _really_ would have appreciated some coordination here, because I said I was going to take a look at it. I have something that I think is much simpler in practice. These patches are also way too complex to be dropped just before the merge window.
Some comments:
Quoted 6 lines
In order to reduce the competition of the
i_mmaplock, this patch does
following:
1.) Split the singlei_mmaptree into several sibling trees:
Each tree has a lock. TheCONFIG_SPLIT_I_MMAPis used to turn on/off this feature.
There is no need for a config option. This needs to Just Work.
Quoted 2 lines
2.) Introduce a new field "
tree_idx" forvm_area_structto save the
sibling tree index for this VMA.
This is possibly contentious, but there are holes in vm_area_struct. So I think this is fine.
Quoted 91 lines
3.) Introduce a new field "
vma_count" foraddress_space.
The newmapping_mapped()will use it.
4.) Rewrite thevma_interval_tree_foreach()
5.) Rewrite the lock functions.After this patch, the VMA insert/remove operations will work faster,
and we can get over 400% performance improvement with the above test.Signed-off-by Huang Shijie <[email protected]>--- fs/Kconfig | 8 ++ fs/hugetlbfs/inode.c | 20 ++++- fs/inode.c | 75 ++++++++++++++++- include/linux/fs.h | 174 ++++++++++++++++++++++++++++++++++++++- include/linux/mm.h | 80 ++++++++++++++++++ include/linux/mm_types.h | 3 + mm/internal.h | 3 +- mm/mmap.c | 11 ++- mm/nommu.c | 23 ++++-- mm/pagewalk.c | 2 +- mm/vma.c | 72 +++++++++++----- mm/vma_init.c | 3 + 12 files changed, 436 insertions(+), 38 deletions(-)fs/Kconfig +8
-0@@ -9,6 +9,14 @@ menu "File systems" config DCACHE_WORD_ACCESS bool +config SPLIT_I_MMAP + bool "Split the file's i_mmap to several trees" + default n + help + Split the file's i_mmap to several trees, each tree has a separate + lock. This will reduce the lock contention of file's i_mmap tree, + but it will cost more memory for per inode. + config VALIDATE_FS_PARSER bool "Validate filesystem parameter description" helpfs/hugetlbfs/inode.c +18
-2@@ -891,6 +891,23 @@ static struct inode *hugetlbfs_get_root(struct super_block *sb,*/ static struct lock_class_key hugetlbfs_i_mmap_rwsem_key; +#ifdef CONFIG_SPLIT_I_MMAP +static void hugetlbfs_lockdep_set_class (struct address_space *mapping) +{ +int i; + +for (i =0 ; i < split_tree_num; i++) { +lockdep_set_class (&mapping->i_mmap[i].rwsem, + &hugetlbfs_i_mmap_rwsem_key); + } +} +#else +static void hugetlbfs_lockdep_set_class (struct address_space *mapping) +{ +lockdep_set_class (&mapping->i_mmap_rwsem, &hugetlbfs_i_mmap_rwsem_key); +} +#endif + static struct inode *hugetlbfs_get_inode(struct super_block *sb, struct mnt_idmap *idmap, struct inode *dir,@@ -915,8 +932,7 @@ static struct inode *hugetlbfs_get_inode(struct super_block *sb, inode->i_ino =get_next_ino ();inode_init_owner (idmap, inode, dir, mode); -lockdep_set_class (&inode->i_mapping->i_mmap_rwsem, - &hugetlbfs_i_mmap_rwsem_key); +hugetlbfs_lockdep_set_class (inode->i_mapping); inode->i_mapping->a_ops = &hugetlbfs_aops;simple_inode_init_ts (inode); info->resv_map = resv_map;fs/inode.c +2
-0@@ -214,6 +214,70 @@ static int no_open(struct inode *inode, struct file *file)return -ENXIO ; } +#ifdef CONFIG_SPLIT_I_MMAP +int split_tree_num;
fs/inode.c static int no_open(struct inode *inode, struct file *file) · line 228
Quoted 10 lines
fs/inode.c +10
-0+static int split_tree_align__maybe_unused =32 ; + +static void __init init_split_tree_num (void ) +{ +#ifdef CONFIG_NUMA + split_tree_num = nr_node_ids; +#else + split_tree_num =ALIGN (nr_cpu_ids, split_tree_align); +#endif +}
Again, too configurable. I think you're too stuck up on the NUMA case - which does not matter for many people - and may actively harm NUMA users. If I have a 128 core 2 NUMA node system, what should I shard by?
Quoted 22 lines
fs/inode.c +22
-0+ +static void free_mapping_i_mmap (struct address_space *mapping) +{ +int i; + +if (!mapping->i_mmap) +return ; + +for (i =0 ; i < split_tree_num; i++) +kfree (mapping->i_mmap[i]); + +kfree (mapping->i_mmap); + mapping->i_mmap =NULL ; +} + +static int init_mapping_i_mmap (struct address_space *mapping,gfp_t gfp) +{ +struct i_mmap_tree *tree; +int i; + +/* The extra one is used as terminator in vma_interval_tree_foreach() */ + mapping->i_mmap =kzalloc (sizeof (tree) * (split_tree_num +1 ), gfp);
fs/inode.c static int no_open(struct inode *inode, struct file *file) · line 260
Quoted 10 lines
fs/inode.c +10
-0+if (!mapping->i_mmap) +return -ENOMEM ; + +for (i =0 ; i < split_tree_num; i++) { + tree =kzalloc_node (sizeof (*tree), gfp, i); +if (!tree) + goto nomem; + + tree->root =RB_ROOT_CACHED ; +init_rwsem (&tree->rwsem);
This (as-is) should blow up with lockdep + the locking loops down there.
fs/inode.c static int no_open(struct inode *inode, struct file *file) · line 268
Quoted 8 lines
fs/inode.c +8
-0+ + mapping->i_mmap[i] = tree; + } +return 0 ; +nomem: +free_mapping_i_mmap (mapping); +return -ENOMEM ; +}
Honestly, it's likely that a simple static array in struct address_space suffices. I would not go through the trouble of getting everything very tight and NUMA correct.
Quoted 71 lines
fs/inode.c +20
-3+#else +static int init_mapping_i_mmap (struct address_space *mapping,gfp_t gfp) +{ + mapping->i_mmap =RB_ROOT_CACHED ; +init_rwsem (&mapping->i_mmap_rwsem); +return 0 ; +} + +static void free_mapping_i_mmap (struct address_space *mapping) { } +static void __init init_split_tree_num (void ) {} +#endif +/** * inode_init_always_gfp - perform inode structure initialisation * @sb: superblock inode belongs to @@ -302,9 +366,14 @@ int inode_init_always_gfp(struct super_block *sb, struct inode *inode, gfp_t gfp#endif inode->i_flctx =NULL ; -if (unlikely (security_inode_alloc (inode, gfp))) +if (init_mapping_i_mmap (mapping, gfp))return -ENOMEM ; +if (unlikely (security_inode_alloc (inode, gfp))) { +free_mapping_i_mmap (mapping); +return -ENOMEM ; + } +this_cpu_inc (nr_inodes);return 0 ;@@ -380,6 +449,7 @@ void __destroy_inode(struct inode *inode)if (inode->i_default_acl && !is_uncached_acl (inode->i_default_acl))posix_acl_release (inode->i_default_acl);#endif +free_mapping_i_mmap (&inode->i_data);this_cpu_dec (nr_inodes); }EXPORT_SYMBOL (__destroy_inode);@@ -480,9 +550,7 @@ EXPORT_SYMBOL(inc_nlink);static void __address_space_init_once (struct address_space *mapping) {xa_init_flags (&mapping->i_pages,XA_FLAGS_LOCK_IRQ |XA_FLAGS_ACCOUNT ); -init_rwsem (&mapping->i_mmap_rwsem);spin_lock_init (&mapping->i_private_lock); - mapping->i_mmap =RB_ROOT_CACHED ; }void address_space_init_once (struct address_space *mapping)@@ -2619,6 +2687,7 @@ void __init inode_init(void) &i_hash_mask,0 , 0); +init_split_tree_num (); }void init_special_inode (struct inode *inode,umode_t mode,dev_t rdev)include/linux/fs.h +6
-0@@ -450,6 +450,25 @@ struct mapping_metadata_bhs {struct list_head list;/* The list of bhs (b_assoc_buffers) */ }; +#ifdef CONFIG_SPLIT_I_MMAP +/* +* struct i_mmap_tree - A single sibling tree of the file's split i_mmap. +* @root: The red/black interval tree root. +* @rwsem: Protects insert/remove operations on this sibling tree. +* @vma_count: Number of VMAs in this sibling tree.
include/linux/fs.h struct mapping_metadata_bhs { · line 468
Quoted 10 lines
include/linux/fs.h +10
-0+* +* When CONFIG_SPLIT_I_MMAP is enabled, the file's single i_mmap tree is +* split into split_tree_num sibling trees, each with its own lock. This +* reduces lock contention by allowing concurrent VMA insert/remove +* operations on different sibling trees. +*/ +struct i_mmap_tree { +struct rb_root_cached root; +struct rw_semaphore rwsem; +atomic_t vma_count;
I don't see what you need this vma_count for? I get the one in address_space, but this one does not seem useful.
Quoted 7 lines
include/linux/fs.h +3
-0+}; +#endif +/** * struct address_space - Contents of a cacheable, mappable object. * @host: Owner, either the inode or the block_device.
include/linux/fs.h struct mapping_metadata_bhs { · line 485
Quoted 8 lines
include/linux/fs.h +3
-2@@ -461,8 +480,13 @@ struct mapping_metadata_bhs {* @gfp_mask: Memory allocation flags to use for allocating pages. * @i_mmap_writable: Number of VM_SHARED, VM_MAYWRITE mappings. * @nr_thps: Number of THPs in the pagecache (non-shmem only). -* @i_mmap: Tree of private and shared mappings. -* @i_mmap_rwsem: Protects @i_mmap and @i_mmap_writable. +* @i_mmap: Tree of private and shared mappings. When CONFIG_SPLIT_I_MMAP +* is enabled, this is an array of split_tree_num struct i_mmap_tree +* pointers (plus a NULL terminator).
NULL terminator wastes more memory, so I would really strongly avoid it as well.
include/linux/fs.h struct mapping_metadata_bhs { · line 489
Quoted 4 lines
include/linux/fs.h +4
-0+* @vma_count: Total number of VMAs across all sibling trees (only when +* CONFIG_SPLIT_I_MMAP is enabled). Used by mapping_mapped(). +* @i_mmap_rwsem: Protects @i_mmap and @i_mmap_writable (only when +* CONFIG_SPLIT_I_MMAP is disabled; otherwise per-tree rwsem is used).
So, there are very good reasons why you still need an i_mmap_rwsem protecting state, even with split mmap trees. Which I'll go into later.
Quoted 11 lines
include/linux/fs.h +4
-0* @nrpages: Number of page entries, protected by the i_pages lock. * @writeback_index: Writeback starts here. * @a_ops: Methods. @@ -480,14 +504,19 @@ struct address_space {/* number of thp, only for non-shmem files */ atomic_t nr_thps;#endif +#ifdef CONFIG_SPLIT_I_MMAP +struct i_mmap_tree **i_mmap; +atomic_t vma_count; +#else
include/linux/fs.h struct address_space { · line 490
Quoted 10 lines
include/linux/fs.h +2
-1struct rb_root_cached i_mmap; +struct rw_semaphore i_mmap_rwsem; +#endif unsigned long nrpages;pgoff_t writeback_index;const struct address_space_operations *a_ops;unsigned long flags;errseq_t wb_err;spinlock_t i_private_lock; -struct rw_semaphore i_mmap_rwsem;
See d3b1a9a778e1 ("fs/address_space: move i_mmap_rwsem to mitigate a false sharing with i_mmap.")
include/linux/fs.h static inline bool mapping_tagged(const struct address_space *mapping, xa_mark_t · line 543
Quoted 11 lines
include/linux/fs.h +4
-0} __attribute__((aligned(sizeof(long)))) __randomize_layout;/* * On most architectures that alignment is already the case; but @@ -508,6 +537,133 @@ static inline bool mapping_tagged(const struct address_space *mapping, xa_mark_treturn xa_marked (&mapping->i_pages, tag); } +#ifdef CONFIG_SPLIT_I_MMAP +static inline int mapping_mapped (const struct address_space *mapping) +{ +return atomic_read (&mapping->vma_count);
Now that I think of it, I don't think we need atomic_t, only unsigned long + READ_ONCE() suffices. Increments can race just fine, we don't expect any consistency there - if you want consistency you probably hold the i_mmap lock.
Quoted 9 lines
include/linux/fs.h +9
-0+} + +static inline void inc_mapping_vma (struct address_space *mapping, +struct vm_area_struct *vma) +{ +struct i_mmap_tree *tree = mapping->i_mmap[vma->tree_idx]; + +atomic_inc (&tree->vma_count); +atomic_inc (&mapping->vma_count);
include/linux/fs.h static inline bool mapping_tagged(const struct address_space *mapping, xa_mark_t · line 562
Quoted 10 lines
include/linux/fs.h +10
-0+} + +static inline void dec_mapping_vma (struct address_space *mapping, +struct vm_area_struct *vma) +{ +struct i_mmap_tree *tree = mapping->i_mmap[vma->tree_idx]; + +atomic_dec (&tree->vma_count); +atomic_dec (&mapping->vma_count); +}
This probably shouldn't be in linux/fs.h.
Quoted 14 lines
include/linux/fs.h +14
-0+ +static inline struct rb_root_cached *get_i_mmap_root (struct address_space *mapping) +{ +return (struct rb_root_cached *)mapping->i_mmap; +} + +static inline void i_mmap_tree_lock_write (struct address_space *mapping, +struct vm_area_struct *vma) +{ +struct i_mmap_tree *tree = mapping->i_mmap[vma->tree_idx]; + +down_write (&tree->rwsem); +} +
include/linux/fs.h static inline bool mapping_tagged(const struct address_space *mapping, xa_mark_t · line 586
Quoted 10 lines
include/linux/fs.h +10
-0+static inline void i_mmap_tree_unlock_write (struct address_space *mapping, +struct vm_area_struct *vma) +{ +struct i_mmap_tree *tree = mapping->i_mmap[vma->tree_idx]; + +up_write (&tree->rwsem); +} + +#define i_mmap_lock_write_prepare (mapping) +#define i_mmap_unlock_write_complete (mapping)
It's unclear to me why you added write_prepare() and write_complete().
include/linux/fs.h static inline bool mapping_tagged(const struct address_space *mapping, xa_mark_t · line 594
Quoted 8 lines
include/linux/fs.h +8
-0+ +extern int split_tree_num; +static inline void i_mmap_lock_write (struct address_space *mapping) +{ +int i; + +for (i =0 ; i < split_tree_num; i++) +down_write (&mapping->i_mmap[i]->rwsem);
Oof, this is an incredibly large hammer. This is basically why I think keeping i_mmap_rwsem (in a different form) is required. You do not want to take $nr_cpus locks (read _or_ write). For my design, I keep i_mmap_rwsem, but I invert its meaning - taking it in write = I'm reading from the tree; taking it in read = I'm writing to the tree. This provides some lighter-weight exclusion between rmap walks and rmap tree manipulation.
_Technically_, you shouldn't need to always take a lock when manipulating the tree. A pattern like mnt_hold_writers()/mnt_get_write_access() can probably work well. But it may be too complex ATM.
Also, note that you pretty much do not want i_mmap_lock_write() users after the conversion is done.
Quoted 114 lines
include/linux/fs.h +84
-0+} + +static inline int i_mmap_trylock_write (struct address_space *mapping) +{ +int i; + +for (i =0 ; i < split_tree_num; i++) { +if (!down_write_trylock (&mapping->i_mmap[i]->rwsem)) { +while (i--) +up_write (&mapping->i_mmap[i]->rwsem); +return 0 ; + } + } +return 1 ; +} + +static inline void i_mmap_unlock_write (struct address_space *mapping) +{ +int i; + +for (i =0 ; i < split_tree_num; i++) +up_write (&mapping->i_mmap[i]->rwsem); +} + +static inline int i_mmap_trylock_read (struct address_space *mapping) +{ +int i; + +for (i =0 ; i < split_tree_num; i++) { +if (!down_read_trylock (&mapping->i_mmap[i]->rwsem)) { +while (i--) +up_read (&mapping->i_mmap[i]->rwsem); +return 0 ; + } + } +return 1 ; +} + +static inline void i_mmap_lock_read (struct address_space *mapping) +{ +int i; + +for (i =0 ; i < split_tree_num; i++) +down_read (&mapping->i_mmap[i]->rwsem); +} + +static inline void i_mmap_unlock_read (struct address_space *mapping) +{ +int i; + +for (i =0 ; i < split_tree_num; i++) +up_read (&mapping->i_mmap[i]->rwsem); +} + +static inline void i_mmap_assert_locked (struct address_space *mapping) +{ +int i; + +for (i =0 ; i < split_tree_num; i++) +lockdep_assert_held (&mapping->i_mmap[i]->rwsem); +} + +static inline void i_mmap_assert_write_locked (struct address_space *mapping) +{ +int i; + +for (i =0 ; i < split_tree_num; i++) +lockdep_assert_held_write (&mapping->i_mmap[i]->rwsem); +} + +#else +static inline void i_mmap_lock_write (struct address_space *mapping) {down_write (&mapping->i_mmap_rwsem);@@ -561,6 +717,18 @@ static inline struct rb_root_cached *get_i_mmap_root(struct address_space *mappi return &mapping->i_mmap; } +static inline void inc_mapping_vma (struct address_space *mapping, +struct vm_area_struct *vma) { } +static inline void dec_mapping_vma (struct address_space *mapping, +struct vm_area_struct *vma) { } + +#define i_mmap_lock_write_prepare (mapping) i_mmap_lock_write(mapping) +#define i_mmap_unlock_write_complete (mapping) i_mmap_unlock_write(mapping) +#define i_mmap_tree_lock_write (mapping, vma) +#define i_mmap_tree_unlock_write (mapping, vma) + +#endif +/* * Might pages of this file have been modified in userspace? * Note that i_mmap_writable counts all VM_SHARED, VM_MAYWRITE vmas: do_mmap include/linux/mm.h +12
-0@@ -4041,11 +4041,91 @@ struct vm_area_struct *vma_interval_tree_iter_first(struct rb_root_cached *root,struct vm_area_struct *vma_interval_tree_iter_next (struct vm_area_struct *node,unsigned long start,unsigned long last); +#ifdef CONFIG_SPLIT_I_MMAP +extern int split_tree_num; + +static inline int smallest_tree_idx (struct file *file) +{ +struct address_space *mapping = file->f_mapping; +int tmp =INT_MAX , count; +int i, j =0 ; + +/* +* Since a not 100% accurate value is still okay, +* we do not need any lock here.
include/linux/mm.h struct vm_area_struct *vma_interval_tree_iter_first(struct rb_root_cached *root, · line 4065
Quoted 10 lines
include/linux/mm.h +10
-0+*/ +for (i =0 ; i < split_tree_num; i++) { + count =atomic_read (&mapping->i_mmap[i]->vma_count); +if (count < tmp) { + j = i; + tmp = count; +if (!tmp) +break ; + } + }
Ohh, I see why you want the per-subtree vma_count now. But is this a net-win? I think doing something like vma-pointer-hashing or just smp_processor_id() would work a-ok.
Quoted 71 lines
include/linux/mm.h +58
-0+return j; +} + +static inline void vma_set_tree_idx (struct vm_area_struct *vma) +{ +#ifdef CONFIG_NUMA + vma->tree_idx =numa_node_id (); +#else + vma->tree_idx =smallest_tree_idx (vma->vm_file); +#endif +} + +static inline struct rb_root_cached *get_rb_root (struct vm_area_struct *vma, +struct address_space *mapping) +{ +return &mapping->i_mmap[vma->tree_idx]->root; +} + +/* Find the first valid VMA in the sibling trees */ +static inline struct vm_area_struct *first_vma (struct i_mmap_tree ***__r, +unsigned long start,unsigned long last) +{ +struct vm_area_struct *vma =NULL ; +struct i_mmap_tree **tree = *__r; +struct rb_root_cached *root; + +while (*tree) { + root = &(*tree)->root; + tree++; + vma =vma_interval_tree_iter_first (root, start, last); +if (vma) +break ; + } + +/* Save for the next loop */ + *__r = tree; +return vma; +} + +/* +* Please use get_i_mmap_root() to get the @root. +* @_tmp is referenced to avoid unused variable warning. +*/ +#define vma_interval_tree_foreach (vma, root, start, last) \ + for (struct i_mmap_tree **_r = (struct i_mmap_tree **)(root), \ + **_tmp = (vma = first_vma(&_r, start, last)) ? _r : NULL;\ + ((_tmp && vma) || (vma = first_vma(&_r, start, last))); \ + vma = vma_interval_tree_iter_next(vma, start, last)) +#else /* Please use get_i_mmap_root() to get the @root */ #define vma_interval_tree_foreach (vma, root, start, last) \ for (vma = vma_interval_tree_iter_first(root, start, last); \ vma; vma = vma_interval_tree_iter_next(vma, start, last)) +static inline void vma_set_tree_idx (struct vm_area_struct *vma) { } + +static inline struct rb_root_cached *get_rb_root (struct vm_area_struct *vma, +struct address_space *mapping) +{ +return &mapping->i_mmap; +} +#endif +void anon_vma_interval_tree_insert (struct anon_vma_chain *node,struct rb_root_cached *root); void anon_vma_interval_tree_remove(struct anon_vma_chain *node,include/linux/mm_types.h +0
-0
include/linux/mm_types.h struct vm_area_struct { · line 1077
Quoted 6 lines
include/linux/mm_types.h +3
-0@@ -1072,6 +1072,9 @@ struct vm_area_struct {#ifdef __HAVE_PFNMAP_TRACKINGstruct pfnmap_track_ctx *pfnmap_track_ctx;#endif +#ifdef CONFIG_SPLIT_I_MMAP +int tree_idx;/* The sibling tree index for the VMA */ +#endif
FTR the struct hole isn't here, but right after vm_lock_seq or vm_refcnt in most configs.
Quoted 27 lines
include/linux/mm_types.h +0
-0} __randomize_layout;/* Clears all bits in the VMA flags bitmap, non-atomically. */ mm/internal.h +2
-1@@ -1888,7 +1888,8 @@ static inline void maybe_rmap_unlock_action(struct vm_area_struct *vma,VM_WARN_ON_ONCE (vma_is_anonymous (vma)); file = vma->vm_file; -i_mmap_unlock_write (file->f_mapping); +i_mmap_tree_unlock_write (file->f_mapping, vma); +i_mmap_unlock_write_complete (file->f_mapping); action->hide_from_rmap_until_complete = false; }mm/mmap.c +1
-1@@ -1825,15 +1825,20 @@ __latent_entropy int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)struct address_space *mapping = file->f_mapping;get_file (file); -i_mmap_lock_write (mapping); +i_mmap_lock_write_prepare (mapping);
mm/mmap.c __latent_entropy int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm) · line 1837
Quoted 10 lines
mm/mmap.c +4
-1+i_mmap_tree_lock_write (mapping, mpnt); +if (vma_is_shared_maywrite (tmp))mapping_allow_writable (mapping);flush_dcache_mmap_lock (mapping);/* insert tmp into the share list, just after mpnt */ vma_interval_tree_insert_after (tmp, mpnt, -get_i_mmap_root (mapping)); +get_rb_root (mpnt, mapping)); +inc_mapping_vma (mapping, tmp);
Honestly, would prefer to hide all of these details from mmap.
Quoted 53 lines
mm/mmap.c +3
-1flush_dcache_mmap_unlock (mapping); -i_mmap_unlock_write (mapping); + +i_mmap_tree_unlock_write (mapping, mpnt); +i_mmap_unlock_write_complete (mapping); }if (!(tmp->vm_flags &VM_WIPEONFORK ))mm/nommu.c +16
-6@@ -567,11 +567,16 @@ static void setup_vma_to_mm(struct vm_area_struct *vma, struct mm_struct *mm)if (vma->vm_file) {struct address_space *mapping = vma->vm_file->f_mapping; -i_mmap_lock_write (mapping); +i_mmap_lock_write_prepare (mapping); +i_mmap_tree_lock_write (mapping, vma); +flush_dcache_mmap_lock (mapping); -vma_interval_tree_insert (vma,get_i_mmap_root (mapping)); +vma_interval_tree_insert (vma,get_rb_root (vma, mapping)); +inc_mapping_vma (mapping, vma);flush_dcache_mmap_unlock (mapping); -i_mmap_unlock_write (mapping); + +i_mmap_tree_unlock_write (mapping, vma); +i_mmap_unlock_write_complete (mapping); } }@@ -583,11 +588,16 @@ static void cleanup_vma_from_mm(struct vm_area_struct *vma)struct address_space *mapping; mapping = vma->vm_file->f_mapping; -i_mmap_lock_write (mapping); +i_mmap_lock_write_prepare (mapping); +i_mmap_tree_lock_write (mapping, vma); +flush_dcache_mmap_lock (mapping); -vma_interval_tree_remove (vma,get_i_mmap_root (mapping)); +vma_interval_tree_remove (vma,get_rb_root (vma, mapping)); +dec_mapping_vma (mapping, vma);flush_dcache_mmap_unlock (mapping); -i_mmap_unlock_write (mapping); + +i_mmap_tree_unlock_write (mapping, vma); +i_mmap_unlock_write_complete (mapping); } }
mm/nommu.c unsigned long do_mmap(struct file *file, · line 1076
Quoted 4 lines
mm/nommu.c +1
-0@@ -1063,6 +1073,7 @@ unsigned long do_mmap(struct file *file,if (file) { region->vm_file =get_file (file); vma->vm_file =get_file (file); +vma_set_tree_idx (vma);
This is unrelated, shouldn't be done here.
mm/pagewalk.c int walk_page_mapping(struct address_space *mapping, pgoff_t first_index, · line 812
Quoted 13 lines
mm/nommu.c +0
-0}down_write (&nommu_region_sem);mm/pagewalk.c +1
-1@@ -809,7 +809,7 @@ int walk_page_mapping(struct address_space *mapping, pgoff_t first_index,if (!check_ops_safe (ops))return -EINVAL ; -lockdep_assert_held (&mapping->i_mmap_rwsem); +i_mmap_assert_locked (mapping);
This kind of conversion should be done in a separate step.
Quoted 8 lines
mm/pagewalk.c +0
-0vma_interval_tree_foreach (vma,get_i_mmap_root (mapping), first_index, first_index + nr - 1) {/* Clip to the vma */ mm/vma.c +0
-0
mm/vma.c static void __vma_link_file(struct vm_area_struct *vma, · line 238
Quoted 6 lines
mm/vma.c +2
-1@@ -234,22 +234,23 @@ static void __vma_link_file(struct vm_area_struct *vma,mapping_allow_writable (mapping);flush_dcache_mmap_lock (mapping); -vma_interval_tree_insert (vma,get_i_mmap_root (mapping)); +vma_interval_tree_insert (vma,get_rb_root (vma, mapping)); +inc_mapping_vma (mapping, vma);
inc_mapping_vma() should probably be done implicitly by insertion?
Quoted 125 lines
mm/vma.c +32
-17flush_dcache_mmap_unlock (mapping); } -/* -* Requires inode->i_mapping->i_mmap_rwsem -*/ static void __remove_shared_vm_struct (struct vm_area_struct *vma,struct address_space *mapping) { +i_mmap_tree_lock_write (mapping, vma);if (vma_is_shared_maywrite (vma))mapping_unmap_writable (mapping);flush_dcache_mmap_lock (mapping); -vma_interval_tree_remove (vma,get_i_mmap_root (mapping)); +vma_interval_tree_remove (vma,get_rb_root (vma, mapping)); +dec_mapping_vma (mapping, vma);flush_dcache_mmap_unlock (mapping); +i_mmap_tree_unlock_write (mapping, vma); }/* @@ -297,8 +298,9 @@ static void vma_prepare(struct vma_prepare *vp)uprobe_munmap (vp->adj_next, vp->adj_next->vm_start, vp->adj_next->vm_end); -i_mmap_lock_write (vp->mapping); +i_mmap_lock_write_prepare (vp->mapping);if (vp->insert && vp->insert->vm_file) { +i_mmap_tree_lock_write (vp->mapping, vp->insert);/* * Put into interval tree now, so instantiated pages * are visible to arm/parisc __flush_dcache_page @@ -307,6 +309,7 @@ static void vma_prepare(struct vma_prepare *vp)*/ __vma_link_file (vp->insert, vp->insert->vm_file->f_mapping); +i_mmap_tree_unlock_write (vp->mapping, vp->insert); } }@@ -318,12 +321,17 @@ static void vma_prepare(struct vma_prepare *vp) }if (vp->file) { +i_mmap_tree_lock_write (vp->mapping, vp->vma);flush_dcache_mmap_lock (vp->mapping);vma_interval_tree_remove (vp->vma, -get_i_mmap_root (vp->mapping)); -if (vp->adj_next) +get_rb_root (vp->vma, vp->mapping)); +dec_mapping_vma (vp->mapping, vp->vma); +if (vp->adj_next) { +i_mmap_tree_lock_write (vp->mapping, vp->adj_next);vma_interval_tree_remove (vp->adj_next, -get_i_mmap_root (vp->mapping)); +get_rb_root (vp->adj_next, vp->mapping)); +dec_mapping_vma (vp->mapping, vp->adj_next); + } } }@@ -340,12 +348,17 @@ static void vma_complete(struct vma_prepare *vp, struct vma_iterator *vmi, struct mm_struct *mm) {if (vp->file) { -if (vp->adj_next) +if (vp->adj_next) {vma_interval_tree_insert (vp->adj_next, -get_i_mmap_root (vp->mapping)); +get_rb_root (vp->adj_next, vp->mapping)); +inc_mapping_vma (vp->mapping, vp->adj_next); +i_mmap_tree_unlock_write (vp->mapping, vp->adj_next); + }vma_interval_tree_insert (vp->vma, -get_i_mmap_root (vp->mapping)); +get_rb_root (vp->vma, vp->mapping)); +inc_mapping_vma (vp->mapping, vp->vma);flush_dcache_mmap_unlock (vp->mapping); +i_mmap_tree_unlock_write (vp->mapping, vp->vma); }if (vp->remove && vp->file) {@@ -370,7 +383,7 @@ static void vma_complete(struct vma_prepare *vp, struct vma_iterator *vmi, }if (vp->file) { -i_mmap_unlock_write (vp->mapping); +i_mmap_unlock_write_complete (vp->mapping);if (!vp->skip_vma_uprobe) {uprobe_mmap (vp->vma);@@ -1799,12 +1812,12 @@ static void unlink_file_vma_batch_process(struct unlink_vma_file_batch *vb)int i; mapping = vb->vmas[0 ]->vm_file->f_mapping; -i_mmap_lock_write (mapping); +i_mmap_lock_write_prepare (mapping);for (i =0 ; i < vb->count; i++) {VM_WARN_ON_ONCE (vb->vmas[i]->vm_file->f_mapping != mapping);__remove_shared_vm_struct (vb->vmas[i], mapping); } -i_mmap_unlock_write (mapping); +i_mmap_unlock_write_complete (mapping);unlink_file_vma_batch_init (vb); }@@ -1836,10 +1849,13 @@ static void vma_link_file(struct vm_area_struct *vma, bool hold_rmap_lock)if (file) { mapping = file->f_mapping; -i_mmap_lock_write (mapping); +i_mmap_lock_write_prepare (mapping); +i_mmap_tree_lock_write (mapping, vma);__vma_link_file (vma, mapping); -if (!hold_rmap_lock) -i_mmap_unlock_write (mapping); +if (!hold_rmap_lock) { +i_mmap_tree_unlock_write (mapping, vma); +i_mmap_unlock_write_complete (mapping); + } } }
mm/vma.c static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma) · line 2181
Quoted 2 lines
mm/vma.c +0
-0@@ -2164,6 +2180,23 @@ static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma) } }
I can but hope that all of the above is quite simplified before we get to the "making file rmap more complicated" bit.
Quoted 52 lines
mm/vma.c +19
-1+#ifdef CONFIG_SPLIT_I_MMAP +static inline void i_mmap_nest_lock (struct address_space *mapping, +struct rw_semaphore *lock) +{ +int i; + +for (i =0 ; i < split_tree_num; i++) +down_write_nest_lock (&mapping->i_mmap[i]->rwsem, lock); +} +#else +static inline void i_mmap_nest_lock (struct address_space *mapping, +struct rw_semaphore *lock) +{ +down_write_nest_lock (&mapping->i_mmap_rwsem, lock); +} +#endif +static void vm_lock_mapping (struct mm_struct *mm,struct address_space *mapping) {if (!test_bit (AS_MM_ALL_LOCKS , &mapping->flags)) {@@ -2178,7 +2211,7 @@ static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping)*/ if (test_and_set_bit (AS_MM_ALL_LOCKS , &mapping->flags))BUG (); -down_write_nest_lock (&mapping->i_mmap_rwsem, &mm->mmap_lock); +i_mmap_nest_lock (mapping, &mm->mmap_lock); } }@@ -2489,6 +2522,7 @@ static int __mmap_new_file_vma(struct mmap_state *map,int error; vma->vm_file = map->file; +vma_set_tree_idx (vma);if (!map->file_doesnt_need_get)get_file (map->file);mm/vma_init.c +3
-0@@ -72,6 +72,9 @@ static void vm_area_init_from(const struct vm_area_struct *src,#ifdef CONFIG_NUMA dest->vm_policy = src->vm_policy;#endif +#ifdef CONFIG_SPLIT_I_MMAP + dest->tree_idx = src->tree_idx; +#endif #ifdef __HAVE_PFNMAP_TRACKING dest->pfnmap_track_ctx =NULL ;#endif
Signature · 1 line
Pedro
Lorenzo Stoakes
maintainerRe: [PATCH v2 3/4] mm/fs: split the file's i_mmap tree
Quoted 12 lines · Pedro Falcato
Hi,
Quoted 8 lines · Huang Shijie
In the UnixBench tests, there is a test "execl" which tests
the execve system call.
For example, a Hygon's server has 12 NUMA nodes, and 384 CPUs.
When we test our server with "./Run -c 384 execl",
the test result is not good enough. Thei_mmaplocks contended heavily on "libc.so" and "ld.so". Thei_mmaptree for "libc.so" can be
over 6000 VMAs, all the VMAs can be in different NUMA mode. The insert/remove operations do not run quickly enough.
Quoted 2 lines
I _really_ would have appreciated some coordination here, because I said I was going to take a look at it. I have something that I think is much simpler
Agreed, this is the second (or in fact third?) time in recent weeks that I'm aware of where publicly discussed work has been duplicated with a series that came in later.
It's really important, when doing work that impact core stuff to have a look around and see if others are looking at it, as there's nothing more frustrating than to work on something, discuss it publicly, only to find somebody sends a competing series.
It can be tricky, as sometimes it's not obvious, or it might not be so easily found, but I would strongly suggest always making an effort on that front.
But you didn't even try to send this as an RFC either :)
Quoted 2 lines
in practice. These patches are also way too complex to be dropped just before the merge window.
This late in the cycle means -> next cycle. So you'd have needed to resend it at rc1 in a couple weeks anyway.
Quoted 11 lines
Some comments:
Quoted 6 lines
In order to reduce the competition of the
i_mmaplock, this patch does
following:
1.) Split the singlei_mmaptree into several sibling trees:
Each tree has a lock. TheCONFIG_SPLIT_I_MMAPis used to turn on/off this feature.There is no need for a config option. This needs to Just Work.
Yeah, this is just a no-go. We don't add config options for changes to core rmap code.
Quoted 6 lines
Quoted 3 lines
2.) Introduce a new field "
tree_idx" forvm_area_structto save the
sibling tree index for this VMA.This is possibly contentious, but there are holes in
vm_area_struct. So I think this is fine.
Yeah no thanks for the extra field, I already have plans for those gaps in vm_area_struct.
I am in fact writing code right now that uses them...
Quoted 4 lines
Quoted 4 lines
3.) Introduce a new field "
vma_count" foraddress_space.
The newmapping_mapped()will use it.
4.) Rewrite thevma_interval_tree_foreach()
I also intend to send a series that does a bunch of changes in the rmap code that this would conflict with.
So let's all coordinate please.
Quoted 1 line
Quoted 1 line
5.) Rewrite the lock functions.
Yeah looping on file rmap lock/unlock is gross.
Quoted 5 lines
Quoted 5 lines
After this patch, the VMA insert/remove operations will work faster,
and we can get over 400% performance improvement with the above test.Signed-off-by Huang Shijie <[email protected]>
I had a look through and this code is really overwrought and you're putting a bunch of confusing open-coded all over the codebase without comments.
This isn't upstreamable quality and you really should have sent this as an RFC first so we could discuss the approach.
Thanks, Lorenzo
Re: [PATCH v2 3/4] mm/fs: split the file's i_mmap tree
In reply to Pedro FalcatoQuoted 12 lines · Pedro Falcato
Hi,
Quoted 8 lines · Huang Shijie
In the UnixBench tests, there is a test "execl" which tests
the execve system call.
For example, a Hygon's server has 12 NUMA nodes, and 384 CPUs.
When we test our server with "./Run -c 384 execl",
the test result is not good enough. Thei_mmaplocks contended heavily on "libc.so" and "ld.so". Thei_mmaptree for "libc.so" can be
over 6000 VMAs, all the VMAs can be in different NUMA mode. The insert/remove operations do not run quickly enough.
Quoted 2 lines
I _really_ would have appreciated some coordination here, because I said I was going to take a look at it. I have something that I think is much simpler
Okay, no problem.
I waited for more then a month, I thought you are busy at other things. So I spent more then a week to finish the patch set v2.
Quoted 109 lines
in practice. These patches are also way too complex to be dropped just before the merge window.
Some comments:
Quoted 6 lines
In order to reduce the competition of the
i_mmaplock, this patch does
following:
1.) Split the singlei_mmaptree into several sibling trees:
Each tree has a lock. TheCONFIG_SPLIT_I_MMAPis used to turn on/off this feature.There is no need for a config option. This needs to Just Work.
Quoted 2 lines
2.) Introduce a new field "
tree_idx" forvm_area_structto save the
sibling tree index for this VMA.This is possibly contentious, but there are holes in
vm_area_struct. So I think this is fine.Quoted 89 lines
3.) Introduce a new field "
vma_count" foraddress_space.
The newmapping_mapped()will use it.
4.) Rewrite thevma_interval_tree_foreach()
5.) Rewrite the lock functions.After this patch, the VMA insert/remove operations will work faster,
and we can get over 400% performance improvement with the above test.Signed-off-by Huang Shijie <[email protected]>--- fs/Kconfig | 8 ++ fs/hugetlbfs/inode.c | 20 ++++- fs/inode.c | 75 ++++++++++++++++- include/linux/fs.h | 174 ++++++++++++++++++++++++++++++++++++++- include/linux/mm.h | 80 ++++++++++++++++++ include/linux/mm_types.h | 3 + mm/internal.h | 3 +- mm/mmap.c | 11 ++- mm/nommu.c | 23 ++++-- mm/pagewalk.c | 2 +- mm/vma.c | 72 +++++++++++----- mm/vma_init.c | 3 + 12 files changed, 436 insertions(+), 38 deletions(-)fs/Kconfig +8
-0@@ -9,6 +9,14 @@ menu "File systems" config DCACHE_WORD_ACCESS bool +config SPLIT_I_MMAP + bool "Split the file's i_mmap to several trees" + default n + help + Split the file's i_mmap to several trees, each tree has a separate + lock. This will reduce the lock contention of file's i_mmap tree, + but it will cost more memory for per inode. + config VALIDATE_FS_PARSER bool "Validate filesystem parameter description" helpfs/hugetlbfs/inode.c +18
-2@@ -891,6 +891,23 @@ static struct inode *hugetlbfs_get_root(struct super_block *sb,*/ static struct lock_class_key hugetlbfs_i_mmap_rwsem_key; +#ifdef CONFIG_SPLIT_I_MMAP +static void hugetlbfs_lockdep_set_class (struct address_space *mapping) +{ +int i; + +for (i =0 ; i < split_tree_num; i++) { +lockdep_set_class (&mapping->i_mmap[i].rwsem, + &hugetlbfs_i_mmap_rwsem_key); + } +} +#else +static void hugetlbfs_lockdep_set_class (struct address_space *mapping) +{ +lockdep_set_class (&mapping->i_mmap_rwsem, &hugetlbfs_i_mmap_rwsem_key); +} +#endif + static struct inode *hugetlbfs_get_inode(struct super_block *sb, struct mnt_idmap *idmap, struct inode *dir,@@ -915,8 +932,7 @@ static struct inode *hugetlbfs_get_inode(struct super_block *sb, inode->i_ino =get_next_ino ();inode_init_owner (idmap, inode, dir, mode); -lockdep_set_class (&inode->i_mapping->i_mmap_rwsem, - &hugetlbfs_i_mmap_rwsem_key); +hugetlbfs_lockdep_set_class (inode->i_mapping); inode->i_mapping->a_ops = &hugetlbfs_aops;simple_inode_init_ts (inode); info->resv_map = resv_map;fs/inode.c +0
-0@@ -214,6 +214,70 @@ static int no_open(struct inode *inode, struct file *file)return -ENXIO ; }
Quoted 14 lines
fs/inode.c static int no_open(struct inode *inode, struct file *file) · line 228
Quoted 12 lines
fs/inode.c +12
-0+#ifdef CONFIG_SPLIT_I_MMAP +int split_tree_num; +static int split_tree_align__maybe_unused =32 ; + +static void __init init_split_tree_num (void ) +{ +#ifdef CONFIG_NUMA + split_tree_num = nr_node_ids; +#else + split_tree_num =ALIGN (nr_cpu_ids, split_tree_align); +#endif +}Again, too configurable. I think you're too stuck up on the NUMA case -
If you do not care about the NUMA. The performance will _NOT_ get improved in our NUMA server. I had ever tested code which do not care about the NUMA, and I got a bad performance. Avoid the remote access is a very important thing for the NUMA server.
Quoted 2 lines
which does not matter for many people - and may actively harm NUMA users. If I have a 128 core 2 NUMA node system, what should I shard by?
It is easy to extend the tree number for NUMA. :)
For the 128 core 2 NUMA, we can extend to more trees, such as:
Two trees for each NUMA node.
Quoted 27 lines
Quoted 27 lines
fs/inode.c +26
-0+ +static void free_mapping_i_mmap (struct address_space *mapping) +{ +int i; + +if (!mapping->i_mmap) +return ; + +for (i =0 ; i < split_tree_num; i++) +kfree (mapping->i_mmap[i]); + +kfree (mapping->i_mmap); + mapping->i_mmap =NULL ; +} + +static int init_mapping_i_mmap (struct address_space *mapping,gfp_t gfp) +{ +struct i_mmap_tree *tree; +int i; + +/* The extra one is used as terminator in vma_interval_tree_foreach() */ + mapping->i_mmap =kzalloc (sizeof (tree) * (split_tree_num +1 ), gfp); +if (!mapping->i_mmap) +return -ENOMEM ; + +for (i =0 ; i < split_tree_num; i++) {
Quoted 8 lines
fs/inode.c static int no_open(struct inode *inode, struct file *file) · line 260
Quoted 6 lines
fs/inode.c +6
-0+ tree =kzalloc_node (sizeof (*tree), gfp, i); +if (!tree) + goto nomem; + + tree->root =RB_ROOT_CACHED ; +init_rwsem (&tree->rwsem);This (as-is) should blow up with lockdep + the locking loops down there.
okay, I will check it later.
thanks a lot.
Quoted 11 lines
fs/inode.c static int no_open(struct inode *inode, struct file *file) · line 268
Quoted 9 lines
fs/inode.c +8
-0+ + mapping->i_mmap[i] = tree; + } +return 0 ; +nomem: +free_mapping_i_mmap (mapping); +return -ENOMEM ; +}Honestly, it's likely that a simple static array in struct
address_space
The array size is not fixed, so we cannot add a static array in address_space.
Quoted 79 lines
suffices. I would not go through the trouble of getting everything very tight and NUMA correct.
Quoted 76 lines
fs/inode.c +20
-3+#else +static int init_mapping_i_mmap (struct address_space *mapping,gfp_t gfp) +{ + mapping->i_mmap =RB_ROOT_CACHED ; +init_rwsem (&mapping->i_mmap_rwsem); +return 0 ; +} + +static void free_mapping_i_mmap (struct address_space *mapping) { } +static void __init init_split_tree_num (void ) {} +#endif +/** * inode_init_always_gfp - perform inode structure initialisation * @sb: superblock inode belongs to @@ -302,9 +366,14 @@ int inode_init_always_gfp(struct super_block *sb, struct inode *inode, gfp_t gfp#endif inode->i_flctx =NULL ; -if (unlikely (security_inode_alloc (inode, gfp))) +if (init_mapping_i_mmap (mapping, gfp))return -ENOMEM ; +if (unlikely (security_inode_alloc (inode, gfp))) { +free_mapping_i_mmap (mapping); +return -ENOMEM ; + } +this_cpu_inc (nr_inodes);return 0 ;@@ -380,6 +449,7 @@ void __destroy_inode(struct inode *inode)if (inode->i_default_acl && !is_uncached_acl (inode->i_default_acl))posix_acl_release (inode->i_default_acl);#endif +free_mapping_i_mmap (&inode->i_data);this_cpu_dec (nr_inodes); }EXPORT_SYMBOL (__destroy_inode);@@ -480,9 +550,7 @@ EXPORT_SYMBOL(inc_nlink);static void __address_space_init_once (struct address_space *mapping) {xa_init_flags (&mapping->i_pages,XA_FLAGS_LOCK_IRQ |XA_FLAGS_ACCOUNT ); -init_rwsem (&mapping->i_mmap_rwsem);spin_lock_init (&mapping->i_private_lock); - mapping->i_mmap =RB_ROOT_CACHED ; }void address_space_init_once (struct address_space *mapping)@@ -2619,6 +2687,7 @@ void __init inode_init(void) &i_hash_mask,0 , 0); +init_split_tree_num (); }void init_special_inode (struct inode *inode,umode_t mode,dev_t rdev)include/linux/fs.h +11
-0@@ -450,6 +450,25 @@ struct mapping_metadata_bhs {struct list_head list;/* The list of bhs (b_assoc_buffers) */ }; +#ifdef CONFIG_SPLIT_I_MMAP +/* +* struct i_mmap_tree - A single sibling tree of the file's split i_mmap. +* @root: The red/black interval tree root. +* @rwsem: Protects insert/remove operations on this sibling tree. +* @vma_count: Number of VMAs in this sibling tree. +* +* When CONFIG_SPLIT_I_MMAP is enabled, the file's single i_mmap tree is +* split into split_tree_num sibling trees, each with its own lock. This +* reduces lock contention by allowing concurrent VMA insert/remove +* operations on different sibling trees.
Quoted 8 lines
include/linux/fs.h struct mapping_metadata_bhs { · line 468
Quoted 5 lines
include/linux/fs.h +5
-0+*/ +struct i_mmap_tree { +struct rb_root_cached root; +struct rw_semaphore rwsem; +atomic_t vma_count;I don't see what you need this
vma_countfor? I get the one inaddress_space, but this one does not seem useful.
For non-NUMA case, we can use it to determine which tree we should put the new VMA.
Round-robin is not good enough for a dynamic system.
Quoted 11 lines
Quoted 11 lines
include/linux/fs.h +3
-0+}; +#endif +/** * struct address_space - Contents of a cacheable, mappable object. * @host: Owner, either the inode or the block_device. @@ -461,8 +480,13 @@ struct mapping_metadata_bhs {* @gfp_mask: Memory allocation flags to use for allocating pages. * @i_mmap_writable: Number of VM_SHARED, VM_MAYWRITE mappings. * @nr_thps: Number of THPs in the pagecache (non-shmem only).
Quoted 8 lines
include/linux/fs.h struct mapping_metadata_bhs { · line 485
Quoted 5 lines
include/linux/fs.h +3
-2-* @i_mmap: Tree of private and shared mappings. -* @i_mmap_rwsem: Protects @i_mmap and @i_mmap_writable. +* @i_mmap: Tree of private and shared mappings. When CONFIG_SPLIT_I_MMAP +* is enabled, this is an array of split_tree_num struct i_mmap_tree +* pointers (plus a NULL terminator). NULL terminator wastes more memory, so I would really strongly avoid it as well.
any better idea?
Quoted 24 lines
include/linux/fs.h struct mapping_metadata_bhs { · line 489
Quoted 5 lines
include/linux/fs.h +4
-0+* @vma_count: Total number of VMAs across all sibling trees (only when +* CONFIG_SPLIT_I_MMAP is enabled). Used by mapping_mapped(). +* @i_mmap_rwsem: Protects @i_mmap and @i_mmap_writable (only when +* CONFIG_SPLIT_I_MMAP is disabled; otherwise per-tree rwsem is used). So, there are very good reasons why you still need an
i_mmap_rwsemprotecting state, even with split mmap trees. Which I'll go into later.Quoted 15 lines
include/linux/fs.h +6
-0* @nrpages: Number of page entries, protected by the i_pages lock. * @writeback_index: Writeback starts here. * @a_ops: Methods. @@ -480,14 +504,19 @@ struct address_space {/* number of thp, only for non-shmem files */ atomic_t nr_thps;#endif +#ifdef CONFIG_SPLIT_I_MMAP +struct i_mmap_tree **i_mmap; +atomic_t vma_count; +#else struct rb_root_cached i_mmap; +struct rw_semaphore i_mmap_rwsem; +#endif unsigned long nrpages;
Quoted 8 lines
include/linux/fs.h struct address_space { · line 490
Quoted 6 lines
include/linux/fs.h +0
-1pgoff_t writeback_index;const struct address_space_operations *a_ops;unsigned long flags;errseq_t wb_err;spinlock_t i_private_lock; -struct rw_semaphore i_mmap_rwsem;See
d3b1a9a778e1("fs/address_space: movei_mmap_rwsemto mitigate a false sharing withi_mmap.")
Got it.
Quoted 8 lines
Quoted 8 lines
include/linux/fs.h +0
-0} __attribute__((aligned(sizeof(long)))) __randomize_layout;/* * On most architectures that alignment is already the case; but @@ -508,6 +537,133 @@ static inline bool mapping_tagged(const struct address_space *mapping, xa_mark_treturn xa_marked (&mapping->i_pages, tag); }
Quoted 9 lines
include/linux/fs.h static inline bool mapping_tagged(const struct address_space *mapping, xa_mark_t · line 543
Quoted 4 lines
include/linux/fs.h +4
-0+#ifdef CONFIG_SPLIT_I_MMAP +static inline int mapping_mapped (const struct address_space *mapping) +{ +return atomic_read (&mapping->vma_count);Now that I think of it, I don't think we need
atomic_t, only unsigned long +READ_ONCE()suffices. Increments can race just fine, we don't expect any consistency there - if you want consistency you probably hold thei_mmaplock.
okay. I will check it.
Quoted 49 lines
Quoted 9 lines
include/linux/fs.h +9
-0+} + +static inline void inc_mapping_vma (struct address_space *mapping, +struct vm_area_struct *vma) +{ +struct i_mmap_tree *tree = mapping->i_mmap[vma->tree_idx]; + +atomic_inc (&tree->vma_count); +atomic_inc (&mapping->vma_count);include/linux/fs.h static inline bool mapping_tagged(const struct address_space *mapping, xa_mark_t · line 562
Quoted 10 lines
include/linux/fs.h +10
-0+} + +static inline void dec_mapping_vma (struct address_space *mapping, +struct vm_area_struct *vma) +{ +struct i_mmap_tree *tree = mapping->i_mmap[vma->tree_idx]; + +atomic_dec (&tree->vma_count); +atomic_dec (&mapping->vma_count); +}This probably shouldn't be in
linux/fs.h.Quoted 14 lines
include/linux/fs.h +14
-0+ +static inline struct rb_root_cached *get_i_mmap_root (struct address_space *mapping) +{ +return (struct rb_root_cached *)mapping->i_mmap; +} + +static inline void i_mmap_tree_lock_write (struct address_space *mapping, +struct vm_area_struct *vma) +{ +struct i_mmap_tree *tree = mapping->i_mmap[vma->tree_idx]; + +down_write (&tree->rwsem); +} +include/linux/fs.h static inline bool mapping_tagged(const struct address_space *mapping, xa_mark_t · line 586
Quoted 10 lines
include/linux/fs.h +10
-0+static inline void i_mmap_tree_unlock_write (struct address_space *mapping, +struct vm_area_struct *vma) +{ +struct i_mmap_tree *tree = mapping->i_mmap[vma->tree_idx]; + +up_write (&tree->rwsem); +} + +#define i_mmap_lock_write_prepare (mapping) +#define i_mmap_unlock_write_complete (mapping)It's unclear to me why you added
write_prepare()andwrite_complete().
Quoted 15 lines
include/linux/fs.h static inline bool mapping_tagged(const struct address_space *mapping, xa_mark_t · line 594
Quoted 8 lines
include/linux/fs.h +8
-0+ +extern int split_tree_num; +static inline void i_mmap_lock_write (struct address_space *mapping) +{ +int i; + +for (i =0 ; i < split_tree_num; i++) +down_write (&mapping->i_mmap[i]->rwsem);Oof, this is an incredibly large hammer. This is basically why I think keeping
i_mmap_rwsem(in a different form) is required. You do not want to take $nr_cpus locks (read _or_ write). For my design, I keepi_mmap_rwsem, but I invert its meaning - taking it in write = I'm reading from the tree; taking it in read = I'm writing to the tree. This provides some lighter-weight exclusion between rmap walks and rmap tree manipulation.
okay, it seem your method is better. I am waiting for your patch.
Quoted 127 lines
_Technically_, you shouldn't need to always take a lock when manipulating the tree. A pattern like mnt_hold_writers()/mnt_get_write_access() can probably work well. But it may be too complex ATM.
Also, note that you pretty much do not want
i_mmap_lock_write()users after the conversion is done.Quoted 118 lines
include/linux/fs.h +84
-0+} + +static inline int i_mmap_trylock_write (struct address_space *mapping) +{ +int i; + +for (i =0 ; i < split_tree_num; i++) { +if (!down_write_trylock (&mapping->i_mmap[i]->rwsem)) { +while (i--) +up_write (&mapping->i_mmap[i]->rwsem); +return 0 ; + } + } +return 1 ; +} + +static inline void i_mmap_unlock_write (struct address_space *mapping) +{ +int i; + +for (i =0 ; i < split_tree_num; i++) +up_write (&mapping->i_mmap[i]->rwsem); +} + +static inline int i_mmap_trylock_read (struct address_space *mapping) +{ +int i; + +for (i =0 ; i < split_tree_num; i++) { +if (!down_read_trylock (&mapping->i_mmap[i]->rwsem)) { +while (i--) +up_read (&mapping->i_mmap[i]->rwsem); +return 0 ; + } + } +return 1 ; +} + +static inline void i_mmap_lock_read (struct address_space *mapping) +{ +int i; + +for (i =0 ; i < split_tree_num; i++) +down_read (&mapping->i_mmap[i]->rwsem); +} + +static inline void i_mmap_unlock_read (struct address_space *mapping) +{ +int i; + +for (i =0 ; i < split_tree_num; i++) +up_read (&mapping->i_mmap[i]->rwsem); +} + +static inline void i_mmap_assert_locked (struct address_space *mapping) +{ +int i; + +for (i =0 ; i < split_tree_num; i++) +lockdep_assert_held (&mapping->i_mmap[i]->rwsem); +} + +static inline void i_mmap_assert_write_locked (struct address_space *mapping) +{ +int i; + +for (i =0 ; i < split_tree_num; i++) +lockdep_assert_held_write (&mapping->i_mmap[i]->rwsem); +} + +#else +static inline void i_mmap_lock_write (struct address_space *mapping) {down_write (&mapping->i_mmap_rwsem);@@ -561,6 +717,18 @@ static inline struct rb_root_cached *get_i_mmap_root(struct address_space *mappi return &mapping->i_mmap; } +static inline void inc_mapping_vma (struct address_space *mapping, +struct vm_area_struct *vma) { } +static inline void dec_mapping_vma (struct address_space *mapping, +struct vm_area_struct *vma) { } + +#define i_mmap_lock_write_prepare (mapping) i_mmap_lock_write(mapping) +#define i_mmap_unlock_write_complete (mapping) i_mmap_unlock_write(mapping) +#define i_mmap_tree_lock_write (mapping, vma) +#define i_mmap_tree_unlock_write (mapping, vma) + +#endif +/* * Might pages of this file have been modified in userspace? * Note that i_mmap_writable counts all VM_SHARED, VM_MAYWRITE vmas: do_mmap include/linux/mm.h +16
-0@@ -4041,11 +4041,91 @@ struct vm_area_struct *vma_interval_tree_iter_first(struct rb_root_cached *root,struct vm_area_struct *vma_interval_tree_iter_next (struct vm_area_struct *node,unsigned long start,unsigned long last); +#ifdef CONFIG_SPLIT_I_MMAP +extern int split_tree_num; + +static inline int smallest_tree_idx (struct file *file) +{ +struct address_space *mapping = file->f_mapping; +int tmp =INT_MAX , count; +int i, j =0 ; + +/* +* Since a not 100% accurate value is still okay, +* we do not need any lock here. +*/ +for (i =0 ; i < split_tree_num; i++) { + count =atomic_read (&mapping->i_mmap[i]->vma_count); +if (count < tmp) {
Quoted 8 lines
include/linux/mm.h struct vm_area_struct *vma_interval_tree_iter_first(struct rb_root_cached *root, · line 4065
Quoted 6 lines
include/linux/mm.h +6
-0+ j = i; + tmp = count; +if (!tmp) +break ; + } + }Ohh, I see why you want the per-subtree
vma_countnow. But is this a net-win?
It keep the trees as even as possible.
Quoted 75 lines
I think doing something like vma-pointer-hashing or just
smp_processor_id()would work a-ok.Quoted 72 lines
include/linux/mm.h +58
-0+return j; +} + +static inline void vma_set_tree_idx (struct vm_area_struct *vma) +{ +#ifdef CONFIG_NUMA + vma->tree_idx =numa_node_id (); +#else + vma->tree_idx =smallest_tree_idx (vma->vm_file); +#endif +} + +static inline struct rb_root_cached *get_rb_root (struct vm_area_struct *vma, +struct address_space *mapping) +{ +return &mapping->i_mmap[vma->tree_idx]->root; +} + +/* Find the first valid VMA in the sibling trees */ +static inline struct vm_area_struct *first_vma (struct i_mmap_tree ***__r, +unsigned long start,unsigned long last) +{ +struct vm_area_struct *vma =NULL ; +struct i_mmap_tree **tree = *__r; +struct rb_root_cached *root; + +while (*tree) { + root = &(*tree)->root; + tree++; + vma =vma_interval_tree_iter_first (root, start, last); +if (vma) +break ; + } + +/* Save for the next loop */ + *__r = tree; +return vma; +} + +/* +* Please use get_i_mmap_root() to get the @root. +* @_tmp is referenced to avoid unused variable warning. +*/ +#define vma_interval_tree_foreach (vma, root, start, last) \ + for (struct i_mmap_tree **_r = (struct i_mmap_tree **)(root), \ + **_tmp = (vma = first_vma(&_r, start, last)) ? _r : NULL;\ + ((_tmp && vma) || (vma = first_vma(&_r, start, last))); \ + vma = vma_interval_tree_iter_next(vma, start, last)) +#else /* Please use get_i_mmap_root() to get the @root */ #define vma_interval_tree_foreach (vma, root, start, last) \ for (vma = vma_interval_tree_iter_first(root, start, last); \ vma; vma = vma_interval_tree_iter_next(vma, start, last)) +static inline void vma_set_tree_idx (struct vm_area_struct *vma) { } + +static inline struct rb_root_cached *get_rb_root (struct vm_area_struct *vma, +struct address_space *mapping) +{ +return &mapping->i_mmap; +} +#endif +void anon_vma_interval_tree_insert (struct anon_vma_chain *node,struct rb_root_cached *root); void anon_vma_interval_tree_remove(struct anon_vma_chain *node,include/linux/mm_types.h +0
-0@@ -1072,6 +1072,9 @@ struct vm_area_struct {#ifdef __HAVE_PFNMAP_TRACKING
Quoted 8 lines
include/linux/mm_types.h struct vm_area_struct { · line 1077
Quoted 5 lines
include/linux/mm_types.h +3
-0struct pfnmap_track_ctx *pfnmap_track_ctx;#endif +#ifdef CONFIG_SPLIT_I_MMAP +int tree_idx;/* The sibling tree index for the VMA */ +#endif FTR the struct hole isn't here, but right after
vm_lock_seqorvm_refcntin most configs.
okay, thanks.
I did not notice the struct hole issue.
Quoted 25 lines
Quoted 25 lines
include/linux/mm_types.h +0
-0} __randomize_layout;/* Clears all bits in the VMA flags bitmap, non-atomically. */ mm/internal.h +2
-1@@ -1888,7 +1888,8 @@ static inline void maybe_rmap_unlock_action(struct vm_area_struct *vma,VM_WARN_ON_ONCE (vma_is_anonymous (vma)); file = vma->vm_file; -i_mmap_unlock_write (file->f_mapping); +i_mmap_tree_unlock_write (file->f_mapping, vma); +i_mmap_unlock_write_complete (file->f_mapping); action->hide_from_rmap_until_complete = false; }mm/mmap.c +0
-0@@ -1825,15 +1825,20 @@ __latent_entropy int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)struct address_space *mapping = file->f_mapping;
Quoted 15 lines
mm/mmap.c __latent_entropy int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm) · line 1837
Quoted 13 lines
mm/mmap.c +5
-2get_file (file); -i_mmap_lock_write (mapping); +i_mmap_lock_write_prepare (mapping); +i_mmap_tree_lock_write (mapping, mpnt); +if (vma_is_shared_maywrite (tmp))mapping_allow_writable (mapping);flush_dcache_mmap_lock (mapping);/* insert tmp into the share list, just after mpnt */ vma_interval_tree_insert_after (tmp, mpnt, -get_i_mmap_root (mapping)); +get_rb_root (mpnt, mapping)); +inc_mapping_vma (mapping, tmp);Honestly, would prefer to hide all of these details from mmap.
yes, we can.
But we need to change the functions in mm/interval_tree.c
Quoted 87 lines
Quoted 54 lines
mm/mmap.c +3
-1flush_dcache_mmap_unlock (mapping); -i_mmap_unlock_write (mapping); + +i_mmap_tree_unlock_write (mapping, mpnt); +i_mmap_unlock_write_complete (mapping); }if (!(tmp->vm_flags &VM_WIPEONFORK ))mm/nommu.c +16
-6@@ -567,11 +567,16 @@ static void setup_vma_to_mm(struct vm_area_struct *vma, struct mm_struct *mm)if (vma->vm_file) {struct address_space *mapping = vma->vm_file->f_mapping; -i_mmap_lock_write (mapping); +i_mmap_lock_write_prepare (mapping); +i_mmap_tree_lock_write (mapping, vma); +flush_dcache_mmap_lock (mapping); -vma_interval_tree_insert (vma,get_i_mmap_root (mapping)); +vma_interval_tree_insert (vma,get_rb_root (vma, mapping)); +inc_mapping_vma (mapping, vma);flush_dcache_mmap_unlock (mapping); -i_mmap_unlock_write (mapping); + +i_mmap_tree_unlock_write (mapping, vma); +i_mmap_unlock_write_complete (mapping); } }@@ -583,11 +588,16 @@ static void cleanup_vma_from_mm(struct vm_area_struct *vma)struct address_space *mapping; mapping = vma->vm_file->f_mapping; -i_mmap_lock_write (mapping); +i_mmap_lock_write_prepare (mapping); +i_mmap_tree_lock_write (mapping, vma); +flush_dcache_mmap_lock (mapping); -vma_interval_tree_remove (vma,get_i_mmap_root (mapping)); +vma_interval_tree_remove (vma,get_rb_root (vma, mapping)); +dec_mapping_vma (mapping, vma);flush_dcache_mmap_unlock (mapping); -i_mmap_unlock_write (mapping); + +i_mmap_tree_unlock_write (mapping, vma); +i_mmap_unlock_write_complete (mapping); } }mm/nommu.c unsigned long do_mmap(struct file *file, · line 1076
Quoted 4 lines
mm/nommu.c +1
-0@@ -1063,6 +1073,7 @@ unsigned long do_mmap(struct file *file,if (file) { region->vm_file =get_file (file); vma->vm_file =get_file (file); +vma_set_tree_idx (vma);This is unrelated, shouldn't be done here.
mm/pagewalk.c int walk_page_mapping(struct address_space *mapping, pgoff_t first_index, · line 812
Quoted 13 lines
mm/nommu.c +0
-0}down_write (&nommu_region_sem);mm/pagewalk.c +1
-1@@ -809,7 +809,7 @@ int walk_page_mapping(struct address_space *mapping, pgoff_t first_index,if (!check_ops_safe (ops))return -EINVAL ; -lockdep_assert_held (&mapping->i_mmap_rwsem); +i_mmap_assert_locked (mapping);This kind of conversion should be done in a separate step.
Quoted 10 lines
mm/pagewalk.c +0
-0vma_interval_tree_foreach (vma,get_i_mmap_root (mapping), first_index, first_index + nr - 1) {/* Clip to the vma */ mm/vma.c +0
-0@@ -234,22 +234,23 @@ static void __vma_link_file(struct vm_area_struct *vma,mapping_allow_writable (mapping);
Quoted 6 lines
mm/vma.c static void __vma_link_file(struct vm_area_struct *vma, · line 238
Quoted 4 lines
mm/vma.c +2
-1flush_dcache_mmap_lock (mapping); -vma_interval_tree_insert (vma,get_i_mmap_root (mapping)); +vma_interval_tree_insert (vma,get_rb_root (vma, mapping)); +inc_mapping_vma (mapping, vma);
inc_mapping_vma()should probably be done implicitly by insertion?
Yes, we can.
It is more grace to hide it in vma_interval_tree_insert.
Quoted 125 lines
Quoted 125 lines
mm/vma.c +32
-17flush_dcache_mmap_unlock (mapping); } -/* -* Requires inode->i_mapping->i_mmap_rwsem -*/ static void __remove_shared_vm_struct (struct vm_area_struct *vma,struct address_space *mapping) { +i_mmap_tree_lock_write (mapping, vma);if (vma_is_shared_maywrite (vma))mapping_unmap_writable (mapping);flush_dcache_mmap_lock (mapping); -vma_interval_tree_remove (vma,get_i_mmap_root (mapping)); +vma_interval_tree_remove (vma,get_rb_root (vma, mapping)); +dec_mapping_vma (mapping, vma);flush_dcache_mmap_unlock (mapping); +i_mmap_tree_unlock_write (mapping, vma); }/* @@ -297,8 +298,9 @@ static void vma_prepare(struct vma_prepare *vp)uprobe_munmap (vp->adj_next, vp->adj_next->vm_start, vp->adj_next->vm_end); -i_mmap_lock_write (vp->mapping); +i_mmap_lock_write_prepare (vp->mapping);if (vp->insert && vp->insert->vm_file) { +i_mmap_tree_lock_write (vp->mapping, vp->insert);/* * Put into interval tree now, so instantiated pages * are visible to arm/parisc __flush_dcache_page @@ -307,6 +309,7 @@ static void vma_prepare(struct vma_prepare *vp)*/ __vma_link_file (vp->insert, vp->insert->vm_file->f_mapping); +i_mmap_tree_unlock_write (vp->mapping, vp->insert); } }@@ -318,12 +321,17 @@ static void vma_prepare(struct vma_prepare *vp) }if (vp->file) { +i_mmap_tree_lock_write (vp->mapping, vp->vma);flush_dcache_mmap_lock (vp->mapping);vma_interval_tree_remove (vp->vma, -get_i_mmap_root (vp->mapping)); -if (vp->adj_next) +get_rb_root (vp->vma, vp->mapping)); +dec_mapping_vma (vp->mapping, vp->vma); +if (vp->adj_next) { +i_mmap_tree_lock_write (vp->mapping, vp->adj_next);vma_interval_tree_remove (vp->adj_next, -get_i_mmap_root (vp->mapping)); +get_rb_root (vp->adj_next, vp->mapping)); +dec_mapping_vma (vp->mapping, vp->adj_next); + } } }@@ -340,12 +348,17 @@ static void vma_complete(struct vma_prepare *vp, struct vma_iterator *vmi, struct mm_struct *mm) {if (vp->file) { -if (vp->adj_next) +if (vp->adj_next) {vma_interval_tree_insert (vp->adj_next, -get_i_mmap_root (vp->mapping)); +get_rb_root (vp->adj_next, vp->mapping)); +inc_mapping_vma (vp->mapping, vp->adj_next); +i_mmap_tree_unlock_write (vp->mapping, vp->adj_next); + }vma_interval_tree_insert (vp->vma, -get_i_mmap_root (vp->mapping)); +get_rb_root (vp->vma, vp->mapping)); +inc_mapping_vma (vp->mapping, vp->vma);flush_dcache_mmap_unlock (vp->mapping); +i_mmap_tree_unlock_write (vp->mapping, vp->vma); }if (vp->remove && vp->file) {@@ -370,7 +383,7 @@ static void vma_complete(struct vma_prepare *vp, struct vma_iterator *vmi, }if (vp->file) { -i_mmap_unlock_write (vp->mapping); +i_mmap_unlock_write_complete (vp->mapping);if (!vp->skip_vma_uprobe) {uprobe_mmap (vp->vma);@@ -1799,12 +1812,12 @@ static void unlink_file_vma_batch_process(struct unlink_vma_file_batch *vb)int i; mapping = vb->vmas[0 ]->vm_file->f_mapping; -i_mmap_lock_write (mapping); +i_mmap_lock_write_prepare (mapping);for (i =0 ; i < vb->count; i++) {VM_WARN_ON_ONCE (vb->vmas[i]->vm_file->f_mapping != mapping);__remove_shared_vm_struct (vb->vmas[i], mapping); } -i_mmap_unlock_write (mapping); +i_mmap_unlock_write_complete (mapping);unlink_file_vma_batch_init (vb); }@@ -1836,10 +1849,13 @@ static void vma_link_file(struct vm_area_struct *vma, bool hold_rmap_lock)if (file) { mapping = file->f_mapping; -i_mmap_lock_write (mapping); +i_mmap_lock_write_prepare (mapping); +i_mmap_tree_lock_write (mapping, vma);__vma_link_file (vma, mapping); -if (!hold_rmap_lock) -i_mmap_unlock_write (mapping); +if (!hold_rmap_lock) { +i_mmap_tree_unlock_write (mapping, vma); +i_mmap_unlock_write_complete (mapping); + } } }
Quoted 6 lines
mm/vma.c static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma) · line 2181
Quoted 3 lines
mm/vma.c +0
-0@@ -2164,6 +2180,23 @@ static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma) } }I can but hope that all of the above is quite simplified before we get to the "making file rmap more complicated" bit.
:(
If we can do not care about the ARM device, we can make it simple.
Thanks
Huang Shijie
PATCH v2 4/4 docs/mm: update document for split i_mmap tree
In reply to Huang ShijieDocument the i_mmap locking changes introduced by the following patches:
- Use mapping_mapped() to simplify the code
- Use get_i_mmap_root() to access the file's i_mmap
- Split the file's i_mmap tree (CONFIG_SPLIT_I_MMAP)
Add documentation for:
- CONFIG_SPLIT_I_MMAP split i_mmap tree architecture with per-tree locks
- New per-tree lock helpers: i_mmap_tree_lock_write/unlock_write
- New vm_area_struct.tree_idx field for sibling tree selection
- Updated i_mmap_lock_read/write semantics acquiring all per-tree locks
- Updated lock ordering notes for split tree configuration
- Updated page table freeing section for split tree scenario
--- Documentation/mm/process_addrs.rst | 63 +++++++++++++++++++++++------- 1 file changed, 49 insertions(+), 14 deletions(-)
Documentation/mm/process_addrs.rst +49 -14
@@ -60,6 +60,15 @@ Terminology :c:func:`!i_mmap_[try]lock_write` for file-backed memory. We refer to these locks as the reverse mapping locks, or 'rmap locks' for brevity. + When :c:macro:`!CONFIG_SPLIT_I_MMAP` is enabled, the file-backed i_mmap tree + is split into multiple sibling trees (one per NUMA node or a number based on + CPU count), each with its own :c:type:`!struct i_mmap_tree` containing a + red/black interval tree and a :c:type:`!struct rw_semaphore`. In this + configuration, :c:func:`!i_mmap_lock_read` and :c:func:`!i_mmap_lock_write` + acquire all per-tree locks, while VMA insert/remove operations use the + per-tree granularity :c:func:`!i_mmap_tree_lock_write` to lock only the + relevant sibling tree, significantly reducing lock contention. + We discuss page table locks separately in the dedicated section below. The first thing **any** of these locks achieve is to **stabilise** the VMA
@@ -230,12 +239,16 @@ These are the core fields which describe the MM the VMA belongs to and its attri Updated under mmap read lock by :c:func:`!task_numa_work`. :c:member:`!vm_userfaultfd_ctx` CONFIG_USERFAULTFD Userfaultfd context wrapper object of mmap write, - type :c:type:`!vm_userfaultfd_ctx`, VMA write. - either of zero size if userfaultfd is - disabled, or containing a pointer - to an underlying - :c:type:`!userfaultfd_ctx` object which - describes userfaultfd metadata. + type :c:type:`!vm_userfaultfd_ctx`, VMA write. + either of zero size if userfaultfd is + disabled, or containing a pointer + to an underlying + :c:type:`!userfaultfd_ctx` object which + describes userfaultfd metadata. + :c:member:`!tree_idx` CONFIG_SPLIT_I_MMAP The index of the sibling i_mmap tree Written once on + that this VMA belongs to, set at initial map. + VMA creation time based on the NUMA + node or the smallest sibling tree. ================================= ===================== ======================================== =============== These fields are present or not depending on whether the relevant kernel
@@ -247,12 +260,18 @@ configuration option is set. Field Description Write lock =================================== ========================================= ============================ :c:member:`!shared.rb` A red/black tree node used, if the mmap write, VMA write, - mapping is file-backed, to place the VMA i_mmap write. - in the - :c:member:`!struct address_space->i_mmap` - red/black interval tree. + mapping is file-backed, to place the VMA i_mmap write (or per-tree + in the i_mmap write when + :c:member:`!struct address_space->i_mmap` :c:macro:`!CONFIG_SPLIT_I_MMAP` + red/black interval tree (or one of the is set). + sibling trees when + :c:macro:`!CONFIG_SPLIT_I_MMAP` + is enabled). :c:member:`!shared.rb_subtree_last` Metadata used for management of the mmap write, VMA write, - interval tree if the VMA is file-backed. i_mmap write. + interval tree if the VMA is file-backed. i_mmap write (or per-tree + i_mmap write when + :c:macro:`!CONFIG_SPLIT_I_MMAP` + is set). :c:member:`!anon_vma_chain` List of pointers to both forked/CoW’d mmap read, anon_vma write. :c:type:`!anon_vma` objects and :c:member:`!vma->anon_vma` if it is
@@ -490,6 +509,16 @@ There is also a file-system specific lock ordering comment located at the top of Please check the current state of these comments which may have changed since the time of writing of this document. +.. note:: When :c:macro:`!CONFIG_SPLIT_I_MMAP` is enabled, the single + ``mapping->i_mmap_rwsem`` is replaced by an array of per-tree locks + ``mapping->i_mmap[i]->rwsem``. The lock ordering positions of + ``mapping->i_mmap_rwsem`` above apply to each per-tree lock + equivalently. VMA insert/remove operations acquire only the relevant + per-tree lock via :c:func:`!i_mmap_tree_lock_write`, while operations + that require all trees to be locked (such as + :c:func:`!unmap_mapping_range`) acquire all per-tree locks via + :c:func:`!i_mmap_lock_write` or :c:func:`!i_mmap_lock_read`. + ------------------------------ Locking Implementation Details ------------------------------
@@ -704,11 +733,15 @@ traversed or referenced by concurrent tasks. It is insufficient to simply hold an mmap write lock and VMA lock (which will prevent racing faults, and rmap operations), as a file-backed mapping can be -truncated under the :c:struct:`!struct address_space->i_mmap_rwsem` alone. +truncated under the :c:struct:`!struct address_space->i_mmap_rwsem` alone +(or, when :c:macro:`!CONFIG_SPLIT_I_MMAP` is enabled, under all per-tree +``mapping->i_mmap[i]->rwsem`` locks acquired via +:c:func:`!i_mmap_lock_write`). As a result, no VMA which can be accessed via the reverse mapping (either through the :c:struct:`!struct anon_vma->rb_root` or the :c:member:`!struct -address_space->i_mmap` interval trees) can have its page tables torn down. +address_space->i_mmap` interval trees, or the sibling trees when +:c:macro:`!CONFIG_SPLIT_I_MMAP` is enabled) can have its page tables torn down. The operation is typically performed via :c:func:`!free_pgtables`, which assumes either the mmap write lock has been taken (as specified by its
@@ -729,7 +762,9 @@ cleared without page table locks (in the :c:func:`!pgd_clear`, :c:func:`!p4d_cle .. note:: It is possible for leaf page tables to be torn down independent of the page tables above it as is done by :c:func:`!retract_page_tables`, which is performed under the i_mmap - read lock, PMD, and PTE page table locks, without this level of care. + read lock (or all per-tree ``mapping->i_mmap[i]->rwsem`` locks in + read mode when :c:macro:`!CONFIG_SPLIT_I_MMAP` is enabled), PMD, and + PTE page table locks, without this level of care. Page table moving ^^^^^^^^^^^^^^^^^
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v2 4/4] docs/mm: update document for split i_mmap tree
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The commit message claims to document the `i_mmap_tree_unlock_write` helper, but it is…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The commit message claims to document the i_mmap_tree_unlock_write helper, but it is completely omitted from the documentation text changes. --
commit d14051a8e507a6d5364fa4f0f61edf41becf1526
Author: Huang Shijie <[email protected]>
docs/mm: update document for split i_mmap tree
This commit updates the documentation to reflect i_mmap locking changes. It details the split i_mmap tree architecture, new per-tree lock helpers, and updated lock ordering and page table freeing semantics.
Quoted 3 lines
Add documentation for:
-CONFIG_SPLIT_I_MMAPspliti_mmaptree architecture with per-tree locks
- New per-tree lock helpers: i_mmap_tree_lock_write/unlock_write
[Severity: Medium]
Does the commit message accurately reflect the changes in the patch?
The message mentions adding documentation for the unlock helper, but looking at the updated text in the patch, it appears to be missing.
Quoted 4 lines
Documentation/mm/process_addrs.rst +0
-0
[ ... ]
Documentation/mm/process_addrs.rst Terminology · line 70
Quoted 12 lines
Documentation/mm/process_addrs.rst +8
-0@@ -60,6 +60,15 @@ Terminology :c:func:`!i_mmap_[try]lock_write` for file-backed memory. We refer to these locks as the reverse mapping locks, or 'rmap locks' for brevity. + When :c:macro:`!CONFIG_SPLIT_I_MMAP` is enabled, the file-backed i_mmap tree + is split into multiple sibling trees (one per NUMA node or a number based on + CPU count), each with its own :c:type:`!struct i_mmap_tree` containing a + red/black interval tree and a :c:type:`!struct rw_semaphore`. In this + configuration, :c:func:`!i_mmap_lock_read` and :c:func:`!i_mmap_lock_write` + acquire all per-tree locks, while VMA insert/remove operations use the + per-tree granularity :c:func:`!i_mmap_tree_lock_write` to lock only the + relevant sibling tree, significantly reducing lock contention.
[Severity: Medium]
Was the documentation for i_mmap_tree_unlock_write accidentally omitted from this section? The text explains the locking side but does not mention the corresponding unlock function.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Lorenzo Stoakes
maintainerHi Huang,
You seem to be replacing the file rmap altogether here, so you really ought to have sent this as an RFC so we could discuss it as a community first.
Especially so as Pedro had publicly mentioned his plans to implement something similar here, so coordination would have been appreciated.
Anyway, as Pedro has pointed out, the code is overly complicated, it's far too configurable (not always a good thing), and the locking implementation is questionable.
You seem to be adding a whole bunch of open-coded complexity too, which is not something we want. Abstraction is key for the rmap.
You're also not adding any kdoc comments or really many comments at all, and you've not added any tests (though perhaps it's difficult given how core this is).
So I would suggest that perhaps any respin should be sent as an RFC so we can engage in that conversation and ensure we're all on the same page?
Especially since Pedro plans to send an alternative, simpler, solution I believe.
It's also not helpful that you haven't examined the non-NUMA case :) perhaps your particular server behaves a certain way that this approach aids, but regresses other NUMA configurations?
We'd really need to be sure of this before accepting invasive changes like this.
Thanks, Lorenzo
Quoted 10 lines · Huang Shijie
In NUMA, there are maybe many NUMA nodes and many CPUs.
For example, a Hygon's server has 12 NUMA nodes, and 384 CPUs. In the UnixBench tests, there is a test "execl" which tests the execve system call.When we test our server with "./Run -c 384 execl",
the test result is not good enough. Thei_mmaplocks contended heavily on "libc.so" and "ld.so". For example, thei_mmaptree for "libc.so" can have over 6000 VMAs, all the VMAs can be in different NUMA mode. The insert/remove operations do not run quickly enough.
You really need to send detailed, statistically valid numbers across different NUMA configurations for changes like this to be considered.
Quoted 7 lines
patch 1 & patch 2 are try to hide the direct access of
i_mmap. patch 3 splits thei_mmapinto sibling trees, each tree has separate lock, and we can get better performance with this patch set in our NUMA server:
we can get over 400% performance improvement.I did not test the non-NUMA case, since I do not have such server.
Yeah this isn't a great thing to hear :) you need to demonstrate this doesn't regress non-NUMA machines or NUMA machines of a different configuration.
Quoted 28 lines
v1 --> v2:
Not only split the immap tree, but also split the lock. v1 : https://lkml.org/lkml/2026/4/13/199Huang Shijie (4):
mm: usemapping_mappedto simplify the code
mm: useget_i_mmap_rootto access the file'si_mmapmm/fs: split the file'si_mmaptree docs/mm: update document for spliti_mmaptreeDocumentation/mm/process_addrs.rst | 63 +++++++--- arch/arm/mm/fault-armv.c | 3 +- arch/arm/mm/flush.c | 3 +- arch/nios2/mm/cacheflush.c | 3 +- arch/parisc/kernel/cache.c | 4 +- fs/Kconfig | 8 ++ fs/dax.c | 3 +- fs/hugetlbfs/inode.c | 30 +++-- fs/inode.c | 75 +++++++++++- include/linux/fs.h | 179 ++++++++++++++++++++++++++++- include/linux/mm.h | 81 +++++++++++++ include/linux/mm_types.h | 3 + kernel/events/uprobes.c | 3 +- mm/hugetlb.c | 7 +- mm/internal.h | 3 +- mm/khugepaged.c | 6 +- mm/memory-failure.c | 8 +-
Quoted 8 lines
mm/memory.c | 8 +- mm/mmap.c | 11 +- mm/nommu.c | 28 +++-- mm/pagewalk.c | 4 +- mm/rmap.c | 2 +- mm/vma.c | 74 +++++++++--- mm/vma_init.c | 3 + 24 files changed, 534 insertions(+), 78 deletions(-)
This is a _lot_ of changes you're making here. It therefore feels like the abstraction is broken somewhat?
Quoted 5 lines
--
2.53.0
Thanks, Lorenzo
Quoted 4 lines · Lorenzo Stoakes
Hi Huang,
You seem to be replacing the file rmap altogether here, so you really ought to have sent this as an RFC so we could discuss it as a community first.
No problem.
Quoted 3 lines
Especially so as Pedro had publicly mentioned his plans to implement something similar here, so coordination would have been appreciated.
Yes. I am very happy to work with Pedro.
Quoted 4 lines
Anyway, as Pedro has pointed out, the code is overly complicated, it's far too configurable (not always a good thing), and the locking implementation is questionable.
I can make the code more simple. :)
Quoted 7 lines
You seem to be adding a whole bunch of open-coded complexity too, which is not something we want. Abstraction is key for the rmap.
You're also not adding any kdoc comments or really many comments at all, and you've not added any tests (though perhaps it's difficult given how core this is).
Got it.
Quoted 10 lines
So I would suggest that perhaps any respin should be sent as an RFC so we can engage in that conversation and ensure we're all on the same page?
Especially since Pedro plans to send an alternative, simpler, solution I believe.
It's also not helpful that you haven't examined the non-NUMA case :) perhaps your particular server behaves a certain way that this approach aids, but regresses other NUMA configurations?
emm. I ever hoped someone can help me to test this patch set on the non-NUMA server.
It seems I should find some non-NUMA server before I send out the patch set. :)
Quoted 3 lines
We'd really need to be sure of this before accepting invasive changes like this.
Okay.
Thanks
Huang Shijie
syzbot ci has tested the following series
syzbot ci has tested the following series
[v2] mm: split the file's i_mmap tree for NUMA
https://lore.kernel.org/all/[email protected]
* [PATCH v2 1/4] mm: use mapping_mapped to simplify the code
* [PATCH v2 2/4] mm: use get_i_mmap_root to access the file's i_mmap
* [PATCH v2 3/4] mm/fs: split the file's i_mmap tree
* [PATCH v2 4/4] docs/mm: update document for split i_mmap tree
and found the following issue:
INFO: trying to register non-static key in do_one_initcall
Full report is available here:
https://ci.syzbot.org/series/a9bada61-06e7-40d5-b423-5f2d69a60209
***
INFO: trying to register non-static key in do_one_initcall
tree: linux-next URL: https://kernel.googlesource.com/pub/scm/linux/kernel/git/next/linux-next base: 14546c7bef6c1036fc82e36c1a200b0caccd339a arch: amd64 compiler: Debian clang version 21.1.8 (++20251221033036+2078da43e25a-1~exp1~20251221153213.50), Debian LLD 21.1.8 config: https://ci.syzbot.org/builds/2f92f704-660a-4108-9172-7e620e10ce46/config
acpi PNP0A08:00: _OSC: platform does not support [PCIeHotplug LTR] acpi PNP0A08:00: _OSC: OS now controls [PME AER PCIeCapability] PCI host bridge to bus 0000:00pci_bus 0000:00: Unknown NUMA node; performance will be reduced pci_bus 0000:00: root bus resource [io 0x0000-0x0cf7 window] pci_bus 0000:00: root bus resource [io 0x0d00-0xffff window] pci_bus 0000:00: root bus resource [mem 0x000a0000-0x000bffff window] pci_bus 0000:00: root bus resource [mem 0x80000000-0xafffffff window] pci_bus 0000:00: root bus resource [mem 0xc0000000-0xfebfffff window] pci_bus 0000:00: root bus resource [mem 0x240000000-0xa3fffffff window] pci_bus 0000:00: root bus resource [bus 00-ff]
pci 0000:00:00.0: [8086:29c0] type 00 class 0x060000 conventional PCI endpoint pci 0000:00:01.0: [1234:1111] type 00 class 0x030000 conventional PCI endpoint pci 0000:00:01.0: BAR 0 [mem 0xfd000000-0xfdffffff pref]
pci 0000:00:01.0: BAR 2 [mem 0xfebf0000-0xfebf0fff]
pci 0000:00:01.0: ROM [mem 0xfebe0000-0xfebeffff pref]
pci 0000:00:01.0: Video device with shadowed ROM at [mem 0x000c0000-0x000dffff] pci 0000:00:02.0: [1af4:1005] type 00 class 0x00ff00 conventional PCI endpoint pci 0000:00:02.0: BAR 0 [io 0xc080-0xc09f]
pci 0000:00:02.0: BAR 1 [mem 0xfebf1000-0xfebf1fff]
pci 0000:00:02.0: BAR 4 [mem 0xfe000000-0xfe003fff 64bit pref]
pci 0000:00:03.0: [8086:100e] type 00 class 0x020000 conventional PCI endpoint pci 0000:00:03.0: BAR 0 [mem 0xfebc0000-0xfebdffff]
pci 0000:00:03.0: BAR 1 [io 0xc000-0xc03f]
pci 0000:00:03.0: ROM [mem 0xfeb80000-0xfebbffff pref]
pci 0000:00:1f.0: [8086:2918] type 00 class 0x060100 conventional PCI endpoint pci 0000:00:1f.0: quirk: [io 0x0600-0x067f] claimed by ICH6 ACPI/GPIO/TCO pci 0000:00:1f.2: [8086:2922] type 00 class 0x010601 conventional PCI endpoint pci 0000:00:1f.2: BAR 4 [io 0xc0a0-0xc0bf]
pci 0000:00:1f.2: BAR 5 [mem 0xfebf2000-0xfebf2fff]
pci 0000:00:1f.3: [8086:2930] type 00 class 0x0c0500 conventional PCI endpoint pci 0000:00:1f.3: BAR 4 [io 0x0700-0x073f]
ACPI: PCI: Interrupt link LNKA configured for IRQ 10
ACPI: PCI: Interrupt link LNKB configured for IRQ 10
ACPI: PCI: Interrupt link LNKC configured for IRQ 11
ACPI: PCI: Interrupt link LNKD configured for IRQ 11
ACPI: PCI: Interrupt link LNKE configured for IRQ 10
ACPI: PCI: Interrupt link LNKF configured for IRQ 10
ACPI: PCI: Interrupt link LNKG configured for IRQ 11
ACPI: PCI: Interrupt link LNKH configured for IRQ 11
ACPI: PCI: Interrupt link GSIA configured for IRQ 16
ACPI: PCI: Interrupt link GSIB configured for IRQ 17
ACPI: PCI: Interrupt link GSIC configured for IRQ 18
ACPI: PCI: Interrupt link GSID configured for IRQ 19
ACPI: PCI: Interrupt link GSIE configured for IRQ 20
ACPI: PCI: Interrupt link GSIF configured for IRQ 21
ACPI: PCI: Interrupt link GSIG configured for IRQ 22
ACPI: PCI: Interrupt link GSIH configured for IRQ 23
iommu: Default domain type: Translated
iommu: DMA domain TLB invalidation policy: lazy mode
INFO: trying to register non-static key.
The code is fine but needs lockdep annotation, or maybe
you didn't initialize this object before use?
turning off the locking correctness validator.
CPU: 0 UID: 0 PID: 1 Comm: swapper/0 Not tainted syzkaller #0 PREEMPT(full) Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.2-debian-1.16.2-1 04/01/2014 Call Trace: <TASK> dump_stack_lvl+0xe8/0x150 assign_lock_key+0x133/0x150 register_lock_class+0xcc/0x2e0 __lock_acquire+0xad/0x2cf0 lock_acquire+0x106/0x350 down_write+0x96/0x200 dma_resv_lockdep+0x39c/0x660 do_one_initcall+0x250/0x870 do_initcall_level+0x104/0x190 do_initcalls+0x59/0xa0 kernel_init_freeable+0x2a6/0x3e0 kernel_init+0x1d/0x1d0 ret_from_fork+0x514/0xb70 ret_from_fork_asm+0x1a/0x30 </TASK> ------------[ cut here ]------------ DEBUG_RWSEMS_WARN_ON(sem->magic != sem): count = 0x1, magic = 0x0, owner = 0xffff888102a95940, curr 0xffff888102a95940, list not empty WARNING: kernel/locking/rwsem.c:1405 at up_write+0x1e2/0x410, CPU#0: swapper/0/1 Modules linked in: CPU: 0 UID: 0 PID: 1 Comm: swapper/0 Not tainted syzkaller #0 PREEMPT(full) Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.2-debian-1.16.2-1 04/01/2014 RIP: 0010:up_write+0x2b1/0x410 Code: c0 c0 e6 cc 8b 49 c7 c2 a0 e6 cc 8b 4c 0f 44 d0 48 8b 7c 24 10 48 c7 c6 40 e8 cc 8b 48 8b 54 24 08 48 8b 0c 24 4d 89 f9 41 52 <67> 48 0f b9 3a 48 83 c4 08 e8 21 1f 0d 03 e9 b2 fd ff ff 90 0f 0b RSP: 0000:ffffc90000067480 EFLAGS: 00010246 RAX: ffffffff8bcce6c0 RBX: ffffc900000677d0 RCX: 0000000000000000 RDX: 0000000000000001 RSI: ffffffff8bcce840 RDI: ffffffff90338290 RBP: ffffc90000067830 R08: ffff888102a95940 R09: ffff888102a95940 R10: ffffffff8bcce6c0 R11: fffff5200000cefc R12: ffffc90000067828 R13: dffffc0000000000 R14: 1ffff9200000cf06 R15: ffff888102a95940 FS: 0000000000000000(0000) GS:ffff88818dc9e000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: ffff88823ffff000 CR3: 000000000e74a000 CR4: 00000000000006f0 Call Trace: <TASK> dma_resv_lockdep+0x3a4/0x660 do_one_initcall+0x250/0x870 do_initcall_level+0x104/0x190 do_initcalls+0x59/0xa0 kernel_init_freeable+0x2a6/0x3e0 kernel_init+0x1d/0x1d0 ret_from_fork+0x514/0xb70 ret_from_fork_asm+0x1a/0x30 </TASK>
***
If these findings have caused you to resend the series or submit a separate fix, please add the following tag to your commit message:
---
This report is generated by a bot. It may contain errors. syzbot ci engineers can be reached at [email protected].
To test a patch for this bug, please reply with #syz test (should be on a separate line).
The patch should be attached to the email.
Note: arguments like custom git repos and branches are not supported.