Skip to contentSkip to conversation

selftests: harness: refactor __constructor_order

PATCH In review Masahiro Yamada 3 participants 5 messages Jul 27, 2024 – Aug 6, 2024

Subsystems (7)
KVM Kselftest BPF HID +3 more subsystems
lore
Share & tools
lore
#0 lore raw

v2 cover selftests: harness: refactor __constructor_order

This series refactors __constructor_order because
__constructor_order_last() is unneeded.

No code change since v1.
I reworded "reverse-order" to "backward-order" in commit description.

Masahiro Yamada (2):
selftests: harness: remove unneeded __constructor_order_last()
selftests: harness: rename __constructor_order for clarification

 .../drivers/s390x/uvdevice/test_uvdevice.c     |  6 ------
 tools/testing/selftests/hid/hid_bpf.c          |  6 ------
 tools/testing/selftests/kselftest_harness.h    | 18 ++++--------------
 tools/testing/selftests/rtc/rtctest.c          |  7 -------
 4 files changed, 4 insertions(+), 33 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/2 selftests: harness: remove unneeded __constructor_order_last()

__constructor_order_last() is unneeded.

If __constructor_order_last() is not called on backward-order systems, __constructor_order will remain 0 instead of being set to _CONSTRUCTOR_ORDER_BACKWARD (= -1).

__LIST_APPEND() will still take the 'else' branch, so there is no difference in the behavior.

---

Changes in v2:
- reword "reverse-order" to "backward-order" in commit description

 .../selftests/drivers/s390x/uvdevice/test_uvdevice.c   |  6 ------
 tools/testing/selftests/hid/hid_bpf.c                  |  6 ------
 tools/testing/selftests/kselftest_harness.h            | 10 +---------
 tools/testing/selftests/rtc/rtctest.c                  |  7 -------
 4 files changed, 1 insertion(+), 28 deletions(-)
tools/testing/selftests/drivers/s390x/uvdevice/test_uvdevice.c +0 -6
index ea0cdc37b44f..7ee7492138c6 100644
@@ -257,12 +257,6 @@ TEST_F(attest_fixture, att_inval_addr)
 	att_inval_addr_test(&self->uvio_attest.meas_addr, _metadata, self);
 }
 
