* [PATCH] mm/gup: fault in ranges that start at address 0
@ 2026-11-25 17:32 Hengyu Liang
2026-11-25 22:51 ` David Hildenbrand (Arm)
0 siblings, 1 reply; 2+ messages in thread
From: Hengyu Liang @ 2026-11-25 17:32 UTC (permalink / raw)
To: Andrew Morton, David Hildenbrand
Cc: Baoquan He, Jason Gunthorpe, John Hubbard, Peter Xu, linux-mm,
linux-kernel, stable
Commit a7797e74bd39 ("mm/gup: clean up codes in fault_in_xxx()
functions") made the loops of fault_in_readable(), fault_in_writeable()
and fault_in_safe_writeable() stop when the current address is 0, to
detect an overflow.
However, the loops then do not run at all for a range that starts at
address 0, and the functions report that nothing was faulted in. As of
now, a write() from a buffer at address 0 fails with EFAULT when the
page is mapped but not present yet.
The issue can be reproduced with this program, run as root:
#include <fcntl.h>
#include <stdio.h>
#include <sys/mman.h>
#include <unistd.h>
int main(void)
{
int fd = open("file", O_WRONLY | O_CREAT, 0644);
mmap(NULL, 4096, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
printf("%zd\n", write(fd, NULL, 7));
return 0;
}
Before commit a7797e74bd39 ("mm/gup: clean up codes in fault_in_xxx()
functions"), the result is:
7
After that commit, the result is:
-1
With this patch, the result is the same as before that commit.
This patch will stop the loops when the current address is below the
start address instead, which is only the case after an overflow.
Fixes: a7797e74bd39 ("mm/gup: clean up codes in fault_in_xxx() functions")
Cc: stable@vger.kernel.org # v6.16
Signed-off-by: Hengyu Liang <hengyul@cs.unc.edu>
---
I found this while comparing the behavior of different kernel versions.
I used an LLM to analyze the problem and to draft the patch and this
message, and I analyzed the result myself.
Tested on v7.4-rc3 with KASAN and lockdep: with the buffer at address 0
and at 0x10000, on ext4, tmpfs and xfs, write(), pwritev() and O_DIRECT
reads and writes give the same results as before commit a7797e74bd39.
mm/gup.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/mm/gup.c b/mm/gup.c
index 2b405cce2ca5..a174e93327e9 100644
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -2074,7 +2074,7 @@ size_t fault_in_writeable(char __user *uaddr, size_t size)
return size;
/* Stop once we overflow to 0. */
- for (cur = start; cur && cur < end; cur = PAGE_ALIGN_DOWN(cur + PAGE_SIZE))
+ for (cur = start; cur >= start && cur < end; cur = PAGE_ALIGN_DOWN(cur + PAGE_SIZE))
unsafe_put_user(0, (char __user *)cur, out);
out:
user_write_access_end();
@@ -2144,7 +2144,7 @@ size_t fault_in_safe_writeable(const char __user *uaddr, size_t size)
mmap_read_lock(mm);
/* Stop once we overflow to 0. */
- for (cur = start; cur && cur < end; cur = PAGE_ALIGN_DOWN(cur + PAGE_SIZE))
+ for (cur = start; cur >= start && cur < end; cur = PAGE_ALIGN_DOWN(cur + PAGE_SIZE))
if (fixup_user_fault(mm, cur, FAULT_FLAG_WRITE, &unlocked))
break;
mmap_read_unlock(mm);
@@ -2176,7 +2176,7 @@ size_t fault_in_readable(const char __user *uaddr, size_t size)
return size;
/* Stop once we overflow to 0. */
- for (cur = start; cur && cur < end; cur = PAGE_ALIGN_DOWN(cur + PAGE_SIZE))
+ for (cur = start; cur >= start && cur < end; cur = PAGE_ALIGN_DOWN(cur + PAGE_SIZE))
unsafe_get_user(c, (const char __user *)cur, out);
out:
user_read_access_end();
base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
--
2.53.0
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH] mm/gup: fault in ranges that start at address 0
2026-11-25 17:32 [PATCH] mm/gup: fault in ranges that start at address 0 Hengyu Liang
@ 2026-11-25 22:51 ` David Hildenbrand (Arm)
0 siblings, 0 replies; 2+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-25 22:51 UTC (permalink / raw)
To: Hengyu Liang, Andrew Morton
Cc: Baoquan He, Jason Gunthorpe, John Hubbard, Peter Xu, linux-mm,
linux-kernel, stable
On 11/25/26 18:32, Hengyu Liang wrote:
> Commit a7797e74bd39 ("mm/gup: clean up codes in fault_in_xxx()
> functions") made the loops of fault_in_readable(), fault_in_writeable()
> and fault_in_safe_writeable() stop when the current address is 0, to
> detect an overflow.
>
> However, the loops then do not run at all for a range that starts at
> address 0, and the functions report that nothing was faulted in. As of
> now, a write() from a buffer at address 0 fails with EFAULT when the
> page is mapped but not present yet.
>
> The issue can be reproduced with this program, run as root:
>
> #include <fcntl.h>
> #include <stdio.h>
> #include <sys/mman.h>
> #include <unistd.h>
>
> int main(void)
> {
> int fd = open("file", O_WRONLY | O_CREAT, 0644);
>
> mmap(NULL, 4096, PROT_READ | PROT_WRITE,
> MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
> printf("%zd\n", write(fd, NULL, 7));
> return 0;
> }
Thanks for the report.
Ah, so we require root, because usually we have vm.mmap_min_addr limits in place.
Where we disallow mapping to address 0 for good reasons ;)
>
> Before commit a7797e74bd39 ("mm/gup: clean up codes in fault_in_xxx()
> functions"), the result is:
>
> 7
>
> After that commit, the result is:
>
> -1
>
> With this patch, the result is the same as before that commit.
>
> This patch will stop the loops when the current address is below the
> start address instead, which is only the case after an overflow.
>
> Fixes: a7797e74bd39 ("mm/gup: clean up codes in fault_in_xxx() functions")
> Cc: stable@vger.kernel.org # v6.16
> Signed-off-by: Hengyu Liang <hengyul@cs.unc.edu>
> ---
> I found this while comparing the behavior of different kernel versions.
> I used an LLM to analyze the problem and to draft the patch and this
> message, and I analyzed the result myself.
Okay, but you did not actually run into this yourself, right?
Doing some research, mapping to address 0 should not really have any users.
There are some legacy dosem and wine emulators, but looks like that's only a
problem for 32bit and all legacy.
Likely this is "nobody cares" and we don't need CC stable.
>
> Tested on v7.4-rc3 with KASAN and lockdep: with the buffer at address 0
> and at 0x10000, on ext4, tmpfs and xfs, write(), pwritev() and O_DIRECT
> reads and writes give the same results as before commit a7797e74bd39.
>
> mm/gup.c | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/mm/gup.c b/mm/gup.c
> index 2b405cce2ca5..a174e93327e9 100644
> --- a/mm/gup.c
> +++ b/mm/gup.c
> @@ -2074,7 +2074,7 @@ size_t fault_in_writeable(char __user *uaddr, size_t size)
> return size;
>
> /* Stop once we overflow to 0. */
These comments are not really correct anymore. We don't stop once we overflow to
0 anymore.
Likely we want to be more careful when calcualting+handling end instead.
--
Cheers,
David
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-25 22:51 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25 17:32 [PATCH] mm/gup: fault in ranges that start at address 0 Hengyu Liang
2026-11-25 22:51 ` 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