Skip to content

RFC mm/rmap: interface overhaul

David Hildenbrand linux-mm lkml 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 RFC Latest39 patches 42 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/39mm/rmap: rename hugepage_add* to hugetlb_add*include/linux/rmap.h mm/hugetlb.c mm/migrate.c +1 more+12 -120 replies
2/39mm/rmap: introduce and use hugetlb_remove_rmap()include/linux/rmap.h mm/hugetlb.c mm/rmap.c+15 -113 replies
3/39mm/rmap: introduce and use hugetlb_add_file_rmap()include/linux/rmap.h mm/hugetlb.c mm/migrate.c+11 -41 reply
4/39mm/rmap: introduce and use hugetlb_try_dup_anon_rmap()include/linux/mm.h include/linux/rmap.h mm/hugetlb.c+25 -51 reply
5/39mm/rmap: introduce and use hugetlb_try_share_anon_rmap()include/linux/rmap.h mm/rmap.c+30 -51 reply
6/39mm/rmap: add hugetlb sanity checksinclude/linux/rmap.h mm/rmap.c+13 -01 reply
7/39mm/rmap: convert folio_add_file_rmap_range() into folio_add_file_rmap_[pte|ptes|pmd]()include/linux/rmap.h mm/memory.c mm/rmap.c+92 -294 replies
8/39mm/memory: page_add_file_rmap() -> folio_add_file_rmap_[pte|pmd]()mm/memory.c+8 -61 reply
9/39mm/huge_memory: page_add_file_rmap() -> folio_add_file_rmap_pmd()mm/huge_memory.c+6 -51 reply
10/39mm/migrate: page_add_file_rmap() -> folio_add_file_rmap_pte()mm/migrate.c+1 -11 reply
11/39mm/userfaultfd: page_add_file_rmap() -> folio_add_file_rmap_pte()mm/userfaultfd.c+1 -11 reply
12/39mm/rmap: remove page_add_file_rmap()include/linux/rmap.h mm/rmap.c+0 -231 reply
13/39mm/rmap: factor out adding folio mappings into __folio_add_rmap()mm/rmap.c+42 -331 reply
14/39mm/rmap: introduce folio_add_anon_rmap_[pte|ptes|pmd]()include/linux/rmap.h mm/rmap.c+85 -360 replies
15/39mm/huge_memory: batch rmap operations in __split_huge_pmd_locked()mm/huge_memory.c+15 -82 replies
16/39mm/huge_memory: page_add_anon_rmap() -> folio_add_anon_rmap_pmd()mm/huge_memory.c+2 -20 replies
17/39mm/migrate: page_add_anon_rmap() -> folio_add_anon_rmap_pte()mm/migrate.c+2 -20 replies
18/39mm/ksm: page_add_anon_rmap() -> folio_add_anon_rmap_pte()mm/ksm.c+5 -30 replies
19/39mm/swapfile: page_add_anon_rmap() -> folio_add_anon_rmap_pte()mm/swapfile.c+1 -10 replies
20/39mm/memory: page_add_anon_rmap() -> folio_add_anon_rmap_pte()mm/memory.c+7 -40 replies
21/39mm/rmap: remove page_add_anon_rmap()include/linux/rmap.h mm/rmap.c+4 -290 replies
22/39mm/rmap: remove RMAP_COMPOUNDinclude/linux/rmap.h mm/rmap.c+3 -110 replies
23/39mm/rmap: introduce folio_remove_rmap_[pte|ptes|pmd]()include/linux/rmap.h mm/rmap.c+68 -143 replies
24/39kernel/events/uprobes: page_remove_rmap() -> folio_remove_rmap_pte()kernel/events/uprobes.c+1 -10 replies
25/39mm/huge_memory: page_remove_rmap() -> folio_remove_rmap_pmd()mm/huge_memory.c+14 -120 replies
26/39mm/khugepaged: page_remove_rmap() -> folio_remove_rmap_pte()mm/khugepaged.c+7 -100 replies
27/39mm/ksm: page_remove_rmap() -> folio_remove_rmap_pte()mm/ksm.c+1 -10 replies
28/39mm/memory: page_remove_rmap() -> folio_remove_rmap_pte()mm/memory.c mm/mmu_gather.c+14 -110 replies
29/39mm/migrate_device: page_remove_rmap() -> folio_remove_rmap_pte()mm/migrate_device.c+21 -180 replies
30/39mm/rmap: page_remove_rmap() -> folio_remove_rmap_pte()mm/rmap.c+5 -50 replies
31/39Documentation: stop referring to page_remove_rmap()Documentation/mm/transhuge.rst Documentation/mm/unevictable-lru.rst+3 -30 replies
32/39mm/rmap: remove page_remove_rmap()include/linux/rmap.h mm/internal.h mm/memory-failure.c +1 more+6 -270 replies
33/39mm/rmap: convert page_dup_file_rmap() to folio_dup_file_rmap_[pte|ptes|pmd]()include/linux/rmap.h mm/memory.c+52 -60 replies
34/39mm/rmap: introduce folio_try_dup_anon_rmap_[pte|ptes|pmd]()include/linux/mm.h include/linux/rmap.h+100 -5111 replies
35/39mm/huge_memory: page_try_dup_anon_rmap() -> folio_try_dup_anon_rmap_pmd()mm/huge_memory.c+7 -50 replies
36/39mm/memory: page_try_dup_anon_rmap() -> folio_try_dup_anon_rmap_pte()mm/memory.c+5 -30 replies
37/39mm/rmap: remove page_try_dup_anon_rmap()include/linux/rmap.h+3 -130 replies
38/39mm: convert page_try_share_anon_rmap() to folio_try_share_anon_rmap_[pte|pmd]()include/linux/rmap.h mm/gup.c mm/huge_memory.c +4 more+89 -400 replies
39/39mm/rmap: rename COMPOUND_MAPPED to ENTIRELY_MAPPEDDocumentation/mm/transhuge.rst mm/internal.h mm/rmap.c+13 -130 replies

