swapon(2)
๐ swapon(2) ๐
okay bestie, welcome to the System Calls Manual, this one's swapon(2) โ literally the syscall that starts and stops your swap. putting the whole manual-name thing in the intro bc we're cute like that, mwah โจ
โจ NAME โจ
swapon, swapoff โ start/stop swapping to file/device ๐ธ
๐ LIBRARY ๐
Standard C library (libc, -lc) โ the classic, the icon, the one you always link ๐ซถ
๐ง SYNOPSIS ๐ง
#include <sys/swap.h>
int swapon(const char *path, int swapflags);
int swapoff(const char *path);
๐ DESCRIPTION ๐
okay so swapon() sets the swap area to the file or block device that path points at. and swapoff() does the opposite โ it stops swapping to the file or block device that path points at. that's it, that's the whole vibe ๐
now, the fun part. if the SWAP_FLAG_PREFER flag is chillin' in the swapon() swapflags argument, your brand-new swap area gets a higher priority than the default. the priority is encoded inside swapflags like this:
(prio << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK
and if SWAP_FLAG_DISCARD is sitting in the swapon() swapflags argument, then freed swap pages get discarded before they're reused โ but only if the swap device actually supports the discard or trim operation. (this may improve performance on some Solid State Devices, but often it does not.) see also NOTES, bestie, iykyk ๐
๐จ these functions may be used only by a privileged process โ meaning one that has the CAP_SYS_ADMIN capability. sorry to the unprivileged girlies, this one is not for you ๐ญ
๐ Priority ๐
every swap area has a priority, and it's either high or low. the default is low. and within the low-priority areas? newer areas are even lower priority than older areas. like, the new girl is literally at the bottom of the group chat, no cap ๐
all priorities set with swapflags are high-priority โ higher than default. they can be any nonnegative value the caller wants. bigger number = higher priority, periodt ๐ฅ
swap pages get allocated from areas in priority order, highest priority first. for areas with different priorities, a higher-priority area gets fully exhausted before a lower-priority area is even touched. but if two or more areas have the same priority and it's the highest one available, then pages get allocated round-robin between them. sharing is caring ๐
as of Linux 1.3.6, the kernel usually follows these rules โ but there are exceptions. love that for us ๐
๐ซถ RETURN VALUE ๐ซถ
on success, zero is returned. ๐ on error, -1 is returned, and errno is set to tell you what went wrong. ๐ญ
๐ ERRORS ๐
EBUSYโ (forswapon()) thatpathis already being used as a swap area. it's taken, bestie, move on ๐EINVALโ the filepathexists, but it refers to neither a regular file nor a block device. it's giving... neither โEINVALโ (swapon()) the indicatedpathdoesn't contain a valid swap signature, or it lives on an in-memory filesystem such astmpfs(5).EINVAL(since Linux 3.4) โ (swapon()) an invalid flag value was specified inswapflags.EINVALโ (swapoff()) thatpathis not currently a swap area.ENFILEโ the system-wide limit on the total number of open files has been reached. too many open files, the ultimate group-chat-overload energy ๐ตENOENTโ the filepathdoes not exist. she's not here, girl ๐ENOMEMโ the system has insufficient memory to start swapping. the audacity โจEPERMโ the caller doesn't have theCAP_SYS_ADMINcapability. alternatively, the maximum number of swap files are already in use; see NOTES below.
๐ STANDARDS ๐
Linux. that's the standard. that's the whole standard ๐ง
๐ฆ HISTORY ๐ฆ
the swapflags argument was introduced in Linux 1.3.2. a little vintage moment for you โจ
๐ NOTES ๐
the partition or path must be prepared with mkswap(8) first. no skipping steps, bestie ๐ง
there's an upper limit on how many swap files you can use, and it's defined by the kernel constant MAX_SWAPFILES. so here's the whole timeline, and yes i'm listing every single one bc we don't do half-info here:
- Before Linux 2.4.10,
MAX_SWAPFILEShas the value 8. - Since Linux 2.4.10, it has the value 32.
- Since Linux 2.6.18, the limit is decreased by 2 (thus 30).
- Since Linux 5.19, the limit is decreased by 3 (thus: 29) if the kernel is built with the
CONFIG_MIGRATIONoption (which reserves two swap table entries for the page migration features ofmbind(2)andmigrate_pages(2)). - Since Linux 2.6.32, the limit is further decreased by 1 if the kernel is built with the
CONFIG_MEMORY_FAILUREoption. - Since Linux 5.14, the limit is further decreased by 4 if the kernel is built with the
CONFIG_DEVICE_PRIVATEoption. - Since Linux 5.19, the limit is further decreased by 1 if the kernel is built with the
CONFIG_PTE_MARKERoption.
like, the math is mathing but it's also mathing against you, lowkey ๐๐ญ
discard of swap pages was introduced in Linux 2.6.29, then made conditional on the SWAP_FLAG_DISCARD flag in Linux 2.6.36 โ which still discards the entire swap area when swapon() is called, even if that flag bit is not set. yeah. even when you didn't ask. iconic behaviour honestly ๐
๐ฅ SEE ALSO ๐ฅ
mkswap(8), swapoff(8), swapon(8)
๐ that's the whole page bestie!! this was ripped straight out of Linux man-pages 6.20, dated 2026-02-08 โ go forth and swap responsibly, mwah ๐โจ๐ฆ
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: Linux-man-pages-copyleft
- Copyright 1992, Drew Eckhardt <drew@cs.colorado.edu>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms