Skip to contentSkip to conversation

bpf: enable some functions in cgroup programs

PATCH Superseded [email protected] 2 participants 5 messages Jul 25, 2024 – Aug 12, 2024

Subsystems (5)
BPF Tracing BPF core BPF tracing +1 more subsystems

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v3 cover 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:
https://github.com/systemd/systemd/pull/32212

Matteo Croce (2):
bpf: enable generic kfuncs for BPF_CGROUP_* programs
bpf: allow bpf_current_task_under_cgroup() with BPF_CGROUP_*

 include/linux/bpf.h      |  1 +
 kernel/bpf/cgroup.c      |  2 ++
 kernel/bpf/helpers.c     | 29 +++++++++++++++++++++++++++++
 kernel/trace/bpf_trace.c | 23 -----------------------
 4 files changed, 32 insertions(+), 23 deletions(-)
Signature · 1 line
2.45.2
#1 lore raw

PATCH v3 1/2 bpf: enable generic kfuncs for BPF_CGROUP_* programs

From: Matteo Croce <[email protected]>

These kfuncs are enabled even in BPF_PROG_TYPE_TRACING, so they should be safe also in BPF_CGROUP_* programs.

---
 kernel/bpf/helpers.c | 6 ++++++
 1 file changed, 6 insertions(+)
kernel/bpf/helpers.c +6 -0
index d02ae323996b..0d1d97d968b0 100644
@@ -3052,6 +3052,12 @@ static int __init kfunc_init(void)
 	ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP, &generic_kfunc_set);
 	ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &generic_kfunc_set);
 	ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &generic_kfunc_set);
+	ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SKB, &generic_kfunc_set);
+	ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SOCK, &generic_kfunc_set);
+	ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_DEVICE, &generic_kfunc_set);
+	ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SOCK_ADDR, &generic_kfunc_set);
+	ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SYSCTL, &generic_kfunc_set);
+	ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SOCKOPT, &generic_kfunc_set);
 	ret = ret ?: register_btf_id_dtor_kfuncs(generic_dtors,
 						  ARRAY_SIZE(generic_dtors),
 						  THIS_MODULE);
Signature · 1 line
2.45.2
#2 lore raw

Re: [PATCH bpf-next v3 1/2] bpf: enable generic kfuncs for BPF_CGROUP_* programs

Il giorno gio 25 lug 2024 alle ore 02:14 <[email protected]> ha scritto:

Quoted 5 lines
kernel/bpf/helpers.c +0 -0
@@ -3052,6 +3052,12 @@ static int __init kfunc_init(void)
        ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP, &generic_kfunc_set);
        ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &generic_kfunc_set);

kernel/bpf/helpers.c static int __init kfunc_init(void) · line 3063

Quoted 10 lines
kernel/bpf/helpers.c +6 -0
        ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &generic_kfunc_set);
+       ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SKB, &generic_kfunc_set);
+       ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SOCK, &generic_kfunc_set);
+       ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_DEVICE, &generic_kfunc_set);
+       ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SOCK_ADDR, &generic_kfunc_set);
+       ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SYSCTL, &generic_kfunc_set);
+       ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SOCKOPT, &generic_kfunc_set);
        ret = ret ?: register_btf_id_dtor_kfuncs(generic_dtors,
                                                  ARRAY_SIZE(generic_dtors),
                                                  THIS_MODULE);

This seems not enough, some kfuncs like bpf_cgroup_from_id are still rejected. To fix this we need also this chunk:

kernel/bpf/btf.c +0 -0
@@ -8309,7 +8319,11 @@ static int bpf_prog_type_to_kfunc_hook(enum

bpf_prog_type prog_type)

        case BPF_PROG_TYPE_SYSCALL:
                return BTF_KFUNC_HOOK_SYSCALL;
        case BPF_PROG_TYPE_CGROUP_SKB:

+ case BPF_PROG_TYPE_CGROUP_SOCK:
+ case BPF_PROG_TYPE_CGROUP_DEVICE:
case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
+ case BPF_PROG_TYPE_CGROUP_SYSCTL:
+ case BPF_PROG_TYPE_CGROUP_SOCKOPT:

                return BTF_KFUNC_HOOK_CGROUP_SKB;
        case BPF_PROG_TYPE_SCHED_ACT:
                return BTF_KFUNC_HOOK_SCHED_ACT;

