Skip to contentSkip to conversation

xen/sched: Optimise when only one scheduler is compiled in

RFC PATCH In review Andrew Cooper 3 participants 6 messages Mar 3, 2022 – Mar 4, 2022

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 xen/sched: Optimise when only one scheduler is compiled in

When only one scheduler is compiled in, function pointers can be optimised to direct calls, and the hooks hardened against controlflow hijacking.

RFC for several reasons.

1) There's an almost beautiful way of not introducing MAYBE_SCHED() and hiding the magic in REGISTER_SCHEDULER(), except it falls over
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91765 which has no comment or resolution at all.

2) A different alternative which almost works is to remove the indirection in .data.schedulers, but the singleton scheduler object can't be both there and in .init.rodata.cf_clobber.

3) I can't think of a way of build time check to enforce that new schedulers get added to the preprocessor magic.

And the blocker:
4) This isn't compatible with how sched_idle_ops get used for granularity > 1.

Suggestions very welcome.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
CC: Juergen Gross <[email protected]>
CC: Dario Faggioli <[email protected]>

---
 xen/common/sched/arinc653.c |  2 +-
 xen/common/sched/core.c     |  4 +-
 xen/common/sched/credit.c   |  2 +-
 xen/common/sched/credit2.c  |  2 +-
 xen/common/sched/null.c     |  2 +-
 xen/common/sched/private.h  | 91 ++++++++++++++++++++++++++++++++-------------
 xen/common/sched/rt.c       |  2 +-
 7 files changed, 72 insertions(+), 33 deletions(-)
