Skip to content

Commit 166afb6

Browse files
KAGA-KOKOjohnstultz-work
authored andcommitted
ktime: Sanitize ktime_to_us/ms conversion
With the plain nanoseconds based ktime_t we can simply use ktime_divns() instead of going through loops and hoops of timespec/timeval conversion. Reported-by: John Stultz <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Signed-off-by: John Stultz <[email protected]>
1 parent 24e4a8c commit 166afb6

File tree

3 files changed

+9
-10
lines changed

3 files changed

+9
-10
lines changed

include/linux/hrtimer.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -457,12 +457,6 @@ extern void hrtimer_run_pending(void);
457457
/* Bootup initialization: */
458458
extern void __init hrtimers_init(void);
459459

460-
#if BITS_PER_LONG < 64
461-
extern u64 ktime_divns(const ktime_t kt, s64 div);
462-
#else /* BITS_PER_LONG < 64 */
463-
# define ktime_divns(kt, div) (u64)((kt).tv64 / (div))
464-
#endif
465-
466460
/* Show pending timers: */
467461
extern void sysrq_timer_list_show(void);
468462

include/linux/ktime.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,16 +157,20 @@ static inline bool ktime_before(const ktime_t cmp1, const ktime_t cmp2)
157157
return ktime_compare(cmp1, cmp2) < 0;
158158
}
159159

160+
#if BITS_PER_LONG < 64
161+
extern u64 ktime_divns(const ktime_t kt, s64 div);
162+
#else /* BITS_PER_LONG < 64 */
163+
# define ktime_divns(kt, div) (u64)((kt).tv64 / (div))
164+
#endif
165+
160166
static inline s64 ktime_to_us(const ktime_t kt)
161167
{
162-
struct timeval tv = ktime_to_timeval(kt);
163-
return (s64) tv.tv_sec * USEC_PER_SEC + tv.tv_usec;
168+
return ktime_divns(kt, NSEC_PER_USEC);
164169
}
165170

166171
static inline s64 ktime_to_ms(const ktime_t kt)
167172
{
168-
struct timeval tv = ktime_to_timeval(kt);
169-
return (s64) tv.tv_sec * MSEC_PER_SEC + tv.tv_usec / USEC_PER_MSEC;
173+
return ktime_divns(kt, NSEC_PER_MSEC);
170174
}
171175

172176
static inline s64 ktime_us_delta(const ktime_t later, const ktime_t earlier)

kernel/time/hrtimer.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ u64 ktime_divns(const ktime_t kt, s64 div)
280280

281281
return dclc;
282282
}
283+
EXPORT_SYMBOL_GPL(ktime_divns);
283284
#endif /* BITS_PER_LONG >= 64 */
284285

285286
/*

0 commit comments

Comments
 (0)