Skip to content

Commit 120f1a9

Browse files
ameryhungAlexei Starovoitov
authored andcommitted
selftests/bpf: Test basic task local data operations
Test basic operations of task local data with valid and invalid tld_create_key(). For invalid calls, make sure they return the right error code and check that the TLDs are not inserted by running tld_get_data(" value_not_exists") on the bpf side. The call should a null pointer. For valid calls, first make sure the TLDs are created by calling tld_get_data() on the bpf side. The call should return a valid pointer. Finally, verify that the TLDs are indeed task-specific (i.e., their addresses do not overlap) with multiple user threads. This done by writing values unique to each thread, reading them from both user space and bpf, and checking if the value read back matches the value written. Signed-off-by: Amery Hung <[email protected]> Reviewed-by: Emil Tsalapatis <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 31e838e commit 120f1a9

File tree

2 files changed

+257
-0
lines changed

2 files changed

+257
-0
lines changed
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <pthread.h>
3+
#include <bpf/btf.h>
4+
#include <test_progs.h>
5+
6+
#define TLD_FREE_DATA_ON_THREAD_EXIT
7+
#define TLD_DYN_DATA_SIZE 4096
8+
#include "task_local_data.h"
9+
10+
struct test_tld_struct {
11+
__u64 a;
12+
__u64 b;
13+
__u64 c;
14+
__u64 d;
15+
};
16+
17+
#include "test_task_local_data.skel.h"
18+
19+
TLD_DEFINE_KEY(value0_key, "value0", sizeof(int));
20+
21+
/*
22+
* Reset task local data between subtests by clearing metadata other
23+
* than the statically defined value0. This is safe as subtests run
24+
* sequentially. Users of task local data library should not touch
25+
* library internal.
26+
*/
27+
static void reset_tld(void)
28+
{
29+
if (TLD_READ_ONCE(tld_meta_p)) {
30+
/* Remove TLDs created by tld_create_key() */
31+
tld_meta_p->cnt = 1;
32+
tld_meta_p->size = TLD_DYN_DATA_SIZE;
33+
memset(&tld_meta_p->metadata[1], 0,
34+
(TLD_MAX_DATA_CNT - 1) * sizeof(struct tld_metadata));
35+
}
36+
}
37+
38+
/* Serialize access to bpf program's global variables */
39+
static pthread_mutex_t global_mutex;
40+
41+
static tld_key_t *tld_keys;
42+
43+
#define TEST_BASIC_THREAD_NUM 32
44+
45+
void *test_task_local_data_basic_thread(void *arg)
46+
{
47+
LIBBPF_OPTS(bpf_test_run_opts, opts);
48+
struct test_task_local_data *skel = (struct test_task_local_data *)arg;
49+
int fd, err, tid, *value0, *value1;
50+
struct test_tld_struct *value2;
51+
52+
fd = bpf_map__fd(skel->maps.tld_data_map);
53+
54+
value0 = tld_get_data(fd, value0_key);
55+
if (!ASSERT_OK_PTR(value0, "tld_get_data"))
56+
goto out;
57+
58+
value1 = tld_get_data(fd, tld_keys[1]);
59+
if (!ASSERT_OK_PTR(value1, "tld_get_data"))
60+
goto out;
61+
62+
value2 = tld_get_data(fd, tld_keys[2]);
63+
if (!ASSERT_OK_PTR(value2, "tld_get_data"))
64+
goto out;
65+
66+
tid = gettid();
67+
68+
*value0 = tid + 0;
69+
*value1 = tid + 1;
70+
value2->a = tid + 2;
71+
value2->b = tid + 3;
72+
value2->c = tid + 4;
73+
value2->d = tid + 5;
74+
75+
pthread_mutex_lock(&global_mutex);
76+
/* Run task_main that read task local data and save to global variables */
77+
err = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.task_main), &opts);
78+
ASSERT_OK(err, "run task_main");
79+
ASSERT_OK(opts.retval, "task_main retval");
80+
81+
ASSERT_EQ(skel->bss->test_value0, tid + 0, "tld_get_data value0");
82+
ASSERT_EQ(skel->bss->test_value1, tid + 1, "tld_get_data value1");
83+
ASSERT_EQ(skel->bss->test_value2.a, tid + 2, "tld_get_data value2.a");
84+
ASSERT_EQ(skel->bss->test_value2.b, tid + 3, "tld_get_data value2.b");
85+
ASSERT_EQ(skel->bss->test_value2.c, tid + 4, "tld_get_data value2.c");
86+
ASSERT_EQ(skel->bss->test_value2.d, tid + 5, "tld_get_data value2.d");
87+
pthread_mutex_unlock(&global_mutex);
88+
89+
/* Make sure valueX are indeed local to threads */
90+
ASSERT_EQ(*value0, tid + 0, "value0");
91+
ASSERT_EQ(*value1, tid + 1, "value1");
92+
ASSERT_EQ(value2->a, tid + 2, "value2.a");
93+
ASSERT_EQ(value2->b, tid + 3, "value2.b");
94+
ASSERT_EQ(value2->c, tid + 4, "value2.c");
95+
ASSERT_EQ(value2->d, tid + 5, "value2.d");
96+
97+
*value0 = tid + 5;
98+
*value1 = tid + 4;
99+
value2->a = tid + 3;
100+
value2->b = tid + 2;
101+
value2->c = tid + 1;
102+
value2->d = tid + 0;
103+
104+
/* Run task_main again */
105+
pthread_mutex_lock(&global_mutex);
106+
err = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.task_main), &opts);
107+
ASSERT_OK(err, "run task_main");
108+
ASSERT_OK(opts.retval, "task_main retval");
109+
110+
ASSERT_EQ(skel->bss->test_value0, tid + 5, "tld_get_data value0");
111+
ASSERT_EQ(skel->bss->test_value1, tid + 4, "tld_get_data value1");
112+
ASSERT_EQ(skel->bss->test_value2.a, tid + 3, "tld_get_data value2.a");
113+
ASSERT_EQ(skel->bss->test_value2.b, tid + 2, "tld_get_data value2.b");
114+
ASSERT_EQ(skel->bss->test_value2.c, tid + 1, "tld_get_data value2.c");
115+
ASSERT_EQ(skel->bss->test_value2.d, tid + 0, "tld_get_data value2.d");
116+
pthread_mutex_unlock(&global_mutex);
117+
118+
out:
119+
pthread_exit(NULL);
120+
}
121+
122+
static void test_task_local_data_basic(void)
123+
{
124+
struct test_task_local_data *skel;
125+
pthread_t thread[TEST_BASIC_THREAD_NUM];
126+
char dummy_key_name[TLD_NAME_LEN];
127+
tld_key_t key;
128+
int i, err;
129+
130+
reset_tld();
131+
132+
ASSERT_OK(pthread_mutex_init(&global_mutex, NULL), "pthread_mutex_init");
133+
134+
skel = test_task_local_data__open_and_load();
135+
if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
136+
return;
137+
138+
tld_keys = calloc(TLD_MAX_DATA_CNT, sizeof(tld_key_t));
139+
if (!ASSERT_OK_PTR(tld_keys, "calloc tld_keys"))
140+
goto out;
141+
142+
ASSERT_FALSE(tld_key_is_err(value0_key), "TLD_DEFINE_KEY");
143+
tld_keys[1] = tld_create_key("value1", sizeof(int));
144+
ASSERT_FALSE(tld_key_is_err(tld_keys[1]), "tld_create_key");
145+
tld_keys[2] = tld_create_key("value2", sizeof(struct test_tld_struct));
146+
ASSERT_FALSE(tld_key_is_err(tld_keys[2]), "tld_create_key");
147+
148+
/*
149+
* Shouldn't be able to store data exceed a page. Create a TLD just big
150+
* enough to exceed a page. TLDs already created are int value0, int
151+
* value1, and struct test_tld_struct value2.
152+
*/
153+
key = tld_create_key("value_not_exist",
154+
TLD_PAGE_SIZE - 2 * sizeof(int) - sizeof(struct test_tld_struct) + 1);
155+
ASSERT_EQ(tld_key_err_or_zero(key), -E2BIG, "tld_create_key");
156+
157+
key = tld_create_key("value2", sizeof(struct test_tld_struct));
158+
ASSERT_EQ(tld_key_err_or_zero(key), -EEXIST, "tld_create_key");
159+
160+
/* Shouldn't be able to create the (TLD_MAX_DATA_CNT+1)-th TLD */
161+
for (i = 3; i < TLD_MAX_DATA_CNT; i++) {
162+
snprintf(dummy_key_name, TLD_NAME_LEN, "dummy_value%d", i);
163+
tld_keys[i] = tld_create_key(dummy_key_name, sizeof(int));
164+
ASSERT_FALSE(tld_key_is_err(tld_keys[i]), "tld_create_key");
165+
}
166+
key = tld_create_key("value_not_exist", sizeof(struct test_tld_struct));
167+
ASSERT_EQ(tld_key_err_or_zero(key), -ENOSPC, "tld_create_key");
168+
169+
/* Access TLDs from multiple threads and check if they are thread-specific */
170+
for (i = 0; i < TEST_BASIC_THREAD_NUM; i++) {
171+
err = pthread_create(&thread[i], NULL, test_task_local_data_basic_thread, skel);
172+
if (!ASSERT_OK(err, "pthread_create"))
173+
goto out;
174+
}
175+
176+
out:
177+
for (i = 0; i < TEST_BASIC_THREAD_NUM; i++)
178+
pthread_join(thread[i], NULL);
179+
180+
if (tld_keys) {
181+
free(tld_keys);
182+
tld_keys = NULL;
183+
}
184+
tld_free();
185+
test_task_local_data__destroy(skel);
186+
}
187+
188+
void test_task_local_data(void)
189+
{
190+
if (test__start_subtest("task_local_data_basic"))
191+
test_task_local_data_basic();
192+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <vmlinux.h>
4+
#include <errno.h>
5+
#include <bpf/bpf_helpers.h>
6+
7+
#include "task_local_data.bpf.h"
8+
9+
struct tld_keys {
10+
tld_key_t value0;
11+
tld_key_t value1;
12+
tld_key_t value2;
13+
tld_key_t value_not_exist;
14+
};
15+
16+
struct test_tld_struct {
17+
__u64 a;
18+
__u64 b;
19+
__u64 c;
20+
__u64 d;
21+
};
22+
23+
int test_value0;
24+
int test_value1;
25+
struct test_tld_struct test_value2;
26+
27+
SEC("syscall")
28+
int task_main(void *ctx)
29+
{
30+
struct tld_object tld_obj;
31+
struct test_tld_struct *struct_p;
32+
struct task_struct *task;
33+
int err, *int_p;
34+
35+
task = bpf_get_current_task_btf();
36+
err = tld_object_init(task, &tld_obj);
37+
if (err)
38+
return 1;
39+
40+
int_p = tld_get_data(&tld_obj, value0, "value0", sizeof(int));
41+
if (int_p)
42+
test_value0 = *int_p;
43+
else
44+
return 2;
45+
46+
int_p = tld_get_data(&tld_obj, value1, "value1", sizeof(int));
47+
if (int_p)
48+
test_value1 = *int_p;
49+
else
50+
return 3;
51+
52+
struct_p = tld_get_data(&tld_obj, value2, "value2", sizeof(struct test_tld_struct));
53+
if (struct_p)
54+
test_value2 = *struct_p;
55+
else
56+
return 4;
57+
58+
int_p = tld_get_data(&tld_obj, value_not_exist, "value_not_exist", sizeof(int));
59+
if (int_p)
60+
return 5;
61+
62+
return 0;
63+
}
64+
65+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)