Skip to content

Commit 7f62874

Browse files
teknoraveranakryiko
authored andcommitted
bpf: Allow bpf_current_task_under_cgroup() with BPF_CGROUP_*
The helper bpf_current_task_under_cgroup() currently is only allowed for tracing programs, allow its usage also in the BPF_CGROUP_* program types. Move the code from kernel/trace/bpf_trace.c to kernel/bpf/helpers.c, so it compiles also without CONFIG_BPF_EVENTS. This will be used in systemd-networkd to monitor the sysctl writes, and filter it's own writes from others: systemd/systemd#32212 Signed-off-by: Matteo Croce <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 6766647 commit 7f62874

File tree

4 files changed

+28
-25
lines changed

4 files changed

+28
-25
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/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: 23 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

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)