Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Yeoreum Yun <yeoreum.yun@arm.com>
To: "Liam R. Howlett" <liam@infradead.org>
Cc: Yeoreum Yun <yeoreum.yun@arm.com>, Jason Gunthorpe <jgg@ziepe.ca>,
	John Hubbard <jhubbard@nvidia.com>, Peter Xu <peterx@redhat.com>,
	linux-mm@kvack.org, Jan Kara <jack@suse.cz>,
	Josef Bacik <josef@toxicpanda.com>,
	Johannes Weiner <hannes@cmpxchg.org>,
	Lorenzo Stoakes <ljs@kernel.org>,
	Vlastimil Babka <vbabka@kernel.org>,
	Mike Rapoport <rppt@kernel.org>,
	Suren Baghdasaryan <surenb@google.com>,
	Michal Hocko <mhocko@suse.com>, Jann Horn <jannh@google.com>,
	Pedro Falcato <pfalcato@suse.de>,
	David Hildenbrand <david@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	Harrison Crosse <harrison@crosse.dev>
Subject: Re: [PATCH 1/3] mm: gup: handle fault retries in __mm_populate()
Date: Fri, 4 Dec 2026 08:30:15 +0000	[thread overview]
Message-ID: <axJ6F_epADx0JFqx@e129823.arm.com> (raw)
In-Reply-To: <5tquqhc2n25vmdevfqsq76ws6r2cb4ifp2b3pz4fs6mrhjgio4@uktohq7tjr56>

On Thu, Dec 03, 2026 at 10:23:05PM -0500, Liam R. Howlett wrote:
> On 26/12/03 03:17PM, Yeoreum Yun wrote:
> > When populating a file mapping whose reads repeatedly fail,
> > __mm_populate() can retry the same page indefinitely.
> > 
> > On the first fault, filemap_fault() may drop mmap_lock before reading
> > the page and return VM_FAULT_RETRY without reporting the read error.
> > __get_user_pages() then returns the number of pages processed, which
> > may be zero. However, __mm_populate() retries the faulting page without
> > FOLL_TRIED, allowing this sequence to repeat indefinitely.
> > 
> > To address this, track whether __get_user_pages() dropped mmap_lock
> > After reacquiring the lock and looking up the VMA again, retry a single
> > page with FOLL_TRIED.
> > 
> > Fixes: 6b4c9f446981 ("filemap: drop the mmap_sem for all blocking operations")
> 
> Cc: Stable ?

Ah I missed. Thanks.


> 
> > Co-developed-by: Harrison Crosse <harrison@crosse.dev>
> 
> You are missing Harrison's signed-off-by here.

Yeap. Will do.

