git: 71679cf468ba - main - init_main: Switch from SLIST to STAILQ, fix order

From: Colin Percival <cperciva_at_FreeBSD.org>
Date: Wed, 06 Sep 2023 18:37:03 UTC
The branch main has been updated by cperciva:

URL: https://cgit.FreeBSD.org/src/commit/?id=71679cf468ba28c0e3c88371f3fde13fccec99bd

commit 71679cf468ba28c0e3c88371f3fde13fccec99bd
Author:     Colin Percival <cperciva@FreeBSD.org>
AuthorDate: 2023-09-05 23:46:38 +0000
Commit:     Colin Percival <cperciva@FreeBSD.org>
CommitDate: 2023-09-06 18:36:36 +0000

    init_main: Switch from SLIST to STAILQ, fix order
    
    Constructing an SLIST of SYSINITs by inserting them one by one at the
    head of the list resulted in them being sorted in anti-stable order:
    When two SYSINITs tied for (subsystem, order), they were executed in
    the reverse order to the order in which they appeared in the linker
    set.
    
    Note that while this changes struct sysinit, it doesn't affect ABI
    since SLIST_ENTRY and STAILQ_ENTRY are compatible (in both cases a
    single pointer to the next element).
    
    Fixes:  9a7add6d01f3 "init_main: Switch from sysinit array to SLIST"
    Reported by:    gallatin
    Reviewed by:    jhb, gallatin, emaste
    Tested by:      gallatin
    MFC after:      1 week
    Differential Revision:  https://reviews.freebsd.org/D41748
---
 sys/kern/init_main.c | 22 +++++++++++-----------
 sys/sys/kernel.h     |  2 +-
 2 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/sys/kern/init_main.c b/sys/kern/init_main.c
index aa82eafff9b0..a6b327339c80 100644
--- a/sys/kern/init_main.c
+++ b/sys/kern/init_main.c
@@ -165,7 +165,7 @@ SET_DECLARE(sysinit_set, struct sysinit);
 /*
  * The sysinit list itself.  Items are removed as they are run.
  */
-static SLIST_HEAD(sysinitlist, sysinit) sysinit_list;
+static STAILQ_HEAD(sysinitlist, sysinit) sysinit_list;
 
 /*
  * Compare two sysinits; return -1, 0, or 1 if a comes before, at the same time
@@ -194,12 +194,12 @@ sysinit_mklist(struct sysinitlist *list, struct sysinit **set,
 
 	TSENTER();
 	TSENTER2("listify");
-	SLIST_INIT(list);
+	STAILQ_INIT(list);
 	for (sipp = set; sipp < set_end; sipp++)
-		SLIST_INSERT_HEAD(list, *sipp, next);
+		STAILQ_INSERT_TAIL(list, *sipp, next);
 	TSEXIT2("listify");
 	TSENTER2("mergesort");
-	SLIST_MERGESORT(list, NULL, sysinit_compar, sysinit, next);
+	STAILQ_MERGESORT(list, NULL, sysinit_compar, sysinit, next);
 	TSEXIT2("mergesort");
 	TSEXIT();
 }
@@ -218,9 +218,9 @@ sysinit_add(struct sysinit **set, struct sysinit **set_end)
 	sysinit_mklist(&new_list, set, set_end);
 
 	/* Merge the new list into the existing one. */
-	TSENTER2("SLIST_MERGE");
-	SLIST_MERGE(&sysinit_list, &new_list, NULL, sysinit_compar, sysinit, next);
-	TSEXIT2("SLIST_MERGE");
+	TSENTER2("STAILQ_MERGE");
+	STAILQ_MERGE(&sysinit_list, &new_list, NULL, sysinit_compar, sysinit, next);
+	TSEXIT2("STAILQ_MERGE");
 
 	TSEXIT();
 }
@@ -284,11 +284,11 @@ mi_startup(void)
 	 * Perform each system initialization task from the ordered list.  Note
 	 * that if sysinit_list is modified (e.g. by a KLD) we will nonetheless
 	 * always perform the earlist-sorted sysinit at each step; using the
-	 * SLIST_FOREACH macro would result in items being skipped if inserted
+	 * STAILQ_FOREACH macro would result in items being skipped if inserted
 	 * earlier than the "current item".
 	 */
-	while ((sip = SLIST_FIRST(&sysinit_list)) != NULL) {
-		SLIST_REMOVE_HEAD(&sysinit_list, next);
+	while ((sip = STAILQ_FIRST(&sysinit_list)) != NULL) {
+		STAILQ_REMOVE_HEAD(&sysinit_list, next);
 
 		if (sip->subsystem == SI_SUB_DUMMY)
 			continue;	/* skip dummy task(s)*/
@@ -904,7 +904,7 @@ DB_SHOW_COMMAND_FLAGS(sysinit, db_show_sysinit, DB_CMD_MEMSAFE)
 	db_printf("SYSINIT vs Name(Ptr)\n");
 	db_printf("  Subsystem  Order\n");
 	db_printf("  Function(Name)(Arg)\n");
-	SLIST_FOREACH(sip, &sysinit_list, next) {
+	STAILQ_FOREACH(sip, &sysinit_list, next) {
 		db_show_print_syinit(sip, true);
 		if (db_pager_quit)
 			break;
diff --git a/sys/sys/kernel.h b/sys/sys/kernel.h
index 12e346514d1a..62b9e6a02b3c 100644
--- a/sys/sys/kernel.h
+++ b/sys/sys/kernel.h
@@ -220,7 +220,7 @@ typedef void (*sysinit_cfunc_t)(const void *);
 struct sysinit {
 	enum sysinit_sub_id	subsystem;	/* subsystem identifier*/
 	enum sysinit_elem_order	order;		/* init order within subsystem*/
-	SLIST_ENTRY(sysinit)	next;		/* singly-linked list */
+	STAILQ_ENTRY(sysinit)	next;		/* singly-linked list */
 	sysinit_cfunc_t func;			/* function		*/
 	const void	*udata;			/* multiplexer/argument */
 };