Today's Question:  What weekend projects have you created?        GIVE A SHOUT

Technical Article => Operating System =>  Linux/Unix

About tmpfs

  sonic0002      2013-06-14 12:10:56      2,445    0

tmpfs is another confusing name in Linux kernel, its implementation is in mm/shmem.c, shmem has no relation to tmpfs at first glance although we know tmpfs is based on memory. We can understand why we use this name by seeing where this is used.

In a desktop Linux system, tmpfs is loaded usually:

% grep tmpfs /proc/mounts
devtmpfs /dev devtmpfs rw,seclabel,nosuid,relatime,size=1958956k,nr_inodes=489739,mode=755 0 0
tmpfs /dev/shm tmpfs rw,seclabel,nosuid,nodev,relatime 0 0
tmpfs /run tmpfs rw,seclabel,nosuid,nodev,relatime,mode=755 0 0
tmpfs /sys/fs/cgroup tmpfs rw,seclabel,nosuid,nodev,noexec,relatime,mode=755 0 0
tmpfs /media tmpfs rw,rootcontext=system_u:object_r:mnt_t:s0,seclabel,nosuid,nodev,noexec,relatime,mode=755 0 0

/dev/shm is used by POSIX IPC to realize communication among processes. Apart from the oflag parameter in sem_open(3), we basically cannot see it has relation to files.

Another place which uses it is in anonymous shared mapping.


        if (file) {
        } else if (vm_flags & VM_SHARED) {
                error = shmem_zero_setup(vma);
                if (error)
                        goto free_vma;

shmem_zero_setup() creates a dev/zero file in tmpfs root directory mounted by kem_mount(), please note this can be repeatedly created since the kernel skips similar may_create() check. This file itself is also very special, it is unlinked at the beginning, so kernel actually implements the anonymous shared mapping through one file in tmpfs.

Also tmpfs can be flexibly mounted through mount -t tmpfs, you can do any file operation on it, so the kernel connects below three things through tmpfs:

  1. Anonymous shared mapping
  3. tmpfs file operation.

Mel Gorman explained:

This is a very clean interface that is conceptually easy to understand but it does not help anonymous pages as there is no file backing. To keep this nice interface, Linux creates an artifical file-backing for anonymous pages using a RAM-based filesystem where each VMA is backed by a “file” in this filesystem. Every inode in the filesystem is placed on a linked list called shmem_inodes so that they may always be easily located. This allows the same file-based interface to be used without treating anonymous pages as a special case.

The reason why using tmpfs instead of ramfs is:

  1. tmpfs file can be swapped
  2. tmpfs has size limitation

Source :



Share on Facebook  Share on Twitter  Share on Google+  Share on Weibo  Share on Reddit  Share on Digg  Share on Tumblr    Delicious



No comment for this article.


Unexpected behavior due to a bug

By sonic0002