kernel/bpf/btf.c
Source file repositories/reference/linux-study-clean/kernel/bpf/btf.c
File Facts
- System
- Linux kernel
- Corpus path
kernel/bpf/btf.c- Extension
.c- Size
- 262264 bytes
- Lines
- 9931
- Domain
- Core OS
- Bucket
- Scheduler, Processes, Timers, Sync, And Syscalls
- Inferred role
- Core OS: operation-table or driver-model contract
- Status
- pattern 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 an operation table; this is where Linux turns generic core objects into subsystem-specific behavior.
- 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.huapi/linux/bpf.huapi/linux/bpf_perf_event.huapi/linux/types.hlinux/seq_file.hlinux/compiler.hlinux/ctype.hlinux/errno.hlinux/slab.hlinux/anon_inodes.hlinux/file.hlinux/uaccess.hlinux/kernel.hlinux/idr.hlinux/sort.hlinux/bpf_verifier.hlinux/btf.hlinux/btf_ids.hlinux/bpf.hlinux/bpf_lsm.hlinux/skmsg.hlinux/perf_event.hlinux/bsearch.hlinux/kobject.hlinux/string.hlinux/sysfs.hlinux/overflow.hnet/netfilter/nf_bpf_link.hnet/sock.hnet/xdp.h../tools/lib/bpf/relo_core.hlinux/bpf_types.h
Detected Declarations
struct btf_kfunc_hook_filterstruct btf_kfunc_set_tabstruct btf_id_dtor_kfunc_tabstruct btf_struct_ops_tabstruct btfstruct resolve_vertexstruct btf_sec_infostruct btf_verifier_envstruct btf_showstruct btf_kind_operationsstruct btf_field_infostruct bpf_ctx_convertstruct user_regs_structstruct user_pt_regsstruct bpf_raw_tp_null_argsstruct bpf_cand_cachestruct btf_show_snprintfstruct btf_moduleenum btf_kfunc_hookenum verifier_phaseenum visit_stateenum resolve_modeenum bpf_struct_walk_resultenum btf_arg_tagfunction btf_type_is_modifierfunction btf_start_idfunction btf_type_is_voidfunction btf_type_is_datasecfunction btf_type_is_decl_tagfunction btf_type_nosizefunction btf_type_nosize_or_nullfunction btf_type_is_decl_tag_targetfunction btf_is_vmlinuxfunction btf_nr_typesfunction btf_check_sortedfunction btf_named_start_idfunction btf_find_by_name_kind_bsearchfunction btf_find_by_name_kindfunction bpf_find_btf_idfunction btf_type_is_resolve_source_onlyfunction btf_type_is_modifierfunction btf_type_has_sizefunction btf_type_intfunction btf_name_offset_validfunction __btf_name_char_okfunction btf_name_valid_identifierfunction btf_name_valid_sectionfunction __btf_type_int_is_regular
Annotated Snippet
const struct file_operations btf_fops = {
#ifdef CONFIG_PROC_FS
.show_fdinfo = bpf_btf_show_fdinfo,
#endif
.release = btf_release,
};
static int __btf_new_fd(struct btf *btf)
{
return anon_inode_getfd("btf", &btf_fops, btf, O_RDONLY | O_CLOEXEC);
}
int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr, struct bpf_log_attr *attr_log)
{
struct btf *btf;
int ret;
btf = btf_parse(attr, uattr, attr_log);
if (IS_ERR(btf))
return PTR_ERR(btf);
ret = btf_alloc_id(btf);
if (ret) {
btf_free(btf);
return ret;
}
/*
* The BTF ID is published to the userspace.
* All BTF free must go through call_rcu() from
* now on (i.e. free by calling btf_put()).
*/
ret = __btf_new_fd(btf);
if (ret < 0)
btf_put(btf);
return ret;
}
struct btf *btf_get_by_fd(int fd)
{
struct btf *btf;
CLASS(fd, f)(fd);
btf = __btf_get_by_fd(f);
if (!IS_ERR(btf))
refcount_inc(&btf->refcnt);
return btf;
}
int btf_get_info_by_fd(const struct btf *btf,
const union bpf_attr *attr,
union bpf_attr __user *uattr)
{
struct bpf_btf_info __user *uinfo;
struct bpf_btf_info info;
u32 info_copy, btf_copy;
void __user *ubtf;
char __user *uname;
u32 uinfo_len, uname_len, name_len;
int ret = 0;
uinfo = u64_to_user_ptr(attr->info.info);
uinfo_len = attr->info.info_len;
info_copy = min_t(u32, uinfo_len, sizeof(info));
memset(&info, 0, sizeof(info));
if (copy_from_user(&info, uinfo, info_copy))
return -EFAULT;
info.id = READ_ONCE(btf->id);
ubtf = u64_to_user_ptr(info.btf);
btf_copy = min_t(u32, btf->data_size, info.btf_size);
if (copy_to_user(ubtf, btf->data, btf_copy))
return -EFAULT;
info.btf_size = btf->data_size;
info.kernel_btf = btf->kernel_btf;
uname = u64_to_user_ptr(info.name);
uname_len = info.name_len;
if (!uname ^ !uname_len)
return -EINVAL;
name_len = strlen(btf->name);
info.name_len = name_len;
if (uname) {
Annotation
- Immediate include surface: `uapi/linux/btf.h`, `uapi/linux/bpf.h`, `uapi/linux/bpf_perf_event.h`, `uapi/linux/types.h`, `linux/seq_file.h`, `linux/compiler.h`, `linux/ctype.h`, `linux/errno.h`.
- Detected declarations: `struct btf_kfunc_hook_filter`, `struct btf_kfunc_set_tab`, `struct btf_id_dtor_kfunc_tab`, `struct btf_struct_ops_tab`, `struct btf`, `struct resolve_vertex`, `struct btf_sec_info`, `struct btf_verifier_env`, `struct btf_show`, `struct btf_kind_operations`.
- Atlas domain: Core OS / Scheduler, Processes, Timers, Sync, And Syscalls.
- Implementation status: pattern 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.