| 1 |
.\" Copyright 1992, Drew Eckhardt <[email protected]> |
| 2 |
.\" Copyright, the authors of the Linux man-pages project |
| 3 |
.\" |
| 4 |
.\" SPDX-License-Identifier: Linux-man-pages-copyleft |
| 5 |
.\" |
| 6 |
.\" FIXME Linux 3.11 added SWAP_FLAG_DISCARD_ONCE and SWAP_FLAG_DISCARD_PAGES |
| 7 |
.\" commit dcf6b7ddd7df8965727746f89c59229b23180e5a |
| 8 |
.\" Author: Rafael Aquini <[email protected]> |
| 9 |
.\" Date: Wed Jul 3 15:02:46 2013 -0700 |
| 10 |
.\" |
| 11 |
.TH swapon 2 2026-02-08 "Linux man-pages 6.18" |
| 12 |
.SH NAME |
| 13 |
swapon, swapoff \- start/stop swapping to file/device |
| 14 |
.SH LIBRARY |
| 15 |
Standard C library |
| 16 |
.RI ( libc ,\~ \-lc ) |
| 17 |
.SH SYNOPSIS |
| 18 |
.nf |
| 19 |
.B #include <sys/swap.h> |
| 20 |
.P |
| 21 |
.BI "int swapon(const char *" path ", int " swapflags ); |
| 22 |
.BI "int swapoff(const char *" path ); |
| 23 |
.fi |
| 24 |
.SH DESCRIPTION |
| 25 |
.BR swapon () |
| 26 |
sets the swap area to the file or block device specified by |
| 27 |
.IR path . |
| 28 |
.BR swapoff () |
| 29 |
stops swapping to the file or block device specified by |
| 30 |
.IR path . |
| 31 |
.P |
| 32 |
If the |
| 33 |
.B SWAP_FLAG_PREFER |
| 34 |
flag is specified in the |
| 35 |
.BR swapon () |
| 36 |
.I swapflags |
| 37 |
argument, the new swap area will have a higher priority than default. |
| 38 |
The priority is encoded within |
| 39 |
.I swapflags |
| 40 |
as: |
| 41 |
.P |
| 42 |
.in +4n |
| 43 |
.EX |
| 44 |
.I "(prio << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK" |
| 45 |
.EE |
| 46 |
.in |
| 47 |
.P |
| 48 |
If the |
| 49 |
.B SWAP_FLAG_DISCARD |
| 50 |
flag is specified in the |
| 51 |
.BR swapon () |
| 52 |
.I swapflags |
| 53 |
argument, freed swap pages will be discarded before they are reused, |
| 54 |
if the swap device supports the discard or trim operation. |
| 55 |
(This may improve performance on some Solid State Devices, |
| 56 |
but often it does not.) |
| 57 |
See also NOTES. |
| 58 |
.P |
| 59 |
These functions may be used only by a privileged process (one having the |
| 60 |
.B CAP_SYS_ADMIN |
| 61 |
capability). |
| 62 |
.SS Priority |
| 63 |
Each swap area has a priority, either high or low. |
| 64 |
The default priority is low. |
| 65 |
Within the low-priority areas, |
| 66 |
newer areas are even lower priority than older areas. |
| 67 |
.P |
| 68 |
All priorities set with |
| 69 |
.I swapflags |
| 70 |
are high-priority, higher than default. |
| 71 |
They may have any nonnegative value chosen by the caller. |
| 72 |
Higher numbers mean higher priority. |
| 73 |
.P |
| 74 |
Swap pages are allocated from areas in priority order, |
| 75 |
highest priority first. |
| 76 |
For areas with different priorities, |
| 77 |
a higher-priority area is exhausted before using a lower-priority area. |
| 78 |
If two or more areas have the same priority, |
| 79 |
and it is the highest priority available, |
| 80 |
pages are allocated on a round-robin basis between them. |
| 81 |
.P |
| 82 |
As of Linux 1.3.6, the kernel usually follows these rules, |
| 83 |
but there are exceptions. |
| 84 |
.SH RETURN VALUE |
| 85 |
On success, zero is returned. |
| 86 |
On error, \-1 is returned, and |
| 87 |
.I errno |
| 88 |
is set to indicate the error. |
| 89 |
.SH ERRORS |
| 90 |
.TP |
| 91 |
.B EBUSY |
| 92 |
(for |
| 93 |
.BR swapon ()) |
| 94 |
The specified |
| 95 |
.I path |
| 96 |
is already being used as a swap area. |
| 97 |
.TP |
| 98 |
.B EINVAL |
| 99 |
The file |
| 100 |
.I path |
| 101 |
exists, but refers neither to a regular file nor to a block device; |
| 102 |
.TP |
| 103 |
.B EINVAL |
| 104 |
.RB ( swapon ()) |
| 105 |
The indicated path does not contain a valid swap signature or |
| 106 |
resides on an in-memory filesystem such as |
| 107 |
.BR tmpfs (5). |
| 108 |
.TP |
| 109 |
.BR EINVAL " (since Linux 3.4)" |
| 110 |
.RB ( swapon ()) |
| 111 |
An invalid flag value was specified in |
| 112 |
.IR swapflags . |
| 113 |
.TP |
| 114 |
.B EINVAL |
| 115 |
.RB ( swapoff ()) |
| 116 |
.I path |
| 117 |
is not currently a swap area. |
| 118 |
.TP |
| 119 |
.B ENFILE |
| 120 |
The system-wide limit on the total number of open files has been reached. |
| 121 |
.TP |
| 122 |
.B ENOENT |
| 123 |
The file |
| 124 |
.I path |
| 125 |
does not exist. |
| 126 |
.TP |
| 127 |
.B ENOMEM |
| 128 |
The system has insufficient memory to start swapping. |
| 129 |
.TP |
| 130 |
.B EPERM |
| 131 |
The caller does not have the |
| 132 |
.B CAP_SYS_ADMIN |
| 133 |
capability. |
| 134 |
Alternatively, the maximum number of swap files are already in use; |
| 135 |
see NOTES below. |
| 136 |
.SH STANDARDS |
| 137 |
Linux. |
| 138 |
.SH HISTORY |
| 139 |
The |
| 140 |
.I swapflags |
| 141 |
argument was introduced in Linux 1.3.2. |
| 142 |
.SH NOTES |
| 143 |
The partition or path must be prepared with |
| 144 |
.BR mkswap (8). |
| 145 |
.P |
| 146 |
There is an upper limit on the number of swap files that may be used, |
| 147 |
defined by the kernel constant |
| 148 |
.BR MAX_SWAPFILES . |
| 149 |
Before Linux 2.4.10, |
| 150 |
.B MAX_SWAPFILES |
| 151 |
has the value 8; |
| 152 |
since Linux 2.4.10, it has the value 32. |
| 153 |
Since Linux 2.6.18, the limit is decreased by 2 (thus 30), |
| 154 |
since Linux 5.19, the limit is decreased by 3 (thus: 29) |
| 155 |
if the kernel is built with the |
| 156 |
.B CONFIG_MIGRATION |
| 157 |
option |
| 158 |
(which reserves two swap table entries for the page migration features of |
| 159 |
.BR mbind (2) |
| 160 |
and |
| 161 |
.BR migrate_pages (2)). |
| 162 |
Since Linux 2.6.32, the limit is further decreased by 1 |
| 163 |
if the kernel is built with the |
| 164 |
.B CONFIG_MEMORY_FAILURE |
| 165 |
option. |
| 166 |
Since Linux 5.14, the limit is further decreased by 4 |
| 167 |
if the kernel is built with the |
| 168 |
.B CONFIG_DEVICE_PRIVATE |
| 169 |
option. |
| 170 |
Since Linux 5.19, the limit is further decreased by 1 |
| 171 |
if the kernel is built with the |
| 172 |
.B CONFIG_PTE_MARKER |
| 173 |
option. |
| 174 |
.P |
| 175 |
Discard of swap pages was introduced in Linux 2.6.29, |
| 176 |
then made conditional |
| 177 |
on the |
| 178 |
.B SWAP_FLAG_DISCARD |
| 179 |
flag in Linux 2.6.36, |
| 180 |
.\" To be precise: 2.6.35.5 |
| 181 |
which still discards the |
| 182 |
entire swap area when |
| 183 |
.BR swapon () |
| 184 |
is called, even if that flag bit is not set. |
| 185 |
.SH SEE ALSO |
| 186 |
.BR mkswap (8), |
| 187 |
.BR swapoff (8), |
| 188 |
.BR swapon (8) |