#include <sys/swap.h> int swapon(const char *path, int swapflags); int swapoff(const char *path);
swapon() の swapflags 引数に SWAP_FLAG_PREFER フラグが指定された場合、 新しいスワップ領域はデフォルトよりも高い優先度を持つ。 優先度は以下のように変換されて swapflags に指定する。
(prio << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK
SWAP_FLAG_DISCARD フラグが swapon() の swapflags 引数に指定された場合、 スワップデバイスが破棄 (discard) 操作や trim 操作をサポートしている場合には、 解放されたスワップページは再利用される前に破棄される (これにより、SSD (Solid State Device) によっては性能が向上することがあるが、 たいていは性能の向上はない)。 「注意」も参照のこと。
これらの関数は特権プロセス (CAP_SYS_ADMIN ケーパビリティ (capability) を持つプロセス) のみが使用できる。
swapflags が設定されたものは全て高い優先度となり、デフォルトよりも高い優先度を持つ。 使用者はそれらに負でない値が指定できる。 大きな数字は高い優先度を意味する。
高い優先度の領域から順にスワップページとして使用される。 より低い優先度の領域を使用する前により高い優先度の 領域を使い切る。もし二つ以上の領域が同じ優先度を持ち、 使える中で一番高い優先度であれば、それらのページは間で ラウンドロビン方式で配分される。
Linux 1.3.6 において、カーネルは通常はこれらの規則に従っている。 しかし例外も存在している。
There is an upper limit on the number of swap files that may be used, defined by the kernel constant MAX_SWAPFILES. Before Linux 2.4.10, MAX_SWAPFILES has 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_MIGRATION option (which reserves two swap table entries for the page migration features of mbind(2) and migrate_pages(2)). Since Linux 2.6.32, the limit is further decreased by 1 if the kernel is built with the CONFIG_MEMORY_FAILURE option. Since Linux 5.14, the limit is further decreased by 4 if the kernel is built with the CONFIG_DEVICE_PRIVATE option. Since Linux 5.19, the limit is further decreased by 1 if the kernel is built with the CONFIG_PTE_MARKER option.
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.