Skip to content

Commit b9ff604

Browse files
arndbKAGA-KOKO
authored andcommitted
timekeeping: Add ktime_get_coarse_with_offset
I have run into a couple of drivers using current_kernel_time() suffering from the y2038 problem, and they could be converted to using ktime_t, but don't have interfaces that skip the nanosecond calculation at the moment. This introduces ktime_get_coarse_with_offset() as a simpler variant of ktime_get_with_offset(), and adds wrappers for the three time domains we support with the existing function. Signed-off-by: Arnd Bergmann <arnd@arndb.de> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Cc: Stephen Boyd <sboyd@kernel.org> Cc: y2038@lists.linaro.org Cc: John Stultz <john.stultz@linaro.org> Link: https://lkml.kernel.org/r/20180427134016.2525989-5-arnd@arndb.de
1 parent fb7fcc9 commit b9ff604

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

include/linux/timekeeping.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ enum tk_offsets {
5151

5252
extern ktime_t ktime_get(void);
5353
extern ktime_t ktime_get_with_offset(enum tk_offsets offs);
54+
extern ktime_t ktime_get_coarse_with_offset(enum tk_offsets offs);
5455
extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs);
5556
extern ktime_t ktime_get_raw(void);
5657
extern u32 ktime_get_resolution_ns(void);
@@ -63,6 +64,11 @@ static inline ktime_t ktime_get_real(void)
6364
return ktime_get_with_offset(TK_OFFS_REAL);
6465
}
6566

67+
static inline ktime_t ktime_get_coarse_real(void)
68+
{
69+
return ktime_get_coarse_with_offset(TK_OFFS_REAL);
70+
}
71+
6672
/**
6773
* ktime_get_boottime - Returns monotonic time since boot in ktime_t format
6874
*
@@ -74,6 +80,11 @@ static inline ktime_t ktime_get_boottime(void)
7480
return ktime_get_with_offset(TK_OFFS_BOOT);
7581
}
7682

83+
static inline ktime_t ktime_get_coarse_boottime(void)
84+
{
85+
return ktime_get_coarse_with_offset(TK_OFFS_BOOT);
86+
}
87+
7788
/**
7889
* ktime_get_clocktai - Returns the TAI time of day in ktime_t format
7990
*/
@@ -82,6 +93,11 @@ static inline ktime_t ktime_get_clocktai(void)
8293
return ktime_get_with_offset(TK_OFFS_TAI);
8394
}
8495

96+
static inline ktime_t ktime_get_coarse_clocktai(void)
97+
{
98+
return ktime_get_coarse_with_offset(TK_OFFS_TAI);
99+
}
100+
85101
/**
86102
* ktime_mono_to_real - Convert monotonic time to clock realtime
87103
*/

kernel/time/timekeeping.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,25 @@ ktime_t ktime_get_with_offset(enum tk_offsets offs)
795795
}
796796
EXPORT_SYMBOL_GPL(ktime_get_with_offset);
797797

798+
ktime_t ktime_get_coarse_with_offset(enum tk_offsets offs)
799+
{
800+
struct timekeeper *tk = &tk_core.timekeeper;
801+
unsigned int seq;
802+
ktime_t base, *offset = offsets[offs];
803+
804+
WARN_ON(timekeeping_suspended);
805+
806+
do {
807+
seq = read_seqcount_begin(&tk_core.seq);
808+
base = ktime_add(tk->tkr_mono.base, *offset);
809+
810+
} while (read_seqcount_retry(&tk_core.seq, seq));
811+
812+
return base;
813+
814+
}
815+
EXPORT_SYMBOL_GPL(ktime_get_coarse_with_offset);
816+
798817
/**
799818
* ktime_mono_to_any() - convert mononotic time to any other time
800819
* @tmono: time to convert.

0 commit comments

Comments
 (0)