Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/bpf: Test task_file iterator without visiting pthreads

Modified existing bpf_iter_test_file.c program to check whether
all accessed files from the main thread or not.

Modified existing bpf_iter_test_file program to check
whether all accessed files from the main thread or not.
$ ./test_progs -n 4
...
#4/7 task_file:OK
...
#4 bpf_iter:OK
Summary: 1/24 PASSED, 0 SKIPPED, 0 FAILED

Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/20200902023113.1672863-1-yhs@fb.com

authored by

Yonghong Song and committed by
Daniel Borkmann
858e8b2e 203d7b05

+30 -1
+21
tools/testing/selftests/bpf/prog_tests/bpf_iter.c
··· 132 132 bpf_iter_task_stack__destroy(skel); 133 133 } 134 134 135 + static void *do_nothing(void *arg) 136 + { 137 + pthread_exit(arg); 138 + } 139 + 135 140 static void test_task_file(void) 136 141 { 137 142 struct bpf_iter_task_file *skel; 143 + pthread_t thread_id; 144 + void *ret; 138 145 139 146 skel = bpf_iter_task_file__open_and_load(); 140 147 if (CHECK(!skel, "bpf_iter_task_file__open_and_load", 141 148 "skeleton open_and_load failed\n")) 142 149 return; 143 150 151 + skel->bss->tgid = getpid(); 152 + 153 + if (CHECK(pthread_create(&thread_id, NULL, &do_nothing, NULL), 154 + "pthread_create", "pthread_create failed\n")) 155 + goto done; 156 + 144 157 do_dummy_read(skel->progs.dump_task_file); 145 158 159 + if (CHECK(pthread_join(thread_id, &ret) || ret != NULL, 160 + "pthread_join", "pthread_join failed\n")) 161 + goto done; 162 + 163 + CHECK(skel->bss->count != 0, "check_count", 164 + "invalid non pthread file visit count %d\n", skel->bss->count); 165 + 166 + done: 146 167 bpf_iter_task_file__destroy(skel); 147 168 } 148 169
+9 -1
tools/testing/selftests/bpf/progs/bpf_iter_task_file.c
··· 6 6 7 7 char _license[] SEC("license") = "GPL"; 8 8 9 + int count = 0; 10 + int tgid = 0; 11 + 9 12 SEC("iter/task_file") 10 13 int dump_task_file(struct bpf_iter__task_file *ctx) 11 14 { ··· 20 17 if (task == (void *)0 || file == (void *)0) 21 18 return 0; 22 19 23 - if (ctx->meta->seq_num == 0) 20 + if (ctx->meta->seq_num == 0) { 21 + count = 0; 24 22 BPF_SEQ_PRINTF(seq, " tgid gid fd file\n"); 23 + } 24 + 25 + if (tgid == task->tgid && task->tgid != task->pid) 26 + count++; 25 27 26 28 BPF_SEQ_PRINTF(seq, "%8d %8d %8d %lx\n", task->tgid, task->pid, fd, 27 29 (long)file->f_op);