kernel/bpf/core.c
Source file repositories/reference/linux-study-clean/kernel/bpf/core.c
File Facts
- System
- Linux kernel
- Corpus path
kernel/bpf/core.c- Extension
.c- Size
- 91685 bytes
- Lines
- 3538
- Domain
- Core OS
- Bucket
- Scheduler, Processes, Timers, Sync, And Syscalls
- 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.
- 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
uapi/linux/btf.hlinux/filter.hlinux/skbuff.hlinux/vmalloc.hlinux/prandom.hlinux/bpf.hlinux/btf.hlinux/hex.hlinux/objtool.hlinux/overflow.hlinux/rbtree_latch.hlinux/kallsyms.hlinux/rcupdate.hlinux/perf_event.hlinux/extable.hlinux/log2.hlinux/bpf_verifier.hlinux/nodemask.hlinux/nospec.hlinux/bpf_mem_alloc.hlinux/memcontrol.hlinux/execmem.hcrypto/sha2.hasm/barrier.hlinux/unaligned.hlinux/bpf_trace.h
Detected Declarations
struct bpf_prog_packstruct walk_stack_ctxenum page_size_enumfunction for_each_possible_cpufunction bpf_prog_alloc_jited_linfofunction bpf_prog_jit_attempt_donefunction mappingfunction __bpf_prog_freefunction bpf_prog_calc_tagfunction bpf_adj_delta_to_immfunction bpf_adj_delta_to_offfunction bpf_adj_branchesfunction bpf_adj_linfofunction bpf_remove_insnsfunction bpf_prog_kallsyms_del_subprogsfunction bpf_prog_kallsyms_del_allfunction bpf_prog_ksym_set_addrfunction bpf_prog_ksym_set_namefunction bpf_get_ksym_startfunction bpf_tree_lessfunction bpf_tree_compfunction bpf_ksym_addfunction __bpf_ksym_delfunction bpf_ksym_delfunction bpf_prog_kallsyms_candidatefunction bpf_prog_kallsyms_addfunction bpf_prog_kallsyms_delfunction bpf_address_lookupfunction is_bpf_text_addressfunction bpf_has_frame_pointerfunction bpf_get_kallsymfunction bpf_jit_add_poke_descriptorfunction bpf_jit_fill_hole_with_zerofunction num_possible_nodesfunction bpf_prog_pack_freefunction list_for_each_entryfunction bpf_jit_alloc_exec_limitfunction bpf_jit_charge_initfunction bpf_jit_charge_modmemfunction bpf_jit_uncharge_modmemfunction bpf_jit_alloc_execfunction bpf_jit_free_execfunction bpf_jit_binary_allocfunction bpf_jit_binary_freefunction bpf_jit_binary_pack_allocfunction bpf_jit_binary_pack_finalizefunction bpf_jit_binary_pack_finalizefunction bpf_jit_binary_pack_hdr
Annotated Snippet
struct bpf_prog_pack {
struct list_head list;
void *ptr;
unsigned long bitmap[];
};
void bpf_jit_fill_hole_with_zero(void *area, unsigned int size)
{
memset(area, 0, size);
}
#define BPF_PROG_SIZE_TO_NBITS(size) (round_up(size, BPF_PROG_CHUNK_SIZE) / BPF_PROG_CHUNK_SIZE)
static DEFINE_MUTEX(pack_mutex);
static LIST_HEAD(pack_list);
/* PMD_SIZE is not available in some special config, e.g. ARCH=arm with
* CONFIG_MMU=n. Use PAGE_SIZE in these cases.
*/
#ifdef PMD_SIZE
/* PMD_SIZE is really big for some archs. It doesn't make sense to
* reserve too much memory in one allocation. Hardcode BPF_PROG_PACK_SIZE to
* 2MiB * num_possible_nodes(). On most architectures PMD_SIZE will be
* greater than or equal to 2MB.
*/
#define BPF_PROG_PACK_SIZE (SZ_2M * num_possible_nodes())
#else
#define BPF_PROG_PACK_SIZE PAGE_SIZE
#endif
#define BPF_PROG_CHUNK_COUNT (BPF_PROG_PACK_SIZE / BPF_PROG_CHUNK_SIZE)
static struct bpf_prog_pack *alloc_new_pack(bpf_jit_fill_hole_t bpf_fill_ill_insns)
{
struct bpf_prog_pack *pack;
int err;
pack = kzalloc_flex(*pack, bitmap, BITS_TO_LONGS(BPF_PROG_CHUNK_COUNT));
if (!pack)
return NULL;
pack->ptr = bpf_jit_alloc_exec(BPF_PROG_PACK_SIZE);
if (!pack->ptr)
goto out;
bpf_fill_ill_insns(pack->ptr, BPF_PROG_PACK_SIZE);
bitmap_zero(pack->bitmap, BPF_PROG_PACK_SIZE / BPF_PROG_CHUNK_SIZE);
set_vm_flush_reset_perms(pack->ptr);
err = set_memory_rox((unsigned long)pack->ptr,
BPF_PROG_PACK_SIZE / PAGE_SIZE);
if (err)
goto out;
list_add_tail(&pack->list, &pack_list);
return pack;
out:
bpf_jit_free_exec(pack->ptr);
kfree(pack);
return NULL;
}
void *bpf_prog_pack_alloc(u32 size, bpf_jit_fill_hole_t bpf_fill_ill_insns)
{
unsigned int nbits = BPF_PROG_SIZE_TO_NBITS(size);
struct bpf_prog_pack *pack;
unsigned long pos;
void *ptr = NULL;
mutex_lock(&pack_mutex);
if (size > BPF_PROG_PACK_SIZE) {
size = round_up(size, PAGE_SIZE);
ptr = bpf_jit_alloc_exec(size);
if (ptr) {
int err;
bpf_fill_ill_insns(ptr, size);
set_vm_flush_reset_perms(ptr);
err = set_memory_rox((unsigned long)ptr,
size / PAGE_SIZE);
if (err) {
bpf_jit_free_exec(ptr);
ptr = NULL;
}
}
goto out;
}
list_for_each_entry(pack, &pack_list, list) {
pos = bitmap_find_next_zero_area(pack->bitmap, BPF_PROG_CHUNK_COUNT, 0,
nbits, 0);
if (pos < BPF_PROG_CHUNK_COUNT)
goto found_free_area;
Annotation
- Immediate include surface: `uapi/linux/btf.h`, `linux/filter.h`, `linux/skbuff.h`, `linux/vmalloc.h`, `linux/prandom.h`, `linux/bpf.h`, `linux/btf.h`, `linux/hex.h`.
- Detected declarations: `struct bpf_prog_pack`, `struct walk_stack_ctx`, `enum page_size_enum`, `function for_each_possible_cpu`, `function bpf_prog_alloc_jited_linfo`, `function bpf_prog_jit_attempt_done`, `function mapping`, `function __bpf_prog_free`, `function bpf_prog_calc_tag`, `function bpf_adj_delta_to_imm`.
- Atlas domain: Core OS / Scheduler, Processes, Timers, Sync, And Syscalls.
- Implementation status: integration 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.