Rev 6082 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 6082 | Rev 6934 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * linux/kernel/time.c |
2 | * linux/kernel/time.c |
3 | * |
3 | * |
4 | * Copyright (C) 1991, 1992 Linus Torvalds |
4 | * Copyright (C) 1991, 1992 Linus Torvalds |
5 | * |
5 | * |
6 | * This file contains the interface functions for the various |
6 | * This file contains the interface functions for the various |
7 | * time related system calls: time, stime, gettimeofday, settimeofday, |
7 | * time related system calls: time, stime, gettimeofday, settimeofday, |
8 | * adjtime |
8 | * adjtime |
9 | */ |
9 | */ |
10 | /* |
10 | /* |
11 | * Modification history kernel/time.c |
11 | * Modification history kernel/time.c |
12 | * |
12 | * |
13 | * 1993-09-02 Philip Gladstone |
13 | * 1993-09-02 Philip Gladstone |
14 | * Created file with time related functions from sched/core.c and adjtimex() |
14 | * Created file with time related functions from sched/core.c and adjtimex() |
15 | * 1993-10-08 Torsten Duwe |
15 | * 1993-10-08 Torsten Duwe |
16 | * adjtime interface update and CMOS clock write code |
16 | * adjtime interface update and CMOS clock write code |
17 | * 1995-08-13 Torsten Duwe |
17 | * 1995-08-13 Torsten Duwe |
18 | * kernel PLL updated to 1994-12-13 specs (rfc-1589) |
18 | * kernel PLL updated to 1994-12-13 specs (rfc-1589) |
19 | * 1999-01-16 Ulrich Windl |
19 | * 1999-01-16 Ulrich Windl |
20 | * Introduced error checking for many cases in adjtimex(). |
20 | * Introduced error checking for many cases in adjtimex(). |
21 | * Updated NTP code according to technical memorandum Jan '96 |
21 | * Updated NTP code according to technical memorandum Jan '96 |
22 | * "A Kernel Model for Precision Timekeeping" by Dave Mills |
22 | * "A Kernel Model for Precision Timekeeping" by Dave Mills |
23 | * Allow time_constant larger than MAXTC(6) for NTP v4 (MAXTC == 10) |
23 | * Allow time_constant larger than MAXTC(6) for NTP v4 (MAXTC == 10) |
24 | * (Even though the technical memorandum forbids it) |
24 | * (Even though the technical memorandum forbids it) |
25 | * 2004-07-14 Christoph Lameter |
25 | * 2004-07-14 Christoph Lameter |
26 | * Added getnstimeofday to allow the posix timer functions to return |
26 | * Added getnstimeofday to allow the posix timer functions to return |
27 | * with nanosecond accuracy |
27 | * with nanosecond accuracy |
28 | */ |
28 | */ |
29 | 29 | ||
30 | #include |
30 | #include |
31 | #include |
31 | #include |
32 | #include |
32 | #include |
33 | 33 | ||
34 | 34 | ||
35 | 35 | ||
36 | #define HZ_TO_MSEC_MUL32 0xA0000000 |
36 | #define HZ_TO_MSEC_MUL32 0xA0000000 |
37 | #define HZ_TO_MSEC_ADJ32 0x0 |
37 | #define HZ_TO_MSEC_ADJ32 0x0 |
38 | #define HZ_TO_MSEC_SHR32 28 |
38 | #define HZ_TO_MSEC_SHR32 28 |
39 | #define HZ_TO_MSEC_MUL64 0xA000000000000000 |
39 | #define HZ_TO_MSEC_MUL64 0xA000000000000000 |
40 | #define HZ_TO_MSEC_ADJ64 0x0 |
40 | #define HZ_TO_MSEC_ADJ64 0x0 |
41 | #define HZ_TO_MSEC_SHR64 60 |
41 | #define HZ_TO_MSEC_SHR64 60 |
42 | #define MSEC_TO_HZ_MUL32 0xCCCCCCCD |
42 | #define MSEC_TO_HZ_MUL32 0xCCCCCCCD |
43 | #define MSEC_TO_HZ_ADJ32 0x733333333 |
43 | #define MSEC_TO_HZ_ADJ32 0x733333333 |
44 | #define MSEC_TO_HZ_SHR32 35 |
44 | #define MSEC_TO_HZ_SHR32 35 |
45 | #define MSEC_TO_HZ_MUL64 0xCCCCCCCCCCCCCCCD |
45 | #define MSEC_TO_HZ_MUL64 0xCCCCCCCCCCCCCCCD |
46 | #define MSEC_TO_HZ_ADJ64 0x73333333333333333 |
46 | #define MSEC_TO_HZ_ADJ64 0x73333333333333333 |
47 | #define MSEC_TO_HZ_SHR64 67 |
47 | #define MSEC_TO_HZ_SHR64 67 |
48 | #define HZ_TO_MSEC_NUM 10 |
48 | #define HZ_TO_MSEC_NUM 10 |
49 | #define HZ_TO_MSEC_DEN 1 |
49 | #define HZ_TO_MSEC_DEN 1 |
50 | #define MSEC_TO_HZ_NUM 1 |
50 | #define MSEC_TO_HZ_NUM 1 |
51 | #define MSEC_TO_HZ_DEN 10 |
51 | #define MSEC_TO_HZ_DEN 10 |
52 | 52 | ||
53 | #define HZ_TO_USEC_MUL32 0x9C400000 |
53 | #define HZ_TO_USEC_MUL32 0x9C400000 |
54 | #define HZ_TO_USEC_ADJ32 0x0 |
54 | #define HZ_TO_USEC_ADJ32 0x0 |
55 | #define HZ_TO_USEC_SHR32 18 |
55 | #define HZ_TO_USEC_SHR32 18 |
56 | #define HZ_TO_USEC_MUL64 0x9C40000000000000 |
56 | #define HZ_TO_USEC_MUL64 0x9C40000000000000 |
57 | #define HZ_TO_USEC_ADJ64 0x0 |
57 | #define HZ_TO_USEC_ADJ64 0x0 |
58 | #define HZ_TO_USEC_SHR64 50 |
58 | #define HZ_TO_USEC_SHR64 50 |
59 | #define USEC_TO_HZ_MUL32 0xD1B71759 |
59 | #define USEC_TO_HZ_MUL32 0xD1B71759 |
60 | #define USEC_TO_HZ_ADJ32 0x1FFF2E48E8A7 |
60 | #define USEC_TO_HZ_ADJ32 0x1FFF2E48E8A7 |
61 | #define USEC_TO_HZ_SHR32 45 |
61 | #define USEC_TO_HZ_SHR32 45 |
62 | #define USEC_TO_HZ_MUL64 0xD1B71758E219652C |
62 | #define USEC_TO_HZ_MUL64 0xD1B71758E219652C |
63 | #define USEC_TO_HZ_ADJ64 0x1FFF2E48E8A71DE69AD4 |
63 | #define USEC_TO_HZ_ADJ64 0x1FFF2E48E8A71DE69AD4 |
64 | #define USEC_TO_HZ_SHR64 77 |
64 | #define USEC_TO_HZ_SHR64 77 |
65 | #define HZ_TO_USEC_NUM 10000 |
65 | #define HZ_TO_USEC_NUM 10000 |
66 | #define HZ_TO_USEC_DEN 1 |
66 | #define HZ_TO_USEC_DEN 1 |
67 | #define USEC_TO_HZ_NUM 1 |
67 | #define USEC_TO_HZ_NUM 1 |
68 | #define USEC_TO_HZ_DEN 10000 |
68 | #define USEC_TO_HZ_DEN 10000 |
69 | 69 | ||
70 | 70 | ||
71 | #define MSEC_PER_SEC 1000L |
71 | #define MSEC_PER_SEC 1000L |
72 | #define USEC_PER_MSEC 1000L |
72 | #define USEC_PER_MSEC 1000L |
73 | #define NSEC_PER_USEC 1000L |
73 | #define NSEC_PER_USEC 1000L |
74 | #define NSEC_PER_MSEC 1000000L |
74 | #define NSEC_PER_MSEC 1000000L |
75 | #define USEC_PER_SEC 1000000L |
75 | #define USEC_PER_SEC 1000000L |
76 | #define NSEC_PER_SEC 1000000000L |
76 | #define NSEC_PER_SEC 1000000000L |
77 | #define FSEC_PER_SEC 1000000000000000LL |
77 | #define FSEC_PER_SEC 1000000000000000LL |
78 | 78 | ||
79 | # define USER_HZ 100 |
79 | # define USER_HZ 100 |
80 | /* |
80 | /* |
81 | * Convert jiffies to milliseconds and back. |
81 | * Convert jiffies to milliseconds and back. |
82 | * |
82 | * |
83 | * Avoid unnecessary multiplications/divisions in the |
83 | * Avoid unnecessary multiplications/divisions in the |
84 | * two most common HZ cases: |
84 | * two most common HZ cases: |
85 | */ |
85 | */ |
86 | unsigned int jiffies_to_msecs(const unsigned long j) |
86 | unsigned int jiffies_to_msecs(const unsigned long j) |
87 | { |
87 | { |
88 | #if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ) |
88 | #if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ) |
89 | return (MSEC_PER_SEC / HZ) * j; |
89 | return (MSEC_PER_SEC / HZ) * j; |
90 | #elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC) |
90 | #elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC) |
91 | return (j + (HZ / MSEC_PER_SEC) - 1)/(HZ / MSEC_PER_SEC); |
91 | return (j + (HZ / MSEC_PER_SEC) - 1)/(HZ / MSEC_PER_SEC); |
92 | #else |
92 | #else |
93 | # if BITS_PER_LONG == 32 |
93 | # if BITS_PER_LONG == 32 |
94 | return (HZ_TO_MSEC_MUL32 * j) >> HZ_TO_MSEC_SHR32; |
94 | return (HZ_TO_MSEC_MUL32 * j) >> HZ_TO_MSEC_SHR32; |
95 | # else |
95 | # else |
96 | return (j * HZ_TO_MSEC_NUM) / HZ_TO_MSEC_DEN; |
96 | return (j * HZ_TO_MSEC_NUM) / HZ_TO_MSEC_DEN; |
97 | # endif |
97 | # endif |
98 | #endif |
98 | #endif |
99 | } |
99 | } |
100 | EXPORT_SYMBOL(jiffies_to_msecs); |
100 | EXPORT_SYMBOL(jiffies_to_msecs); |
101 | 101 | ||
102 | unsigned int jiffies_to_usecs(const unsigned long j) |
102 | unsigned int jiffies_to_usecs(const unsigned long j) |
103 | { |
103 | { |
104 | #if HZ <= USEC_PER_SEC && !(USEC_PER_SEC % HZ) |
104 | #if HZ <= USEC_PER_SEC && !(USEC_PER_SEC % HZ) |
105 | return (USEC_PER_SEC / HZ) * j; |
105 | return (USEC_PER_SEC / HZ) * j; |
106 | #elif HZ > USEC_PER_SEC && !(HZ % USEC_PER_SEC) |
106 | #elif HZ > USEC_PER_SEC && !(HZ % USEC_PER_SEC) |
107 | return (j + (HZ / USEC_PER_SEC) - 1)/(HZ / USEC_PER_SEC); |
107 | return (j + (HZ / USEC_PER_SEC) - 1)/(HZ / USEC_PER_SEC); |
108 | #else |
108 | #else |
109 | # if BITS_PER_LONG == 32 |
109 | # if BITS_PER_LONG == 32 |
110 | return (HZ_TO_USEC_MUL32 * j) >> HZ_TO_USEC_SHR32; |
110 | return (HZ_TO_USEC_MUL32 * j) >> HZ_TO_USEC_SHR32; |
111 | # else |
111 | # else |
112 | return (j * HZ_TO_USEC_NUM) / HZ_TO_USEC_DEN; |
112 | return (j * HZ_TO_USEC_NUM) / HZ_TO_USEC_DEN; |
113 | # endif |
113 | # endif |
114 | #endif |
114 | #endif |
115 | } |
115 | } |
116 | EXPORT_SYMBOL(jiffies_to_usecs); |
116 | EXPORT_SYMBOL(jiffies_to_usecs); |
117 | 117 | ||
118 | /** |
118 | /** |
119 | * timespec_trunc - Truncate timespec to a granularity |
119 | * timespec_trunc - Truncate timespec to a granularity |
120 | * @t: Timespec |
120 | * @t: Timespec |
121 | * @gran: Granularity in ns. |
121 | * @gran: Granularity in ns. |
122 | * |
122 | * |
123 | * Truncate a timespec to a granularity. Always rounds down. gran must |
123 | * Truncate a timespec to a granularity. Always rounds down. gran must |
124 | * not be 0 nor greater than a second (NSEC_PER_SEC, or 10^9 ns). |
124 | * not be 0 nor greater than a second (NSEC_PER_SEC, or 10^9 ns). |
125 | */ |
125 | */ |
126 | struct timespec timespec_trunc(struct timespec t, unsigned gran) |
126 | struct timespec timespec_trunc(struct timespec t, unsigned gran) |
127 | { |
127 | { |
128 | /* Avoid division in the common cases 1 ns and 1 s. */ |
128 | /* Avoid division in the common cases 1 ns and 1 s. */ |
129 | if (gran == 1) { |
129 | if (gran == 1) { |
130 | /* nothing */ |
130 | /* nothing */ |
131 | } else if (gran == NSEC_PER_SEC) { |
131 | } else if (gran == NSEC_PER_SEC) { |
132 | t.tv_nsec = 0; |
132 | t.tv_nsec = 0; |
133 | } else if (gran > 1 && gran < NSEC_PER_SEC) { |
133 | } else if (gran > 1 && gran < NSEC_PER_SEC) { |
134 | t.tv_nsec -= t.tv_nsec % gran; |
134 | t.tv_nsec -= t.tv_nsec % gran; |
135 | } else { |
135 | } else { |
136 | WARN(1, "illegal file time granularity: %u", gran); |
136 | WARN(1, "illegal file time granularity: %u", gran); |
137 | } |
137 | } |
138 | return t; |
138 | return t; |
139 | } |
139 | } |
140 | EXPORT_SYMBOL(timespec_trunc); |
140 | EXPORT_SYMBOL(timespec_trunc); |
141 | 141 | ||
142 | /* |
142 | /* |
143 | * mktime64 - Converts date to seconds. |
143 | * mktime64 - Converts date to seconds. |
144 | * Converts Gregorian date to seconds since 1970-01-01 00:00:00. |
144 | * Converts Gregorian date to seconds since 1970-01-01 00:00:00. |
145 | * Assumes input in normal date format, i.e. 1980-12-31 23:59:59 |
145 | * Assumes input in normal date format, i.e. 1980-12-31 23:59:59 |
146 | * => year=1980, mon=12, day=31, hour=23, min=59, sec=59. |
146 | * => year=1980, mon=12, day=31, hour=23, min=59, sec=59. |
147 | * |
147 | * |
148 | * [For the Julian calendar (which was used in Russia before 1917, |
148 | * [For the Julian calendar (which was used in Russia before 1917, |
149 | * Britain & colonies before 1752, anywhere else before 1582, |
149 | * Britain & colonies before 1752, anywhere else before 1582, |
150 | * and is still in use by some communities) leave out the |
150 | * and is still in use by some communities) leave out the |
151 | * -year/100+year/400 terms, and add 10.] |
151 | * -year/100+year/400 terms, and add 10.] |
152 | * |
152 | * |
153 | * This algorithm was first published by Gauss (I think). |
153 | * This algorithm was first published by Gauss (I think). |
154 | */ |
154 | */ |
155 | time64_t mktime64(const unsigned int year0, const unsigned int mon0, |
155 | time64_t mktime64(const unsigned int year0, const unsigned int mon0, |
156 | const unsigned int day, const unsigned int hour, |
156 | const unsigned int day, const unsigned int hour, |
157 | const unsigned int min, const unsigned int sec) |
157 | const unsigned int min, const unsigned int sec) |
158 | { |
158 | { |
159 | unsigned int mon = mon0, year = year0; |
159 | unsigned int mon = mon0, year = year0; |
160 | 160 | ||
161 | /* 1..12 -> 11,12,1..10 */ |
161 | /* 1..12 -> 11,12,1..10 */ |
162 | if (0 >= (int) (mon -= 2)) { |
162 | if (0 >= (int) (mon -= 2)) { |
163 | mon += 12; /* Puts Feb last since it has leap day */ |
163 | mon += 12; /* Puts Feb last since it has leap day */ |
164 | year -= 1; |
164 | year -= 1; |
165 | } |
165 | } |
166 | 166 | ||
167 | return ((((time64_t) |
167 | return ((((time64_t) |
168 | (year/4 - year/100 + year/400 + 367*mon/12 + day) + |
168 | (year/4 - year/100 + year/400 + 367*mon/12 + day) + |
169 | year*365 - 719499 |
169 | year*365 - 719499 |
170 | )*24 + hour /* now have hours */ |
170 | )*24 + hour /* now have hours */ |
171 | )*60 + min /* now have minutes */ |
171 | )*60 + min /* now have minutes */ |
172 | )*60 + sec; /* finally seconds */ |
172 | )*60 + sec; /* finally seconds */ |
173 | } |
173 | } |
174 | EXPORT_SYMBOL(mktime64); |
174 | EXPORT_SYMBOL(mktime64); |
175 | 175 | ||
176 | /** |
176 | /** |
177 | * set_normalized_timespec - set timespec sec and nsec parts and normalize |
177 | * set_normalized_timespec - set timespec sec and nsec parts and normalize |
178 | * |
178 | * |
179 | * @ts: pointer to timespec variable to be set |
179 | * @ts: pointer to timespec variable to be set |
180 | * @sec: seconds to set |
180 | * @sec: seconds to set |
181 | * @nsec: nanoseconds to set |
181 | * @nsec: nanoseconds to set |
182 | * |
182 | * |
183 | * Set seconds and nanoseconds field of a timespec variable and |
183 | * Set seconds and nanoseconds field of a timespec variable and |
184 | * normalize to the timespec storage format |
184 | * normalize to the timespec storage format |
185 | * |
185 | * |
186 | * Note: The tv_nsec part is always in the range of |
186 | * Note: The tv_nsec part is always in the range of |
187 | * 0 <= tv_nsec < NSEC_PER_SEC |
187 | * 0 <= tv_nsec < NSEC_PER_SEC |
188 | * For negative values only the tv_sec field is negative ! |
188 | * For negative values only the tv_sec field is negative ! |
189 | */ |
189 | */ |
190 | void set_normalized_timespec(struct timespec *ts, time_t sec, s64 nsec) |
190 | void set_normalized_timespec(struct timespec *ts, time_t sec, s64 nsec) |
191 | { |
191 | { |
192 | while (nsec >= NSEC_PER_SEC) { |
192 | while (nsec >= NSEC_PER_SEC) { |
193 | /* |
193 | /* |
194 | * The following asm() prevents the compiler from |
194 | * The following asm() prevents the compiler from |
195 | * optimising this loop into a modulo operation. See |
195 | * optimising this loop into a modulo operation. See |
196 | * also __iter_div_u64_rem() in include/linux/time.h |
196 | * also __iter_div_u64_rem() in include/linux/time.h |
197 | */ |
197 | */ |
198 | asm("" : "+rm"(nsec)); |
198 | asm("" : "+rm"(nsec)); |
199 | nsec -= NSEC_PER_SEC; |
199 | nsec -= NSEC_PER_SEC; |
200 | ++sec; |
200 | ++sec; |
201 | } |
201 | } |
202 | while (nsec < 0) { |
202 | while (nsec < 0) { |
203 | asm("" : "+rm"(nsec)); |
203 | asm("" : "+rm"(nsec)); |
204 | nsec += NSEC_PER_SEC; |
204 | nsec += NSEC_PER_SEC; |
205 | --sec; |
205 | --sec; |
206 | } |
206 | } |
207 | ts->tv_sec = sec; |
207 | ts->tv_sec = sec; |
208 | ts->tv_nsec = nsec; |
208 | ts->tv_nsec = nsec; |
209 | } |
209 | } |
210 | EXPORT_SYMBOL(set_normalized_timespec); |
210 | EXPORT_SYMBOL(set_normalized_timespec); |
211 | 211 | ||
212 | /** |
212 | /** |
213 | * ns_to_timespec - Convert nanoseconds to timespec |
213 | * ns_to_timespec - Convert nanoseconds to timespec |
214 | * @nsec: the nanoseconds value to be converted |
214 | * @nsec: the nanoseconds value to be converted |
215 | * |
215 | * |
216 | * Returns the timespec representation of the nsec parameter. |
216 | * Returns the timespec representation of the nsec parameter. |
217 | */ |
217 | */ |
218 | struct timespec ns_to_timespec(const s64 nsec) |
218 | struct timespec ns_to_timespec(const s64 nsec) |
219 | { |
219 | { |
220 | struct timespec ts; |
220 | struct timespec ts; |
221 | s32 rem; |
221 | s32 rem; |
222 | 222 | ||
223 | if (!nsec) |
223 | if (!nsec) |
224 | return (struct timespec) {0, 0}; |
224 | return (struct timespec) {0, 0}; |
225 | 225 | ||
226 | ts.tv_sec = div_s64_rem(nsec, NSEC_PER_SEC, &rem); |
226 | ts.tv_sec = div_s64_rem(nsec, NSEC_PER_SEC, &rem); |
227 | if (unlikely(rem < 0)) { |
227 | if (unlikely(rem < 0)) { |
228 | ts.tv_sec--; |
228 | ts.tv_sec--; |
229 | rem += NSEC_PER_SEC; |
229 | rem += NSEC_PER_SEC; |
230 | } |
230 | } |
231 | ts.tv_nsec = rem; |
231 | ts.tv_nsec = rem; |
232 | 232 | ||
233 | return ts; |
233 | return ts; |
234 | } |
234 | } |
235 | EXPORT_SYMBOL(ns_to_timespec); |
235 | EXPORT_SYMBOL(ns_to_timespec); |
236 | 236 | ||
237 | /** |
237 | /** |
238 | * ns_to_timeval - Convert nanoseconds to timeval |
238 | * ns_to_timeval - Convert nanoseconds to timeval |
239 | * @nsec: the nanoseconds value to be converted |
239 | * @nsec: the nanoseconds value to be converted |
240 | * |
240 | * |
241 | * Returns the timeval representation of the nsec parameter. |
241 | * Returns the timeval representation of the nsec parameter. |
242 | */ |
242 | */ |
243 | struct timeval ns_to_timeval(const s64 nsec) |
243 | struct timeval ns_to_timeval(const s64 nsec) |
244 | { |
244 | { |
245 | struct timespec ts = ns_to_timespec(nsec); |
245 | struct timespec ts = ns_to_timespec(nsec); |
246 | struct timeval tv; |
246 | struct timeval tv; |
247 | 247 | ||
248 | tv.tv_sec = ts.tv_sec; |
248 | tv.tv_sec = ts.tv_sec; |
249 | tv.tv_usec = (suseconds_t) ts.tv_nsec / 1000; |
249 | tv.tv_usec = (suseconds_t) ts.tv_nsec / 1000; |
250 | 250 | ||
251 | return tv; |
251 | return tv; |
252 | } |
252 | } |
253 | EXPORT_SYMBOL(ns_to_timeval); |
253 | EXPORT_SYMBOL(ns_to_timeval); |
254 | 254 | ||
255 | #if BITS_PER_LONG == 32 |
255 | #if BITS_PER_LONG == 32 |
256 | /** |
256 | /** |
257 | * set_normalized_timespec - set timespec sec and nsec parts and normalize |
257 | * set_normalized_timespec - set timespec sec and nsec parts and normalize |
258 | * |
258 | * |
259 | * @ts: pointer to timespec variable to be set |
259 | * @ts: pointer to timespec variable to be set |
260 | * @sec: seconds to set |
260 | * @sec: seconds to set |
261 | * @nsec: nanoseconds to set |
261 | * @nsec: nanoseconds to set |
262 | * |
262 | * |
263 | * Set seconds and nanoseconds field of a timespec variable and |
263 | * Set seconds and nanoseconds field of a timespec variable and |
264 | * normalize to the timespec storage format |
264 | * normalize to the timespec storage format |
265 | * |
265 | * |
266 | * Note: The tv_nsec part is always in the range of |
266 | * Note: The tv_nsec part is always in the range of |
267 | * 0 <= tv_nsec < NSEC_PER_SEC |
267 | * 0 <= tv_nsec < NSEC_PER_SEC |
268 | * For negative values only the tv_sec field is negative ! |
268 | * For negative values only the tv_sec field is negative ! |
269 | */ |
269 | */ |
270 | void set_normalized_timespec64(struct timespec64 *ts, time64_t sec, s64 nsec) |
270 | void set_normalized_timespec64(struct timespec64 *ts, time64_t sec, s64 nsec) |
271 | { |
271 | { |
272 | while (nsec >= NSEC_PER_SEC) { |
272 | while (nsec >= NSEC_PER_SEC) { |
273 | /* |
273 | /* |
274 | * The following asm() prevents the compiler from |
274 | * The following asm() prevents the compiler from |
275 | * optimising this loop into a modulo operation. See |
275 | * optimising this loop into a modulo operation. See |
276 | * also __iter_div_u64_rem() in include/linux/time.h |
276 | * also __iter_div_u64_rem() in include/linux/time.h |
277 | */ |
277 | */ |
278 | asm("" : "+rm"(nsec)); |
278 | asm("" : "+rm"(nsec)); |
279 | nsec -= NSEC_PER_SEC; |
279 | nsec -= NSEC_PER_SEC; |
280 | ++sec; |
280 | ++sec; |
281 | } |
281 | } |
282 | while (nsec < 0) { |
282 | while (nsec < 0) { |
283 | asm("" : "+rm"(nsec)); |
283 | asm("" : "+rm"(nsec)); |
284 | nsec += NSEC_PER_SEC; |
284 | nsec += NSEC_PER_SEC; |
285 | --sec; |
285 | --sec; |
286 | } |
286 | } |
287 | ts->tv_sec = sec; |
287 | ts->tv_sec = sec; |
288 | ts->tv_nsec = nsec; |
288 | ts->tv_nsec = nsec; |
289 | } |
289 | } |
290 | EXPORT_SYMBOL(set_normalized_timespec64); |
290 | EXPORT_SYMBOL(set_normalized_timespec64); |
291 | 291 | ||
292 | /** |
292 | /** |
293 | * ns_to_timespec64 - Convert nanoseconds to timespec64 |
293 | * ns_to_timespec64 - Convert nanoseconds to timespec64 |
294 | * @nsec: the nanoseconds value to be converted |
294 | * @nsec: the nanoseconds value to be converted |
295 | * |
295 | * |
296 | * Returns the timespec64 representation of the nsec parameter. |
296 | * Returns the timespec64 representation of the nsec parameter. |
297 | */ |
297 | */ |
298 | struct timespec64 ns_to_timespec64(const s64 nsec) |
298 | struct timespec64 ns_to_timespec64(const s64 nsec) |
299 | { |
299 | { |
300 | struct timespec64 ts; |
300 | struct timespec64 ts; |
301 | s32 rem; |
301 | s32 rem; |
302 | 302 | ||
303 | if (!nsec) |
303 | if (!nsec) |
304 | return (struct timespec64) {0, 0}; |
304 | return (struct timespec64) {0, 0}; |
305 | 305 | ||
306 | ts.tv_sec = div_s64_rem(nsec, NSEC_PER_SEC, &rem); |
306 | ts.tv_sec = div_s64_rem(nsec, NSEC_PER_SEC, &rem); |
307 | if (unlikely(rem < 0)) { |
307 | if (unlikely(rem < 0)) { |
308 | ts.tv_sec--; |
308 | ts.tv_sec--; |
309 | rem += NSEC_PER_SEC; |
309 | rem += NSEC_PER_SEC; |
310 | } |
310 | } |
311 | ts.tv_nsec = rem; |
311 | ts.tv_nsec = rem; |
312 | 312 | ||
313 | return ts; |
313 | return ts; |
314 | } |
314 | } |
315 | EXPORT_SYMBOL(ns_to_timespec64); |
315 | EXPORT_SYMBOL(ns_to_timespec64); |
316 | #endif |
316 | #endif |
317 | /** |
317 | /** |
318 | * msecs_to_jiffies: - convert milliseconds to jiffies |
318 | * msecs_to_jiffies: - convert milliseconds to jiffies |
319 | * @m: time in milliseconds |
319 | * @m: time in milliseconds |
320 | * |
320 | * |
321 | * conversion is done as follows: |
321 | * conversion is done as follows: |
322 | * |
322 | * |
323 | * - negative values mean 'infinite timeout' (MAX_JIFFY_OFFSET) |
323 | * - negative values mean 'infinite timeout' (MAX_JIFFY_OFFSET) |
324 | * |
324 | * |
325 | * - 'too large' values [that would result in larger than |
325 | * - 'too large' values [that would result in larger than |
326 | * MAX_JIFFY_OFFSET values] mean 'infinite timeout' too. |
326 | * MAX_JIFFY_OFFSET values] mean 'infinite timeout' too. |
327 | * |
327 | * |
328 | * - all other values are converted to jiffies by either multiplying |
328 | * - all other values are converted to jiffies by either multiplying |
329 | * the input value by a factor or dividing it with a factor and |
329 | * the input value by a factor or dividing it with a factor and |
330 | * handling any 32-bit overflows. |
330 | * handling any 32-bit overflows. |
331 | * for the details see __msecs_to_jiffies() |
331 | * for the details see __msecs_to_jiffies() |
332 | * |
332 | * |
333 | * msecs_to_jiffies() checks for the passed in value being a constant |
333 | * msecs_to_jiffies() checks for the passed in value being a constant |
334 | * via __builtin_constant_p() allowing gcc to eliminate most of the |
334 | * via __builtin_constant_p() allowing gcc to eliminate most of the |
335 | * code, __msecs_to_jiffies() is called if the value passed does not |
335 | * code, __msecs_to_jiffies() is called if the value passed does not |
336 | * allow constant folding and the actual conversion must be done at |
336 | * allow constant folding and the actual conversion must be done at |
337 | * runtime. |
337 | * runtime. |
338 | * the _msecs_to_jiffies helpers are the HZ dependent conversion |
338 | * the _msecs_to_jiffies helpers are the HZ dependent conversion |
339 | * routines found in include/linux/jiffies.h |
339 | * routines found in include/linux/jiffies.h |
340 | */ |
340 | */ |
341 | unsigned long __msecs_to_jiffies(const unsigned int m) |
341 | unsigned long __msecs_to_jiffies(const unsigned int m) |
342 | { |
342 | { |
343 | /* |
343 | /* |
344 | * Negative value, means infinite timeout: |
344 | * Negative value, means infinite timeout: |
345 | */ |
345 | */ |
346 | if ((int)m < 0) |
346 | if ((int)m < 0) |
347 | return MAX_JIFFY_OFFSET; |
347 | return MAX_JIFFY_OFFSET; |
348 | return _msecs_to_jiffies(m); |
348 | return _msecs_to_jiffies(m); |
349 | } |
349 | } |
350 | EXPORT_SYMBOL(__msecs_to_jiffies); |
350 | EXPORT_SYMBOL(__msecs_to_jiffies); |
351 | 351 | ||
352 | unsigned long __usecs_to_jiffies(const unsigned int u) |
352 | unsigned long __usecs_to_jiffies(const unsigned int u) |
353 | { |
353 | { |
354 | if (u > jiffies_to_usecs(MAX_JIFFY_OFFSET)) |
354 | if (u > jiffies_to_usecs(MAX_JIFFY_OFFSET)) |
355 | return MAX_JIFFY_OFFSET; |
355 | return MAX_JIFFY_OFFSET; |
356 | return _usecs_to_jiffies(u); |
356 | return _usecs_to_jiffies(u); |
357 | } |
357 | } |
358 | EXPORT_SYMBOL(__usecs_to_jiffies); |
358 | EXPORT_SYMBOL(__usecs_to_jiffies); |
359 | 359 | ||
360 | /* |
360 | /* |
361 | * The TICK_NSEC - 1 rounds up the value to the next resolution. Note |
361 | * The TICK_NSEC - 1 rounds up the value to the next resolution. Note |
362 | * that a remainder subtract here would not do the right thing as the |
362 | * that a remainder subtract here would not do the right thing as the |
363 | * resolution values don't fall on second boundries. I.e. the line: |
363 | * resolution values don't fall on second boundries. I.e. the line: |
364 | * nsec -= nsec % TICK_NSEC; is NOT a correct resolution rounding. |
364 | * nsec -= nsec % TICK_NSEC; is NOT a correct resolution rounding. |
365 | * Note that due to the small error in the multiplier here, this |
365 | * Note that due to the small error in the multiplier here, this |
366 | * rounding is incorrect for sufficiently large values of tv_nsec, but |
366 | * rounding is incorrect for sufficiently large values of tv_nsec, but |
367 | * well formed timespecs should have tv_nsec < NSEC_PER_SEC, so we're |
367 | * well formed timespecs should have tv_nsec < NSEC_PER_SEC, so we're |
368 | * OK. |
368 | * OK. |
369 | * |
369 | * |
370 | * Rather, we just shift the bits off the right. |
370 | * Rather, we just shift the bits off the right. |
371 | * |
371 | * |
372 | * The >> (NSEC_JIFFIE_SC - SEC_JIFFIE_SC) converts the scaled nsec |
372 | * The >> (NSEC_JIFFIE_SC - SEC_JIFFIE_SC) converts the scaled nsec |
373 | * value to a scaled second value. |
373 | * value to a scaled second value. |
374 | */ |
374 | */ |
375 | static unsigned long |
375 | static unsigned long |
376 | __timespec64_to_jiffies(u64 sec, long nsec) |
376 | __timespec64_to_jiffies(u64 sec, long nsec) |
377 | { |
377 | { |
378 | nsec = nsec + TICK_NSEC - 1; |
378 | nsec = nsec + TICK_NSEC - 1; |
379 | 379 | ||
380 | if (sec >= MAX_SEC_IN_JIFFIES){ |
380 | if (sec >= MAX_SEC_IN_JIFFIES){ |
381 | sec = MAX_SEC_IN_JIFFIES; |
381 | sec = MAX_SEC_IN_JIFFIES; |
382 | nsec = 0; |
382 | nsec = 0; |
383 | } |
383 | } |
384 | return ((sec * SEC_CONVERSION) + |
384 | return ((sec * SEC_CONVERSION) + |
385 | (((u64)nsec * NSEC_CONVERSION) >> |
385 | (((u64)nsec * NSEC_CONVERSION) >> |
386 | (NSEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC; |
386 | (NSEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC; |
387 | 387 | ||
388 | } |
388 | } |
389 | 389 | ||
390 | static unsigned long |
390 | static unsigned long |
391 | __timespec_to_jiffies(unsigned long sec, long nsec) |
391 | __timespec_to_jiffies(unsigned long sec, long nsec) |
392 | { |
392 | { |
393 | return __timespec64_to_jiffies((u64)sec, nsec); |
393 | return __timespec64_to_jiffies((u64)sec, nsec); |
394 | } |
394 | } |
395 | 395 | ||
396 | unsigned long |
396 | unsigned long |
397 | timespec64_to_jiffies(const struct timespec64 *value) |
397 | timespec64_to_jiffies(const struct timespec64 *value) |
398 | { |
398 | { |
399 | return __timespec64_to_jiffies(value->tv_sec, value->tv_nsec); |
399 | return __timespec64_to_jiffies(value->tv_sec, value->tv_nsec); |
400 | } |
400 | } |
401 | EXPORT_SYMBOL(timespec64_to_jiffies); |
401 | EXPORT_SYMBOL(timespec64_to_jiffies); |
402 | 402 | ||
403 | void |
403 | void |
404 | jiffies_to_timespec64(const unsigned long jiffies, struct timespec64 *value) |
404 | jiffies_to_timespec64(const unsigned long jiffies, struct timespec64 *value) |
405 | { |
405 | { |
406 | /* |
406 | /* |
407 | * Convert jiffies to nanoseconds and separate with |
407 | * Convert jiffies to nanoseconds and separate with |
408 | * one divide. |
408 | * one divide. |
409 | */ |
409 | */ |
410 | u32 rem; |
410 | u32 rem; |
411 | value->tv_sec = div_u64_rem((u64)jiffies * TICK_NSEC, |
411 | value->tv_sec = div_u64_rem((u64)jiffies * TICK_NSEC, |
412 | NSEC_PER_SEC, &rem); |
412 | NSEC_PER_SEC, &rem); |
413 | value->tv_nsec = rem; |
413 | value->tv_nsec = rem; |
414 | } |
414 | } |
415 | EXPORT_SYMBOL(jiffies_to_timespec64); |
415 | EXPORT_SYMBOL(jiffies_to_timespec64); |
416 | 416 | ||
417 | /* |
417 | /* |
418 | * We could use a similar algorithm to timespec_to_jiffies (with a |
418 | * We could use a similar algorithm to timespec_to_jiffies (with a |
419 | * different multiplier for usec instead of nsec). But this has a |
419 | * different multiplier for usec instead of nsec). But this has a |
420 | * problem with rounding: we can't exactly add TICK_NSEC - 1 to the |
420 | * problem with rounding: we can't exactly add TICK_NSEC - 1 to the |
421 | * usec value, since it's not necessarily integral. |
421 | * usec value, since it's not necessarily integral. |
422 | * |
422 | * |
423 | * We could instead round in the intermediate scaled representation |
423 | * We could instead round in the intermediate scaled representation |
424 | * (i.e. in units of 1/2^(large scale) jiffies) but that's also |
424 | * (i.e. in units of 1/2^(large scale) jiffies) but that's also |
425 | * perilous: the scaling introduces a small positive error, which |
425 | * perilous: the scaling introduces a small positive error, which |
426 | * combined with a division-rounding-upward (i.e. adding 2^(scale) - 1 |
426 | * combined with a division-rounding-upward (i.e. adding 2^(scale) - 1 |
427 | * units to the intermediate before shifting) leads to accidental |
427 | * units to the intermediate before shifting) leads to accidental |
428 | * overflow and overestimates. |
428 | * overflow and overestimates. |
429 | * |
429 | * |
430 | * At the cost of one additional multiplication by a constant, just |
430 | * At the cost of one additional multiplication by a constant, just |
431 | * use the timespec implementation. |
431 | * use the timespec implementation. |
432 | */ |
432 | */ |
433 | unsigned long |
433 | unsigned long |
434 | timeval_to_jiffies(const struct timeval *value) |
434 | timeval_to_jiffies(const struct timeval *value) |
435 | { |
435 | { |
436 | return __timespec_to_jiffies(value->tv_sec, |
436 | return __timespec_to_jiffies(value->tv_sec, |
437 | value->tv_usec * NSEC_PER_USEC); |
437 | value->tv_usec * NSEC_PER_USEC); |
438 | } |
438 | } |
439 | EXPORT_SYMBOL(timeval_to_jiffies); |
439 | EXPORT_SYMBOL(timeval_to_jiffies); |
440 | 440 | ||
441 | void jiffies_to_timeval(const unsigned long jiffies, struct timeval *value) |
441 | void jiffies_to_timeval(const unsigned long jiffies, struct timeval *value) |
442 | { |
442 | { |
443 | /* |
443 | /* |
444 | * Convert jiffies to nanoseconds and separate with |
444 | * Convert jiffies to nanoseconds and separate with |
445 | * one divide. |
445 | * one divide. |
446 | */ |
446 | */ |
447 | u32 rem; |
447 | u32 rem; |
448 | 448 | ||
449 | value->tv_sec = div_u64_rem((u64)jiffies * TICK_NSEC, |
449 | value->tv_sec = div_u64_rem((u64)jiffies * TICK_NSEC, |
450 | NSEC_PER_SEC, &rem); |
450 | NSEC_PER_SEC, &rem); |
451 | value->tv_usec = rem / NSEC_PER_USEC; |
451 | value->tv_usec = rem / NSEC_PER_USEC; |
452 | } |
452 | } |
453 | EXPORT_SYMBOL(jiffies_to_timeval); |
453 | EXPORT_SYMBOL(jiffies_to_timeval); |
454 | 454 | ||
455 | /* |
455 | /* |
456 | * Convert jiffies/jiffies_64 to clock_t and back. |
456 | * Convert jiffies/jiffies_64 to clock_t and back. |
457 | */ |
457 | */ |
458 | clock_t jiffies_to_clock_t(unsigned long x) |
458 | clock_t jiffies_to_clock_t(unsigned long x) |
459 | { |
459 | { |
460 | #if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0 |
460 | #if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0 |
461 | # if HZ < USER_HZ |
461 | # if HZ < USER_HZ |
462 | return x * (USER_HZ / HZ); |
462 | return x * (USER_HZ / HZ); |
463 | # else |
463 | # else |
464 | return x / (HZ / USER_HZ); |
464 | return x / (HZ / USER_HZ); |
465 | # endif |
465 | # endif |
466 | #else |
466 | #else |
467 | return div_u64((u64)x * TICK_NSEC, NSEC_PER_SEC / USER_HZ); |
467 | return div_u64((u64)x * TICK_NSEC, NSEC_PER_SEC / USER_HZ); |
468 | #endif |
468 | #endif |
469 | } |
469 | } |
470 | EXPORT_SYMBOL(jiffies_to_clock_t); |
470 | EXPORT_SYMBOL(jiffies_to_clock_t); |
471 | 471 | ||
472 | unsigned long clock_t_to_jiffies(unsigned long x) |
472 | unsigned long clock_t_to_jiffies(unsigned long x) |
473 | { |
473 | { |
474 | #if (HZ % USER_HZ)==0 |
474 | #if (HZ % USER_HZ)==0 |
475 | if (x >= ~0UL / (HZ / USER_HZ)) |
475 | if (x >= ~0UL / (HZ / USER_HZ)) |
476 | return ~0UL; |
476 | return ~0UL; |
477 | return x * (HZ / USER_HZ); |
477 | return x * (HZ / USER_HZ); |
478 | #else |
478 | #else |
479 | /* Don't worry about loss of precision here .. */ |
479 | /* Don't worry about loss of precision here .. */ |
480 | if (x >= ~0UL / HZ * USER_HZ) |
480 | if (x >= ~0UL / HZ * USER_HZ) |
481 | return ~0UL; |
481 | return ~0UL; |
482 | 482 | ||
483 | /* .. but do try to contain it here */ |
483 | /* .. but do try to contain it here */ |
484 | return div_u64((u64)x * HZ, USER_HZ); |
484 | return div_u64((u64)x * HZ, USER_HZ); |
485 | #endif |
485 | #endif |
486 | } |
486 | } |
487 | EXPORT_SYMBOL(clock_t_to_jiffies); |
487 | EXPORT_SYMBOL(clock_t_to_jiffies); |
488 | 488 | ||
489 | u64 jiffies_64_to_clock_t(u64 x) |
489 | u64 jiffies_64_to_clock_t(u64 x) |
490 | { |
490 | { |
491 | #if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0 |
491 | #if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0 |
492 | # if HZ < USER_HZ |
492 | # if HZ < USER_HZ |
493 | x = div_u64(x * USER_HZ, HZ); |
493 | x = div_u64(x * USER_HZ, HZ); |
494 | # elif HZ > USER_HZ |
494 | # elif HZ > USER_HZ |
495 | x = div_u64(x, HZ / USER_HZ); |
495 | x = div_u64(x, HZ / USER_HZ); |
496 | # else |
496 | # else |
497 | /* Nothing to do */ |
497 | /* Nothing to do */ |
498 | # endif |
498 | # endif |
499 | #else |
499 | #else |
500 | /* |
500 | /* |
501 | * There are better ways that don't overflow early, |
501 | * There are better ways that don't overflow early, |
502 | * but even this doesn't overflow in hundreds of years |
502 | * but even this doesn't overflow in hundreds of years |
503 | * in 64 bits, so.. |
503 | * in 64 bits, so.. |
504 | */ |
504 | */ |
505 | x = div_u64(x * TICK_NSEC, (NSEC_PER_SEC / USER_HZ)); |
505 | x = div_u64(x * TICK_NSEC, (NSEC_PER_SEC / USER_HZ)); |
506 | #endif |
506 | #endif |
507 | return x; |
507 | return x; |
508 | } |
508 | } |
509 | EXPORT_SYMBOL(jiffies_64_to_clock_t); |
509 | EXPORT_SYMBOL(jiffies_64_to_clock_t); |
510 | 510 | ||
511 | u64 nsec_to_clock_t(u64 x) |
511 | u64 nsec_to_clock_t(u64 x) |
512 | { |
512 | { |
513 | #if (NSEC_PER_SEC % USER_HZ) == 0 |
513 | #if (NSEC_PER_SEC % USER_HZ) == 0 |
514 | return div_u64(x, NSEC_PER_SEC / USER_HZ); |
514 | return div_u64(x, NSEC_PER_SEC / USER_HZ); |
515 | #elif (USER_HZ % 512) == 0 |
515 | #elif (USER_HZ % 512) == 0 |
516 | return div_u64(x * USER_HZ / 512, NSEC_PER_SEC / 512); |
516 | return div_u64(x * USER_HZ / 512, NSEC_PER_SEC / 512); |
517 | #else |
517 | #else |
518 | /* |
518 | /* |
519 | * max relative error 5.7e-8 (1.8s per year) for USER_HZ <= 1024, |
519 | * max relative error 5.7e-8 (1.8s per year) for USER_HZ <= 1024, |
520 | * overflow after 64.99 years. |
520 | * overflow after 64.99 years. |
521 | * exact for HZ=60, 72, 90, 120, 144, 180, 300, 600, 900, ... |
521 | * exact for HZ=60, 72, 90, 120, 144, 180, 300, 600, 900, ... |
522 | */ |
522 | */ |
523 | return div_u64(x * 9, (9ull * NSEC_PER_SEC + (USER_HZ / 2)) / USER_HZ); |
523 | return div_u64(x * 9, (9ull * NSEC_PER_SEC + (USER_HZ / 2)) / USER_HZ); |
524 | #endif |
524 | #endif |
525 | } |
525 | } |
526 | 526 | ||
527 | /** |
527 | /** |
528 | * nsecs_to_jiffies64 - Convert nsecs in u64 to jiffies64 |
528 | * nsecs_to_jiffies64 - Convert nsecs in u64 to jiffies64 |
529 | * |
529 | * |
530 | * @n: nsecs in u64 |
530 | * @n: nsecs in u64 |
531 | * |
531 | * |
532 | * Unlike {m,u}secs_to_jiffies, type of input is not unsigned int but u64. |
532 | * Unlike {m,u}secs_to_jiffies, type of input is not unsigned int but u64. |
533 | * And this doesn't return MAX_JIFFY_OFFSET since this function is designed |
533 | * And this doesn't return MAX_JIFFY_OFFSET since this function is designed |
534 | * for scheduler, not for use in device drivers to calculate timeout value. |
534 | * for scheduler, not for use in device drivers to calculate timeout value. |
535 | * |
535 | * |
536 | * note: |
536 | * note: |
537 | * NSEC_PER_SEC = 10^9 = (5^9 * 2^9) = (1953125 * 512) |
537 | * NSEC_PER_SEC = 10^9 = (5^9 * 2^9) = (1953125 * 512) |
538 | * ULLONG_MAX ns = 18446744073.709551615 secs = about 584 years |
538 | * ULLONG_MAX ns = 18446744073.709551615 secs = about 584 years |
539 | */ |
539 | */ |
540 | u64 nsecs_to_jiffies64(u64 n) |
540 | u64 nsecs_to_jiffies64(u64 n) |
541 | { |
541 | { |
542 | #if (NSEC_PER_SEC % HZ) == 0 |
542 | #if (NSEC_PER_SEC % HZ) == 0 |
543 | /* Common case, HZ = 100, 128, 200, 250, 256, 500, 512, 1000 etc. */ |
543 | /* Common case, HZ = 100, 128, 200, 250, 256, 500, 512, 1000 etc. */ |
544 | return div_u64(n, NSEC_PER_SEC / HZ); |
544 | return div_u64(n, NSEC_PER_SEC / HZ); |
545 | #elif (HZ % 512) == 0 |
545 | #elif (HZ % 512) == 0 |
546 | /* overflow after 292 years if HZ = 1024 */ |
546 | /* overflow after 292 years if HZ = 1024 */ |
547 | return div_u64(n * HZ / 512, NSEC_PER_SEC / 512); |
547 | return div_u64(n * HZ / 512, NSEC_PER_SEC / 512); |
548 | #else |
548 | #else |
549 | /* |
549 | /* |
550 | * Generic case - optimized for cases where HZ is a multiple of 3. |
550 | * Generic case - optimized for cases where HZ is a multiple of 3. |
551 | * overflow after 64.99 years, exact for HZ = 60, 72, 90, 120 etc. |
551 | * overflow after 64.99 years, exact for HZ = 60, 72, 90, 120 etc. |
552 | */ |
552 | */ |
553 | return div_u64(n * 9, (9ull * NSEC_PER_SEC + HZ / 2) / HZ); |
553 | return div_u64(n * 9, (9ull * NSEC_PER_SEC + HZ / 2) / HZ); |
554 | #endif |
554 | #endif |
555 | } |
555 | } |
556 | EXPORT_SYMBOL(nsecs_to_jiffies64); |
556 | EXPORT_SYMBOL(nsecs_to_jiffies64); |
557 | 557 | ||
558 | /** |
558 | /** |
559 | * nsecs_to_jiffies - Convert nsecs in u64 to jiffies |
559 | * nsecs_to_jiffies - Convert nsecs in u64 to jiffies |
560 | * |
560 | * |
561 | * @n: nsecs in u64 |
561 | * @n: nsecs in u64 |
562 | * |
562 | * |
563 | * Unlike {m,u}secs_to_jiffies, type of input is not unsigned int but u64. |
563 | * Unlike {m,u}secs_to_jiffies, type of input is not unsigned int but u64. |
564 | * And this doesn't return MAX_JIFFY_OFFSET since this function is designed |
564 | * And this doesn't return MAX_JIFFY_OFFSET since this function is designed |
565 | * for scheduler, not for use in device drivers to calculate timeout value. |
565 | * for scheduler, not for use in device drivers to calculate timeout value. |
566 | * |
566 | * |
567 | * note: |
567 | * note: |
568 | * NSEC_PER_SEC = 10^9 = (5^9 * 2^9) = (1953125 * 512) |
568 | * NSEC_PER_SEC = 10^9 = (5^9 * 2^9) = (1953125 * 512) |
569 | * ULLONG_MAX ns = 18446744073.709551615 secs = about 584 years |
569 | * ULLONG_MAX ns = 18446744073.709551615 secs = about 584 years |
570 | */ |
570 | */ |
571 | unsigned long nsecs_to_jiffies(u64 n) |
571 | unsigned long nsecs_to_jiffies(u64 n) |
572 | { |
572 | { |
573 | return (unsigned long)nsecs_to_jiffies64(n); |
573 | return (unsigned long)nsecs_to_jiffies64(n); |
574 | } |
574 | } |
575 | EXPORT_SYMBOL_GPL(nsecs_to_jiffies); |
575 | EXPORT_SYMBOL_GPL(nsecs_to_jiffies); |
576 | 576 | ||
577 | /* |
577 | /* |
578 | * Add two timespec values and do a safety check for overflow. |
578 | * Add two timespec values and do a safety check for overflow. |
579 | * It's assumed that both values are valid (>= 0) |
579 | * It's assumed that both values are valid (>= 0) |
580 | */ |
580 | */ |
581 | struct timespec timespec_add_safe(const struct timespec lhs, |
581 | struct timespec timespec_add_safe(const struct timespec lhs, |
582 | const struct timespec rhs) |
582 | const struct timespec rhs) |
583 | { |
583 | { |
584 | struct timespec res; |
584 | struct timespec res; |
585 | 585 | ||
586 | set_normalized_timespec(&res, lhs.tv_sec + rhs.tv_sec, |
586 | set_normalized_timespec(&res, lhs.tv_sec + rhs.tv_sec, |
587 | lhs.tv_nsec + rhs.tv_nsec); |
587 | lhs.tv_nsec + rhs.tv_nsec); |
588 | 588 | ||
589 | if (res.tv_sec < lhs.tv_sec || res.tv_sec < rhs.tv_sec) |
589 | if (res.tv_sec < lhs.tv_sec || res.tv_sec < rhs.tv_sec) |
590 | res.tv_sec = TIME_T_MAX; |
590 | res.tv_sec = TIME_T_MAX; |
591 | 591 | ||
592 | return res; |
592 | return res; |
593 | } |
593 | }>>=>>>>>>>>=>>>>=>>=>=> |
594 | - | ||
595 | s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder) |
- | |
596 | { |
- | |
597 | u64 quotient; |
- | |
598 | - | ||
599 | if (dividend < 0) { |
- | |
600 | quotient = div_u64_rem(-dividend, abs(divisor), (u32 *)remainder); |
- | |
601 | *remainder = -*remainder; |
- | |
602 | if (divisor > 0) |
- | |
603 | quotient = -quotient; |
- | |
604 | } else { |
- | |
605 | quotient = div_u64_rem(dividend, abs(divisor), (u32 *)remainder); |
- | |
606 | if (divisor < 0) |
- | |
607 | quotient = -quotient; |
- | |
608 | } |
- | |
609 | return quotient; |
- | |
610 | }>>>>=>>>>>>>>=>>>>=>>=>=> |
- | |
611 | - | ||
612 | - |