74 lines · c
1// SPDX-License-Identifier: GPL-2.02/* Copyright (C) 2023. Huawei Technologies Co., Ltd */3#include <vmlinux.h>4#include <bpf/bpf_tracing.h>5#include <bpf/bpf_helpers.h>6 7#include "bpf_misc.h"8#include "bpf_experimental.h"9 10struct node_data {11 __u64 data;12 struct bpf_list_node node;13};14 15struct map_value {16 struct bpf_list_head head __contains(node_data, node);17 struct bpf_spin_lock lock;18};19 20struct inner_array_type {21 __uint(type, BPF_MAP_TYPE_ARRAY);22 __type(key, int);23 __type(value, struct map_value);24 __uint(max_entries, 1);25} inner_array SEC(".maps");26 27struct {28 __uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);29 __uint(key_size, 4);30 __uint(value_size, 4);31 __uint(max_entries, 1);32 __array(values, struct inner_array_type);33} outer_array SEC(".maps") = {34 .values = {35 [0] = &inner_array,36 },37};38 39char _license[] SEC("license") = "GPL";40 41int pid = 0;42bool done = false;43 44SEC("fentry/" SYS_PREFIX "sys_nanosleep")45int add_to_list_in_inner_array(void *ctx)46{47 struct map_value *value;48 struct node_data *new;49 struct bpf_map *map;50 int zero = 0;51 52 if (done || (u32)bpf_get_current_pid_tgid() != pid)53 return 0;54 55 map = bpf_map_lookup_elem(&outer_array, &zero);56 if (!map)57 return 0;58 59 value = bpf_map_lookup_elem(map, &zero);60 if (!value)61 return 0;62 63 new = bpf_obj_new(typeof(*new));64 if (!new)65 return 0;66 67 bpf_spin_lock(&value->lock);68 bpf_list_push_back(&value->head, &new->node);69 bpf_spin_unlock(&value->lock);70 done = true;71 72 return 0;73}74