tools/testing/selftests/bpf/libarena/src/buddy.bpf.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/libarena/src/buddy.bpf.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/libarena/src/buddy.bpf.c- Extension
.c- Size
- 24144 bytes
- Lines
- 904
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- 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
libarena/common.hlibarena/asan.hlibarena/buddy.h
Detected Declarations
function buddy_lockfunction buddy_unlockfunction boundaryfunction buddy_unreserve_arena_vaddrfunction buddy_alloc_arena_vaddrfunction arena_next_pow2function idx_set_allocatedfunction idx_is_allocatedfunction idx_set_orderfunction idx_get_orderfunction header_add_freelistfunction header_remove_freelistfunction size_to_orderfunction add_leftovers_to_freelistfunction buddy_initfunction buddy_destroyfunction buddy_chunk_allocfunction buddy_alloc_from_existing_chunksfunction buddy_alloc_from_new_chunkfunction buddy_free_unlockedfunction buddy_free
Annotated Snippet
if (unlikely(!header)) {
arena_stderr("idx %u has no header", idx);
return -EINVAL;
}
asan_unpoison(header, sizeof(*header));
header_add_freelist(chunk, header, idx, ord);
}
return 0;
}
static struct buddy_chunk __arena *buddy_chunk_get(struct buddy __arena *buddy)
{
u64 order, ord, min_order, max_order;
struct buddy_chunk __arena *chunk;
size_t left;
int power2;
u64 vaddr;
u32 idx;
int ret;
/*
* Step 1: Allocate a properly aligned chunk, and
* prep it for insertion into the buddy allocator.
* We don't need the allocator lock until step 2.
*/
ret = buddy_alloc_arena_vaddr(buddy, &vaddr);
if (ret)
return NULL;
/* Addresses must be aligned to the chunk boundary. */
if (vaddr % BUDDY_CHUNK_BYTES)
return NULL;
/* Unreserve the address space. */
bpf_arena_free_pages(&arena, (void __arena *)vaddr,
BUDDY_CHUNK_PAGES);
chunk = bpf_arena_alloc_pages(&arena, (void __arena *)vaddr,
BUDDY_CHUNK_PAGES, NUMA_NO_NODE, 0);
if (!chunk) {
arena_stderr("[ALLOC FAILED]");
return NULL;
}
if (buddy_lock(buddy)) {
/*
* We cannot reclaim the vaddr space, but that is ok - this
* operation should always succeed. The error path is to catch
* accidental deadlocks that will cause -ENOMEMs to the program as
* the allocator fails to refill itself, in which case vaddr usage
* is the least of our worries.
*/
bpf_arena_free_pages(&arena, (void __arena *)vaddr, BUDDY_CHUNK_PAGES);
return NULL;
}
asan_poison(chunk, BUDDY_POISONED, BUDDY_CHUNK_PAGES * __PAGE_SIZE);
/* Unpoison the chunk itself. */
asan_unpoison(chunk, sizeof(*chunk));
/* Mark all freelists as empty. */
for (ord = zero; ord < BUDDY_CHUNK_NUM_ORDERS && can_loop; ord++)
chunk->freelists[ord] = BUDDY_CHUNK_ITEMS;
/*
* Initialize the chunk by carving out a page range to hold the metadata
* struct above, then dumping the rest of the pages into the allocator.
*/
_Static_assert(BUDDY_CHUNK_PAGES * __PAGE_SIZE >=
BUDDY_MIN_ALLOC_BYTES *
BUDDY_CHUNK_ITEMS,
"chunk must fit within the allocation");
/*
* Step 2: Reserve a chunk for the chunk metadata, then breaks
* the rest of the full allocation into the different buckets.
* We allocating the memory by grabbing blocks of progressively
* smaller sizes from the allocator, which are guaranteed to be
* continuous.
*
* This operation also populates the allocator.
*
* Algorithm:
*
Annotation
- Immediate include surface: `libarena/common.h`, `libarena/asan.h`, `libarena/buddy.h`.
- Detected declarations: `function buddy_lock`, `function buddy_unlock`, `function boundary`, `function buddy_unreserve_arena_vaddr`, `function buddy_alloc_arena_vaddr`, `function arena_next_pow2`, `function idx_set_allocated`, `function idx_is_allocated`, `function idx_set_order`, `function idx_get_order`.
- Atlas domain: Support Tooling And Documentation / tools.
- 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.