Skip to content

swapon

Intro

swapon - allow a process to start using a designated swap area

Description

The swapon() system call allows a process to start using a designated swap area on a file or a device that is setup for paging and/or swapping. Both regular files and block special files can be used for this purpose.

When path is NULL, the kernel will use the first entry in the /etc/fstab as the default swap area. If the swap flag is passed as NULL, the kernel will default to 0. swapon() provides an easy way to enable swap on a single partition at a time.

Some of the advantages of using swapon() are that the swap area can be easily re-enabled on reboot, the memory use can be easily monitored with the free command, and it can be used on freshly installed systems. However, one of the drawbacks is that the system will still be left with active memory pages until they are paged out. This could lead to excessive paging activity during bootup, leading to degraded system performance.

Arguments

  • path:const char*[K] - Path to a block special device or a regular file that will be used for swapping. If set to NULL, then the first entry in the /etc/fstab will be used.
  • swapflags:int[K] - Flags which indicate what type of swap to use. The allowed flags are:
  • SWAP_FLAG_PREFER - Only use the specified swap file if it's better than the existing one.
  • SWAP_FLAG_DISCARD - Discard pages from the swap file as soon as they are used.

Available Tags

  • K - Originated from kernel-space.

Hooks

do_swapon

Type

Kprobe

Purpose

Hooking do_swapon allows tracing when the system starts using a designated swap area on a file or a device.

sys_swapon

Type

Kprobe

Purpose

Hooking sys_swapon provides a way to monitor when a user process attempts to start using a designated swap area.

Example Use Case

Swapon may be used when a user needs to add a new swap area to an existing system. If the file or block special device is set up correctly, the user can call swapon to enable the swap area.

Issues

Due to the way the kernel manages swap, it is possible that this system call may return an error (EINVAL) if the swap area specified is already in use.

The swapon() system call is related to the swaponall() system call, which is used to enable all swap areas specified in the /etc/fstab. It is also related to the swapoff() system call, which is used to disable a swap area.

This document was automatically generated by OpenAI and needs review. It might not be accurate and might contain errors. The authors of Tracee recommend that the user reads the "events.go" source file to understand the events and their arguments better.