All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] mm/memory: use folio lock/ref helpers in do_swap_page
@ 2026-07-21  1:33 Hongfu Li
  2026-07-21 10:21 ` David Hildenbrand (Arm)
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Hongfu Li @ 2026-07-21  1:33 UTC (permalink / raw)
  To: akpm, david, ljs, liam, vbabka, rppt, surenb, mhocko
  Cc: linux-mm, linux-kernel, hongfu.li, Hongfu Li

From: Hongfu Li <lihongfu@kylinos.cn>

Replace page-level lock/ref operations with their folio equivalents
in the device_private migration path of do_swap_page().

Introduce a local fault_folio to hold page_folio(vmf->page) to avoid
repeated page_folio() invocations.

Signed-off-by: Hongfu Li <lihongfu@kylinos.cn>
---
 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;
+
 			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.
 			 */
-			if (trylock_page(vmf->page)) {
+			if (folio_trylock(fault_folio)) {
 				struct dev_pagemap *pgmap;
 
-				get_page(vmf->page);
+				folio_get(fault_folio);
 				pte_unmap_unlock(vmf->pte, vmf->ptl);
 				pgmap = page_pgmap(vmf->page);
 				ret = pgmap->ops->migrate_to_ram(vmf);
-				unlock_page(vmf->page);
-				put_page(vmf->page);
+				folio_unlock(fault_folio);
+				folio_put(fault_folio);
 			} else {
 				pte_unmap(vmf->pte);
 				softleaf_entry_wait_on_locked(entry, vmf->ptl);
-- 
2.54.0


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

* Re: [PATCH] mm/memory: use folio lock/ref helpers in do_swap_page
  2026-07-21  1:33 [PATCH] mm/memory: use folio lock/ref helpers in do_swap_page Hongfu Li
@ 2026-07-21 10:21 ` David Hildenbrand (Arm)
  2026-07-22  1:02 ` Andrew Morton
  2026-07-22  1:25 ` Matthew Wilcox
  2 siblings, 0 replies; 6+ messages in thread
From: David Hildenbrand (Arm) @ 2026-07-21 10:21 UTC (permalink / raw)
  To: Hongfu Li, akpm, ljs, liam, vbabka, rppt, surenb, mhocko
  Cc: linux-mm, linux-kernel, Hongfu Li

On 7/21/26 03:33, Hongfu Li wrote:
> From: Hongfu Li <lihongfu@kylinos.cn>
> 
> Replace page-level lock/ref operations with their folio equivalents
> in the device_private migration path of do_swap_page().
> 
> Introduce a local fault_folio to hold page_folio(vmf->page) to avoid
> repeated page_folio() invocations.
> 
> Signed-off-by: Hongfu Li <lihongfu@kylinos.cn>
> ---

Acked-by: David Hildenbrand (Arm) <david@kernel.org>

-- 
Cheers,

David


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

* Re: [PATCH] mm/memory: use folio lock/ref helpers in do_swap_page
  2026-07-21  1:33 [PATCH] mm/memory: use folio lock/ref helpers in do_swap_page Hongfu Li
  2026-07-21 10:21 ` David Hildenbrand (Arm)
@ 2026-07-22  1:02 ` Andrew Morton
  2026-07-22  6:23   ` Hongfu Li
  2026-07-22  1:25 ` Matthew Wilcox
  2 siblings, 1 reply; 6+ messages in thread
From: Andrew Morton @ 2026-07-22  1:02 UTC (permalink / raw)
  To: Hongfu Li
  Cc: david, ljs, liam, vbabka, rppt, surenb, mhocko, linux-mm,
	linux-kernel, Hongfu Li

On Tue, 21 Jul 2026 09:33:47 +0800 Hongfu Li <hongfu.li@linux.dev> wrote:

> From: Hongfu Li <lihongfu@kylinos.cn>
> 
> Replace page-level lock/ref operations with their folio equivalents
> in the device_private migration path of do_swap_page().
> 
> Introduce a local fault_folio to hold page_folio(vmf->page) to avoid
> repeated page_folio() invocations.
> 
> ...
>
> --- 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;
> +
>  			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);

