From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9832B606A330 for ; Fri, 4 Dec 2026 08:30:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=Ws9C2aiw Received: by kanga.kvack.org (Postfix) id 246A86B05B1; Fri, 4 Dec 2026 03:30:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1F78A6B05B4; Fri, 4 Dec 2026 03:30:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0E5FE6B05BB; Fri, 4 Dec 2026 03:30:25 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id D5FDA6B05B4 for ; Fri, 4 Dec 2026 03:30:24 -0500 (EST) Received: from smtpin23.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 707C6A02CB for ; Fri, 4 Dec 2026 08:30:24 +0000 (UTC) X-FDA: 85505628768.23.8684D04 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf24.hostedemail.com (Postfix) with ESMTP id 8230E18000D for ; Fri, 4 Dec 2026 08:30:22 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=arm.com header.s=foss header.b=Ws9C2aiw; spf=pass (imf24.hostedemail.com: domain of yeoreum.yun@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=yeoreum.yun@arm.com; dmarc=pass (policy=none) header.from=arm.com ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796373022; b=tIPI3nWSHP+QMk4RzyS21L86OI3TuMCmkCLoBT0K1JaVurJbuFh79VfRBQdlW8KlAoq/tN Rr6U5ZbRCFk56B2lVA8knAeeJ7BQDYXpPd50VJkTq5Uui0/D0o0rBdVt7gFKr1f3lYuEp7 vUVZAdk5aHnWwPQLNYJRpmc/gMFPR6Q= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796373022; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=8iX3G8JIbTZJklLi5dH9yNRr+8Bepze49G/2rprdqLQ=; b=0Y/QTK++jrnWXzx0n4iY0trmuzkVWlw1khVW3+csmisrjlcLdePIpyOsUkBbFLvL+HSZKK 3Bjfsc5mqpFmUnTZZhRid63zTKMJn2LB8jqDhma/uKros/cMYvz55P/dKSLHrPm7g9Ohwc uJU1PkOy9D4c66yuP2zv84Yz8Oo92CQ= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=arm.com header.s=foss header.b=Ws9C2aiw; spf=pass (imf24.hostedemail.com: domain of yeoreum.yun@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=yeoreum.yun@arm.com; dmarc=pass (policy=none) header.from=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 882B9497; Fri, 4 Dec 2026 00:30:18 -0800 (PST) Received: from e129823.arm.com (e129823.arm.com [10.2.213.3]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 4F0A83F86F; Fri, 4 Dec 2026 00:30:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1796373021; bh=Lzlmq6juQoD6UW/dowj9zhiAjFEpGUH7SnCUiS4VB5Q=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=Ws9C2aiwKh0epJhJNxB9Sn81yMtIhk6vY/qfEsckTAJahuPfsBkukqrgKgDBf7IAc xwvxn77XV56yn1U0C1dc8ujG/JDgFtBNYdSXI8qeAvZtefc0DFnUdZxButfJfK1pLI 2xnIJNFTHC5pILkom3WcpmMqZfF8Nou7XU6GMr8A= Date: Fri, 4 Dec 2026 08:30:15 +0000 From: Yeoreum Yun To: "Liam R. Howlett" Cc: Yeoreum Yun , Jason Gunthorpe , John Hubbard , Peter Xu , linux-mm@kvack.org, Jan Kara , Josef Bacik , Johannes Weiner , Lorenzo Stoakes , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Jann Horn , Pedro Falcato , David Hildenbrand , Andrew Morton , Harrison Crosse Subject: Re: [PATCH 1/3] mm: gup: handle fault retries in __mm_populate() Message-ID: References: <20261203-infinite_mm_populate-v1-0-da5f1bccb3a2@arm.com> <20261203-infinite_mm_populate-v1-1-da5f1bccb3a2@arm.com> <5tquqhc2n25vmdevfqsq76ws6r2cb4ifp2b3pz4fs6mrhjgio4@uktohq7tjr56> X-Archived-By: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <5tquqhc2n25vmdevfqsq76ws6r2cb4ifp2b3pz4fs6mrhjgio4@uktohq7tjr56> X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 8230E18000D X-Rspam-User: X-Stat-Signature: 9mjrwf93q14bo6ro615u5cy7twbpdmhi X-Spam-Status: No, score=-16.0 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS autolearn=ham autolearn_force=no version=4.0.1 X-HE-Tag: 1796373022-516124 X-HE-Meta: U2FsdGVkX1/FizMZUdYoKQcYA8ighmCJCEwR+ID1eKjhuGFJvWRovab0DEP0AyuJL6ZQEhlMIdagy/5V3hyhd3ck3y/Zx37NWUTzjXfXF237xetKbzU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uHS05vxrw2T6hL8WJ1Mu8nHFYEvl78/zT0nFHRjbJb0brjuz9w+mUCu+meahSumHZcYCZM2N/hBZxhQuMOC4heSYs5xHny4a+0fTdDYfuaOFBN5mX2Sf3XxGxmeuGM3dUMMGDzCmvUWiAIcN0U5eLrckYajoeDUCiAu+pYO1E9LgMUCX7hI2SiAo5kxwVloPU7eU01iWClWva7G/01EfrosQyYkgLLllc5J5vbwVPeSXDn6fiavExiqY0HijDDYZI/JwV8Pl/k41WnoUeilmnPYffQWvkKcNxICaitW6quX2klEUj63hOXfJ5erbKSYatB1US42SULz1DZqoQwPH4XcJ5Y4w6 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 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 > > You are missing Harrison's signed-off-by here. Yeap. Will do. > > > Signed-off-by: Yeoreum Yun > > --- > > 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