Skip to content

Commit ca42aaf

Browse files
Nicholas Mc GuireKAGA-KOKO
authored andcommitted
time: Refactor msecs_to_jiffies
Refactor the msecs_to_jiffies conditional code part in time.c and jiffies.h putting it into conditional functions rather than #ifdefs to improve readability. [ tglx: Verified that there is no binary code change ] Signed-off-by: Nicholas Mc Guire <[email protected]> Cc: Masahiro Yamada <[email protected]> Cc: Sam Ravnborg <[email protected]> Cc: Joe Perches <[email protected]> Cc: John Stultz <[email protected]> Cc: Andrew Hunter <[email protected]> Cc: Paul Turner <[email protected]> Cc: Michal Marek <[email protected]> Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Thomas Gleixner <[email protected]>
1 parent 0a22798 commit ca42aaf

File tree

2 files changed

+82
-41
lines changed

2 files changed

+82
-41
lines changed

include/linux/jiffies.h

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <linux/time.h>
88
#include <linux/timex.h>
99
#include <asm/param.h> /* for HZ */
10+
#include <generated/timeconst.h>
1011

1112
/*
1213
* The following defines establish the engineering parameters of the PLL
@@ -288,7 +289,68 @@ static inline u64 jiffies_to_nsecs(const unsigned long j)
288289
return (u64)jiffies_to_usecs(j) * NSEC_PER_USEC;
289290
}
290291

291-
extern unsigned long msecs_to_jiffies(const unsigned int m);
292+
extern unsigned long __msecs_to_jiffies(const unsigned int m);
293+
#if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ)
294+
/*
295+
* HZ is equal to or smaller than 1000, and 1000 is a nice round
296+
* multiple of HZ, divide with the factor between them, but round
297+
* upwards:
298+
*/
299+
static inline unsigned long _msecs_to_jiffies(const unsigned int m)
300+
{
301+
return (m + (MSEC_PER_SEC / HZ) - 1) / (MSEC_PER_SEC / HZ);
302+
}
303+
#elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC)
304+
/*
305+
* HZ is larger than 1000, and HZ is a nice round multiple of 1000 -
306+
* simply multiply with the factor between them.
307+
*
308+
* But first make sure the multiplication result cannot overflow:
309+
*/
310+
static inline unsigned long _msecs_to_jiffies(const unsigned int m)
311+
{
312+
if (m > jiffies_to_msecs(MAX_JIFFY_OFFSET))
313+
return MAX_JIFFY_OFFSET;
314+
return m * (HZ / MSEC_PER_SEC);
315+
}
316+
#else
317+
/*
318+
* Generic case - multiply, round and divide. But first check that if
319+
* we are doing a net multiplication, that we wouldn't overflow:
320+
*/
321+
static inline unsigned long _msecs_to_jiffies(const unsigned int m)
322+
{
323+
if (HZ > MSEC_PER_SEC && m > jiffies_to_msecs(MAX_JIFFY_OFFSET))
324+
return MAX_JIFFY_OFFSET;
325+
326+
return (MSEC_TO_HZ_MUL32 * m + MSEC_TO_HZ_ADJ32)
327+
>> MSEC_TO_HZ_SHR32;
328+
}
329+
#endif
330+
/**
331+
* msecs_to_jiffies: - convert milliseconds to jiffies
332+
* @m: time in milliseconds
333+
*
334+
* conversion is done as follows:
335+
*
336+
* - negative values mean 'infinite timeout' (MAX_JIFFY_OFFSET)
337+
*
338+
* - 'too large' values [that would result in larger than
339+
* MAX_JIFFY_OFFSET values] mean 'infinite timeout' too.
340+
*
341+
* - all other values are converted to jiffies by either multiplying
342+
* the input value by a factor or dividing it with a factor and
343+
* handling any 32-bit overflows.
344+
* for the details see __msecs_to_jiffies()
345+
*
346+
* the HZ range specific helpers _msecs_to_jiffies() are called from
347+
* __msecs_to_jiffies().
348+
*/
349+
static inline unsigned long msecs_to_jiffies(const unsigned int m)
350+
{
351+
return __msecs_to_jiffies(m);
352+
}
353+
292354
extern unsigned long usecs_to_jiffies(const unsigned int u);
293355
extern unsigned long timespec_to_jiffies(const struct timespec *value);
294356
extern void jiffies_to_timespec(const unsigned long jiffies,

kernel/time/time.c

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -483,61 +483,40 @@ struct timespec64 ns_to_timespec64(const s64 nsec)
483483
}
484484
EXPORT_SYMBOL(ns_to_timespec64);
485485
#endif
486-
/*
487-
* When we convert to jiffies then we interpret incoming values
488-
* the following way:
486+
/**
487+
* msecs_to_jiffies: - convert milliseconds to jiffies
488+
* @m: time in milliseconds
489+
*
490+
* conversion is done as follows:
489491
*
490492
* - negative values mean 'infinite timeout' (MAX_JIFFY_OFFSET)
491493
*
492494
* - 'too large' values [that would result in larger than
493495
* MAX_JIFFY_OFFSET values] mean 'infinite timeout' too.
494496
*
495497
* - all other values are converted to jiffies by either multiplying
496-
* the input value by a factor or dividing it with a factor
497-
*
498-
* We must also be careful about 32-bit overflows.
498+
* the input value by a factor or dividing it with a factor and
499+
* handling any 32-bit overflows.
500+
* for the details see __msecs_to_jiffies()
501+
*
502+
* msecs_to_jiffies() checks for the passed in value being a constant
503+
* via __builtin_constant_p() allowing gcc to eliminate most of the
504+
* code, __msecs_to_jiffies() is called if the value passed does not
505+
* allow constant folding and the actual conversion must be done at
506+
* runtime.
507+
* the _msecs_to_jiffies helpers are the HZ dependent conversion
508+
* routines found in include/linux/jiffies.h
499509
*/
500-
unsigned long msecs_to_jiffies(const unsigned int m)
510+
unsigned long __msecs_to_jiffies(const unsigned int m)
501511
{
502512
/*
503513
* Negative value, means infinite timeout:
504514
*/
505515
if ((int)m < 0)
506516
return MAX_JIFFY_OFFSET;
507-
508-
#if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ)
509-
/*
510-
* HZ is equal to or smaller than 1000, and 1000 is a nice
511-
* round multiple of HZ, divide with the factor between them,
512-
* but round upwards:
513-
*/
514-
return (m + (MSEC_PER_SEC / HZ) - 1) / (MSEC_PER_SEC / HZ);
515-
#elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC)
516-
/*
517-
* HZ is larger than 1000, and HZ is a nice round multiple of
518-
* 1000 - simply multiply with the factor between them.
519-
*
520-
* But first make sure the multiplication result cannot
521-
* overflow:
522-
*/
523-
if (m > jiffies_to_msecs(MAX_JIFFY_OFFSET))
524-
return MAX_JIFFY_OFFSET;
525-
526-
return m * (HZ / MSEC_PER_SEC);
527-
#else
528-
/*
529-
* Generic case - multiply, round and divide. But first
530-
* check that if we are doing a net multiplication, that
531-
* we wouldn't overflow:
532-
*/
533-
if (HZ > MSEC_PER_SEC && m > jiffies_to_msecs(MAX_JIFFY_OFFSET))
534-
return MAX_JIFFY_OFFSET;
535-
536-
return (MSEC_TO_HZ_MUL32 * m + MSEC_TO_HZ_ADJ32)
537-
>> MSEC_TO_HZ_SHR32;
538-
#endif
517+
return _msecs_to_jiffies(m);
539518
}
540-
EXPORT_SYMBOL(msecs_to_jiffies);
519+
EXPORT_SYMBOL(__msecs_to_jiffies);
541520

542521
unsigned long usecs_to_jiffies(const unsigned int u)
543522
{

0 commit comments

Comments
 (0)