remap_file_pages(2)

πŸ’– remap_file_pages β€” create a nonlinear file mapping πŸ’–

okay bestie, sit down, we gotta talk. this one's a remap_file_pages(2) situation and it's giving legacy, it's giving please migrate, it's giving the kernel said no more. 🫢


🌸 NAME 🌸

remap_file_pages β€” create a nonlinear file mapping

πŸ“š LIBRARY πŸ“š

Standard C library (libc, -lc)

✨ SYNOPSIS ✨

#define _GNU_SOURCE         /* See feature_test_macros(7) */
#include <sys/mman.h>

[[deprecated]] int remap_file_pages(size_t size;
                                    void addr[size], size_t size,
                                    int prot, size_t pgoff, int flags);

πŸ‘‘ DESCRIPTION πŸ‘‘

🚨 bestie listen. this syscall got marked deprecated starting with Linux 3.16. and then in Linux 4.0 the actual implementation got replaced by a slower in-kernel emulation. not a glow-up, a glow-down. 😭

Those few applications that use this system call should consider migrating to alternatives, fr fr. Why did this happen? Because the kernel code for this system call was complex, and it is believed to be little used or perhaps even completely unused. πŸ’€ It did have some use cases in database applications on 32-bit systems, but those use cases don't exist on 64-bit systems. so like… okay. periodt.

So what is remap_file_pages()? The tea is: it's used to create a nonlinear mapping, that is, a mapping in which the pages of the file are mapped into a nonsequential order in memory. 🌈 Chaos mapping. The pages said "we're not doing sequential today."

The advantage of using remap_file_pages() over using repeated calls to mmap(2) is that the former approach does not require the kernel to create additional VMA (Virtual Memory Area) data structures. So it's the low-effort route for the kernel. slay efficiency, kinda.

To create a nonlinear mapping we perform the following steps:

  1. Use mmap(2) to create a mapping (which is initially linear). This mapping must be created with the MAP_SHARED flag. βœ…
  2. Use one or more calls to remap_file_pages() to rearrange the correspondence between the pages of the mapping and the pages of the file. It is possible to map the same page of a file into multiple locations within the mapped region. πŸ” iconic behaviour honestly.

The pgoff and size arguments specify the region of the file that is to be relocated within the mapping: pgoff is a file offset in units of the system page size; size is the length of the region in bytes.

The addr argument serves two purposes. First, it identifies the mapping whose pages we want to rearrange. Thus, addr must be an address that falls within a region previously mapped by a call to mmap(2). Second, addr specifies the address at which the file pages identified by pgoff and size will be placed. two jobs, one argument, main character energy πŸ’…

The values specified in addr and size should be multiples of the system page size. If they are not, then the kernel rounds both values down to the nearest multiple of the page size. (yes bestie, it just rounds down. no warning, no drama, just math. πŸ™ˆ)

The prot argument must be specified as 0. That's it. Zero. Nothing else. Don't get creative.

The flags argument has the same meaning as for mmap(2), but all flags other than MAP_NONBLOCK are ignored. so basically only MAP_NONBLOCK matters here, everything else gets ghosted. πŸ‘»


πŸŽ€ RETURN VALUE πŸŽ€

On success, remap_file_pages() returns 0. πŸ₯³

On error, -1 is returned, and errno is set to indicate the error. 😭

πŸ’… ERRORS πŸ’…

  • EINVAL β€” addr does not refer to a valid mapping created with the MAP_SHARED flag.
  • EINVAL β€” addr, size, prot, or pgoff is invalid.

(yes, both of them are EINVAL. iconic of it to double up. 😌)

⭐ STANDARDS ⭐

Linux.

πŸ¦‹ HISTORY πŸ¦‹

Linux 2.5.46, glibc 2.3.3. ✨ a vintage, an antique, a relic πŸ’œ

πŸ“ NOTES πŸ“

Since Linux 2.6.23, remap_file_pages() creates non-linear mappings only on in-memory filesystems such as tmpfs(5), hugetlbfs or ramfs. 🧚 If you're on a filesystem with a backing store, remap_file_pages() is not much more efficient than using mmap(2) to adjust which parts of the file are mapped to which addresses. So like… literally just use mmap(2) at that point bestie, no cap.

πŸ”₯ SEE ALSO πŸ”₯

getpagesize(2), mmap(2), mmap2(2), mprotect(2), mremap(2), msync(2)


and that's the whole page, bestie!! πŸŒΈπŸ’– this one came from Linux man-pages 6.20, dated 2026-02-08, and honestly it ate. now go use mmap(2) like a good citizen πŸ«Άβœ¨πŸ’œ

πŸ“¦ where this page comes from
  • arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • πŸ“œ the page's license: Linux-man-pages-copyleft
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms