1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
5 #include <bpf/bpf_tracing.h>
6 #include <bpf/bpf_helpers.h>
7 #include <bpf/bpf_core_read.h>
8 #include "../bpf_experimental.h"
9 #include "../bpf_testmod/bpf_testmod_kfunc.h"
14 struct bpf_rb_node node;
23 struct prog_test_ref_kfunc *not_kptr;
24 struct prog_test_ref_kfunc __kptr *val;
25 struct node_data __kptr *node;
26 struct plain_local __kptr *plain;
29 /* This is necessary so that LLVM generates BTF for node_data struct
30 * If it's not included, a fwd reference for node_data will be generated but
31 * no struct. Example BTF of "node" field in map_value when not included:
33 * [10] PTR '(anon)' type_id=35
34 * [34] FWD 'node_data' fwd_kind=struct
35 * [35] TYPE_TAG 'kptr_ref' type_id=34
37 * (with no node_data struct defined)
38 * Had to do the same w/ bpf_kfunc_call_test_release below
40 struct node_data *just_here_because_btf_bug;
43 __uint(type, BPF_MAP_TYPE_ARRAY);
45 __type(value, struct map_value);
46 __uint(max_entries, 2);
47 } some_nodes SEC(".maps");
49 static int create_and_stash(int idx, int val)
51 struct map_value *mapval;
52 struct node_data *res;
54 mapval = bpf_map_lookup_elem(&some_nodes, &idx);
58 res = bpf_obj_new(typeof(*res));
63 res = bpf_kptr_xchg(&mapval->node, res);
70 long stash_rb_nodes(void *ctx)
72 return create_and_stash(0, 41) ?: create_and_stash(1, 42);
76 long stash_plain(void *ctx)
78 struct map_value *mapval;
79 struct plain_local *res;
82 mapval = bpf_map_lookup_elem(&some_nodes, &idx);
86 res = bpf_obj_new(typeof(*res));
91 res = bpf_kptr_xchg(&mapval->plain, res);
98 long unstash_rb_node(void *ctx)
100 struct map_value *mapval;
101 struct node_data *res;
105 mapval = bpf_map_lookup_elem(&some_nodes, &key);
109 res = bpf_kptr_xchg(&mapval->node, NULL);
119 long stash_test_ref_kfunc(void *ctx)
121 struct prog_test_ref_kfunc *res;
122 struct map_value *mapval;
125 mapval = bpf_map_lookup_elem(&some_nodes, &key);
129 res = bpf_kptr_xchg(&mapval->val, NULL);
131 bpf_kfunc_call_test_release(res);
135 char _license[] SEC("license") = "GPL";