[PATCH] xen/sched: fix scheduler callback verification on init

Ruslan Ruslichenko posted 1 patch 3 months ago
Patches applied successfully (tree, apply log)
git fetch https://gitlab.com/xen-project/patchew/xen tags/patchew/20260410164039.1000284-1-ruslichenko.r@gmail.com
xen/common/sched/core.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
[PATCH] xen/sched: fix scheduler callback verification on init
Posted by Ruslan Ruslichenko 3 months ago
From: Ruslan Ruslichenko <Ruslan_Ruslichenko@epam.com>

During core scheduler initialization, each registered scheduler
is sanity tested in two steps:

- it must provide required callbacks (e.g. init, do_schedule).
- if global_init callback is present, it must succeed.

If any of the steps fail, scheduler entry is cleared in global
'schedulers' array.

However, in the current implementation, if verification fails during
the first step, the scheduler entry is cleared but verification
sequence is not interrupted. This lead to NULL pointer dereference
when subsequent required callbacks verified, and possible during
the second step.

The patch fixes the crashes by adding check inside sched_test_func
macro and skipping the call to a global_init if first step did not pass.

Signed-off-by: Ruslan Ruslichenko <Ruslan_Ruslichenko@epam.com>
---
 xen/common/sched/core.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/xen/common/sched/core.c b/xen/common/sched/core.c
