Skip to content

[Deepin-Kernel-SIG] [linux 6.6.y] [Upstream] [PATCH v3 00/10] mm: convert mm counter to take a folio#1977

Merged
opsiff merged 10 commits into
deepin-community:linux-6.6.yfrom
opsiff:linux-6.6.y-2026-07-13-update-2
Jul 13, 2026
Merged

[Deepin-Kernel-SIG] [linux 6.6.y] [Upstream] [PATCH v3 00/10] mm: convert mm counter to take a folio#1977
opsiff merged 10 commits into
deepin-community:linux-6.6.yfrom
opsiff:linux-6.6.y-2026-07-13-update-2

Conversation

@opsiff

@opsiff opsiff commented Jul 13, 2026

Copy link
Copy Markdown
Member

Link: https://lore.kernel.org/all/20240111152429.3374566-1-willy@infradead.org/T/#u

Make sure all mm_counter() and mm_counter_file() callers have a folio,
then convert mm counter functions to take a folio, which saves some
compound_head() calls.

v3:

  • Rebase on top of my similar patch to add pfn_swap_entry_folio()
  • Various wordsmithing & notes about where we're actually saving
    calls to compound_head().

v2:

  • rebase on v6.7, since most of mm counter callers already with a folio,
    drop some unneeded changes.

v1:

  • rename should_zap_page() to should_zap_folio(), per Matthew Wilcox
  • pass page to page_remove_rmap/page_try_dup_anon_rmap for
    device private page, per Matthew Wilcox

Kefeng Wang (7):
s390: use pfn_swap_entry_folio() in ptep_zap_swap_entry()
mm: use pfn_swap_entry_folio() in __split_huge_pmd_locked()
mm: use pfn_swap_entry_to_folio() in zap_huge_pmd()
mm: use pfn_swap_entry_folio() in copy_nonpresent_pte()
mm: Convert to should_zap_page() to should_zap_folio()
mm: convert mm_counter() to take a folio
mm: convert mm_counter_file() to take a folio

Matthew Wilcox (Oracle) (3):
mm: Add pfn_swap_entry_folio()
proc: Use pfn_swap_entry_folio where obvious
mprotect: Use pfn_swap_entry_folio

arch/s390/mm/pgtable.c | 4 ++--
fs/proc/task_mmu.c | 4 ++--
include/linux/mm.h | 12 +++++-----
include/linux/swapops.h | 13 +++++++++++
kernel/events/uprobes.c | 2 +-
mm/filemap.c | 2 +-
mm/huge_memory.c | 23 ++++++++++---------
mm/khugepaged.c | 4 ++--
mm/memory.c | 49 ++++++++++++++++++++++-------------------
mm/mprotect.c | 4 ++--
mm/rmap.c | 10 ++++-----
mm/userfaultfd.c | 2 +-
12 files changed, 74 insertions(+), 55 deletions(-)

--
2.43.0

Summary by Sourcery

Convert memory management accounting and swap migration helpers to operate on folios instead of pages, and propagate these changes through mm, arch, and proc code paths to reduce compound_head() usage and align with folio-based APIs.

Enhancements:

  • Introduce pfn_swap_entry_folio() for accessing folios from PFN-based swap entries with migration-entry safety checks.
  • Update mm_counter() and mm_counter_file() and all their callers to take folio arguments, standardizing RSS accounting on folios.
  • Rename and adapt should_zap_page() to should_zap_folio() and adjust zap paths and migration handling to use folio-aware checks.
  • Replace pfn_swap_entry_to_page() usage with pfn_swap_entry_folio() in huge page, rmap, mprotect, userfaultfd, proc, uprobe, and s390 MM code paths for folio-centric handling.

Matthew Wilcox (Oracle) and others added 9 commits July 13, 2026 17:45
mainline inclusion
from mainline-v6.9-rc1
category: performance

Patch series "mm: convert mm counter to take a folio", v3.

Make sure all mm_counter() and mm_counter_file() callers have a folio,
then convert mm counter functions to take a folio, which saves some
compound_head() calls.

This patch (of 10):

Thanks to the compound_head() hidden inside PageLocked(), this saves a
call to compound_head() over calling page_folio(pfn_swap_entry_to_page())

Link: https://lkml.kernel.org/r/20240111152429.3374566-1-willy@infradead.org
Link: https://lkml.kernel.org/r/20240111152429.3374566-2-willy@infradead.org
Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
Cc: David Hildenbrand <david@redhat.com>
Cc: Kefeng Wang <wangkefeng.wang@huawei.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
(cherry picked from commit 5662400)
Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
mainline inclusion
from mainline-v6.9-rc1
category: performance

