Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] mm/rmap: correct VMA write lock position in lock ordering
@ 2026-11-21 16:31 Lorenzo Stoakes (ARM)
  2026-11-21 19:58 ` Suren Baghdasaryan
  0 siblings, 1 reply; 3+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-11-21 16:31 UTC (permalink / raw)
  To: David Hildenbrand, Rik van Riel, Liam R. Howlett, Vlastimil Babka,
	Harry Yoo, Jann Horn, Lance Yang, Suren Baghdasaryan,
	Shakeel Butt, Pedro Falcato
  Cc: linux-mm, ljs

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) <ljs@kernel.org>
---
 mm/rmap.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

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) <ljs@kernel.org>



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

* Re: [PATCH] mm/rmap: correct VMA write lock position in lock ordering
  2026-11-21 16:31 [PATCH] mm/rmap: correct VMA write lock position in lock ordering Lorenzo Stoakes (ARM)
@ 2026-11-21 19:58 ` Suren Baghdasaryan
  2026-11-23 12:00   ` Lorenzo Stoakes (ARM)
  0 siblings, 1 reply; 3+ messages in thread
From: Suren Baghdasaryan @ 2026-11-21 19:58 UTC (permalink / raw)
  To: Lorenzo Stoakes (ARM)
  Cc: David Hildenbrand, Rik van Riel, Liam R. Howlett, Vlastimil Babka,
	Harry Yoo, Jann Horn, Lance Yang, Shakeel Butt, Pedro Falcato,
	linux-mm

On Sat, Nov 21, 2026 at 8:31 AM Lorenzo Stoakes (ARM) <ljs@kernel.org> wrote:
>
> 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().

Thanks! Could you please fix the documentation here as well:

https://elixir.bootlin.com/linux/v7.4-rc1/source/Documentation/mm/process_addrs.rst#L424

>
> Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
> ---
>  mm/rmap.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
>
> 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) <ljs@kernel.org>
>


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

* Re: [PATCH] mm/rmap: correct VMA write lock position in lock ordering
  2026-11-21 19:58 ` Suren Baghdasaryan
@ 2026-11-23 12:00   ` Lorenzo Stoakes (ARM)
  0 siblings, 0 replies; 3+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-11-23 12:00 UTC (permalink / raw)
  To: Suren Baghdasaryan
  Cc: David Hildenbrand, Rik van Riel, Liam R. Howlett, Vlastimil Babka,
	Harry Yoo, Jann Horn, Lance Yang, Shakeel Butt, Pedro Falcato,
	linux-mm

On Sat, Nov 21, 2026 at 11:58:56AM -0800, Suren Baghdasaryan wrote:
> Thanks! Could you please fix the documentation here as well:
>
> https://elixir.bootlin.com/linux/v7.4-rc1/source/Documentation/mm/process_addrs.rst#L424

Good spot :) Ack will do!

--
Cheers, Lorenzo


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

end of thread, other threads:[~2026-11-23 12:01 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-21 16:31 [PATCH] mm/rmap: correct VMA write lock position in lock ordering Lorenzo Stoakes (ARM)
2026-11-21 19:58 ` Suren Baghdasaryan
2026-11-23 12:00   ` Lorenzo Stoakes (ARM)

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