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

From: Peter Zijlstra
Date: Thu Sep 24 2020 - 07:50:51 EST


On Fri, Sep 18, 2020 at 01:27:59PM -0400, Tejun Heo wrote:
> 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.


diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index ebb90572e10d..259a4ae8ab8e 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -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);