Skip to contentSkip to conversation

sched/cpupri: fix the task priority BUG_ON checks

PATCH In review Zeng Tao 2 participants 2 messages Mar 26, 2021 – Apr 6, 2021 Scheduler

lore
Share & tools
lore

Zeng Tao

author
#0 lore raw

PATCH 1/1 sched/cpupri: fix the task priority BUG_ON checks

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")
---
 kernel/sched/cpupri.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
kernel/sched/cpupri.c +2 -4
index ec9be78..c5a0e6e 100644
@@ -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;
 
Signature · 1 line
2.8.1
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 35 lines · Zeng Tao

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")
---
 kernel/sched/cpupri.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
kernel/sched/cpupri.c +2 -2
index ec9be78..c5a0e6e 100644
@@ -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))

kernel/sched/cpupri.c void cpupri_set(struct cpupri *cp, int cpu, int newpri) · line 220

Quoted 8 lines
kernel/sched/cpupri.c +0 -2
@@ -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;
 

LGTM. Currently, convert_prio() is never called with prio < CPUPRI_INVALID (-1) or prio > MAX_RT_PRIO (100) but in case it would then this would be caught much easier with this patch.