From: Sarthak Sharma <sarthak.sharma@arm.com>
To: "David Hildenbrand (Arm)" <david@kernel.org>,
Andrew Morton <akpm@linux-foundation.org>
Cc: Jason Gunthorpe <jgg@ziepe.ca>,
John Hubbard <jhubbard@nvidia.com>, Peter Xu <peterx@redhat.com>,
Kiryl Shutsemau <kas@kernel.org>,
linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v4] mm/gup_test: safely calculate GUP batch size
Date: Fri, 11 Sep 2026 17:43:28 +0530 [thread overview]
Message-ID: <366c39ee-d452-4a1c-af0d-2badd4d8210f@arm.com> (raw)
In-Reply-To: <cc0fdb0e-ab6a-4095-b559-10e1d8a616d9@kernel.org_quarantine>
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 :)
next prev parent reply other threads:[~2026-09-11 12:13 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
2026-09-11 18:16 ` David Hildenbrand (Arm)
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=366c39ee-d452-4a1c-af0d-2badd4d8210f@arm.com \
--to=sarthak.sharma@arm.com \
--cc=akpm@linux-foundation.org \
--cc=david@kernel.org \
--cc=jgg@ziepe.ca \
--cc=jhubbard@nvidia.com \
--cc=kas@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=peterx@redhat.com \
/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