-static void __attribute__((constructor)) __constructor_order_last(void)
-{
-	if (!__constructor_order)
-		__constructor_order = _CONSTRUCTOR_ORDER_BACKWARD;
-}
-
 int main(int argc, char **argv)
 {
 	int fd = open(UV_PATH, O_ACCMODE);
tools/testing/selftests/hid/hid_bpf.c +0 -6
index dc0408a831d0..f2bd20ca88bb 100644
@@ -1331,12 +1331,6 @@ static int libbpf_print_fn(enum libbpf_print_level level,
 	return 0;
 }
 
-static void __attribute__((constructor)) __constructor_order_last(void)
-{
-	if (!__constructor_order)
-		__constructor_order = _CONSTRUCTOR_ORDER_BACKWARD;
-}
-
 int main(int argc, char **argv)
 {
 	/* Use libbpf 1.0 API mode */
tools/testing/selftests/kselftest_harness.h +1 -9
index 40723a6a083f..71648d2102cb 100644
@@ -488,12 +488,6 @@
  * Use once to append a main() to the test file.
  */
 #define TEST_HARNESS_MAIN \
-	static void __attribute__((constructor)) \
-	__constructor_order_last(void) \
-	{ \
-		if (!__constructor_order) \
-			__constructor_order = _CONSTRUCTOR_ORDER_BACKWARD; \
-	} \
 	int main(int argc, char **argv) { \
 		return test_harness_run(argc, argv); \
 	}
@@ -891,7 +885,6 @@ static struct __fixture_metadata *__fixture_list = &_fixture_global;
 static int __constructor_order;
 
 #define _CONSTRUCTOR_ORDER_FORWARD   1
-#define _CONSTRUCTOR_ORDER_BACKWARD -1
 
 static inline void __register_fixture(struct __fixture_metadata *f)
 {
@@ -1337,8 +1330,7 @@ static int test_harness_run(int argc, char **argv)
 
 static void __attribute__((constructor)) __constructor_order_first(void)
 {
-	if (!__constructor_order)
-		__constructor_order = _CONSTRUCTOR_ORDER_FORWARD;
+	__constructor_order = _CONSTRUCTOR_ORDER_FORWARD;
 }
 
 #endif  /* __KSELFTEST_HARNESS_H */
tools/testing/selftests/rtc/rtctest.c +0 -7
index 63ce02d1d5cc..9647b14b47c5 100644
@@ -410,13 +410,6 @@ TEST_F_TIMEOUT(rtc, alarm_wkalm_set_minute, 65) {
 	ASSERT_EQ(new, secs);
 }
 
-static void __attribute__((constructor))
-__constructor_order_last(void)
-{
-	if (!__constructor_order)
-		__constructor_order = _CONSTRUCTOR_ORDER_BACKWARD;
-}
-
 int main(int argc, char **argv)
 {
 	switch (argc) {
Signature · 1 line
2.43.0
#2 lore raw

PATCH v2 2/2 selftests: harness: rename __constructor_order for clarification

In reply to Masahiro Yamada

Now, __constructor_order is boolean; 1 for forward-order systems, 0 for backward-order systems while parsing __LIST_APPEND().

Change it into a bool variable, and rename it for clarification.

---

Changes in v2:
- reword "reverse-order" to "backward-order" in commit description

 tools/testing/selftests/kselftest_harness.h | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
tools/testing/selftests/kselftest_harness.h +4 -6
index 71648d2102cb..a5a72415e37b 100644
@@ -818,7 +818,7 @@
 		item->prev = item; \
 		return;	\
 	} \
-	if (__constructor_order == _CONSTRUCTOR_ORDER_FORWARD) { \
+	if (__constructor_order_forward) { \
 		item->next = NULL; \
 		item->prev = head->prev; \
 		item->prev->next = item; \
@@ -882,9 +882,7 @@ struct __test_xfail {
 	}
 
 static struct __fixture_metadata *__fixture_list = &_fixture_global;
-static int __constructor_order;
-
-#define _CONSTRUCTOR_ORDER_FORWARD   1
+static bool __constructor_order_forward;
 
 static inline void __register_fixture(struct __fixture_metadata *f)
 {
@@ -935,7 +933,7 @@ static inline bool __test_passed(struct __test_metadata *metadata)
  * list so tests are run in source declaration order.
  * https://gcc.gnu.org/onlinedocs/gccint/Initialization.html
  * However, it seems not all toolchains do this correctly, so use
- * __constructor_order to detect which direction is called first
+ * __constructor_order_foward to detect which direction is called first
  * and adjust list building logic to get things running in the right
  * direction.
  */
@@ -1330,7 +1328,7 @@ static int test_harness_run(int argc, char **argv)
 
 static void __attribute__((constructor)) __constructor_order_first(void)
 {
-	__constructor_order = _CONSTRUCTOR_ORDER_FORWARD;
+	__constructor_order_forward = true;
 }
 
 #endif  /* __KSELFTEST_HARNESS_H */
Signature · 1 line
2.43.0
maintainer Reviewed-by on the v2 series
#3 lore raw
In reply to Masahiro Yamada
Quoted 11 lines · Masahiro Yamada

This series refactors __constructor_order because
__constructor_order_last() is unneeded.

No code change since v1.
I reworded "reverse-order" to "backward-order" in commit description.

Masahiro Yamada (2):
selftests: harness: remove unneeded __constructor_order_last()
selftests: harness: rename __constructor_order for clarification

Thanks for resending this!

Shuah, do you want to take this via kselftest? If not, I can carry it...

-Kees

Signature · 1 line
Kees Cook
maintainer
#4 lore raw
Quoted 13 lines · Kees Cook
Quoted 11 lines · Masahiro Yamada

This series refactors __constructor_order because
__constructor_order_last() is unneeded.

No code change since v1.
I reworded "reverse-order" to "backward-order" in commit description.

Masahiro Yamada (2):
selftests: harness: remove unneeded __constructor_order_last()
selftests: harness: rename __constructor_order for clarification

Quoted 8 lines

Thanks for resending this!

Shuah, do you want to take this via kselftest? If not, I can carry it...

-Kees

Just about to ask you if you want me take it :)

Yes I can take this - I will apply this for Linux 6.12-rc1.

thanks,
-- Shuah