Skip to contentSkip to conversation

mm/gup: fix handling of errors from arch_make_folio_accessible() in follow_page_pte()

PATCH Applied David Hildenbrand 2 participants 4 messages Sep 8, 2025 – Sep 10, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/gup: fix handling of errors from arch_make_folio_accessible() in follow_page_pte()

In case we call arch_make_folio_accessible() and it fails, we would incorrectly return a value that is "!= 0" to the caller, indicating that we pinned all requested pages and that the caller can keep going.

follow_page_pte() is not supposed to return error values, but instead "0" on failure and "1" on success -- we'll clean that up separately.

In case we return "!= 0", the caller will just keep going pinning more pages. If we happen to pin a page afterwards, we're in trouble, because we essentially skipped some pages in the requested range.

Staring at the arch_make_folio_accessible() implementation on s390x, I assume it should actually never really fail unless something unexpected happens (BUG?). So let's not CC stable and just fix common code to do the right thing.

Clean up the code a bit now that there is no reason to store the return value of arch_make_folio_accessible().

Fixes f28d43636d6f ("mm/gup/writeback: add callbacks for inaccessible pages")
Cc Andrew Morton <[email protected]>
Cc Claudio Imbrenda <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc John Hubbard <[email protected]>
Cc Peter Xu <[email protected]>
Cc Christian Borntraeger <[email protected]>
---
 mm/gup.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
mm/gup.c +3 -6
index d27b6b9818a18..c969259d095c9 100644
@@ -2920,12 +2920,9 @@ static int gup_fast_pte_range(pmd_t pmd, pmd_t *pmdp, unsigned long addr,
 		 * see Documentation/core-api/pin_user_pages.rst for
 		 * details.
 		 */
-		if (flags & FOLL_PIN) {
-			ret = arch_make_folio_accessible(folio);
-			if (ret) {
-				gup_put_folio(folio, 1, flags);
-				goto pte_unmap;
-			}
+		if ((flags & FOLL_PIN) && arch_make_folio_accessible(folio)) {
+			gup_put_folio(folio, 1, flags);
+			goto pte_unmap;
 		}
 		folio_set_referenced(folio);
 		pages[*nr] = page;
Signature · 1 line
2.50.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 16 lines · On Mon, 8 Sep 2025 11:45:17 +0200 David Hildenbrand <[email protected]> wrote:

In case we call arch_make_folio_accessible() and it fails, we would incorrectly return a value that is "!= 0" to the caller, indicating that we pinned all requested pages and that the caller can keep going.

follow_page_pte() is not supposed to return error values, but instead "0" on failure and "1" on success -- we'll clean that up separately.

In case we return "!= 0", the caller will just keep going pinning more pages. If we happen to pin a page afterwards, we're in trouble, because we essentially skipped some pages in the requested range.

Staring at the arch_make_folio_accessible() implementation on s390x, I assume it should actually never really fail unless something unexpected happens (BUG?). So let's not CC stable and just fix common code to do the right thing.

Quoted 4 lines

Clean up the code a bit now that there is no reason to store the return value of arch_make_folio_accessible().

Fixes f28d43636d6f ("mm/gup/writeback: add callbacks for inaccessible pages")

Ooops!

thanks for finding and fixing this

Quoted 31 lines
Cc Andrew Morton <[email protected]>
Cc Claudio Imbrenda <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc John Hubbard <[email protected]>
Cc Peter Xu <[email protected]>
Cc Christian Borntraeger <[email protected]>
---
 mm/gup.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
mm/gup.c +3 -6
index d27b6b9818a18..c969259d095c9 100644
@@ -2920,12 +2920,9 @@ static int gup_fast_pte_range(pmd_t pmd, pmd_t *pmdp, unsigned long addr,
 		 * see Documentation/core-api/pin_user_pages.rst for
 		 * details.
 		 */
-		if (flags & FOLL_PIN) {
-			ret = arch_make_folio_accessible(folio);
-			if (ret) {
-				gup_put_folio(folio, 1, flags);
-				goto pte_unmap;
-			}
+		if ((flags & FOLL_PIN) && arch_make_folio_accessible(folio)) {
+			gup_put_folio(folio, 1, flags);
+			goto pte_unmap;
 		}
 		folio_set_referenced(folio);
 		pages[*nr] = page;
#2 lore raw
Quoted 19 lines · Claudio Imbrenda
Quoted 16 lines · On Mon, 8 Sep 2025 11:45:17 +0200 David Hildenbrand <[email protected]> wrote:

In case we call arch_make_folio_accessible() and it fails, we would incorrectly return a value that is "!= 0" to the caller, indicating that we pinned all requested pages and that the caller can keep going.

follow_page_pte() is not supposed to return error values, but instead "0" on failure and "1" on success -- we'll clean that up separately.

In case we return "!= 0", the caller will just keep going pinning more pages. If we happen to pin a page afterwards, we're in trouble, because we essentially skipped some pages in the requested range.

Staring at the arch_make_folio_accessible() implementation on s390x, I assume it should actually never really fail unless something unexpected happens (BUG?). So let's not CC stable and just fix common code to do the right thing.

Quoted 8 lines
Quoted 4 lines

Clean up the code a bit now that there is no reason to store the return value of arch_make_folio_accessible().

Fixes f28d43636d6f ("mm/gup/writeback: add callbacks for inaccessible pages")

Ooops!

thanks for finding and fixing this

Thanks! Is my assumption correct that this is not stable material?

Signature · 3 lines
Cheers

David / dhildenb
#3 lore raw
Quoted 23 lines · On Tue, 9 Sep 2025 18:15:17 +0200 David Hildenbrand <[email protected]> wrote:
Quoted 22 lines · Claudio Imbrenda
Quoted 19 lines · On Mon, 8 Sep 2025 11:45:17 +0200 David Hildenbrand <[email protected]> wrote:

In case we call arch_make_folio_accessible() and it fails, we would incorrectly return a value that is "!= 0" to the caller, indicating that we pinned all requested pages and that the caller can keep going.

follow_page_pte() is not supposed to return error values, but instead "0" on failure and "1" on success -- we'll clean that up separately.

In case we return "!= 0", the caller will just keep going pinning more pages. If we happen to pin a page afterwards, we're in trouble, because we essentially skipped some pages in the requested range.

Staring at the arch_make_folio_accessible() implementation on s390x, I assume it should actually never really fail unless something unexpected happens (BUG?). So let's not CC stable and just fix common code to do the right thing.

Clean up the code a bit now that there is no reason to store the return value of arch_make_folio_accessible().

Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Fixes f28d43636d6f ("mm/gup/writeback: add callbacks for inaccessible pages")

Ooops!

thanks for finding and fixing this

Thanks! Is my assumption correct that this is not stable material?

your assessment looks correct, an error return value can only be caused by a bug