xref: /OK3568_Linux_fs/kernel/Documentation/filesystems/tmpfs.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun.. SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun
3*4882a593Smuzhiyun=====
4*4882a593SmuzhiyunTmpfs
5*4882a593Smuzhiyun=====
6*4882a593Smuzhiyun
7*4882a593SmuzhiyunTmpfs is a file system which keeps all files in virtual memory.
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun
10*4882a593SmuzhiyunEverything in tmpfs is temporary in the sense that no files will be
11*4882a593Smuzhiyuncreated on your hard drive. If you unmount a tmpfs instance,
12*4882a593Smuzhiyuneverything stored therein is lost.
13*4882a593Smuzhiyun
14*4882a593Smuzhiyuntmpfs puts everything into the kernel internal caches and grows and
15*4882a593Smuzhiyunshrinks to accommodate the files it contains and is able to swap
16*4882a593Smuzhiyununneeded pages out to swap space. It has maximum size limits which can
17*4882a593Smuzhiyunbe adjusted on the fly via 'mount -o remount ...'
18*4882a593Smuzhiyun
19*4882a593SmuzhiyunIf you compare it to ramfs (which was the template to create tmpfs)
20*4882a593Smuzhiyunyou gain swapping and limit checking. Another similar thing is the RAM
21*4882a593Smuzhiyundisk (/dev/ram*), which simulates a fixed size hard disk in physical
22*4882a593SmuzhiyunRAM, where you have to create an ordinary filesystem on top. Ramdisks
23*4882a593Smuzhiyuncannot swap and you do not have the possibility to resize them.
24*4882a593Smuzhiyun
25*4882a593SmuzhiyunSince tmpfs lives completely in the page cache and on swap, all tmpfs
26*4882a593Smuzhiyunpages will be shown as "Shmem" in /proc/meminfo and "Shared" in
27*4882a593Smuzhiyunfree(1). Notice that these counters also include shared memory
28*4882a593Smuzhiyun(shmem, see ipcs(1)). The most reliable way to get the count is
29*4882a593Smuzhiyunusing df(1) and du(1).
30*4882a593Smuzhiyun
31*4882a593Smuzhiyuntmpfs has the following uses:
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun1) There is always a kernel internal mount which you will not see at
34*4882a593Smuzhiyun   all. This is used for shared anonymous mappings and SYSV shared
35*4882a593Smuzhiyun   memory.
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun   This mount does not depend on CONFIG_TMPFS. If CONFIG_TMPFS is not
38*4882a593Smuzhiyun   set, the user visible part of tmpfs is not build. But the internal
39*4882a593Smuzhiyun   mechanisms are always present.
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun2) glibc 2.2 and above expects tmpfs to be mounted at /dev/shm for
42*4882a593Smuzhiyun   POSIX shared memory (shm_open, shm_unlink). Adding the following
43*4882a593Smuzhiyun   line to /etc/fstab should take care of this::
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun	tmpfs	/dev/shm	tmpfs	defaults	0 0
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun   Remember to create the directory that you intend to mount tmpfs on
48*4882a593Smuzhiyun   if necessary.
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun   This mount is _not_ needed for SYSV shared memory. The internal
51*4882a593Smuzhiyun   mount is used for that. (In the 2.3 kernel versions it was
52*4882a593Smuzhiyun   necessary to mount the predecessor of tmpfs (shm fs) to use SYSV
53*4882a593Smuzhiyun   shared memory)
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun3) Some people (including me) find it very convenient to mount it
56*4882a593Smuzhiyun   e.g. on /tmp and /var/tmp and have a big swap partition. And now
57*4882a593Smuzhiyun   loop mounts of tmpfs files do work, so mkinitrd shipped by most
58*4882a593Smuzhiyun   distributions should succeed with a tmpfs /tmp.
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun4) And probably a lot more I do not know about :-)
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun
63*4882a593Smuzhiyuntmpfs has three mount options for sizing:
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun=========  ============================================================
66*4882a593Smuzhiyunsize       The limit of allocated bytes for this tmpfs instance. The
67*4882a593Smuzhiyun           default is half of your physical RAM without swap. If you
68*4882a593Smuzhiyun           oversize your tmpfs instances the machine will deadlock
69*4882a593Smuzhiyun           since the OOM handler will not be able to free that memory.
70*4882a593Smuzhiyunnr_blocks  The same as size, but in blocks of PAGE_SIZE.
71*4882a593Smuzhiyunnr_inodes  The maximum number of inodes for this instance. The default
72*4882a593Smuzhiyun           is half of the number of your physical RAM pages, or (on a
73*4882a593Smuzhiyun           machine with highmem) the number of lowmem RAM pages,
74*4882a593Smuzhiyun           whichever is the lower.
75*4882a593Smuzhiyun=========  ============================================================
76*4882a593Smuzhiyun
77*4882a593SmuzhiyunThese parameters accept a suffix k, m or g for kilo, mega and giga and
78*4882a593Smuzhiyuncan be changed on remount.  The size parameter also accepts a suffix %
79*4882a593Smuzhiyunto limit this tmpfs instance to that percentage of your physical RAM:
80*4882a593Smuzhiyunthe default, when neither size nor nr_blocks is specified, is size=50%
81*4882a593Smuzhiyun
82*4882a593SmuzhiyunIf nr_blocks=0 (or size=0), blocks will not be limited in that instance;
83*4882a593Smuzhiyunif nr_inodes=0, inodes will not be limited.  It is generally unwise to
84*4882a593Smuzhiyunmount with such options, since it allows any user with write access to
85*4882a593Smuzhiyunuse up all the memory on the machine; but enhances the scalability of
86*4882a593Smuzhiyunthat instance in a system with many cpus making intensive use of it.
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun
89*4882a593Smuzhiyuntmpfs has a mount option to set the NUMA memory allocation policy for
90*4882a593Smuzhiyunall files in that instance (if CONFIG_NUMA is enabled) - which can be
91*4882a593Smuzhiyunadjusted on the fly via 'mount -o remount ...'
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun======================== ==============================================
94*4882a593Smuzhiyunmpol=default             use the process allocation policy
95*4882a593Smuzhiyun                         (see set_mempolicy(2))
96*4882a593Smuzhiyunmpol=prefer:Node         prefers to allocate memory from the given Node
97*4882a593Smuzhiyunmpol=bind:NodeList       allocates memory only from nodes in NodeList
98*4882a593Smuzhiyunmpol=interleave          prefers to allocate from each node in turn
99*4882a593Smuzhiyunmpol=interleave:NodeList allocates from each node of NodeList in turn
100*4882a593Smuzhiyunmpol=local		 prefers to allocate memory from the local node
101*4882a593Smuzhiyun======================== ==============================================
102*4882a593Smuzhiyun
103*4882a593SmuzhiyunNodeList format is a comma-separated list of decimal numbers and ranges,
104*4882a593Smuzhiyuna range being two hyphen-separated decimal numbers, the smallest and
105*4882a593Smuzhiyunlargest node numbers in the range.  For example, mpol=bind:0-3,5,7,9-15
106*4882a593Smuzhiyun
107*4882a593SmuzhiyunA memory policy with a valid NodeList will be saved, as specified, for
108*4882a593Smuzhiyunuse at file creation time.  When a task allocates a file in the file
109*4882a593Smuzhiyunsystem, the mount option memory policy will be applied with a NodeList,
110*4882a593Smuzhiyunif any, modified by the calling task's cpuset constraints
111*4882a593Smuzhiyun[See Documentation/admin-guide/cgroup-v1/cpusets.rst] and any optional flags,
112*4882a593Smuzhiyunlisted below.  If the resulting NodeLists is the empty set, the effective
113*4882a593Smuzhiyunmemory policy for the file will revert to "default" policy.
114*4882a593Smuzhiyun
115*4882a593SmuzhiyunNUMA memory allocation policies have optional flags that can be used in
116*4882a593Smuzhiyunconjunction with their modes.  These optional flags can be specified
117*4882a593Smuzhiyunwhen tmpfs is mounted by appending them to the mode before the NodeList.
118*4882a593SmuzhiyunSee Documentation/admin-guide/mm/numa_memory_policy.rst for a list of
119*4882a593Smuzhiyunall available memory allocation policy mode flags and their effect on
120*4882a593Smuzhiyunmemory policy.
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun::
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun	=static		is equivalent to	MPOL_F_STATIC_NODES
125*4882a593Smuzhiyun	=relative	is equivalent to	MPOL_F_RELATIVE_NODES
126*4882a593Smuzhiyun
127*4882a593SmuzhiyunFor example, mpol=bind=static:NodeList, is the equivalent of an
128*4882a593Smuzhiyunallocation policy of MPOL_BIND | MPOL_F_STATIC_NODES.
129*4882a593Smuzhiyun
130*4882a593SmuzhiyunNote that trying to mount a tmpfs with an mpol option will fail if the
131*4882a593Smuzhiyunrunning kernel does not support NUMA; and will fail if its nodelist
132*4882a593Smuzhiyunspecifies a node which is not online.  If your system relies on that
133*4882a593Smuzhiyuntmpfs being mounted, but from time to time runs a kernel built without
134*4882a593SmuzhiyunNUMA capability (perhaps a safe recovery kernel), or with fewer nodes
135*4882a593Smuzhiyunonline, then it is advisable to omit the mpol option from automatic
136*4882a593Smuzhiyunmount options.  It can be added later, when the tmpfs is already mounted
137*4882a593Smuzhiyunon MountPoint, by 'mount -o remount,mpol=Policy:NodeList MountPoint'.
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun
140*4882a593SmuzhiyunTo specify the initial root directory you can use the following mount
141*4882a593Smuzhiyunoptions:
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun====	==================================
144*4882a593Smuzhiyunmode	The permissions as an octal number
145*4882a593Smuzhiyunuid	The user id
146*4882a593Smuzhiyungid	The group id
147*4882a593Smuzhiyun====	==================================
148*4882a593Smuzhiyun
149*4882a593SmuzhiyunThese options do not have any effect on remount. You can change these
150*4882a593Smuzhiyunparameters with chmod(1), chown(1) and chgrp(1) on a mounted filesystem.
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun
153*4882a593Smuzhiyuntmpfs has a mount option to select whether it will wrap at 32- or 64-bit inode
154*4882a593Smuzhiyunnumbers:
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun=======   ========================
157*4882a593Smuzhiyuninode64   Use 64-bit inode numbers
158*4882a593Smuzhiyuninode32   Use 32-bit inode numbers
159*4882a593Smuzhiyun=======   ========================
160*4882a593Smuzhiyun
161*4882a593SmuzhiyunOn a 32-bit kernel, inode32 is implicit, and inode64 is refused at mount time.
162*4882a593SmuzhiyunOn a 64-bit kernel, CONFIG_TMPFS_INODE64 sets the default.  inode64 avoids the
163*4882a593Smuzhiyunpossibility of multiple files with the same inode number on a single device;
164*4882a593Smuzhiyunbut risks glibc failing with EOVERFLOW once 33-bit inode numbers are reached -
165*4882a593Smuzhiyunif a long-lived tmpfs is accessed by 32-bit applications so ancient that
166*4882a593Smuzhiyunopening a file larger than 2GiB fails with EINVAL.
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun
169*4882a593SmuzhiyunSo 'mount -t tmpfs -o size=10G,nr_inodes=10k,mode=700 tmpfs /mytmpfs'
170*4882a593Smuzhiyunwill give you tmpfs instance on /mytmpfs which can allocate 10GB
171*4882a593SmuzhiyunRAM/SWAP in 10240 inodes and it is only accessible by root.
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun:Author:
175*4882a593Smuzhiyun   Christoph Rohland <cr@sap.com>, 1.12.01
176*4882a593Smuzhiyun:Updated:
177*4882a593Smuzhiyun   Hugh Dickins, 4 June 2007
178*4882a593Smuzhiyun:Updated:
179*4882a593Smuzhiyun   KOSAKI Motohiro, 16 Mar 2010
180*4882a593Smuzhiyun:Updated:
181*4882a593Smuzhiyun   Chris Down, 13 July 2020
182