2016-12-08 20:53:23

by Thomas Gleixner

[permalink] [raw]
Subject: [patch 4/6] timekeeping: Use mul_u64_u32_shr() instead of open coding it

The resume code must deal with a clocksource delta which is potentially big
enough to overflow the 64bit mult.

Replace the open coded handling with the proper function.

Signed-off-by: Thomas Gleixner <[email protected]>
---
kernel/time/timekeeping.c | 26 +++++---------------------
1 file changed, 5 insertions(+), 21 deletions(-)

--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -1615,7 +1615,7 @@ void timekeeping_resume(void)
struct clocksource *clock = tk->tkr_mono.clock;
unsigned long flags;
struct timespec64 ts_new, ts_delta;
- cycle_t cycle_now, cycle_delta;
+ cycle_t cycle_now;

sleeptime_injected = false;
read_persistent_clock64(&ts_new);
@@ -1641,27 +1641,11 @@ void timekeeping_resume(void)
cycle_now = tk->tkr_mono.read(clock);
if ((clock->flags & CLOCK_SOURCE_SUSPEND_NONSTOP) &&
cycle_now > tk->tkr_mono.cycle_last) {
- u64 num, max = ULLONG_MAX;
- u32 mult = clock->mult;
- u32 shift = clock->shift;
- s64 nsec = 0;
-
- cycle_delta = clocksource_delta(cycle_now, tk->tkr_mono.cycle_last,
- tk->tkr_mono.mask);
-
- /*
- * "cycle_delta * mutl" may cause 64 bits overflow, if the
- * suspended time is too long. In that case we need do the
- * 64 bits math carefully
- */
- do_div(max, mult);
- if (cycle_delta > max) {
- num = div64_u64(cycle_delta, max);
- nsec = (((u64) max * mult) >> shift) * num;
- cycle_delta -= num * max;
- }
- nsec += ((u64) cycle_delta * mult) >> shift;
+ u64 nsec, cyc_delta;

+ cyc_delta = clocksource_delta(cycle_now, tk->tkr_mono.cycle_last,
+ tk->tkr_mono.mask);
+ nsec = mul_u64_u32_shr(cyc_delta, clock->mult, clock->shift);
ts_delta = ns_to_timespec64(nsec);
sleeptime_injected = true;
} else if (timespec64_compare(&ts_new, &timekeeping_suspend_time) > 0) {



2016-12-08 23:47:45

by David Gibson

[permalink] [raw]
Subject: Re: [patch 4/6] timekeeping: Use mul_u64_u32_shr() instead of open coding it

On Thu, Dec 08, 2016 at 08:49:38PM -0000, Thomas Gleixner wrote:
> The resume code must deal with a clocksource delta which is potentially big
> enough to overflow the 64bit mult.
>
> Replace the open coded handling with the proper function.
>
> Signed-off-by: Thomas Gleixner <[email protected]>

Reviewed-by: David Gibson <[email protected]>

> ---
> kernel/time/timekeeping.c | 26 +++++---------------------
> 1 file changed, 5 insertions(+), 21 deletions(-)
>
> --- a/kernel/time/timekeeping.c
> +++ b/kernel/time/timekeeping.c
> @@ -1615,7 +1615,7 @@ void timekeeping_resume(void)
> struct clocksource *clock = tk->tkr_mono.clock;
> unsigned long flags;
> struct timespec64 ts_new, ts_delta;
> - cycle_t cycle_now, cycle_delta;
> + cycle_t cycle_now;
>
> sleeptime_injected = false;
> read_persistent_clock64(&ts_new);
> @@ -1641,27 +1641,11 @@ void timekeeping_resume(void)
> cycle_now = tk->tkr_mono.read(clock);
> if ((clock->flags & CLOCK_SOURCE_SUSPEND_NONSTOP) &&
> cycle_now > tk->tkr_mono.cycle_last) {
> - u64 num, max = ULLONG_MAX;
> - u32 mult = clock->mult;
> - u32 shift = clock->shift;
> - s64 nsec = 0;
> -
> - cycle_delta = clocksource_delta(cycle_now, tk->tkr_mono.cycle_last,
> - tk->tkr_mono.mask);
> -
> - /*
> - * "cycle_delta * mutl" may cause 64 bits overflow, if the
> - * suspended time is too long. In that case we need do the
> - * 64 bits math carefully
> - */
> - do_div(max, mult);
> - if (cycle_delta > max) {
> - num = div64_u64(cycle_delta, max);
> - nsec = (((u64) max * mult) >> shift) * num;
> - cycle_delta -= num * max;
> - }
> - nsec += ((u64) cycle_delta * mult) >> shift;
> + u64 nsec, cyc_delta;
>
> + cyc_delta = clocksource_delta(cycle_now, tk->tkr_mono.cycle_last,
> + tk->tkr_mono.mask);
> + nsec = mul_u64_u32_shr(cyc_delta, clock->mult, clock->shift);
> ts_delta = ns_to_timespec64(nsec);
> sleeptime_injected = true;
> } else if (timespec64_compare(&ts_new, &timekeeping_suspend_time) > 0) {
>
>

--
David Gibson | I'll have my music baroque, and my code
david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_
| _way_ _around_!
http://www.ozlabs.org/~dgibson


Attachments:
(No filename) (2.18 kB)
signature.asc (819.00 B)
Download all attachments
Subject: [tip:timers/core] timekeeping: Use mul_u64_u32_shr() instead of open coding it

Commit-ID: c029a2bec66e42e57538cb65e28618baf6a4b311
Gitweb: http://git.kernel.org/tip/c029a2bec66e42e57538cb65e28618baf6a4b311
Author: Thomas Gleixner <[email protected]>
AuthorDate: Thu, 8 Dec 2016 20:49:38 +0000
Committer: Thomas Gleixner <[email protected]>
CommitDate: Fri, 9 Dec 2016 12:06:42 +0100

timekeeping: Use mul_u64_u32_shr() instead of open coding it

The resume code must deal with a clocksource delta which is potentially big
enough to overflow the 64bit mult.

Replace the open coded handling with the proper function.

Signed-off-by: Thomas Gleixner <[email protected]>
Reviewed-by: David Gibson <[email protected]>
Acked-by: Peter Zijlstra (Intel) <[email protected]>
Cc: Parit Bhargava <[email protected]>
Cc: Laurent Vivier <[email protected]>
Cc: "Christopher S. Hall" <[email protected]>
Cc: Chris Metcalf <[email protected]>
Cc: Richard Cochran <[email protected]>
Cc: Liav Rehana <[email protected]>
Cc: John Stultz <[email protected]>
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Thomas Gleixner <[email protected]>

---
kernel/time/timekeeping.c | 26 +++++---------------------
1 file changed, 5 insertions(+), 21 deletions(-)

diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index 82e1b5c..da233cd 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -1644,7 +1644,7 @@ void timekeeping_resume(void)
struct clocksource *clock = tk->tkr_mono.clock;
unsigned long flags;
struct timespec64 ts_new, ts_delta;
- cycle_t cycle_now, cycle_delta;
+ cycle_t cycle_now;

sleeptime_injected = false;
read_persistent_clock64(&ts_new);
@@ -1670,27 +1670,11 @@ void timekeeping_resume(void)
cycle_now = tk->tkr_mono.read(clock);
if ((clock->flags & CLOCK_SOURCE_SUSPEND_NONSTOP) &&
cycle_now > tk->tkr_mono.cycle_last) {
- u64 num, max = ULLONG_MAX;
- u32 mult = clock->mult;
- u32 shift = clock->shift;
- s64 nsec = 0;
-
- cycle_delta = clocksource_delta(cycle_now, tk->tkr_mono.cycle_last,
- tk->tkr_mono.mask);
-
- /*
- * "cycle_delta * mutl" may cause 64 bits overflow, if the
- * suspended time is too long. In that case we need do the
- * 64 bits math carefully
- */
- do_div(max, mult);
- if (cycle_delta > max) {
- num = div64_u64(cycle_delta, max);
- nsec = (((u64) max * mult) >> shift) * num;
- cycle_delta -= num * max;
- }
- nsec += ((u64) cycle_delta * mult) >> shift;
+ u64 nsec, cyc_delta;

+ cyc_delta = clocksource_delta(cycle_now, tk->tkr_mono.cycle_last,
+ tk->tkr_mono.mask);
+ nsec = mul_u64_u32_shr(cyc_delta, clock->mult, clock->shift);
ts_delta = ns_to_timespec64(nsec);
sleeptime_injected = true;
} else if (timespec64_compare(&ts_new, &timekeeping_suspend_time) > 0) {