[PATCH] sched/cpupri: fix the task priority BUG_ON checks

From: Zeng Tao
Date: Thu Mar 25 2021 - 23:19:12 EST


The BUG_ON checks are intended to check if the task priotity is valid,
but in the function convert_prio, if the task priority is not valid, it
will be converted to an uninitialized stack variable. Fix it by moving
the BUG_ON checks to the default branch of convert_prio.

Fixes: 934fc3314b39 ("sched/cpupri: Remap CPUPRI_NORMAL to MAX_RT_PRIO-1")
Signed-off-by: Zeng Tao <prime.zeng@xxxxxxxxxxxxx>
---
kernel/sched/cpupri.c | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/kernel/sched/cpupri.c b/kernel/sched/cpupri.c
index ec9be78..c5a0e6e 100644
--- a/kernel/sched/cpupri.c
+++ b/kernel/sched/cpupri.c
@@ -60,6 +60,8 @@ static int convert_prio(int prio)
case MAX_RT_PRIO:
cpupri = CPUPRI_HIGHER; /* 100 */
break;
+ default:
+ BUG();
}

return cpupri;
@@ -148,8 +150,6 @@ int cpupri_find_fitness(struct cpupri *cp, struct task_struct *p,
int task_pri = convert_prio(p->prio);
int idx, cpu;

- BUG_ON(task_pri >= CPUPRI_NR_PRIORITIES);
-
for (idx = 0; idx < task_pri; idx++) {

if (!__cpupri_find(cp, p, lowest_mask, idx))
@@ -215,8 +215,6 @@ void cpupri_set(struct cpupri *cp, int cpu, int newpri)

newpri = convert_prio(newpri);

- BUG_ON(newpri >= CPUPRI_NR_PRIORITIES);
-
if (newpri == oldpri)
return;

--
2.8.1