[PATCH 3/7] mm/gup: local lru_add_drain() to avoid lru_add_drain_all()

Hugh Dickins posted 7 patches 1 month ago
There is a newer version of this series
[PATCH 3/7] mm/gup: local lru_add_drain() to avoid lru_add_drain_all()
Posted by Hugh Dickins 1 month ago
In many cases, if collect_longterm_unpinnable_folios() does need to
drain the LRU cache to release a reference, the cache in question is
on this same CPU, and much more efficiently drained by a preliminary
local lru_add_drain(), than the later cross-CPU lru_add_drain_all().

Marked for stable, to counter the increase in lru_add_drain_all()s
from "mm/gup: check ref_count instead of lru before migration".
Note for clean backports: can take 6.16 commit a03db236aebf ("gup:
optimize longterm pin_user_pages() for large folio") first.

Signed-off-by: Hugh Dickins <hughd@google.com>
Cc: <stable@vger.kernel.org>
---
 mm/gup.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/mm/gup.c b/mm/gup.c
index 82aec6443c0a..9f7c87f504a9 100644
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -2291,6 +2291,8 @@ static unsigned long collect_longterm_unpinnable_folios(
 	struct folio *folio;
 	long i = 0;
 
+	lru_add_drain();
+
 	for (folio = pofs_get_folio(pofs, i); folio;
 	     folio = pofs_next_folio(folio, pofs, &i)) {
 
-- 
2.51.0
Re: [PATCH 3/7] mm/gup: local lru_add_drain() to avoid lru_add_drain_all()
Posted by David Hildenbrand 1 month ago
On 31.08.25 11:08, Hugh Dickins wrote:
> In many cases, if collect_longterm_unpinnable_folios() does need to
> drain the LRU cache to release a reference, the cache in question is
> on this same CPU, and much more efficiently drained by a preliminary
> local lru_add_drain(), than the later cross-CPU lru_add_drain_all().
> 
> Marked for stable, to counter the increase in lru_add_drain_all()s
> from "mm/gup: check ref_count instead of lru before migration".
> Note for clean backports: can take 6.16 commit a03db236aebf ("gup:
> optimize longterm pin_user_pages() for large folio") first.
> 
> Signed-off-by: Hugh Dickins <hughd@google.com>
> Cc: <stable@vger.kernel.org>
> ---
>   mm/gup.c | 2 ++
>   1 file changed, 2 insertions(+)
> 
> diff --git a/mm/gup.c b/mm/gup.c
> index 82aec6443c0a..9f7c87f504a9 100644
> --- a/mm/gup.c
> +++ b/mm/gup.c
> @@ -2291,6 +2291,8 @@ static unsigned long collect_longterm_unpinnable_folios(
>   	struct folio *folio;
>   	long i = 0;
>   
> +	lru_add_drain();
> +
>   	for (folio = pofs_get_folio(pofs, i); folio;
>   	     folio = pofs_next_folio(folio, pofs, &i)) {
>   

Do we really want to drain all the time we enter 
collect_longterm_unpinnable_folios(), or only if we detect an actual 
problem? (unexpected reference?)

-- 
Cheers

David / dhildenb
Re: [PATCH 3/7] mm/gup: local lru_add_drain() to avoid lru_add_drain_all()
Posted by Hugh Dickins 3 weeks, 4 days ago
On Mon, 1 Sep 2025, David Hildenbrand wrote:
> On 31.08.25 11:08, Hugh Dickins wrote:
> > In many cases, if collect_longterm_unpinnable_folios() does need to
> > drain the LRU cache to release a reference, the cache in question is
> > on this same CPU, and much more efficiently drained by a preliminary
> > local lru_add_drain(), than the later cross-CPU lru_add_drain_all().
> > 
> > Marked for stable, to counter the increase in lru_add_drain_all()s
> > from "mm/gup: check ref_count instead of lru before migration".
> > Note for clean backports: can take 6.16 commit a03db236aebf ("gup:
> > optimize longterm pin_user_pages() for large folio") first.
> > 
> > Signed-off-by: Hugh Dickins <hughd@google.com>
> > Cc: <stable@vger.kernel.org>
> > ---
> >   mm/gup.c | 2 ++
> >   1 file changed, 2 insertions(+)
> > 
> > diff --git a/mm/gup.c b/mm/gup.c
> > index 82aec6443c0a..9f7c87f504a9 100644
> > --- a/mm/gup.c
> > +++ b/mm/gup.c
> > @@ -2291,6 +2291,8 @@ static unsigned long
> > collect_longterm_unpinnable_folios(
> >    struct folio *folio;
> >    long i = 0;
> >   +	lru_add_drain();
> > +
> >    for (folio = pofs_get_folio(pofs, i); folio;
> >         folio = pofs_next_folio(folio, pofs, &i)) {
> >   
> 
> Do we really want to drain all the time we enter
> collect_longterm_unpinnable_folios(), or only if we detect an actual problem?
> (unexpected reference?)

It looked nice and simple to me (hmm, where's the blank line before
lru_add_drain() gone? weird, something wrong with my mail setup),
I've never avoided an lru_add_drain() before; but you're right,
we dom't need to do that every time, fixed in v2 - thanks.

Hugh