* [PATCH v4] mm/gup_test: safely calculate GUP batch size
@ 2026-09-03 11:50 Sarthak Sharma
2026-09-03 12:39 ` Kiryl Shutsemau
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: Sarthak Sharma @ 2026-09-03 11:50 UTC (permalink / raw)
To: Andrew Morton, David Hildenbrand
Cc: Jason Gunthorpe, John Hubbard, Peter Xu, Kiryl Shutsemau,
linux-mm, linux-kernel, Sarthak Sharma
__gup_test_ioctl() calculates the end of a GUP batch using:
next = addr + nr * PAGE_SIZE;
If nr is too large, it can cause next to overflow and wrap around.
If it wraps, the next > end check is bypassed and a large value
of nr is passed to the GUP call, even though the pages array was
allocated according to gup->size. This can lead to out of bounds writes.
Also, when fewer than PAGE_SIZE bytes remain, the calculated batch
contains zero pages. The code still calls GUP functions with pages + i,
which can point past the allocated array.
Calculate nr by taking the minimum of the number of pages per call and
the pages remaining in the address range. Stop processing when no
pages remain and calculate the end of the batch using this bounded
value of nr.
Fixes: 64c349f4ae78 ("mm: add infrastructure for get_user_pages_fast() benchmarking")
Signed-off-by: Sarthak Sharma <sarthak.sharma@arm.com>
---
Sashiko reported the issue fixed by this patch while reviewing the patch
"mm/gup_test: report actual pinned bytes". That patch has been applied to
mm-new. Since the two fixes are independent, this revision contains
only the GUP batch size fix.
Changes in v4:
- Simplify batch clamping logic as suggested by Kiryl
- Don't call GUP functions for an empty batch
- Drop the pinned byte reporting patch since it has been applied to mm-new
v3: https://lore.kernel.org/all/20260901083452.115365-1-sarthak.sharma@arm.com/
mm/gup_test.c | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/mm/gup_test.c b/mm/gup_test.c
index 185ba3bb8ed1..1b64e932c4c5 100644
--- a/mm/gup_test.c
+++ b/mm/gup_test.c
@@ -139,11 +139,12 @@ static int __gup_test_ioctl(unsigned int cmd,
if (nr != gup->nr_pages_per_call)
break;
+ nr = min_t(unsigned long, gup->nr_pages_per_call,
+ (end - addr) / PAGE_SIZE);
+ if (!nr)
+ break;
+
next = addr + nr * PAGE_SIZE;
- if (next > end) {
- next = end;
- nr = (next - addr) / PAGE_SIZE;
- }
switch (cmd) {
case GUP_FAST_BENCHMARK:
base-commit: 178b3d97bf1f15f598ea7cc615a40c115e528e1c
--
2.53.0
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH v4] mm/gup_test: safely calculate GUP batch size 2026-09-03 11:50 [PATCH v4] mm/gup_test: safely calculate GUP batch size Sarthak Sharma @ 2026-09-03 12:39 ` Kiryl Shutsemau 2026-09-04 4:45 ` Sarthak Sharma 2026-09-07 14:50 ` David Hildenbrand (Arm) [not found] ` <cc0fdb0e-ab6a-4095-b559-10e1d8a616d9@kernel.org_quarantine> 2 siblings, 1 reply; 6+ messages in thread From: Kiryl Shutsemau @ 2026-09-03 12:39 UTC (permalink / raw) To: Sarthak Sharma Cc: Andrew Morton, David Hildenbrand, Jason Gunthorpe, John Hubbard, Peter Xu, linux-mm, linux-kernel On Thu, Sep 03, 2026 at 05:20:33PM +0530, Sarthak Sharma wrote: > __gup_test_ioctl() calculates the end of a GUP batch using: > > next = addr + nr * PAGE_SIZE; > > If nr is too large, it can cause next to overflow and wrap around. > If it wraps, the next > end check is bypassed and a large value > of nr is passed to the GUP call, even though the pages array was > allocated according to gup->size. This can lead to out of bounds writes. > > Also, when fewer than PAGE_SIZE bytes remain, the calculated batch > contains zero pages. The code still calls GUP functions with pages + i, > which can point past the allocated array. > > Calculate nr by taking the minimum of the number of pages per call and > the pages remaining in the address range. Stop processing when no > pages remain and calculate the end of the batch using this bounded > value of nr. > > Fixes: 64c349f4ae78 ("mm: add infrastructure for get_user_pages_fast() benchmarking") > Signed-off-by: Sarthak Sharma <sarthak.sharma@arm.com> Reviewed-by: Kiryl Shutsemau (Meta) <kas@kernel.org> On nit below. > --- > Sashiko reported the issue fixed by this patch while reviewing the patch > "mm/gup_test: report actual pinned bytes". That patch has been applied to > mm-new. Since the two fixes are independent, this revision contains > only the GUP batch size fix. > > Changes in v4: > - Simplify batch clamping logic as suggested by Kiryl > - Don't call GUP functions for an empty batch > - Drop the pinned byte reporting patch since it has been applied to mm-new > > v3: https://lore.kernel.org/all/20260901083452.115365-1-sarthak.sharma@arm.com/ > > mm/gup_test.c | 9 +++++---- > 1 file changed, 5 insertions(+), 4 deletions(-) > > diff --git a/mm/gup_test.c b/mm/gup_test.c > index 185ba3bb8ed1..1b64e932c4c5 100644 > --- a/mm/gup_test.c > +++ b/mm/gup_test.c > @@ -139,11 +139,12 @@ static int __gup_test_ioctl(unsigned int cmd, > if (nr != gup->nr_pages_per_call) > break; > > + nr = min_t(unsigned long, gup->nr_pages_per_call, > + (end - addr) / PAGE_SIZE); I personally would rather have it in two statements, as I suggested initially: nr = gup->nr_pages_per_call; nr = min_t(unsigned long, nr, (end - addr) / PAGE_SIZE); It seems to be more readable to me. > + if (!nr) > + break; > + > next = addr + nr * PAGE_SIZE; > - if (next > end) { > - next = end; > - nr = (next - addr) / PAGE_SIZE; > - } > > switch (cmd) { > case GUP_FAST_BENCHMARK: > > base-commit: 178b3d97bf1f15f598ea7cc615a40c115e528e1c > -- > 2.53.0 > -- Kiryl Shutsemau / Kirill A. Shutemov ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v4] mm/gup_test: safely calculate GUP batch size 2026-09-03 12:39 ` Kiryl Shutsemau @ 2026-09-04 4:45 ` Sarthak Sharma 0 siblings, 0 replies; 6+ messages in thread From: Sarthak Sharma @ 2026-09-04 4:45 UTC (permalink / raw) To: Kiryl Shutsemau Cc: Andrew Morton, David Hildenbrand, Jason Gunthorpe, John Hubbard, Peter Xu, linux-mm, linux-kernel Hi Kiryl! On 9/3/26 6:09 PM, Kiryl Shutsemau wrote: > On Thu, Sep 03, 2026 at 05:20:33PM +0530, Sarthak Sharma wrote: >> __gup_test_ioctl() calculates the end of a GUP batch using: >> >> next = addr + nr * PAGE_SIZE; >> >> If nr is too large, it can cause next to overflow and wrap around. >> If it wraps, the next > end check is bypassed and a large value >> of nr is passed to the GUP call, even though the pages array was >> allocated according to gup->size. This can lead to out of bounds writes. >> >> Also, when fewer than PAGE_SIZE bytes remain, the calculated batch >> contains zero pages. The code still calls GUP functions with pages + i, >> which can point past the allocated array. >> >> Calculate nr by taking the minimum of the number of pages per call and >> the pages remaining in the address range. Stop processing when no >> pages remain and calculate the end of the batch using this bounded >> value of nr. >> >> Fixes: 64c349f4ae78 ("mm: add infrastructure for get_user_pages_fast() benchmarking") >> Signed-off-by: Sarthak Sharma <sarthak.sharma@arm.com> > > Reviewed-by: Kiryl Shutsemau (Meta) <kas@kernel.org> Thank you! > > On nit below. > >> --- >> Sashiko reported the issue fixed by this patch while reviewing the patch >> "mm/gup_test: report actual pinned bytes". That patch has been applied to >> mm-new. Since the two fixes are independent, this revision contains >> only the GUP batch size fix. >> >> Changes in v4: >> - Simplify batch clamping logic as suggested by Kiryl >> - Don't call GUP functions for an empty batch >> - Drop the pinned byte reporting patch since it has been applied to mm-new >> >> v3: https://lore.kernel.org/all/20260901083452.115365-1-sarthak.sharma@arm.com/ >> >> mm/gup_test.c | 9 +++++---- >> 1 file changed, 5 insertions(+), 4 deletions(-) >> >> diff --git a/mm/gup_test.c b/mm/gup_test.c >> index 185ba3bb8ed1..1b64e932c4c5 100644 >> --- a/mm/gup_test.c >> +++ b/mm/gup_test.c >> @@ -139,11 +139,12 @@ static int __gup_test_ioctl(unsigned int cmd, >> if (nr != gup->nr_pages_per_call) >> break; >> >> + nr = min_t(unsigned long, gup->nr_pages_per_call, >> + (end - addr) / PAGE_SIZE); > > I personally would rather have it in two statements, as I suggested > initially: > > nr = gup->nr_pages_per_call; > nr = min_t(unsigned long, nr, (end - addr) / PAGE_SIZE); > > It seems to be more readable to me. Both forms are equivalent, and I would prefer to keep it in a single line instead of having two statements to do the same thing. I don't think I need to respin with this change unless you feel strongly about it :) > > >> + if (!nr) >> + break; >> + >> next = addr + nr * PAGE_SIZE; >> - if (next > end) { >> - next = end; >> - nr = (next - addr) / PAGE_SIZE; >> - } >> >> switch (cmd) { >> case GUP_FAST_BENCHMARK: >> >> base-commit: 178b3d97bf1f15f598ea7cc615a40c115e528e1c >> -- >> 2.53.0 >> > ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v4] mm/gup_test: safely calculate GUP batch size 2026-09-03 11:50 [PATCH v4] mm/gup_test: safely calculate GUP batch size Sarthak Sharma 2026-09-03 12:39 ` Kiryl Shutsemau @ 2026-09-07 14:50 ` David Hildenbrand (Arm) [not found] ` <cc0fdb0e-ab6a-4095-b559-10e1d8a616d9@kernel.org_quarantine> 2 siblings, 0 replies; 6+ messages in thread From: David Hildenbrand (Arm) @ 2026-09-07 14:50 UTC (permalink / raw) To: Sarthak Sharma, Andrew Morton Cc: Jason Gunthorpe, John Hubbard, Peter Xu, Kiryl Shutsemau, linux-mm, linux-kernel On 9/3/26 13:50, Sarthak Sharma wrote: > __gup_test_ioctl() calculates the end of a GUP batch using: > > next = addr + nr * PAGE_SIZE; > > If nr is too large, it can cause next to overflow and wrap around. > If it wraps, the next > end check is bypassed and a large value > of nr is passed to the GUP call, even though the pages array was > allocated according to gup->size. This can lead to out of bounds writes. > > Also, when fewer than PAGE_SIZE bytes remain, the calculated batch > contains zero pages. The code still calls GUP functions with pages + i, > which can point past the allocated array. > > Calculate nr by taking the minimum of the number of pages per call and > the pages remaining in the address range. Stop processing when no > pages remain and calculate the end of the batch using this bounded > value of nr. > > Fixes: 64c349f4ae78 ("mm: add infrastructure for get_user_pages_fast() benchmarking") > Signed-off-by: Sarthak Sharma <sarthak.sharma@arm.com> > --- > Sashiko reported the issue fixed by this patch while reviewing the patch > "mm/gup_test: report actual pinned bytes". That patch has been applied to > mm-new. Since the two fixes are independent, this revision contains > only the GUP batch size fix. > > Changes in v4: > - Simplify batch clamping logic as suggested by Kiryl > - Don't call GUP functions for an empty batch > - Drop the pinned byte reporting patch since it has been applied to mm-new > > v3: https://lore.kernel.org/all/20260901083452.115365-1-sarthak.sharma@arm.com/ > > mm/gup_test.c | 9 +++++---- > 1 file changed, 5 insertions(+), 4 deletions(-) > > diff --git a/mm/gup_test.c b/mm/gup_test.c > index 185ba3bb8ed1..1b64e932c4c5 100644 > --- a/mm/gup_test.c > +++ b/mm/gup_test.c > @@ -139,11 +139,12 @@ static int __gup_test_ioctl(unsigned int cmd, > if (nr != gup->nr_pages_per_call) > break; > > + nr = min_t(unsigned long, gup->nr_pages_per_call, > + (end - addr) / PAGE_SIZE); > + if (!nr) > + break; Can !nr actually happen? -- Cheers, David ^ permalink raw reply [flat|nested] 6+ messages in thread
[parent not found: <cc0fdb0e-ab6a-4095-b559-10e1d8a616d9@kernel.org_quarantine>]
* Re: [PATCH v4] mm/gup_test: safely calculate GUP batch size [not found] ` <cc0fdb0e-ab6a-4095-b559-10e1d8a616d9@kernel.org_quarantine> @ 2026-09-11 12:13 ` Sarthak Sharma 2026-09-11 18:16 ` David Hildenbrand (Arm) 0 siblings, 1 reply; 6+ messages in thread From: Sarthak Sharma @ 2026-09-11 12:13 UTC (permalink / raw) To: David Hildenbrand (Arm), Andrew Morton Cc: Jason Gunthorpe, John Hubbard, Peter Xu, Kiryl Shutsemau, linux-mm, linux-kernel Hi David! On 9/7/26 8:20 PM, David Hildenbrand (Arm) wrote: > On 9/3/26 13:50, Sarthak Sharma wrote: >> __gup_test_ioctl() calculates the end of a GUP batch using: >> >> next = addr + nr * PAGE_SIZE; >> >> If nr is too large, it can cause next to overflow and wrap around. >> If it wraps, the next > end check is bypassed and a large value >> of nr is passed to the GUP call, even though the pages array was >> allocated according to gup->size. This can lead to out of bounds writes. >> >> Also, when fewer than PAGE_SIZE bytes remain, the calculated batch >> contains zero pages. The code still calls GUP functions with pages + i, >> which can point past the allocated array. >> >> Calculate nr by taking the minimum of the number of pages per call and >> the pages remaining in the address range. Stop processing when no >> pages remain and calculate the end of the batch using this bounded >> value of nr. >> >> Fixes: 64c349f4ae78 ("mm: add infrastructure for get_user_pages_fast() benchmarking") >> Signed-off-by: Sarthak Sharma <sarthak.sharma@arm.com> >> --- >> Sashiko reported the issue fixed by this patch while reviewing the patch >> "mm/gup_test: report actual pinned bytes". That patch has been applied to >> mm-new. Since the two fixes are independent, this revision contains >> only the GUP batch size fix. >> >> Changes in v4: >> - Simplify batch clamping logic as suggested by Kiryl >> - Don't call GUP functions for an empty batch >> - Drop the pinned byte reporting patch since it has been applied to mm-new >> >> v3: https://lore.kernel.org/all/20260901083452.115365-1-sarthak.sharma@arm.com/ >> >> mm/gup_test.c | 9 +++++---- >> 1 file changed, 5 insertions(+), 4 deletions(-) >> >> diff --git a/mm/gup_test.c b/mm/gup_test.c >> index 185ba3bb8ed1..1b64e932c4c5 100644 >> --- a/mm/gup_test.c >> +++ b/mm/gup_test.c >> @@ -139,11 +139,12 @@ static int __gup_test_ioctl(unsigned int cmd, >> if (nr != gup->nr_pages_per_call) >> break; >> >> + nr = min_t(unsigned long, gup->nr_pages_per_call, >> + (end - addr) / PAGE_SIZE); >> + if (!nr) >> + break; > > Can !nr actually happen? Yes, it is possible in 2 cases: 1. We calculate end as gup->addr + gup->size. So, if gup->size is not page aligned, then at some point, we can have (end - addr) < PAGE_SIZE and (end != addr). Then (end - addr) / PAGESIZE will be 0. Right now all callers seem to provide a page aligned gup->size, but any userspace program which can open /sys/kernel/debug/gup_test can provide a non page aligned gup->size. 2. If the userspace supplies nr_pages_per_call = 0 (we never validate nr_pages_per_call so it is possible). Sorry for the delay, your email didn't reach my inbox due to some reason :) ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v4] mm/gup_test: safely calculate GUP batch size 2026-09-11 12:13 ` Sarthak Sharma @ 2026-09-11 18:16 ` David Hildenbrand (Arm) 0 siblings, 0 replies; 6+ messages in thread From: David Hildenbrand (Arm) @ 2026-09-11 18:16 UTC (permalink / raw) To: Sarthak Sharma, Andrew Morton Cc: Jason Gunthorpe, John Hubbard, Peter Xu, Kiryl Shutsemau, linux-mm, linux-kernel On 9/11/26 14:13, Sarthak Sharma wrote: > Hi David! > > On 9/7/26 8:20 PM, David Hildenbrand (Arm) wrote: >> On 9/3/26 13:50, Sarthak Sharma wrote: >>> __gup_test_ioctl() calculates the end of a GUP batch using: >>> >>> next = addr + nr * PAGE_SIZE; >>> >>> If nr is too large, it can cause next to overflow and wrap around. >>> If it wraps, the next > end check is bypassed and a large value >>> of nr is passed to the GUP call, even though the pages array was >>> allocated according to gup->size. This can lead to out of bounds writes. >>> >>> Also, when fewer than PAGE_SIZE bytes remain, the calculated batch >>> contains zero pages. The code still calls GUP functions with pages + i, >>> which can point past the allocated array. >>> >>> Calculate nr by taking the minimum of the number of pages per call and >>> the pages remaining in the address range. Stop processing when no >>> pages remain and calculate the end of the batch using this bounded >>> value of nr. >>> >>> Fixes: 64c349f4ae78 ("mm: add infrastructure for get_user_pages_fast() benchmarking") >>> Signed-off-by: Sarthak Sharma <sarthak.sharma@arm.com> >>> --- >>> Sashiko reported the issue fixed by this patch while reviewing the patch >>> "mm/gup_test: report actual pinned bytes". That patch has been applied to >>> mm-new. Since the two fixes are independent, this revision contains >>> only the GUP batch size fix. >>> >>> Changes in v4: >>> - Simplify batch clamping logic as suggested by Kiryl >>> - Don't call GUP functions for an empty batch >>> - Drop the pinned byte reporting patch since it has been applied to mm-new >>> >>> v3: https://lore.kernel.org/all/20260901083452.115365-1-sarthak.sharma@arm.com/ >>> >>> mm/gup_test.c | 9 +++++---- >>> 1 file changed, 5 insertions(+), 4 deletions(-) >>> >>> diff --git a/mm/gup_test.c b/mm/gup_test.c >>> index 185ba3bb8ed1..1b64e932c4c5 100644 >>> --- a/mm/gup_test.c >>> +++ b/mm/gup_test.c >>> @@ -139,11 +139,12 @@ static int __gup_test_ioctl(unsigned int cmd, >>> if (nr != gup->nr_pages_per_call) >>> break; >>> >>> + nr = min_t(unsigned long, gup->nr_pages_per_call, >>> + (end - addr) / PAGE_SIZE); >>> + if (!nr) >>> + break; >> >> Can !nr actually happen? > > Yes, it is possible in 2 cases: > > 1. We calculate end as gup->addr + gup->size. So, if gup->size is not > page aligned, then at some point, we can have (end - addr) < PAGE_SIZE > and (end != addr). Then (end - addr) / PAGESIZE will be 0. Right now all > callers seem to provide a page aligned gup->size, but any userspace > program which can open /sys/kernel/debug/gup_test can provide a non page > aligned gup->size. Note that this is a pure testing interface. So we don't care about weird user space doing stupid things. Rather reject unaligned gup->size early in __gup_test_ioctl(). > > 2. If the userspace supplies nr_pages_per_call = 0 (we never validate > nr_pages_per_call so it is possible). > Same here, user space won't do that. If you want to be sure, rather reject exactly that early in __gup_test_ioctl(). Something like diff --git a/mm/gup_test.c b/mm/gup_test.c index 185ba3bb8ed10..9e3e1df8dd526 100644 --- a/mm/gup_test.c +++ b/mm/gup_test.c @@ -116,7 +116,8 @@ static int __gup_test_ioctl(unsigned int cmd, bool needs_mmap_lock = cmd != GUP_FAST_BENCHMARK && cmd != PIN_FAST_BENCHMARK; - if (gup->addr > ULONG_MAX || gup->size > ULONG_MAX) + if (gup->addr > ULONG_MAX || gup->size > ULONG_MAX || !gup->size || + !PAGE_ALIGNED(gup->size)) return -EINVAL; if (check_add_overflow((unsigned long)gup->addr, (unsigned long)gup->size, &end)) > Sorry for the delay, your email didn't reach my inbox due to some reason :) No worries. -- Cheers, David ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-09-11 18:16 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-03 11:50 [PATCH v4] mm/gup_test: safely calculate GUP batch size Sarthak Sharma
2026-09-03 12:39 ` Kiryl Shutsemau
2026-09-04 4:45 ` Sarthak Sharma
2026-09-07 14:50 ` David Hildenbrand (Arm)
[not found] ` <cc0fdb0e-ab6a-4095-b559-10e1d8a616d9@kernel.org_quarantine>
2026-09-11 12:13 ` Sarthak Sharma
2026-09-11 18:16 ` David Hildenbrand (Arm)
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®