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 #define MAGIC_VALUE 0xabcd1234 25 26 pid_t target_pid = 0; 27 int mismatch_cnt = 0; 28 int enter_cnt = 0; 29 int exit_cnt = 0; 30 31 SEC("tp_btf/sys_enter") 32 int BPF_PROG(on_enter, struct pt_regs *regs, long id) 33 { 34 struct task_struct *task; 35 long *ptr; 36 int err; 37 38 task = bpf_get_current_task_btf(); 39 if (task->pid != target_pid) 40 return 0; 41 42 /* populate value 0 */ 43 ptr = bpf_cgrp_storage_get(&map_a, task->cgroups->dfl_cgrp, 0, 44 BPF_LOCAL_STORAGE_GET_F_CREATE); 45 if (!ptr) 46 return 0; 47 48 /* delete value 0 */ 49 err = bpf_cgrp_storage_delete(&map_a, task->cgroups->dfl_cgrp); 50 if (err) 51 return 0; 52 53 /* value is not available */ 54 ptr = bpf_cgrp_storage_get(&map_a, task->cgroups->dfl_cgrp, 0, 0); 55 if (ptr) 56 return 0; 57 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); 61 if (!ptr) 62 return 0; 63 __sync_fetch_and_add(&enter_cnt, 1); 64 *ptr = MAGIC_VALUE + enter_cnt; 65 66 return 0; 67 } 68 69 SEC("tp_btf/sys_exit") 70 int BPF_PROG(on_exit, struct pt_regs *regs, long id) 71 { 72 struct task_struct *task; 73 long *ptr; 74 75 task = bpf_get_current_task_btf(); 76 if (task->pid != target_pid) 77 return 0; 78 79 ptr = bpf_cgrp_storage_get(&map_a, task->cgroups->dfl_cgrp, 0, 80 BPF_LOCAL_STORAGE_GET_F_CREATE); 81 if (!ptr) 82 return 0; 83 84 __sync_fetch_and_add(&exit_cnt, 1); 85 if (*ptr != MAGIC_VALUE + exit_cnt) 86 __sync_fetch_and_add(&mismatch_cnt, 1); 87 return 0; 88 } 89