1fb301594SYonghong Song // SPDX-License-Identifier: GPL-2.0
2fb301594SYonghong Song /* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
3fb301594SYonghong Song
4fb301594SYonghong Song #include <vmlinux.h>
5fb301594SYonghong Song #include <bpf/bpf_helpers.h>
6fb301594SYonghong Song #include <bpf/bpf_tracing.h>
7fb301594SYonghong Song #include <bpf/bpf_core_read.h>
8fb301594SYonghong Song #include "../bpf_experimental.h"
9fb301594SYonghong Song #include "bpf_misc.h"
10fb301594SYonghong Song
11fb301594SYonghong Song struct node_data {
12fb301594SYonghong Song long key;
13fb301594SYonghong Song long data;
14fb301594SYonghong Song struct bpf_rb_node node;
15fb301594SYonghong Song };
16fb301594SYonghong Song
17fb301594SYonghong Song struct map_value {
18fb301594SYonghong Song struct node_data __kptr *node;
19fb301594SYonghong Song };
20fb301594SYonghong Song
21fb301594SYonghong Song struct node_data2 {
22fb301594SYonghong Song long key[4];
23fb301594SYonghong Song };
24fb301594SYonghong Song
25fb301594SYonghong Song /* This is necessary so that LLVM generates BTF for node_data struct
26fb301594SYonghong Song * If it's not included, a fwd reference for node_data will be generated but
27fb301594SYonghong Song * no struct. Example BTF of "node" field in map_value when not included:
28fb301594SYonghong Song *
29fb301594SYonghong Song * [10] PTR '(anon)' type_id=35
30fb301594SYonghong Song * [34] FWD 'node_data' fwd_kind=struct
31fb301594SYonghong Song * [35] TYPE_TAG 'kptr_ref' type_id=34
32fb301594SYonghong Song */
33fb301594SYonghong Song struct node_data *just_here_because_btf_bug;
34fb301594SYonghong Song
35fb301594SYonghong Song struct {
36fb301594SYonghong Song __uint(type, BPF_MAP_TYPE_ARRAY);
37fb301594SYonghong Song __type(key, int);
38fb301594SYonghong Song __type(value, struct map_value);
39fb301594SYonghong Song __uint(max_entries, 2);
40fb301594SYonghong Song } some_nodes SEC(".maps");
41fb301594SYonghong Song
42fb301594SYonghong Song SEC("tc")
43fb301594SYonghong Song __failure __msg("invalid kptr access, R2 type=ptr_node_data2 expected=ptr_node_data")
stash_rb_nodes(void * ctx)44fb301594SYonghong Song long stash_rb_nodes(void *ctx)
45fb301594SYonghong Song {
46fb301594SYonghong Song struct map_value *mapval;
47fb301594SYonghong Song struct node_data2 *res;
48fb301594SYonghong Song int idx = 0;
49fb301594SYonghong Song
50fb301594SYonghong Song mapval = bpf_map_lookup_elem(&some_nodes, &idx);
51fb301594SYonghong Song if (!mapval)
52fb301594SYonghong Song return 1;
53fb301594SYonghong Song
54fb301594SYonghong Song res = bpf_obj_new(typeof(*res));
55fb301594SYonghong Song if (!res)
56fb301594SYonghong Song return 1;
57fb301594SYonghong Song res->key[0] = 40;
58fb301594SYonghong Song
59fb301594SYonghong Song res = bpf_kptr_xchg(&mapval->node, res);
60fb301594SYonghong Song if (res)
61fb301594SYonghong Song bpf_obj_drop(res);
62fb301594SYonghong Song return 0;
63fb301594SYonghong Song }
64fb301594SYonghong Song
65*fbc5bc4cSKumar Kartikeya Dwivedi SEC("tc")
66*fbc5bc4cSKumar Kartikeya Dwivedi __failure __msg("R1 must have zero offset when passed to release func")
drop_rb_node_off(void * ctx)67*fbc5bc4cSKumar Kartikeya Dwivedi long drop_rb_node_off(void *ctx)
68*fbc5bc4cSKumar Kartikeya Dwivedi {
69*fbc5bc4cSKumar Kartikeya Dwivedi struct map_value *mapval;
70*fbc5bc4cSKumar Kartikeya Dwivedi struct node_data *res;
71*fbc5bc4cSKumar Kartikeya Dwivedi int idx = 0;
72*fbc5bc4cSKumar Kartikeya Dwivedi
73*fbc5bc4cSKumar Kartikeya Dwivedi mapval = bpf_map_lookup_elem(&some_nodes, &idx);
74*fbc5bc4cSKumar Kartikeya Dwivedi if (!mapval)
75*fbc5bc4cSKumar Kartikeya Dwivedi return 1;
76*fbc5bc4cSKumar Kartikeya Dwivedi
77*fbc5bc4cSKumar Kartikeya Dwivedi res = bpf_obj_new(typeof(*res));
78*fbc5bc4cSKumar Kartikeya Dwivedi if (!res)
79*fbc5bc4cSKumar Kartikeya Dwivedi return 1;
80*fbc5bc4cSKumar Kartikeya Dwivedi /* Try releasing with graph node offset */
81*fbc5bc4cSKumar Kartikeya Dwivedi bpf_obj_drop(&res->node);
82*fbc5bc4cSKumar Kartikeya Dwivedi return 0;
83*fbc5bc4cSKumar Kartikeya Dwivedi }
84*fbc5bc4cSKumar Kartikeya Dwivedi
85fb301594SYonghong Song char _license[] SEC("license") = "GPL";
86