Skip to content

Commit bf6fa2c

Browse files
yonghong-songAlexei Starovoitov
authored and
Alexei Starovoitov
committed
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. Acked-by: Alexei Starovoitov <[email protected]> Signed-off-by: Yonghong Song <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent ea9916e commit bf6fa2c

File tree

5 files changed

+26
-1
lines changed

5 files changed

+26
-1
lines changed

include/linux/bpf.h

+1
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,7 @@ extern const struct bpf_func_proto bpf_get_stackid_proto;
746746
extern const struct bpf_func_proto bpf_get_stack_proto;
747747
extern const struct bpf_func_proto bpf_sock_map_update_proto;
748748
extern const struct bpf_func_proto bpf_sock_hash_update_proto;
749+
extern const struct bpf_func_proto bpf_get_current_cgroup_id_proto;
749750

750751
/* Shared helpers among cBPF and eBPF. */
751752
void bpf_user_rnd_init_once(void);

include/uapi/linux/bpf.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -2070,6 +2070,11 @@ union bpf_attr {
20702070
* **CONFIG_SOCK_CGROUP_DATA** configuration option.
20712071
* Return
20722072
* The id is returned or 0 in case the id could not be retrieved.
2073+
*
2074+
* u64 bpf_get_current_cgroup_id(void)
2075+
* Return
2076+
* A 64-bit integer containing the current cgroup id based
2077+
* on the cgroup within which the current task is running.
20732078
*/
20742079
#define __BPF_FUNC_MAPPER(FN) \
20752080
FN(unspec), \
@@ -2151,7 +2156,8 @@ union bpf_attr {
21512156
FN(lwt_seg6_action), \
21522157
FN(rc_repeat), \
21532158
FN(rc_keydown), \
2154-
FN(skb_cgroup_id),
2159+
FN(skb_cgroup_id), \
2160+
FN(get_current_cgroup_id),
21552161

21562162
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
21572163
* function eBPF program intends to call

kernel/bpf/core.c

+1
Original file line numberDiff line numberDiff line change
@@ -1765,6 +1765,7 @@ const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;
17651765
const struct bpf_func_proto bpf_get_current_comm_proto __weak;
17661766
const struct bpf_func_proto bpf_sock_map_update_proto __weak;
17671767
const struct bpf_func_proto bpf_sock_hash_update_proto __weak;
1768+
const struct bpf_func_proto bpf_get_current_cgroup_id_proto __weak;
17681769

17691770
const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void)
17701771
{

kernel/bpf/helpers.c

+15
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,18 @@ const struct bpf_func_proto bpf_get_current_comm_proto = {
179179
.arg1_type = ARG_PTR_TO_UNINIT_MEM,
180180
.arg2_type = ARG_CONST_SIZE,
181181
};
182+
183+
#ifdef CONFIG_CGROUPS
184+
BPF_CALL_0(bpf_get_current_cgroup_id)
185+
{
186+
struct cgroup *cgrp = task_dfl_cgroup(current);
187+
188+
return cgrp->kn->id.id;
189+
}
190+
191+
const struct bpf_func_proto bpf_get_current_cgroup_id_proto = {
192+
.func = bpf_get_current_cgroup_id,
193+
.gpl_only = false,
194+
.ret_type = RET_INTEGER,
195+
};
196+
#endif

kernel/trace/bpf_trace.c

+2
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,8 @@ tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
564564
return &bpf_get_prandom_u32_proto;
565565
case BPF_FUNC_probe_read_str:
566566
return &bpf_probe_read_str_proto;
567+
case BPF_FUNC_get_current_cgroup_id:
568+
return &bpf_get_current_cgroup_id_proto;
567569
default:
568570
return NULL;
569571
}

0 commit comments

Comments
 (0)