mm/vma: don't remove VMA from rmap if pgoff unchanged
Subsystems (4)
Lorenzo Stoakes (ARM)
maintainerPATCH v2 1/1 mm/vma: don't remove VMA from rmap if pgoff unchanged
When updating a VMA, vma_prepare() unconditionally removes it from its rmap interval trees under the rmap lock, and vma_complete() reinserts it before releasing the lock.
This is wholly unnecessary if its page offset (file rmap) or anonymous page offset (anon rmap) is unchanged.
So, track whether they will change in the newly introduced vp->file_pgoff_unchanged and vp->anon_pgoff_unchanged fields, and use them to determine whether to remove the VMA or not.
The rmap lock keeps things safe as no rmap walks can concurrently occur during the operation.
Additionally, some architectures (arm, parisc, nios2, csky) have dcache flush rmap walkers which take only flush_dcache_mmap_lock(), which is likewise held across the operation.
If the VMA remains in the tree, it's necessary to keep the augmented rb_subtree_last field updated to reflect its changed range.
Provide mapping_rmap_tree_[pre, post]_update() and anon_rmap_tree_[pre, post]_update_vma() (replacing the existing logic in the anonymous case) to handle both the changed and unchanged cases.
For the anon rmap case, with CONFIG_DEBUG_VM_RB set, avc->cached_vma_last is also updated when propagating in place.
When performing a VMA shrink or a split where the VMA is the lower one, the page offset cannot change, so set the flags unconditionally in these cases.
When merging VMAs the page offset is unchanged only in some cases, so update init_multi_vma_prep() to set the flags only if the page offsets remain the same.
Finally, while we're here, also update expand_upwards() similarly.
These changes ultimately result in less rmap lock contention.
Pan Deng reported results using the UnixBench/excel benchmark on a 2-socket 192 core, 384 thread x86-64 system for v7.3-rc4 with/without the patch applied:
Execl Throughput, index score:
avg %stdev min max
v7.3-rc4 3511.5 0.44% 3494.5 3543.4 + patch 4069.0 0.48% 4047.4 4109.5 (+15.9%)
Average wait on file rmap lock in ms, 5 runs per kernel:
avg %stdev min max v7.3-rc4 9.470 2.91% 9.070 9.820 + patch 8.420 3.34% 8.100 8.770 (-11.1%)
Profiling data obtained during the operation highlighted the file rmap lock as the primary source of contention.
---
v2:
* Added Suggested-by, as per Pan.
* Added tags (Thanks Rik!)
* Updated commit message to provide perf data from Pan (thanks! :).
* Renamed pgoff_unchanged to file_pgoff_unchanged as per Pedro.
* Implemented mapping_rmap_tree_[pre, post]_update() and anon_rmap_tree_[pre, post]_update_vma() to separate out logic into the rmap/interval tree code as per Pedro.
* Since this change touched other code, and expand_upwards() is one such case, correctly mark it as pgoff unchanged.
v1:
https://lore.kernel.org/r/[email protected]
--- include/linux/mm.h | 10 ++++ mm/interval_tree.c | 101 ++++++++++++++++++++++++++++++++++++++ mm/vma.c | 74 +++++++++++----------------- mm/vma.h | 2 + tools/testing/vma/include/stubs.h | 22 +++++++++ 5 files changed, 164 insertions(+), 45 deletions(-)
include/linux/mm.h +10 -0
@@ -4357,6 +4357,12 @@ void mapping_rmap_tree_insert_after(struct vm_area_struct *vma, struct address_space *mapping);void mapping_rmap_tree_remove (struct vm_area_struct *vma,struct address_space *mapping); +void mapping_rmap_tree_pre_update (struct vm_area_struct *vma, +struct address_space *mapping, +bool pgoff_unchanged); +void mapping_rmap_tree_post_update (struct vm_area_struct *vma, +struct address_space *mapping, +bool pgoff_unchanged);struct vm_area_struct *mapping_rmap_tree_iter_first (struct address_space *mapping,pgoff_t pgoff_start,pgoff_t pgoff_last);
@@ -4374,6 +4380,10 @@ void anon_rmap_tree_insert(struct anon_vma_chain *avc, struct anon_vma *anon_vma);void anon_rmap_tree_remove (struct anon_vma_chain *avc,struct anon_vma *anon_vma); +void anon_rmap_tree_pre_update_vma (struct vm_area_struct *vma, +bool anon_pgoff_unchanged); +void anon_rmap_tree_post_update_vma (struct vm_area_struct *vma, +bool anon_pgoff_unchanged);struct anon_vma_chain *anon_rmap_tree_iter_first (struct anon_vma *anon_vma,pgoff_t pgoff_start,pgoff_t pgoff_last);
mm/interval_tree.c +101 -0
113 lines not on this page · raw message
mm/vma.c +29 -45
143 lines not on this page · raw message
mm/vma.h +2 -0
@@ -28,6 +28,8 @@ struct vma_prepare {struct vm_area_struct *remove2; bool skip_vma_uprobe :1; + bool file_pgoff_unchanged :1; + bool anon_pgoff_unchanged :1; };struct unlink_vma_file_batch {
tools/testing/vma/include/stubs.h +22 -0
@@ -267,6 +267,18 @@ static inline void mapping_rmap_tree_remove(struct vm_area_struct *vma, { } +static inline void mapping_rmap_tree_pre_update (struct vm_area_struct *vma, +struct address_space *mapping, +bool pgoff_unchanged) +{ +} + +static inline void mapping_rmap_tree_post_update (struct vm_area_struct *vma, +struct address_space *mapping, +bool pgoff_unchanged) +{ +} +static inline void flush_dcache_mmap_unlock (struct address_space *mapping) { }
@@ -281,6 +293,16 @@ static inline void anon_rmap_tree_remove(struct anon_vma_chain *avc, { } +static inline void anon_rmap_tree_pre_update_vma (struct vm_area_struct *vma, +bool anon_pgoff_unchanged) +{ +} + +static inline void anon_rmap_tree_post_update_vma (struct vm_area_struct *vma, +bool anon_pgoff_unchanged) +{ +} +static inline void uprobe_mmap (struct vm_area_struct *vma) { }
---
base-commit: e8d0f6a1b2a447d02845984fa6288787543cb03c
change-id: 20260925-speed-up-inplace-rmap-808fbb3848f5
Cheers,
Signature · 1 line
Lorenzo Stoakes (ARM) <[email protected]>
Quoted 55 lines · Lorenzo Stoakes (ARM)
When updating a VMA,
vma_prepare()unconditionally removes it from its rmap interval trees under the rmap lock, andvma_complete()reinserts it before releasing the lock.This is wholly unnecessary if its page offset (file rmap) or anonymous page offset (anon rmap) is unchanged.
So, track whether they will change in the newly introduced
vp->file_pgoff_unchangedandvp->anon_pgoff_unchangedfields, and use them to determine whether to remove the VMA or not.The rmap lock keeps things safe as no rmap walks can concurrently occur during the operation.
Additionally, some architectures (arm, parisc, nios2, csky) have dcache flush rmap walkers which take only
flush_dcache_mmap_lock(), which is likewise held across the operation.If the VMA remains in the tree, it's necessary to keep the augmented
rb_subtree_lastfield updated to reflect its changed range.Provide mapping_rmap_tree_[pre, post]_update() and anon_rmap_tree_[pre, post]_update_vma() (replacing the existing logic in the anonymous case) to handle both the changed and unchanged cases.
For the anon rmap case, with
CONFIG_DEBUG_VM_RBset,avc->cached_vma_lastis also updated when propagating in place.When performing a VMA shrink or a split where the VMA is the lower one, the page offset cannot change, so set the flags unconditionally in these cases.
When merging VMAs the page offset is unchanged only in some cases, so update
init_multi_vma_prep()to set the flags only if the page offsets remain the same.Finally, while we're here, also update
expand_upwards()similarly.These changes ultimately result in less rmap lock contention.
Pan Deng reported results using the UnixBench/excel benchmark on a 2-socket 192 core, 384 thread x86-64 system for v7.3-rc4 with/without the patch applied:
Execl Throughput, index score:
avg %stdev min max
v7.3-rc4 3511.5 0.44% 3494.5 3543.4 + patch 4069.0 0.48% 4047.4 4109.5 (+15.9%)Average wait on file rmap lock in ms, 5 runs per kernel:
avg %stdev min max v7.3-rc4 9.470 2.91% 9.070 9.820 + patch 8.420 3.34% 8.100 8.770 (-11.1%)
Quoted 7 lines
Profiling data obtained during the operation highlighted the file rmap lock as the primary source of contention.
Suggested-by Pan Deng <[email protected]>Reviewed-by Rik van Riel <[email protected]>Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>---
Wow, pretty cool stuff. That's a nice speedup!
[...]
mm/interval_tree.c void anon_rmap_tree_remove(struct anon_vma_chain *avc, · line 164
Quoted 6 lines
mm/interval_tree.c +6
-0+static void anon_rmap_tree_update_inplace (struct anon_vma_chain *avc) +{ +#ifdef CONFIG_DEBUG_VM_RB + avc->cached_vma_last =avc_last_pgoff (avc); +#endif +/* Propagate all the way up the tree. */
Nit: propagate() can stop early when rb_subtree_last is unchanged ...
Maybe:
/* Update the subtree maximum and propagate any changes up the tree. */
Quoted 3 lines
mm/interval_tree.c +3
-0+ __anon_rmap_tree_augment.propagate (&avc->rb,NULL ); +} +
[...]
Hammered it with VMA churn (split/merge/mremap/madvise/fork) + concurrent rmap walks + hwpoison injection. Nothing complained :D
Cheers, Lance
Lorenzo Stoakes (ARM)
maintainerQuoted 77 lines · Lance Yang
Quoted 55 lines · Lorenzo Stoakes (ARM)
When updating a VMA,
vma_prepare()unconditionally removes it from its rmap interval trees under the rmap lock, andvma_complete()reinserts it before releasing the lock.This is wholly unnecessary if its page offset (file rmap) or anonymous page offset (anon rmap) is unchanged.
So, track whether they will change in the newly introduced
vp->file_pgoff_unchangedandvp->anon_pgoff_unchangedfields, and use them to determine whether to remove the VMA or not.The rmap lock keeps things safe as no rmap walks can concurrently occur during the operation.
Additionally, some architectures (arm, parisc, nios2, csky) have dcache flush rmap walkers which take only
flush_dcache_mmap_lock(), which is likewise held across the operation.If the VMA remains in the tree, it's necessary to keep the augmented
rb_subtree_lastfield updated to reflect its changed range.Provide mapping_rmap_tree_[pre, post]_update() and anon_rmap_tree_[pre, post]_update_vma() (replacing the existing logic in the anonymous case) to handle both the changed and unchanged cases.
For the anon rmap case, with
CONFIG_DEBUG_VM_RBset,avc->cached_vma_lastis also updated when propagating in place.When performing a VMA shrink or a split where the VMA is the lower one, the page offset cannot change, so set the flags unconditionally in these cases.
When merging VMAs the page offset is unchanged only in some cases, so update
init_multi_vma_prep()to set the flags only if the page offsets remain the same.Finally, while we're here, also update
expand_upwards()similarly.These changes ultimately result in less rmap lock contention.
Pan Deng reported results using the UnixBench/excel benchmark on a 2-socket 192 core, 384 thread x86-64 system for v7.3-rc4 with/without the patch applied:
Execl Throughput, index score:
avg %stdev min max
v7.3-rc4 3511.5 0.44% 3494.5 3543.4 + patch 4069.0 0.48% 4047.4 4109.5 (+15.9%)Average wait on file rmap lock in ms, 5 runs per kernel:
avg %stdev min max v7.3-rc4 9.470 2.91% 9.070 9.820 + patch 8.420 3.34% 8.100 8.770 (-11.1%)Quoted 7 lines
Profiling data obtained during the operation highlighted the file rmap lock as the primary source of contention.
Suggested-by Pan Deng <[email protected]>Reviewed-by Rik van Riel <[email protected]>Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>---
Wow, pretty cool stuff. That's a nice speedup!
[...]
mm/interval_tree.c void anon_rmap_tree_remove(struct anon_vma_chain *avc, · line 164
Quoted 6 lines
mm/interval_tree.c +6
-0+static void anon_rmap_tree_update_inplace (struct anon_vma_chain *avc) +{ +#ifdef CONFIG_DEBUG_VM_RB + avc->cached_vma_last =avc_last_pgoff (avc); +#endif +/* Propagate all the way up the tree. */ Nit:
propagate()can stop early whenrb_subtree_lastis unchanged ...
Quoted 3 lines
Maybe:
/* Update the subtree maximum and propagate any changes up the tree. */
I think in this case it's ok to be a bit blurry about it :P it deciding not to unnecessary work is fine but I don't want to put too much in there.
The point is as a simple sign or pointer to help somebody wondering wtf that's for even if it's not quite the full story!
Hopefully that's ok? :)
Quoted 7 lines
Quoted 4 lines
mm/interval_tree.c +3
-0+ __anon_rmap_tree_augment.propagate (&avc->rb,NULL ); +} +[...]
Acked-by Lance Yang <[email protected]>
Thanks :)
Quoted 5 lines
Hammered it with VMA churn (split/merge/mremap/madvise/fork) + concurrent rmap walks + hwpoison injection. Nothing complained :D
Tested-by Lance Yang <[email protected]>
Thanks, very much appreciated! :)
Quoted 2 lines
Cheers, Lance
--
Cheers, Lorenzo
Lance Yang
reviewerQuoted 1 line · Lorenzo Stoakes (ARM)
On Thu, Oct 01, 2026 at 11:45:11PM +0800, Lance Yang wrote:
[...]
Quoted 13 lines
Quoted 9 lines
mm/interval_tree.c void anon_rmap_tree_remove(struct anon_vma_chain *avc, · line 164
Quoted 6 lines
mm/interval_tree.c +6
-0+static void anon_rmap_tree_update_inplace (struct anon_vma_chain *avc) +{ +#ifdef CONFIG_DEBUG_VM_RB + avc->cached_vma_last =avc_last_pgoff (avc); +#endif +/* Propagate all the way up the tree. */ Nit:
propagate()can stop early whenrb_subtree_lastis unchanged ...Quoted 3 lines
Maybe:
/* Update the subtree maximum and propagate any changes up the tree. */
Quoted 7 lines
I think in this case it's ok to be a bit blurry about it :P it deciding not to unnecessary work is fine but I don't want to put too much in there.
The point is as a simple sign or pointer to help somebody wondering wtf that's for even if it's not quite the full story!
Hopefully that's ok? :)
Fair enough. Let’s leave it as-is ;)
Quoted 12 lines
Quoted 8 lines
Quoted 5 lines
mm/interval_tree.c +3
-0+ __anon_rmap_tree_augment.propagate (&avc->rb,NULL ); +} +[...]
Acked-by Lance Yang <[email protected]>Thanks :)
Quoted 1 line
Quoted 6 lines
Quoted 4 lines
Hammered it with VMA churn (split/merge/mremap/madvise/fork) + concurrent rmap walks + hwpoison injection. Nothing complained :D
Tested-by Lance Yang <[email protected]>Thanks, very much appreciated! :)
Cheers!
Quoted 55 lines · Lorenzo Stoakes (ARM)
When updating a VMA,
vma_prepare()unconditionally removes it from its rmap interval trees under the rmap lock, andvma_complete()reinserts it before releasing the lock.This is wholly unnecessary if its page offset (file rmap) or anonymous page offset (anon rmap) is unchanged.
So, track whether they will change in the newly introduced
vp->file_pgoff_unchangedandvp->anon_pgoff_unchangedfields, and use them to determine whether to remove the VMA or not.The rmap lock keeps things safe as no rmap walks can concurrently occur during the operation.
Additionally, some architectures (arm, parisc, nios2, csky) have dcache flush rmap walkers which take only
flush_dcache_mmap_lock(), which is likewise held across the operation.If the VMA remains in the tree, it's necessary to keep the augmented
rb_subtree_lastfield updated to reflect its changed range.Provide mapping_rmap_tree_[pre, post]_update() and anon_rmap_tree_[pre, post]_update_vma() (replacing the existing logic in the anonymous case) to handle both the changed and unchanged cases.
For the anon rmap case, with
CONFIG_DEBUG_VM_RBset,avc->cached_vma_lastis also updated when propagating in place.When performing a VMA shrink or a split where the VMA is the lower one, the page offset cannot change, so set the flags unconditionally in these cases.
When merging VMAs the page offset is unchanged only in some cases, so update
init_multi_vma_prep()to set the flags only if the page offsets remain the same.Finally, while we're here, also update
expand_upwards()similarly.These changes ultimately result in less rmap lock contention.
Pan Deng reported results using the UnixBench/excel benchmark on a 2-socket 192 core, 384 thread x86-64 system for v7.3-rc4 with/without the patch applied:
Execl Throughput, index score:
avg %stdev min max
v7.3-rc4 3511.5 0.44% 3494.5 3543.4 + patch 4069.0 0.48% 4047.4 4109.5 (+15.9%)Average wait on file rmap lock in ms, 5 runs per kernel:
avg %stdev min max v7.3-rc4 9.470 2.91% 9.070 9.820 + patch 8.420 3.34% 8.100 8.770 (-11.1%)
Quoted 6 lines
Profiling data obtained during the operation highlighted the file rmap lock as the primary source of contention.
Suggested-by Pan Deng <[email protected]>Reviewed-by Rik van Riel <[email protected]>Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>
Looks great, thanks!
<snip>
Quoted 7 lines
index
077e23694143..ad6be42cc65a100644mm/vma.c +0
-0@@ -201,8 +201,15 @@ static void init_multi_vma_prep(struct vma_prepare *vp,if (vp->file) vp->mapping = vma->vm_file->f_mapping;
mm/vma.c static void init_multi_vma_prep(struct vma_prepare *vp, · line 212
Quoted 10 lines
mm/vma.c +8
-1-if (vmg && vmg->skip_vma_uprobe) +if (!vmg) +return ; + +if (vmg->skip_vma_uprobe) vp->skip_vma_uprobe = true; +if (vma_start_pgoff (vma) ==vmg_start_pgoff (vmg)) + vp->file_pgoff_unchanged = true; +if (vma_start_anon_pgoff (vma) ==vmg_start_anon_pgoff (vmg)) + vp->anon_pgoff_unchanged = true;
FWIW I kinda would've liked to abstract this out to rmap code, but a quick look shows that it is likely to become a header dependency hell if put in rmap.h. Could perhaps have been separated out to a function call.
Anyway, your call :) This version already hides most of the ugly to interval tree code.
Signature · 1 line
Pedro
Lorenzo Stoakes (ARM)
maintainerQuoted 8 lines · Pedro Falcato
Quoted 3 lines · Lorenzo Stoakes (ARM)
Suggested-by Pan Deng <[email protected]>Reviewed-by Rik van Riel <[email protected]>Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>Looks great, thanks!
Reviewed-by Pedro Falcato <[email protected]>
Thanks!
Quoted 21 lines
Quoted 6 lines
mm/vma.c +0
-0@@ -201,8 +201,15 @@ static void init_multi_vma_prep(struct vma_prepare *vp,if (vp->file) vp->mapping = vma->vm_file->f_mapping;mm/vma.c static void init_multi_vma_prep(struct vma_prepare *vp, · line 212
Quoted 10 lines
mm/vma.c +8
-1-if (vmg && vmg->skip_vma_uprobe) +if (!vmg) +return ; + +if (vmg->skip_vma_uprobe) vp->skip_vma_uprobe = true; +if (vma_start_pgoff (vma) ==vmg_start_pgoff (vmg)) + vp->file_pgoff_unchanged = true; +if (vma_start_anon_pgoff (vma) ==vmg_start_anon_pgoff (vmg)) + vp->anon_pgoff_unchanged = true;FWIW I kinda would've liked to abstract this out to rmap code, but a quick look shows that it is likely to become a header dependency hell if put in
rmap.h. Could perhaps have been separated out to a function call.
Quoted 2 lines
Anyway, your call :) This version already hides most of the ugly to interval tree code.
I'd rather keep it as-is. These case are explicitly setting the state because of things known at that point, and as you say it's not possible to do it sensibly due to headers etc.
--
Cheers, Lorenzo