xref: /linux/kernel/time/jiffies.c (revision afdf35cfae0d039a4a6c907fa5d8391f1ef0a0aa)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * This file contains the jiffies based clocksource.
4  *
5  * Copyright (C) 2004, 2005 IBM, John Stultz (johnstul@us.ibm.com)
6  */
7 #include <linux/clocksource.h>
8 #include <linux/jiffies.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 
12 #include "timekeeping.h"
13 #include "tick-internal.h"
14 
15 
16 static u64 jiffies_read(struct clocksource *cs)
17 {
18 	return (u64) jiffies;
19 }
20 
21 /*
22  * The Jiffies based clocksource is the lowest common
23  * denominator clock source which should function on
24  * all systems. It has the same coarse resolution as
25  * the timer interrupt frequency HZ and it suffers
26  * inaccuracies caused by missed or lost timer
27  * interrupts and the inability for the timer
28  * interrupt hardware to accurately tick at the
29  * requested HZ value. It is also not recommended
30  * for "tick-less" systems.
31  */
32 static struct clocksource clocksource_jiffies = {
33 	.name			= "jiffies",
34 	.rating			= 1, /* lowest valid rating*/
35 	.read			= jiffies_read,
36 	.mask			= CLOCKSOURCE_MASK(32),
37 	.mult			= TICK_NSEC << JIFFIES_SHIFT, /* details above */
38 	.shift			= JIFFIES_SHIFT,
39 	.max_cycles		= 10,
40 };
41 
42 __cacheline_aligned_in_smp DEFINE_RAW_SPINLOCK(jiffies_lock);
43 __cacheline_aligned_in_smp seqcount_raw_spinlock_t jiffies_seq =
44 	SEQCNT_RAW_SPINLOCK_ZERO(jiffies_seq, &jiffies_lock);
45 
46 #if (BITS_PER_LONG < 64)
47 u64 get_jiffies_64(void)
48 {
49 	unsigned int seq;
50 	u64 ret;
51 
52 	do {
53 		seq = read_seqcount_begin(&jiffies_seq);
54 		ret = jiffies_64;
55 	} while (read_seqcount_retry(&jiffies_seq, seq));
56 	return ret;
57 }
58 EXPORT_SYMBOL(get_jiffies_64);
59 #endif
60 
61 EXPORT_SYMBOL(jiffies);
62 
63 static bool cs_jiffies_registered __initdata;
64 
65 struct clocksource * __init __weak clocksource_default_clock(void)
66 {
67 	if (!cs_jiffies_registered) {
68 		__clocksource_register(&clocksource_jiffies);
69 		cs_jiffies_registered = true;
70 	}
71 	return &clocksource_jiffies;
72 }
73 
74 static struct clocksource refined_jiffies;
75 
76 void __init register_refined_jiffies(long cycles_per_second)
77 {
78 	u64 nsec_per_tick, shift_hz;
79 	long cycles_per_tick;
80 
81 	refined_jiffies = clocksource_jiffies;
82 	refined_jiffies.name = "refined-jiffies";
83 	refined_jiffies.rating++;
84 
85 	/* Calc cycles per tick */
86 	cycles_per_tick = (cycles_per_second + HZ/2)/HZ;
87 	/* shift_hz stores hz<<8 for extra accuracy */
88 	shift_hz = (u64)cycles_per_second << 8;
89 	shift_hz += cycles_per_tick/2;
90 	do_div(shift_hz, cycles_per_tick);
91 	/* Calculate nsec_per_tick using shift_hz */
92 	nsec_per_tick = (u64)NSEC_PER_SEC << 8;
93 	nsec_per_tick += (u32)shift_hz/2;
94 	do_div(nsec_per_tick, (u32)shift_hz);
95 
96 	refined_jiffies.mult = ((u32)nsec_per_tick) << JIFFIES_SHIFT;
97 
98 	__clocksource_register(&refined_jiffies);
99 }
100 
101 #ifdef CONFIG_SYSCTL
102 static ulong mult_hz(const ulong val)
103 {
104 	return val * HZ;
105 }
106 
107 static ulong div_hz(const ulong val)
108 {
109 	return val / HZ;
110 }
111 
112 static int sysctl_u2k_int_conv_hz(const bool *negp, const ulong *u_ptr, int *k_ptr)
113 {
114 	return proc_int_u2k_conv_uop(u_ptr, k_ptr, negp, mult_hz);
115 }
116 
117 static int sysctl_k2u_int_conv_hz(bool *negp, ulong *u_ptr, const int *k_ptr)
118 {
119 	return proc_int_k2u_conv_kop(u_ptr, k_ptr, negp, div_hz);
120 }
121 
122 static int sysctl_u2k_int_conv_userhz(const bool *negp, const ulong *u_ptr, int *k_ptr)
123 {
124 	return proc_int_u2k_conv_uop(u_ptr, k_ptr, negp, clock_t_to_jiffies);
125 }
126 
127 static ulong sysctl_jiffies_to_clock_t(const ulong val)
128 {
129 	return jiffies_to_clock_t(val);
130 }
131 
132 static int sysctl_k2u_int_conv_userhz(bool *negp, ulong *u_ptr, const int *k_ptr)
133 {
134 	return proc_int_k2u_conv_kop(u_ptr, k_ptr, negp, sysctl_jiffies_to_clock_t);
135 }
136 
137 static ulong sysctl_msecs_to_jiffies(const ulong val)
138 {
139 	if (val > jiffies_to_msecs(MAX_JIFFY_OFFSET))
140 		return MAX_JIFFY_OFFSET;
141 	return msecs_to_jiffies(val);
142 }
143 
144 static int sysctl_u2k_int_conv_ms(const bool *negp, const ulong *u_ptr, int *k_ptr)
145 {
146 	return proc_int_u2k_conv_uop(u_ptr, k_ptr, negp, sysctl_msecs_to_jiffies);
147 }
148 
149 static ulong sysctl_jiffies_to_msecs(const ulong val)
150 {
151 	return jiffies_to_msecs(val);
152 }
153 
154 static int sysctl_k2u_int_conv_ms(bool *negp, ulong *u_ptr, const int *k_ptr)
155 {
156 	return proc_int_k2u_conv_kop(u_ptr, k_ptr, negp, sysctl_jiffies_to_msecs);
157 }
158 
159 static int do_proc_int_conv_jiffies(bool *negp, ulong *u_ptr, int *k_ptr,
160 				    int dir, const struct ctl_table *tbl)
161 {
162 	return proc_int_conv(negp, u_ptr, k_ptr, dir, tbl, false,
163 			     sysctl_u2k_int_conv_hz, sysctl_k2u_int_conv_hz);
164 }
165 
166 static int do_proc_int_conv_userhz_jiffies(bool *negp, ulong *u_ptr,
167 					   int *k_ptr, int dir,
168 					   const struct ctl_table *tbl)
169 {
170 	return proc_int_conv(negp, u_ptr, k_ptr, dir, tbl, false,
171 			     sysctl_u2k_int_conv_userhz,
172 			     sysctl_k2u_int_conv_userhz);
173 }
174 
175 static int do_proc_int_conv_ms_jiffies(bool *negp, ulong *u_ptr, int *k_ptr,
176 				       int dir, const struct ctl_table *tbl)
177 {
178 	return proc_int_conv(negp, u_ptr, k_ptr, dir, tbl, false,
179 			     sysctl_u2k_int_conv_ms, sysctl_k2u_int_conv_ms);
180 }
181 
182 static int do_proc_int_conv_ms_jiffies_minmax(bool *negp, ulong *u_ptr,
183 					      int *k_ptr, int dir,
184 					      const struct ctl_table *tbl)
185 {
186 	return proc_int_conv(negp, u_ptr, k_ptr, dir, tbl, true,
187 			     sysctl_u2k_int_conv_ms, sysctl_k2u_int_conv_ms);
188 }
189 
190 static int sysctl_u2k_ulong_conv_ms(const ulong *u_ptr, ulong *k_ptr)
191 {
192 	return proc_ulong_u2k_conv_uop(u_ptr, k_ptr, sysctl_msecs_to_jiffies);
193 }
194 
195 static int sysctl_k2u_ulong_conv_ms(ulong *u_ptr, const ulong *k_ptr)
196 {
197 	return proc_ulong_k2u_conv_kop(u_ptr, k_ptr, sysctl_jiffies_to_msecs);
198 }
199 
200 static int do_proc_ulong_conv_ms_jiffies_minmax(bool *negp, ulong *u_ptr, ulong *k_ptr,
201 						int dir, const struct ctl_table *tbl)
202 {
203 	return proc_ulong_conv(u_ptr, k_ptr, dir, tbl, true,
204 			       sysctl_u2k_ulong_conv_ms, sysctl_k2u_ulong_conv_ms);
205 }
206 
207 #else // CONFIG_SYSCTL
208 static int do_proc_int_conv_jiffies(bool *negp, ulong *u_ptr, int *k_ptr,
209 				    int dir, const struct ctl_table *tbl)
210 {
211 	return -ENOSYS;
212 }
213 
214 static int do_proc_int_conv_userhz_jiffies(bool *negp, ulong *u_ptr,
215 					   int *k_ptr, int dir,
216 					   const struct ctl_table *tbl)
217 {
218 	return -ENOSYS;
219 }
220 
221 static int do_proc_int_conv_ms_jiffies(bool *negp, ulong *u_ptr, int *k_ptr,
222 				       int dir, const struct ctl_table *tbl)
223 {
224 	return -ENOSYS;
225 }
226 
227 static int do_proc_int_conv_ms_jiffies_minmax(bool *negp, ulong *u_ptr,
228 					      int *k_ptr, int dir,
229 					      const struct ctl_table *tbl)
230 {
231 	return -ENOSYS;
232 }
233 
234 static int do_proc_ulong_conv_ms_jiffies_minmax(bool *negp, ulong *u_ptr, ulong *k_ptr,
235 						int dir, const struct ctl_table *tbl)
236 {
237 	return -ENOSYS;
238 }
239 #endif
240 
241 /**
242  * proc_dointvec_jiffies - read a vector of integers as seconds
243  * @table: the sysctl table
244  * @dir: %TRUE if this is a write to the sysctl file
245  * @buffer: the user buffer
246  * @lenp: the size of the user buffer
247  * @ppos: file position
248  *
249  * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
250  * values from/to the user buffer, treated as an ASCII string.
251  * The values read are assumed to be in seconds, and are converted into
252  * jiffies.
253  *
254  * Returns 0 on success.
255  */
256 int proc_dointvec_jiffies(const struct ctl_table *table, int dir,
257 			  void *buffer, size_t *lenp, loff_t *ppos)
258 {
259 	return proc_dointvec_conv(table, dir, buffer, lenp, ppos,
260 				  do_proc_int_conv_jiffies);
261 }
262 EXPORT_SYMBOL(proc_dointvec_jiffies);
263 
264 /**
265  * proc_dointvec_userhz_jiffies - read a vector of integers as 1/USER_HZ seconds
266  * @table: the sysctl table
267  * @dir: %TRUE if this is a write to the sysctl file
268  * @buffer: the user buffer
269  * @lenp: the size of the user buffer
270  * @ppos: pointer to the file position
271  *
272  * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
273  * values from/to the user buffer, treated as an ASCII string.
274  * The values read are assumed to be in 1/USER_HZ seconds, and
275  * are converted into jiffies.
276  *
277  * Returns 0 on success.
278  */
279 int proc_dointvec_userhz_jiffies(const struct ctl_table *table, int dir,
280 				 void *buffer, size_t *lenp, loff_t *ppos)
281 {
282 	return proc_dointvec_conv(table, dir, buffer, lenp, ppos,
283 				  do_proc_int_conv_userhz_jiffies);
284 }
285 EXPORT_SYMBOL(proc_dointvec_userhz_jiffies);
286 
287 /**
288  * proc_dointvec_ms_jiffies - read a vector of integers as 1 milliseconds
289  * @table: the sysctl table
290  * @dir: %TRUE if this is a write to the sysctl file
291  * @buffer: the user buffer
292  * @lenp: the size of the user buffer
293  * @ppos: the current position in the file
294  *
295  * Reads/writes up to table->maxlen/sizeof(unsigned int) integer
296  * values from/to the user buffer, treated as an ASCII string.
297  * The values read are assumed to be in 1/1000 seconds, and
298  * are converted into jiffies.
299  *
300  * Returns 0 on success.
301  */
302 int proc_dointvec_ms_jiffies(const struct ctl_table *table, int dir, void *buffer,
303 		size_t *lenp, loff_t *ppos)
304 {
305 	return proc_dointvec_conv(table, dir, buffer, lenp, ppos,
306 				  do_proc_int_conv_ms_jiffies);
307 }
308 EXPORT_SYMBOL(proc_dointvec_ms_jiffies);
309 
310 int proc_dointvec_ms_jiffies_minmax(const struct ctl_table *table, int dir,
311 			  void *buffer, size_t *lenp, loff_t *ppos)
312 {
313 	return proc_dointvec_conv(table, dir, buffer, lenp, ppos,
314 				  do_proc_int_conv_ms_jiffies_minmax);
315 }
316 
317 /**
318  * proc_doulongvec_ms_jiffies_minmax - read a vector of millisecond values with min/max values
319  * @table: the sysctl table
320  * @dir: %TRUE if this is a write to the sysctl file
321  * @buffer: the user buffer
322  * @lenp: the size of the user buffer
323  * @ppos: file position
324  *
325  * Reads/writes up to table->maxlen/sizeof(unsigned long) unsigned long
326  * values from/to the user buffer, treated as an ASCII string. The values
327  * are treated as milliseconds, and converted to jiffies when they are stored.
328  *
329  * This routine will ensure the values are within the range specified by
330  * table->extra1 (min) and table->extra2 (max).
331  *
332  * Returns 0 on success.
333  */
334 int proc_doulongvec_ms_jiffies_minmax(const struct ctl_table *table, int dir,
335 				      void *buffer, size_t *lenp, loff_t *ppos)
336 {
337 	return proc_doulongvec_conv(table, dir, buffer, lenp, ppos,
338 				    do_proc_ulong_conv_ms_jiffies_minmax);
339 }
340 EXPORT_SYMBOL(proc_doulongvec_ms_jiffies_minmax);
341 
342