bpf: implement bpf_get_current_cgroup_id() helper
Subsystems (9)
v2 cover bpf: implement bpf_get_current_cgroup_id() helper
bpf has been used extensively for tracing. For example, bcc contains an almost full set of bpf-based tools to trace kernel and user functions/events. Most tracing tools are currently either filtered based on pid or system-wide.
Containers have been used quite extensively in industry and cgroup is often used together to provide resource isolation and protection. Several processes may run inside the same container. It is often desirable to get container-level tracing results as well, e.g. syscall count, function count, I/O activity, etc.
This patch implements a new helper, bpf_get_current_cgroup_id(), which will return cgroup id based on the cgroup within which the current task is running.
Patch #1 implements the new helper in the kernel. Patch #2 syncs the uapi bpf.h header and helper between tools and kernel.
Patch #3 shows how to get the same cgroup id in user space, so a filter or policy could be configgured in the bpf program based on current task cgroup.
Changelog:
v1 -> v2:
. rebase to resolve merge conflict with latest bpf-next.
Yonghong Song (3):
bpf: implement bpf_get_current_cgroup_id() helper tools/bpf: sync uapi bpf.h for bpf_get_current_cgroup_id() helper tools/bpf: add a selftest for bpf_get_current_cgroup_id() helper
include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 8 +- kernel/bpf/core.c | 1 + kernel/bpf/helpers.c | 15 +++ kernel/trace/bpf_trace.c | 2 + tools/include/uapi/linux/bpf.h | 8 +- tools/testing/selftests/bpf/.gitignore | 1 + tools/testing/selftests/bpf/Makefile | 6 +- tools/testing/selftests/bpf/bpf_helpers.h | 2 + tools/testing/selftests/bpf/cgroup_helpers.c | 57 +++++++++ tools/testing/selftests/bpf/cgroup_helpers.h | 1 + tools/testing/selftests/bpf/get_cgroup_id_kern.c | 28 +++++ tools/testing/selftests/bpf/get_cgroup_id_user.c | 141 +++++++++++++++++++++++ 13 files changed, 267 insertions(+), 4 deletions(-) create mode 100644 tools/testing/selftests/bpf/get_cgroup_id_kern.c create mode 100644 tools/testing/selftests/bpf/get_cgroup_id_user.c
Signature · 1 line
2.9.5
PATCH v2 1/3 bpf: implement bpf_get_current_cgroup_id() helper
bpf has been used extensively for tracing. For example, bcc contains an almost full set of bpf-based tools to trace kernel and user functions/events. Most tracing tools are currently either filtered based on pid or system-wide.
Containers have been used quite extensively in industry and cgroup is often used together to provide resource isolation and protection. Several processes may run inside the same container. It is often desirable to get container-level tracing results as well, e.g. syscall count, function count, I/O activity, etc.
This patch implements a new helper, bpf_get_current_cgroup_id(), which will return cgroup id based on the cgroup within which the current task is running.
The later patch will provide an example to show that userspace can get the same cgroup id so it could configure a filter or policy in the bpf program based on task cgroup id.
The helper is currently implemented for tracing. It can be added to other program types as well when needed.
--- include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 8 +++++++- kernel/bpf/core.c | 1 + kernel/bpf/helpers.c | 15 +++++++++++++++ kernel/trace/bpf_trace.c | 2 ++ 5 files changed, 26 insertions(+), 1 deletion(-)
include/linux/bpf.h +1 -0
@@ -746,6 +746,7 @@ extern const struct bpf_func_proto bpf_get_stackid_proto;extern const struct bpf_func_proto bpf_get_stack_proto;extern const struct bpf_func_proto bpf_sock_map_update_proto;extern const struct bpf_func_proto bpf_sock_hash_update_proto; +extern const struct bpf_func_proto bpf_get_current_cgroup_id_proto;/* Shared helpers among cBPF and eBPF. */ void bpf_user_rnd_init_once (void );
include/uapi/linux/bpf.h +7 -1
@@ -2070,6 +2070,11 @@ union bpf_attr {* **CONFIG_SOCK_CGROUP_DATA** configuration option. * Return * The id is returned or 0 in case the id could not be retrieved. +* +* u64 bpf_get_current_cgroup_id(void) +* Return +* A 64-bit integer containing the current cgroup id based +* on the cgroup within which the current task is running. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -2151,7 +2156,8 @@ union bpf_attr {FN (lwt_seg6_action), \FN (rc_repeat), \FN (rc_keydown), \ -FN (skb_cgroup_id), +FN (skb_cgroup_id), \ +FN (get_current_cgroup_id),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call
kernel/bpf/core.c +1 -0
@@ -1765,6 +1765,7 @@ const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;const struct bpf_func_proto bpf_get_current_comm_proto__weak ;const struct bpf_func_proto bpf_sock_map_update_proto__weak ;const struct bpf_func_proto bpf_sock_hash_update_proto__weak ; +const struct bpf_func_proto bpf_get_current_cgroup_id_proto__weak ;const struct bpf_func_proto *__weak bpf_get_trace_printk_proto (void ) {
kernel/bpf/helpers.c +15 -0
@@ -179,3 +179,18 @@ const struct bpf_func_proto bpf_get_current_comm_proto = { .arg1_type = ARG_PTR_TO_UNINIT_MEM, .arg2_type = ARG_CONST_SIZE, }; + +#ifdef CONFIG_CGROUPS +BPF_CALL_0 (bpf_get_current_cgroup_id) +{ +struct cgroup *cgrp =task_dfl_cgroup (current); + +return cgrp->kn->id.id; +} + +const struct bpf_func_proto bpf_get_current_cgroup_id_proto = { + .func = bpf_get_current_cgroup_id, + .gpl_only = false, + .ret_type =RET_INTEGER , +}; +#endif
kernel/trace/bpf_trace.c +2 -0
@@ -564,6 +564,8 @@ tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)return &bpf_get_prandom_u32_proto;case BPF_FUNC_probe_read_str:return &bpf_probe_read_str_proto; +case BPF_FUNC_get_current_cgroup_id: +return &bpf_get_current_cgroup_id_proto;default :return NULL ; }
Signature · 1 line
2.9.5
PATCH v2 2/3 tools/bpf: sync uapi bpf.h for bpf_get_current_cgroup_id() helper
In reply to Yonghong SongSync kernel uapi/linux/bpf.h with tools uapi/linux/bpf.h. Also add the necessary helper define in bpf_helpers.h.
--- tools/include/uapi/linux/bpf.h | 8 +++++++- tools/testing/selftests/bpf/bpf_helpers.h | 2 ++ 2 files changed, 9 insertions(+), 1 deletion(-)
tools/include/uapi/linux/bpf.h +7 -1
@@ -2070,6 +2070,11 @@ union bpf_attr {* **CONFIG_SOCK_CGROUP_DATA** configuration option. * Return * The id is returned or 0 in case the id could not be retrieved. +* +* u64 bpf_get_current_cgroup_id(void) +* Return +* A 64-bit integer containing the current cgroup id based +* on the cgroup within which the current task is running. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -2151,7 +2156,8 @@ union bpf_attr {FN (lwt_seg6_action), \FN (rc_repeat), \FN (rc_keydown), \ -FN (skb_cgroup_id), +FN (skb_cgroup_id), \ +FN (get_current_cgroup_id),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call
tools/testing/selftests/bpf/bpf_helpers.h +2 -0
@@ -131,6 +131,8 @@ static int (*bpf_rc_repeat)(void *ctx) =static int (*bpf_rc_keydown)(void *ctx,unsigned int protocol,unsigned long long scancode,unsigned int toggle) = (void *) BPF_FUNC_rc_keydown; +static unsigned long long (*bpf_get_current_cgroup_id)(void ) = + (void *) BPF_FUNC_get_current_cgroup_id;/* llvm builtin functions that eBPF C program may use to * emit BPF_LD_ABS and BPF_LD_IND instructions
Signature · 1 line
2.9.5
PATCH v2 3/3 tools/bpf: add a selftest for bpf_get_current_cgroup_id() helper
In reply to Yonghong SongSyscall name_to_handle_at() can be used to get cgroup id for a particular cgroup path in user space. The selftest got cgroup id from both user and kernel, and compare to ensure they are equal to each other.
--- tools/testing/selftests/bpf/.gitignore | 1 + tools/testing/selftests/bpf/Makefile | 6 +- tools/testing/selftests/bpf/cgroup_helpers.c | 57 +++++++++ tools/testing/selftests/bpf/cgroup_helpers.h | 1 + tools/testing/selftests/bpf/get_cgroup_id_kern.c | 28 +++++ tools/testing/selftests/bpf/get_cgroup_id_user.c | 141 +++++++++++++++++++++++ 6 files changed, 232 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/bpf/get_cgroup_id_kern.c create mode 100644 tools/testing/selftests/bpf/get_cgroup_id_user.c
tools/testing/selftests/bpf/.gitignore +1 -0
@@ -18,3 +18,4 @@ urandom_read test_btf test_sockmap test_lirc_mode2_user +get_cgroup_id_user
tools/testing/selftests/bpf/Makefile +4 -2
@@ -24,7 +24,7 @@ urandom_read: urandom_read.c # Order correspond to 'make run_tests' order TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test_progs \ test_align test_verifier_log test_dev_cgroup test_tcpbpf_user \ - test_sock test_btf test_sockmap test_lirc_mode2_user + test_sock test_btf test_sockmap test_lirc_mode2_user get_cgroup_id_user TEST_GEN_FILES = test_pkt_access.o test_xdp.o test_l4lb.o test_tcp_estats.o test_obj_id.o \ test_pkt_md_access.o test_xdp_redirect.o test_xdp_meta.o sockmap_parse_prog.o \
@@ -34,7 +34,8 @@ TEST_GEN_FILES = test_pkt_access.o test_xdp.o test_l4lb.o test_tcp_estats.o test sockmap_tcp_msg_prog.o connect4_prog.o connect6_prog.o test_adjust_tail.o \ test_btf_haskv.o test_btf_nokv.o test_sockmap_kern.o test_tunnel_kern.o \ test_get_stack_rawtp.o test_sockmap_kern.o test_sockhash_kern.o \ - test_lwt_seg6local.o sendmsg4_prog.o sendmsg6_prog.o test_lirc_mode2_kern.o + test_lwt_seg6local.o sendmsg4_prog.o sendmsg6_prog.o test_lirc_mode2_kern.o \ + get_cgroup_id_kern.o # Order correspond to 'make run_tests' order TEST_PROGS := test_kmod.sh \
@@ -63,6 +64,7 @@ $(OUTPUT)/test_sock: cgroup_helpers.c $(OUTPUT)/test_sock_addr: cgroup_helpers.c $(OUTPUT)/test_sockmap: cgroup_helpers.c $(OUTPUT)/test_progs: trace_helpers.c +$(OUTPUT)/get_cgroup_id_user: cgroup_helpers.c .PHONY: force
tools/testing/selftests/bpf/cgroup_helpers.c +57 -0
66 lines not on this page · raw message
tools/testing/selftests/bpf/cgroup_helpers.h +1 -0
@@ -13,5 +13,6 @@ int create_and_get_cgroup(char *path);int join_cgroup (char *path);int setup_cgroup_environment (void );void cleanup_cgroup_environment (void ); +unsigned long long get_cgroup_id (char *path);#endif
tools/testing/selftests/bpf/get_cgroup_id_kern.c new file +28 -0
@@ -0,0 +1,28 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (c) 2018 Facebook + +#include <linux/bpf.h> +#include "bpf_helpers.h" + +struct bpf_map_def SEC("maps") cg_ids = { + .type = BPF_MAP_TYPE_ARRAY, + .key_size = sizeof(__u32), + .value_size = sizeof(__u64), + .max_entries = 1, +}; + +SEC("tracepoint/syscalls/sys_enter_nanosleep") +int trace (void *ctx) +{ +__u32 key =0 ; +__u64 *val; + + val =bpf_map_lookup_elem (&cg_ids, &key); +if (val) + *val =bpf_get_current_cgroup_id (); + +return 0 ; +} + +char _license[] SEC("license") = "GPL"; +__u32 _version SEC("version") = 1; /* ignored by tracepoints, required by libbpf.a */
tools/testing/selftests/bpf/get_cgroup_id_user.c new file +141 -0
141 lines not on this page · raw message
Signature · 1 line
2.9.5
Alexei Starovoitov
AppliedQuoted 23 lines · Yonghong Song
bpf has been used extensively for tracing. For example, bcc contains an almost full set of bpf-based tools to trace kernel and user functions/events. Most tracing tools are currently either filtered based on pid or system-wide.
Containers have been used quite extensively in industry and cgroup is often used together to provide resource isolation and protection. Several processes may run inside the same container. It is often desirable to get container-level tracing results as well, e.g. syscall count, function count, I/O activity, etc.
This patch implements a new helper,
bpf_get_current_cgroup_id(), which will return cgroup id based on the cgroup within which the current task is running.Patch #1 implements the new helper in the kernel. Patch #2 syncs the uapi
bpf.hheader and helper between tools and kernel.
Patch #3 shows how to get the same cgroup id in user space, so a filter or policy could be configgured in the bpf program based on current task cgroup.
Quoted 3 lines
Changelog:
v1 -> v2:
. rebase to resolve merge conflict with latest bpf-next.
Applied, Thanks.
Daniel Borkmann
maintainerQuoted 97 lines · Yonghong Song
bpf has been used extensively for tracing. For example, bcc contains an almost full set of bpf-based tools to trace kernel and user functions/events. Most tracing tools are currently either filtered based on pid or system-wide.
Containers have been used quite extensively in industry and cgroup is often used together to provide resource isolation and protection. Several processes may run inside the same container. It is often desirable to get container-level tracing results as well, e.g. syscall count, function count, I/O activity, etc.
This patch implements a new helper,
bpf_get_current_cgroup_id(), which will return cgroup id based on the cgroup within which the current task is running.The later patch will provide an example to show that userspace can get the same cgroup id so it could configure a filter or policy in the bpf program based on task cgroup id.
The helper is currently implemented for tracing. It can be added to other program types as well when needed.
Signed-off-by Yonghong Song <[email protected]>--- include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 8 +++++++- kernel/bpf/core.c | 1 + kernel/bpf/helpers.c | 15 +++++++++++++++ kernel/trace/bpf_trace.c | 2 ++ 5 files changed, 26 insertions(+), 1 deletion(-)include/linux/bpf.h +1
-0@@ -746,6 +746,7 @@ extern const struct bpf_func_proto bpf_get_stackid_proto;extern const struct bpf_func_proto bpf_get_stack_proto;extern const struct bpf_func_proto bpf_sock_map_update_proto;extern const struct bpf_func_proto bpf_sock_hash_update_proto; +extern const struct bpf_func_proto bpf_get_current_cgroup_id_proto;/* Shared helpers among cBPF and eBPF. */ void bpf_user_rnd_init_once (void );include/uapi/linux/bpf.h +7
-1@@ -2070,6 +2070,11 @@ union bpf_attr {* **CONFIG_SOCK_CGROUP_DATA** configuration option. * Return * The id is returned or 0 in case the id could not be retrieved. +* +* u64 bpf_get_current_cgroup_id(void) +* Return +* A 64-bit integer containing the current cgroup id based +* on the cgroup within which the current task is running. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \@@ -2151,7 +2156,8 @@ union bpf_attr {FN (lwt_seg6_action), \FN (rc_repeat), \FN (rc_keydown), \ -FN (skb_cgroup_id), +FN (skb_cgroup_id), \ +FN (get_current_cgroup_id),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call kernel/bpf/core.c +1
-0@@ -1765,6 +1765,7 @@ const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;const struct bpf_func_proto bpf_get_current_comm_proto__weak ;const struct bpf_func_proto bpf_sock_map_update_proto__weak ;const struct bpf_func_proto bpf_sock_hash_update_proto__weak ; +const struct bpf_func_proto bpf_get_current_cgroup_id_proto__weak ;const struct bpf_func_proto *__weak bpf_get_trace_printk_proto (void ) {kernel/bpf/helpers.c +5
-0@@ -179,3 +179,18 @@ const struct bpf_func_proto bpf_get_current_comm_proto = { .arg1_type = ARG_PTR_TO_UNINIT_MEM, .arg2_type = ARG_CONST_SIZE, }; + +#ifdef CONFIG_CGROUPS +BPF_CALL_0 (bpf_get_current_cgroup_id) +{ +struct cgroup *cgrp =task_dfl_cgroup (current);
kernel/bpf/helpers.c const struct bpf_func_proto bpf_get_current_comm_proto = { · line 196
Quoted 10 lines
kernel/bpf/helpers.c +10
-0+ + return cgrp->kn->id.id; +} + +const struct bpf_func_proto bpf_get_current_cgroup_id_proto = { + .func = bpf_get_current_cgroup_id, + .gpl_only = false, + .ret_type =RET_INTEGER , +}; +#endif
Nit: why not moving this function directly to bpf_trace.c?
kernel/trace/bpf_trace.c tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · line 568
Quoted 10 lines
kernel/trace/bpf_trace.c +2
-0@@ -564,6 +564,8 @@ tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)return &bpf_get_prandom_u32_proto;case BPF_FUNC_probe_read_str:return &bpf_probe_read_str_proto; +case BPF_FUNC_get_current_cgroup_id: +return &bpf_get_current_cgroup_id_proto;
When you have !CONFIG_CGROUPS, then it relies on the weak definition of the bpf_get_current_cgroup_id_proto, which I would think at latest in fixup_bpf_calls() bails out with 'kernel subsystem misconfigured func' due to func being NULL.
Can't we just do the #ifdef CONFIG_CGROUPS around BPF_FUNC_get_current_cgroup_id case instead? Then we bail out normally with 'unknown func' when cgroups are not configured?
Quoted 4 lines
kernel/trace/bpf_trace.c +0
-0default :return NULL ; }
Quoted 102 lines · Daniel Borkmann
Quoted 101 lines · Yonghong Song
bpf has been used extensively for tracing. For example, bcc contains an almost full set of bpf-based tools to trace kernel and user functions/events. Most tracing tools are currently either filtered based on pid or system-wide.
Containers have been used quite extensively in industry and cgroup is often used together to provide resource isolation and protection. Several processes may run inside the same container. It is often desirable to get container-level tracing results as well, e.g. syscall count, function count, I/O activity, etc.
This patch implements a new helper,
bpf_get_current_cgroup_id(), which will return cgroup id based on the cgroup within which the current task is running.The later patch will provide an example to show that userspace can get the same cgroup id so it could configure a filter or policy in the bpf program based on task cgroup id.
The helper is currently implemented for tracing. It can be added to other program types as well when needed.
Signed-off-by Yonghong Song <[email protected]>--- include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 8 +++++++- kernel/bpf/core.c | 1 + kernel/bpf/helpers.c | 15 +++++++++++++++ kernel/trace/bpf_trace.c | 2 ++ 5 files changed, 26 insertions(+), 1 deletion(-)include/linux/bpf.h +1
-0@@ -746,6 +746,7 @@ extern const struct bpf_func_proto bpf_get_stackid_proto;extern const struct bpf_func_proto bpf_get_stack_proto;extern const struct bpf_func_proto bpf_sock_map_update_proto;extern const struct bpf_func_proto bpf_sock_hash_update_proto; +extern const struct bpf_func_proto bpf_get_current_cgroup_id_proto;/* Shared helpers among cBPF and eBPF. */ void bpf_user_rnd_init_once (void );include/uapi/linux/bpf.h +7
-1@@ -2070,6 +2070,11 @@ union bpf_attr {* **CONFIG_SOCK_CGROUP_DATA** configuration option. * Return * The id is returned or 0 in case the id could not be retrieved. +* +* u64 bpf_get_current_cgroup_id(void) +* Return +* A 64-bit integer containing the current cgroup id based +* on the cgroup within which the current task is running. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \@@ -2151,7 +2156,8 @@ union bpf_attr {FN (lwt_seg6_action), \FN (rc_repeat), \FN (rc_keydown), \ -FN (skb_cgroup_id), +FN (skb_cgroup_id), \ +FN (get_current_cgroup_id),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call kernel/bpf/core.c +1
-0@@ -1765,6 +1765,7 @@ const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;const struct bpf_func_proto bpf_get_current_comm_proto__weak ;const struct bpf_func_proto bpf_sock_map_update_proto__weak ;const struct bpf_func_proto bpf_sock_hash_update_proto__weak ; +const struct bpf_func_proto bpf_get_current_cgroup_id_proto__weak ;const struct bpf_func_proto *__weak bpf_get_trace_printk_proto (void ) {kernel/bpf/helpers.c +9
-0@@ -179,3 +179,18 @@ const struct bpf_func_proto bpf_get_current_comm_proto = { .arg1_type = ARG_PTR_TO_UNINIT_MEM, .arg2_type = ARG_CONST_SIZE, }; + +#ifdef CONFIG_CGROUPS +BPF_CALL_0 (bpf_get_current_cgroup_id) +{ +struct cgroup *cgrp =task_dfl_cgroup (current); + +return cgrp->kn->id.id; +} +
Quoted 8 lines
kernel/bpf/helpers.c const struct bpf_func_proto bpf_get_current_comm_proto = { · line 196
Quoted 6 lines
kernel/bpf/helpers.c +6
-0+const struct bpf_func_proto bpf_get_current_cgroup_id_proto = { + .func = bpf_get_current_cgroup_id, + .gpl_only = false, + .ret_type =RET_INTEGER , +}; +#endif Nit: why not moving this function directly to
bpf_trace.c?
my preference would be to keep it in helpers.c as-is. imo bpf_trace.c is only for things that depend on kernel/trace/
Quoted 15 lines
kernel/trace/bpf_trace.c tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · line 568
Quoted 10 lines
kernel/trace/bpf_trace.c +2
-0@@ -564,6 +564,8 @@ tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)return &bpf_get_prandom_u32_proto;case BPF_FUNC_probe_read_str:return &bpf_probe_read_str_proto; +case BPF_FUNC_get_current_cgroup_id: +return &bpf_get_current_cgroup_id_proto;When you have !CONFIG_CGROUPS, then it relies on the weak definition of the
bpf_get_current_cgroup_id_proto, which I would think at latest infixup_bpf_calls()bails out with 'kernel subsystem misconfigured func' due to func being NULL.
Quoted 3 lines
Can't we just do the #ifdef
CONFIG_CGROUPSaroundBPF_FUNC_get_current_cgroup_idcase instead? Then we bail out normally with 'unknown func' when cgroups are not configured?
good idea.
Quoted 112 lines · Alexei Starovoitov
Quoted 102 lines · Daniel Borkmann
Quoted 101 lines · Yonghong Song
bpf has been used extensively for tracing. For example, bcc contains an almost full set of bpf-based tools to trace kernel and user functions/events. Most tracing tools are currently either filtered based on pid or system-wide.
Containers have been used quite extensively in industry and cgroup is often used together to provide resource isolation and protection. Several processes may run inside the same container. It is often desirable to get container-level tracing results as well, e.g. syscall count, function count, I/O activity, etc.
This patch implements a new helper,
bpf_get_current_cgroup_id(), which will return cgroup id based on the cgroup within which the current task is running.The later patch will provide an example to show that userspace can get the same cgroup id so it could configure a filter or policy in the bpf program based on task cgroup id.
The helper is currently implemented for tracing. It can be added to other program types as well when needed.
Signed-off-by Yonghong Song <[email protected]>--- include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 8 +++++++- kernel/bpf/core.c | 1 + kernel/bpf/helpers.c | 15 +++++++++++++++ kernel/trace/bpf_trace.c | 2 ++ 5 files changed, 26 insertions(+), 1 deletion(-)include/linux/bpf.h +1
-0@@ -746,6 +746,7 @@ extern const struct bpf_func_proto bpf_get_stackid_proto;extern const struct bpf_func_proto bpf_get_stack_proto;extern const struct bpf_func_proto bpf_sock_map_update_proto;extern const struct bpf_func_proto bpf_sock_hash_update_proto; +extern const struct bpf_func_proto bpf_get_current_cgroup_id_proto;/* Shared helpers among cBPF and eBPF. */ void bpf_user_rnd_init_once (void );include/uapi/linux/bpf.h +7
-1@@ -2070,6 +2070,11 @@ union bpf_attr {* **CONFIG_SOCK_CGROUP_DATA** configuration option. * Return * The id is returned or 0 in case the id could not be retrieved. +* +* u64 bpf_get_current_cgroup_id(void) +* Return +* A 64-bit integer containing the current cgroup id based +* on the cgroup within which the current task is running. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \@@ -2151,7 +2156,8 @@ union bpf_attr {FN (lwt_seg6_action), \FN (rc_repeat), \FN (rc_keydown), \ -FN (skb_cgroup_id), +FN (skb_cgroup_id), \ +FN (get_current_cgroup_id),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call kernel/bpf/core.c +1
-0@@ -1765,6 +1765,7 @@ const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;const struct bpf_func_proto bpf_get_current_comm_proto__weak ;const struct bpf_func_proto bpf_sock_map_update_proto__weak ;const struct bpf_func_proto bpf_sock_hash_update_proto__weak ; +const struct bpf_func_proto bpf_get_current_cgroup_id_proto__weak ;const struct bpf_func_proto *__weak bpf_get_trace_printk_proto (void ) {kernel/bpf/helpers.c +9
-0@@ -179,3 +179,18 @@ const struct bpf_func_proto bpf_get_current_comm_proto = { .arg1_type = ARG_PTR_TO_UNINIT_MEM, .arg2_type = ARG_CONST_SIZE, }; + +#ifdef CONFIG_CGROUPS +BPF_CALL_0 (bpf_get_current_cgroup_id) +{ +struct cgroup *cgrp =task_dfl_cgroup (current); + +return cgrp->kn->id.id; +} +Quoted 8 lines
kernel/bpf/helpers.c const struct bpf_func_proto bpf_get_current_comm_proto = { · line 196
Quoted 6 lines
kernel/bpf/helpers.c +6
-0+const struct bpf_func_proto bpf_get_current_cgroup_id_proto = { + .func = bpf_get_current_cgroup_id, + .gpl_only = false, + .ret_type =RET_INTEGER , +}; +#endif Nit: why not moving this function directly to
bpf_trace.c?
Quoted 21 lines
my preference would be to keep it in
helpers.cas-is. imobpf_trace.cis only for things that depend onkernel/trace/Quoted 18 lines
kernel/trace/bpf_trace.c tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · line 568
Quoted 10 lines
kernel/trace/bpf_trace.c +2
-0@@ -564,6 +564,8 @@ tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)return &bpf_get_prandom_u32_proto;case BPF_FUNC_probe_read_str:return &bpf_probe_read_str_proto; +case BPF_FUNC_get_current_cgroup_id: +return &bpf_get_current_cgroup_id_proto;When you have !CONFIG_CGROUPS, then it relies on the weak definition of the
bpf_get_current_cgroup_id_proto, which I would think at latest infixup_bpf_calls()bails out with 'kernel subsystem misconfigured func' due to func being NULL.Can't we just do the #ifdef
CONFIG_CGROUPSaroundBPF_FUNC_get_current_cgroup_idcase instead? Then we bail out normally with 'unknown func' when cgroups are not configured?
Thanks. Will send a followup patch soon.
Quoted 3 lines
good idea.