kernel/bpf/helpers.c
Source file repositories/reference/linux-study-clean/kernel/bpf/helpers.c
File Facts
- System
- Linux kernel
- Corpus path
kernel/bpf/helpers.c- Extension
.c- Size
- 141752 bytes
- Lines
- 5038
- 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
linux/bpf.hlinux/btf.hlinux/bpf-cgroup.hlinux/cgroup.hlinux/rcupdate.hlinux/random.hlinux/smp.hlinux/topology.hlinux/ktime.hlinux/sched.hlinux/uidgid.hlinux/filter.hlinux/ctype.hlinux/jiffies.hlinux/pid_namespace.hlinux/poison.hlinux/proc_ns.hlinux/sched/task.hlinux/security.hlinux/btf_ids.hlinux/bpf_mem_alloc.hlinux/kasan.hlinux/bpf_verifier.hlinux/uaccess.hlinux/verification.hlinux/task_work.hlinux/irq_work.hlinux/buildid.h../../lib/kstrtox.h
Detected Declarations
struct bpf_async_cmdstruct bpf_async_cbstruct bpf_hrtimerstruct bpf_workstruct bpf_async_kernstruct bpf_dynptr_file_implstruct bpf_throw_ctxstruct bpf_iter_bitsstruct bpf_iter_bits_kernstruct bpf_task_work_ctxstruct bpf_task_work_kernenum bpf_async_typeenum bpf_async_openum bpf_task_work_statefunction rcu_read_lock_trace_heldfunction __bpf_spin_lockfunction __bpf_spin_unlockfunction __bpf_spin_lockfunction __bpf_spin_unlockfunction __bpf_spin_lock_irqsavefunction __bpf_spin_unlock_irqrestorefunction copy_map_value_lockedfunction __bpf_strtoullfunction __bpf_strtollfunction bpf_trace_copy_stringfunction bpf_try_get_buffersfunction bpf_put_buffersfunction bpf_bprintf_cleanupfunction bpf_bprintf_preparefunction bpf_timer_cbfunction bpf_wq_workfunction bpf_async_cb_rcu_freefunction bpf_async_cb_rcu_tasks_trace_freefunction bpf_async_cancel_and_freefunction worker_for_call_rcufunction bpf_async_refcount_putfunction __bpf_async_initfunction bpf_async_update_prog_callbackfunction bpf_async_schedule_opfunction __bpf_async_set_callbackfunction defer_timer_wq_opfunction bpf_async_process_opfunction bpf_async_irq_workerfunction bpf_async_cancel_and_freefunction refcount_inc_not_zerofunction bpf_async_schedule_opfunction bpf_wq_cancel_and_freefunction __bpf_dynptr_is_rdonly
Annotated Snippet
struct bpf_async_cmd {
struct llist_node node;
u64 nsec;
u32 mode;
enum bpf_async_op op;
};
struct bpf_async_cb {
struct bpf_map *map;
struct bpf_prog *prog;
void __rcu *callback_fn;
void *value;
struct rcu_head rcu;
u64 flags;
struct irq_work worker;
refcount_t refcnt;
enum bpf_async_type type;
struct llist_head async_cmds;
};
/* BPF map elements can contain 'struct bpf_timer'.
* Such map owns all of its BPF timers.
* 'struct bpf_timer' is allocated as part of map element allocation
* and it's zero initialized.
* That space is used to keep 'struct bpf_async_kern'.
* bpf_timer_init() allocates 'struct bpf_hrtimer', inits hrtimer, and
* remembers 'struct bpf_map *' pointer it's part of.
* bpf_timer_set_callback() increments prog refcnt and assign bpf callback_fn.
* bpf_timer_start() arms the timer.
* If user space reference to a map goes to zero at this point
* ops->map_release_uref callback is responsible for cancelling the timers,
* freeing their memory, and decrementing prog's refcnts.
* bpf_timer_cancel() cancels the timer and decrements prog's refcnt.
* Inner maps can contain bpf timers as well. ops->map_release_uref is
* freeing the timers when inner map is replaced or deleted by user space.
*/
struct bpf_hrtimer {
struct bpf_async_cb cb;
struct hrtimer timer;
atomic_t cancelling;
};
struct bpf_work {
struct bpf_async_cb cb;
struct work_struct work;
};
/* the actual struct hidden inside uapi struct bpf_timer and bpf_wq */
struct bpf_async_kern {
union {
struct bpf_async_cb *cb;
struct bpf_hrtimer *timer;
struct bpf_work *work;
};
} __attribute__((aligned(8)));
static DEFINE_PER_CPU(struct bpf_hrtimer *, hrtimer_running);
static void bpf_async_refcount_put(struct bpf_async_cb *cb);
static enum hrtimer_restart bpf_timer_cb(struct hrtimer *hrtimer)
{
struct bpf_hrtimer *t = container_of(hrtimer, struct bpf_hrtimer, timer);
struct bpf_map *map = t->cb.map;
void *value = t->cb.value;
bpf_callback_t callback_fn;
void *key;
u32 idx;
BTF_TYPE_EMIT(struct bpf_timer);
callback_fn = rcu_dereference_check(t->cb.callback_fn, rcu_read_lock_bh_held());
if (!callback_fn)
goto out;
/* bpf_timer_cb() runs in hrtimer_run_softirq. It doesn't migrate and
* cannot be preempted by another bpf_timer_cb() on the same cpu.
* Remember the timer this callback is servicing to prevent
* deadlock if callback_fn() calls bpf_timer_cancel() or
* bpf_map_delete_elem() on the same timer.
*/
this_cpu_write(hrtimer_running, t);
key = map_key_from_value(map, value, &idx);
callback_fn((u64)(long)map, (u64)(long)key, (u64)(long)value, 0, 0);
/* The verifier checked that return value is zero. */
this_cpu_write(hrtimer_running, NULL);
out:
return HRTIMER_NORESTART;
Annotation
- Immediate include surface: `linux/bpf.h`, `linux/btf.h`, `linux/bpf-cgroup.h`, `linux/cgroup.h`, `linux/rcupdate.h`, `linux/random.h`, `linux/smp.h`, `linux/topology.h`.
- Detected declarations: `struct bpf_async_cmd`, `struct bpf_async_cb`, `struct bpf_hrtimer`, `struct bpf_work`, `struct bpf_async_kern`, `struct bpf_dynptr_file_impl`, `struct bpf_throw_ctx`, `struct bpf_iter_bits`, `struct bpf_iter_bits_kern`, `struct bpf_task_work_ctx`.
- 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.