linuxtmpfs

Do all tmpfs instances on Linux share same memory pool?


I have embedded Linux system and it has several tmpfs mounts defined in the fstab. Some of these mounts are involved in systemd.

The customer asks why there is not just one mount for all the volatile "RAM FS" stuff.

Questions:

  1. Is physical memory allocated for all tmpfs instances from same common source (cache or whatever) so that we do not have to manage it manually?
  2. When files on one tmpfs mount are deleted, can the memory be reused by other mounts?
  3. Are there Linux flavors (or config options) where memory for tmpfs is allocated statically?
  4. Does tmpfs allocate virtual memory space in kernel or consume other precious resources proportional to the defined size of tmpfs?

Solution

  • Quotes from man tmpfs.

    1. The tmpfs [...] contents reside in virtual memory. Virtual memory is a big topic, you can browse it on the net. The sources for physical memory may be RAM (I don't know if you count separate RAM modules as different source) or from swap or from any other source if you write your driver for it. Virtual memory is build on top of that. The memory can be compressed "on the fly" zswap and have more futures (like removing duplication KSM and so on), so it's not like 5MB in virtual memory is 5MB in physical memory. I guess each tmpfs mountpoint has it's own virtual memory address space, but I think it's up to the implementation really.

    2. The [tmpfs] consumes only as much physical memory and swap space as is required to store the current contents of the filesystem..

    3. I don't really get that. I don't know what "static allocation" on a tmpfs/virtual memory level means. I guess you can write your own kernel which doesn't add a specified RAM module into the memory pool or creates a memory pool just for that RAM module and write your own kernel driver which uses that specified RAM module for your purposes, which will then simulate tmpfs.

    4. -

      4.1. Yes, the tmpfs driver allocates virtual memory from kernel.

      4.2. size=bytes Specify an upper limit on the size of the filesystem.. Only as much as big the resources in tmpfs are. You can optionally specify an upper limit. I guess tmpfs kernel driver also consumes some memory while being loaded, but that's negligible.

    5. There is not just one mount point for tmpfs stuff because of the granulation of developers and the software on your computer. If I write an application which will store big chunks of files in memory, I will mount a tmpfs just for my application. If you write another application, you will mount another tmpfs just for your application. That way we won't be writing together to the same folder, we can have same filenames for different applications. But the standard way is to use /tmp directory as the temporary place for files (POSIX.1-2008) and use function like mktemp to create files and folders there, so in reality there are one or two tmpfs mounted on a PC.

    6. If you are referring to proc, sysfs, cgroup or like devtmpfs (see udevd) and others, they are not tmpfs and are a separate topic.