Search code examples
linuxmmap

How to return physical pages while keeping virtual addresses mapped?


I'd like to reserve a large contiguous region (4GiB) of virtual address space. I can do this with mmap. Then, as I write to the memory, the kernel will gradually cause it to become physically backed. At some stage I'd like to return physical pages within this range back to the kernel, while keeping the entire region of virtual address space still mapped. My issue is that if I return physical pages with munmap, then I not only return the physical pages, but I also return the virtual address space.

Is it possible to return just the physical pages while still keeping the virtual address space?


Solution

  • Yes, call madvise(2) with advice set to MADV_DONTNEED.