Skip to contentSkip to conversation

+ mm-vma-predicate-vma_assert_stabilised-on-config_debug_vm.patch added to mm-new branch

PATCH Andrew Morton 1 participant 1 message Nov 10

lore
Share & tools
lore

Andrew Morton

author tree notice
#0 lore raw

The patch titled
Subject: mm/vma: predicate vma_assert_stabilised() on CONFIG_DEBUG_VM
has been added to the -mm mm-new branch. Its filename is
mm-vma-predicate-vma_assert_stabilised-on-config_debug_vm.patch

This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-vma-predicate-vma_assert_stabilised-on-config_debug_vm.patch

This patch will later appear in the mm-new branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

Note, mm-new is a provisional staging ground for work-in-progress patches, and acceptance into mm-new is a notification for others take notice and to finish up reviews. Please do not hesitate to respond to review feedback and post updated versions to replace or incrementally fixup patches in mm-new.

The mm-new branch of mm.git is not included in linux-next

If a few days of testing in mm-new is successful, the patch will me moved into mm.git's mm-unstable branch, which is included in linux-next

Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's

*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***

The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days

------------------------------------------------------
From: "Lorenzo Stoakes (ARM)" <[email protected]>
Subject: mm/vma: predicate vma_assert_stabilised() on CONFIG_DEBUG_VM
Date: Tue, 10 Nov 2026 15:10:40 +0000

I introduced commit 17fd82c3abe0 ("mm/vma: add and use vma_assert_stabilised()") with the intent of picking up programmatic bugs when debugging mm code, but kept the general practice of asserting even when CONFIG_DEBUG_VM is not set in line with existing code.

However, it turns out doing so can cause performance regressions, especially when asserted in vma_has_anon_rmap() which is referenced in a number of hotpaths.

Since the intent was always a debug-only assertion, explicitly check for CONFIG_DEBUG_VM and do not assert if it isn't set.

Really all of the assert paths need to be predicated on this, but start with this one only, out of caution.

Additionally, since a detached VMA is trivially stabilised (nothing to stabilise against), check for this case at the outset, and update vma_has_anon_rmap() to not explicitly need to check for this.

This also, importantly, avoids reading any VMA fields if CONFIG_DEBUG_VM is not set.

Target the commit which introduced vma_has_anon_rmap() for the fix, as this is where the issue started manifesting in practice.

---

 include/linux/mmap_lock.h |   19 ++++++++++++++-----
 mm/vma.h                  |    3 +--
 2 files changed, 15 insertions(+), 7 deletions(-)
include/linux/mmap_lock.h renamed from include/linux/mmap_lock.h~mm-vma-predicate-vma_assert_stabilised-on-config_debug_vm +14 -5
@@ -408,6 +408,11 @@ static inline void vma_assert_locked(con
 	vma_assert_write_locked(vma);
 }
 
+static inline bool vma_is_attached(const struct vm_area_struct *vma)
+{
+	return refcount_read(&vma->vm_refcnt);
+}
+
 /**
  * vma_assert_stabilised() - assert that this VMA cannot be changed from
  * underneath us either by having a VMA or mmap lock held.
@@ -418,9 +423,18 @@ static inline void vma_assert_locked(con
  *
  * With lockdep disabled we may sometimes race with other threads acquiring the
  * mmap read lock simultaneous with our VMA read lock.
+ *
+ * Assert only if CONFIG_DEBUG_VM is set.
  */
 static inline void vma_assert_stabilised(const struct vm_area_struct *vma)
 {
+	if (!IS_ENABLED(CONFIG_DEBUG_VM))
+		return;
+
+	/* Detached VMAs are trivially stabilised. */
+	if (!vma_is_attached(vma))
+		return;
+
 	/*
 	 * If another thread owns an mmap lock, it may go away at any time, and
 	 * thus is no guarantee of stability.
@@ -454,11 +468,6 @@ static inline void vma_assert_stabilised
 	vma_assert_locked(vma);
 }
 
-static inline bool vma_is_attached(const struct vm_area_struct *vma)
-{
-	return refcount_read(&vma->vm_refcnt);
-}
-
 /*
  * 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
mm/vma.h renamed from mm/vma.h~mm-vma-predicate-vma_assert_stabilised-on-config_debug_vm +1 -2
@@ -278,8 +278,7 @@ static inline pgoff_t vmg_end_pgoff(cons
  */
 static inline bool vma_has_anon_rmap(const struct vm_area_struct *vma)
 {
-	if (vma_is_attached(vma))
-		vma_assert_stabilised(vma);
+	vma_assert_stabilised(vma);
 	/* KCSAN gets confused about the optimistic check. Silence it. */
 	return data_race(vma->anon_vma);
 }

_

Patches currently in -mm which might be from [email protected] are

mm-vma-predicate-vma_assert_stabilised-on-config_debug_vm.patch