Skip to content

Commit fd27b18

Browse files
anakryikoborkmann
authored andcommitted
selftests/bpf: Reset process and thread affinity after each test/sub-test
Some tests and sub-tests are setting "custom" thread/process affinity and don't reset it back. Instead of requiring each test to undo all this, ensure that thread affinity is restored by test_progs test runner itself. Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent fc32490 commit fd27b18

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

tools/testing/selftests/bpf/test_progs.c

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
// SPDX-License-Identifier: GPL-2.0-only
22
/* Copyright (c) 2017 Facebook
33
*/
4+
#define _GNU_SOURCE
45
#include "test_progs.h"
56
#include "cgroup_helpers.h"
67
#include "bpf_rlimit.h"
78
#include <argp.h>
8-
#include <string.h>
9+
#include <pthread.h>
10+
#include <sched.h>
911
#include <signal.h>
12+
#include <string.h>
1013
#include <execinfo.h> /* backtrace */
1114

1215
/* defined in test_progs.h */
@@ -90,6 +93,34 @@ static void skip_account(void)
9093
}
9194
}
9295

96+
static void stdio_restore(void);
97+
98+
/* A bunch of tests set custom affinity per-thread and/or per-process. Reset
99+
* it after each test/sub-test.
100+
*/
101+
static void reset_affinity() {
102+
103+
cpu_set_t cpuset;
104+
int i, err;
105+
106+
CPU_ZERO(&cpuset);
107+
for (i = 0; i < env.nr_cpus; i++)
108+
CPU_SET(i, &cpuset);
109+
110+
err = sched_setaffinity(0, sizeof(cpuset), &cpuset);
111+
if (err < 0) {
112+
stdio_restore();
113+
fprintf(stderr, "Failed to reset process affinity: %d!\n", err);
114+
exit(-1);
115+
}
116+
err = pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset);
117+
if (err < 0) {
118+
stdio_restore();
119+
fprintf(stderr, "Failed to reset thread affinity: %d!\n", err);
120+
exit(-1);
121+
}
122+
}
123+
93124
void test__end_subtest()
94125
{
95126
struct prog_test_def *test = env.test;
@@ -107,6 +138,8 @@ void test__end_subtest()
107138
test->test_num, test->subtest_num,
108139
test->subtest_name, sub_error_cnt ? "FAIL" : "OK");
109140

141+
reset_affinity();
142+
110143
free(test->subtest_name);
111144
test->subtest_name = NULL;
112145
}
@@ -679,6 +712,12 @@ int main(int argc, char **argv)
679712
srand(time(NULL));
680713

681714
env.jit_enabled = is_jit_enabled();
715+
env.nr_cpus = libbpf_num_possible_cpus();
716+
if (env.nr_cpus < 0) {
717+
fprintf(stderr, "Failed to get number of CPUs: %d!\n",
718+
env.nr_cpus);
719+
return -1;
720+
}
682721

683722
stdio_hijack();
684723
for (i = 0; i < prog_test_cnt; i++) {
@@ -709,6 +748,7 @@ int main(int argc, char **argv)
709748
test->test_num, test->test_name,
710749
test->error_cnt ? "FAIL" : "OK");
711750

751+
reset_affinity();
712752
if (test->need_cgroup_cleanup)
713753
cleanup_cgroup_environment();
714754
}

tools/testing/selftests/bpf/test_progs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ struct test_env {
7171
FILE *stderr;
7272
char *log_buf;
7373
size_t log_cnt;
74+
int nr_cpus;
7475

7576
int succ_cnt; /* successful tests */
7677
int sub_succ_cnt; /* successful sub-tests */

0 commit comments

Comments
 (0)