History

bpf: pass attached BTF to the bpf_struct_ops subsystem

Pass the fd of a btf from the userspace to the bpf() syscall, and then convert the fd into a btf. The btf is generated from the module that defines the target BPF struct_ops type. In order to inform the kernel about the module that defines the target struct_ops type, the userspace program needs to provide a btf fd for the respective module's btf. This btf contains essential information on the types defined within the module, including the target struct_ops type. A btf fd must be provided to the kernel for struct_ops maps and for the bpf programs attached to those maps. In the case of the bpf programs, the attach_btf_obj_fd parameter is passed as part of the bpf_attr and is converted into a btf. This btf is then stored in the prog->aux->attach_btf field. Here, it just let the verifier access attach_btf directly. In the case of struct_ops maps, a btf fd is passed as value_type_btf_obj_fd of bpf_attr. The bpf_struct_ops_map_alloc() function converts the fd to a btf and stores it as st_map->btf. A flag BPF_F_VTYPE_BTF_OBJ_FD is added for map_flags to indicate that the value of value_type_btf_obj_fd is set.

Kui-Feng Lee committed 2 years ago

Commit fcc2c1f1 parent 689423d5 files · +70 −22Browse files

Unified · Split

5 files
  1. include/uapi/linux/bpf.h +8 −0
  2. kernel/bpf/bpf_struct_ops.c +47 −18
  3. kernel/bpf/syscall.c +1 −1
  4. kernel/bpf/verifier.c +6 −3
  5. tools/include/uapi/linux/bpf.h +8 −0
