xref: /linux/Documentation/filesystems/tmpfs.rst (revision a1c613ae4c322ddd58d5a8539dbfba2a0380a8c0)
17e7cd458SMauro Carvalho Chehab.. SPDX-License-Identifier: GPL-2.0
27e7cd458SMauro Carvalho Chehab
37e7cd458SMauro Carvalho Chehab=====
47e7cd458SMauro Carvalho ChehabTmpfs
57e7cd458SMauro Carvalho Chehab=====
67e7cd458SMauro Carvalho Chehab
7f38d58b7SRandy DunlapTmpfs is a file system which keeps all of its files in virtual memory.
87e7cd458SMauro Carvalho Chehab
97e7cd458SMauro Carvalho Chehab
107e7cd458SMauro Carvalho ChehabEverything in tmpfs is temporary in the sense that no files will be
117e7cd458SMauro Carvalho Chehabcreated on your hard drive. If you unmount a tmpfs instance,
127e7cd458SMauro Carvalho Chehabeverything stored therein is lost.
137e7cd458SMauro Carvalho Chehab
147e7cd458SMauro Carvalho Chehabtmpfs puts everything into the kernel internal caches and grows and
157e7cd458SMauro Carvalho Chehabshrinks to accommodate the files it contains and is able to swap
162c6efe9cSLuis Chamberlainunneeded pages out to swap space, if swap was enabled for the tmpfs
172c6efe9cSLuis Chamberlainmount. tmpfs also supports THP.
187e7cd458SMauro Carvalho Chehab
19d0f5a854SLuis Chamberlaintmpfs extends ramfs with a few userspace configurable options listed and
20d0f5a854SLuis Chamberlainexplained further below, some of which can be reconfigured dynamically on the
21d0f5a854SLuis Chamberlainfly using a remount ('mount -o remount ...') of the filesystem. A tmpfs
22d0f5a854SLuis Chamberlainfilesystem can be resized but it cannot be resized to a size below its current
23d0f5a854SLuis Chamberlainusage. tmpfs also supports POSIX ACLs, and extended attributes for the
24*2daf18a7SHugh Dickinstrusted.*, security.* and user.* namespaces. ramfs does not use swap and you
25*2daf18a7SHugh Dickinscannot modify any parameter for a ramfs filesystem. The size limit of a ramfs
26d0f5a854SLuis Chamberlainfilesystem is how much memory you have available, and so care must be taken if
27d0f5a854SLuis Chamberlainused so to not run out of memory.
28d0f5a854SLuis Chamberlain
29d0f5a854SLuis ChamberlainAn alternative to tmpfs and ramfs is to use brd to create RAM disks
30d0f5a854SLuis Chamberlain(/dev/ram*), which allows you to simulate a block device disk in physical RAM.
31d0f5a854SLuis ChamberlainTo write data you would just then need to create an regular filesystem on top
32d0f5a854SLuis Chamberlainthis ramdisk. As with ramfs, brd ramdisks cannot swap. brd ramdisks are also
33d0f5a854SLuis Chamberlainconfigured in size at initialization and you cannot dynamically resize them.
34d0f5a854SLuis ChamberlainContrary to brd ramdisks, tmpfs has its own filesystem, it does not rely on the
35d0f5a854SLuis Chamberlainblock layer at all.
367e7cd458SMauro Carvalho Chehab
372c6efe9cSLuis ChamberlainSince tmpfs lives completely in the page cache and optionally on swap,
382c6efe9cSLuis Chamberlainall tmpfs pages will be shown as "Shmem" in /proc/meminfo and "Shared" in
397e7cd458SMauro Carvalho Chehabfree(1). Notice that these counters also include shared memory
407e7cd458SMauro Carvalho Chehab(shmem, see ipcs(1)). The most reliable way to get the count is
417e7cd458SMauro Carvalho Chehabusing df(1) and du(1).
427e7cd458SMauro Carvalho Chehab
437e7cd458SMauro Carvalho Chehabtmpfs has the following uses:
447e7cd458SMauro Carvalho Chehab
457e7cd458SMauro Carvalho Chehab1) There is always a kernel internal mount which you will not see at
467e7cd458SMauro Carvalho Chehab   all. This is used for shared anonymous mappings and SYSV shared
477e7cd458SMauro Carvalho Chehab   memory.
487e7cd458SMauro Carvalho Chehab
497e7cd458SMauro Carvalho Chehab   This mount does not depend on CONFIG_TMPFS. If CONFIG_TMPFS is not
50f38d58b7SRandy Dunlap   set, the user visible part of tmpfs is not built. But the internal
517e7cd458SMauro Carvalho Chehab   mechanisms are always present.
527e7cd458SMauro Carvalho Chehab
537e7cd458SMauro Carvalho Chehab2) glibc 2.2 and above expects tmpfs to be mounted at /dev/shm for
547e7cd458SMauro Carvalho Chehab   POSIX shared memory (shm_open, shm_unlink). Adding the following
557e7cd458SMauro Carvalho Chehab   line to /etc/fstab should take care of this::
567e7cd458SMauro Carvalho Chehab
577e7cd458SMauro Carvalho Chehab	tmpfs	/dev/shm	tmpfs	defaults	0 0
587e7cd458SMauro Carvalho Chehab
597e7cd458SMauro Carvalho Chehab   Remember to create the directory that you intend to mount tmpfs on
607e7cd458SMauro Carvalho Chehab   if necessary.
617e7cd458SMauro Carvalho Chehab
627e7cd458SMauro Carvalho Chehab   This mount is _not_ needed for SYSV shared memory. The internal
637e7cd458SMauro Carvalho Chehab   mount is used for that. (In the 2.3 kernel versions it was
647e7cd458SMauro Carvalho Chehab   necessary to mount the predecessor of tmpfs (shm fs) to use SYSV
65f38d58b7SRandy Dunlap   shared memory.)
667e7cd458SMauro Carvalho Chehab
677e7cd458SMauro Carvalho Chehab3) Some people (including me) find it very convenient to mount it
687e7cd458SMauro Carvalho Chehab   e.g. on /tmp and /var/tmp and have a big swap partition. And now
697e7cd458SMauro Carvalho Chehab   loop mounts of tmpfs files do work, so mkinitrd shipped by most
707e7cd458SMauro Carvalho Chehab   distributions should succeed with a tmpfs /tmp.
717e7cd458SMauro Carvalho Chehab
727e7cd458SMauro Carvalho Chehab4) And probably a lot more I do not know about :-)
737e7cd458SMauro Carvalho Chehab
747e7cd458SMauro Carvalho Chehab
757e7cd458SMauro Carvalho Chehabtmpfs has three mount options for sizing:
767e7cd458SMauro Carvalho Chehab
777e7cd458SMauro Carvalho Chehab=========  ============================================================
787e7cd458SMauro Carvalho Chehabsize       The limit of allocated bytes for this tmpfs instance. The
797e7cd458SMauro Carvalho Chehab           default is half of your physical RAM without swap. If you
807e7cd458SMauro Carvalho Chehab           oversize your tmpfs instances the machine will deadlock
817e7cd458SMauro Carvalho Chehab           since the OOM handler will not be able to free that memory.
827e7cd458SMauro Carvalho Chehabnr_blocks  The same as size, but in blocks of PAGE_SIZE.
837e7cd458SMauro Carvalho Chehabnr_inodes  The maximum number of inodes for this instance. The default
847e7cd458SMauro Carvalho Chehab           is half of the number of your physical RAM pages, or (on a
857e7cd458SMauro Carvalho Chehab           machine with highmem) the number of lowmem RAM pages,
867e7cd458SMauro Carvalho Chehab           whichever is the lower.
877e7cd458SMauro Carvalho Chehab=========  ============================================================
887e7cd458SMauro Carvalho Chehab
897e7cd458SMauro Carvalho ChehabThese parameters accept a suffix k, m or g for kilo, mega and giga and
907e7cd458SMauro Carvalho Chehabcan be changed on remount.  The size parameter also accepts a suffix %
917e7cd458SMauro Carvalho Chehabto limit this tmpfs instance to that percentage of your physical RAM:
927e7cd458SMauro Carvalho Chehabthe default, when neither size nor nr_blocks is specified, is size=50%
937e7cd458SMauro Carvalho Chehab
947e7cd458SMauro Carvalho ChehabIf nr_blocks=0 (or size=0), blocks will not be limited in that instance;
957e7cd458SMauro Carvalho Chehabif nr_inodes=0, inodes will not be limited.  It is generally unwise to
967e7cd458SMauro Carvalho Chehabmount with such options, since it allows any user with write access to
977e7cd458SMauro Carvalho Chehabuse up all the memory on the machine; but enhances the scalability of
98f38d58b7SRandy Dunlapthat instance in a system with many CPUs making intensive use of it.
997e7cd458SMauro Carvalho Chehab
100*2daf18a7SHugh DickinsIf nr_inodes is not 0, that limited space for inodes is also used up by
101*2daf18a7SHugh Dickinsextended attributes: "df -i"'s IUsed and IUse% increase, IFree decreases.
102*2daf18a7SHugh Dickins
103253e5df8SHugh Dickinstmpfs blocks may be swapped out, when there is a shortage of memory.
104253e5df8SHugh Dickinstmpfs has a mount option to disable its use of swap:
105253e5df8SHugh Dickins
106253e5df8SHugh Dickins======  ===========================================================
107253e5df8SHugh Dickinsnoswap  Disables swap. Remounts must respect the original settings.
108253e5df8SHugh Dickins        By default swap is enabled.
109253e5df8SHugh Dickins======  ===========================================================
110253e5df8SHugh Dickins
111d0f5a854SLuis Chamberlaintmpfs also supports Transparent Huge Pages which requires a kernel
112d0f5a854SLuis Chamberlainconfigured with CONFIG_TRANSPARENT_HUGEPAGE and with huge supported for
113d0f5a854SLuis Chamberlainyour system (has_transparent_hugepage(), which is architecture specific).
114d0f5a854SLuis ChamberlainThe mount options for this are:
115d0f5a854SLuis Chamberlain
116253e5df8SHugh Dickins================ ==============================================================
117253e5df8SHugh Dickinshuge=never       Do not allocate huge pages.  This is the default.
118253e5df8SHugh Dickinshuge=always      Attempt to allocate huge page every time a new page is needed.
119253e5df8SHugh Dickinshuge=within_size Only allocate huge page if it will be fully within i_size.
120253e5df8SHugh Dickins                 Also respect madvise(2) hints.
121253e5df8SHugh Dickinshuge=advise      Only allocate huge page if requested with madvise(2).
122253e5df8SHugh Dickins================ ==============================================================
123d0f5a854SLuis Chamberlain
124253e5df8SHugh DickinsSee also Documentation/admin-guide/mm/transhuge.rst, which describes the
125253e5df8SHugh Dickinssysfs file /sys/kernel/mm/transparent_hugepage/shmem_enabled: which can
126253e5df8SHugh Dickinsbe used to deny huge pages on all tmpfs mounts in an emergency, or to
127253e5df8SHugh Dickinsforce huge pages on all tmpfs mounts for testing.
1287e7cd458SMauro Carvalho Chehab
129e09764cfSCarlos Maiolinotmpfs also supports quota with the following mount options
130e09764cfSCarlos Maiolino
131de4c0e7cSLukas Czerner======================== =================================================
132de4c0e7cSLukas Czernerquota                    User and group quota accounting and enforcement
133de4c0e7cSLukas Czerner                         is enabled on the mount. Tmpfs is using hidden
134de4c0e7cSLukas Czerner                         system quota files that are initialized on mount.
135de4c0e7cSLukas Czernerusrquota                 User quota accounting and enforcement is enabled
136de4c0e7cSLukas Czerner                         on the mount.
137de4c0e7cSLukas Czernergrpquota                 Group quota accounting and enforcement is enabled
138de4c0e7cSLukas Czerner                         on the mount.
139de4c0e7cSLukas Czernerusrquota_block_hardlimit Set global user quota block hard limit.
140de4c0e7cSLukas Czernerusrquota_inode_hardlimit Set global user quota inode hard limit.
141de4c0e7cSLukas Czernergrpquota_block_hardlimit Set global group quota block hard limit.
142de4c0e7cSLukas Czernergrpquota_inode_hardlimit Set global group quota inode hard limit.
143de4c0e7cSLukas Czerner======================== =================================================
144de4c0e7cSLukas Czerner
145de4c0e7cSLukas CzernerNone of the quota related mount options can be set or changed on remount.
146de4c0e7cSLukas Czerner
147de4c0e7cSLukas CzernerQuota limit parameters accept a suffix k, m or g for kilo, mega and giga
148de4c0e7cSLukas Czernerand can't be changed on remount. Default global quota limits are taking
149de4c0e7cSLukas Czernereffect for any and all user/group/project except root the first time the
150de4c0e7cSLukas Czernerquota entry for user/group/project id is being accessed - typically the
151de4c0e7cSLukas Czernerfirst time an inode with a particular id ownership is being created after
152de4c0e7cSLukas Czernerthe mount. In other words, instead of the limits being initialized to zero,
153de4c0e7cSLukas Czernerthey are initialized with the particular value provided with these mount
154de4c0e7cSLukas Czerneroptions. The limits can be changed for any user/group id at any time as they
155de4c0e7cSLukas Czernernormally can be.
156e09764cfSCarlos Maiolino
157e09764cfSCarlos MaiolinoNote that tmpfs quotas do not support user namespaces so no uid/gid
158e09764cfSCarlos Maiolinotranslation is done if quotas are enabled inside user namespaces.
159e09764cfSCarlos Maiolino
1607e7cd458SMauro Carvalho Chehabtmpfs has a mount option to set the NUMA memory allocation policy for
1617e7cd458SMauro Carvalho Chehaball files in that instance (if CONFIG_NUMA is enabled) - which can be
1627e7cd458SMauro Carvalho Chehabadjusted on the fly via 'mount -o remount ...'
1637e7cd458SMauro Carvalho Chehab
1647e7cd458SMauro Carvalho Chehab======================== ==============================================
1657e7cd458SMauro Carvalho Chehabmpol=default             use the process allocation policy
1667e7cd458SMauro Carvalho Chehab                         (see set_mempolicy(2))
1677e7cd458SMauro Carvalho Chehabmpol=prefer:Node         prefers to allocate memory from the given Node
1687e7cd458SMauro Carvalho Chehabmpol=bind:NodeList       allocates memory only from nodes in NodeList
1697e7cd458SMauro Carvalho Chehabmpol=interleave          prefers to allocate from each node in turn
1707e7cd458SMauro Carvalho Chehabmpol=interleave:NodeList allocates from each node of NodeList in turn
1717e7cd458SMauro Carvalho Chehabmpol=local		 prefers to allocate memory from the local node
1727e7cd458SMauro Carvalho Chehab======================== ==============================================
1737e7cd458SMauro Carvalho Chehab
1747e7cd458SMauro Carvalho ChehabNodeList format is a comma-separated list of decimal numbers and ranges,
1757e7cd458SMauro Carvalho Chehaba range being two hyphen-separated decimal numbers, the smallest and
1767e7cd458SMauro Carvalho Chehablargest node numbers in the range.  For example, mpol=bind:0-3,5,7,9-15
1777e7cd458SMauro Carvalho Chehab
1787e7cd458SMauro Carvalho ChehabA memory policy with a valid NodeList will be saved, as specified, for
1797e7cd458SMauro Carvalho Chehabuse at file creation time.  When a task allocates a file in the file
1807e7cd458SMauro Carvalho Chehabsystem, the mount option memory policy will be applied with a NodeList,
1817e7cd458SMauro Carvalho Chehabif any, modified by the calling task's cpuset constraints
1827e7cd458SMauro Carvalho Chehab[See Documentation/admin-guide/cgroup-v1/cpusets.rst] and any optional flags,
1837e7cd458SMauro Carvalho Chehablisted below.  If the resulting NodeLists is the empty set, the effective
1847e7cd458SMauro Carvalho Chehabmemory policy for the file will revert to "default" policy.
1857e7cd458SMauro Carvalho Chehab
1867e7cd458SMauro Carvalho ChehabNUMA memory allocation policies have optional flags that can be used in
1877e7cd458SMauro Carvalho Chehabconjunction with their modes.  These optional flags can be specified
1887e7cd458SMauro Carvalho Chehabwhen tmpfs is mounted by appending them to the mode before the NodeList.
1897e7cd458SMauro Carvalho ChehabSee Documentation/admin-guide/mm/numa_memory_policy.rst for a list of
1907e7cd458SMauro Carvalho Chehaball available memory allocation policy mode flags and their effect on
1917e7cd458SMauro Carvalho Chehabmemory policy.
1927e7cd458SMauro Carvalho Chehab
1937e7cd458SMauro Carvalho Chehab::
1947e7cd458SMauro Carvalho Chehab
1957e7cd458SMauro Carvalho Chehab	=static		is equivalent to	MPOL_F_STATIC_NODES
1967e7cd458SMauro Carvalho Chehab	=relative	is equivalent to	MPOL_F_RELATIVE_NODES
1977e7cd458SMauro Carvalho Chehab
1987e7cd458SMauro Carvalho ChehabFor example, mpol=bind=static:NodeList, is the equivalent of an
1997e7cd458SMauro Carvalho Chehaballocation policy of MPOL_BIND | MPOL_F_STATIC_NODES.
2007e7cd458SMauro Carvalho Chehab
2017e7cd458SMauro Carvalho ChehabNote that trying to mount a tmpfs with an mpol option will fail if the
2027e7cd458SMauro Carvalho Chehabrunning kernel does not support NUMA; and will fail if its nodelist
2037e7cd458SMauro Carvalho Chehabspecifies a node which is not online.  If your system relies on that
2047e7cd458SMauro Carvalho Chehabtmpfs being mounted, but from time to time runs a kernel built without
2057e7cd458SMauro Carvalho ChehabNUMA capability (perhaps a safe recovery kernel), or with fewer nodes
2067e7cd458SMauro Carvalho Chehabonline, then it is advisable to omit the mpol option from automatic
2077e7cd458SMauro Carvalho Chehabmount options.  It can be added later, when the tmpfs is already mounted
2087e7cd458SMauro Carvalho Chehabon MountPoint, by 'mount -o remount,mpol=Policy:NodeList MountPoint'.
2097e7cd458SMauro Carvalho Chehab
2107e7cd458SMauro Carvalho Chehab
2117e7cd458SMauro Carvalho ChehabTo specify the initial root directory you can use the following mount
2127e7cd458SMauro Carvalho Chehaboptions:
2137e7cd458SMauro Carvalho Chehab
2147e7cd458SMauro Carvalho Chehab====	==================================
2157e7cd458SMauro Carvalho Chehabmode	The permissions as an octal number
2167e7cd458SMauro Carvalho Chehabuid	The user id
2177e7cd458SMauro Carvalho Chehabgid	The group id
2187e7cd458SMauro Carvalho Chehab====	==================================
2197e7cd458SMauro Carvalho Chehab
2207e7cd458SMauro Carvalho ChehabThese options do not have any effect on remount. You can change these
2217e7cd458SMauro Carvalho Chehabparameters with chmod(1), chown(1) and chgrp(1) on a mounted filesystem.
2227e7cd458SMauro Carvalho Chehab
2237e7cd458SMauro Carvalho Chehab
224ea3271f7SChris Downtmpfs has a mount option to select whether it will wrap at 32- or 64-bit inode
225ea3271f7SChris Downnumbers:
226ea3271f7SChris Down
227ea3271f7SChris Down=======   ========================
228ea3271f7SChris Downinode64   Use 64-bit inode numbers
229ea3271f7SChris Downinode32   Use 32-bit inode numbers
230ea3271f7SChris Down=======   ========================
231ea3271f7SChris Down
232ea3271f7SChris DownOn a 32-bit kernel, inode32 is implicit, and inode64 is refused at mount time.
233ea3271f7SChris DownOn a 64-bit kernel, CONFIG_TMPFS_INODE64 sets the default.  inode64 avoids the
234ea3271f7SChris Downpossibility of multiple files with the same inode number on a single device;
235ea3271f7SChris Downbut risks glibc failing with EOVERFLOW once 33-bit inode numbers are reached -
236ea3271f7SChris Downif a long-lived tmpfs is accessed by 32-bit applications so ancient that
237ea3271f7SChris Downopening a file larger than 2GiB fails with EINVAL.
238ea3271f7SChris Down
239ea3271f7SChris Down
2407e7cd458SMauro Carvalho ChehabSo 'mount -t tmpfs -o size=10G,nr_inodes=10k,mode=700 tmpfs /mytmpfs'
2417e7cd458SMauro Carvalho Chehabwill give you tmpfs instance on /mytmpfs which can allocate 10GB
2427e7cd458SMauro Carvalho ChehabRAM/SWAP in 10240 inodes and it is only accessible by root.
2437e7cd458SMauro Carvalho Chehab
2447e7cd458SMauro Carvalho Chehab
2457e7cd458SMauro Carvalho Chehab:Author:
2467e7cd458SMauro Carvalho Chehab   Christoph Rohland <cr@sap.com>, 1.12.01
2477e7cd458SMauro Carvalho Chehab:Updated:
2487e7cd458SMauro Carvalho Chehab   Hugh Dickins, 4 June 2007
2497e7cd458SMauro Carvalho Chehab:Updated:
2507e7cd458SMauro Carvalho Chehab   KOSAKI Motohiro, 16 Mar 2010
251ea3271f7SChris Down:Updated:
252ea3271f7SChris Down   Chris Down, 13 July 2020
253