How does memory splitting work with mmap() and virtual memory?

85 Views Asked by At

Forepart:

  1. Assume we're on a 32bit Linux OS, with 4GB of physical memory.

  2. There is no swap partition!

  3. There's a kernel, reserving 200MB of non-pageable memory which cannot be taken by anyone else (this whole statement is only my assumption and you may correct the value or state that kernel doesn't do that).

  4. At time t₀ there are three processes p1, p2 and p3 consuming 1GB of physical memory.

  5. Now a 4th process starts which mmap(2)s 4GB of memory with MAP_PRIVATE of an empty file, with PROT_WRITE! This process starts a for loop, writing some value to each element of this mmaped array; like this:

    for(int i=0; i<4GB; i++)
        mmap_array[i] = i*i%255;
    

Question:

  1. When process p4 has written data up to 2.8GB, asking to write the next byte, does kernel halt the other 3 processes, create a image of them, and since there is no swap, store them back into RAM; Which again consumes 1GB of physical memory (because they had 1GB of RAM when they were running, so same image size)?
  2. If above question is a YES, does it mean that if a process asks for more memory, which is held by others, there will be context switch?
  3. Regarding how page fault works, from now on, since this 1GB couldn't be freed and available RAM is still 2.8GB, when p4 asks for the next element of array, does kernel unload the very first page, and then load new page instead of unloaded page?
  4. If the 3th question is a YES (which I doubt) how does the kernel keep the value of that unloaded page whose physical memory is to be used by this new page? (There's no swap partition; would it force disk swap space anyway?)
  5. What if it was MAP_SHARED? Would the value of the unloaded page be carried through the backing file, and then the physical space get freed?
  6. When a process like p4 does that (or other cases when RAM gets 100%) why system hangs? Is it because it needs extra space to restore those images of p1, p2 and p3? Or those will be restored in that 1GB, but as soon as they ask for more memory, they'll freeze?
  7. Again if #6 is a YES, and those 3 processes used MAP_SHARED couldn't or do they act as #5? Continue but slow (only one page of RAM at the time)?
  8. SysRq still works; does it mean every process will hang, but since kernel has reserved memory, it won't?

The reason why all these questions came all together in one post is by understanding all these, I'm able to understand the actual question which is the one I wrote in title!

I just numbered the things I'm looking to get answer for, to avoid commenting on answers people write.

1

There are 1 best solutions below

12
datenwolf On

Let me introduce you to the "Linux Out-Of-Memory (OOM) Killer" https://rakeshjain-devops.medium.com/linux-out-of-memory-killer-31e477a45759

Basically the Linux kernel does maintain a score of "sacrificability" for each and every process, and if it ever encounters are situation where it cannot fulfill a memory allocation, it will sacrifice processes in order of the score, to make space.

If push comes to shove, the process that attempts a mmap-ed write that can not be done, because no matter how hard the kernel tries to make space, eventually that process will get raised a SIGBUS (bus error, bad memory access) signal.