Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bpf: Assign ID to vmlinux BTF and return extra info for BTF in GET_OBJ_INFO

Allocate ID for vmlinux BTF. This makes it visible when iterating over all BTF
objects in the system. To allow distinguishing vmlinux BTF (and later kernel
module BTF) from user-provided BTFs, expose extra kernel_btf flag, as well as
BTF name ("vmlinux" for vmlinux BTF, will equal to module's name for module
BTF). We might want to later allow specifying BTF name for user-provided BTFs
as well, if that makes sense. But currently this is reserved only for
in-kernel BTFs.

Having in-kernel BTFs exposed IDs will allow to extend BPF APIs that require
in-kernel BTF type with ability to specify BTF types from kernel modules, not
just vmlinux BTF. This will be implemented in a follow up patch set for
fentry/fexit/fmod_ret/lsm/etc.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Song Liu <songliubraving@fb.com>
Link: https://lore.kernel.org/bpf/20201110011932.3201430-3-andrii@kernel.org

authored by

Andrii Nakryiko and committed by
Alexei Starovoitov
53297220 951bb646

+46 -3
+3
include/uapi/linux/bpf.h
··· 4466 4466 __aligned_u64 btf; 4467 4467 __u32 btf_size; 4468 4468 __u32 id; 4469 + __aligned_u64 name; 4470 + __u32 name_len; 4471 + __u32 kernel_btf; 4469 4472 } __attribute__((aligned(8))); 4470 4473 4471 4474 struct bpf_link_info {
+40 -3
kernel/bpf/btf.c
··· 214 214 struct btf *base_btf; 215 215 u32 start_id; /* first type ID in this BTF (0 for base BTF) */ 216 216 u32 start_str_off; /* first string offset (0 for base BTF) */ 217 + char name[MODULE_NAME_LEN]; 218 + bool kernel_btf; 217 219 }; 218 220 219 221 enum verifier_phase { ··· 4431 4429 4432 4430 btf->data = __start_BTF; 4433 4431 btf->data_size = __stop_BTF - __start_BTF; 4432 + btf->kernel_btf = true; 4433 + snprintf(btf->name, sizeof(btf->name), "vmlinux"); 4434 4434 4435 4435 err = btf_parse_hdr(env); 4436 4436 if (err) ··· 4458 4454 4459 4455 bpf_struct_ops_init(btf, log); 4460 4456 4461 - btf_verifier_env_free(env); 4462 4457 refcount_set(&btf->refcnt, 1); 4458 + 4459 + err = btf_alloc_id(btf); 4460 + if (err) 4461 + goto errout; 4462 + 4463 + btf_verifier_env_free(env); 4463 4464 return btf; 4464 4465 4465 4466 errout: ··· 5562 5553 struct bpf_btf_info info; 5563 5554 u32 info_copy, btf_copy; 5564 5555 void __user *ubtf; 5565 - u32 uinfo_len; 5556 + char __user *uname; 5557 + u32 uinfo_len, uname_len, name_len; 5558 + int ret = 0; 5566 5559 5567 5560 uinfo = u64_to_user_ptr(attr->info.info); 5568 5561 uinfo_len = attr->info.info_len; ··· 5581 5570 return -EFAULT; 5582 5571 info.btf_size = btf->data_size; 5583 5572 5573 + info.kernel_btf = btf->kernel_btf; 5574 + 5575 + uname = u64_to_user_ptr(info.name); 5576 + uname_len = info.name_len; 5577 + if (!uname ^ !uname_len) 5578 + return -EINVAL; 5579 + 5580 + name_len = strlen(btf->name); 5581 + info.name_len = name_len; 5582 + 5583 + if (uname) { 5584 + if (uname_len >= name_len + 1) { 5585 + if (copy_to_user(uname, btf->name, name_len + 1)) 5586 + return -EFAULT; 5587 + } else { 5588 + char zero = '\0'; 5589 + 5590 + if (copy_to_user(uname, btf->name, uname_len - 1)) 5591 + return -EFAULT; 5592 + if (put_user(zero, uname + uname_len - 1)) 5593 + return -EFAULT; 5594 + /* let user-space know about too short buffer */ 5595 + ret = -ENOSPC; 5596 + } 5597 + } 5598 + 5584 5599 if (copy_to_user(uinfo, &info, info_copy) || 5585 5600 put_user(info_copy, &uattr->info.info_len)) 5586 5601 return -EFAULT; 5587 5602 5588 - return 0; 5603 + return ret; 5589 5604 } 5590 5605 5591 5606 int btf_get_fd_by_id(u32 id)
+3
tools/include/uapi/linux/bpf.h
··· 4466 4466 __aligned_u64 btf; 4467 4467 __u32 btf_size; 4468 4468 __u32 id; 4469 + __aligned_u64 name; 4470 + __u32 name_len; 4471 + __u32 kernel_btf; 4469 4472 } __attribute__((aligned(8))); 4470 4473 4471 4474 struct bpf_link_info {