delayacct: Account blkio completion on the correct task

Before commit:

e33a9bba85a8 ("sched/core: move IO scheduling accounting from io_schedule_timeout() into scheduler")

delayacct_blkio_end() was called after context-switching into the task which
completed I/O.

This resulted in double counting: the task would account a delay both waiting
for I/O and for time spent in the runqueue.

With e33a9bba85a8, delayacct_blkio_end() is called by try_to_wake_up().
In ttwu, we have not yet context-switched. This is more correct, in that
the delay accounting ends when the I/O is complete.

But delayacct_blkio_end() relies on 'get_current()', and we have not yet
context-switched into the task whose I/O completed. This results in the
wrong task having its delay accounting statistics updated.

Instead of doing that, pass the task_struct being woken to delayacct_blkio_end(),
so that it can update the statistics of the correct task.

Signed-off-by: Josh Snyder <joshs@netflix.com>
Acked-by: Tejun Heo <tj@kernel.org>
Acked-by: Balbir Singh <bsingharora@gmail.com>
Cc: <stable@vger.kernel.org>
Cc: Brendan Gregg <bgregg@netflix.com>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: linux-block@vger.kernel.org
Fixes: e33a9bba85a8 ("sched/core: move IO scheduling accounting from io_schedule_timeout() into scheduler")
Link: http://lkml.kernel.org/r/1513613712-571-1-git-send-email-joshs@netflix.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>

authored by Josh Snyder and committed by Ingo Molnar c96f5471 a8750ddc

+33 -23
+4 -4
include/linux/delayacct.h
··· 71 71 extern void __delayacct_tsk_init(struct task_struct *); 72 72 extern void __delayacct_tsk_exit(struct task_struct *); 73 73 extern void __delayacct_blkio_start(void); 74 - extern void __delayacct_blkio_end(void); 74 + extern void __delayacct_blkio_end(struct task_struct *); 75 75 extern int __delayacct_add_tsk(struct taskstats *, struct task_struct *); 76 76 extern __u64 __delayacct_blkio_ticks(struct task_struct *); 77 77 extern void __delayacct_freepages_start(void); ··· 122 122 __delayacct_blkio_start(); 123 123 } 124 124 125 - static inline void delayacct_blkio_end(void) 125 + static inline void delayacct_blkio_end(struct task_struct *p) 126 126 { 127 127 if (current->delays) 128 - __delayacct_blkio_end(); 128 + __delayacct_blkio_end(p); 129 129 delayacct_clear_flag(DELAYACCT_PF_BLKIO); 130 130 } 131 131 ··· 169 169 {} 170 170 static inline void delayacct_blkio_start(void) 171 171 {} 172 - static inline void delayacct_blkio_end(void) 172 + static inline void delayacct_blkio_end(struct task_struct *p) 173 173 {} 174 174 static inline int delayacct_add_tsk(struct taskstats *d, 175 175 struct task_struct *tsk)
+26 -16
kernel/delayacct.c
··· 51 51 * Finish delay accounting for a statistic using its timestamps (@start), 52 52 * accumalator (@total) and @count 53 53 */ 54 - static void delayacct_end(u64 *start, u64 *total, u32 *count) 54 + static void delayacct_end(spinlock_t *lock, u64 *start, u64 *total, u32 *count) 55 55 { 56 56 s64 ns = ktime_get_ns() - *start; 57 57 unsigned long flags; 58 58 59 59 if (ns > 0) { 60 - spin_lock_irqsave(&current->delays->lock, flags); 60 + spin_lock_irqsave(lock, flags); 61 61 *total += ns; 62 62 (*count)++; 63 - spin_unlock_irqrestore(&current->delays->lock, flags); 63 + spin_unlock_irqrestore(lock, flags); 64 64 } 65 65 } 66 66 ··· 69 69 current->delays->blkio_start = ktime_get_ns(); 70 70 } 71 71 72 - void __delayacct_blkio_end(void) 72 + /* 73 + * We cannot rely on the `current` macro, as we haven't yet switched back to 74 + * the process being woken. 75 + */ 76 + void __delayacct_blkio_end(struct task_struct *p) 73 77 { 74 - if (current->delays->flags & DELAYACCT_PF_SWAPIN) 75 - /* Swapin block I/O */ 76 - delayacct_end(&current->delays->blkio_start, 77 - &current->delays->swapin_delay, 78 - &current->delays->swapin_count); 79 - else /* Other block I/O */ 80 - delayacct_end(&current->delays->blkio_start, 81 - &current->delays->blkio_delay, 82 - &current->delays->blkio_count); 78 + struct task_delay_info *delays = p->delays; 79 + u64 *total; 80 + u32 *count; 81 + 82 + if (p->delays->flags & DELAYACCT_PF_SWAPIN) { 83 + total = &delays->swapin_delay; 84 + count = &delays->swapin_count; 85 + } else { 86 + total = &delays->blkio_delay; 87 + count = &delays->blkio_count; 88 + } 89 + 90 + delayacct_end(&delays->lock, &delays->blkio_start, total, count); 83 91 } 84 92 85 93 int __delayacct_add_tsk(struct taskstats *d, struct task_struct *tsk) ··· 161 153 162 154 void __delayacct_freepages_end(void) 163 155 { 164 - delayacct_end(&current->delays->freepages_start, 165 - &current->delays->freepages_delay, 166 - &current->delays->freepages_count); 156 + delayacct_end( 157 + &current->delays->lock, 158 + &current->delays->freepages_start, 159 + &current->delays->freepages_delay, 160 + &current->delays->freepages_count); 167 161 } 168 162
+3 -3
kernel/sched/core.c
··· 2056 2056 p->state = TASK_WAKING; 2057 2057 2058 2058 if (p->in_iowait) { 2059 - delayacct_blkio_end(); 2059 + delayacct_blkio_end(p); 2060 2060 atomic_dec(&task_rq(p)->nr_iowait); 2061 2061 } 2062 2062 ··· 2069 2069 #else /* CONFIG_SMP */ 2070 2070 2071 2071 if (p->in_iowait) { 2072 - delayacct_blkio_end(); 2072 + delayacct_blkio_end(p); 2073 2073 atomic_dec(&task_rq(p)->nr_iowait); 2074 2074 } 2075 2075 ··· 2122 2122 2123 2123 if (!task_on_rq_queued(p)) { 2124 2124 if (p->in_iowait) { 2125 - delayacct_blkio_end(); 2125 + delayacct_blkio_end(p); 2126 2126 atomic_dec(&rq->nr_iowait); 2127 2127 } 2128 2128 ttwu_activate(rq, p, ENQUEUE_WAKEUP | ENQUEUE_NOCLOCK);