Search code examples
memorylinux-kerneltmpfs

Is executable code stored uniquely in tmpfs copied to another part of RAM when run?


An executable on disk needs to first have it's code and data sections loaded into RAM before it can be executed. When an executable is stored in tmpfs, it's already in RAM, so does the kernel bypass the step of loading the executable into RAM by just mapping the tmpfs pages into the processes address space? Does the answer apply to both executables and loaded libraries?


Solution

  • Your question appears to have been answered in a post on the Linux Kernel Mailing List in 2007

    (As tmps is a scheme for storing in the filesystem cache code, with no backing storage, the mentioned buffer cache should I believe be the "original")

    Phillip Susi asked:

    The question is, when you execute a binary on tmpfs, does its code segment get mapped directly where it's at in the buffer cache, or does it get copied to another page for the executing process? At least, assuming this is possible due to the vma and file offsets of the segment being aligned.

    And Hugh Dickens replied

    Its pages are mapped directly into the executing process, without copying.

    You might want to read the full thread - a note is made that this depends on having a system with an MMU, and then the discussion veers into tmpfs's non-persistence.

    Linux's copy-on-write behavior would I believe mean that any data page you write to would get a unique copy created for your process at the time of the first writing.