Skip to content

Commit c96f547

Browse files
hashbrowncipherIngo Molnar
authored and
Ingo Molnar
committed
delayacct: Account blkio completion on the correct task
Before commit: e33a9bb ("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 e33a9bb, 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 <[email protected]> Acked-by: Tejun Heo <[email protected]> Acked-by: Balbir Singh <[email protected]> Cc: <[email protected]> Cc: Brendan Gregg <[email protected]> Cc: Jens Axboe <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: [email protected] Fixes: e33a9bb ("sched/core: move IO scheduling accounting from io_schedule_timeout() into scheduler") Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Ingo Molnar <[email protected]>
1 parent a8750dd commit c96f547

File tree

3 files changed

+33
-23
lines changed

3 files changed

+33
-23
lines changed

include/linux/delayacct.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ extern void delayacct_init(void);
7171
extern void __delayacct_tsk_init(struct task_struct *);
7272
extern void __delayacct_tsk_exit(struct task_struct *);
7373
extern void __delayacct_blkio_start(void);
74-
extern void __delayacct_blkio_end(void);
74+
extern void __delayacct_blkio_end(struct task_struct *);
7575
extern int __delayacct_add_tsk(struct taskstats *, struct task_struct *);
7676
extern __u64 __delayacct_blkio_ticks(struct task_struct *);
7777
extern void __delayacct_freepages_start(void);
@@ -122,10 +122,10 @@ static inline void delayacct_blkio_start(void)
122122
__delayacct_blkio_start();
123123
}
124124

125-
static inline void delayacct_blkio_end(void)
125+
static inline void delayacct_blkio_end(struct task_struct *p)
126126
{
127127
if (current->delays)
128-
__delayacct_blkio_end();
128+
__delayacct_blkio_end(p);
129129
delayacct_clear_flag(DELAYACCT_PF_BLKIO);
130130
}
131131

@@ -169,7 +169,7 @@ static inline void delayacct_tsk_free(struct task_struct *tsk)
169169
{}
170170
static inline void delayacct_blkio_start(void)
171171
{}
172-
static inline void delayacct_blkio_end(void)
172+
static inline void delayacct_blkio_end(struct task_struct *p)
173173
{}
174174
static inline int delayacct_add_tsk(struct taskstats *d,
175175
struct task_struct *tsk)

kernel/delayacct.c

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,16 @@ void __delayacct_tsk_init(struct task_struct *tsk)
5151
* Finish delay accounting for a statistic using its timestamps (@start),
5252
* accumalator (@total) and @count
5353
*/
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)
5555
{
5656
s64 ns = ktime_get_ns() - *start;
5757
unsigned long flags;
5858

5959
if (ns > 0) {
60-
spin_lock_irqsave(&current->delays->lock, flags);
60+
spin_lock_irqsave(lock, flags);
6161
*total += ns;
6262
(*count)++;
63-
spin_unlock_irqrestore(&current->delays->lock, flags);
63+
spin_unlock_irqrestore(lock, flags);
6464
}
6565
}
6666

@@ -69,17 +69,25 @@ void __delayacct_blkio_start(void)
6969
current->delays->blkio_start = ktime_get_ns();
7070
}
7171

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)
7377
{
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);
8391
}
8492

8593
int __delayacct_add_tsk(struct taskstats *d, struct task_struct *tsk)
@@ -153,8 +161,10 @@ void __delayacct_freepages_start(void)
153161

154162
void __delayacct_freepages_end(void)
155163
{
156-
delayacct_end(&current->delays->freepages_start,
157-
&current->delays->freepages_delay,
158-
&current->delays->freepages_count);
164+
delayacct_end(
165+
&current->delays->lock,
166+
&current->delays->freepages_start,
167+
&current->delays->freepages_delay,
168+
&current->delays->freepages_count);
159169
}
160170

kernel/sched/core.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2056,7 +2056,7 @@ try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
20562056
p->state = TASK_WAKING;
20572057

20582058
if (p->in_iowait) {
2059-
delayacct_blkio_end();
2059+
delayacct_blkio_end(p);
20602060
atomic_dec(&task_rq(p)->nr_iowait);
20612061
}
20622062

@@ -2069,7 +2069,7 @@ try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
20692069
#else /* CONFIG_SMP */
20702070

20712071
if (p->in_iowait) {
2072-
delayacct_blkio_end();
2072+
delayacct_blkio_end(p);
20732073
atomic_dec(&task_rq(p)->nr_iowait);
20742074
}
20752075

@@ -2122,7 +2122,7 @@ static void try_to_wake_up_local(struct task_struct *p, struct rq_flags *rf)
21222122

21232123
if (!task_on_rq_queued(p)) {
21242124
if (p->in_iowait) {
2125-
delayacct_blkio_end();
2125+
delayacct_blkio_end(p);
21262126
atomic_dec(&rq->nr_iowait);
21272127
}
21282128
ttwu_activate(rq, p, ENQUEUE_WAKEUP | ENQUEUE_NOCLOCK);

0 commit comments

Comments
 (0)