Skip to content

Commit 91c9684

Browse files
davemarchevskyAlexei Starovoitov
authored andcommitted
selftests/bpf: Test bpf_kptr_xchg stashing into local kptr
Test stashing both referenced kptr and local kptr into local kptrs. Then, test unstashing them. Acked-by: Martin KaFai Lau <[email protected]> Acked-by: Hou Tao <[email protected]> Signed-off-by: Dave Marchevsky <[email protected]> Signed-off-by: Amery Hung <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent b0966c7 commit 91c9684

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

tools/testing/selftests/bpf/progs/local_kptr_stash.c

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
#include "../bpf_experimental.h"
99
#include "../bpf_testmod/bpf_testmod_kfunc.h"
1010

11+
struct plain_local;
12+
1113
struct node_data {
1214
long key;
1315
long data;
16+
struct plain_local __kptr * stashed_in_local_kptr;
1417
struct bpf_rb_node node;
1518
};
1619

@@ -85,18 +88,33 @@ static bool less(struct bpf_rb_node *a, const struct bpf_rb_node *b)
8588

8689
static int create_and_stash(int idx, int val)
8790
{
91+
struct plain_local *inner_local_kptr;
8892
struct map_value *mapval;
8993
struct node_data *res;
9094

9195
mapval = bpf_map_lookup_elem(&some_nodes, &idx);
9296
if (!mapval)
9397
return 1;
9498

99+
inner_local_kptr = bpf_obj_new(typeof(*inner_local_kptr));
100+
if (!inner_local_kptr)
101+
return 2;
102+
95103
res = bpf_obj_new(typeof(*res));
96-
if (!res)
97-
return 1;
104+
if (!res) {
105+
bpf_obj_drop(inner_local_kptr);
106+
return 3;
107+
}
98108
res->key = val;
99109

110+
inner_local_kptr = bpf_kptr_xchg(&res->stashed_in_local_kptr, inner_local_kptr);
111+
if (inner_local_kptr) {
112+
/* Should never happen, we just obj_new'd res */
113+
bpf_obj_drop(inner_local_kptr);
114+
bpf_obj_drop(res);
115+
return 4;
116+
}
117+
100118
res = bpf_kptr_xchg(&mapval->node, res);
101119
if (res)
102120
bpf_obj_drop(res);
@@ -169,6 +187,7 @@ long stash_local_with_root(void *ctx)
169187
SEC("tc")
170188
long unstash_rb_node(void *ctx)
171189
{
190+
struct plain_local *inner_local_kptr = NULL;
172191
struct map_value *mapval;
173192
struct node_data *res;
174193
long retval;
@@ -180,6 +199,13 @@ long unstash_rb_node(void *ctx)
180199

181200
res = bpf_kptr_xchg(&mapval->node, NULL);
182201
if (res) {
202+
inner_local_kptr = bpf_kptr_xchg(&res->stashed_in_local_kptr, inner_local_kptr);
203+
if (!inner_local_kptr) {
204+
bpf_obj_drop(res);
205+
return 1;
206+
}
207+
bpf_obj_drop(inner_local_kptr);
208+
183209
retval = res->key;
184210
bpf_obj_drop(res);
185211
return retval;

tools/testing/selftests/bpf/progs/task_kfunc_success.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <bpf/bpf_tracing.h>
66
#include <bpf/bpf_helpers.h>
77

8+
#include "../bpf_experimental.h"
89
#include "task_kfunc_common.h"
910

1011
char _license[] SEC("license") = "GPL";
@@ -143,7 +144,7 @@ SEC("tp_btf/task_newtask")
143144
int BPF_PROG(test_task_xchg_release, struct task_struct *task, u64 clone_flags)
144145
{
145146
struct task_struct *kptr;
146-
struct __tasks_kfunc_map_value *v;
147+
struct __tasks_kfunc_map_value *v, *local;
147148
long status;
148149

149150
if (!is_test_kfunc_task())
@@ -167,6 +168,29 @@ int BPF_PROG(test_task_xchg_release, struct task_struct *task, u64 clone_flags)
167168
return 0;
168169
}
169170

171+
local = bpf_obj_new(typeof(*local));
172+
if (!local) {
173+
err = 4;
174+
bpf_task_release(kptr);
175+
return 0;
176+
}
177+
178+
kptr = bpf_kptr_xchg(&local->task, kptr);
179+
if (kptr) {
180+
err = 5;
181+
bpf_obj_drop(local);
182+
bpf_task_release(kptr);
183+
return 0;
184+
}
185+
186+
kptr = bpf_kptr_xchg(&local->task, NULL);
187+
if (!kptr) {
188+
err = 6;
189+
bpf_obj_drop(local);
190+
return 0;
191+
}
192+
193+
bpf_obj_drop(local);
170194
bpf_task_release(kptr);
171195

172196
return 0;

0 commit comments

Comments
 (0)