Skip to content

Commit 955bba7

Browse files
committed
Merge branch 'bpf-enable-some-functions-in-cgroup-programs'
Matteo Croce says: ==================== bpf: enable some functions in cgroup programs From: Matteo Croce <[email protected]> Enable some BPF kfuncs and the helper bpf_current_task_under_cgroup() for program types BPF_CGROUP_*. These will be used by systemd-networkd: systemd/systemd#32212 v5->v6: Called register_btf_kfunc_id_set() only once Fixed build error with !CONFIG_CGROUPS v4->v5: Same code, but v4 had an old cover letter v3->v4: Reset all the acked-by tags because the code changed a bit. Signed-off-by: Matteo Croce <[email protected]> ==================== Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Andrii Nakryiko <[email protected]>
2 parents fdf1c72 + 7f62874 commit 955bba7

File tree

5 files changed

+35
-27
lines changed

5 files changed

+35
-27
lines changed

include/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3206,6 +3206,7 @@ extern const struct bpf_func_proto bpf_sock_hash_update_proto;
32063206
extern const struct bpf_func_proto bpf_get_current_cgroup_id_proto;
32073207
extern const struct bpf_func_proto bpf_get_current_ancestor_cgroup_id_proto;
32083208
extern const struct bpf_func_proto bpf_get_cgroup_classid_curr_proto;
3209+
extern const struct bpf_func_proto bpf_current_task_under_cgroup_proto;
32093210
extern const struct bpf_func_proto bpf_msg_redirect_hash_proto;
32103211
extern const struct bpf_func_proto bpf_msg_redirect_map_proto;
32113212
extern const struct bpf_func_proto bpf_sk_redirect_hash_proto;

kernel/bpf/btf.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ enum btf_kfunc_hook {
212212
BTF_KFUNC_HOOK_TRACING,
213213
BTF_KFUNC_HOOK_SYSCALL,
214214
BTF_KFUNC_HOOK_FMODRET,
215-
BTF_KFUNC_HOOK_CGROUP_SKB,
215+
BTF_KFUNC_HOOK_CGROUP,
216216
BTF_KFUNC_HOOK_SCHED_ACT,
217217
BTF_KFUNC_HOOK_SK_SKB,
218218
BTF_KFUNC_HOOK_SOCKET_FILTER,
@@ -8307,8 +8307,12 @@ static int bpf_prog_type_to_kfunc_hook(enum bpf_prog_type prog_type)
83078307
case BPF_PROG_TYPE_SYSCALL:
83088308
return BTF_KFUNC_HOOK_SYSCALL;
83098309
case BPF_PROG_TYPE_CGROUP_SKB:
8310+
case BPF_PROG_TYPE_CGROUP_SOCK:
8311+
case BPF_PROG_TYPE_CGROUP_DEVICE:
83108312
case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
8311-
return BTF_KFUNC_HOOK_CGROUP_SKB;
8313+
case BPF_PROG_TYPE_CGROUP_SOCKOPT:
8314+
case BPF_PROG_TYPE_CGROUP_SYSCTL:
8315+
return BTF_KFUNC_HOOK_CGROUP;
83128316
case BPF_PROG_TYPE_SCHED_ACT:
83138317
return BTF_KFUNC_HOOK_SCHED_ACT;
83148318
case BPF_PROG_TYPE_SK_SKB:

kernel/bpf/cgroup.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2581,6 +2581,8 @@ cgroup_current_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
25812581
case BPF_FUNC_get_cgroup_classid:
25822582
return &bpf_get_cgroup_classid_curr_proto;
25832583
#endif
2584+
case BPF_FUNC_current_task_under_cgroup:
2585+
return &bpf_current_task_under_cgroup_proto;
25842586
default:
25852587
return NULL;
25862588
}

kernel/bpf/helpers.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2458,6 +2458,29 @@ __bpf_kfunc long bpf_task_under_cgroup(struct task_struct *task,
24582458
return ret;
24592459
}
24602460

2461+
BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
2462+
{
2463+
struct bpf_array *array = container_of(map, struct bpf_array, map);
2464+
struct cgroup *cgrp;
2465+
2466+
if (unlikely(idx >= array->map.max_entries))
2467+
return -E2BIG;
2468+
2469+
cgrp = READ_ONCE(array->ptrs[idx]);
2470+
if (unlikely(!cgrp))
2471+
return -EAGAIN;
2472+
2473+
return task_under_cgroup_hierarchy(current, cgrp);
2474+
}
2475+
2476+
const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
2477+
.func = bpf_current_task_under_cgroup,
2478+
.gpl_only = false,
2479+
.ret_type = RET_INTEGER,
2480+
.arg1_type = ARG_CONST_MAP_PTR,
2481+
.arg2_type = ARG_ANYTHING,
2482+
};
2483+
24612484
/**
24622485
* bpf_task_get_cgroup1 - Acquires the associated cgroup of a task within a
24632486
* specific cgroup1 hierarchy. The cgroup1 hierarchy is identified by its
@@ -3052,6 +3075,7 @@ static int __init kfunc_init(void)
30523075
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP, &generic_kfunc_set);
30533076
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &generic_kfunc_set);
30543077
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &generic_kfunc_set);
3078+
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SKB, &generic_kfunc_set);
30553079
ret = ret ?: register_btf_id_dtor_kfuncs(generic_dtors,
30563080
ARRAY_SIZE(generic_dtors),
30573081
THIS_MODULE);

kernel/trace/bpf_trace.c

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -797,29 +797,6 @@ const struct bpf_func_proto bpf_task_pt_regs_proto = {
797797
.ret_btf_id = &bpf_task_pt_regs_ids[0],
798798
};
799799

800-
BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
801-
{
802-
struct bpf_array *array = container_of(map, struct bpf_array, map);
803-
struct cgroup *cgrp;
804-
805-
if (unlikely(idx >= array->map.max_entries))
806-
return -E2BIG;
807-
808-
cgrp = READ_ONCE(array->ptrs[idx]);
809-
if (unlikely(!cgrp))
810-
return -EAGAIN;
811-
812-
return task_under_cgroup_hierarchy(current, cgrp);
813-
}
814-
815-
static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
816-
.func = bpf_current_task_under_cgroup,
817-
.gpl_only = false,
818-
.ret_type = RET_INTEGER,
819-
.arg1_type = ARG_CONST_MAP_PTR,
820-
.arg2_type = ARG_ANYTHING,
821-
};
822-
823800
struct send_signal_irq_work {
824801
struct irq_work irq_work;
825802
struct task_struct *task;
@@ -1480,8 +1457,6 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
14801457
return &bpf_get_numa_node_id_proto;
14811458
case BPF_FUNC_perf_event_read:
14821459
return &bpf_perf_event_read_proto;
1483-
case BPF_FUNC_current_task_under_cgroup:
1484-
return &bpf_current_task_under_cgroup_proto;
14851460
case BPF_FUNC_get_prandom_u32:
14861461
return &bpf_get_prandom_u32_proto;
14871462
case BPF_FUNC_probe_write_user:
@@ -1510,6 +1485,8 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
15101485
return &bpf_cgrp_storage_get_proto;
15111486
case BPF_FUNC_cgrp_storage_delete:
15121487
return &bpf_cgrp_storage_delete_proto;
1488+
case BPF_FUNC_current_task_under_cgroup:
1489+
return &bpf_current_task_under_cgroup_proto;
15131490
#endif
15141491
case BPF_FUNC_send_signal:
15151492
return &bpf_send_signal_proto;

0 commit comments

Comments
 (0)