These callers only pass the result to PageAnon(), so we can save the extra
call to compound_head() by using pfn_swap_entry_folio().

Link: https://lkml.kernel.org/r/20240111152429.3374566-3-willy@infradead.org
Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
Cc: David Hildenbrand <david@redhat.com>
Cc: Kefeng Wang <wangkefeng.wang@huawei.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
(cherry picked from commit 7101422)
Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
mainline inclusion
from mainline-v6.9-rc1
category: performance

We only want to know whether the folio is anonymous, so use
pfn_swap_entry_folio() and save a call to compound_head().

Link: https://lkml.kernel.org/r/20240111152429.3374566-4-willy@infradead.org
Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
Cc: David Hildenbrand <david@redhat.com>
Cc: Kefeng Wang <wangkefeng.wang@huawei.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
(cherry picked from commit f2d571b)
Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
mainline inclusion
from mainline-v6.9-rc1
category: performance

Call pfn_swap_entry_folio() in ptep_zap_swap_entry() as preparation for
converting mm counter functions to take a folio.

Link: https://lkml.kernel.org/r/20240111152429.3374566-5-willy@infradead.org
Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
Cc: David Hildenbrand <david@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
(cherry picked from commit 0601ac8)
Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
mainline inclusion
from mainline-v6.9-rc1
category: performance

Call pfn_swap_entry_folio() in __split_huge_pmd_locked() as preparation
for converting mm counter functions to take a folio.

Link: https://lkml.kernel.org/r/20240111152429.3374566-6-willy@infradead.org
Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
Cc: David Hildenbrand <david@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
(cherry picked from commit 439992f)
Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
mainline inclusion
from mainline-v6.9-rc1
category: performance

Call pfn_swap_entry_to_folio() in zap_huge_pmd() as preparation for
converting mm counter functions to take a folio.  Saves a call to
compound_head() embedded inside PageAnon().

Link: https://lkml.kernel.org/r/20240111152429.3374566-7-willy@infradead.org
Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
Cc: David Hildenbrand <david@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
(cherry picked from commit 0103b27)
Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
mainline inclusion
from mainline-v6.9-rc1
category: performance

Call pfn_swap_entry_folio() as preparation for converting mm counter
functions to take a folio.

Link: https://lkml.kernel.org/r/20240111152429.3374566-8-willy@infradead.org
Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
Cc: David Hildenbrand <david@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
(cherry picked from commit 530c2a0)
Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
mainline inclusion
from mainline-v6.9-rc1
category: performance

Make should_zap_page() take a folio and rename it to should_zap_folio() as
preparation for converting mm counter functions to take a folio.  Saves a
call to compound_head() hidden inside PageAnon().

[wangkefeng.wang@huawei.com: fix used-uninitialized warning]
  Link: https://lkml.kernel.org/r/962a7993-fce9-4de8-85cd-25e290f25736@huawei.com
Link: https://lkml.kernel.org/r/20240111152429.3374566-9-willy@infradead.org
Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
Cc: David Hildenbrand <david@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
(cherry picked from commit eabafaa)
Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
mainline inclusion
from mainline-v6.9-rc1
category: performance

Now all callers of mm_counter() have a folio, convert mm_counter() to take
a folio.  Saves a call to compound_head() hidden inside PageAnon().

Link: https://lkml.kernel.org/r/20240111152429.3374566-10-willy@infradead.org
Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
Cc: David Hildenbrand <david@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Conflicts:
	mm/memory.c
(cherry picked from commit a23f517)
Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
@sourcery-ai

sourcery-ai Bot commented Jul 13, 2026

Copy link
Copy Markdown

Reviewer's Guide

This PR introduces folio-aware mm counter helpers and a pfn_swap_entry_folio() helper, then systematically updates migration, zap, rmap, THP, userfaultfd, proc, uprobes and s390 code paths to operate on folios instead of pages, reducing compound_head() usage and aligning swap/migration handling with folio-centric APIs.

Flow diagram for folio-based mm counter and swap entry handling

flowchart LR
  entry[swp_entry_t entry]
  pfn["pfn_swap_entry_folio(entry)"]
  folio[folio]
  anon["folio_test_anon(folio)"]
  swapb["folio_test_swapbacked(folio)"]
  mcf["mm_counter_file(folio)"]
  mc["mm_counter(folio)"]
  anonrss[MM_ANONPAGES]
  filepages[MM_FILEPAGES]
  shmem[MM_SHMEMPAGES]

  entry --> pfn --> folio
  folio --> anon
  anon -- true --> anonrss
  anon -- false --> swapb
  swapb -- true --> mcf --> shmem
  swapb -- false --> mcf --> filepages
  folio --> mc --> anonrss
  folio --> mc --> mcf
Loading

File-Level Changes

Change Details Files
Introduce pfn_swap_entry_folio() and update swap/migration users to work on folios rather than pages.
  • Add pfn_swap_entry_folio() helper that returns a folio from a pfn-backed swap entry and asserts the folio is locked for migration entries.
  • Replace pfn_swap_entry_to_page()+page_folio() sequences with pfn_swap_entry_folio() in migration and swap-handling paths.
  • Update checks like PageAnon(...) to folio_test_anon(...) where swap entries are converted to folios.
include/linux/swapops.h
mm/huge_memory.c
mm/mprotect.c
mm/filemap.c
mm/memory.c
mm/khugepaged.c
mm/rmap.c
fs/proc/task_mmu.c
arch/s390/mm/pgtable.c
Convert mm_counter() and mm_counter_file() to take a struct folio, and adjust all call sites in mm and related subsystems.
  • Redefine mm_counter_file() and mm_counter() to take a folio and use folio_test_swapbacked()/folio_test_anon() internally.
  • Update inc_mm_counter(), dec_mm_counter(), add_mm_counter(), and RSS accounting sites to pass folios instead of pages.
  • Clean up call sites that previously needed PageAnon()/PageSwapBacked() on struct page and now rely on folio helpers.
include/linux/mm.h
mm/memory.c
mm/huge_memory.c
mm/khugepaged.c
mm/rmap.c
mm/userfaultfd.c
kernel/events/uprobes.c
arch/s390/mm/pgtable.c
Rename should_zap_page() to should_zap_folio() and refactor zap paths to operate purely on folios for decision-making and RSS accounting.
  • Introduce should_zap_folio() that accepts a folio pointer (possibly NULL) and bases decisions on folio_test_anon().
  • Adjust zap_pte_range() and related zap logic to obtain folios up front and call should_zap_folio() instead of should_zap_page().
  • Update RSS updates in zap paths to use mm_counter(folio) and remove now-unnecessary page-to-folio conversions.
mm/memory.c

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please ask for approval from opsiff. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • In zap_huge_pmd(), the non-present huge PMD path when !thp_migration_supported() leaves folio as NULL but still calls folio_test_anon(folio) and mm_counter_file(folio), which will dereference NULL; this path should either return early, set folio appropriately, or guard those calls.
  • The new pfn_swap_entry_folio() BUG_ON(!folio_test_locked(folio)) tightens the locking requirements for migration entries; please double-check all call sites (e.g. pagemap_* helpers, migration_entry_wait_on_locked, zap_pte_range) to ensure the folio is guaranteed locked there or consider weakening/annotating the invariant if that’s not universally true.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In zap_huge_pmd(), the non-present huge PMD path when !thp_migration_supported() leaves folio as NULL but still calls folio_test_anon(folio) and mm_counter_file(folio), which will dereference NULL; this path should either return early, set folio appropriately, or guard those calls.
- The new pfn_swap_entry_folio() BUG_ON(!folio_test_locked(folio)) tightens the locking requirements for migration entries; please double-check all call sites (e.g. pagemap_* helpers, migration_entry_wait_on_locked, zap_pte_range) to ensure the folio is guaranteed locked there or consider weakening/annotating the invariant if that’s not universally true.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

mainline inclusion
from mainline-v6.9-rc1
category: performance

Now all callers of mm_counter_file() have a folio, convert
mm_counter_file() to take a folio.  Saves a call to compound_head() hidden
inside PageSwapBacked().

Link: https://lkml.kernel.org/r/20240111152429.3374566-11-willy@infradead.org
Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
Cc: David Hildenbrand <david@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
(cherry picked from commit 6b27cc6)
Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
@opsiff opsiff force-pushed the linux-6.6.y-2026-07-13-update-2 branch from c928ce0 to 8850dd8 Compare July 13, 2026 10:03
@opsiff opsiff merged commit 1130478 into deepin-community:linux-6.6.y Jul 13, 2026
14 of 16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants