drivers/block/zram/zram_drv.c
Source file repositories/reference/linux-study-clean/drivers/block/zram/zram_drv.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/block/zram/zram_drv.c- Extension
.c- Size
- 78168 bytes
- Lines
- 3302
- Domain
- Driver Families
- Bucket
- drivers/block
- Inferred role
- Driver Families: operation-table or driver-model contract
- Status
- pattern implementation candidate
Why This File Exists
Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Defines an operation table; this is where Linux turns generic core objects into subsystem-specific behavior.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- 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/kernel.hlinux/bio.hlinux/bitops.hlinux/blkdev.hlinux/buffer_head.hlinux/device.hlinux/highmem.hlinux/slab.hlinux/backing-dev.hlinux/string.hlinux/vmalloc.hlinux/err.hlinux/idr.hlinux/sysfs.hlinux/debugfs.hlinux/cpuhotplug.hlinux/part_stat.hlinux/kernel_read_file.hlinux/rcupdate.hzram_drv.h
Detected Declarations
struct zram_pp_slotstruct zram_pp_ctlstruct zram_wb_ctlstruct zram_wb_reqstruct zram_rb_reqfunction slot_lock_initfunction slot_trylockfunction slot_lockfunction slot_unlockfunction init_donefunction get_slot_handlefunction set_slot_handlefunction test_slot_flagfunction set_slot_flagfunction clear_slot_flagfunction get_slot_sizefunction set_slot_sizefunction slot_allocatedfunction set_slot_comp_priorityfunction get_slot_comp_priorityfunction mark_slot_accessedfunction update_used_maxfunction zram_can_store_pagefunction is_partial_iofunction is_partial_iofunction release_pp_slotfunction release_pp_ctlfunction place_pp_slotfunction zram_fill_pagefunction page_same_filledfunction initstate_showfunction disksize_showfunction mem_limit_storefunction mem_used_max_storefunction mark_idlefunction idle_storefunction bd_stat_showfunction compressed_writeback_storefunction compressed_writeback_showfunction writeback_limit_enable_storefunction writeback_limit_enable_showfunction writeback_limit_storefunction writeback_limit_showfunction writeback_batch_size_storefunction writeback_batch_size_showfunction reset_bdevfunction backing_dev_showfunction backing_dev_store
Annotated Snippet
static const struct file_operations proc_zram_block_state_op = {
.open = simple_open,
.read = read_block_state,
.llseek = default_llseek,
};
static void zram_debugfs_register(struct zram *zram)
{
if (!zram_debugfs_root)
return;
zram->debugfs_dir = debugfs_create_dir(zram->disk->disk_name,
zram_debugfs_root);
debugfs_create_file("block_state", 0400, zram->debugfs_dir,
zram, &proc_zram_block_state_op);
}
static void zram_debugfs_unregister(struct zram *zram)
{
debugfs_remove_recursive(zram->debugfs_dir);
}
#else
static void zram_debugfs_create(void) {};
static void zram_debugfs_destroy(void) {};
static void zram_debugfs_register(struct zram *zram) {};
static void zram_debugfs_unregister(struct zram *zram) {};
#endif
/* Only algo parameter given, lookup by algo name */
static int lookup_algo_priority(struct zram *zram, const char *algo,
u32 min_prio)
{
s32 prio;
for (prio = min_prio; prio < ZRAM_MAX_COMPS; prio++) {
if (!zram->comp_algs[prio])
continue;
if (!strcmp(zram->comp_algs[prio], algo))
return prio;
}
return -EINVAL;
}
/* Both algo and priority parameters given, validate them */
static int validate_algo_priority(struct zram *zram, const char *algo, u32 prio)
{
if (prio >= ZRAM_MAX_COMPS)
return -EINVAL;
/* No algo at given priority */
if (!zram->comp_algs[prio])
return -EINVAL;
/* A different algo at given priority */
if (strcmp(zram->comp_algs[prio], algo))
return -EINVAL;
return 0;
}
static void comp_algorithm_set(struct zram *zram, u32 prio, const char *alg)
{
zram->comp_algs[prio] = alg;
}
static int __comp_algorithm_store(struct zram *zram, u32 prio, const char *buf)
{
const char *alg;
size_t sz;
sz = strlen(buf);
if (sz >= ZRAM_MAX_ALGO_NAME_SZ)
return -E2BIG;
alg = zcomp_lookup_backend_name(buf);
if (!alg)
return -EINVAL;
guard(rwsem_write)(&zram->dev_lock);
if (init_done(zram)) {
pr_info("Can't change algorithm for initialized device\n");
return -EBUSY;
}
comp_algorithm_set(zram, prio, alg);
return 0;
}
static void comp_params_reset(struct zram *zram, u32 prio)
{
struct zcomp_params *params = &zram->params[prio];
Annotation
- Immediate include surface: `linux/module.h`, `linux/kernel.h`, `linux/bio.h`, `linux/bitops.h`, `linux/blkdev.h`, `linux/buffer_head.h`, `linux/device.h`, `linux/highmem.h`.
- Detected declarations: `struct zram_pp_slot`, `struct zram_pp_ctl`, `struct zram_wb_ctl`, `struct zram_wb_req`, `struct zram_rb_req`, `function slot_lock_init`, `function slot_trylock`, `function slot_lock`, `function slot_unlock`, `function init_done`.
- Atlas domain: Driver Families / drivers/block.
- Implementation status: pattern implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
- 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.