mm/mempool.c
Source file repositories/reference/linux-study-clean/mm/mempool.c
File Facts
- System
- Linux kernel
- Corpus path
mm/mempool.c- Extension
.c- Size
- 22640 bytes
- Lines
- 760
- Domain
- Core OS
- Bucket
- Memory Management
- Inferred role
- Core OS: exported/initcall integration point
- Status
- integration 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.
- 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.
- 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/fault-inject.hlinux/mm.hlinux/slab.hlinux/highmem.hlinux/kasan.hlinux/kmemleak.hlinux/export.hlinux/mempool.hlinux/writeback.hslab.h
Detected Declarations
function mempool_faul_inject_initfunction poison_errorfunction __check_elementfunction check_elementfunction __poison_elementfunction poison_elementfunction check_elementfunction kasan_unpoison_elementfunction add_elementfunction mempool_initfunction mempool_createfunction mempool_init_nodefunction mempool_createfunction mempool_createfunction mempool_alloc_from_poolfunction mempool_adjust_gfpfunction mempool_alloc_bulk_noproffunction mempool_allocfunction mempool_free_bulkfunction READ_ONCEfunction mempool_freefunction mempool_free_slabfunction mempool_kfreefunction mempool_free_pagesexport mempool_exitexport mempool_destroyexport mempool_init_nodeexport mempool_init_noprofexport mempool_create_node_noprofexport mempool_resizeexport mempool_alloc_bulk_noprofexport mempool_alloc_noprofexport mempool_alloc_preallocatedexport mempool_free_bulkexport mempool_freeexport mempool_alloc_slabexport mempool_free_slabexport mempool_kmallocexport mempool_kfreeexport mempool_alloc_pagesexport mempool_free_pages
Annotated Snippet
if (obj[i] != exp) {
poison_error(pool, element, size, i);
return;
}
}
memset(obj, POISON_INUSE, size);
}
static void check_element(struct mempool *pool, void *element)
{
/* Skip checking: KASAN might save its metadata in the element. */
if (kasan_enabled())
return;
/* Mempools backed by slab allocator */
if (pool->free == mempool_kfree) {
__check_element(pool, element, (size_t)pool->pool_data);
} else if (pool->free == mempool_free_slab) {
__check_element(pool, element, kmem_cache_size(pool->pool_data));
} else if (pool->free == mempool_free_pages) {
/* Mempools backed by page allocator */
int order = (int)(long)pool->pool_data;
#ifdef CONFIG_HIGHMEM
for (int i = 0; i < (1 << order); i++) {
struct page *page = (struct page *)element;
void *addr = kmap_local_page(page + i);
__check_element(pool, addr, PAGE_SIZE);
kunmap_local(addr);
}
#else
void *addr = page_address((struct page *)element);
__check_element(pool, addr, PAGE_SIZE << order);
#endif
}
}
static void __poison_element(void *element, size_t size)
{
u8 *obj = element;
memset(obj, POISON_FREE, size - 1);
obj[size - 1] = POISON_END;
}
static void poison_element(struct mempool *pool, void *element)
{
/* Skip poisoning: KASAN might save its metadata in the element. */
if (kasan_enabled())
return;
/* Mempools backed by slab allocator */
if (pool->alloc == mempool_kmalloc) {
__poison_element(element, (size_t)pool->pool_data);
} else if (pool->alloc == mempool_alloc_slab) {
__poison_element(element, kmem_cache_size(pool->pool_data));
} else if (pool->alloc == mempool_alloc_pages) {
/* Mempools backed by page allocator */
int order = (int)(long)pool->pool_data;
#ifdef CONFIG_HIGHMEM
for (int i = 0; i < (1 << order); i++) {
struct page *page = (struct page *)element;
void *addr = kmap_local_page(page + i);
__poison_element(addr, PAGE_SIZE);
kunmap_local(addr);
}
#else
void *addr = page_address((struct page *)element);
__poison_element(addr, PAGE_SIZE << order);
#endif
}
}
#else /* CONFIG_SLUB_DEBUG_ON */
static inline void check_element(struct mempool *pool, void *element)
{
}
static inline void poison_element(struct mempool *pool, void *element)
{
}
#endif /* CONFIG_SLUB_DEBUG_ON */
static __always_inline bool kasan_poison_element(struct mempool *pool,
void *element)
{
if (pool->alloc == mempool_alloc_slab || pool->alloc == mempool_kmalloc)
Annotation
- Immediate include surface: `linux/fault-inject.h`, `linux/mm.h`, `linux/slab.h`, `linux/highmem.h`, `linux/kasan.h`, `linux/kmemleak.h`, `linux/export.h`, `linux/mempool.h`.
- Detected declarations: `function mempool_faul_inject_init`, `function poison_error`, `function __check_element`, `function check_element`, `function __poison_element`, `function poison_element`, `function check_element`, `function kasan_unpoison_element`, `function add_element`, `function mempool_init`.
- Atlas domain: Core OS / Memory Management.
- Implementation status: integration 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.