lib/genalloc.c
Source file repositories/reference/linux-study-clean/lib/genalloc.c
File Facts
- System
- Linux kernel
- Corpus path
lib/genalloc.c- Extension
.c- Size
- 27116 bytes
- Lines
- 913
- Domain
- Kernel Services
- Bucket
- lib
- Inferred role
- Kernel Services: exported/initcall integration point
- Status
- integration implementation candidate
Why This File Exists
Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Touches IRQ or DMA behavior; this matters for the representative real-device path.
- 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/slab.hlinux/export.hlinux/bitmap.hlinux/rculist.hlinux/interrupt.hlinux/genalloc.hlinux/of.hlinux/of_platform.hlinux/platform_device.hlinux/vmalloc.h
Detected Declarations
function chunk_sizefunction set_bits_llfunction clear_bits_llfunction bitmap_set_llfunction bitmap_clear_llfunction gen_pool_add_ownerfunction gen_pool_virt_to_physfunction gen_pool_destroyfunction list_for_each_safefunction functionfunction functionfunction functionfunction gen_pool_free_ownerfunction gen_pool_for_each_chunkfunction gen_pool_has_addrfunction gen_pool_availfunction gen_pool_sizefunction gen_pool_set_algofunction requirementfunction requirementfunction gen_pool_fixed_allocfunction gen_pool_first_fit_order_alignfunction requirementfunction devm_gen_pool_releasefunction devm_gen_pool_matchexport gen_pool_createexport gen_pool_add_ownerexport gen_pool_virt_to_physexport gen_pool_destroyexport gen_pool_alloc_algo_ownerexport gen_pool_dma_allocexport gen_pool_dma_alloc_algoexport gen_pool_dma_alloc_alignexport gen_pool_dma_zallocexport gen_pool_dma_zalloc_algoexport gen_pool_dma_zalloc_alignexport gen_pool_free_ownerexport gen_pool_for_each_chunkexport gen_pool_has_addrexport gen_pool_availexport gen_pool_sizeexport gen_pool_set_algoexport gen_pool_first_fitexport gen_pool_first_fit_alignexport gen_pool_fixed_allocexport gen_pool_first_fit_order_alignexport gen_pool_best_fitexport gen_pool_get
Annotated Snippet
if (addr >= chunk->start_addr && addr <= chunk->end_addr) {
paddr = chunk->phys_addr + (addr - chunk->start_addr);
break;
}
}
rcu_read_unlock();
return paddr;
}
EXPORT_SYMBOL(gen_pool_virt_to_phys);
/**
* gen_pool_destroy - destroy a special memory pool
* @pool: pool to destroy
*
* Destroy the specified special memory pool. Verifies that there are no
* outstanding allocations.
*/
void gen_pool_destroy(struct gen_pool *pool)
{
struct list_head *_chunk, *_next_chunk;
struct gen_pool_chunk *chunk;
int order = pool->min_alloc_order;
unsigned long bit, end_bit;
list_for_each_safe(_chunk, _next_chunk, &pool->chunks) {
chunk = list_entry(_chunk, struct gen_pool_chunk, next_chunk);
list_del(&chunk->next_chunk);
end_bit = chunk_size(chunk) >> order;
bit = find_first_bit(chunk->bits, end_bit);
BUG_ON(bit < end_bit);
vfree(chunk);
}
kfree_const(pool->name);
kfree(pool);
}
EXPORT_SYMBOL(gen_pool_destroy);
/**
* gen_pool_alloc_algo_owner - allocate special memory from the pool
* @pool: pool to allocate from
* @size: number of bytes to allocate from the pool
* @algo: algorithm passed from caller
* @data: data passed to algorithm
* @owner: optionally retrieve the chunk owner
*
* Allocate the requested number of bytes from the specified pool.
* Uses the pool allocation function (with first-fit algorithm by default).
* Can not be used in NMI handler on architectures without
* NMI-safe cmpxchg implementation.
*/
unsigned long gen_pool_alloc_algo_owner(struct gen_pool *pool, size_t size,
genpool_algo_t algo, void *data, void **owner)
{
struct gen_pool_chunk *chunk;
unsigned long addr = 0;
int order = pool->min_alloc_order;
unsigned long nbits, start_bit, end_bit, remain;
#ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
BUG_ON(in_nmi());
#endif
if (owner)
*owner = NULL;
if (size == 0)
return 0;
nbits = (size + (1UL << order) - 1) >> order;
rcu_read_lock();
list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) {
if (size > atomic_long_read(&chunk->avail))
continue;
start_bit = 0;
end_bit = chunk_size(chunk) >> order;
retry:
start_bit = algo(chunk->bits, end_bit, start_bit,
nbits, data, pool, chunk->start_addr);
if (start_bit >= end_bit)
continue;
remain = bitmap_set_ll(chunk->bits, start_bit, nbits);
if (remain) {
remain = bitmap_clear_ll(chunk->bits, start_bit,
nbits - remain);
BUG_ON(remain);
goto retry;
Annotation
- Immediate include surface: `linux/slab.h`, `linux/export.h`, `linux/bitmap.h`, `linux/rculist.h`, `linux/interrupt.h`, `linux/genalloc.h`, `linux/of.h`, `linux/of_platform.h`.
- Detected declarations: `function chunk_size`, `function set_bits_ll`, `function clear_bits_ll`, `function bitmap_set_ll`, `function bitmap_clear_ll`, `function gen_pool_add_owner`, `function gen_pool_virt_to_phys`, `function gen_pool_destroy`, `function list_for_each_safe`, `function function`.
- Atlas domain: Kernel Services / lib.
- Implementation status: integration implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
- IRQ or DMA behavior appears here, which is relevant to the selected PCIe/NVMe device path.
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.