Skip to contentSkip to conversation

mm/rmap: correct VMA write lock position in lock ordering

PATCH In review Lorenzo Stoakes (ARM) 3 participants 3 messages Nov 23

Subsystems (5)
lore
Share & tools
lore

Lorenzo Stoakes (ARM)

author maintainer
#0 lore raw

PATCH v2 1/1 mm/rmap: correct VMA write lock position in lock ordering

The list at the top of mm/rmap.c is taken to be the canonical order in which locks must be taken in mm to avoid lock inversion.

However, VMA write locks appear to be misplaced. These were added there by commit 55fd6fccad31 ("mm/khugepaged: write-lock VMA while collapsing a huge page"), which was focused on expressing the dependency of locks after VMA locks in the ordering on them, disregarding those prior to it.

Of course, a VMA write lock requires an mmap write lock to be held, so this leaves three locks to consider:

mapping->invalidate_lock (in filemap_fault)
folio_lock
hugetlbfs_i_mmap_rwsem_key (in huge_pmd_share, see hugetlbfs below)

Each of these locks is either downstream of a VMA, or is not able to acquire a VMA lock at all (e.g. in an rmap operation).

This is because accessing a VMA's file (thus mapping), traversing page tables to a folio, or interacting with hugetlbfs requires the VMA to be stabilised in the first instance.

Nothing in the kernel takes any of these 3 locks before a VMA lock, and indeed in each case that a VMA lock is taken it is taken before - so the kernel is already violating the ordering as-written.

So fix it. Also add [_killable] to reflect the existence of vma_start_write_killable().

---
v2:
* Updated docs to reflect change as per Suren.

v1:
https://lore.kernel.org/r/20261121-fix-rmap-lock-ordering-vma-locks-v1-1-952c0dd5bc3a@kernel.org

---
 Documentation/mm/process_addrs.rst | 8 ++++----
 mm/rmap.c                          | 8 ++++----
 2 files changed, 8 insertions(+), 8 deletions(-)
Documentation/mm/process_addrs.rst +4 -4
index 04b649818cc1..40be3a46a521 100644
@@ -418,10 +418,10 @@ ordering of locks within memory management code:
 
   inode->i_rwsem        (while writing or truncating, not reading or faulting)
     mm->mmap_lock
-      mapping->invalidate_lock (in filemap_fault)
-        folio_lock
-          hugetlbfs_i_mmap_rwsem_key (in huge_pmd_share, see hugetlbfs below)
-            vma_start_write
+      vma_start_write[_killable]
+        mapping->invalidate_lock (in filemap_fault)
+          folio_lock
+            hugetlbfs_i_mmap_rwsem_key (in huge_pmd_share, see hugetlbfs below)
               mapping->i_mmap_rwsem
                 anon_vma->rwsem
                   mm->page_table_lock or pte_lock
mm/rmap.c +4 -4
index 65646ed6fc45..55367e7d3eec 100644
@@ -22,10 +22,10 @@
  *
  * inode->i_rwsem	(while writing or truncating, not reading or faulting)
  *   mm->mmap_lock
- *     mapping->invalidate_lock (in filemap_fault)
- *       folio_lock
- *         hugetlbfs_i_mmap_rwsem_key (in huge_pmd_share, see hugetlbfs below)
- *           vma_start_write
+ *     vma_start_write[_killable]
+ *       mapping->invalidate_lock (in filemap_fault)
+ *         folio_lock
+ *           hugetlbfs_i_mmap_rwsem_key (in huge_pmd_share, see hugetlbfs below)
  *             mapping->i_mmap_rwsem
  *               anon_vma->rwsem
  *                 mm->page_table_lock or pte_lock

---
base-commit: d12eccbcb79a4fa35c3aa588eb79056af62eb859
change-id: 20261121-fix-rmap-lock-ordering-vma-locks-62593469bf59

Cheers,

Signature · 1 line
Lorenzo Stoakes (ARM) <[email protected]>
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 23 lines · Lorenzo Stoakes (ARM)

The list at the top of mm/rmap.c is taken to be the canonical order in which locks must be taken in mm to avoid lock inversion.

However, VMA write locks appear to be misplaced. These were added there by commit 55fd6fccad31 ("mm/khugepaged: write-lock VMA while collapsing a huge page"), which was focused on expressing the dependency of locks after VMA locks in the ordering on them, disregarding those prior to it.

Of course, a VMA write lock requires an mmap write lock to be held, so this leaves three locks to consider:

mapping->invalidate_lock (in filemap_fault)
folio_lock
hugetlbfs_i_mmap_rwsem_key (in huge_pmd_share, see hugetlbfs below)

Each of these locks is either downstream of a VMA, or is not able to acquire a VMA lock at all (e.g. in an rmap operation).

This is because accessing a VMA's file (thus mapping), traversing page tables to a folio, or interacting with hugetlbfs requires the VMA to be stabilised in the first instance.

Quoted 8 lines

Nothing in the kernel takes any of these 3 locks before a VMA lock, and indeed in each case that a VMA lock is taken it is taken before - so the kernel is already violating the ordering as-written.

So fix it. Also add [_killable] to reflect the existence of vma_start_write_killable().

Quoted 58 lines

---
v2:
* Updated docs to reflect change as per Suren.

v1:
https://lore.kernel.org/r/20261121-fix-rmap-lock-ordering-vma-locks-v1-1-952c0dd5bc3a@kernel.org

---
 Documentation/mm/process_addrs.rst | 8 ++++----
 mm/rmap.c                          | 8 ++++----
 2 files changed, 8 insertions(+), 8 deletions(-)
Documentation/mm/process_addrs.rst +4 -4
index 04b649818cc1..40be3a46a521 100644
@@ -418,10 +418,10 @@ ordering of locks within memory management code:
 
   inode->i_rwsem        (while writing or truncating, not reading or faulting)
     mm->mmap_lock
-      mapping->invalidate_lock (in filemap_fault)
-        folio_lock
-          hugetlbfs_i_mmap_rwsem_key (in huge_pmd_share, see hugetlbfs below)
-            vma_start_write
+      vma_start_write[_killable]
+        mapping->invalidate_lock (in filemap_fault)
+          folio_lock
+            hugetlbfs_i_mmap_rwsem_key (in huge_pmd_share, see hugetlbfs below)
               mapping->i_mmap_rwsem
                 anon_vma->rwsem
                   mm->page_table_lock or pte_lock
mm/rmap.c +4 -5
index 65646ed6fc45..55367e7d3eec 100644
@@ -22,10 +22,10 @@
  *
  * inode->i_rwsem      (while writing or truncating, not reading or faulting)
  *   mm->mmap_lock
- *     mapping->invalidate_lock (in filemap_fault)
- *       folio_lock
- *         hugetlbfs_i_mmap_rwsem_key (in huge_pmd_share, see hugetlbfs below)
- *           vma_start_write
+ *     vma_start_write[_killable]
+ *       mapping->invalidate_lock (in filemap_fault)
+ *         folio_lock
+ *           hugetlbfs_i_mmap_rwsem_key (in huge_pmd_share, see hugetlbfs below)
  *             mapping->i_mmap_rwsem
  *               anon_vma->rwsem
  *                 mm->page_table_lock or pte_lock
 
---

base-commit: d12eccbcb79a4fa35c3aa588eb79056af62eb859
change-id: 20261121-fix-rmap-lock-ordering-vma-locks-62593469bf59

Cheers,
--
Lorenzo Stoakes (ARM) <[email protected]>

maintainer Reviewed-by on v2 1/1
#2 lore raw
In reply to Lorenzo Stoakes (ARM)
Quoted 70 lines · Lorenzo Stoakes (ARM)

The list at the top of mm/rmap.c is taken to be the canonical order in which locks must be taken in mm to avoid lock inversion.

However, VMA write locks appear to be misplaced. These were added there by commit 55fd6fccad31 ("mm/khugepaged: write-lock VMA while collapsing a huge page"), which was focused on expressing the dependency of locks after VMA locks in the ordering on them, disregarding those prior to it.

Of course, a VMA write lock requires an mmap write lock to be held, so this leaves three locks to consider:

mapping->invalidate_lock (in filemap_fault)
folio_lock
hugetlbfs_i_mmap_rwsem_key (in huge_pmd_share, see hugetlbfs below)

Each of these locks is either downstream of a VMA, or is not able to acquire a VMA lock at all (e.g. in an rmap operation).

This is because accessing a VMA's file (thus mapping), traversing page tables to a folio, or interacting with hugetlbfs requires the VMA to be stabilised in the first instance.

Nothing in the kernel takes any of these 3 locks before a VMA lock, and indeed in each case that a VMA lock is taken it is taken before - so the kernel is already violating the ordering as-written.

So fix it. Also add [_killable] to reflect the existence of vma_start_write_killable().

---
v2:
* Updated docs to reflect change as per Suren.

v1:
https://lore.kernel.org/r/20261121-fix-rmap-lock-ordering-vma-locks-v1-1-952c0dd5bc3a@kernel.org

---
 Documentation/mm/process_addrs.rst | 8 ++++----
 mm/rmap.c                          | 8 ++++----
 2 files changed, 8 insertions(+), 8 deletions(-)
Documentation/mm/process_addrs.rst +4 -4
index 04b649818cc1..40be3a46a521 100644
@@ -418,10 +418,10 @@ ordering of locks within memory management code:
 
   inode->i_rwsem        (while writing or truncating, not reading or faulting)
     mm->mmap_lock
-      mapping->invalidate_lock (in filemap_fault)
-        folio_lock
-          hugetlbfs_i_mmap_rwsem_key (in huge_pmd_share, see hugetlbfs below)
-            vma_start_write
+      vma_start_write[_killable]
+        mapping->invalidate_lock (in filemap_fault)
+          folio_lock
+            hugetlbfs_i_mmap_rwsem_key (in huge_pmd_share, see hugetlbfs below)
               mapping->i_mmap_rwsem
                 anon_vma->rwsem
                   mm->page_table_lock or pte_lock
mm/rmap.c +0 -1
index 65646ed6fc45..55367e7d3eec 100644
@@ -22,10 +22,10 @@
  *
  * inode->i_rwsem	(while writing or truncating, not reading or faulting)
  *   mm->mmap_lock
- *     mapping->invalidate_lock (in filemap_fault)

mm/rmap.c line 31

Quoted 10 lines
mm/rmap.c +4 -3
- *       folio_lock
- *         hugetlbfs_i_mmap_rwsem_key (in huge_pmd_share, see hugetlbfs below)
- *           vma_start_write
+ *     vma_start_write[_killable]
+ *       mapping->invalidate_lock (in filemap_fault)
+ *         folio_lock
+ *           hugetlbfs_i_mmap_rwsem_key (in huge_pmd_share, see hugetlbfs below)
  *             mapping->i_mmap_rwsem
  *               anon_vma->rwsem
  *                 mm->page_table_lock or pte_lock

Queuing this in a sec ...

Signature · 3 lines
Cheers,

David