xen/common/sched/arinc653.c +1 -1
index a82c0d7314a1..73738b007e7d 100644
@@ -694,7 +694,7 @@ a653sched_adjust_global(const struct scheduler *ops,
  * callback functions.
  * The symbol must be visible to the rest of Xen at link time.
  */
-static const struct scheduler sched_arinc653_def = {
+const struct scheduler MAYBE_SCHED(sched_arinc653_def) = {
     .name           = "ARINC 653 Scheduler",
     .opt_name       = "arinc653",
     .sched_id       = XEN_SCHEDULER_ARINC653,
xen/common/sched/core.c +2 -2
index 19ab67818106..020a5741ca31 100644
@@ -2263,7 +2263,7 @@ static struct sched_unit *do_schedule(struct sched_unit *prev, s_time_t now,
     struct sched_unit *next;
 
     /* get policy-specific decision on scheduling... */
-    sched->do_schedule(sched, prev, now, sched_tasklet_check(cpu));
+    sched_vcall(sched, do_schedule, sched, prev, now, sched_tasklet_check(cpu));
 
     next = prev->next_task;
 
@@ -2975,7 +2975,7 @@ void __init scheduler_init(void)
 
 #undef sched_test_func
 
-        if ( schedulers[i]->global_init && schedulers[i]->global_init() < 0 )
+        if ( sched_global_init(schedulers[i]) < 0 )
         {
             printk("scheduler %s failed initialization, dropped\n",
                    schedulers[i]->opt_name);
xen/common/sched/credit.c +1 -1
index 4d3bd8cba6fc..8b85e9617fc0 100644
@@ -2230,7 +2230,7 @@ csched_deinit(struct scheduler *ops)
     }
 }
 
-static const struct scheduler sched_credit_def = {
+const struct scheduler MAYBE_SCHED(sched_credit_def) = {
     .name           = "SMP Credit Scheduler",
     .opt_name       = "credit",
     .sched_id       = XEN_SCHEDULER_CREDIT,
xen/common/sched/credit2.c +1 -1
index 0e3f89e5378e..fda3812d7ac1 100644
@@ -4199,7 +4199,7 @@ csched2_deinit(struct scheduler *ops)
     xfree(prv);
 }
 
-static const struct scheduler sched_credit2_def = {
+const struct scheduler MAYBE_SCHED(sched_credit2_def) = {
     .name           = "SMP Credit Scheduler rev2",
     .opt_name       = "credit2",
     .sched_id       = XEN_SCHEDULER_CREDIT2,
xen/common/sched/null.c +1 -1
index 65a0a6c5312d..907a8ae1ca50 100644
@@ -1025,7 +1025,7 @@ static void cf_check null_dump(const struct scheduler *ops)
     spin_unlock_irqrestore(&prv->lock, flags);
 }
 
-static const struct scheduler sched_null_def = {
+const struct scheduler MAYBE_SCHED(sched_null_def) = {
     .name           = "null Scheduler",
     .opt_name       = "null",
     .sched_id       = XEN_SCHEDULER_NULL,
xen/common/sched/private.h +65 -26

231 lines not on this page · raw message

xen/common/sched/rt.c +1 -1
index d6de25531b3c..9b42852b2de5 100644
@@ -1529,7 +1529,7 @@ static void cf_check repl_timer_handler(void *data)
     spin_unlock_irq(&prv->lock);
 }
 
-static const struct scheduler sched_rtds_def = {
+const struct scheduler MAYBE_SCHED(sched_rtds_def) = {
     .name           = "SMP RTDS Scheduler",
     .opt_name       = "rtds",
     .sched_id       = XEN_SCHEDULER_RTDS,
Signature · 1 line
2.11.0
#1 lore raw
Quoted 5 lines · Andrew Cooper

When only one scheduler is compiled in, function pointers can be optimised to direct calls, and the hooks hardened against controlflow hijacking.

RFC for several reasons.

Quoted 8 lines

1) There's an almost beautiful way of not introducing MAYBE_SCHED() and hiding the magic in REGISTER_SCHEDULER(), except it falls over
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91765 which has no comment or resolution at all.

2) A different alternative which almost works is to remove the indirection in .data.schedulers, but the singleton scheduler object can't be both there and in .init.rodata.cf_clobber.

Couldn't we name the section differently when there's just one member, placing that section inside __initdata_cf_clobber_{start,end} in the linker script?

Quoted 2 lines

3) I can't think of a way of build time check to enforce that new schedulers get added to the preprocessor magic.

An assertion in the linker script, checking that .data.schedulers has a single entry when the sched_ops symbol exists? This may involve a PROVIDE(sched_ops = 0) as there doesn't look to be a way to probe for symbol defined-ness in expressions.

Quoted 2 lines

And the blocker:
4) This isn't compatible with how sched_idle_ops get used for granularity > 1.

Special case it just like we special case plt_tsc in x86/time.c?

xen/common/sched/private.h static inline spinlock_t *pcpu_schedule_trylock(unsigned int cpu) · line 280

Quoted 13 lines
xen/common/sched/private.h +7 -0
@@ -271,6 +271,33 @@ static inline spinlock_t *pcpu_schedule_trylock(unsigned int cpu)
     return NULL;
 }
 
+#if 1 ==                                                                \
+    defined(CONFIG_SCHED_CREDIT) + defined(CONFIG_SCHED_CREDIT2) +      \
+    defined(CONFIG_SCHED_RTDS) + defined(CONFIG_SCHED_ARINC653) +       \
+    defined(CONFIG_SCHED_NULL)
+
+extern const struct scheduler sched_ops;
+#define MAYBE_SCHED(x) __initdata_cf_clobber sched_ops

__initconst_cf_clobber, seeing that all use sites also use const?

xen/common/sched/private.h struct scheduler { · line 368

Quoted 10 lines
xen/common/sched/private.h +6 -0
@@ -333,39 +360,48 @@ struct scheduler {
     void         (*dump_cpu_state) (const struct scheduler *, int);
 };
 
+static inline int sched_global_init(const struct scheduler *s)
+{
+    if ( s->global_init )
+        return sched_call(s, global_init);
+    return 0;
+}

Is it really a good idea to expose this here when it's supposed to be used from core.c only, and even there in just a single place?

Jan

#2 lore raw
In reply to Andrew Cooper
Quoted 14 lines · Andrew Cooper

When only one scheduler is compiled in, function pointers can be optimised to direct calls, and the hooks hardened against controlflow hijacking.

RFC for several reasons.

1) There's an almost beautiful way of not introducing MAYBE_SCHED() and hiding the magic in REGISTER_SCHEDULER(), except it falls over
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91765 which has no comment or resolution at all.

2) A different alternative which almost works is to remove the indirection in .data.schedulers, but the singleton scheduler object can't be both there and in .init.rodata.cf_clobber.

Quoted 7 lines

3) I can't think of a way of build time check to enforce that new schedulers get added to the preprocessor magic.

And the blocker:
4) This isn't compatible with how sched_idle_ops get used for granularity > 1.

