Commit fd27b183 authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Daniel Borkmann

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: default avatarAndrii Nakryiko <andriin@fb.com>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20200314013932.4035712-3-andriin@fb.com
parent fc32490b
// SPDX-License-Identifier: GPL-2.0-only // SPDX-License-Identifier: GPL-2.0-only
/* Copyright (c) 2017 Facebook /* Copyright (c) 2017 Facebook
*/ */
#define _GNU_SOURCE
#include "test_progs.h" #include "test_progs.h"
#include "cgroup_helpers.h" #include "cgroup_helpers.h"
#include "bpf_rlimit.h" #include "bpf_rlimit.h"
#include <argp.h> #include <argp.h>
#include <string.h> #include <pthread.h>
#include <sched.h>
#include <signal.h> #include <signal.h>
#include <string.h>
#include <execinfo.h> /* backtrace */ #include <execinfo.h> /* backtrace */
/* defined in test_progs.h */ /* defined in test_progs.h */
...@@ -90,6 +93,34 @@ static void skip_account(void) ...@@ -90,6 +93,34 @@ static void skip_account(void)
} }
} }
static void stdio_restore(void);
/* A bunch of tests set custom affinity per-thread and/or per-process. Reset
* it after each test/sub-test.
*/
static void reset_affinity() {
cpu_set_t cpuset;
int i, err;
CPU_ZERO(&cpuset);
for (i = 0; i < env.nr_cpus; i++)
CPU_SET(i, &cpuset);
err = sched_setaffinity(0, sizeof(cpuset), &cpuset);
if (err < 0) {
stdio_restore();
fprintf(stderr, "Failed to reset process affinity: %d!\n", err);
exit(-1);
}
err = pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset);
if (err < 0) {
stdio_restore();
fprintf(stderr, "Failed to reset thread affinity: %d!\n", err);
exit(-1);
}
}
void test__end_subtest() void test__end_subtest()
{ {
struct prog_test_def *test = env.test; struct prog_test_def *test = env.test;
...@@ -107,6 +138,8 @@ void test__end_subtest() ...@@ -107,6 +138,8 @@ void test__end_subtest()
test->test_num, test->subtest_num, test->test_num, test->subtest_num,
test->subtest_name, sub_error_cnt ? "FAIL" : "OK"); test->subtest_name, sub_error_cnt ? "FAIL" : "OK");
reset_affinity();
free(test->subtest_name); free(test->subtest_name);
test->subtest_name = NULL; test->subtest_name = NULL;
} }
...@@ -679,6 +712,12 @@ int main(int argc, char **argv) ...@@ -679,6 +712,12 @@ int main(int argc, char **argv)
srand(time(NULL)); srand(time(NULL));
env.jit_enabled = is_jit_enabled(); env.jit_enabled = is_jit_enabled();
env.nr_cpus = libbpf_num_possible_cpus();
if (env.nr_cpus < 0) {
fprintf(stderr, "Failed to get number of CPUs: %d!\n",
env.nr_cpus);
return -1;
}
stdio_hijack(); stdio_hijack();
for (i = 0; i < prog_test_cnt; i++) { for (i = 0; i < prog_test_cnt; i++) {
...@@ -709,6 +748,7 @@ int main(int argc, char **argv) ...@@ -709,6 +748,7 @@ int main(int argc, char **argv)
test->test_num, test->test_name, test->test_num, test->test_name,
test->error_cnt ? "FAIL" : "OK"); test->error_cnt ? "FAIL" : "OK");
reset_affinity();
if (test->need_cgroup_cleanup) if (test->need_cgroup_cleanup)
cleanup_cgroup_environment(); cleanup_cgroup_environment();
} }
......
...@@ -71,6 +71,7 @@ struct test_env { ...@@ -71,6 +71,7 @@ struct test_env {
FILE *stderr; FILE *stderr;
char *log_buf; char *log_buf;
size_t log_cnt; size_t log_cnt;
int nr_cpus;
int succ_cnt; /* successful tests */ int succ_cnt; /* successful tests */
int sub_succ_cnt; /* successful sub-tests */ int sub_succ_cnt; /* successful sub-tests */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment