[libc-commits] [libc] [libc] Leave mmap offsets with the top bit set to the kernel (PR #239985)

Mike Lothian via libc-commits libc-commits at lists.llvm.org
Sat Nov 7 06:45:23 PST 2026


https://github.com/FireBurn created https://github.com/llvm/llvm-project/pull/239985

The kernel reads the mmap offset as unsigned, and drivers use its top bits
to tell mappings apart: KFD's doorbells sit at 0xebff000000000000. Rejecting
negative offsets in libc made ROCr's doorbell mapping fail, and HIP then
died on its first dispatch. mmap2 keeps the check, as there the offset is
divided before it reaches the kernel.

Assisted-by: Claude

>From 5cd695f478163c3805bd87d5499bc11c226f1c69 Mon Sep 17 00:00:00 2001
From: Mike Lothian <mike at fireburn.co.uk>
Date: Sat, 17 Oct 2026 11:12:37 +0100
Subject: [PATCH] [libc] Leave mmap offsets with the top bit set to the kernel

The kernel reads the mmap offset as unsigned, and drivers use its top bits
to tell mappings apart: KFD's doorbells sit at 0xebff000000000000. Rejecting
negative offsets in libc made ROCr's doorbell mapping fail, and HIP then
died on its first dispatch. mmap2 keeps the check, as there the offset is
divided before it reaches the kernel.

Assisted-by: Claude
---
 .../__support/OSUtil/linux/syscall_wrappers/mmap.h |  5 ++++-
 libc/test/src/sys/mman/linux/mmap_test.cpp         | 14 +++++++++++++-
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/mmap.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/mmap.h
index 0bda412368133e..c8296a2c1da731 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/mmap.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/mmap.h
@@ -22,9 +22,9 @@ namespace linux_syscalls {
 
 LIBC_INLINE ErrorOr<void *> mmap(void *addr, size_t size, int prot, int flags,
                                  int fd, off_t offset) {
+#ifdef SYS_mmap2
   if (offset < 0)
     return Error(EINVAL);
-#ifdef SYS_mmap2
   // The mmap2 syscall uses 4k units, regardless of the actual page, size on
   // almost every architecture. If porting to a new architecture (Openrisc,
   // hexagon?), please confirm this code is correct.
@@ -34,6 +34,9 @@ LIBC_INLINE ErrorOr<void *> mmap(void *addr, size_t size, int prot, int flags,
   offset /= MMAP2_FACTOR;
   long syscall_number = SYS_mmap2;
 #elif defined(SYS_mmap)
+  // The kernel takes the offset as unsigned, and drivers use the top bits to
+  // say what a mapping is: KFD's doorbells are at 0xebff000000000000. Leave
+  // validating the offset to the kernel.
   long syscall_number = SYS_mmap;
 #else
 #error "mmap or mmap2 syscalls not available."
diff --git a/libc/test/src/sys/mman/linux/mmap_test.cpp b/libc/test/src/sys/mman/linux/mmap_test.cpp
index eb1edd13f6467b..8072a69847c61d 100644
--- a/libc/test/src/sys/mman/linux/mmap_test.cpp
+++ b/libc/test/src/sys/mman/linux/mmap_test.cpp
@@ -57,10 +57,22 @@ TEST_F(LlvmLibcMMapTest, FileOffsets) {
   ASSERT_ERRNO_SUCCESS();
   EXPECT_THAT(LIBC_NAMESPACE::munmap(addr, 47), Succeeds());
 
-  // Mapping negative offsets fails.
+  // Mapping negative offsets fails. -1 is not page aligned, so this holds
+  // whichever of libc or the kernel looks at it.
   EXPECT_THAT(LIBC_NAMESPACE::mmap(nullptr, 47, PROT_READ, MAP_PRIVATE, fd, -1),
               Fails(EINVAL, MAP_FAILED));
 
+  if constexpr (sizeof(long) == sizeof(off_t)) {
+    // An aligned offset with the top bit set is the kernel's to judge: it is
+    // unsigned there, and drivers such as KFD encode the kind of mapping in
+    // it. A memfd cannot be that large, so the kernel says EOVERFLOW, where
+    // libc rejecting the offset itself would say EINVAL.
+    EXPECT_THAT(
+        LIBC_NAMESPACE::mmap(nullptr, 47, PROT_READ, MAP_PRIVATE, fd,
+                             static_cast<off_t>(1) << (sizeof(off_t) * 8 - 1)),
+        Fails(EOVERFLOW, MAP_FAILED));
+  }
+
   // So do offsets that are not page aligned. This should be rejected in the
   // kernel or by our mmap2 rounding code. Note that POSIX permits (but does not
   // require) mapping at unaligned offsets, but linux does not support it.



More information about the libc-commits mailing list