1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
3 
4 #include "vmlinux.h"
5 #include <bpf/bpf_helpers.h>
6 #include <bpf/bpf_tracing.h>
7 
8 char _license[] SEC("license") = "GPL";
9 
10 struct {
11 	__uint(type, BPF_MAP_TYPE_CGRP_STORAGE);
12 	__uint(map_flags, BPF_F_NO_PREALLOC);
13 	__type(key, int);
14 	__type(value, long);
15 } map_a SEC(".maps");
16 
17 struct {
18 	__uint(type, BPF_MAP_TYPE_CGRP_STORAGE);
19 	__uint(map_flags, BPF_F_NO_PREALLOC);
20 	__type(key, int);
21 	__type(value, long);
22 } map_b SEC(".maps");
23 
24 SEC("fentry/bpf_local_storage_lookup")
25 int BPF_PROG(on_lookup)
26 {
27 	struct task_struct *task = bpf_get_current_task_btf();
28 
29 	bpf_cgrp_storage_delete(&map_a, task->cgroups->dfl_cgrp);
30 	bpf_cgrp_storage_delete(&map_b, task->cgroups->dfl_cgrp);
31 	return 0;
32 }
33 
34 SEC("fentry/bpf_local_storage_update")
35 int BPF_PROG(on_update)
36 {
37 	struct task_struct *task = bpf_get_current_task_btf();
38 	long *ptr;
39 
40 	ptr = bpf_cgrp_storage_get(&map_a, task->cgroups->dfl_cgrp, 0,
41 				   BPF_LOCAL_STORAGE_GET_F_CREATE);
42 	if (ptr)
43 		*ptr += 1;
44 
45 	ptr = bpf_cgrp_storage_get(&map_b, task->cgroups->dfl_cgrp, 0,
46 				   BPF_LOCAL_STORAGE_GET_F_CREATE);
47 	if (ptr)
48 		*ptr += 1;
49 
50 	return 0;
51 }
52 
53 SEC("tp_btf/sys_enter")
54 int BPF_PROG(on_enter, struct pt_regs *regs, long id)
55 {
56 	struct task_struct *task;
57 	long *ptr;
58 
59 	task = bpf_get_current_task_btf();
60 	ptr = bpf_cgrp_storage_get(&map_a, task->cgroups->dfl_cgrp, 0,
61 				   BPF_LOCAL_STORAGE_GET_F_CREATE);
62 	if (ptr)
63 		*ptr = 200;
64 
65 	ptr = bpf_cgrp_storage_get(&map_b, task->cgroups->dfl_cgrp, 0,
66 				   BPF_LOCAL_STORAGE_GET_F_CREATE);
67 	if (ptr)
68 		*ptr = 100;
69 	return 0;
70 }
71