Reviews trailers per patch

11 of 39 patches have a Reviewed-by or Acked-by.

PatchMuchun SongYin Fengwei
1/39R-b
2/39
3/39R-b
4/39R-b
5/39R-b
6/39R-b
7/39
8/39R-b
9/39R-b
10/39R-b
11/39R-b
12/39R-b
13/39R-b
14/39
15/39
16/39
17/39
18/39
19/39
20/39
21/39
22/39
23/39
24/39
25/39
26/39
27/39
28/39
29/39
30/39
31/39
32/39
33/39
34/39
35/39
36/39
37/39
38/39
39/39

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

Read the v1 thread

Cover letter [PATCH RFC 00/39] mm/rmap: interface overhaul

Baed on mm-stable from a couple of days.

This series proposes an overhaul to our rmap interface, to get rid of the "bool compound" / RMAP_COMPOUND parameter with the goal of making the interface less error prone, more future proof, and more natural to extend to "batching". Also, this converts the interface to always consume folio+subpage, which speeds up operations on large folios.

Further, this series adds PTE-batching variants for 4 rmap functions, whereby only folio_add_anon_rmap_ptes() is used for batching in this series when PTE-remapping a PMD-mapped THP.

Ryan has series where we would make use of folio_remove_rmap_ptes() [1] -- he carries his own batching variant right now -- and folio_try_dup_anon_rmap_ptes()/folio_dup_file_rmap_ptes() [2].

There is some overlap with both series (and some other work, like multi-size THP [3]), so that will need some coordination, and likely a stepwise inclusion.

I got that started [4], but it made sense to show the whole picture. The patches of [4] are contained in here, with one additional patch added ("mm/rmap: introduce and use hugetlb_try_share_anon_rmap()") and some slight patch description changes.

