Skip to contentSkip to conversation

sched/fair: WARN and refuse to set buddy when !se->on_rq

PATCH Superseded Daniel Axtens 3 participants 4 messages May 8, 2017 – May 9, 2017 Scheduler

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sched/fair: WARN and refuse to set buddy when !se->on_rq

If we set a next or last buddy for a se that is not on_rq, we will end up taking a NULL pointer dereference in wakeup_preempt_entity via pick_next_task_fair.

Detect when we would be about to do that, throw a warning and then refuse to actually set it.

This has been suggested at least twice[0][1]: just do it.

[0] https://marc.info/?l=linux-kernel&m=146651668921468&w=2
[1] https://lkml.org/lkml/2016/6/16/663

Cc Konstantin Khlebnikov <[email protected]>
Cc Ben Segall <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Thomas Gleixner <[email protected]>

---

I recently had to debug a problem with these (we hadn't backported Konstantin's patches in this area) and this would have saved a lot of time/pain.

---
 kernel/sched/fair.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
kernel/sched/fair.c +8 -2
index d71109321841..9b988815b4ad 100644
@@ -6168,8 +6168,11 @@ static void set_last_buddy(struct sched_entity *se)
 	if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
 		return;
 
-	for_each_sched_entity(se)
+	for_each_sched_entity(se) {
+		if (WARN_ON_ONCE(!se->on_rq))
+			return;
 		cfs_rq_of(se)->last = se;
+	}
 }
 
 static void set_next_buddy(struct sched_entity *se)
@@ -6177,8 +6180,11 @@ static void set_next_buddy(struct sched_entity *se)
 	if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
 		return;
 
-	for_each_sched_entity(se)
+	for_each_sched_entity(se) {
+		if (WARN_ON_ONCE(!se->on_rq))
+			return;
 		cfs_rq_of(se)->next = se;
+	}
 }
 
 static void set_skip_buddy(struct sched_entity *se)
Signature · 1 line
2.11.0
#1 lore raw
Quoted 18 lines · Daniel Axtens

If we set a next or last buddy for a se that is not on_rq, we will end up taking a NULL pointer dereference in wakeup_preempt_entity via pick_next_task_fair.

Detect when we would be about to do that, throw a warning and then refuse to actually set it.

This has been suggested at least twice[0][1]: just do it.

[0] https://marc.info/?l=linux-kernel&m=146651668921468&w=2
[1] https://lkml.org/lkml/2016/6/16/663

Cc Konstantin Khlebnikov <[email protected]>
Cc Ben Segall <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Thomas Gleixner <[email protected]>
Quoted 5 lines

---

I recently had to debug a problem with these (we hadn't backported Konstantin's patches in this area) and this would have saved a lot of time/pain.

Yep, this is worth debug. But I think it should be under compile-time config option.
Something like CONFIG_DEBUG_VM which enables VM_BUG_ON and VM_WARN_ON.

Quoted 37 lines
---
 kernel/sched/fair.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
kernel/sched/fair.c +8 -3
index d71109321841..9b988815b4ad 100644
@@ -6168,8 +6168,11 @@ static void set_last_buddy(struct sched_entity *se)
        if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
                return;
 
-       for_each_sched_entity(se)
+       for_each_sched_entity(se) {
+               if (WARN_ON_ONCE(!se->on_rq))
+                       return;
                cfs_rq_of(se)->last = se;
+       }
 }
 
 static void set_next_buddy(struct sched_entity *se)
@@ -6177,8 +6180,11 @@ static void set_next_buddy(struct sched_entity *se)
        if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
                return;
 
-       for_each_sched_entity(se)
+       for_each_sched_entity(se) {
+               if (WARN_ON_ONCE(!se->on_rq))
+                       return;
                cfs_rq_of(se)->next = se;
+       }
 }
 
 static void set_skip_buddy(struct sched_entity *se)
--

2.11.0

#2 lore raw
Quoted 21 lines · Konstantin Khlebnikov
Quoted 20 lines · Daniel Axtens

If we set a next or last buddy for a se that is not on_rq, we will end up taking a NULL pointer dereference in wakeup_preempt_entity via pick_next_task_fair.

Detect when we would be about to do that, throw a warning and then refuse to actually set it.

This has been suggested at least twice[0][1]: just do it.

[0] https://marc.info/?l=linux-kernel&m=146651668921468&w=2
[1] https://lkml.org/lkml/2016/6/16/663

Cc Konstantin Khlebnikov <[email protected]>
Cc Ben Segall <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Thomas Gleixner <[email protected]>

---

Quoted 7 lines
Quoted 3 lines

I recently had to debug a problem with these (we hadn't backported Konstantin's patches in this area) and this would have saved a lot of time/pain.

Yep, this is worth debug. But I think it should be under compile-time config option.
Something like CONFIG_DEBUG_VM which enables VM_BUG_ON and VM_WARN_ON.

OK, will respin.

Regards,
Daniel

Quoted 38 lines
Quoted 38 lines
---
 kernel/sched/fair.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
kernel/sched/fair.c +8 -3
index d71109321841..9b988815b4ad 100644
@@ -6168,8 +6168,11 @@ static void set_last_buddy(struct sched_entity *se)
        if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
                return;
 
-       for_each_sched_entity(se)
+       for_each_sched_entity(se) {
+               if (WARN_ON_ONCE(!se->on_rq))
+                       return;
                cfs_rq_of(se)->last = se;
+       }
 }
 
 static void set_next_buddy(struct sched_entity *se)
@@ -6177,8 +6180,11 @@ static void set_next_buddy(struct sched_entity *se)
        if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
                return;
 
-       for_each_sched_entity(se)
+       for_each_sched_entity(se) {
+               if (WARN_ON_ONCE(!se->on_rq))
+                       return;
                cfs_rq_of(se)->next = se;
+       }
 }
 
 static void set_skip_buddy(struct sched_entity *se)
--

2.11.0

maintainer
#3 lore raw
In reply to Konstantin Khlebnikov
Quoted 9 lines · Konstantin Khlebnikov
Quoted 3 lines · Daniel Axtens

I recently had to debug a problem with these (we hadn't backported Konstantin's patches in this area) and this would have saved a lot of time/pain.

Yep, this is worth debug. But I think it should be under compile-time config option.
Something like CONFIG_DEBUG_VM which enables VM_BUG_ON and VM_WARN_ON.

See SCHED_WARN_ON() and feel free to introduce SCHED_WARN_ON_ONCE() if you feel that's needed.