Skip to contentSkip to conversation

mm/gup: honour FOLL_PIN in NOMMU __get_user_pages_locked()

PATCH In review Greg Kroah-Hartman 3 participants 12 messages Apr 23 – May 31

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

PATCH v2 1/1 mm/gup: honour FOLL_PIN in NOMMU __get_user_pages_locked()

The !CONFIG_MMU implementation of __get_user_pages_locked() takes a bare get_page() reference for each page regardless of foll_flags:

	if (pages[i])
		get_page(pages[i]);

This is reached from pin_user_pages*() with FOLL_PIN set. unpin_user_page() is shared between MMU and NOMMU configurations and unconditionally calls gup_put_folio(..., FOLL_PIN), which subtracts GUP_PIN_COUNTING_BIAS (1024) from the folio refcount.

This means that pin adds 1, and then unpin will subtract 1024.

If a user maps a page (refcount 1), registers it 1023 times as an io_uring fixed buffer (1023 pin_user_pages calls -> refcount 1024), then
unregisters: the first unpin_user_page subtracts 1024, refcount hits 0, the page is freed and returned to the buddy allocator. The remaining 1022 unpins write into whatever was reallocated, and the user's VMA still maps the freed page (NOMMU has no MMU to invalidate it). Reallocating the page for an io_uring pbuf_ring then lets userspace corrupt the new owner's data through the stale mapping.

Use try_grab_folio() which adds GUP_PIN_COUNTING_BIAS for FOLL_PIN and 1 for FOLL_GET, mirroring the CONFIG_MMU path so pin and unpin are symmetric.

Cc Andrew Morton <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc John Hubbard <[email protected]>
Cc Peter Xu <[email protected]>
Reported-by Anthropic
Assisted-by gkh_clanker_t1000

---
v2: - drop huge comment
- rework error return value based on David's suggestion (heck, pretty much the full patch was written by him now)
Link to v1: https://lore.kernel.org/r/2026042334-acutely-unadorned-e05c@gregkh

 mm/gup.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
