Skip to content

Commit e7f2be1

Browse files
Frederic WeisbeckerKAGA-KOKO
authored andcommitted
sched/cputime: Fix getrusage(RUSAGE_THREAD) with nohz_full
getrusage(RUSAGE_THREAD) with nohz_full may return shorter utime/stime than the actual time. task_cputime_adjusted() snapshots utime and stime and then adjust their sum to match the scheduler maintained cputime.sum_exec_runtime. Unfortunately in nohz_full, sum_exec_runtime is only updated once per second in the worst case, causing a discrepancy against utime and stime that can be updated anytime by the reader using vtime. To fix this situation, perform an update of cputime.sum_exec_runtime when the cputime snapshot reports the task as actually running while the tick is disabled. The related overhead is then contained within the relevant situations. Reported-by: Hasegawa Hitomi <hasegawa-hitomi@fujitsu.com> Signed-off-by: Frederic Weisbecker <frederic@kernel.org> Signed-off-by: Hasegawa Hitomi <hasegawa-hitomi@fujitsu.com> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Tested-by: Masayoshi Mizuma <m.mizuma@jp.fujitsu.com> Acked-by: Phil Auld <pauld@redhat.com> Link: https://lore.kernel.org/r/20211026141055.57358-3-frederic@kernel.org
1 parent d58071a commit e7f2be1

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

include/linux/sched/cputime.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@
1818
#endif /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
1919

2020
#ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
21-
extern void task_cputime(struct task_struct *t,
21+
extern bool task_cputime(struct task_struct *t,
2222
u64 *utime, u64 *stime);
2323
extern u64 task_gtime(struct task_struct *t);
2424
#else
25-
static inline void task_cputime(struct task_struct *t,
25+
static inline bool task_cputime(struct task_struct *t,
2626
u64 *utime, u64 *stime)
2727
{
2828
*utime = t->utime;
2929
*stime = t->stime;
30+
return false;
3031
}
3132

3233
static inline u64 task_gtime(struct task_struct *t)

kernel/sched/cputime.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,8 @@ void task_cputime_adjusted(struct task_struct *p, u64 *ut, u64 *st)
615615
.sum_exec_runtime = p->se.sum_exec_runtime,
616616
};
617617

618-
task_cputime(p, &cputime.utime, &cputime.stime);
618+
if (task_cputime(p, &cputime.utime, &cputime.stime))
619+
cputime.sum_exec_runtime = task_sched_runtime(p);
619620
cputime_adjust(&cputime, &p->prev_cputime, ut, st);
620621
}
621622
EXPORT_SYMBOL_GPL(task_cputime_adjusted);
@@ -828,19 +829,21 @@ u64 task_gtime(struct task_struct *t)
828829
* add up the pending nohz execution time since the last
829830
* cputime snapshot.
830831
*/
831-
void task_cputime(struct task_struct *t, u64 *utime, u64 *stime)
832+
bool task_cputime(struct task_struct *t, u64 *utime, u64 *stime)
832833
{
833834
struct vtime *vtime = &t->vtime;
834835
unsigned int seq;
835836
u64 delta;
837+
int ret;
836838

837839
if (!vtime_accounting_enabled()) {
838840
*utime = t->utime;
839841
*stime = t->stime;
840-
return;
842+
return false;
841843
}
842844

843845
do {
846+
ret = false;
844847
seq = read_seqcount_begin(&vtime->seqcount);
845848

846849
*utime = t->utime;
@@ -850,6 +853,7 @@ void task_cputime(struct task_struct *t, u64 *utime, u64 *stime)
850853
if (vtime->state < VTIME_SYS)
851854
continue;
852855

856+
ret = true;
853857
delta = vtime_delta(vtime);
854858

855859
/*
@@ -861,6 +865,8 @@ void task_cputime(struct task_struct *t, u64 *utime, u64 *stime)
861865
else
862866
*utime += vtime->utime + delta;
863867
} while (read_seqcount_retry(&vtime->seqcount, seq));
868+
869+
return ret;
864870
}
865871

866872
static int vtime_state_fetch(struct vtime *vtime, int cpu)

0 commit comments

Comments
 (0)