drivers/block/zram/zcomp.c
Source file repositories/reference/linux-study-clean/drivers/block/zram/zcomp.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/block/zram/zcomp.c- Extension
.c- Size
- 5819 bytes
- Lines
- 263
- Domain
- Driver Families
- Bucket
- drivers/block
- Inferred role
- Driver Families: implementation source
- Status
- source 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.
- 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/kernel.hlinux/string.hlinux/err.hlinux/slab.hlinux/wait.hlinux/sched.hlinux/cpuhotplug.hlinux/vmalloc.hlinux/sysfs.hzcomp.hbackend_lzo.hbackend_lzorle.hbackend_lz4.hbackend_lz4hc.hbackend_zstd.hbackend_deflate.hbackend_842.h
Detected Declarations
function zcomp_strm_freefunction zcomp_strm_initfunction zcomp_available_showfunction zcomp_stream_putfunction zcomp_compressfunction zcomp_decompressfunction zcomp_cpu_up_preparefunction zcomp_cpu_deadfunction zcomp_initfunction zcomp_destroy
Annotated Snippet
if (!strcmp(comp, backends[i]->name)) {
at += sysfs_emit_at(buf, at, "[%s] ",
backends[i]->name);
} else {
at += sysfs_emit_at(buf, at, "%s ", backends[i]->name);
}
}
at += sysfs_emit_at(buf, at, "\n");
return at;
}
struct zcomp_strm *zcomp_stream_get(struct zcomp *comp)
{
for (;;) {
struct zcomp_strm *zstrm = raw_cpu_ptr(comp->stream);
/*
* Inspired by zswap
*
* stream is returned with ->mutex locked which prevents
* cpu_dead() from releasing this stream under us, however
* there is still a race window between raw_cpu_ptr() and
* mutex_lock(), during which we could have been migrated
* from a CPU that has already destroyed its stream. If
* so then unlock and re-try on the current CPU.
*/
mutex_lock(&zstrm->lock);
if (likely(zstrm->buffer))
return zstrm;
mutex_unlock(&zstrm->lock);
}
}
void zcomp_stream_put(struct zcomp_strm *zstrm)
{
mutex_unlock(&zstrm->lock);
}
int zcomp_compress(struct zcomp *comp, struct zcomp_strm *zstrm,
const void *src, unsigned int *dst_len)
{
struct zcomp_req req = {
.src = src,
.dst = zstrm->buffer,
.src_len = PAGE_SIZE,
.dst_len = 2 * PAGE_SIZE,
};
int ret;
might_sleep();
ret = comp->ops->compress(comp->params, &zstrm->ctx, &req);
if (!ret)
*dst_len = req.dst_len;
return ret;
}
int zcomp_decompress(struct zcomp *comp, struct zcomp_strm *zstrm,
const void *src, unsigned int src_len, void *dst)
{
struct zcomp_req req = {
.src = src,
.dst = dst,
.src_len = src_len,
.dst_len = PAGE_SIZE,
};
might_sleep();
return comp->ops->decompress(comp->params, &zstrm->ctx, &req);
}
int zcomp_cpu_up_prepare(unsigned int cpu, struct hlist_node *node)
{
struct zcomp *comp = hlist_entry(node, struct zcomp, node);
struct zcomp_strm *zstrm = per_cpu_ptr(comp->stream, cpu);
int ret;
ret = zcomp_strm_init(comp, zstrm);
if (ret)
pr_err("Can't allocate a compression stream\n");
return ret;
}
int zcomp_cpu_dead(unsigned int cpu, struct hlist_node *node)
{
struct zcomp *comp = hlist_entry(node, struct zcomp, node);
struct zcomp_strm *zstrm = per_cpu_ptr(comp->stream, cpu);
mutex_lock(&zstrm->lock);
zcomp_strm_free(comp, zstrm);
Annotation
- Immediate include surface: `linux/kernel.h`, `linux/string.h`, `linux/err.h`, `linux/slab.h`, `linux/wait.h`, `linux/sched.h`, `linux/cpuhotplug.h`, `linux/vmalloc.h`.
- Detected declarations: `function zcomp_strm_free`, `function zcomp_strm_init`, `function zcomp_available_show`, `function zcomp_stream_put`, `function zcomp_compress`, `function zcomp_decompress`, `function zcomp_cpu_up_prepare`, `function zcomp_cpu_dead`, `function zcomp_init`, `function zcomp_destroy`.
- Atlas domain: Driver Families / drivers/block.
- 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.