git: bab24f22ba45 - main - kern/sched_shim.c: Provide a scheduler selection machinery

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Thu, 29 Jan 2026 18:12:25 UTC
The branch main has been updated by kib:

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

commit bab24f22ba4518e640d14765dbd196e7709e1f0e
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-01-22 05:09:57 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2026-01-29 18:11:55 +0000

    kern/sched_shim.c: Provide a scheduler selection machinery
    
    Reviewed by:    olce
    Tested by:      pho
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D54831
---
 sys/amd64/amd64/machdep.c     |  2 ++
 sys/arm/arm/machdep.c         |  3 +++
 sys/arm64/arm64/machdep.c     |  1 +
 sys/i386/i386/machdep.c       |  1 +
 sys/kern/sched_shim.c         | 49 +++++++++++++++++++++++++++++++++++++++++++
 sys/powerpc/powerpc/machdep.c |  2 ++
 sys/riscv/riscv/machdep.c     |  2 ++
 sys/sys/sched.h               | 14 +++++++++++++
 8 files changed, 74 insertions(+)

diff --git a/sys/amd64/amd64/machdep.c b/sys/amd64/amd64/machdep.c
index 8daa9c250db8..e56de986ccba 100644
--- a/sys/amd64/amd64/machdep.c
+++ b/sys/amd64/amd64/machdep.c
@@ -1352,6 +1352,8 @@ hammer_time(u_int64_t modulep, u_int64_t physfree)
 		TUNABLE_INT_FETCH("hw.use_xsave", &use_xsave);
 	}
 
+	sched_instance_select();
+
 	link_elf_ireloc();
 
 	/*
diff --git a/sys/arm/arm/machdep.c b/sys/arm/arm/machdep.c
index 9532e19a11b3..0b395d42fc4a 100644
--- a/sys/arm/arm/machdep.c
+++ b/sys/arm/arm/machdep.c
@@ -523,6 +523,9 @@ initarm(struct arm_boot_params *abp)
 	/* Do basic tuning, hz etc */
 	init_param1();
 
+	sched_instance_select();
+	/* link_elf_ireloc(); */
+
 	/*
 	 * Allocate a page for the system page mapped to 0xffff0000
 	 * This page will just contain the system vectors and can be
diff --git a/sys/arm64/arm64/machdep.c b/sys/arm64/arm64/machdep.c
index 6790f47a0f82..b1e22c900f3f 100644
--- a/sys/arm64/arm64/machdep.c
+++ b/sys/arm64/arm64/machdep.c
@@ -825,6 +825,7 @@ initarm(struct arm64_bootparams *abp)
 	PCPU_SET(curthread, &thread0);
 	PCPU_SET(midr, get_midr());
 
+	sched_instance_select();
 	link_elf_ireloc();
 #ifdef FDT
 	try_load_dtb();
diff --git a/sys/i386/i386/machdep.c b/sys/i386/i386/machdep.c
index 3f659432552c..821265cc2911 100644
--- a/sys/i386/i386/machdep.c
+++ b/sys/i386/i386/machdep.c
@@ -1544,6 +1544,7 @@ init386(int first)
 
 	/* Initialize preload_kmdp */
 	preload_initkmdp(!metadata_missing);
+	sched_instance_select();
 	link_elf_ireloc();
 
 	vm86_initialize();
diff --git a/sys/kern/sched_shim.c b/sys/kern/sched_shim.c
index 079d7d73ec45..2dbb6b928961 100644
--- a/sys/kern/sched_shim.c
+++ b/sys/kern/sched_shim.c
@@ -15,6 +15,7 @@
 #include <sys/proc.h>
 #include <sys/runq.h>
 #include <sys/sched.h>
+#include <sys/sysctl.h>
 #include <machine/ifunc.h>
 
 const struct sched_instance *active_sched;
@@ -92,3 +93,51 @@ DEFINE_SHIM0(sizeof_thread, int, sched_sizeof_thread)
 DEFINE_SHIM1(tdname, char *, sched_tdname, struct thread *, td)
 DEFINE_SHIM1(clear_tdname, void, sched_clear_tdname, struct thread *, td)
 DEFINE_SHIM0(init_ap, void, schedinit_ap)
