]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - kernel/time/alarmtimer.c
timers: Remove delayed irqwork from alarmtimers implementation
[mirror_ubuntu-bionic-kernel.git] / kernel / time / alarmtimer.c
CommitLineData
ff3ead96
JS
1/*
2 * Alarmtimer interface
3 *
4 * This interface provides a timer which is similarto hrtimers,
5 * but triggers a RTC alarm if the box is suspend.
6 *
7 * This interface is influenced by the Android RTC Alarm timer
8 * interface.
9 *
10 * Copyright (C) 2010 IBM Corperation
11 *
12 * Author: John Stultz <john.stultz@linaro.org>
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
17 */
18#include <linux/time.h>
19#include <linux/hrtimer.h>
20#include <linux/timerqueue.h>
21#include <linux/rtc.h>
22#include <linux/alarmtimer.h>
23#include <linux/mutex.h>
24#include <linux/platform_device.h>
25#include <linux/posix-timers.h>
26#include <linux/workqueue.h>
27#include <linux/freezer.h>
28
180bf812
JS
29/**
30 * struct alarm_base - Alarm timer bases
31 * @lock: Lock for syncrhonized access to the base
32 * @timerqueue: Timerqueue head managing the list of events
33 * @timer: hrtimer used to schedule events while running
34 * @gettime: Function to read the time correlating to the base
35 * @base_clockid: clockid for the base
180bf812 36 */
ff3ead96
JS
37static struct alarm_base {
38 spinlock_t lock;
39 struct timerqueue_head timerqueue;
40 struct hrtimer timer;
41 ktime_t (*gettime)(void);
42 clockid_t base_clockid;
ff3ead96
JS
43} alarm_bases[ALARM_NUMTYPE];
44
180bf812 45/* rtc timer and device for setting alarm wakeups at suspend */
ff3ead96
JS
46static struct rtc_timer rtctimer;
47static struct rtc_device *rtcdev;
48
180bf812 49/* freezer delta & lock used to handle clock_nanosleep triggered wakeups */
ff3ead96
JS
50static ktime_t freezer_delta;
51static DEFINE_SPINLOCK(freezer_delta_lock);
52
53
180bf812 54/**
ff3ead96
JS
55 * alarmtimer_enqueue - Adds an alarm timer to an alarm_base timerqueue
56 * @base: pointer to the base where the timer is being run
57 * @alarm: pointer to alarm being enqueued.
58 *
59 * Adds alarm to a alarm_base timerqueue and if necessary sets
60 * an hrtimer to run.
61 *
62 * Must hold base->lock when calling.
63 */
64static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm)
65{
66 timerqueue_add(&base->timerqueue, &alarm->node);
67 if (&alarm->node == timerqueue_getnext(&base->timerqueue)) {
68 hrtimer_try_to_cancel(&base->timer);
69 hrtimer_start(&base->timer, alarm->node.expires,
70 HRTIMER_MODE_ABS);
71 }
72}
73
180bf812 74/**
ff3ead96
JS
75 * alarmtimer_remove - Removes an alarm timer from an alarm_base timerqueue
76 * @base: pointer to the base where the timer is running
77 * @alarm: pointer to alarm being removed
78 *
79 * Removes alarm to a alarm_base timerqueue and if necessary sets
80 * a new timer to run.
81 *
82 * Must hold base->lock when calling.
83 */
84static void alarmtimer_remove(struct alarm_base *base, struct alarm *alarm)
85{
86 struct timerqueue_node *next = timerqueue_getnext(&base->timerqueue);
87
88 timerqueue_del(&base->timerqueue, &alarm->node);
89 if (next == &alarm->node) {
90 hrtimer_try_to_cancel(&base->timer);
91 next = timerqueue_getnext(&base->timerqueue);
92 if (!next)
93 return;
94 hrtimer_start(&base->timer, next->expires, HRTIMER_MODE_ABS);
95 }
96}
97
7068b7a1 98
180bf812 99/**
7068b7a1
JS
100 * alarmtimer_fired - Handles alarm hrtimer being fired.
101 * @timer: pointer to hrtimer being run
ff3ead96 102 *
180bf812
JS
103 * When a alarm timer fires, this runs through the timerqueue to
104 * see which alarms expired, and runs those. If there are more alarm
105 * timers queued for the future, we set the hrtimer to fire when
106 * when the next future alarm timer expires.
ff3ead96 107 */
7068b7a1 108static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
ff3ead96 109{
7068b7a1 110 struct alarm_base *base = container_of(timer, struct alarm_base, timer);
ff3ead96
JS
111 struct timerqueue_node *next;
112 unsigned long flags;
113 ktime_t now;
7068b7a1 114 int ret = HRTIMER_NORESTART;
ff3ead96
JS
115
116 spin_lock_irqsave(&base->lock, flags);
117 now = base->gettime();
118 while ((next = timerqueue_getnext(&base->timerqueue))) {
119 struct alarm *alarm;
120 ktime_t expired = next->expires;
121
122 if (expired.tv64 >= now.tv64)
123 break;
124
125 alarm = container_of(next, struct alarm, node);
126
127 timerqueue_del(&base->timerqueue, &alarm->node);
128 alarm->enabled = 0;
129 /* Re-add periodic timers */
130 if (alarm->period.tv64) {
131 alarm->node.expires = ktime_add(expired, alarm->period);
132 timerqueue_add(&base->timerqueue, &alarm->node);
133 alarm->enabled = 1;
134 }
135 spin_unlock_irqrestore(&base->lock, flags);
136 if (alarm->function)
137 alarm->function(alarm);
138 spin_lock_irqsave(&base->lock, flags);
139 }
140
141 if (next) {
7068b7a1
JS
142 hrtimer_set_expires(&base->timer, next->expires);
143 ret = HRTIMER_RESTART;
ff3ead96
JS
144 }
145 spin_unlock_irqrestore(&base->lock, flags);
ff3ead96 146
7068b7a1 147 return ret;
ff3ead96 148
ff3ead96
JS
149}
150
151
180bf812 152/**
ff3ead96
JS
153 * alarmtimer_suspend - Suspend time callback
154 * @dev: unused
155 * @state: unused
156 *
157 * When we are going into suspend, we look through the bases
158 * to see which is the soonest timer to expire. We then
159 * set an rtc timer to fire that far into the future, which
160 * will wake us from suspend.
161 */
162static int alarmtimer_suspend(struct device *dev)
163{
164 struct rtc_time tm;
165 ktime_t min, now;
166 unsigned long flags;
167 int i;
168
169 spin_lock_irqsave(&freezer_delta_lock, flags);
170 min = freezer_delta;
171 freezer_delta = ktime_set(0, 0);
172 spin_unlock_irqrestore(&freezer_delta_lock, flags);
173
174 /* If we have no rtcdev, just return */
175 if (!rtcdev)
176 return 0;
177
178 /* Find the soonest timer to expire*/
179 for (i = 0; i < ALARM_NUMTYPE; i++) {
180 struct alarm_base *base = &alarm_bases[i];
181 struct timerqueue_node *next;
182 ktime_t delta;
183
184 spin_lock_irqsave(&base->lock, flags);
185 next = timerqueue_getnext(&base->timerqueue);
186 spin_unlock_irqrestore(&base->lock, flags);
187 if (!next)
188 continue;
189 delta = ktime_sub(next->expires, base->gettime());
190 if (!min.tv64 || (delta.tv64 < min.tv64))
191 min = delta;
192 }
193 if (min.tv64 == 0)
194 return 0;
195
196 /* XXX - Should we enforce a minimum sleep time? */
197 WARN_ON(min.tv64 < NSEC_PER_SEC);
198
199 /* Setup an rtc timer to fire that far in the future */
200 rtc_timer_cancel(rtcdev, &rtctimer);
201 rtc_read_time(rtcdev, &tm);
202 now = rtc_tm_to_ktime(tm);
203 now = ktime_add(now, min);
204
205 rtc_timer_start(rtcdev, &rtctimer, now, ktime_set(0, 0));
206
207 return 0;
208}
209
210
9a7adcf5
JS
211static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
212{
213 ktime_t delta;
214 unsigned long flags;
215 struct alarm_base *base = &alarm_bases[type];
216
217 delta = ktime_sub(absexp, base->gettime());
218
219 spin_lock_irqsave(&freezer_delta_lock, flags);
220 if (!freezer_delta.tv64 || (delta.tv64 < freezer_delta.tv64))
221 freezer_delta = delta;
222 spin_unlock_irqrestore(&freezer_delta_lock, flags);
223}
224
225
180bf812 226/**
ff3ead96
JS
227 * alarm_init - Initialize an alarm structure
228 * @alarm: ptr to alarm to be initialized
229 * @type: the type of the alarm
230 * @function: callback that is run when the alarm fires
ff3ead96
JS
231 */
232void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
233 void (*function)(struct alarm *))
234{
235 timerqueue_init(&alarm->node);
236 alarm->period = ktime_set(0, 0);
237 alarm->function = function;
238 alarm->type = type;
239 alarm->enabled = 0;
240}
241
180bf812 242/**
ff3ead96
JS
243 * alarm_start - Sets an alarm to fire
244 * @alarm: ptr to alarm to set
245 * @start: time to run the alarm
246 * @period: period at which the alarm will recur
ff3ead96
JS
247 */
248void alarm_start(struct alarm *alarm, ktime_t start, ktime_t period)
249{
250 struct alarm_base *base = &alarm_bases[alarm->type];
251 unsigned long flags;
252
253 spin_lock_irqsave(&base->lock, flags);
254 if (alarm->enabled)
255 alarmtimer_remove(base, alarm);
256 alarm->node.expires = start;
257 alarm->period = period;
258 alarmtimer_enqueue(base, alarm);
259 alarm->enabled = 1;
260 spin_unlock_irqrestore(&base->lock, flags);
261}
262
180bf812 263/**
ff3ead96
JS
264 * alarm_cancel - Tries to cancel an alarm timer
265 * @alarm: ptr to alarm to be canceled
ff3ead96
JS
266 */
267void alarm_cancel(struct alarm *alarm)
268{
269 struct alarm_base *base = &alarm_bases[alarm->type];
270 unsigned long flags;
271
272 spin_lock_irqsave(&base->lock, flags);
273 if (alarm->enabled)
274 alarmtimer_remove(base, alarm);
275 alarm->enabled = 0;
276 spin_unlock_irqrestore(&base->lock, flags);
277}
278
279
180bf812 280/**
9a7adcf5
JS
281 * clock2alarm - helper that converts from clockid to alarmtypes
282 * @clockid: clockid.
9a7adcf5
JS
283 */
284static enum alarmtimer_type clock2alarm(clockid_t clockid)
285{
286 if (clockid == CLOCK_REALTIME_ALARM)
287 return ALARM_REALTIME;
288 if (clockid == CLOCK_BOOTTIME_ALARM)
289 return ALARM_BOOTTIME;
290 return -1;
291}
292
180bf812 293/**
9a7adcf5
JS
294 * alarm_handle_timer - Callback for posix timers
295 * @alarm: alarm that fired
296 *
297 * Posix timer callback for expired alarm timers.
298 */
299static void alarm_handle_timer(struct alarm *alarm)
300{
301 struct k_itimer *ptr = container_of(alarm, struct k_itimer,
302 it.alarmtimer);
303 if (posix_timer_event(ptr, 0) != 0)
304 ptr->it_overrun++;
305}
306
180bf812 307/**
9a7adcf5
JS
308 * alarm_clock_getres - posix getres interface
309 * @which_clock: clockid
310 * @tp: timespec to fill
311 *
312 * Returns the granularity of underlying alarm base clock
313 */
314static int alarm_clock_getres(const clockid_t which_clock, struct timespec *tp)
315{
316 clockid_t baseid = alarm_bases[clock2alarm(which_clock)].base_clockid;
317
318 return hrtimer_get_res(baseid, tp);
319}
320
321/**
322 * alarm_clock_get - posix clock_get interface
323 * @which_clock: clockid
324 * @tp: timespec to fill.
325 *
326 * Provides the underlying alarm base time.
327 */
328static int alarm_clock_get(clockid_t which_clock, struct timespec *tp)
329{
330 struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
331
332 *tp = ktime_to_timespec(base->gettime());
333 return 0;
334}
335
336/**
337 * alarm_timer_create - posix timer_create interface
338 * @new_timer: k_itimer pointer to manage
339 *
340 * Initializes the k_itimer structure.
341 */
342static int alarm_timer_create(struct k_itimer *new_timer)
343{
344 enum alarmtimer_type type;
345 struct alarm_base *base;
346
347 if (!capable(CAP_WAKE_ALARM))
348 return -EPERM;
349
350 type = clock2alarm(new_timer->it_clock);
351 base = &alarm_bases[type];
352 alarm_init(&new_timer->it.alarmtimer, type, alarm_handle_timer);
353 return 0;
354}
355
356/**
357 * alarm_timer_get - posix timer_get interface
358 * @new_timer: k_itimer pointer
359 * @cur_setting: itimerspec data to fill
360 *
361 * Copies the itimerspec data out from the k_itimer
362 */
363static void alarm_timer_get(struct k_itimer *timr,
364 struct itimerspec *cur_setting)
365{
366 cur_setting->it_interval =
367 ktime_to_timespec(timr->it.alarmtimer.period);
368 cur_setting->it_value =
369 ktime_to_timespec(timr->it.alarmtimer.node.expires);
370 return;
371}
372
373/**
374 * alarm_timer_del - posix timer_del interface
375 * @timr: k_itimer pointer to be deleted
376 *
377 * Cancels any programmed alarms for the given timer.
378 */
379static int alarm_timer_del(struct k_itimer *timr)
380{
381 alarm_cancel(&timr->it.alarmtimer);
382 return 0;
383}
384
385/**
386 * alarm_timer_set - posix timer_set interface
387 * @timr: k_itimer pointer to be deleted
388 * @flags: timer flags
389 * @new_setting: itimerspec to be used
390 * @old_setting: itimerspec being replaced
391 *
392 * Sets the timer to new_setting, and starts the timer.
393 */
394static int alarm_timer_set(struct k_itimer *timr, int flags,
395 struct itimerspec *new_setting,
396 struct itimerspec *old_setting)
397{
398 /* Save old values */
399 old_setting->it_interval =
400 ktime_to_timespec(timr->it.alarmtimer.period);
401 old_setting->it_value =
402 ktime_to_timespec(timr->it.alarmtimer.node.expires);
403
404 /* If the timer was already set, cancel it */
405 alarm_cancel(&timr->it.alarmtimer);
406
407 /* start the timer */
408 alarm_start(&timr->it.alarmtimer,
409 timespec_to_ktime(new_setting->it_value),
410 timespec_to_ktime(new_setting->it_interval));
411 return 0;
412}
413
414/**
415 * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep
416 * @alarm: ptr to alarm that fired
417 *
418 * Wakes up the task that set the alarmtimer
419 */
420static void alarmtimer_nsleep_wakeup(struct alarm *alarm)
421{
422 struct task_struct *task = (struct task_struct *)alarm->data;
423
424 alarm->data = NULL;
425 if (task)
426 wake_up_process(task);
427}
428
429/**
430 * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation
431 * @alarm: ptr to alarmtimer
432 * @absexp: absolute expiration time
433 *
434 * Sets the alarm timer and sleeps until it is fired or interrupted.
435 */
436static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp)
437{
438 alarm->data = (void *)current;
439 do {
440 set_current_state(TASK_INTERRUPTIBLE);
441 alarm_start(alarm, absexp, ktime_set(0, 0));
442 if (likely(alarm->data))
443 schedule();
444
445 alarm_cancel(alarm);
446 } while (alarm->data && !signal_pending(current));
447
448 __set_current_state(TASK_RUNNING);
449
450 return (alarm->data == NULL);
451}
452
453
454/**
455 * update_rmtp - Update remaining timespec value
456 * @exp: expiration time
457 * @type: timer type
458 * @rmtp: user pointer to remaining timepsec value
459 *
460 * Helper function that fills in rmtp value with time between
461 * now and the exp value
462 */
463static int update_rmtp(ktime_t exp, enum alarmtimer_type type,
464 struct timespec __user *rmtp)
465{
466 struct timespec rmt;
467 ktime_t rem;
468
469 rem = ktime_sub(exp, alarm_bases[type].gettime());
470
471 if (rem.tv64 <= 0)
472 return 0;
473 rmt = ktime_to_timespec(rem);
474
475 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
476 return -EFAULT;
477
478 return 1;
479
480}
481
482/**
483 * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep
484 * @restart: ptr to restart block
485 *
486 * Handles restarted clock_nanosleep calls
487 */
488static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
489{
490 enum alarmtimer_type type = restart->nanosleep.index;
491 ktime_t exp;
492 struct timespec __user *rmtp;
493 struct alarm alarm;
494 int ret = 0;
495
496 exp.tv64 = restart->nanosleep.expires;
497 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
498
499 if (alarmtimer_do_nsleep(&alarm, exp))
500 goto out;
501
502 if (freezing(current))
503 alarmtimer_freezerset(exp, type);
504
505 rmtp = restart->nanosleep.rmtp;
506 if (rmtp) {
507 ret = update_rmtp(exp, type, rmtp);
508 if (ret <= 0)
509 goto out;
510 }
511
512
513 /* The other values in restart are already filled in */
514 ret = -ERESTART_RESTARTBLOCK;
515out:
516 return ret;
517}
518
519/**
520 * alarm_timer_nsleep - alarmtimer nanosleep
521 * @which_clock: clockid
522 * @flags: determins abstime or relative
523 * @tsreq: requested sleep time (abs or rel)
524 * @rmtp: remaining sleep time saved
525 *
526 * Handles clock_nanosleep calls against _ALARM clockids
527 */
528static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
529 struct timespec *tsreq, struct timespec __user *rmtp)
530{
531 enum alarmtimer_type type = clock2alarm(which_clock);
532 struct alarm alarm;
533 ktime_t exp;
534 int ret = 0;
535 struct restart_block *restart;
536
537 if (!capable(CAP_WAKE_ALARM))
538 return -EPERM;
539
540 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
541
542 exp = timespec_to_ktime(*tsreq);
543 /* Convert (if necessary) to absolute time */
544 if (flags != TIMER_ABSTIME) {
545 ktime_t now = alarm_bases[type].gettime();
546 exp = ktime_add(now, exp);
547 }
548
549 if (alarmtimer_do_nsleep(&alarm, exp))
550 goto out;
551
552 if (freezing(current))
553 alarmtimer_freezerset(exp, type);
554
555 /* abs timers don't set remaining time or restart */
556 if (flags == TIMER_ABSTIME) {
557 ret = -ERESTARTNOHAND;
558 goto out;
559 }
560
561 if (rmtp) {
562 ret = update_rmtp(exp, type, rmtp);
563 if (ret <= 0)
564 goto out;
565 }
566
567 restart = &current_thread_info()->restart_block;
568 restart->fn = alarm_timer_nsleep_restart;
569 restart->nanosleep.index = type;
570 restart->nanosleep.expires = exp.tv64;
571 restart->nanosleep.rmtp = rmtp;
572 ret = -ERESTART_RESTARTBLOCK;
573
574out:
575 return ret;
576}
ff3ead96 577
ff3ead96
JS
578
579/* Suspend hook structures */
580static const struct dev_pm_ops alarmtimer_pm_ops = {
581 .suspend = alarmtimer_suspend,
582};
583
584static struct platform_driver alarmtimer_driver = {
585 .driver = {
586 .name = "alarmtimer",
587 .pm = &alarmtimer_pm_ops,
588 }
589};
590
591/**
592 * alarmtimer_init - Initialize alarm timer code
593 *
594 * This function initializes the alarm bases and registers
595 * the posix clock ids.
596 */
597static int __init alarmtimer_init(void)
598{
599 int error = 0;
600 int i;
9a7adcf5
JS
601 struct k_clock alarm_clock = {
602 .clock_getres = alarm_clock_getres,
603 .clock_get = alarm_clock_get,
604 .timer_create = alarm_timer_create,
605 .timer_set = alarm_timer_set,
606 .timer_del = alarm_timer_del,
607 .timer_get = alarm_timer_get,
608 .nsleep = alarm_timer_nsleep,
609 };
610
611 posix_timers_register_clock(CLOCK_REALTIME_ALARM, &alarm_clock);
612 posix_timers_register_clock(CLOCK_BOOTTIME_ALARM, &alarm_clock);
ff3ead96
JS
613
614 /* Initialize alarm bases */
615 alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME;
616 alarm_bases[ALARM_REALTIME].gettime = &ktime_get_real;
617 alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
618 alarm_bases[ALARM_BOOTTIME].gettime = &ktime_get_boottime;
619 for (i = 0; i < ALARM_NUMTYPE; i++) {
620 timerqueue_init_head(&alarm_bases[i].timerqueue);
621 spin_lock_init(&alarm_bases[i].lock);
622 hrtimer_init(&alarm_bases[i].timer,
623 alarm_bases[i].base_clockid,
624 HRTIMER_MODE_ABS);
625 alarm_bases[i].timer.function = alarmtimer_fired;
ff3ead96
JS
626 }
627 error = platform_driver_register(&alarmtimer_driver);
628 platform_device_register_simple("alarmtimer", -1, NULL, 0);
629
630 return error;
631}
632device_initcall(alarmtimer_init);
633
634/**
635 * has_wakealarm - check rtc device has wakealarm ability
636 * @dev: current device
637 * @name_ptr: name to be returned
638 *
639 * This helper function checks to see if the rtc device can wake
640 * from suspend.
641 */
642static int __init has_wakealarm(struct device *dev, void *name_ptr)
643{
644 struct rtc_device *candidate = to_rtc_device(dev);
645
646 if (!candidate->ops->set_alarm)
647 return 0;
648 if (!device_may_wakeup(candidate->dev.parent))
649 return 0;
650
651 *(const char **)name_ptr = dev_name(dev);
652 return 1;
653}
654
655/**
656 * alarmtimer_init_late - Late initializing of alarmtimer code
657 *
658 * This function locates a rtc device to use for wakealarms.
659 * Run as late_initcall to make sure rtc devices have been
660 * registered.
661 */
662static int __init alarmtimer_init_late(void)
663{
664 char *str;
665
666 /* Find an rtc device and init the rtc_timer */
667 class_find_device(rtc_class, NULL, &str, has_wakealarm);
668 if (str)
669 rtcdev = rtc_class_open(str);
670 if (!rtcdev) {
671 printk(KERN_WARNING "No RTC device found, ALARM timers will"
672 " not wake from suspend");
673 }
674 rtc_timer_init(&rtctimer, NULL, NULL);
675
676 return 0;
677}
678late_initcall(alarmtimer_init_late);