linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH RESEND v3 00/10] mm: add and use vma_assert_stabilised() helper
@ 2026-01-22 13:01 Lorenzo Stoakes
  2026-01-22 13:01 ` [PATCH RESEND v3 01/10] mm/vma: rename VMA_LOCK_OFFSET to VM_REFCNT_EXCLUDE_READERS_FLAG Lorenzo Stoakes
                   ` (10 more replies)
  0 siblings, 11 replies; 73+ messages in thread
From: Lorenzo Stoakes @ 2026-01-22 13:01 UTC (permalink / raw)
  To: Andrew Morton
  Cc: David Hildenbrand, Liam R . Howlett, Vlastimil Babka,
	Mike Rapoport, Suren Baghdasaryan, Michal Hocko, Shakeel Butt,
	Jann Horn, linux-mm, linux-kernel, linux-rt-devel,
	Peter Zijlstra, Ingo Molnar, Will Deacon, Boqun Feng,
	Waiman Long, Sebastian Andrzej Siewior, Clark Williams,
	Steven Rostedt

Sometimes we wish to assert that a VMA is stable, that is - the VMA cannot
be changed underneath us. This will be the case if EITHER the VMA lock or
the mmap lock is held.

We already open-code this in two places - anon_vma_name() in mm/madvise.c
and vma_flag_set_atomic() in include/linux/mm.h.

This series adds vma_assert_stablised() which abstract this can be used in
these callsites instead.

This implementation uses lockdep where possible - that is VMA read locks -
which correctly track read lock acquisition/release via:

vma_start_read() ->
rwsem_acquire_read()

vma_start_read_locked() ->
vma_start_read_locked_nested() ->
rwsem_acquire_read()

And:

vma_end_read() ->
vma_refcount_put() ->
rwsem_release()

We don't track the VMA locks using lockdep for VMA write locks, however
these are predicated upon mmap write locks whose lockdep state we do track,
and additionally vma_assert_stabillised() asserts this check if VMA read
lock is not held, so we get lockdep coverage in this case also.

We also add extensive comments to describe what we're doing.

There's some tricky stuff around mmap locking and stabilisation races that
we have to be careful of that I describe in the patch introducing
vma_assert_stabilised().

This change also lays the foundation for future series to add this assert
in further places where we wish to make it clear that we rely upon a
stabilised VMA.

The motivation for this change was precisely this.

Addiitonally, refactor the VMA locks logic to be clearer, less confusing,
self-documenting as far as possible and more easily extendable and
debuggable in future.


v3:
* Added 8 patches of refactoring the VMA lock implementation :)
* Dropped the vma_is_*locked() predicates as too difficult to get entirely
  right.
* Updated vma_assert_locked() to assert what we sensibly can, use lockdep
  if possible and invoke vma_assert_write_locked() to share code as before.
* Took into account extensive feedback received from Vlastimil (thanks! :)

v2:
* Added lockdep as much as possible to the mix as per Peter and Sebastian.
* Added comments to make clear what we're doing in each case.
* I realise I made a mistake in saying the previous duplicative VMA stable
  asserts were wrong - vma_assert_locked() is not a no-op if
  !CONFIG_PER_VMA_LOCK, instead it degrades to asserting that the mmap lock
  is held, so this is correct, though means we'd have checked this twice,
  only triggering an assert the second time.
* Accounted for is_vma_writer_only() case in vma_is_read_locked().
* Accounted for two hideous issues - we cannot check VMA lock first,
  because we may be holding a VMA write lock and be raced by VMA readers of
  _other_ VMA's. If we check the mmap lock first and assert, we may hold a
  VMA read lock and race other threads which hodl the mmap read lock and
  fail an assert. We resolve this by a precise mmap ownership check if
  lockdep is used, and allowing the check to be approximate if no lockdep.
* Added more comments and updated commit logs.
* Dropped Suren's Suggested-by as significant changes in this set (this was for
  the vma_is_read_locked() as a concept).
https://lore.kernel.org/all/cover.1768855783.git.lorenzo.stoakes@oracle.com/

v1:
https://lore.kernel.org/all/cover.1768569863.git.lorenzo.stoakes@oracle.com/


Lorenzo Stoakes (10):
  mm/vma: rename VMA_LOCK_OFFSET to VM_REFCNT_EXCLUDE_READERS_FLAG
  mm/vma: document possible vma->vm_refcnt values and reference comment
  mm/vma: rename is_vma_write_only(), separate out shared refcount put
  mm/vma: add+use vma lockdep acquire/release defines
  mm/vma: de-duplicate __vma_enter_locked() error path
  mm/vma: clean up __vma_enter/exit_locked()
  mm/vma: introduce helper struct + thread through exclusive lock fns
  mm/vma: improve and document __is_vma_write_locked()
  mm/vma: update vma_assert_locked() to use lockdep
  mm/vma: add and use vma_assert_stabilised()

 include/linux/mm.h        |   5 +-
 include/linux/mm_types.h  |  54 ++++++++-
 include/linux/mmap_lock.h | 223 ++++++++++++++++++++++++++++++++++----
 mm/madvise.c              |   4 +-
 mm/mmap_lock.c            | 180 ++++++++++++++++++++----------
 5 files changed, 373 insertions(+), 93 deletions(-)

--
2.52.0


^ permalink raw reply	[flat|nested] 73+ messages in thread

end of thread, other threads:[~2026-01-26 10:04 UTC | newest]

