Skip to content

Commit e75c8b4

Browse files
committed
Merge branch 'sched/core' of git://git.kernel.org/pub/scm/linux/kernel/git/frederic/linux-dynticks into sched/core
Pull CPU runtime stats/accounting fixes from Frederic Weisbecker: " Some users are complaining that their threadgroup's runtime accounting freezes after a week or so of intense cpu-bound workload. This set tries to fix the issue by reducing the risk of multiplication overflow in the cputime scaling code. " Stanislaw Gruszka further explained the historic context and impact of the bug: " Commit 0cf55e1 start to use scalling for whole thread group, so increase chances of hitting multiplication overflow, depending on how many CPUs are on the system. We have multiplication utime * rtime for one thread since commit b27f03d. Overflow will happen after: rtime * utime > 0xffffffffffffffff jiffies if thread utilize 100% of CPU time, that gives: rtime > sqrt(0xffffffffffffffff) jiffies ritme > sqrt(0xffffffffffffffff) / (24 * 60 * 60 * HZ) days For HZ 100 it will be 497 days for HZ 1000 it will be 49 days. Bug affect only users, who run CPU intensive application for that long period. Also they have to be interested on utime,stime values, as bug has no other visible effect as making those values incorrect. " Signed-off-by: Ingo Molnar <mingo@kernel.org>
2 parents 1bf0823 + d9a3c98 commit e75c8b4

3 files changed

Lines changed: 65 additions & 19 deletions

File tree

‎include/linux/math64.h‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ static inline s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder)
2929
return dividend / divisor;
3030
}
3131

32+
/**
33+
* div64_u64_rem - unsigned 64bit divide with 64bit divisor
34+
*/
35+
static inline u64 div64_u64_rem(u64 dividend, u64 divisor, u64 *remainder)
36+
{
37+
*remainder = dividend % divisor;
38+
return dividend / divisor;
39+
}
40+
3241
/**
3342
* div64_u64 - unsigned 64bit divide with 64bit divisor
3443
*/
@@ -61,8 +70,16 @@ static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
6170
extern s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder);
6271
#endif
6372

73+
#ifndef div64_u64_rem
74+
extern u64 div64_u64_rem(u64 dividend, u64 divisor, u64 *remainder);
75+
#endif
76+
6477
#ifndef div64_u64
65-
extern u64 div64_u64(u64 dividend, u64 divisor);
78+
static inline u64 div64_u64(u64 dividend, u64 divisor)
79+
{
80+
u64 remainder;
81+
return div64_u64_rem(dividend, divisor, &remainder);
82+
}
6683
#endif
6784

6885
#ifndef div64_s64

‎kernel/sched/cputime.c‎

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -521,18 +521,36 @@ void account_idle_ticks(unsigned long ticks)
521521
account_idle_time(jiffies_to_cputime(ticks));
522522
}
523523

524-
static cputime_t scale_stime(cputime_t stime, cputime_t rtime, cputime_t total)
524+
/*
525+
* Perform (stime * rtime) / total with reduced chances
526+
* of multiplication overflows by using smaller factors
527+
* like quotient and remainders of divisions between
528+
* rtime and total.
529+
*/
530+
static cputime_t scale_stime(u64 stime, u64 rtime, u64 total)
525531
{
526-
u64 temp = (__force u64) rtime;
532+
u64 rem, res, scaled;
527533

528-
temp *= (__force u64) stime;
529-
530-
if (sizeof(cputime_t) == 4)
531-
temp = div_u64(temp, (__force u32) total);
532-
else
533-
temp = div64_u64(temp, (__force u64) total);
534+
if (rtime >= total) {
535+
/*
536+
* Scale up to rtime / total then add
537+
* the remainder scaled to stime / total.
538+
*/
539+
res = div64_u64_rem(rtime, total, &rem);
540+
scaled = stime * res;
541+
scaled += div64_u64(stime * rem, total);
542+
} else {
543+
/*
544+
* Same in reverse: scale down to total / rtime
545+
* then substract that result scaled to
546+
* to the remaining part.
547+
*/
548+
res = div64_u64_rem(total, rtime, &rem);
549+
scaled = div64_u64(stime, res);
550+
scaled -= div64_u64(scaled * rem, total);
551+
}
534552

535-
return (__force cputime_t) temp;
553+
return (__force cputime_t) scaled;
536554
}
537555

538556
/*
@@ -566,10 +584,14 @@ static void cputime_adjust(struct task_cputime *curr,
566584
*/
567585
rtime = nsecs_to_cputime(curr->sum_exec_runtime);
568586

569-
if (total)
570-
stime = scale_stime(stime, rtime, total);
571-
else
587+
if (!rtime) {
588+
stime = 0;
589+
} else if (!total) {
572590
stime = rtime;
591+
} else {
592+
stime = scale_stime((__force u64)stime,
593+
(__force u64)rtime, (__force u64)total);
594+
}
573595

574596
/*
575597
* If the tick based count grows faster than the scheduler one,

‎lib/div64.c‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,37 +79,44 @@ EXPORT_SYMBOL(div_s64_rem);
7979
#endif
8080

8181
/**
82-
* div64_u64 - unsigned 64bit divide with 64bit divisor
82+
* div64_u64_rem - unsigned 64bit divide with 64bit divisor and 64bit remainder
8383
* @dividend: 64bit dividend
8484
* @divisor: 64bit divisor
85+
* @remainder: 64bit remainder
8586
*
8687
* This implementation is a modified version of the algorithm proposed
8788
* by the book 'Hacker's Delight'. The original source and full proof
8889
* can be found here and is available for use without restriction.
8990
*
9091
* 'http://www.hackersdelight.org/HDcode/newCode/divDouble.c.txt'
9192
*/
92-
#ifndef div64_u64
93-
u64 div64_u64(u64 dividend, u64 divisor)
93+
#ifndef div64_u64_rem
94+
u64 div64_u64_rem(u64 dividend, u64 divisor, u64 *remainder)
9495
{
9596
u32 high = divisor >> 32;
9697
u64 quot;
9798

9899
if (high == 0) {
99-
quot = div_u64(dividend, divisor);
100+
u32 rem32;
101+
quot = div_u64_rem(dividend, divisor, &rem32);
102+
*remainder = rem32;
100103
} else {
101104
int n = 1 + fls(high);
102105
quot = div_u64(dividend >> n, divisor >> n);
103106

104107
if (quot != 0)
105108
quot--;
106-
if ((dividend - quot * divisor) >= divisor)
109+
110+
*remainder = dividend - quot * divisor;
111+
if (*remainder >= divisor) {
107112
quot++;
113+
*remainder -= divisor;
114+
}
108115
}
109116

110117
return quot;
111118
}
112-
EXPORT_SYMBOL(div64_u64);
119+
EXPORT_SYMBOL(div64_u64_rem);
113120
#endif
114121

115122
/**

0 commit comments

Comments
 (0)