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:	9a7add6d01 "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
This commit is contained in:
Colin Percival 2023-09-05 16:46:38 -07:00
parent 8f26d01f53
commit 71679cf468
2 changed files with 12 additions and 12 deletions

View file

@ -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;

View file

@ -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 */
};