mm/swap.h
Source file repositories/reference/linux-study-clean/mm/swap.h
File Facts
- System
- Linux kernel
- Corpus path
mm/swap.h- Extension
.h- Size
- 14470 bytes
- Lines
- 477
- 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/atomic.hlinux/mm.hlinux/swapops.hlinux/blk_types.h
Detected Declarations
struct mempolicystruct swap_iocbstruct swap_memcg_tablestruct swap_cluster_infostruct swap_iocbstruct swap_iocbenum swap_cluster_flagsfunction swp_cluster_offsetfunction swap_cluster_unlockfunction swap_cluster_unlock_irqfunction swap_read_unplugfunction swap_dev_posfunction folio_matches_swap_entryfunction folio_swap_flagsfunction swap_cluster_unlockfunction folio_alloc_swapfunction folio_dup_swapfunction folio_put_swapfunction folio_matches_swap_entryfunction show_swap_cache_infofunction swap_update_readaheadfunction swap_retry_table_allocfunction swap_cache_has_foliofunction swap_cache_del_folio
Annotated Snippet
struct swap_cluster_info {
spinlock_t lock; /*
* Protect swap_cluster_info fields
* other than list, and swap_info_struct->swap_map
* elements corresponding to the swap cluster.
*/
u16 count;
u8 flags;
u8 order;
atomic_long_t __rcu *table; /* Swap table entries, see mm/swap_table.h */
unsigned int *extend_table; /* For large swap count, protected by ci->lock */
#ifdef CONFIG_MEMCG
struct swap_memcg_table *memcg_table; /* Swap table entries' cgroup record */
#endif
#if !SWAP_TABLE_HAS_ZEROFLAG
unsigned long *zero_bitmap;
#endif
struct list_head list;
};
/* All on-list cluster must have a non-zero flag. */
enum swap_cluster_flags {
CLUSTER_FLAG_NONE = 0, /* For temporary off-list cluster */
CLUSTER_FLAG_FREE,
CLUSTER_FLAG_NONFULL,
CLUSTER_FLAG_FRAG,
/* Clusters with flags above are allocatable */
CLUSTER_FLAG_USABLE = CLUSTER_FLAG_FRAG,
CLUSTER_FLAG_FULL,
CLUSTER_FLAG_DISCARD,
CLUSTER_FLAG_MAX,
};
#ifdef CONFIG_SWAP
#include <linux/swapops.h> /* for swp_offset */
#include <linux/blk_types.h> /* for bio_end_io_t */
static inline unsigned int swp_cluster_offset(swp_entry_t entry)
{
return swp_offset(entry) % SWAPFILE_CLUSTER;
}
/*
* Callers of all helpers below must ensure the entry, type, or offset is
* valid, and protect the swap device with reference count or locks.
*/
static inline struct swap_info_struct *__swap_type_to_info(int type)
{
struct swap_info_struct *si;
si = READ_ONCE(swap_info[type]); /* rcu_dereference() */
VM_WARN_ON_ONCE(percpu_ref_is_zero(&si->users)); /* race with swapoff */
return si;
}
static inline struct swap_info_struct *__swap_entry_to_info(swp_entry_t entry)
{
return __swap_type_to_info(swp_type(entry));
}
static inline struct swap_cluster_info *__swap_offset_to_cluster(
struct swap_info_struct *si, pgoff_t offset)
{
VM_WARN_ON_ONCE(percpu_ref_is_zero(&si->users)); /* race with swapoff */
VM_WARN_ON_ONCE(offset >= roundup(si->max, SWAPFILE_CLUSTER));
return &si->cluster_info[offset / SWAPFILE_CLUSTER];
}
static inline struct swap_cluster_info *__swap_entry_to_cluster(swp_entry_t entry)
{
return __swap_offset_to_cluster(__swap_entry_to_info(entry),
swp_offset(entry));
}
static __always_inline struct swap_cluster_info *__swap_cluster_lock(
struct swap_info_struct *si, unsigned long offset, bool irq)
{
struct swap_cluster_info *ci = __swap_offset_to_cluster(si, offset);
/*
* Nothing modifies swap cache in an IRQ context. All access to
* swap cache is wrapped by swap_cache_* helpers, and swap cache
* writeback is handled outside of IRQs. Swapin or swapout never
* occurs in IRQ, and neither does in-place split or replace.
*
* Besides, modifying swap cache requires synchronization with
* swap_map, which was never IRQ safe.
*/
VM_WARN_ON_ONCE(!in_task());
VM_WARN_ON_ONCE(percpu_ref_is_zero(&si->users)); /* race with swapoff */
Annotation
- Immediate include surface: `linux/atomic.h`, `linux/mm.h`, `linux/swapops.h`, `linux/blk_types.h`.
- Detected declarations: `struct mempolicy`, `struct swap_iocb`, `struct swap_memcg_table`, `struct swap_cluster_info`, `struct swap_iocb`, `struct swap_iocb`, `enum swap_cluster_flags`, `function swp_cluster_offset`, `function swap_cluster_unlock`, `function swap_cluster_unlock_irq`.
- 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.