2023-01-28 16:26:31

by Pietro Borrello

[permalink] [raw]
Subject: [PATCH] sched: pick_next_rt_entity(): checked list_entry

Commit 326587b84078 ("sched: fix goto retry in pick_next_task_rt()")
removed any path which could make pick_next_rt_entity() return NULL.
However, BUG_ON(!rt_se) in _pick_next_task_rt() (the only caller of
pick_next_rt_entity()) still checks the error condition, which can
never happen, since list_entry() never returns NULL. Return
list_first_entry_or_null(queue, ...) to allow BUG to check the only
possible error condition here: the queue being empty which should
never happen.

Fixes: 326587b84078 ("sched: fix goto retry in pick_next_task_rt()")
Signed-off-by: Pietro Borrello <[email protected]>
---
kernel/sched/rt.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/sched/rt.c b/kernel/sched/rt.c
index ed2a47e4ddae..6088d5d83b75 100644
--- a/kernel/sched/rt.c
+++ b/kernel/sched/rt.c
@@ -1777,7 +1777,7 @@ static struct sched_rt_entity *pick_next_rt_entity(struct rt_rq *rt_rq)
BUG_ON(idx >= MAX_RT_PRIO);

queue = array->queue + idx;
- next = list_entry(queue->next, struct sched_rt_entity, run_list);
+ next = list_first_entry_or_null(queue, struct sched_rt_entity, run_list);

return next;
}

---
base-commit: 2241ab53cbb5cdb08a6b2d4688feb13971058f65
change-id: 20230128-list-entry-null-check-sched-a3f3dfd6d468

Best regards,
--
Pietro Borrello <[email protected]>


2023-01-30 13:43:12

by Ingo Molnar

[permalink] [raw]
Subject: Re: [PATCH] sched: pick_next_rt_entity(): checked list_entry


* Pietro Borrello <[email protected]> wrote:

> Commit 326587b84078 ("sched: fix goto retry in pick_next_task_rt()")
> removed any path which could make pick_next_rt_entity() return NULL.
> However, BUG_ON(!rt_se) in _pick_next_task_rt() (the only caller of
> pick_next_rt_entity()) still checks the error condition, which can
> never happen, since list_entry() never returns NULL. Return
> list_first_entry_or_null(queue, ...) to allow BUG to check the only
> possible error condition here: the queue being empty which should
> never happen.
>
> Fixes: 326587b84078 ("sched: fix goto retry in pick_next_task_rt()")
> Signed-off-by: Pietro Borrello <[email protected]>
> ---
> kernel/sched/rt.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/kernel/sched/rt.c b/kernel/sched/rt.c
> index ed2a47e4ddae..6088d5d83b75 100644
> --- a/kernel/sched/rt.c
> +++ b/kernel/sched/rt.c
> @@ -1777,7 +1777,7 @@ static struct sched_rt_entity *pick_next_rt_entity(struct rt_rq *rt_rq)
> BUG_ON(idx >= MAX_RT_PRIO);
>
> queue = array->queue + idx;
> - next = list_entry(queue->next, struct sched_rt_entity, run_list);
> + next = list_first_entry_or_null(queue, struct sched_rt_entity, run_list);
>
> return next;

I'd much rather have something like this:

SCHED_WARN_ON(list_empty(next));

So the debug check is part of CONFIG_SCHED_DEBUG=y, and not if it's turned
off. Also, this makes sure we don't crash (BUG_ON()) on the error
condition, in the hope of getting debug info out of the system.

Thanks,

Ingo