> 
> > Signed-off-by: Yeoreum Yun <yeoreum.yun@arm.com>
> > ---
> >  mm/gup.c | 27 ++++++++++++++++++++++-----
> >  1 file changed, 22 insertions(+), 5 deletions(-)
> > 
> > diff --git a/mm/gup.c b/mm/gup.c
> > index ec0bc9920216..d2ff0b667e37 100644
> > --- a/mm/gup.c
> > +++ b/mm/gup.c
> > @@ -1886,7 +1886,7 @@ static __always_inline long __get_user_pages_locked(struct mm_struct *mm,
> >  }
> >  
> >  /**
> > - * populate_vma_page_range() -  populate a range of pages in the vma.
> > + * __populate_vma_page_range() -  populate a range of pages in the vma.
> >   * @vma:   target vma
> >   * @start: start address
> >   * @end:   end address
> 
> Please add @retry: to the documentation, it'll create a warning for the
> docs people otherwise.

Okay. Thanks!

> 
> > @@ -1905,8 +1905,8 @@ static __always_inline long __get_user_pages_locked(struct mm_struct *mm,
> >   * If @locked is non-NULL, it must held for read only and may be
> >   * released.  If it's released, *@locked will be set to 0.
> >   */
> > -long populate_vma_page_range(struct vm_area_struct *vma,
> > -		unsigned long start, unsigned long end, int *locked)
> > +static long __populate_vma_page_range(struct vm_area_struct *vma,
> > +		unsigned long start, unsigned long end, int *locked, bool retry)
> >  {
> >  	struct mm_struct *mm = vma->vm_mm;
> >  	unsigned long nr_pages = (end - start) / PAGE_SIZE;
> > @@ -1949,8 +1949,10 @@ long populate_vma_page_range(struct vm_area_struct *vma,
> >  	else
> >  		gup_flags |= FOLL_FORCE;
> >  
> > -	if (locked)
> > +	if (locked) {
> >  		gup_flags |= FOLL_UNLOCKABLE;
> > +		gup_flags |= (retry) ? FOLL_TRIED : 0;
> > +	}
> 
> An if statement is longer, but worth it here, imo.
> 
> >  
> >  	/*
> >  	 * We made sure addr is within a VMA, so the following will
> > @@ -1962,6 +1964,13 @@ long populate_vma_page_range(struct vm_area_struct *vma,
> >  	return ret;
> >  }
> >  
> > +
> 
> double blank line.

Grr. Sorry. I'll get rid of it.

> 
> > +long populate_vma_page_range(struct vm_area_struct *vma,
> > +		unsigned long start, unsigned long end, int *locked)
> > +{
> > +	return __populate_vma_page_range(vma, start, end, locked, false);
> > +}
> > +
> >  /*
> >   * faultin_page_range() - populate (prefault) page tables inside the
> >   *			  given range readable/writable
> > @@ -2028,10 +2037,12 @@ int __mm_populate(unsigned long start, unsigned long len, int ignore_errors)
> >  	struct vm_area_struct *vma = NULL;
> >  	int locked = 0;
> >  	long ret = 0;
> > +	bool tried = false;
> >  
> >  	end = start + len;
> >  
> >  	for (nstart = start; nstart < end; nstart = nend) {
> > +		unsigned long populate_end;
> >  		/*
> >  		 * We want to fault in pages for [nstart; end) address range.
> >  		 * Find first corresponding VMA.
> > @@ -2058,8 +2069,14 @@ int __mm_populate(unsigned long start, unsigned long len, int ignore_errors)
> >  		 * Now fault in a range of pages. populate_vma_page_range()
> >  		 * double checks the vma flags, so that it won't mlock pages
> >  		 * if the vma was already munlocked.
> > +		 *
> > +		 * If we dropped the mmap_lock due to VM_FAULT_RETRY, retry only
> > +		 * the faulting page with FOLL_TRIED, so the fault handler
> > +		 * reports errors instead of making us retry forever.
> >  		 */
> > -		ret = populate_vma_page_range(vma, nstart, nend, &locked);
> > +		populate_end = (tried) ? nstart + PAGE_SIZE : nend;
> > +		ret = __populate_vma_page_range(vma, nstart, populate_end, &locked, tried);
> > +		tried = !locked;
> 
> if the lock is dropped and another thread unmaps or moves the page, the
> retry will look up the vma at or after the start address and will hit
> the wrong one.
> 
> The initial patch saved the retry_addr to avoid this.

You're right. I handled this in a separate wrapper in an earlier version
(See another attached code), but dropped that handling while simplifying
the code because the wrapper seemed overly complicated.
I'm not sure why I overlooked it...

Anyway, I think we don't need to save the retry_addr. it's enough to clear
tried when it's required like:

@@ -2028,17 +2037,20 @@ int __mm_populate(unsigned long start, unsigned long len, int ignore_errors)
        struct vm_area_struct *vma = NULL;
        int locked = 0;
        long ret = 0;
+       bool tried = false;

        end = start + len;

        for (nstart = start; nstart < end; nstart = nend) {
+               unsigned long populate_end;
                /*
                 * We want to fault in pages for [nstart; end) address range.
                 * Find first corresponding VMA.
                 */
                if (!locked) {
                        locked = 1;
-                       mmap_read_lock(mm);
+                       if (mmap_read_lock_killable(mm))
+                               return -EINTR;
                        vma = find_vma_intersection(mm, nstart, end);
                } else if (nstart >= vma->vm_end)
                        vma = find_vma_intersection(mm, vma->vm_end, end);
                        vma = find_vma_intersection(mm, nstart, end);
                } else if (nstart >= vma->vm_end)
                        vma = find_vma_intersection(mm, vma->vm_end, end);
@@ -2050,16 +2062,28 @@ int __mm_populate(unsigned long start, unsigned long len, int ignore_errors)
                 * range with the first VMA. Also, skip undesirable VMA types.
                 */
                nend = min(end, vma->vm_end);
-               if (vma_test_any(vma, VMA_IO_BIT, VMA_PFNMAP_BIT))
+               if (vma_test_any(vma, VMA_IO_BIT, VMA_PFNMAP_BIT)) {
+                       tried = false;
                        continue;
-               if (nstart < vma->vm_start)
+               }
+
+               if (nstart < vma->vm_start) {
+                       tried = false;
                        nstart = vma->vm_start;
+               }
+
                /*
                 * Now fault in a range of pages. populate_vma_page_range()
                 * double checks the vma flags, so that it won't mlock pages
                 * if the vma was already munlocked.
+                *
+                * If we dropped the mmap_lock due to VM_FAULT_RETRY, retry only
+                * the faulting page with FOLL_TRIED, so the fault handler
+                * reports errors instead of making us retry forever.
                 */
-               ret = populate_vma_page_range(vma, nstart, nend, &locked);
+               populate_end = (tried) ? nstart + PAGE_SIZE : nend;
+               ret = __populate_vma_page_range(vma, nstart, populate_end, &locked, tried);
+               tried = !locked;
                if (ret < 0) {
                        if (ignore_errors) {
                                ret = 0;

> 
> >  		if (ret < 0) {
> >  			if (ignore_errors) {
> >  				ret = 0;
> > 
> > -- 
> > 2.43.0
> > 


---------&<--------

mm/gup.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 66 insertions(+), 18 deletions(-)

diff --git a/mm/gup.c b/mm/gup.c
index d19944c3e7915..8f77b6bf3d34d 100644
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -1886,11 +1886,12 @@ static __always_inline long __get_user_pages_locked(struct mm_struct *mm,
 }

 /**
- * populate_vma_page_range() -  populate a range of pages in the vma.
+ * __populate_vma_page_range() -  populate a range of pages in the vma.
  * @vma:   target vma
  * @start: start address
  * @end:   end address
  * @locked: whether the mmap_lock is still held
+ * @retry: whether retry or not
  *
  * This takes care of mlocking the pages too if VM_LOCKED is set.
  *
@@ -1905,8 +1906,8 @@ static __always_inline long __get_user_pages_locked(struct mm_struct *mm,
  * If @locked is non-NULL, it must held for read only and may be
  * released.  If it's released, *@locked will be set to 0.
  */
-long populate_vma_page_range(struct vm_area_struct *vma,
-               unsigned long start, unsigned long end, int *locked)
+static long __populate_vma_page_range(struct vm_area_struct *vma,
+               unsigned long start, unsigned long end, int *locked, bool retry)
 {
         struct mm_struct *mm = vma->vm_mm;
         unsigned long nr_pages = (end - start) / PAGE_SIZE;
@@ -1949,8 +1950,10 @@ long populate_vma_page_range(struct vm_area_struct *vma,
         else
                 gup_flags |= FOLL_FORCE;

-       if (locked)
+       if (locked) {
                 gup_flags |= FOLL_UNLOCKABLE;
+               gup_flags |= (retry) ? FOLL_TRIED : 0;
+       }

         /*
          * We made sure addr is within a VMA, so the following will
@@ -1962,6 +1965,47 @@ long populate_vma_page_range(struct vm_area_struct *vma,
         return ret;
 }

+static long __populate_vma_page_range_locked(struct vm_area_struct *vma,
+               unsigned long start, unsigned long end, int *locked)
+{
+       struct mm_struct *mm = current->mm;
+       struct vm_area_struct *orig_vma = vma;
+       long ret = 0;
+
+       ret = __populate_vma_page_range(vma, start, end, locked, false);
+       if (ret < 0)
+               return ret;
+
+retry:
+       if (!*locked) {
+               if (mmap_read_lock_killable(mm))
+                       return -EINTR;
+               *locked = 1;
+
+               vma = find_vma_intersection(mm, start, end);
+               if (orig_vma != vma || start < vma->vm_start)
+                       return 0;
+       }
+
+       if (ret)
+               return ret;
+
+       ret = __populate_vma_page_range(vma, start, start + PAGE_SIZE,
+                                       locked, true);
+       if (!*locked) {
+               VM_WARN_ON_ONCE(ret != 0);
+               goto retry;
+       }
+
+       return ret;
+}
+
+long populate_vma_page_range(struct vm_area_struct *vma,
+               unsigned long start, unsigned long end, int *locked)
+{
+       return __populate_vma_page_range(vma, start, end, NULL, false);
+}
+
 /*
  * faultin_page_range() - populate (prefault) page tables inside the
  *                       given range readable/writable
@@ -2027,25 +2071,27 @@ int __mm_populate(unsigned long start, unsigned long len, int ignore_errors)
         unsigned long end, nstart, nend;
         struct vm_area_struct *vma = NULL;
         int locked = 0;
+       bool renew_vma = 1;
         long ret = 0;

         end = start + len;

+       if (mmap_read_lock_killable(mm))
+               return -EINTR;
+       locked = 1;
+
         for (nstart = start; nstart < end; nstart = nend) {
                 /*
                  * We want to fault in pages for [nstart; end) address range.
                  * Find first corresponding VMA.
                  */
-               if (!locked) {
-                       locked = 1;
-                       if (mmap_read_lock_killable(mm))
-                               return -EINTR;
+               if (renew_vma || nstart >= vma->vm_end) {
                         vma = find_vma_intersection(mm, nstart, end);
-               } else if (nstart >= vma->vm_end)
-                       vma = find_vma_intersection(mm, vma->vm_end, end);
-
+                       renew_vma = 0;
+               }
                 if (!vma)
                         break;
+
                 /*
                  * Set [nstart; nend) to intersection of desired address
                  * range with the first VMA. Also, skip undesirable VMA types.
@@ -2060,14 +2106,16 @@ int __mm_populate(unsigned long start, unsigned long len, int ignore_errors)
                  * double checks the vma flags, so that it won't mlock pages
                  * if the vma was already munlocked.
                  */
-               ret = populate_vma_page_range(vma, nstart, nend, &locked);
-               if (ret < 0) {
-                       if (ignore_errors) {
-                               ret = 0;
-                               continue;       /* continue at next VMA */
-                       }
-                       break;
+               ret = __populate_vma_page_range_locked(vma, nstart, nend, &locked);
+               if (!ret) {
+                       renew_vma = 1;
+               } else if (ret < 0) {
+                       if (ret == -EINTR || !ignore_errors)
+                               break;
+                       ret = 0;
+                       continue;       /* continue at next VMA */
                 }
+
                 nend = nstart + ret * PAGE_SIZE;
                 ret = 0;
         }

-- 
Sincerely,
Yeoreum Yun


  reply	other threads:[~2026-12-04  8:30 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 15:17 [PATCH 0/3] mm: gup: handle fault retries in __mm_populate() Yeoreum Yun
2026-12-03 15:17 ` [PATCH 1/3] " Yeoreum Yun
2026-12-04  3:23   ` Liam R. Howlett
2026-12-04  8:30     ` Yeoreum Yun [this message]
2026-12-04  5:29   ` kernel test robot
2026-12-03 15:17 ` [PATCH 2/3] mm: gup: remove unused argument in populate_vma_page_range() Yeoreum Yun
2026-12-04  3:28   ` Liam R. Howlett
2026-12-04  8:41     ` Yeoreum Yun
2026-12-03 15:17 ` [PATCH 3/3] mm: gup: make mmap_lock acquisition in __mm_populate() killable Yeoreum Yun
2026-12-04  3:35   ` Liam R. Howlett
2026-12-04  8:38     ` Yeoreum Yun
2026-12-04  3:23 ` [PATCH 0/3] mm: gup: handle fault retries in __mm_populate() Liam R. Howlett

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=axJ6F_epADx0JFqx@e129823.arm.com \
    --to=yeoreum.yun@arm.com \
    --cc=akpm@linux-foundation.org \
    --cc=david@kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=harrison@crosse.dev \
    --cc=jack@suse.cz \
    --cc=jannh@google.com \
    --cc=jgg@ziepe.ca \
    --cc=jhubbard@nvidia.com \
    --cc=josef@toxicpanda.com \
    --cc=liam@infradead.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=mhocko@suse.com \
    --cc=peterx@redhat.com \
    --cc=pfalcato@suse.de \
    --cc=rppt@kernel.org \
    --cc=surenb@google.com \
    --cc=vbabka@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox