mm/swap_table.h
Source file repositories/reference/linux-study-clean/mm/swap_table.h
File Facts
- System
- Linux kernel
- Corpus path
mm/swap_table.h- Extension
.h- Size
- 10601 bytes
- Lines
- 370
- Domain
- Core OS
- Bucket
- Memory Management
- Inferred role
- Core OS: implementation source
- Status
- source implementation candidate
Why This File Exists
Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/rcupdate.hlinux/atomic.hswap.h
Detected Declarations
struct swap_tablestruct swap_memcg_tablefunction null_to_swp_tbfunction __count_to_swp_tbfunction __flags_to_swp_tbfunction pfn_to_swp_tbfunction folio_to_swp_tbfunction shadow_to_swp_tbfunction swp_tb_is_nullfunction swp_tb_is_foliofunction swp_tb_is_shadowfunction swp_tb_is_badfunction swp_tb_is_countablefunction __swp_tb_get_countfunction __swp_tb_get_flagsfunction swp_tb_get_countfunction __swp_tb_mk_countfunction __swap_table_setfunction __swap_table_xchgfunction __swap_table_getfunction swap_table_getfunction __swap_table_set_zerofunction __swap_table_test_zerofunction __swap_table_clear_zerofunction __swap_cgroup_setfunction __swap_cgroup_getfunction __swap_cgroup_clearfunction __swap_cgroup_setfunction __swap_cgroup_clear
Annotated Snippet
struct swap_table {
atomic_long_t entries[SWAPFILE_CLUSTER];
};
/* For storing memcg private id */
struct swap_memcg_table {
unsigned short id[SWAPFILE_CLUSTER];
};
#define SWP_TABLE_USE_PAGE (sizeof(struct swap_table) == PAGE_SIZE)
/*
* A swap table entry represents the status of a swap slot on a swap
* (physical or virtual) device. The swap table in each cluster is a
* 1:1 map of the swap slots in this cluster.
*
* Swap table entry type and bits layouts:
*
* NULL: |---------------- 0 ---------------| - Free slot
* Shadow: |SWAP_COUNT|Z|---- SHADOW_VAL ---|1| - Swapped out slot
* PFN: |SWAP_COUNT|Z|------ PFN -------|10| - Cached slot
* Pointer: |----------- Pointer ----------|100| - (Unused)
* Bad: |------------- 1 -------------|1000| - Bad slot
*
* COUNT is `SWP_TB_COUNT_BITS` long, Z is the `SWP_TB_ZERO_FLAG` bit,
* and together they form the `SWP_TB_FLAGS_BITS` wide flags field.
* Each entry is an atomic long.
*
* Usages:
*
* - NULL: Swap slot is unused, could be allocated.
*
* - Shadow: Swap slot is used and not cached (usually swapped out). It reuses
* the XA_VALUE format to be compatible with working set shadows. SHADOW_VAL
* part might be all 0 if the working shadow info is absent. In such a case,
* we still want to keep the shadow format as a placeholder.
*
* Memcg ID is embedded in SHADOW_VAL.
*
* - PFN: Swap slot is in use, and cached. Memcg info is recorded on the page
* struct.
*
* - Pointer: Unused yet. `0b100` is reserved for potential pointer usage
* because only the lower three bits can be used as a marker for 8 bytes
* aligned pointers.
*
* - Bad: Swap slot is reserved, protects swap header or holes on swap devices.
*/
/* NULL Entry, all 0 */
#define SWP_TB_NULL 0UL
/* Swapped out: shadow */
#define SWP_TB_SHADOW_MARK 0b1UL
/* Cached: PFN */
#define SWP_TB_PFN_BITS (SWAP_CACHE_PFN_BITS + SWAP_CACHE_PFN_MARK_BITS)
#define SWP_TB_PFN_MARK 0b10UL
#define SWP_TB_PFN_MARK_MASK (BIT(SWAP_CACHE_PFN_MARK_BITS) - 1)
/* Flags: For PFN or shadow, contains SWAP_COUNT, width changes */
#define SWP_TB_FLAGS_BITS min(5, BITS_PER_LONG - SWP_TB_PFN_BITS)
#define SWP_TB_COUNT_BITS (SWP_TB_FLAGS_BITS - SWAP_TABLE_HAS_ZEROFLAG)
#define SWP_TB_FLAGS_MASK (~((~0UL) >> SWP_TB_FLAGS_BITS))
#define SWP_TB_COUNT_MASK (~((~0UL) >> SWP_TB_COUNT_BITS))
#define SWP_TB_FLAGS_SHIFT (BITS_PER_LONG - SWP_TB_FLAGS_BITS)
#define SWP_TB_COUNT_SHIFT (BITS_PER_LONG - SWP_TB_COUNT_BITS)
#define SWP_TB_COUNT_MAX ((1 << SWP_TB_COUNT_BITS) - 1)
/* The first flag is zero bit (SWAP_TABLE_HAS_ZEROFLAG) */
#define SWP_TB_ZERO_FLAG BIT(BITS_PER_LONG - SWP_TB_FLAGS_BITS)
/* Bad slot: ends with 0b1000 and rests of bits are all 1 */
#define SWP_TB_BAD ((~0UL) << 3)
/* Macro for shadow offset calculation */
#define SWAP_COUNT_SHIFT SWP_TB_FLAGS_BITS
/*
* Helpers for casting one type of info into a swap table entry.
*/
static inline unsigned long null_to_swp_tb(void)
{
BUILD_BUG_ON(sizeof(unsigned long) != sizeof(atomic_long_t));
return 0;
}
static inline unsigned long __count_to_swp_tb(unsigned char count)
{
/*
* At least three values are needed to distinguish free (0),
Annotation
- Immediate include surface: `linux/rcupdate.h`, `linux/atomic.h`, `swap.h`.
- Detected declarations: `struct swap_table`, `struct swap_memcg_table`, `function null_to_swp_tb`, `function __count_to_swp_tb`, `function __flags_to_swp_tb`, `function pfn_to_swp_tb`, `function folio_to_swp_tb`, `function shadow_to_swp_tb`, `function swp_tb_is_null`, `function swp_tb_is_folio`.
- Atlas domain: Core OS / Memory Management.
- Implementation status: source implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.