Skip to contentSkip to conversation

sched: rq->nr_iowait transiently going negative after the recent p->on_cpu optimization

DISCUSSION Tejun Heo 2 participants 5 messages Sep 18, 2020 – Sep 24, 2020 Scheduler

lore
Share & tools
lore

Tejun Heo

author
#0 lore raw

Hello,

Peter, I noticed /proc/stat::procs_blocked going U64_MAX transiently once in the blue moon without any other persistent issues. After looking at the code with Rik for a bit, the culprit seems to be c6e7bd7afaeb ("sched/core:
Optimize ttwu() spinning on p->on_cpu") - it changed where ttwu dec's nr_iowait and it looks like that can happen before the target task gets to inc.

Thanks.

Signature · 1 line
tejun
maintainer
#1 lore raw
Quoted 8 lines · Tejun Heo

Hello,

Peter, I noticed /proc/stat::procs_blocked going U64_MAX transiently once in the blue moon without any other persistent issues. After looking at the code with Rik for a bit, the culprit seems to be c6e7bd7afaeb ("sched/core:
Optimize ttwu() spinning on p->on_cpu") - it changed where ttwu dec's nr_iowait and it looks like that can happen before the target task gets to inc.

Hurmph.. I suppose you're right :/ And this is an actual problem?

I think the below should cure that, but blergh, not nice. If you could confirm, I'll try and think of something nicer.

kernel/sched/core.c +10 -5
index ebb90572e10d..259a4ae8ab8e 100644
@@ -2505,7 +2505,12 @@ ttwu_do_activate(struct rq *rq, struct task_struct *p, int wake_flags,
 #ifdef CONFIG_SMP
 	if (wake_flags & WF_MIGRATED)
 		en_flags |= ENQUEUE_MIGRATED;
+	else
 #endif
+	if (p->in_iowait) {
+		delayacct_blkio_end(p);
+		atomic_dec(&task_rq(p)->nr_iowait);
+	}
 
 	activate_task(rq, p, en_flags);
 	ttwu_do_wakeup(rq, p, wake_flags, rf);
@@ -2892,11 +2897,6 @@ try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
 	if (READ_ONCE(p->on_rq) && ttwu_runnable(p, wake_flags))
 		goto unlock;
 
-	if (p->in_iowait) {
-		delayacct_blkio_end(p);
-		atomic_dec(&task_rq(p)->nr_iowait);
-	}
-
 #ifdef CONFIG_SMP
 	/*
 	 * Ensure we load p->on_cpu _after_ p->on_rq, otherwise it would be
@@ -2967,6 +2967,11 @@ try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
 
 	cpu = select_task_rq(p, p->wake_cpu, SD_BALANCE_WAKE, wake_flags);
 	if (task_cpu(p) != cpu) {
+		if (p->in_iowait) {
+			delayacct_blkio_end(p);
+			atomic_dec(&task_rq(p)->nr_iowait);
+		}
+
 		wake_flags |= WF_MIGRATED;
 		psi_ttwu_dequeue(p);
 		set_task_cpu(p, cpu);
author
#2 lore raw

Hello,

Quoted 1 line · Peter Zijlstra

Hurmph.. I suppose you're right :/ And this is an actual problem?

Yeah, this got exposed to userspace as a full 64bit number which overflowed u32 conversion in the rust procfs library which aborted a program I was working on multiple times over several months.

On a more theoretical side, it might also surprise nr_iowait_cpu() users. However, a real problem that may be.

Quoted 2 lines

I think the below should cure that, but blergh, not nice. If you could confirm, I'll try and think of something nicer.

Rik suggested that it'd be sufficient to return 0 on underflow especially given that 0 is actually the right number to describe the state. So, maybe that can be a nicer code-wise?

Thanks.

Signature · 1 line
tejun
maintainer
#3 lore raw
Quoted 12 lines · Tejun Heo

Hello,

Quoted 1 line · Peter Zijlstra

Hurmph.. I suppose you're right :/ And this is an actual problem?

Yeah, this got exposed to userspace as a full 64bit number which overflowed u32 conversion in the rust procfs library which aborted a program I was working on multiple times over several months.

On a more theoretical side, it might also surprise nr_iowait_cpu() users. However, a real problem that may be.

Quoted 6 lines
Quoted 2 lines

I think the below should cure that, but blergh, not nice. If you could confirm, I'll try and think of something nicer.

Rik suggested that it'd be sufficient to return 0 on underflow especially given that 0 is actually the right number to describe the state. So, maybe that can be a nicer code-wise?

I worry about things where one CPU has a positive value and one or more (other) CPUs have a temporary negative value.

author
#4 lore raw
Quoted 6 lines · Peter Zijlstra
Quoted 3 lines

Rik suggested that it'd be sufficient to return 0 on underflow especially given that 0 is actually the right number to describe the state. So, maybe that can be a nicer code-wise?

I worry about things where one CPU has a positive value and one or more (other) CPUs have a temporary negative value.

I think it'd make more sense to max'ing them per-cpu as that's the right per-cpu state. nr_iowait_cpu() needs that anyway, so maybe the summing function can just use nr_iowait_cpu()?

Thanks.

Signature · 1 line
tejun