1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/time.h>
16#include <linux/hrtimer.h>
17#include <linux/timerqueue.h>
18#include <linux/rtc.h>
19#include <linux/sched/signal.h>
20#include <linux/sched/debug.h>
21#include <linux/alarmtimer.h>
22#include <linux/mutex.h>
23#include <linux/platform_device.h>
24#include <linux/posix-timers.h>
25#include <linux/workqueue.h>
26#include <linux/freezer.h>
27#include <linux/compat.h>
28#include <linux/module.h>
29
30#include "posix-timers.h"
31
32#define CREATE_TRACE_POINTS
33#include <trace/events/alarmtimer.h>
34
35
36
37
38
39
40
41
42static struct alarm_base {
43 spinlock_t lock;
44 struct timerqueue_head timerqueue;
45 ktime_t (*gettime)(void);
46 clockid_t base_clockid;
47} alarm_bases[ALARM_NUMTYPE];
48
49#if defined(CONFIG_POSIX_TIMERS) || defined(CONFIG_RTC_CLASS)
50
51static enum alarmtimer_type freezer_alarmtype;
52static ktime_t freezer_expires;
53static ktime_t freezer_delta;
54static DEFINE_SPINLOCK(freezer_delta_lock);
55#endif
56
57#ifdef CONFIG_RTC_CLASS
58static struct wakeup_source *ws;
59
60
61static struct rtc_timer rtctimer;
62static struct rtc_device *rtcdev;
63static DEFINE_SPINLOCK(rtcdev_lock);
64
65
66
67
68
69
70
71
72struct rtc_device *alarmtimer_get_rtcdev(void)
73{
74 unsigned long flags;
75 struct rtc_device *ret;
76
77 spin_lock_irqsave(&rtcdev_lock, flags);
78 ret = rtcdev;
79 spin_unlock_irqrestore(&rtcdev_lock, flags);
80
81 return ret;
82}
83EXPORT_SYMBOL_GPL(alarmtimer_get_rtcdev);
84
85static int alarmtimer_rtc_add_device(struct device *dev,
86 struct class_interface *class_intf)
87{
88 unsigned long flags;
89 struct rtc_device *rtc = to_rtc_device(dev);
90 struct wakeup_source *__ws;
91
92 if (rtcdev)
93 return -EBUSY;
94
95 if (!rtc->ops->set_alarm)
96 return -1;
97 if (!device_may_wakeup(rtc->dev.parent))
98 return -1;
99
100 __ws = wakeup_source_register("alarmtimer");
101
102 spin_lock_irqsave(&rtcdev_lock, flags);
103 if (!rtcdev) {
104 if (!try_module_get(rtc->owner)) {
105 spin_unlock_irqrestore(&rtcdev_lock, flags);
106 return -1;
107 }
108
109 rtcdev = rtc;
110
111 get_device(dev);
112 ws = __ws;
113 __ws = NULL;
114 }
115 spin_unlock_irqrestore(&rtcdev_lock, flags);
116
117 wakeup_source_unregister(__ws);
118
119 return 0;
120}
121
122static inline void alarmtimer_rtc_timer_init(void)
123{
124 rtc_timer_init(&rtctimer, NULL, NULL);
125}
126
127static struct class_interface alarmtimer_rtc_interface = {
128 .add_dev = &alarmtimer_rtc_add_device,
129};
130
131static int alarmtimer_rtc_interface_setup(void)
132{
133 alarmtimer_rtc_interface.class = rtc_class;
134 return class_interface_register(&alarmtimer_rtc_interface);
135}
136static void alarmtimer_rtc_interface_remove(void)
137{
138 class_interface_unregister(&alarmtimer_rtc_interface);
139}
140#else
141struct rtc_device *alarmtimer_get_rtcdev(void)
142{
143 return NULL;
144}
145#define rtcdev (NULL)
146static inline int alarmtimer_rtc_interface_setup(void) { return 0; }
147static inline void alarmtimer_rtc_interface_remove(void) { }
148static inline void alarmtimer_rtc_timer_init(void) { }
149#endif
150
151
152
153
154
155
156
157
158
159
160static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm)
161{
162 if (alarm->state & ALARMTIMER_STATE_ENQUEUED)
163 timerqueue_del(&base->timerqueue, &alarm->node);
164
165 timerqueue_add(&base->timerqueue, &alarm->node);
166 alarm->state |= ALARMTIMER_STATE_ENQUEUED;
167}
168
169
170
171
172
173
174
175
176
177
178static void alarmtimer_dequeue(struct alarm_base *base, struct alarm *alarm)
179{
180 if (!(alarm->state & ALARMTIMER_STATE_ENQUEUED))
181 return;
182
183 timerqueue_del(&base->timerqueue, &alarm->node);
184 alarm->state &= ~ALARMTIMER_STATE_ENQUEUED;
185}
186
187
188
189
190
191
192
193
194
195
196
197static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
198{
199 struct alarm *alarm = container_of(timer, struct alarm, timer);
200 struct alarm_base *base = &alarm_bases[alarm->type];
201 unsigned long flags;
202 int ret = HRTIMER_NORESTART;
203 int restart = ALARMTIMER_NORESTART;
204
205 spin_lock_irqsave(&base->lock, flags);
206 alarmtimer_dequeue(base, alarm);
207 spin_unlock_irqrestore(&base->lock, flags);
208
209 if (alarm->function)
210 restart = alarm->function(alarm, base->gettime());
211
212 spin_lock_irqsave(&base->lock, flags);
213 if (restart != ALARMTIMER_NORESTART) {
214 hrtimer_set_expires(&alarm->timer, alarm->node.expires);
215 alarmtimer_enqueue(base, alarm);
216 ret = HRTIMER_RESTART;
217 }
218 spin_unlock_irqrestore(&base->lock, flags);
219
220 trace_alarmtimer_fired(alarm, base->gettime());
221 return ret;
222
223}
224
225ktime_t alarm_expires_remaining(const struct alarm *alarm)
226{
227 struct alarm_base *base = &alarm_bases[alarm->type];
228 return ktime_sub(alarm->node.expires, base->gettime());
229}
230EXPORT_SYMBOL_GPL(alarm_expires_remaining);
231
232#ifdef CONFIG_RTC_CLASS
233
234
235
236
237
238
239
240
241
242
243static int alarmtimer_suspend(struct device *dev)
244{
245 ktime_t min, now, expires;
246 int i, ret, type;
247 struct rtc_device *rtc;
248 unsigned long flags;
249 struct rtc_time tm;
250
251 spin_lock_irqsave(&freezer_delta_lock, flags);
252 min = freezer_delta;
253 expires = freezer_expires;
254 type = freezer_alarmtype;
255 freezer_delta = 0;
256 spin_unlock_irqrestore(&freezer_delta_lock, flags);
257
258 rtc = alarmtimer_get_rtcdev();
259
260 if (!rtc)
261 return 0;
262
263
264 for (i = 0; i < ALARM_NUMTYPE; i++) {
265 struct alarm_base *base = &alarm_bases[i];
266 struct timerqueue_node *next;
267 ktime_t delta;
268
269 spin_lock_irqsave(&base->lock, flags);
270 next = timerqueue_getnext(&base->timerqueue);
271 spin_unlock_irqrestore(&base->lock, flags);
272 if (!next)
273 continue;
274 delta = ktime_sub(next->expires, base->gettime());
275 if (!min || (delta < min)) {
276 expires = next->expires;
277 min = delta;
278 type = i;
279 }
280 }
281 if (min == 0)
282 return 0;
283
284 if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) {
285 __pm_wakeup_event(ws, 2 * MSEC_PER_SEC);
286 return -EBUSY;
287 }
288
289 trace_alarmtimer_suspend(expires, type);
290
291
292 rtc_timer_cancel(rtc, &rtctimer);
293 rtc_read_time(rtc, &tm);
294 now = rtc_tm_to_ktime(tm);
295 now = ktime_add(now, min);
296
297
298 ret = rtc_timer_start(rtc, &rtctimer, now, 0);
299 if (ret < 0)
300 __pm_wakeup_event(ws, MSEC_PER_SEC);
301 return ret;
302}
303
304static int alarmtimer_resume(struct device *dev)
305{
306 struct rtc_device *rtc;
307
308 rtc = alarmtimer_get_rtcdev();
309 if (rtc)
310 rtc_timer_cancel(rtc, &rtctimer);
311 return 0;
312}
313
314#else
315static int alarmtimer_suspend(struct device *dev)
316{
317 return 0;
318}
319
320static int alarmtimer_resume(struct device *dev)
321{
322 return 0;
323}
324#endif
325
326static void
327__alarm_init(struct alarm *alarm, enum alarmtimer_type type,
328 enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
329{
330 timerqueue_init(&alarm->node);
331 alarm->timer.function = alarmtimer_fired;
332 alarm->function = function;
333 alarm->type = type;
334 alarm->state = ALARMTIMER_STATE_INACTIVE;
335}
336
337
338
339
340
341
342
343void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
344 enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
345{
346 hrtimer_init(&alarm->timer, alarm_bases[type].base_clockid,
347 HRTIMER_MODE_ABS);
348 __alarm_init(alarm, type, function);
349}
350EXPORT_SYMBOL_GPL(alarm_init);
351
352
353
354
355
356
357void alarm_start(struct alarm *alarm, ktime_t start)
358{
359 struct alarm_base *base = &alarm_bases[alarm->type];
360 unsigned long flags;
361
362 spin_lock_irqsave(&base->lock, flags);
363 alarm->node.expires = start;
364 alarmtimer_enqueue(base, alarm);
365 hrtimer_start(&alarm->timer, alarm->node.expires, HRTIMER_MODE_ABS);
366 spin_unlock_irqrestore(&base->lock, flags);
367
368 trace_alarmtimer_start(alarm, base->gettime());
369}
370EXPORT_SYMBOL_GPL(alarm_start);
371
372
373
374
375
376
377void alarm_start_relative(struct alarm *alarm, ktime_t start)
378{
379 struct alarm_base *base = &alarm_bases[alarm->type];
380
381 start = ktime_add_safe(start, base->gettime());
382 alarm_start(alarm, start);
383}
384EXPORT_SYMBOL_GPL(alarm_start_relative);
385
386void alarm_restart(struct alarm *alarm)
387{
388 struct alarm_base *base = &alarm_bases[alarm->type];
389 unsigned long flags;
390
391 spin_lock_irqsave(&base->lock, flags);
392 hrtimer_set_expires(&alarm->timer, alarm->node.expires);
393 hrtimer_restart(&alarm->timer);
394 alarmtimer_enqueue(base, alarm);
395 spin_unlock_irqrestore(&base->lock, flags);
396}
397EXPORT_SYMBOL_GPL(alarm_restart);
398
399
400
401
402
403
404
405
406int alarm_try_to_cancel(struct alarm *alarm)
407{
408 struct alarm_base *base = &alarm_bases[alarm->type];
409 unsigned long flags;
410 int ret;
411
412 spin_lock_irqsave(&base->lock, flags);
413 ret = hrtimer_try_to_cancel(&alarm->timer);
414 if (ret >= 0)
415 alarmtimer_dequeue(base, alarm);
416 spin_unlock_irqrestore(&base->lock, flags);
417
418 trace_alarmtimer_cancel(alarm, base->gettime());
419 return ret;
420}
421EXPORT_SYMBOL_GPL(alarm_try_to_cancel);
422
423
424
425
426
427
428
429
430int alarm_cancel(struct alarm *alarm)
431{
432 for (;;) {
433 int ret = alarm_try_to_cancel(alarm);
434 if (ret >= 0)
435 return ret;
436 cpu_relax();
437 }
438}
439EXPORT_SYMBOL_GPL(alarm_cancel);
440
441
442u64 alarm_forward(struct alarm *alarm, ktime_t now, ktime_t interval)
443{
444 u64 overrun = 1;
445 ktime_t delta;
446
447 delta = ktime_sub(now, alarm->node.expires);
448
449 if (delta < 0)
450 return 0;
451
452 if (unlikely(delta >= interval)) {
453 s64 incr = ktime_to_ns(interval);
454
455 overrun = ktime_divns(delta, incr);
456
457 alarm->node.expires = ktime_add_ns(alarm->node.expires,
458 incr*overrun);
459
460 if (alarm->node.expires > now)
461 return overrun;
462
463
464
465
466 overrun++;
467 }
468
469 alarm->node.expires = ktime_add_safe(alarm->node.expires, interval);
470 return overrun;
471}
472EXPORT_SYMBOL_GPL(alarm_forward);
473
474u64 alarm_forward_now(struct alarm *alarm, ktime_t interval)
475{
476 struct alarm_base *base = &alarm_bases[alarm->type];
477
478 return alarm_forward(alarm, base->gettime(), interval);
479}
480EXPORT_SYMBOL_GPL(alarm_forward_now);
481
482#ifdef CONFIG_POSIX_TIMERS
483
484static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
485{
486 struct alarm_base *base;
487 unsigned long flags;
488 ktime_t delta;
489
490 switch(type) {
491 case ALARM_REALTIME:
492 base = &alarm_bases[ALARM_REALTIME];
493 type = ALARM_REALTIME_FREEZER;
494 break;
495 case ALARM_BOOTTIME:
496 base = &alarm_bases[ALARM_BOOTTIME];
497 type = ALARM_BOOTTIME_FREEZER;
498 break;
499 default:
500 WARN_ONCE(1, "Invalid alarm type: %d\n", type);
501 return;
502 }
503
504 delta = ktime_sub(absexp, base->gettime());
505
506 spin_lock_irqsave(&freezer_delta_lock, flags);
507 if (!freezer_delta || (delta < freezer_delta)) {
508 freezer_delta = delta;
509 freezer_expires = absexp;
510 freezer_alarmtype = type;
511 }
512 spin_unlock_irqrestore(&freezer_delta_lock, flags);
513}
514
515
516
517
518
519static enum alarmtimer_type clock2alarm(clockid_t clockid)
520{
521 if (clockid == CLOCK_REALTIME_ALARM)
522 return ALARM_REALTIME;
523 if (clockid == CLOCK_BOOTTIME_ALARM)
524 return ALARM_BOOTTIME;
525 return -1;
526}
527
528
529
530
531
532
533
534static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm,
535 ktime_t now)
536{
537 struct k_itimer *ptr = container_of(alarm, struct k_itimer,
538 it.alarm.alarmtimer);
539 enum alarmtimer_restart result = ALARMTIMER_NORESTART;
540 unsigned long flags;
541 int si_private = 0;
542
543 spin_lock_irqsave(&ptr->it_lock, flags);
544
545 ptr->it_active = 0;
546 if (ptr->it_interval)
547 si_private = ++ptr->it_requeue_pending;
548
549 if (posix_timer_event(ptr, si_private) && ptr->it_interval) {
550
551
552
553
554 ptr->it_overrun += alarm_forward_now(alarm, ptr->it_interval);
555 ++ptr->it_requeue_pending;
556 ptr->it_active = 1;
557 result = ALARMTIMER_RESTART;
558 }
559 spin_unlock_irqrestore(&ptr->it_lock, flags);
560
561 return result;
562}
563
564
565
566
567
568static void alarm_timer_rearm(struct k_itimer *timr)
569{
570 struct alarm *alarm = &timr->it.alarm.alarmtimer;
571
572 timr->it_overrun += alarm_forward_now(alarm, timr->it_interval);
573 alarm_start(alarm, alarm->node.expires);
574}
575
576
577
578
579
580
581static s64 alarm_timer_forward(struct k_itimer *timr, ktime_t now)
582{
583 struct alarm *alarm = &timr->it.alarm.alarmtimer;
584
585 return alarm_forward(alarm, timr->it_interval, now);
586}
587
588
589
590
591
592
593static ktime_t alarm_timer_remaining(struct k_itimer *timr, ktime_t now)
594{
595 struct alarm *alarm = &timr->it.alarm.alarmtimer;
596
597 return ktime_sub(alarm->node.expires, now);
598}
599
600
601
602
603
604static int alarm_timer_try_to_cancel(struct k_itimer *timr)
605{
606 return alarm_try_to_cancel(&timr->it.alarm.alarmtimer);
607}
608
609
610
611
612
613
614
615
616static void alarm_timer_arm(struct k_itimer *timr, ktime_t expires,
617 bool absolute, bool sigev_none)
618{
619 struct alarm *alarm = &timr->it.alarm.alarmtimer;
620 struct alarm_base *base = &alarm_bases[alarm->type];
621
622 if (!absolute)
623 expires = ktime_add_safe(expires, base->gettime());
624 if (sigev_none)
625 alarm->node.expires = expires;
626 else
627 alarm_start(&timr->it.alarm.alarmtimer, expires);
628}
629
630
631
632
633
634
635
636
637static int alarm_clock_getres(const clockid_t which_clock, struct timespec64 *tp)
638{
639 if (!alarmtimer_get_rtcdev())
640 return -EINVAL;
641
642 tp->tv_sec = 0;
643 tp->tv_nsec = hrtimer_resolution;
644 return 0;
645}
646
647
648
649
650
651
652
653
654static int alarm_clock_get(clockid_t which_clock, struct timespec64 *tp)
655{
656 struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
657
658 if (!alarmtimer_get_rtcdev())
659 return -EINVAL;
660
661 *tp = ktime_to_timespec64(base->gettime());
662 return 0;
663}
664
665
666
667
668
669
670
671static int alarm_timer_create(struct k_itimer *new_timer)
672{
673 enum alarmtimer_type type;
674
675 if (!alarmtimer_get_rtcdev())
676 return -ENOTSUPP;
677
678 if (!capable(CAP_WAKE_ALARM))
679 return -EPERM;
680
681 type = clock2alarm(new_timer->it_clock);
682 alarm_init(&new_timer->it.alarm.alarmtimer, type, alarm_handle_timer);
683 return 0;
684}
685
686
687
688
689
690
691
692static enum alarmtimer_restart alarmtimer_nsleep_wakeup(struct alarm *alarm,
693 ktime_t now)
694{
695 struct task_struct *task = (struct task_struct *)alarm->data;
696
697 alarm->data = NULL;
698 if (task)
699 wake_up_process(task);
700 return ALARMTIMER_NORESTART;
701}
702
703
704
705
706
707
708
709
710static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp,
711 enum alarmtimer_type type)
712{
713 struct restart_block *restart;
714 alarm->data = (void *)current;
715 do {
716 set_current_state(TASK_INTERRUPTIBLE);
717 alarm_start(alarm, absexp);
718 if (likely(alarm->data))
719 schedule();
720
721 alarm_cancel(alarm);
722 } while (alarm->data && !signal_pending(current));
723
724 __set_current_state(TASK_RUNNING);
725
726 destroy_hrtimer_on_stack(&alarm->timer);
727
728 if (!alarm->data)
729 return 0;
730
731 if (freezing(current))
732 alarmtimer_freezerset(absexp, type);
733 restart = ¤t->restart_block;
734 if (restart->nanosleep.type != TT_NONE) {
735 struct timespec64 rmt;
736 ktime_t rem;
737
738 rem = ktime_sub(absexp, alarm_bases[type].gettime());
739
740 if (rem <= 0)
741 return 0;
742 rmt = ktime_to_timespec64(rem);
743
744 return nanosleep_copyout(restart, &rmt);
745 }
746 return -ERESTART_RESTARTBLOCK;
747}
748
749static void
750alarm_init_on_stack(struct alarm *alarm, enum alarmtimer_type type,
751 enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
752{
753 hrtimer_init_on_stack(&alarm->timer, alarm_bases[type].base_clockid,
754 HRTIMER_MODE_ABS);
755 __alarm_init(alarm, type, function);
756}
757
758
759
760
761
762
763
764static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
765{
766 enum alarmtimer_type type = restart->nanosleep.clockid;
767 ktime_t exp = restart->nanosleep.expires;
768 struct alarm alarm;
769
770 alarm_init_on_stack(&alarm, type, alarmtimer_nsleep_wakeup);
771
772 return alarmtimer_do_nsleep(&alarm, exp, type);
773}
774
775
776
777
778
779
780
781
782
783
784static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
785 const struct timespec64 *tsreq)
786{
787 enum alarmtimer_type type = clock2alarm(which_clock);
788 struct restart_block *restart = ¤t->restart_block;
789 struct alarm alarm;
790 ktime_t exp;
791 int ret = 0;
792
793 if (!alarmtimer_get_rtcdev())
794 return -ENOTSUPP;
795
796 if (flags & ~TIMER_ABSTIME)
797 return -EINVAL;
798
799 if (!capable(CAP_WAKE_ALARM))
800 return -EPERM;
801
802 alarm_init_on_stack(&alarm, type, alarmtimer_nsleep_wakeup);
803
804 exp = timespec64_to_ktime(*tsreq);
805
806 if (flags != TIMER_ABSTIME) {
807 ktime_t now = alarm_bases[type].gettime();
808
809 exp = ktime_add_safe(now, exp);
810 }
811
812 ret = alarmtimer_do_nsleep(&alarm, exp, type);
813 if (ret != -ERESTART_RESTARTBLOCK)
814 return ret;
815
816
817 if (flags == TIMER_ABSTIME)
818 return -ERESTARTNOHAND;
819
820 restart->fn = alarm_timer_nsleep_restart;
821 restart->nanosleep.clockid = type;
822 restart->nanosleep.expires = exp;
823 return ret;
824}
825
826const struct k_clock alarm_clock = {
827 .clock_getres = alarm_clock_getres,
828 .clock_get = alarm_clock_get,
829 .timer_create = alarm_timer_create,
830 .timer_set = common_timer_set,
831 .timer_del = common_timer_del,
832 .timer_get = common_timer_get,
833 .timer_arm = alarm_timer_arm,
834 .timer_rearm = alarm_timer_rearm,
835 .timer_forward = alarm_timer_forward,
836 .timer_remaining = alarm_timer_remaining,
837 .timer_try_to_cancel = alarm_timer_try_to_cancel,
838 .nsleep = alarm_timer_nsleep,
839};
840#endif
841
842
843
844static const struct dev_pm_ops alarmtimer_pm_ops = {
845 .suspend = alarmtimer_suspend,
846 .resume = alarmtimer_resume,
847};
848
849static struct platform_driver alarmtimer_driver = {
850 .driver = {
851 .name = "alarmtimer",
852 .pm = &alarmtimer_pm_ops,
853 }
854};
855
856
857
858
859
860
861
862static int __init alarmtimer_init(void)
863{
864 struct platform_device *pdev;
865 int error = 0;
866 int i;
867
868 alarmtimer_rtc_timer_init();
869
870
871 alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME;
872 alarm_bases[ALARM_REALTIME].gettime = &ktime_get_real;
873 alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
874 alarm_bases[ALARM_BOOTTIME].gettime = &ktime_get_boottime;
875 for (i = 0; i < ALARM_NUMTYPE; i++) {
876 timerqueue_init_head(&alarm_bases[i].timerqueue);
877 spin_lock_init(&alarm_bases[i].lock);
878 }
879
880 error = alarmtimer_rtc_interface_setup();
881 if (error)
882 return error;
883
884 error = platform_driver_register(&alarmtimer_driver);
885 if (error)
886 goto out_if;
887
888 pdev = platform_device_register_simple("alarmtimer", -1, NULL, 0);
889 if (IS_ERR(pdev)) {
890 error = PTR_ERR(pdev);
891 goto out_drv;
892 }
893 return 0;
894
895out_drv:
896 platform_driver_unregister(&alarmtimer_driver);
897out_if:
898 alarmtimer_rtc_interface_remove();
899 return error;
900}
901device_initcall(alarmtimer_init);
902