shmem vs tmpfs
In Linux, shmem (Shared Memory) and tmpfs refer to two aspects of the exact same underlying kernel mechanism.
The cleanest distinction is:
shmemis the internal kernel subsystem (and page cache infrastructure) that manages volatile, swap-backed memory pages.tmpfsis the user-facing RAM filesystem driver mounted by the OS to expose that shared memory abstraction as a standard directory.
In the Linux kernel source code, both live in the same file: mm/shmem.c.
How They Relate Architecturally
In the Linux kernel, shmem is the foundation, and tmpfs is a wrapper layer over it:
┌─────────────────────────────────────────┐
│ Userspace Applications │
└────┬──────────────────────────────┬─────┘
│ │
│ POSIX / SysV IPC │ File Operations
│ (shm_open, shmget) │ (open, read, write)
▼ ▼
┌──────────────────┐ ┌──────────────────┐
│ Internal shmem │ │ User-Visible │
│ Mount │ │ tmpfs Mounts │
│ (Unlinked Inodes)│ │ (/dev/shm, /tmp) │
└────────┬─────────┘ └────────┬─────────┘
│ │
└──────────────┬───────────────┘
▼
┌───────────────────────┐
│ Kernel mm/shmem │
│ (Page Cache + Swap) │
└───────────────────────┘
When you inspect mm/shmem.c in the Linux kernel tree, you will literally see the filesystem registration defined as:
static struct file_system_type shmem_fs_type = {
.owner = THIS_MODULE,
.name = "tmpfs", /* Register name */
.init_fs_context = shmem_init_fs_context,
...
};
Key Differences in Usage & Context
While they share the same engine, the terms are used differently depending on where you look in the system:
| Context | shmem |
tmpfs |
|---|---|---|
| System Visibility | Internal kernel memory accounting (Shmem entry in /proc/meminfo). |
User-visible mounted filesystems (df -h, showing mount points like /dev/shm or /tmp). |
| Kernel Config | Always built into the kernel, regardless of options (used for anonymous mmap(MAP_SHARED), System V IPC). |
Can be disabled at build time via CONFIG_TMPFS (though almost every distribution enables it). |
| Primary Access Point | IPC APIs (shm_open, shmget, shared mmap). |
File system utilities (mkdir, cp, touch inside /tmp or /dev/shm). |
The Internal shmem Mount vs. User tmpfs Mounts
- Kernel Internal Mount: The kernel secretly maintains an invisible, internal
shmemmount. When you create shared anonymous memory (viammap(..., MAP_SHARED | MAP_ANONYMOUS, ...)or System Vshmget), the kernel behind the scenes creates an unlinked file inside this internalshmemsubsystem. - User Mounts (
tmpfs): When glibc executes POSIXshm_open(), or when your init system mounts/tmpor/dev/shm, it simply creates a user-visibletmpfsfilesystem instance over that sameshmemsubsystem.
Summary
- Are they equivalent? Functionally, yes.
tmpfsis just the filesystem interface built on top of the kernel'sshmempage management subsystem. - Memory source: Both store data in the Linux Page Cache and can be swapped out to disk swap space if the system comes under memory pressure.