Skip to contentSkip to conversation

selftests/harness: fix XFAIL_ADD matching when test metadata is not initialized

PATCH In review Sun Jian 2 participants 13 messages Feb 22 – Feb 25

Subsystems (4)
lore
Share & tools
lore

Sun Jian

author
#0 lore raw

PATCH 1/2 selftests/harness: fix XFAIL_ADD matching when test metadata is not initialized

XFAIL_ADD() registers expected failures using constructor order, but the associated struct __test_metadata pointer may not be initialized yet. As a result, xfail entries can end up with a NULL test pointer and never match at runtime, causing expected failures to be reported as FAIL.

Store the test case name in the xfail entry and fall back to name-based matching when the test pointer is unavailable, while keeping the original pointer-based matching for compatibility.

Fixes 2709473c9386 ("selftests: kselftest_harness: support using xfail")
---
 tools/testing/selftests/kselftest_harness.h | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)
tools/testing/selftests/kselftest_harness.h +11 -7
index 16a119a4656c..1082024b2d77 100644
@@ -860,6 +860,7 @@ struct __test_xfail {
 	struct __fixture_metadata *fixture;
 	struct __fixture_variant_metadata *variant;
 	struct __test_metadata *test;
+	const char *case_name;
 	struct __test_xfail *prev, *next;
 };
 