Thread overview: 73+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-01-22 13:01 [PATCH RESEND v3 00/10] mm: add and use vma_assert_stabilised() helper Lorenzo Stoakes
2026-01-22 13:01 ` [PATCH RESEND v3 01/10] mm/vma: rename VMA_LOCK_OFFSET to VM_REFCNT_EXCLUDE_READERS_FLAG Lorenzo Stoakes
2026-01-22 16:26   ` Vlastimil Babka
2026-01-22 16:29     ` Lorenzo Stoakes
2026-01-23 13:52       ` Lorenzo Stoakes
2026-01-22 16:37   ` Suren Baghdasaryan
2026-01-23 13:26     ` Lorenzo Stoakes
2026-01-22 13:01 ` [PATCH RESEND v3 02/10] mm/vma: document possible vma->vm_refcnt values and reference comment Lorenzo Stoakes
2026-01-22 16:48   ` Vlastimil Babka
2026-01-22 17:28     ` Suren Baghdasaryan
2026-01-23 15:06       ` Lorenzo Stoakes
2026-01-23 13:45     ` Lorenzo Stoakes
2026-01-22 13:01 ` [PATCH RESEND v3 03/10] mm/vma: rename is_vma_write_only(), separate out shared refcount put Lorenzo Stoakes
2026-01-22 17:36   ` Vlastimil Babka
2026-01-22 19:31     ` Suren Baghdasaryan
2026-01-23  8:24       ` Vlastimil Babka
2026-01-23 14:52         ` Lorenzo Stoakes
2026-01-23 15:05           ` Vlastimil Babka
2026-01-23 15:07             ` Lorenzo Stoakes
2026-01-23 14:41       ` Lorenzo Stoakes
2026-01-26 10:04         ` Lorenzo Stoakes
2026-01-23 14:02     ` Lorenzo Stoakes
2026-01-22 13:01 ` [PATCH RESEND v3 04/10] mm/vma: add+use vma lockdep acquire/release defines Lorenzo Stoakes
2026-01-22 19:32   ` Suren Baghdasaryan
2026-01-22 19:41     ` Suren Baghdasaryan
2026-01-23  8:41       ` Vlastimil Babka
2026-01-23 15:08         ` Lorenzo Stoakes
2026-01-23 15:00     ` Lorenzo Stoakes
2026-01-23  8:48   ` Vlastimil Babka
2026-01-23 15:10     ` Lorenzo Stoakes
2026-01-22 13:01 ` [PATCH RESEND v3 05/10] mm/vma: de-duplicate __vma_enter_locked() error path Lorenzo Stoakes
2026-01-22 19:39   ` Suren Baghdasaryan
2026-01-23 15:11     ` Lorenzo Stoakes
2026-01-23  8:54   ` Vlastimil Babka
2026-01-23 15:10     ` Lorenzo Stoakes
2026-01-22 13:01 ` [PATCH v3 06/10] mm/vma: clean up __vma_enter/exit_locked() Lorenzo Stoakes
2026-01-22 13:08   ` Lorenzo Stoakes
2026-01-22 20:15   ` Suren Baghdasaryan
2026-01-22 20:55     ` Andrew Morton
2026-01-23 16:15       ` Lorenzo Stoakes
2026-01-23 16:33     ` Lorenzo Stoakes
2026-01-23  9:16   ` Vlastimil Babka
2026-01-23 16:17     ` Lorenzo Stoakes
2026-01-23 16:28       ` Lorenzo Stoakes
2026-01-22 13:01 ` [PATCH RESEND v3 07/10] mm/vma: introduce helper struct + thread through exclusive lock fns Lorenzo Stoakes
2026-01-22 21:41   ` Suren Baghdasaryan
2026-01-23 17:59     ` Lorenzo Stoakes
2026-01-23 19:34       ` Suren Baghdasaryan
2026-01-23 20:04         ` Lorenzo Stoakes
2026-01-23 22:07           ` Suren Baghdasaryan
2026-01-24  8:54             ` Lorenzo Stoakes
2026-01-26  6:09               ` Suren Baghdasaryan
2026-01-23 10:02   ` Vlastimil Babka
2026-01-23 18:18     ` Lorenzo Stoakes
2026-01-22 13:02 ` [PATCH RESEND v3 08/10] mm/vma: improve and document __is_vma_write_locked() Lorenzo Stoakes
2026-01-22 21:55   ` Suren Baghdasaryan
2026-01-23 16:21     ` Vlastimil Babka
2026-01-23 17:42       ` Suren Baghdasaryan
2026-01-23 18:44       ` Lorenzo Stoakes
2026-01-22 13:02 ` [PATCH RESEND v3 09/10] mm/vma: update vma_assert_locked() to use lockdep Lorenzo Stoakes
2026-01-22 22:02   ` Suren Baghdasaryan
2026-01-23 18:45     ` Lorenzo Stoakes
2026-01-23 16:55   ` Vlastimil Babka
2026-01-23 18:49     ` Lorenzo Stoakes
2026-01-22 13:02 ` [PATCH RESEND v3 10/10] mm/vma: add and use vma_assert_stabilised() Lorenzo Stoakes
2026-01-22 22:12   ` Suren Baghdasaryan
2026-01-23 18:54     ` Lorenzo Stoakes
2026-01-23 17:10   ` Vlastimil Babka
2026-01-23 18:51     ` Lorenzo Stoakes
2026-01-23 23:35   ` Hillf Danton
2026-01-22 15:48 ` [PATCH RESEND v3 00/10] mm: add and use vma_assert_stabilised() helper Andrew Morton
2026-01-22 15:57   ` Lorenzo Stoakes
2026-01-22 16:01     ` Lorenzo Stoakes

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox