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