kernel/bpf/syscall.c
Source file repositories/reference/linux-study-clean/kernel/bpf/syscall.c
File Facts
- System
- Linux kernel
- Corpus path
kernel/bpf/syscall.c- Extension
.c- Size
- 174948 bytes
- Lines
- 6791
- Domain
- Core OS
- Bucket
- Scheduler, Processes, Timers, Sync, And Syscalls
- Inferred role
- Core OS: syscall or user/kernel boundary
- Status
- core 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.
- Defines or participates in a user/kernel boundary; inspect argument validation, copy_from_user/copy_to_user, credentials, and dispatch target.
- 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
crypto/sha2.hlinux/bpf.hlinux/bpf-cgroup.hlinux/bpf_trace.hlinux/bpf_lirc.hlinux/bpf_verifier.hlinux/bsearch.hlinux/btf.hlinux/hex.hlinux/syscalls.hlinux/slab.hlinux/sched/signal.hlinux/vmalloc.hlinux/mmzone.hlinux/anon_inodes.hlinux/fdtable.hlinux/file.hlinux/fs.hlinux/license.hlinux/filter.hlinux/kernel.hlinux/idr.hlinux/cred.hlinux/timekeeping.hlinux/ctype.hlinux/nospec.hlinux/audit.huapi/linux/btf.hlinux/pgtable.hlinux/bpf_lsm.hlinux/poll.hlinux/sort.h
Detected Declarations
syscall bpfstruct bpf_prog_kstatsstruct bpf_perf_linkenum bpf_auditfunction copy_from_userfunction bpf_map_write_active_incfunction bpf_map_write_active_decfunction bpf_map_write_activefunction bpf_map_value_sizefunction maybe_wait_bpf_programsfunction unpin_uptr_kaddrfunction __bpf_obj_unpin_uptrsfunction bpf_obj_unpin_uptrsfunction bpf_obj_pin_uptrsfunction bpf_map_update_valuefunction bpf_map_copy_valuefunction cgroupfunction bpf_map_area_freefunction bpf_map_flags_retain_permanentfunction bpf_map_init_from_attrfunction bpf_map_alloc_idfunction bpf_map_free_idfunction bpf_map_save_memcgfunction bpf_map_release_memcgfunction bpf_map_memcg_enterfunction bpf_map_memcg_exitfunction bpf_map_save_memcgfunction bpf_map_alloc_pagesfunction btf_field_cmpfunction btf_record_freefunction bpf_map_free_recordfunction btf_record_equalfunction bpf_obj_free_timerfunction bpf_obj_free_workqueuefunction bpf_obj_free_task_workfunction bpf_obj_cancel_fieldsfunction bpf_obj_free_fieldsfunction bpf_map_freefunction bpf_map_free_deferredfunction bpf_map_put_ureffunction bpf_map_free_in_workfunction bpf_map_free_rcu_gpfunction bpf_map_putfunction bpf_map_put_with_ureffunction bpf_map_releasefunction map_get_sys_permsfunction bpf_map_memory_usagefunction bpf_map_show_fdinfo
Annotated Snippet
SYSCALL_DEFINE5(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size,
struct bpf_common_attr __user *, uattr_common, unsigned int, size_common)
{
return __sys_bpf(cmd, USER_BPFPTR(uattr), size, USER_BPFPTR(uattr_common), size_common);
}
static bool syscall_prog_is_valid_access(int off, int size,
enum bpf_access_type type,
const struct bpf_prog *prog,
struct bpf_insn_access_aux *info)
{
if (off < 0 || off >= U16_MAX)
return false;
/* No alignment requirements for syscall ctx accesses. */
return true;
}
BPF_CALL_3(bpf_sys_bpf, int, cmd, union bpf_attr *, attr, u32, attr_size)
{
switch (cmd) {
case BPF_MAP_CREATE:
case BPF_MAP_DELETE_ELEM:
case BPF_MAP_UPDATE_ELEM:
case BPF_MAP_FREEZE:
case BPF_MAP_GET_FD_BY_ID:
case BPF_PROG_LOAD:
case BPF_BTF_LOAD:
case BPF_LINK_CREATE:
case BPF_RAW_TRACEPOINT_OPEN:
break;
default:
return -EINVAL;
}
return __sys_bpf(cmd, KERNEL_BPFPTR(attr), attr_size, KERNEL_BPFPTR(NULL), 0);
}
/* To shut up -Wmissing-prototypes.
* This function is used by the kernel light skeleton
* to load bpf programs when modules are loaded or during kernel boot.
* See tools/lib/bpf/skel_internal.h
*/
int kern_sys_bpf(int cmd, union bpf_attr *attr, unsigned int size);
int kern_sys_bpf(int cmd, union bpf_attr *attr, unsigned int size)
{
struct bpf_prog * __maybe_unused prog;
struct bpf_tramp_run_ctx __maybe_unused run_ctx;
switch (cmd) {
#ifdef CONFIG_BPF_JIT /* __bpf_prog_enter_sleepable used by trampoline and JIT */
case BPF_PROG_TEST_RUN:
if (attr->test.data_in || attr->test.data_out ||
attr->test.ctx_out || attr->test.duration ||
attr->test.repeat || attr->test.flags)
return -EINVAL;
prog = bpf_prog_get_type(attr->test.prog_fd, BPF_PROG_TYPE_SYSCALL);
if (IS_ERR(prog))
return PTR_ERR(prog);
if (attr->test.ctx_size_in < prog->aux->max_ctx_offset ||
attr->test.ctx_size_in > U16_MAX) {
bpf_prog_put(prog);
return -EINVAL;
}
run_ctx.bpf_cookie = 0;
if (!__bpf_prog_enter_sleepable_recur(prog, &run_ctx)) {
/* recursion detected */
__bpf_prog_exit_sleepable_recur(prog, 0, &run_ctx);
bpf_prog_put(prog);
return -EBUSY;
}
attr->test.retval = bpf_prog_run(prog, (void *) (long) attr->test.ctx_in);
__bpf_prog_exit_sleepable_recur(prog, 0 /* bpf_prog_run does runtime stats */,
&run_ctx);
bpf_prog_put(prog);
return 0;
#endif
default:
return ____bpf_sys_bpf(cmd, attr, size);
}
}
EXPORT_SYMBOL_NS(kern_sys_bpf, "BPF_INTERNAL");
static const struct bpf_func_proto bpf_sys_bpf_proto = {
.func = bpf_sys_bpf,
.gpl_only = false,
.ret_type = RET_INTEGER,
Annotation
- Immediate include surface: `crypto/sha2.h`, `linux/bpf.h`, `linux/bpf-cgroup.h`, `linux/bpf_trace.h`, `linux/bpf_lirc.h`, `linux/bpf_verifier.h`, `linux/bsearch.h`, `linux/btf.h`.
- Detected declarations: `syscall bpf`, `struct bpf_prog_kstats`, `struct bpf_perf_link`, `enum bpf_audit`, `function copy_from_user`, `function bpf_map_write_active_inc`, `function bpf_map_write_active_dec`, `function bpf_map_write_active`, `function bpf_map_value_size`, `function maybe_wait_bpf_programs`.
- Atlas domain: Core OS / Scheduler, Processes, Timers, Sync, And Syscalls.
- Implementation status: core 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.