Suggestions very welcome.

Did you consider to generate the needed code dynamically instead?

I guess this could even be extended to avoid function pointers completely using the same technique as in my hypercall series.

In order to avoid the need for a central table the per-scheduler hooks could use standard names (as most of them do already).

I think I could come up with a patch in a few hours if you like that approach.

Juergen

#3 lore raw
Quoted 25 lines · Juergen Gross
Quoted 14 lines · Andrew Cooper

When only one scheduler is compiled in, function pointers can be optimised to direct calls, and the hooks hardened against controlflow hijacking.

RFC for several reasons.

1) There's an almost beautiful way of not introducing MAYBE_SCHED() and hiding the magic in REGISTER_SCHEDULER(), except it falls over
    https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91765 which has no comment or resolution at all.

2) A different alternative which almost works is to remove the indirection in .data.schedulers, but the singleton scheduler object can't be both there and in .init.rodata.cf_clobber.

Quoted 7 lines

3) I can't think of a way of build time check to enforce that new schedulers get added to the preprocessor magic.

And the blocker:
4) This isn't compatible with how sched_idle_ops get used for granularity > 1.

Suggestions very welcome.

Did you consider to generate the needed code dynamically instead?

Quoted 8 lines

I guess this could even be extended to avoid function pointers completely using the same technique as in my hypercall series.

In order to avoid the need for a central table the per-scheduler hooks could use standard names (as most of them do already).

I think I could come up with a patch in a few hours if you like that approach.

BTW, in theory this approach could be generalized for other function vectors in the hypervisor, too (maybe even all?).

Juergen

#4 lore raw
In reply to Juergen Gross
Quoted 25 lines · Juergen Gross
Quoted 14 lines · Andrew Cooper

When only one scheduler is compiled in, function pointers can be optimised to direct calls, and the hooks hardened against controlflow hijacking.

RFC for several reasons.

1) There's an almost beautiful way of not introducing MAYBE_SCHED() and hiding the magic in REGISTER_SCHEDULER(), except it falls over
    https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91765 which has no comment or resolution at all.

2) A different alternative which almost works is to remove the indirection in .data.schedulers, but the singleton scheduler object can't be both there and in .init.rodata.cf_clobber.

Quoted 7 lines

3) I can't think of a way of build time check to enforce that new schedulers get added to the preprocessor magic.

And the blocker:
4) This isn't compatible with how sched_idle_ops get used for granularity > 1.

Suggestions very welcome.

Did you consider to generate the needed code dynamically instead?

Quoted 8 lines

I guess this could even be extended to avoid function pointers completely using the same technique as in my hypercall series.

In order to avoid the need for a central table the per-scheduler hooks could use standard names (as most of them do already).

I think I could come up with a patch in a few hours if you like that approach.

BTW, in theory this approach could be generalized for other function vectors in the hypervisor, too (maybe even all?).

Juergen

#5 lore raw
In reply to Andrew Cooper
Quoted 14 lines · Andrew Cooper

When only one scheduler is compiled in, function pointers can be optimised to direct calls, and the hooks hardened against controlflow hijacking.

RFC for several reasons.

1) There's an almost beautiful way of not introducing MAYBE_SCHED() and hiding the magic in REGISTER_SCHEDULER(), except it falls over
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91765 which has no comment or resolution at all.

2) A different alternative which almost works is to remove the indirection in .data.schedulers, but the singleton scheduler object can't be both there and in .init.rodata.cf_clobber.

