Skip to content

Commit 0279b3c

Browse files
committed
Merge branch 'sched-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull scheduler fixes from Ingo Molnar: "This fixes the cputime scaling overflow problems for good without having bad 32-bit overhead, and gets rid of the div64_u64_rem() helper as well." * 'sched-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: Revert "math64: New div64_u64_rem helper" sched: Avoid prev->stime underflow sched: Do not account bogus utime sched: Avoid cputime scaling overflow
2 parents 797994f + f300213 commit 0279b3c

File tree

3 files changed

+58
-60
lines changed

3 files changed

+58
-60
lines changed

include/linux/math64.h

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,6 @@ 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-
4132
/**
4233
* div64_u64 - unsigned 64bit divide with 64bit divisor
4334
*/
@@ -70,16 +61,8 @@ static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
7061
extern s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder);
7162
#endif
7263

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

8568
#ifndef div64_s64

kernel/sched/cputime.c

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -506,34 +506,47 @@ void account_idle_ticks(unsigned long ticks)
506506
}
507507

508508
/*
509-
* Perform (stime * rtime) / total with reduced chances
510-
* of multiplication overflows by using smaller factors
511-
* like quotient and remainders of divisions between
512-
* rtime and total.
509+
* Perform (stime * rtime) / total, but avoid multiplication overflow by
510+
* loosing precision when the numbers are big.
513511
*/
514512
static cputime_t scale_stime(u64 stime, u64 rtime, u64 total)
515513
{
516-
u64 rem, res, scaled;
514+
u64 scaled;
517515

518-
if (rtime >= total) {
519-
/*
520-
* Scale up to rtime / total then add
521-
* the remainder scaled to stime / total.
522-
*/
523-
res = div64_u64_rem(rtime, total, &rem);
524-
scaled = stime * res;
525-
scaled += div64_u64(stime * rem, total);
526-
} else {
527-
/*
528-
* Same in reverse: scale down to total / rtime
529-
* then substract that result scaled to
530-
* to the remaining part.
531-
*/
532-
res = div64_u64_rem(total, rtime, &rem);
533-
scaled = div64_u64(stime, res);
534-
scaled -= div64_u64(scaled * rem, total);
516+
for (;;) {
517+
/* Make sure "rtime" is the bigger of stime/rtime */
518+
if (stime > rtime) {
519+
u64 tmp = rtime; rtime = stime; stime = tmp;
520+
}
521+
522+
/* Make sure 'total' fits in 32 bits */
523+
if (total >> 32)
524+
goto drop_precision;
525+
526+
/* Does rtime (and thus stime) fit in 32 bits? */
527+
if (!(rtime >> 32))
528+
break;
529+
530+
/* Can we just balance rtime/stime rather than dropping bits? */
531+
if (stime >> 31)
532+
goto drop_precision;
533+
534+
/* We can grow stime and shrink rtime and try to make them both fit */
535+
stime <<= 1;
536+
rtime >>= 1;
537+
continue;
538+
539+
drop_precision:
540+
/* We drop from rtime, it has more bits than stime */
541+
rtime >>= 1;
542+
total >>= 1;
535543
}
536544

545+
/*
546+
* Make sure gcc understands that this is a 32x32->64 multiply,
547+
* followed by a 64/32->64 divide.
548+
*/
549+
scaled = div_u64((u64) (u32) stime * (u64) (u32) rtime, (u32)total);
537550
return (__force cputime_t) scaled;
538551
}
539552

@@ -545,7 +558,7 @@ static void cputime_adjust(struct task_cputime *curr,
545558
struct cputime *prev,
546559
cputime_t *ut, cputime_t *st)
547560
{
548-
cputime_t rtime, stime, total;
561+
cputime_t rtime, stime, utime, total;
549562

550563
if (vtime_accounting_enabled()) {
551564
*ut = curr->utime;
@@ -568,13 +581,21 @@ static void cputime_adjust(struct task_cputime *curr,
568581
*/
569582
rtime = nsecs_to_cputime(curr->sum_exec_runtime);
570583

571-
if (!rtime) {
572-
stime = 0;
573-
} else if (!total) {
574-
stime = rtime;
575-
} else {
584+
/*
585+
* Update userspace visible utime/stime values only if actual execution
586+
* time is bigger than already exported. Note that can happen, that we
587+
* provided bigger values due to scaling inaccuracy on big numbers.
588+
*/
589+
if (prev->stime + prev->utime >= rtime)
590+
goto out;
591+
592+
if (total) {
576593
stime = scale_stime((__force u64)stime,
577594
(__force u64)rtime, (__force u64)total);
595+
utime = rtime - stime;
596+
} else {
597+
stime = rtime;
598+
utime = 0;
578599
}
579600

580601
/*
@@ -583,8 +604,9 @@ static void cputime_adjust(struct task_cputime *curr,
583604
* Let's enforce monotonicity.
584605
*/
585606
prev->stime = max(prev->stime, stime);
586-
prev->utime = max(prev->utime, rtime - prev->stime);
607+
prev->utime = max(prev->utime, utime);
587608

609+
out:
588610
*ut = prev->utime;
589611
*st = prev->stime;
590612
}

lib/div64.c

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

8181
/**
82-
* div64_u64_rem - unsigned 64bit divide with 64bit divisor and 64bit remainder
82+
* div64_u64 - unsigned 64bit divide with 64bit divisor
8383
* @dividend: 64bit dividend
8484
* @divisor: 64bit divisor
85-
* @remainder: 64bit remainder
8685
*
8786
* This implementation is a modified version of the algorithm proposed
8887
* by the book 'Hacker's Delight'. The original source and full proof
8988
* can be found here and is available for use without restriction.
9089
*
9190
* 'http://www.hackersdelight.org/HDcode/newCode/divDouble.c.txt'
9291
*/
93-
#ifndef div64_u64_rem
94-
u64 div64_u64_rem(u64 dividend, u64 divisor, u64 *remainder)
92+
#ifndef div64_u64
93+
u64 div64_u64(u64 dividend, u64 divisor)
9594
{
9695
u32 high = divisor >> 32;
9796
u64 quot;
9897

9998
if (high == 0) {
100-
u32 rem32;
101-
quot = div_u64_rem(dividend, divisor, &rem32);
102-
*remainder = rem32;
99+
quot = div_u64(dividend, divisor);
103100
} else {
104101
int n = 1 + fls(high);
105102
quot = div_u64(dividend >> n, divisor >> n);
106103

107104
if (quot != 0)
108105
quot--;
109-
110-
*remainder = dividend - quot * divisor;
111-
if (*remainder >= divisor) {
106+
if ((dividend - quot * divisor) >= divisor)
112107
quot++;
113-
*remainder -= divisor;
114-
}
115108
}
116109

117110
return quot;
118111
}
119-
EXPORT_SYMBOL(div64_u64_rem);
112+
EXPORT_SYMBOL(div64_u64);
120113
#endif
121114

122115
/**

0 commit comments

Comments
 (0)