]> asedeno.scripts.mit.edu Git - linux.git/commitdiff
bpf: implement bpf_get_current_cgroup_id() helper
authorYonghong Song <yhs@fb.com>
Sun, 3 Jun 2018 22:59:41 +0000 (15:59 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Mon, 4 Jun 2018 01:22:41 +0000 (18:22 -0700)
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 <ast@kernel.org>
Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
include/linux/bpf.h
include/uapi/linux/bpf.h
kernel/bpf/core.c
kernel/bpf/helpers.c
kernel/trace/bpf_trace.c

index bbe297436e5d60509300ff9deed4a26700a1db4d..995c3b1e59bfa82ef3ad0504b090ab28a898f016 100644 (file)
@@ -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);
index f0b6608b1f1cad4268a6ff4b4a96ea9ca15541e7..18712b0dbfe7c749fbd36d82218e0ece95cdd5ca 100644 (file)
@@ -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
index 527587de8a67a4bae28c5015e994af01c15a82e8..9f1493705f4043066033dd44ec6deb95e7418287 100644 (file)
@@ -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)
 {
index 3d24e238221ecdf4208fa31fef63b645d1e0437a..73065e2d23c2810757e660bf9ad51266e5480803 100644 (file)
@@ -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
index 752992ce35131a68145cb61b7bdc4bcb77c1852e..e2ab5b7f29d25dc5661c892e082c86cbd1347885 100644 (file)
@@ -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;
        }