mm/zswap.c
Source file repositories/reference/linux-study-clean/mm/zswap.c
File Facts
- System
- Linux kernel
- Corpus path
mm/zswap.c- Extension
.c- Size
- 51343 bytes
- Lines
- 1833
- 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/module.hlinux/cpu.hlinux/highmem.hlinux/slab.hlinux/spinlock.hlinux/types.hlinux/atomic.hlinux/swap.hlinux/crypto.hlinux/scatterlist.hlinux/mempolicy.hlinux/mempool.hcrypto/acompress.hcrypto/scatterwalk.hlinux/zswap.hlinux/mm_types.hlinux/page-flags.hlinux/swapops.hlinux/writeback.hlinux/pagemap.hlinux/workqueue.hlinux/list_lru.hlinux/zsmalloc.hswap.hinternal.hlinux/debugfs.h
Detected Declarations
struct crypto_acomp_ctxstruct zswap_poolstruct zswap_entryenum zswap_init_typefunction zswap_is_enabledfunction zswap_never_enabledfunction acomp_ctx_freefunction strcmpfunction zswap_pool_destroyfunction __zswap_pool_releasefunction __zswap_pool_emptyfunction zswap_pool_trygetfunction zswap_pool_getfunction zswap_pool_putfunction list_for_each_entry_rcufunction zswap_max_pagesfunction zswap_accept_thr_pagesfunction zswap_total_pagesfunction zswap_check_limitsfunction zswap_compressor_param_setfunction zswap_enabled_param_setfunction entry_to_nidfunction zswap_lru_addfunction zswap_lru_delfunction zswap_lruvec_state_initfunction zswap_folio_swapinfunction shrink_workerfunction zswap_entry_cache_freefunction zswap_entry_freefunction zswap_cpu_comp_preparefunction zswap_compressfunction zswap_decompressfunction zswap_storefunction zswap_shrinker_countfunction zswap_shrinker_scanfunction zswap_shrinker_countfunction shrink_memcgfunction for_each_node_statefunction shrink_workerfunction zswap_store_pagefunction zswap_storefunction zswap_loadfunction zswap_invalidatefunction zswap_swaponfunction zswap_swapofffunction debugfs_get_total_sizefunction debugfs_get_stored_pagesfunction debugfs_get_stored_incompressible_pages
Annotated Snippet
struct crypto_acomp_ctx {
struct crypto_acomp *acomp;
struct acomp_req *req;
struct crypto_wait wait;
u8 *buffer;
struct mutex mutex;
};
/*
* The lock ordering is zswap_tree.lock -> zswap_pool.lru_lock.
* The only case where lru_lock is not acquired while holding tree.lock is
* when a zswap_entry is taken off the lru for writeback, in that case it
* needs to be verified that it's still valid in the tree.
*/
struct zswap_pool {
struct zs_pool *zs_pool;
struct crypto_acomp_ctx __percpu *acomp_ctx;
struct percpu_ref ref;
struct list_head list;
struct work_struct release_work;
struct hlist_node node;
char tfm_name[CRYPTO_MAX_ALG_NAME];
};
/* Global LRU lists shared by all zswap pools. */
static struct list_lru zswap_list_lru;
/* The lock protects zswap_next_shrink updates. */
static DEFINE_SPINLOCK(zswap_shrink_lock);
static struct mem_cgroup *zswap_next_shrink;
static struct work_struct zswap_shrink_work;
static struct shrinker *zswap_shrinker;
/*
* struct zswap_entry
*
* This structure contains the metadata for tracking a single compressed
* page within zswap.
*
* swpentry - associated swap entry, the offset indexes into the xarray
* length - the length in bytes of the compressed page data. Needed during
* decompression.
* referenced - true if the entry recently entered the zswap pool. Unset by the
* writeback logic. The entry is only reclaimed by the writeback
* logic if referenced is unset. See comments in the shrinker
* section for context.
* pool - the zswap_pool the entry's data is in
* handle - zsmalloc allocation handle that stores the compressed page data
* objcg - the obj_cgroup that the compressed memory is charged to
* lru - handle to the pool's lru used to evict pages.
*/
struct zswap_entry {
swp_entry_t swpentry;
unsigned int length;
bool referenced;
struct zswap_pool *pool;
unsigned long handle;
struct obj_cgroup *objcg;
struct list_head lru;
};
static struct xarray *zswap_trees[MAX_SWAPFILES];
static unsigned int nr_zswap_trees[MAX_SWAPFILES];
/* RCU-protected iteration */
static LIST_HEAD(zswap_pools);
/* protects zswap_pools list modification */
static DEFINE_SPINLOCK(zswap_pools_lock);
/* pool counter to provide unique names to zsmalloc */
static atomic_t zswap_pools_count = ATOMIC_INIT(0);
enum zswap_init_type {
ZSWAP_UNINIT,
ZSWAP_INIT_SUCCEED,
ZSWAP_INIT_FAILED
};
static enum zswap_init_type zswap_init_state;
/* used to ensure the integrity of initialization */
static DEFINE_MUTEX(zswap_init_lock);
/* init completed, but couldn't create the initial pool */
static bool zswap_has_pool;
/*********************************
* helpers and fwd declarations
**********************************/
/* One swap address space for each 64M swap space */
Annotation
- Immediate include surface: `linux/module.h`, `linux/cpu.h`, `linux/highmem.h`, `linux/slab.h`, `linux/spinlock.h`, `linux/types.h`, `linux/atomic.h`, `linux/swap.h`.
- Detected declarations: `struct crypto_acomp_ctx`, `struct zswap_pool`, `struct zswap_entry`, `enum zswap_init_type`, `function zswap_is_enabled`, `function zswap_never_enabled`, `function acomp_ctx_free`, `function strcmp`, `function zswap_pool_destroy`, `function __zswap_pool_release`.
- 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.