In general, RMAP batching is an important optimization for PTE-mapped THP, especially once we want to move towards a total mapcount or further, as shown with my WIP patches on "mapped shared vs. mapped exclusively" [5].

The rmap batching part of [5] is also contained here in a slightly reworked fork [and I found a bug du to the "compound" parameter handling in these patches that should be fixed here :) ].

This series performs a lot of folio conversion, that could be separated if there is a good reason. Most of the added LOC in the diff are only due to documentation.

As we're moving to a pte/pmd interface where we clearly express the mapping granularity we are dealing with, we first get the remainder of hugetlb out of the way, as it is special and expected to remain special: it treats everything as a "single logical PTE" and only currently allows entire mappings.

Even if we'd ever support partial mappings, I strongly
assume the interface and implementation will still differ heavily:
hopefull we can avoid working on subpages/subpage mapcounts completely and only add a "count" parameter for them to enable batching.

New (extended) hugetlb interface that operate on entire folio:
* hugetlb_add_new_anon_rmap() -> Already existed
* hugetlb_add_anon_rmap() -> Already existed
* hugetlb_try_dup_anon_rmap()
* hugetlb_try_share_anon_rmap()
* hugetlb_add_file_rmap()
* hugetlb_remove_rmap()

New "ordinary" interface for small folios / THP::
* folio_add_new_anon_rmap() -> Already existed
* folio_add_anon_rmap_[pte|ptes|pmd]()
* folio_try_dup_anon_rmap_[pte|ptes|pmd]()
* folio_try_share_anon_rmap_[pte|pmd]()

 * folio_add_file_rmap_[pte|ptes|pmd]()
 * folio_dup_file_rmap_[pte|ptes|pmd]()

* folio_remove_rmap_[pte|ptes|pmd]()

folio_add_new_anon_rmap() will always map at the biggest granularity possible (currently, a single PMD to cover a PMD-sized THP). Could be extended if ever required.

In the future, we might want "_pud" variants and eventually "_pmds" variants for batching. Further, if hugepd is ever a thing outside hugetlb code, we might want some variants for that. All stuff for the distant future.

I ran some simple microbenchmarks from [5] on an Intel(R) Xeon(R) Silver 4210R: munmap(), fork(), cow, MADV_DONTNEED on each PTE ... and PTE remapping PMD-mapped THPs on 1 GiB of memory.

For small folios, there is barely a change (< 1 % performance improvement), whereby fork() still stands out with 0.74% performance improvement, but it might be just some noise. Folio optimizations don't help that much with small folios.

For PTE-mapped THP:
* PTE-remapping a PMD-mapped THP is more than 10% faster. -> RMAP batching
* fork() is more than 4% faster.
-> folio conversion
* MADV_DONTNEED is 2% faster
-> folio conversion
* COW by writing only a single byte on a COW-shared PTE -> folio conversion
* munmap() is only slightly faster (< 1%).

[1] https://lkml.kernel.org/r/[email protected]
[2] https://lkml.kernel.org/r/[email protected]
[3] https://lkml.kernel.org/r/[email protected]
[4] https://lkml.kernel.org/r/[email protected]
[5] https://lkml.kernel.org/r/[email protected]

Cc Andrew Morton <[email protected]>
Cc "Matthew Wilcox (Oracle)" <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Yin Fengwei <[email protected]>
Cc Mike Kravetz <[email protected]>
Cc Muchun Song <[email protected]>
Cc Peter Xu <[email protected]>