Quoted 5 lines

3) I can't think of a way of build time check to enforce that new schedulers get added to the preprocessor magic.

And the blocker:
4) This isn't compatible with how sched_idle_ops get used for granularity > 1.

Correct. Either you have core scheduling or you can optimize the indirect calls to direct ones.

Quoted 1 line

Suggestions very welcome.

Dynamic code generation for replacing the function vector with scheduler id based if/switch statements.

Something like the attached patch for generating the code (won't build right now as the related scheduler code adaptions are missing, but the header is created).

The code generation script could share quite some code with my hypercall series, and expansion for other function vectors should be rather easy.

Juergen

From 4bdee23501739236bee34d9c5dae90fef2bde057 Mon Sep 17 00:00:00 2001
From: Juergen Gross <[email protected]>
Date: Thu, 3 Mar 2022 10:32:22 +0100
Subject: [PATCH] xen/sched: generate code for calling scheduler callbacks

In order to avoid calls via function pointers don't use per scheduler call vectors, but generate the respective call stubs dynamically based on the defined schedulers.

Use a definition file for defining the templates of the callbacks and the configured schedulers. The stubs will be using if () and switch () statements to call into the correct scheduler using the scheduler id. The default scheduler is likely to be used in most cases, so it is called using an if ( likely() ) construct.

In case only a single scheduler is configured the stub will be just a wrapper for that scheduler.

The callbacks inside the single schedulers need to have standard names in the form "sched_callback" with "sched" being the scheduler name (e.g. "credit2"), and "callback" being the specific function (e.g. "init"), resulting in a function name like "credit2_init".

---
 .gitignore                          |   1 +
 xen/common/sched/Makefile           |  12 +++
 xen/common/sched/gen-sched-defs.awk | 152 ++++++++++++++++++++++++++++
 xen/common/sched/private.h          |   2 +
 xen/common/sched/sched-defs.c       |  62 ++++++++++++
 5 files changed, 229 insertions(+)
 create mode 100644 xen/common/sched/gen-sched-defs.awk
 create mode 100644 xen/common/sched/sched-defs.c
.gitignore +1 -0
index d425be4bd9..d976cbf794 100644
@@ -316,6 +316,7 @@ xen/arch/*/efi/runtime.c
 xen/arch/*/include/asm/asm-offsets.h
 xen/common/config_data.S
 xen/common/config.gz
+xen/common/sched/sched-defs.h
 xen/include/headers*.chk
 xen/include/compat/*
 xen/include/config/
xen/common/sched/Makefile +12 -0
index 3537f2a68d..f7ba8b184c 100644
@@ -5,3 +5,15 @@ obj-$(CONFIG_SCHED_CREDIT2) += credit2.o
 obj-$(CONFIG_SCHED_RTDS) += rt.o
 obj-$(CONFIG_SCHED_NULL) += null.o
 obj-y += core.o
+
+quiet_cmd_gendefs = GEN     $@
+cmd_gendefs = awk -f $(src)/gen-sched-defs.awk <$< >$@
+
+$(addprefix $(obj)/,$(obj-y)): $(obj)/sched-defs.h
+
+$(obj)/sched-defs.h: $(obj)/sched-defs.i $(src)/gen-sched-defs.awk FORCE
+	$(call if_changed,gendefs)
+
+targets += sched-defs.h
+
+clean-files := sched-defs.h sched-defs.i
xen/common/sched/gen-sched-defs.awk new file +152 -0

152 lines not on this page · raw message

xen/common/sched/private.h +2 -0
index a870320146..1669e82f6b 100644
@@ -608,4 +608,6 @@ void cpupool_put(struct cpupool *pool);
 int cpupool_add_domain(struct domain *d, unsigned int poolid);
 void cpupool_rm_domain(struct domain *d);
 
+#include "sched-defs.h"
+
 #endif /* __XEN_SCHED_IF_H__ */
xen/common/sched/sched-defs.c new file +62 -0

62 lines not on this page · raw message

Signature · 1 line
2.34.1