Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: Ackerley Tng <ackerleytng@google.com>
To: Kees Cook <kees@kernel.org>,
	Andy Lutomirski <luto@amacapital.net>,
	Will Drewry <wad@chromium.org>,  Shuah Khan <shuah@kernel.org>,
	Paolo Bonzini <pbonzini@redhat.com>,
	 Sean Christopherson <seanjc@google.com>,
	thomas.weissschuh@linutronix.de, linux@weissschuh.net
Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
	 kvm@vger.kernel.org, Ackerley Tng <ackerleytng@google.com>
Subject: [PATCH RFC 3/4] KVM: selftests: Do teardown from kselftest harness if kselftest_harness is used
Date: Tue, 14 Apr 2026 11:07:50 -0700	[thread overview]
Message-ID: <20260414-selftest-global-metadata-v1-3-fd223922bc57@google.com> (raw)
In-Reply-To: <20260414-selftest-global-metadata-v1-0-fd223922bc57@google.com>

If kselftest_harness is used, when an KVM selftest assertion fails, make
sure to perform teardown (like kselftest_harness) before exiting.

Provide a weak struct definition and default the pointer's value to NULL so
KVM selftests that don't use kselftest_harness will still work as before.

Signed-off-by: Ackerley Tng <ackerleytng@google.com>
---
 tools/testing/selftests/kvm/lib/assert.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/tools/testing/selftests/kvm/lib/assert.c b/tools/testing/selftests/kvm/lib/assert.c
index b49690658c606..06970611317fd 100644
--- a/tools/testing/selftests/kvm/lib/assert.c
+++ b/tools/testing/selftests/kvm/lib/assert.c
@@ -9,6 +9,7 @@
 #include <execinfo.h>
 #include <sys/syscall.h>
 
+#include "kselftest_harness_structs.h"
 #include "kselftest.h"
 
 /* Dumps the current stack trace to stderr. */
@@ -63,6 +64,8 @@ static pid_t _gettid(void)
 	return syscall(SYS_gettid);
 }
 
+struct __test_metadata *current_test_metadata __attribute__((weak)) = NULL;
+
 void __attribute__((noinline))
 test_assert(bool exp, const char *exp_str,
 	const char *file, unsigned int line, const char *fmt, ...)
@@ -89,6 +92,19 @@ test_assert(bool exp, const char *exp_str,
 			print_skip("Access denied - Exiting");
 			exit(KSFT_SKIP);
 		}
+
+		/*
+		 * When used with kselftest_harness, do teardown like
+		 * __bail() does. KVM only has TEST_ASSERT (no EXPECT
+		 * equivalent) and will definitely exit.
+		 */
+		if (current_test_metadata) {
+			struct __test_metadata *t = current_test_metadata;
+
+			if (t->teardown_fn)
+				t->teardown_fn(false, t, t->self, t->variant);
+		}
+
 		exit(254);
 	}
 

-- 
2.54.0.rc0.605.g598a273b03-goog


  parent reply	other threads:[~2026-04-14 18:08 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-14 18:07 [PATCH RFC 0/4] selftests: harness: Provide global metadata pointer to allow clean teardown from selftest libraries Ackerley Tng
2026-04-14 18:07 ` [PATCH RFC 1/4] selftests: harness: Move metadata structs to separate header file Ackerley Tng
2026-04-14 18:07 ` [PATCH RFC 2/4] selftests: harness: Set global current_test_metadata for each test run Ackerley Tng
2026-04-14 18:07 ` Ackerley Tng [this message]
2026-04-14 18:07 ` [PATCH RFC 4/4] HACK: Show that the teardown function is called from KVM selftests Ackerley Tng
2026-05-12 20:24 ` [PATCH RFC 0/4] selftests: harness: Provide global metadata pointer to allow clean teardown from selftest libraries Sean Christopherson
2026-05-13  1:01 ` Kees Cook

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260414-selftest-global-metadata-v1-3-fd223922bc57@google.com \
    --to=ackerleytng@google.com \
    --cc=kees@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux@weissschuh.net \
    --cc=luto@amacapital.net \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.com \
    --cc=shuah@kernel.org \
    --cc=thomas.weissschuh@linutronix.de \
    --cc=wad@chromium.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox