Fundamentals 26 min read

Can a New Swap Table Cut Linux Memory Overhead and Boost Performance by 20%?

This article analyzes the multi‑year modernization of the Linux kernel swap subsystem, detailing the introduction of the swap table, removal of the swap map, virtual swap concepts, and tiered swap designs, and shows how these changes can reduce metadata overhead by up to 30% and improve throughput by 5‑20%.

Tencent Cloud Developer
Tencent Cloud Developer
Tencent Cloud Developer
Can a New Swap Table Cut Linux Memory Overhead and Boost Performance by 20%?

Background

The Linux kernel swap subsystem manages anonymous pages that have no permanent backing store. Each swap device is described by struct swap_info_struct and pages are indexed with swp_entry_t, which encodes the device type and the slot offset.

Stage 1 – Swap Table (Linux 6.18)

To reduce metadata overhead and lock contention, the long‑standing XArray swapper_spaces was replaced by a simple array of unsigned long entries, called the swap table . Each entry represents the state of a swap slot (empty, occupied by a folio, or a shadow entry). The table is allocated lazily: a single physical page backs the array and additional pages are added only when a cluster is actually used.

typedef struct { unsigned long val; } swp_entry_t;
atomic_long_t __rcu *table; /* one page per cluster */

This change cuts per‑slot memory usage by roughly 30 % for a 1 TB swap file (≈256 MiB saved) and eliminates XArray look‑ups, yielding 5‑20 % performance improvements in benchmarks and build‑time tests.

Stage 2 – Swap Map Removal

The historic swap_map was a unsigned char * array storing a reference count for every slot. Special bits such as SWAP_HAS_CACHE (0x40) were used for synchronization and to distinguish allocated but unreferenced slots. By moving reference‑count handling into the swap table, the map can be eliminated, saving another ~512 MiB for a 1 TB swap device and simplifying the code path for swap‑cache management.

unsigned char *swap_map; /* vmalloc'ed array of usage counts */

Virtual Swap Space Proposals

Two designs aim to make swap space optional and more flexible.

Meta’s approach introduces a mandatory virtual‑swap layer represented by struct swp_desc . It abstracts the underlying device, allowing pages to migrate between devices without pre‑allocating slots.

TencentOS proposes a “Virtual GhostSwap” that uses an XArray‑backed dynamic swap file, enabling near‑petabyte virtual swap with minimal impact on existing workloads.

struct swp_desc {
    union { swp_slot_t slot; struct zswap_entry *zswap_entry; };
    union { struct folio *swap_cache; void *shadow; };
    unsigned int swap_count;
    unsigned short memcgid:16;
    bool in_swapcache:1;
    enum swap_type type:2;
};

The structure supports four slot types (real swapfile slot, zero page, zswap entry, and resident folio) and consolidates metadata that previously lived in both the swap map and swap cache.

Swap Tiers

Patch set by Youngjun Park adds tiered swap devices. High‑performance media form the “fast” tier for latency‑sensitive workloads, while slower devices form lower tiers. Cgroup hooks let administrators bind specific process groups to particular tiers, providing fine‑grained control over swap latency.

Future Directions (Stage 3)

The next phase targets complete removal of the swap map and integration of memory‑controller limits into the swap table. Ongoing discussions aim to merge the virtual‑swap and tiered‑swap concepts, further reducing memory pressure and improving scalability.

References

Modernizing swapping: introducing the swap table – https://lwn.net/Articles/1056405/

Modernizing swapping: the end of the swap map – https://lwn.net/Articles/1057102/

Modernizing swapping: virtual swap spaces – https://lwn.net/Articles/1059201/

Architecture‑specific swap entry layout (x86_64) – https://elixir.bootlin.com/linux/v6.18.6/source/arch/x86/include/asm/pgtable_64.h#L183

zswap documentation – https://docs.kernel.org/admin-guide/mm/zswap.html

memory managementKernelLinuxSwap
Tencent Cloud Developer
Written by

Tencent Cloud Developer

Official Tencent Cloud community account that brings together developers, shares practical tech insights, and fosters an influential tech exchange community.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.