linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [DISCUSSION] anon_vma root lock contention and per anon_vma lock
@ 2025-09-11  7:17 Barry Song
  2025-09-11  8:14 ` David Hildenbrand
                   ` (2 more replies)
  0 siblings, 3 replies; 23+ messages in thread
From: Barry Song @ 2025-09-11  7:17 UTC (permalink / raw)
  To: Nicolas Geoffray, Lokesh Gidra, David Hildenbrand,
	Lorenzo Stoakes, Harry Yoo, Suren Baghdasaryan, Andrew Morton,
	Rik van Riel, Liam R . Howlett, Vlastimil Babka, Jann Horn
  Cc: Linux-MM, Kalesh Singh, SeongJae Park, Barry Song, Peter Xu

Hi All,

I’m aware that Lokesh started a discussion on the concurrency issue
between usefaultfd_move and memory reclamation [1]. However, my
concern is different, so I’m starting a separate discussion.

In the process tree, many processes may share anon_vma->root, even if
they don’t share the anon_vma itself. This causes serious lock contention
between memory reclamation (which calls folio_referenced and try_to_unmap)
and other processes calling fork(), exit(), mprotect(), etc.

On Android, this issue becomes more severe since many processes are
descendants of zygote.

Memory reclamation path:
  folio_lock_anon_vma_read

mprotect path:
  mprotect
    split_vma
      anon_vma_clone

fork / copy_process path:
  copy_process
    dup_mmap
      anon_vma_fork

exit path:
  exit_mmap
    free_pgtables
      unlink_anon_vmas

To be honest, memory reclamation—especially folio_referenced()—is a
problem. It is called very frequently and can block other important
user threads waiting for the anon_vma root lock, causing UI lag.

I have a rough idea: since the vast majority of anon folios are actually
exclusive (I observed almost 98% of Android anon folios fall into this
category), they don’t need to iterate the anon_vma tree. They belong to
a single process, and even for rmap, it is per-process.

I propose introducing a per-anon_vma lock. For exclusive folios whose
anon_vma is not shared, we could use this per-anon_vma lock.
folio_referenced declares that it will begin reading, and Lokesh’s
folio_lock may also help maintain folios as exclusive, so I am
somewhat in favor of his RFC. Any thread writing to such an anon_vma
would take the per-vma write lock, and possibly also the anon_vma
root write lock. If folio_referenced fails to declare the per-vma lock,
it can fall back to the global anon_vma->root read mutex, similar to
mmap_lock.

I haven’t carefully considered this or written any code yet—just a
very rough idea. Sorry if it comes across as too naive.

[1] https://lore.kernel.org/linux-mm/CA+EESO4Z6wtX7ZMdDHQRe5jAAS_bQ-POq5+4aDx5jh2DvY6UHg@mail.gmail.com/

Thanks
Barry


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

end of thread, other threads:[~2025-09-15 10:56 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-09-11  7:17 [DISCUSSION] anon_vma root lock contention and per anon_vma lock Barry Song
2025-09-11  8:14 ` David Hildenbrand
2025-09-11  8:34   ` Lorenzo Stoakes
2025-09-11  9:18   ` Barry Song
2025-09-11 10:47     ` Lorenzo Stoakes
2025-09-11  8:28 ` Lorenzo Stoakes
2025-09-11 18:22   ` Jann Horn
2025-09-12  4:49     ` Lorenzo Stoakes
2025-09-12 11:37       ` Jann Horn
2025-09-12 11:56         ` Lorenzo Stoakes
2025-09-14 23:53 ` Matthew Wilcox
2025-09-15  0:23   ` Barry Song
2025-09-15  1:47     ` Suren Baghdasaryan
2025-09-15  8:41       ` Lorenzo Stoakes
2025-09-15  2:50     ` Matthew Wilcox
2025-09-15  5:17       ` David Hildenbrand
2025-09-15  9:42         ` Lorenzo Stoakes
2025-09-15 10:29           ` David Hildenbrand
2025-09-15 10:56             ` Lorenzo Stoakes
2025-09-15  9:22       ` Lorenzo Stoakes
2025-09-15 10:41         ` David Hildenbrand
2025-09-15 10:51           ` Lorenzo Stoakes
2025-09-15  8:57   ` Lorenzo Stoakes

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