Skip to content

mm/ksm: use per-VMA locking to reduce mmap_lock contention

[email protected] linux-mm linux-s390 lkml sashiko-reviews 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v2 Latest4 patches 3 repliesIn review

Changes in v2 from the cover letter

- Patch 1: rewrote the commit message to explain the introducing commit, how the VMA is actually used, and the audit performed; dropped the Fixes: tag.

- Patch 3:
* replaced the boolean 'lock_vma' with a KSM-local enum ksm_walk_lock instead of exposing the internal enum page_walk_lock as a parameter;
* build the mm_walk_ops on the stack rather than mutating a global struct, so the function-pointer table stays const and there is no shared mutable state; rewrote the commit message.

- Patch 4:
* Fixed a per-VMA read-lock leak in find_mergeable_vma_locked() when the VMA is not mergeable;
made __ksm_exit() write-lock every VMA (vma_start_write()) in addition to mmap_write_lock(), so that it still serializes against a ksmd walk that holds only a per-VMA read lock.

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/4mm/pagewalk: delete the unused memberinclude/linux/pagewalk.h+0 -11 reply
2/4mm: make folio_walk_start()'s locking asserts scalablearch/s390/mm/fault.c include/linux/pagewalk.h kernel/events/uprobes.c +5 more+31 -82 replies
3/4mm/ksm: make break_ksm() more scalablemm/ksm.c+27 -150 replies
4/4ksm: add find_mergeable_vma_locked() to use per-VMA lockingmm/ksm.c+60 -220 replies

Reviews trailers per patch

1 of 4 patches has a Reviewed-by or Acked-by.

PatchDavid Hildenbrand (Arm)
1/4R-b
2/4
3/4
4/4

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v2 thread

Cover letter [PATCH v2 0/4] mm/ksm: use per-VMA locking to reduce mmap_lock contention

From: Xu Xin <[email protected]>

KSM scans VM_MERGEABLE VMAs and currently protects each scan with mmap_read_lock(). Per-VMA locking allows KSM to read-lock only the VMA it is actually interested in, so that unrelated mmap()/munmap() activity in the same mm no longer blocks ksmd.

This series is organized as follows:

Patch 1 removes an unused 'vma' member from struct folio_walk. It has never been used since its introduction and is pure cleanup.

Patch 2 adds a 'walk_lock' member to struct folio_walk and extends folio_walk_start() to assert the required locking mode. Existing callers are converted to pass PGWALK_RDLOCK, so there is no functional change. This prepares folio_walk_start() for callers that hold a per-VMA read lock instead of mmap_read_lock(), which is needed by the Patch 4. No functional change.

Patch 3 replaces the boolean 'lock_vma' argument of break_ksm() with a KSM-local enum ksm_walk_lock and builds the mm_walk_ops on the stack, so the function-pointer table stays const and there is no shared mutable state. No functional change.

Patch 4 introduces find_mergeable_vma_locked(), which uses the universal per-VMA locking helper vma_start_read_unlocked() to look up and read-lock a VM_MERGEABLE VMA without taking mmap_read_lock(). All KSM call sites that previously used find_mergeable_vma() under mmap_read_lock() are converted to the new helper, and the locking in get_mergeable_page() is switched to PGWALK_VMA_RDLOCK_VERIFY so that folio_walk_start() can verify the per-VMA lock is held. Because ksmd no longer holds mmap_read_lock() while walking, __ksm_exit() must also write-lock every VMA (in addition to mmap_write_lock()) to keep serializing against an in-flight ksmd walk before the page tables are freed.

A microbenchmark was run to measure the time KSM takes to merge a victim region under mmap_lock contention. Under interference from 4 churner threads, the merge time of the per-VMA KSM-optimized kernel is significantly reduced by 50%.

Changelog:
v2:
- Patch 1: rewrote the commit message to explain the introducing commit, how the VMA is actually used, and the audit performed; dropped the Fixes: tag.

- Patch 3:
* replaced the boolean 'lock_vma' with a KSM-local enum ksm_walk_lock instead of exposing the internal enum page_walk_lock as a parameter;
* build the mm_walk_ops on the stack rather than mutating a global struct, so the function-pointer table stays const and there is no shared mutable state; rewrote the commit message.

- Patch 4:
* Fixed a per-VMA read-lock leak in find_mergeable_vma_locked() when the VMA is not mergeable;
made __ksm_exit() write-lock every VMA (vma_start_write()) in addition to mmap_write_lock(), so that it still serializes against a ksmd walk that holds only a per-VMA read lock.

Xu Xin (4):
mm/pagewalk: delete the unused member
mm: make folio_walk_start()'s locking asserts scalable mm/ksm: make break_ksm() more scalable
ksm: add find_mergeable_vma_locked() to use per-VMA locking

 arch/s390/mm/fault.c     |   4 +-
 include/linux/pagewalk.h |   2 +-
 kernel/events/uprobes.c  |   4 +-
 mm/huge_memory.c         |   4 +-
 mm/ksm.c                 | 124 +++++++++++++++++++++++++++------------
 mm/migrate.c             |   8 ++-
 mm/pagewalk.c            |  10 +++-
 mm/rmap.c                |   4 +-
 8 files changed, 116 insertions(+), 44 deletions(-)

--
2.34.1