Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I detect whether a specific page is mapped in memory?

Tags:

c

linux

unix

mmap

I would like to detect whether or not a specific page has already been mapped in memory. The goal here is to be able to perform this check before calling mmap with a fixed memory address. The following code illustrates what happens in this case by default: mmap silently remaps the original memory pages.

#include <sys/mman.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
  int page_size;
  void *ptr;
  page_size = getpagesize();
  ptr = mmap(0, 10 * page_size, PROT_READ | PROT_WRITE,
             MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
  if (ptr == MAP_FAILED) {
    printf ("map1 failed\n");
    return 1;
  }
  ((int *)ptr)[0] = 0xdeadbeaf;
  ptr = mmap(ptr, 2 * page_size, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, 0, 0);
  if (ptr == MAP_FAILED) {
    printf ("map2 failed\n");
    return 1;
  }
  if (((int *)ptr)[0] != 0xdeadbeaf) {
    printf ("oops, data gone !\n");
  }
  return 0;
}

I understand that I could open and parse /proc/self/maps to figure out which memory range has been allocated and infer from that if I can safely request a specific memory range with mmap but I am looking for a proper API: is there such a thing ?

like image 210
mathieu Avatar asked Dec 02 '11 21:12

mathieu


People also ask

How does memmap work?

mmap works by manipulating your process's page table, a data structure your CPU uses to map address spaces. The CPU will translate "virtual" addresses to "physical" ones, and does so according to the page table set up by your kernel. When you access the mapped memory for the first time, your CPU generates a page fault.

How memory mapping works?

Memory-mapping is a mechanism that maps a portion of a file, or an entire file, on disk to a range of addresses within an application's address space. The application can then access files on disk in the same way it accesses dynamic memory.

When to use memory mapped files?

Memory mapped files are a good choice when you need to work with a large amount of data and you would like to avoid the cost associated with marshaling and un-marshaling while sharing data across process boundaries.

What is the meaning of memory mapped?

A memory map is a massive table, in effect a database, that comprises complete information about how the memory is structured in a computer system. A memory map works something like a gigantic office organizer.


2 Answers

msync(addr, len, 0) and checking for ENOMEM seems to work (with a fairly superficial test).

like image 122
Richard Kettlewell Avatar answered Sep 18 '22 15:09

Richard Kettlewell


This doesn't "remap" the memory, but creates another mapping at different address (since the one you give it is already taken, and it's treated as a hint anyway). The old one is still valid, you just lose the reference to it since you overwrite the ptr variable.

If you want to create multiple mappings to the same memory like that look into shm_open(2).

If you just want to check if address is mapped then the MAP_FIXED trick pointed out by @MerickOWA should work.

Edit 0:

You are right about MAP_FIXED, it doesn't help in this case. What you might try is mincore(2). One of the errors it returns is:

ENOMEMaddr to addr + length contained unmapped memory.

like image 25
Nikolai Fetissov Avatar answered Sep 18 '22 15:09

Nikolai Fetissov