mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Hengyu Liang <hengyul@cs.unc.edu>
To: Andrew Morton <akpm@linux-foundation.org>,
	David Hildenbrand <david@kernel.org>
Cc: Baoquan He <baoquan.he@linux.dev>, Jason Gunthorpe <jgg@ziepe.ca>,
	John Hubbard <jhubbard@nvidia.com>, Peter Xu <peterx@redhat.com>,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	stable@vger.kernel.org
Subject: [PATCH] mm/gup: fault in ranges that start at address 0
Date: Wed, 25 Nov 2026 12:32:02 -0500	[thread overview]
Message-ID: <20261125173202.3636133-1-hengyul@cs.unc.edu> (raw)

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


             reply	other threads:[~2026-11-25 17:32 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 17:32 Hengyu Liang [this message]
2026-11-25 22:51 ` David Hildenbrand (Arm)
2026-11-27 10:04   ` Hengyu Liang
2026-11-30 12:38     ` 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=20261125173202.3636133-1-hengyul@cs.unc.edu \
    --to=hengyul@cs.unc.edu \
    --cc=akpm@linux-foundation.org \
    --cc=baoquan.he@linux.dev \
    --cc=david@kernel.org \
    --cc=jgg@ziepe.ca \
    --cc=jhubbard@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=peterx@redhat.com \
    --cc=stable@vger.kernel.org \
    /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

all inboxes | Powered by JetHome®