OSDN Git Service

mm: vmalloc: show number of vmalloc pages in /proc/meminfo
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / include / linux / hrtimer.h
1 /*
2  *  include/linux/hrtimer.h
3  *
4  *  hrtimers - High-resolution kernel timers
5  *
6  *   Copyright(C) 2005, Thomas Gleixner <tglx@linutronix.de>
7  *   Copyright(C) 2005, Red Hat, Inc., Ingo Molnar
8  *
9  *  data type definitions, declarations, prototypes
10  *
11  *  Started by: Thomas Gleixner and Ingo Molnar
12  *
13  *  For licencing details see kernel-base/COPYING
14  */
15 #ifndef _LINUX_HRTIMER_H
16 #define _LINUX_HRTIMER_H
17
18 #include <linux/rbtree.h>
19 #include <linux/ktime.h>
20 #include <linux/init.h>
21 #include <linux/list.h>
22 #include <linux/wait.h>
23 #include <linux/percpu.h>
24 #include <linux/timer.h>
25 #include <linux/timerqueue.h>
26
27 struct hrtimer_clock_base;
28 struct hrtimer_cpu_base;
29
30 /*
31  * Mode arguments of xxx_hrtimer functions:
32  */
33 enum hrtimer_mode {
34         HRTIMER_MODE_ABS = 0x0,         /* Time value is absolute */
35         HRTIMER_MODE_REL = 0x1,         /* Time value is relative to now */
36         HRTIMER_MODE_PINNED = 0x02,     /* Timer is bound to CPU */
37         HRTIMER_MODE_ABS_PINNED = 0x02,
38         HRTIMER_MODE_REL_PINNED = 0x03,
39 };
40
41 /*
42  * Return values for the callback function
43  */
44 enum hrtimer_restart {
45         HRTIMER_NORESTART,      /* Timer is not restarted */
46         HRTIMER_RESTART,        /* Timer must be restarted */
47 };
48
49 /*
50  * Values to track state of the timer
51  *
52  * Possible states:
53  *
54  * 0x00         inactive
55  * 0x01         enqueued into rbtree
56  * 0x02         timer is pinned to a cpu
57  *
58  * The callback state is not part of the timer->state because clearing it would
59  * mean touching the timer after the callback, this makes it impossible to free
60  * the timer from the callback function.
61  *
62  * Therefore we track the callback state in:
63  *
64  *      timer->base->cpu_base->running == timer
65  *
66  * On SMP it is possible to have a "callback function running and enqueued"
67  * status. It happens for example when a posix timer expired and the callback
68  * queued a signal. Between dropping the lock which protects the posix timer
69  * and reacquiring the base lock of the hrtimer, another CPU can deliver the
70  * signal and rearm the timer.
71  *
72  * All state transitions are protected by cpu_base->lock.
73  */
74 #define HRTIMER_STATE_INACTIVE  0x00
75 #define HRTIMER_STATE_ENQUEUED  0x01
76 #define HRTIMER_PINNED_SHIFT    1
77 #define HRTIMER_STATE_PINNED    (1 << HRTIMER_PINNED_SHIFT)
78
79 /**
80  * struct hrtimer - the basic hrtimer structure
81  * @node:       timerqueue node, which also manages node.expires,
82  *              the absolute expiry time in the hrtimers internal
83  *              representation. The time is related to the clock on
84  *              which the timer is based. Is setup by adding
85  *              slack to the _softexpires value. For non range timers
86  *              identical to _softexpires.
87  * @_softexpires: the absolute earliest expiry time of the hrtimer.
88  *              The time which was given as expiry time when the timer
89  *              was armed.
90  * @function:   timer expiry callback function
91  * @base:       pointer to the timer base (per cpu and per clock)
92  * @state:      state information (See bit values above)
93  * @is_rel:     Set if the timer was armed relative
94  *
95  * The hrtimer structure must be initialized by hrtimer_init()
96  */
97 struct hrtimer {
98         struct timerqueue_node          node;
99         ktime_t                         _softexpires;
100         enum hrtimer_restart            (*function)(struct hrtimer *);
101         struct hrtimer_clock_base       *base;
102         u8                              state;
103         u8                              is_rel;
104 };
105
106 /**
107  * struct hrtimer_sleeper - simple sleeper structure
108  * @timer:      embedded timer structure
109  * @task:       task to wake up
110  *
111  * task is set to NULL, when the timer expires.
112  */
113 struct hrtimer_sleeper {
114         struct hrtimer timer;
115         struct task_struct *task;
116 };
117
118 #ifdef CONFIG_64BIT
119 # define HRTIMER_CLOCK_BASE_ALIGN       64
120 #else
121 # define HRTIMER_CLOCK_BASE_ALIGN       32
122 #endif
123
124 /**
125  * struct hrtimer_clock_base - the timer base for a specific clock
126  * @cpu_base:           per cpu clock base
127  * @index:              clock type index for per_cpu support when moving a
128  *                      timer to a base on another cpu.
129  * @clockid:            clock id for per_cpu support
130  * @active:             red black tree root node for the active timers
131  * @get_time:           function to retrieve the current time of the clock
132  * @offset:             offset of this clock to the monotonic base
133  */
134 struct hrtimer_clock_base {
135         struct hrtimer_cpu_base *cpu_base;
136         int                     index;
137         clockid_t               clockid;
138         struct timerqueue_head  active;
139         ktime_t                 (*get_time)(void);
140         ktime_t                 offset;
141 } __attribute__((__aligned__(HRTIMER_CLOCK_BASE_ALIGN)));
142
143 enum  hrtimer_base_type {
144         HRTIMER_BASE_MONOTONIC,
145         HRTIMER_BASE_REALTIME,
146         HRTIMER_BASE_BOOTTIME,
147         HRTIMER_BASE_TAI,
148         HRTIMER_MAX_CLOCK_BASES,
149 };
150
151 /*
152  * struct hrtimer_cpu_base - the per cpu clock bases
153  * @lock:               lock protecting the base and associated clock bases
154  *                      and timers
155  * @seq:                seqcount around __run_hrtimer
156  * @running:            pointer to the currently running hrtimer
157  * @cpu:                cpu number
158  * @active_bases:       Bitfield to mark bases with active timers
159  * @clock_was_set_seq:  Sequence counter of clock was set events
160  * @migration_enabled:  The migration of hrtimers to other cpus is enabled
161  * @nohz_active:        The nohz functionality is enabled
162  * @expires_next:       absolute time of the next event which was scheduled
163  *                      via clock_set_next_event()
164  * @next_timer:         Pointer to the first expiring timer
165  * @in_hrtirq:          hrtimer_interrupt() is currently executing
166  * @hres_active:        State of high resolution mode
167  * @hang_detected:      The last hrtimer interrupt detected a hang
168  * @nr_events:          Total number of hrtimer interrupt events
169  * @nr_retries:         Total number of hrtimer interrupt retries
170  * @nr_hangs:           Total number of hrtimer interrupt hangs
171  * @max_hang_time:      Maximum time spent in hrtimer_interrupt
172  * @clock_base:         array of clock bases for this cpu
173  *
174  * Note: next_timer is just an optimization for __remove_hrtimer().
175  *       Do not dereference the pointer because it is not reliable on
176  *       cross cpu removals.
177  */
178 struct hrtimer_cpu_base {
179         raw_spinlock_t                  lock;
180         seqcount_t                      seq;
181         struct hrtimer                  *running;
182         unsigned int                    cpu;
183         unsigned int                    active_bases;
184         unsigned int                    clock_was_set_seq;
185         bool                            migration_enabled;
186         bool                            nohz_active;
187 #ifdef CONFIG_HIGH_RES_TIMERS
188         unsigned int                    in_hrtirq       : 1,
189                                         hres_active     : 1,
190                                         hang_detected   : 1;
191         ktime_t                         expires_next;
192         struct hrtimer                  *next_timer;
193         unsigned int                    nr_events;
194         unsigned int                    nr_retries;
195         unsigned int                    nr_hangs;
196         unsigned int                    max_hang_time;
197 #endif
198         struct hrtimer_clock_base       clock_base[HRTIMER_MAX_CLOCK_BASES];
199 } ____cacheline_aligned;
200
201 static inline void hrtimer_set_expires(struct hrtimer *timer, ktime_t time)
202 {
203         BUILD_BUG_ON(sizeof(struct hrtimer_clock_base) > HRTIMER_CLOCK_BASE_ALIGN);
204
205         timer->node.expires = time;
206         timer->_softexpires = time;
207 }
208
209 static inline void hrtimer_set_expires_range(struct hrtimer *timer, ktime_t time, ktime_t delta)
210 {
211         timer->_softexpires = time;
212         timer->node.expires = ktime_add_safe(time, delta);
213 }
214
215 static inline void hrtimer_set_expires_range_ns(struct hrtimer *timer, ktime_t time, u64 delta)
216 {
217         timer->_softexpires = time;
218         timer->node.expires = ktime_add_safe(time, ns_to_ktime(delta));
219 }
220
221 static inline void hrtimer_set_expires_tv64(struct hrtimer *timer, s64 tv64)
222 {
223         timer->node.expires.tv64 = tv64;
224         timer->_softexpires.tv64 = tv64;
225 }
226
227 static inline void hrtimer_add_expires(struct hrtimer *timer, ktime_t time)
228 {
229         timer->node.expires = ktime_add_safe(timer->node.expires, time);
230         timer->_softexpires = ktime_add_safe(timer->_softexpires, time);
231 }
232
233 static inline void hrtimer_add_expires_ns(struct hrtimer *timer, u64 ns)
234 {
235         timer->node.expires = ktime_add_ns(timer->node.expires, ns);
236         timer->_softexpires = ktime_add_ns(timer->_softexpires, ns);
237 }
238
239 static inline ktime_t hrtimer_get_expires(const struct hrtimer *timer)
240 {
241         return timer->node.expires;
242 }
243
244 static inline ktime_t hrtimer_get_softexpires(const struct hrtimer *timer)
245 {
246         return timer->_softexpires;
247 }
248
249 static inline s64 hrtimer_get_expires_tv64(const struct hrtimer *timer)
250 {
251         return timer->node.expires.tv64;
252 }
253 static inline s64 hrtimer_get_softexpires_tv64(const struct hrtimer *timer)
254 {
255         return timer->_softexpires.tv64;
256 }
257
258 static inline s64 hrtimer_get_expires_ns(const struct hrtimer *timer)
259 {
260         return ktime_to_ns(timer->node.expires);
261 }
262
263 static inline ktime_t hrtimer_expires_remaining(const struct hrtimer *timer)
264 {
265         return ktime_sub(timer->node.expires, timer->base->get_time());
266 }
267
268 static inline ktime_t hrtimer_cb_get_time(struct hrtimer *timer)
269 {
270         return timer->base->get_time();
271 }
272
273 #ifdef CONFIG_HIGH_RES_TIMERS
274 struct clock_event_device;
275
276 extern void hrtimer_interrupt(struct clock_event_device *dev);
277
278 static inline int hrtimer_is_hres_active(struct hrtimer *timer)
279 {
280         return timer->base->cpu_base->hres_active;
281 }
282
283 extern void hrtimer_peek_ahead_timers(void);
284
285 /*
286  * The resolution of the clocks. The resolution value is returned in
287  * the clock_getres() system call to give application programmers an
288  * idea of the (in)accuracy of timers. Timer values are rounded up to
289  * this resolution values.
290  */
291 # define HIGH_RES_NSEC          1
292 # define KTIME_HIGH_RES         (ktime_t) { .tv64 = HIGH_RES_NSEC }
293 # define MONOTONIC_RES_NSEC     HIGH_RES_NSEC
294 # define KTIME_MONOTONIC_RES    KTIME_HIGH_RES
295
296 extern void clock_was_set_delayed(void);
297
298 extern unsigned int hrtimer_resolution;
299
300 #else
301
302 # define MONOTONIC_RES_NSEC     LOW_RES_NSEC
303 # define KTIME_MONOTONIC_RES    KTIME_LOW_RES
304
305 #define hrtimer_resolution      (unsigned int)LOW_RES_NSEC
306
307 static inline void hrtimer_peek_ahead_timers(void) { }
308
309 static inline int hrtimer_is_hres_active(struct hrtimer *timer)
310 {
311         return 0;
312 }
313
314 static inline void clock_was_set_delayed(void) { }
315
316 #endif
317
318 static inline ktime_t
319 __hrtimer_expires_remaining_adjusted(const struct hrtimer *timer, ktime_t now)
320 {
321         ktime_t rem = ktime_sub(timer->node.expires, now);
322
323         /*
324          * Adjust relative timers for the extra we added in
325          * hrtimer_start_range_ns() to prevent short timeouts.
326          */
327         if (IS_ENABLED(CONFIG_TIME_LOW_RES) && timer->is_rel)
328                 rem.tv64 -= hrtimer_resolution;
329         return rem;
330 }
331
332 static inline ktime_t
333 hrtimer_expires_remaining_adjusted(const struct hrtimer *timer)
334 {
335         return __hrtimer_expires_remaining_adjusted(timer,
336                                                     timer->base->get_time());
337 }
338
339 extern void clock_was_set(void);
340 #ifdef CONFIG_TIMERFD
341 extern void timerfd_clock_was_set(void);
342 #else
343 static inline void timerfd_clock_was_set(void) { }
344 #endif
345 extern void hrtimers_resume(void);
346
347 DECLARE_PER_CPU(struct tick_device, tick_cpu_device);
348
349
350 /* Exported timer functions: */
351
352 /* To be used from cpusets, only */
353 extern void hrtimer_quiesce_cpu(void *cpup);
354
355 /* Initialize timers: */
356 extern void hrtimer_init(struct hrtimer *timer, clockid_t which_clock,
357                          enum hrtimer_mode mode);
358
359 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
360 extern void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t which_clock,
361                                   enum hrtimer_mode mode);
362
363 extern void destroy_hrtimer_on_stack(struct hrtimer *timer);
364 #else
365 static inline void hrtimer_init_on_stack(struct hrtimer *timer,
366                                          clockid_t which_clock,
367                                          enum hrtimer_mode mode)
368 {
369         hrtimer_init(timer, which_clock, mode);
370 }
371 static inline void destroy_hrtimer_on_stack(struct hrtimer *timer) { }
372 #endif
373
374 /* Basic timer operations: */
375 extern void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
376                                    u64 range_ns, const enum hrtimer_mode mode);
377
378 /**
379  * hrtimer_start - (re)start an hrtimer on the current CPU
380  * @timer:      the timer to be added
381  * @tim:        expiry time
382  * @mode:       expiry mode: absolute (HRTIMER_MODE_ABS) or
383  *              relative (HRTIMER_MODE_REL)
384  */
385 static inline void hrtimer_start(struct hrtimer *timer, ktime_t tim,
386                                  const enum hrtimer_mode mode)
387 {
388         hrtimer_start_range_ns(timer, tim, 0, mode);
389 }
390
391 extern int hrtimer_cancel(struct hrtimer *timer);
392 extern int hrtimer_try_to_cancel(struct hrtimer *timer);
393
394 static inline void hrtimer_start_expires(struct hrtimer *timer,
395                                          enum hrtimer_mode mode)
396 {
397         u64 delta;
398         ktime_t soft, hard;
399         soft = hrtimer_get_softexpires(timer);
400         hard = hrtimer_get_expires(timer);
401         delta = ktime_to_ns(ktime_sub(hard, soft));
402         hrtimer_start_range_ns(timer, soft, delta, mode);
403 }
404
405 static inline void hrtimer_restart(struct hrtimer *timer)
406 {
407         hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
408 }
409
410 /* Query timers: */
411 extern ktime_t __hrtimer_get_remaining(const struct hrtimer *timer, bool adjust);
412
413 static inline ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
414 {
415         return __hrtimer_get_remaining(timer, false);
416 }
417
418 extern u64 hrtimer_get_next_event(void);
419
420 extern bool hrtimer_active(const struct hrtimer *timer);
421
422 /*
423  * Helper function to check, whether the timer is on one of the queues
424  */
425 static inline int hrtimer_is_queued(struct hrtimer *timer)
426 {
427         return timer->state & HRTIMER_STATE_ENQUEUED;
428 }
429
430 /*
431  * Helper function to check, whether the timer is running the callback
432  * function
433  */
434 static inline int hrtimer_callback_running(struct hrtimer *timer)
435 {
436         return timer->base->cpu_base->running == timer;
437 }
438
439 /* Forward a hrtimer so it expires after now: */
440 extern u64
441 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval);
442
443 /**
444  * hrtimer_forward_now - forward the timer expiry so it expires after now
445  * @timer:      hrtimer to forward
446  * @interval:   the interval to forward
447  *
448  * Forward the timer expiry so it will expire after the current time
449  * of the hrtimer clock base. Returns the number of overruns.
450  *
451  * Can be safely called from the callback function of @timer. If
452  * called from other contexts @timer must neither be enqueued nor
453  * running the callback and the caller needs to take care of
454  * serialization.
455  *
456  * Note: This only updates the timer expiry value and does not requeue
457  * the timer.
458  */
459 static inline u64 hrtimer_forward_now(struct hrtimer *timer,
460                                       ktime_t interval)
461 {
462         return hrtimer_forward(timer, timer->base->get_time(), interval);
463 }
464
465 /* Precise sleep: */
466 extern long hrtimer_nanosleep(struct timespec *rqtp,
467                               struct timespec __user *rmtp,
468                               const enum hrtimer_mode mode,
469                               const clockid_t clockid);
470 extern long hrtimer_nanosleep_restart(struct restart_block *restart_block);
471
472 extern void hrtimer_init_sleeper(struct hrtimer_sleeper *sl,
473                                  struct task_struct *tsk);
474
475 extern int schedule_hrtimeout_range(ktime_t *expires, u64 delta,
476                                                 const enum hrtimer_mode mode);
477 extern int schedule_hrtimeout_range_clock(ktime_t *expires,
478                                           u64 delta,
479                                           const enum hrtimer_mode mode,
480                                           int clock);
481 extern int schedule_hrtimeout(ktime_t *expires, const enum hrtimer_mode mode);
482
483 /* Soft interrupt function to run the hrtimer queues: */
484 extern void hrtimer_run_queues(void);
485
486 /* Bootup initialization: */
487 extern void __init hrtimers_init(void);
488
489 /* Show pending timers: */
490 extern void sysrq_timer_list_show(void);
491
492 #endif