]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
selftests: harness: Move teardown conditional into test metadata
authorThomas Weißschuh <thomas.weissschuh@linutronix.de>
Mon, 5 May 2025 15:15:27 +0000 (17:15 +0200)
committerThomas Weißschuh <linux@weissschuh.net>
Wed, 21 May 2025 13:32:35 +0000 (15:32 +0200)
To get rid of setjmp()/longjmp(), the teardown logic needs to be usable
from __bail(). To access the atomic teardown conditional from there,
move it into the test metadata.
This also allows the removal of "setup_completed".

Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
Acked-by: Shuah Khan <skhan@linuxfoundation.org>
Link: https://lore.kernel.org/r/20250505-nolibc-kselftest-harness-v4-9-ee4dd5257135@linutronix.de
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
tools/testing/selftests/kselftest_harness.h

index 905986debbfb0ce8c9659dbd52b6c67c6759cae7..895821af3e5c5752065561d0a108210d79e9eeee 100644 (file)
                pid_t child = 1; \
                int status = 0; \
                /* Makes sure there is only one teardown, even when child forks again. */ \
-               bool *teardown = mmap(NULL, sizeof(*teardown), \
+               _metadata->no_teardown = mmap(NULL, sizeof(*_metadata->no_teardown), \
                        PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); \
-               *teardown = false; \
+               *_metadata->no_teardown = true; \
                if (sizeof(*self) > 0) { \
                        if (fixture_name##_teardown_parent) { \
                                self = mmap(NULL, sizeof(*self), PROT_READ | PROT_WRITE, \
                                /* Let setup failure terminate early. */ \
                                if (_metadata->exit_code) \
                                        _exit(0); \
-                               _metadata->setup_completed = true; \
+                               *_metadata->no_teardown = false; \
                                fixture_name##_##test_name(_metadata, self, variant->data); \
                        } else if (child < 0 || child != waitpid(child, &status, 0)) { \
                                ksft_print_msg("ERROR SPAWNING TEST GRANDCHILD\n"); \
                        } \
                } \
                if (child == 0) { \
-                       if (_metadata->setup_completed && !fixture_name##_teardown_parent && \
-                                       !__atomic_test_and_set(teardown, __ATOMIC_RELAXED)) \
+                       if (!fixture_name##_teardown_parent && \
+                                       !__atomic_test_and_set(_metadata->no_teardown, __ATOMIC_RELAXED)) \
                                fixture_name##_teardown(_metadata, self, variant->data); \
                        _exit(0); \
                } \
-               if (_metadata->setup_completed && fixture_name##_teardown_parent && \
-                               !__atomic_test_and_set(teardown, __ATOMIC_RELAXED)) \
+               if (fixture_name##_teardown_parent && \
+                               !__atomic_test_and_set(_metadata->no_teardown, __ATOMIC_RELAXED)) \
                        fixture_name##_teardown(_metadata, self, variant->data); \
-               munmap(teardown, sizeof(*teardown)); \
+               munmap(_metadata->no_teardown, sizeof(*_metadata->no_teardown)); \
+               _metadata->no_teardown = NULL; \
                if (self && fixture_name##_teardown_parent) \
                        munmap(self, sizeof(*self)); \
                if (WIFEXITED(status)) { \
@@ -916,7 +917,7 @@ struct __test_metadata {
        int trigger; /* extra handler after the evaluation */
        int timeout;    /* seconds to wait for test timeout */
        bool aborted;   /* stopped test due to failed ASSERT */
-       bool setup_completed; /* did setup finish? */
+       bool *no_teardown; /* fixture needs teardown */
        jmp_buf env;    /* for exiting out of test early */
        struct __test_results *results;
        struct __test_metadata *prev, *next;
@@ -1195,7 +1196,7 @@ static void __run_test(struct __fixture_metadata *f,
        t->exit_code = KSFT_PASS;
        t->trigger = 0;
        t->aborted = false;
-       t->setup_completed = false;
+       t->no_teardown = NULL;
        memset(t->env, 0, sizeof(t->env));
        memset(t->results->reason, 0, sizeof(t->results->reason));