kernel/bpf/queue_stack_maps.c
Source file repositories/reference/linux-study-clean/kernel/bpf/queue_stack_maps.c
File Facts
- System
- Linux kernel
- Corpus path
kernel/bpf/queue_stack_maps.c- Extension
.c- Size
- 7058 bytes
- Lines
- 289
- 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.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/bpf.hlinux/list.hlinux/slab.hlinux/btf_ids.hpercpu_freelist.hasm/rqspinlock.h
Detected Declarations
struct bpf_queue_stackfunction queue_stack_map_is_emptyfunction queue_stack_map_is_fullfunction queue_stack_map_alloc_checkfunction queue_stack_map_freefunction __queue_map_getfunction __stack_map_getfunction queue_map_peek_elemfunction stack_map_peek_elemfunction queue_map_pop_elemfunction stack_map_pop_elemfunction queue_stack_map_push_elemfunction queue_stack_map_update_elemfunction queue_stack_map_delete_elemfunction queue_stack_map_get_next_keyfunction queue_stack_map_mem_usage
Annotated Snippet
struct bpf_queue_stack {
struct bpf_map map;
rqspinlock_t lock;
u32 head, tail;
u32 size; /* max_entries + 1 */
char elements[] __aligned(8);
};
static struct bpf_queue_stack *bpf_queue_stack(struct bpf_map *map)
{
return container_of(map, struct bpf_queue_stack, map);
}
static bool queue_stack_map_is_empty(struct bpf_queue_stack *qs)
{
return qs->head == qs->tail;
}
static bool queue_stack_map_is_full(struct bpf_queue_stack *qs)
{
u32 head = qs->head + 1;
if (unlikely(head >= qs->size))
head = 0;
return head == qs->tail;
}
/* Called from syscall */
static int queue_stack_map_alloc_check(union bpf_attr *attr)
{
/* check sanity of attributes */
if (attr->max_entries == 0 || attr->key_size != 0 ||
attr->value_size == 0 ||
attr->map_flags & ~QUEUE_STACK_CREATE_FLAG_MASK ||
!bpf_map_flags_access_ok(attr->map_flags))
return -EINVAL;
if (attr->value_size > KMALLOC_MAX_SIZE)
/* if value_size is bigger, the user space won't be able to
* access the elements.
*/
return -E2BIG;
return 0;
}
static struct bpf_map *queue_stack_map_alloc(union bpf_attr *attr)
{
int numa_node = bpf_map_attr_numa_node(attr);
struct bpf_queue_stack *qs;
u64 size, queue_size;
size = (u64) attr->max_entries + 1;
queue_size = sizeof(*qs) + size * attr->value_size;
qs = bpf_map_area_alloc(queue_size, numa_node);
if (!qs)
return ERR_PTR(-ENOMEM);
bpf_map_init_from_attr(&qs->map, attr);
qs->size = size;
raw_res_spin_lock_init(&qs->lock);
return &qs->map;
}
/* Called when map->refcnt goes to zero, either from workqueue or from syscall */
static void queue_stack_map_free(struct bpf_map *map)
{
struct bpf_queue_stack *qs = bpf_queue_stack(map);
bpf_map_area_free(qs);
}
static long __queue_map_get(struct bpf_map *map, void *value, bool delete)
{
struct bpf_queue_stack *qs = bpf_queue_stack(map);
unsigned long flags;
int err = 0;
void *ptr;
if (raw_res_spin_lock_irqsave(&qs->lock, flags))
return -EBUSY;
if (queue_stack_map_is_empty(qs)) {
memset(value, 0, qs->map.value_size);
Annotation
- Immediate include surface: `linux/bpf.h`, `linux/list.h`, `linux/slab.h`, `linux/btf_ids.h`, `percpu_freelist.h`, `asm/rqspinlock.h`.
- Detected declarations: `struct bpf_queue_stack`, `function queue_stack_map_is_empty`, `function queue_stack_map_is_full`, `function queue_stack_map_alloc_check`, `function queue_stack_map_free`, `function __queue_map_get`, `function __stack_map_get`, `function queue_map_peek_elem`, `function stack_map_peek_elem`, `function queue_map_pop_elem`.
- 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.