Skip to content

Commit 9ca3085

Browse files
wangbaolin719johnstultz-work
authored andcommitted
time: Introduce timespec64_to_jiffies()/jiffies_to_timespec64()
The conversion between struct timespec and jiffies is not year 2038 safe on 32bit systems. Introduce timespec64_to_jiffies() and jiffies_to_timespec64() functions which use struct timespec64 to make it ready for 2038 issue. Cc: Prarit Bhargava <[email protected]> Cc: Richard Cochran <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Thomas Gleixner <[email protected]> Signed-off-by: Baolin Wang <[email protected]> Signed-off-by: John Stultz <[email protected]>
1 parent 8758a24 commit 9ca3085

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

include/linux/jiffies.h

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -416,9 +416,25 @@ static inline unsigned long usecs_to_jiffies(const unsigned int u)
416416
}
417417
}
418418

419-
extern unsigned long timespec_to_jiffies(const struct timespec *value);
420-
extern void jiffies_to_timespec(const unsigned long jiffies,
421-
struct timespec *value);
419+
extern unsigned long timespec64_to_jiffies(const struct timespec64 *value);
420+
extern void jiffies_to_timespec64(const unsigned long jiffies,
421+
struct timespec64 *value);
422+
static inline unsigned long timespec_to_jiffies(const struct timespec *value)
423+
{
424+
struct timespec64 ts = timespec_to_timespec64(*value);
425+
426+
return timespec64_to_jiffies(&ts);
427+
}
428+
429+
static inline void jiffies_to_timespec(const unsigned long jiffies,
430+
struct timespec *value)
431+
{
432+
struct timespec64 ts;
433+
434+
jiffies_to_timespec64(jiffies, &ts);
435+
*value = timespec64_to_timespec(ts);
436+
}
437+
422438
extern unsigned long timeval_to_jiffies(const struct timeval *value);
423439
extern void jiffies_to_timeval(const unsigned long jiffies,
424440
struct timeval *value);

kernel/time/time.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -540,30 +540,35 @@ EXPORT_SYMBOL(__usecs_to_jiffies);
540540
* value to a scaled second value.
541541
*/
542542
static unsigned long
543-
__timespec_to_jiffies(unsigned long sec, long nsec)
543+
__timespec64_to_jiffies(u64 sec, long nsec)
544544
{
545545
nsec = nsec + TICK_NSEC - 1;
546546

547547
if (sec >= MAX_SEC_IN_JIFFIES){
548548
sec = MAX_SEC_IN_JIFFIES;
549549
nsec = 0;
550550
}
551-
return (((u64)sec * SEC_CONVERSION) +
551+
return ((sec * SEC_CONVERSION) +
552552
(((u64)nsec * NSEC_CONVERSION) >>
553553
(NSEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC;
554554

555555
}
556556

557-
unsigned long
558-
timespec_to_jiffies(const struct timespec *value)
557+
static unsigned long
558+
__timespec_to_jiffies(unsigned long sec, long nsec)
559559
{
560-
return __timespec_to_jiffies(value->tv_sec, value->tv_nsec);
560+
return __timespec64_to_jiffies((u64)sec, nsec);
561561
}
562562

563-
EXPORT_SYMBOL(timespec_to_jiffies);
563+
unsigned long
564+
timespec64_to_jiffies(const struct timespec64 *value)
565+
{
566+
return __timespec64_to_jiffies(value->tv_sec, value->tv_nsec);
567+
}
568+
EXPORT_SYMBOL(timespec64_to_jiffies);
564569

565570
void
566-
jiffies_to_timespec(const unsigned long jiffies, struct timespec *value)
571+
jiffies_to_timespec64(const unsigned long jiffies, struct timespec64 *value)
567572
{
568573
/*
569574
* Convert jiffies to nanoseconds and separate with
@@ -574,7 +579,7 @@ jiffies_to_timespec(const unsigned long jiffies, struct timespec *value)
574579
NSEC_PER_SEC, &rem);
575580
value->tv_nsec = rem;
576581
}
577-
EXPORT_SYMBOL(jiffies_to_timespec);
582+
EXPORT_SYMBOL(jiffies_to_timespec64);
578583

579584
/*
580585
* We could use a similar algorithm to timespec_to_jiffies (with a

0 commit comments

Comments
 (0)