]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
selftests/bpf: Clean up call sites of stdio_restore()
authorAmery Hung <ameryhung@gmail.com>
Wed, 5 Mar 2025 18:20:55 +0000 (10:20 -0800)
committerAlexei Starovoitov <ast@kernel.org>
Sat, 15 Mar 2025 18:48:57 +0000 (11:48 -0700)
reset_affinity() and save_ns() are only called in run_one_test(). There is
no need to call stdio_restore() in reset_affinity() and save_ns() if
stdio_restore() is moved right after a test finishes in run_one_test().

Also remove an unnecessary check of env.stdout_saved in crash_handler()
by moving env.stdout_saved assignment to the beginning of main().

Signed-off-by: Amery Hung <ameryhung@gmail.com>
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://patch.msgid.link/20250305182057.2802606-1-ameryhung@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/test_progs.c

index 0cb759632225d27ad8df7bc478bf1adb5651bf33..ab0f2fed3c580d958a13c5c2b555c9ec331ab00a 100644 (file)
@@ -474,8 +474,6 @@ static void dump_test_log(const struct prog_test_def *test,
        print_test_result(test, test_state);
 }
 
-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.
  */
@@ -490,13 +488,11 @@ static void reset_affinity(void)
 
        err = sched_setaffinity(0, sizeof(cpuset), &cpuset);
        if (err < 0) {
-               stdio_restore();
                fprintf(stderr, "Failed to reset process affinity: %d!\n", err);
                exit(EXIT_ERR_SETUP_INFRA);
        }
        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(EXIT_ERR_SETUP_INFRA);
        }
@@ -514,7 +510,6 @@ static void save_netns(void)
 static void restore_netns(void)
 {
        if (setns(env.saved_netns_fd, CLONE_NEWNET) == -1) {
-               stdio_restore();
                perror("setns(CLONE_NEWNS)");
                exit(EXIT_ERR_SETUP_INFRA);
        }
@@ -1270,8 +1265,7 @@ void crash_handler(int signum)
 
        sz = backtrace(bt, ARRAY_SIZE(bt));
 
-       if (env.stdout_saved)
-               stdio_restore();
+       stdio_restore();
        if (env.test) {
                env.test_state->error_cnt++;
                dump_test_log(env.test, env.test_state, true, false, NULL);
@@ -1400,6 +1394,8 @@ static void run_one_test(int test_num)
 
        state->tested = true;
 
+       stdio_restore();
+
        if (verbose() && env.worker_id == -1)
                print_test_result(test, state);
 
@@ -1408,7 +1404,6 @@ static void run_one_test(int test_num)
        if (test->need_cgroup_cleanup)
                cleanup_cgroup_environment();
 
-       stdio_restore();
        free(stop_libbpf_log_capture());
 
        dump_test_log(test, state, false, false, NULL);
@@ -1943,6 +1938,9 @@ int main(int argc, char **argv)
 
        sigaction(SIGSEGV, &sigact, NULL);
 
+       env.stdout_saved = stdout;
+       env.stderr_saved = stderr;
+
        env.secs_till_notify = 10;
        env.secs_till_kill = 120;
        err = argp_parse(&argp, argc, argv, 0, NULL, &env);
@@ -1969,9 +1967,6 @@ int main(int argc, char **argv)
                return -1;
        }
 
-       env.stdout_saved = stdout;
-       env.stderr_saved = stderr;
-
        env.has_testmod = true;
        if (!env.list_test_names) {
                /* ensure previous instance of the module is unloaded */