but even with this it won't work, because bpf_prog_type_to_kfunc_hook() aliases many program types with a single hook, and btf_kfunc_id_set_contains() will fail to check if the kfunc is in the set.

One solution could be to extend the btf_kfunc_hook enum with an entry for every CGROUP program type, but a thing I wanted to avoid is to let this enum proliferate in this way.
I wish to group all the CGROUP_ program types into a single hook, and perhaps drop the "SKB" in "BTF_KFUNC_HOOK_CGROUP_SKB" which will have no meaning anymore.

Ideas?

Signature · 3 lines
Matteo Croce

perl -e 'for($t=0;;$t++){print chr($t*($t>>8|$t>>13)&255)}' |aplay
#3 lore raw

Re: [PATCH bpf-next v3 1/2] bpf: enable generic kfuncs for BPF_CGROUP_* programs

Quoted 42 lines · Matteo Croce

Il giorno gio 25 lug 2024 alle ore 02:14 <[email protected]> ha scritto:

Quoted 5 lines
kernel/bpf/helpers.c +0 -0
@@ -3052,6 +3052,12 @@ static int __init kfunc_init(void)
        ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP, &generic_kfunc_set);
        ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &generic_kfunc_set);

kernel/bpf/helpers.c static int __init kfunc_init(void) · line 3063

Quoted 10 lines
kernel/bpf/helpers.c +6 -0
        ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &generic_kfunc_set);
+       ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SKB, &generic_kfunc_set);
+       ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SOCK, &generic_kfunc_set);
+       ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_DEVICE, &generic_kfunc_set);
+       ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SOCK_ADDR, &generic_kfunc_set);
+       ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SYSCTL, &generic_kfunc_set);
+       ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SOCKOPT, &generic_kfunc_set);
        ret = ret ?: register_btf_id_dtor_kfuncs(generic_dtors,
                                                  ARRAY_SIZE(generic_dtors),
                                                  THIS_MODULE);

This seems not enough, some kfuncs like bpf_cgroup_from_id are still rejected. To fix this we need also this chunk:

kernel/bpf/btf.c +4 -0
@@ -8309,7 +8319,11 @@ static int bpf_prog_type_to_kfunc_hook(enum
 bpf_prog_type prog_type)
        case BPF_PROG_TYPE_SYSCALL:
                return BTF_KFUNC_HOOK_SYSCALL;
        case BPF_PROG_TYPE_CGROUP_SKB:
+       case BPF_PROG_TYPE_CGROUP_SOCK:
+       case BPF_PROG_TYPE_CGROUP_DEVICE:
        case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
+       case BPF_PROG_TYPE_CGROUP_SYSCTL:
+       case BPF_PROG_TYPE_CGROUP_SOCKOPT:
                return BTF_KFUNC_HOOK_CGROUP_SKB;
        case BPF_PROG_TYPE_SCHED_ACT:
                return BTF_KFUNC_HOOK_SCHED_ACT;

but even with this it won't work, because bpf_prog_type_to_kfunc_hook() aliases many program types with a single hook, and btf_kfunc_id_set_contains() will fail to check if the kfunc is in the set.

Quoted 8 lines

One solution could be to extend the btf_kfunc_hook enum with an entry for every CGROUP program type, but a thing I wanted to avoid is to let this enum proliferate in this way.
I wish to group all the CGROUP_ program types into a single hook, and perhaps drop the "SKB" in "BTF_KFUNC_HOOK_CGROUP_SKB" which will have no meaning anymore.

Ideas?

Given BPF_PROG_TYPE_CGROUP_SKB and BPF_PROG_TYPE_CGROUP_SOCK_ADDR are already grouped into the same hook type, and bpf_sock_addr_set_sun_path() is meant to be enabled for BPF_PROG_TYPE_CGROUP_SOCK_ADDR (but really it's enabled for BPF_PROG_TYPE_CGROUP_SKB as well), I guess it's fine to just have one CGROUP hook type for all those cgroup-related program types and just rely on BTF type matching logic.

I'd say that register_btf_kfunc_id_set() should just accept enum bpf_kfunc_hook instead of enum bpf_prog_type (I don't see where we remember or use prog_type beyond just translating that to bpf_kfunc_hook), but that can be left for follow up clean ups.

Quoted 5 lines

--
Matteo Croce

perl -e 'for($t=0;;$t++){print chr($t*($t>>8|$t>>13)&255)}' |aplay

#4 lore raw

PATCH v3 2/2 bpf: allow bpf_current_task_under_cgroup() with BPF_CGROUP_*

In reply to [email protected]

From: Matteo Croce <[email protected]>

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:
https://github.com/systemd/systemd/pull/32212

---
 include/linux/bpf.h      |  1 +
 kernel/bpf/cgroup.c      |  2 ++
 kernel/bpf/helpers.c     | 23 +++++++++++++++++++++++
 kernel/trace/bpf_trace.c | 23 -----------------------
 4 files changed, 26 insertions(+), 23 deletions(-)
include/linux/bpf.h +1 -0
index 7ad37cbdc815..3820fcf360b6 100644
@@ -3202,6 +3202,7 @@ extern const struct bpf_func_proto bpf_sock_hash_update_proto;
 extern const struct bpf_func_proto bpf_get_current_cgroup_id_proto;
 extern const struct bpf_func_proto bpf_get_current_ancestor_cgroup_id_proto;
 extern const struct bpf_func_proto bpf_get_cgroup_classid_curr_proto;
+extern const struct bpf_func_proto bpf_current_task_under_cgroup_proto;
 extern const struct bpf_func_proto bpf_msg_redirect_hash_proto;
 extern const struct bpf_func_proto bpf_msg_redirect_map_proto;
 extern const struct bpf_func_proto bpf_sk_redirect_hash_proto;
kernel/bpf/cgroup.c +2 -0
index 8ba73042a239..e7113d700b87 100644
@@ -2581,6 +2581,8 @@ cgroup_current_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
 	case BPF_FUNC_get_cgroup_classid:
 		return &bpf_get_cgroup_classid_curr_proto;
 #endif
+	case BPF_FUNC_current_task_under_cgroup:
+		return &bpf_current_task_under_cgroup_proto;
 	default:
 		return NULL;
 	}
kernel/bpf/helpers.c +23 -0
index 0d1d97d968b0..8502cfed2926 100644
@@ -2458,6 +2458,29 @@ __bpf_kfunc long bpf_task_under_cgroup(struct task_struct *task,
 	return ret;
 }
 
+BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
+{
+	struct bpf_array *array = container_of(map, struct bpf_array, map);
+	struct cgroup *cgrp;
+
+	if (unlikely(idx >= array->map.max_entries))
+		return -E2BIG;
+
+	cgrp = READ_ONCE(array->ptrs[idx]);
+	if (unlikely(!cgrp))
+		return -EAGAIN;
+
+	return task_under_cgroup_hierarchy(current, cgrp);
+}
+
+const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
+	.func           = bpf_current_task_under_cgroup,
+	.gpl_only       = false,
+	.ret_type       = RET_INTEGER,
+	.arg1_type      = ARG_CONST_MAP_PTR,
+	.arg2_type      = ARG_ANYTHING,
+};
+
 /**
  * bpf_task_get_cgroup1 - Acquires the associated cgroup of a task within a
  * specific cgroup1 hierarchy. The cgroup1 hierarchy is identified by its
kernel/trace/bpf_trace.c +0 -23
index cd098846e251..cc9301054f84 100644
@@ -798,29 +798,6 @@ const struct bpf_func_proto bpf_task_pt_regs_proto = {
 	.ret_btf_id	= &bpf_task_pt_regs_ids[0],
 };
 
-BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx)
-{
-	struct bpf_array *array = container_of(map, struct bpf_array, map);
-	struct cgroup *cgrp;
-
-	if (unlikely(idx >= array->map.max_entries))
-		return -E2BIG;
-
-	cgrp = READ_ONCE(array->ptrs[idx]);
-	if (unlikely(!cgrp))
-		return -EAGAIN;
-
-	return task_under_cgroup_hierarchy(current, cgrp);
-}
-
-static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = {
-	.func           = bpf_current_task_under_cgroup,
-	.gpl_only       = false,
-	.ret_type       = RET_INTEGER,
-	.arg1_type      = ARG_CONST_MAP_PTR,
-	.arg2_type      = ARG_ANYTHING,
-};
-
 struct send_signal_irq_work {
 	struct irq_work irq_work;
 	struct task_struct *task;
Signature · 1 line
2.45.2