index a57d5dd929..4270c89491 100644
--- a/xen/common/sched/core.c
+++ b/xen/common/sched/core.c
@@ -3017,7 +3017,7 @@ void __init scheduler_init(void)
     for ( i = 0; i < NUM_SCHEDULERS; i++)
     {
 #define sched_test_func(f)                               \
-        if ( !schedulers[i]->f )                         \
+        if ( schedulers[i] && !schedulers[i]->f )        \
         {                                                \
             printk("scheduler %s misses .%s, dropped\n", \
                    schedulers[i]->opt_name, #f);         \
@@ -3034,6 +3034,9 @@ void __init scheduler_init(void)
 
 #undef sched_test_func
 
+        if ( !schedulers[i] )
+            continue;
+
         if ( schedulers[i]->global_init && schedulers[i]->global_init() < 0 )
         {
             printk("scheduler %s failed initialization, dropped\n",
-- 
2.43.0
Re: [PATCH] xen/sched: fix scheduler callback verification on init
Posted by Jan Beulich 2 months, 3 weeks ago
On 10.04.2026 18:40, Ruslan Ruslichenko wrote:
> From: Ruslan Ruslichenko <Ruslan_Ruslichenko@epam.com>
> 
> During core scheduler initialization, each registered scheduler
> is sanity tested in two steps:
> 
> - it must provide required callbacks (e.g. init, do_schedule).
> - if global_init callback is present, it must succeed.
> 
> If any of the steps fail, scheduler entry is cleared in global
> 'schedulers' array.
> 
> However, in the current implementation, if verification fails during
> the first step, the scheduler entry is cleared but verification
> sequence is not interrupted. This lead to NULL pointer dereference
> when subsequent required callbacks verified, and possible during
> the second step.
> 
> The patch fixes the crashes by adding check inside sched_test_func
> macro and skipping the call to a global_init if first step did not pass.
> 
> Signed-off-by: Ruslan Ruslichenko <Ruslan_Ruslichenko@epam.com>

This likely also wants a Fixes: tag.

Jan
Re: [PATCH] xen/sched: fix scheduler callback verification on init
Posted by Mykyta Poturai 2 months, 3 weeks ago
On 4/10/26 19:40, Ruslan Ruslichenko wrote:
> From: Ruslan Ruslichenko <Ruslan_Ruslichenko@epam.com>
> 
> During core scheduler initialization, each registered scheduler
> is sanity tested in two steps:
> 
> - it must provide required callbacks (e.g. init, do_schedule).
> - if global_init callback is present, it must succeed.
> 
> If any of the steps fail, scheduler entry is cleared in global
> 'schedulers' array.
> 
> However, in the current implementation, if verification fails during
> the first step, the scheduler entry is cleared but verification
> sequence is not interrupted. This lead to NULL pointer dereference
> when subsequent required callbacks verified, and possible during
> the second step.
> 
> The patch fixes the crashes by adding check inside sched_test_func
> macro and skipping the call to a global_init if first step did not pass.
> 
> Signed-off-by: Ruslan Ruslichenko <Ruslan_Ruslichenko@epam.com>
> ---
>   xen/common/sched/core.c | 5 ++++-
>   1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/xen/common/sched/core.c b/xen/common/sched/core.c
> index a57d5dd929..4270c89491 100644
> --- a/xen/common/sched/core.c
> +++ b/xen/common/sched/core.c
> @@ -3017,7 +3017,7 @@ void __init scheduler_init(void)
>       for ( i = 0; i < NUM_SCHEDULERS; i++)
>       {
>   #define sched_test_func(f)                               \
> -        if ( !schedulers[i]->f )                         \
> +        if ( schedulers[i] && !schedulers[i]->f )        \
>           {                                                \
>               printk("scheduler %s misses .%s, dropped\n", \
>                      schedulers[i]->opt_name, #f);         \
 >               schedulers[i] = NULL;                        \

Maybe it would be cleaner to just add "continue" here?

> @@ -3034,6 +3034,9 @@ void __init scheduler_init(void)
>   
>   #undef sched_test_func
>   
> +        if ( !schedulers[i] )
> +            continue;
> +
>           if ( schedulers[i]->global_init && schedulers[i]->global_init() < 0 )
>           {
>               printk("scheduler %s failed initialization, dropped\n",

-- 
Mykyta
Re: [PATCH] xen/sched: fix scheduler callback verification on init
Posted by Ruslan Ruslichenko 2 months, 3 weeks ago
On Tue, Apr 14, 2026 at 1:10 PM Mykyta Poturai <Mykyta_Poturai@epam.com> wrote:
>
> On 4/10/26 19:40, Ruslan Ruslichenko wrote:
> > From: Ruslan Ruslichenko <Ruslan_Ruslichenko@epam.com>
> >
> > During core scheduler initialization, each registered scheduler
> > is sanity tested in two steps:
> >
> > - it must provide required callbacks (e.g. init, do_schedule).
> > - if global_init callback is present, it must succeed.
> >
> > If any of the steps fail, scheduler entry is cleared in global
> > 'schedulers' array.
> >
> > However, in the current implementation, if verification fails during
> > the first step, the scheduler entry is cleared but verification
> > sequence is not interrupted. This lead to NULL pointer dereference
> > when subsequent required callbacks verified, and possible during
> > the second step.
> >
> > The patch fixes the crashes by adding check inside sched_test_func
> > macro and skipping the call to a global_init if first step did not pass.
> >
> > Signed-off-by: Ruslan Ruslichenko <Ruslan_Ruslichenko@epam.com>
> > ---
> >   xen/common/sched/core.c | 5 ++++-
> >   1 file changed, 4 insertions(+), 1 deletion(-)
> >
> > diff --git a/xen/common/sched/core.c b/xen/common/sched/core.c
> > index a57d5dd929..4270c89491 100644
> > --- a/xen/common/sched/core.c
> > +++ b/xen/common/sched/core.c
> > @@ -3017,7 +3017,7 @@ void __init scheduler_init(void)
> >       for ( i = 0; i < NUM_SCHEDULERS; i++)
> >       {
> >   #define sched_test_func(f)                               \
> > -        if ( !schedulers[i]->f )                         \
> > +        if ( schedulers[i] && !schedulers[i]->f )        \
> >           {                                                \
> >               printk("scheduler %s misses .%s, dropped\n", \
> >                      schedulers[i]->opt_name, #f);         \
>  >               schedulers[i] = NULL;                        \
>
> Maybe it would be cleaner to just add "continue" here?
>

Probably, yes. This will also skip unneeded checks after the first
missing callback.
I will update the patch then.

--
BR,
Ruslan

> > @@ -3034,6 +3034,9 @@ void __init scheduler_init(void)
> >
> >   #undef sched_test_func
> >
> > +        if ( !schedulers[i] )
> > +            continue;
> > +
> >           if ( schedulers[i]->global_init && schedulers[i]->global_init() < 0 )
> >           {
> >               printk("scheduler %s failed initialization, dropped\n",
>
> --
> Mykyta