reimplement per-vma lock as a refcount
Subsystems (13)
Suren Baghdasaryan
maintainerv9 cover reimplement per-vma lock as a refcount
Back when per-vma locks were introduces, vm_lock was moved out of vm_area_struct in [1] because of the performance regression caused by false cacheline sharing. Recent investigation [2] revealed that the regressions is limited to a rather old Broadwell microarchitecture and even there it can be mitigated by disabling adjacent cacheline prefetching, see [3].
Splitting single logical structure into multiple ones leads to more complicated management, extra pointer dereferences and overall less maintainable code. When that split-away part is a lock, it complicates things even further. With no performance benefits, there are no reasons for this split. Merging the vm_lock back into vm_area_struct also allows vm_area_struct to use SLAB_TYPESAFE_BY_RCU later in this patchset. This patchset:
1. moves vm_lock back into vm_area_struct, aligning it at the cacheline boundary and changing the cache to be cacheline-aligned to minimize cacheline sharing;
2. changes vm_area_struct initialization to mark new vma as detached until it is inserted into vma tree;
3. replaces vm_lock and vma->detached flag with a reference counter;
4. regroups vm_area_struct members to fit them into 3 cachelines;
5. changes vm_area_struct cache to SLAB_TYPESAFE_BY_RCU to allow for their reuse and to minimize call_rcu() calls.
Pagefault microbenchmarks show performance improvement:
Hmean faults/cpu-1 507926.5547 ( 0.00%) 506519.3692 * -0.28%* Hmean faults/cpu-4 479119.7051 ( 0.00%) 481333.6802 * 0.46%* Hmean faults/cpu-7 452880.2961 ( 0.00%) 455845.6211 * 0.65%* Hmean faults/cpu-12 347639.1021 ( 0.00%) 352004.2254 * 1.26%* Hmean faults/cpu-21 200061.2238 ( 0.00%) 229597.0317 * 14.76%* Hmean faults/cpu-30 145251.2001 ( 0.00%) 164202.5067 * 13.05%* Hmean faults/cpu-48 106848.4434 ( 0.00%) 120641.5504 * 12.91%* Hmean faults/cpu-56 92472.3835 ( 0.00%) 103464.7916 * 11.89%* Hmean faults/sec-1 507566.1468 ( 0.00%) 506139.0811 * -0.28%* Hmean faults/sec-4 1880478.2402 ( 0.00%) 1886795.6329 * 0.34%* Hmean faults/sec-7 3106394.3438 ( 0.00%) 3140550.7485 * 1.10%* Hmean faults/sec-12 4061358.4795 ( 0.00%) 4112477.0206 * 1.26%* Hmean faults/sec-21 3988619.1169 ( 0.00%) 4577747.1436 * 14.77%* Hmean faults/sec-30 3909839.5449 ( 0.00%) 4311052.2787 * 10.26%* Hmean faults/sec-48 4761108.4691 ( 0.00%) 5283790.5026 * 10.98%* Hmean faults/sec-56 4885561.4590 ( 0.00%) 5415839.4045 * 10.85%*
Changes since v8 [4]:
- Change subject for the cover letter, per Vlastimil Babka
- Added Reviewed-by and Acked-by, per Vlastimil Babka
- Added static check for no-limit case in __refcount_add_not_zero_limited, per David Laight
- Fixed vma_refcount_put() to call rwsem_release() unconditionally, per Hillf Danton and Vlastimil Babka
- Use a copy of vma->vm_mm in vma_refcount_put() in case vma is freed from under us, per Vlastimil Babka
- Removed extra rcu_read_lock()/rcu_read_unlock() in vma_end_read(), per Vlastimil Babka
- Changed __vma_enter_locked() parameter to centralize refcount logic, per Vlastimil Babka
- Amended description in vm_lock replacement patch explaining the effects of the patch on vm_area_struct size, per Vlastimil Babka
- Added vm_area_struct member regrouping patch [5] into the series
- Renamed vma_copy() into vm_area_init_from(), per Liam R. Howlett
- Added a comment for vm_area_struct to update vm_area_init_from() when adding new members, per Vlastimil Babka
- Updated a comment about unstable src->shared.rb when copying a vma in vm_area_init_from(), per Vlastimil Babka
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/ZsQyI%2F087V34JoIt@xsang-OptiPlex-9020/
[3] https://lore.kernel.org/all/CAJuCfpEisU8Lfe96AYJDZ+OM4NoPmnw9bP53cT_kbfP_pR+-2g@mail.gmail.com/
[4] https://lore.kernel.org/all/[email protected]/
[5] https://lore.kernel.org/all/[email protected]/
Patchset applies over mm-unstable after reverting v8 (current SHA range: 235b5129cb7b - 9e6b24c58985)
Suren Baghdasaryan (17):
mm: introduce vma_start_read_locked{_nested} helpers
mm: move per-vma lock into vm_area_struct
mm: mark vma as detached until it's added into vma tree
mm: introduce vma_iter_store_attached() to use with attached vmas
mm: mark vmas detached upon exit
types: move struct rcuwait into types.h
mm: allow vma_start_read_locked/vma_start_read_locked_nested to fail
mm: move mmap_init_lock() out of the header file
mm: uninline the main body of vma_start_write()
refcount: introduce __refcount_{add|inc}_not_zero_limited
mm: replace vm_lock and detached flag with a reference count
mm: move lesser used vma_area_struct members into the last cacheline mm/debug: print vm_refcnt state when dumping the vma
mm: remove extra vma_numab_state_init() call
mm: prepare lock_vma_under_rcu() for vma reuse possibility
mm: make vma cache SLAB_TYPESAFE_BY_RCU
docs/mm: document latest changes to vm_lock
Documentation/mm/process_addrs.rst | 44 ++++---- include/linux/mm.h | 156 ++++++++++++++++++++++------- include/linux/mm_types.h | 75 +++++++------- include/linux/mmap_lock.h | 6 -- include/linux/rcuwait.h | 13 +-- include/linux/refcount.h | 24 ++++- include/linux/slab.h | 6 -- include/linux/types.h | 12 +++ kernel/fork.c | 129 +++++++++++------------- mm/debug.c | 12 +++ mm/init-mm.c | 1 + mm/memory.c | 97 ++++++++++++++++-- mm/mmap.c | 3 +- mm/userfaultfd.c | 32 +++--- mm/vma.c | 23 ++--- mm/vma.h | 15 ++- tools/testing/vma/linux/atomic.h | 5 + tools/testing/vma/vma_internal.h | 93 ++++++++--------- 18 files changed, 465 insertions(+), 281 deletions(-)
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
Suren Baghdasaryan
maintainerPATCH v9 1/17 mm: introduce vma_start_read_locked{_nested} helpers
Introduce helper functions which can be used to read-lock a VMA when holding mmap_lock for read. Replace direct accesses to vma->vm_lock with these new helpers.
--- include/linux/mm.h | 24 ++++++++++++++++++++++++ mm/userfaultfd.c | 22 +++++----------------- 2 files changed, 29 insertions(+), 17 deletions(-)
include/linux/mm.h +24 -0
@@ -735,6 +735,30 @@ static inline bool vma_start_read(struct vm_area_struct *vma)return true; } +/* +* Use only while holding mmap read lock which guarantees that locking will not +* fail (nobody can concurrently write-lock the vma). vma_start_read() should +* not be used in such cases because it might fail due to mm_lock_seq overflow. +* This functionality is used to obtain vma read lock and drop the mmap read lock. +*/ +static inline void vma_start_read_locked_nested (struct vm_area_struct *vma,int subclass) +{ +mmap_assert_locked (vma->vm_mm); +down_read_nested (&vma->vm_lock->lock, subclass); +} + +/* +* Use only while holding mmap read lock which guarantees that locking will not +* fail (nobody can concurrently write-lock the vma). vma_start_read() should +* not be used in such cases because it might fail due to mm_lock_seq overflow. +* This functionality is used to obtain vma read lock and drop the mmap read lock. +*/ +static inline void vma_start_read_locked (struct vm_area_struct *vma) +{ +mmap_assert_locked (vma->vm_mm); +down_read (&vma->vm_lock->lock); +} +static inline void vma_end_read (struct vm_area_struct *vma) {rcu_read_lock ();/* keeps vma alive till the end of up_read */
mm/userfaultfd.c +5 -17
@@ -84,16 +84,8 @@ static struct vm_area_struct *uffd_lock_vma(struct mm_struct *mm,mmap_read_lock (mm); vma =find_vma_and_prepare_anon (mm, address); -if (!IS_ERR (vma)) { -/* -* We cannot use vma_start_read() as it may fail due to -* false locked (see comment in vma_start_read()). We -* can avoid that by directly locking vm_lock under -* mmap_lock, which guarantees that nobody can lock the -* vma for write (vma_start_write()) under us. -*/ -down_read (&vma->vm_lock->lock); - } +if (!IS_ERR (vma)) +vma_start_read_locked (vma);mmap_read_unlock (mm);return vma;
@@ -1491,14 +1483,10 @@ static int uffd_move_lock(struct mm_struct *mm,mmap_read_lock (mm); err =find_vmas_mm_locked (mm, dst_start, src_start, dst_vmap, src_vmap);if (!err) { -/* -* See comment in uffd_lock_vma() as to why not using -* vma_start_read() here. -*/ -down_read (&(*dst_vmap)->vm_lock->lock); +vma_start_read_locked (*dst_vmap);if (*dst_vmap != *src_vmap) -down_read_nested (&(*src_vmap)->vm_lock->lock, -SINGLE_DEPTH_NESTING ); +vma_start_read_locked_nested (*src_vmap, +SINGLE_DEPTH_NESTING ); }mmap_read_unlock (mm);return err;
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
Suren Baghdasaryan
maintainerPATCH v9 2/17 mm: move per-vma lock into vm_area_struct
In reply to Suren BaghdasaryanBack when per-vma locks were introduces, vm_lock was moved out of vm_area_struct in [1] because of the performance regression caused by false cacheline sharing. Recent investigation [2] revealed that the regressions is limited to a rather old Broadwell microarchitecture and even there it can be mitigated by disabling adjacent cacheline prefetching, see [3].
Splitting single logical structure into multiple ones leads to more complicated management, extra pointer dereferences and overall less maintainable code. When that split-away part is a lock, it complicates things even further. With no performance benefits, there are no reasons for this split. Merging the vm_lock back into vm_area_struct also allows vm_area_struct to use SLAB_TYPESAFE_BY_RCU later in this patchset. Move vm_lock back into vm_area_struct, aligning it at the cacheline boundary and changing the cache to be cacheline-aligned as well. With kernel compiled using defconfig, this causes VMA memory consumption to grow from 160 (vm_area_struct) + 40 (vm_lock) bytes to 256 bytes:
slabinfo before:
<name> ... <objsize> <objperslab> <pagesperslab> : ...
vma_lock ... 40 102 1 : ...
vm_area_struct ... 160 51 2 : ...
slabinfo after moving vm_lock:
<name> ... <objsize> <objperslab> <pagesperslab> : ...
vm_area_struct ... 256 32 2 : ...
Aggregate VMA memory consumption per 1000 VMAs grows from 50 to 64 pages, which is 5.5MB per 100000 VMAs. Note that the size of this structure is dependent on the kernel configuration and typically the original size is higher than 160 bytes. Therefore these calculations are close to the worst case scenario. A more realistic vm_area_struct usage before this change is:
<name> ... <objsize> <objperslab> <pagesperslab> : ...
vma_lock ... 40 102 1 : ...
vm_area_struct ... 176 46 2 : ...Aggregate VMA memory consumption per 1000 VMAs grows from 54 to 64 pages, which is 3.9MB per 100000 VMAs. This memory consumption growth can be addressed later by optimizing the vm_lock.
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/ZsQyI%2F087V34JoIt@xsang-OptiPlex-9020/
[3] https://lore.kernel.org/all/CAJuCfpEisU8Lfe96AYJDZ+OM4NoPmnw9bP53cT_kbfP_pR+-2g@mail.gmail.com/
--- include/linux/mm.h | 28 ++++++++++-------- include/linux/mm_types.h | 6 ++-- kernel/fork.c | 49 ++++---------------------------- tools/testing/vma/vma_internal.h | 33 +++++---------------- 4 files changed, 32 insertions(+), 84 deletions(-)
include/linux/mm.h +16 -12
@@ -697,6 +697,12 @@ static inline void vma_numab_state_free(struct vm_area_struct *vma) {}#endif /* CONFIG_NUMA_BALANCING */ #ifdef CONFIG_PER_VMA_LOCK +static inline void vma_lock_init (struct vm_area_struct *vma) +{ +init_rwsem (&vma->vm_lock.lock); + vma->vm_lock_seq =UINT_MAX ; +} +/* * Try to read-lock a vma. The function is allowed to occasionally yield false * locked result to avoid performance overhead, in which case we fall back to
@@ -714,7 +720,7 @@ static inline bool vma_start_read(struct vm_area_struct *vma)if (READ_ONCE (vma->vm_lock_seq) ==READ_ONCE (vma->vm_mm->mm_lock_seq.sequence))return false; -if (unlikely (down_read_trylock (&vma->vm_lock->lock) ==0 )) +if (unlikely (down_read_trylock (&vma->vm_lock.lock) ==0 ))return false;/*
@@ -729,7 +735,7 @@ static inline bool vma_start_read(struct vm_area_struct *vma)* This pairs with RELEASE semantics in vma_end_write_all(). */ if (unlikely (vma->vm_lock_seq ==raw_read_seqcount (&vma->vm_mm->mm_lock_seq))) { -up_read (&vma->vm_lock->lock); +up_read (&vma->vm_lock.lock);return false; }return true;
@@ -744,7 +750,7 @@ static inline bool vma_start_read(struct vm_area_struct *vma)static inline void vma_start_read_locked_nested (struct vm_area_struct *vma,int subclass) {mmap_assert_locked (vma->vm_mm); -down_read_nested (&vma->vm_lock->lock, subclass); +down_read_nested (&vma->vm_lock.lock, subclass); }/*
@@ -756,13 +762,13 @@ static inline void vma_start_read_locked_nested(struct vm_area_struct *vma, intstatic inline void vma_start_read_locked (struct vm_area_struct *vma) {mmap_assert_locked (vma->vm_mm); -down_read (&vma->vm_lock->lock); +down_read (&vma->vm_lock.lock); }static inline void vma_end_read (struct vm_area_struct *vma) {rcu_read_lock ();/* keeps vma alive till the end of up_read */ -up_read (&vma->vm_lock->lock); +up_read (&vma->vm_lock.lock);rcu_read_unlock (); }
@@ -791,7 +797,7 @@ static inline void vma_start_write(struct vm_area_struct *vma)if (__is_vma_write_locked (vma, &mm_lock_seq))return ; -down_write (&vma->vm_lock->lock); +down_write (&vma->vm_lock.lock);/* * We should use WRITE_ONCE() here because we can have concurrent reads * from the early lockless pessimistic check in vma_start_read().
@@ -799,7 +805,7 @@ static inline void vma_start_write(struct vm_area_struct *vma)* we should use WRITE_ONCE() for cleanliness and to keep KCSAN happy. */ WRITE_ONCE (vma->vm_lock_seq, mm_lock_seq); -up_write (&vma->vm_lock->lock); +up_write (&vma->vm_lock.lock); }static inline void vma_assert_write_locked (struct vm_area_struct *vma)
@@ -811,7 +817,7 @@ static inline void vma_assert_write_locked(struct vm_area_struct *vma)static inline void vma_assert_locked (struct vm_area_struct *vma) { -if (!rwsem_is_locked (&vma->vm_lock->lock)) +if (!rwsem_is_locked (&vma->vm_lock.lock))vma_assert_write_locked (vma); }
@@ -844,6 +850,7 @@ struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm,#else /* CONFIG_PER_VMA_LOCK */ +static inline void vma_lock_init (struct vm_area_struct *vma) {}static inline bool vma_start_read (struct vm_area_struct *vma) {return false; }static inline void vma_end_read (struct vm_area_struct *vma) {}
@@ -878,10 +885,6 @@ static inline void assert_fault_locked(struct vm_fault *vmf)extern const struct vm_operations_struct vma_dummy_vm_ops; -/* -* WARNING: vma_init does not initialize vma->vm_lock. -* Use vm_area_alloc()/vm_area_free() if vma needs locking. -*/ static inline void vma_init (struct vm_area_struct *vma,struct mm_struct *mm) {memset (vma,0 ,sizeof (*vma));
@@ -890,6 +893,7 @@ static inline void vma_init(struct vm_area_struct *vma, struct mm_struct *mm)INIT_LIST_HEAD (&vma->anon_vma_chain);vma_mark_detached (vma, false);vma_numab_state_init (vma); +vma_lock_init (vma); }/* Use when VMA is not part of the VMA tree and needs no locking */
include/linux/mm_types.h +4 -2
@@ -730,8 +730,6 @@ struct vm_area_struct {* slowpath. */ unsigned int vm_lock_seq; -/* Unstable RCU readers are allowed to read this. */ -struct vma_lock *vm_lock;#endif /*
@@ -784,6 +782,10 @@ struct vm_area_struct {struct vma_numab_state *numab_state;/* NUMA Balancing state */ #endif struct vm_userfaultfd_ctx vm_userfaultfd_ctx; +#ifdef CONFIG_PER_VMA_LOCK +/* Unstable RCU readers are allowed to read this. */ +struct vma_lock vm_lock____cacheline_aligned_in_smp ; +#endif } __randomize_layout;#ifdef CONFIG_NUMA
kernel/fork.c +5 -44
85 lines not on this page · raw message
tools/testing/vma/vma_internal.h +7 -26
@@ -274,10 +274,10 @@ struct vm_area_struct {/* * Can only be written (using WRITE_ONCE()) while holding both: * - mmap_lock (in write mode) -* - vm_lock->lock (in write mode) +* - vm_lock.lock (in write mode) * Can be read reliably while holding one of: * - mmap_lock (in read or write mode) -* - vm_lock->lock (in read or write mode) +* - vm_lock.lock (in read or write mode) * Can be read unreliably (using READ_ONCE()) for pessimistic bailout * while holding nothing (except RCU to keep the VMA struct allocated). *
@@ -286,7 +286,7 @@ struct vm_area_struct {* slowpath. */ unsigned int vm_lock_seq; -struct vma_lock *vm_lock; +struct vma_lock vm_lock;#endif /*
@@ -463,17 +463,10 @@ static inline struct vm_area_struct *vma_next(struct vma_iterator *vmi)return mas_find (&vmi->mas,ULONG_MAX ); } -static inline bool vma_lock_alloc (struct vm_area_struct *vma) +static inline void vma_lock_init (struct vm_area_struct *vma) { - vma->vm_lock =calloc (1 ,sizeof (struct vma_lock )); - -if (!vma->vm_lock) -return false; - -init_rwsem (&vma->vm_lock->lock); +init_rwsem (&vma->vm_lock.lock); vma->vm_lock_seq =UINT_MAX ; - -return true; }static inline void vma_assert_write_locked (struct vm_area_struct *);
@@ -496,6 +489,7 @@ static inline void vma_init(struct vm_area_struct *vma, struct mm_struct *mm) vma->vm_ops = &vma_dummy_vm_ops;INIT_LIST_HEAD (&vma->anon_vma_chain);vma_mark_detached (vma, false); +vma_lock_init (vma); }static inline struct vm_area_struct *vm_area_alloc (struct mm_struct *mm)
@@ -506,10 +500,6 @@ static inline struct vm_area_struct *vm_area_alloc(struct mm_struct *mm)return NULL ;vma_init (vma, mm); -if (!vma_lock_alloc (vma)) { -free (vma); -return NULL ; - }return vma; }
@@ -522,10 +512,7 @@ static inline struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig)return NULL ;memcpy (new, orig,sizeof (*new)); -if (!vma_lock_alloc (new)) { -free (new); -return NULL ; - } +vma_lock_init (new);INIT_LIST_HEAD (&new->anon_vma_chain);return new;
@@ -695,14 +682,8 @@ static inline void mpol_put(struct mempolicy *) { } -static inline void vma_lock_free (struct vm_area_struct *vma) -{ -free (vma->vm_lock); -} -static inline void __vm_area_free (struct vm_area_struct *vma) { -vma_lock_free (vma);free (vma); }
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
Suren Baghdasaryan
maintainerPATCH v9 3/17 mm: mark vma as detached until it's added into vma tree
In reply to Suren BaghdasaryanCurrent implementation does not set detached flag when a VMA is first allocated. This does not represent the real state of the VMA, which is detached until it is added into mm's VMA tree. Fix this by marking new VMAs as detached and resetting detached flag only after VMA is added into a tree.
Introduce vma_mark_attached() to make the API more readable and to simplify possible future cleanup when vma->vm_mm might be used to indicate detached vma and vma_mark_attached() will need an additional mm parameter.
--- include/linux/mm.h | 27 ++++++++++++++++++++------- kernel/fork.c | 4 ++++ mm/memory.c | 2 +- mm/vma.c | 6 +++--- mm/vma.h | 2 ++ tools/testing/vma/vma_internal.h | 17 ++++++++++++----- 6 files changed, 42 insertions(+), 16 deletions(-)
include/linux/mm.h +20 -7
@@ -821,12 +821,21 @@ static inline void vma_assert_locked(struct vm_area_struct *vma)vma_assert_write_locked (vma); } -static inline void vma_mark_detached (struct vm_area_struct *vma,bool detached) +static inline void vma_mark_attached (struct vm_area_struct *vma) +{ + vma->detached = false; +} + +static inline void vma_mark_detached (struct vm_area_struct *vma) {/* When detaching vma should be write-locked */ -if (detached) -vma_assert_write_locked (vma); - vma->detached = detached; +vma_assert_write_locked (vma); + vma->detached = true; +} + +static inline bool is_vma_detached (struct vm_area_struct *vma) +{ +return vma->detached; }static inline void release_fault_lock (struct vm_fault *vmf)
@@ -857,8 +866,8 @@ static inline void vma_end_read(struct vm_area_struct *vma) {}static inline void vma_start_write (struct vm_area_struct *vma) {}static inline void vma_assert_write_locked (struct vm_area_struct *vma) {mmap_assert_write_locked (vma->vm_mm); } -static inline void vma_mark_detached (struct vm_area_struct *vma, -bool detached) {} +static inline void vma_mark_attached (struct vm_area_struct *vma) {} +static inline void vma_mark_detached (struct vm_area_struct *vma) {}static inline struct vm_area_struct *lock_vma_under_rcu (struct mm_struct *mm,unsigned long address)
@@ -891,7 +900,10 @@ static inline void vma_init(struct vm_area_struct *vma, struct mm_struct *mm) vma->vm_mm = mm; vma->vm_ops = &vma_dummy_vm_ops;INIT_LIST_HEAD (&vma->anon_vma_chain); -vma_mark_detached (vma, false); +#ifdef CONFIG_PER_VMA_LOCK +/* vma is not locked, can't use vma_mark_detached() */ + vma->detached = true; +#endif vma_numab_state_init (vma);vma_lock_init (vma); }
@@ -1086,6 +1098,7 @@ static inline int vma_iter_bulk_store(struct vma_iterator *vmi,if (unlikely (mas_is_err (&vmi->mas)))return -ENOMEM ; +vma_mark_attached (vma);return 0 ; }
kernel/fork.c +4 -0
@@ -465,6 +465,10 @@ struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig)data_race (memcpy (new, orig,sizeof (*new)));vma_lock_init (new);INIT_LIST_HEAD (&new->anon_vma_chain); +#ifdef CONFIG_PER_VMA_LOCK +/* vma is not locked, can't use vma_mark_detached() */ + new->detached = true; +#endif vma_numab_state_init (new);dup_anon_vma_name (orig, new);
mm/memory.c +1 -1
@@ -6349,7 +6349,7 @@ struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm, goto inval;/* Check if the VMA got isolated after we found it */ -if (vma->detached) { +if (is_vma_detached (vma)) {vma_end_read (vma);count_vm_vma_lock_event (VMA_LOCK_MISS );/* The area was replaced with another one */
mm/vma.c +3 -3
@@ -327,7 +327,7 @@ static void vma_complete(struct vma_prepare *vp, struct vma_iterator *vmi,if (vp->remove) { again: -vma_mark_detached (vp->remove, true); +vma_mark_detached (vp->remove);if (vp->file) {uprobe_munmap (vp->remove, vp->remove->vm_start, vp->remove->vm_end);
@@ -1221,7 +1221,7 @@ static void reattach_vmas(struct ma_state *mas_detach)mas_set (mas_detach,0 );mas_for_each (mas_detach, vma,ULONG_MAX ) -vma_mark_detached (vma, false); +vma_mark_attached (vma);__mt_destroy (mas_detach->tree); }
@@ -1296,7 +1296,7 @@ static int vms_gather_munmap_vmas(struct vma_munmap_struct *vms,if (error) goto munmap_gather_failed; -vma_mark_detached (next, true); +vma_mark_detached (next); nrpages =vma_pages (next); vms->nr_pages += nrpages;
mm/vma.h +2 -0
@@ -157,6 +157,7 @@ static inline int vma_iter_store_gfp(struct vma_iterator *vmi,if (unlikely (mas_is_err (&vmi->mas)))return -ENOMEM ; +vma_mark_attached (vma);return 0 ; }
@@ -389,6 +390,7 @@ static inline void vma_iter_store(struct vma_iterator *vmi,__mas_set_range (&vmi->mas, vma->vm_start, vma->vm_end -1 );mas_store_prealloc (&vmi->mas, vma); +vma_mark_attached (vma); }static inline unsigned long vma_iter_addr (struct vma_iterator *vmi)
tools/testing/vma/vma_internal.h +12 -5
@@ -469,13 +469,17 @@ static inline void vma_lock_init(struct vm_area_struct *vma) vma->vm_lock_seq =UINT_MAX ; } +static inline void vma_mark_attached (struct vm_area_struct *vma) +{ + vma->detached = false; +} +static inline void vma_assert_write_locked (struct vm_area_struct *); -static inline void vma_mark_detached (struct vm_area_struct *vma,bool detached) +static inline void vma_mark_detached (struct vm_area_struct *vma) {/* When detaching vma should be write-locked */ -if (detached) -vma_assert_write_locked (vma); - vma->detached = detached; +vma_assert_write_locked (vma); + vma->detached = true; }extern const struct vm_operations_struct vma_dummy_vm_ops;
@@ -488,7 +492,8 @@ static inline void vma_init(struct vm_area_struct *vma, struct mm_struct *mm) vma->vm_mm = mm; vma->vm_ops = &vma_dummy_vm_ops;INIT_LIST_HEAD (&vma->anon_vma_chain); -vma_mark_detached (vma, false); +/* vma is not locked, can't use vma_mark_detached() */ + vma->detached = true;vma_lock_init (vma); }
@@ -514,6 +519,8 @@ static inline struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig)memcpy (new, orig,sizeof (*new));vma_lock_init (new);INIT_LIST_HEAD (&new->anon_vma_chain); +/* vma is not locked, can't use vma_mark_detached() */ + new->detached = true;return new; }
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
Suren Baghdasaryan
maintainerPATCH v9 4/17 mm: introduce vma_iter_store_attached() to use with attached vmas
In reply to Suren Baghdasaryanvma_iter_store() functions can be used both when adding a new vma and when updating an existing one. However for existing ones we do not need to mark them attached as they are already marked that way. Introduce vma_iter_store_attached() to be used with already attached vmas.
--- include/linux/mm.h | 12 ++++++++++++ mm/vma.c | 8 ++++---- mm/vma.h | 11 +++++++++-- 3 files changed, 25 insertions(+), 6 deletions(-)
include/linux/mm.h +12 -0
@@ -821,6 +821,16 @@ static inline void vma_assert_locked(struct vm_area_struct *vma)vma_assert_write_locked (vma); } +static inline void vma_assert_attached (struct vm_area_struct *vma) +{ +VM_BUG_ON_VMA (vma->detached, vma); +} + +static inline void vma_assert_detached (struct vm_area_struct *vma) +{ +VM_BUG_ON_VMA (!vma->detached, vma); +} +static inline void vma_mark_attached (struct vm_area_struct *vma) { vma->detached = false;
@@ -866,6 +876,8 @@ static inline void vma_end_read(struct vm_area_struct *vma) {}static inline void vma_start_write (struct vm_area_struct *vma) {}static inline void vma_assert_write_locked (struct vm_area_struct *vma) {mmap_assert_write_locked (vma->vm_mm); } +static inline void vma_assert_attached (struct vm_area_struct *vma) {} +static inline void vma_assert_detached (struct vm_area_struct *vma) {}static inline void vma_mark_attached (struct vm_area_struct *vma) {}static inline void vma_mark_detached (struct vm_area_struct *vma) {}
mm/vma.c +4 -4
@@ -660,14 +660,14 @@ static int commit_merge(struct vma_merge_struct *vmg,vma_set_range (vmg->vma, vmg->start, vmg->end, vmg->pgoff);if (expanded) -vma_iter_store (vmg->vmi, vmg->vma); +vma_iter_store_attached (vmg->vmi, vmg->vma);if (adj_start) { adjust->vm_start += adj_start; adjust->vm_pgoff +=PHYS_PFN (adj_start);if (adj_start <0 ) {WARN_ON (expanded); -vma_iter_store (vmg->vmi, adjust); +vma_iter_store_attached (vmg->vmi, adjust); } }
@@ -2845,7 +2845,7 @@ int expand_upwards(struct vm_area_struct *vma, unsigned long address)anon_vma_interval_tree_pre_update_vma (vma); vma->vm_end = address;/* Overwrite old entry in mtree. */ -vma_iter_store (&vmi, vma); +vma_iter_store_attached (&vmi, vma);anon_vma_interval_tree_post_update_vma (vma);perf_event_mmap (vma);
@@ -2925,7 +2925,7 @@ int expand_downwards(struct vm_area_struct *vma, unsigned long address) vma->vm_start = address; vma->vm_pgoff -= grow;/* Overwrite old entry in mtree. */ -vma_iter_store (&vmi, vma); +vma_iter_store_attached (&vmi, vma);anon_vma_interval_tree_post_update_vma (vma);perf_event_mmap (vma);
mm/vma.h +9 -2
@@ -365,9 +365,10 @@ static inline struct vm_area_struct *vma_iter_load(struct vma_iterator *vmi) }/* Store a VMA with preallocated memory */ -static inline void vma_iter_store (struct vma_iterator *vmi, -struct vm_area_struct *vma) +static inline void vma_iter_store_attached (struct vma_iterator *vmi, +struct vm_area_struct *vma) { +vma_assert_attached (vma);#if defined(CONFIG_DEBUG_VM_MAPLE_TREE ) if (MAS_WARN_ON(&vmi->mas, vmi->mas.status != ma_start &&
@@ -390,7 +391,13 @@ static inline void vma_iter_store(struct vma_iterator *vmi,__mas_set_range (&vmi->mas, vma->vm_start, vma->vm_end -1 );mas_store_prealloc (&vmi->mas, vma); +} + +static inline void vma_iter_store (struct vma_iterator *vmi, +struct vm_area_struct *vma) +{vma_mark_attached (vma); +vma_iter_store_attached (vmi, vma); }static inline unsigned long vma_iter_addr (struct vma_iterator *vmi)
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
Re: [PATCH v9 04/17] mm: introduce vma_iter_store_attached() to use with attached vmas
Quoted 4 lines · Suren Baghdasaryan
vma_iter_store()functions can be used both when adding a new vma and when updating an existing one. However for existing ones we do not need to mark them attached as they are already marked that way. Introducevma_iter_store_attached()to be used with already attached vmas.
OK I guess the intent of this is to reinstate the previously existing asserts, only explicitly checking those places where we attach.
I'm a little concerned that by doing this, somebody might simply invoke this function without realising the implications.
Can we have something functional like
vma_iter_store_new() and vma_iter_store_overwrite()
?
I don't like us just leaving vma_iter_store() quietly making an assumption that a caller doesn't necessarily realise.
Also it's more greppable this way.
I had a look through callers and it does seem you've snagged them all correctly.
Quoted 49 lines
Signed-off-by Suren Baghdasaryan <[email protected]>Reviewed-by Vlastimil Babka <[email protected]>--- include/linux/mm.h | 12 ++++++++++++ mm/vma.c | 8 ++++---- mm/vma.h | 11 +++++++++-- 3 files changed, 25 insertions(+), 6 deletions(-)include/linux/mm.h +12
-0@@ -821,6 +821,16 @@ static inline void vma_assert_locked(struct vm_area_struct *vma)vma_assert_write_locked (vma); } +static inline void vma_assert_attached (struct vm_area_struct *vma) +{ +VM_BUG_ON_VMA (vma->detached, vma); +} + +static inline void vma_assert_detached (struct vm_area_struct *vma) +{ +VM_BUG_ON_VMA (!vma->detached, vma); +} +static inline void vma_mark_attached (struct vm_area_struct *vma) { vma->detached = false;@@ -866,6 +876,8 @@ static inline void vma_end_read(struct vm_area_struct *vma) {}static inline void vma_start_write (struct vm_area_struct *vma) {}static inline void vma_assert_write_locked (struct vm_area_struct *vma) {mmap_assert_write_locked (vma->vm_mm); } +static inline void vma_assert_attached (struct vm_area_struct *vma) {} +static inline void vma_assert_detached (struct vm_area_struct *vma) {}static inline void vma_mark_attached (struct vm_area_struct *vma) {}static inline void vma_mark_detached (struct vm_area_struct *vma) {}mm/vma.c +1
-1@@ -660,14 +660,14 @@ static int commit_merge(struct vma_merge_struct *vmg,vma_set_range (vmg->vma, vmg->start, vmg->end, vmg->pgoff);if (expanded) -vma_iter_store (vmg->vmi, vmg->vma); +vma_iter_store_attached (vmg->vmi, vmg->vma);
mm/vma.c static int commit_merge(struct vma_merge_struct *vmg, · line 672
Quoted 10 lines
mm/vma.c +1
-1if (adj_start) { adjust->vm_start += adj_start; adjust->vm_pgoff +=PHYS_PFN (adj_start);if (adj_start <0 ) {WARN_ON (expanded); -vma_iter_store (vmg->vmi, adjust); +vma_iter_store_attached (vmg->vmi, adjust); } }
I kind of feel this whole function (that yes, I added :>) though derived from existing logic) needs rework, as it's necessarily rather confusing.
But hey, that's on me :)
But this does look right... OK see this as a note-to-self...
Quoted 39 lines
mm/vma.c +2
-2@@ -2845,7 +2845,7 @@ int expand_upwards(struct vm_area_struct *vma, unsigned long address)anon_vma_interval_tree_pre_update_vma (vma); vma->vm_end = address;/* Overwrite old entry in mtree. */ -vma_iter_store (&vmi, vma); +vma_iter_store_attached (&vmi, vma);anon_vma_interval_tree_post_update_vma (vma);perf_event_mmap (vma);@@ -2925,7 +2925,7 @@ int expand_downwards(struct vm_area_struct *vma, unsigned long address) vma->vm_start = address; vma->vm_pgoff -= grow;/* Overwrite old entry in mtree. */ -vma_iter_store (&vmi, vma); +vma_iter_store_attached (&vmi, vma);anon_vma_interval_tree_post_update_vma (vma);perf_event_mmap (vma);mm/vma.h +3
-2@@ -365,9 +365,10 @@ static inline struct vm_area_struct *vma_iter_load(struct vma_iterator *vmi) }/* Store a VMA with preallocated memory */ -static inline void vma_iter_store (struct vma_iterator *vmi, -struct vm_area_struct *vma) +static inline void vma_iter_store_attached (struct vma_iterator *vmi, +struct vm_area_struct *vma) { +vma_assert_attached (vma);#if defined(CONFIG_DEBUG_VM_MAPLE_TREE ) if (MAS_WARN_ON(&vmi->mas, vmi->mas.status != ma_start &&@@ -390,7 +391,13 @@ static inline void vma_iter_store(struct vma_iterator *vmi,__mas_set_range (&vmi->mas, vma->vm_start, vma->vm_end -1 );
mm/vma.h static inline void vma_iter_store(struct vma_iterator *vmi, · line 402
Quoted 10 lines
mm/vma.h +6
-0mas_store_prealloc (&vmi->mas, vma); +} + +static inline void vma_iter_store (struct vma_iterator *vmi, +struct vm_area_struct *vma) +{vma_mark_attached (vma); +vma_iter_store_attached (vmi, vma); }
See comment at top, and we need some comments here to explain why we're going to pains to do this.
What about mm/nommu.c? I guess these cases are always new VMAs.
We probably definitely need to check this series in a nommu setup, have you done this? As I can see this breaking things. Then again I suppose you'd have expected bots to moan by now...
Quoted 4 lines
mm/vma.h +0
-1static inline unsigned long vma_iter_addr (struct vma_iterator *vmi) --2.47.1.613.gc27f4b7a9f-goog
Suren Baghdasaryan
maintainerRe: [PATCH v9 04/17] mm: introduce vma_iter_store_attached() to use with attached vmas
Quoted 9 lines · Lorenzo Stoakes
Quoted 4 lines · Suren Baghdasaryan
vma_iter_store()functions can be used both when adding a new vma and when updating an existing one. However for existing ones we do not need to mark them attached as they are already marked that way. Introducevma_iter_store_attached()to be used with already attached vmas.OK I guess the intent of this is to reinstate the previously existing asserts, only explicitly checking those places where we attach.
No, the motivation is to prevern re-attaching an already attached vma or re-detaching an already detached vma for state consistency. I guess I should amend the description to make that clear.
Quoted 3 lines
I'm a little concerned that by doing this, somebody might simply invoke this function without realising the implications.
Well, in that case somebody should get an assertion. If vma_iter_store() is called against already attached vma, we get this assertion:
vma_iter_store()
vma_mark_attached()
vma_assert_detached()
If vma_iter_store_attached() is called against a detached vma, we get this one:
vma_iter_store_attached()
vma_assert_attached()
Does that address your concern?
Quoted 4 lines
Can we have something functional like
vma_iter_store_new()andvma_iter_store_overwrite()
Ok. A bit more churn but should not be too bad.
Quoted 116 lines
?
I don't like us just leaving
vma_iter_store()quietly making an assumption that a caller doesn't necessarily realise.Also it's more greppable this way.
I had a look through callers and it does seem you've snagged them all correctly.
Quoted 49 lines
Signed-off-by Suren Baghdasaryan <[email protected]>Reviewed-by Vlastimil Babka <[email protected]>--- include/linux/mm.h | 12 ++++++++++++ mm/vma.c | 8 ++++---- mm/vma.h | 11 +++++++++-- 3 files changed, 25 insertions(+), 6 deletions(-)include/linux/mm.h +12
-0@@ -821,6 +821,16 @@ static inline void vma_assert_locked(struct vm_area_struct *vma)vma_assert_write_locked (vma); } +static inline void vma_assert_attached (struct vm_area_struct *vma) +{ +VM_BUG_ON_VMA (vma->detached, vma); +} + +static inline void vma_assert_detached (struct vm_area_struct *vma) +{ +VM_BUG_ON_VMA (!vma->detached, vma); +} +static inline void vma_mark_attached (struct vm_area_struct *vma) { vma->detached = false;@@ -866,6 +876,8 @@ static inline void vma_end_read(struct vm_area_struct *vma) {}static inline void vma_start_write (struct vm_area_struct *vma) {}static inline void vma_assert_write_locked (struct vm_area_struct *vma) {mmap_assert_write_locked (vma->vm_mm); } +static inline void vma_assert_attached (struct vm_area_struct *vma) {} +static inline void vma_assert_detached (struct vm_area_struct *vma) {}static inline void vma_mark_attached (struct vm_area_struct *vma) {}static inline void vma_mark_detached (struct vm_area_struct *vma) {}mm/vma.c +1
-1@@ -660,14 +660,14 @@ static int commit_merge(struct vma_merge_struct *vmg,vma_set_range (vmg->vma, vmg->start, vmg->end, vmg->pgoff);if (expanded) -vma_iter_store (vmg->vmi, vmg->vma); +vma_iter_store_attached (vmg->vmi, vmg->vma);mm/vma.c static int commit_merge(struct vma_merge_struct *vmg, · line 672
Quoted 10 lines
mm/vma.c +1
-1if (adj_start) { adjust->vm_start += adj_start; adjust->vm_pgoff +=PHYS_PFN (adj_start);if (adj_start <0 ) {WARN_ON (expanded); -vma_iter_store (vmg->vmi, adjust); +vma_iter_store_attached (vmg->vmi, adjust); } }I kind of feel this whole function (that yes, I added :>) though derived from existing logic) needs rework, as it's necessarily rather confusing.
But hey, that's on me :)
But this does look right... OK see this as a note-to-self...
Quoted 38 lines
mm/vma.c +2
-2@@ -2845,7 +2845,7 @@ int expand_upwards(struct vm_area_struct *vma, unsigned long address)anon_vma_interval_tree_pre_update_vma (vma); vma->vm_end = address;/* Overwrite old entry in mtree. */ -vma_iter_store (&vmi, vma); +vma_iter_store_attached (&vmi, vma);anon_vma_interval_tree_post_update_vma (vma);perf_event_mmap (vma);@@ -2925,7 +2925,7 @@ int expand_downwards(struct vm_area_struct *vma, unsigned long address) vma->vm_start = address; vma->vm_pgoff -= grow;/* Overwrite old entry in mtree. */ -vma_iter_store (&vmi, vma); +vma_iter_store_attached (&vmi, vma);anon_vma_interval_tree_post_update_vma (vma);perf_event_mmap (vma);mm/vma.h +3
-2@@ -365,9 +365,10 @@ static inline struct vm_area_struct *vma_iter_load(struct vma_iterator *vmi) }/* Store a VMA with preallocated memory */ -static inline void vma_iter_store (struct vma_iterator *vmi, -struct vm_area_struct *vma) +static inline void vma_iter_store_attached (struct vma_iterator *vmi, +struct vm_area_struct *vma) { +vma_assert_attached (vma);#if defined(CONFIG_DEBUG_VM_MAPLE_TREE ) if (MAS_WARN_ON(&vmi->mas, vmi->mas.status != ma_start &&@@ -390,7 +391,13 @@ static inline void vma_iter_store(struct vma_iterator *vmi,
Quoted 14 lines
mm/vma.h static inline void vma_iter_store(struct vma_iterator *vmi, · line 402
Quoted 11 lines
mm/vma.h +6
-0__mas_set_range (&vmi->mas, vma->vm_start, vma->vm_end -1 );mas_store_prealloc (&vmi->mas, vma); +} + +static inline void vma_iter_store (struct vma_iterator *vmi, +struct vm_area_struct *vma) +{vma_mark_attached (vma); +vma_iter_store_attached (vmi, vma); }See comment at top, and we need some comments here to explain why we're going to pains to do this.
Ack. I'll amend the patch description to make that clear.
Quoted 2 lines
What about
mm/nommu.c? I guess these cases are always new VMAs.
CONFIG_PER_VMA_LOCK depends on !CONFIG_NOMMU, so for nommu case all these attach/detach functions become NOPs.
Quoted 9 lines
We probably definitely need to check this series in a nommu setup, have you done this? As I can see this breaking things. Then again I suppose you'd have expected bots to moan by now...
Quoted 4 lines
mm/vma.h +0
-1static inline unsigned long vma_iter_addr (struct vma_iterator *vmi) --2.47.1.613.gc27f4b7a9f-goog
Re: [PATCH v9 04/17] mm: introduce vma_iter_store_attached() to use with attached vmas
Quoted 28 lines · Suren Baghdasaryan
Quoted 9 lines · Lorenzo Stoakes
Quoted 4 lines · Suren Baghdasaryan
vma_iter_store()functions can be used both when adding a new vma and when updating an existing one. However for existing ones we do not need to mark them attached as they are already marked that way. Introducevma_iter_store_attached()to be used with already attached vmas.OK I guess the intent of this is to reinstate the previously existing asserts, only explicitly checking those places where we attach.
No, the motivation is to prevern re-attaching an already attached vma or re-detaching an already detached vma for state consistency. I guess I should amend the description to make that clear.
Quoted 3 lines
I'm a little concerned that by doing this, somebody might simply invoke this function without realising the implications.
Well, in that case somebody should get an assertion. If
vma_iter_store()is called against already attached vma, we get this assertion:
vma_iter_store()
vma_mark_attached()
vma_assert_detached()
Quoted 6 lines
If
vma_iter_store_attached()is called against a detached vma, we get this one:
vma_iter_store_attached()
vma_assert_attached()Does that address your concern?
Well the issue is that you might only get that assertion in some code path that isn't immediately exercised by code the bots run :)
See my comment re: testing to 00/17 (though I absolutely accept testing this is a giant pain).
But yes we are protected in cases where a user has CONFIG_DEBUG_VM turned on.
I honestly wonder if we need to be stronger than that though, it's really serious if we do this wrong isn't it? Maybe it should be a WARN_ON_ONCE() or something?
In any case, a rename means somebody isn't going to do this by mistake.
Quoted 7 lines
Quoted 5 lines
Can we have something functional like
vma_iter_store_new()andvma_iter_store_overwrite()Ok. A bit more churn but should not be too bad.
Yeah sorry for churn (though hey - I _am_ the churn king right? ;) - but I think in this case it's really valuable for understanding.
Quoted 132 lines
Quoted 117 lines
?
I don't like us just leaving
vma_iter_store()quietly making an assumption that a caller doesn't necessarily realise.Also it's more greppable this way.
I had a look through callers and it does seem you've snagged them all correctly.
Quoted 49 lines
Signed-off-by Suren Baghdasaryan <[email protected]>Reviewed-by Vlastimil Babka <[email protected]>--- include/linux/mm.h | 12 ++++++++++++ mm/vma.c | 8 ++++---- mm/vma.h | 11 +++++++++-- 3 files changed, 25 insertions(+), 6 deletions(-)include/linux/mm.h +12
-0@@ -821,6 +821,16 @@ static inline void vma_assert_locked(struct vm_area_struct *vma)vma_assert_write_locked (vma); } +static inline void vma_assert_attached (struct vm_area_struct *vma) +{ +VM_BUG_ON_VMA (vma->detached, vma); +} + +static inline void vma_assert_detached (struct vm_area_struct *vma) +{ +VM_BUG_ON_VMA (!vma->detached, vma); +} +static inline void vma_mark_attached (struct vm_area_struct *vma) { vma->detached = false;@@ -866,6 +876,8 @@ static inline void vma_end_read(struct vm_area_struct *vma) {}static inline void vma_start_write (struct vm_area_struct *vma) {}static inline void vma_assert_write_locked (struct vm_area_struct *vma) {mmap_assert_write_locked (vma->vm_mm); } +static inline void vma_assert_attached (struct vm_area_struct *vma) {} +static inline void vma_assert_detached (struct vm_area_struct *vma) {}static inline void vma_mark_attached (struct vm_area_struct *vma) {}static inline void vma_mark_detached (struct vm_area_struct *vma) {}mm/vma.c +1
-1@@ -660,14 +660,14 @@ static int commit_merge(struct vma_merge_struct *vmg,vma_set_range (vmg->vma, vmg->start, vmg->end, vmg->pgoff);if (expanded) -vma_iter_store (vmg->vmi, vmg->vma); +vma_iter_store_attached (vmg->vmi, vmg->vma);mm/vma.c static int commit_merge(struct vma_merge_struct *vmg, · line 672
Quoted 10 lines
mm/vma.c +1
-1if (adj_start) { adjust->vm_start += adj_start; adjust->vm_pgoff +=PHYS_PFN (adj_start);if (adj_start <0 ) {WARN_ON (expanded); -vma_iter_store (vmg->vmi, adjust); +vma_iter_store_attached (vmg->vmi, adjust); } }I kind of feel this whole function (that yes, I added :>) though derived from existing logic) needs rework, as it's necessarily rather confusing.
But hey, that's on me :)
But this does look right... OK see this as a note-to-self...
Quoted 38 lines
mm/vma.c +2
-2@@ -2845,7 +2845,7 @@ int expand_upwards(struct vm_area_struct *vma, unsigned long address)anon_vma_interval_tree_pre_update_vma (vma); vma->vm_end = address;/* Overwrite old entry in mtree. */ -vma_iter_store (&vmi, vma); +vma_iter_store_attached (&vmi, vma);anon_vma_interval_tree_post_update_vma (vma);perf_event_mmap (vma);@@ -2925,7 +2925,7 @@ int expand_downwards(struct vm_area_struct *vma, unsigned long address) vma->vm_start = address; vma->vm_pgoff -= grow;/* Overwrite old entry in mtree. */ -vma_iter_store (&vmi, vma); +vma_iter_store_attached (&vmi, vma);anon_vma_interval_tree_post_update_vma (vma);perf_event_mmap (vma);mm/vma.h +3
-2@@ -365,9 +365,10 @@ static inline struct vm_area_struct *vma_iter_load(struct vma_iterator *vmi) }/* Store a VMA with preallocated memory */ -static inline void vma_iter_store (struct vma_iterator *vmi, -struct vm_area_struct *vma) +static inline void vma_iter_store_attached (struct vma_iterator *vmi, +struct vm_area_struct *vma) { +vma_assert_attached (vma);#if defined(CONFIG_DEBUG_VM_MAPLE_TREE ) if (MAS_WARN_ON(&vmi->mas, vmi->mas.status != ma_start &&@@ -390,7 +391,13 @@ static inline void vma_iter_store(struct vma_iterator *vmi,Quoted 14 lines
mm/vma.h static inline void vma_iter_store(struct vma_iterator *vmi, · line 402
Quoted 11 lines
mm/vma.h +6
-0__mas_set_range (&vmi->mas, vma->vm_start, vma->vm_end -1 );mas_store_prealloc (&vmi->mas, vma); +} + +static inline void vma_iter_store (struct vma_iterator *vmi, +struct vm_area_struct *vma) +{vma_mark_attached (vma); +vma_iter_store_attached (vmi, vma); }See comment at top, and we need some comments here to explain why we're going to pains to do this.
Quoted 1 line
Ack. I'll amend the patch description to make that clear.
Thanks!
Quoted 6 lines
Quoted 3 lines
What about
mm/nommu.c? I guess these cases are always new VMAs.
CONFIG_PER_VMA_LOCKdepends on !CONFIG_NOMMU, so for nommu case all these attach/detach functions become NOPs.
Ack. OK good, I usually like to pretend nommu doesn't exist, but sometimes have to ack that it does, now in this case I can go back to not caring... :>)
Quoted 10 lines
Quoted 10 lines
We probably definitely need to check this series in a nommu setup, have you done this? As I can see this breaking things. Then again I suppose you'd have expected bots to moan by now...
Quoted 4 lines
mm/vma.h +0
-1static inline unsigned long vma_iter_addr (struct vma_iterator *vmi) --2.47.1.613.gc27f4b7a9f-goog
Re: [PATCH v9 04/17] mm: introduce vma_iter_store_attached() to use with attached vmas
In reply to Suren BaghdasaryanQuoted 9 lines · Suren Baghdasaryan
Quoted 7 lines · Lorenzo Stoakes
Quoted 4 lines · Suren Baghdasaryan
vma_iter_store()functions can be used both when adding a new vma and when updating an existing one. However for existing ones we do not need to mark them attached as they are already marked that way. Introducevma_iter_store_attached()to be used with already attached vmas.
Quoted 6 lines
Quoted 2 lines
OK I guess the intent of this is to reinstate the previously existing asserts, only explicitly checking those places where we attach.
No, the motivation is to prevern re-attaching an already attached vma or re-detaching an already detached vma for state consistency. I guess I should amend the description to make that clear.
Sorry for noise, missed this reply.
What I mean by this is, in a past iteration of this series I reviewed code where you did this but did _not_ differentiate between cases of new VMAs vs. existing, which caused an assert in your series which I reported.
So I"m saying - now you _are_ differentiating between the two cases.
It's certainly worth belabouring the point of exactly what it is you are trying to catch here, however! :) So yes please do add a little more to commit msg that'd be great, thanks!
Quoted 175 lines
Quoted 4 lines
I'm a little concerned that by doing this, somebody might simply invoke this function without realising the implications.
Well, in that case somebody should get an assertion. If
vma_iter_store()is called against already attached vma, we get this assertion:
vma_iter_store()
vma_mark_attached()
vma_assert_detached()If
vma_iter_store_attached()is called against a detached vma, we get this one:
vma_iter_store_attached()
vma_assert_attached()Does that address your concern?
Quoted 4 lines
Can we have something functional like
vma_iter_store_new()andvma_iter_store_overwrite()Ok. A bit more churn but should not be too bad.
Quoted 116 lines
?
I don't like us just leaving
vma_iter_store()quietly making an assumption that a caller doesn't necessarily realise.Also it's more greppable this way.
I had a look through callers and it does seem you've snagged them all correctly.
Quoted 49 lines
Signed-off-by Suren Baghdasaryan <[email protected]>Reviewed-by Vlastimil Babka <[email protected]>--- include/linux/mm.h | 12 ++++++++++++ mm/vma.c | 8 ++++---- mm/vma.h | 11 +++++++++-- 3 files changed, 25 insertions(+), 6 deletions(-)include/linux/mm.h +12
-0@@ -821,6 +821,16 @@ static inline void vma_assert_locked(struct vm_area_struct *vma)vma_assert_write_locked (vma); } +static inline void vma_assert_attached (struct vm_area_struct *vma) +{ +VM_BUG_ON_VMA (vma->detached, vma); +} + +static inline void vma_assert_detached (struct vm_area_struct *vma) +{ +VM_BUG_ON_VMA (!vma->detached, vma); +} +static inline void vma_mark_attached (struct vm_area_struct *vma) { vma->detached = false;@@ -866,6 +876,8 @@ static inline void vma_end_read(struct vm_area_struct *vma) {}static inline void vma_start_write (struct vm_area_struct *vma) {}static inline void vma_assert_write_locked (struct vm_area_struct *vma) {mmap_assert_write_locked (vma->vm_mm); } +static inline void vma_assert_attached (struct vm_area_struct *vma) {} +static inline void vma_assert_detached (struct vm_area_struct *vma) {}static inline void vma_mark_attached (struct vm_area_struct *vma) {}static inline void vma_mark_detached (struct vm_area_struct *vma) {}mm/vma.c +1
-1@@ -660,14 +660,14 @@ static int commit_merge(struct vma_merge_struct *vmg,vma_set_range (vmg->vma, vmg->start, vmg->end, vmg->pgoff);if (expanded) -vma_iter_store (vmg->vmi, vmg->vma); +vma_iter_store_attached (vmg->vmi, vmg->vma);mm/vma.c static int commit_merge(struct vma_merge_struct *vmg, · line 672
Quoted 10 lines
mm/vma.c +1
-1if (adj_start) { adjust->vm_start += adj_start; adjust->vm_pgoff +=PHYS_PFN (adj_start);if (adj_start <0 ) {WARN_ON (expanded); -vma_iter_store (vmg->vmi, adjust); +vma_iter_store_attached (vmg->vmi, adjust); } }I kind of feel this whole function (that yes, I added :>) though derived from existing logic) needs rework, as it's necessarily rather confusing.
But hey, that's on me :)
But this does look right... OK see this as a note-to-self...
Quoted 38 lines
mm/vma.c +2
-2@@ -2845,7 +2845,7 @@ int expand_upwards(struct vm_area_struct *vma, unsigned long address)anon_vma_interval_tree_pre_update_vma (vma); vma->vm_end = address;/* Overwrite old entry in mtree. */ -vma_iter_store (&vmi, vma); +vma_iter_store_attached (&vmi, vma);anon_vma_interval_tree_post_update_vma (vma);perf_event_mmap (vma);@@ -2925,7 +2925,7 @@ int expand_downwards(struct vm_area_struct *vma, unsigned long address) vma->vm_start = address; vma->vm_pgoff -= grow;/* Overwrite old entry in mtree. */ -vma_iter_store (&vmi, vma); +vma_iter_store_attached (&vmi, vma);anon_vma_interval_tree_post_update_vma (vma);perf_event_mmap (vma);mm/vma.h +3
-2@@ -365,9 +365,10 @@ static inline struct vm_area_struct *vma_iter_load(struct vma_iterator *vmi) }/* Store a VMA with preallocated memory */ -static inline void vma_iter_store (struct vma_iterator *vmi, -struct vm_area_struct *vma) +static inline void vma_iter_store_attached (struct vma_iterator *vmi, +struct vm_area_struct *vma) { +vma_assert_attached (vma);#if defined(CONFIG_DEBUG_VM_MAPLE_TREE ) if (MAS_WARN_ON(&vmi->mas, vmi->mas.status != ma_start &&@@ -390,7 +391,13 @@ static inline void vma_iter_store(struct vma_iterator *vmi,Quoted 14 lines
mm/vma.h static inline void vma_iter_store(struct vma_iterator *vmi, · line 402
Quoted 11 lines
mm/vma.h +6
-0__mas_set_range (&vmi->mas, vma->vm_start, vma->vm_end -1 );mas_store_prealloc (&vmi->mas, vma); +} + +static inline void vma_iter_store (struct vma_iterator *vmi, +struct vm_area_struct *vma) +{vma_mark_attached (vma); +vma_iter_store_attached (vmi, vma); }See comment at top, and we need some comments here to explain why we're going to pains to do this.
Ack. I'll amend the patch description to make that clear.
Quoted 2 lines
What about
mm/nommu.c? I guess these cases are always new VMAs.
CONFIG_PER_VMA_LOCKdepends on !CONFIG_NOMMU, so for nommu case all these attach/detach functions become NOPs.Quoted 9 lines
We probably definitely need to check this series in a nommu setup, have you done this? As I can see this breaking things. Then again I suppose you'd have expected bots to moan by now...
Quoted 4 lines
mm/vma.h +0
-1static inline unsigned long vma_iter_addr (struct vma_iterator *vmi) --2.47.1.613.gc27f4b7a9f-goog
Suren Baghdasaryan
maintainerRe: [PATCH v9 04/17] mm: introduce vma_iter_store_attached() to use with attached vmas
Quoted 24 lines · Lorenzo Stoakes
Quoted 9 lines · Suren Baghdasaryan
Quoted 7 lines · Lorenzo Stoakes
Quoted 4 lines · Suren Baghdasaryan
vma_iter_store()functions can be used both when adding a new vma and when updating an existing one. However for existing ones we do not need to mark them attached as they are already marked that way. Introducevma_iter_store_attached()to be used with already attached vmas.Quoted 6 lines
Quoted 2 lines
OK I guess the intent of this is to reinstate the previously existing asserts, only explicitly checking those places where we attach.
No, the motivation is to prevern re-attaching an already attached vma or re-detaching an already detached vma for state consistency. I guess I should amend the description to make that clear.
Sorry for noise, missed this reply.
What I mean by this is, in a past iteration of this series I reviewed code where you did this but did _not_ differentiate between cases of new VMAs vs. existing, which caused an assert in your series which I reported.
Quoted 5 lines
So I"m saying - now you _are_ differentiating between the two cases.
It's certainly worth belabouring the point of exactly what it is you are trying to catch here, however! :) So yes please do add a little more to commit msg that'd be great, thanks!
Sure. How about:
With vma->detached being a separate flag, double-marking a vmas as attached or detached is not an issue because the flag will simply be overwritten with the same value. However once we fold this flag into the refcount later in this series, re-attaching or re-detaching a vma becomes an issue since these operations will be incrementing/decrementing a refcount. Fix the places where we currently re-attaching a vma during vma update and add assertions in vma_mark_attached()/vma_mark_detached() to catch invalid usage.
Quoted 176 lines
Quoted 176 lines
Quoted 5 lines
I'm a little concerned that by doing this, somebody might simply invoke this function without realising the implications.
Well, in that case somebody should get an assertion. If
vma_iter_store()is called against already attached vma, we get this assertion:
vma_iter_store()
vma_mark_attached()
vma_assert_detached()If
vma_iter_store_attached()is called against a detached vma, we get this one:
vma_iter_store_attached()
vma_assert_attached()Does that address your concern?
Quoted 4 lines
Can we have something functional like
vma_iter_store_new()andvma_iter_store_overwrite()Ok. A bit more churn but should not be too bad.
Quoted 116 lines
?
I don't like us just leaving
vma_iter_store()quietly making an assumption that a caller doesn't necessarily realise.Also it's more greppable this way.
I had a look through callers and it does seem you've snagged them all correctly.
Quoted 49 lines
Signed-off-by Suren Baghdasaryan <[email protected]>Reviewed-by Vlastimil Babka <[email protected]>--- include/linux/mm.h | 12 ++++++++++++ mm/vma.c | 8 ++++---- mm/vma.h | 11 +++++++++-- 3 files changed, 25 insertions(+), 6 deletions(-)include/linux/mm.h +12
-0@@ -821,6 +821,16 @@ static inline void vma_assert_locked(struct vm_area_struct *vma)vma_assert_write_locked (vma); } +static inline void vma_assert_attached (struct vm_area_struct *vma) +{ +VM_BUG_ON_VMA (vma->detached, vma); +} + +static inline void vma_assert_detached (struct vm_area_struct *vma) +{ +VM_BUG_ON_VMA (!vma->detached, vma); +} +static inline void vma_mark_attached (struct vm_area_struct *vma) { vma->detached = false;@@ -866,6 +876,8 @@ static inline void vma_end_read(struct vm_area_struct *vma) {}static inline void vma_start_write (struct vm_area_struct *vma) {}static inline void vma_assert_write_locked (struct vm_area_struct *vma) {mmap_assert_write_locked (vma->vm_mm); } +static inline void vma_assert_attached (struct vm_area_struct *vma) {} +static inline void vma_assert_detached (struct vm_area_struct *vma) {}static inline void vma_mark_attached (struct vm_area_struct *vma) {}static inline void vma_mark_detached (struct vm_area_struct *vma) {}mm/vma.c +1
-1@@ -660,14 +660,14 @@ static int commit_merge(struct vma_merge_struct *vmg,vma_set_range (vmg->vma, vmg->start, vmg->end, vmg->pgoff);if (expanded) -vma_iter_store (vmg->vmi, vmg->vma); +vma_iter_store_attached (vmg->vmi, vmg->vma);mm/vma.c static int commit_merge(struct vma_merge_struct *vmg, · line 672
Quoted 10 lines
mm/vma.c +1
-1if (adj_start) { adjust->vm_start += adj_start; adjust->vm_pgoff +=PHYS_PFN (adj_start);if (adj_start <0 ) {WARN_ON (expanded); -vma_iter_store (vmg->vmi, adjust); +vma_iter_store_attached (vmg->vmi, adjust); } }I kind of feel this whole function (that yes, I added :>) though derived from existing logic) needs rework, as it's necessarily rather confusing.
But hey, that's on me :)
But this does look right... OK see this as a note-to-self...
Quoted 38 lines
mm/vma.c +2
-2@@ -2845,7 +2845,7 @@ int expand_upwards(struct vm_area_struct *vma, unsigned long address)anon_vma_interval_tree_pre_update_vma (vma); vma->vm_end = address;/* Overwrite old entry in mtree. */ -vma_iter_store (&vmi, vma); +vma_iter_store_attached (&vmi, vma);anon_vma_interval_tree_post_update_vma (vma);perf_event_mmap (vma);@@ -2925,7 +2925,7 @@ int expand_downwards(struct vm_area_struct *vma, unsigned long address) vma->vm_start = address; vma->vm_pgoff -= grow;/* Overwrite old entry in mtree. */ -vma_iter_store (&vmi, vma); +vma_iter_store_attached (&vmi, vma);anon_vma_interval_tree_post_update_vma (vma);perf_event_mmap (vma);mm/vma.h +3
-2@@ -365,9 +365,10 @@ static inline struct vm_area_struct *vma_iter_load(struct vma_iterator *vmi) }/* Store a VMA with preallocated memory */ -static inline void vma_iter_store (struct vma_iterator *vmi, -struct vm_area_struct *vma) +static inline void vma_iter_store_attached (struct vma_iterator *vmi, +struct vm_area_struct *vma) { +vma_assert_attached (vma);#if defined(CONFIG_DEBUG_VM_MAPLE_TREE ) if (MAS_WARN_ON(&vmi->mas, vmi->mas.status != ma_start &&@@ -390,7 +391,13 @@ static inline void vma_iter_store(struct vma_iterator *vmi,Quoted 14 lines
mm/vma.h static inline void vma_iter_store(struct vma_iterator *vmi, · line 402
Quoted 11 lines
mm/vma.h +6
-0__mas_set_range (&vmi->mas, vma->vm_start, vma->vm_end -1 );mas_store_prealloc (&vmi->mas, vma); +} + +static inline void vma_iter_store (struct vma_iterator *vmi, +struct vm_area_struct *vma) +{vma_mark_attached (vma); +vma_iter_store_attached (vmi, vma); }See comment at top, and we need some comments here to explain why we're going to pains to do this.
Ack. I'll amend the patch description to make that clear.
Quoted 2 lines
What about
mm/nommu.c? I guess these cases are always new VMAs.
CONFIG_PER_VMA_LOCKdepends on !CONFIG_NOMMU, so for nommu case all these attach/detach functions become NOPs.Quoted 9 lines
We probably definitely need to check this series in a nommu setup, have you done this? As I can see this breaking things. Then again I suppose you'd have expected bots to moan by now...
Quoted 4 lines
mm/vma.h +0
-1static inline unsigned long vma_iter_addr (struct vma_iterator *vmi) --2.47.1.613.gc27f4b7a9f-goog
Re: [PATCH v9 04/17] mm: introduce vma_iter_store_attached() to use with attached vmas
Quoted 34 lines · Suren Baghdasaryan
Quoted 24 lines · Lorenzo Stoakes
Quoted 9 lines · Suren Baghdasaryan
Quoted 7 lines · Lorenzo Stoakes
Quoted 4 lines · Suren Baghdasaryan
vma_iter_store()functions can be used both when adding a new vma and when updating an existing one. However for existing ones we do not need to mark them attached as they are already marked that way. Introducevma_iter_store_attached()to be used with already attached vmas.Quoted 6 lines
Quoted 2 lines
OK I guess the intent of this is to reinstate the previously existing asserts, only explicitly checking those places where we attach.
No, the motivation is to prevern re-attaching an already attached vma or re-detaching an already detached vma for state consistency. I guess I should amend the description to make that clear.
Sorry for noise, missed this reply.
What I mean by this is, in a past iteration of this series I reviewed code where you did this but did _not_ differentiate between cases of new VMAs vs. existing, which caused an assert in your series which I reported.
Quoted 5 lines
So I"m saying - now you _are_ differentiating between the two cases.
It's certainly worth belabouring the point of exactly what it is you are trying to catch here, however! :) So yes please do add a little more to commit msg that'd be great, thanks!
Sure. How about:
Quoted 8 lines
With
vma->detachedbeing a separate flag, double-marking a vmas as attached or detached is not an issue because the flag will simply be overwritten with the same value. However once we fold this flag into the refcount later in this series, re-attaching or re-detaching a vma becomes an issue since these operations will be incrementing/decrementing a refcount. Fix the places where we currently re-attaching a vma during vma update and add assertions in vma_mark_attached()/vma_mark_detached() to catch invalid usage.
That's awesome, thanks!
Quoted 177 lines
Quoted 177 lines
Quoted 177 lines
Quoted 6 lines
I'm a little concerned that by doing this, somebody might simply invoke this function without realising the implications.
Well, in that case somebody should get an assertion. If
vma_iter_store()is called against already attached vma, we get this assertion:
vma_iter_store()
vma_mark_attached()
vma_assert_detached()If
vma_iter_store_attached()is called against a detached vma, we get this one:
vma_iter_store_attached()
vma_assert_attached()Does that address your concern?
Quoted 4 lines
Can we have something functional like
vma_iter_store_new()andvma_iter_store_overwrite()Ok. A bit more churn but should not be too bad.
Quoted 116 lines
?
I don't like us just leaving
vma_iter_store()quietly making an assumption that a caller doesn't necessarily realise.Also it's more greppable this way.
I had a look through callers and it does seem you've snagged them all correctly.
Quoted 49 lines
Signed-off-by Suren Baghdasaryan <[email protected]>Reviewed-by Vlastimil Babka <[email protected]>--- include/linux/mm.h | 12 ++++++++++++ mm/vma.c | 8 ++++---- mm/vma.h | 11 +++++++++-- 3 files changed, 25 insertions(+), 6 deletions(-)include/linux/mm.h +12
-0@@ -821,6 +821,16 @@ static inline void vma_assert_locked(struct vm_area_struct *vma)vma_assert_write_locked (vma); } +static inline void vma_assert_attached (struct vm_area_struct *vma) +{ +VM_BUG_ON_VMA (vma->detached, vma); +} + +static inline void vma_assert_detached (struct vm_area_struct *vma) +{ +VM_BUG_ON_VMA (!vma->detached, vma); +} +static inline void vma_mark_attached (struct vm_area_struct *vma) { vma->detached = false;@@ -866,6 +876,8 @@ static inline void vma_end_read(struct vm_area_struct *vma) {}static inline void vma_start_write (struct vm_area_struct *vma) {}static inline void vma_assert_write_locked (struct vm_area_struct *vma) {mmap_assert_write_locked (vma->vm_mm); } +static inline void vma_assert_attached (struct vm_area_struct *vma) {} +static inline void vma_assert_detached (struct vm_area_struct *vma) {}static inline void vma_mark_attached (struct vm_area_struct *vma) {}static inline void vma_mark_detached (struct vm_area_struct *vma) {}mm/vma.c +1
-1@@ -660,14 +660,14 @@ static int commit_merge(struct vma_merge_struct *vmg,vma_set_range (vmg->vma, vmg->start, vmg->end, vmg->pgoff);if (expanded) -vma_iter_store (vmg->vmi, vmg->vma); +vma_iter_store_attached (vmg->vmi, vmg->vma);mm/vma.c static int commit_merge(struct vma_merge_struct *vmg, · line 672
Quoted 10 lines
mm/vma.c +1
-1if (adj_start) { adjust->vm_start += adj_start; adjust->vm_pgoff +=PHYS_PFN (adj_start);if (adj_start <0 ) {WARN_ON (expanded); -vma_iter_store (vmg->vmi, adjust); +vma_iter_store_attached (vmg->vmi, adjust); } }I kind of feel this whole function (that yes, I added :>) though derived from existing logic) needs rework, as it's necessarily rather confusing.
But hey, that's on me :)
But this does look right... OK see this as a note-to-self...
Quoted 38 lines
mm/vma.c +2
-2@@ -2845,7 +2845,7 @@ int expand_upwards(struct vm_area_struct *vma, unsigned long address)anon_vma_interval_tree_pre_update_vma (vma); vma->vm_end = address;/* Overwrite old entry in mtree. */ -vma_iter_store (&vmi, vma); +vma_iter_store_attached (&vmi, vma);anon_vma_interval_tree_post_update_vma (vma);perf_event_mmap (vma);@@ -2925,7 +2925,7 @@ int expand_downwards(struct vm_area_struct *vma, unsigned long address) vma->vm_start = address; vma->vm_pgoff -= grow;/* Overwrite old entry in mtree. */ -vma_iter_store (&vmi, vma); +vma_iter_store_attached (&vmi, vma);anon_vma_interval_tree_post_update_vma (vma);perf_event_mmap (vma);mm/vma.h +3
-2@@ -365,9 +365,10 @@ static inline struct vm_area_struct *vma_iter_load(struct vma_iterator *vmi) }/* Store a VMA with preallocated memory */ -static inline void vma_iter_store (struct vma_iterator *vmi, -struct vm_area_struct *vma) +static inline void vma_iter_store_attached (struct vma_iterator *vmi, +struct vm_area_struct *vma) { +vma_assert_attached (vma);#if defined(CONFIG_DEBUG_VM_MAPLE_TREE ) if (MAS_WARN_ON(&vmi->mas, vmi->mas.status != ma_start &&@@ -390,7 +391,13 @@ static inline void vma_iter_store(struct vma_iterator *vmi,Quoted 14 lines
mm/vma.h static inline void vma_iter_store(struct vma_iterator *vmi, · line 402
Quoted 11 lines
mm/vma.h +6
-0__mas_set_range (&vmi->mas, vma->vm_start, vma->vm_end -1 );mas_store_prealloc (&vmi->mas, vma); +} + +static inline void vma_iter_store (struct vma_iterator *vmi, +struct vm_area_struct *vma) +{vma_mark_attached (vma); +vma_iter_store_attached (vmi, vma); }See comment at top, and we need some comments here to explain why we're going to pains to do this.
Ack. I'll amend the patch description to make that clear.
Quoted 2 lines
What about
mm/nommu.c? I guess these cases are always new VMAs.
CONFIG_PER_VMA_LOCKdepends on !CONFIG_NOMMU, so for nommu case all these attach/detach functions become NOPs.Quoted 9 lines
We probably definitely need to check this series in a nommu setup, have you done this? As I can see this breaking things. Then again I suppose you'd have expected bots to moan by now...
Quoted 4 lines
mm/vma.h +0
-1static inline unsigned long vma_iter_addr (struct vma_iterator *vmi) --2.47.1.613.gc27f4b7a9f-goog
Suren Baghdasaryan
maintainerPATCH v9 5/17 mm: mark vmas detached upon exit
In reply to Suren BaghdasaryanWhen exit_mmap() removes vmas belonging to an exiting task, it does not mark them as detached since they can't be reached by other tasks and they will be freed shortly. Once we introduce vma reuse, all vmas will have to be in detached state before they are freed to ensure vma when reused is in a consistent state. Add missing vma_mark_detached() before freeing the vma.
--- mm/vma.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
mm/vma.c +4 -2
@@ -413,10 +413,12 @@ void remove_vma(struct vm_area_struct *vma, bool unreachable)if (vma->vm_file)fput (vma->vm_file);mpol_put (vma_policy (vma)); -if (unreachable) +if (unreachable) { +vma_mark_detached (vma);__vm_area_free (vma); -else + }else {vm_area_free (vma); + } }/*
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
Re: [PATCH v9 05/17] mm: mark vmas detached upon exit
Quoted 6 lines · Suren Baghdasaryan
When
exit_mmap()removes vmas belonging to an exiting task, it does not mark them as detached since they can't be reached by other tasks and they will be freed shortly. Once we introduce vma reuse, all vmas will have to be in detached state before they are freed to ensure vma when reused is in a consistent state. Add missingvma_mark_detached()before freeing the vma.
Hmm this really makes me worry that we'll see bugs from this detached stuff, do we make this assumption anywhere else I wonder?
Quoted 3 lines
Signed-off-by Suren Baghdasaryan <[email protected]>Reviewed-by Vlastimil Babka <[email protected]>
But regardless, prima facie, this looks fine, so:
Quoted 26 lines
--- mm/vma.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)mm/vma.c +4
-3@@ -413,10 +413,12 @@ void remove_vma(struct vm_area_struct *vma, bool unreachable)if (vma->vm_file)fput (vma->vm_file);mpol_put (vma_policy (vma)); -if (unreachable) +if (unreachable) { +vma_mark_detached (vma);__vm_area_free (vma); -else + }else {vm_area_free (vma); + } }/* -- 2.47.1.613.gc27f4b7a9f-goog
Suren Baghdasaryan
maintainerRe: [PATCH v9 05/17] mm: mark vmas detached upon exit
Quoted 11 lines · Lorenzo Stoakes
Quoted 6 lines · Suren Baghdasaryan
When
exit_mmap()removes vmas belonging to an exiting task, it does not mark them as detached since they can't be reached by other tasks and they will be freed shortly. Once we introduce vma reuse, all vmas will have to be in detached state before they are freed to ensure vma when reused is in a consistent state. Add missingvma_mark_detached()before freeing the vma.Hmm this really makes me worry that we'll see bugs from this detached stuff, do we make this assumption anywhere else I wonder?
This is the only place which does not currently detach the vma before freeing it. If someone tries adding a case like that in the future, they will be met with vma_assert_detached() inside vm_area_free().
Quoted 35 lines
Quoted 4 lines
Signed-off-by Suren Baghdasaryan <[email protected]>Reviewed-by Vlastimil Babka <[email protected]>But regardless, prima facie, this looks fine, so:
Reviewed-by Lorenzo Stoakes <[email protected]>Quoted 26 lines
--- mm/vma.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)mm/vma.c +4
-3@@ -413,10 +413,12 @@ void remove_vma(struct vm_area_struct *vma, bool unreachable)if (vma->vm_file)fput (vma->vm_file);mpol_put (vma_policy (vma)); -if (unreachable) +if (unreachable) { +vma_mark_detached (vma);__vm_area_free (vma); -else + }else {vm_area_free (vma); + } }/* -- 2.47.1.613.gc27f4b7a9f-goog
Re: [PATCH v9 05/17] mm: mark vmas detached upon exit
Quoted 11 lines · Suren Baghdasaryan
Quoted 9 lines · Lorenzo Stoakes
Quoted 6 lines · Suren Baghdasaryan
When
exit_mmap()removes vmas belonging to an exiting task, it does not mark them as detached since they can't be reached by other tasks and they will be freed shortly. Once we introduce vma reuse, all vmas will have to be in detached state before they are freed to ensure vma when reused is in a consistent state. Add missingvma_mark_detached()before freeing the vma.
Quoted 6 lines
Quoted 2 lines
Hmm this really makes me worry that we'll see bugs from this detached stuff, do we make this assumption anywhere else I wonder?
This is the only place which does not currently detach the vma before freeing it. If someone tries adding a case like that in the future, they will be met with
vma_assert_detached()insidevm_area_free().
OK good to know!
Again, I wonder if we should make these assertions stronger as commented elsewhere, because if we see them in production isn't that worth an actual non-debug WARN_ON_ONCE()?
Quoted 36 lines
Quoted 36 lines
Quoted 5 lines
Signed-off-by Suren Baghdasaryan <[email protected]>Reviewed-by Vlastimil Babka <[email protected]>But regardless, prima facie, this looks fine, so:
Reviewed-by Lorenzo Stoakes <[email protected]>Quoted 26 lines
--- mm/vma.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)mm/vma.c +4
-3@@ -413,10 +413,12 @@ void remove_vma(struct vm_area_struct *vma, bool unreachable)if (vma->vm_file)fput (vma->vm_file);mpol_put (vma_policy (vma)); -if (unreachable) +if (unreachable) { +vma_mark_detached (vma);__vm_area_free (vma); -else + }else {vm_area_free (vma); + } }/* -- 2.47.1.613.gc27f4b7a9f-goog
Suren Baghdasaryan
maintainerRe: [PATCH v9 05/17] mm: mark vmas detached upon exit
Quoted 20 lines · Lorenzo Stoakes
Quoted 11 lines · Suren Baghdasaryan
Quoted 9 lines · Lorenzo Stoakes
Quoted 6 lines · Suren Baghdasaryan
When
exit_mmap()removes vmas belonging to an exiting task, it does not mark them as detached since they can't be reached by other tasks and they will be freed shortly. Once we introduce vma reuse, all vmas will have to be in detached state before they are freed to ensure vma when reused is in a consistent state. Add missingvma_mark_detached()before freeing the vma.Quoted 6 lines
Quoted 2 lines
Hmm this really makes me worry that we'll see bugs from this detached stuff, do we make this assumption anywhere else I wonder?
This is the only place which does not currently detach the vma before freeing it. If someone tries adding a case like that in the future, they will be met with
vma_assert_detached()insidevm_area_free().
Quoted 5 lines
OK good to know!
Again, I wonder if we should make these assertions stronger as commented elsewhere, because if we see them in production isn't that worth an actual non-debug
WARN_ON_ONCE()?
Sure. I'll change vma_assert_attached()/vma_assert_detached() to use WARN_ON_ONCE() and to return a bool (see also my reply in the patch [0/17]).
Quoted 37 lines
Quoted 37 lines
Quoted 37 lines
Quoted 6 lines
Signed-off-by Suren Baghdasaryan <[email protected]>Reviewed-by Vlastimil Babka <[email protected]>But regardless, prima facie, this looks fine, so:
Reviewed-by Lorenzo Stoakes <[email protected]>Quoted 26 lines
--- mm/vma.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)mm/vma.c +4
-3@@ -413,10 +413,12 @@ void remove_vma(struct vm_area_struct *vma, bool unreachable)if (vma->vm_file)fput (vma->vm_file);mpol_put (vma_policy (vma)); -if (unreachable) +if (unreachable) { +vma_mark_detached (vma);__vm_area_free (vma); -else + }else {vm_area_free (vma); + } }/* -- 2.47.1.613.gc27f4b7a9f-goog
Re: [PATCH v9 05/17] mm: mark vmas detached upon exit
Quoted 24 lines · Suren Baghdasaryan
Quoted 22 lines · Lorenzo Stoakes
Quoted 11 lines · Suren Baghdasaryan
Quoted 9 lines · Lorenzo Stoakes
Quoted 6 lines · Suren Baghdasaryan
When
exit_mmap()removes vmas belonging to an exiting task, it does not mark them as detached since they can't be reached by other tasks and they will be freed shortly. Once we introduce vma reuse, all vmas will have to be in detached state before they are freed to ensure vma when reused is in a consistent state. Add missingvma_mark_detached()before freeing the vma.Quoted 6 lines
Quoted 2 lines
Hmm this really makes me worry that we'll see bugs from this detached stuff, do we make this assumption anywhere else I wonder?
This is the only place which does not currently detach the vma before freeing it. If someone tries adding a case like that in the future, they will be met with
vma_assert_detached()insidevm_area_free().OK good to know!
Quoted 7 lines
Quoted 3 lines
Again, I wonder if we should make these assertions stronger as commented elsewhere, because if we see them in production isn't that worth an actual non-debug
WARN_ON_ONCE()?Sure. I'll change vma_assert_attached()/vma_assert_detached() to use
WARN_ON_ONCE()and to return a bool (see also my reply in the patch [0/17]).
So is this a case of "someone might introduce code later that will violate them" as alluded to above? Unconditional WARN_ON_ONCE seems too much then.
In general it's not easy to determine how paranoid we should be in non-debug code, but I'm not sure what's the need here specifically.
Suren Baghdasaryan
maintainerRe: [PATCH v9 05/17] mm: mark vmas detached upon exit
Quoted 30 lines · Vlastimil Babka
Quoted 28 lines · Suren Baghdasaryan
Quoted 25 lines · Lorenzo Stoakes
Quoted 11 lines · Suren Baghdasaryan
Quoted 9 lines · Lorenzo Stoakes
Quoted 6 lines · Suren Baghdasaryan
When
exit_mmap()removes vmas belonging to an exiting task, it does not mark them as detached since they can't be reached by other tasks and they will be freed shortly. Once we introduce vma reuse, all vmas will have to be in detached state before they are freed to ensure vma when reused is in a consistent state. Add missingvma_mark_detached()before freeing the vma.Quoted 6 lines
Quoted 2 lines
Hmm this really makes me worry that we'll see bugs from this detached stuff, do we make this assumption anywhere else I wonder?
This is the only place which does not currently detach the vma before freeing it. If someone tries adding a case like that in the future, they will be met with
vma_assert_detached()insidevm_area_free().OK good to know!
Again, I wonder if we should make these assertions stronger as commented elsewhere, because if we see them in production isn't that worth an actual non-debug
WARN_ON_ONCE()?
Quoted 6 lines
Quoted 3 lines
Sure. I'll change vma_assert_attached()/vma_assert_detached() to use
WARN_ON_ONCE()and to return a bool (see also my reply in the patch [0/17]).So is this a case of "someone might introduce code later that will violate them" as alluded to above? Unconditional
WARN_ON_ONCEseems too much then.
Yes, I wanted to make sure refcounting will not be broken by someone doing re-attach/re-detach.
Quoted 3 lines
In general it's not easy to determine how paranoid we should be in non-debug code, but I'm not sure what's the need here specifically.
I'm not sure how strict we should be but we definitely should try to catch refcounting mistakes and that's my goal here.
Quoted 1 line
Re: [PATCH v9 05/17] mm: mark vmas detached upon exit
Quoted 31 lines · Suren Baghdasaryan
Quoted 30 lines · Vlastimil Babka
Quoted 28 lines · Suren Baghdasaryan
Quoted 25 lines · Lorenzo Stoakes
Quoted 11 lines · Suren Baghdasaryan
Quoted 9 lines · Lorenzo Stoakes
Quoted 6 lines · Suren Baghdasaryan
When
exit_mmap()removes vmas belonging to an exiting task, it does not mark them as detached since they can't be reached by other tasks and they will be freed shortly. Once we introduce vma reuse, all vmas will have to be in detached state before they are freed to ensure vma when reused is in a consistent state. Add missingvma_mark_detached()before freeing the vma.Quoted 6 lines
Quoted 2 lines
Hmm this really makes me worry that we'll see bugs from this detached stuff, do we make this assumption anywhere else I wonder?
This is the only place which does not currently detach the vma before freeing it. If someone tries adding a case like that in the future, they will be met with
vma_assert_detached()insidevm_area_free().OK good to know!
Again, I wonder if we should make these assertions stronger as commented elsewhere, because if we see them in production isn't that worth an actual non-debug
WARN_ON_ONCE()?
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
Sure. I'll change vma_assert_attached()/vma_assert_detached() to use
WARN_ON_ONCE()and to return a bool (see also my reply in the patch [0/17]).So is this a case of "someone might introduce code later that will violate them" as alluded to above? Unconditional
WARN_ON_ONCEseems too much then.
My concern is that there is a broken case that remains hidden because nothing is actually checked in production, which would then become really difficult to debug should somebody report it.
We intend the WARN_ONxxx() functions to be asserting things that -should not be- for precisely this kind of purpose so I think it makes sense here.
Quoted 3 lines
Yes, I wanted to make sure refcounting will not be broken by someone doing re-attach/re-detach.
Yes, and debugging this without it could be really horrible.
Quoted 7 lines
Quoted 4 lines
In general it's not easy to determine how paranoid we should be in non-debug code, but I'm not sure what's the need here specifically.
I'm not sure how strict we should be but we definitely should try to catch refcounting mistakes and that's my goal here.
Yes I think it is worth it here (obviously :)
Quoted 2 lines
Quoted 2 lines
Suren Baghdasaryan
maintainerPATCH v9 6/17 types: move struct rcuwait into types.h
In reply to Suren BaghdasaryanMove rcuwait struct definition into types.h so that rcuwait can be used without including rcuwait.h which includes other headers. Without this change mm_types.h can't use rcuwait due to a the following circular dependency:
mm_types.h -> rcuwait.h -> signal.h -> mm_types.h
--- include/linux/rcuwait.h | 13 +------------ include/linux/types.h | 12 ++++++++++++ 2 files changed, 13 insertions(+), 12 deletions(-)
include/linux/rcuwait.h +1 -12
@@ -4,18 +4,7 @@#include <linux/rcupdate.h> #include <linux/sched/signal.h> - -/* -* rcuwait provides a way of blocking and waking up a single -* task in an rcu-safe manner. -* -* The only time @task is non-nil is when a user is blocked (or -* checking if it needs to) on a condition, and reset as soon as we -* know that the condition has succeeded and are awoken. -*/ -struct rcuwait { -struct task_struct __rcu *task; -}; +#include <linux/types.h> #define __RCUWAIT_INITIALIZER (name) \ { .task = NULL, }
include/linux/types.h +12 -0
@@ -248,5 +248,17 @@ typedef void (*swap_func_t)(void *a, void *b, int size);typedef int (*cmp_r_func_t )(const void *a,const void *b,const void *priv);typedef int (*cmp_func_t )(const void *a,const void *b); +/* +* rcuwait provides a way of blocking and waking up a single +* task in an rcu-safe manner. +* +* The only time @task is non-nil is when a user is blocked (or +* checking if it needs to) on a condition, and reset as soon as we +* know that the condition has succeeded and are awoken. +*/ +struct rcuwait { +struct task_struct __rcu *task; +}; +#endif /* __ASSEMBLY__ */ #endif /* _LINUX_TYPES_H */
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
Re: [PATCH v9 06/17] types: move struct rcuwait into types.h
Quoted 6 lines · Suren Baghdasaryan
Move rcuwait struct definition into
types.hso that rcuwait can be used without includingrcuwait.hwhich includes other headers. Without this changemm_types.hcan't use rcuwait due to a the following circular dependency:
mm_types.h->rcuwait.h->signal.h->mm_types.h
Thanks for including details of motivation for this move :)
Quoted 5 lines
Suggested-by Matthew Wilcox <[email protected]>Signed-off-by Suren Baghdasaryan <[email protected]>Acked-by Davidlohr Bueso <[email protected]>Acked-by Liam R. Howlett <[email protected]>
Quoted 54 lines
--- include/linux/rcuwait.h | 13 +------------ include/linux/types.h | 12 ++++++++++++ 2 files changed, 13 insertions(+), 12 deletions(-)include/linux/rcuwait.h +1
-12@@ -4,18 +4,7 @@#include <linux/rcupdate.h> #include <linux/sched/signal.h> - -/* -* rcuwait provides a way of blocking and waking up a single -* task in an rcu-safe manner. -* -* The only time @task is non-nil is when a user is blocked (or -* checking if it needs to) on a condition, and reset as soon as we -* know that the condition has succeeded and are awoken. -*/ -struct rcuwait { -struct task_struct __rcu *task; -}; +#include <linux/types.h> #define __RCUWAIT_INITIALIZER (name) \ { .task = NULL, }include/linux/types.h +12
-1@@ -248,5 +248,17 @@ typedef void (*swap_func_t)(void *a, void *b, int size);typedef int (*cmp_r_func_t )(const void *a,const void *b,const void *priv);typedef int (*cmp_func_t )(const void *a,const void *b); +/* +* rcuwait provides a way of blocking and waking up a single +* task in an rcu-safe manner. +* +* The only time @task is non-nil is when a user is blocked (or +* checking if it needs to) on a condition, and reset as soon as we +* know that the condition has succeeded and are awoken. +*/ +struct rcuwait { +struct task_struct __rcu *task; +}; +#endif /* __ASSEMBLY__ */ #endif /* _LINUX_TYPES_H */ --2.47.1.613.gc27f4b7a9f-goog
Suren Baghdasaryan
maintainerPATCH v9 7/17 mm: allow vma_start_read_locked/vma_start_read_locked_nested to fail
In reply to Suren BaghdasaryanWith upcoming replacement of vm_lock with vm_refcnt, we need to handle a possibility of vma_start_read_locked/vma_start_read_locked_nested failing due to refcount overflow. Prepare for such possibility by changing these APIs and adjusting their users.
--- include/linux/mm.h | 6 ++++-- mm/userfaultfd.c | 18 +++++++++++++----- 2 files changed, 17 insertions(+), 7 deletions(-)
include/linux/mm.h +4 -2
@@ -747,10 +747,11 @@ static inline bool vma_start_read(struct vm_area_struct *vma)* not be used in such cases because it might fail due to mm_lock_seq overflow. * This functionality is used to obtain vma read lock and drop the mmap read lock. */ -static inline void vma_start_read_locked_nested (struct vm_area_struct *vma,int subclass) +static inline bool vma_start_read_locked_nested (struct vm_area_struct *vma,int subclass) {mmap_assert_locked (vma->vm_mm);down_read_nested (&vma->vm_lock.lock, subclass); +return true; }/*
@@ -759,10 +760,11 @@ static inline void vma_start_read_locked_nested(struct vm_area_struct *vma, int* not be used in such cases because it might fail due to mm_lock_seq overflow. * This functionality is used to obtain vma read lock and drop the mmap read lock. */ -static inline void vma_start_read_locked (struct vm_area_struct *vma) +static inline bool vma_start_read_locked (struct vm_area_struct *vma) {mmap_assert_locked (vma->vm_mm);down_read (&vma->vm_lock.lock); +return true; }static inline void vma_end_read (struct vm_area_struct *vma)
mm/userfaultfd.c +13 -5
@@ -85,7 +85,8 @@ static struct vm_area_struct *uffd_lock_vma(struct mm_struct *mm,mmap_read_lock (mm); vma =find_vma_and_prepare_anon (mm, address);if (!IS_ERR (vma)) -vma_start_read_locked (vma); +if (!vma_start_read_locked (vma)) + vma =ERR_PTR (-EAGAIN );mmap_read_unlock (mm);return vma;
@@ -1483,10 +1484,17 @@ static int uffd_move_lock(struct mm_struct *mm,mmap_read_lock (mm); err =find_vmas_mm_locked (mm, dst_start, src_start, dst_vmap, src_vmap);if (!err) { -vma_start_read_locked (*dst_vmap); -if (*dst_vmap != *src_vmap) -vma_start_read_locked_nested (*src_vmap, -SINGLE_DEPTH_NESTING ); +if (vma_start_read_locked (*dst_vmap)) { +if (*dst_vmap != *src_vmap) { +if (!vma_start_read_locked_nested (*src_vmap, +SINGLE_DEPTH_NESTING )) { +vma_end_read (*dst_vmap); + err = -EAGAIN ; + } + } + }else { + err = -EAGAIN ; + } }mmap_read_unlock (mm);return err;
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
Re: [PATCH v9 07/17] mm: allow vma_start_read_locked/vma_start_read_locked_nested to fail
Quoted 48 lines · Suren Baghdasaryan
With upcoming replacement of
vm_lockwithvm_refcnt, we need to handle a possibility of vma_start_read_locked/vma_start_read_locked_nested failing due to refcount overflow. Prepare for such possibility by changing these APIs and adjusting their users.Signed-off-by Suren Baghdasaryan <[email protected]>Acked-by Vlastimil Babka <[email protected]>Cc Lokesh Gidra <[email protected]>--- include/linux/mm.h | 6 ++++-- mm/userfaultfd.c | 18 +++++++++++++----- 2 files changed, 17 insertions(+), 7 deletions(-)include/linux/mm.h +4
-2@@ -747,10 +747,11 @@ static inline bool vma_start_read(struct vm_area_struct *vma)* not be used in such cases because it might fail due to mm_lock_seq overflow. * This functionality is used to obtain vma read lock and drop the mmap read lock. */ -static inline void vma_start_read_locked_nested (struct vm_area_struct *vma,int subclass) +static inline bool vma_start_read_locked_nested (struct vm_area_struct *vma,int subclass) {mmap_assert_locked (vma->vm_mm);down_read_nested (&vma->vm_lock.lock, subclass); +return true; }/* @@ -759,10 +760,11 @@ static inline void vma_start_read_locked_nested(struct vm_area_struct *vma, int* not be used in such cases because it might fail due to mm_lock_seq overflow. * This functionality is used to obtain vma read lock and drop the mmap read lock. */ -static inline void vma_start_read_locked (struct vm_area_struct *vma) +static inline bool vma_start_read_locked (struct vm_area_struct *vma) {mmap_assert_locked (vma->vm_mm);down_read (&vma->vm_lock.lock); +return true; }static inline void vma_end_read (struct vm_area_struct *vma)mm/userfaultfd.c +0
-0
mm/userfaultfd.c static struct vm_area_struct *uffd_lock_vma(struct mm_struct *mm, · line 89
Quoted 6 lines
mm/userfaultfd.c +2
-1@@ -85,7 +85,8 @@ static struct vm_area_struct *uffd_lock_vma(struct mm_struct *mm,mmap_read_lock (mm); vma =find_vma_and_prepare_anon (mm, address);if (!IS_ERR (vma)) -vma_start_read_locked (vma); +if (!vma_start_read_locked (vma)) + vma =ERR_PTR (-EAGAIN );
Nit but this kind of reads a bit weirdly now:
if (!IS_ERR(vma)) if (!vma_start_read_locked(vma)) vma = ERR_PTR(-EAGAIN);
Wouldn't this be nicer as:
if (!IS_ERR(vma) && !vma_start_read_locked(vma)) vma = ERR_PTR(-EAGAIN);
On the other hand, this embeds an action in an expression, but then it sort of still looks weird.
if (!IS_ERR(vma)) {
bool ok = vma_start_read_locked(vma);
if (!ok)
vma = ERR_PTR(-EAGAIN);
}This makes me wonder, now yes, we are truly bikeshedding, sorry, but maybe we could just have vma_start_read_locked return a VMA pointer that could be an error?
Then this becomes:
if (!IS_ERR(vma)) vma = vma_start_read_locked(vma);
Quoted 6 lines
mm/userfaultfd.c +0
-0mmap_read_unlock (mm);return vma;@@ -1483,10 +1484,17 @@ static int uffd_move_lock(struct mm_struct *mm,mmap_read_lock (mm); err =find_vmas_mm_locked (mm, dst_start, src_start, dst_vmap, src_vmap);
mm/userfaultfd.c static int uffd_move_lock(struct mm_struct *mm, · line 1491
Quoted 10 lines
mm/userfaultfd.c +5
-4if (!err) { -vma_start_read_locked (*dst_vmap); -if (*dst_vmap != *src_vmap) -vma_start_read_locked_nested (*src_vmap, -SINGLE_DEPTH_NESTING ); +if (vma_start_read_locked (*dst_vmap)) { +if (*dst_vmap != *src_vmap) { +if (!vma_start_read_locked_nested (*src_vmap, +SINGLE_DEPTH_NESTING )) { +vma_end_read (*dst_vmap);
Hmm, why do we end read if the lock failed here but not above?
mm/userfaultfd.c static int uffd_move_lock(struct mm_struct *mm, · line 1498
Quoted 7 lines
mm/userfaultfd.c +6
-0+ err = -EAGAIN ; + } + } + }else { + err = -EAGAIN ; + } }
This whole block is really ugly now, this really needs refactoring.
How about (on assumption the vma_end_read() is correct):
err = find_vmas_mm_locked(mm, dst_start, src_start, dst_vmap, src_vmap);
if (err)
goto out;
if (!vma_start_read_locked(*dst_vmap)) {
err = -EAGAIN;
goto out;
}
/* Nothing further to do. */
if (*dst_vmap == *src_vmap)
goto out;
if (!vma_start_read_locked_nested(*src_vmap,
SINGLE_DEPTH_NESTING)) {
vma_end_read(*dst_vmap);
err = -EAGAIN;
}out:
mmap_read_unlock(mm); return err; }
Quoted 5 lines
mm/userfaultfd.c +0
-1mmap_read_unlock (mm);return err; --2.47.1.613.gc27f4b7a9f-goog
Suren Baghdasaryan
maintainerRe: [PATCH v9 07/17] mm: allow vma_start_read_locked/vma_start_read_locked_nested to fail
Quoted 78 lines · Lorenzo Stoakes
Quoted 48 lines · Suren Baghdasaryan
With upcoming replacement of
vm_lockwithvm_refcnt, we need to handle a possibility of vma_start_read_locked/vma_start_read_locked_nested failing due to refcount overflow. Prepare for such possibility by changing these APIs and adjusting their users.Signed-off-by Suren Baghdasaryan <[email protected]>Acked-by Vlastimil Babka <[email protected]>Cc Lokesh Gidra <[email protected]>--- include/linux/mm.h | 6 ++++-- mm/userfaultfd.c | 18 +++++++++++++----- 2 files changed, 17 insertions(+), 7 deletions(-)include/linux/mm.h +4
-2@@ -747,10 +747,11 @@ static inline bool vma_start_read(struct vm_area_struct *vma)* not be used in such cases because it might fail due to mm_lock_seq overflow. * This functionality is used to obtain vma read lock and drop the mmap read lock. */ -static inline void vma_start_read_locked_nested (struct vm_area_struct *vma,int subclass) +static inline bool vma_start_read_locked_nested (struct vm_area_struct *vma,int subclass) {mmap_assert_locked (vma->vm_mm);down_read_nested (&vma->vm_lock.lock, subclass); +return true; }/* @@ -759,10 +760,11 @@ static inline void vma_start_read_locked_nested(struct vm_area_struct *vma, int* not be used in such cases because it might fail due to mm_lock_seq overflow. * This functionality is used to obtain vma read lock and drop the mmap read lock. */ -static inline void vma_start_read_locked (struct vm_area_struct *vma) +static inline bool vma_start_read_locked (struct vm_area_struct *vma) {mmap_assert_locked (vma->vm_mm);down_read (&vma->vm_lock.lock); +return true; }static inline void vma_end_read (struct vm_area_struct *vma)mm/userfaultfd.c +0
-0mm/userfaultfd.c static struct vm_area_struct *uffd_lock_vma(struct mm_struct *mm, · line 89
Quoted 6 lines
mm/userfaultfd.c +2
-1@@ -85,7 +85,8 @@ static struct vm_area_struct *uffd_lock_vma(struct mm_struct *mm,mmap_read_lock (mm); vma =find_vma_and_prepare_anon (mm, address);if (!IS_ERR (vma)) -vma_start_read_locked (vma); +if (!vma_start_read_locked (vma)) + vma =ERR_PTR (-EAGAIN );Nit but this kind of reads a bit weirdly now:
if (!IS_ERR(vma)) if (!vma_start_read_locked(vma)) vma = ERR_PTR(-EAGAIN);Wouldn't this be nicer as:
if (!IS_ERR(vma) && !vma_start_read_locked(vma)) vma = ERR_PTR(-EAGAIN);On the other hand, this embeds an action in an expression, but then it sort of still looks weird.
if (!IS_ERR(vma)) { bool ok = vma_start_read_locked(vma); if (!ok) vma = ERR_PTR(-EAGAIN); }
Quoted 8 lines
This makes me wonder, now yes, we are truly bikeshedding, sorry, but maybe we could just have
vma_start_read_lockedreturn a VMA pointer that could be an error?Then this becomes:
if (!IS_ERR(vma)) vma = vma_start_read_locked(vma);
No, I think it would be wrong for vma_start_read_locked() to always return EAGAIN when it can't lock the vma. The error code here is context-dependent, so while EAGAIN is the right thing here, it might not work for other future users.
Quoted 11 lines
Quoted 11 lines
mm/userfaultfd.c +0
-3mmap_read_unlock (mm);return vma;@@ -1483,10 +1484,17 @@ static int uffd_move_lock(struct mm_struct *mm,mmap_read_lock (mm); err =find_vmas_mm_locked (mm, dst_start, src_start, dst_vmap, src_vmap);if (!err) { -vma_start_read_locked (*dst_vmap); -if (*dst_vmap != *src_vmap) - vma_start_read_locked_nested(*src_vmap,
Quoted 8 lines
mm/userfaultfd.c static int uffd_move_lock(struct mm_struct *mm, · line 1491
Quoted 6 lines
mm/userfaultfd.c +5
-1- SINGLE_DEPTH_NESTING); +if (vma_start_read_locked (*dst_vmap)) { +if (*dst_vmap != *src_vmap) { +if (!vma_start_read_locked_nested (*src_vmap, +SINGLE_DEPTH_NESTING )) { +vma_end_read (*dst_vmap);Hmm, why do we end read if the lock failed here but not above?
We have successfully done vma_start_read_locked(dst_vmap) (we locked dest vma) but we failed to do vma_start_read_locked_nested(src_vmap) (we could not lock src vma). So we should undo the dest vma locking. Does that clarify the logic?
Quoted 33 lines
mm/userfaultfd.c static int uffd_move_lock(struct mm_struct *mm, · line 1498
Quoted 8 lines
mm/userfaultfd.c +6
-0+ err = -EAGAIN ; + } + } + }else { + err = -EAGAIN ; + } }This whole block is really ugly now, this really needs refactoring.
How about (on assumption the
vma_end_read()is correct):err = find_vmas_mm_locked(mm, dst_start, src_start, dst_vmap, src_vmap); if (err) goto out; if (!vma_start_read_locked(*dst_vmap)) { err = -EAGAIN; goto out; } /* Nothing further to do. */ if (*dst_vmap == *src_vmap) goto out; if (!vma_start_read_locked_nested(*src_vmap, SINGLE_DEPTH_NESTING)) { vma_end_read(*dst_vmap); err = -EAGAIN; }
Quoted 4 lines
out:
mmap_read_unlock(mm); return err; }
Ok, that looks good to me. Will change this way.
Thanks!
Quoted 6 lines
Quoted 6 lines
mm/userfaultfd.c +0
-1mmap_read_unlock (mm);return err; --2.47.1.613.gc27f4b7a9f-goog
Re: [PATCH v9 07/17] mm: allow vma_start_read_locked/vma_start_read_locked_nested to fail
Quoted 86 lines · Suren Baghdasaryan
Quoted 84 lines · Lorenzo Stoakes
Quoted 48 lines · Suren Baghdasaryan
With upcoming replacement of
vm_lockwithvm_refcnt, we need to handle a possibility of vma_start_read_locked/vma_start_read_locked_nested failing due to refcount overflow. Prepare for such possibility by changing these APIs and adjusting their users.Signed-off-by Suren Baghdasaryan <[email protected]>Acked-by Vlastimil Babka <[email protected]>Cc Lokesh Gidra <[email protected]>--- include/linux/mm.h | 6 ++++-- mm/userfaultfd.c | 18 +++++++++++++----- 2 files changed, 17 insertions(+), 7 deletions(-)include/linux/mm.h +4
-2@@ -747,10 +747,11 @@ static inline bool vma_start_read(struct vm_area_struct *vma)* not be used in such cases because it might fail due to mm_lock_seq overflow. * This functionality is used to obtain vma read lock and drop the mmap read lock. */ -static inline void vma_start_read_locked_nested (struct vm_area_struct *vma,int subclass) +static inline bool vma_start_read_locked_nested (struct vm_area_struct *vma,int subclass) {mmap_assert_locked (vma->vm_mm);down_read_nested (&vma->vm_lock.lock, subclass); +return true; }/* @@ -759,10 +760,11 @@ static inline void vma_start_read_locked_nested(struct vm_area_struct *vma, int* not be used in such cases because it might fail due to mm_lock_seq overflow. * This functionality is used to obtain vma read lock and drop the mmap read lock. */ -static inline void vma_start_read_locked (struct vm_area_struct *vma) +static inline bool vma_start_read_locked (struct vm_area_struct *vma) {mmap_assert_locked (vma->vm_mm);down_read (&vma->vm_lock.lock); +return true; }static inline void vma_end_read (struct vm_area_struct *vma)mm/userfaultfd.c +0
-0mm/userfaultfd.c static struct vm_area_struct *uffd_lock_vma(struct mm_struct *mm, · line 89
Quoted 6 lines
mm/userfaultfd.c +2
-1@@ -85,7 +85,8 @@ static struct vm_area_struct *uffd_lock_vma(struct mm_struct *mm,mmap_read_lock (mm); vma =find_vma_and_prepare_anon (mm, address);if (!IS_ERR (vma)) -vma_start_read_locked (vma); +if (!vma_start_read_locked (vma)) + vma =ERR_PTR (-EAGAIN );Nit but this kind of reads a bit weirdly now:
if (!IS_ERR(vma)) if (!vma_start_read_locked(vma)) vma = ERR_PTR(-EAGAIN);Wouldn't this be nicer as:
if (!IS_ERR(vma) && !vma_start_read_locked(vma)) vma = ERR_PTR(-EAGAIN);On the other hand, this embeds an action in an expression, but then it sort of still looks weird.
if (!IS_ERR(vma)) { bool ok = vma_start_read_locked(vma); if (!ok) vma = ERR_PTR(-EAGAIN); }This makes me wonder, now yes, we are truly bikeshedding, sorry, but maybe we could just have
vma_start_read_lockedreturn a VMA pointer that could be an error?Then this becomes:
Quoted 7 lines
Quoted 2 lines
if (!IS_ERR(vma)) vma = vma_start_read_locked(vma);No, I think it would be wrong for
vma_start_read_locked()to always return EAGAIN when it can't lock the vma. The error code here is context-dependent, so while EAGAIN is the right thing here, it might not work for other future users.
Ack, makes sense.
But it'd be nice to clean this up so it isn't this arrow-shaped-code thing. I mean obviously this is subjective and sorry to bikeshed this late in a series... but :)
Are you with:
if (!IS_ERR(vma)) {
bool ok = vma_start_read_locked(vma);
if (!ok)
vma = ERR_PTR(-EAGAIN);
}?
I think this reads better.
Sorry to be a pain! :)
Quoted 21 lines
Quoted 12 lines
Quoted 12 lines
mm/userfaultfd.c +0
-3mmap_read_unlock (mm);return vma;@@ -1483,10 +1484,17 @@ static int uffd_move_lock(struct mm_struct *mm,mmap_read_lock (mm); err =find_vmas_mm_locked (mm, dst_start, src_start, dst_vmap, src_vmap);if (!err) { -vma_start_read_locked (*dst_vmap); -if (*dst_vmap != *src_vmap) - vma_start_read_locked_nested(*src_vmap,Quoted 8 lines
mm/userfaultfd.c static int uffd_move_lock(struct mm_struct *mm, · line 1491
Quoted 6 lines
mm/userfaultfd.c +5
-1- SINGLE_DEPTH_NESTING); +if (vma_start_read_locked (*dst_vmap)) { +if (*dst_vmap != *src_vmap) { +if (!vma_start_read_locked_nested (*src_vmap, +SINGLE_DEPTH_NESTING )) { +vma_end_read (*dst_vmap);Hmm, why do we end read if the lock failed here but not above?
Quoted 4 lines
We have successfully done
vma_start_read_locked(dst_vmap)(we locked dest vma) but we failed to dovma_start_read_locked_nested(src_vmap)(we could not lock src vma). So we should undo the dest vma locking. Does that clarify the logic?
Ahh right makes sense. Maybe a quick cheeky comment to that effect here too?
Quoted 34 lines
Quoted 34 lines
mm/userfaultfd.c static int uffd_move_lock(struct mm_struct *mm, · line 1498
Quoted 9 lines
mm/userfaultfd.c +6
-0+ err = -EAGAIN ; + } + } + }else { + err = -EAGAIN ; + } }This whole block is really ugly now, this really needs refactoring.
How about (on assumption the
vma_end_read()is correct):err = find_vmas_mm_locked(mm, dst_start, src_start, dst_vmap, src_vmap); if (err) goto out; if (!vma_start_read_locked(*dst_vmap)) { err = -EAGAIN; goto out; } /* Nothing further to do. */ if (*dst_vmap == *src_vmap) goto out; if (!vma_start_read_locked_nested(*src_vmap, SINGLE_DEPTH_NESTING)) { vma_end_read(*dst_vmap); err = -EAGAIN; }
Quoted 8 lines
Quoted 4 lines
out:
mmap_read_unlock(mm); return err; }Ok, that looks good to me. Will change this way.
Thanks!
Thanks!
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
mm/userfaultfd.c +0
-1mmap_read_unlock (mm);return err; --2.47.1.613.gc27f4b7a9f-goog
Suren Baghdasaryan
maintainerPATCH v9 8/17 mm: move mmap_init_lock() out of the header file
In reply to Suren Baghdasaryanmmap_init_lock() is used only from mm_init() in fork.c, therefore it does not have to reside in the header file. This move lets us avoid including additional headers in mmap_lock.h later, when mmap_init_lock() needs to initialize rcuwait object.
--- include/linux/mmap_lock.h | 6 ------ kernel/fork.c | 6 ++++++ 2 files changed, 6 insertions(+), 6 deletions(-)
include/linux/mmap_lock.h +0 -6
@@ -122,12 +122,6 @@ static inline bool mmap_lock_speculate_retry(struct mm_struct *mm, unsigned int#endif /* CONFIG_PER_VMA_LOCK */ -static inline void mmap_init_lock (struct mm_struct *mm) -{ -init_rwsem (&mm->mmap_lock); -mm_lock_seqcount_init (mm); -} -static inline void mmap_write_lock (struct mm_struct *mm) {__mmap_lock_trace_start_locking (mm, true);
kernel/fork.c +6 -0
@@ -1219,6 +1219,12 @@ static void mm_init_uprobes_state(struct mm_struct *mm)#endif } +static inline void mmap_init_lock (struct mm_struct *mm) +{ +init_rwsem (&mm->mmap_lock); +mm_lock_seqcount_init (mm); +} +static struct mm_struct *mm_init (struct mm_struct *mm,struct task_struct *p,struct user_namespace *user_ns) {
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
Re: [PATCH v9 08/17] mm: move mmap_init_lock() out of the header file
Quoted 7 lines · Suren Baghdasaryan
mmap_init_lock()is used only frommm_init()infork.c, therefore it does not have to reside in the header file. This move lets us avoid including additional headers inmmap_lock.hlater, whenmmap_init_lock()needs to initialize rcuwait object.Signed-off-by Suren Baghdasaryan <[email protected]>Reviewed-by Vlastimil Babka <[email protected]>
Aside from nit below, LGTM:
Quoted 27 lines
--- include/linux/mmap_lock.h | 6 ------ kernel/fork.c | 6 ++++++ 2 files changed, 6 insertions(+), 6 deletions(-)include/linux/mmap_lock.h +0
-6@@ -122,12 +122,6 @@ static inline bool mmap_lock_speculate_retry(struct mm_struct *mm, unsigned int#endif /* CONFIG_PER_VMA_LOCK */ -static inline void mmap_init_lock (struct mm_struct *mm) -{ -init_rwsem (&mm->mmap_lock); -mm_lock_seqcount_init (mm); -} -static inline void mmap_write_lock (struct mm_struct *mm) {__mmap_lock_trace_start_locking (mm, true);kernel/fork.c +0
-0
kernel/fork.c static void mm_init_uprobes_state(struct mm_struct *mm) · line 1222
Quoted 4 lines
kernel/fork.c +1
-0@@ -1219,6 +1219,12 @@ static void mm_init_uprobes_state(struct mm_struct *mm)#endif } +static inline void mmap_init_lock (struct mm_struct *mm)
we don't need inline here, please drop it.
Quoted 11 lines
kernel/fork.c +5
-1+{ +init_rwsem (&mm->mmap_lock); +mm_lock_seqcount_init (mm); +} +static struct mm_struct *mm_init (struct mm_struct *mm,struct task_struct *p,struct user_namespace *user_ns) { --2.47.1.613.gc27f4b7a9f-goog
Suren Baghdasaryan
maintainerRe: [PATCH v9 08/17] mm: move mmap_init_lock() out of the header file
Quoted 44 lines · Lorenzo Stoakes
Quoted 7 lines · Suren Baghdasaryan
mmap_init_lock()is used only frommm_init()infork.c, therefore it does not have to reside in the header file. This move lets us avoid including additional headers inmmap_lock.hlater, whenmmap_init_lock()needs to initialize rcuwait object.Signed-off-by Suren Baghdasaryan <[email protected]>Reviewed-by Vlastimil Babka <[email protected]>Aside from nit below, LGTM:
Reviewed-by Lorenzo Stoakes <[email protected]>Quoted 30 lines
--- include/linux/mmap_lock.h | 6 ------ kernel/fork.c | 6 ++++++ 2 files changed, 6 insertions(+), 6 deletions(-)include/linux/mmap_lock.h +0
-6@@ -122,12 +122,6 @@ static inline bool mmap_lock_speculate_retry(struct mm_struct *mm, unsigned int#endif /* CONFIG_PER_VMA_LOCK */ -static inline void mmap_init_lock (struct mm_struct *mm) -{ -init_rwsem (&mm->mmap_lock); -mm_lock_seqcount_init (mm); -} -static inline void mmap_write_lock (struct mm_struct *mm) {__mmap_lock_trace_start_locking (mm, true);kernel/fork.c +0
-0@@ -1219,6 +1219,12 @@ static void mm_init_uprobes_state(struct mm_struct *mm)#endif }
Quoted 3 lines
kernel/fork.c static void mm_init_uprobes_state(struct mm_struct *mm) · line 1222
Quoted 1 line
kernel/fork.c +1
-0+static inline void mmap_init_lock (struct mm_struct *mm)we don't need inline here, please drop it.
Ack.
Quoted 12 lines
Quoted 12 lines
kernel/fork.c +5
-1+{ +init_rwsem (&mm->mmap_lock); +mm_lock_seqcount_init (mm); +} +static struct mm_struct *mm_init (struct mm_struct *mm,struct task_struct *p,struct user_namespace *user_ns) { --2.47.1.613.gc27f4b7a9f-goog
Suren Baghdasaryan
maintainerPATCH v9 9/17 mm: uninline the main body of vma_start_write()
In reply to Suren Baghdasaryanvma_start_write() is used in many places and will grow in size very soon. It is not used in performance critical paths and uninlining it should limit the future code size growth.
No functional changes.
--- include/linux/mm.h | 12 +++--------- mm/memory.c | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 9 deletions(-)
include/linux/mm.h +3 -9
@@ -787,6 +787,8 @@ static bool __is_vma_write_locked(struct vm_area_struct *vma, unsigned int *mm_lreturn (vma->vm_lock_seq == *mm_lock_seq); } +void __vma_start_write (struct vm_area_struct *vma,unsigned int mm_lock_seq); +/* * Begin writing to a VMA. * Exclude concurrent readers under the per-VMA lock until the currently
@@ -799,15 +801,7 @@ static inline void vma_start_write(struct vm_area_struct *vma)if (__is_vma_write_locked (vma, &mm_lock_seq))return ; -down_write (&vma->vm_lock.lock); -/* -* We should use WRITE_ONCE() here because we can have concurrent reads -* from the early lockless pessimistic check in vma_start_read(). -* We don't really care about the correctness of that early check, but -* we should use WRITE_ONCE() for cleanliness and to keep KCSAN happy. -*/ -WRITE_ONCE (vma->vm_lock_seq, mm_lock_seq); -up_write (&vma->vm_lock.lock); +__vma_start_write (vma, mm_lock_seq); }static inline void vma_assert_write_locked (struct vm_area_struct *vma)
mm/memory.c +14 -0
@@ -6328,6 +6328,20 @@ struct vm_area_struct *lock_mm_and_find_vma(struct mm_struct *mm,#endif #ifdef CONFIG_PER_VMA_LOCK +void __vma_start_write (struct vm_area_struct *vma,unsigned int mm_lock_seq) +{ +down_write (&vma->vm_lock.lock); +/* +* We should use WRITE_ONCE() here because we can have concurrent reads +* from the early lockless pessimistic check in vma_start_read(). +* We don't really care about the correctness of that early check, but +* we should use WRITE_ONCE() for cleanliness and to keep KCSAN happy. +*/ +WRITE_ONCE (vma->vm_lock_seq, mm_lock_seq); +up_write (&vma->vm_lock.lock); +} +EXPORT_SYMBOL_GPL (__vma_start_write); +/* * Lookup and lock a VMA under RCU protection. Returned VMA is guaranteed to be * stable and not isolated. If the VMA is not found or is being modified the
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
Re: [PATCH v9 09/17] mm: uninline the main body of vma_start_write()
Quoted 7 lines · Suren Baghdasaryan
vma_start_write()is used in many places and will grow in size very soon. It is not used in performance critical paths and uninlining it should limit the future code size growth.
No functional changes.Signed-off-by Suren Baghdasaryan <[email protected]>Reviewed-by Vlastimil Babka <[email protected]>
LGTM,
Quoted 63 lines
--- include/linux/mm.h | 12 +++--------- mm/memory.c | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 9 deletions(-)include/linux/mm.h +3
-9@@ -787,6 +787,8 @@ static bool __is_vma_write_locked(struct vm_area_struct *vma, unsigned int *mm_lreturn (vma->vm_lock_seq == *mm_lock_seq); } +void __vma_start_write (struct vm_area_struct *vma,unsigned int mm_lock_seq); +/* * Begin writing to a VMA. * Exclude concurrent readers under the per-VMA lock until the currently @@ -799,15 +801,7 @@ static inline void vma_start_write(struct vm_area_struct *vma)if (__is_vma_write_locked (vma, &mm_lock_seq))return ; -down_write (&vma->vm_lock.lock); -/* -* We should use WRITE_ONCE() here because we can have concurrent reads -* from the early lockless pessimistic check in vma_start_read(). -* We don't really care about the correctness of that early check, but -* we should use WRITE_ONCE() for cleanliness and to keep KCSAN happy. -*/ -WRITE_ONCE (vma->vm_lock_seq, mm_lock_seq); -up_write (&vma->vm_lock.lock); +__vma_start_write (vma, mm_lock_seq); }static inline void vma_assert_write_locked (struct vm_area_struct *vma)mm/memory.c +14
-1@@ -6328,6 +6328,20 @@ struct vm_area_struct *lock_mm_and_find_vma(struct mm_struct *mm,#endif #ifdef CONFIG_PER_VMA_LOCK +void __vma_start_write (struct vm_area_struct *vma,unsigned int mm_lock_seq) +{ +down_write (&vma->vm_lock.lock); +/* +* We should use WRITE_ONCE() here because we can have concurrent reads +* from the early lockless pessimistic check in vma_start_read(). +* We don't really care about the correctness of that early check, but +* we should use WRITE_ONCE() for cleanliness and to keep KCSAN happy. +*/ +WRITE_ONCE (vma->vm_lock_seq, mm_lock_seq); +up_write (&vma->vm_lock.lock); +} +EXPORT_SYMBOL_GPL (__vma_start_write); +/* * Lookup and lock a VMA under RCU protection. Returned VMA is guaranteed to be * stable and not isolated. If the VMA is not found or is being modified the -- 2.47.1.613.gc27f4b7a9f-goog
Suren Baghdasaryan
maintainerPATCH v9 10/17 refcount: introduce __refcount_{add|inc}_not_zero_limited
In reply to Suren BaghdasaryanIntroduce functions to increase refcount but with a top limit above which they will fail to increase (the limit is inclusive). Setting the limit to INT_MAX indicates no limit.
--- include/linux/refcount.h | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-)
include/linux/refcount.h +23 -1
@@ -137,13 +137,23 @@ static inline unsigned int refcount_read(const refcount_t *r) }static inline __must_check __signed_wrap -bool __refcount_add_not_zero(int i, refcount_t *r, int *oldp) +bool __refcount_add_not_zero_limited(int i, refcount_t *r, int *oldp, +int limit) {int old =refcount_read (r);do {if (!old)break ; + +if (statically_true (limit ==INT_MAX )) +continue ; + +if (i > limit - old) { +if (oldp) + *oldp = old; +return false; + } }while (!atomic_try_cmpxchg_relaxed (&r->refs, &old, old + i));if (oldp)
@@ -155,6 +165,12 @@ bool __refcount_add_not_zero(int i, refcount_t *r, int *oldp)return old; } +static inline __must_check __signed_wrap +bool __refcount_add_not_zero(int i, refcount_t *r, int *oldp) +{ +return __refcount_add_not_zero_limited (i, r, oldp,INT_MAX ); +} +/** * refcount_add_not_zero - add a value to a refcount unless it is 0 * @i: the value to add to the refcount
@@ -213,6 +229,12 @@ static inline void refcount_add(int i, refcount_t *r)__refcount_add (i, r,NULL ); } +static inline __must_check bool __refcount_inc_not_zero_limited (refcount_t *r, +int *oldp,int limit) +{ +return __refcount_add_not_zero_limited (1 , r, oldp, limit); +} +static inline __must_check bool __refcount_inc_not_zero (refcount_t *r,int *oldp) {return __refcount_add_not_zero (1 , r, oldp);
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
Re: [PATCH v9 10/17] refcount: introduce __refcount_{add|inc}_not_zero_limited
On Fri, 10 Jan 2025 20:25:57 -0800 Suren Baghdasaryan <[email protected]>
Quoted 9 lines
include/linux/refcount.h +2
-1-bool __refcount_add_not_zero (int i,refcount_t *r,int *oldp) +bool __refcount_add_not_zero_limited (int i,refcount_t *r,int *oldp, +int limit) {int old =refcount_read (r); do { if (!old) break;
include/linux/refcount.h static inline unsigned int refcount_read(const refcount_t *r) · line 157
Quoted 10 lines
include/linux/refcount.h +9
-0+ +if (statically_true (limit ==INT_MAX )) +continue ; + +if (i > limit - old) { +if (oldp) + *oldp = old; +return false; + } }while (!atomic_try_cmpxchg_relaxed (&r->refs, &old, old + i));
The acquire version should be used, see atomic_long_try_cmpxchg_acquire() in kernel/locking/rwsem.c.
Why not use the atomic_long_t without bothering to add this limited version?
Suren Baghdasaryan
maintainerRe: [PATCH v9 10/17] refcount: introduce __refcount_{add|inc}_not_zero_limited
Quoted 8 lines · Hillf Danton
On Fri, 10 Jan 2025 20:25:57 -0800 Suren Baghdasaryan <[email protected]>
Quoted 6 lines
include/linux/refcount.h +2
-1-bool __refcount_add_not_zero (int i,refcount_t *r,int *oldp) +bool __refcount_add_not_zero_limited (int i,refcount_t *r,int *oldp, +int limit) {int old =refcount_read (r);
Quoted 16 lines
include/linux/refcount.h static inline unsigned int refcount_read(const refcount_t *r) · line 157
Quoted 13 lines
do { if (!old) break;include/linux/refcount.h +9
-0+ +if (statically_true (limit ==INT_MAX )) +continue ; + +if (i > limit - old) { +if (oldp) + *oldp = old; +return false; + } }while (!atomic_try_cmpxchg_relaxed (&r->refs, &old, old + i));The acquire version should be used, see
atomic_long_try_cmpxchg_acquire()inkernel/locking/rwsem.c.
This is how __refcount_add_not_zero() is already implemented and I'm only adding support for a limit. If you think it's implemented wrong then IMHO it should be fixed separately.
Quoted 2 lines
Why not use the
atomic_long_twithout bothering to add this limited version?
The check against the limit is not only for overflow protection but also to avoid refcount increment when the writer bit is set. It makes the locking code simpler if we have a function that prevents refcounting when the vma is detached (vm_refcnt==0) or when it's write-locked (vm_refcnt<VMA_REF_LIMIT).
Quoted 3 lines
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
Suren Baghdasaryan
maintainerRe: [PATCH v9 10/17] refcount: introduce __refcount_{add|inc}_not_zero_limited
Quoted 32 lines · Suren Baghdasaryan
Quoted 8 lines · Hillf Danton
On Fri, 10 Jan 2025 20:25:57 -0800 Suren Baghdasaryan <[email protected]>
Quoted 6 lines
include/linux/refcount.h +2
-1-bool __refcount_add_not_zero (int i,refcount_t *r,int *oldp) +bool __refcount_add_not_zero_limited (int i,refcount_t *r,int *oldp, +int limit) {int old =refcount_read (r);Quoted 16 lines
include/linux/refcount.h static inline unsigned int refcount_read(const refcount_t *r) · line 157
Quoted 13 lines
do { if (!old) break;include/linux/refcount.h +9
-0+ +if (statically_true (limit ==INT_MAX )) +continue ; + +if (i > limit - old) { +if (oldp) + *oldp = old; +return false; + } }while (!atomic_try_cmpxchg_relaxed (&r->refs, &old, old + i));The acquire version should be used, see
atomic_long_try_cmpxchg_acquire()inkernel/locking/rwsem.c.This is how
__refcount_add_not_zero()is already implemented and I'm only adding support for a limit. If you think it's implemented wrong then IMHO it should be fixed separately.Quoted 1 line
Quoted 7 lines
Quoted 1 line
Why not use the
atomic_long_twithout bothering to add this limited version?The check against the limit is not only for overflow protection but also to avoid refcount increment when the writer bit is set. It makes the locking code simpler if we have a function that prevents refcounting when the vma is detached (vm_refcnt==0) or when it's write-locked (vm_refcnt<VMA_REF_LIMIT).
s / vm_refcnt<VMA_REF_LIMIT / vm_refcnt>VMA_REF_LIMIT
Quoted 4 lines
Quoted 4 lines
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
Re: [PATCH v9 10/17] refcount: introduce __refcount_{add|inc}_not_zero_limited
In reply to Suren BaghdasaryanOn Sat, 11 Jan 2025 01:59:41 -0800 Suren Baghdasaryan <[email protected]>
Quoted 25 lines · Hillf Danton
Quoted 7 lines
On Fri, 10 Jan 2025 20:25:57 -0800 Suren Baghdasaryan <[email protected]>
Quoted 6 lines
include/linux/refcount.h +2
-1-bool __refcount_add_not_zero (int i,refcount_t *r,int *oldp) +bool __refcount_add_not_zero_limited (int i,refcount_t *r,int *oldp, +int limit) {int old =refcount_read (r);Quoted 16 lines
include/linux/refcount.h static inline unsigned int refcount_read(const refcount_t *r) · line 157
Quoted 13 lines
do { if (!old) break;include/linux/refcount.h +9
-0+ +if (statically_true (limit ==INT_MAX )) +continue ; + +if (i > limit - old) { +if (oldp) + *oldp = old; +return false; + } }while (!atomic_try_cmpxchg_relaxed (&r->refs, &old, old + i));The acquire version should be used, see
atomic_long_try_cmpxchg_acquire()inkernel/locking/rwsem.c.
Quoted 4 lines
This is how
__refcount_add_not_zero()is already implemented and I'm only adding support for a limit. If you think it's implemented wrong then IMHO it should be fixed separately.
Two different things - refcount has nothing to do with locking at the first place, while what you are adding to the mm directory is something that replaces rwsem, so from the locking POV you have to mark the boundaries of the locking section.
Suren Baghdasaryan
maintainerRe: [PATCH v9 10/17] refcount: introduce __refcount_{add|inc}_not_zero_limited
Quoted 27 lines · Hillf Danton
On Sat, 11 Jan 2025 01:59:41 -0800 Suren Baghdasaryan <[email protected]>
Quoted 25 lines · Hillf Danton
Quoted 23 lines
On Fri, 10 Jan 2025 20:25:57 -0800 Suren Baghdasaryan <[email protected]>
Quoted 9 lines
include/linux/refcount.h +2
-1-bool __refcount_add_not_zero (int i,refcount_t *r,int *oldp) +bool __refcount_add_not_zero_limited (int i,refcount_t *r,int *oldp, +int limit) {int old =refcount_read (r); do { if (!old) break;include/linux/refcount.h static inline unsigned int refcount_read(const refcount_t *r) · line 157
Quoted 10 lines
include/linux/refcount.h +9
-0+ +if (statically_true (limit ==INT_MAX )) +continue ; + +if (i > limit - old) { +if (oldp) + *oldp = old; +return false; + } }while (!atomic_try_cmpxchg_relaxed (&r->refs, &old, old + i));The acquire version should be used, see
atomic_long_try_cmpxchg_acquire()inkernel/locking/rwsem.c.
Quoted 8 lines
Quoted 4 lines
This is how
__refcount_add_not_zero()is already implemented and I'm only adding support for a limit. If you think it's implemented wrong then IMHO it should be fixed separately.Two different things - refcount has nothing to do with locking at the first place, while what you are adding to the mm directory is something that replaces rwsem, so from the locking POV you have to mark the boundaries of the locking section.
I see your point. I think it's a strong argument to use atomic directly instead of refcount for this locking. I'll try that and see how it looks. Thanks for the feedback!
Quoted 3 lines
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
Re: [PATCH v9 10/17] refcount: introduce __refcount_{add|inc}_not_zero_limited
On Sat, 11 Jan 2025 09:11:52 -0800 Suren Baghdasaryan <[email protected]>
Quoted 4 lines
I see your point. I think it's a strong argument to use atomic directly instead of refcount for this locking. I'll try that and see how it looks. Thanks for the feedback!
Better not before having a clear answer to why is it sane to invent anything like rwsem in 2025. What, the 40 bytes? Nope it is the fair price paid for finer locking granuality.
BTW Vlastimil, the cc list is cut down because I have to walk around the spam check on the mail agent side.
Suren Baghdasaryan
maintainerRe: [PATCH v9 10/17] refcount: introduce __refcount_{add|inc}_not_zero_limited
Quoted 9 lines · Hillf Danton
On Sat, 11 Jan 2025 09:11:52 -0800 Suren Baghdasaryan <[email protected]>
Quoted 4 lines
I see your point. I think it's a strong argument to use atomic directly instead of refcount for this locking. I'll try that and see how it looks. Thanks for the feedback!
Better not before having a clear answer to why is it sane to invent anything like rwsem in 2025. What, the 40 bytes? Nope it is the fair price paid for finer locking granuality.
It's not just about the 40 bytes. It allows us to fold the separate vma->detached flag nicely into the same refcounter, which consolidates the vma state in one place. Later that makes it much easier to add SLAB_TYPESAFE_BY_RCU because now we have to preserve only this refcounter during the vma reuse.
Quoted 4 lines
BTW Vlastimil, the cc list is cut down because I have to walk around the spam check on the mail agent side.
Peter Zijlstra
maintainerRe: [PATCH v9 10/17] refcount: introduce __refcount_{add|inc}_not_zero_limited
In reply to Suren BaghdasaryanQuoted 32 lines · Suren Baghdasaryan
Quoted 31 lines · Hillf Danton
On Sat, 11 Jan 2025 01:59:41 -0800 Suren Baghdasaryan <[email protected]>
Quoted 29 lines · Hillf Danton
Quoted 7 lines
On Fri, 10 Jan 2025 20:25:57 -0800 Suren Baghdasaryan <[email protected]>
Quoted 6 lines
include/linux/refcount.h +2
-1-bool __refcount_add_not_zero (int i,refcount_t *r,int *oldp) +bool __refcount_add_not_zero_limited (int i,refcount_t *r,int *oldp, +int limit) {int old =refcount_read (r);Quoted 16 lines
include/linux/refcount.h static inline unsigned int refcount_read(const refcount_t *r) · line 157
Quoted 13 lines
do { if (!old) break;include/linux/refcount.h +9
-0+ +if (statically_true (limit ==INT_MAX )) +continue ; + +if (i > limit - old) { +if (oldp) + *oldp = old; +return false; + } }while (!atomic_try_cmpxchg_relaxed (&r->refs, &old, old + i));The acquire version should be used, see
atomic_long_try_cmpxchg_acquire()inkernel/locking/rwsem.c.This is how
__refcount_add_not_zero()is already implemented and I'm only adding support for a limit. If you think it's implemented wrong then IMHO it should be fixed separately.
Quoted 8 lines
Quoted 4 lines
Two different things - refcount has nothing to do with locking at the first place, while what you are adding to the mm directory is something that replaces rwsem, so from the locking POV you have to mark the boundaries of the locking section.
I see your point. I think it's a strong argument to use atomic directly instead of refcount for this locking. I'll try that and see how it looks. Thanks for the feedback!
Sigh; don't let hillf confuse you. *IF* you need an acquire it will be in the part where you wait for readers to go away. But even there, think about what you're serializing against. Readers don't typically modify things.
And modifications are fully serialized by mmap_sem^H^H^Hlock.
Re: [PATCH v9 10/17] refcount: introduce __refcount_{add|inc}_not_zero_limited
On Wed, 15 Jan 2025 10:39:55 +0100 Peter Zijlstra <[email protected]>
Quoted 8 lines
Sigh; don't let hillf confuse you. *IF* you need an acquire it will be in the part where you wait for readers to go away. But even there, think about what you're serializing against. Readers don't typically modify things.
And modifications are fully serialized by mmap_sem^H^H^Hlock.
^H^H^H
- * Do note that inc_not_zero() provides a control dependency which will order
- * future stores against the inc, this ensures we'll never modify the object
- * if we did not in fact acquire a reference.
+ * Do note that inc_not_zero() does provide acquire order, which will order
+ * future load and stores against the inc, this ensures all subsequent accesses
+ * are from this object and not anything previously occupying this memory --
+ * consider SLAB_TYPESAFE_BY_RCU.
*
* The decrements will provide release order, such that all the prior loads and
* stores will be issued before, it also provides a control dependency, which
@@ -144,7 +145,7 @@ bool __refcount_add_not_zero(int i, refcount_t *r, int *oldp)
do {
if (!old)
break;
- } while (!atomic_try_cmpxchg_relaxed(&r->refs, &old, old + i));
+ } while (!atomic_try_cmpxchg_acquire(&r->refs, &old, old + i));
Re: [PATCH v9 10/17] refcount: introduce __refcount_{add|inc}_not_zero_limited
In reply to Suren BaghdasaryanQuoted 3 lines · On Fri, 10 Jan 2025 20:25:57 -0800 Suren Baghdasaryan <[email protected]> wrote:
Introduce functions to increase refcount but with a top limit above which they will fail to increase (the limit is inclusive). Setting the limit to
INT_MAXindicates no limit.
This function has never worked as expected!
I've removed the update and added in the rest of the code.
Quoted 19 lines
include/linux/refcount.h +0
-1@@ -137,13 +137,23 @@ static inline unsigned int refcount_read(const refcount_t *r) } static inline __must_check __signed_wrap -bool __refcount_add_not_zero(int i, refcount_t *r, int *oldp) {int old =refcount_read (r);do {if (!old)break ; }while (!atomic_try_cmpxchg_relaxed (&r->refs, &old, old + i));if (oldp)
Quoted 1 line
*oldp = old;
?
Quoted 5 lines
if (unlikely(old < 0 || old + i < 0)) refcount_warn_saturate(r, REFCOUNT_ADD_NOT_ZERO_OVF);return old;
}
The saturate test just doesn't work as expected.
In C signed integer overflow is undefined (probably so that cpu that saturate/trap signed overflow can be conformant) and gcc uses that to optimise code.
So if you compile (https://www.godbolt.org/z/WYWo84Weq):
int inc_wraps(int i)
{
return i < 0 || i + 1 < 0;
}the second test is optimised away.
I don't think the kernel compiles disable this optimisation.
David
Re: [PATCH v9 10/17] refcount: introduce __refcount_{add|inc}_not_zero_limited
Quoted 1 line · David Laight
I don't think the kernel compiles disable this optimisation.
You're wrong.
Paul E. McKenney
maintainerRe: [PATCH v9 10/17] refcount: introduce __refcount_{add|inc}_not_zero_limited
In reply to David LaightQuoted 41 lines · David Laight
Quoted 3 lines · On Fri, 10 Jan 2025 20:25:57 -0800 Suren Baghdasaryan <[email protected]> wrote:
Introduce functions to increase refcount but with a top limit above which they will fail to increase (the limit is inclusive). Setting the limit to
INT_MAXindicates no limit.This function has never worked as expected!
I've removed the update and added in the rest of the code.Quoted 19 lines
include/linux/refcount.h +0
-1@@ -137,13 +137,23 @@ static inline unsigned int refcount_read(const refcount_t *r) } static inline __must_check __signed_wrap -bool __refcount_add_not_zero(int i, refcount_t *r, int *oldp) {int old =refcount_read (r);do {if (!old)break ; }while (!atomic_try_cmpxchg_relaxed (&r->refs, &old, old + i));if (oldp)Quoted 1 line
*oldp = old;?
Quoted 5 lines
if (unlikely(old < 0 || old + i < 0)) refcount_warn_saturate(r, REFCOUNT_ADD_NOT_ZERO_OVF);return old;
}The saturate test just doesn't work as expected.
In C signed integer overflow is undefined (probably so that cpu that saturate/trap signed overflow can be conformant) and gcc uses that to optimise code.
Quoted 7 lines
So if you compile (https://www.godbolt.org/z/WYWo84Weq):
intinc_wraps(int i){ return i < 0 || i + 1 < 0; }the second test is optimised away.
I don't think the kernel compiles disable this optimisation.
Last I checked, my kernel compiles specified -fno-strict-overflow. What happens if you try that in godbolt?
Thanx, Paul
Re: [PATCH v9 10/17] refcount: introduce __refcount_{add|inc}_not_zero_limited
Quoted 50 lines · On Sat, 11 Jan 2025 10:30:40 -0800 "Paul E. McKenney" <[email protected]>…
Quoted 41 lines · David Laight
Quoted 3 lines · On Fri, 10 Jan 2025 20:25:57 -0800 Suren Baghdasaryan <[email protected]> wrote:
Introduce functions to increase refcount but with a top limit above which they will fail to increase (the limit is inclusive). Setting the limit to
INT_MAXindicates no limit.This function has never worked as expected!
I've removed the update and added in the rest of the code.Quoted 19 lines
include/linux/refcount.h +0
-1@@ -137,13 +137,23 @@ static inline unsigned int refcount_read(const refcount_t *r) } static inline __must_check __signed_wrap -bool __refcount_add_not_zero(int i, refcount_t *r, int *oldp) {int old =refcount_read (r);do {if (!old)break ; }while (!atomic_try_cmpxchg_relaxed (&r->refs, &old, old + i));if (oldp)Quoted 1 line
*oldp = old;?
Quoted 5 lines
if (unlikely(old < 0 || old + i < 0)) refcount_warn_saturate(r, REFCOUNT_ADD_NOT_ZERO_OVF);return old;
}The saturate test just doesn't work as expected.
In C signed integer overflow is undefined (probably so that cpu that saturate/trap signed overflow can be conformant) and gcc uses that to optimise code.Quoted 7 lines
So if you compile (https://www.godbolt.org/z/WYWo84Weq):
intinc_wraps(int i){ return i < 0 || i + 1 < 0; }the second test is optimised away.
I don't think the kernel compiles disable this optimisation.
Quoted 2 lines
Last I checked, my kernel compiles specified -fno-strict-overflow. What happens if you try that in godbolt?
That does make gcc generated the wanted object code.
I know that compilation option has come up before, but I couldn't remember the name or whether it was disabled :-(
You do get much better object code from return (i | i + 1) < 0; And that is likely to be much better still if you need a conditional jump.
David
Re: [PATCH v9 10/17] refcount: introduce __refcount_{add|inc}_not_zero_limited - clang 17.0.1 bug
Quoted 56 lines · On Sat, 11 Jan 2025 22:19:39 +0000 David Laight <[email protected]>…
Quoted 50 lines · On Sat, 11 Jan 2025 10:30:40 -0800 "Paul E. McKenney" <[email protected]>…
Quoted 41 lines · David Laight
Quoted 3 lines · On Fri, 10 Jan 2025 20:25:57 -0800 Suren Baghdasaryan <[email protected]> wrote:
Introduce functions to increase refcount but with a top limit above which they will fail to increase (the limit is inclusive). Setting the limit to
INT_MAXindicates no limit.This function has never worked as expected!
I've removed the update and added in the rest of the code.Quoted 19 lines
include/linux/refcount.h +0
-1@@ -137,13 +137,23 @@ static inline unsigned int refcount_read(const refcount_t *r) } static inline __must_check __signed_wrap -bool __refcount_add_not_zero(int i, refcount_t *r, int *oldp) {int old =refcount_read (r);do {if (!old)break ; }while (!atomic_try_cmpxchg_relaxed (&r->refs, &old, old + i));if (oldp)Quoted 1 line
*oldp = old;?
Quoted 5 lines
if (unlikely(old < 0 || old + i < 0)) refcount_warn_saturate(r, REFCOUNT_ADD_NOT_ZERO_OVF);return old;
}The saturate test just doesn't work as expected.
In C signed integer overflow is undefined (probably so that cpu that saturate/trap signed overflow can be conformant) and gcc uses that to optimise code.Quoted 7 lines
So if you compile (https://www.godbolt.org/z/WYWo84Weq):
intinc_wraps(int i){ return i < 0 || i + 1 < 0; }the second test is optimised away.
I don't think the kernel compiles disable this optimisation.Quoted 2 lines
Last I checked, my kernel compiles specified -fno-strict-overflow. What happens if you try that in godbolt?
Quoted 6 lines
That does make gcc generated the wanted object code.
I know that compilation option has come up before, but I couldn't remember the name or whether it was disabled :-(You do get much better object code from return (i | i + 1) < 0; And that is likely to be much better still if you need a conditional jump.
I've just checked some more cases (see https://www.godbolt.org/z/YoM9odTbe).
gcc 11 onwards generates the same code code for the two expressions.
Rather more worryingly clang 17.0.1 is getting this one wrong:
return i < 0 || i + 1 < 0 ? foo(i) : bar(i);
It ignores the 'i + 1' test even with -fno-strict-overflow. That is more representative of the actual code.
What have I missed now?
David
Re: [PATCH v9 10/17] refcount: introduce __refcount_{add|inc}_not_zero_limited - clang 17.0.1 bug
Quoted 69 lines · On Sat, 11 Jan 2025 22:50:16 +0000 David Laight <[email protected]>…
Quoted 56 lines · On Sat, 11 Jan 2025 22:19:39 +0000 David Laight <[email protected]>…
Quoted 50 lines · On Sat, 11 Jan 2025 10:30:40 -0800 "Paul E. McKenney" <[email protected]>…
Quoted 41 lines · David Laight
Quoted 3 lines · On Fri, 10 Jan 2025 20:25:57 -0800 Suren Baghdasaryan <[email protected]> wrote:
Introduce functions to increase refcount but with a top limit above which they will fail to increase (the limit is inclusive). Setting the limit to
INT_MAXindicates no limit.This function has never worked as expected!
I've removed the update and added in the rest of the code.Quoted 19 lines
include/linux/refcount.h +0
-1@@ -137,13 +137,23 @@ static inline unsigned int refcount_read(const refcount_t *r) } static inline __must_check __signed_wrap -bool __refcount_add_not_zero(int i, refcount_t *r, int *oldp) {int old =refcount_read (r);do {if (!old)break ; }while (!atomic_try_cmpxchg_relaxed (&r->refs, &old, old + i));if (oldp)Quoted 1 line
*oldp = old;?
Quoted 5 lines
if (unlikely(old < 0 || old + i < 0)) refcount_warn_saturate(r, REFCOUNT_ADD_NOT_ZERO_OVF);return old;
}The saturate test just doesn't work as expected.
In C signed integer overflow is undefined (probably so that cpu that saturate/trap signed overflow can be conformant) and gcc uses that to optimise code.Quoted 7 lines
So if you compile (https://www.godbolt.org/z/WYWo84Weq):
intinc_wraps(int i){ return i < 0 || i + 1 < 0; }the second test is optimised away.
I don't think the kernel compiles disable this optimisation.Quoted 2 lines
Last I checked, my kernel compiles specified -fno-strict-overflow. What happens if you try that in godbolt?
Quoted 6 lines
That does make gcc generated the wanted object code.
I know that compilation option has come up before, but I couldn't remember the name or whether it was disabled :-(You do get much better object code from return (i | i + 1) < 0; And that is likely to be much better still if you need a conditional jump.
I've just checked some more cases (see https://www.godbolt.org/z/YoM9odTbe).
gcc 11 onwards generates the same code code for the two expressions.
Quoted 6 lines
Rather more worryingly clang 17.0.1 is getting this one wrong:
return i < 0 || i + 1 < 0 ? foo(i) : bar(i);It ignores the 'i + 1' test even with -fno-strict-overflow. That is more representative of the actual code.
What have I missed now?
A different optimisation :-(
Quoted 3 lines
David
Paul E. McKenney
maintainerRe: [PATCH v9 10/17] refcount: introduce __refcount_{add|inc}_not_zero_limited - clang 17.0.1 bug
Quoted 72 lines · David Laight
Quoted 69 lines · On Sat, 11 Jan 2025 22:50:16 +0000 David Laight <[email protected]>…
Quoted 56 lines · On Sat, 11 Jan 2025 22:19:39 +0000 David Laight <[email protected]>…
Quoted 50 lines · On Sat, 11 Jan 2025 10:30:40 -0800 "Paul E. McKenney" <[email protected]>…
Quoted 41 lines · David Laight
Quoted 3 lines · On Fri, 10 Jan 2025 20:25:57 -0800 Suren Baghdasaryan <[email protected]> wrote:
Introduce functions to increase refcount but with a top limit above which they will fail to increase (the limit is inclusive). Setting the limit to
INT_MAXindicates no limit.This function has never worked as expected!
I've removed the update and added in the rest of the code.Quoted 19 lines
include/linux/refcount.h +0
-1@@ -137,13 +137,23 @@ static inline unsigned int refcount_read(const refcount_t *r) } static inline __must_check __signed_wrap -bool __refcount_add_not_zero(int i, refcount_t *r, int *oldp) {int old =refcount_read (r);do {if (!old)break ; }while (!atomic_try_cmpxchg_relaxed (&r->refs, &old, old + i));if (oldp)Quoted 1 line
*oldp = old;?
Quoted 5 lines
if (unlikely(old < 0 || old + i < 0)) refcount_warn_saturate(r, REFCOUNT_ADD_NOT_ZERO_OVF);return old;
}The saturate test just doesn't work as expected.
In C signed integer overflow is undefined (probably so that cpu that saturate/trap signed overflow can be conformant) and gcc uses that to optimise code.Quoted 7 lines
So if you compile (https://www.godbolt.org/z/WYWo84Weq):
intinc_wraps(int i){ return i < 0 || i + 1 < 0; }the second test is optimised away.
I don't think the kernel compiles disable this optimisation.Quoted 2 lines
Last I checked, my kernel compiles specified -fno-strict-overflow. What happens if you try that in godbolt?
Quoted 6 lines
That does make gcc generated the wanted object code.
I know that compilation option has come up before, but I couldn't remember the name or whether it was disabled :-(You do get much better object code from return (i | i + 1) < 0; And that is likely to be much better still if you need a conditional jump.
I've just checked some more cases (see https://www.godbolt.org/z/YoM9odTbe).
gcc 11 onwards generates the same code code for the two expressions.
Quoted 8 lines
Quoted 6 lines
Rather more worryingly clang 17.0.1 is getting this one wrong:
return i < 0 || i + 1 < 0 ? foo(i) : bar(i);It ignores the 'i + 1' test even with -fno-strict-overflow. That is more representative of the actual code.
What have I missed now?
A different optimisation :-(
So the Linux kernel is good with signed integer overflow, right?
(Give or take compiler bugs, of course...)
Thanx, Paul
Suren Baghdasaryan
maintainerPATCH v9 11/17 mm: replace vm_lock and detached flag with a reference count
In reply to Suren Baghdasaryanrw_semaphore is a sizable structure of 40 bytes and consumes considerable space for each vm_area_struct. However vma_lock has two important specifics which can be used to replace rw_semaphore with a simpler structure:
1. Readers never wait. They try to take the vma_lock and fall back to mmap_lock if that fails.
2. Only one writer at a time will ever try to write-lock a vma_lock because writers first take mmap_lock in write mode.
Because of these requirements, full rw_semaphore functionality is not needed and we can replace rw_semaphore and the vma->detached flag with a refcount (vm_refcnt).
When vma is in detached state, vm_refcnt is 0 and only a call to vma_mark_attached() can take it out of this state. Note that unlike before, now we enforce both vma_mark_attached() and vma_mark_detached() to be done only after vma has been write-locked. vma_mark_attached() changes vm_refcnt to 1 to indicate that it has been attached to the vma tree. When a reader takes read lock, it increments vm_refcnt, unless the top usable bit of vm_refcnt (0x40000000) is set, indicating presence of a writer. When writer takes write lock, it sets the top usable bit to indicate its presence. If there are readers, writer will wait using newly introduced mm->vma_writer_wait. Since all writers take mmap_lock in write mode first, there can be only one writer at a time. The last reader to release the lock will signal the writer to wake up.
refcount might overflow if there are many competing readers, in which case read-locking will fail. Readers are expected to handle such failures. In summary:
1. all readers increment the vm_refcnt;
2. writer sets top usable (writer) bit of vm_refcnt;
3. readers cannot increment the vm_refcnt if the writer bit is set;
4. in the presence of readers, writer must wait for the vm_refcnt to drop to 1 (ignoring the writer bit), indicating an attached vma with no readers;
5. vm_refcnt overflow is handled by the readers.
While this vm_lock replacement does not yet result in a smaller vm_area_struct (it stays at 256 bytes due to cacheline alignment), it allows for further size optimization by structure member regrouping to bring the size of vm_area_struct below 192 bytes.
--- include/linux/mm.h | 102 +++++++++++++++++++++---------- include/linux/mm_types.h | 22 +++---- kernel/fork.c | 13 ++-- mm/init-mm.c | 1 + mm/memory.c | 80 +++++++++++++++++++++--- tools/testing/vma/linux/atomic.h | 5 ++ tools/testing/vma/vma_internal.h | 66 +++++++++++--------- 7 files changed, 198 insertions(+), 91 deletions(-)
include/linux/mm.h +69 -33
@@ -32,6 +32,7 @@#include <linux/memremap.h> #include <linux/slab.h> #include <linux/cacheinfo.h> +#include <linux/rcuwait.h> struct mempolicy ;struct anon_vma ;
@@ -697,12 +698,43 @@ static inline void vma_numab_state_free(struct vm_area_struct *vma) {}#endif /* CONFIG_NUMA_BALANCING */ #ifdef CONFIG_PER_VMA_LOCK -static inline void vma_lock_init (struct vm_area_struct *vma) +static inline void vma_lock_init (struct vm_area_struct *vma,bool reset_refcnt) { -init_rwsem (&vma->vm_lock.lock); +#ifdef CONFIG_DEBUG_LOCK_ALLOC +static struct lock_class_key lockdep_key; + +lockdep_init_map (&vma->vmlock_dep_map,"vm_lock" , &lockdep_key,0 ); +#endif +if (reset_refcnt) +refcount_set (&vma->vm_refcnt,0 ); vma->vm_lock_seq =UINT_MAX ; } +static inline bool is_vma_writer_only (int refcnt) +{ +/* +* With a writer and no readers, refcnt is VMA_LOCK_OFFSET if the vma +* is detached and (VMA_LOCK_OFFSET + 1) if it is attached. Waiting on +* a detached vma happens only in vma_mark_detached() and is a rare +* case, therefore most of the time there will be no unnecessary wakeup. +*/ +return refcnt &VMA_LOCK_OFFSET && refcnt <=VMA_LOCK_OFFSET +1 ; +} + +static inline void vma_refcount_put (struct vm_area_struct *vma) +{ +/* Use a copy of vm_mm in case vma is freed after we drop vm_refcnt */ +struct mm_struct *mm = vma->vm_mm; +int oldcnt; + +rwsem_release (&vma->vmlock_dep_map, _RET_IP_); +if (!__refcount_dec_and_test (&vma->vm_refcnt, &oldcnt)) { + +if (is_vma_writer_only (oldcnt -1 )) +rcuwait_wake_up (&mm->vma_writer_wait); + } +} +/* * Try to read-lock a vma. The function is allowed to occasionally yield false * locked result to avoid performance overhead, in which case we fall back to
@@ -710,6 +742,8 @@ static inline void vma_lock_init(struct vm_area_struct *vma)*/ static inline bool vma_start_read (struct vm_area_struct *vma) { +int oldcnt; +/* * Check before locking. A race might cause false locked result. * We can use READ_ONCE() for the mm_lock_seq here, and don't need
@@ -720,13 +754,19 @@ static inline bool vma_start_read(struct vm_area_struct *vma)if (READ_ONCE (vma->vm_lock_seq) ==READ_ONCE (vma->vm_mm->mm_lock_seq.sequence))return false; -if (unlikely (down_read_trylock (&vma->vm_lock.lock) ==0 )) +/* +* If VMA_LOCK_OFFSET is set, __refcount_inc_not_zero_limited() will fail +* because VMA_REF_LIMIT is less than VMA_LOCK_OFFSET. +*/ +if (unlikely (!__refcount_inc_not_zero_limited (&vma->vm_refcnt, &oldcnt, +VMA_REF_LIMIT )))return false; +rwsem_acquire_read (&vma->vmlock_dep_map,0 ,1 , _RET_IP_);/* -* Overflow might produce false locked result. +* Overflow of vm_lock_seq/mm_lock_seq might produce false locked result. * False unlocked result is impossible because we modify and check -* vma->vm_lock_seq under vma->vm_lock protection and mm->mm_lock_seq +* vma->vm_lock_seq under vma->vm_refcnt protection and mm->mm_lock_seq * modification invalidates all existing locks. * * We must use ACQUIRE semantics for the mm_lock_seq so that if we are
@@ -735,9 +775,10 @@ static inline bool vma_start_read(struct vm_area_struct *vma)* This pairs with RELEASE semantics in vma_end_write_all(). */ if (unlikely (vma->vm_lock_seq ==raw_read_seqcount (&vma->vm_mm->mm_lock_seq))) { -up_read (&vma->vm_lock.lock); +vma_refcount_put (vma);return false; } +return true; }
@@ -749,8 +790,14 @@ static inline bool vma_start_read(struct vm_area_struct *vma)*/ static inline bool vma_start_read_locked_nested (struct vm_area_struct *vma,int subclass) { +int oldcnt; +mmap_assert_locked (vma->vm_mm); -down_read_nested (&vma->vm_lock.lock, subclass); +if (unlikely (!__refcount_inc_not_zero_limited (&vma->vm_refcnt, &oldcnt, +VMA_REF_LIMIT ))) +return false; + +rwsem_acquire_read (&vma->vmlock_dep_map,0 ,1 , _RET_IP_);return true; }
@@ -762,16 +809,12 @@ static inline bool vma_start_read_locked_nested(struct vm_area_struct *vma, int*/ static inline bool vma_start_read_locked (struct vm_area_struct *vma) { -mmap_assert_locked (vma->vm_mm); -down_read (&vma->vm_lock.lock); -return true; +return vma_start_read_locked_nested (vma,0 ); }static inline void vma_end_read (struct vm_area_struct *vma) { -rcu_read_lock ();/* keeps vma alive till the end of up_read */ -up_read (&vma->vm_lock.lock); -rcu_read_unlock (); +vma_refcount_put (vma); }/* WARNING! Can only be used if mmap_lock is expected to be write-locked */
@@ -813,36 +856,33 @@ static inline void vma_assert_write_locked(struct vm_area_struct *vma)static inline void vma_assert_locked (struct vm_area_struct *vma) { -if (!rwsem_is_locked (&vma->vm_lock.lock)) +if (refcount_read (&vma->vm_refcnt) <=1 )vma_assert_write_locked (vma); } +/* +* WARNING: to avoid racing with vma_mark_attached()/vma_mark_detached(), these +* assertions should be made either under mmap_write_lock or when the object +* has been isolated under mmap_write_lock, ensuring no competing writers. +*/ static inline void vma_assert_attached (struct vm_area_struct *vma) { -VM_BUG_ON_VMA (vma->detached, vma); +VM_BUG_ON_VMA (!refcount_read (&vma->vm_refcnt), vma); }static inline void vma_assert_detached (struct vm_area_struct *vma) { -VM_BUG_ON_VMA (!vma->detached, vma); +VM_BUG_ON_VMA (refcount_read (&vma->vm_refcnt), vma); }static inline void vma_mark_attached (struct vm_area_struct *vma) { - vma->detached = false; -} - -static inline void vma_mark_detached (struct vm_area_struct *vma) -{ -/* When detaching vma should be write-locked */ vma_assert_write_locked (vma); - vma->detached = true; +vma_assert_detached (vma); +refcount_set (&vma->vm_refcnt,1 ); } -static inline bool is_vma_detached (struct vm_area_struct *vma) -{ -return vma->detached; -} +void vma_mark_detached (struct vm_area_struct *vma);static inline void release_fault_lock (struct vm_fault *vmf) {
@@ -865,7 +905,7 @@ struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm,#else /* CONFIG_PER_VMA_LOCK */ -static inline void vma_lock_init (struct vm_area_struct *vma) {} +static inline void vma_lock_init (struct vm_area_struct *vma,bool reset_refcnt) {}static inline bool vma_start_read (struct vm_area_struct *vma) {return false; }static inline void vma_end_read (struct vm_area_struct *vma) {}
@@ -908,12 +948,8 @@ static inline void vma_init(struct vm_area_struct *vma, struct mm_struct *mm) vma->vm_mm = mm; vma->vm_ops = &vma_dummy_vm_ops;INIT_LIST_HEAD (&vma->anon_vma_chain); -#ifdef CONFIG_PER_VMA_LOCK -/* vma is not locked, can't use vma_mark_detached() */ - vma->detached = true; -#endif vma_numab_state_init (vma); -vma_lock_init (vma); +vma_lock_init (vma, false); }/* Use when VMA is not part of the VMA tree and needs no locking */
include/linux/mm_types.h +10 -12
@@ -19,6 +19,7 @@#include <linux/workqueue.h> #include <linux/seqlock.h> #include <linux/percpu_counter.h> +#include <linux/types.h> #include <asm/mmu.h>
@@ -629,9 +630,8 @@ static inline struct anon_vma_name *anon_vma_name_alloc(const char *name) }#endif -struct vma_lock { -struct rw_semaphore lock; -}; +#define VMA_LOCK_OFFSET 0x40000000 +#define VMA_REF_LIMIT (VMA_LOCK_OFFSET - 1)struct vma_numab_state {/*
@@ -709,19 +709,13 @@ struct vm_area_struct { };#ifdef CONFIG_PER_VMA_LOCK -/* -* Flag to indicate areas detached from the mm->mm_mt tree. -* Unstable RCU readers are allowed to read this. -*/ -bool detached; -/* * Can only be written (using WRITE_ONCE()) while holding both: * - mmap_lock (in write mode) -* - vm_lock->lock (in write mode) +* - vm_refcnt bit at VMA_LOCK_OFFSET is set * Can be read reliably while holding one of: * - mmap_lock (in read or write mode) -* - vm_lock->lock (in read or write mode) +* - vm_refcnt bit at VMA_LOCK_OFFSET is set or vm_refcnt > 1 * Can be read unreliably (using READ_ONCE()) for pessimistic bailout * while holding nothing (except RCU to keep the VMA struct allocated). *
@@ -784,7 +778,10 @@ struct vm_area_struct {struct vm_userfaultfd_ctx vm_userfaultfd_ctx;#ifdef CONFIG_PER_VMA_LOCK /* Unstable RCU readers are allowed to read this. */ -struct vma_lock vm_lock____cacheline_aligned_in_smp ; +refcount_t vm_refcnt____cacheline_aligned_in_smp ; +#ifdef CONFIG_DEBUG_LOCK_ALLOC +struct lockdep_map vmlock_dep_map; +#endif #endif } __randomize_layout;
@@ -919,6 +916,7 @@ struct mm_struct {* by mmlist_lock */ #ifdef CONFIG_PER_VMA_LOCK +struct rcuwait vma_writer_wait;/* * This field has lock-like semantics, meaning it is sometimes * accessed with ACQUIRE/RELEASE semantics.
kernel/fork.c +6 -7
@@ -463,12 +463,8 @@ struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig)* will be reinitialized. */ data_race (memcpy (new, orig,sizeof (*new))); -vma_lock_init (new); +vma_lock_init (new, true);INIT_LIST_HEAD (&new->anon_vma_chain); -#ifdef CONFIG_PER_VMA_LOCK -/* vma is not locked, can't use vma_mark_detached() */ - new->detached = true; -#endif vma_numab_state_init (new);dup_anon_vma_name (orig, new);
@@ -477,6 +473,8 @@ struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig)void __vm_area_free (struct vm_area_struct *vma) { +/* The vma should be detached while being destroyed. */ +vma_assert_detached (vma);vma_numab_state_free (vma);free_anon_vma_name (vma);kmem_cache_free (vm_area_cachep, vma);
@@ -488,8 +486,6 @@ static void vm_area_free_rcu_cb(struct rcu_head *head) struct vm_area_struct *vma = container_of(head, struct vm_area_struct, vm_rcu); -/* The vma should not be locked while being destroyed. */ -VM_BUG_ON_VMA (rwsem_is_locked (&vma->vm_lock.lock), vma); __vm_area_free(vma); }#endif
@@ -1223,6 +1219,9 @@ static inline void mmap_init_lock(struct mm_struct *mm) {init_rwsem (&mm->mmap_lock);mm_lock_seqcount_init (mm); +#ifdef CONFIG_PER_VMA_LOCK +rcuwait_init (&mm->vma_writer_wait); +#endif } static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p,
mm/init-mm.c +1 -0
@@ -40,6 +40,7 @@ struct mm_struct init_mm = { .arg_lock = __SPIN_LOCK_UNLOCKED(init_mm.arg_lock), .mmlist =LIST_HEAD_INIT (init_mm.mmlist), #ifdef CONFIG_PER_VMA_LOCK + .vma_writer_wait =__RCUWAIT_INITIALIZER (init_mm.vma_writer_wait), .mm_lock_seq = SEQCNT_ZERO(init_mm.mm_lock_seq), #endif .user_ns = &init_user_ns,
mm/memory.c +70 -10
109 lines not on this page · raw message
tools/testing/vma/linux/atomic.h +5 -0
@@ -9,4 +9,9 @@#define atomic_set (x, y) uatomic_set(x, y)#define U8_MAX UCHAR_MAX +#ifndef atomic_cmpxchg_relaxed +#define atomic_cmpxchg_relaxed uatomic_cmpxchg +#define atomic_cmpxchg_release uatomic_cmpxchg +#endif /* atomic_cmpxchg_relaxed */ +#endif /* _LINUX_ATOMIC_H */
tools/testing/vma/vma_internal.h +37 -29
140 lines not on this page · raw message
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
Re: [PATCH v9 11/17] mm: replace vm_lock and detached flag with a reference count
On Fri, Jan 10, 2025 at 08:25:58PM -0800, Suren Baghdasaryan wrote:
So there were quite a few iterations of the patch and I have not been reading majority of the feedback, so it may be I missed something, apologies upfront. :)
Quoted 15 lines
/*
* Try to read-lock a vma. The function is allowed to occasionally yield false
* locked result to avoid performance overhead, in which case we fall back toinclude/linux/mm.h +2
-0@@ -710,6 +742,8 @@ static inline void vma_lock_init(struct vm_area_struct *vma)*/ static inline bool vma_start_read (struct vm_area_struct *vma) { +int oldcnt; +/* * Check before locking. A race might cause false locked result. * We can use READ_ONCE() for the mm_lock_seq here, and don't need @@ -720,13 +754,19 @@ static inline bool vma_start_read(struct vm_area_struct *vma)if (READ_ONCE (vma->vm_lock_seq) ==READ_ONCE (vma->vm_mm->mm_lock_seq.sequence))return false;
include/linux/mm.h static inline bool vma_start_read(struct vm_area_struct *vma) · line 764
Quoted 10 lines
include/linux/mm.h +6
-1-if (unlikely (down_read_trylock (&vma->vm_lock.lock) ==0 )) +/* +* If VMA_LOCK_OFFSET is set, __refcount_inc_not_zero_limited() will fail +* because VMA_REF_LIMIT is less than VMA_LOCK_OFFSET. +*/ +if (unlikely (!__refcount_inc_not_zero_limited (&vma->vm_refcnt, &oldcnt, +VMA_REF_LIMIT )))return false;
Replacing down_read_trylock() with the new routine loses an acquire fence. That alone is not a problem, but see below.
Quoted 11 lines
include/linux/mm.h +3
-2+rwsem_acquire_read (&vma->vmlock_dep_map,0 ,1 , _RET_IP_);/* -* Overflow might produce false locked result. +* Overflow of vm_lock_seq/mm_lock_seq might produce false locked result. * False unlocked result is impossible because we modify and check -* vma->vm_lock_seq under vma->vm_lock protection and mm->mm_lock_seq +* vma->vm_lock_seq under vma->vm_refcnt protection and mm->mm_lock_seq * modification invalidates all existing locks. * * We must use ACQUIRE semantics for the mm_lock_seq so that if we are
include/linux/mm.h static inline bool vma_start_read(struct vm_area_struct *vma) · line 777
Quoted 3 lines
include/linux/mm.h +0
-0@@ -735,9 +775,10 @@ static inline bool vma_start_read(struct vm_area_struct *vma)* This pairs with RELEASE semantics in vma_end_write_all(). */ if (unlikely (vma->vm_lock_seq ==raw_read_seqcount (&vma->vm_mm->mm_lock_seq))) {
The previous modification of this spot to raw_read_seqcount loses the acquire fence, making the above comment not line up with the code.
I don't know if the stock code (with down_read_trylock()) is correct as is -- looks fine for cursory reading fwiw. However, if it indeed works, the acquire fence stemming from the lock routine is a mandatory part of it afaics.
I think the best way forward is to add a new refcount routine which ships with an acquire fence.
Otherwise I would suggest:
1. a comment above __refcount_inc_not_zero_limited saying there is an acq fence issued later
2. smp_rmb() slapped between that and seq accesses
If the now removed fence is somehow not needed, I think a comment explaining it is necessary.
include/linux/mm.h static inline void vma_assert_write_locked(struct vm_area_struct *vma) · line 862
Quoted 9 lines
include/linux/mm.h +1
-1@@ -813,36 +856,33 @@ static inline void vma_assert_write_locked(struct vm_area_struct *vma)static inline void vma_assert_locked (struct vm_area_struct *vma) { -if (!rwsem_is_locked (&vma->vm_lock.lock)) +if (refcount_read (&vma->vm_refcnt) <=1 )vma_assert_write_locked (vma); }
This now forces the compiler to emit a load from vm_refcnt even if vma_assert_write_locked expands to nothing. iow this wants to hide behind the same stuff as vma_assert_write_locked.
Suren Baghdasaryan
maintainerRe: [PATCH v9 11/17] mm: replace vm_lock and detached flag with a reference count
Quoted 23 lines · Mateusz Guzik
On Fri, Jan 10, 2025 at 08:25:58PM -0800, Suren Baghdasaryan wrote:
So there were quite a few iterations of the patch and I have not been reading majority of the feedback, so it may be I missed something, apologies upfront. :)
Quoted 16 lines
/*
* Try to read-lock a vma. The function is allowed to occasionally yield false
* locked result to avoid performance overhead, in which case we fall back toinclude/linux/mm.h +2
-0@@ -710,6 +742,8 @@ static inline void vma_lock_init(struct vm_area_struct *vma)*/ static inline bool vma_start_read (struct vm_area_struct *vma) { +int oldcnt; +/* * Check before locking. A race might cause false locked result. * We can use READ_ONCE() for the mm_lock_seq here, and don't need @@ -720,13 +754,19 @@ static inline bool vma_start_read(struct vm_area_struct *vma)if (READ_ONCE (vma->vm_lock_seq) ==READ_ONCE (vma->vm_mm->mm_lock_seq.sequence))return false;
Quoted 12 lines
include/linux/mm.h static inline bool vma_start_read(struct vm_area_struct *vma) · line 764
Quoted 9 lines
include/linux/mm.h +6
-1-if (unlikely (down_read_trylock (&vma->vm_lock.lock) ==0 )) +/* +* If VMA_LOCK_OFFSET is set, __refcount_inc_not_zero_limited() will fail +* because VMA_REF_LIMIT is less than VMA_LOCK_OFFSET. +*/ +if (unlikely (!__refcount_inc_not_zero_limited (&vma->vm_refcnt, &oldcnt, +VMA_REF_LIMIT )))return false;Replacing
down_read_trylock()with the new routine loses an acquire fence. That alone is not a problem, but see below.
Hmm. I think this acquire fence is actually necessary. We don't want the later vm_lock_seq check to be reordered and happen before we take the refcount. Otherwise this might happen:
reader writer
if (vm_lock_seq == mm_lock_seq) // check got reordered
return false;
vm_refcnt += VMA_LOCK_OFFSET
vm_lock_seq == mm_lock_seq
vm_refcnt -= VMA_LOCK_OFFSET
if (!__refcount_inc_not_zero_limited())
return false;Both reader's checks will pass and the reader would read-lock a vma that was write-locked.
Quoted 10 lines
Quoted 10 lines
include/linux/mm.h +3
-2+rwsem_acquire_read (&vma->vmlock_dep_map,0 ,1 , _RET_IP_);/* -* Overflow might produce false locked result. +* Overflow of vm_lock_seq/mm_lock_seq might produce false locked result. * False unlocked result is impossible because we modify and check -* vma->vm_lock_seq under vma->vm_lock protection and mm->mm_lock_seq +* vma->vm_lock_seq under vma->vm_refcnt protection and mm->mm_lock_seq * modification invalidates all existing locks. *
Quoted 8 lines
include/linux/mm.h static inline bool vma_start_read(struct vm_area_struct *vma) · line 777
Quoted 5 lines
include/linux/mm.h +0
-0* We must use ACQUIRE semantics for the mm_lock_seq so that if we are @@ -735,9 +775,10 @@ static inline bool vma_start_read(struct vm_area_struct *vma)* This pairs with RELEASE semantics in vma_end_write_all(). */ if (unlikely (vma->vm_lock_seq ==raw_read_seqcount (&vma->vm_mm->mm_lock_seq))) {The previous modification of this spot to
raw_read_seqcountloses the acquire fence, making the above comment not line up with the code.
Is it? From reading the seqcount code
(https://elixir.bootlin.com/linux/v6.13-rc3/source/include/linux/seqlock.h#L211):
raw_read_seqcount()
seqprop_sequence()
__seqprop(s, sequence)
__seqprop_sequence()
smp_load_acquire()
smp_load_acquire() still provides the acquire fence. Am I missing something?
Quoted 8 lines
I don't know if the stock code (with
down_read_trylock()) is correct as is -- looks fine for cursory reading fwiw. However, if it indeed works, the acquire fence stemming from the lock routine is a mandatory part of it afaics.I think the best way forward is to add a new refcount routine which ships with an acquire fence.
I plan on replacing refcount_t usage here with an atomic since, as Hillf noted, refcount is not designed to be used for locking. And will make sure the down_read_trylock() replacement will provide an acquire fence.
Quoted 11 lines
Otherwise I would suggest:
1. a comment above__refcount_inc_not_zero_limitedsaying there is an acq fence issued later
2.smp_rmb()slapped between that and seq accessesIf the now removed fence is somehow not needed, I think a comment explaining it is necessary.
Quoted 2 lines
(quoted diff) +0
-0@@ -813,36 +856,33 @@ static inline void vma_assert_write_locked(struct vm_area_struct *vma)
Quoted 11 lines
include/linux/mm.h static inline void vma_assert_write_locked(struct vm_area_struct *vma) · line 862
Quoted 7 lines
include/linux/mm.h +1
-1static inline void vma_assert_locked (struct vm_area_struct *vma) { -if (!rwsem_is_locked (&vma->vm_lock.lock)) +if (refcount_read (&vma->vm_refcnt) <=1 )vma_assert_write_locked (vma); }This now forces the compiler to emit a load from
vm_refcnteven ifvma_assert_write_lockedexpands to nothing. iow this wants to hide behind the same stuff asvma_assert_write_locked.
True. I guess I'll have to avoid using vma_assert_write_locked() like this:
static inline void vma_assert_locked(struct vm_area_struct *vma)
{
unsigned int mm_lock_seq;
VM_BUG_ON_VMA(refcount_read(&vma->vm_refcnt) <= 1 &&
!__is_vma_write_locked(vma,&mm_lock_seq), vma);
}
Will make the change.
Thanks for the feedback!
Suren Baghdasaryan
maintainerRe: [PATCH v9 11/17] mm: replace vm_lock and detached flag with a reference count
Quoted 42 lines · Suren Baghdasaryan
Quoted 23 lines · Mateusz Guzik
On Fri, Jan 10, 2025 at 08:25:58PM -0800, Suren Baghdasaryan wrote:
So there were quite a few iterations of the patch and I have not been reading majority of the feedback, so it may be I missed something, apologies upfront. :)
Quoted 16 lines
/*
* Try to read-lock a vma. The function is allowed to occasionally yield false
* locked result to avoid performance overhead, in which case we fall back toinclude/linux/mm.h +2
-0@@ -710,6 +742,8 @@ static inline void vma_lock_init(struct vm_area_struct *vma)*/ static inline bool vma_start_read (struct vm_area_struct *vma) { +int oldcnt; +/* * Check before locking. A race might cause false locked result. * We can use READ_ONCE() for the mm_lock_seq here, and don't need @@ -720,13 +754,19 @@ static inline bool vma_start_read(struct vm_area_struct *vma)if (READ_ONCE (vma->vm_lock_seq) ==READ_ONCE (vma->vm_mm->mm_lock_seq.sequence))return false;Quoted 12 lines
include/linux/mm.h static inline bool vma_start_read(struct vm_area_struct *vma) · line 764
Quoted 9 lines
include/linux/mm.h +6
-1-if (unlikely (down_read_trylock (&vma->vm_lock.lock) ==0 )) +/* +* If VMA_LOCK_OFFSET is set, __refcount_inc_not_zero_limited() will fail +* because VMA_REF_LIMIT is less than VMA_LOCK_OFFSET. +*/ +if (unlikely (!__refcount_inc_not_zero_limited (&vma->vm_refcnt, &oldcnt, +VMA_REF_LIMIT )))return false;Replacing
down_read_trylock()with the new routine loses an acquire fence. That alone is not a problem, but see below.Hmm. I think this acquire fence is actually necessary. We don't want the later
vm_lock_seqcheck to be reordered and happen before we take the refcount. Otherwise this might happen:
Quoted 5 lines
reader writer
if (vm_lock_seq==mm_lock_seq) // check got reorderedreturn false; vm_refcnt += VMA_LOCK_OFFSET vm_lock_seq == mm_lock_seq
s/vm_lock_seq == mm_lock_seq/vm_lock_seq = mm_lock_seq
Quoted 88 lines
vm_refcnt-=VMA_LOCK_OFFSET
if (!__refcount_inc_not_zero_limited())return false;Both reader's checks will pass and the reader would read-lock a vma that was write-locked.
Quoted 10 lines
Quoted 10 lines
include/linux/mm.h +3
-2+rwsem_acquire_read (&vma->vmlock_dep_map,0 ,1 , _RET_IP_);/* -* Overflow might produce false locked result. +* Overflow of vm_lock_seq/mm_lock_seq might produce false locked result. * False unlocked result is impossible because we modify and check -* vma->vm_lock_seq under vma->vm_lock protection and mm->mm_lock_seq +* vma->vm_lock_seq under vma->vm_refcnt protection and mm->mm_lock_seq * modification invalidates all existing locks. * Quoted 8 lines
include/linux/mm.h static inline bool vma_start_read(struct vm_area_struct *vma) · line 777
Quoted 5 lines
include/linux/mm.h +0
-0* We must use ACQUIRE semantics for the mm_lock_seq so that if we are @@ -735,9 +775,10 @@ static inline bool vma_start_read(struct vm_area_struct *vma)* This pairs with RELEASE semantics in vma_end_write_all(). */ if (unlikely (vma->vm_lock_seq ==raw_read_seqcount (&vma->vm_mm->mm_lock_seq))) {The previous modification of this spot to
raw_read_seqcountloses the acquire fence, making the above comment not line up with the code.Is it? From reading the seqcount code
(https://elixir.bootlin.com/linux/v6.13-rc3/source/include/linux/seqlock.h#L211):
raw_read_seqcount()
seqprop_sequence()
__seqprop(s, sequence)
__seqprop_sequence()
smp_load_acquire()
smp_load_acquire()still provides the acquire fence. Am I missing something?Quoted 8 lines
I don't know if the stock code (with
down_read_trylock()) is correct as is -- looks fine for cursory reading fwiw. However, if it indeed works, the acquire fence stemming from the lock routine is a mandatory part of it afaics.I think the best way forward is to add a new refcount routine which ships with an acquire fence.
I plan on replacing
refcount_tusage here with an atomic since, as Hillf noted, refcount is not designed to be used for locking. And will make sure thedown_read_trylock()replacement will provide an acquire fence.Quoted 11 lines
Otherwise I would suggest:
1. a comment above__refcount_inc_not_zero_limitedsaying there is an acq fence issued later
2.smp_rmb()slapped between that and seq accessesIf the now removed fence is somehow not needed, I think a comment explaining it is necessary.
Quoted 2 lines
(quoted diff) +0
-0@@ -813,36 +856,33 @@ static inline void vma_assert_write_locked(struct vm_area_struct *vma)Quoted 11 lines
include/linux/mm.h static inline void vma_assert_write_locked(struct vm_area_struct *vma) · line 862
Quoted 7 lines
include/linux/mm.h +1
-1static inline void vma_assert_locked (struct vm_area_struct *vma) { -if (!rwsem_is_locked (&vma->vm_lock.lock)) +if (refcount_read (&vma->vm_refcnt) <=1 )vma_assert_write_locked (vma); }This now forces the compiler to emit a load from
vm_refcnteven ifvma_assert_write_lockedexpands to nothing. iow this wants to hide behind the same stuff asvma_assert_write_locked.True. I guess I'll have to avoid using
vma_assert_write_locked()like this:static inline void
vma_assert_locked(struct vm_area_struct *vma){ unsigned int mm_lock_seq; VM_BUG_ON_VMA(refcount_read(&vma->vm_refcnt) <= 1 && !__is_vma_write_locked(vma,
&mm_lock_seq), vma);
}Will make the change.
Thanks for the feedback!
Re: [PATCH v9 11/17] mm: replace vm_lock and detached flag with a reference count
In reply to Suren BaghdasaryanQuoted 8 lines · Suren Baghdasaryan
Quoted 2 lines · Mateusz Guzik
The previous modification of this spot to
raw_read_seqcountloses the acquire fence, making the above comment not line up with the code.Is it? From reading the seqcount code
(https://elixir.bootlin.com/linux/v6.13-rc3/source/include/linux/seqlock.h#L211):
Quoted 8 lines
raw_read_seqcount()
seqprop_sequence()
__seqprop(s, sequence)
__seqprop_sequence()
smp_load_acquire()
smp_load_acquire()still provides the acquire fence. Am I missing something?
That's fine indeed.
In a different project there is an equivalent API which skips barriers, too quick glance suggested this is what's going on here. My bad, sorry for false alarm on this front. :)
Signature · 1 line
Mateusz Guzik <mjguzik gmail.com>
Suren Baghdasaryan
maintainerRe: [PATCH v9 11/17] mm: replace vm_lock and detached flag with a reference count
Quoted 16 lines · Mateusz Guzik
Quoted 14 lines · Suren Baghdasaryan
Quoted 2 lines · Mateusz Guzik
The previous modification of this spot to
raw_read_seqcountloses the acquire fence, making the above comment not line up with the code.Is it? From reading the seqcount code
(https://elixir.bootlin.com/linux/v6.13-rc3/source/include/linux/seqlock.h#L211):
raw_read_seqcount()
seqprop_sequence()
__seqprop(s, sequence)
__seqprop_sequence()
smp_load_acquire()
Quoted 8 lines
Quoted 2 lines
smp_load_acquire()still provides the acquire fence. Am I missing something?That's fine indeed.
In a different project there is an equivalent API which skips barriers, too quick glance suggested this is what's going on here. My bad, sorry for false alarm on this front. :)
No worries. Better to double-check than to merge a bug. Thanks,
Suren.
Quoted 3 lines
--
Mateusz Guzik <mjguzik gmail.com>
Re: [PATCH v9 11/17] mm: replace vm_lock and detached flag with a reference count
In reply to Suren BaghdasaryanQuoted 4 lines · Suren Baghdasaryan
I plan on replacing
refcount_tusage here with an atomic since, as Hillf noted, refcount is not designed to be used for locking. And will make sure thedown_read_trylock()replacement will provide an acquire fence.
Could Hillf stop reducing the Cc list on his replies? The whole subthread went to only few people :(
Re: [PATCH v9 11/17] mm: replace vm_lock and detached flag with a reference count
In reply to Suren BaghdasaryanQuoted 8 lines · Suren Baghdasaryan
Quoted 7 lines · Mateusz Guzik
On Fri, Jan 10, 2025 at 08:25:58PM -0800, Suren Baghdasaryan wrote:
So there were quite a few iterations of the patch and I have not been reading majority of the feedback, so it may be I missed something, apologies upfront. :)
Hi, I am new to memory barriers. Hope not bothering.
Quoted 42 lines
Quoted 16 lines
Quoted 16 lines
/*
* Try to read-lock a vma. The function is allowed to occasionally yield false
* locked result to avoid performance overhead, in which case we fall back toinclude/linux/mm.h +2
-0@@ -710,6 +742,8 @@ static inline void vma_lock_init(struct vm_area_struct *vma)*/ static inline bool vma_start_read (struct vm_area_struct *vma) { +int oldcnt; +/* * Check before locking. A race might cause false locked result. * We can use READ_ONCE() for the mm_lock_seq here, and don't need @@ -720,13 +754,19 @@ static inline bool vma_start_read(struct vm_area_struct *vma)if (READ_ONCE (vma->vm_lock_seq) ==READ_ONCE (vma->vm_mm->mm_lock_seq.sequence))return false;Quoted 12 lines
include/linux/mm.h static inline bool vma_start_read(struct vm_area_struct *vma) · line 764
Quoted 9 lines
include/linux/mm.h +6
-1-if (unlikely (down_read_trylock (&vma->vm_lock.lock) ==0 )) +/* +* If VMA_LOCK_OFFSET is set, __refcount_inc_not_zero_limited() will fail +* because VMA_REF_LIMIT is less than VMA_LOCK_OFFSET. +*/ +if (unlikely (!__refcount_inc_not_zero_limited (&vma->vm_refcnt, &oldcnt, +VMA_REF_LIMIT )))return false;Replacing
down_read_trylock()with the new routine loses an acquire fence. That alone is not a problem, but see below.Hmm. I think this acquire fence is actually necessary. We don't want the later
vm_lock_seqcheck to be reordered and happen before we take the refcount. Otherwise this might happen:reader writer
if (vm_lock_seq==mm_lock_seq) // check got reorderedreturn false; vm_refcnt += VMA_LOCK_OFFSET vm_lock_seq == mm_lock_seq vm_refcnt -= VMA_LOCK_OFFSET if (!__refcount_inc_not_zero_limited()) return false;
Quoted 3 lines
Both reader's checks will pass and the reader would read-lock a vma that was write-locked.
Here what we plan to do is define __refcount_inc_not_zero_limited() with acquire fence, e.g. with atomic_try_cmpxchg_acquire(), right?
Quoted 15 lines
Quoted 15 lines
Quoted 12 lines
include/linux/mm.h +3
-2+rwsem_acquire_read (&vma->vmlock_dep_map,0 ,1 , _RET_IP_);/* -* Overflow might produce false locked result. +* Overflow of vm_lock_seq/mm_lock_seq might produce false locked result. * False unlocked result is impossible because we modify and check -* vma->vm_lock_seq under vma->vm_lock protection and mm->mm_lock_seq +* vma->vm_lock_seq under vma->vm_refcnt protection and mm->mm_lock_seq * modification invalidates all existing locks. * * We must use ACQUIRE semantics for the mm_lock_seq so that if we are include/linux/mm.h static inline bool vma_start_read(struct vm_area_struct *vma) · line 777
Quoted 3 lines
include/linux/mm.h +0
-0@@ -735,9 +775,10 @@ static inline bool vma_start_read(struct vm_area_struct *vma)* This pairs with RELEASE semantics in vma_end_write_all(). */ if (unlikely (vma->vm_lock_seq ==raw_read_seqcount (&vma->vm_mm->mm_lock_seq))) {
One question here is would compiler optimize the read of vm_lock_seq here, since we have read it at the beginning?
Or with the acquire fence added above, compiler won't optimize it. Or we should use REACE_ONCE(vma->vm_lock_seq) here?
Quoted 21 lines
Quoted 3 lines
The previous modification of this spot to
raw_read_seqcountloses the acquire fence, making the above comment not line up with the code.Is it? From reading the seqcount code
(https://elixir.bootlin.com/linux/v6.13-rc3/source/include/linux/seqlock.h#L211):
raw_read_seqcount()
seqprop_sequence()
__seqprop(s, sequence)
__seqprop_sequence()
smp_load_acquire()
smp_load_acquire()still provides the acquire fence. Am I missing something?Quoted 6 lines
I don't know if the stock code (with
down_read_trylock()) is correct as is -- looks fine for cursory reading fwiw. However, if it indeed works, the acquire fence stemming from the lock routine is a mandatory part of it afaics.
Quoted 8 lines
Quoted 2 lines
I think the best way forward is to add a new refcount routine which ships with an acquire fence.
I plan on replacing
refcount_tusage here with an atomic since, as Hillf noted, refcount is not designed to be used for locking. And will make sure thedown_read_trylock()replacement will provide an acquire fence.
Hmm.. refcount_t is defined with atomic_t. I am lost why replacing refcount_t with atomic_t would help.
Quoted 37 lines
Quoted 11 lines
Otherwise I would suggest:
1. a comment above__refcount_inc_not_zero_limitedsaying there is an acq fence issued later
2.smp_rmb()slapped between that and seq accessesIf the now removed fence is somehow not needed, I think a comment explaining it is necessary.
Quoted 2 lines
(quoted diff) +0
-0@@ -813,36 +856,33 @@ static inline void vma_assert_write_locked(struct vm_area_struct *vma)Quoted 11 lines
include/linux/mm.h static inline void vma_assert_write_locked(struct vm_area_struct *vma) · line 862
Quoted 7 lines
include/linux/mm.h +1
-1static inline void vma_assert_locked (struct vm_area_struct *vma) { -if (!rwsem_is_locked (&vma->vm_lock.lock)) +if (refcount_read (&vma->vm_refcnt) <=1 )vma_assert_write_locked (vma); }This now forces the compiler to emit a load from
vm_refcnteven ifvma_assert_write_lockedexpands to nothing. iow this wants to hide behind the same stuff asvma_assert_write_locked.True. I guess I'll have to avoid using
vma_assert_write_locked()like this:static inline void
vma_assert_locked(struct vm_area_struct *vma){ unsigned int mm_lock_seq; VM_BUG_ON_VMA(refcount_read(&vma->vm_refcnt) <= 1 && !__is_vma_write_locked(vma,
&mm_lock_seq), vma);
}Will make the change.
Thanks for the feedback!
Signature · 2 lines
Wei Yang Help you, Help me
Re: [PATCH v9 11/17] mm: replace vm_lock and detached flag with a reference count
Quoted 54 lines · Wei Yang
Quoted 8 lines · Suren Baghdasaryan
Quoted 7 lines · Mateusz Guzik
On Fri, Jan 10, 2025 at 08:25:58PM -0800, Suren Baghdasaryan wrote:
So there were quite a few iterations of the patch and I have not been reading majority of the feedback, so it may be I missed something, apologies upfront. :)
Hi, I am new to memory barriers. Hope not bothering.
Quoted 42 lines
Quoted 16 lines
Quoted 16 lines
/*
* Try to read-lock a vma. The function is allowed to occasionally yield false
* locked result to avoid performance overhead, in which case we fall back toinclude/linux/mm.h +2
-0@@ -710,6 +742,8 @@ static inline void vma_lock_init(struct vm_area_struct *vma)*/ static inline bool vma_start_read (struct vm_area_struct *vma) { +int oldcnt; +/* * Check before locking. A race might cause false locked result. * We can use READ_ONCE() for the mm_lock_seq here, and don't need @@ -720,13 +754,19 @@ static inline bool vma_start_read(struct vm_area_struct *vma)if (READ_ONCE (vma->vm_lock_seq) ==READ_ONCE (vma->vm_mm->mm_lock_seq.sequence))return false;Quoted 12 lines
include/linux/mm.h static inline bool vma_start_read(struct vm_area_struct *vma) · line 764
Quoted 9 lines
include/linux/mm.h +6
-1-if (unlikely (down_read_trylock (&vma->vm_lock.lock) ==0 )) +/* +* If VMA_LOCK_OFFSET is set, __refcount_inc_not_zero_limited() will fail +* because VMA_REF_LIMIT is less than VMA_LOCK_OFFSET. +*/ +if (unlikely (!__refcount_inc_not_zero_limited (&vma->vm_refcnt, &oldcnt, +VMA_REF_LIMIT )))return false;Replacing
down_read_trylock()with the new routine loses an acquire fence. That alone is not a problem, but see below.Hmm. I think this acquire fence is actually necessary. We don't want the later
vm_lock_seqcheck to be reordered and happen before we take the refcount. Otherwise this might happen:reader writer
if (vm_lock_seq==mm_lock_seq) // check got reorderedreturn false; vm_refcnt += VMA_LOCK_OFFSET vm_lock_seq == mm_lock_seq vm_refcnt -= VMA_LOCK_OFFSET if (!__refcount_inc_not_zero_limited()) return false;
Quoted 7 lines
Quoted 3 lines
Both reader's checks will pass and the reader would read-lock a vma that was write-locked.
Here what we plan to do is define
__refcount_inc_not_zero_limited()with acquire fence, e.g. withatomic_try_cmpxchg_acquire(), right?
BTW, usually we pair acquire with release.
The __vma_start_write() provide release fence when locked, so for this part we are ok, right?
Signature · 2 lines
Wei Yang Help you, Help me
Suren Baghdasaryan
maintainerRe: [PATCH v9 11/17] mm: replace vm_lock and detached flag with a reference count
Quoted 60 lines · Wei Yang
Quoted 58 lines · Wei Yang
Quoted 8 lines · Suren Baghdasaryan
Quoted 7 lines · Mateusz Guzik
On Fri, Jan 10, 2025 at 08:25:58PM -0800, Suren Baghdasaryan wrote:
So there were quite a few iterations of the patch and I have not been reading majority of the feedback, so it may be I missed something, apologies upfront. :)
Hi, I am new to memory barriers. Hope not bothering.
Quoted 45 lines
Quoted 16 lines
Quoted 16 lines
/*
* Try to read-lock a vma. The function is allowed to occasionally yield false
* locked result to avoid performance overhead, in which case we fall back toinclude/linux/mm.h +2
-0@@ -710,6 +742,8 @@ static inline void vma_lock_init(struct vm_area_struct *vma)*/ static inline bool vma_start_read (struct vm_area_struct *vma) { +int oldcnt; +/* * Check before locking. A race might cause false locked result. * We can use READ_ONCE() for the mm_lock_seq here, and don't need @@ -720,13 +754,19 @@ static inline bool vma_start_read(struct vm_area_struct *vma)if (READ_ONCE (vma->vm_lock_seq) ==READ_ONCE (vma->vm_mm->mm_lock_seq.sequence))return false;Quoted 12 lines
include/linux/mm.h static inline bool vma_start_read(struct vm_area_struct *vma) · line 764
Quoted 9 lines
include/linux/mm.h +6
-1-if (unlikely (down_read_trylock (&vma->vm_lock.lock) ==0 )) +/* +* If VMA_LOCK_OFFSET is set, __refcount_inc_not_zero_limited() will fail +* because VMA_REF_LIMIT is less than VMA_LOCK_OFFSET. +*/ +if (unlikely (!__refcount_inc_not_zero_limited (&vma->vm_refcnt, &oldcnt, +VMA_REF_LIMIT )))return false;Replacing
down_read_trylock()with the new routine loses an acquire fence. That alone is not a problem, but see below.Hmm. I think this acquire fence is actually necessary. We don't want the later
vm_lock_seqcheck to be reordered and happen before we take the refcount. Otherwise this might happen:reader writer
if (vm_lock_seq==mm_lock_seq) // check got reorderedreturn false; vm_refcnt += VMA_LOCK_OFFSET vm_lock_seq == mm_lock_seq vm_refcnt -= VMA_LOCK_OFFSET if (!__refcount_inc_not_zero_limited()) return false;Both reader's checks will pass and the reader would read-lock a vma that was write-locked.
Quoted 8 lines
Quoted 3 lines
Here what we plan to do is define
__refcount_inc_not_zero_limited()with acquire fence, e.g. withatomic_try_cmpxchg_acquire(), right?BTW, usually we pair acquire with release.
The
__vma_start_write()provide release fence when locked, so for this part we are ok, right?
Yes, __vma_start_write() -> __vma_exit_locked() ->refcount_sub_and_test() and this function provides release memory
ordering, see https://elixir.bootlin.com/linux/v6.12.6/source/include/linux/refcount.h#L289
Quoted 5 lines
--
Wei Yang
Help you, Help me
Suren Baghdasaryan
maintainerRe: [PATCH v9 11/17] mm: replace vm_lock and detached flag with a reference count
In reply to Wei YangQuoted 55 lines · Wei Yang
Quoted 8 lines · Suren Baghdasaryan
Quoted 7 lines · Mateusz Guzik
On Fri, Jan 10, 2025 at 08:25:58PM -0800, Suren Baghdasaryan wrote:
So there were quite a few iterations of the patch and I have not been reading majority of the feedback, so it may be I missed something, apologies upfront. :)
Hi, I am new to memory barriers. Hope not bothering.
Quoted 42 lines
Quoted 16 lines
Quoted 16 lines
/*
* Try to read-lock a vma. The function is allowed to occasionally yield false
* locked result to avoid performance overhead, in which case we fall back toinclude/linux/mm.h +2
-0@@ -710,6 +742,8 @@ static inline void vma_lock_init(struct vm_area_struct *vma)*/ static inline bool vma_start_read (struct vm_area_struct *vma) { +int oldcnt; +/* * Check before locking. A race might cause false locked result. * We can use READ_ONCE() for the mm_lock_seq here, and don't need @@ -720,13 +754,19 @@ static inline bool vma_start_read(struct vm_area_struct *vma)if (READ_ONCE (vma->vm_lock_seq) ==READ_ONCE (vma->vm_mm->mm_lock_seq.sequence))return false;Quoted 12 lines
include/linux/mm.h static inline bool vma_start_read(struct vm_area_struct *vma) · line 764
Quoted 9 lines
include/linux/mm.h +6
-1-if (unlikely (down_read_trylock (&vma->vm_lock.lock) ==0 )) +/* +* If VMA_LOCK_OFFSET is set, __refcount_inc_not_zero_limited() will fail +* because VMA_REF_LIMIT is less than VMA_LOCK_OFFSET. +*/ +if (unlikely (!__refcount_inc_not_zero_limited (&vma->vm_refcnt, &oldcnt, +VMA_REF_LIMIT )))return false;Replacing
down_read_trylock()with the new routine loses an acquire fence. That alone is not a problem, but see below.Hmm. I think this acquire fence is actually necessary. We don't want the later
vm_lock_seqcheck to be reordered and happen before we take the refcount. Otherwise this might happen:reader writer
if (vm_lock_seq==mm_lock_seq) // check got reorderedreturn false; vm_refcnt += VMA_LOCK_OFFSET vm_lock_seq == mm_lock_seq vm_refcnt -= VMA_LOCK_OFFSET if (!__refcount_inc_not_zero_limited()) return false;
Quoted 6 lines
Quoted 3 lines
Both reader's checks will pass and the reader would read-lock a vma that was write-locked.
Here what we plan to do is define
__refcount_inc_not_zero_limited()with acquire fence, e.g. withatomic_try_cmpxchg_acquire(), right?
Correct. __refcount_inc_not_zero_limited() does not do that in this version but I'll fix that.
Quoted 20 lines
Quoted 16 lines
Quoted 16 lines
Quoted 13 lines
include/linux/mm.h +3
-2+rwsem_acquire_read (&vma->vmlock_dep_map,0 ,1 , _RET_IP_);/* -* Overflow might produce false locked result. +* Overflow of vm_lock_seq/mm_lock_seq might produce false locked result. * False unlocked result is impossible because we modify and check -* vma->vm_lock_seq under vma->vm_lock protection and mm->mm_lock_seq +* vma->vm_lock_seq under vma->vm_refcnt protection and mm->mm_lock_seq * modification invalidates all existing locks. * * We must use ACQUIRE semantics for the mm_lock_seq so that if we are include/linux/mm.h static inline bool vma_start_read(struct vm_area_struct *vma) · line 777
Quoted 3 lines
include/linux/mm.h +0
-0@@ -735,9 +775,10 @@ static inline bool vma_start_read(struct vm_area_struct *vma)* This pairs with RELEASE semantics in vma_end_write_all(). */ if (unlikely (vma->vm_lock_seq ==raw_read_seqcount (&vma->vm_mm->mm_lock_seq))) {One question here is would compiler optimize the read of
vm_lock_seqhere, since we have read it at the beginning?
Quoted 1 line
Or with the acquire fence added above, compiler won't optimize it.
Correct. See "ACQUIRE operations" section in
https://www.kernel.org/doc/Documentation/memory-barriers.txt,
specifically this: "It guarantees that all memory operations after the ACQUIRE operation will appear to happen after the ACQUIRE operation with respect to the other components of the system.".
Quoted 26 lines
Or we should use
REACE_ONCE(vma->vm_lock_seq)here?Quoted 24 lines
Quoted 3 lines
The previous modification of this spot to
raw_read_seqcountloses the acquire fence, making the above comment not line up with the code.Is it? From reading the seqcount code
(https://elixir.bootlin.com/linux/v6.13-rc3/source/include/linux/seqlock.h#L211):
raw_read_seqcount()
seqprop_sequence()
__seqprop(s, sequence)
__seqprop_sequence()
smp_load_acquire()
smp_load_acquire()still provides the acquire fence. Am I missing something?Quoted 8 lines
I don't know if the stock code (with
down_read_trylock()) is correct as is -- looks fine for cursory reading fwiw. However, if it indeed works, the acquire fence stemming from the lock routine is a mandatory part of it afaics.I think the best way forward is to add a new refcount routine which ships with an acquire fence.
Quoted 8 lines
Quoted 5 lines
I plan on replacing
refcount_tusage here with an atomic since, as Hillf noted, refcount is not designed to be used for locking. And will make sure thedown_read_trylock()replacement will provide an acquire fence.Hmm..
refcount_tis defined withatomic_t. I am lost why replacingrefcount_twithatomic_twould help.
My point is that refcount_t is not designed for locking, so changing refcount-related functions and adding fences there would be wrong. So, I'll move to using more generic atomic_t and will implement the functionality I need without affecting refcounting functions.
Quoted 42 lines
Quoted 32 lines
Quoted 12 lines
Otherwise I would suggest:
1. a comment above__refcount_inc_not_zero_limitedsaying there is an acq fence issued later
2.smp_rmb()slapped between that and seq accessesIf the now removed fence is somehow not needed, I think a comment explaining it is necessary.
Quoted 2 lines
(quoted diff) +0
-0@@ -813,36 +856,33 @@ static inline void vma_assert_write_locked(struct vm_area_struct *vma)Quoted 11 lines
include/linux/mm.h static inline void vma_assert_write_locked(struct vm_area_struct *vma) · line 862
Quoted 7 lines
include/linux/mm.h +1
-1static inline void vma_assert_locked (struct vm_area_struct *vma) { -if (!rwsem_is_locked (&vma->vm_lock.lock)) +if (refcount_read (&vma->vm_refcnt) <=1 )vma_assert_write_locked (vma); }This now forces the compiler to emit a load from
vm_refcnteven ifvma_assert_write_lockedexpands to nothing. iow this wants to hide behind the same stuff asvma_assert_write_locked.True. I guess I'll have to avoid using
vma_assert_write_locked()like this:static inline void
vma_assert_locked(struct vm_area_struct *vma){ unsigned int mm_lock_seq; VM_BUG_ON_VMA(refcount_read(&vma->vm_refcnt) <= 1 && !__is_vma_write_locked(vma,Quoted 6 lines
&mm_lock_seq), vma);
}Will make the change.
Thanks for the feedback!
--
Wei Yang
Help you, Help me
Peter Zijlstra
maintainerRe: [PATCH v9 11/17] mm: replace vm_lock and detached flag with a reference count
In reply to Suren BaghdasaryanQuoted 16 lines · Suren Baghdasaryan
Quoted 2 lines
Replacing
down_read_trylock()with the new routine loses an acquire fence. That alone is not a problem, but see below.Hmm. I think this acquire fence is actually necessary. We don't want the later
vm_lock_seqcheck to be reordered and happen before we take the refcount. Otherwise this might happen:reader writer
if (vm_lock_seq==mm_lock_seq) // check got reorderedreturn false; vm_refcnt += VMA_LOCK_OFFSET vm_lock_seq == mm_lock_seq vm_refcnt -= VMA_LOCK_OFFSET if (!__refcount_inc_not_zero_limited()) return false;
Quoted 2 lines
Both reader's checks will pass and the reader would read-lock a vma that was write-locked.
Hmm, you're right. That acquire does matter here.
Peter Zijlstra
maintainerRe: [PATCH v9 11/17] mm: replace vm_lock and detached flag with a reference count
Quoted 18 lines · Peter Zijlstra
Quoted 16 lines · Suren Baghdasaryan
Quoted 2 lines
Replacing
down_read_trylock()with the new routine loses an acquire fence. That alone is not a problem, but see below.Hmm. I think this acquire fence is actually necessary. We don't want the later
vm_lock_seqcheck to be reordered and happen before we take the refcount. Otherwise this might happen:reader writer
if (vm_lock_seq==mm_lock_seq) // check got reorderedreturn false; vm_refcnt += VMA_LOCK_OFFSET vm_lock_seq == mm_lock_seq vm_refcnt -= VMA_LOCK_OFFSET if (!__refcount_inc_not_zero_limited()) return false;
Quoted 4 lines
Quoted 2 lines
Both reader's checks will pass and the reader would read-lock a vma that was write-locked.
Hmm, you're right. That acquire does matter here.
Notably, it means refcount_t is entirely unsuitable for anything SLAB_TYPESAFE_BY_RCU, since they all will need secondary validation conditions after the refcount succeeds.
And this is probably fine, but let me ponder this all a little more.
Suren Baghdasaryan
maintainerRe: [PATCH v9 11/17] mm: replace vm_lock and detached flag with a reference count
Quoted 20 lines · Peter Zijlstra
Quoted 18 lines · Peter Zijlstra
Quoted 16 lines · Suren Baghdasaryan
Quoted 2 lines
Replacing
down_read_trylock()with the new routine loses an acquire fence. That alone is not a problem, but see below.Hmm. I think this acquire fence is actually necessary. We don't want the later
vm_lock_seqcheck to be reordered and happen before we take the refcount. Otherwise this might happen:reader writer
if (vm_lock_seq==mm_lock_seq) // check got reorderedreturn false; vm_refcnt += VMA_LOCK_OFFSET vm_lock_seq == mm_lock_seq vm_refcnt -= VMA_LOCK_OFFSET if (!__refcount_inc_not_zero_limited()) return false;
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Both reader's checks will pass and the reader would read-lock a vma that was write-locked.
Hmm, you're right. That acquire does matter here.
Notably, it means
refcount_tis entirely unsuitable for anythingSLAB_TYPESAFE_BY_RCU, since they all will need secondary validation conditions after the refcount succeeds.
Thanks for reviewing, Peter!
Yes, I'm changing the code to use atomic_t instead of refcount_t and it comes out quite nicely I think. I had to add two small helper functions:vm_refcount_inc() - similar to refcount_add_not_zero() but with an acquired fence.vm_refcnt_sub() - similar to refcount_sub_and_test(). I could use atomic_sub_and_test() but that would add unnecessary acquire fence in the pagefault path, so I'm using refcount_sub_and_test() logic instead.
For SLAB_TYPESAFE_BY_RCU I think we are ok with the __vma_enter_locked()/__vma_exit_locked() transition in the vma_mark_detached() before freeing the vma and would not need secondary validation. In __vma_enter_locked(), vm_refcount gets VMA_LOCK_OFFSET set, which prevents readers from taking the refcount. In __vma_exit_locked() vm_refcnt transitions to 0, so again that prevents readers from taking the refcount. IOW, the readers won't get to the secondary validation and will fail early on __refcount_inc_not_zero_limited(). I think this transition correctly serves the purpose of waiting for current temporary readers to exit and preventing new readers from read-locking and using the vma.
Quoted 2 lines
And this is probably fine, but let me ponder this all a little more.
Thanks for taking the time!
Peter Zijlstra
maintainerRe: [PATCH v9 11/17] mm: replace vm_lock and detached flag with a reference count
Quoted 30 lines · Suren Baghdasaryan
Quoted 20 lines · Peter Zijlstra
Quoted 18 lines · Peter Zijlstra
Quoted 16 lines · Suren Baghdasaryan
Quoted 2 lines
Replacing
down_read_trylock()with the new routine loses an acquire fence. That alone is not a problem, but see below.Hmm. I think this acquire fence is actually necessary. We don't want the later
vm_lock_seqcheck to be reordered and happen before we take the refcount. Otherwise this might happen:reader writer
if (vm_lock_seq==mm_lock_seq) // check got reorderedreturn false; vm_refcnt += VMA_LOCK_OFFSET vm_lock_seq == mm_lock_seq vm_refcnt -= VMA_LOCK_OFFSET if (!__refcount_inc_not_zero_limited()) return false;Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Both reader's checks will pass and the reader would read-lock a vma that was write-locked.
Hmm, you're right. That acquire does matter here.
Notably, it means
refcount_tis entirely unsuitable for anythingSLAB_TYPESAFE_BY_RCU, since they all will need secondary validation conditions after the refcount succeeds.
Quoted 10 lines
Thanks for reviewing, Peter!
Yes, I'm changing the code to useatomic_tinstead ofrefcount_tand it comes out quite nicely I think. I had to add two small helper functions:vm_refcount_inc()- similar torefcount_add_not_zero()but with an acquired fence.vm_refcnt_sub()- similar torefcount_sub_and_test(). I could useatomic_sub_and_test()but that would add unnecessary acquire fence in the pagefault path, so I'm usingrefcount_sub_and_test()logic instead.
Right.
Quoted 11 lines
For
SLAB_TYPESAFE_BY_RCUI think we are ok with the __vma_enter_locked()/__vma_exit_locked() transition in thevma_mark_detached()before freeing the vma and would not need secondary validation. In__vma_enter_locked(),vm_refcountgetsVMA_LOCK_OFFSETset, which prevents readers from taking the refcount. In__vma_exit_locked()vm_refcnttransitions to 0, so again that prevents readers from taking the refcount. IOW, the readers won't get to the secondary validation and will fail early on__refcount_inc_not_zero_limited(). I think this transition correctly serves the purpose of waiting for current temporary readers to exit and preventing new readers from read-locking and using the vma.
Consider:
CPU0 CPU1
rcu_read_lock();
vma = vma_lookup(mm, vaddr);
... cpu goes sleep for a *long time* ...
__vma_exit_locked();
vma_area_free()
..
vma = vma_area_alloc();
vma_mark_attached();
... comes back once vma is re-used ...
vma_start_read()
vm_refcount_inc(); // success!!At which point we need to validate vma is for mm and covers vaddr, which is what patch 15 does, no?
Also, I seem to have forgotten some braces back in 2008 :-)
---
include/linux/slab.h +2 -1
@@ -115,9 +115,10 @@ enum _slab_flag_bits {* rcu_read_lock(); * obj = lockless_lookup(key); * if (obj) { -* if (!try_get_ref(obj)) // might fail for free objects +* if (!try_get_ref(obj)) { // might fail for free objects * rcu_read_unlock(); * goto begin; +* } * * if (obj->key != key) { // not the object we expected * put_ref(obj);
Peter Zijlstra
maintainerRe: [PATCH v9 11/17] mm: replace vm_lock and detached flag with a reference count
Quoted 20 lines · Peter Zijlstra
Consider:
CPU0 CPU1 rcu_read_lock(); vma = vma_lookup(mm, vaddr); ... cpu goes sleep for a *long time* ... __vma_exit_locked(); vma_area_free() .. vma = vma_area_alloc(); vma_mark_attached(); ... comes back once vma is re-used ... vma_start_read() vm_refcount_inc(); // success!!
Quoted 2 lines
At which point we need to validate vma is for mm and covers vaddr, which is what patch 15 does, no?
Also, critically, we want these reads to happen *after* the refcount increment.
Suren Baghdasaryan
maintainerRe: [PATCH v9 11/17] mm: replace vm_lock and detached flag with a reference count
Quoted 18 lines · Peter Zijlstra
Quoted 15 lines · Peter Zijlstra
Consider:
CPU0 CPU1 rcu_read_lock(); vma = vma_lookup(mm, vaddr); ... cpu goes sleep for a *long time* ... __vma_exit_locked(); vma_area_free() .. vma = vma_area_alloc(); vma_mark_attached();
Quoted 7 lines
Quoted 7 lines
... comes back once vma is re-used ...
vma_start_read()
vm_refcount_inc(); // success!!At which point we need to validate vma is for mm and covers vaddr, which is what patch 15 does, no?
Correct. Sorry, I thought by "secondary validation" you only meant vm_lock_seq check in vma_start_read(). Now I understand your point. Yes, if the vma we found gets reused before we read-lock it then the checks for mm and address range should catch a possibly incorrect vma. If these checks fail, we retry. If they succeed we have the correct vma even if it was recycled since we found it.
Quoted 3 lines
Also, critically, we want these reads to happen *after* the refcount increment.
Yes, and I think the acquire fence in the refcount_add_not_zero_limited() replacement should guarantee that ordering.
Quoted 1 line
Peter Zijlstra
maintainerPATCH 1/1 refcount: Strengthen inc_not_zero()
In reply to Peter ZijlstraQuoted 5 lines · Peter Zijlstra
Notably, it means
refcount_tis entirely unsuitable for anythingSLAB_TYPESAFE_BY_RCU, since they all will need secondary validation conditions after the refcount succeeds.And this is probably fine, but let me ponder this all a little more.
Even though SLAB_TYPESAFE_BY_RCU is relatively rare, I think we'd better fix this, these things are hard enough as they are.
Will, others, wdyt?
---
Subject: refcount: Strengthen inc_not_zero()
For speculative lookups where a successful inc_not_zero() pins the object, but where we still need to double check if the object acquired is indeed the one we set out to aquire, needs this validation to happen *after* the increment.
Notably SLAB_TYPESAFE_BY_RCU is one such an example.
--- include/linux/refcount.h | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)
include/linux/refcount.h +8 -7
@@ -69,9 +69,10 @@* its the lock acquire, for RCU/lockless data structures its the dependent * load. * -* Do note that inc_not_zero() provides a control dependency which will order -* future stores against the inc, this ensures we'll never modify the object -* if we did not in fact acquire a reference. +* Do note that inc_not_zero() does provide acquire order, which will order +* future load and stores against the inc, this ensures all subsequent accesses +* are from this object and not anything previously occupying this memory -- +* consider SLAB_TYPESAFE_BY_RCU. * * The decrements will provide release order, such that all the prior loads and * stores will be issued before, it also provides a control dependency, which
@@ -144,7 +145,7 @@ bool __refcount_add_not_zero(int i, refcount_t *r, int *oldp)do {if (!old)break ; - }while (!atomic_try_cmpxchg_relaxed (&r->refs, &old, old + i)); + }while (!atomic_try_cmpxchg_acquire (&r->refs, &old, old + i));if (oldp) *oldp = old;
@@ -225,9 +226,9 @@ static inline __must_check bool __refcount_inc_not_zero(refcount_t *r, int *oldp* Similar to atomic_inc_not_zero(), but will saturate at REFCOUNT_SATURATED * and WARN. * -* Provides no memory ordering, it is assumed the caller has guaranteed the -* object memory to be stable (RCU, etc.). It does provide a control dependency -* and thereby orders future stores. See the comment on top. +* Provides acquire ordering, such that subsequent accesses are after the +* increment. This is important for the cases where secondary validation is +* required, eg. SLAB_TYPESAFE_BY_RCU. * * Return: true if the increment was successful, false otherwise */
Suren Baghdasaryan
maintainerRe: [PATCH] refcount: Strengthen inc_not_zero()
Quoted 7 lines · Peter Zijlstra
Quoted 4 lines · Peter Zijlstra
Notably, it means
refcount_tis entirely unsuitable for anythingSLAB_TYPESAFE_BY_RCU, since they all will need secondary validation conditions after the refcount succeeds.
Quoted 6 lines
Quoted 1 line
And this is probably fine, but let me ponder this all a little more.
Even though
SLAB_TYPESAFE_BY_RCUis relatively rare, I think we'd better fix this, these things are hard enough as they are.Will, others, wdyt?
I'll wait for the verdict on this patch before proceeding with my series. It obviously simplifies my job. Thanks Peter!
Quoted 56 lines
---
Subject: refcount: Strengtheninc_not_zero()For speculative lookups where a successful
inc_not_zero()pins the object, but where we still need to double check if the object acquired is indeed the one we set out to aquire, needs this validation to happen *after* the increment.Notably
SLAB_TYPESAFE_BY_RCUis one such an example.Signed-off-by Peter Zijlstra (Intel) <[email protected]>--- include/linux/refcount.h | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)include/linux/refcount.h +8
-7@@ -69,9 +69,10 @@* its the lock acquire, for RCU/lockless data structures its the dependent * load. * -* Do note that inc_not_zero() provides a control dependency which will order -* future stores against the inc, this ensures we'll never modify the object -* if we did not in fact acquire a reference. +* Do note that inc_not_zero() does provide acquire order, which will order +* future load and stores against the inc, this ensures all subsequent accesses +* are from this object and not anything previously occupying this memory -- +* consider SLAB_TYPESAFE_BY_RCU. * * The decrements will provide release order, such that all the prior loads and * stores will be issued before, it also provides a control dependency, which @@ -144,7 +145,7 @@ bool __refcount_add_not_zero(int i, refcount_t *r, int *oldp)do {if (!old)break ; - }while (!atomic_try_cmpxchg_relaxed (&r->refs, &old, old + i)); + }while (!atomic_try_cmpxchg_acquire (&r->refs, &old, old + i));if (oldp) *oldp = old;@@ -225,9 +226,9 @@ static inline __must_check bool __refcount_inc_not_zero(refcount_t *r, int *oldp* Similar to atomic_inc_not_zero(), but will saturate at REFCOUNT_SATURATED * and WARN. * -* Provides no memory ordering, it is assumed the caller has guaranteed the -* object memory to be stable (RCU, etc.). It does provide a control dependency -* and thereby orders future stores. See the comment on top. +* Provides acquire ordering, such that subsequent accesses are after the +* increment. This is important for the cases where secondary validation is +* required, eg. SLAB_TYPESAFE_BY_RCU. * * Return: true if the increment was successful, false otherwise */
Will Deacon
maintainerRe: [PATCH] refcount: Strengthen inc_not_zero()
In reply to Peter ZijlstraQuoted 6 lines · Peter Zijlstra
Quoted 4 lines · Peter Zijlstra
Notably, it means
refcount_tis entirely unsuitable for anythingSLAB_TYPESAFE_BY_RCU, since they all will need secondary validation conditions after the refcount succeeds.
Quoted 6 lines
Quoted 1 line
And this is probably fine, but let me ponder this all a little more.
Even though
SLAB_TYPESAFE_BY_RCUis relatively rare, I think we'd better fix this, these things are hard enough as they are.Will, others, wdyt?
We should also update the Documentation (atomic_t.txt and refcount-vs-atomic.rst) if we strengthen this.
Quoted 34 lines
---
Subject: refcount: Strengtheninc_not_zero()For speculative lookups where a successful
inc_not_zero()pins the object, but where we still need to double check if the object acquired is indeed the one we set out to aquire, needs this validation to happen *after* the increment.Notably
SLAB_TYPESAFE_BY_RCUis one such an example.Signed-off-by Peter Zijlstra (Intel) <[email protected]>--- include/linux/refcount.h | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)include/linux/refcount.h +4
-3@@ -69,9 +69,10 @@* its the lock acquire, for RCU/lockless data structures its the dependent * load. * -* Do note that inc_not_zero() provides a control dependency which will order -* future stores against the inc, this ensures we'll never modify the object -* if we did not in fact acquire a reference. +* Do note that inc_not_zero() does provide acquire order, which will order +* future load and stores against the inc, this ensures all subsequent accesses +* are from this object and not anything previously occupying this memory -- +* consider SLAB_TYPESAFE_BY_RCU. * * The decrements will provide release order, such that all the prior loads and * stores will be issued before, it also provides a control dependency, which
include/linux/refcount.h bool __refcount_add_not_zero(int i, refcount_t *r, int *oldp) · line 148
Quoted 5 lines
include/linux/refcount.h +1
-1@@ -144,7 +145,7 @@ bool __refcount_add_not_zero(int i, refcount_t *r, int *oldp)do {if (!old)break ; - }while (!atomic_try_cmpxchg_relaxed (&r->refs, &old, old + i)); + }while (!atomic_try_cmpxchg_acquire (&r->refs, &old, old + i));
Hmm, do the later memory accesses need to be ordered against the store part of the increment or just the read? If it's the former, then I don't think that _acquire is sufficient -- accesses can still get in-between the read and write parts of the RmW.
Will
Will Deacon
maintainerRe: [PATCH] refcount: Strengthen inc_not_zero()
Quoted 53 lines · Will Deacon
Quoted 6 lines · Peter Zijlstra
Quoted 4 lines · Peter Zijlstra
Notably, it means
refcount_tis entirely unsuitable for anythingSLAB_TYPESAFE_BY_RCU, since they all will need secondary validation conditions after the refcount succeeds.Quoted 6 lines
Quoted 1 line
And this is probably fine, but let me ponder this all a little more.
Even though
SLAB_TYPESAFE_BY_RCUis relatively rare, I think we'd better fix this, these things are hard enough as they are.Will, others, wdyt?
We should also update the Documentation (atomic_t.txt and
refcount-vs-atomic.rst) if we strengthen this.Quoted 36 lines
---
Subject: refcount: Strengtheninc_not_zero()For speculative lookups where a successful
inc_not_zero()pins the object, but where we still need to double check if the object acquired is indeed the one we set out to aquire, needs this validation to happen *after* the increment.Notably
SLAB_TYPESAFE_BY_RCUis one such an example.Signed-off-by Peter Zijlstra (Intel) <[email protected]>--- include/linux/refcount.h | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)include/linux/refcount.h +4
-3@@ -69,9 +69,10 @@* its the lock acquire, for RCU/lockless data structures its the dependent * load. * -* Do note that inc_not_zero() provides a control dependency which will order -* future stores against the inc, this ensures we'll never modify the object -* if we did not in fact acquire a reference. +* Do note that inc_not_zero() does provide acquire order, which will order +* future load and stores against the inc, this ensures all subsequent accesses +* are from this object and not anything previously occupying this memory -- +* consider SLAB_TYPESAFE_BY_RCU. * * The decrements will provide release order, such that all the prior loads and * stores will be issued before, it also provides a control dependency, which @@ -144,7 +145,7 @@ bool __refcount_add_not_zero(int i, refcount_t *r, int *oldp) do { if (!old)
Quoted 8 lines
include/linux/refcount.h bool __refcount_add_not_zero(int i, refcount_t *r, int *oldp) · line 148
Quoted 3 lines
include/linux/refcount.h +1
-1break ; - }while (!atomic_try_cmpxchg_relaxed (&r->refs, &old, old + i)); + }while (!atomic_try_cmpxchg_acquire (&r->refs, &old, old + i));Hmm, do the later memory accesses need to be ordered against the store part of the increment or just the read? If it's the former, then I don't think that
_acquireis sufficient -- accesses can still get in-between the read and write parts of the RmW.
I dug some more into this at the end of last week. For the SLAB_TYPESAFE_BY_RCU where we're racing inc_not_zero() with dec_and_test(), then I think using _acquire() above is correct as the later references can only move up into the critical section in the case that we successfully obtained a reference.
However, if we're going to make the barriers implicit in the refcount operations here then I think we also need to do something on the producer side for when the object is re-initialised after being destroyed and allocated again. I think that would necessitate release ordering for refcount_set() so that whatever allows the consumer to validate the object (e.g. sequence number) is published *before* the refcount.
Will
Suren Baghdasaryan
maintainerRe: [PATCH] refcount: Strengthen inc_not_zero()
Quoted 70 lines · Will Deacon
Quoted 53 lines · Will Deacon
Quoted 6 lines · Peter Zijlstra
Quoted 4 lines · Peter Zijlstra
Notably, it means
refcount_tis entirely unsuitable for anythingSLAB_TYPESAFE_BY_RCU, since they all will need secondary validation conditions after the refcount succeeds.Quoted 6 lines
Quoted 1 line
And this is probably fine, but let me ponder this all a little more.
Even though
SLAB_TYPESAFE_BY_RCUis relatively rare, I think we'd better fix this, these things are hard enough as they are.Will, others, wdyt?
We should also update the Documentation (atomic_t.txt and
refcount-vs-atomic.rst) if we strengthen this.Quoted 36 lines
---
Subject: refcount: Strengtheninc_not_zero()For speculative lookups where a successful
inc_not_zero()pins the object, but where we still need to double check if the object acquired is indeed the one we set out to aquire, needs this validation to happen *after* the increment.Notably
SLAB_TYPESAFE_BY_RCUis one such an example.Signed-off-by Peter Zijlstra (Intel) <[email protected]>--- include/linux/refcount.h | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)include/linux/refcount.h +4
-3@@ -69,9 +69,10 @@* its the lock acquire, for RCU/lockless data structures its the dependent * load. * -* Do note that inc_not_zero() provides a control dependency which will order -* future stores against the inc, this ensures we'll never modify the object -* if we did not in fact acquire a reference. +* Do note that inc_not_zero() does provide acquire order, which will order +* future load and stores against the inc, this ensures all subsequent accesses +* are from this object and not anything previously occupying this memory -- +* consider SLAB_TYPESAFE_BY_RCU. * * The decrements will provide release order, such that all the prior loads and * stores will be issued before, it also provides a control dependency, which @@ -144,7 +145,7 @@ bool __refcount_add_not_zero(int i, refcount_t *r, int *oldp) do { if (!old)Quoted 8 lines
include/linux/refcount.h bool __refcount_add_not_zero(int i, refcount_t *r, int *oldp) · line 148
Quoted 3 lines
include/linux/refcount.h +1
-1break ; - }while (!atomic_try_cmpxchg_relaxed (&r->refs, &old, old + i)); + }while (!atomic_try_cmpxchg_acquire (&r->refs, &old, old + i));Hmm, do the later memory accesses need to be ordered against the store part of the increment or just the read? If it's the former, then I don't think that
_acquireis sufficient -- accesses can still get in-between the read and write parts of the RmW.I dug some more into this at the end of last week. For the
SLAB_TYPESAFE_BY_RCUwhere we're racinginc_not_zero()withdec_and_test(), then I think using_acquire()above is correct as the later references can only move up into the critical section in the case that we successfully obtained a reference.
Quoted 6 lines
However, if we're going to make the barriers implicit in the refcount operations here then I think we also need to do something on the producer side for when the object is re-initialised after being destroyed and allocated again. I think that would necessitate release ordering for
refcount_set()so that whatever allows the consumer to validate the object (e.g. sequence number) is published *before* the refcount.
Thanks Will!
I would like to expand on your answer to provide an example of the race that would happen without release ordering in the producer. To save reader's time I provide a simplified flow and reasoning first. More detailed code of what I'm considering a typical SLAB_TYPESAFE_BY_RCU refcounting example is added at the end of my reply (Addendum).
Simplified flow looks like this:
consumer:
obj = lookup(collection, key);
if (!refcount_inc_not_zero(&obj->ref))
return;
smp_rmb(); /* Peter's new acquire fence */
if (READ_ONCE(obj->key) != key) {
put_ref(obj);
return;
}
use(obj->value);producer:
old_key = obj->key;
remove(collection, old_key);
if (!refcount_dec_and_test(&obj->ref))
return;
obj->key = KEY_INVALID;
free(objj);
...
obj = malloc(); /* obj is reused */
obj->key = new_key;
obj->value = new_value;
smp_wmb(); /* Will's proposed release fence */
refcount_set(obj->ref, 1);
insert(collection, key, obj);Let's consider a case when new_key == old_key. Will call both of them "key". Without WIll's proposed fence the following reordering is possible:
consumer:
obj = lookup(collection, key);
producer:
key = obj->key
remove(collection, key);
if (!refcount_dec_and_test(&obj->ref))
return;
obj->key = KEY_INVALID;
free(objj);
obj = malloc(); /* obj is reused */
refcount_set(obj->ref, 1);
obj->key = key; /* same key */
if (!refcount_inc_not_zero(&obj->ref))
return;
smp_rmb();
if (READ_ONCE(obj->key) != key) {
put_ref(obj);
return;
}
use(obj->value);
obj->value = new_value; /* reordered store */
add(collection, key, obj);So, the consumer finds the old object, successfully takes a refcount and validates the key. It succeeds because the object is allocated and has the same key, which is fine. However it proceeds to use stale obj->value. Will's proposed release ordering would prevent that.
The example in https://elixir.bootlin.com/linux/v6.12.6/source/include/linux/slab.h#L102
omits all these ordering issues for SLAB_TYPESAFE_BY_RCU.
I think it would be better to introduce two new functions:refcount_add_not_zero_acquire() and refcount_set_release(), clearly document that they should be used when a freed object can be recycled
and reused, like in SLAB_TYPESAFE_BY_RCU case. refcount_set_release() should also clarify that once it's called, the object can be accessed
by consumers even if it was not added yet into the collection used for object lookup (like in the example above). SLAB_TYPESAFE_BY_RCU
comment at https://elixir.bootlin.com/linux/v6.12.6/source/include/linux/slab.h#L102
then can explicitly use these new functions in the example code,
further clarifying their purpose and proper use.
WDYT?
ADDENDUM.
Detailed code for typical use of refcounting with SLAB_TYPESAFE_BY_RCU:
struct object {
refcount_t ref;
u64 key;
u64 value;
};void init(struct object *obj, u64 key, u64 value)
{
obj->key = key;
obj->value = value;
smp_wmb(); /* Will's proposed release fence */
refcount_set(obj->ref, 1);
}bool get_ref(struct object *obj, u64 key)
{
if (!refcount_inc_not_zero(&obj->ref))
return false;
smp_rmb(); /* Peter's new acquire fence */
if (READ_ONCE(obj->key) != key) {
put_ref(obj);
return false;
}
return true;
}void put_ref(struct object *obj)
{
if (!refcount_dec_and_test(&obj->ref))
return;
obj->key = KEY_INVALID;
free(obj);
}consumer:
obj = lookup(collection, key);
if (!get_ref(obj, key)
return;
use(obj->value);producer:
remove(collection, old_obj->key);
put_ref(old_obj);
new_obj = malloc();
init(new_obj, new_key, new_value);
insert(collection, new_key, new_obj);With SLAB_TYPESAFE_BY_RCU old_obj in the producer can be reused and be equal to new_obj.
Quoted 2 lines
Will
Suren Baghdasaryan
maintainerRe: [PATCH] refcount: Strengthen inc_not_zero()
Quoted 149 lines · Suren Baghdasaryan
Quoted 70 lines · Will Deacon
Quoted 53 lines · Will Deacon
Quoted 6 lines · Peter Zijlstra
Quoted 4 lines · Peter Zijlstra
Notably, it means
refcount_tis entirely unsuitable for anythingSLAB_TYPESAFE_BY_RCU, since they all will need secondary validation conditions after the refcount succeeds.Quoted 6 lines
Quoted 1 line
And this is probably fine, but let me ponder this all a little more.
Even though
SLAB_TYPESAFE_BY_RCUis relatively rare, I think we'd better fix this, these things are hard enough as they are.Will, others, wdyt?
We should also update the Documentation (atomic_t.txt and
refcount-vs-atomic.rst) if we strengthen this.Quoted 36 lines
---
Subject: refcount: Strengtheninc_not_zero()For speculative lookups where a successful
inc_not_zero()pins the object, but where we still need to double check if the object acquired is indeed the one we set out to aquire, needs this validation to happen *after* the increment.Notably
SLAB_TYPESAFE_BY_RCUis one such an example.Signed-off-by Peter Zijlstra (Intel) <[email protected]>--- include/linux/refcount.h | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)include/linux/refcount.h +4
-3@@ -69,9 +69,10 @@* its the lock acquire, for RCU/lockless data structures its the dependent * load. * -* Do note that inc_not_zero() provides a control dependency which will order -* future stores against the inc, this ensures we'll never modify the object -* if we did not in fact acquire a reference. +* Do note that inc_not_zero() does provide acquire order, which will order +* future load and stores against the inc, this ensures all subsequent accesses +* are from this object and not anything previously occupying this memory -- +* consider SLAB_TYPESAFE_BY_RCU. * * The decrements will provide release order, such that all the prior loads and * stores will be issued before, it also provides a control dependency, which @@ -144,7 +145,7 @@ bool __refcount_add_not_zero(int i, refcount_t *r, int *oldp) do { if (!old)Quoted 8 lines
include/linux/refcount.h bool __refcount_add_not_zero(int i, refcount_t *r, int *oldp) · line 148
Quoted 3 lines
include/linux/refcount.h +1
-1break ; - }while (!atomic_try_cmpxchg_relaxed (&r->refs, &old, old + i)); + }while (!atomic_try_cmpxchg_acquire (&r->refs, &old, old + i));Hmm, do the later memory accesses need to be ordered against the store part of the increment or just the read? If it's the former, then I don't think that
_acquireis sufficient -- accesses can still get in-between the read and write parts of the RmW.I dug some more into this at the end of last week. For the
SLAB_TYPESAFE_BY_RCUwhere we're racinginc_not_zero()withdec_and_test(), then I think using_acquire()above is correct as the later references can only move up into the critical section in the case that we successfully obtained a reference.Quoted 6 lines
However, if we're going to make the barriers implicit in the refcount operations here then I think we also need to do something on the producer side for when the object is re-initialised after being destroyed and allocated again. I think that would necessitate release ordering for
refcount_set()so that whatever allows the consumer to validate the object (e.g. sequence number) is published *before* the refcount.Thanks Will!
I would like to expand on your answer to provide an example of the race that would happen without release ordering in the producer. To save reader's time I provide a simplified flow and reasoning first. More detailed code of what I'm considering a typicalSLAB_TYPESAFE_BY_RCUrefcounting example is added at the end of my reply (Addendum).
Simplified flow looks like this:consumer:
obj = lookup(collection, key); if (!refcount_inc_not_zero(&obj->ref)) return; smp_rmb(); /* Peter's new acquire fence */ if (READ_ONCE(obj->key) != key) { put_ref(obj); return; } use(obj->value);producer:
old_key = obj->key; remove(collection, old_key); if (!refcount_dec_and_test(&obj->ref)) return; obj->key = KEY_INVALID; free(objj); ... obj = malloc(); /* obj is reused */ obj->key = new_key; obj->value = new_value; smp_wmb(); /* Will's proposed release fence */ refcount_set(obj->ref, 1); insert(collection, key, obj);Let's consider a case when
new_key==old_key. Will call both of them "key". Without WIll's proposed fence the following reordering is possible:consumer:
obj = lookup(collection, key); producer: key = obj->key remove(collection, key); if (!refcount_dec_and_test(&obj->ref)) return; obj->key = KEY_INVALID; free(objj); obj = malloc(); /* obj is reused */ refcount_set(obj->ref, 1); obj->key = key; /* same key */ if (!refcount_inc_not_zero(&obj->ref)) return; smp_rmb(); if (READ_ONCE(obj->key) != key) { put_ref(obj); return; } use(obj->value); obj->value = new_value; /* reordered store */ add(collection, key, obj);So, the consumer finds the old object, successfully takes a refcount and validates the key. It succeeds because the object is allocated and has the same key, which is fine. However it proceeds to use stale
obj->value. Will's proposed release ordering would prevent that.
Quoted 13 lines
The example in https://elixir.bootlin.com/linux/v6.12.6/source/include/linux/slab.h#L102
omits all these ordering issues forSLAB_TYPESAFE_BY_RCU.
I think it would be better to introduce two new functions:refcount_add_not_zero_acquire()andrefcount_set_release(), clearly document that they should be used when a freed object can be recycled
and reused, like inSLAB_TYPESAFE_BY_RCUcase.refcount_set_release()should also clarify that once it's called, the object can be accessed
by consumers even if it was not added yet into the collection used for object lookup (like in the example above).SLAB_TYPESAFE_BY_RCU
comment at https://elixir.bootlin.com/linux/v6.12.6/source/include/linux/slab.h#L102
then can explicitly use these new functions in the example code,
further clarifying their purpose and proper use.
WDYT?
Hi Peter,
Should I take a stab at preparing a patch to add the two new refcounting functions suggested above with updates to the documentation and comments?
If you disagree with that or need more time to decide then I'll wait. Please let me know.
Thanks,
Suren.
Quoted 57 lines
ADDENDUM.
Detailed code for typical use of refcounting withSLAB_TYPESAFE_BY_RCU:struct object { refcount_t ref; u64 key; u64 value; };void
init(struct object *obj, u64 key, u64 value){ obj->key = key; obj->value = value; smp_wmb(); /* Will's proposed release fence */ refcount_set(obj->ref, 1); }bool
get_ref(struct object *obj, u64 key){ if (!refcount_inc_not_zero(&obj->ref)) return false; smp_rmb(); /* Peter's new acquire fence */ if (READ_ONCE(obj->key) != key) { put_ref(obj); return false; } return true; }void
put_ref(struct object *obj){ if (!refcount_dec_and_test(&obj->ref)) return; obj->key = KEY_INVALID; free(obj); }consumer:
obj = lookup(collection, key); if (!get_ref(obj, key) return; use(obj->value);producer:
remove(collection, old_obj->key); put_ref(old_obj); new_obj = malloc(); init(new_obj, new_key, new_value); insert(collection, new_key, new_obj);With
SLAB_TYPESAFE_BY_RCUold_objin the producer can be reused and be equal tonew_obj.Quoted 2 lines
Will
Suren Baghdasaryan
maintainerPATCH 1/1 refcount: provide ops for cases when object's memory can be reused
For speculative lookups where a successful inc_not_zero() pins the object, but where we still need to double check if the object acquired is indeed the one we set out to acquire (identity check), needs this validation to happen *after* the increment.
Similarly, when a new object is initialized and its memory might have been previously occupied by another object, all stores to initialize the object should happen *before* refcount initialization.
Notably SLAB_TYPESAFE_BY_RCU is one such an example when this ordering is required for reference counting.
Add refcount_{add|inc}_not_zero_acquire() to guarantee the proper ordering between acquiring a reference count on an object and performing the identity check for that object.
Add refcount_set_release() to guarantee proper ordering between stores initializing object attributes and the store initializing the refcount. refcount_set_release() should be done after all other object attributes are initialized. Once refcount_set_release() is called, the object should be considered visible to other tasks even if it was not yet added into an object collection normally used to discover it. This is because other tasks might have discovered the object previously occupying the same memory and after memory reuse they can succeed in taking refcount for the new object and start using it.
Object reuse example to consider:
consumer:
obj = lookup(collection, key);
if (!refcount_inc_not_zero_acquire(&obj->ref))
return;
if (READ_ONCE(obj->key) != key) { /* identity check */
put_ref(obj);
return;
}
use(obj->value);
producer:
remove(collection, obj->key);
if (!refcount_dec_and_test(&obj->ref))
return;
obj->key = KEY_INVALID;
free(obj);
obj = malloc(); /* obj is reused */
obj->key = new_key;
obj->value = new_value;
refcount_set_release(obj->ref, 1);
add(collection, new_key, obj);refcount_{add|inc}_not_zero_acquire() is required to prevent the following reordering when refcount_inc_not_zero() is used instead:
consumer:
obj = lookup(collection, key);
if (READ_ONCE(obj->key) != key) { /* reordered identity check */
put_ref(obj);
return;
}
producer:
remove(collection, obj->key);
if (!refcount_dec_and_test(&obj->ref))
return;
obj->key = KEY_INVALID;
free(obj);
obj = malloc(); /* obj is reused */
obj->key = new_key;
obj->value = new_value;
refcount_set_release(obj->ref, 1);
add(collection, new_key, obj);
if (!refcount_inc_not_zero(&obj->ref))
return;
use(obj->value); /* USING WRONG OBJECT */refcount_set_release() is required to prevent the following reordering when refcount_set() is used instead:
consumer:
obj = lookup(collection, key);
producer:
remove(collection, obj->key);
if (!refcount_dec_and_test(&obj->ref))
return;
obj->key = KEY_INVALID;
free(obj);
obj = malloc(); /* obj is reused */
obj->key = new_key; /* new_key == old_key */
refcount_set(obj->ref, 1);
if (!refcount_inc_not_zero_acquire(&obj->ref))
return;
if (READ_ONCE(obj->key) != key) { /* pass since new_key == old_key */
put_ref(obj);
return;
}
use(obj->value); /* USING STALE obj->value */
obj->value = new_value; /* reordered store */
add(collection, key, obj);--- Documentation/RCU/whatisRCU.rst | 10 ++ Documentation/core-api/refcount-vs-atomic.rst | 37 +++++- include/linux/refcount.h | 106 ++++++++++++++++++ include/linux/slab.h | 9 ++ 4 files changed, 156 insertions(+), 6 deletions(-)
Documentation/RCU/whatisRCU.rst +10 -0
@@ -971,6 +971,16 @@ unfortunately any spinlock in a ``SLAB_TYPESAFE_BY_RCU`` object must be initialized after each and every call to kmem_cache_alloc(), which renders reference-free spinlock acquisition completely unsafe. Therefore, when using ``SLAB_TYPESAFE_BY_RCU``, make proper use of a reference counter. +If using refcount_t, the specialized refcount_{add|inc}_not_zero_acquire() +and refcount_set_release() APIs should be used to ensure correct operation +ordering when verifying object identity and when initializing newly +allocated objects. Acquire fence in refcount_{add|inc}_not_zero_acquire() +ensures that identity checks happen *after* reference count is taken. +refcount_set_release() should be called after a newly allocated object is +fully initialized and release fence ensures that new values are visible +*before* refcount can be successfully taken by other users. Once +refcount_set_release() is called, the object should be considered visible +by other tasks. (Those willing to initialize their locks in a kmem_cache constructor may also use locking, including cache-friendly sequence locking.)
Documentation/core-api/refcount-vs-atomic.rst +31 -6
@@ -86,7 +86,19 @@ Memory ordering guarantee changes: * none (both fully unordered) -case 2) - increment-based ops that return no value +case 2) - non-"Read/Modify/Write" (RMW) ops with release ordering +------------------------------------------- + +Function changes: + + * atomic_set_release() --> refcount_set_release() + +Memory ordering guarantee changes: + + * none (both provide RELEASE ordering) + + +case 3) - increment-based ops that return no value -------------------------------------------------- Function changes:
@@ -98,7 +110,7 @@ Memory ordering guarantee changes: * none (both fully unordered) -case 3) - decrement-based RMW ops that return no value +case 4) - decrement-based RMW ops that return no value ------------------------------------------------------ Function changes:
@@ -110,7 +122,7 @@ Memory ordering guarantee changes: * fully unordered --> RELEASE ordering -case 4) - increment-based RMW ops that return a value +case 5) - increment-based RMW ops that return a value ----------------------------------------------------- Function changes:
@@ -126,7 +138,20 @@ Memory ordering guarantees changes: result of obtaining pointer to the object! -case 5) - generic dec/sub decrement-based RMW ops that return a value +case 6) - increment-based RMW ops with acquire ordering that return a value +----------------------------------------------------- + +Function changes: + + * atomic_inc_not_zero() --> refcount_inc_not_zero_acquire() + * no atomic counterpart --> refcount_add_not_zero_acquire() + +Memory ordering guarantees changes: + + * fully ordered --> ACQUIRE ordering on success + + +case 7) - generic dec/sub decrement-based RMW ops that return a value --------------------------------------------------------------------- Function changes:
@@ -139,7 +164,7 @@ Memory ordering guarantees changes: * fully ordered --> RELEASE ordering + ACQUIRE ordering on success -case 6) other decrement-based RMW ops that return a value +case 8) other decrement-based RMW ops that return a value --------------------------------------------------------- Function changes:
@@ -154,7 +179,7 @@ Memory ordering guarantees changes: .. note:: atomic_add_unless() only provides full order on success. -case 7) - lock-based RMW +case 9) - lock-based RMW ------------------------ Function changes:
include/linux/refcount.h +106 -0
130 lines not on this page · raw message
include/linux/slab.h +9 -0
@@ -136,6 +136,15 @@ enum _slab_flag_bits {* rcu_read_lock before reading the address, then rcu_read_unlock after * taking the spinlock within the structure expected at that address. * +* Note that object identity check has to be done *after* acquiring a +* reference, therefore user has to ensure proper ordering for loads. +* Similarly, when initializing objects allocated with SLAB_TYPESAFE_BY_RCU, +* the newly allocated object has to be fully initialized *before* its +* refcount gets initialized and proper ordering for stores is required. +* refcount_{add|inc}_not_zero_acquire() and refcount_set_release() are +* designed with the proper fences required for reference counting objects +* allocated with SLAB_TYPESAFE_BY_RCU. +* * Note that it is not possible to acquire a lock within a structure * allocated with SLAB_TYPESAFE_BY_RCU without first acquiring a reference * as described above. The reason is that SLAB_TYPESAFE_BY_RCU pages
base-commit: 92514ef226f511f2ca1fb1b8752966097518edc0
Signature · 1 line
2.48.1.362.g079036d154-goog
Re: [PATCH 1/1] refcount: provide ops for cases when object's memory can be reused
Quoted 100 lines · Suren Baghdasaryan
For speculative lookups where a successful
inc_not_zero()pins the object, but where we still need to double check if the object acquired is indeed the one we set out to acquire (identity check), needs this validation to happen *after* the increment.
Similarly, when a new object is initialized and its memory might have been previously occupied by another object, all stores to initialize the object should happen *before* refcount initialization.Notably
SLAB_TYPESAFE_BY_RCUis one such an example when this ordering is required for reference counting.Add refcount_{add|inc}_not_zero_acquire() to guarantee the proper ordering between acquiring a reference count on an object and performing the identity check for that object.
Addrefcount_set_release()to guarantee proper ordering between stores initializing object attributes and the store initializing the refcount.refcount_set_release()should be done after all other object attributes are initialized. Oncerefcount_set_release()is called, the object should be considered visible to other tasks even if it was not yet added into an object collection normally used to discover it. This is because other tasks might have discovered the object previously occupying the same memory and after memory reuse they can succeed in taking refcount for the new object and start using it.Object reuse example to consider:
consumer:
obj = lookup(collection, key); if (!refcount_inc_not_zero_acquire(&obj->ref)) return; if (READ_ONCE(obj->key) != key) { /* identity check */ put_ref(obj); return; } use(obj->value); producer: remove(collection, obj->key); if (!refcount_dec_and_test(&obj->ref)) return; obj->key = KEY_INVALID; free(obj); obj = malloc(); /* obj is reused */ obj->key = new_key; obj->value = new_value; refcount_set_release(obj->ref, 1); add(collection, new_key, obj);refcount_{add|inc}_not_zero_acquire() is required to prevent the following reordering when
refcount_inc_not_zero()is used instead:consumer:
obj = lookup(collection, key); if (READ_ONCE(obj->key) != key) { /* reordered identity check */ put_ref(obj); return; } producer: remove(collection, obj->key); if (!refcount_dec_and_test(&obj->ref)) return; obj->key = KEY_INVALID; free(obj); obj = malloc(); /* obj is reused */ obj->key = new_key; obj->value = new_value; refcount_set_release(obj->ref, 1); add(collection, new_key, obj); if (!refcount_inc_not_zero(&obj->ref)) return; use(obj->value); /* USING WRONG OBJECT */
refcount_set_release()is required to prevent the following reordering whenrefcount_set()is used instead:consumer:
obj = lookup(collection, key); producer: remove(collection, obj->key); if (!refcount_dec_and_test(&obj->ref)) return; obj->key = KEY_INVALID; free(obj); obj = malloc(); /* obj is reused */ obj->key = new_key; /* new_key == old_key */ refcount_set(obj->ref, 1); if (!refcount_inc_not_zero_acquire(&obj->ref)) return; if (READ_ONCE(obj->key) != key) { /* pass since new_key == old_key */ put_ref(obj); return; } use(obj->value); /* USING STALE obj->value */ obj->value = new_value; /* reordered store */ add(collection, key, obj);
Quoted 4 lines
Signed-off-by Suren Baghdasaryan <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Will Deacon <[email protected]>Cc Paul E. McKenney <[email protected]>
Suren Baghdasaryan
maintainerRe: [PATCH] refcount: Strengthen inc_not_zero()
In reply to Suren BaghdasaryanQuoted 165 lines · Suren Baghdasaryan
Quoted 149 lines · Suren Baghdasaryan
Quoted 70 lines · Will Deacon
Quoted 53 lines · Will Deacon
Quoted 6 lines · Peter Zijlstra
Quoted 4 lines · Peter Zijlstra
Notably, it means
refcount_tis entirely unsuitable for anythingSLAB_TYPESAFE_BY_RCU, since they all will need secondary validation conditions after the refcount succeeds.Quoted 6 lines
Quoted 1 line
And this is probably fine, but let me ponder this all a little more.
Even though
SLAB_TYPESAFE_BY_RCUis relatively rare, I think we'd better fix this, these things are hard enough as they are.Will, others, wdyt?
We should also update the Documentation (atomic_t.txt and
refcount-vs-atomic.rst) if we strengthen this.Quoted 36 lines
---
Subject: refcount: Strengtheninc_not_zero()For speculative lookups where a successful
inc_not_zero()pins the object, but where we still need to double check if the object acquired is indeed the one we set out to aquire, needs this validation to happen *after* the increment.Notably
SLAB_TYPESAFE_BY_RCUis one such an example.Signed-off-by Peter Zijlstra (Intel) <[email protected]>--- include/linux/refcount.h | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)include/linux/refcount.h +4
-3@@ -69,9 +69,10 @@* its the lock acquire, for RCU/lockless data structures its the dependent * load. * -* Do note that inc_not_zero() provides a control dependency which will order -* future stores against the inc, this ensures we'll never modify the object -* if we did not in fact acquire a reference. +* Do note that inc_not_zero() does provide acquire order, which will order +* future load and stores against the inc, this ensures all subsequent accesses +* are from this object and not anything previously occupying this memory -- +* consider SLAB_TYPESAFE_BY_RCU. * * The decrements will provide release order, such that all the prior loads and * stores will be issued before, it also provides a control dependency, which @@ -144,7 +145,7 @@ bool __refcount_add_not_zero(int i, refcount_t *r, int *oldp) do { if (!old)Quoted 8 lines
include/linux/refcount.h bool __refcount_add_not_zero(int i, refcount_t *r, int *oldp) · line 148
Quoted 3 lines
include/linux/refcount.h +1
-1break ; - }while (!atomic_try_cmpxchg_relaxed (&r->refs, &old, old + i)); + }while (!atomic_try_cmpxchg_acquire (&r->refs, &old, old + i));Hmm, do the later memory accesses need to be ordered against the store part of the increment or just the read? If it's the former, then I don't think that
_acquireis sufficient -- accesses can still get in-between the read and write parts of the RmW.I dug some more into this at the end of last week. For the
SLAB_TYPESAFE_BY_RCUwhere we're racinginc_not_zero()withdec_and_test(), then I think using_acquire()above is correct as the later references can only move up into the critical section in the case that we successfully obtained a reference.Quoted 6 lines
However, if we're going to make the barriers implicit in the refcount operations here then I think we also need to do something on the producer side for when the object is re-initialised after being destroyed and allocated again. I think that would necessitate release ordering for
refcount_set()so that whatever allows the consumer to validate the object (e.g. sequence number) is published *before* the refcount.Thanks Will!
I would like to expand on your answer to provide an example of the race that would happen without release ordering in the producer. To save reader's time I provide a simplified flow and reasoning first. More detailed code of what I'm considering a typicalSLAB_TYPESAFE_BY_RCUrefcounting example is added at the end of my reply (Addendum).
Simplified flow looks like this:consumer:
obj = lookup(collection, key); if (!refcount_inc_not_zero(&obj->ref)) return; smp_rmb(); /* Peter's new acquire fence */ if (READ_ONCE(obj->key) != key) { put_ref(obj); return; } use(obj->value);producer:
old_key = obj->key; remove(collection, old_key); if (!refcount_dec_and_test(&obj->ref)) return; obj->key = KEY_INVALID; free(objj); ... obj = malloc(); /* obj is reused */ obj->key = new_key; obj->value = new_value; smp_wmb(); /* Will's proposed release fence */ refcount_set(obj->ref, 1); insert(collection, key, obj);Let's consider a case when
new_key==old_key. Will call both of them "key". Without WIll's proposed fence the following reordering is possible:consumer:
obj = lookup(collection, key); producer: key = obj->key remove(collection, key); if (!refcount_dec_and_test(&obj->ref)) return; obj->key = KEY_INVALID; free(objj); obj = malloc(); /* obj is reused */ refcount_set(obj->ref, 1); obj->key = key; /* same key */ if (!refcount_inc_not_zero(&obj->ref)) return; smp_rmb(); if (READ_ONCE(obj->key) != key) { put_ref(obj); return; } use(obj->value); obj->value = new_value; /* reordered store */ add(collection, key, obj);So, the consumer finds the old object, successfully takes a refcount and validates the key. It succeeds because the object is allocated and has the same key, which is fine. However it proceeds to use stale
obj->value. Will's proposed release ordering would prevent that.Quoted 13 lines
The example in https://elixir.bootlin.com/linux/v6.12.6/source/include/linux/slab.h#L102
omits all these ordering issues forSLAB_TYPESAFE_BY_RCU.
I think it would be better to introduce two new functions:refcount_add_not_zero_acquire()andrefcount_set_release(), clearly document that they should be used when a freed object can be recycled
and reused, like inSLAB_TYPESAFE_BY_RCUcase.refcount_set_release()should also clarify that once it's called, the object can be accessed
by consumers even if it was not added yet into the collection used for object lookup (like in the example above).SLAB_TYPESAFE_BY_RCU
comment at https://elixir.bootlin.com/linux/v6.12.6/source/include/linux/slab.h#L102
then can explicitly use these new functions in the example code,
further clarifying their purpose and proper use.
WDYT?
Quoted 6 lines
Hi Peter,
Should I take a stab at preparing a patch to add the two new refcounting functions suggested above with updates to the documentation and comments?
If you disagree with that or need more time to decide then I'll wait. Please let me know.
Not sure if "--in-reply-to" worked but I just posted a patch adding new refcounting APIs for SLAB_TYPESAFE_BY_RCU here:
https://lore.kernel.org/all/[email protected]/
Since Peter seems to be busy I discussed these ordering requirements for SLAB_TYPESAFE_BY_RCU with Paul McKenney and he was leaning towards having separate functions with the additional fences for this case. That's what I provided in my patch.
Another possible option is to add acquire ordering in the __refcount_add_not_zero() as Peter suggested and add refcount_set_release() function.
Thanks,
Suren.
Quoted 61 lines
Thanks,
Suren.Quoted 57 lines
ADDENDUM.
Detailed code for typical use of refcounting withSLAB_TYPESAFE_BY_RCU:struct object { refcount_t ref; u64 key; u64 value; };void
init(struct object *obj, u64 key, u64 value){ obj->key = key; obj->value = value; smp_wmb(); /* Will's proposed release fence */ refcount_set(obj->ref, 1); }bool
get_ref(struct object *obj, u64 key){ if (!refcount_inc_not_zero(&obj->ref)) return false; smp_rmb(); /* Peter's new acquire fence */ if (READ_ONCE(obj->key) != key) { put_ref(obj); return false; } return true; }void
put_ref(struct object *obj){ if (!refcount_dec_and_test(&obj->ref)) return; obj->key = KEY_INVALID; free(obj); }consumer:
obj = lookup(collection, key); if (!get_ref(obj, key) return; use(obj->value);producer:
remove(collection, old_obj->key); put_ref(old_obj); new_obj = malloc(); init(new_obj, new_key, new_value); insert(collection, new_key, new_obj);With
SLAB_TYPESAFE_BY_RCUold_objin the producer can be reused and be equal tonew_obj.Quoted 2 lines
Will
Suren Baghdasaryan
maintainerRe: [PATCH] refcount: Strengthen inc_not_zero()
Quoted 174 lines · Suren Baghdasaryan
Quoted 165 lines · Suren Baghdasaryan
Quoted 149 lines · Suren Baghdasaryan
Quoted 70 lines · Will Deacon
Quoted 53 lines · Will Deacon
Quoted 6 lines · Peter Zijlstra
Quoted 4 lines · Peter Zijlstra
Notably, it means
refcount_tis entirely unsuitable for anythingSLAB_TYPESAFE_BY_RCU, since they all will need secondary validation conditions after the refcount succeeds.Quoted 6 lines
Quoted 1 line
And this is probably fine, but let me ponder this all a little more.
Even though
SLAB_TYPESAFE_BY_RCUis relatively rare, I think we'd better fix this, these things are hard enough as they are.Will, others, wdyt?
We should also update the Documentation (atomic_t.txt and
refcount-vs-atomic.rst) if we strengthen this.Quoted 36 lines
---
Subject: refcount: Strengtheninc_not_zero()For speculative lookups where a successful
inc_not_zero()pins the object, but where we still need to double check if the object acquired is indeed the one we set out to aquire, needs this validation to happen *after* the increment.Notably
SLAB_TYPESAFE_BY_RCUis one such an example.Signed-off-by Peter Zijlstra (Intel) <[email protected]>--- include/linux/refcount.h | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)include/linux/refcount.h +4
-3@@ -69,9 +69,10 @@* its the lock acquire, for RCU/lockless data structures its the dependent * load. * -* Do note that inc_not_zero() provides a control dependency which will order -* future stores against the inc, this ensures we'll never modify the object -* if we did not in fact acquire a reference. +* Do note that inc_not_zero() does provide acquire order, which will order +* future load and stores against the inc, this ensures all subsequent accesses +* are from this object and not anything previously occupying this memory -- +* consider SLAB_TYPESAFE_BY_RCU. * * The decrements will provide release order, such that all the prior loads and * stores will be issued before, it also provides a control dependency, which @@ -144,7 +145,7 @@ bool __refcount_add_not_zero(int i, refcount_t *r, int *oldp) do { if (!old)Quoted 8 lines
include/linux/refcount.h bool __refcount_add_not_zero(int i, refcount_t *r, int *oldp) · line 148
Quoted 3 lines
include/linux/refcount.h +1
-1break ; - }while (!atomic_try_cmpxchg_relaxed (&r->refs, &old, old + i)); + }while (!atomic_try_cmpxchg_acquire (&r->refs, &old, old + i));Hmm, do the later memory accesses need to be ordered against the store part of the increment or just the read? If it's the former, then I don't think that
_acquireis sufficient -- accesses can still get in-between the read and write parts of the RmW.I dug some more into this at the end of last week. For the
SLAB_TYPESAFE_BY_RCUwhere we're racinginc_not_zero()withdec_and_test(), then I think using_acquire()above is correct as the later references can only move up into the critical section in the case that we successfully obtained a reference.Quoted 6 lines
However, if we're going to make the barriers implicit in the refcount operations here then I think we also need to do something on the producer side for when the object is re-initialised after being destroyed and allocated again. I think that would necessitate release ordering for
refcount_set()so that whatever allows the consumer to validate the object (e.g. sequence number) is published *before* the refcount.Thanks Will!
I would like to expand on your answer to provide an example of the race that would happen without release ordering in the producer. To save reader's time I provide a simplified flow and reasoning first. More detailed code of what I'm considering a typicalSLAB_TYPESAFE_BY_RCUrefcounting example is added at the end of my reply (Addendum).
Simplified flow looks like this:consumer:
obj = lookup(collection, key); if (!refcount_inc_not_zero(&obj->ref)) return; smp_rmb(); /* Peter's new acquire fence */ if (READ_ONCE(obj->key) != key) { put_ref(obj); return; } use(obj->value);producer:
old_key = obj->key; remove(collection, old_key); if (!refcount_dec_and_test(&obj->ref)) return; obj->key = KEY_INVALID; free(objj); ... obj = malloc(); /* obj is reused */ obj->key = new_key; obj->value = new_value; smp_wmb(); /* Will's proposed release fence */ refcount_set(obj->ref, 1); insert(collection, key, obj);Let's consider a case when
new_key==old_key. Will call both of them "key". Without WIll's proposed fence the following reordering is possible:consumer:
obj = lookup(collection, key); producer: key = obj->key remove(collection, key); if (!refcount_dec_and_test(&obj->ref)) return; obj->key = KEY_INVALID; free(objj); obj = malloc(); /* obj is reused */ refcount_set(obj->ref, 1); obj->key = key; /* same key */ if (!refcount_inc_not_zero(&obj->ref)) return; smp_rmb(); if (READ_ONCE(obj->key) != key) { put_ref(obj); return; } use(obj->value); obj->value = new_value; /* reordered store */ add(collection, key, obj);So, the consumer finds the old object, successfully takes a refcount and validates the key. It succeeds because the object is allocated and has the same key, which is fine. However it proceeds to use stale
obj->value. Will's proposed release ordering would prevent that.Quoted 13 lines
The example in https://elixir.bootlin.com/linux/v6.12.6/source/include/linux/slab.h#L102
omits all these ordering issues forSLAB_TYPESAFE_BY_RCU.
I think it would be better to introduce two new functions:refcount_add_not_zero_acquire()andrefcount_set_release(), clearly document that they should be used when a freed object can be recycled
and reused, like inSLAB_TYPESAFE_BY_RCUcase.refcount_set_release()should also clarify that once it's called, the object can be accessed
by consumers even if it was not added yet into the collection used for object lookup (like in the example above).SLAB_TYPESAFE_BY_RCU
comment at https://elixir.bootlin.com/linux/v6.12.6/source/include/linux/slab.h#L102
then can explicitly use these new functions in the example code,
further clarifying their purpose and proper use.
WDYT?Quoted 6 lines
Hi Peter,
Should I take a stab at preparing a patch to add the two new refcounting functions suggested above with updates to the documentation and comments?
If you disagree with that or need more time to decide then I'll wait. Please let me know.
Quoted 3 lines
Not sure if "--in-reply-to" worked but I just posted a patch adding new refcounting APIs for
SLAB_TYPESAFE_BY_RCUhere:
https://lore.kernel.org/all/[email protected]/
Since I did not get any replies other than Vlastimil's Ack on the above patch, I went ahead and posted v10 of my patchset [1] and included the patch above in it [2]. Feedback is highly appreciated!
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
Quoted 72 lines
Since Peter seems to be busy I discussed these ordering requirements for
SLAB_TYPESAFE_BY_RCUwith Paul McKenney and he was leaning towards having separate functions with the additional fences for this case. That's what I provided in my patch.
Another possible option is to add acquire ordering in the__refcount_add_not_zero()as Peter suggested and addrefcount_set_release()function.
Thanks,
Suren.Quoted 61 lines
Thanks,
Suren.Quoted 57 lines
ADDENDUM.
Detailed code for typical use of refcounting withSLAB_TYPESAFE_BY_RCU:struct object { refcount_t ref; u64 key; u64 value; };void
init(struct object *obj, u64 key, u64 value){ obj->key = key; obj->value = value; smp_wmb(); /* Will's proposed release fence */ refcount_set(obj->ref, 1); }bool
get_ref(struct object *obj, u64 key){ if (!refcount_inc_not_zero(&obj->ref)) return false; smp_rmb(); /* Peter's new acquire fence */ if (READ_ONCE(obj->key) != key) { put_ref(obj); return false; } return true; }void
put_ref(struct object *obj){ if (!refcount_dec_and_test(&obj->ref)) return; obj->key = KEY_INVALID; free(obj); }consumer:
obj = lookup(collection, key); if (!get_ref(obj, key) return; use(obj->value);producer:
remove(collection, old_obj->key); put_ref(old_obj); new_obj = malloc(); init(new_obj, new_key, new_value); insert(collection, new_key, new_obj);With
SLAB_TYPESAFE_BY_RCUold_objin the producer can be reused and be equal tonew_obj.Quoted 2 lines
Will
Quoted 249 lines · Suren Baghdasaryan
Quoted 165 lines · Suren Baghdasaryan
Quoted 149 lines · Suren Baghdasaryan
Quoted 70 lines · Will Deacon
Quoted 53 lines · Will Deacon
Quoted 6 lines · Peter Zijlstra
Quoted 4 lines · Peter Zijlstra
Notably, it means
refcount_tis entirely unsuitable for anythingSLAB_TYPESAFE_BY_RCU, since they all will need secondary validation conditions after the refcount succeeds.Quoted 6 lines
Quoted 1 line
And this is probably fine, but let me ponder this all a little more.
Even though
SLAB_TYPESAFE_BY_RCUis relatively rare, I think we'd better fix this, these things are hard enough as they are.Will, others, wdyt?
We should also update the Documentation (atomic_t.txt and
refcount-vs-atomic.rst) if we strengthen this.Quoted 36 lines
---
Subject: refcount: Strengtheninc_not_zero()For speculative lookups where a successful
inc_not_zero()pins the object, but where we still need to double check if the object acquired is indeed the one we set out to aquire, needs this validation to happen *after* the increment.Notably
SLAB_TYPESAFE_BY_RCUis one such an example.Signed-off-by Peter Zijlstra (Intel) <[email protected]>--- include/linux/refcount.h | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)include/linux/refcount.h +4
-3@@ -69,9 +69,10 @@* its the lock acquire, for RCU/lockless data structures its the dependent * load. * -* Do note that inc_not_zero() provides a control dependency which will order -* future stores against the inc, this ensures we'll never modify the object -* if we did not in fact acquire a reference. +* Do note that inc_not_zero() does provide acquire order, which will order +* future load and stores against the inc, this ensures all subsequent accesses +* are from this object and not anything previously occupying this memory -- +* consider SLAB_TYPESAFE_BY_RCU. * * The decrements will provide release order, such that all the prior loads and * stores will be issued before, it also provides a control dependency, which @@ -144,7 +145,7 @@ bool __refcount_add_not_zero(int i, refcount_t *r, int *oldp) do { if (!old)Quoted 8 lines
include/linux/refcount.h bool __refcount_add_not_zero(int i, refcount_t *r, int *oldp) · line 148
Quoted 3 lines
include/linux/refcount.h +1
-1break ; - }while (!atomic_try_cmpxchg_relaxed (&r->refs, &old, old + i)); + }while (!atomic_try_cmpxchg_acquire (&r->refs, &old, old + i));Hmm, do the later memory accesses need to be ordered against the store part of the increment or just the read? If it's the former, then I don't think that
_acquireis sufficient -- accesses can still get in-between the read and write parts of the RmW.I dug some more into this at the end of last week. For the
SLAB_TYPESAFE_BY_RCUwhere we're racinginc_not_zero()withdec_and_test(), then I think using_acquire()above is correct as the later references can only move up into the critical section in the case that we successfully obtained a reference.Quoted 6 lines
However, if we're going to make the barriers implicit in the refcount operations here then I think we also need to do something on the producer side for when the object is re-initialised after being destroyed and allocated again. I think that would necessitate release ordering for
refcount_set()so that whatever allows the consumer to validate the object (e.g. sequence number) is published *before* the refcount.Thanks Will!
I would like to expand on your answer to provide an example of the race that would happen without release ordering in the producer. To save reader's time I provide a simplified flow and reasoning first. More detailed code of what I'm considering a typicalSLAB_TYPESAFE_BY_RCUrefcounting example is added at the end of my reply (Addendum).
Simplified flow looks like this:consumer:
obj = lookup(collection, key); if (!refcount_inc_not_zero(&obj->ref)) return; smp_rmb(); /* Peter's new acquire fence */ if (READ_ONCE(obj->key) != key) { put_ref(obj); return; } use(obj->value);producer:
old_key = obj->key; remove(collection, old_key); if (!refcount_dec_and_test(&obj->ref)) return; obj->key = KEY_INVALID; free(objj); ... obj = malloc(); /* obj is reused */ obj->key = new_key; obj->value = new_value; smp_wmb(); /* Will's proposed release fence */ refcount_set(obj->ref, 1); insert(collection, key, obj);Let's consider a case when
new_key==old_key. Will call both of them "key". Without WIll's proposed fence the following reordering is possible:consumer:
obj = lookup(collection, key); producer: key = obj->key remove(collection, key); if (!refcount_dec_and_test(&obj->ref)) return; obj->key = KEY_INVALID; free(objj); obj = malloc(); /* obj is reused */ refcount_set(obj->ref, 1); obj->key = key; /* same key */ if (!refcount_inc_not_zero(&obj->ref)) return; smp_rmb(); if (READ_ONCE(obj->key) != key) { put_ref(obj); return; } use(obj->value); obj->value = new_value; /* reordered store */ add(collection, key, obj);So, the consumer finds the old object, successfully takes a refcount and validates the key. It succeeds because the object is allocated and has the same key, which is fine. However it proceeds to use stale
obj->value. Will's proposed release ordering would prevent that.Quoted 13 lines
The example in https://elixir.bootlin.com/linux/v6.12.6/source/include/linux/slab.h#L102
omits all these ordering issues forSLAB_TYPESAFE_BY_RCU.
I think it would be better to introduce two new functions:refcount_add_not_zero_acquire()andrefcount_set_release(), clearly document that they should be used when a freed object can be recycled
and reused, like inSLAB_TYPESAFE_BY_RCUcase.refcount_set_release()should also clarify that once it's called, the object can be accessed
by consumers even if it was not added yet into the collection used for object lookup (like in the example above).SLAB_TYPESAFE_BY_RCU
comment at https://elixir.bootlin.com/linux/v6.12.6/source/include/linux/slab.h#L102
then can explicitly use these new functions in the example code,
further clarifying their purpose and proper use.
WDYT?Quoted 6 lines
Hi Peter,
Should I take a stab at preparing a patch to add the two new refcounting functions suggested above with updates to the documentation and comments?
If you disagree with that or need more time to decide then I'll wait. Please let me know.Not sure if "--in-reply-to" worked but I just posted a patch adding new refcounting APIs for
SLAB_TYPESAFE_BY_RCUhere:
https://lore.kernel.org/all/[email protected]/
Since Peter seems to be busy I discussed these ordering requirements forSLAB_TYPESAFE_BY_RCUwith Paul McKenney and he was leaning towards having separate functions with the additional fences for this case. That's what I provided in my patch.
Another possible option is to add acquire ordering in the__refcount_add_not_zero()as Peter suggested and addrefcount_set_release()function.
Thanks,
Suren.Quoted 61 lines
Thanks,
Suren.Quoted 57 lines
ADDENDUM.
Detailed code for typical use of refcounting withSLAB_TYPESAFE_BY_RCU:struct object { refcount_t ref; u64 key; u64 value; };void
init(struct object *obj, u64 key, u64 value){ obj->key = key; obj->value = value; smp_wmb(); /* Will's proposed release fence */ refcount_set(obj->ref, 1); }bool
get_ref(struct object *obj, u64 key){ if (!refcount_inc_not_zero(&obj->ref)) return false; smp_rmb(); /* Peter's new acquire fence */ if (READ_ONCE(obj->key) != key) { put_ref(obj); return false; } return true; }void
put_ref(struct object *obj){ if (!refcount_dec_and_test(&obj->ref)) return; obj->key = KEY_INVALID; free(obj); }consumer:
obj = lookup(collection, key); if (!get_ref(obj, key) return; use(obj->value);producer:
remove(collection, old_obj->key); put_ref(old_obj); new_obj = malloc(); init(new_obj, new_key, new_value); insert(collection, new_key, new_obj);With
SLAB_TYPESAFE_BY_RCUold_objin the producer can be reused and be equal tonew_obj.Quoted 2 lines
Will
Re: [PATCH] refcount: Strengthen inc_not_zero()
In reply to Peter ZijlstraQuoted 8 lines · Peter Zijlstra
Subject: refcount: Strengthen
inc_not_zero()For speculative lookups where a successful
inc_not_zero()pins the object, but where we still need to double check if the object acquired is indeed the one we set out to aquire, needs this validation to happen *after* the increment.Notably
SLAB_TYPESAFE_BY_RCUis one such an example.
While you're looking at inc_not_zero(), have you already thought about doing something like this? ie failing rather than saturating since all users of this already have to check for failure. It looks like two comparisons per call rather than three.
include/linux/refcount.h +1 -4
@@ -142,16 +142,13 @@ bool __refcount_add_not_zero(int i, refcount_t *r, int *oldp)int old =refcount_read (r);do { -if (!old) +if (old <=0 || old + i <0 )break ; }while (!atomic_try_cmpxchg_relaxed (&r->refs, &old, old + i));if (oldp) *oldp = old; -if (unlikely (old <0 || old + i <0 )) -refcount_warn_saturate (r,REFCOUNT_ADD_NOT_ZERO_OVF ); -return old; }
$ ./scripts/bloat-o-meter before.o after.o add/remove: 0/0 grow/shrink: 0/4 up/down: 0/-91 (-91) Function old new delta io_wq_for_each_worker.isra 162 158 -4 io_worker_handle_work 1403 1387 -16 io_wq_activate_free_worker 187 158 -29 io_queue_worker_create 367 325 -42 Total: Before=10068, After=9977, chg -0.90%
(that's io_uring/io-wq.o as an example user of refcount_inc_not_zero())
Re: [PATCH v9 11/17] mm: replace vm_lock and detached flag with a reference count
In reply to Suren BaghdasaryanQuoted 11 lines · Suren Baghdasaryan
rw_semaphoreis a sizable structure of 40 bytes and consumes considerable space for eachvm_area_struct. Howevervma_lockhas two important specifics which can be used to replacerw_semaphorewith a simpler structure:
1. Readers never wait. They try to take thevma_lockand fall back tommap_lockif that fails.
2. Only one writer at a time will ever try to write-lock avma_lockbecause writers first takemmap_lockin write mode.
Because of these requirements, fullrw_semaphorefunctionality is not needed and we can replacerw_semaphoreand thevma->detachedflag with a refcount (vm_refcnt).
This paragraph is merged into the above one in the commit log, which may not what you expect.
Just a format issue, not sure why they are not separated.
Quoted 12 lines
When vma is in detached state,
vm_refcntis 0 and only a call tovma_mark_attached()can take it out of this state. Note that unlike before, now we enforce bothvma_mark_attached()andvma_mark_detached()to be done only after vma has been write-locked.vma_mark_attached()changesvm_refcntto 1 to indicate that it has been attached to the vma tree. When a reader takes read lock, it incrementsvm_refcnt, unless the top usable bit ofvm_refcnt(0x40000000) is set, indicating presence of a writer. When writer takes write lock, it sets the top usable bit to indicate its presence. If there are readers, writer will wait using newly introducedmm->vma_writer_wait. Since all writers takemmap_lockin write mode first, there can be only one writer at a time. The last reader to release the lock will signal the writer to wake up.
Quoted 8 lines
refcount might overflow if there are many competing readers, in which case read-locking will fail. Readers are expected to handle such failures. In summary:
1. all readers increment thevm_refcnt;
2. writer sets top usable (writer) bit ofvm_refcnt;
3. readers cannot increment thevm_refcntif the writer bit is set;
4. in the presence of readers, writer must wait for thevm_refcntto drop to 1 (ignoring the writer bit), indicating an attached vma with no readers;
It waits until to (VMA_LOCK_OFFSET + 1) as indicates in __vma_start_write(), if I am right.
Quoted 7 lines
5.
vm_refcntoverflow is handled by the readers.While this
vm_lockreplacement does not yet result in a smallervm_area_struct(it stays at 256 bytes due to cacheline alignment), it allows for further size optimization by structure member regrouping to bring the size ofvm_area_structbelow 192 bytes.
Signature · 2 lines
Wei Yang Help you, Help me
Suren Baghdasaryan
maintainerRe: [PATCH v9 11/17] mm: replace vm_lock and detached flag with a reference count
Quoted 14 lines · Wei Yang
Quoted 11 lines · Suren Baghdasaryan
rw_semaphoreis a sizable structure of 40 bytes and consumes considerable space for eachvm_area_struct. Howevervma_lockhas two important specifics which can be used to replacerw_semaphorewith a simpler structure:
1. Readers never wait. They try to take thevma_lockand fall back tommap_lockif that fails.
2. Only one writer at a time will ever try to write-lock avma_lockbecause writers first takemmap_lockin write mode.
Because of these requirements, fullrw_semaphorefunctionality is not needed and we can replacerw_semaphoreand thevma->detachedflag with a refcount (vm_refcnt).
Quoted 4 lines
This paragraph is merged into the above one in the commit log, which may not what you expect.
Just a format issue, not sure why they are not separated.
I'll double-check the formatting. Thanks!
Quoted 22 lines
Quoted 13 lines
When vma is in detached state,
vm_refcntis 0 and only a call tovma_mark_attached()can take it out of this state. Note that unlike before, now we enforce bothvma_mark_attached()andvma_mark_detached()to be done only after vma has been write-locked.vma_mark_attached()changesvm_refcntto 1 to indicate that it has been attached to the vma tree. When a reader takes read lock, it incrementsvm_refcnt, unless the top usable bit ofvm_refcnt(0x40000000) is set, indicating presence of a writer. When writer takes write lock, it sets the top usable bit to indicate its presence. If there are readers, writer will wait using newly introducedmm->vma_writer_wait. Since all writers takemmap_lockin write mode first, there can be only one writer at a time. The last reader to release the lock will signal the writer to wake up.Quoted 8 lines
refcount might overflow if there are many competing readers, in which case read-locking will fail. Readers are expected to handle such failures. In summary:
1. all readers increment thevm_refcnt;
2. writer sets top usable (writer) bit ofvm_refcnt;
3. readers cannot increment thevm_refcntif the writer bit is set;
4. in the presence of readers, writer must wait for thevm_refcntto drop to 1 (ignoring the writer bit), indicating an attached vma with no readers;
Quoted 2 lines
It waits until to (
VMA_LOCK_OFFSET+ 1) as indicates in__vma_start_write(), if I am right.
Yeah, that's why I mentioned "(ignoring the writer bit)" but maybe that's too confusing. How about "drop to 1 (plus the VMA_LOCK_OFFSET writer bit)?
Quoted 11 lines
Quoted 8 lines
5.
vm_refcntoverflow is handled by the readers.While this
vm_lockreplacement does not yet result in a smallervm_area_struct(it stays at 256 bytes due to cacheline alignment), it allows for further size optimization by structure member regrouping to bring the size ofvm_area_structbelow 192 bytes.--
Wei Yang
Help you, Help me
Re: [PATCH v9 11/17] mm: replace vm_lock and detached flag with a reference count
Quoted 44 lines · Suren Baghdasaryan
Quoted 14 lines · Wei Yang
Quoted 11 lines · Suren Baghdasaryan
rw_semaphoreis a sizable structure of 40 bytes and consumes considerable space for eachvm_area_struct. Howevervma_lockhas two important specifics which can be used to replacerw_semaphorewith a simpler structure:
1. Readers never wait. They try to take thevma_lockand fall back tommap_lockif that fails.
2. Only one writer at a time will ever try to write-lock avma_lockbecause writers first takemmap_lockin write mode.
Because of these requirements, fullrw_semaphorefunctionality is not needed and we can replacerw_semaphoreand thevma->detachedflag with a refcount (vm_refcnt).Quoted 4 lines
This paragraph is merged into the above one in the commit log, which may not what you expect.
Just a format issue, not sure why they are not separated.
I'll double-check the formatting. Thanks!
Quoted 22 lines
Quoted 21 lines
When vma is in detached state,
vm_refcntis 0 and only a call tovma_mark_attached()can take it out of this state. Note that unlike before, now we enforce bothvma_mark_attached()andvma_mark_detached()to be done only after vma has been write-locked.vma_mark_attached()changesvm_refcntto 1 to indicate that it has been attached to the vma tree. When a reader takes read lock, it incrementsvm_refcnt, unless the top usable bit ofvm_refcnt(0x40000000) is set, indicating presence of a writer. When writer takes write lock, it sets the top usable bit to indicate its presence. If there are readers, writer will wait using newly introducedmm->vma_writer_wait. Since all writers takemmap_lockin write mode first, there can be only one writer at a time. The last reader to release the lock will signal the writer to wake up.
refcount might overflow if there are many competing readers, in which case read-locking will fail. Readers are expected to handle such failures. In summary:
1. all readers increment thevm_refcnt;
2. writer sets top usable (writer) bit ofvm_refcnt;
3. readers cannot increment thevm_refcntif the writer bit is set;
4. in the presence of readers, writer must wait for thevm_refcntto drop to 1 (ignoring the writer bit), indicating an attached vma with no readers;
Quoted 7 lines
Quoted 2 lines
It waits until to (
VMA_LOCK_OFFSET+ 1) as indicates in__vma_start_write(), if I am right.Yeah, that's why I mentioned "(ignoring the writer bit)" but maybe that's too confusing. How about "drop to 1 (plus the
VMA_LOCK_OFFSETwriter bit)?
Hmm.. hard to say. It is a little confusing, but I don't have a better one :-(
Quoted 11 lines
Quoted 11 lines
Quoted 8 lines
5.
vm_refcntoverflow is handled by the readers.While this
vm_lockreplacement does not yet result in a smallervm_area_struct(it stays at 256 bytes due to cacheline alignment), it allows for further size optimization by structure member regrouping to bring the size ofvm_area_structbelow 192 bytes.--
Wei Yang
Help you, Help me
Signature · 2 lines
Wei Yang Help you, Help me
Re: [PATCH v9 11/17] mm: replace vm_lock and detached flag with a reference count
In reply to Suren Baghdasaryaninclude/linux/mm.h static inline void vma_init(struct vm_area_struct *vma, struct mm_struct *mm) · line 952
Quoted 12 lines · Suren Baghdasaryan
static inline void
vma_end_read(struct vm_area_struct *vma){}include/linux/mm.h +1
-5@@ -908,12 +948,8 @@ static inline void vma_init(struct vm_area_struct *vma, struct mm_struct *mm) vma->vm_mm = mm; vma->vm_ops = &vma_dummy_vm_ops;INIT_LIST_HEAD (&vma->anon_vma_chain); -#ifdef CONFIG_PER_VMA_LOCK -/* vma is not locked, can't use vma_mark_detached() */ - vma->detached = true; -#endif vma_numab_state_init (vma); -vma_lock_init (vma); +vma_lock_init (vma, false);
vma_init(vma, mm)
memset(vma, 0, sizeof(*vma))
...
vma_lock_init(vma, false);
It looks the vm_refcnt must be reset.
BTW, I don't figure out why we want to skip the reset of vm_refcnt. Is this related to SLAB_TYPESAFE_BY_RCU?
Quoted 2 lines
(quoted diff) +0
-0}
Signature · 2 lines
Wei Yang Help you, Help me
Suren Baghdasaryan
maintainerRe: [PATCH v9 11/17] mm: replace vm_lock and detached flag with a reference count
Quoted 20 lines · Wei Yang
include/linux/mm.h static inline void vma_init(struct vm_area_struct *vma, struct mm_struct *mm) · line 952
Quoted 12 lines · Suren Baghdasaryan
static inline void
vma_end_read(struct vm_area_struct *vma){}include/linux/mm.h +1
-5@@ -908,12 +948,8 @@ static inline void vma_init(struct vm_area_struct *vma, struct mm_struct *mm) vma->vm_mm = mm; vma->vm_ops = &vma_dummy_vm_ops;INIT_LIST_HEAD (&vma->anon_vma_chain); -#ifdef CONFIG_PER_VMA_LOCK -/* vma is not locked, can't use vma_mark_detached() */ - vma->detached = true; -#endif vma_numab_state_init (vma); -vma_lock_init (vma); +vma_lock_init (vma, false);
vma_init(vma, mm)
memset(vma, 0, sizeof(*vma))
...
vma_lock_init(vma, false);
Quoted 4 lines
It looks the
vm_refcntmust be reset.BTW, I don't figure out why we want to skip the reset of
vm_refcnt. Is this related toSLAB_TYPESAFE_BY_RCU?
Earlier memset(vma, 0, sizeof(*vma)) already zeroes the entire structure, so vm_refcnt is already 0 and does not need to be reset again.
Quoted 7 lines
Review static inline void vma_init(struct vm_area_struct *vma, struct mm_struct *mm) · line 954
Quoted 3 lines
(quoted diff) +0
-0}--
Wei Yang
Help you, Help me
Re: [PATCH v9 11/17] mm: replace vm_lock and detached flag with a reference count
In reply to Suren BaghdasaryanOn Fri, Jan 10, 2025 at 08:25:58PM -0800, Suren Baghdasaryan wrote:
[...]
include/linux/mm.h static inline void vma_numab_state_free(struct vm_area_struct *vma) {} · line 721
Quoted 10 lines
include/linux/mm.h +9
-0+static inline bool is_vma_writer_only (int refcnt) +{ +/* +* With a writer and no readers, refcnt is VMA_LOCK_OFFSET if the vma +* is detached and (VMA_LOCK_OFFSET + 1) if it is attached. Waiting on +* a detached vma happens only in vma_mark_detached() and is a rare +* case, therefore most of the time there will be no unnecessary wakeup. +*/ +return refcnt &VMA_LOCK_OFFSET && refcnt <=VMA_LOCK_OFFSET +1 ;
It looks equivalent to
return (refcnt == VMA_LOCK_OFFSET) || (refcnt == VMA_LOCK_OFFSET + 1);
And its generated code looks a little simpler.
Quoted 16 lines
include/linux/mm.h +16
-0+} + +static inline void vma_refcount_put (struct vm_area_struct *vma) +{ +/* Use a copy of vm_mm in case vma is freed after we drop vm_refcnt */ +struct mm_struct *mm = vma->vm_mm; +int oldcnt; + +rwsem_release (&vma->vmlock_dep_map, _RET_IP_); +if (!__refcount_dec_and_test (&vma->vm_refcnt, &oldcnt)) { + +if (is_vma_writer_only (oldcnt -1 )) +rcuwait_wake_up (&mm->vma_writer_wait); + } +} +
Signature · 2 lines
Wei Yang Help you, Help me
Suren Baghdasaryan
maintainerRe: [PATCH v9 11/17] mm: replace vm_lock and detached flag with a reference count
Quoted 16 lines · Wei Yang
On Fri, Jan 10, 2025 at 08:25:58PM -0800, Suren Baghdasaryan wrote:
[...]include/linux/mm.h static inline void vma_numab_state_free(struct vm_area_struct *vma) {} · line 721
Quoted 10 lines
include/linux/mm.h +9
-0+static inline bool is_vma_writer_only (int refcnt) +{ +/* +* With a writer and no readers, refcnt is VMA_LOCK_OFFSET if the vma +* is detached and (VMA_LOCK_OFFSET + 1) if it is attached. Waiting on +* a detached vma happens only in vma_mark_detached() and is a rare +* case, therefore most of the time there will be no unnecessary wakeup. +*/ +return refcnt &VMA_LOCK_OFFSET && refcnt <=VMA_LOCK_OFFSET +1 ;It looks equivalent to
Quoted 3 lines
return (refcnt == VMA_LOCK_OFFSET) || (refcnt == VMA_LOCK_OFFSET + 1);And its generated code looks a little simpler.
Yeah, but I think the original version is a bit more descriptive, checking that (1) there is a writer and (2) there are no readers.
Quoted 21 lines
Quoted 7 lines
include/linux/mm.h +6
-0+} + +static inline void vma_refcount_put (struct vm_area_struct *vma) +{ +/* Use a copy of vm_mm in case vma is freed after we drop vm_refcnt */ +struct mm_struct *mm = vma->vm_mm;include/linux/mm.h static inline void vma_numab_state_free(struct vm_area_struct *vma) {} · line 737
Quoted 10 lines
include/linux/mm.h +10
-0+int oldcnt; + +rwsem_release (&vma->vmlock_dep_map, _RET_IP_); +if (!__refcount_dec_and_test (&vma->vm_refcnt, &oldcnt)) { + +if (is_vma_writer_only (oldcnt -1 )) +rcuwait_wake_up (&mm->vma_writer_wait); + } +} +--
Wei Yang
Help you, Help me
Re: [PATCH v9 11/17] mm: replace vm_lock and detached flag with a reference count
In reply to Suren BaghdasaryanQuoted 9 lines · Suren Baghdasaryan
mm/memory.c +0
-1@@ -6354,7 +6422,6 @@ struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm,struct vm_area_struct *vma;rcu_read_lock (); -retry: vma =mas_walk (&mas);if (!vma) goto inval;
mm/memory.c struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm, · line 6371
Quoted 10 lines
mm/memory.c +0
-7@@ -6362,13 +6429,6 @@ struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm,if (!vma_start_read (vma)) goto inval; -/* Check if the VMA got isolated after we found it */ -if (is_vma_detached (vma)) { -vma_end_read (vma); -count_vm_vma_lock_event (VMA_LOCK_MISS ); -/* The area was replaced with another one */ - goto retry; - }
We have a little behavior change here.
Originally, if we found an detached vma, we may retry. But now, we would go to the slow path directly.
Maybe we can compare the event VMA_LOCK_MISS and VMA_LOCK_ABORT to see the percentage of this case. If it shows this is a too rare case to impact performance, we can ignore it.
Also the event VMA_LOCK_MISS recording is removed, but the definition is there. We may record it in the vma_start_read() when oldcnt is 0.
BTW, the name of VMA_LOCK_SUCCESS confuse me a little. I thought it indicates lock_vma_under_rcu() successfully get a valid vma. But seems not. Sounds we don't have an overall success/failure statistic in vmstat.
Quoted 3 lines
mm/memory.c +0
-0/* * At this point, we have a stable reference to a VMA: The VMA is * locked and we know it hasn't already been isolated.
Signature · 2 lines
Wei Yang Help you, Help me
Suren Baghdasaryan
maintainerRe: [PATCH v9 11/17] mm: replace vm_lock and detached flag with a reference count
Quoted 24 lines · Wei Yang
Quoted 9 lines · Suren Baghdasaryan
mm/memory.c +0
-1@@ -6354,7 +6422,6 @@ struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm,struct vm_area_struct *vma;rcu_read_lock (); -retry: vma =mas_walk (&mas);if (!vma) goto inval;mm/memory.c struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm, · line 6371
Quoted 10 lines
mm/memory.c +0
-7@@ -6362,13 +6429,6 @@ struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm,if (!vma_start_read (vma)) goto inval; -/* Check if the VMA got isolated after we found it */ -if (is_vma_detached (vma)) { -vma_end_read (vma); -count_vm_vma_lock_event (VMA_LOCK_MISS ); -/* The area was replaced with another one */ - goto retry; - }We have a little behavior change here.
Quoted 2 lines
Originally, if we found an detached vma, we may retry. But now, we would go to the slow path directly.
Hmm. Good point. I think the easiest way to keep the same functionality is to make vma_start_read() return vm_area_struct* on success, NULL on locking failure and EAGAIN if vma was detached (vm_refcnt==0). Then the same retry with VMA_LOCK_MISS can be done in the case of EAGAIN.
Quoted 11 lines
Maybe we can compare the event
VMA_LOCK_MISSandVMA_LOCK_ABORTto see the percentage of this case. If it shows this is a too rare case to impact performance, we can ignore it.Also the event
VMA_LOCK_MISSrecording is removed, but the definition is there. We may record it in thevma_start_read()when oldcnt is 0.BTW, the name of
VMA_LOCK_SUCCESSconfuse me a little. I thought it indicateslock_vma_under_rcu()successfully get a valid vma. But seems not. Sounds we don't have an overall success/failure statistic in vmstat.
Are you referring to the fact that we do not increment VMA_LOCK_SUCCESS if we successfully locked a vma but have to retry the page fault (in which we increment VMA_LOCK_RETRY instead)?
Quoted 8 lines
mm/memory.c struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm, · line 6434
Quoted 4 lines
mm/memory.c +0
-0/* * At this point, we have a stable reference to a VMA: The VMA is * locked and we know it hasn't already been isolated. --
Wei Yang
Help you, Help me
Re: [PATCH v9 11/17] mm: replace vm_lock and detached flag with a reference count
Quoted 25 lines · Suren Baghdasaryan
Quoted 24 lines · Wei Yang
Quoted 9 lines · Suren Baghdasaryan
mm/memory.c +0
-1@@ -6354,7 +6422,6 @@ struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm,struct vm_area_struct *vma;rcu_read_lock (); -retry: vma =mas_walk (&mas);if (!vma) goto inval;mm/memory.c struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm, · line 6371
Quoted 10 lines
mm/memory.c +0
-7@@ -6362,13 +6429,6 @@ struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm,if (!vma_start_read (vma)) goto inval; -/* Check if the VMA got isolated after we found it */ -if (is_vma_detached (vma)) { -vma_end_read (vma); -count_vm_vma_lock_event (VMA_LOCK_MISS ); -/* The area was replaced with another one */ - goto retry; - }We have a little behavior change here.
Quoted 9 lines
Quoted 2 lines
Originally, if we found an detached vma, we may retry. But now, we would go to the slow path directly.
Hmm. Good point. I think the easiest way to keep the same functionality is to make
vma_start_read()return vm_area_struct* on success, NULL on locking failure and EAGAIN if vma was detached (vm_refcnt==0). Then the same retry withVMA_LOCK_MISScan be done in the case of EAGAIN.
Looks good to me.
Quoted 8 lines
Quoted 8 lines
Maybe we can compare the event
VMA_LOCK_MISSandVMA_LOCK_ABORTto see the percentage of this case. If it shows this is a too rare case to impact performance, we can ignore it.Also the event
VMA_LOCK_MISSrecording is removed, but the definition is there. We may record it in thevma_start_read()when oldcnt is 0.
Quoted 6 lines
Quoted 3 lines
BTW, the name of
VMA_LOCK_SUCCESSconfuse me a little. I thought it indicateslock_vma_under_rcu()successfully get a valid vma. But seems not. Sounds we don't have an overall success/failure statistic in vmstat.Are you referring to the fact that we do not increment
VMA_LOCK_SUCCESSif we successfully locked a vma but have to retry the
Something like this. I thought we would increase VMA_LOCK_SUCCESS on success.
Quoted 2 lines
page fault (in which we increment
VMA_LOCK_RETRYinstead)?
I don't follow this.
Quoted 8 lines
Quoted 8 lines
mm/memory.c struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm, · line 6434
Quoted 4 lines
mm/memory.c +0
-0/* * At this point, we have a stable reference to a VMA: The VMA is * locked and we know it hasn't already been isolated. --
Wei Yang
Help you, Help me
Signature · 2 lines
Wei Yang Help you, Help me
Suren Baghdasaryan
maintainerRe: [PATCH v9 11/17] mm: replace vm_lock and detached flag with a reference count
Quoted 54 lines · Wei Yang
Quoted 25 lines · Suren Baghdasaryan
Quoted 24 lines · Wei Yang
Quoted 9 lines · Suren Baghdasaryan
mm/memory.c +0
-1@@ -6354,7 +6422,6 @@ struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm,struct vm_area_struct *vma;rcu_read_lock (); -retry: vma =mas_walk (&mas);if (!vma) goto inval;mm/memory.c struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm, · line 6371
Quoted 10 lines
mm/memory.c +0
-7@@ -6362,13 +6429,6 @@ struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm,if (!vma_start_read (vma)) goto inval; -/* Check if the VMA got isolated after we found it */ -if (is_vma_detached (vma)) { -vma_end_read (vma); -count_vm_vma_lock_event (VMA_LOCK_MISS ); -/* The area was replaced with another one */ - goto retry; - }We have a little behavior change here.
Quoted 9 lines
Quoted 2 lines
Originally, if we found an detached vma, we may retry. But now, we would go to the slow path directly.
Hmm. Good point. I think the easiest way to keep the same functionality is to make
vma_start_read()return vm_area_struct* on success, NULL on locking failure and EAGAIN if vma was detached (vm_refcnt==0). Then the same retry withVMA_LOCK_MISScan be done in the case of EAGAIN.Looks good to me.
Quoted 8 lines
Quoted 8 lines
Maybe we can compare the event
VMA_LOCK_MISSandVMA_LOCK_ABORTto see the percentage of this case. If it shows this is a too rare case to impact performance, we can ignore it.Also the event
VMA_LOCK_MISSrecording is removed, but the definition is there. We may record it in thevma_start_read()when oldcnt is 0.Quoted 6 lines
Quoted 3 lines
BTW, the name of
VMA_LOCK_SUCCESSconfuse me a little. I thought it indicateslock_vma_under_rcu()successfully get a valid vma. But seems not. Sounds we don't have an overall success/failure statistic in vmstat.Are you referring to the fact that we do not increment
VMA_LOCK_SUCCESSif we successfully locked a vma but have to retry the
Quoted 6 lines
Something like this. I thought we would increase
VMA_LOCK_SUCCESSon success.Quoted 2 lines
page fault (in which we increment
VMA_LOCK_RETRYinstead)?I don't follow this.
Sorry, I meant to say "in which case we increment VMA_LOCK_RETRY instead". IOW, when we successfully lock the vma but have to retry the pagefault, we increment VMA_LOCK_RETRY without incrementing VMA_LOCK_SUCCESS.
Quoted 13 lines
Quoted 9 lines
Quoted 9 lines
mm/memory.c struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm, · line 6434
Quoted 5 lines
mm/memory.c +0
-0/* * At this point, we have a stable reference to a VMA: The VMA is * locked and we know it hasn't already been isolated. --
Wei Yang
Help you, Help me--
Wei Yang
Help you, Help me
Re: [PATCH v9 11/17] mm: replace vm_lock and detached flag with a reference count
Quoted 60 lines · Suren Baghdasaryan
Quoted 59 lines · Wei Yang
Quoted 25 lines · Suren Baghdasaryan
Quoted 24 lines · Wei Yang
Quoted 9 lines · Suren Baghdasaryan
mm/memory.c +0
-1@@ -6354,7 +6422,6 @@ struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm,struct vm_area_struct *vma;rcu_read_lock (); -retry: vma =mas_walk (&mas);if (!vma) goto inval;mm/memory.c struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm, · line 6371
Quoted 10 lines
mm/memory.c +0
-7@@ -6362,13 +6429,6 @@ struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm,if (!vma_start_read (vma)) goto inval; -/* Check if the VMA got isolated after we found it */ -if (is_vma_detached (vma)) { -vma_end_read (vma); -count_vm_vma_lock_event (VMA_LOCK_MISS ); -/* The area was replaced with another one */ - goto retry; - }We have a little behavior change here.
Quoted 9 lines
Quoted 2 lines
Originally, if we found an detached vma, we may retry. But now, we would go to the slow path directly.
Hmm. Good point. I think the easiest way to keep the same functionality is to make
vma_start_read()return vm_area_struct* on success, NULL on locking failure and EAGAIN if vma was detached (vm_refcnt==0). Then the same retry withVMA_LOCK_MISScan be done in the case of EAGAIN.Looks good to me.
Quoted 8 lines
Quoted 8 lines
Maybe we can compare the event
VMA_LOCK_MISSandVMA_LOCK_ABORTto see the percentage of this case. If it shows this is a too rare case to impact performance, we can ignore it.Also the event
VMA_LOCK_MISSrecording is removed, but the definition is there. We may record it in thevma_start_read()when oldcnt is 0.Quoted 6 lines
Quoted 3 lines
BTW, the name of
VMA_LOCK_SUCCESSconfuse me a little. I thought it indicateslock_vma_under_rcu()successfully get a valid vma. But seems not. Sounds we don't have an overall success/failure statistic in vmstat.Are you referring to the fact that we do not increment
VMA_LOCK_SUCCESSif we successfully locked a vma but have to retry theSomething like this. I thought we would increase
VMA_LOCK_SUCCESSon success.Quoted 2 lines
page fault (in which we increment
VMA_LOCK_RETRYinstead)?
Quoted 7 lines
Quoted 1 line
I don't follow this.
Sorry, I meant to say "in which case we increment
VMA_LOCK_RETRYinstead". IOW, when we successfully lock the vma but have to retry the pagefault, we incrementVMA_LOCK_RETRYwithout incrementingVMA_LOCK_SUCCESS.
Yes, this makes me confused about what VMA_LOCK_SUCCESS represents.
Quoted 13 lines
Quoted 13 lines
Quoted 9 lines
Quoted 9 lines
mm/memory.c struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm, · line 6434
Quoted 5 lines
mm/memory.c +0
-0/* * At this point, we have a stable reference to a VMA: The VMA is * locked and we know it hasn't already been isolated. --
Wei Yang
Help you, Help me--
Wei Yang
Help you, Help me
Signature · 2 lines
Wei Yang Help you, Help me
Suren Baghdasaryan
maintainerRe: [PATCH v9 11/17] mm: replace vm_lock and detached flag with a reference count
Quoted 64 lines · Wei Yang
Quoted 62 lines · Suren Baghdasaryan
Quoted 60 lines · Wei Yang
Quoted 25 lines · Suren Baghdasaryan
Quoted 24 lines · Wei Yang
Quoted 9 lines · Suren Baghdasaryan
mm/memory.c +0
-1@@ -6354,7 +6422,6 @@ struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm,struct vm_area_struct *vma;rcu_read_lock (); -retry: vma =mas_walk (&mas);if (!vma) goto inval;mm/memory.c struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm, · line 6371
Quoted 10 lines
mm/memory.c +0
-7@@ -6362,13 +6429,6 @@ struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm,if (!vma_start_read (vma)) goto inval; -/* Check if the VMA got isolated after we found it */ -if (is_vma_detached (vma)) { -vma_end_read (vma); -count_vm_vma_lock_event (VMA_LOCK_MISS ); -/* The area was replaced with another one */ - goto retry; - }We have a little behavior change here.
Quoted 9 lines
Quoted 2 lines
Originally, if we found an detached vma, we may retry. But now, we would go to the slow path directly.
Hmm. Good point. I think the easiest way to keep the same functionality is to make
vma_start_read()return vm_area_struct* on success, NULL on locking failure and EAGAIN if vma was detached (vm_refcnt==0). Then the same retry withVMA_LOCK_MISScan be done in the case of EAGAIN.Looks good to me.
Quoted 8 lines
Quoted 8 lines
Maybe we can compare the event
VMA_LOCK_MISSandVMA_LOCK_ABORTto see the percentage of this case. If it shows this is a too rare case to impact performance, we can ignore it.Also the event
VMA_LOCK_MISSrecording is removed, but the definition is there. We may record it in thevma_start_read()when oldcnt is 0.Quoted 6 lines
Quoted 3 lines
BTW, the name of
VMA_LOCK_SUCCESSconfuse me a little. I thought it indicateslock_vma_under_rcu()successfully get a valid vma. But seems not. Sounds we don't have an overall success/failure statistic in vmstat.Are you referring to the fact that we do not increment
VMA_LOCK_SUCCESSif we successfully locked a vma but have to retry theSomething like this. I thought we would increase
VMA_LOCK_SUCCESSon success.Quoted 2 lines
page fault (in which we increment
VMA_LOCK_RETRYinstead)?I don't follow this.
Quoted 7 lines
Quoted 5 lines
Sorry, I meant to say "in which case we increment
VMA_LOCK_RETRYinstead". IOW, when we successfully lock the vma but have to retry the pagefault, we incrementVMA_LOCK_RETRYwithout incrementingVMA_LOCK_SUCCESS.Yes, this makes me confused about what
VMA_LOCK_SUCCESSrepresents.
I'll need to look into the history of why we account it this way but this is out of scope for this patchset.
Quoted 18 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
mm/memory.c +0
-0/* * At this point, we have a stable reference to a VMA: The VMA is * locked and we know it hasn't already been isolated. Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
--
Wei Yang
Help you, Help me--
Wei Yang
Help you, Help me--
Wei Yang
Help you, Help me
Re: [PATCH v9 11/17] mm: replace vm_lock and detached flag with a reference count
On Wed, Jan 15, 2025 at 05:41:27PM -0800, Suren Baghdasaryan wrote:
[...]
Quoted 33 lines
Quoted 33 lines
Quoted 32 lines
Quoted 20 lines
Quoted 2 lines
the case of EAGAIN.
Looks good to me.
Quoted 8 lines
Quoted 8 lines
Maybe we can compare the event
VMA_LOCK_MISSandVMA_LOCK_ABORTto see the percentage of this case. If it shows this is a too rare case to impact performance, we can ignore it.Also the event
VMA_LOCK_MISSrecording is removed, but the definition is there. We may record it in thevma_start_read()when oldcnt is 0.Quoted 6 lines
Quoted 3 lines
BTW, the name of
VMA_LOCK_SUCCESSconfuse me a little. I thought it indicateslock_vma_under_rcu()successfully get a valid vma. But seems not. Sounds we don't have an overall success/failure statistic in vmstat.Are you referring to the fact that we do not increment
VMA_LOCK_SUCCESSif we successfully locked a vma but have to retry theQuoted 6 lines
Something like this. I thought we would increase
VMA_LOCK_SUCCESSon success.Quoted 2 lines
page fault (in which we increment
VMA_LOCK_RETRYinstead)?I don't follow this.
Sorry, I meant to say "in which case we increment
VMA_LOCK_RETRYinstead". IOW, when we successfully lock the vma but have to retry the pagefault, we incrementVMA_LOCK_RETRYwithout incrementingVMA_LOCK_SUCCESS.
Quoted 5 lines
Quoted 1 line
Yes, this makes me confused about what
VMA_LOCK_SUCCESSrepresents.I'll need to look into the history of why we account it this way but this is out of scope for this patchset.
Agree.
Signature · 2 lines
Wei Yang Help you, Help me
Suren Baghdasaryan
maintainerPATCH v9 12/17 mm: move lesser used vma_area_struct members into the last cacheline
In reply to Suren BaghdasaryanMove several vma_area_struct members which are rarely or never used during page fault handling into the last cacheline to better pack vm_area_struct. As a result vm_area_struct will fit into 3 as opposed to 4 cachelines. New typical vm_area_struct layout:
struct vm_area_struct {
union {
struct {
long unsigned int vm_start; /* 0 8 */
long unsigned int vm_end; /* 8 8 */
}; /* 0 16 */
freeptr_t vm_freeptr; /* 0 8 */
}; /* 0 16 */
struct mm_struct * vm_mm; /* 16 8 */
pgprot_t vm_page_prot; /* 24 8 */
union {
const vm_flags_t vm_flags; /* 32 8 */
vm_flags_t __vm_flags; /* 32 8 */
}; /* 32 8 */
unsigned int vm_lock_seq; /* 40 4 */
/* XXX 4 bytes hole, try to pack */
struct list_head anon_vma_chain; /* 48 16 */
/* --- cacheline 1 boundary (64 bytes) --- */
struct anon_vma * anon_vma; /* 64 8 */
const struct vm_operations_struct * vm_ops; /* 72 8 */
long unsigned int vm_pgoff; /* 80 8 */
struct file * vm_file; /* 88 8 */
void * vm_private_data; /* 96 8 */
atomic_long_t swap_readahead_info; /* 104 8 */
struct mempolicy * vm_policy; /* 112 8 */
struct vma_numab_state * numab_state; /* 120 8 */
/* --- cacheline 2 boundary (128 bytes) --- */
refcount_t vm_refcnt (__aligned__(64)); /* 128 4 */
/* XXX 4 bytes hole, try to pack */
struct {
struct rb_node rb (__aligned__(8)); /* 136 24 */
long unsigned int rb_subtree_last; /* 160 8 */
} __attribute__((__aligned__(8))) shared; /* 136 32 */
struct anon_vma_name * anon_name; /* 168 8 */
struct vm_userfaultfd_ctx vm_userfaultfd_ctx; /* 176 8 */
/* size: 192, cachelines: 3, members: 18 */
/* sum members: 176, holes: 2, sum holes: 8 */
/* padding: 8 */
/* forced alignments: 2, forced holes: 1, sum forced holes: 4 */
} __attribute__((__aligned__(64)));
Memory consumption per 1000 VMAs becomes 48 pages:
slabinfo after vm_area_struct changes:
<name> ... <objsize> <objperslab> <pagesperslab> : ...
vm_area_struct ... 192 42 2 : ...
--- include/linux/mm_types.h | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-)
include/linux/mm_types.h +18 -20
@@ -725,17 +725,6 @@ struct vm_area_struct {*/ unsigned int vm_lock_seq;#endif - -/* -* For areas with an address space and backing store, -* linkage into the address_space->i_mmap interval tree. -* -*/ -struct { -struct rb_node rb; -unsigned long rb_subtree_last; - } shared; -/* * A file's MAP_PRIVATE vma can be in both i_mmap tree and anon_vma * list, after a COW of one of the file pages. A MAP_SHARED vma
@@ -755,14 +744,6 @@ struct vm_area_struct {struct file * vm_file;/* File we map to (can be NULL). */ void * vm_private_data;/* was vm_pte (shared mem) */ -#ifdef CONFIG_ANON_VMA_NAME -/* -* For private and shared anonymous mappings, a pointer to a null -* terminated string containing the name given to the vma, or NULL if -* unnamed. Serialized by mmap_lock. Use anon_vma_name to access. -*/ -struct anon_vma_name *anon_name; -#endif #ifdef CONFIG_SWAP atomic_long_t swap_readahead_info;#endif
@@ -775,7 +756,6 @@ struct vm_area_struct {#ifdef CONFIG_NUMA_BALANCING struct vma_numab_state *numab_state;/* NUMA Balancing state */ #endif -struct vm_userfaultfd_ctx vm_userfaultfd_ctx;#ifdef CONFIG_PER_VMA_LOCK /* Unstable RCU readers are allowed to read this. */ refcount_t vm_refcnt____cacheline_aligned_in_smp ;
@@ -783,6 +763,24 @@ struct vm_area_struct {struct lockdep_map vmlock_dep_map;#endif #endif +/* +* For areas with an address space and backing store, +* linkage into the address_space->i_mmap interval tree. +* +*/ +struct { +struct rb_node rb; +unsigned long rb_subtree_last; + } shared; +#ifdef CONFIG_ANON_VMA_NAME +/* +* For private and shared anonymous mappings, a pointer to a null +* terminated string containing the name given to the vma, or NULL if +* unnamed. Serialized by mmap_lock. Use anon_vma_name to access. +*/ +struct anon_vma_name *anon_name; +#endif +struct vm_userfaultfd_ctx vm_userfaultfd_ctx; } __randomize_layout;#ifdef CONFIG_NUMA
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
Re: [PATCH v9 12/17] mm: move lesser used vma_area_struct members into the last cacheline
Quoted 51 lines · Suren Baghdasaryan
Move several
vma_area_structmembers which are rarely or never used during page fault handling into the last cacheline to better packvm_area_struct. As a resultvm_area_structwill fit into 3 as opposed to 4 cachelines. New typicalvm_area_structlayout:struct
vm_area_struct{
union {
struct {
long unsigned intvm_start; /* 0 8 */
long unsigned intvm_end; /* 8 8 */
}; /* 0 16 */
freeptr_tvm_freeptr; /* 0 8 */
}; /* 0 16 */
structmm_struct*vm_mm; /* 16 8 */
pgprot_tvm_page_prot; /* 24 8 */
union {
constvm_flags_tvm_flags; /* 32 8 */
vm_flags_t__vm_flags; /* 32 8 */
}; /* 32 8 */
unsigned intvm_lock_seq; /* 40 4 *//* XXX 4 bytes hole, try to pack */
struct
list_headanon_vma_chain; /* 48 16 */
/* --- cacheline 1 boundary (64 bytes) --- */
structanon_vma*anon_vma; /* 64 8 */
const structvm_operations_struct*vm_ops; /* 72 8 */
long unsigned intvm_pgoff; /* 80 8 */
struct file *vm_file; /* 88 8 */
void *vm_private_data; /* 96 8 */
atomic_long_tswap_readahead_info; /* 104 8 */
struct mempolicy *vm_policy; /* 112 8 */
structvma_numab_state*numab_state; /* 120 8 */
/* --- cacheline 2 boundary (128 bytes) --- */
refcount_tvm_refcnt(__aligned__(64)); /* 128 4 *//* XXX 4 bytes hole, try to pack */
struct {
structrb_noderb (__aligned__(8)); /* 136 24 */
long unsigned intrb_subtree_last; /* 160 8 */
} __attribute__((__aligned__(8))) shared; /* 136 32 */
structanon_vma_name*anon_name; /* 168 8 */
structvm_userfaultfd_ctxvm_userfaultfd_ctx; /* 176 8 *//* size: 192, cachelines: 3, members: 18 */
/* sum members: 176, holes: 2, sum holes: 8 */
/* padding: 8 */
/* forced alignments: 2, forced holes: 1, sum forced holes: 4 */
} __attribute__((__aligned__(64)));
Quoted 7 lines
Memory consumption per 1000 VMAs becomes 48 pages:
slabinfo after
vm_area_structchanges:
<name> ... <objsize> <objperslab> <pagesperslab> : ...
vm_area_struct... 192 42 2 : ...Signed-off-by Suren Baghdasaryan <[email protected]>
Looks sensible to me:
Quoted 77 lines
--- include/linux/mm_types.h | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-)include/linux/mm_types.h +18
-21@@ -725,17 +725,6 @@ struct vm_area_struct {*/ unsigned int vm_lock_seq;#endif - -/* -* For areas with an address space and backing store, -* linkage into the address_space->i_mmap interval tree. -* -*/ -struct { -struct rb_node rb; -unsigned long rb_subtree_last; - } shared; -/* * A file's MAP_PRIVATE vma can be in both i_mmap tree and anon_vma * list, after a COW of one of the file pages. A MAP_SHARED vma @@ -755,14 +744,6 @@ struct vm_area_struct {struct file * vm_file;/* File we map to (can be NULL). */ void * vm_private_data;/* was vm_pte (shared mem) */ -#ifdef CONFIG_ANON_VMA_NAME -/* -* For private and shared anonymous mappings, a pointer to a null -* terminated string containing the name given to the vma, or NULL if -* unnamed. Serialized by mmap_lock. Use anon_vma_name to access. -*/ -struct anon_vma_name *anon_name; -#endif #ifdef CONFIG_SWAP atomic_long_t swap_readahead_info;#endif @@ -775,7 +756,6 @@ struct vm_area_struct {#ifdef CONFIG_NUMA_BALANCING struct vma_numab_state *numab_state;/* NUMA Balancing state */ #endif -struct vm_userfaultfd_ctx vm_userfaultfd_ctx;#ifdef CONFIG_PER_VMA_LOCK /* Unstable RCU readers are allowed to read this. */ refcount_t vm_refcnt____cacheline_aligned_in_smp ;@@ -783,6 +763,24 @@ struct vm_area_struct {struct lockdep_map vmlock_dep_map;#endif #endif +/* +* For areas with an address space and backing store, +* linkage into the address_space->i_mmap interval tree. +* +*/ +struct { +struct rb_node rb; +unsigned long rb_subtree_last; + } shared; +#ifdef CONFIG_ANON_VMA_NAME +/* +* For private and shared anonymous mappings, a pointer to a null +* terminated string containing the name given to the vma, or NULL if +* unnamed. Serialized by mmap_lock. Use anon_vma_name to access. +*/ +struct anon_vma_name *anon_name; +#endif +struct vm_userfaultfd_ctx vm_userfaultfd_ctx; } __randomize_layout;#ifdef CONFIG_NUMA --2.47.1.613.gc27f4b7a9f-goog
Peter Zijlstra
maintainerRe: [PATCH v9 12/17] mm: move lesser used vma_area_struct members into the last cacheline
In reply to Suren BaghdasaryanQuoted 5 lines · Suren Baghdasaryan
Move several
vma_area_structmembers which are rarely or never used during page fault handling into the last cacheline to better packvm_area_struct. As a resultvm_area_structwill fit into 3 as opposed to 4 cachelines. New typicalvm_area_structlayout:
Quoted 15 lines
struct
vm_area_struct{
union {
struct {
long unsigned intvm_start; /* 0 8 */
long unsigned intvm_end; /* 8 8 */
}; /* 0 16 */
freeptr_tvm_freeptr; /* 0 8 */
}; /* 0 16 */
structmm_struct*vm_mm; /* 16 8 */
pgprot_tvm_page_prot; /* 24 8 */
union {
constvm_flags_tvm_flags; /* 32 8 */
vm_flags_t__vm_flags; /* 32 8 */
}; /* 32 8 */
unsigned intvm_lock_seq; /* 40 4 */
Does it not make sense to move this seq field near the refcnt?
Quoted 29 lines
/* XXX 4 bytes hole, try to pack */
struct
list_headanon_vma_chain; /* 48 16 */
/* --- cacheline 1 boundary (64 bytes) --- */
structanon_vma*anon_vma; /* 64 8 */
const structvm_operations_struct*vm_ops; /* 72 8 */
long unsigned intvm_pgoff; /* 80 8 */
struct file *vm_file; /* 88 8 */
void *vm_private_data; /* 96 8 */
atomic_long_tswap_readahead_info; /* 104 8 */
struct mempolicy *vm_policy; /* 112 8 */
structvma_numab_state*numab_state; /* 120 8 */
/* --- cacheline 2 boundary (128 bytes) --- */
refcount_tvm_refcnt(__aligned__(64)); /* 128 4 *//* XXX 4 bytes hole, try to pack */
struct {
structrb_noderb (__aligned__(8)); /* 136 24 */
long unsigned intrb_subtree_last; /* 160 8 */
} __attribute__((__aligned__(8))) shared; /* 136 32 */
structanon_vma_name*anon_name; /* 168 8 */
structvm_userfaultfd_ctxvm_userfaultfd_ctx; /* 176 8 *//* size: 192, cachelines: 3, members: 18 */
/* sum members: 176, holes: 2, sum holes: 8 */
/* padding: 8 */
/* forced alignments: 2, forced holes: 1, sum forced holes: 4 */
} __attribute__((__aligned__(64)));
Suren Baghdasaryan
maintainerRe: [PATCH v9 12/17] mm: move lesser used vma_area_struct members into the last cacheline
Quoted 23 lines · Peter Zijlstra
Quoted 5 lines · Suren Baghdasaryan
Move several
vma_area_structmembers which are rarely or never used during page fault handling into the last cacheline to better packvm_area_struct. As a resultvm_area_structwill fit into 3 as opposed to 4 cachelines. New typicalvm_area_structlayout:Quoted 15 lines
struct
vm_area_struct{
union {
struct {
long unsigned intvm_start; /* 0 8 */
long unsigned intvm_end; /* 8 8 */
}; /* 0 16 */
freeptr_tvm_freeptr; /* 0 8 */
}; /* 0 16 */
structmm_struct*vm_mm; /* 16 8 */
pgprot_tvm_page_prot; /* 24 8 */
union {
constvm_flags_tvm_flags; /* 32 8 */
vm_flags_t__vm_flags; /* 32 8 */
}; /* 32 8 */
unsigned intvm_lock_seq; /* 40 4 */
Quoted 1 line
Does it not make sense to move this seq field near the refcnt?
In an earlier version, when vm_lock was not a refcount yet, I tried that and moving vm_lock_seq introduced regression in the pft test. We have that early vm_lock_seq check in the beginning of vma_start_read() and if it fails we bail out early without locking. I think that might be the reason why keeping vm_lock_seq in the first cacheling is beneficial. But I'll try moving it again now that we have vm_refcnt instead of the lock and see if pft still shows any regression.
Quoted 32 lines
Quoted 30 lines
/* XXX 4 bytes hole, try to pack */
struct
list_headanon_vma_chain; /* 48 16 */
/* --- cacheline 1 boundary (64 bytes) --- */
structanon_vma*anon_vma; /* 64 8 */
const structvm_operations_struct*vm_ops; /* 72 8 */
long unsigned intvm_pgoff; /* 80 8 */
struct file *vm_file; /* 88 8 */
void *vm_private_data; /* 96 8 */
atomic_long_tswap_readahead_info; /* 104 8 */
struct mempolicy *vm_policy; /* 112 8 */
structvma_numab_state*numab_state; /* 120 8 */
/* --- cacheline 2 boundary (128 bytes) --- */
refcount_tvm_refcnt(__aligned__(64)); /* 128 4 *//* XXX 4 bytes hole, try to pack */
struct {
structrb_noderb (__aligned__(8)); /* 136 24 */
long unsigned intrb_subtree_last; /* 160 8 */
} __attribute__((__aligned__(8))) shared; /* 136 32 */
structanon_vma_name*anon_name; /* 168 8 */
structvm_userfaultfd_ctxvm_userfaultfd_ctx; /* 176 8 *//* size: 192, cachelines: 3, members: 18 */
/* sum members: 176, holes: 2, sum holes: 8 */
/* padding: 8 */
/* forced alignments: 2, forced holes: 1, sum forced holes: 4 */
} __attribute__((__aligned__(64)));
Suren Baghdasaryan
maintainerRe: [PATCH v9 12/17] mm: move lesser used vma_area_struct members into the last cacheline
Quoted 27 lines · Suren Baghdasaryan
Quoted 23 lines · Peter Zijlstra
Quoted 5 lines · Suren Baghdasaryan
Move several
vma_area_structmembers which are rarely or never used during page fault handling into the last cacheline to better packvm_area_struct. As a resultvm_area_structwill fit into 3 as opposed to 4 cachelines. New typicalvm_area_structlayout:Quoted 15 lines
struct
vm_area_struct{
union {
struct {
long unsigned intvm_start; /* 0 8 */
long unsigned intvm_end; /* 8 8 */
}; /* 0 16 */
freeptr_tvm_freeptr; /* 0 8 */
}; /* 0 16 */
structmm_struct*vm_mm; /* 16 8 */
pgprot_tvm_page_prot; /* 24 8 */
union {
constvm_flags_tvm_flags; /* 32 8 */
vm_flags_t__vm_flags; /* 32 8 */
}; /* 32 8 */
unsigned intvm_lock_seq; /* 40 4 */Quoted 1 line
Does it not make sense to move this seq field near the refcnt?
Quoted 7 lines
In an earlier version, when
vm_lockwas not a refcount yet, I tried that and movingvm_lock_seqintroduced regression in the pft test. We have that earlyvm_lock_seqcheck in the beginning ofvma_start_read()and if it fails we bail out early without locking. I think that might be the reason why keepingvm_lock_seqin the first cacheling is beneficial. But I'll try moving it again now that we havevm_refcntinstead of the lock and see if pft still shows any regression.
I confirmed that moving vm_lock_seq next to vm_refcnt regresses pagefault performance:
Hmean faults/cpu-1 508634.6876 ( 0.00%) 508548.5498 * -0.02%* Hmean faults/cpu-4 474767.2684 ( 0.00%) 475620.7653 * 0.18%* Hmean faults/cpu-7 451356.6844 ( 0.00%) 446738.2381 * -1.02%* Hmean faults/cpu-12 360114.9092 ( 0.00%) 337121.8189 * -6.38%* Hmean faults/cpu-21 227567.8237 ( 0.00%) 205277.2029 * -9.80%* Hmean faults/cpu-30 163383.6765 ( 0.00%) 152765.1451 * -6.50%* Hmean faults/cpu-48 118048.2568 ( 0.00%) 109959.2027 * -6.85%* Hmean faults/cpu-56 103189.6761 ( 0.00%) 92989.3749 * -9.89%* Hmean faults/sec-1 508228.4512 ( 0.00%) 508129.1963 * -0.02%* Hmean faults/sec-4 1854868.9033 ( 0.00%) 1862443.6146 * 0.41%* Hmean faults/sec-7 3088881.6158 ( 0.00%) 3050403.1664 * -1.25%* Hmean faults/sec-12 4222540.9948 ( 0.00%) 3951163.9557 * -6.43%* Hmean faults/sec-21 4555777.5386 ( 0.00%) 4130470.6021 * -9.34%* Hmean faults/sec-30 4336721.3467 ( 0.00%) 4150477.5095 * -4.29%* Hmean faults/sec-48 5163921.7465 ( 0.00%) 4857286.2166 * -5.94%* Hmean faults/sec-56 5413622.8890 ( 0.00%) 4936484.0021 * -8.81%*
So, I kept it unchanged in v10
(https://lore.kernel.org/all/[email protected]/)
Quoted 33 lines
Quoted 33 lines
Quoted 31 lines
/* XXX 4 bytes hole, try to pack */
struct
list_headanon_vma_chain; /* 48 16 */
/* --- cacheline 1 boundary (64 bytes) --- */
structanon_vma*anon_vma; /* 64 8 */
const structvm_operations_struct*vm_ops; /* 72 8 */
long unsigned intvm_pgoff; /* 80 8 */
struct file *vm_file; /* 88 8 */
void *vm_private_data; /* 96 8 */
atomic_long_tswap_readahead_info; /* 104 8 */
struct mempolicy *vm_policy; /* 112 8 */
structvma_numab_state*numab_state; /* 120 8 */
/* --- cacheline 2 boundary (128 bytes) --- */
refcount_tvm_refcnt(__aligned__(64)); /* 128 4 *//* XXX 4 bytes hole, try to pack */
struct {
structrb_noderb (__aligned__(8)); /* 136 24 */
long unsigned intrb_subtree_last; /* 160 8 */
} __attribute__((__aligned__(8))) shared; /* 136 32 */
structanon_vma_name*anon_name; /* 168 8 */
structvm_userfaultfd_ctxvm_userfaultfd_ctx; /* 176 8 *//* size: 192, cachelines: 3, members: 18 */
/* sum members: 176, holes: 2, sum holes: 8 */
/* padding: 8 */
/* forced alignments: 2, forced holes: 1, sum forced holes: 4 */
} __attribute__((__aligned__(64)));
Suren Baghdasaryan
maintainerPATCH v9 13/17 mm/debug: print vm_refcnt state when dumping the vma
In reply to Suren Baghdasaryanvm_refcnt encodes a number of useful states:
- whether vma is attached or detached
- the number of current vma readers
- presence of a vma writer
Let's include it in the vma dump.
--- mm/debug.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
mm/debug.c +12 -0
@@ -178,6 +178,17 @@ EXPORT_SYMBOL(dump_page);void dump_vma (const struct vm_area_struct *vma) { +#ifdef CONFIG_PER_VMA_LOCK +pr_emerg ("vma %px start %px end %px mm %px\n" +"prot %lx anon_vma %px vm_ops %px\n" +"pgoff %lx file %px private_data %px\n" +"flags: %#lx(%pGv) refcnt %x\n" , + vma, (void *)vma->vm_start, (void *)vma->vm_end, vma->vm_mm, + (unsigned long )pgprot_val (vma->vm_page_prot), + vma->anon_vma, vma->vm_ops, vma->vm_pgoff, + vma->vm_file, vma->vm_private_data, + vma->vm_flags, &vma->vm_flags,refcount_read (&vma->vm_refcnt)); +#else pr_emerg ("vma %px start %px end %px mm %px\n" "prot %lx anon_vma %px vm_ops %px\n" "pgoff %lx file %px private_data %px\n"
@@ -187,6 +198,7 @@ void dump_vma(const struct vm_area_struct *vma) vma->anon_vma, vma->vm_ops, vma->vm_pgoff, vma->vm_file, vma->vm_private_data, vma->vm_flags, &vma->vm_flags); +#endif }EXPORT_SYMBOL (dump_vma);
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
Re: [PATCH v9 13/17] mm/debug: print vm_refcnt state when dumping the vma
Quoted 35 lines · Suren Baghdasaryan
vm_refcntencodes a number of useful states:
- whether vma is attached or detached
- the number of current vma readers
- presence of a vma writer
Let's include it in the vma dump.Signed-off-by Suren Baghdasaryan <[email protected]>Acked-by Vlastimil Babka <[email protected]>--- mm/debug.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)mm/debug.c +11
-0@@ -178,6 +178,17 @@ EXPORT_SYMBOL(dump_page);void dump_vma (const struct vm_area_struct *vma) { +#ifdef CONFIG_PER_VMA_LOCK +pr_emerg ("vma %px start %px end %px mm %px\n" +"prot %lx anon_vma %px vm_ops %px\n" +"pgoff %lx file %px private_data %px\n" +"flags: %#lx(%pGv) refcnt %x\n" , + vma, (void *)vma->vm_start, (void *)vma->vm_end, vma->vm_mm, + (unsigned long )pgprot_val (vma->vm_page_prot), + vma->anon_vma, vma->vm_ops, vma->vm_pgoff, + vma->vm_file, vma->vm_private_data, + vma->vm_flags, &vma->vm_flags,refcount_read (&vma->vm_refcnt)); +#else pr_emerg ("vma %px start %px end %px mm %px\n" "prot %lx anon_vma %px vm_ops %px\n" "pgoff %lx file %px private_data %px\n"
mm/debug.c void dump_vma(const struct vm_area_struct *vma) · line 202
Quoted 5 lines
mm/debug.c +1
-0@@ -187,6 +198,7 @@ void dump_vma(const struct vm_area_struct *vma) vma->anon_vma, vma->vm_ops, vma->vm_pgoff, vma->vm_file, vma->vm_private_data, vma->vm_flags, &vma->vm_flags); +#endif }
This is pretty horribly duplicative and not in line with how this kind of thing is done in the rest of the file. You're just adding one entry, so why not:
void dump_vma(const struct vm_area_struct *vma) {
pr_emerg("vma %px start %px end %px mm %px\n"
"prot %lx anon_vma %px vm_ops %px\n" "pgoff %lx file %px private_data %px\n"
#ifdef CONFIG_PER_VMA_LOCK "refcnt %x\n" #endif "flags: %#lx(%pGv)\n", vma, (void *)vma->vm_start, (void *)vma->vm_end, vma->vm_mm, (unsigned long)pgprot_val(vma->vm_page_prot), vma->anon_vma, vma->vm_ops, vma->vm_pgoff, vma->vm_file, vma->vm_private_data, vma->vm_flags, #ifdef CONFIG_PER_VMA_LOCK refcount_read(&vma->vm_refcnt), #endif &vma->vm_flags); }
?
Quoted 5 lines
mm/debug.c +0
-1EXPORT_SYMBOL (dump_vma); --2.47.1.613.gc27f4b7a9f-goog
Re: [PATCH v9 13/17] mm/debug: print vm_refcnt state when dumping the vma
Quoted 51 lines · Lorenzo Stoakes
Quoted 35 lines · Suren Baghdasaryan
vm_refcntencodes a number of useful states:
- whether vma is attached or detached
- the number of current vma readers
- presence of a vma writer
Let's include it in the vma dump.Signed-off-by Suren Baghdasaryan <[email protected]>Acked-by Vlastimil Babka <[email protected]>--- mm/debug.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)mm/debug.c +11
-0@@ -178,6 +178,17 @@ EXPORT_SYMBOL(dump_page);void dump_vma (const struct vm_area_struct *vma) { +#ifdef CONFIG_PER_VMA_LOCK +pr_emerg ("vma %px start %px end %px mm %px\n" +"prot %lx anon_vma %px vm_ops %px\n" +"pgoff %lx file %px private_data %px\n" +"flags: %#lx(%pGv) refcnt %x\n" , + vma, (void *)vma->vm_start, (void *)vma->vm_end, vma->vm_mm, + (unsigned long )pgprot_val (vma->vm_page_prot), + vma->anon_vma, vma->vm_ops, vma->vm_pgoff, + vma->vm_file, vma->vm_private_data, + vma->vm_flags, &vma->vm_flags,refcount_read (&vma->vm_refcnt)); +#else pr_emerg ("vma %px start %px end %px mm %px\n" "prot %lx anon_vma %px vm_ops %px\n" "pgoff %lx file %px private_data %px\n"mm/debug.c void dump_vma(const struct vm_area_struct *vma) · line 202
Quoted 5 lines
mm/debug.c +1
-0@@ -187,6 +198,7 @@ void dump_vma(const struct vm_area_struct *vma) vma->anon_vma, vma->vm_ops, vma->vm_pgoff, vma->vm_file, vma->vm_private_data, vma->vm_flags, &vma->vm_flags); +#endif }This is pretty horribly duplicative and not in line with how this kind of thing is done in the rest of the file. You're just adding one entry, so why not:
void
dump_vma(const struct vm_area_struct *vma){
pr_emerg("vma %px start %px end %px mm %px\n"
"prot %lxanon_vma%pxvm_ops%px\n" "pgoff %lx file %pxprivate_data%px\n"
Quoted 14 lines
#ifdef CONFIG_PER_VMA_LOCK "refcnt %x\n" #endif "flags: %#lx(%pGv)\n", vma, (void *)vma->vm_start, (void *)vma->vm_end, vma->vm_mm, (unsigned long)pgprot_val(vma->vm_page_prot), vma->anon_vma, vma->vm_ops, vma->vm_pgoff, vma->vm_file, vma->vm_private_data, vma->vm_flags, #ifdef CONFIG_PER_VMA_LOCK refcount_read(&vma->vm_refcnt), #endif &vma->vm_flags); }
right, I had an issue with this as well.
Another option would be:
pr_emerg("vma %px start %px end %px mm %px\n"
"prot %lx anon_vma %px vm_ops %px\n" "pgoff %lx file %px private_data %px\n",
<big mess here>);
dump_vma_refcnt();
pr_emerg("flags:...", vma_vm_flags);Then dump_vma_refcnt() either dumps the refcnt or does nothing, depending on the config option.
Either way is good with me. Lorenzo's suggestion is in line with the file and it's clear as to why the refcnt might be missing, but I don't really see this being an issue in practice.
Thanks,
Liam
Suren Baghdasaryan
maintainerRe: [PATCH v9 13/17] mm/debug: print vm_refcnt state when dumping the vma
Quoted 80 lines · Liam R. Howlett
Quoted 51 lines · Lorenzo Stoakes
Quoted 35 lines · Suren Baghdasaryan
vm_refcntencodes a number of useful states:
- whether vma is attached or detached
- the number of current vma readers
- presence of a vma writer
Let's include it in the vma dump.Signed-off-by Suren Baghdasaryan <[email protected]>Acked-by Vlastimil Babka <[email protected]>--- mm/debug.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)mm/debug.c +11
-0@@ -178,6 +178,17 @@ EXPORT_SYMBOL(dump_page);void dump_vma (const struct vm_area_struct *vma) { +#ifdef CONFIG_PER_VMA_LOCK +pr_emerg ("vma %px start %px end %px mm %px\n" +"prot %lx anon_vma %px vm_ops %px\n" +"pgoff %lx file %px private_data %px\n" +"flags: %#lx(%pGv) refcnt %x\n" , + vma, (void *)vma->vm_start, (void *)vma->vm_end, vma->vm_mm, + (unsigned long )pgprot_val (vma->vm_page_prot), + vma->anon_vma, vma->vm_ops, vma->vm_pgoff, + vma->vm_file, vma->vm_private_data, + vma->vm_flags, &vma->vm_flags,refcount_read (&vma->vm_refcnt)); +#else pr_emerg ("vma %px start %px end %px mm %px\n" "prot %lx anon_vma %px vm_ops %px\n" "pgoff %lx file %px private_data %px\n"mm/debug.c void dump_vma(const struct vm_area_struct *vma) · line 202
Quoted 5 lines
mm/debug.c +1
-0@@ -187,6 +198,7 @@ void dump_vma(const struct vm_area_struct *vma) vma->anon_vma, vma->vm_ops, vma->vm_pgoff, vma->vm_file, vma->vm_private_data, vma->vm_flags, &vma->vm_flags); +#endif }This is pretty horribly duplicative and not in line with how this kind of thing is done in the rest of the file. You're just adding one entry, so why not:
void
dump_vma(const struct vm_area_struct *vma){
pr_emerg("vma %px start %px end %px mm %px\n"
"prot %lxanon_vma%pxvm_ops%px\n" "pgoff %lx file %pxprivate_data%px\n"Quoted 14 lines
#ifdef CONFIG_PER_VMA_LOCK "refcnt %x\n" #endif "flags: %#lx(%pGv)\n", vma, (void *)vma->vm_start, (void *)vma->vm_end, vma->vm_mm, (unsigned long)pgprot_val(vma->vm_page_prot), vma->anon_vma, vma->vm_ops, vma->vm_pgoff, vma->vm_file, vma->vm_private_data, vma->vm_flags, #ifdef CONFIG_PER_VMA_LOCK refcount_read(&vma->vm_refcnt), #endif &vma->vm_flags); }right, I had an issue with this as well.
Another option would be:
pr_emerg("vma %px start %px end %px mm %px\n"
"prot %lxanon_vma%pxvm_ops%px\n" "pgoff %lx file %pxprivate_data%px\n", <big mess here>);
dump_vma_refcnt();pr_emerg("flags:...", vma_vm_flags);
Quoted 6 lines
Then
dump_vma_refcnt()either dumps the refcnt or does nothing, depending on the config option.Either way is good with me. Lorenzo's suggestion is in line with the file and it's clear as to why the refcnt might be missing, but I don't really see this being an issue in practice.
Thanks for clarifying! Lorenzo's suggestion LGTM too. I'll adopt it. Thanks!
Quoted 4 lines
Thanks,
Liam
Re: [PATCH v9 13/17] mm/debug: print vm_refcnt state when dumping the vma
Quoted 81 lines · Suren Baghdasaryan
Quoted 80 lines · Liam R. Howlett
Quoted 51 lines · Lorenzo Stoakes
Quoted 35 lines · Suren Baghdasaryan
vm_refcntencodes a number of useful states:
- whether vma is attached or detached
- the number of current vma readers
- presence of a vma writer
Let's include it in the vma dump.Signed-off-by Suren Baghdasaryan <[email protected]>Acked-by Vlastimil Babka <[email protected]>--- mm/debug.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)mm/debug.c +11
-0@@ -178,6 +178,17 @@ EXPORT_SYMBOL(dump_page);void dump_vma (const struct vm_area_struct *vma) { +#ifdef CONFIG_PER_VMA_LOCK +pr_emerg ("vma %px start %px end %px mm %px\n" +"prot %lx anon_vma %px vm_ops %px\n" +"pgoff %lx file %px private_data %px\n" +"flags: %#lx(%pGv) refcnt %x\n" , + vma, (void *)vma->vm_start, (void *)vma->vm_end, vma->vm_mm, + (unsigned long )pgprot_val (vma->vm_page_prot), + vma->anon_vma, vma->vm_ops, vma->vm_pgoff, + vma->vm_file, vma->vm_private_data, + vma->vm_flags, &vma->vm_flags,refcount_read (&vma->vm_refcnt)); +#else pr_emerg ("vma %px start %px end %px mm %px\n" "prot %lx anon_vma %px vm_ops %px\n" "pgoff %lx file %px private_data %px\n"mm/debug.c void dump_vma(const struct vm_area_struct *vma) · line 202
Quoted 5 lines
mm/debug.c +1
-0@@ -187,6 +198,7 @@ void dump_vma(const struct vm_area_struct *vma) vma->anon_vma, vma->vm_ops, vma->vm_pgoff, vma->vm_file, vma->vm_private_data, vma->vm_flags, &vma->vm_flags); +#endif }This is pretty horribly duplicative and not in line with how this kind of thing is done in the rest of the file. You're just adding one entry, so why not:
void
dump_vma(const struct vm_area_struct *vma){
pr_emerg("vma %px start %px end %px mm %px\n"
"prot %lxanon_vma%pxvm_ops%px\n" "pgoff %lx file %pxprivate_data%px\n"Quoted 14 lines
#ifdef CONFIG_PER_VMA_LOCK "refcnt %x\n" #endif "flags: %#lx(%pGv)\n", vma, (void *)vma->vm_start, (void *)vma->vm_end, vma->vm_mm, (unsigned long)pgprot_val(vma->vm_page_prot), vma->anon_vma, vma->vm_ops, vma->vm_pgoff, vma->vm_file, vma->vm_private_data, vma->vm_flags, #ifdef CONFIG_PER_VMA_LOCK refcount_read(&vma->vm_refcnt), #endif &vma->vm_flags); }right, I had an issue with this as well.
Another option would be:
pr_emerg("vma %px start %px end %px mm %px\n"
"prot %lxanon_vma%pxvm_ops%px\n" "pgoff %lx file %pxprivate_data%px\n", <big mess here>);
dump_vma_refcnt();pr_emerg("flags:...", vma_vm_flags);
Quoted 9 lines
Quoted 6 lines
Then
dump_vma_refcnt()either dumps the refcnt or does nothing, depending on the config option.Either way is good with me. Lorenzo's suggestion is in line with the file and it's clear as to why the refcnt might be missing, but I don't really see this being an issue in practice.
Thanks for clarifying! Lorenzo's suggestion LGTM too. I'll adopt it. Thanks!
Cheers guys!
Quoted 4 lines
Quoted 4 lines
Thanks,
Liam
Suren Baghdasaryan
maintainerPATCH v9 14/17 mm: remove extra vma_numab_state_init() call
In reply to Suren Baghdasaryanvma_init() already memset's the whole vm_area_struct to 0, so there is no need to an additional vma_numab_state_init().
--- include/linux/mm.h | 1 - 1 file changed, 1 deletion(-)
include/linux/mm.h +0 -1
@@ -948,7 +948,6 @@ static inline void vma_init(struct vm_area_struct *vma, struct mm_struct *mm) vma->vm_mm = mm; vma->vm_ops = &vma_dummy_vm_ops;INIT_LIST_HEAD (&vma->anon_vma_chain); -vma_numab_state_init (vma);vma_lock_init (vma, false); }
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
Re: [PATCH v9 14/17] mm: remove extra vma_numab_state_init() call
Quoted 2 lines · Suren Baghdasaryan
vma_init()already memset's the wholevm_area_structto 0, so there is no need to an additionalvma_numab_state_init().
Hm strangely random change :) I'm guessing this was a pre-existing thing?
Quoted 3 lines
Signed-off-by Suren Baghdasaryan <[email protected]>Reviewed-by Vlastimil Babka <[email protected]>
I mean this looks fine, so fair enough just feels a bit incongruous with series. But regardless:
Quoted 9 lines
--- include/linux/mm.h | 1 - 1 file changed, 1 deletion(-)include/linux/mm.h +0
-0
include/linux/mm.h static inline void vma_init(struct vm_area_struct *vma, struct mm_struct *mm) · line 952
Quoted 6 lines
include/linux/mm.h +0
-1@@ -948,7 +948,6 @@ static inline void vma_init(struct vm_area_struct *vma, struct mm_struct *mm) vma->vm_mm = mm; vma->vm_ops = &vma_dummy_vm_ops;INIT_LIST_HEAD (&vma->anon_vma_chain); -vma_numab_state_init (vma);vma_lock_init (vma, false); }
This leaves one other caller in vm_area_dup() (I _hate_ that this lives in the fork code... - might very well look at churning some VMA stuff over from there to an appropriate place).
While we're here, I mean this thing seems a bit of out scope for the series but if we're doing it, can we just remove vma_numab_state_init() and instead edit vm_area_init_from() to #ifdef ... this like the other fields now?
I's not exactly urgent though as this stuff in the fork code is a bit of a mess anyway...
Quoted 4 lines
include/linux/mm.h +0
-1--2.47.1.613.gc27f4b7a9f-goog
Suren Baghdasaryan
maintainerRe: [PATCH v9 14/17] mm: remove extra vma_numab_state_init() call
Quoted 6 lines · Lorenzo Stoakes
Quoted 2 lines · Suren Baghdasaryan
vma_init()already memset's the wholevm_area_structto 0, so there is no need to an additionalvma_numab_state_init().Hm strangely random change :) I'm guessing this was a pre-existing thing?
Yeah, I stumbled on it while working on an earlier version of this patchset which involved ctor usage.
Quoted 30 lines
Quoted 4 lines
Signed-off-by Suren Baghdasaryan <[email protected]>Reviewed-by Vlastimil Babka <[email protected]>I mean this looks fine, so fair enough just feels a bit incongruous with series. But regardless:
Reviewed-by Lorenzo Stoakes <[email protected]>Quoted 9 lines
--- include/linux/mm.h | 1 - 1 file changed, 1 deletion(-)include/linux/mm.h +0
-0include/linux/mm.h static inline void vma_init(struct vm_area_struct *vma, struct mm_struct *mm) · line 952
Quoted 6 lines
include/linux/mm.h +0
-1@@ -948,7 +948,6 @@ static inline void vma_init(struct vm_area_struct *vma, struct mm_struct *mm) vma->vm_mm = mm; vma->vm_ops = &vma_dummy_vm_ops;INIT_LIST_HEAD (&vma->anon_vma_chain); -vma_numab_state_init (vma);vma_lock_init (vma, false); }This leaves one other caller in
vm_area_dup()(I _hate_ that this lives in the fork code... - might very well look at churning some VMA stuff over from there to an appropriate place).
Quoted 7 lines
While we're here, I mean this thing seems a bit of out scope for the series but if we're doing it, can we just remove
vma_numab_state_init()and instead editvm_area_init_from()to #ifdef ... this like the other fields now?I's not exactly urgent though as this stuff in the fork code is a bit of a mess anyway...
Yeah, let's keep the cleanup out for now. The series is already quite big. I included this one-line cleanup since it was uncontroversial and simple.
Quoted 5 lines
Quoted 5 lines
include/linux/mm.h +0
-1--2.47.1.613.gc27f4b7a9f-goog
Re: [PATCH v9 14/17] mm: remove extra vma_numab_state_init() call
Quoted 47 lines · Suren Baghdasaryan
Quoted 6 lines · Lorenzo Stoakes
Quoted 2 lines · Suren Baghdasaryan
vma_init()already memset's the wholevm_area_structto 0, so there is no need to an additionalvma_numab_state_init().Hm strangely random change :) I'm guessing this was a pre-existing thing?
Yeah, I stumbled on it while working on an earlier version of this patchset which involved ctor usage.
Quoted 35 lines
Quoted 4 lines
Signed-off-by Suren Baghdasaryan <[email protected]>Reviewed-by Vlastimil Babka <[email protected]>I mean this looks fine, so fair enough just feels a bit incongruous with series. But regardless:
Reviewed-by Lorenzo Stoakes <[email protected]>Quoted 9 lines
--- include/linux/mm.h | 1 - 1 file changed, 1 deletion(-)include/linux/mm.h +0
-0include/linux/mm.h static inline void vma_init(struct vm_area_struct *vma, struct mm_struct *mm) · line 952
Quoted 6 lines
include/linux/mm.h +0
-1@@ -948,7 +948,6 @@ static inline void vma_init(struct vm_area_struct *vma, struct mm_struct *mm) vma->vm_mm = mm; vma->vm_ops = &vma_dummy_vm_ops;INIT_LIST_HEAD (&vma->anon_vma_chain); -vma_numab_state_init (vma);vma_lock_init (vma, false); }This leaves one other caller in
vm_area_dup()(I _hate_ that this lives in the fork code... - might very well look at churning some VMA stuff over from there to an appropriate place).While we're here, I mean this thing seems a bit of out scope for the series but if we're doing it, can we just remove
vma_numab_state_init()and instead editvm_area_init_from()to #ifdef ... this like the other fields now?
Quoted 6 lines
Quoted 2 lines
I's not exactly urgent though as this stuff in the fork code is a bit of a mess anyway...
Yeah, let's keep the cleanup out for now. The series is already quite big. I included this one-line cleanup since it was uncontroversial and simple.
Yeah it's fine not a big deal. We can address this trivia later.
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
include/linux/mm.h +0
-1--2.47.1.613.gc27f4b7a9f-goog
Suren Baghdasaryan
maintainerPATCH v9 15/17 mm: prepare lock_vma_under_rcu() for vma reuse possibility
In reply to Suren BaghdasaryanOnce we make vma cache SLAB_TYPESAFE_BY_RCU, it will be possible for a vma to be reused and attached to another mm after lock_vma_under_rcu() locks the vma. lock_vma_under_rcu() should ensure that vma_start_read() is using the original mm and after locking the vma it should ensure that vma->vm_mm has not changed from under us.
--- include/linux/mm.h | 10 ++++++---- mm/memory.c | 7 ++++--- 2 files changed, 10 insertions(+), 7 deletions(-)
include/linux/mm.h +6 -4
@@ -739,8 +739,10 @@ static inline void vma_refcount_put(struct vm_area_struct *vma)* Try to read-lock a vma. The function is allowed to occasionally yield false * locked result to avoid performance overhead, in which case we fall back to * using mmap_lock. The function should never yield false unlocked result. +* False locked result is possible if mm_lock_seq overflows or if vma gets +* reused and attached to a different mm before we lock it. */ -static inline bool vma_start_read (struct vm_area_struct *vma) +static inline bool vma_start_read (struct mm_struct *mm,struct vm_area_struct *vma) {int oldcnt;
@@ -751,7 +753,7 @@ static inline bool vma_start_read(struct vm_area_struct *vma)* we don't rely on for anything - the mm_lock_seq read against which we * need ordering is below. */ -if (READ_ONCE (vma->vm_lock_seq) ==READ_ONCE (vma->vm_mm->mm_lock_seq.sequence)) +if (READ_ONCE (vma->vm_lock_seq) ==READ_ONCE (mm->mm_lock_seq.sequence))return false;/*
@@ -774,7 +776,7 @@ static inline bool vma_start_read(struct vm_area_struct *vma)* after it has been unlocked. * This pairs with RELEASE semantics in vma_end_write_all(). */ -if (unlikely (vma->vm_lock_seq ==raw_read_seqcount (&vma->vm_mm->mm_lock_seq))) { +if (unlikely (vma->vm_lock_seq ==raw_read_seqcount (&mm->mm_lock_seq))) {vma_refcount_put (vma);return false; }
@@ -906,7 +908,7 @@ struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm,#else /* CONFIG_PER_VMA_LOCK */ static inline void vma_lock_init (struct vm_area_struct *vma,bool reset_refcnt) {} -static inline bool vma_start_read (struct vm_area_struct *vma) +static inline bool vma_start_read (struct mm_struct *mm,struct vm_area_struct *vma) {return false; }static inline void vma_end_read (struct vm_area_struct *vma) {}static inline void vma_start_write (struct vm_area_struct *vma) {}
mm/memory.c +4 -3
@@ -6426,7 +6426,7 @@ struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm,if (!vma) goto inval; -if (!vma_start_read (vma)) +if (!vma_start_read (mm, vma)) goto inval;/*
@@ -6436,8 +6436,9 @@ struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm,* fields are accessible for RCU readers. */ -/* Check since vm_start/vm_end might change before we lock the VMA */ -if (unlikely (address < vma->vm_start || address >= vma->vm_end)) +/* Check if the vma we locked is the right one. */ +if (unlikely (vma->vm_mm != mm || + address < vma->vm_start || address >= vma->vm_end)) goto inval_end_read;rcu_read_unlock ();
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
Suren Baghdasaryan
maintainerPATCH v9 16/17 mm: make vma cache SLAB_TYPESAFE_BY_RCU
In reply to Suren BaghdasaryanTo enable SLAB_TYPESAFE_BY_RCU for vma cache we need to ensure that object reuse before RCU grace period is over will be detected by lock_vma_under_rcu().
Current checks are sufficient as long as vma is detached before it is freed. The only place this is not currently happening is in exit_mmap(). Add the missing vma_mark_detached() in exit_mmap().
Another issue which might trick lock_vma_under_rcu() during vma reuse is vm_area_dup(), which copies the entire content of the vma into a new one, overriding new vma's vm_refcnt and temporarily making it appear as attached. This might trick a racing lock_vma_under_rcu() to operate on a reused vma if it found the vma before it got reused. To prevent this situation, we should ensure that vm_refcnt stays at detached state (0) when it is copied and advances to attached state only after it is added into the vma tree. Introduce vm_area_init_from() which preserves new vma's vm_refcnt and use it in vm_area_dup(). Since all vmas are in detached state with no current readers when they are freed, lock_vma_under_rcu() will not be able to take vm_refcnt after vma got detached even if vma is reused.
Finally, make vm_area_cachep SLAB_TYPESAFE_BY_RCU. This will facilitate vm_area_struct reuse and will minimize the number of call_rcu() calls.
--- include/linux/mm.h | 2 - include/linux/mm_types.h | 13 ++++-- include/linux/slab.h | 6 --- kernel/fork.c | 73 ++++++++++++++++++++------------ mm/mmap.c | 3 +- mm/vma.c | 11 ++--- mm/vma.h | 2 +- tools/testing/vma/vma_internal.h | 7 +-- 8 files changed, 63 insertions(+), 54 deletions(-)
include/linux/mm.h +0 -2
@@ -258,8 +258,6 @@ void setup_initial_init_mm(void *start_code, void *end_code,struct vm_area_struct *vm_area_alloc (struct mm_struct *);struct vm_area_struct *vm_area_dup (struct vm_area_struct *);void vm_area_free (struct vm_area_struct *); -/* Use only if VMA has no other users */ -void __vm_area_free (struct vm_area_struct *vma);#ifndef CONFIG_MMU extern struct rb_root nommu_region_tree;
include/linux/mm_types.h +10 -3
@@ -574,6 +574,12 @@ static inline void *folio_get_private(struct folio *folio)typedef unsigned long vm_flags_t ; +/* +* freeptr_t represents a SLUB freelist pointer, which might be encoded +* and not dereferenceable if CONFIG_SLAB_FREELIST_HARDENED is enabled. +*/ +typedef struct {unsigned long v; }freeptr_t ; +/* * A region containing a mapping of a non-memory backed file under NOMMU * conditions. These are held in a global tree and are pinned by the VMAs that
@@ -677,6 +683,9 @@ struct vma_numab_state {* * Only explicitly marked struct members may be accessed by RCU readers before * getting a stable reference. +* +* WARNING: when adding new members, please update vm_area_init_from() to copy +* them during vm_area_struct content duplication. */ struct vm_area_struct {/* The first cache line has the info for VMA tree walking. */
@@ -687,9 +696,7 @@ struct vm_area_struct {unsigned long vm_start;unsigned long vm_end; }; -#ifdef CONFIG_PER_VMA_LOCK -struct rcu_head vm_rcu;/* Used for deferred freeing. */ -#endif +freeptr_t vm_freeptr;/* Pointer used by SLAB_TYPESAFE_BY_RCU */ };/*
include/linux/slab.h +0 -6
@@ -234,12 +234,6 @@ enum _slab_flag_bits {#define SLAB_NO_OBJ_EXT __SLAB_FLAG_UNUSED#endif -/* -* freeptr_t represents a SLUB freelist pointer, which might be encoded -* and not dereferenceable if CONFIG_SLAB_FREELIST_HARDENED is enabled. -*/ -typedef struct {unsigned long v; }freeptr_t ; -/* * ZERO_SIZE_PTR will be returned for zero sized kmalloc requests. *
kernel/fork.c +46 -27
109 lines not on this page · raw message
mm/mmap.c +2 -1
@@ -1305,7 +1305,8 @@ void exit_mmap(struct mm_struct *mm) do { if (vma->vm_flags & VM_ACCOUNT) nr_accounted += vma_pages(vma); - remove_vma(vma, /* unreachable = */ true); + vma_mark_detached(vma); + remove_vma(vma); count++; cond_resched(); vma = vma_next(&vmi);
mm/vma.c +3 -8
@@ -406,19 +406,14 @@ static bool can_vma_merge_right(struct vma_merge_struct *vmg,/* * Close a vm structure and free it. */ -void remove_vma (struct vm_area_struct *vma,bool unreachable) +void remove_vma (struct vm_area_struct *vma) {might_sleep ();vma_close (vma);if (vma->vm_file)fput (vma->vm_file);mpol_put (vma_policy (vma)); -if (unreachable) { -vma_mark_detached (vma); -__vm_area_free (vma); - }else { -vm_area_free (vma); - } +vm_area_free (vma); }/*
@@ -1201,7 +1196,7 @@ static void vms_complete_munmap_vmas(struct vma_munmap_struct *vms,/* Remove and clean up vmas */ mas_set (mas_detach,0 );mas_for_each (mas_detach, vma,ULONG_MAX ) -remove_vma (vma,/* unreachable = */ false); +remove_vma (vma);vm_unacct_memory (vms->nr_accounted);validate_mm (mm);
mm/vma.h +1 -1
@@ -170,7 +170,7 @@ int do_vmi_munmap(struct vma_iterator *vmi, struct mm_struct *mm, unsigned long start, size_t len, struct list_head *uf, bool unlock); -void remove_vma (struct vm_area_struct *vma,bool unreachable); +void remove_vma (struct vm_area_struct *vma);void unmap_region (struct ma_state *mas,struct vm_area_struct *vma,struct vm_area_struct *prev,struct vm_area_struct *next);
tools/testing/vma/vma_internal.h +1 -6
@@ -697,14 +697,9 @@ static inline void mpol_put(struct mempolicy *) { } -static inline void __vm_area_free (struct vm_area_struct *vma) -{ -free (vma); -} -static inline void vm_area_free (struct vm_area_struct *vma) { -__vm_area_free (vma); +free (vma); }static inline void lru_add_drain (void )
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
Re: [PATCH v9 16/17] mm: make vma cache SLAB_TYPESAFE_BY_RCU
Quoted 33 lines · Suren Baghdasaryan
kernel/fork.c +25
-0@@ -449,6 +449,42 @@ struct vm_area_struct *vm_area_alloc(struct mm_struct *mm)return vma; } +static void vm_area_init_from (const struct vm_area_struct *src, +struct vm_area_struct *dest) +{ + dest->vm_mm = src->vm_mm; + dest->vm_ops = src->vm_ops; + dest->vm_start = src->vm_start; + dest->vm_end = src->vm_end; + dest->anon_vma = src->anon_vma; + dest->vm_pgoff = src->vm_pgoff; + dest->vm_file = src->vm_file; + dest->vm_private_data = src->vm_private_data; +vm_flags_init (dest, src->vm_flags); +memcpy (&dest->vm_page_prot, &src->vm_page_prot, +sizeof (dest->vm_page_prot)); +/* +* src->shared.rb may be modified concurrently when called from +* dup_mmap(), but the clone will reinitialize it. +*/ +data_race (memcpy (&dest->shared, &src->shared,sizeof (dest->shared))); +memcpy (&dest->vm_userfaultfd_ctx, &src->vm_userfaultfd_ctx, +sizeof (dest->vm_userfaultfd_ctx)); +#ifdef CONFIG_ANON_VMA_NAME + dest->anon_name = src->anon_name; +#endif +#ifdef CONFIG_SWAP
kernel/fork.c struct vm_area_struct *vm_area_alloc(struct mm_struct *mm) · line 486
Quoted 10 lines
kernel/fork.c +10
-0+memcpy (&dest->swap_readahead_info, &src->swap_readahead_info, +sizeof (dest->swap_readahead_info)); +#endif +#ifndef CONFIG_MMU + dest->vm_region = src->vm_region; +#endif +#ifdef CONFIG_NUMA + dest->vm_policy = src->vm_policy; +#endif +}
Would this be difficult to maintain? We should make sure not miss or overwrite anything.
Signature · 2 lines
Wei Yang Help you, Help me
Suren Baghdasaryan
maintainerRe: [PATCH v9 16/17] mm: make vma cache SLAB_TYPESAFE_BY_RCU
Quoted 41 lines · Wei Yang
Quoted 38 lines · Suren Baghdasaryan
kernel/fork.c +30
-0@@ -449,6 +449,42 @@ struct vm_area_struct *vm_area_alloc(struct mm_struct *mm)return vma; } +static void vm_area_init_from (const struct vm_area_struct *src, +struct vm_area_struct *dest) +{ + dest->vm_mm = src->vm_mm; + dest->vm_ops = src->vm_ops; + dest->vm_start = src->vm_start; + dest->vm_end = src->vm_end; + dest->anon_vma = src->anon_vma; + dest->vm_pgoff = src->vm_pgoff; + dest->vm_file = src->vm_file; + dest->vm_private_data = src->vm_private_data; +vm_flags_init (dest, src->vm_flags); +memcpy (&dest->vm_page_prot, &src->vm_page_prot, +sizeof (dest->vm_page_prot)); +/* +* src->shared.rb may be modified concurrently when called from +* dup_mmap(), but the clone will reinitialize it. +*/ +data_race (memcpy (&dest->shared, &src->shared,sizeof (dest->shared))); +memcpy (&dest->vm_userfaultfd_ctx, &src->vm_userfaultfd_ctx, +sizeof (dest->vm_userfaultfd_ctx)); +#ifdef CONFIG_ANON_VMA_NAME + dest->anon_name = src->anon_name; +#endif +#ifdef CONFIG_SWAP +memcpy (&dest->swap_readahead_info, &src->swap_readahead_info, +sizeof (dest->swap_readahead_info)); +#endif +#ifndef CONFIG_MMU + dest->vm_region = src->vm_region;
Quoted 8 lines
kernel/fork.c struct vm_area_struct *vm_area_alloc(struct mm_struct *mm) · line 486
Quoted 5 lines
kernel/fork.c +5
-0+#endif +#ifdef CONFIG_NUMA + dest->vm_policy = src->vm_policy; +#endif +}Would this be difficult to maintain? We should make sure not miss or overwrite anything.
Yeah, it is less maintainable than a simple memcpy() but I did not find a better alternative. I added a warning above the struct vm_area_struct definition to update this function every time we change that structure. Not sure if there is anything else I can do to help with this.
Quoted 4 lines
--
Wei Yang
Help you, Help me
Re: [PATCH v9 16/17] mm: make vma cache SLAB_TYPESAFE_BY_RCU
Quoted 51 lines · Suren Baghdasaryan
Quoted 41 lines · Wei Yang
Quoted 38 lines · Suren Baghdasaryan
kernel/fork.c +30
-0@@ -449,6 +449,42 @@ struct vm_area_struct *vm_area_alloc(struct mm_struct *mm)return vma; } +static void vm_area_init_from (const struct vm_area_struct *src, +struct vm_area_struct *dest) +{ + dest->vm_mm = src->vm_mm; + dest->vm_ops = src->vm_ops; + dest->vm_start = src->vm_start; + dest->vm_end = src->vm_end; + dest->anon_vma = src->anon_vma; + dest->vm_pgoff = src->vm_pgoff; + dest->vm_file = src->vm_file; + dest->vm_private_data = src->vm_private_data; +vm_flags_init (dest, src->vm_flags); +memcpy (&dest->vm_page_prot, &src->vm_page_prot, +sizeof (dest->vm_page_prot)); +/* +* src->shared.rb may be modified concurrently when called from +* dup_mmap(), but the clone will reinitialize it. +*/ +data_race (memcpy (&dest->shared, &src->shared,sizeof (dest->shared))); +memcpy (&dest->vm_userfaultfd_ctx, &src->vm_userfaultfd_ctx, +sizeof (dest->vm_userfaultfd_ctx)); +#ifdef CONFIG_ANON_VMA_NAME + dest->anon_name = src->anon_name; +#endif +#ifdef CONFIG_SWAP +memcpy (&dest->swap_readahead_info, &src->swap_readahead_info, +sizeof (dest->swap_readahead_info)); +#endif +#ifndef CONFIG_MMU + dest->vm_region = src->vm_region;Quoted 8 lines
kernel/fork.c struct vm_area_struct *vm_area_alloc(struct mm_struct *mm) · line 486
Quoted 5 lines
kernel/fork.c +5
-0+#endif +#ifdef CONFIG_NUMA + dest->vm_policy = src->vm_policy; +#endif +}Would this be difficult to maintain? We should make sure not miss or overwrite anything.
Quoted 5 lines
Yeah, it is less maintainable than a simple
memcpy()but I did not find a better alternative. I added a warning above the structvm_area_structdefinition to update this function every time we change that structure. Not sure if there is anything else I can do to help with this.
Here's a horrible idea.. if we put the ref count at the end or start of the struct, we could set the ref count to zero and copy the other area in one mmecpy().
Even worse idea, we could use a pointer_of like macro to get the position of the ref count in the vma struct, set the ref count to zero and carefully copy the other two parts in two memcpy() operations.
Feel free to disregard these ideas as it is late here and I'm having fun thinking up bad ways to make this "more" maintainable.
Either of these would make updating the struct easier, but very painful to debug when it goes wrong (or reading the function).
Thanks,
Liam
Suren Baghdasaryan
maintainerRe: [PATCH v9 16/17] mm: make vma cache SLAB_TYPESAFE_BY_RCU
Quoted 59 lines · Liam R. Howlett
Quoted 51 lines · Suren Baghdasaryan
Quoted 41 lines · Wei Yang
Quoted 38 lines · Suren Baghdasaryan
kernel/fork.c +30
-0@@ -449,6 +449,42 @@ struct vm_area_struct *vm_area_alloc(struct mm_struct *mm)return vma; } +static void vm_area_init_from (const struct vm_area_struct *src, +struct vm_area_struct *dest) +{ + dest->vm_mm = src->vm_mm; + dest->vm_ops = src->vm_ops; + dest->vm_start = src->vm_start; + dest->vm_end = src->vm_end; + dest->anon_vma = src->anon_vma; + dest->vm_pgoff = src->vm_pgoff; + dest->vm_file = src->vm_file; + dest->vm_private_data = src->vm_private_data; +vm_flags_init (dest, src->vm_flags); +memcpy (&dest->vm_page_prot, &src->vm_page_prot, +sizeof (dest->vm_page_prot)); +/* +* src->shared.rb may be modified concurrently when called from +* dup_mmap(), but the clone will reinitialize it. +*/ +data_race (memcpy (&dest->shared, &src->shared,sizeof (dest->shared))); +memcpy (&dest->vm_userfaultfd_ctx, &src->vm_userfaultfd_ctx, +sizeof (dest->vm_userfaultfd_ctx)); +#ifdef CONFIG_ANON_VMA_NAME + dest->anon_name = src->anon_name; +#endif +#ifdef CONFIG_SWAP +memcpy (&dest->swap_readahead_info, &src->swap_readahead_info, +sizeof (dest->swap_readahead_info)); +#endif +#ifndef CONFIG_MMU + dest->vm_region = src->vm_region;Quoted 8 lines
kernel/fork.c struct vm_area_struct *vm_area_alloc(struct mm_struct *mm) · line 486
Quoted 5 lines
kernel/fork.c +5
-0+#endif +#ifdef CONFIG_NUMA + dest->vm_policy = src->vm_policy; +#endif +}Would this be difficult to maintain? We should make sure not miss or overwrite anything.
Quoted 5 lines
Yeah, it is less maintainable than a simple
memcpy()but I did not find a better alternative. I added a warning above the structvm_area_structdefinition to update this function every time we change that structure. Not sure if there is anything else I can do to help with this.
Quoted 7 lines
Here's a horrible idea.. if we put the ref count at the end or start of the struct, we could set the ref count to zero and copy the other area in one
mmecpy().Even worse idea, we could use a
pointer_oflike macro to get the position of the ref count in the vma struct, set the ref count to zero and carefully copy the other two parts in twomemcpy()operations.
I implemented this approach in v3 of this patchset here:
https://lore.kernel.org/all/[email protected]/
like this:
#define VMA_BEFORE_LOCK offsetof(struct vm_area_struct, vm_lock)
#define VMA_LOCK_END(vma) \
(((void *)(vma)) + offsetofend(struct vm_area_struct, vm_lock))#define VMA_AFTER_LOCK \
(sizeof(struct vm_area_struct) - offsetofend(structvm_area_struct, vm_lock))
static inline void vma_copy(struct vm_area_struct *new, struct vm_area_struct *orig)
{
/* Preserve vma->vm_lock */
data_race(memcpy(new, orig, VMA_BEFORE_LOCK));
data_race(memcpy(VMA_LOCK_END(new), VMA_LOCK_END(orig),VMA_AFTER_LOCK));
}
If this looks more maintainable I can revive it. Maybe introduce a more generic function to copy any structure excluding a specific field and use it like this:
copy_struct_except(new, orig, struct vm_area_struct, vm_refcnt);
Would that be better?
Quoted 9 lines
Feel free to disregard these ideas as it is late here and I'm having fun thinking up bad ways to make this "more" maintainable.
Either of these would make updating the struct easier, but very painful to debug when it goes wrong (or reading the function).
Thanks,
Liam
Re: [PATCH v9 16/17] mm: make vma cache SLAB_TYPESAFE_BY_RCU
In reply to Suren BaghdasaryanQuoted 16 lines · Suren Baghdasaryan
Quoted 14 lines · Wei Yang
Quoted 11 lines · Suren Baghdasaryan
kernel/fork.c +3
-0@@ -449,6 +449,42 @@ struct vm_area_struct *vm_area_alloc(struct mm_struct *mm)return vma; } +static void vm_area_init_from (const struct vm_area_struct *src, +struct vm_area_struct *dest) +{
[snip]
Quoted 9 lines
Quoted 2 lines
Would this be difficult to maintain? We should make sure not miss or overwrite anything.
Yeah, it is less maintainable than a simple
memcpy()but I did not find a better alternative. I added a warning above the structvm_area_structdefinition to update this function every time we change that structure. Not sure if there is anything else I can do to help with this.
Bare minimum this could have a BUILD_BUG_ON in below the func for the known-covered size. But it would have to be conditional on arch and some macros, somewhat nasty.
KASAN or KMSAN (I don't remember which) can be used to find missing copies. To that end the target struct could be marked as fully uninitialized before copy and have a full read performed from it afterwards -- guaranteed to trip over any field which any field not explicitly covered (including padding though). I don't know what magic macros can be used to do in Linux, I am saying the support to get this result is there. I understand most people don't use this, but this still should be enough to trip over buggy patches in -next.
Finally, the struct could have macros delimiting copy/non-copy sections (with macros expanding to field names), for illustrative purposes:
include/linux/mm_types.h +2 -0
@@ -677,6 +677,7 @@ struct vma_numab_state {* getting a stable reference. */ struct vm_area_struct { +#define vma_start_copy0 vm_rcu/* The first cache line has the info for VMA tree walking. */ union {
@@ -731,6 +732,7 @@ struct vm_area_struct {/* Unstable RCU readers are allowed to read this. */ struct vma_lock *vm_lock;#endif +#define vma_end_copy1 vm_lock/* * For areas with an address space and backing store,
then you would do everything with a series of calls
however, the __randomize_layout annotation whacks that idea (maybe it can be retired?)
Signature · 1 line
Mateusz Guzik <mjguzik gmail.com>
Suren Baghdasaryan
maintainerRe: [PATCH v9 16/17] mm: make vma cache SLAB_TYPESAFE_BY_RCU
Quoted 33 lines · Mateusz Guzik
Quoted 16 lines · Suren Baghdasaryan
Quoted 14 lines · Wei Yang
Quoted 11 lines · Suren Baghdasaryan
kernel/fork.c +3
-0@@ -449,6 +449,42 @@ struct vm_area_struct *vm_area_alloc(struct mm_struct *mm)return vma; } +static void vm_area_init_from (const struct vm_area_struct *src, +struct vm_area_struct *dest) +{[snip]
Quoted 9 lines
Quoted 2 lines
Would this be difficult to maintain? We should make sure not miss or overwrite anything.
Yeah, it is less maintainable than a simple
memcpy()but I did not find a better alternative. I added a warning above the structvm_area_structdefinition to update this function every time we change that structure. Not sure if there is anything else I can do to help with this.Bare minimum this could have a
BUILD_BUG_ONin below the func for the known-covered size. But it would have to be conditional on arch and some macros, somewhat nasty.
Quoted 8 lines
KASAN or KMSAN (I don't remember which) can be used to find missing copies. To that end the target struct could be marked as fully uninitialized before copy and have a full read performed from it afterwards -- guaranteed to trip over any field which any field not explicitly covered (including padding though). I don't know what magic macros can be used to do in Linux, I am saying the support to get this result is there. I understand most people don't use this, but this still should be enough to trip over buggy patches in -next.
If my previous suggestion does not fly I'll start digging into KASAN to see how we can use it. Thanks for the tip.
Quoted 25 lines
Finally, the struct could have macros delimiting copy/non-copy sections (with macros expanding to field names), for illustrative purposes:
include/linux/mm_types.h +2
-0@@ -677,6 +677,7 @@ struct vma_numab_state {* getting a stable reference. */ struct vm_area_struct { +#define vma_start_copy0 vm_rcu/* The first cache line has the info for VMA tree walking. */ union {@@ -731,6 +732,7 @@ struct vm_area_struct {/* Unstable RCU readers are allowed to read this. */ struct vma_lock *vm_lock;#endif +#define vma_end_copy1 vm_lock/* * For areas with an address space and backing store,
Quoted 1 line
then you would do everything with a series of calls
I'm not sure... My proposed approach with offsetof() I think is a bit cleaner than adding macros to denote copy sections. WDYT?
Quoted 6 lines
however, the
__randomize_layoutannotation whacks that idea (maybe it can be retired?)--
Mateusz Guzik <mjguzik gmail.com>
Re: [PATCH v9 16/17] mm: make vma cache SLAB_TYPESAFE_BY_RCU
Quoted 69 lines · Suren Baghdasaryan
Quoted 33 lines · Mateusz Guzik
Quoted 16 lines · Suren Baghdasaryan
Quoted 14 lines · Wei Yang
Quoted 11 lines · Suren Baghdasaryan
kernel/fork.c +3
-0@@ -449,6 +449,42 @@ struct vm_area_struct *vm_area_alloc(struct mm_struct *mm)return vma; } +static void vm_area_init_from (const struct vm_area_struct *src, +struct vm_area_struct *dest) +{[snip]
Quoted 9 lines
Quoted 2 lines
Would this be difficult to maintain? We should make sure not miss or overwrite anything.
Yeah, it is less maintainable than a simple
memcpy()but I did not find a better alternative. I added a warning above the structvm_area_structdefinition to update this function every time we change that structure. Not sure if there is anything else I can do to help with this.Bare minimum this could have a
BUILD_BUG_ONin below the func for the known-covered size. But it would have to be conditional on arch and some macros, somewhat nasty.Quoted 8 lines
KASAN or KMSAN (I don't remember which) can be used to find missing copies. To that end the target struct could be marked as fully uninitialized before copy and have a full read performed from it afterwards -- guaranteed to trip over any field which any field not explicitly covered (including padding though). I don't know what magic macros can be used to do in Linux, I am saying the support to get this result is there. I understand most people don't use this, but this still should be enough to trip over buggy patches in -next.
If my previous suggestion does not fly I'll start digging into KASAN to see how we can use it. Thanks for the tip.
Quoted 22 lines
Finally, the struct could have macros delimiting copy/non-copy sections (with macros expanding to field names), for illustrative purposes:
include/linux/mm_types.h +2
-0@@ -677,6 +677,7 @@ struct vma_numab_state {* getting a stable reference. */ struct vm_area_struct { +#define vma_start_copy0 vm_rcu/* The first cache line has the info for VMA tree walking. */ union {@@ -731,6 +732,7 @@ struct vm_area_struct {/* Unstable RCU readers are allowed to read this. */ struct vma_lock *vm_lock;#endif +#define vma_end_copy1 vm_lock
Quoted 8 lines
Quoted 4 lines
include/linux/mm_types.h +0
-0/* * For areas with an address space and backing store, then you would do everything with a series of calls
I'm not sure... My proposed approach with
offsetof()I think is a bit cleaner than adding macros to denote copy sections. WDYT?
another non-copy field may show up down the road and then the person adding it is going to be a sad panda. wont happen if the "infra" is there.
but I concede this is not a big deal and i'm not going to bikeshed about it.
Signature · 1 line
Mateusz Guzik <mjguzik gmail.com>
Suren Baghdasaryan
maintainerRe: [PATCH v9 16/17] mm: make vma cache SLAB_TYPESAFE_BY_RCU
Quoted 80 lines · Mateusz Guzik
Quoted 69 lines · Suren Baghdasaryan
Quoted 33 lines · Mateusz Guzik
Quoted 16 lines · Suren Baghdasaryan
Quoted 14 lines · Wei Yang
Quoted 11 lines · Suren Baghdasaryan
kernel/fork.c +3
-0@@ -449,6 +449,42 @@ struct vm_area_struct *vm_area_alloc(struct mm_struct *mm)return vma; } +static void vm_area_init_from (const struct vm_area_struct *src, +struct vm_area_struct *dest) +{[snip]
Quoted 9 lines
Quoted 2 lines
Would this be difficult to maintain? We should make sure not miss or overwrite anything.
Yeah, it is less maintainable than a simple
memcpy()but I did not find a better alternative. I added a warning above the structvm_area_structdefinition to update this function every time we change that structure. Not sure if there is anything else I can do to help with this.Bare minimum this could have a
BUILD_BUG_ONin below the func for the known-covered size. But it would have to be conditional on arch and some macros, somewhat nasty.Quoted 8 lines
KASAN or KMSAN (I don't remember which) can be used to find missing copies. To that end the target struct could be marked as fully uninitialized before copy and have a full read performed from it afterwards -- guaranteed to trip over any field which any field not explicitly covered (including padding though). I don't know what magic macros can be used to do in Linux, I am saying the support to get this result is there. I understand most people don't use this, but this still should be enough to trip over buggy patches in -next.
If my previous suggestion does not fly I'll start digging into KASAN to see how we can use it. Thanks for the tip.
Quoted 22 lines
Finally, the struct could have macros delimiting copy/non-copy sections (with macros expanding to field names), for illustrative purposes:
include/linux/mm_types.h +2
-0@@ -677,6 +677,7 @@ struct vma_numab_state {* getting a stable reference. */ struct vm_area_struct { +#define vma_start_copy0 vm_rcu/* The first cache line has the info for VMA tree walking. */ union {@@ -731,6 +732,7 @@ struct vm_area_struct {/* Unstable RCU readers are allowed to read this. */ struct vma_lock *vm_lock;#endif +#define vma_end_copy1 vm_lockQuoted 8 lines
Quoted 4 lines
include/linux/mm_types.h +0
-0/* * For areas with an address space and backing store, then you would do everything with a series of calls
I'm not sure... My proposed approach with
offsetof()I think is a bit cleaner than adding macros to denote copy sections. WDYT?
Quoted 5 lines
another non-copy field may show up down the road and then the person adding it is going to be a sad panda. wont happen if the "infra" is there.
but I concede this is not a big deal and i'm not going to bikeshed about it.
Yeah, I can't think of a perfect solution. I think we should pick a sane one and if requirements change we can change the implementation of vm_area_init_from() accordingly.
Quoted 3 lines
--
Mateusz Guzik <mjguzik gmail.com>
Re: [PATCH v9 16/17] mm: make vma cache SLAB_TYPESAFE_BY_RCU
In reply to Suren BaghdasaryanQuoted 51 lines · Suren Baghdasaryan
Quoted 41 lines · Wei Yang
Quoted 38 lines · Suren Baghdasaryan
kernel/fork.c +30
-0@@ -449,6 +449,42 @@ struct vm_area_struct *vm_area_alloc(struct mm_struct *mm)return vma; } +static void vm_area_init_from (const struct vm_area_struct *src, +struct vm_area_struct *dest) +{ + dest->vm_mm = src->vm_mm; + dest->vm_ops = src->vm_ops; + dest->vm_start = src->vm_start; + dest->vm_end = src->vm_end; + dest->anon_vma = src->anon_vma; + dest->vm_pgoff = src->vm_pgoff; + dest->vm_file = src->vm_file; + dest->vm_private_data = src->vm_private_data; +vm_flags_init (dest, src->vm_flags); +memcpy (&dest->vm_page_prot, &src->vm_page_prot, +sizeof (dest->vm_page_prot)); +/* +* src->shared.rb may be modified concurrently when called from +* dup_mmap(), but the clone will reinitialize it. +*/ +data_race (memcpy (&dest->shared, &src->shared,sizeof (dest->shared))); +memcpy (&dest->vm_userfaultfd_ctx, &src->vm_userfaultfd_ctx, +sizeof (dest->vm_userfaultfd_ctx)); +#ifdef CONFIG_ANON_VMA_NAME + dest->anon_name = src->anon_name; +#endif +#ifdef CONFIG_SWAP +memcpy (&dest->swap_readahead_info, &src->swap_readahead_info, +sizeof (dest->swap_readahead_info)); +#endif +#ifndef CONFIG_MMU + dest->vm_region = src->vm_region;Quoted 8 lines
kernel/fork.c struct vm_area_struct *vm_area_alloc(struct mm_struct *mm) · line 486
Quoted 5 lines
kernel/fork.c +5
-0+#endif +#ifdef CONFIG_NUMA + dest->vm_policy = src->vm_policy; +#endif +}Would this be difficult to maintain? We should make sure not miss or overwrite anything.
Quoted 2 lines
Yeah, it is less maintainable than a simple
memcpy()but I did not find a better alternative.
Willy knows one but refuses to share it :(
Quoted 9 lines
I added a warning above the struct
vm_area_structdefinition to update this function every time we change that structure. Not sure if there is anything else I can do to help with this.Quoted 4 lines
--
Wei Yang
Help you, Help me
Suren Baghdasaryan
maintainerRe: [PATCH v9 16/17] mm: make vma cache SLAB_TYPESAFE_BY_RCU
Quoted 53 lines · Vlastimil Babka
Quoted 51 lines · Suren Baghdasaryan
Quoted 49 lines · Wei Yang
Quoted 33 lines · Suren Baghdasaryan
kernel/fork.c +25
-0@@ -449,6 +449,42 @@ struct vm_area_struct *vm_area_alloc(struct mm_struct *mm)return vma; } +static void vm_area_init_from (const struct vm_area_struct *src, +struct vm_area_struct *dest) +{ + dest->vm_mm = src->vm_mm; + dest->vm_ops = src->vm_ops; + dest->vm_start = src->vm_start; + dest->vm_end = src->vm_end; + dest->anon_vma = src->anon_vma; + dest->vm_pgoff = src->vm_pgoff; + dest->vm_file = src->vm_file; + dest->vm_private_data = src->vm_private_data; +vm_flags_init (dest, src->vm_flags); +memcpy (&dest->vm_page_prot, &src->vm_page_prot, +sizeof (dest->vm_page_prot)); +/* +* src->shared.rb may be modified concurrently when called from +* dup_mmap(), but the clone will reinitialize it. +*/ +data_race (memcpy (&dest->shared, &src->shared,sizeof (dest->shared))); +memcpy (&dest->vm_userfaultfd_ctx, &src->vm_userfaultfd_ctx, +sizeof (dest->vm_userfaultfd_ctx)); +#ifdef CONFIG_ANON_VMA_NAME + dest->anon_name = src->anon_name; +#endif +#ifdef CONFIG_SWAP kernel/fork.c struct vm_area_struct *vm_area_alloc(struct mm_struct *mm) · line 486
Quoted 10 lines
kernel/fork.c +10
-0+memcpy (&dest->swap_readahead_info, &src->swap_readahead_info, +sizeof (dest->swap_readahead_info)); +#endif +#ifndef CONFIG_MMU + dest->vm_region = src->vm_region; +#endif +#ifdef CONFIG_NUMA + dest->vm_policy = src->vm_policy; +#endif +}Would this be difficult to maintain? We should make sure not miss or overwrite anything.
Quoted 4 lines
Quoted 2 lines
Yeah, it is less maintainable than a simple
memcpy()but I did not find a better alternative.Willy knows one but refuses to share it :(
Ah, that reminds me why I dropped this approach :) But to be honest, back then we also had vma_clear() and that added to the ugliness. Now I could simply to this without all those macros:
static inline void vma_copy(struct vm_area_struct *new,
struct vm_area_struct *orig)
{
/* Copy the vma while preserving vma->vm_lock */ data_race(memcpy(new, orig, offsetof(struct vm_area_struct, vm_lock))); data_race(memcpy(new + offsetofend(struct vm_area_struct, vm_lock),
orig + offsetofend(struct vm_area_struct, vm_lock), sizeof(struct vm_area_struct) -
offsetofend(struct vm_area_struct, vm_lock));
}
Would that be better than the current approach?
Quoted 11 lines
Quoted 10 lines
I added a warning above the struct
vm_area_structdefinition to update this function every time we change that structure. Not sure if there is anything else I can do to help with this.Quoted 4 lines
--
Wei Yang
Help you, Help me
Suren Baghdasaryan
maintainerRe: [PATCH v9 16/17] mm: make vma cache SLAB_TYPESAFE_BY_RCU
Quoted 75 lines · Suren Baghdasaryan
Quoted 53 lines · Vlastimil Babka
Quoted 51 lines · Suren Baghdasaryan
Quoted 49 lines · Wei Yang
Quoted 33 lines · Suren Baghdasaryan
kernel/fork.c +25
-0@@ -449,6 +449,42 @@ struct vm_area_struct *vm_area_alloc(struct mm_struct *mm)return vma; } +static void vm_area_init_from (const struct vm_area_struct *src, +struct vm_area_struct *dest) +{ + dest->vm_mm = src->vm_mm; + dest->vm_ops = src->vm_ops; + dest->vm_start = src->vm_start; + dest->vm_end = src->vm_end; + dest->anon_vma = src->anon_vma; + dest->vm_pgoff = src->vm_pgoff; + dest->vm_file = src->vm_file; + dest->vm_private_data = src->vm_private_data; +vm_flags_init (dest, src->vm_flags); +memcpy (&dest->vm_page_prot, &src->vm_page_prot, +sizeof (dest->vm_page_prot)); +/* +* src->shared.rb may be modified concurrently when called from +* dup_mmap(), but the clone will reinitialize it. +*/ +data_race (memcpy (&dest->shared, &src->shared,sizeof (dest->shared))); +memcpy (&dest->vm_userfaultfd_ctx, &src->vm_userfaultfd_ctx, +sizeof (dest->vm_userfaultfd_ctx)); +#ifdef CONFIG_ANON_VMA_NAME + dest->anon_name = src->anon_name; +#endif +#ifdef CONFIG_SWAP kernel/fork.c struct vm_area_struct *vm_area_alloc(struct mm_struct *mm) · line 486
Quoted 10 lines
kernel/fork.c +10
-0+memcpy (&dest->swap_readahead_info, &src->swap_readahead_info, +sizeof (dest->swap_readahead_info)); +#endif +#ifndef CONFIG_MMU + dest->vm_region = src->vm_region; +#endif +#ifdef CONFIG_NUMA + dest->vm_policy = src->vm_policy; +#endif +}Would this be difficult to maintain? We should make sure not miss or overwrite anything.
Quoted 4 lines
Quoted 2 lines
Yeah, it is less maintainable than a simple
memcpy()but I did not find a better alternative.Willy knows one but refuses to share it :(
Ah, that reminds me why I dropped this approach :) But to be honest, back then we also had
vma_clear()and that added to the ugliness. Now I could simply to this without all those macros:static inline void vma_copy(struct
vm_area_struct*new,
structvm_area_struct*orig)
{
/* Copy the vma while preservingvma->vm_lock*/data_race(memcpy(new, orig, offsetof(struct vm_area_struct, vm_lock))); data_race(memcpy(new +offsetofend(struct vm_area_struct, vm_lock),
orig +offsetofend(struct vm_area_struct, vm_lock),sizeof(struct vm_area_struct)-
offsetofend(struct vm_area_struct, vm_lock));
}
Quoted 1 line
Would that be better than the current approach?
I discussed proposed alternatives with Willy and he prefers the current field-by-field copy approach. I also tried using kmsan_check_memory() to check for uninitialized memory in the vm_area_struct but unfortunately KMSAN stumbles on the holes in this structure and there are 4 of them (I attached pahole output at the end of this email). I tried unpoisoning holes but that gets very ugly very fast. So, I posted v10
(https://lore.kernel.org/all/[email protected]/)
without changing this part.
struct vm_area_struct {
union {
struct {
unsigned long vm_start; /* 0 8 */
unsigned long vm_end; /* 8 8 */
}; /* 0 16 */
freeptr_t vm_freeptr; /* 0 8 */
}; /* 0 16 */
union {
struct {
unsigned long vm_start; /*
0 8 */
unsigned long vm_end; /*
8 8 */
}; /* 0 16 */
freeptr_t vm_freeptr; /*
0 8 */
};
struct mm_struct * vm_mm; /* 16 8 */
pgprot_t vm_page_prot; /* 24 8 */
union {
const vm_flags_t vm_flags; /* 32 8 */
vm_flags_t __vm_flags; /* 32 8 */
}; /* 32 8 */
union {
const vm_flags_t vm_flags; /*
0 8 */
vm_flags_t __vm_flags; /*
0 8 */
};
unsigned int vm_lock_seq; /* 40 4 */
/* XXX 4 bytes hole, try to pack */
struct list_head anon_vma_chain; /* 48 16 */
/* --- cacheline 1 boundary (64 bytes) --- */
struct anon_vma * anon_vma; /* 64 8 */
const struct vm_operations_struct * vm_ops; /* 72 8 */
unsigned long vm_pgoff; /* 80 8 */
struct file * vm_file; /* 88 8 */
void * vm_private_data; /* 96 8 */
atomic_long_t swap_readahead_info; /* 104 8 */
struct mempolicy * vm_policy; /* 112 8 */
/* XXX 8 bytes hole, try to pack */
/* --- cacheline 2 boundary (128 bytes) --- */
refcount_t vm_refcnt
__attribute__((__aligned__(64))); /* 128 4 */
/* XXX 4 bytes hole, try to pack */
struct {
struct rb_node rb __attribute__((__aligned__(8)));
/* 136 24 */
unsigned long rb_subtree_last; /* 160 8 */
} shared; /* 136 32 */ struct {
struct rb_node rb
__attribute__((__aligned__(8))); /* 0 24 */
unsigned long rb_subtree_last; /*
24 8 */
/* size: 32, cachelines: 1, members: 2 */
/* forced alignments: 1 */
/* last cacheline: 32 bytes */
};
struct vm_userfaultfd_ctx vm_userfaultfd_ctx; /* 168 0 */
/* size: 192, cachelines: 3, members: 16 */
/* sum members: 152, holes: 3, sum holes: 16 */
/* padding: 24 */
/* forced alignments: 1, forced holes: 1, sum forced holes: 8 */
};Quoted 12 lines
Quoted 12 lines
Quoted 11 lines
I added a warning above the struct
vm_area_structdefinition to update this function every time we change that structure. Not sure if there is anything else I can do to help with this.Quoted 4 lines
--
Wei Yang
Help you, Help me
Re: [PATCH v9 16/17] mm: make vma cache SLAB_TYPESAFE_BY_RCU
In reply to Suren BaghdasaryanQuoted 51 lines · Suren Baghdasaryan
Quoted 41 lines · Wei Yang
Quoted 38 lines · Suren Baghdasaryan
kernel/fork.c +30
-0@@ -449,6 +449,42 @@ struct vm_area_struct *vm_area_alloc(struct mm_struct *mm)return vma; } +static void vm_area_init_from (const struct vm_area_struct *src, +struct vm_area_struct *dest) +{ + dest->vm_mm = src->vm_mm; + dest->vm_ops = src->vm_ops; + dest->vm_start = src->vm_start; + dest->vm_end = src->vm_end; + dest->anon_vma = src->anon_vma; + dest->vm_pgoff = src->vm_pgoff; + dest->vm_file = src->vm_file; + dest->vm_private_data = src->vm_private_data; +vm_flags_init (dest, src->vm_flags); +memcpy (&dest->vm_page_prot, &src->vm_page_prot, +sizeof (dest->vm_page_prot)); +/* +* src->shared.rb may be modified concurrently when called from +* dup_mmap(), but the clone will reinitialize it. +*/ +data_race (memcpy (&dest->shared, &src->shared,sizeof (dest->shared))); +memcpy (&dest->vm_userfaultfd_ctx, &src->vm_userfaultfd_ctx, +sizeof (dest->vm_userfaultfd_ctx)); +#ifdef CONFIG_ANON_VMA_NAME + dest->anon_name = src->anon_name; +#endif +#ifdef CONFIG_SWAP +memcpy (&dest->swap_readahead_info, &src->swap_readahead_info, +sizeof (dest->swap_readahead_info)); +#endif +#ifndef CONFIG_MMU + dest->vm_region = src->vm_region;Quoted 8 lines
kernel/fork.c struct vm_area_struct *vm_area_alloc(struct mm_struct *mm) · line 486
Quoted 5 lines
kernel/fork.c +5
-0+#endif +#ifdef CONFIG_NUMA + dest->vm_policy = src->vm_policy; +#endif +}Would this be difficult to maintain? We should make sure not miss or overwrite anything.
Quoted 6 lines
Yeah, it is less maintainable than a simple
memcpy()but I did not find a better alternative. I added a warning above the structvm_area_structdefinition to update this function every time we change that structure. Not sure if there is anything else I can do to help with this.
For !PER_VMA_LOCK, maybe we can use memcpy() as usual.
For PER_VMA_LOCK, I just come up the same idea with you:-)
Quoted 4 lines
Quoted 4 lines
--
Wei Yang
Help you, Help me
Signature · 2 lines
Wei Yang Help you, Help me
Suren Baghdasaryan
maintainerRe: [PATCH v9 16/17] mm: make vma cache SLAB_TYPESAFE_BY_RCU
Quoted 60 lines · Wei Yang
Quoted 51 lines · Suren Baghdasaryan
Quoted 41 lines · Wei Yang
Quoted 38 lines · Suren Baghdasaryan
kernel/fork.c +30
-0@@ -449,6 +449,42 @@ struct vm_area_struct *vm_area_alloc(struct mm_struct *mm)return vma; } +static void vm_area_init_from (const struct vm_area_struct *src, +struct vm_area_struct *dest) +{ + dest->vm_mm = src->vm_mm; + dest->vm_ops = src->vm_ops; + dest->vm_start = src->vm_start; + dest->vm_end = src->vm_end; + dest->anon_vma = src->anon_vma; + dest->vm_pgoff = src->vm_pgoff; + dest->vm_file = src->vm_file; + dest->vm_private_data = src->vm_private_data; +vm_flags_init (dest, src->vm_flags); +memcpy (&dest->vm_page_prot, &src->vm_page_prot, +sizeof (dest->vm_page_prot)); +/* +* src->shared.rb may be modified concurrently when called from +* dup_mmap(), but the clone will reinitialize it. +*/ +data_race (memcpy (&dest->shared, &src->shared,sizeof (dest->shared))); +memcpy (&dest->vm_userfaultfd_ctx, &src->vm_userfaultfd_ctx, +sizeof (dest->vm_userfaultfd_ctx)); +#ifdef CONFIG_ANON_VMA_NAME + dest->anon_name = src->anon_name; +#endif +#ifdef CONFIG_SWAP +memcpy (&dest->swap_readahead_info, &src->swap_readahead_info, +sizeof (dest->swap_readahead_info)); +#endif +#ifndef CONFIG_MMU + dest->vm_region = src->vm_region;Quoted 8 lines
kernel/fork.c struct vm_area_struct *vm_area_alloc(struct mm_struct *mm) · line 486
Quoted 5 lines
kernel/fork.c +5
-0+#endif +#ifdef CONFIG_NUMA + dest->vm_policy = src->vm_policy; +#endif +}Would this be difficult to maintain? We should make sure not miss or overwrite anything.
Quoted 6 lines
Yeah, it is less maintainable than a simple
memcpy()but I did not find a better alternative. I added a warning above the structvm_area_structdefinition to update this function every time we change that structure. Not sure if there is anything else I can do to help with this.
Quoted 3 lines
For !PER_VMA_LOCK, maybe we can use
memcpy()as usual.For
PER_VMA_LOCK, I just come up the same idea with you:-)
Missed this comment. Yeah, in one of the previous versions I had different !PER_VMA_LOCK and PER_VMA_LOCK versions of vma_copy() but David raised a question whether it is worth having two versions. From performance POV there is no reason for that and it unnecessarily complicates the code. So, I dropped that in favor of having one version.
Quoted 9 lines
Quoted 5 lines
Quoted 5 lines
--
Wei Yang
Help you, Help me--
Wei Yang
Help you, Help me
Suren Baghdasaryan
maintainerPATCH v9 17/17 docs/mm: document latest changes to vm_lock
In reply to Suren BaghdasaryanChange the documentation to reflect that vm_lock is integrated into vma and replaced with vm_refcnt.
Document newly introduced vma_start_read_locked{_nested} functions.
--- Documentation/mm/process_addrs.rst | 44 ++++++++++++++++++------------ 1 file changed, 26 insertions(+), 18 deletions(-)
Documentation/mm/process_addrs.rst +26 -18
@@ -716,9 +716,14 @@ calls :c:func:`!rcu_read_lock` to ensure that the VMA is looked up in an RCU critical section, then attempts to VMA lock it via :c:func:`!vma_start_read`, before releasing the RCU lock via :c:func:`!rcu_read_unlock`. -VMA read locks hold the read lock on the :c:member:`!vma->vm_lock` semaphore for -their duration and the caller of :c:func:`!lock_vma_under_rcu` must release it -via :c:func:`!vma_end_read`. +In cases when the user already holds mmap read lock, :c:func:`!vma_start_read_locked` +and :c:func:`!vma_start_read_locked_nested` can be used. These functions do not +fail due to lock contention but the caller should still check their return values +in case they fail for other reasons. + +VMA read locks increment :c:member:`!vma.vm_refcnt` reference counter for their +duration and the caller of :c:func:`!lock_vma_under_rcu` must drop it via +:c:func:`!vma_end_read`. VMA **write** locks are acquired via :c:func:`!vma_start_write` in instances where a VMA is about to be modified, unlike :c:func:`!vma_start_read` the lock is always
@@ -726,9 +731,9 @@ acquired. An mmap write lock **must** be held for the duration of the VMA write lock, releasing or downgrading the mmap write lock also releases the VMA write lock so there is no :c:func:`!vma_end_write` function. -Note that a semaphore write lock is not held across a VMA lock. Rather, a -sequence number is used for serialisation, and the write semaphore is only -acquired at the point of write lock to update this. +Note that when write-locking a VMA lock, the :c:member:`!vma.vm_refcnt` is temporarily +modified so that readers can detect the presense of a writer. The reference counter is +restored once the vma sequence number used for serialisation is updated. This ensures the semantics we require - VMA write locks provide exclusive write access to the VMA.
@@ -738,7 +743,7 @@ Implementation details The VMA lock mechanism is designed to be a lightweight means of avoiding the use of the heavily contended mmap lock. It is implemented using a combination of a -read/write semaphore and sequence numbers belonging to the containing +reference counter and sequence numbers belonging to the containing :c:struct:`!struct mm_struct` and the VMA. Read locks are acquired via :c:func:`!vma_start_read`, which is an optimistic
@@ -779,28 +784,31 @@ release of any VMA locks on its release makes sense, as you would never want to keep VMAs locked across entirely separate write operations. It also maintains correct lock ordering. -Each time a VMA read lock is acquired, we acquire a read lock on the -:c:member:`!vma->vm_lock` read/write semaphore and hold it, while checking that -the sequence count of the VMA does not match that of the mm. +Each time a VMA read lock is acquired, we increment :c:member:`!vma.vm_refcnt` +reference counter and check that the sequence count of the VMA does not match +that of the mm. -If it does, the read lock fails. If it does not, we hold the lock, excluding -writers, but permitting other readers, who will also obtain this lock under RCU. +If it does, the read lock fails and :c:member:`!vma.vm_refcnt` is dropped. +If it does not, we keep the reference counter raised, excluding writers, but +permitting other readers, who can also obtain this lock under RCU. Importantly, maple tree operations performed in :c:func:`!lock_vma_under_rcu` are also RCU safe, so the whole read lock operation is guaranteed to function correctly. -On the write side, we acquire a write lock on the :c:member:`!vma->vm_lock` -read/write semaphore, before setting the VMA's sequence number under this lock, -also simultaneously holding the mmap write lock. +On the write side, we set a bit in :c:member:`!vma.vm_refcnt` which can't be +modified by readers and wait for all readers to drop their reference count. +Once there are no readers, VMA's sequence number is set to match that of the +mm. During this entire operation mmap write lock is held. This way, if any read locks are in effect, :c:func:`!vma_start_write` will sleep until these are finished and mutual exclusion is achieved. -After setting the VMA's sequence number, the lock is released, avoiding -complexity with a long-term held write lock. +After setting the VMA's sequence number, the bit in :c:member:`!vma.vm_refcnt` +indicating a writer is cleared. From this point on, VMA's sequence number will +indicate VMA's write-locked state until mmap write lock is dropped or downgraded. -This clever combination of a read/write semaphore and sequence count allows for +This clever combination of a reference counter and sequence count allows for fast RCU-based per-VMA lock acquisition (especially on page fault, though utilised elsewhere) with minimal complexity around lock ordering.
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
Re: [PATCH v9 17/17] docs/mm: document latest changes to vm_lock
Quoted 6 lines · Suren Baghdasaryan
Change the documentation to reflect that
vm_lockis integrated into vma and replaced withvm_refcnt.
Document newly introduced vma_start_read_locked{_nested} functions.Signed-off-by Suren Baghdasaryan <[email protected]>Reviewed-by Liam R. Howlett <[email protected]>
Apart from small nit, LGTM, thanks for doing this!
Quoted 65 lines
--- Documentation/mm/process_addrs.rst | 44 ++++++++++++++++++------------ 1 file changed, 26 insertions(+), 18 deletions(-)Documentation/mm/process_addrs.rst +18
-12@@ -716,9 +716,14 @@ calls :c:func:`!rcu_read_lock` to ensure that the VMA is looked up in an RCU critical section, then attempts to VMA lock it via :c:func:`!vma_start_read`, before releasing the RCU lock via :c:func:`!rcu_read_unlock`. -VMA read locks hold the read lock on the :c:member:`!vma->vm_lock` semaphore for -their duration and the caller of :c:func:`!lock_vma_under_rcu` must release it -via :c:func:`!vma_end_read`. +In cases when the user already holds mmap read lock, :c:func:`!vma_start_read_locked` +and :c:func:`!vma_start_read_locked_nested` can be used. These functions do not +fail due to lock contention but the caller should still check their return values +in case they fail for other reasons. + +VMA read locks increment :c:member:`!vma.vm_refcnt` reference counter for their +duration and the caller of :c:func:`!lock_vma_under_rcu` must drop it via +:c:func:`!vma_end_read`. VMA **write** locks are acquired via :c:func:`!vma_start_write` in instances where a VMA is about to be modified, unlike :c:func:`!vma_start_read` the lock is always@@ -726,9 +731,9 @@ acquired. An mmap write lock **must** be held for the duration of the VMA write lock, releasing or downgrading the mmap write lock also releases the VMA write lock so there is no :c:func:`!vma_end_write` function. -Note that a semaphore write lock is not held across a VMA lock. Rather, a -sequence number is used for serialisation, and the write semaphore is only -acquired at the point of write lock to update this. +Note that when write-locking a VMA lock, the :c:member:`!vma.vm_refcnt` is temporarily +modified so that readers can detect the presense of a writer. The reference counter is +restored once the vma sequence number used for serialisation is updated. This ensures the semantics we require - VMA write locks provide exclusive write access to the VMA.@@ -738,7 +743,7 @@ Implementation details The VMA lock mechanism is designed to be a lightweight means of avoiding the use of the heavily contended mmap lock. It is implemented using a combination of a -read/write semaphore and sequence numbers belonging to the containing +reference counter and sequence numbers belonging to the containing :c:struct:`!struct mm_struct` and the VMA. Read locks are acquired via :c:func:`!vma_start_read`, which is an optimistic@@ -779,28 +784,31 @@ release of any VMA locks on its release makes sense, as you would never want to keep VMAs locked across entirely separate write operations. It also maintains correct lock ordering. -Each time a VMA read lock is acquired, we acquire a read lock on the -:c:member:`!vma->vm_lock` read/write semaphore and hold it, while checking that -the sequence count of the VMA does not match that of the mm. +Each time a VMA read lock is acquired, we increment :c:member:`!vma.vm_refcnt` +reference counter and check that the sequence count of the VMA does not match +that of the mm. -If it does, the read lock fails. If it does not, we hold the lock, excluding -writers, but permitting other readers, who will also obtain this lock under RCU. +If it does, the read lock fails and :c:member:`!vma.vm_refcnt` is dropped. +If it does not, we keep the reference counter raised, excluding writers, but +permitting other readers, who can also obtain this lock under RCU.
Documentation/mm/process_addrs.rst release of any VMA locks on its release makes sense, as you would never want to · line 801
Quoted 10 lines
Documentation/mm/process_addrs.rst +3
-3Importantly, maple tree operations performed in :c:func:`!lock_vma_under_rcu` are also RCU safe, so the whole read lock operation is guaranteed to function correctly. -On the write side, we acquire a write lock on the :c:member:`!vma->vm_lock` -read/write semaphore, before setting the VMA's sequence number under this lock, -also simultaneously holding the mmap write lock. +On the write side, we set a bit in :c:member:`!vma.vm_refcnt` which can't be +modified by readers and wait for all readers to drop their reference count. +Once there are no readers, VMA's sequence number is set to match that of the
Nit: 'the VMA's sequence number' seems to read better here.
Quoted 19 lines
Documentation/mm/process_addrs.rst +5
-4+mm. During this entire operation mmap write lock is held. This way, if any read locks are in effect, :c:func:`!vma_start_write` will sleep until these are finished and mutual exclusion is achieved. -After setting the VMA's sequence number, the lock is released, avoiding -complexity with a long-term held write lock. +After setting the VMA's sequence number, the bit in :c:member:`!vma.vm_refcnt` +indicating a writer is cleared. From this point on, VMA's sequence number will +indicate VMA's write-locked state until mmap write lock is dropped or downgraded. -This clever combination of a read/write semaphore and sequence count allows for +This clever combination of a reference counter and sequence count allows for fast RCU-based per-VMA lock acquisition (especially on page fault, though utilised elsewhere) with minimal complexity around lock ordering. --2.47.1.613.gc27f4b7a9f-goog
Suren Baghdasaryan
maintainerRe: [PATCH v9 17/17] docs/mm: document latest changes to vm_lock
Quoted 82 lines · Lorenzo Stoakes
Quoted 6 lines · Suren Baghdasaryan
Change the documentation to reflect that
vm_lockis integrated into vma and replaced withvm_refcnt.
Document newly introduced vma_start_read_locked{_nested} functions.Signed-off-by Suren Baghdasaryan <[email protected]>Reviewed-by Liam R. Howlett <[email protected]>Apart from small nit, LGTM, thanks for doing this!
Reviewed-by Lorenzo Stoakes <[email protected]>Quoted 69 lines
--- Documentation/mm/process_addrs.rst | 44 ++++++++++++++++++------------ 1 file changed, 26 insertions(+), 18 deletions(-)Documentation/mm/process_addrs.rst +18
-12@@ -716,9 +716,14 @@ calls :c:func:`!rcu_read_lock` to ensure that the VMA is looked up in an RCU critical section, then attempts to VMA lock it via :c:func:`!vma_start_read`, before releasing the RCU lock via :c:func:`!rcu_read_unlock`. -VMA read locks hold the read lock on the :c:member:`!vma->vm_lock` semaphore for -their duration and the caller of :c:func:`!lock_vma_under_rcu` must release it -via :c:func:`!vma_end_read`. +In cases when the user already holds mmap read lock, :c:func:`!vma_start_read_locked` +and :c:func:`!vma_start_read_locked_nested` can be used. These functions do not +fail due to lock contention but the caller should still check their return values +in case they fail for other reasons. + +VMA read locks increment :c:member:`!vma.vm_refcnt` reference counter for their +duration and the caller of :c:func:`!lock_vma_under_rcu` must drop it via +:c:func:`!vma_end_read`. VMA **write** locks are acquired via :c:func:`!vma_start_write` in instances where a VMA is about to be modified, unlike :c:func:`!vma_start_read` the lock is always@@ -726,9 +731,9 @@ acquired. An mmap write lock **must** be held for the duration of the VMA write lock, releasing or downgrading the mmap write lock also releases the VMA write lock so there is no :c:func:`!vma_end_write` function. -Note that a semaphore write lock is not held across a VMA lock. Rather, a -sequence number is used for serialisation, and the write semaphore is only -acquired at the point of write lock to update this. +Note that when write-locking a VMA lock, the :c:member:`!vma.vm_refcnt` is temporarily +modified so that readers can detect the presense of a writer. The reference counter is +restored once the vma sequence number used for serialisation is updated. This ensures the semantics we require - VMA write locks provide exclusive write access to the VMA.@@ -738,7 +743,7 @@ Implementation details The VMA lock mechanism is designed to be a lightweight means of avoiding the use of the heavily contended mmap lock. It is implemented using a combination of a -read/write semaphore and sequence numbers belonging to the containing +reference counter and sequence numbers belonging to the containing :c:struct:`!struct mm_struct` and the VMA. Read locks are acquired via :c:func:`!vma_start_read`, which is an optimistic@@ -779,28 +784,31 @@ release of any VMA locks on its release makes sense, as you would never want to keep VMAs locked across entirely separate write operations. It also maintains correct lock ordering. -Each time a VMA read lock is acquired, we acquire a read lock on the -:c:member:`!vma->vm_lock` read/write semaphore and hold it, while checking that -the sequence count of the VMA does not match that of the mm. +Each time a VMA read lock is acquired, we increment :c:member:`!vma.vm_refcnt` +reference counter and check that the sequence count of the VMA does not match +that of the mm. -If it does, the read lock fails. If it does not, we hold the lock, excluding -writers, but permitting other readers, who will also obtain this lock under RCU. +If it does, the read lock fails and :c:member:`!vma.vm_refcnt` is dropped. +If it does not, we keep the reference counter raised, excluding writers, but +permitting other readers, who can also obtain this lock under RCU. Importantly, maple tree operations performed in :c:func:`!lock_vma_under_rcu` are also RCU safe, so the whole read lock operation is guaranteed to function correctly.
Quoted 8 lines
Documentation/mm/process_addrs.rst release of any VMA locks on its release makes sense, as you would never want to · line 801
Quoted 6 lines
Documentation/mm/process_addrs.rst +3
-3-On the write side, we acquire a write lock on the :c:member:`!vma->vm_lock` -read/write semaphore, before setting the VMA's sequence number under this lock, -also simultaneously holding the mmap write lock. +On the write side, we set a bit in :c:member:`!vma.vm_refcnt` which can't be +modified by readers and wait for all readers to drop their reference count. +Once there are no readers, VMA's sequence number is set to match that of theNit: 'the VMA's sequence number' seems to read better here.
Ack.
Quoted 20 lines
Quoted 20 lines
Documentation/mm/process_addrs.rst +5
-4+mm. During this entire operation mmap write lock is held. This way, if any read locks are in effect, :c:func:`!vma_start_write` will sleep until these are finished and mutual exclusion is achieved. -After setting the VMA's sequence number, the lock is released, avoiding -complexity with a long-term held write lock. +After setting the VMA's sequence number, the bit in :c:member:`!vma.vm_refcnt` +indicating a writer is cleared. From this point on, VMA's sequence number will +indicate VMA's write-locked state until mmap write lock is dropped or downgraded. -This clever combination of a read/write semaphore and sequence count allows for +This clever combination of a reference counter and sequence count allows for fast RCU-based per-VMA lock acquisition (especially on page fault, though utilised elsewhere) with minimal complexity around lock ordering. --2.47.1.613.gc27f4b7a9f-goog
Quoted 2 lines · Suren Baghdasaryan
- Added static check for no-limit case in
__refcount_add_not_zero_limited, per David Laight
Ugh, no, don't listen to David.
Suren Baghdasaryan
maintainerQuoted 6 lines · Matthew Wilcox
Quoted 2 lines · Suren Baghdasaryan
- Added static check for no-limit case in
__refcount_add_not_zero_limited, per David LaightUgh, no, don't listen to David.
I thought his suggestion to add a check which can be verified at compile time made sense. Could you please explain why that's a bad idea? I'm really curious.
A nit on subject, I mean this is part of what this series does, and hey - we have only so much text to put in here - but isn't this both reimplementing per-VMA lock as a refcount _and_ importantly allocating VMAs using the RCU typesafe mechanism?
Do we have to do both in one series? Can we split this out? I mean maybe that's just churny and unnecessary, but to me this series is 'allocate VMAs RCU safe and refcount VMA lock' or something like this. Maybe this is nitty... but still :)
One general comment here - this is a really major change in how this stuff works, and yet I don't see any tests anywhere in the series.
I know it's tricky to write tests for this, but the new VMA testing environment should make it possible to test a _lot_ more than we previously could.
However due to some (*ahem*) interesting distribution of where functions are, most notably stuff in kernel/fork.c, I guess we can't test _everything_ there effectively.
But I do feel like we should be able to do better than having absolutely no testing added for this?
I think there's definitely quite a bit you could test now, at least in asserting fundamentals in tools/testing/vma/vma.c.
This can cover at least detached state asserts in various scenarios.
But that won't cover off the really gnarly stuff here around RCU slab allocation, and determining precisely how to test that in a sensible way is maybe less clear.
But I'd like to see _something_ here please, this is more or less fundamentally changing how all VMAs are allocated and to just have nothing feels unfortunate.
I'm already nervous because we've hit issues coming up to v9 and we're not 100% sure if a recent syzkaller is related to these changes or not, I'm not sure how much we can get assurances with tests but I'd like something.
Thanks!
Quoted 114 lines · Suren Baghdasaryan
Back when per-vma locks were introduces,
vm_lockwas moved out ofvm_area_structin [1] because of the performance regression caused by false cacheline sharing. Recent investigation [2] revealed that the regressions is limited to a rather old Broadwell microarchitecture and even there it can be mitigated by disabling adjacent cacheline prefetching, see [3].
Splitting single logical structure into multiple ones leads to more complicated management, extra pointer dereferences and overall less maintainable code. When that split-away part is a lock, it complicates things even further. With no performance benefits, there are no reasons for this split. Merging thevm_lockback intovm_area_structalso allowsvm_area_structto useSLAB_TYPESAFE_BY_RCUlater in this patchset. This patchset:
1. movesvm_lockback intovm_area_struct, aligning it at the cacheline boundary and changing the cache to be cacheline-aligned to minimize cacheline sharing;
2. changesvm_area_structinitialization to mark new vma as detached until it is inserted into vma tree;
3. replacesvm_lockandvma->detachedflag with a reference counter;
4. regroupsvm_area_structmembers to fit them into 3 cachelines;
5. changesvm_area_structcache toSLAB_TYPESAFE_BY_RCUto allow for their reuse and to minimizecall_rcu()calls.Pagefault microbenchmarks show performance improvement:
Hmean faults/cpu-1 507926.5547 ( 0.00%) 506519.3692 * -0.28%* Hmean faults/cpu-4 479119.7051 ( 0.00%) 481333.6802 * 0.46%* Hmean faults/cpu-7 452880.2961 ( 0.00%) 455845.6211 * 0.65%* Hmean faults/cpu-12 347639.1021 ( 0.00%) 352004.2254 * 1.26%* Hmean faults/cpu-21 200061.2238 ( 0.00%) 229597.0317 * 14.76%* Hmean faults/cpu-30 145251.2001 ( 0.00%) 164202.5067 * 13.05%* Hmean faults/cpu-48 106848.4434 ( 0.00%) 120641.5504 * 12.91%* Hmean faults/cpu-56 92472.3835 ( 0.00%) 103464.7916 * 11.89%* Hmean faults/sec-1 507566.1468 ( 0.00%) 506139.0811 * -0.28%* Hmean faults/sec-4 1880478.2402 ( 0.00%) 1886795.6329 * 0.34%* Hmean faults/sec-7 3106394.3438 ( 0.00%) 3140550.7485 * 1.10%* Hmean faults/sec-12 4061358.4795 ( 0.00%) 4112477.0206 * 1.26%* Hmean faults/sec-21 3988619.1169 ( 0.00%) 4577747.1436 * 14.77%* Hmean faults/sec-30 3909839.5449 ( 0.00%) 4311052.2787 * 10.26%* Hmean faults/sec-48 4761108.4691 ( 0.00%) 5283790.5026 * 10.98%* Hmean faults/sec-56 4885561.4590 ( 0.00%) 5415839.4045 * 10.85%*Changes since v8 [4]:
- Change subject for the cover letter, per Vlastimil Babka
- Added Reviewed-by and Acked-by, per Vlastimil Babka
- Added static check for no-limit case in__refcount_add_not_zero_limited, per David Laight
- Fixedvma_refcount_put()to callrwsem_release()unconditionally, per Hillf Danton and Vlastimil Babka
- Use a copy ofvma->vm_mminvma_refcount_put()in case vma is freed from under us, per Vlastimil Babka
- Removed extra rcu_read_lock()/rcu_read_unlock() invma_end_read(), per Vlastimil Babka
- Changed__vma_enter_locked()parameter to centralize refcount logic, per Vlastimil Babka
- Amended description invm_lockreplacement patch explaining the effects of the patch onvm_area_structsize, per Vlastimil Babka
- Addedvm_area_structmember regrouping patch [5] into the series
- Renamedvma_copy()intovm_area_init_from(), per Liam R. Howlett
- Added a comment forvm_area_structto updatevm_area_init_from()when adding new members, per Vlastimil Babka
- Updated a comment about unstablesrc->shared.rbwhen copying a vma invm_area_init_from(), per Vlastimil Babka[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/ZsQyI%2F087V34JoIt@xsang-OptiPlex-9020/
[3] https://lore.kernel.org/all/CAJuCfpEisU8Lfe96AYJDZ+OM4NoPmnw9bP53cT_kbfP_pR+-2g@mail.gmail.com/
[4] https://lore.kernel.org/all/[email protected]/
[5] https://lore.kernel.org/all/[email protected]/Patchset applies over mm-unstable after reverting v8 (current SHA range:
235b5129cb7b-9e6b24c58985)Suren Baghdasaryan (17):
mm: introduce vma_start_read_locked{_nested} helpers
mm: move per-vma lock intovm_area_struct
mm: mark vma as detached until it's added into vma tree
mm: introducevma_iter_store_attached()to use with attached vmas
mm: mark vmas detached upon exit
types: move struct rcuwait intotypes.h
mm: allow vma_start_read_locked/vma_start_read_locked_nested to fail
mm: movemmap_init_lock()out of the header file
mm: uninline the main body ofvma_start_write()
refcount: introduce __refcount_{add|inc}_not_zero_limited
mm: replacevm_lockand detached flag with a reference count
mm: move lesser usedvma_area_structmembers into the last cacheline mm/debug: printvm_refcntstate when dumping the vma
mm: remove extravma_numab_state_init()call
mm: preparelock_vma_under_rcu()for vma reuse possibility
mm: make vma cacheSLAB_TYPESAFE_BY_RCU
docs/mm: document latest changes tovm_lockDocumentation/mm/process_addrs.rst | 44 ++++---- include/linux/mm.h | 156 ++++++++++++++++++++++------- include/linux/mm_types.h | 75 +++++++------- include/linux/mmap_lock.h | 6 -- include/linux/rcuwait.h | 13 +-- include/linux/refcount.h | 24 ++++- include/linux/slab.h | 6 -- include/linux/types.h | 12 +++ kernel/fork.c | 129 +++++++++++------------- mm/debug.c | 12 +++ mm/init-mm.c | 1 + mm/memory.c | 97 ++++++++++++++++-- mm/mmap.c | 3 +- mm/userfaultfd.c | 32 +++--- mm/vma.c | 23 ++--- mm/vma.h | 15 ++- tools/testing/vma/linux/atomic.h | 5 + tools/testing/vma/vma_internal.h | 93 ++++++++--------- 18 files changed, 465 insertions(+), 281 deletions(-)--
2.47.1.613.gc27f4b7a9f-goog
Suren Baghdasaryan
maintainerQuoted 10 lines · Lorenzo Stoakes
A nit on subject, I mean this is part of what this series does, and hey - we have only so much text to put in here - but isn't this both reimplementing per-VMA lock as a refcount _and_ importantly allocating VMAs using the RCU typesafe mechanism?
Do we have to do both in one series? Can we split this out? I mean maybe that's just churny and unnecessary, but to me this series is 'allocate VMAs RCU safe and refcount VMA lock' or something like this. Maybe this is nitty... but still :)
There is "motivational dependency" because one of the main reasons I'm converting the vm_lock into vm_refcnt is to make it easier to add SLAB_TYPESAFE_BY_RCU (see my last reply to Hillf). But technically we can leave the SLAB_TYPESAFE_BY_RCU out of this series if that makes thighs easier. That would be the 2 patches at the end:
mm: prepare lock_vma_under_rcu() for vma reuse possibility
mm: make vma cache SLAB_TYPESAFE_BY_RCU
I made sure that each patch is bisectable, so there should not be a problem with tracking issues.
Quoted 3 lines
One general comment here - this is a really major change in how this stuff works, and yet I don't see any tests anywhere in the series.
Hmm. I was diligently updating the tests to reflect the replacement of vm_lock with vm_refcnt and adding assertions for detach/attach cases. This actually reminds me that I missed updading vm_area_struct in vma_internal.h for the member regrouping patch; will add that. I think the only part that did not affect tests is SLAB_TYPESAFE_BY_RCU but I was not sure what kind of testing I can add for that. Any suggestions would be welcomed.
Quoted 11 lines
I know it's tricky to write tests for this, but the new VMA testing environment should make it possible to test a _lot_ more than we previously could.
However due to some (*ahem*) interesting distribution of where functions are, most notably stuff in
kernel/fork.c, I guess we can't test _everything_ there effectively.But I do feel like we should be able to do better than having absolutely no testing added for this?
Again, I'm open to suggestions for SLAB_TYPESAFE_BY_RCU testing but for the rest I thought the tests were modified accordingly.
Quoted 5 lines
I think there's definitely quite a bit you could test now, at least in asserting fundamentals in
tools/testing/vma/vma.c.This can cover at least detached state asserts in various scenarios.
Ok, you mean to check that VMA re-attachment/re-detachment would trigger assertions? I'll look into adding tests for that.
Quoted 8 lines
But that won't cover off the really gnarly stuff here around RCU slab allocation, and determining precisely how to test that in a sensible way is maybe less clear.
But I'd like to see _something_ here please, this is more or less fundamentally changing how all VMAs are allocated and to just have nothing feels unfortunate.
Again, I'm open to suggestions on what kind of testing I can add for SLAB_TYPESAFE_BY_RCU change.
Quoted 4 lines
I'm already nervous because we've hit issues coming up to v9 and we're not 100% sure if a recent syzkaller is related to these changes or not, I'm not sure how much we can get assurances with tests but I'd like something.
If you are referring to the issue at [1], I think David ran the syzcaller against mm-stable that does not contain this patchset and the issue still triggered (see [2]). This of course does not guarantee that this patchset has no other issues :) I'll try adding tests for re-attaching, re-detaching and welcome ideas on how to test SLAB_TYPESAFE_BY_RCU transition.
Thanks,
Suren.
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
Quoted 118 lines
Thanks!
Quoted 114 lines · Suren Baghdasaryan
Back when per-vma locks were introduces,
vm_lockwas moved out ofvm_area_structin [1] because of the performance regression caused by false cacheline sharing. Recent investigation [2] revealed that the regressions is limited to a rather old Broadwell microarchitecture and even there it can be mitigated by disabling adjacent cacheline prefetching, see [3].
Splitting single logical structure into multiple ones leads to more complicated management, extra pointer dereferences and overall less maintainable code. When that split-away part is a lock, it complicates things even further. With no performance benefits, there are no reasons for this split. Merging thevm_lockback intovm_area_structalso allowsvm_area_structto useSLAB_TYPESAFE_BY_RCUlater in this patchset. This patchset:
1. movesvm_lockback intovm_area_struct, aligning it at the cacheline boundary and changing the cache to be cacheline-aligned to minimize cacheline sharing;
2. changesvm_area_structinitialization to mark new vma as detached until it is inserted into vma tree;
3. replacesvm_lockandvma->detachedflag with a reference counter;
4. regroupsvm_area_structmembers to fit them into 3 cachelines;
5. changesvm_area_structcache toSLAB_TYPESAFE_BY_RCUto allow for their reuse and to minimizecall_rcu()calls.Pagefault microbenchmarks show performance improvement:
Hmean faults/cpu-1 507926.5547 ( 0.00%) 506519.3692 * -0.28%* Hmean faults/cpu-4 479119.7051 ( 0.00%) 481333.6802 * 0.46%* Hmean faults/cpu-7 452880.2961 ( 0.00%) 455845.6211 * 0.65%* Hmean faults/cpu-12 347639.1021 ( 0.00%) 352004.2254 * 1.26%* Hmean faults/cpu-21 200061.2238 ( 0.00%) 229597.0317 * 14.76%* Hmean faults/cpu-30 145251.2001 ( 0.00%) 164202.5067 * 13.05%* Hmean faults/cpu-48 106848.4434 ( 0.00%) 120641.5504 * 12.91%* Hmean faults/cpu-56 92472.3835 ( 0.00%) 103464.7916 * 11.89%* Hmean faults/sec-1 507566.1468 ( 0.00%) 506139.0811 * -0.28%* Hmean faults/sec-4 1880478.2402 ( 0.00%) 1886795.6329 * 0.34%* Hmean faults/sec-7 3106394.3438 ( 0.00%) 3140550.7485 * 1.10%* Hmean faults/sec-12 4061358.4795 ( 0.00%) 4112477.0206 * 1.26%* Hmean faults/sec-21 3988619.1169 ( 0.00%) 4577747.1436 * 14.77%* Hmean faults/sec-30 3909839.5449 ( 0.00%) 4311052.2787 * 10.26%* Hmean faults/sec-48 4761108.4691 ( 0.00%) 5283790.5026 * 10.98%* Hmean faults/sec-56 4885561.4590 ( 0.00%) 5415839.4045 * 10.85%*Changes since v8 [4]:
- Change subject for the cover letter, per Vlastimil Babka
- Added Reviewed-by and Acked-by, per Vlastimil Babka
- Added static check for no-limit case in__refcount_add_not_zero_limited, per David Laight
- Fixedvma_refcount_put()to callrwsem_release()unconditionally, per Hillf Danton and Vlastimil Babka
- Use a copy ofvma->vm_mminvma_refcount_put()in case vma is freed from under us, per Vlastimil Babka
- Removed extra rcu_read_lock()/rcu_read_unlock() invma_end_read(), per Vlastimil Babka
- Changed__vma_enter_locked()parameter to centralize refcount logic, per Vlastimil Babka
- Amended description invm_lockreplacement patch explaining the effects of the patch onvm_area_structsize, per Vlastimil Babka
- Addedvm_area_structmember regrouping patch [5] into the series
- Renamedvma_copy()intovm_area_init_from(), per Liam R. Howlett
- Added a comment forvm_area_structto updatevm_area_init_from()when adding new members, per Vlastimil Babka
- Updated a comment about unstablesrc->shared.rbwhen copying a vma invm_area_init_from(), per Vlastimil Babka[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/ZsQyI%2F087V34JoIt@xsang-OptiPlex-9020/
[3] https://lore.kernel.org/all/CAJuCfpEisU8Lfe96AYJDZ+OM4NoPmnw9bP53cT_kbfP_pR+-2g@mail.gmail.com/
[4] https://lore.kernel.org/all/[email protected]/
[5] https://lore.kernel.org/all/[email protected]/Patchset applies over mm-unstable after reverting v8 (current SHA range:
235b5129cb7b-9e6b24c58985)Suren Baghdasaryan (17):
mm: introduce vma_start_read_locked{_nested} helpers
mm: move per-vma lock intovm_area_struct
mm: mark vma as detached until it's added into vma tree
mm: introducevma_iter_store_attached()to use with attached vmas
mm: mark vmas detached upon exit
types: move struct rcuwait intotypes.h
mm: allow vma_start_read_locked/vma_start_read_locked_nested to fail
mm: movemmap_init_lock()out of the header file
mm: uninline the main body ofvma_start_write()
refcount: introduce __refcount_{add|inc}_not_zero_limited
mm: replacevm_lockand detached flag with a reference count
mm: move lesser usedvma_area_structmembers into the last cacheline mm/debug: printvm_refcntstate when dumping the vma
mm: remove extravma_numab_state_init()call
mm: preparelock_vma_under_rcu()for vma reuse possibility
mm: make vma cacheSLAB_TYPESAFE_BY_RCU
docs/mm: document latest changes tovm_lockDocumentation/mm/process_addrs.rst | 44 ++++---- include/linux/mm.h | 156 ++++++++++++++++++++++------- include/linux/mm_types.h | 75 +++++++------- include/linux/mmap_lock.h | 6 -- include/linux/rcuwait.h | 13 +-- include/linux/refcount.h | 24 ++++- include/linux/slab.h | 6 -- include/linux/types.h | 12 +++ kernel/fork.c | 129 +++++++++++------------- mm/debug.c | 12 +++ mm/init-mm.c | 1 + mm/memory.c | 97 ++++++++++++++++-- mm/mmap.c | 3 +- mm/userfaultfd.c | 32 +++--- mm/vma.c | 23 ++--- mm/vma.h | 15 ++- tools/testing/vma/linux/atomic.h | 5 + tools/testing/vma/vma_internal.h | 93 ++++++++--------- 18 files changed, 465 insertions(+), 281 deletions(-)--
2.47.1.613.gc27f4b7a9f-goog
Quoted 13 lines · Suren Baghdasaryan
Quoted 10 lines · Lorenzo Stoakes
A nit on subject, I mean this is part of what this series does, and hey - we have only so much text to put in here - but isn't this both reimplementing per-VMA lock as a refcount _and_ importantly allocating VMAs using the RCU typesafe mechanism?
Do we have to do both in one series? Can we split this out? I mean maybe that's just churny and unnecessary, but to me this series is 'allocate VMAs RCU safe and refcount VMA lock' or something like this. Maybe this is nitty... but still :)
Quoted 5 lines
There is "motivational dependency" because one of the main reasons I'm converting the
vm_lockintovm_refcntis to make it easier to addSLAB_TYPESAFE_BY_RCU(see my last reply to Hillf). But technically we can leave theSLAB_TYPESAFE_BY_RCUout of this series if that makes thighs easier. That would be the 2 patches at the end:
Right yeah... maybe it's better to do it in one hit.
Quoted 11 lines
mm: prepare
lock_vma_under_rcu()for vma reuse possibility
mm: make vma cacheSLAB_TYPESAFE_BY_RCUI made sure that each patch is bisectable, so there should not be a problem with tracking issues.
Quoted 3 lines
One general comment here - this is a really major change in how this stuff works, and yet I don't see any tests anywhere in the series.
Quoted 7 lines
Hmm. I was diligently updating the tests to reflect the replacement of
vm_lockwithvm_refcntand adding assertions for detach/attach cases. This actually reminds me that I missed updadingvm_area_structinvma_internal.hfor the member regrouping patch; will add that. I think the only part that did not affect tests isSLAB_TYPESAFE_BY_RCUbut I was not sure what kind of testing I can add for that. Any suggestions would be welcomed.
And to be clear I'm super grateful you did that :) thanks, be good to change the member regrouping thing also.
But that doesn't change the fact that this series has exactly zero tests for it. And for something so broad, it feels like a big issue, we really want to be careful with something so big here.
You've also noticed that I've cleverly failed to _actually_ suggest SLAB_TYPESAFE_BY_RCU tests, and mea culpa - it's super hard to think of how to test that.
Liam has experience doing RCU testing this for the maple tree stuff, but it wasn't pretty and wasn't easy and would probably require massive rework to expose this stuff to some viable testing environment, or in other words - is unworkable.
HOWEVER, I feel like maybe we could try to create scenarios where we might trigger reuse bugs?
Perhaps some userland code, perhaps even constrained by cgroup, that maps a ton of stuff and unmaps in a loop in parallel?
Perhaps create scenarios with shared memory where we up refcounts a lot too?
Anyway, this is necessarily nebulous without further investigation, what I was thinking more concretely is:
Using the VMA userland testing:
1. Assert reference count correctness across locking scenarios and various VMA operations.
2. Assert correct detached/not detached state across different scenarios.
This won't quite be complete as not everything is separated out quite enough to allow things like process tear down/forking etc. to be explicitly tested but you can unit tests the VMA bits at least.
One note on this, I intend to split the vma.c file into multiple files in tools/testing/vma/ so if you add tests here it'd be worth probably putting them into a new file.
I'm happy to help with this if you need any assistance, feel free to ping!
Sorry to put this on you so late in the series, I realise it's annoying, but I feel like things have changed a lot and obviously aggregated with two series in one in effect and these are genuine concerns that at this stage I feel like we need to try to at least make some headway on.
Quoted 10 lines
Quoted 10 lines
I know it's tricky to write tests for this, but the new VMA testing environment should make it possible to test a _lot_ more than we previously could.
However due to some (*ahem*) interesting distribution of where functions are, most notably stuff in
kernel/fork.c, I guess we can't test _everything_ there effectively.
Quoted 5 lines
Quoted 2 lines
But I do feel like we should be able to do better than having absolutely no testing added for this?
Again, I'm open to suggestions for
SLAB_TYPESAFE_BY_RCUtesting but for the rest I thought the tests were modified accordingly.
See above ^
Quoted 9 lines
Quoted 6 lines
I think there's definitely quite a bit you could test now, at least in asserting fundamentals in
tools/testing/vma/vma.c.This can cover at least detached state asserts in various scenarios.
Ok, you mean to check that VMA re-attachment/re-detachment would trigger assertions? I'll look into adding tests for that.
Yeah this is one, see above :)
Quoted 6 lines
Quoted 6 lines
But that won't cover off the really gnarly stuff here around RCU slab allocation, and determining precisely how to test that in a sensible way is maybe less clear.
Quoted 6 lines
Quoted 3 lines
But I'd like to see _something_ here please, this is more or less fundamentally changing how all VMAs are allocated and to just have nothing feels unfortunate.
Again, I'm open to suggestions on what kind of testing I can add for
SLAB_TYPESAFE_BY_RCUchange.
See above
Quoted 6 lines
Quoted 5 lines
I'm already nervous because we've hit issues coming up to v9 and we're not 100% sure if a recent syzkaller is related to these changes or not, I'm not sure how much we can get assurances with tests but I'd like something.
Quoted 8 lines
If you are referring to the issue at [1], I think David ran the syzcaller against mm-stable that does not contain this patchset and the issue still triggered (see [2]). This of course does not guarantee that this patchset has no other issues :) I'll try adding tests for re-attaching, re-detaching and welcome ideas on how to test
SLAB_TYPESAFE_BY_RCUtransition.
Thanks,
Suren.
OK that's reassuring!
Quoted 122 lines
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/Quoted 118 lines
Thanks!
Quoted 114 lines · Suren Baghdasaryan
Back when per-vma locks were introduces,
vm_lockwas moved out ofvm_area_structin [1] because of the performance regression caused by false cacheline sharing. Recent investigation [2] revealed that the regressions is limited to a rather old Broadwell microarchitecture and even there it can be mitigated by disabling adjacent cacheline prefetching, see [3].
Splitting single logical structure into multiple ones leads to more complicated management, extra pointer dereferences and overall less maintainable code. When that split-away part is a lock, it complicates things even further. With no performance benefits, there are no reasons for this split. Merging thevm_lockback intovm_area_structalso allowsvm_area_structto useSLAB_TYPESAFE_BY_RCUlater in this patchset. This patchset:
1. movesvm_lockback intovm_area_struct, aligning it at the cacheline boundary and changing the cache to be cacheline-aligned to minimize cacheline sharing;
2. changesvm_area_structinitialization to mark new vma as detached until it is inserted into vma tree;
3. replacesvm_lockandvma->detachedflag with a reference counter;
4. regroupsvm_area_structmembers to fit them into 3 cachelines;
5. changesvm_area_structcache toSLAB_TYPESAFE_BY_RCUto allow for their reuse and to minimizecall_rcu()calls.Pagefault microbenchmarks show performance improvement:
Hmean faults/cpu-1 507926.5547 ( 0.00%) 506519.3692 * -0.28%* Hmean faults/cpu-4 479119.7051 ( 0.00%) 481333.6802 * 0.46%* Hmean faults/cpu-7 452880.2961 ( 0.00%) 455845.6211 * 0.65%* Hmean faults/cpu-12 347639.1021 ( 0.00%) 352004.2254 * 1.26%* Hmean faults/cpu-21 200061.2238 ( 0.00%) 229597.0317 * 14.76%* Hmean faults/cpu-30 145251.2001 ( 0.00%) 164202.5067 * 13.05%* Hmean faults/cpu-48 106848.4434 ( 0.00%) 120641.5504 * 12.91%* Hmean faults/cpu-56 92472.3835 ( 0.00%) 103464.7916 * 11.89%* Hmean faults/sec-1 507566.1468 ( 0.00%) 506139.0811 * -0.28%* Hmean faults/sec-4 1880478.2402 ( 0.00%) 1886795.6329 * 0.34%* Hmean faults/sec-7 3106394.3438 ( 0.00%) 3140550.7485 * 1.10%* Hmean faults/sec-12 4061358.4795 ( 0.00%) 4112477.0206 * 1.26%* Hmean faults/sec-21 3988619.1169 ( 0.00%) 4577747.1436 * 14.77%* Hmean faults/sec-30 3909839.5449 ( 0.00%) 4311052.2787 * 10.26%* Hmean faults/sec-48 4761108.4691 ( 0.00%) 5283790.5026 * 10.98%* Hmean faults/sec-56 4885561.4590 ( 0.00%) 5415839.4045 * 10.85%*Changes since v8 [4]:
- Change subject for the cover letter, per Vlastimil Babka
- Added Reviewed-by and Acked-by, per Vlastimil Babka
- Added static check for no-limit case in__refcount_add_not_zero_limited, per David Laight
- Fixedvma_refcount_put()to callrwsem_release()unconditionally, per Hillf Danton and Vlastimil Babka
- Use a copy ofvma->vm_mminvma_refcount_put()in case vma is freed from under us, per Vlastimil Babka
- Removed extra rcu_read_lock()/rcu_read_unlock() invma_end_read(), per Vlastimil Babka
- Changed__vma_enter_locked()parameter to centralize refcount logic, per Vlastimil Babka
- Amended description invm_lockreplacement patch explaining the effects of the patch onvm_area_structsize, per Vlastimil Babka
- Addedvm_area_structmember regrouping patch [5] into the series
- Renamedvma_copy()intovm_area_init_from(), per Liam R. Howlett
- Added a comment forvm_area_structto updatevm_area_init_from()when adding new members, per Vlastimil Babka
- Updated a comment about unstablesrc->shared.rbwhen copying a vma invm_area_init_from(), per Vlastimil Babka[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/ZsQyI%2F087V34JoIt@xsang-OptiPlex-9020/
[3] https://lore.kernel.org/all/CAJuCfpEisU8Lfe96AYJDZ+OM4NoPmnw9bP53cT_kbfP_pR+-2g@mail.gmail.com/
[4] https://lore.kernel.org/all/[email protected]/
[5] https://lore.kernel.org/all/[email protected]/Patchset applies over mm-unstable after reverting v8 (current SHA range:
235b5129cb7b-9e6b24c58985)Suren Baghdasaryan (17):
mm: introduce vma_start_read_locked{_nested} helpers
mm: move per-vma lock intovm_area_struct
mm: mark vma as detached until it's added into vma tree
mm: introducevma_iter_store_attached()to use with attached vmas
mm: mark vmas detached upon exit
types: move struct rcuwait intotypes.h
mm: allow vma_start_read_locked/vma_start_read_locked_nested to fail
mm: movemmap_init_lock()out of the header file
mm: uninline the main body ofvma_start_write()
refcount: introduce __refcount_{add|inc}_not_zero_limited
mm: replacevm_lockand detached flag with a reference count
mm: move lesser usedvma_area_structmembers into the last cacheline mm/debug: printvm_refcntstate when dumping the vma
mm: remove extravma_numab_state_init()call
mm: preparelock_vma_under_rcu()for vma reuse possibility
mm: make vma cacheSLAB_TYPESAFE_BY_RCU
docs/mm: document latest changes tovm_lockDocumentation/mm/process_addrs.rst | 44 ++++---- include/linux/mm.h | 156 ++++++++++++++++++++++------- include/linux/mm_types.h | 75 +++++++------- include/linux/mmap_lock.h | 6 -- include/linux/rcuwait.h | 13 +-- include/linux/refcount.h | 24 ++++- include/linux/slab.h | 6 -- include/linux/types.h | 12 +++ kernel/fork.c | 129 +++++++++++------------- mm/debug.c | 12 +++ mm/init-mm.c | 1 + mm/memory.c | 97 ++++++++++++++++-- mm/mmap.c | 3 +- mm/userfaultfd.c | 32 +++--- mm/vma.c | 23 ++--- mm/vma.h | 15 ++- tools/testing/vma/linux/atomic.h | 5 + tools/testing/vma/vma_internal.h | 93 ++++++++--------- 18 files changed, 465 insertions(+), 281 deletions(-)--
2.47.1.613.gc27f4b7a9f-goog
Suren Baghdasaryan
maintainerQuoted 58 lines · Lorenzo Stoakes
Quoted 13 lines · Suren Baghdasaryan
Quoted 10 lines · Lorenzo Stoakes
A nit on subject, I mean this is part of what this series does, and hey - we have only so much text to put in here - but isn't this both reimplementing per-VMA lock as a refcount _and_ importantly allocating VMAs using the RCU typesafe mechanism?
Do we have to do both in one series? Can we split this out? I mean maybe that's just churny and unnecessary, but to me this series is 'allocate VMAs RCU safe and refcount VMA lock' or something like this. Maybe this is nitty... but still :)
Quoted 5 lines
There is "motivational dependency" because one of the main reasons I'm converting the
vm_lockintovm_refcntis to make it easier to addSLAB_TYPESAFE_BY_RCU(see my last reply to Hillf). But technically we can leave theSLAB_TYPESAFE_BY_RCUout of this series if that makes thighs easier. That would be the 2 patches at the end:Right yeah... maybe it's better to do it in one hit.
Quoted 11 lines
mm: prepare
lock_vma_under_rcu()for vma reuse possibility
mm: make vma cacheSLAB_TYPESAFE_BY_RCUI made sure that each patch is bisectable, so there should not be a problem with tracking issues.
Quoted 3 lines
One general comment here - this is a really major change in how this stuff works, and yet I don't see any tests anywhere in the series.
Quoted 7 lines
Hmm. I was diligently updating the tests to reflect the replacement of
vm_lockwithvm_refcntand adding assertions for detach/attach cases. This actually reminds me that I missed updadingvm_area_structinvma_internal.hfor the member regrouping patch; will add that. I think the only part that did not affect tests isSLAB_TYPESAFE_BY_RCUbut I was not sure what kind of testing I can add for that. Any suggestions would be welcomed.And to be clear I'm super grateful you did that :) thanks, be good to change the member regrouping thing also.
But that doesn't change the fact that this series has exactly zero tests for it. And for something so broad, it feels like a big issue, we really want to be careful with something so big here.
You've also noticed that I've cleverly failed to _actually_ suggest
SLAB_TYPESAFE_BY_RCUtests, and mea culpa - it's super hard to think of how to test that.Liam has experience doing RCU testing this for the maple tree stuff, but it wasn't pretty and wasn't easy and would probably require massive rework to expose this stuff to some viable testing environment, or in other words - is unworkable.
Quoted 7 lines
HOWEVER, I feel like maybe we could try to create scenarios where we might trigger reuse bugs?
Perhaps some userland code, perhaps even constrained by cgroup, that maps a ton of stuff and unmaps in a loop in parallel?
Perhaps create scenarios with shared memory where we up refcounts a lot too?
I have this old spf_test
(https://github.com/surenbaghdasaryan/spf_test/blob/main/spf_test.c)
which I often use to weed out vma locking issues because it starts multiple threads doing mmap + page faults. Perhaps we can repackage it into a test/benchmark for testing contention on mmap/vma locks?
Quoted 14 lines
Anyway, this is necessarily nebulous without further investigation, what I was thinking more concretely is:
Using the VMA userland testing:
1. Assert reference count correctness across locking scenarios and various VMA operations.
2. Assert correct detached/not detached state across different scenarios.This won't quite be complete as not everything is separated out quite enough to allow things like process tear down/forking etc. to be explicitly tested but you can unit tests the VMA bits at least.
Quoted 5 lines
One note on this, I intend to split the
vma.cfile into multiple files intools/testing/vma/so if you add tests here it'd be worth probably putting them into a new file.I'm happy to help with this if you need any assistance, feel free to ping!
As a starting point I was thinking of changing vma_assert_attached()/vma_assert_detached() and vma_mark_attached()/vma_mark_detached() to return a bool and use WARN_ON_ONCE() (to address your concern about asserts being dependent on CONFIG_DEBUG_VM) like this:
static inline bool vma_assert_detached()
{
return !WARN_ON_ONCE(atomic_read(&vma->vm_refcnt));
}static inline bool vma_mark_attached(struct vm_area_struct *vma)
{
vma_assert_write_locked(vma);
if (!vma_assert_detached(vma))
return false;
atomic_set(&vma->vm_refcnt, 1);
return true;
}With that we can add correctness checks in the tools/testing/vma/vma.c for different states, for example in the alloc_and_link_vma() we can check that after vma_link() the vma is indeed attached:
ASSERT_TRUE(vma_assert_attached(vma));
This might not cover all states but is probably a good starting point. WDYT?
Quoted 190 lines
Sorry to put this on you so late in the series, I realise it's annoying, but I feel like things have changed a lot and obviously aggregated with two series in one in effect and these are genuine concerns that at this stage I feel like we need to try to at least make some headway on.
Quoted 10 lines
Quoted 10 lines
I know it's tricky to write tests for this, but the new VMA testing environment should make it possible to test a _lot_ more than we previously could.
However due to some (*ahem*) interesting distribution of where functions are, most notably stuff in
kernel/fork.c, I guess we can't test _everything_ there effectively.Quoted 5 lines
Quoted 2 lines
But I do feel like we should be able to do better than having absolutely no testing added for this?
Again, I'm open to suggestions for
SLAB_TYPESAFE_BY_RCUtesting but for the rest I thought the tests were modified accordingly.See above ^
Quoted 9 lines
Quoted 6 lines
I think there's definitely quite a bit you could test now, at least in asserting fundamentals in
tools/testing/vma/vma.c.This can cover at least detached state asserts in various scenarios.
Ok, you mean to check that VMA re-attachment/re-detachment would trigger assertions? I'll look into adding tests for that.
Yeah this is one, see above :)
Quoted 6 lines
Quoted 6 lines
But that won't cover off the really gnarly stuff here around RCU slab allocation, and determining precisely how to test that in a sensible way is maybe less clear.
Quoted 6 lines
Quoted 3 lines
But I'd like to see _something_ here please, this is more or less fundamentally changing how all VMAs are allocated and to just have nothing feels unfortunate.
Again, I'm open to suggestions on what kind of testing I can add for
SLAB_TYPESAFE_BY_RCUchange.See above
Quoted 6 lines
Quoted 5 lines
I'm already nervous because we've hit issues coming up to v9 and we're not 100% sure if a recent syzkaller is related to these changes or not, I'm not sure how much we can get assurances with tests but I'd like something.
Quoted 8 lines
If you are referring to the issue at [1], I think David ran the syzcaller against mm-stable that does not contain this patchset and the issue still triggered (see [2]). This of course does not guarantee that this patchset has no other issues :) I'll try adding tests for re-attaching, re-detaching and welcome ideas on how to test
SLAB_TYPESAFE_BY_RCUtransition.
Thanks,
Suren.OK that's reassuring!
Quoted 122 lines
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/Quoted 118 lines
Thanks!
Quoted 114 lines · Suren Baghdasaryan
Back when per-vma locks were introduces,
vm_lockwas moved out ofvm_area_structin [1] because of the performance regression caused by false cacheline sharing. Recent investigation [2] revealed that the regressions is limited to a rather old Broadwell microarchitecture and even there it can be mitigated by disabling adjacent cacheline prefetching, see [3].
Splitting single logical structure into multiple ones leads to more complicated management, extra pointer dereferences and overall less maintainable code. When that split-away part is a lock, it complicates things even further. With no performance benefits, there are no reasons for this split. Merging thevm_lockback intovm_area_structalso allowsvm_area_structto useSLAB_TYPESAFE_BY_RCUlater in this patchset. This patchset:
1. movesvm_lockback intovm_area_struct, aligning it at the cacheline boundary and changing the cache to be cacheline-aligned to minimize cacheline sharing;
2. changesvm_area_structinitialization to mark new vma as detached until it is inserted into vma tree;
3. replacesvm_lockandvma->detachedflag with a reference counter;
4. regroupsvm_area_structmembers to fit them into 3 cachelines;
5. changesvm_area_structcache toSLAB_TYPESAFE_BY_RCUto allow for their reuse and to minimizecall_rcu()calls.Pagefault microbenchmarks show performance improvement:
Hmean faults/cpu-1 507926.5547 ( 0.00%) 506519.3692 * -0.28%* Hmean faults/cpu-4 479119.7051 ( 0.00%) 481333.6802 * 0.46%* Hmean faults/cpu-7 452880.2961 ( 0.00%) 455845.6211 * 0.65%* Hmean faults/cpu-12 347639.1021 ( 0.00%) 352004.2254 * 1.26%* Hmean faults/cpu-21 200061.2238 ( 0.00%) 229597.0317 * 14.76%* Hmean faults/cpu-30 145251.2001 ( 0.00%) 164202.5067 * 13.05%* Hmean faults/cpu-48 106848.4434 ( 0.00%) 120641.5504 * 12.91%* Hmean faults/cpu-56 92472.3835 ( 0.00%) 103464.7916 * 11.89%* Hmean faults/sec-1 507566.1468 ( 0.00%) 506139.0811 * -0.28%* Hmean faults/sec-4 1880478.2402 ( 0.00%) 1886795.6329 * 0.34%* Hmean faults/sec-7 3106394.3438 ( 0.00%) 3140550.7485 * 1.10%* Hmean faults/sec-12 4061358.4795 ( 0.00%) 4112477.0206 * 1.26%* Hmean faults/sec-21 3988619.1169 ( 0.00%) 4577747.1436 * 14.77%* Hmean faults/sec-30 3909839.5449 ( 0.00%) 4311052.2787 * 10.26%* Hmean faults/sec-48 4761108.4691 ( 0.00%) 5283790.5026 * 10.98%* Hmean faults/sec-56 4885561.4590 ( 0.00%) 5415839.4045 * 10.85%*Changes since v8 [4]:
- Change subject for the cover letter, per Vlastimil Babka
- Added Reviewed-by and Acked-by, per Vlastimil Babka
- Added static check for no-limit case in__refcount_add_not_zero_limited, per David Laight
- Fixedvma_refcount_put()to callrwsem_release()unconditionally, per Hillf Danton and Vlastimil Babka
- Use a copy ofvma->vm_mminvma_refcount_put()in case vma is freed from under us, per Vlastimil Babka
- Removed extra rcu_read_lock()/rcu_read_unlock() invma_end_read(), per Vlastimil Babka
- Changed__vma_enter_locked()parameter to centralize refcount logic, per Vlastimil Babka
- Amended description invm_lockreplacement patch explaining the effects of the patch onvm_area_structsize, per Vlastimil Babka
- Addedvm_area_structmember regrouping patch [5] into the series
- Renamedvma_copy()intovm_area_init_from(), per Liam R. Howlett
- Added a comment forvm_area_structto updatevm_area_init_from()when adding new members, per Vlastimil Babka
- Updated a comment about unstablesrc->shared.rbwhen copying a vma invm_area_init_from(), per Vlastimil Babka[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/ZsQyI%2F087V34JoIt@xsang-OptiPlex-9020/
[3] https://lore.kernel.org/all/CAJuCfpEisU8Lfe96AYJDZ+OM4NoPmnw9bP53cT_kbfP_pR+-2g@mail.gmail.com/
[4] https://lore.kernel.org/all/[email protected]/
[5] https://lore.kernel.org/all/[email protected]/Patchset applies over mm-unstable after reverting v8 (current SHA range:
235b5129cb7b-9e6b24c58985)Suren Baghdasaryan (17):
mm: introduce vma_start_read_locked{_nested} helpers
mm: move per-vma lock intovm_area_struct
mm: mark vma as detached until it's added into vma tree
mm: introducevma_iter_store_attached()to use with attached vmas
mm: mark vmas detached upon exit
types: move struct rcuwait intotypes.h
mm: allow vma_start_read_locked/vma_start_read_locked_nested to fail
mm: movemmap_init_lock()out of the header file
mm: uninline the main body ofvma_start_write()
refcount: introduce __refcount_{add|inc}_not_zero_limited
mm: replacevm_lockand detached flag with a reference count
mm: move lesser usedvma_area_structmembers into the last cacheline mm/debug: printvm_refcntstate when dumping the vma
mm: remove extravma_numab_state_init()call
mm: preparelock_vma_under_rcu()for vma reuse possibility
mm: make vma cacheSLAB_TYPESAFE_BY_RCU
docs/mm: document latest changes tovm_lockDocumentation/mm/process_addrs.rst | 44 ++++---- include/linux/mm.h | 156 ++++++++++++++++++++++------- include/linux/mm_types.h | 75 +++++++------- include/linux/mmap_lock.h | 6 -- include/linux/rcuwait.h | 13 +-- include/linux/refcount.h | 24 ++++- include/linux/slab.h | 6 -- include/linux/types.h | 12 +++ kernel/fork.c | 129 +++++++++++------------- mm/debug.c | 12 +++ mm/init-mm.c | 1 + mm/memory.c | 97 ++++++++++++++++-- mm/mmap.c | 3 +- mm/userfaultfd.c | 32 +++--- mm/vma.c | 23 ++--- mm/vma.h | 15 ++- tools/testing/vma/linux/atomic.h | 5 + tools/testing/vma/vma_internal.h | 93 ++++++++--------- 18 files changed, 465 insertions(+), 281 deletions(-)--
2.47.1.613.gc27f4b7a9f-goog
Quoted 66 lines · Suren Baghdasaryan
Quoted 64 lines · Lorenzo Stoakes
Quoted 13 lines · Suren Baghdasaryan
Quoted 10 lines · Lorenzo Stoakes
A nit on subject, I mean this is part of what this series does, and hey - we have only so much text to put in here - but isn't this both reimplementing per-VMA lock as a refcount _and_ importantly allocating VMAs using the RCU typesafe mechanism?
Do we have to do both in one series? Can we split this out? I mean maybe that's just churny and unnecessary, but to me this series is 'allocate VMAs RCU safe and refcount VMA lock' or something like this. Maybe this is nitty... but still :)
Quoted 5 lines
There is "motivational dependency" because one of the main reasons I'm converting the
vm_lockintovm_refcntis to make it easier to addSLAB_TYPESAFE_BY_RCU(see my last reply to Hillf). But technically we can leave theSLAB_TYPESAFE_BY_RCUout of this series if that makes thighs easier. That would be the 2 patches at the end:Right yeah... maybe it's better to do it in one hit.
Quoted 11 lines
mm: prepare
lock_vma_under_rcu()for vma reuse possibility
mm: make vma cacheSLAB_TYPESAFE_BY_RCUI made sure that each patch is bisectable, so there should not be a problem with tracking issues.
Quoted 3 lines
One general comment here - this is a really major change in how this stuff works, and yet I don't see any tests anywhere in the series.
Quoted 7 lines
Hmm. I was diligently updating the tests to reflect the replacement of
vm_lockwithvm_refcntand adding assertions for detach/attach cases. This actually reminds me that I missed updadingvm_area_structinvma_internal.hfor the member regrouping patch; will add that. I think the only part that did not affect tests isSLAB_TYPESAFE_BY_RCUbut I was not sure what kind of testing I can add for that. Any suggestions would be welcomed.And to be clear I'm super grateful you did that :) thanks, be good to change the member regrouping thing also.
But that doesn't change the fact that this series has exactly zero tests for it. And for something so broad, it feels like a big issue, we really want to be careful with something so big here.
You've also noticed that I've cleverly failed to _actually_ suggest
SLAB_TYPESAFE_BY_RCUtests, and mea culpa - it's super hard to think of how to test that.Liam has experience doing RCU testing this for the maple tree stuff, but it wasn't pretty and wasn't easy and would probably require massive rework to expose this stuff to some viable testing environment, or in other words - is unworkable.
HOWEVER, I feel like maybe we could try to create scenarios where we might trigger reuse bugs?
Perhaps some userland code, perhaps even constrained by cgroup, that maps a ton of stuff and unmaps in a loop in parallel?
Quoted 7 lines
Quoted 1 line
Perhaps create scenarios with shared memory where we up refcounts a lot too?
I have this old
spf_test
(https://github.com/surenbaghdasaryan/spf_test/blob/main/spf_test.c)
which I often use to weed out vma locking issues because it starts multiple threads doing mmap + page faults. Perhaps we can repackage it into a test/benchmark for testing contention on mmap/vma locks?
Ah nice yeah that sounds good!
Quoted 33 lines
Quoted 15 lines
Anyway, this is necessarily nebulous without further investigation, what I was thinking more concretely is:
Using the VMA userland testing:
1. Assert reference count correctness across locking scenarios and various VMA operations.
2. Assert correct detached/not detached state across different scenarios.This won't quite be complete as not everything is separated out quite enough to allow things like process tear down/forking etc. to be explicitly tested but you can unit tests the VMA bits at least.
Quoted 5 lines
One note on this, I intend to split the
vma.cfile into multiple files intools/testing/vma/so if you add tests here it'd be worth probably putting them into a new file.I'm happy to help with this if you need any assistance, feel free to ping!
As a starting point I was thinking of changing vma_assert_attached()/vma_assert_detached() and vma_mark_attached()/vma_mark_detached() to return a bool and use
WARN_ON_ONCE()(to address your concern about asserts being dependent onCONFIG_DEBUG_VM) like this:static inline bool
vma_assert_detached(){ return !WARN_ON_ONCE(atomic_read(&vma->vm_refcnt)); }static inline bool
vma_mark_attached(struct vm_area_struct *vma)
Quoted 9 lines
{
vma_assert_write_locked(vma);
if (!vma_assert_detached(vma))
return false;
atomic_set(&vma->vm_refcnt, 1);
return true;
}Sounds good!
Quoted 7 lines
With that we can add correctness checks in the
tools/testing/vma/vma.cfor different states, for example in thealloc_and_link_vma()we can check that aftervma_link()the vma is indeed attached:ASSERT_TRUE(vma_assert_attached(vma));
This might not cover all states but is probably a good starting point. WDYT?
Yeah, this is a good starting point.
I think also we should add explicit asserts in the merge tests to ensure attachment.
I mean part of this is adding more tests in general for standard operations, but I don't want to be silly and suggest you need to do that.
I think this forms a decent basis.
Quoted 191 lines
Quoted 191 lines
Sorry to put this on you so late in the series, I realise it's annoying, but I feel like things have changed a lot and obviously aggregated with two series in one in effect and these are genuine concerns that at this stage I feel like we need to try to at least make some headway on.
Quoted 10 lines
Quoted 10 lines
I know it's tricky to write tests for this, but the new VMA testing environment should make it possible to test a _lot_ more than we previously could.
However due to some (*ahem*) interesting distribution of where functions are, most notably stuff in
kernel/fork.c, I guess we can't test _everything_ there effectively.Quoted 5 lines
Quoted 2 lines
But I do feel like we should be able to do better than having absolutely no testing added for this?
Again, I'm open to suggestions for
SLAB_TYPESAFE_BY_RCUtesting but for the rest I thought the tests were modified accordingly.See above ^
Quoted 9 lines
Quoted 6 lines
I think there's definitely quite a bit you could test now, at least in asserting fundamentals in
tools/testing/vma/vma.c.This can cover at least detached state asserts in various scenarios.
Ok, you mean to check that VMA re-attachment/re-detachment would trigger assertions? I'll look into adding tests for that.
Yeah this is one, see above :)
Quoted 6 lines
Quoted 6 lines
But that won't cover off the really gnarly stuff here around RCU slab allocation, and determining precisely how to test that in a sensible way is maybe less clear.
Quoted 6 lines
Quoted 3 lines
But I'd like to see _something_ here please, this is more or less fundamentally changing how all VMAs are allocated and to just have nothing feels unfortunate.
Again, I'm open to suggestions on what kind of testing I can add for
SLAB_TYPESAFE_BY_RCUchange.See above
Quoted 6 lines
Quoted 5 lines
I'm already nervous because we've hit issues coming up to v9 and we're not 100% sure if a recent syzkaller is related to these changes or not, I'm not sure how much we can get assurances with tests but I'd like something.
Quoted 8 lines
If you are referring to the issue at [1], I think David ran the syzcaller against mm-stable that does not contain this patchset and the issue still triggered (see [2]). This of course does not guarantee that this patchset has no other issues :) I'll try adding tests for re-attaching, re-detaching and welcome ideas on how to test
SLAB_TYPESAFE_BY_RCUtransition.
Thanks,
Suren.OK that's reassuring!
Quoted 122 lines
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/Quoted 118 lines
Thanks!
Quoted 114 lines · Suren Baghdasaryan
Back when per-vma locks were introduces,
vm_lockwas moved out ofvm_area_structin [1] because of the performance regression caused by false cacheline sharing. Recent investigation [2] revealed that the regressions is limited to a rather old Broadwell microarchitecture and even there it can be mitigated by disabling adjacent cacheline prefetching, see [3].
Splitting single logical structure into multiple ones leads to more complicated management, extra pointer dereferences and overall less maintainable code. When that split-away part is a lock, it complicates things even further. With no performance benefits, there are no reasons for this split. Merging thevm_lockback intovm_area_structalso allowsvm_area_structto useSLAB_TYPESAFE_BY_RCUlater in this patchset. This patchset:
1. movesvm_lockback intovm_area_struct, aligning it at the cacheline boundary and changing the cache to be cacheline-aligned to minimize cacheline sharing;
2. changesvm_area_structinitialization to mark new vma as detached until it is inserted into vma tree;
3. replacesvm_lockandvma->detachedflag with a reference counter;
4. regroupsvm_area_structmembers to fit them into 3 cachelines;
5. changesvm_area_structcache toSLAB_TYPESAFE_BY_RCUto allow for their reuse and to minimizecall_rcu()calls.Pagefault microbenchmarks show performance improvement:
Hmean faults/cpu-1 507926.5547 ( 0.00%) 506519.3692 * -0.28%* Hmean faults/cpu-4 479119.7051 ( 0.00%) 481333.6802 * 0.46%* Hmean faults/cpu-7 452880.2961 ( 0.00%) 455845.6211 * 0.65%* Hmean faults/cpu-12 347639.1021 ( 0.00%) 352004.2254 * 1.26%* Hmean faults/cpu-21 200061.2238 ( 0.00%) 229597.0317 * 14.76%* Hmean faults/cpu-30 145251.2001 ( 0.00%) 164202.5067 * 13.05%* Hmean faults/cpu-48 106848.4434 ( 0.00%) 120641.5504 * 12.91%* Hmean faults/cpu-56 92472.3835 ( 0.00%) 103464.7916 * 11.89%* Hmean faults/sec-1 507566.1468 ( 0.00%) 506139.0811 * -0.28%* Hmean faults/sec-4 1880478.2402 ( 0.00%) 1886795.6329 * 0.34%* Hmean faults/sec-7 3106394.3438 ( 0.00%) 3140550.7485 * 1.10%* Hmean faults/sec-12 4061358.4795 ( 0.00%) 4112477.0206 * 1.26%* Hmean faults/sec-21 3988619.1169 ( 0.00%) 4577747.1436 * 14.77%* Hmean faults/sec-30 3909839.5449 ( 0.00%) 4311052.2787 * 10.26%* Hmean faults/sec-48 4761108.4691 ( 0.00%) 5283790.5026 * 10.98%* Hmean faults/sec-56 4885561.4590 ( 0.00%) 5415839.4045 * 10.85%*Changes since v8 [4]:
- Change subject for the cover letter, per Vlastimil Babka
- Added Reviewed-by and Acked-by, per Vlastimil Babka
- Added static check for no-limit case in__refcount_add_not_zero_limited, per David Laight
- Fixedvma_refcount_put()to callrwsem_release()unconditionally, per Hillf Danton and Vlastimil Babka
- Use a copy ofvma->vm_mminvma_refcount_put()in case vma is freed from under us, per Vlastimil Babka
- Removed extra rcu_read_lock()/rcu_read_unlock() invma_end_read(), per Vlastimil Babka
- Changed__vma_enter_locked()parameter to centralize refcount logic, per Vlastimil Babka
- Amended description invm_lockreplacement patch explaining the effects of the patch onvm_area_structsize, per Vlastimil Babka
- Addedvm_area_structmember regrouping patch [5] into the series
- Renamedvma_copy()intovm_area_init_from(), per Liam R. Howlett
- Added a comment forvm_area_structto updatevm_area_init_from()when adding new members, per Vlastimil Babka
- Updated a comment about unstablesrc->shared.rbwhen copying a vma invm_area_init_from(), per Vlastimil Babka[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/ZsQyI%2F087V34JoIt@xsang-OptiPlex-9020/
[3] https://lore.kernel.org/all/CAJuCfpEisU8Lfe96AYJDZ+OM4NoPmnw9bP53cT_kbfP_pR+-2g@mail.gmail.com/
[4] https://lore.kernel.org/all/[email protected]/
[5] https://lore.kernel.org/all/[email protected]/Patchset applies over mm-unstable after reverting v8 (current SHA range:
235b5129cb7b-9e6b24c58985)Suren Baghdasaryan (17):
mm: introduce vma_start_read_locked{_nested} helpers
mm: move per-vma lock intovm_area_struct
mm: mark vma as detached until it's added into vma tree
mm: introducevma_iter_store_attached()to use with attached vmas
mm: mark vmas detached upon exit
types: move struct rcuwait intotypes.h
mm: allow vma_start_read_locked/vma_start_read_locked_nested to fail
mm: movemmap_init_lock()out of the header file
mm: uninline the main body ofvma_start_write()
refcount: introduce __refcount_{add|inc}_not_zero_limited
mm: replacevm_lockand detached flag with a reference count
mm: move lesser usedvma_area_structmembers into the last cacheline mm/debug: printvm_refcntstate when dumping the vma
mm: remove extravma_numab_state_init()call
mm: preparelock_vma_under_rcu()for vma reuse possibility
mm: make vma cacheSLAB_TYPESAFE_BY_RCU
docs/mm: document latest changes tovm_lockDocumentation/mm/process_addrs.rst | 44 ++++---- include/linux/mm.h | 156 ++++++++++++++++++++++------- include/linux/mm_types.h | 75 +++++++------- include/linux/mmap_lock.h | 6 -- include/linux/rcuwait.h | 13 +-- include/linux/refcount.h | 24 ++++- include/linux/slab.h | 6 -- include/linux/types.h | 12 +++ kernel/fork.c | 129 +++++++++++------------- mm/debug.c | 12 +++ mm/init-mm.c | 1 + mm/memory.c | 97 ++++++++++++++++-- mm/mmap.c | 3 +- mm/userfaultfd.c | 32 +++--- mm/vma.c | 23 ++--- mm/vma.h | 15 ++- tools/testing/vma/linux/atomic.h | 5 + tools/testing/vma/vma_internal.h | 93 ++++++++--------- 18 files changed, 465 insertions(+), 281 deletions(-)--
2.47.1.613.gc27f4b7a9f-goog
Andrew Morton
maintainerQuoted 33 lines · On Mon, 13 Jan 2025 12:14:19 +0000 Lorenzo Stoakes…
A nit on subject, I mean this is part of what this series does, and hey - we have only so much text to put in here - but isn't this both reimplementing per-VMA lock as a refcount _and_ importantly allocating VMAs using the RCU typesafe mechanism?
Do we have to do both in one series? Can we split this out? I mean maybe that's just churny and unnecessary, but to me this series is 'allocate VMAs RCU safe and refcount VMA lock' or something like this. Maybe this is nitty... but still :)
One general comment here - this is a really major change in how this stuff works, and yet I don't see any tests anywhere in the series.
I know it's tricky to write tests for this, but the new VMA testing environment should make it possible to test a _lot_ more than we previously could.
However due to some (*ahem*) interesting distribution of where functions are, most notably stuff in
kernel/fork.c, I guess we can't test _everything_ there effectively.But I do feel like we should be able to do better than having absolutely no testing added for this?
I think there's definitely quite a bit you could test now, at least in asserting fundamentals in
tools/testing/vma/vma.c.This can cover at least detached state asserts in various scenarios.
But that won't cover off the really gnarly stuff here around RCU slab allocation, and determining precisely how to test that in a sensible way is maybe less clear.
Quoted 7 lines
But I'd like to see _something_ here please, this is more or less fundamentally changing how all VMAs are allocated and to just have nothing feels unfortunate.
I'm already nervous because we've hit issues coming up to v9 and we're not 100% sure if a recent syzkaller is related to these changes or not, I'm not sure how much we can get assurances with tests but I'd like something.
Thanks.
Yes, we're at -rc7 and this series is rather in panic mode and it seems unnecessarily risky so I'm inclined to set it aside for this cycle.
If the series is considered super desirable and if people are confident that we can address any remaining glitches during two months of -rc then sure, we could push the envelope a bit. But I don't believe this is the case so I'm thinking let's give ourselves another cycle to get this all sorted out?
Suren Baghdasaryan
maintainerQuoted 46 lines · Andrew Morton
Quoted 33 lines · On Mon, 13 Jan 2025 12:14:19 +0000 Lorenzo Stoakes…
A nit on subject, I mean this is part of what this series does, and hey - we have only so much text to put in here - but isn't this both reimplementing per-VMA lock as a refcount _and_ importantly allocating VMAs using the RCU typesafe mechanism?
Do we have to do both in one series? Can we split this out? I mean maybe that's just churny and unnecessary, but to me this series is 'allocate VMAs RCU safe and refcount VMA lock' or something like this. Maybe this is nitty... but still :)
One general comment here - this is a really major change in how this stuff works, and yet I don't see any tests anywhere in the series.
I know it's tricky to write tests for this, but the new VMA testing environment should make it possible to test a _lot_ more than we previously could.
However due to some (*ahem*) interesting distribution of where functions are, most notably stuff in
kernel/fork.c, I guess we can't test _everything_ there effectively.But I do feel like we should be able to do better than having absolutely no testing added for this?
I think there's definitely quite a bit you could test now, at least in asserting fundamentals in
tools/testing/vma/vma.c.This can cover at least detached state asserts in various scenarios.
But that won't cover off the really gnarly stuff here around RCU slab allocation, and determining precisely how to test that in a sensible way is maybe less clear.
Quoted 7 lines
But I'd like to see _something_ here please, this is more or less fundamentally changing how all VMAs are allocated and to just have nothing feels unfortunate.
I'm already nervous because we've hit issues coming up to v9 and we're not 100% sure if a recent syzkaller is related to these changes or not, I'm not sure how much we can get assurances with tests but I'd like something.
Thanks.
Quoted 8 lines
Yes, we're at -rc7 and this series is rather in panic mode and it seems unnecessarily risky so I'm inclined to set it aside for this cycle.
If the series is considered super desirable and if people are confident that we can address any remaining glitches during two months of -rc then sure, we could push the envelope a bit. But I don't believe this is the case so I'm thinking let's give ourselves another cycle to get this all sorted out?
I didn't think this series was in panic mode with one real issue that is not hard to address (memory ordering in __refcount_inc_not_zero_limited()) but I'm obviously biased and might be missing the big picture. In any case, if it makes people nervous I have no objections to your plan.
Thanks,
Suren.
Andrew Morton
maintainerQuoted 12 lines · On Mon, 13 Jan 2025 18:53:11 -0800 Suren Baghdasaryan <[email protected]> wrote:
Quoted 10 lines · Andrew Morton
Yes, we're at -rc7 and this series is rather in panic mode and it seems unnecessarily risky so I'm inclined to set it aside for this cycle.
If the series is considered super desirable and if people are confident that we can address any remaining glitches during two months of -rc then sure, we could push the envelope a bit. But I don't believe this is the case so I'm thinking let's give ourselves another cycle to get this all sorted out?
Quoted 5 lines
I didn't think this series was in panic mode with one real issue that is not hard to address (memory ordering in
__refcount_inc_not_zero_limited()) but I'm obviously biased and might be missing the big picture. In any case, if it makes people nervous I have no objections to your plan.
Well, I'm soliciting opinions here. What do others think?
And do you see much urgency with these changes?
Quoted 20 lines · Andrew Morton
Quoted 12 lines · On Mon, 13 Jan 2025 18:53:11 -0800 Suren Baghdasaryan <[email protected]> wrote:
Quoted 10 lines · Andrew Morton
Yes, we're at -rc7 and this series is rather in panic mode and it seems unnecessarily risky so I'm inclined to set it aside for this cycle.
If the series is considered super desirable and if people are confident that we can address any remaining glitches during two months of -rc then sure, we could push the envelope a bit. But I don't believe this is the case so I'm thinking let's give ourselves another cycle to get this all sorted out?
Quoted 5 lines
I didn't think this series was in panic mode with one real issue that is not hard to address (memory ordering in
__refcount_inc_not_zero_limited()) but I'm obviously biased and might be missing the big picture. In any case, if it makes people nervous I have no objections to your plan.
Quoted 3 lines
Well, I'm soliciting opinions here. What do others think?
And do you see much urgency with these changes?
I don't see the urgency and at this point giving it more time seems wise. Seems like v10 won't be exactly trivial as we'll change from refcount_t to atomic_t? And I'd like to see PeterZ review the lockdep parts too.
On Tue, 14 Jan 2025 10:09:42 +0100 Vlastimil Babka <[email protected]>
Quoted 9 lines · Andrew Morton
Quoted 4 lines
Well, I'm soliciting opinions here. What do others think?
And do you see much urgency with these changes?
I don't see the urgency and at this point giving it more time seems wise. Seems like v10 won't be exactly trivial as we'll change from
refcount_ttoatomic_t? And I'd like to see PeterZ review the lockdep parts too.
+1
Quoted 20 lines · Andrew Morton
Quoted 12 lines · On Mon, 13 Jan 2025 18:53:11 -0800 Suren Baghdasaryan <[email protected]> wrote:
Quoted 10 lines · Andrew Morton
Yes, we're at -rc7 and this series is rather in panic mode and it seems unnecessarily risky so I'm inclined to set it aside for this cycle.
If the series is considered super desirable and if people are confident that we can address any remaining glitches during two months of -rc then sure, we could push the envelope a bit. But I don't believe this is the case so I'm thinking let's give ourselves another cycle to get this all sorted out?
Quoted 5 lines
I didn't think this series was in panic mode with one real issue that is not hard to address (memory ordering in
__refcount_inc_not_zero_limited()) but I'm obviously biased and might be missing the big picture. In any case, if it makes people nervous I have no objections to your plan.
Quoted 3 lines
Well, I'm soliciting opinions here. What do others think?
And do you see much urgency with these changes?
With apologies to Suren (genuinely!) who is doing great work and is super-responsive here, this really needs another cycle in my opinion.
As Vlastimil points out there's some non-trivial bits to go, but I am also firmly of the opinion we need to have as much testing as is practical here.
I don't think this is urgent on any timeline so I'd like to join Vlastimil to firmly but politely push for this to land in 6.15 rather than 6.14.
Just to reiterate - this is absolutely no reflection on Suren who has been really great here - it is purely a product of the complexity and scope of this change.
Quoted 20 lines · Andrew Morton
Quoted 12 lines · On Mon, 13 Jan 2025 18:53:11 -0800 Suren Baghdasaryan <[email protected]> wrote:
Quoted 10 lines · Andrew Morton
Yes, we're at -rc7 and this series is rather in panic mode and it seems unnecessarily risky so I'm inclined to set it aside for this cycle.
If the series is considered super desirable and if people are confident that we can address any remaining glitches during two months of -rc then sure, we could push the envelope a bit. But I don't believe this is the case so I'm thinking let's give ourselves another cycle to get this all sorted out?
Quoted 5 lines
I didn't think this series was in panic mode with one real issue that is not hard to address (memory ordering in
__refcount_inc_not_zero_limited()) but I'm obviously biased and might be missing the big picture. In any case, if it makes people nervous I have no objections to your plan.
Quoted 4 lines
Well, I'm soliciting opinions here. What do others think?
And do you see much urgency with these changes?
I think it's in good shape, but more time for this change is probably the right approach.
I don't think it's had enough testing time with the changes since v7. The series has had significant changes, with the side effect of invalidating some of the test time.
I really like what it does, but if Suren doesn't need it upstream for some reason then I'd say we leave it to soak longer.
If he does need it upstream, we can deal with any fallout and fixes - it will have minimum long term effects as it's not an LTS.
Thanks,
Liam
Suren Baghdasaryan
maintainerQuoted 34 lines · Liam R. Howlett' via kernel-team
Quoted 20 lines · Andrew Morton
Quoted 12 lines · On Mon, 13 Jan 2025 18:53:11 -0800 Suren Baghdasaryan <[email protected]> wrote:
Quoted 10 lines · Andrew Morton
Yes, we're at -rc7 and this series is rather in panic mode and it seems unnecessarily risky so I'm inclined to set it aside for this cycle.
If the series is considered super desirable and if people are confident that we can address any remaining glitches during two months of -rc then sure, we could push the envelope a bit. But I don't believe this is the case so I'm thinking let's give ourselves another cycle to get this all sorted out?
Quoted 5 lines
I didn't think this series was in panic mode with one real issue that is not hard to address (memory ordering in
__refcount_inc_not_zero_limited()) but I'm obviously biased and might be missing the big picture. In any case, if it makes people nervous I have no objections to your plan.Quoted 4 lines
Well, I'm soliciting opinions here. What do others think?
And do you see much urgency with these changes?
I think it's in good shape, but more time for this change is probably the right approach.
I don't think it's had enough testing time with the changes since v7. The series has had significant changes, with the side effect of invalidating some of the test time.
Quoted 5 lines
I really like what it does, but if Suren doesn't need it upstream for some reason then I'd say we leave it to soak longer.
If he does need it upstream, we can deal with any fallout and fixes - it will have minimum long term effects as it's not an LTS.
Thanks for voicing your opinion, folks! There is no real urgency and no objections from me to wait until the next cycle.
I'll be posting v10 shortly purely for reviews while this is fresh on people's mind, and with the understanding that it won't be picked up by Andrew.
Thanks,
Suren.
Quoted 6 lines
Thanks,
LiamTo unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
Quoted 42 lines · Suren Baghdasaryan
Quoted 34 lines · Liam R. Howlett' via kernel-team
Quoted 20 lines · Andrew Morton
Quoted 12 lines · On Mon, 13 Jan 2025 18:53:11 -0800 Suren Baghdasaryan <[email protected]> wrote:
Quoted 10 lines · Andrew Morton
Yes, we're at -rc7 and this series is rather in panic mode and it seems unnecessarily risky so I'm inclined to set it aside for this cycle.
If the series is considered super desirable and if people are confident that we can address any remaining glitches during two months of -rc then sure, we could push the envelope a bit. But I don't believe this is the case so I'm thinking let's give ourselves another cycle to get this all sorted out?
Quoted 5 lines
I didn't think this series was in panic mode with one real issue that is not hard to address (memory ordering in
__refcount_inc_not_zero_limited()) but I'm obviously biased and might be missing the big picture. In any case, if it makes people nervous I have no objections to your plan.Quoted 4 lines
Well, I'm soliciting opinions here. What do others think?
And do you see much urgency with these changes?
I think it's in good shape, but more time for this change is probably the right approach.
I don't think it's had enough testing time with the changes since v7. The series has had significant changes, with the side effect of invalidating some of the test time.
Quoted 5 lines
I really like what it does, but if Suren doesn't need it upstream for some reason then I'd say we leave it to soak longer.
If he does need it upstream, we can deal with any fallout and fixes - it will have minimum long term effects as it's not an LTS.
Quoted 7 lines
Thanks for voicing your opinion, folks! There is no real urgency and no objections from me to wait until the next cycle.
I'll be posting v10 shortly purely for reviews while this is fresh on people's mind, and with the understanding that it won't be picked up by Andrew.
Thanks,
Suren.
(From my side :) Thanks, and definitely no reflection on quality and your responsiveness has been amazing, just a reflection of the complexity of this change.
Quoted 7 lines
Quoted 7 lines
Thanks,
LiamTo unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
Suren Baghdasaryan
maintainerQuoted 52 lines · Lorenzo Stoakes
Quoted 42 lines · Suren Baghdasaryan
Quoted 34 lines · Liam R. Howlett' via kernel-team
Quoted 20 lines · Andrew Morton
Quoted 12 lines · On Mon, 13 Jan 2025 18:53:11 -0800 Suren Baghdasaryan <[email protected]> wrote:
Quoted 10 lines · Andrew Morton
Yes, we're at -rc7 and this series is rather in panic mode and it seems unnecessarily risky so I'm inclined to set it aside for this cycle.
If the series is considered super desirable and if people are confident that we can address any remaining glitches during two months of -rc then sure, we could push the envelope a bit. But I don't believe this is the case so I'm thinking let's give ourselves another cycle to get this all sorted out?
Quoted 5 lines
I didn't think this series was in panic mode with one real issue that is not hard to address (memory ordering in
__refcount_inc_not_zero_limited()) but I'm obviously biased and might be missing the big picture. In any case, if it makes people nervous I have no objections to your plan.Quoted 4 lines
Well, I'm soliciting opinions here. What do others think?
And do you see much urgency with these changes?
I think it's in good shape, but more time for this change is probably the right approach.
I don't think it's had enough testing time with the changes since v7. The series has had significant changes, with the side effect of invalidating some of the test time.
Quoted 5 lines
I really like what it does, but if Suren doesn't need it upstream for some reason then I'd say we leave it to soak longer.
If he does need it upstream, we can deal with any fallout and fixes - it will have minimum long term effects as it's not an LTS.
Quoted 7 lines
Thanks for voicing your opinion, folks! There is no real urgency and no objections from me to wait until the next cycle.
I'll be posting v10 shortly purely for reviews while this is fresh on people's mind, and with the understanding that it won't be picked up by Andrew.
Thanks,
Suren.
Quoted 3 lines
(From my side :) Thanks, and definitely no reflection on quality and your responsiveness has been amazing, just a reflection of the complexity of this change.
No worries, I understand and accept the reasoning.
And thanks for sugar coating the pill, it made it easier to swallow :)
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Thanks,
LiamTo unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
Hi Suren,
I tested the patch-series on AMD EPYC Zen 5 system (2-socket, 64-core per socket with SMT Enabled, 4 NUMA nodes) using mmtests's PFT (config-workload-pft-threads) benchmark on mm-unstable.
Quoted 24 lines · Suren Baghdasaryan
Back when per-vma locks were introduces,
vm_lockwas moved out ofvm_area_structin [1] because of the performance regression caused by false cacheline sharing. Recent investigation [2] revealed that the regressions is limited to a rather old Broadwell microarchitecture and even there it can be mitigated by disabling adjacent cacheline prefetching, see [3].
Splitting single logical structure into multiple ones leads to more complicated management, extra pointer dereferences and overall less maintainable code. When that split-away part is a lock, it complicates things even further. With no performance benefits, there are no reasons for this split. Merging thevm_lockback intovm_area_structalso allowsvm_area_structto useSLAB_TYPESAFE_BY_RCUlater in this patchset. This patchset:
1. movesvm_lockback intovm_area_struct, aligning it at the cacheline boundary and changing the cache to be cacheline-aligned to minimize cacheline sharing;
2. changesvm_area_structinitialization to mark new vma as detached until it is inserted into vma tree;
3. replacesvm_lockandvma->detachedflag with a reference counter;
4. regroupsvm_area_structmembers to fit them into 3 cachelines;
5. changesvm_area_structcache toSLAB_TYPESAFE_BY_RCUto allow for their reuse and to minimizecall_rcu()calls.Pagefault microbenchmarks show performance improvement:
Quoted 16 lines
Hmean faults/cpu-1 507926.5547 ( 0.00%) 506519.3692 * -0.28%* Hmean faults/cpu-4 479119.7051 ( 0.00%) 481333.6802 * 0.46%* Hmean faults/cpu-7 452880.2961 ( 0.00%) 455845.6211 * 0.65%* Hmean faults/cpu-12 347639.1021 ( 0.00%) 352004.2254 * 1.26%* Hmean faults/cpu-21 200061.2238 ( 0.00%) 229597.0317 * 14.76%* Hmean faults/cpu-30 145251.2001 ( 0.00%) 164202.5067 * 13.05%* Hmean faults/cpu-48 106848.4434 ( 0.00%) 120641.5504 * 12.91%* Hmean faults/cpu-56 92472.3835 ( 0.00%) 103464.7916 * 11.89%* Hmean faults/sec-1 507566.1468 ( 0.00%) 506139.0811 * -0.28%* Hmean faults/sec-4 1880478.2402 ( 0.00%) 1886795.6329 * 0.34%* Hmean faults/sec-7 3106394.3438 ( 0.00%) 3140550.7485 * 1.10%* Hmean faults/sec-12 4061358.4795 ( 0.00%) 4112477.0206 * 1.26%* Hmean faults/sec-21 3988619.1169 ( 0.00%) 4577747.1436 * 14.77%* Hmean faults/sec-30 3909839.5449 ( 0.00%) 4311052.2787 * 10.26%* Hmean faults/sec-48 4761108.4691 ( 0.00%) 5283790.5026 * 10.98%* Hmean faults/sec-56 4885561.4590 ( 0.00%) 5415839.4045 * 10.85%*
Results:
mm-unstable-vanilla mm-unstable-v9-pervma-lock
Hmean faults/cpu-1 2018530.3023 ( 0.00%) 2051456.8039 ( 1.63%) Hmean faults/cpu-4 718869.0234 ( 0.00%) 720985.6986 ( 0.29%) Hmean faults/cpu-7 377965.1187 ( 0.00%) 368305.2802 ( -2.56%) Hmean faults/cpu-12 215502.5334 ( 0.00%) 212764.0061 ( -1.27%) Hmean faults/cpu-21 149946.1873 ( 0.00%) 150688.2173 ( 0.49%) Hmean faults/cpu-30 142379.7863 ( 0.00%) 143677.5012 ( 0.91%) Hmean faults/cpu-48 139625.5003 ( 0.00%) 156217.1383 * 11.88%* Hmean faults/cpu-79 119093.6132 ( 0.00%) 140501.1787 * 17.98%* Hmean faults/cpu-110 102446.6879 ( 0.00%) 114128.7155 ( 11.40%) Hmean faults/cpu-128 96640.4022 ( 0.00%) 109474.8875 ( 13.28%) Hmean faults/sec-1 2018197.4666 ( 0.00%) 2051119.1375 ( 1.63%) Hmean faults/sec-4 2853494.9619 ( 0.00%) 2865639.8350 ( 0.43%) Hmean faults/sec-7 2631049.4283 ( 0.00%) 2564037.1696 ( -2.55%) Hmean faults/sec-12 2570378.4204 ( 0.00%) 2540353.2525 ( -1.17%) Hmean faults/sec-21 3018640.3933 ( 0.00%) 3014396.9773 ( -0.14%) Hmean faults/sec-30 4150723.9209 ( 0.00%) 4167550.4070 ( 0.41%) Hmean faults/sec-48 6459327.6559 ( 0.00%) 7217660.4385 * 11.74%* Hmean faults/sec-79 8977397.1421 ( 0.00%) 10695351.6214 * 19.14%* Hmean faults/sec-110 10590055.2262 ( 0.00%) 12309035.9250 ( 16.23%) Hmean faults/sec-128 11448246.6485 ( 0.00%) 13554648.3823 ( 18.40%)
Please add my:
I would be happy to test future versions if needed.
Thanks,
Shivank
Quoted 71 lines
Changes since v8 [4]:
- Change subject for the cover letter, per Vlastimil Babka
- Added Reviewed-by and Acked-by, per Vlastimil Babka
- Added static check for no-limit case in__refcount_add_not_zero_limited, per David Laight
- Fixedvma_refcount_put()to callrwsem_release()unconditionally, per Hillf Danton and Vlastimil Babka
- Use a copy ofvma->vm_mminvma_refcount_put()in case vma is freed from under us, per Vlastimil Babka
- Removed extra rcu_read_lock()/rcu_read_unlock() invma_end_read(), per Vlastimil Babka
- Changed__vma_enter_locked()parameter to centralize refcount logic, per Vlastimil Babka
- Amended description invm_lockreplacement patch explaining the effects of the patch onvm_area_structsize, per Vlastimil Babka
- Addedvm_area_structmember regrouping patch [5] into the series
- Renamedvma_copy()intovm_area_init_from(), per Liam R. Howlett
- Added a comment forvm_area_structto updatevm_area_init_from()when adding new members, per Vlastimil Babka
- Updated a comment about unstablesrc->shared.rbwhen copying a vma invm_area_init_from(), per Vlastimil Babka[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/ZsQyI%2F087V34JoIt@xsang-OptiPlex-9020/
[3] https://lore.kernel.org/all/CAJuCfpEisU8Lfe96AYJDZ+OM4NoPmnw9bP53cT_kbfP_pR+-2g@mail.gmail.com/
[4] https://lore.kernel.org/all/[email protected]/
[5] https://lore.kernel.org/all/[email protected]/Patchset applies over mm-unstable after reverting v8 (current SHA range:
235b5129cb7b-9e6b24c58985)Suren Baghdasaryan (17):
mm: introduce vma_start_read_locked{_nested} helpers
mm: move per-vma lock intovm_area_struct
mm: mark vma as detached until it's added into vma tree
mm: introducevma_iter_store_attached()to use with attached vmas
mm: mark vmas detached upon exit
types: move struct rcuwait intotypes.h
mm: allow vma_start_read_locked/vma_start_read_locked_nested to fail
mm: movemmap_init_lock()out of the header file
mm: uninline the main body ofvma_start_write()
refcount: introduce __refcount_{add|inc}_not_zero_limited
mm: replacevm_lockand detached flag with a reference count
mm: move lesser usedvma_area_structmembers into the last cacheline mm/debug: printvm_refcntstate when dumping the vma
mm: remove extravma_numab_state_init()call
mm: preparelock_vma_under_rcu()for vma reuse possibility
mm: make vma cacheSLAB_TYPESAFE_BY_RCU
docs/mm: document latest changes tovm_lockDocumentation/mm/process_addrs.rst | 44 ++++---- include/linux/mm.h | 156 ++++++++++++++++++++++------- include/linux/mm_types.h | 75 +++++++------- include/linux/mmap_lock.h | 6 -- include/linux/rcuwait.h | 13 +-- include/linux/refcount.h | 24 ++++- include/linux/slab.h | 6 -- include/linux/types.h | 12 +++ kernel/fork.c | 129 +++++++++++------------- mm/debug.c | 12 +++ mm/init-mm.c | 1 + mm/memory.c | 97 ++++++++++++++++-- mm/mmap.c | 3 +- mm/userfaultfd.c | 32 +++--- mm/vma.c | 23 ++--- mm/vma.h | 15 ++- tools/testing/vma/linux/atomic.h | 5 + tools/testing/vma/vma_internal.h | 93 ++++++++--------- 18 files changed, 465 insertions(+), 281 deletions(-)
Suren Baghdasaryan
maintainerQuoted 64 lines · Shivank Garg
Hi Suren,
I tested the patch-series on AMD EPYC Zen 5 system (2-socket, 64-core per socket with SMT Enabled, 4 NUMA nodes) using mmtests's PFT (config-workload-pft-threads) benchmark on mm-unstable.
Quoted 24 lines · Suren Baghdasaryan
Back when per-vma locks were introduces,
vm_lockwas moved out ofvm_area_structin [1] because of the performance regression caused by false cacheline sharing. Recent investigation [2] revealed that the regressions is limited to a rather old Broadwell microarchitecture and even there it can be mitigated by disabling adjacent cacheline prefetching, see [3].
Splitting single logical structure into multiple ones leads to more complicated management, extra pointer dereferences and overall less maintainable code. When that split-away part is a lock, it complicates things even further. With no performance benefits, there are no reasons for this split. Merging thevm_lockback intovm_area_structalso allowsvm_area_structto useSLAB_TYPESAFE_BY_RCUlater in this patchset. This patchset:
1. movesvm_lockback intovm_area_struct, aligning it at the cacheline boundary and changing the cache to be cacheline-aligned to minimize cacheline sharing;
2. changesvm_area_structinitialization to mark new vma as detached until it is inserted into vma tree;
3. replacesvm_lockandvma->detachedflag with a reference counter;
4. regroupsvm_area_structmembers to fit them into 3 cachelines;
5. changesvm_area_structcache toSLAB_TYPESAFE_BY_RCUto allow for their reuse and to minimizecall_rcu()calls.Pagefault microbenchmarks show performance improvement:
Quoted 16 lines
Hmean faults/cpu-1 507926.5547 ( 0.00%) 506519.3692 * -0.28%* Hmean faults/cpu-4 479119.7051 ( 0.00%) 481333.6802 * 0.46%* Hmean faults/cpu-7 452880.2961 ( 0.00%) 455845.6211 * 0.65%* Hmean faults/cpu-12 347639.1021 ( 0.00%) 352004.2254 * 1.26%* Hmean faults/cpu-21 200061.2238 ( 0.00%) 229597.0317 * 14.76%* Hmean faults/cpu-30 145251.2001 ( 0.00%) 164202.5067 * 13.05%* Hmean faults/cpu-48 106848.4434 ( 0.00%) 120641.5504 * 12.91%* Hmean faults/cpu-56 92472.3835 ( 0.00%) 103464.7916 * 11.89%* Hmean faults/sec-1 507566.1468 ( 0.00%) 506139.0811 * -0.28%* Hmean faults/sec-4 1880478.2402 ( 0.00%) 1886795.6329 * 0.34%* Hmean faults/sec-7 3106394.3438 ( 0.00%) 3140550.7485 * 1.10%* Hmean faults/sec-12 4061358.4795 ( 0.00%) 4112477.0206 * 1.26%* Hmean faults/sec-21 3988619.1169 ( 0.00%) 4577747.1436 * 14.77%* Hmean faults/sec-30 3909839.5449 ( 0.00%) 4311052.2787 * 10.26%* Hmean faults/sec-48 4761108.4691 ( 0.00%) 5283790.5026 * 10.98%* Hmean faults/sec-56 4885561.4590 ( 0.00%) 5415839.4045 * 10.85%*Results:
mm-unstable-vanilla mm-unstable-v9-pervma-lockHmean faults/cpu-1 2018530.3023 ( 0.00%) 2051456.8039 ( 1.63%) Hmean faults/cpu-4 718869.0234 ( 0.00%) 720985.6986 ( 0.29%) Hmean faults/cpu-7 377965.1187 ( 0.00%) 368305.2802 ( -2.56%) Hmean faults/cpu-12 215502.5334 ( 0.00%) 212764.0061 ( -1.27%) Hmean faults/cpu-21 149946.1873 ( 0.00%) 150688.2173 ( 0.49%) Hmean faults/cpu-30 142379.7863 ( 0.00%) 143677.5012 ( 0.91%) Hmean faults/cpu-48 139625.5003 ( 0.00%) 156217.1383 * 11.88%* Hmean faults/cpu-79 119093.6132 ( 0.00%) 140501.1787 * 17.98%* Hmean faults/cpu-110 102446.6879 ( 0.00%) 114128.7155 ( 11.40%) Hmean faults/cpu-128 96640.4022 ( 0.00%) 109474.8875 ( 13.28%) Hmean faults/sec-1 2018197.4666 ( 0.00%) 2051119.1375 ( 1.63%) Hmean faults/sec-4 2853494.9619 ( 0.00%) 2865639.8350 ( 0.43%)
Quoted 8 lines
Hmean faults/sec-7 2631049.4283 ( 0.00%) 2564037.1696 ( -2.55%) Hmean faults/sec-12 2570378.4204 ( 0.00%) 2540353.2525 ( -1.17%) Hmean faults/sec-21 3018640.3933 ( 0.00%) 3014396.9773 ( -0.14%) Hmean faults/sec-30 4150723.9209 ( 0.00%) 4167550.4070 ( 0.41%) Hmean faults/sec-48 6459327.6559 ( 0.00%) 7217660.4385 * 11.74%* Hmean faults/sec-79 8977397.1421 ( 0.00%) 10695351.6214 * 19.14%* Hmean faults/sec-110 10590055.2262 ( 0.00%) 12309035.9250 ( 16.23%) Hmean faults/sec-128 11448246.6485 ( 0.00%) 13554648.3823 ( 18.40%)
Hi Shivank,
Thank you for providing more test results! This looks quite good!
Quoted 5 lines
Please add my:
Tested-by Shivank Garg <[email protected]>I would be happy to test future versions if needed.
That would be very much appreciated! I'll CC you on the next version. Thanks,
Suren.
Quoted 78 lines
Thanks,
ShivankQuoted 71 lines
Changes since v8 [4]:
- Change subject for the cover letter, per Vlastimil Babka
- Added Reviewed-by and Acked-by, per Vlastimil Babka
- Added static check for no-limit case in__refcount_add_not_zero_limited, per David Laight
- Fixedvma_refcount_put()to callrwsem_release()unconditionally, per Hillf Danton and Vlastimil Babka
- Use a copy ofvma->vm_mminvma_refcount_put()in case vma is freed from under us, per Vlastimil Babka
- Removed extra rcu_read_lock()/rcu_read_unlock() invma_end_read(), per Vlastimil Babka
- Changed__vma_enter_locked()parameter to centralize refcount logic, per Vlastimil Babka
- Amended description invm_lockreplacement patch explaining the effects of the patch onvm_area_structsize, per Vlastimil Babka
- Addedvm_area_structmember regrouping patch [5] into the series
- Renamedvma_copy()intovm_area_init_from(), per Liam R. Howlett
- Added a comment forvm_area_structto updatevm_area_init_from()when adding new members, per Vlastimil Babka
- Updated a comment about unstablesrc->shared.rbwhen copying a vma invm_area_init_from(), per Vlastimil Babka[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/ZsQyI%2F087V34JoIt@xsang-OptiPlex-9020/
[3] https://lore.kernel.org/all/CAJuCfpEisU8Lfe96AYJDZ+OM4NoPmnw9bP53cT_kbfP_pR+-2g@mail.gmail.com/
[4] https://lore.kernel.org/all/[email protected]/
[5] https://lore.kernel.org/all/[email protected]/Patchset applies over mm-unstable after reverting v8 (current SHA range:
235b5129cb7b-9e6b24c58985)Suren Baghdasaryan (17):
mm: introduce vma_start_read_locked{_nested} helpers
mm: move per-vma lock intovm_area_struct
mm: mark vma as detached until it's added into vma tree
mm: introducevma_iter_store_attached()to use with attached vmas
mm: mark vmas detached upon exit
types: move struct rcuwait intotypes.h
mm: allow vma_start_read_locked/vma_start_read_locked_nested to fail
mm: movemmap_init_lock()out of the header file
mm: uninline the main body ofvma_start_write()
refcount: introduce __refcount_{add|inc}_not_zero_limited
mm: replacevm_lockand detached flag with a reference count
mm: move lesser usedvma_area_structmembers into the last cacheline mm/debug: printvm_refcntstate when dumping the vma
mm: remove extravma_numab_state_init()call
mm: preparelock_vma_under_rcu()for vma reuse possibility
mm: make vma cacheSLAB_TYPESAFE_BY_RCU
docs/mm: document latest changes tovm_lockDocumentation/mm/process_addrs.rst | 44 ++++---- include/linux/mm.h | 156 ++++++++++++++++++++++------- include/linux/mm_types.h | 75 +++++++------- include/linux/mmap_lock.h | 6 -- include/linux/rcuwait.h | 13 +-- include/linux/refcount.h | 24 ++++- include/linux/slab.h | 6 -- include/linux/types.h | 12 +++ kernel/fork.c | 129 +++++++++++------------- mm/debug.c | 12 +++ mm/init-mm.c | 1 + mm/memory.c | 97 ++++++++++++++++-- mm/mmap.c | 3 +- mm/userfaultfd.c | 32 +++--- mm/vma.c | 23 ++--- mm/vma.h | 15 ++- tools/testing/vma/linux/atomic.h | 5 + tools/testing/vma/vma_internal.h | 93 ++++++++--------- 18 files changed, 465 insertions(+), 281 deletions(-)