kernel/bpf/memalloc.c
Source file repositories/reference/linux-study-clean/kernel/bpf/memalloc.c
File Facts
- System
- Linux kernel
- Corpus path
kernel/bpf/memalloc.c- Extension
.c- Size
- 27814 bytes
- Lines
- 1040
- Domain
- Core OS
- Bucket
- Scheduler, Processes, Timers, Sync, And Syscalls
- 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/mm.hlinux/llist.hlinux/bpf.hlinux/irq_work.hlinux/bpf_mem_alloc.hlinux/memcontrol.hasm/local.h
Detected Declarations
struct bpf_mem_cachestruct bpf_mem_cachesfunction bpf_mem_cache_idxfunction inc_activefunction dec_activefunction add_obj_to_free_listfunction alloc_bulkfunction free_onefunction free_allfunction llist_for_each_safefunction __free_rcufunction enque_to_freefunction do_call_rcu_ttracefunction free_bulkfunction __free_by_rcufunction check_free_by_rcufunction bpf_mem_refillfunction irq_work_raisefunction assumingfunction prefill_mem_cachefunction bpf_mem_alloc_initfunction for_each_possible_cpufunction bpf_mem_alloc_percpu_initfunction bpf_mem_alloc_percpu_unit_initfunction for_each_possible_cpufunction drain_mem_cachefunction check_mem_cachefunction check_leaked_objsfunction for_each_possible_cpufunction free_mem_alloc_no_barrierfunction free_mem_allocfunction free_mem_alloc_deferredfunction destroy_mem_allocfunction bpf_mem_alloc_destroyfunction for_each_possible_cpufunction for_each_possible_cpufunction kfreefunction unit_free_rcufunction bpf_mem_freefunction bpf_mem_free_rcufunction bpf_mem_cache_freefunction bpf_mem_cache_free_rcufunction bpf_mem_cache_raw_freefunction bpf_mem_alloc_check_sizefunction bpf_mem_alloc_set_dtorfunction for_each_possible_cpu
Annotated Snippet
struct bpf_mem_cache {
/* per-cpu list of free objects of size 'unit_size'.
* All accesses are done with interrupts disabled and 'active' counter
* protection with __llist_add() and __llist_del_first().
*/
struct llist_head free_llist;
local_t active;
/* Operations on the free_list from unit_alloc/unit_free/bpf_mem_refill
* are sequenced by per-cpu 'active' counter. But unit_free() cannot
* fail. When 'active' is busy the unit_free() will add an object to
* free_llist_extra.
*/
struct llist_head free_llist_extra;
struct irq_work refill_work;
struct obj_cgroup *objcg;
int unit_size;
/* count of objects in free_llist */
int free_cnt;
int low_watermark, high_watermark, batch;
int percpu_size;
bool draining;
struct bpf_mem_cache *tgt;
void (*dtor)(void *obj, void *ctx);
void *dtor_ctx;
/* list of objects to be freed after RCU GP */
struct llist_head free_by_rcu;
struct llist_node *free_by_rcu_tail;
struct llist_head waiting_for_gp;
struct llist_node *waiting_for_gp_tail;
struct rcu_head rcu;
atomic_t call_rcu_in_progress;
struct llist_head free_llist_extra_rcu;
/* list of objects to be freed after RCU tasks trace GP */
struct llist_head free_by_rcu_ttrace;
struct llist_head waiting_for_gp_ttrace;
struct rcu_head rcu_ttrace;
atomic_t call_rcu_ttrace_in_progress;
};
struct bpf_mem_caches {
struct bpf_mem_cache cache[NUM_CACHES];
};
static const u16 sizes[NUM_CACHES] = {96, 192, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096};
static struct llist_node notrace *__llist_del_first(struct llist_head *head)
{
struct llist_node *entry, *next;
entry = head->first;
if (!entry)
return NULL;
next = entry->next;
head->first = next;
return entry;
}
static void *__alloc(struct bpf_mem_cache *c, int node, gfp_t flags)
{
if (c->percpu_size) {
void __percpu **obj = kmalloc_node(c->percpu_size, flags, node);
void __percpu *pptr = __alloc_percpu_gfp(c->unit_size, 8, flags);
if (!obj || !pptr) {
free_percpu(pptr);
kfree(obj);
return NULL;
}
obj[1] = pptr;
return obj;
}
return kmalloc_node(c->unit_size, flags | __GFP_ZERO, node);
}
static struct mem_cgroup *get_memcg(const struct bpf_mem_cache *c)
{
#ifdef CONFIG_MEMCG
if (c->objcg)
return get_mem_cgroup_from_objcg(c->objcg);
return root_mem_cgroup;
#else
return NULL;
#endif
}
Annotation
- Immediate include surface: `linux/mm.h`, `linux/llist.h`, `linux/bpf.h`, `linux/irq_work.h`, `linux/bpf_mem_alloc.h`, `linux/memcontrol.h`, `asm/local.h`.
- Detected declarations: `struct bpf_mem_cache`, `struct bpf_mem_caches`, `function bpf_mem_cache_idx`, `function inc_active`, `function dec_active`, `function add_obj_to_free_list`, `function alloc_bulk`, `function free_one`, `function free_all`, `function llist_for_each_safe`.
- Atlas domain: Core OS / Scheduler, Processes, Timers, Sync, And Syscalls.
- 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.