Linux mapping virtual memory range to existing virtual memory range?


In Linux, is there a way (in user space) to map a virtual address range to the physical pages that back an existing virtual address range? The mmap() function only allows one to map files or "new" physical pages. I need to be able to do something like this:

int* addr1 = malloc(SIZE);
int* addr2 = 0x60000;      // Assume nothing is allocated here
fancy_map_function(addr1, addr2, SIZE);
assert(*addr1 == *addr2);  // Should succeed
assert(addr1 != addr2);    // Should succeed
By : boiler96


Open /proc/self/mem and mmap the range of virtual addresses you need from it.

By : R..

If you have the fd for the file mapped at addr1, you can simply mmap it again at addr2.

Otherwise, the Linux-specific remap_file_pages can modify the virtual address ⇆ file offset translation within a single VMA, with page-sized granularity, including mapping the same file offset to multiple addresses.

By : ephemient

I was curious so I tested the shared memory idea suggested in question comments, and it seems to work:


This video can help you solving your question :)
By: admin