Re: [PATCH] mm/memory: use folio lock/ref helpers in do_swap_page

From: Matthew Wilcox

Date: Tue Jul 21 2026 - 21:26:11 EST


On Tue, Jul 21, 2026 at 09:33:47AM +0800, Hongfu Li wrote:
> From: Hongfu Li <lihongfu@xxxxxxxxxx>
>
> Replace page-level lock/ref operations with their folio equivalents
> in the device_private migration path of do_swap_page().

When I do folio conversions, I like to mention the actual advantage
of the patch. Here, I'd say something like ...

mm: Use a folio in the softleaf_is_device_private path

Use the folio APIs, replacing four calls to compound_head() with one.



> Introduce a local fault_folio to hold page_folio(vmf->page) to avoid
> repeated page_folio() invocations.
>
> Signed-off-by: Hongfu Li <lihongfu@xxxxxxxxxx>
> ---
> mm/memory.c | 11 +++++++----
> 1 file changed, 7 insertions(+), 4 deletions(-)
>
> diff --git a/mm/memory.c b/mm/memory.c
> index d5e87624f692..bd27372a1052 100644
> --- a/mm/memory.c
> +++ b/mm/memory.c
> @@ -4852,6 +4852,8 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
> vmf->page = softleaf_to_page(entry);
> ret = remove_device_exclusive_entry(vmf);
> } else if (softleaf_is_device_private(entry)) {
> + struct folio *fault_folio;

You can just use the existing 'folio' variable.

Although this function is far too long (400 lines! 14 function-scope
variables!) so I don't blame you for not noticing that. Pulling a
do_non_swap_page() out of it might be a kindness.

> if (vmf->flags & FAULT_FLAG_VMA_LOCK) {
> /*
> * migrate_to_ram is not yet ready to operate
> @@ -4863,6 +4865,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
> }
>
> vmf->page = softleaf_to_page(entry);
> + fault_folio = page_folio(vmf->page);
> vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
> vmf->address, &vmf->ptl);
> if (unlikely(!vmf->pte ||
> @@ -4874,15 +4877,15 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
> * Get a page reference while we know the page can't be
> * freed.

You should also replace 'page' with 'folio' here. Pages don't have
refcounts, folios do.