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(). Signed-off-by: Lorenzo Stoakes (ARM) --- 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(-) diff --git a/Documentation/mm/process_addrs.rst b/Documentation/mm/process_addrs.rst index 04b649818cc1..40be3a46a521 100644 --- a/Documentation/mm/process_addrs.rst +++ b/Documentation/mm/process_addrs.rst @@ -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 diff --git a/mm/rmap.c b/mm/rmap.c index 65646ed6fc45..55367e7d3eec 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -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)