[PATCH RESEND v3 08/10] mm/vma: improve and document __is_vma_write_locked()

Lorenzo Stoakes
2026-01-22T13:02:56Z

The function is a little confusing, clean it up a little then add a
descriptive comment.

No functional change intended.

Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
---
include/linux/mmap_lock.h | 23 ++++++++++++++++++-----
1 file changed, 18 insertions(+), 5 deletions(-)

diff --git a/include/linux/mmap_lock.h b/include/linux/mmap_lock.h
index 873bc5f3c97c..b00d34b5ad10 100644
--- a/include/linux/mmap_lock.h
+++ b/include/linux/mmap_lock.h
@@ -252,17 +252,30 @@ static inline void vma_end_read(struct vm_area_struct *vma)
vma_refcount_put(vma);
}

-/* WARNING! Can only be used if mmap_lock is expected to be write-locked */
-static inline bool __is_vma_write_locked(struct vm_area_struct *vma, unsigned int *mm_lock_seq)
+/*
+ * Determine whether a VMA is write-locked. Must be invoked ONLY if the mmap
+ * write lock is held.
+ *
+ * Returns true if write-locked, otherwise false.
+ *
+ * Note that mm_lock_seq is updated only if the VMA is NOT write-locked.
+ */
+static inline bool __is_vma_write_locked(struct vm_area_struct *vma,
+ unsigned int *mm_lock_seq)
{
- mmap_assert_write_locked(vma->vm_mm);
+ struct mm_struct *mm = vma->vm_mm;
+ const unsigned int seq = mm->mm_lock_seq.sequence;
+
+ mmap_assert_write_locked(mm);

/*
* current task is holding mmap_write_lock, both vma->vm_lock_seq and
* mm->mm_lock_seq can't be concurrently modified.
*/
- *mm_lock_seq = vma->vm_mm->mm_lock_seq.sequence;
- return (vma->vm_lock_seq == *mm_lock_seq);
+ if (vma->vm_lock_seq == seq)
+ return true;
+ *mm_lock_seq = seq;
+ return false;
}

int __vma_start_write(struct vm_area_struct *vma, unsigned int mm_lock_seq,
--
2.52.0