include/uapi/linux/bpf.h+8−0
@@ -1330,6 +1330,9 @@ enum { · 1329 unmodified lines
13301330
13311331 /* Get path from provided FD in BPF_OBJ_PIN/BPF_OBJ_GET commands */
13321332 BPF_F_PATH_FD = (1U << 14),
1333+
1334+/* Flag for value_type_btf_obj_fd, the fd is available */
1335+ BPF_F_VTYPE_BTF_OBJ_FD = (1U << 15),
13331336 };
13341337
13351338 /* Flags for BPF_PROG_QUERY. */
@@ -1403,6 +1406,11 @@ union bpf_attr { · 67 unmodified lines
14031406 * to using 5 hash functions).
14041407 */
14051408 __u64 map_extra;
1409+
1410+ __s32 value_type_btf_obj_fd; /* fd pointing to a BTF
1411+ * type data for
1412+ * btf_vmlinux_value_type_id.
1413+ */
14061414 };
14071415
14081416 struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
kernel/bpf/bpf_struct_ops.c+47−18
@@ -641,6 +641,7 @@ static void __bpf_struct_ops_map_free(struct bpf_map *map) · 640 unmodified lines
641641 bpf_jit_uncharge_modmem(PAGE_SIZE);
642642 }
643643 bpf_map_area_free(st_map->uvalue);
644+ btf_put(st_map->btf);
644645 bpf_map_area_free(st_map);
645646 }
646647
@@ -669,7 +670,8 @@ static void bpf_struct_ops_map_free(struct bpf_map *map) · 22 unmodified lines
669670 static int bpf_struct_ops_map_alloc_check(union bpf_attr *attr)
670671 {
671672 if (attr->key_size != sizeof(unsigned int) || attr->max_entries != 1 ||
672− (attr->map_flags & ~BPF_F_LINK) || !attr->btf_vmlinux_value_type_id)
673+ (attr->map_flags & ~(BPF_F_LINK | BPF_F_VTYPE_BTF_OBJ_FD)) ||
674+ !attr->btf_vmlinux_value_type_id)
673675 return -EINVAL;
674676 return 0;
675677 }
@@ -681,15 +683,36 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr) · 5 unmodified lines
681683 struct bpf_struct_ops_map *st_map;
682684 const struct btf_type *t, *vt;
683685 struct bpf_map *map;
686+ struct btf *btf;
684687 int ret;
685688
686− st_ops_desc = bpf_struct_ops_find_value(btf_vmlinux, attr->btf_vmlinux_value_type_id);
687− if (!st_ops_desc)
688− return ERR_PTR(-ENOTSUPP);
689+ if (attr->map_flags & BPF_F_VTYPE_BTF_OBJ_FD) {
690+ /* The map holds btf for its whole life time. */
691+ btf = btf_get_by_fd(attr->value_type_btf_obj_fd);
692+ if (IS_ERR(btf))
693+ return ERR_CAST(btf);
694+ if (!btf_is_module(btf)) {
695+ btf_put(btf);
696+ return ERR_PTR(-EINVAL);
697+ }
698+ } else {
699+ btf = bpf_get_btf_vmlinux();
700+ if (IS_ERR(btf))
701+ return ERR_CAST(btf);
702+ btf_get(btf);
703+ }
704+
705+ st_ops_desc = bpf_struct_ops_find_value(btf, attr->btf_vmlinux_value_type_id);
706+ if (!st_ops_desc) {
707+ ret = -ENOTSUPP;
708+ goto errout;
709+ }
689710
690711 vt = st_ops_desc->value_type;
691− if (attr->value_size != vt->size)
692− return ERR_PTR(-EINVAL);
712+ if (attr->value_size != vt->size) {
713+ ret = -EINVAL;
714+ goto errout;
715+ }
693716
694717 t = st_ops_desc->type;
695718
@@ -700,17 +723,17 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr) · 4 unmodified lines
700723 (vt->size - sizeof(struct bpf_struct_ops_value));
701724
702725 st_map = bpf_map_area_alloc(st_map_size, NUMA_NO_NODE);
703− if (!st_map)
704− return ERR_PTR(-ENOMEM);
726+ if (!st_map) {
727+ ret = -ENOMEM;
728+ goto errout;
729+ }
705730
706731 st_map->st_ops_desc = st_ops_desc;
707732 map = &st_map->map;
708733
709734 ret = bpf_jit_charge_modmem(PAGE_SIZE);
710− if (ret) {
711− __bpf_struct_ops_map_free(map);
712− return ERR_PTR(ret);
713− }
735+ if (ret)
736+ goto errout_free;
714737
715738 st_map->image = arch_alloc_bpf_trampoline(PAGE_SIZE);
716739 if (!st_map->image) {
@@ -719,24 +742,30 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr) · 2 unmodified lines
719742 * here.
720743 */
721744 bpf_jit_uncharge_modmem(PAGE_SIZE);
722− __bpf_struct_ops_map_free(map);
723− return ERR_PTR(-ENOMEM);
745+ ret = -ENOMEM;
746+ goto errout_free;
724747 }
725748 st_map->uvalue = bpf_map_area_alloc(vt->size, NUMA_NO_NODE);
726749 st_map->links =
727750 bpf_map_area_alloc(btf_type_vlen(t) * sizeof(struct bpf_links *),
728751 NUMA_NO_NODE);
729752 if (!st_map->uvalue || !st_map->links) {
730− __bpf_struct_ops_map_free(map);
731− return ERR_PTR(-ENOMEM);
753+ ret = -ENOMEM;
754+ goto errout_free;
732755 }
733−
734− st_map->btf = btf_vmlinux;
756+ st_map->btf = btf;
735757
736758 mutex_init(&st_map->lock);
737759 bpf_map_init_from_attr(map, attr);
738760
739761 return map;
762+
763+errout_free:
764+ __bpf_struct_ops_map_free(map);
765+errout:
766+ btf_put(btf);
767+
768+ return ERR_PTR(ret);
740769 }
741770
742771 static u64 bpf_struct_ops_map_mem_usage(const struct bpf_map *map)
kernel/bpf/syscall.c+1−1
@@ -1123,7 +1123,7 @@ free_map_tab: · 1122 unmodified lines
11231123 return ret;
11241124 }
11251125
1126−#define BPF_MAP_CREATE_LAST_FIELD map_extra
1126+#define BPF_MAP_CREATE_LAST_FIELD value_type_btf_obj_fd
11271127 /* called via syscall */
11281128 static int map_create(union bpf_attr *attr)
11291129 {
kernel/bpf/verifier.c+6−3
@@ -20290,6 +20290,7 @@ static int check_struct_ops_btf_id(struct bpf_verifier_env *env) · 20289 unmodified lines
2029020290 const struct btf_member *member;
2029120291 struct bpf_prog *prog = env->prog;
2029220292 u32 btf_id, member_idx;
20293+ struct btf *btf;
2029320294 const char *mname;
2029420295
2029520296 if (!prog->gpl_compatible) {
@@ -20297,8 +20298,10 @@ static int check_struct_ops_btf_id(struct bpf_verifier_env *env) · 1 unmodified line
2029720298 return -EINVAL;
2029820299 }
2029920300
20301+ btf = prog->aux->attach_btf ?: bpf_get_btf_vmlinux();
20302+
2030020303 btf_id = prog->aux->attach_btf_id;
20301− st_ops_desc = bpf_struct_ops_find(btf_vmlinux, btf_id);
20304+ st_ops_desc = bpf_struct_ops_find(btf, btf_id);
2030220305 if (!st_ops_desc) {
2030320306 verbose(env, "attach_btf_id %u is not a supported struct\n",
2030420307 btf_id);
@@ -20315,8 +20318,8 @@ static int check_struct_ops_btf_id(struct bpf_verifier_env *env) · 10 unmodified lines
2031520318 }
2031620319
2031720320 member = &btf_type_member(t)[member_idx];
20318− mname = btf_name_by_offset(btf_vmlinux, member->name_off);
20319− func_proto = btf_type_resolve_func_ptr(btf_vmlinux, member->type,
20321+ mname = btf_name_by_offset(btf, member->name_off);
20322+ func_proto = btf_type_resolve_func_ptr(btf, member->type,
2032020323 NULL);
2032120324 if (!func_proto) {
2032220325 verbose(env, "attach to invalid member %s(@idx %u) of struct %s\n",
tools/include/uapi/linux/bpf.h+8−0
@@ -1330,6 +1330,9 @@ enum { · 1329 unmodified lines
13301330
13311331 /* Get path from provided FD in BPF_OBJ_PIN/BPF_OBJ_GET commands */
13321332 BPF_F_PATH_FD = (1U << 14),
1333+
1334+/* Flag for value_type_btf_obj_fd, the fd is available */
1335+ BPF_F_VTYPE_BTF_OBJ_FD = (1U << 15),
13331336 };
13341337
13351338 /* Flags for BPF_PROG_QUERY. */
@@ -1403,6 +1406,11 @@ union bpf_attr { · 67 unmodified lines
14031406 * to using 5 hash functions).
14041407 */
14051408 __u64 map_extra;
1409+
1410+ __s32 value_type_btf_obj_fd; /* fd pointing to a BTF
1411+ * type data for
1412+ * btf_vmlinux_value_type_id.
1413+ */
14061414 };
14071415
14081416 struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t