mm/gup.c +10 -3
index ad9ded39609c..2f6f95a167af 100644
@@ -1983,6 +1983,7 @@ static long __get_user_pages_locked(struct mm_struct *mm, unsigned long start,
 	struct vm_area_struct *vma;
 	bool must_unlock = false;
 	vm_flags_t vm_flags;
+	int ret, err = -EFAULT;
 	long i;
 
 	if (!nr_pages)
@@ -2019,8 +2020,14 @@ static long __get_user_pages_locked(struct mm_struct *mm, unsigned long start,
 
 		if (pages) {
 			pages[i] = virt_to_page((void *)start);
-			if (pages[i])
-				get_page(pages[i]);
+			if (!pages[i])
+				break;
+			ret = try_grab_folio(page_folio(pages[i]), 1, foll_flags);
+			if (unlikely(ret)) {
+				pages[i] = NULL;
+				err = ret;
+				break;
+			}
 		}
 
 		start = (start + PAGE_SIZE) & PAGE_MASK;
@@ -2031,7 +2038,7 @@ static long __get_user_pages_locked(struct mm_struct *mm, unsigned long start,
 		*locked = 0;
 	}
 
-	return i ? : -EFAULT;
+	return i ? : err;
 }
 #endif /* !CONFIG_MMU */
 
Signature · 1 line
2.54.0
maintainer Acked-by on v2 1/1
#1 lore raw
Quoted 25 lines · Greg Kroah-Hartman

The !CONFIG_MMU implementation of __get_user_pages_locked() takes a bare get_page() reference for each page regardless of foll_flags:

	if (pages[i])
		get_page(pages[i]);

This is reached from pin_user_pages*() with FOLL_PIN set. unpin_user_page() is shared between MMU and NOMMU configurations and unconditionally calls gup_put_folio(..., FOLL_PIN), which subtracts GUP_PIN_COUNTING_BIAS (1024) from the folio refcount.

This means that pin adds 1, and then unpin will subtract 1024.

If a user maps a page (refcount 1), registers it 1023 times as an io_uring fixed buffer (1023 pin_user_pages calls -> refcount 1024), then
unregisters: the first unpin_user_page subtracts 1024, refcount hits 0, the page is freed and returned to the buddy allocator. The remaining 1022 unpins write into whatever was reallocated, and the user's VMA still maps the freed page (NOMMU has no MMU to invalidate it). Reallocating the page for an io_uring pbuf_ring then lets userspace corrupt the new owner's data through the stale mapping.

Use try_grab_folio() which adds GUP_PIN_COUNTING_BIAS for FOLL_PIN and 1 for FOLL_GET, mirroring the CONFIG_MMU path so pin and unpin are symmetric.

Quoted 7 lines
Cc Andrew Morton <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc John Hubbard <[email protected]>
Cc Peter Xu <[email protected]>
Reported-by Anthropic
Assisted-by gkh_clanker_t1000
Assisted-by David :(

(no, I'm not a tool! :) )

Quoted 23 lines

---
v2: - drop huge comment
- rework error return value based on David's suggestion (heck, pretty much the full patch was written by him now)
Link to v1: https://lore.kernel.org/r/2026042334-acutely-unadorned-e05c@gregkh

 mm/gup.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
mm/gup.c +1 -0
index ad9ded39609c..2f6f95a167af 100644
@@ -1983,6 +1983,7 @@ static long __get_user_pages_locked(struct mm_struct *mm, unsigned long start,
 	struct vm_area_struct *vma;
 	bool must_unlock = false;
 	vm_flags_t vm_flags;
+	int ret, err = -EFAULT;
 	long i;
 
 	if (!nr_pages)

mm/gup.c static long __get_user_pages_locked(struct mm_struct *mm, unsigned long start, · line 2024

Quoted 7 lines
mm/gup.c +2 -2
@@ -2019,8 +2020,14 @@ static long __get_user_pages_locked(struct mm_struct *mm, unsigned long start,
 
 		if (pages) {
 			pages[i] = virt_to_page((void *)start);
-			if (pages[i])
-				get_page(pages[i]);
+			if (!pages[i])
+				break;

Best to mention that change in the patch description. I really think this is the right thing to do (returning NULL in the page array is just very dubious).

Signature · 3 lines
Cheers,

David
#2 lore raw
Quoted 34 lines · David Hildenbrand (Arm)
Quoted 25 lines · Greg Kroah-Hartman

The !CONFIG_MMU implementation of __get_user_pages_locked() takes a bare get_page() reference for each page regardless of foll_flags:

	if (pages[i])
		get_page(pages[i]);

This is reached from pin_user_pages*() with FOLL_PIN set. unpin_user_page() is shared between MMU and NOMMU configurations and unconditionally calls gup_put_folio(..., FOLL_PIN), which subtracts GUP_PIN_COUNTING_BIAS (1024) from the folio refcount.

This means that pin adds 1, and then unpin will subtract 1024.

If a user maps a page (refcount 1), registers it 1023 times as an io_uring fixed buffer (1023 pin_user_pages calls -> refcount 1024), then
unregisters: the first unpin_user_page subtracts 1024, refcount hits 0, the page is freed and returned to the buddy allocator. The remaining 1022 unpins write into whatever was reallocated, and the user's VMA still maps the freed page (NOMMU has no MMU to invalidate it). Reallocating the page for an io_uring pbuf_ring then lets userspace corrupt the new owner's data through the stale mapping.

Use try_grab_folio() which adds GUP_PIN_COUNTING_BIAS for FOLL_PIN and 1 for FOLL_GET, mirroring the CONFIG_MMU path so pin and unpin are symmetric.

Quoted 7 lines
Cc Andrew Morton <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc John Hubbard <[email protected]>
Cc Peter Xu <[email protected]>
Reported-by Anthropic
Assisted-by gkh_clanker_t1000
Quoted 3 lines
Assisted-by David :(

(no, I'm not a tool! :) )

True, sorry, I guess people can "assist", I should have added that.

If Andrew's tools automatically pick this up then:

Assisted-by David Hildenbrand <[email protected]>
Quoted 24 lines
Quoted 24 lines

---
v2: - drop huge comment
- rework error return value based on David's suggestion (heck, pretty much the full patch was written by him now)
Link to v1: https://lore.kernel.org/r/2026042334-acutely-unadorned-e05c@gregkh

 mm/gup.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
mm/gup.c +1 -0
index ad9ded39609c..2f6f95a167af 100644
@@ -1983,6 +1983,7 @@ static long __get_user_pages_locked(struct mm_struct *mm, unsigned long start,
 	struct vm_area_struct *vma;
 	bool must_unlock = false;
 	vm_flags_t vm_flags;
+	int ret, err = -EFAULT;
 	long i;
 
 	if (!nr_pages)
@@ -2019,8 +2020,14 @@ static long __get_user_pages_locked(struct mm_struct *mm, unsigned long start,
 
Quoted 9 lines

mm/gup.c static long __get_user_pages_locked(struct mm_struct *mm, unsigned long start, · line 2024

Quoted 6 lines
mm/gup.c +2 -2
 		if (pages) {
 			pages[i] = virt_to_page((void *)start);
-			if (pages[i])
-				get_page(pages[i]);
+			if (!pages[i])
+				break;

Best to mention that change in the patch description. I really think this is the right thing to do (returning NULL in the page array is just very dubious).

Ick, I see Andrew already grabbed this so I'll just leave it for now, thanks for the help and review!

greg k-h

maintainer
#3 lore raw
Quoted 12 lines · On Fri, 24 Apr 2026 13:31:34 +0200 Greg Kroah-Hartman…
Quoted 11 lines

mm/gup.c static long __get_user_pages_locked(struct mm_struct *mm, unsigned long start, · line 2024

Quoted 8 lines
mm/gup.c +2 -2
@@ -2019,8 +2020,14 @@ static long __get_user_pages_locked(struct mm_struct *mm, unsigned long start,
 
 		if (pages) {
 			pages[i] = virt_to_page((void *)start);
-			if (pages[i])
-				get_page(pages[i]);
+			if (!pages[i])
+				break;

Best to mention that change in the patch description. I really think this is the right thing to do (returning NULL in the page array is just very dubious).

Quoted 2 lines

Ick, I see Andrew already grabbed this so I'll just leave it for now, thanks for the help and review!

Andrew does copy-n-paste ;)

#4 lore raw
In reply to Greg Kroah-Hartman
Quoted 37 lines · Greg Kroah-Hartman
Quoted 36 lines · David Hildenbrand (Arm)
Quoted 25 lines · Greg Kroah-Hartman

The !CONFIG_MMU implementation of __get_user_pages_locked() takes a bare get_page() reference for each page regardless of foll_flags:

	if (pages[i])
		get_page(pages[i]);

This is reached from pin_user_pages*() with FOLL_PIN set. unpin_user_page() is shared between MMU and NOMMU configurations and unconditionally calls gup_put_folio(..., FOLL_PIN), which subtracts GUP_PIN_COUNTING_BIAS (1024) from the folio refcount.

This means that pin adds 1, and then unpin will subtract 1024.

If a user maps a page (refcount 1), registers it 1023 times as an io_uring fixed buffer (1023 pin_user_pages calls -> refcount 1024), then
unregisters: the first unpin_user_page subtracts 1024, refcount hits 0, the page is freed and returned to the buddy allocator. The remaining 1022 unpins write into whatever was reallocated, and the user's VMA still maps the freed page (NOMMU has no MMU to invalidate it). Reallocating the page for an io_uring pbuf_ring then lets userspace corrupt the new owner's data through the stale mapping.

Use try_grab_folio() which adds GUP_PIN_COUNTING_BIAS for FOLL_PIN and 1 for FOLL_GET, mirroring the CONFIG_MMU path so pin and unpin are symmetric.

Quoted 7 lines
Cc Andrew Morton <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc John Hubbard <[email protected]>
Cc Peter Xu <[email protected]>
Reported-by Anthropic
Assisted-by gkh_clanker_t1000
Assisted-by David :(
Quoted 7 lines
Quoted 1 line

(no, I'm not a tool! :) )

True, sorry, I guess people can "assist", I should have added that.

If Andrew's tools automatically pick this up then:

Assisted-by David Hildenbrand <[email protected]>

I think we usually use Suggested-by:, but really I was just joking :)

Quoted 35 lines
Quoted 25 lines
Quoted 25 lines

---
v2: - drop huge comment
- rework error return value based on David's suggestion (heck, pretty much the full patch was written by him now)
Link to v1: https://lore.kernel.org/r/2026042334-acutely-unadorned-e05c@gregkh

 mm/gup.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
mm/gup.c +1 -0
index ad9ded39609c..2f6f95a167af 100644
@@ -1983,6 +1983,7 @@ static long __get_user_pages_locked(struct mm_struct *mm, unsigned long start,
 	struct vm_area_struct *vma;
 	bool must_unlock = false;
 	vm_flags_t vm_flags;
+	int ret, err = -EFAULT;
 	long i;
 
 	if (!nr_pages)
@@ -2019,8 +2020,14 @@ static long __get_user_pages_locked(struct mm_struct *mm, unsigned long start,
 
Quoted 9 lines

mm/gup.c static long __get_user_pages_locked(struct mm_struct *mm, unsigned long start, · line 2024

Quoted 6 lines
mm/gup.c +2 -2
 		if (pages) {
 			pages[i] = virt_to_page((void *)start);
-			if (pages[i])
-				get_page(pages[i]);
+			if (!pages[i])
+				break;

Best to mention that change in the patch description. I really think this is the right thing to do (returning NULL in the page array is just very dubious).

Quoted 2 lines

Ick, I see Andrew already grabbed this so I'll just leave it for now, thanks for the help and review!

Andrew, can you add "While at it, don't return NULL pointers in the page array, as this is really not expected for GUP users; instead, just fail and return -EFAULT."? Thanks!

Signature · 3 lines
Cheers,

David
maintainer
#5 lore raw
Quoted 11 lines · On Fri, 24 Apr 2026 14:16:21 +0200 "David Hildenbrand (Arm)"…
Quoted 11 lines
Quoted 10 lines
Quoted 6 lines
Cc David Hildenbrand <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc John Hubbard <[email protected]>
Cc Peter Xu <[email protected]>
Reported-by Anthropic
Assisted-by gkh_clanker_t1000
Assisted-by David :(

(no, I'm not a tool! :) )

Quoted 7 lines
Quoted 5 lines

True, sorry, I guess people can "assist", I should have added that.

If Andrew's tools automatically pick this up then:

Assisted-by David Hildenbrand <[email protected]>

I think we usually use Suggested-by:, but really I was just joking :)

I thought you were gkh_clanker_t1000

Quoted 3 lines

Andrew, can you add "While at it, don't return NULL pointers in the page array, as this is really not expected for GUP users; instead, just fail and return -EFAULT."? Thanks!

Did.

#6 lore raw
Quoted 5 lines · Andrew Morton
Quoted 3 lines · On Fri, 24 Apr 2026 14:16:21 +0200 "David Hildenbrand (Arm)"…
Quoted 3 lines

True, sorry, I guess people can "assist", I should have added that.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

If Andrew's tools automatically pick this up then:

Assisted-by David Hildenbrand <[email protected]>

I think we usually use Suggested-by:, but really I was just joking :)

I thought you were gkh_clanker_t1000

That was supposed to be our tiny little secret. ;)

Signature · 3 lines
Cheers,

David
maintainer
#7 lore raw
In reply to Greg Kroah-Hartman
Quoted 21 lines · On Thu, 23 Apr 2026 16:28:04 +0200 Greg Kroah-Hartman…

The !CONFIG_MMU implementation of __get_user_pages_locked() takes a bare get_page() reference for each page regardless of foll_flags:

	if (pages[i])
		get_page(pages[i]);

This is reached from pin_user_pages*() with FOLL_PIN set. unpin_user_page() is shared between MMU and NOMMU configurations and unconditionally calls gup_put_folio(..., FOLL_PIN), which subtracts GUP_PIN_COUNTING_BIAS (1024) from the folio refcount.

This means that pin adds 1, and then unpin will subtract 1024.

If a user maps a page (refcount 1), registers it 1023 times as an io_uring fixed buffer (1023 pin_user_pages calls -> refcount 1024), then
unregisters: the first unpin_user_page subtracts 1024, refcount hits 0, the page is freed and returned to the buddy allocator. The remaining 1022 unpins write into whatever was reallocated, and the user's VMA still maps the freed page (NOMMU has no MMU to invalidate it). Reallocating the page for an io_uring pbuf_ring then lets userspace corrupt the new owner's data through the stale mapping.

Quoted 3 lines

Use try_grab_folio() which adds GUP_PIN_COUNTING_BIAS for FOLL_PIN and 1 for FOLL_GET, mirroring the CONFIG_MMU path so pin and unpin are symmetric.

Battle of the bots?

	https://sashiko.dev/#/patchset/2026042303-vendor-outright-b9d2@gregkh
#8 lore raw
Quoted 23 lines · Andrew Morton
Quoted 21 lines · On Thu, 23 Apr 2026 16:28:04 +0200 Greg Kroah-Hartman…

The !CONFIG_MMU implementation of __get_user_pages_locked() takes a bare get_page() reference for each page regardless of foll_flags:

	if (pages[i])
		get_page(pages[i]);

This is reached from pin_user_pages*() with FOLL_PIN set. unpin_user_page() is shared between MMU and NOMMU configurations and unconditionally calls gup_put_folio(..., FOLL_PIN), which subtracts GUP_PIN_COUNTING_BIAS (1024) from the folio refcount.

This means that pin adds 1, and then unpin will subtract 1024.

If a user maps a page (refcount 1), registers it 1023 times as an io_uring fixed buffer (1023 pin_user_pages calls -> refcount 1024), then
unregisters: the first unpin_user_page subtracts 1024, refcount hits 0, the page is freed and returned to the buddy allocator. The remaining 1022 unpins write into whatever was reallocated, and the user's VMA still maps the freed page (NOMMU has no MMU to invalidate it). Reallocating the page for an io_uring pbuf_ring then lets userspace corrupt the new owner's data through the stale mapping.

Quoted 6 lines
Quoted 3 lines

Use try_grab_folio() which adds GUP_PIN_COUNTING_BIAS for FOLL_PIN and 1 for FOLL_GET, mirroring the CONFIG_MMU path so pin and unpin are symmetric.

Battle of the bots?

	https://sashiko.dev/#/patchset/2026042303-vendor-outright-b9d2@gregkh

Odd, I really don't know the answer to that. I can provide my reproducer if anyone wants to tell me this patch is wrong.

thanks,

greg k-h

#9 lore raw
In reply to Andrew Morton
Quoted 23 lines · Andrew Morton
Quoted 21 lines · On Thu, 23 Apr 2026 16:28:04 +0200 Greg Kroah-Hartman…

The !CONFIG_MMU implementation of __get_user_pages_locked() takes a bare get_page() reference for each page regardless of foll_flags:

	if (pages[i])
		get_page(pages[i]);

This is reached from pin_user_pages*() with FOLL_PIN set. unpin_user_page() is shared between MMU and NOMMU configurations and unconditionally calls gup_put_folio(..., FOLL_PIN), which subtracts GUP_PIN_COUNTING_BIAS (1024) from the folio refcount.

This means that pin adds 1, and then unpin will subtract 1024.

If a user maps a page (refcount 1), registers it 1023 times as an io_uring fixed buffer (1023 pin_user_pages calls -> refcount 1024), then
unregisters: the first unpin_user_page subtracts 1024, refcount hits 0, the page is freed and returned to the buddy allocator. The remaining 1022 unpins write into whatever was reallocated, and the user's VMA still maps the freed page (NOMMU has no MMU to invalidate it). Reallocating the page for an io_uring pbuf_ring then lets userspace corrupt the new owner's data through the stale mapping.

Quoted 6 lines
Quoted 3 lines

Use try_grab_folio() which adds GUP_PIN_COUNTING_BIAS for FOLL_PIN and 1 for FOLL_GET, mirroring the CONFIG_MMU path so pin and unpin are symmetric.

Battle of the bots?

	https://sashiko.dev/#/patchset/2026042303-vendor-outright-b9d2@gregkh

It references the

	if (pages && !(flags & FOLL_PIN))
		flags |= FOLL_GET;

I'm not sure if there is actual NOMMU code that triggers it. For example, uprobes uses that pattern, but I suspect that that's not a thing on NOMMU.

Probably best to just squash:

mm/gup.c +0 -0
index ad9ded39609c..44bd28cf6e00 100644
@@ -1988,6 +1988,9 @@ static long __get_user_pages_locked(struct mm_struct *mm,

unsigned long start,

        if (!nr_pages)
                return 0;

+ if (pages && !(foll_flags & FOLL_PIN))
+ foll_flags |= FOLL_GET;
+
/*
* The internal caller expects GUP to manage the lock internally and the
* lock must be released when this returns.

Signature · 3 lines
Cheers,

David
maintainer Acked-by David Hildenbrand (Arm)
#10 lore raw
Quoted 56 lines · On Fri, 24 Apr 2026 16:19:32 +0200 "David Hildenbrand (Arm)"…
Quoted 23 lines · Andrew Morton
Quoted 21 lines · On Thu, 23 Apr 2026 16:28:04 +0200 Greg Kroah-Hartman…

The !CONFIG_MMU implementation of __get_user_pages_locked() takes a bare get_page() reference for each page regardless of foll_flags:

	if (pages[i])
		get_page(pages[i]);

This is reached from pin_user_pages*() with FOLL_PIN set. unpin_user_page() is shared between MMU and NOMMU configurations and unconditionally calls gup_put_folio(..., FOLL_PIN), which subtracts GUP_PIN_COUNTING_BIAS (1024) from the folio refcount.

This means that pin adds 1, and then unpin will subtract 1024.

If a user maps a page (refcount 1), registers it 1023 times as an io_uring fixed buffer (1023 pin_user_pages calls -> refcount 1024), then
unregisters: the first unpin_user_page subtracts 1024, refcount hits 0, the page is freed and returned to the buddy allocator. The remaining 1022 unpins write into whatever was reallocated, and the user's VMA still maps the freed page (NOMMU has no MMU to invalidate it). Reallocating the page for an io_uring pbuf_ring then lets userspace corrupt the new owner's data through the stale mapping.

Quoted 6 lines
Quoted 3 lines

Use try_grab_folio() which adds GUP_PIN_COUNTING_BIAS for FOLL_PIN and 1 for FOLL_GET, mirroring the CONFIG_MMU path so pin and unpin are symmetric.

Battle of the bots?

	https://sashiko.dev/#/patchset/2026042303-vendor-outright-b9d2@gregkh

It references the

	if (pages && !(flags & FOLL_PIN))
		flags |= FOLL_GET;

I'm not sure if there is actual NOMMU code that triggers it. For example, uprobes uses that pattern, but I suspect that that's not a thing on NOMMU.

Probably best to just squash:

mm/gup.c +3 -0
index ad9ded39609c..44bd28cf6e00 100644
@@ -1988,6 +1988,9 @@ static long __get_user_pages_locked(struct mm_struct *mm,
 unsigned long start,
        if (!nr_pages)
                return 0;
 
+       if (pages && !(foll_flags & FOLL_PIN))
+               foll_flags |= FOLL_GET;
+
        /*
         * The internal caller expects GUP to manage the lock internally and the
Quoted 1 line

* lock must be released when this returns.

Nothing happened.

Should we proceed with this as-is?

From: Greg Kroah-Hartman <[email protected]>
Subject: mm/gup: honour FOLL_PIN in NOMMU __get_user_pages_locked()
Date: Thu, 23 Apr 2026 16:28:04 +0200

The !CONFIG_MMU implementation of __get_user_pages_locked() takes a bare get_page() reference for each page regardless of foll_flags:

	if (pages[i])
		get_page(pages[i]);

This is reached from pin_user_pages*() with FOLL_PIN set. unpin_user_page() is shared between MMU and NOMMU configurations and unconditionally calls gup_put_folio(..., FOLL_PIN), which subtracts GUP_PIN_COUNTING_BIAS (1024) from the folio refcount.

This means that pin adds 1, and then unpin will subtract 1024.

If a user maps a page (refcount 1), registers it 1023 times as an io_uring fixed buffer (1023 pin_user_pages calls -> refcount 1024), then
unregisters: the first unpin_user_page subtracts 1024, refcount hits 0, the page is freed and returned to the buddy allocator. The remaining 1022 unpins write into whatever was reallocated, and the user's VMA still maps the freed page (NOMMU has no MMU to invalidate it). Reallocating the page for an io_uring pbuf_ring then lets userspace corrupt the new owner's data through the stale mapping.

Use try_grab_folio() which adds GUP_PIN_COUNTING_BIAS for FOLL_PIN and 1 for FOLL_GET, mirroring the CONFIG_MMU path so pin and unpin are symmetric.

While at it, don't return NULL pointers in the page array, as this is really not expected for GUP users; instead, just fail and return -EFAULT.

[[email protected]: changelog update]
https://lore.kernel.org/[email protected]

---

 mm/gup.c |   13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
mm/gup.c renamed from mm/gup.c~mm-gup-honour-foll_pin-in-nommu-__get_user_pages_locked +10 -3
@@ -1983,6 +1983,7 @@ static long __get_user_pages_locked(stru
 	struct vm_area_struct *vma;
 	bool must_unlock = false;
 	vm_flags_t vm_flags;
+	int ret, err = -EFAULT;
 	long i;
 
 	if (!nr_pages)
@@ -2019,8 +2020,14 @@ static long __get_user_pages_locked(stru
 
 		if (pages) {
 			pages[i] = virt_to_page((void *)start);
-			if (pages[i])
-				get_page(pages[i]);
+			if (!pages[i])
+				break;
+			ret = try_grab_folio(page_folio(pages[i]), 1, foll_flags);
+			if (unlikely(ret)) {
+				pages[i] = NULL;
+				err = ret;
+				break;
+			}
 		}
 
 		start = (start + PAGE_SIZE) & PAGE_MASK;
@@ -2031,7 +2038,7 @@ static long __get_user_pages_locked(stru
 		*locked = 0;
 	}
 
-	return i ? : -EFAULT;
+	return i ? : err;
 }
 #endif /* !CONFIG_MMU */
 

_

#11 lore raw
Quoted 37 lines · Andrew Morton
Quoted 31 lines · On Fri, 24 Apr 2026 16:19:32 +0200 "David Hildenbrand (Arm)"…
Quoted 4 lines · Andrew Morton

Battle of the bots?

	https://sashiko.dev/#/patchset/2026042303-vendor-outright-b9d2@gregkh

It references the

	if (pages && !(flags & FOLL_PIN))
		flags |= FOLL_GET;

I'm not sure if there is actual NOMMU code that triggers it. For example, uprobes uses that pattern, but I suspect that that's not a thing on NOMMU.

Probably best to just squash:

mm/gup.c +3 -0
index ad9ded39609c..44bd28cf6e00 100644
@@ -1988,6 +1988,9 @@ static long __get_user_pages_locked(struct mm_struct *mm,
 unsigned long start,
        if (!nr_pages)
                return 0;
 
+       if (pages && !(foll_flags & FOLL_PIN))
+               foll_flags |= FOLL_GET;
+
        /*
         * The internal caller expects GUP to manage the lock internally and the
Quoted 1 line

* lock must be released when this returns.

Nothing happened.

Quoted 1 line

Should we proceed with this as-is?

No let's squash what I provided. Probably a resend would be best. If greg is too busy I can give it a try tomorrow.

Signature · 3 lines
Cheers,

David