+
+static char sched_name[32] = "ULE";
+
+SET_DECLARE(sched_instance_set, struct sched_selection);
+
+void
+sched_instance_select(void)
+{
+	struct sched_selection *s, **ss;
+	int i;
+
+	TUNABLE_STR_FETCH("kern.sched.name", sched_name, sizeof(sched_name));
+	SET_FOREACH(ss, sched_instance_set) {
+		s = *ss;
+		for (i = 0; s->name[i] == sched_name[i]; i++) {
+			if (s->name[i] == '\0') {
+				active_sched = s->instance;
+				return;
+			}
+		}
+	}
+
+	/*
+	 * No scheduler matching the configuration was found.  If
+	 * there is any scheduler compiled in, at all, use the first
+	 * scheduler from the linker set.
+	 */
+	if (SET_BEGIN(sched_instance_set) < SET_LIMIT(sched_instance_set)) {
+		s = *SET_BEGIN(sched_instance_set);
+		active_sched = s->instance;
+		for (i = 0;; i++) {
+			sched_name[i] = s->name[i];
+			if (s->name[i] == '\0')
+				break;
+		}
+	}
+}
+
+void
+schedinit(void)
+{
+	if (active_sched == NULL)
+		panic("Cannot find scheduler %s", sched_name);
+	active_sched->init();
+}
+
+SYSCTL_STRING(_kern_sched, OID_AUTO, name, CTLFLAG_RD, sched_name, 0,
+    "Scheduler name");
diff --git a/sys/powerpc/powerpc/machdep.c b/sys/powerpc/powerpc/machdep.c
index f4a065e1ce46..04b3967ee110 100644
--- a/sys/powerpc/powerpc/machdep.c
+++ b/sys/powerpc/powerpc/machdep.c
@@ -83,6 +83,7 @@
 #include <sys/reboot.h>
 #include <sys/reg.h>
 #include <sys/rwlock.h>
+#include <sys/sched.h>
 #include <sys/signalvar.h>
 #include <sys/syscallsubr.h>
 #include <sys/sysctl.h>
@@ -467,6 +468,7 @@ powerpc_init(vm_offset_t fdt, vm_offset_t toc, vm_offset_t ofentry, void *mdp,
 	 * Bring up MMU
 	 */
 	pmap_mmu_init();
+	sched_instance_select();
 	link_elf_ireloc();
 	pmap_bootstrap(startkernel, endkernel);
 	mtmsr(psl_kernset & ~PSL_EE);
diff --git a/sys/riscv/riscv/machdep.c b/sys/riscv/riscv/machdep.c
index 235cc651b87e..b213e8812bc7 100644
--- a/sys/riscv/riscv/machdep.c
+++ b/sys/riscv/riscv/machdep.c
@@ -479,6 +479,8 @@ parse_metadata(void)
 
 	/* Initialize preload_kmdp */
 	preload_initkmdp(true);
+	sched_instance_select();
+	/* link_elf_ireloc(); */
 
 	/* Read the boot metadata */
 	boothowto = MD_FETCH(preload_kmdp, MODINFOMD_HOWTO, int);
diff --git a/sys/sys/sched.h b/sys/sys/sched.h
index b733d4d07e34..27d0fc7d0c8d 100644
--- a/sys/sys/sched.h
+++ b/sys/sys/sched.h
@@ -68,6 +68,7 @@
 #ifdef SCHED_STATS
 #include <sys/pcpu.h>
 #endif
+#include <sys/linker_set.h>
 
 struct proc;
 struct thread;
@@ -287,6 +288,19 @@ struct sched_instance {
 
 extern const struct sched_instance *active_sched;
 
+struct sched_selection {
+	const char *name;
+	const struct sched_instance *instance;
+};
+#define	DECLARE_SCHEDULER(xsel_name, xsched_name, xsched_instance)		\
+	static struct sched_selection xsel_name = {				\
+		.name = xsched_name,						\
+		.instance = xsched_instance,				\
+	};									\
+	DATA_SET(sched_instance_set, xsel_name);
+
+void sched_instance_select(void);
+
 #endif /* _KERNEL */
 
 /* POSIX 1003.1b Process Scheduling */