1SWAPON(2) Linux Programmer's Manual SWAPON(2)
2
3
4
6 swapon, swapoff - start/stop swapping to file/device
7
9 #include <sys/swap.h>
10
11 int swapon(const char *path, int swapflags);
12 int swapoff(const char *path);
13
15 swapon() sets the swap area to the file or block device specified by
16 path. swapoff() stops swapping to the file or block device specified
17 by path.
18
19 If the SWAP_FLAG_PREFER flag is specified in the swapon() swapflags ar‐
20 gument, the new swap area will have a higher priority than default.
21 The priority is encoded within swapflags as:
22
23 (prio << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK
24
25 If the SWAP_FLAG_DISCARD flag is specified in the swapon() swapflags
26 argument, freed swap pages will be discarded before they are reused, if
27 the swap device supports the discard or trim operation. (This may im‐
28 prove performance on some Solid State Devices, but often it does not.)
29 See also NOTES.
30
31 These functions may be used only by a privileged process (one having
32 the CAP_SYS_ADMIN capability).
33
34 Priority
35 Each swap area has a priority, either high or low. The default prior‐
36 ity is low. Within the low-priority areas, newer areas are even lower
37 priority than older areas.
38
39 All priorities set with swapflags are high-priority, higher than de‐
40 fault. They may have any nonnegative value chosen by the caller.
41 Higher numbers mean higher priority.
42
43 Swap pages are allocated from areas in priority order, highest priority
44 first. For areas with different priorities, a higher-priority area is
45 exhausted before using a lower-priority area. If two or more areas
46 have the same priority, and it is the highest priority available, pages
47 are allocated on a round-robin basis between them.
48
49 As of Linux 1.3.6, the kernel usually follows these rules, but there
50 are exceptions.
51
53 On success, zero is returned. On error, -1 is returned, and errno is
54 set to indicate the error.
55
57 EBUSY (for swapon()) The specified path is already being used as a
58 swap area.
59
60 EINVAL The file path exists, but refers neither to a regular file nor
61 to a block device;
62
63 EINVAL (swapon()) The indicated path does not contain a valid swap sig‐
64 nature or resides on an in-memory filesystem such as tmpfs(5).
65
66 EINVAL (since Linux 3.4)
67 (swapon()) An invalid flag value was specified in swapflags.
68
69 EINVAL (swapoff()) path is not currently a swap area.
70
71 ENFILE The system-wide limit on the total number of open files has been
72 reached.
73
74 ENOENT The file path does not exist.
75
76 ENOMEM The system has insufficient memory to start swapping.
77
78 EPERM The caller does not have the CAP_SYS_ADMIN capability. Alterna‐
79 tively, the maximum number of swap files are already in use; see
80 NOTES below.
81
83 These functions are Linux-specific and should not be used in programs
84 intended to be portable. The second swapflags argument was introduced
85 in Linux 1.3.2.
86
88 The partition or path must be prepared with mkswap(8).
89
90 There is an upper limit on the number of swap files that may be used,
91 defined by the kernel constant MAX_SWAPFILES. Before kernel 2.4.10,
92 MAX_SWAPFILES has the value 8; since kernel 2.4.10, it has the value
93 32. Since kernel 2.6.18, the limit is decreased by 2 (thus: 30) if the
94 kernel is built with the CONFIG_MIGRATION option (which reserves two
95 swap table entries for the page migration features of mbind(2) and mi‐
96 grate_pages(2)). Since kernel 2.6.32, the limit is further decreased
97 by 1 if the kernel is built with the CONFIG_MEMORY_FAILURE option.
98
99 Discard of swap pages was introduced in kernel 2.6.29, then made condi‐
100 tional on the SWAP_FLAG_DISCARD flag in kernel 2.6.36, which still dis‐
101 cards the entire swap area when swapon() is called, even if that flag
102 bit is not set.
103
105 mkswap(8), swapoff(8), swapon(8)
106
108 This page is part of release 5.12 of the Linux man-pages project. A
109 description of the project, information about reporting bugs, and the
110 latest version of this page, can be found at
111 https://www.kernel.org/doc/man-pages/.
112
113
114
115Linux 2021-03-22 SWAPON(2)