Re: [tip:sched/urgent] sched: Avoid cputime scaling overflow

From: Peter Zijlstra
Date: Thu May 02 2013 - 09:12:13 EST


> + for (;;) {
> + /* Make sure "rtime" is the bigger of stime/rtime */
> + if (stime > rtime) {
> + u64 tmp = rtime; rtime = stime; stime = tmp;

I keep forgetting to mention we have swap(rtime, stime); that does the above.

> + }
> +
> + /* Make sure 'total' fits in 32 bits */
> + if (total >> 32)
> + goto drop_precision;
> +
> + /* Does rtime (and thus stime) fit in 32 bits? */
> + if (!(rtime >> 32))
> + break;
> +
> + /* Can we just balance rtime/stime rather than dropping bits? */
> + if (stime >> 31)
> + goto drop_precision;
> +
> + /* We can grow stime and shrink rtime and try to make them both fit */
> + stime <<= 1;
> + rtime >>= 1;
> + continue;
> +
> +drop_precision:
> + /* We drop from rtime, it has more bits than stime */
> + rtime >>= 1;
> + total >>= 1;
> }
>
> + /*
> + * Make sure gcc understands that this is a 32x32->64 multiply,
> + * followed by a 64/32->64 divide.
> + */
> + scaled = div_u64((u64) (u32) stime * (u64) (u32) rtime, (u32)total);
> return (__force cputime_t) scaled;
> }
>
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/