David Hildenbrand (39):
mm/rmap: rename hugepage_add* to hugetlb_add* mm/rmap: introduce and use hugetlb_remove_rmap() mm/rmap: introduce and use hugetlb_add_file_rmap() mm/rmap: introduce and use hugetlb_try_dup_anon_rmap() mm/rmap: introduce and use hugetlb_try_share_anon_rmap() mm/rmap: add hugetlb sanity checks
mm/rmap: convert folio_add_file_rmap_range() into

    folio_add_file_rmap_[pte|ptes|pmd]()
  mm/memory: page_add_file_rmap() -> folio_add_file_rmap_[pte|pmd]()
  mm/huge_memory: page_add_file_rmap() -> folio_add_file_rmap_pmd()
  mm/migrate: page_add_file_rmap() -> folio_add_file_rmap_pte()
  mm/userfaultfd: page_add_file_rmap() -> folio_add_file_rmap_pte()
  mm/rmap: remove page_add_file_rmap()
  mm/rmap: factor out adding folio mappings into __folio_add_rmap()
  mm/rmap: introduce folio_add_anon_rmap_[pte|ptes|pmd]()
  mm/huge_memory: batch rmap operations in __split_huge_pmd_locked()
  mm/huge_memory: page_add_anon_rmap() -> folio_add_anon_rmap_pmd()
  mm/migrate: page_add_anon_rmap() -> folio_add_anon_rmap_pte()
  mm/ksm: page_add_anon_rmap() -> folio_add_anon_rmap_pte()
  mm/swapfile: page_add_anon_rmap() -> folio_add_anon_rmap_pte()
  mm/memory: page_add_anon_rmap() -> folio_add_anon_rmap_pte()
  mm/rmap: remove page_add_anon_rmap()
  mm/rmap: remove RMAP_COMPOUND
  mm/rmap: introduce folio_remove_rmap_[pte|ptes|pmd]()
  kernel/events/uprobes: page_remove_rmap() -> folio_remove_rmap_pte()
  mm/huge_memory: page_remove_rmap() -> folio_remove_rmap_pmd()
  mm/khugepaged: page_remove_rmap() -> folio_remove_rmap_pte()
  mm/ksm: page_remove_rmap() -> folio_remove_rmap_pte()
  mm/memory: page_remove_rmap() -> folio_remove_rmap_pte()
  mm/migrate_device: page_remove_rmap() -> folio_remove_rmap_pte()
  mm/rmap: page_remove_rmap() -> folio_remove_rmap_pte()
  Documentation: stop referring to page_remove_rmap()
  mm/rmap: remove page_remove_rmap()
  mm/rmap: convert page_dup_file_rmap() to
    folio_dup_file_rmap_[pte|ptes|pmd]()
  mm/rmap: introduce folio_try_dup_anon_rmap_[pte|ptes|pmd]()
  mm/huge_memory: page_try_dup_anon_rmap() ->
    folio_try_dup_anon_rmap_pmd()
  mm/memory: page_try_dup_anon_rmap() -> folio_try_dup_anon_rmap_pte()
  mm/rmap: remove page_try_dup_anon_rmap()
  mm: convert page_try_share_anon_rmap() to
    folio_try_share_anon_rmap_[pte|pmd]()

mm/rmap: rename COMPOUND_MAPPED to ENTIRELY_MAPPED

 Documentation/mm/transhuge.rst       |   4 +-
 Documentation/mm/unevictable-lru.rst |   4 +-
 include/linux/mm.h                   |   6 +-
 include/linux/rmap.h                 | 380 +++++++++++++++++++-----
 kernel/events/uprobes.c              |   2 +-
 mm/gup.c                             |   2 +-
 mm/huge_memory.c                     |  85 +++---
 mm/hugetlb.c                         |  21 +-
 mm/internal.h                        |  12 +-
 mm/khugepaged.c                      |  17 +-
 mm/ksm.c                             |  15 +-
 mm/memory-failure.c                  |   4 +-
 mm/memory.c                          |  60 ++--
 mm/migrate.c                         |  12 +-
 mm/migrate_device.c                  |  41 +--
 mm/mmu_gather.c                      |   2 +-
 mm/rmap.c                            | 422 ++++++++++++++++-----------
 mm/swapfile.c                        |   2 +-
 mm/userfaultfd.c                     |   2 +-
 19 files changed, 709 insertions(+), 384 deletions(-)
Signature · 1 line
2.41.0