AI review thinks we shouldn't be reading vmf->page until after taking
the page table lock:

	https://sashiko.dev/#/patchset/20260721013347.65698-1-hongfu.li@linux.dev




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

* Re: [PATCH] mm/memory: use folio lock/ref helpers in do_swap_page
  2026-07-21  1:33 [PATCH] mm/memory: use folio lock/ref helpers in do_swap_page Hongfu Li
  2026-07-21 10:21 ` David Hildenbrand (Arm)
  2026-07-22  1:02 ` Andrew Morton
@ 2026-07-22  1:25 ` Matthew Wilcox
  2026-07-22  7:46   ` Hongfu Li
  2 siblings, 1 reply; 6+ messages in thread
From: Matthew Wilcox @ 2026-07-22  1:25 UTC (permalink / raw)
  To: Hongfu Li
  Cc: akpm, david, ljs, liam, vbabka, rppt, surenb, mhocko, linux-mm,
	linux-kernel, Hongfu Li

On Tue, Jul 21, 2026 at 09:33:47AM +0800, Hongfu Li wrote:
> From: Hongfu Li <lihongfu@kylinos.cn>
> 
> 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@kylinos.cn>
> ---
>  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.


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

* Re: [PATCH] mm/memory: use folio lock/ref helpers in do_swap_page
  2026-07-22  1:02 ` Andrew Morton
@ 2026-07-22  6:23   ` Hongfu Li
  0 siblings, 0 replies; 6+ messages in thread
From: Hongfu Li @ 2026-07-22  6:23 UTC (permalink / raw)
  To: akpm
  Cc: david, hongfu.li, liam, lihongfu, linux-kernel, linux-mm, ljs,
	mhocko, rppt, surenb, vbabka

> > --- 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;
> > +
> >  			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);
> 
> AI review thinks we shouldn't be reading vmf->page until after taking
> the page table lock:
> 
> 	https://sashiko.dev/#/patchset/20260721013347.65698-1-hongfu.li@linux.dev

Thanks for the review.

The AI review makes a valid observation: `vmf->page` should not be accessed
before holding the page table lock.

I've moved `fault_folio = page_folio(vmf->page)` after `pte_offset_map_lock()`
and the PTE validation check, so `vmf->page` is now read under the page table
lock. This fix will be included in v2.

Best regards,
Hongfu


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

* Re: [PATCH] mm/memory: use folio lock/ref helpers in do_swap_page
  2026-07-22  1:25 ` Matthew Wilcox
@ 2026-07-22  7:46   ` Hongfu Li
  0 siblings, 0 replies; 6+ messages in thread
From: Hongfu Li @ 2026-07-22  7:46 UTC (permalink / raw)
  To: willy
  Cc: akpm, david, hongfu.li, liam, lihongfu, linux-kernel, linux-mm,
	ljs, mhocko, rppt, surenb, vbabka

> > From: Hongfu Li <lihongfu@kylinos.cn>
> > 
> > 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.

Many thanks for your patient guidance.
I will update the commit message per your suggestion for v2.

> > Introduce a local fault_folio to hold page_folio(vmf->page) to avoid
> > repeated page_folio() invocations.
> > 
> > Signed-off-by: Hongfu Li <lihongfu@kylinos.cn>
> > ---
> >  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.

Thanks, will drop the local fault_folio and reuse the existing folio
variable. This change will be included in v2.

Agreed that we can split out helpers from do_swap_page, and I will add
a new patch for this refactoring in v2.

> >  			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.

Right, I'll update the comment to "folio" to match. Will include in v2.

Best regards,
Hongfu

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

end of thread, other threads:[~2026-07-22  7:46 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-21  1:33 [PATCH] mm/memory: use folio lock/ref helpers in do_swap_page Hongfu Li
2026-07-21 10:21 ` David Hildenbrand (Arm)
2026-07-22  1:02 ` Andrew Morton
2026-07-22  6:23   ` Hongfu Li
2026-07-22  1:25 ` Matthew Wilcox
2026-07-22  7:46   ` Hongfu Li

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.