xref: /linux-6.15/include/linux/ktime.h (revision d1fd9729)
197fc79f9SThomas Gleixner /*
297fc79f9SThomas Gleixner  *  include/linux/ktime.h
397fc79f9SThomas Gleixner  *
497fc79f9SThomas Gleixner  *  ktime_t - nanosecond-resolution time format.
597fc79f9SThomas Gleixner  *
697fc79f9SThomas Gleixner  *   Copyright(C) 2005, Thomas Gleixner <[email protected]>
797fc79f9SThomas Gleixner  *   Copyright(C) 2005, Red Hat, Inc., Ingo Molnar
897fc79f9SThomas Gleixner  *
997fc79f9SThomas Gleixner  *  data type definitions, declarations, prototypes and macros.
1097fc79f9SThomas Gleixner  *
1197fc79f9SThomas Gleixner  *  Started by: Thomas Gleixner and Ingo Molnar
1297fc79f9SThomas Gleixner  *
1366188faeSThomas Gleixner  *  Credits:
1466188faeSThomas Gleixner  *
1566188faeSThomas Gleixner  *  	Roman Zippel provided the ideas and primary code snippets of
1666188faeSThomas Gleixner  *  	the ktime_t union and further simplifications of the original
1766188faeSThomas Gleixner  *  	code.
1866188faeSThomas Gleixner  *
1997fc79f9SThomas Gleixner  *  For licencing details see kernel-base/COPYING
2097fc79f9SThomas Gleixner  */
2197fc79f9SThomas Gleixner #ifndef _LINUX_KTIME_H
2297fc79f9SThomas Gleixner #define _LINUX_KTIME_H
2397fc79f9SThomas Gleixner 
240cd39f46SPeter Zijlstra #include <asm/bug.h>
252e346b19SKent Overstreet #include <linux/jiffies.h>
262e346b19SKent Overstreet #include <linux/time.h>
272e346b19SKent Overstreet #include <linux/types.h>
2897fc79f9SThomas Gleixner 
2997fc79f9SThomas Gleixner /**
3097fc79f9SThomas Gleixner  * ktime_set - Set a ktime_t variable from a seconds/nanoseconds value
3197fc79f9SThomas Gleixner  * @secs:	seconds to set
3297fc79f9SThomas Gleixner  * @nsecs:	nanoseconds to set
3397fc79f9SThomas Gleixner  *
3436019265SYacine Belkadi  * Return: The ktime_t representation of the value.
3597fc79f9SThomas Gleixner  */
ktime_set(const s64 secs,const unsigned long nsecs)36b17b20d7SJohn Stultz static inline ktime_t ktime_set(const s64 secs, const unsigned long nsecs)
3797fc79f9SThomas Gleixner {
3896dd7421SThomas Gleixner 	if (unlikely(secs >= KTIME_SEC_MAX))
392456e855SThomas Gleixner 		return KTIME_MAX;
40b17b20d7SJohn Stultz 
412456e855SThomas Gleixner 	return secs * NSEC_PER_SEC + (s64)nsecs;
4297fc79f9SThomas Gleixner }
4397fc79f9SThomas Gleixner 
4497fc79f9SThomas Gleixner /* Subtract two ktime_t variables. rem = lhs -rhs: */
452456e855SThomas Gleixner #define ktime_sub(lhs, rhs)	((lhs) - (rhs))
4697fc79f9SThomas Gleixner 
4797fc79f9SThomas Gleixner /* Add two ktime_t variables. res = lhs + rhs: */
482456e855SThomas Gleixner #define ktime_add(lhs, rhs)	((lhs) + (rhs))
4997fc79f9SThomas Gleixner 
5097fc79f9SThomas Gleixner /*
51979515c5SVegard Nossum  * Same as ktime_add(), but avoids undefined behaviour on overflow; however,
52979515c5SVegard Nossum  * this means that you must check the result for overflow yourself.
53979515c5SVegard Nossum  */
542456e855SThomas Gleixner #define ktime_add_unsafe(lhs, rhs)	((u64) (lhs) + (rhs))
55979515c5SVegard Nossum 
56979515c5SVegard Nossum /*
5797fc79f9SThomas Gleixner  * Add a ktime_t variable and a scalar nanosecond value.
5897fc79f9SThomas Gleixner  * res = kt + nsval:
5997fc79f9SThomas Gleixner  */
602456e855SThomas Gleixner #define ktime_add_ns(kt, nsval)		((kt) + (nsval))
6197fc79f9SThomas Gleixner 
62a272378dSArnaldo Carvalho de Melo /*
63a272378dSArnaldo Carvalho de Melo  * Subtract a scalar nanosecod from a ktime_t variable
64a272378dSArnaldo Carvalho de Melo  * res = kt - nsval:
65a272378dSArnaldo Carvalho de Melo  */
662456e855SThomas Gleixner #define ktime_sub_ns(kt, nsval)		((kt) - (nsval))
67a272378dSArnaldo Carvalho de Melo 
6849cd6f86SJohn Stultz /* convert a timespec64 to ktime_t format: */
timespec64_to_ktime(struct timespec64 ts)6949cd6f86SJohn Stultz static inline ktime_t timespec64_to_ktime(struct timespec64 ts)
7049cd6f86SJohn Stultz {
7149cd6f86SJohn Stultz 	return ktime_set(ts.tv_sec, ts.tv_nsec);
7249cd6f86SJohn Stultz }
7349cd6f86SJohn Stultz 
7449cd6f86SJohn Stultz /* Map the ktime_t to timespec conversion to ns_to_timespec function */
752456e855SThomas Gleixner #define ktime_to_timespec64(kt)		ns_to_timespec64((kt))
7649cd6f86SJohn Stultz 
77a8802d97SEric Dumazet /* Convert ktime_t to nanoseconds */
ktime_to_ns(const ktime_t kt)78a8802d97SEric Dumazet static inline s64 ktime_to_ns(const ktime_t kt)
79a8802d97SEric Dumazet {
80a8802d97SEric Dumazet 	return kt;
81a8802d97SEric Dumazet }
8297fc79f9SThomas Gleixner 
83398f382cSDaniel Borkmann /**
84398f382cSDaniel Borkmann  * ktime_compare - Compares two ktime_t variables for less, greater or equal
85398f382cSDaniel Borkmann  * @cmp1:	comparable1
86398f382cSDaniel Borkmann  * @cmp2:	comparable2
87398f382cSDaniel Borkmann  *
8836019265SYacine Belkadi  * Return: ...
89398f382cSDaniel Borkmann  *   cmp1  < cmp2: return <0
90398f382cSDaniel Borkmann  *   cmp1 == cmp2: return 0
91398f382cSDaniel Borkmann  *   cmp1  > cmp2: return >0
92398f382cSDaniel Borkmann  */
ktime_compare(const ktime_t cmp1,const ktime_t cmp2)93398f382cSDaniel Borkmann static inline int ktime_compare(const ktime_t cmp1, const ktime_t cmp2)
94398f382cSDaniel Borkmann {
952456e855SThomas Gleixner 	if (cmp1 < cmp2)
96398f382cSDaniel Borkmann 		return -1;
972456e855SThomas Gleixner 	if (cmp1 > cmp2)
98398f382cSDaniel Borkmann 		return 1;
99398f382cSDaniel Borkmann 	return 0;
100398f382cSDaniel Borkmann }
101398f382cSDaniel Borkmann 
10267cb9366SDaniel Borkmann /**
10367cb9366SDaniel Borkmann  * ktime_after - Compare if a ktime_t value is bigger than another one.
10467cb9366SDaniel Borkmann  * @cmp1:	comparable1
10567cb9366SDaniel Borkmann  * @cmp2:	comparable2
10667cb9366SDaniel Borkmann  *
10767cb9366SDaniel Borkmann  * Return: true if cmp1 happened after cmp2.
10867cb9366SDaniel Borkmann  */
ktime_after(const ktime_t cmp1,const ktime_t cmp2)10967cb9366SDaniel Borkmann static inline bool ktime_after(const ktime_t cmp1, const ktime_t cmp2)
11067cb9366SDaniel Borkmann {
11167cb9366SDaniel Borkmann 	return ktime_compare(cmp1, cmp2) > 0;
11267cb9366SDaniel Borkmann }
11367cb9366SDaniel Borkmann 
11467cb9366SDaniel Borkmann /**
11567cb9366SDaniel Borkmann  * ktime_before - Compare if a ktime_t value is smaller than another one.
11667cb9366SDaniel Borkmann  * @cmp1:	comparable1
11767cb9366SDaniel Borkmann  * @cmp2:	comparable2
11867cb9366SDaniel Borkmann  *
11967cb9366SDaniel Borkmann  * Return: true if cmp1 happened before cmp2.
12067cb9366SDaniel Borkmann  */
ktime_before(const ktime_t cmp1,const ktime_t cmp2)12167cb9366SDaniel Borkmann static inline bool ktime_before(const ktime_t cmp1, const ktime_t cmp2)
12267cb9366SDaniel Borkmann {
12367cb9366SDaniel Borkmann 	return ktime_compare(cmp1, cmp2) < 0;
12467cb9366SDaniel Borkmann }
12567cb9366SDaniel Borkmann 
126166afb64SThomas Gleixner #if BITS_PER_LONG < 64
127f7bcb70eSJohn Stultz extern s64 __ktime_divns(const ktime_t kt, s64 div);
ktime_divns(const ktime_t kt,s64 div)128f7bcb70eSJohn Stultz static inline s64 ktime_divns(const ktime_t kt, s64 div)
1298b618628SNicolas Pitre {
130f7bcb70eSJohn Stultz 	/*
131f7bcb70eSJohn Stultz 	 * Negative divisors could cause an inf loop,
132f7bcb70eSJohn Stultz 	 * so bug out here.
133f7bcb70eSJohn Stultz 	 */
134f7bcb70eSJohn Stultz 	BUG_ON(div < 0);
1358b618628SNicolas Pitre 	if (__builtin_constant_p(div) && !(div >> 32)) {
1362456e855SThomas Gleixner 		s64 ns = kt;
137f7bcb70eSJohn Stultz 		u64 tmp = ns < 0 ? -ns : ns;
138f7bcb70eSJohn Stultz 
139f7bcb70eSJohn Stultz 		do_div(tmp, div);
140f7bcb70eSJohn Stultz 		return ns < 0 ? -tmp : tmp;
1418b618628SNicolas Pitre 	} else {
1428b618628SNicolas Pitre 		return __ktime_divns(kt, div);
1438b618628SNicolas Pitre 	}
1448b618628SNicolas Pitre }
145166afb64SThomas Gleixner #else /* BITS_PER_LONG < 64 */
ktime_divns(const ktime_t kt,s64 div)146f7bcb70eSJohn Stultz static inline s64 ktime_divns(const ktime_t kt, s64 div)
147f7bcb70eSJohn Stultz {
148f7bcb70eSJohn Stultz 	/*
149f7bcb70eSJohn Stultz 	 * 32-bit implementation cannot handle negative divisors,
150f7bcb70eSJohn Stultz 	 * so catch them on 64bit as well.
151f7bcb70eSJohn Stultz 	 */
152f7bcb70eSJohn Stultz 	WARN_ON(div < 0);
1532456e855SThomas Gleixner 	return kt / div;
154f7bcb70eSJohn Stultz }
155166afb64SThomas Gleixner #endif
156166afb64SThomas Gleixner 
ktime_to_us(const ktime_t kt)15784299b3bSYOSHIFUJI Hideaki static inline s64 ktime_to_us(const ktime_t kt)
15884299b3bSYOSHIFUJI Hideaki {
159166afb64SThomas Gleixner 	return ktime_divns(kt, NSEC_PER_USEC);
16084299b3bSYOSHIFUJI Hideaki }
16184299b3bSYOSHIFUJI Hideaki 
ktime_to_ms(const ktime_t kt)162f56916b9SChuck Lever static inline s64 ktime_to_ms(const ktime_t kt)
163f56916b9SChuck Lever {
164166afb64SThomas Gleixner 	return ktime_divns(kt, NSEC_PER_MSEC);
165f56916b9SChuck Lever }
166f56916b9SChuck Lever 
ktime_us_delta(const ktime_t later,const ktime_t earlier)167f1c91da4SGerrit Renker static inline s64 ktime_us_delta(const ktime_t later, const ktime_t earlier)
168f1c91da4SGerrit Renker {
169f1c91da4SGerrit Renker        return ktime_to_us(ktime_sub(later, earlier));
170f1c91da4SGerrit Renker }
171f1c91da4SGerrit Renker 
ktime_ms_delta(const ktime_t later,const ktime_t earlier)17241fbf3b3SChunyan Zhang static inline s64 ktime_ms_delta(const ktime_t later, const ktime_t earlier)
17341fbf3b3SChunyan Zhang {
17441fbf3b3SChunyan Zhang 	return ktime_to_ms(ktime_sub(later, earlier));
17541fbf3b3SChunyan Zhang }
17641fbf3b3SChunyan Zhang 
ktime_add_us(const ktime_t kt,const u64 usec)1771e180f72SArnaldo Carvalho de Melo static inline ktime_t ktime_add_us(const ktime_t kt, const u64 usec)
1781e180f72SArnaldo Carvalho de Melo {
179a44b8bd6SLiu Ying 	return ktime_add_ns(kt, usec * NSEC_PER_USEC);
1801e180f72SArnaldo Carvalho de Melo }
1811e180f72SArnaldo Carvalho de Melo 
ktime_add_ms(const ktime_t kt,const u64 msec)182d36f82b2SDaniel Borkmann static inline ktime_t ktime_add_ms(const ktime_t kt, const u64 msec)
183d36f82b2SDaniel Borkmann {
184d36f82b2SDaniel Borkmann 	return ktime_add_ns(kt, msec * NSEC_PER_MSEC);
185d36f82b2SDaniel Borkmann }
186d36f82b2SDaniel Borkmann 
ktime_sub_us(const ktime_t kt,const u64 usec)187a272378dSArnaldo Carvalho de Melo static inline ktime_t ktime_sub_us(const ktime_t kt, const u64 usec)
188a272378dSArnaldo Carvalho de Melo {
189a44b8bd6SLiu Ying 	return ktime_sub_ns(kt, usec * NSEC_PER_USEC);
190a272378dSArnaldo Carvalho de Melo }
191a272378dSArnaldo Carvalho de Melo 
ktime_sub_ms(const ktime_t kt,const u64 msec)19277f2efcbSDavid Howells static inline ktime_t ktime_sub_ms(const ktime_t kt, const u64 msec)
19377f2efcbSDavid Howells {
19477f2efcbSDavid Howells 	return ktime_sub_ns(kt, msec * NSEC_PER_MSEC);
19577f2efcbSDavid Howells }
19677f2efcbSDavid Howells 
1975a7780e7SThomas Gleixner extern ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs);
1985a7780e7SThomas Gleixner 
1996e94d1efSDaniel Borkmann /**
20049cd6f86SJohn Stultz  * ktime_to_timespec64_cond - convert a ktime_t variable to timespec64
20149cd6f86SJohn Stultz  *			    format only if the variable contains data
20249cd6f86SJohn Stultz  * @kt:		the ktime_t variable to convert
20349cd6f86SJohn Stultz  * @ts:		the timespec variable to store the result in
20449cd6f86SJohn Stultz  *
20549cd6f86SJohn Stultz  * Return: %true if there was a successful conversion, %false if kt was 0.
20649cd6f86SJohn Stultz  */
ktime_to_timespec64_cond(const ktime_t kt,struct timespec64 * ts)20749cd6f86SJohn Stultz static inline __must_check bool ktime_to_timespec64_cond(const ktime_t kt,
20849cd6f86SJohn Stultz 						       struct timespec64 *ts)
20949cd6f86SJohn Stultz {
2102456e855SThomas Gleixner 	if (kt) {
21149cd6f86SJohn Stultz 		*ts = ktime_to_timespec64(kt);
21249cd6f86SJohn Stultz 		return true;
21349cd6f86SJohn Stultz 	} else {
21449cd6f86SJohn Stultz 		return false;
21549cd6f86SJohn Stultz 	}
21649cd6f86SJohn Stultz }
21749cd6f86SJohn Stultz 
218cc56f32fSVincenzo Frascino #include <vdso/ktime.h>
219c0a31329SThomas Gleixner 
ns_to_ktime(u64 ns)22057d3da29SIngo Molnar static inline ktime_t ns_to_ktime(u64 ns)
22157d3da29SIngo Molnar {
2222456e855SThomas Gleixner 	return ns;
22357d3da29SIngo Molnar }
22457d3da29SIngo Molnar 
us_to_ktime(u64 us)225*d1fd9729SDavid Howells static inline ktime_t us_to_ktime(u64 us)
226*d1fd9729SDavid Howells {
227*d1fd9729SDavid Howells 	return us * NSEC_PER_USEC;
228*d1fd9729SDavid Howells }
229*d1fd9729SDavid Howells 
ms_to_ktime(u64 ms)230d36f82b2SDaniel Borkmann static inline ktime_t ms_to_ktime(u64 ms)
231d36f82b2SDaniel Borkmann {
2322456e855SThomas Gleixner 	return ms * NSEC_PER_MSEC;
233d36f82b2SDaniel Borkmann }
234d36f82b2SDaniel Borkmann 
2358b094cd0SThomas Gleixner # include <linux/timekeeping.h>
2368b094cd0SThomas Gleixner 
23797fc79f9SThomas Gleixner #endif
238