Skip to content

Commit 87d8b9e

Browse files
bebarinojohnstultz-work
authored andcommitted
clocksource: Extract max nsec calculation into separate function
We need to calculate the same number in the clocksource code and the sched_clock code, so extract this code into its own function. We also drop the min_t and just use min() because the two types are the same. Signed-off-by: Stephen Boyd <sboyd@codeaurora.org> Signed-off-by: John Stultz <john.stultz@linaro.org>
1 parent ad81f05 commit 87d8b9e

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

include/linux/clocksource.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,8 @@ extern void clocksource_resume(void);
292292
extern struct clocksource * __init __weak clocksource_default_clock(void);
293293
extern void clocksource_mark_unstable(struct clocksource *cs);
294294

295+
extern u64
296+
clocks_calc_max_nsecs(u32 mult, u32 shift, u32 maxadj, u64 mask);
295297
extern void
296298
clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 minsec);
297299

kernel/time/clocksource.c

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -537,40 +537,55 @@ static u32 clocksource_max_adjustment(struct clocksource *cs)
537537
}
538538

539539
/**
540-
* clocksource_max_deferment - Returns max time the clocksource can be deferred
541-
* @cs: Pointer to clocksource
542-
*
540+
* clocks_calc_max_nsecs - Returns maximum nanoseconds that can be converted
541+
* @mult: cycle to nanosecond multiplier
542+
* @shift: cycle to nanosecond divisor (power of two)
543+
* @maxadj: maximum adjustment value to mult (~11%)
544+
* @mask: bitmask for two's complement subtraction of non 64 bit counters
543545
*/
544-
static u64 clocksource_max_deferment(struct clocksource *cs)
546+
u64 clocks_calc_max_nsecs(u32 mult, u32 shift, u32 maxadj, u64 mask)
545547
{
546548
u64 max_nsecs, max_cycles;
547549

548550
/*
549551
* Calculate the maximum number of cycles that we can pass to the
550552
* cyc2ns function without overflowing a 64-bit signed result. The
551-
* maximum number of cycles is equal to ULLONG_MAX/(cs->mult+cs->maxadj)
553+
* maximum number of cycles is equal to ULLONG_MAX/(mult+maxadj)
552554
* which is equivalent to the below.
553-
* max_cycles < (2^63)/(cs->mult + cs->maxadj)
554-
* max_cycles < 2^(log2((2^63)/(cs->mult + cs->maxadj)))
555-
* max_cycles < 2^(log2(2^63) - log2(cs->mult + cs->maxadj))
556-
* max_cycles < 2^(63 - log2(cs->mult + cs->maxadj))
557-
* max_cycles < 1 << (63 - log2(cs->mult + cs->maxadj))
555+
* max_cycles < (2^63)/(mult + maxadj)
556+
* max_cycles < 2^(log2((2^63)/(mult + maxadj)))
557+
* max_cycles < 2^(log2(2^63) - log2(mult + maxadj))
558+
* max_cycles < 2^(63 - log2(mult + maxadj))
559+
* max_cycles < 1 << (63 - log2(mult + maxadj))
558560
* Please note that we add 1 to the result of the log2 to account for
559561
* any rounding errors, ensure the above inequality is satisfied and
560562
* no overflow will occur.
561563
*/
562-
max_cycles = 1ULL << (63 - (ilog2(cs->mult + cs->maxadj) + 1));
564+
max_cycles = 1ULL << (63 - (ilog2(mult + maxadj) + 1));
563565

564566
/*
565567
* The actual maximum number of cycles we can defer the clocksource is
566-
* determined by the minimum of max_cycles and cs->mask.
568+
* determined by the minimum of max_cycles and mask.
567569
* Note: Here we subtract the maxadj to make sure we don't sleep for
568570
* too long if there's a large negative adjustment.
569571
*/
570-
max_cycles = min_t(u64, max_cycles, (u64) cs->mask);
571-
max_nsecs = clocksource_cyc2ns(max_cycles, cs->mult - cs->maxadj,
572-
cs->shift);
572+
max_cycles = min(max_cycles, mask);
573+
max_nsecs = clocksource_cyc2ns(max_cycles, mult - maxadj, shift);
574+
575+
return max_nsecs;
576+
}
577+
578+
/**
579+
* clocksource_max_deferment - Returns max time the clocksource can be deferred
580+
* @cs: Pointer to clocksource
581+
*
582+
*/
583+
static u64 clocksource_max_deferment(struct clocksource *cs)
584+
{
585+
u64 max_nsecs;
573586

587+
max_nsecs = clocks_calc_max_nsecs(cs->mult, cs->shift, cs->maxadj,
588+
cs->mask);
574589
/*
575590
* To ensure that the clocksource does not wrap whilst we are idle,
576591
* limit the time the clocksource can be deferred by 12.5%. Please

0 commit comments

Comments
 (0)