@@ -873,19 +874,19 @@ struct __test_xfail {
  * to fail. Tests marked this way will report XPASS / XFAIL return codes,
  * instead of PASS / FAIL,and use respective counters.
  */
-#define XFAIL_ADD(fixture_name, variant_name, test_name) \
+#define XFAIL_ADD(fixture_name, variant_name, tc_name) \
 	static struct __test_xfail \
-		_##fixture_name##_##variant_name##_##test_name##_xfail = \
+		_##fixture_name##_##variant_name##_##tc_name##_xfail = \
 	{ \
 		.fixture = &_##fixture_name##_fixture_object, \
 		.variant = &_##fixture_name##_##variant_name##_object, \
 	}; \
 	static void __attribute__((constructor)) \
-		_register_##fixture_name##_##variant_name##_##test_name##_xfail(void) \
+		_register_##fixture_name##_##variant_name##_##tc_name##_xfail(void) \
 	{ \
-		_##fixture_name##_##variant_name##_##test_name##_xfail.test = \
-			_##fixture_name##_##test_name##_object; \
-		__register_xfail(&_##fixture_name##_##variant_name##_##test_name##_xfail); \
+		_##fixture_name##_##variant_name##_##tc_name##_xfail.case_name = \
+			#tc_name; \
+		__register_xfail(&_##fixture_name##_##variant_name##_##tc_name##_xfail); \
 	}
 
 static struct __fixture_metadata *__fixture_list = &_fixture_global;
@@ -1233,7 +1234,10 @@ static void __run_test(struct __fixture_metadata *f,
 
 	/* Check if we're expecting this test to fail */
 	for (xfail = variant->xfails; xfail; xfail = xfail->next)
-		if (xfail->test == t)
+		if (xfail->test == t ||
+		    (!xfail->test &&
+		     xfail->case_name && t->name &&
+		     !strcmp(xfail->case_name, t->name)))
 			break;
 	if (xfail)
 		t->exit_code = __test_passed(t) ? KSFT_XPASS : KSFT_XFAIL;

base-commit: 32a92f8c89326985e05dce8b22d3f0aa07a3e1bd

Signature · 1 line
2.43.0
author
#8 lore raw

PATCH 2/2 selftests: net: tun: don't abort XFAIL cases

The tun UDP tunnel GSO fixture contains XFAIL-marked variants intended to exercise failure paths (e.g. EMSGSIZE / "Message too long"). Using ASSERT_EQ() in these tests aborts the subtest, which prevents the harness from classifying them as XFAIL and can make the overall net: tun test fail.

Switch the relevant ASSERT_EQ() checks to EXPECT_EQ() so the subtests continue running and the failures are correctly reported and accounted as XFAIL where applicable.

---
 tools/testing/selftests/net/tun.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
tools/testing/selftests/net/tun.c +6 -6
index 8a5cd5cb5472..cf106a49b55e 100644
@@ -944,8 +944,8 @@ TEST_F(tun_vnet_udptnl, send_gso_packet)
 	ASSERT_EQ(ret, off);
 
 	ret = receive_gso_packet_from_tunnel(self, variant, &r_num_mss);
-	ASSERT_EQ(ret, variant->data_size);
-	ASSERT_EQ(r_num_mss, variant->r_num_mss);
+	EXPECT_EQ(ret, variant->data_size);
+	EXPECT_EQ(r_num_mss, variant->r_num_mss);
 }
 
 TEST_F(tun_vnet_udptnl, recv_gso_packet)
@@ -955,18 +955,18 @@ TEST_F(tun_vnet_udptnl, recv_gso_packet)
 	int ret, gso_type = VIRTIO_NET_HDR_GSO_UDP_L4;
 
 	ret = send_gso_packet_into_tunnel(self, variant);
-	ASSERT_EQ(ret, variant->data_size);
+	EXPECT_EQ(ret, variant->data_size);
 
 	memset(&vnet_hdr, 0, sizeof(vnet_hdr));
 	ret = receive_gso_packet_from_tun(self, variant, &vnet_hdr);
-	ASSERT_EQ(ret, variant->data_size);
+	EXPECT_EQ(ret, variant->data_size);
 
 	if (!variant->no_gso) {
-		ASSERT_EQ(vh->gso_size, variant->gso_size);
+		EXPECT_EQ(vh->gso_size, variant->gso_size);
 		gso_type |= (variant->tunnel_type & UDP_TUNNEL_OUTER_IPV4) ?
 				    (VIRTIO_NET_HDR_GSO_UDP_TUNNEL_IPV4) :
 				    (VIRTIO_NET_HDR_GSO_UDP_TUNNEL_IPV6);
-		ASSERT_EQ(vh->gso_type, gso_type);
+		EXPECT_EQ(vh->gso_type, gso_type);
 	}
 }
 
Signature · 1 line
2.43.0
maintainer
#3 lore raw
In reply to Sun Jian
Quoted 8 lines · On Sun, 22 Feb 2026 19:18:46 +0800 Sun Jian wrote:

XFAIL_ADD() registers expected failures using constructor order, but the associated struct __test_metadata pointer may not be initialized yet. As a result, xfail entries can end up with a NULL test pointer and never match at runtime, causing expected failures to be reported as FAIL.

Store the test case name in the xfail entry and fall back to name-based matching when the test pointer is unavailable, while keeping the original pointer-based matching for compatibility.

This looks a bit inelegant, what compiler is this failing on for you?

Constructors seem to have a concept of priority so if we have to, we should probably used those for the fix.

Signature · 1 line
pw-bot: cr
author
#4 lore raw
Quoted 7 lines · Jakub Kicinski
Quoted 5 lines · On Sun, 22 Feb 2026 19:18:46 +0800 Sun Jian wrote:

XFAIL_ADD() registers expected failures using constructor order, but the associated struct __test_metadata pointer may not be initialized yet. As a result, xfail entries can end up with a NULL test pointer and never match at runtime, causing expected failures to be reported as FAIL.

Quoted 5 lines
Quoted 3 lines

Store the test case name in the xfail entry and fall back to name-based matching when the test pointer is unavailable, while keeping the original pointer-based matching for compatibility.

This looks a bit inelegant, what compiler is this failing on for you?

Hi Jakub,

I hit this with clang + lld (LLVM=1) in a virtme-ng based net selftests run. In that setup the XFAIL_ADD() constructor runs before the corresponding _*_test_object is initialized, so xfail->test ends up NULL and we never classify XFAIL/XPASS.

Quoted 2 lines

Constructors seem to have a concept of priority so if we have to, we should probably used those for the fix.

I considered constructor priorities, but I'd prefer to avoid relying on toolchain/linker specific init ordering here. (The harness is userspace and we already see different behavior with clang/lld.)

If you prefer a cleaner approach, I can rework this to avoid adding any new field and avoid priorities: keep xfail->test as a cached pointer, but populate it lazily at runtime on first use by looking up the test metadata by name within the fixture’s test list, then do the original pointer match. That removes the dependency on constructor ordering and keeps the matching semantics pointer-based after the first lookup.

Let me know if that direction sounds better and I'll send a v2.

Thanks,
sun jian

maintainer
#5 lore raw
Quoted 5 lines · On Tue, 24 Feb 2026 10:43:15 +0800 sun jian wrote:
Quoted 2 lines

Constructors seem to have a concept of priority so if we have to, we should probably used those for the fix.

I considered constructor priorities, but I'd prefer to avoid relying on toolchain/linker specific init ordering here.

I thought constructor priorities were specifically created to avoid the problem you are seeing. What it is the point of constructor priorities if not to order constructors??

author
#6 lore raw
Quoted 11 lines · Jakub Kicinski
Quoted 5 lines · On Tue, 24 Feb 2026 10:43:15 +0800 sun jian wrote:
Quoted 2 lines

Constructors seem to have a concept of priority so if we have to, we should probably used those for the fix.

I considered constructor priorities, but I'd prefer to avoid relying on toolchain/linker specific init ordering here.

I thought constructor priorities were specifically created to avoid the problem you are seeing. What it is the point of constructor priorities if not to order constructors??

You're right that ctor priorities exist to order constructors.

With clang+lld (LLVM=1) I can reproduce xfail nodes being attached to variant->xfails but with xfail->test == NULL, so the pointer wasn't available at XFAIL_ADD() registration time.

I don't object to ctor priorities per se. My concern is that fixing this via priorities likely requires ordering the entire registration chain, not just XFAIL_ADD(): kselftest_harness.h has multiple constructor-based registration sites (I see at least 5 macro-generated ctor blocks plus the constructor-order helper), so we'd need to thread and maintain explicit priorities across the relevant registration macros to guarantee that the test/fixture/variant metadata is fully registered before XFAIL_ADD() runs.

To avoid relying on init order, I'm happy to send a v2 which doesn't add new state and doesn't rely on ctor ordering: keep xfail->test as a cached pointer, but resolve it lazily at runtime on first use by looking up the test metadata by name within the fixture's test list, then use the original pointer match thereafter. This resolution happens at most once per xfail entry and keeps the hot path pointer-based.

If you prefer ctor priorities, I'm also happy to respin that way — but I'd appreciate guidance on which ctor(s)/registration macros you want ordered against XFAIL_ADD() so we can keep the change minimal.

Thanks,
sun jian

author
#7 lore raw
Quoted 3 lines · On Tue, 24 Feb 2026 10:43:15 +0800 Jakub Kicinski wrote:

I thought constructor priorities were specifically created to avoid the problem you are seeing. What it is the point of constructor priorities if not to order constructors??

Hi Jakub,

You're right. Looking closer at the harness ctor ordering, the NULL comes from TEST_F() setting _##fixture##_##test##_object inside its constructor (after the mmap() allocation), while XFAIL_ADD() reads that pointer in its constructor. If XFAIL_ADD runs first, xfail->test can be NULL.

Using constructor priorities should fix this cleanly by ordering the TEST_F registration ctor before XFAIL_ADD, without adding new fields or runtime lookups.

I'll respin a v2 along those lines and drop the fallback approach.

Thanks,
sun jian

author
#1 lore raw

PATCH 1/2 selftests/harness: order TEST_F and XFAIL_ADD constructors

In reply to Sun Jian

TEST_F() allocates and registers its struct __test_metadata via mmap() inside its constructor, and only then assigns the _##fixture_##test##_object pointer.

XFAIL_ADD() runs in a constructor too and reads _##fixture_##test##_object to initialize xfail->test. If XFAIL_ADD runs first, xfail->test can be NULL and the expected failure will be reported as FAIL.

Use constructor priorities to ensure TEST_F registration runs before XFAIL_ADD, without adding extra state or runtime lookups.

Fixes 2709473c9386 ("selftests: kselftest_harness: support using xfail")

---
v2:
- Switch from name-based fallback matching to ctor priorities.
- Drop the extra xfail state and runtime matching.

---
 tools/testing/selftests/kselftest_harness.h | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
tools/testing/selftests/kselftest_harness.h +5 -2
index 16a119a4656c..afcba0c4e98d 100644
@@ -175,6 +175,9 @@ static inline void __kselftest_memset_safe(void *s, int c, size_t n)
  *
  * EXPECT_* and ASSERT_* are valid in a TEST() { } context.
  */
+#define KSELFTEST_PRIO_TEST_F  20000
+#define KSELFTEST_PRIO_XFAIL   20001
+
 #define TEST_SIGNAL(test_name, signal) __TEST_IMPL(test_name, signal)
 
 #define __TEST_IMPL(test_name, _signal) \
@@ -465,7 +468,7 @@ static inline void __kselftest_memset_safe(void *s, int c, size_t n)
 			fixture_name##_teardown(_metadata, self, variant); \
 	} \
 	static struct __test_metadata *_##fixture_name##_##test_name##_object; \
-	static void __attribute__((constructor)) \
+	static void __attribute__((constructor(KSELFTEST_PRIO_TEST_F))) \
 			_register_##fixture_name##_##test_name(void) \
 	{ \
 		struct __test_metadata *object = mmap(NULL, sizeof(*object), \
@@ -880,7 +883,7 @@ struct __test_xfail {
 		.fixture = &_##fixture_name##_fixture_object, \
 		.variant = &_##fixture_name##_##variant_name##_object, \
 	}; \
-	static void __attribute__((constructor)) \
+	static void __attribute__((constructor(KSELFTEST_PRIO_XFAIL))) \
 		_register_##fixture_name##_##variant_name##_##test_name##_xfail(void) \
 	{ \
 		_##fixture_name##_##variant_name##_##test_name##_xfail.test = \

base-commit: 7dff99b354601dd01829e1511711846e04340a69

Signature · 1 line
2.43.0
author
#2 lore raw

PATCH 2/2 selftests: net: tun: don't abort XFAIL cases

The tun UDP tunnel GSO fixture contains XFAIL-marked variants intended to exercise failure paths (e.g. EMSGSIZE / "Message too long"). Using ASSERT_EQ() in these tests aborts the subtest, which prevents the harness from classifying them as XFAIL and can make the overall net: tun test fail.

Switch the relevant ASSERT_EQ() checks to EXPECT_EQ() so the subtests continue running and the failures are correctly reported and accounted as XFAIL where applicable.

---
 tools/testing/selftests/net/tun.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
tools/testing/selftests/net/tun.c +6 -6
index 8a5cd5cb5472..cf106a49b55e 100644
@@ -944,8 +944,8 @@ TEST_F(tun_vnet_udptnl, send_gso_packet)
 	ASSERT_EQ(ret, off);
 
 	ret = receive_gso_packet_from_tunnel(self, variant, &r_num_mss);
-	ASSERT_EQ(ret, variant->data_size);
-	ASSERT_EQ(r_num_mss, variant->r_num_mss);
+	EXPECT_EQ(ret, variant->data_size);
+	EXPECT_EQ(r_num_mss, variant->r_num_mss);
 }
 
 TEST_F(tun_vnet_udptnl, recv_gso_packet)
@@ -955,18 +955,18 @@ TEST_F(tun_vnet_udptnl, recv_gso_packet)
 	int ret, gso_type = VIRTIO_NET_HDR_GSO_UDP_L4;
 
 	ret = send_gso_packet_into_tunnel(self, variant);
-	ASSERT_EQ(ret, variant->data_size);
+	EXPECT_EQ(ret, variant->data_size);
 
 	memset(&vnet_hdr, 0, sizeof(vnet_hdr));
 	ret = receive_gso_packet_from_tun(self, variant, &vnet_hdr);
-	ASSERT_EQ(ret, variant->data_size);
+	EXPECT_EQ(ret, variant->data_size);
 
 	if (!variant->no_gso) {
-		ASSERT_EQ(vh->gso_size, variant->gso_size);
+		EXPECT_EQ(vh->gso_size, variant->gso_size);
 		gso_type |= (variant->tunnel_type & UDP_TUNNEL_OUTER_IPV4) ?
 				    (VIRTIO_NET_HDR_GSO_UDP_TUNNEL_IPV4) :
 				    (VIRTIO_NET_HDR_GSO_UDP_TUNNEL_IPV6);
-		ASSERT_EQ(vh->gso_type, gso_type);
+		EXPECT_EQ(vh->gso_type, gso_type);
 	}
 }
 
Signature · 1 line
2.43.0
author
#9 lore raw

PATCH v2 1/2 selftests/harness: order TEST_F and XFAIL_ADD constructors

In reply to Sun Jian

TEST_F() allocates and registers its struct __test_metadata via mmap() inside its constructor, and only then assigns the _##fixture_##test##_object pointer.

XFAIL_ADD() runs in a constructor too and reads _##fixture_##test##_object to initialize xfail->test. If XFAIL_ADD runs first, xfail->test can be NULL and the expected failure will be reported as FAIL.

Use constructor priorities to ensure TEST_F registration runs before XFAIL_ADD, without adding extra state or runtime lookups.

Fixes 2709473c9386 ("selftests: kselftest_harness: support using xfail")

---
v2:
- Switch from name-based fallback matching to ctor priorities.
- Drop the extra xfail state and runtime matching.

---
 tools/testing/selftests/kselftest_harness.h | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
tools/testing/selftests/kselftest_harness.h +5 -2
index 16a119a4656c..afcba0c4e98d 100644
@@ -175,6 +175,9 @@ static inline void __kselftest_memset_safe(void *s, int c, size_t n)
  *
  * EXPECT_* and ASSERT_* are valid in a TEST() { } context.
  */
+#define KSELFTEST_PRIO_TEST_F  20000
+#define KSELFTEST_PRIO_XFAIL   20001
+
 #define TEST_SIGNAL(test_name, signal) __TEST_IMPL(test_name, signal)
 
 #define __TEST_IMPL(test_name, _signal) \
@@ -465,7 +468,7 @@ static inline void __kselftest_memset_safe(void *s, int c, size_t n)
 			fixture_name##_teardown(_metadata, self, variant); \
 	} \
 	static struct __test_metadata *_##fixture_name##_##test_name##_object; \
-	static void __attribute__((constructor)) \
+	static void __attribute__((constructor(KSELFTEST_PRIO_TEST_F))) \
 			_register_##fixture_name##_##test_name(void) \
 	{ \
 		struct __test_metadata *object = mmap(NULL, sizeof(*object), \
@@ -880,7 +883,7 @@ struct __test_xfail {
 		.fixture = &_##fixture_name##_fixture_object, \
 		.variant = &_##fixture_name##_##variant_name##_object, \
 	}; \
-	static void __attribute__((constructor)) \
+	static void __attribute__((constructor(KSELFTEST_PRIO_XFAIL))) \
 		_register_##fixture_name##_##variant_name##_##test_name##_xfail(void) \
 	{ \
 		_##fixture_name##_##variant_name##_##test_name##_xfail.test = \

base-commit: 7dff99b354601dd01829e1511711846e04340a69

Signature · 1 line
2.43.0
author
#10 lore raw

PATCH v2 2/2 selftests: net: tun: don't abort XFAIL cases

The tun UDP tunnel GSO fixture contains XFAIL-marked variants intended to exercise failure paths (e.g. EMSGSIZE / "Message too long"). Using ASSERT_EQ() in these tests aborts the subtest, which prevents the harness from classifying them as XFAIL and can make the overall net: tun test fail.

Switch the relevant ASSERT_EQ() checks to EXPECT_EQ() so the subtests continue running and the failures are correctly reported and accounted as XFAIL where applicable.

---
 tools/testing/selftests/net/tun.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
tools/testing/selftests/net/tun.c +6 -6
index 8a5cd5cb5472..cf106a49b55e 100644
@@ -944,8 +944,8 @@ TEST_F(tun_vnet_udptnl, send_gso_packet)
 	ASSERT_EQ(ret, off);
 
 	ret = receive_gso_packet_from_tunnel(self, variant, &r_num_mss);
-	ASSERT_EQ(ret, variant->data_size);
-	ASSERT_EQ(r_num_mss, variant->r_num_mss);
+	EXPECT_EQ(ret, variant->data_size);
+	EXPECT_EQ(r_num_mss, variant->r_num_mss);
 }
 
 TEST_F(tun_vnet_udptnl, recv_gso_packet)
@@ -955,18 +955,18 @@ TEST_F(tun_vnet_udptnl, recv_gso_packet)
 	int ret, gso_type = VIRTIO_NET_HDR_GSO_UDP_L4;
 
 	ret = send_gso_packet_into_tunnel(self, variant);
-	ASSERT_EQ(ret, variant->data_size);
+	EXPECT_EQ(ret, variant->data_size);
 
 	memset(&vnet_hdr, 0, sizeof(vnet_hdr));
 	ret = receive_gso_packet_from_tun(self, variant, &vnet_hdr);
-	ASSERT_EQ(ret, variant->data_size);
+	EXPECT_EQ(ret, variant->data_size);
 
 	if (!variant->no_gso) {
-		ASSERT_EQ(vh->gso_size, variant->gso_size);
+		EXPECT_EQ(vh->gso_size, variant->gso_size);
 		gso_type |= (variant->tunnel_type & UDP_TUNNEL_OUTER_IPV4) ?
 				    (VIRTIO_NET_HDR_GSO_UDP_TUNNEL_IPV4) :
 				    (VIRTIO_NET_HDR_GSO_UDP_TUNNEL_IPV6);
-		ASSERT_EQ(vh->gso_type, gso_type);
+		EXPECT_EQ(vh->gso_type, gso_type);
 	}
 }
 
Signature · 1 line
2.43.0
maintainer
#11 lore raw

Re: [PATCH v2 1/2] selftests/harness: order TEST_F and XFAIL_ADD constructors

In reply to Sun Jian

tools/testing/selftests/kselftest_harness.h static inline void __kselftest_memset_safe(void *s, int c, size_t n) · line 181

Quoted 7 lines · On Tue, 24 Feb 2026 19:38:59 +0800 Sun Jian wrote:

*
* EXPECT_* and ASSERT_* are valid in a TEST() { } context. */

tools/testing/selftests/kselftest_harness.h +3 -0
+#define KSELFTEST_PRIO_TEST_F  20000
+#define KSELFTEST_PRIO_XFAIL   20001
+
 #define TEST_SIGNAL(test_name, signal) __TEST_IMPL(test_name, signal)

You inserted these defines between the kdoc for TEST_SIGNAL() and its definition.

When you respin, two notes:
- please wait 24h after the previous version before sending a new one, to allow people in all timezones to comment and avoid splintering the discussion
- please start a new thread for each submission, and refer to the previous one with a link in the changelog:

v2:
- bla
- bla
v1: https://lore.kernel.org/[email protected]

Signature · 1 line
pw-bot: cr
author
#12 lore raw

Re: [PATCH v2 1/2] selftests/harness: order TEST_F and XFAIL_ADD constructors

Quoted 2 lines · Sun Jian

You inserted these defines between the kdoc for TEST_SIGNAL() and its definition.

Ack. I'll move the KSELFTEST_PRIO_* defines out of the TEST_SIGNAL() KDoc block so the comment stays adjacent to the macro it documents.

Quoted 6 lines

When you respin, two notes:
- please wait 24h after the previous version before sending a new one, to allow people in all timezones to comment and avoid splintering the discussion
- please start a new thread for each submission, and refer to the previous one with a link in the changelog:

Ack as well. I'll wait at least 24h before respinning, and I'll send v3 as a new thread with lore links to v1/v2 in the changelog.

Thanks,
Sun Jian