Skip to content

Commit ee8c7c6

Browse files
Jordan Romeanakryiko
authored andcommitted
bpf: Properly test iter/task tid filtering
Previously test_task_tid was setting `linfo.task.tid` to `getpid()` which is the same as `gettid()` for the parent process. Instead create a new child thread and set `linfo.task.tid` to `gettid()` to make sure the tid filtering logic is working as expected. Signed-off-by: Jordan Rome <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 9495a5b commit ee8c7c6

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

tools/testing/selftests/bpf/prog_tests/bpf_iter.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ static void test_task_common_nocheck(struct bpf_iter_attach_opts *opts,
226226
ASSERT_OK(pthread_create(&thread_id, NULL, &do_nothing_wait, NULL),
227227
"pthread_create");
228228

229-
skel->bss->tid = getpid();
229+
skel->bss->tid = gettid();
230230

231231
do_dummy_read_opts(skel->progs.dump_task, opts);
232232

@@ -249,25 +249,42 @@ static void test_task_common(struct bpf_iter_attach_opts *opts, int num_unknown,
249249
ASSERT_EQ(num_known_tid, num_known, "check_num_known_tid");
250250
}
251251

252-
static void test_task_tid(void)
252+
static void *run_test_task_tid(void *arg)
253253
{
254254
LIBBPF_OPTS(bpf_iter_attach_opts, opts);
255255
union bpf_iter_link_info linfo;
256256
int num_unknown_tid, num_known_tid;
257257

258+
ASSERT_NEQ(getpid(), gettid(), "check_new_thread_id");
259+
258260
memset(&linfo, 0, sizeof(linfo));
259-
linfo.task.tid = getpid();
261+
linfo.task.tid = gettid();
260262
opts.link_info = &linfo;
261263
opts.link_info_len = sizeof(linfo);
262264
test_task_common(&opts, 0, 1);
263265

264266
linfo.task.tid = 0;
265267
linfo.task.pid = getpid();
266-
test_task_common(&opts, 1, 1);
268+
/* This includes the parent thread, this thread,
269+
* and the do_nothing_wait thread
270+
*/
271+
test_task_common(&opts, 2, 1);
267272

268273
test_task_common_nocheck(NULL, &num_unknown_tid, &num_known_tid);
269-
ASSERT_GT(num_unknown_tid, 1, "check_num_unknown_tid");
274+
ASSERT_GT(num_unknown_tid, 2, "check_num_unknown_tid");
270275
ASSERT_EQ(num_known_tid, 1, "check_num_known_tid");
276+
277+
return NULL;
278+
}
279+
280+
static void test_task_tid(void)
281+
{
282+
pthread_t thread_id;
283+
284+
/* Create a new thread so pid and tid aren't the same */
285+
ASSERT_OK(pthread_create(&thread_id, NULL, &run_test_task_tid, NULL),
286+
"pthread_create");
287+
ASSERT_FALSE(pthread_join(thread_id, NULL), "pthread_join");
271288
}
272289

273290
static void test_task_pid(void)

0 commit comments

Comments
 (0)