[PATCH 4/5] linux: Implement remap_file_pages in C (BZ 34917)

Adhemerval Zanella adhemerval.zanella@linaro.org
Thu Nov 5 17:13:10 GMT 2026


The kernel declares the remap_file_pages prot and flags as unsigned
long, and fails with EINVAL if prot is nonzero, including bits 32 to 63
(it masks the flags).  glibc takes both as int, and the assembly
wrapper generated from syscalls.list passes the registers through
unchanged, so a valid call can fail on x86_64, x32, and aarch64, where
the upper half of a register holding a 32-bit argument is unspecified.

Implement remap_file_pages in C, where INLINE_SYSCALL_CALL extends the
arguments to the full register on these ABIs.

The kernel logs a deprecation warning the first time remap_file_pages
is called, so running the test adds it to the kernel log once per boot.

Checked on x86_64-linux-gnu.
---
 sysdeps/unix/sysv/linux/remap_file_pages.c    | 33 +++++++++++++++++++
 sysdeps/unix/sysv/linux/syscalls.list         |  1 -
 .../unix/sysv/linux/tst-syscall-upper-bits.c  | 25 ++++++++++++++
 3 files changed, 58 insertions(+), 1 deletion(-)
 create mode 100644 sysdeps/unix/sysv/linux/remap_file_pages.c

diff --git a/sysdeps/unix/sysv/linux/remap_file_pages.c b/sysdeps/unix/sysv/linux/remap_file_pages.c
new file mode 100644
index 00000000000..341547af281
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/remap_file_pages.c
@@ -0,0 +1,33 @@
+/* Remap pages of a shared file mapping.  Linux version.
+   Copyright (C) 2026 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <sys/mman.h>
+#include <sysdep.h>
+
+/* The kernel takes PROT and FLAGS as unsigned long.  Unlike a
+   syscalls.list wrapper, INLINE_SYSCALL_CALL extends them to the full
+   register on ABIs that leave the upper bits of a 32-bit argument
+   unspecified.  */
+int
+__remap_file_pages (void *start, size_t size, int prot, size_t pgoff,
+		    int flags)
+{
+  return INLINE_SYSCALL_CALL (remap_file_pages, start, size, prot, pgoff,
+			      flags);
+}
+weak_alias (__remap_file_pages, remap_file_pages)
diff --git a/sysdeps/unix/sysv/linux/syscalls.list b/sysdeps/unix/sysv/linux/syscalls.list
index d328416ca98..0db9478e5f4 100644
--- a/sysdeps/unix/sysv/linux/syscalls.list
+++ b/sysdeps/unix/sysv/linux/syscalls.list
@@ -54,7 +54,6 @@ process_madvise EXTRA   process_madvise i:iPniU process_madvise
 process_mrelease EXTRA  process_mrelease i:iU   process_mrelease
 query_module	EXTRA	query_module	i:sipip	__compat_query_module	query_module@GLIBC_2.0:GLIBC_2.23
 quotactl	EXTRA	quotactl	i:isip	quotactl
-remap_file_pages -	remap_file_pages i:pUiUi	__remap_file_pages remap_file_pages
 sched_getp	-	sched_getparam	i:ip	__sched_getparam	sched_getparam
 sched_gets	-	sched_getscheduler	i:i	__sched_getscheduler	sched_getscheduler
 sched_primax	-	sched_get_priority_max	i:i	__sched_get_priority_max	sched_get_priority_max
diff --git a/sysdeps/unix/sysv/linux/tst-syscall-upper-bits.c b/sysdeps/unix/sysv/linux/tst-syscall-upper-bits.c
index 3dba1e65c8a..f46b96b2362 100644
--- a/sysdeps/unix/sysv/linux/tst-syscall-upper-bits.c
+++ b/sysdeps/unix/sysv/linux/tst-syscall-upper-bits.c
@@ -25,6 +25,7 @@
 #include <sys/wait.h>
 #include <unistd.h>
 #include <support/check.h>
+#include <support/temp_file.h>
 #include <support/xunistd.h>
 
 /* Some ABI (x86_64, aarch64) leave the upper 32 bits of a register holding
@@ -129,6 +130,29 @@ test_pkey_alloc (void)
     }
 }
 
+static int __attribute__ ((noipa))
+call_remap_file_pages (void *start, size_t size, uint64_t prot,
+		       size_t pgoff, uint64_t flags)
+{
+  return remap_file_pages (start, size, prot, pgoff, flags);
+}
+
+/* The kernel rejects any nonzero prot.  */
+static void
+test_remap_file_pages (void)
+{
+  size_t pagesize = getpagesize ();
+  int fd = create_temp_file ("tst-syscall-upper-bits-", NULL);
+  TEST_VERIFY_EXIT (fd >= 0);
+  xftruncate (fd, pagesize);
+  void *p = xmmap (NULL, pagesize, PROT_READ, MAP_SHARED, fd);
+  COMPARE_CALLS (remap_file_pages (p, pagesize, 0, 0, 0),
+		 call_remap_file_pages (p, pagesize, DIRTY (0), 0,
+					DIRTY (0)));
+  xmunmap (p, pagesize);
+  xclose (fd);
+}
+
 static long int __attribute__ ((noipa))
 call_ptrace (uint64_t request, pid_t pid, void *addr, void *data)
 {
@@ -167,6 +191,7 @@ do_test (void)
   test_mprotect ();
   test_pkey_mprotect ();
   test_pkey_alloc ();
+  test_remap_file_pages ();
   test_ptrace ();
 
   return 0;
-- 
2.53.0



More information about the Libc-alpha mailing list