1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
5 #include <bpf/bpf_helpers.h>
6 #include <bpf/bpf_tracing.h>
8 char _license[] SEC("license") = "GPL";
11 __uint(type, BPF_MAP_TYPE_CGRP_STORAGE);
12 __uint(map_flags, BPF_F_NO_PREALLOC);
18 __uint(type, BPF_MAP_TYPE_CGRP_STORAGE);
19 __uint(map_flags, BPF_F_NO_PREALLOC);
24 #define MAGIC_VALUE 0xabcd1234
31 SEC("tp_btf/sys_enter")
32 int BPF_PROG(on_enter, struct pt_regs *regs, long id)
34 struct task_struct *task;
38 task = bpf_get_current_task_btf();
39 if (task->pid != target_pid)
42 /* populate value 0 */
43 ptr = bpf_cgrp_storage_get(&map_a, task->cgroups->dfl_cgrp, 0,
44 BPF_LOCAL_STORAGE_GET_F_CREATE);
49 err = bpf_cgrp_storage_delete(&map_a, task->cgroups->dfl_cgrp);
53 /* value is not available */
54 ptr = bpf_cgrp_storage_get(&map_a, task->cgroups->dfl_cgrp, 0, 0);
58 /* re-populate the value */
59 ptr = bpf_cgrp_storage_get(&map_a, task->cgroups->dfl_cgrp, 0,
60 BPF_LOCAL_STORAGE_GET_F_CREATE);
63 __sync_fetch_and_add(&enter_cnt, 1);
64 *ptr = MAGIC_VALUE + enter_cnt;
69 SEC("tp_btf/sys_exit")
70 int BPF_PROG(on_exit, struct pt_regs *regs, long id)
72 struct task_struct *task;
75 task = bpf_get_current_task_btf();
76 if (task->pid != target_pid)
79 ptr = bpf_cgrp_storage_get(&map_a, task->cgroups->dfl_cgrp, 0,
80 BPF_LOCAL_STORAGE_GET_F_CREATE);
84 __sync_fetch_and_add(&exit_cnt, 1);
85 if (*ptr != MAGIC_VALUE + exit_cnt)
86 __sync_fetch_and_add(&mismatch_cnt, 1);