]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - kernel/time/alarmtimer.c
selftests/timens: Check for right timens offsets after fork and exec
[mirror_ubuntu-hirsute-kernel.git] / kernel / time / alarmtimer.c
CommitLineData
35728b82 1// SPDX-License-Identifier: GPL-2.0
ff3ead96
JS
2/*
3 * Alarmtimer interface
4 *
5 * This interface provides a timer which is similarto hrtimers,
6 * but triggers a RTC alarm if the box is suspend.
7 *
8 * This interface is influenced by the Android RTC Alarm timer
9 * interface.
10 *
11 * Copyright (C) 2010 IBM Corperation
12 *
13 * Author: John Stultz <john.stultz@linaro.org>
ff3ead96
JS
14 */
15#include <linux/time.h>
16#include <linux/hrtimer.h>
17#include <linux/timerqueue.h>
18#include <linux/rtc.h>
174cd4b1 19#include <linux/sched/signal.h>
b17b0153 20#include <linux/sched/debug.h>
ff3ead96
JS
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>
edbeda46 27#include <linux/compat.h>
51218298 28#include <linux/module.h>
5a590f35 29#include <linux/time_namespace.h>
ff3ead96 30
bab0aae9
TG
31#include "posix-timers.h"
32
4a057549
BW
33#define CREATE_TRACE_POINTS
34#include <trace/events/alarmtimer.h>
35
180bf812
JS
36/**
37 * struct alarm_base - Alarm timer bases
38 * @lock: Lock for syncrhonized access to the base
39 * @timerqueue: Timerqueue head managing the list of events
41b3b8df 40 * @get_ktime: Function to read the time correlating to the base
2f58bf90 41 * @get_timespec: Function to read the namespace time correlating to the base
180bf812 42 * @base_clockid: clockid for the base
180bf812 43 */
ff3ead96
JS
44static struct alarm_base {
45 spinlock_t lock;
46 struct timerqueue_head timerqueue;
41b3b8df 47 ktime_t (*get_ktime)(void);
2f58bf90 48 void (*get_timespec)(struct timespec64 *tp);
ff3ead96 49 clockid_t base_clockid;
ff3ead96
JS
50} alarm_bases[ALARM_NUMTYPE];
51
b6b3b80f 52#if defined(CONFIG_POSIX_TIMERS) || defined(CONFIG_RTC_CLASS)
4a057549
BW
53/* freezer information to handle clock_nanosleep triggered wakeups */
54static enum alarmtimer_type freezer_alarmtype;
55static ktime_t freezer_expires;
c008ba58
JS
56static ktime_t freezer_delta;
57static DEFINE_SPINLOCK(freezer_delta_lock);
b6b3b80f 58#endif
c008ba58 59
47b4a457 60#ifdef CONFIG_RTC_CLASS
59a93c27
TP
61static struct wakeup_source *ws;
62
180bf812 63/* rtc timer and device for setting alarm wakeups at suspend */
c5e14e76 64static struct rtc_timer rtctimer;
ff3ead96 65static struct rtc_device *rtcdev;
c008ba58 66static DEFINE_SPINLOCK(rtcdev_lock);
ff3ead96 67
c008ba58
JS
68/**
69 * alarmtimer_get_rtcdev - Return selected rtcdevice
70 *
71 * This function returns the rtc device to use for wakealarms.
72 * If one has not already been chosen, it checks to see if a
73 * functional rtc device is available.
74 */
57c498fa 75struct rtc_device *alarmtimer_get_rtcdev(void)
c008ba58 76{
c008ba58
JS
77 unsigned long flags;
78 struct rtc_device *ret;
79
80 spin_lock_irqsave(&rtcdev_lock, flags);
c008ba58
JS
81 ret = rtcdev;
82 spin_unlock_irqrestore(&rtcdev_lock, flags);
83
84 return ret;
85}
71d5d2b7 86EXPORT_SYMBOL_GPL(alarmtimer_get_rtcdev);
8bc0dafb
JS
87
88static int alarmtimer_rtc_add_device(struct device *dev,
89 struct class_interface *class_intf)
90{
91 unsigned long flags;
92 struct rtc_device *rtc = to_rtc_device(dev);
47b4a457 93 struct wakeup_source *__ws;
8bc0dafb
JS
94
95 if (rtcdev)
96 return -EBUSY;
97
98 if (!rtc->ops->set_alarm)
99 return -1;
100 if (!device_may_wakeup(rtc->dev.parent))
101 return -1;
102
c8377adf 103 __ws = wakeup_source_register(dev, "alarmtimer");
47b4a457 104
8bc0dafb
JS
105 spin_lock_irqsave(&rtcdev_lock, flags);
106 if (!rtcdev) {
51218298
AB
107 if (!try_module_get(rtc->owner)) {
108 spin_unlock_irqrestore(&rtcdev_lock, flags);
109 return -1;
110 }
111
8bc0dafb
JS
112 rtcdev = rtc;
113 /* hold a reference so it doesn't go away */
114 get_device(dev);
47b4a457
GU
115 ws = __ws;
116 __ws = NULL;
8bc0dafb
JS
117 }
118 spin_unlock_irqrestore(&rtcdev_lock, flags);
47b4a457
GU
119
120 wakeup_source_unregister(__ws);
121
8bc0dafb
JS
122 return 0;
123}
124
c5e14e76
TG
125static inline void alarmtimer_rtc_timer_init(void)
126{
127 rtc_timer_init(&rtctimer, NULL, NULL);
128}
129
8bc0dafb
JS
130static struct class_interface alarmtimer_rtc_interface = {
131 .add_dev = &alarmtimer_rtc_add_device,
132};
133
4523f6ad 134static int alarmtimer_rtc_interface_setup(void)
8bc0dafb
JS
135{
136 alarmtimer_rtc_interface.class = rtc_class;
4523f6ad
TG
137 return class_interface_register(&alarmtimer_rtc_interface);
138}
139static void alarmtimer_rtc_interface_remove(void)
140{
141 class_interface_unregister(&alarmtimer_rtc_interface);
8bc0dafb 142}
1c6b39ad 143#else
57c498fa 144struct rtc_device *alarmtimer_get_rtcdev(void)
4523f6ad
TG
145{
146 return NULL;
147}
148#define rtcdev (NULL)
149static inline int alarmtimer_rtc_interface_setup(void) { return 0; }
150static inline void alarmtimer_rtc_interface_remove(void) { }
c5e14e76 151static inline void alarmtimer_rtc_timer_init(void) { }
c008ba58 152#endif
ff3ead96 153
180bf812 154/**
ff3ead96
JS
155 * alarmtimer_enqueue - Adds an alarm timer to an alarm_base timerqueue
156 * @base: pointer to the base where the timer is being run
157 * @alarm: pointer to alarm being enqueued.
158 *
dae373be 159 * Adds alarm to a alarm_base timerqueue
ff3ead96
JS
160 *
161 * Must hold base->lock when calling.
162 */
163static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm)
164{
dae373be
JS
165 if (alarm->state & ALARMTIMER_STATE_ENQUEUED)
166 timerqueue_del(&base->timerqueue, &alarm->node);
167
ff3ead96 168 timerqueue_add(&base->timerqueue, &alarm->node);
a28cde81 169 alarm->state |= ALARMTIMER_STATE_ENQUEUED;
ff3ead96
JS
170}
171
180bf812 172/**
a65bcc12 173 * alarmtimer_dequeue - Removes an alarm timer from an alarm_base timerqueue
ff3ead96
JS
174 * @base: pointer to the base where the timer is running
175 * @alarm: pointer to alarm being removed
176 *
dae373be 177 * Removes alarm to a alarm_base timerqueue
ff3ead96
JS
178 *
179 * Must hold base->lock when calling.
180 */
a65bcc12 181static void alarmtimer_dequeue(struct alarm_base *base, struct alarm *alarm)
ff3ead96 182{
a28cde81
JS
183 if (!(alarm->state & ALARMTIMER_STATE_ENQUEUED))
184 return;
185
ff3ead96 186 timerqueue_del(&base->timerqueue, &alarm->node);
a28cde81 187 alarm->state &= ~ALARMTIMER_STATE_ENQUEUED;
ff3ead96
JS
188}
189
7068b7a1 190
180bf812 191/**
7068b7a1
JS
192 * alarmtimer_fired - Handles alarm hrtimer being fired.
193 * @timer: pointer to hrtimer being run
ff3ead96 194 *
180bf812
JS
195 * When a alarm timer fires, this runs through the timerqueue to
196 * see which alarms expired, and runs those. If there are more alarm
197 * timers queued for the future, we set the hrtimer to fire when
198 * when the next future alarm timer expires.
ff3ead96 199 */
7068b7a1 200static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
ff3ead96 201{
dae373be
JS
202 struct alarm *alarm = container_of(timer, struct alarm, timer);
203 struct alarm_base *base = &alarm_bases[alarm->type];
ff3ead96 204 unsigned long flags;
7068b7a1 205 int ret = HRTIMER_NORESTART;
54da23b7 206 int restart = ALARMTIMER_NORESTART;
ff3ead96
JS
207
208 spin_lock_irqsave(&base->lock, flags);
a65bcc12 209 alarmtimer_dequeue(base, alarm);
dae373be 210 spin_unlock_irqrestore(&base->lock, flags);
54da23b7 211
dae373be 212 if (alarm->function)
41b3b8df 213 restart = alarm->function(alarm, base->get_ktime());
ff3ead96 214
dae373be
JS
215 spin_lock_irqsave(&base->lock, flags);
216 if (restart != ALARMTIMER_NORESTART) {
217 hrtimer_set_expires(&alarm->timer, alarm->node.expires);
218 alarmtimer_enqueue(base, alarm);
7068b7a1 219 ret = HRTIMER_RESTART;
ff3ead96
JS
220 }
221 spin_unlock_irqrestore(&base->lock, flags);
ff3ead96 222
41b3b8df 223 trace_alarmtimer_fired(alarm, base->get_ktime());
7068b7a1 224 return ret;
ff3ead96 225
ff3ead96
JS
226}
227
6cffe00f
TP
228ktime_t alarm_expires_remaining(const struct alarm *alarm)
229{
230 struct alarm_base *base = &alarm_bases[alarm->type];
41b3b8df 231 return ktime_sub(alarm->node.expires, base->get_ktime());
6cffe00f 232}
11682a41 233EXPORT_SYMBOL_GPL(alarm_expires_remaining);
6cffe00f 234
472647dc 235#ifdef CONFIG_RTC_CLASS
180bf812 236/**
ff3ead96
JS
237 * alarmtimer_suspend - Suspend time callback
238 * @dev: unused
ff3ead96
JS
239 *
240 * When we are going into suspend, we look through the bases
241 * to see which is the soonest timer to expire. We then
242 * set an rtc timer to fire that far into the future, which
243 * will wake us from suspend.
244 */
245static int alarmtimer_suspend(struct device *dev)
246{
4a057549
BW
247 ktime_t min, now, expires;
248 int i, ret, type;
c008ba58 249 struct rtc_device *rtc;
4a057549
BW
250 unsigned long flags;
251 struct rtc_time tm;
ff3ead96
JS
252
253 spin_lock_irqsave(&freezer_delta_lock, flags);
254 min = freezer_delta;
4a057549
BW
255 expires = freezer_expires;
256 type = freezer_alarmtype;
8b0e1953 257 freezer_delta = 0;
ff3ead96
JS
258 spin_unlock_irqrestore(&freezer_delta_lock, flags);
259
8bc0dafb 260 rtc = alarmtimer_get_rtcdev();
ff3ead96 261 /* If we have no rtcdev, just return */
c008ba58 262 if (!rtc)
ff3ead96
JS
263 return 0;
264
265 /* Find the soonest timer to expire*/
266 for (i = 0; i < ALARM_NUMTYPE; i++) {
267 struct alarm_base *base = &alarm_bases[i];
268 struct timerqueue_node *next;
269 ktime_t delta;
270
271 spin_lock_irqsave(&base->lock, flags);
272 next = timerqueue_getnext(&base->timerqueue);
273 spin_unlock_irqrestore(&base->lock, flags);
274 if (!next)
275 continue;
41b3b8df 276 delta = ktime_sub(next->expires, base->get_ktime());
2456e855 277 if (!min || (delta < min)) {
4a057549 278 expires = next->expires;
ff3ead96 279 min = delta;
4a057549
BW
280 type = i;
281 }
ff3ead96 282 }
2456e855 283 if (min == 0)
ff3ead96
JS
284 return 0;
285
59a93c27
TP
286 if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) {
287 __pm_wakeup_event(ws, 2 * MSEC_PER_SEC);
288 return -EBUSY;
289 }
ff3ead96 290
4a057549
BW
291 trace_alarmtimer_suspend(expires, type);
292
ff3ead96 293 /* Setup an rtc timer to fire that far in the future */
c008ba58
JS
294 rtc_timer_cancel(rtc, &rtctimer);
295 rtc_read_time(rtc, &tm);
ff3ead96
JS
296 now = rtc_tm_to_ktime(tm);
297 now = ktime_add(now, min);
298
59a93c27 299 /* Set alarm, if in the past reject suspend briefly to handle */
8b0e1953 300 ret = rtc_timer_start(rtc, &rtctimer, now, 0);
59a93c27
TP
301 if (ret < 0)
302 __pm_wakeup_event(ws, MSEC_PER_SEC);
303 return ret;
ff3ead96 304}
a0e3213f 305
306static int alarmtimer_resume(struct device *dev)
307{
308 struct rtc_device *rtc;
309
310 rtc = alarmtimer_get_rtcdev();
311 if (rtc)
312 rtc_timer_cancel(rtc, &rtctimer);
313 return 0;
314}
315
472647dc
JS
316#else
317static int alarmtimer_suspend(struct device *dev)
318{
319 return 0;
320}
a0e3213f 321
322static int alarmtimer_resume(struct device *dev)
323{
324 return 0;
325}
472647dc 326#endif
ff3ead96 327
bd031430
TG
328static void
329__alarm_init(struct alarm *alarm, enum alarmtimer_type type,
330 enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
331{
332 timerqueue_init(&alarm->node);
333 alarm->timer.function = alarmtimer_fired;
334 alarm->function = function;
335 alarm->type = type;
336 alarm->state = ALARMTIMER_STATE_INACTIVE;
337}
338
180bf812 339/**
ff3ead96
JS
340 * alarm_init - Initialize an alarm structure
341 * @alarm: ptr to alarm to be initialized
342 * @type: the type of the alarm
343 * @function: callback that is run when the alarm fires
ff3ead96
JS
344 */
345void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
4b41308d 346 enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
ff3ead96 347{
dae373be 348 hrtimer_init(&alarm->timer, alarm_bases[type].base_clockid,
bd031430
TG
349 HRTIMER_MODE_ABS);
350 __alarm_init(alarm, type, function);
ff3ead96 351}
11682a41 352EXPORT_SYMBOL_GPL(alarm_init);
ff3ead96 353
180bf812 354/**
6cffe00f 355 * alarm_start - Sets an absolute alarm to fire
ff3ead96
JS
356 * @alarm: ptr to alarm to set
357 * @start: time to run the alarm
ff3ead96 358 */
b193217e 359void alarm_start(struct alarm *alarm, ktime_t start)
ff3ead96
JS
360{
361 struct alarm_base *base = &alarm_bases[alarm->type];
362 unsigned long flags;
363
364 spin_lock_irqsave(&base->lock, flags);
ff3ead96 365 alarm->node.expires = start;
ff3ead96 366 alarmtimer_enqueue(base, alarm);
b193217e 367 hrtimer_start(&alarm->timer, alarm->node.expires, HRTIMER_MODE_ABS);
ff3ead96 368 spin_unlock_irqrestore(&base->lock, flags);
4a057549 369
41b3b8df 370 trace_alarmtimer_start(alarm, base->get_ktime());
ff3ead96 371}
11682a41 372EXPORT_SYMBOL_GPL(alarm_start);
ff3ead96 373
6cffe00f
TP
374/**
375 * alarm_start_relative - Sets a relative alarm to fire
376 * @alarm: ptr to alarm to set
377 * @start: time relative to now to run the alarm
378 */
b193217e 379void alarm_start_relative(struct alarm *alarm, ktime_t start)
6cffe00f
TP
380{
381 struct alarm_base *base = &alarm_bases[alarm->type];
382
41b3b8df 383 start = ktime_add_safe(start, base->get_ktime());
b193217e 384 alarm_start(alarm, start);
6cffe00f 385}
11682a41 386EXPORT_SYMBOL_GPL(alarm_start_relative);
6cffe00f
TP
387
388void alarm_restart(struct alarm *alarm)
389{
390 struct alarm_base *base = &alarm_bases[alarm->type];
391 unsigned long flags;
392
393 spin_lock_irqsave(&base->lock, flags);
394 hrtimer_set_expires(&alarm->timer, alarm->node.expires);
395 hrtimer_restart(&alarm->timer);
396 alarmtimer_enqueue(base, alarm);
397 spin_unlock_irqrestore(&base->lock, flags);
398}
11682a41 399EXPORT_SYMBOL_GPL(alarm_restart);
6cffe00f 400
180bf812 401/**
9082c465 402 * alarm_try_to_cancel - Tries to cancel an alarm timer
ff3ead96 403 * @alarm: ptr to alarm to be canceled
9082c465
JS
404 *
405 * Returns 1 if the timer was canceled, 0 if it was not running,
406 * and -1 if the callback was running
ff3ead96 407 */
9082c465 408int alarm_try_to_cancel(struct alarm *alarm)
ff3ead96
JS
409{
410 struct alarm_base *base = &alarm_bases[alarm->type];
411 unsigned long flags;
dae373be 412 int ret;
9082c465 413
dae373be
JS
414 spin_lock_irqsave(&base->lock, flags);
415 ret = hrtimer_try_to_cancel(&alarm->timer);
416 if (ret >= 0)
a65bcc12 417 alarmtimer_dequeue(base, alarm);
ff3ead96 418 spin_unlock_irqrestore(&base->lock, flags);
4a057549 419
41b3b8df 420 trace_alarmtimer_cancel(alarm, base->get_ktime());
9082c465 421 return ret;
ff3ead96 422}
11682a41 423EXPORT_SYMBOL_GPL(alarm_try_to_cancel);
ff3ead96
JS
424
425
9082c465
JS
426/**
427 * alarm_cancel - Spins trying to cancel an alarm timer until it is done
428 * @alarm: ptr to alarm to be canceled
429 *
430 * Returns 1 if the timer was canceled, 0 if it was not active.
431 */
432int alarm_cancel(struct alarm *alarm)
433{
434 for (;;) {
435 int ret = alarm_try_to_cancel(alarm);
436 if (ret >= 0)
437 return ret;
51ae3309 438 hrtimer_cancel_wait_running(&alarm->timer);
9082c465
JS
439 }
440}
11682a41 441EXPORT_SYMBOL_GPL(alarm_cancel);
9082c465 442
dce75a8c
JS
443
444u64 alarm_forward(struct alarm *alarm, ktime_t now, ktime_t interval)
445{
446 u64 overrun = 1;
447 ktime_t delta;
448
449 delta = ktime_sub(now, alarm->node.expires);
450
2456e855 451 if (delta < 0)
dce75a8c
JS
452 return 0;
453
2456e855 454 if (unlikely(delta >= interval)) {
dce75a8c
JS
455 s64 incr = ktime_to_ns(interval);
456
457 overrun = ktime_divns(delta, incr);
458
459 alarm->node.expires = ktime_add_ns(alarm->node.expires,
460 incr*overrun);
461
2456e855 462 if (alarm->node.expires > now)
dce75a8c
JS
463 return overrun;
464 /*
465 * This (and the ktime_add() below) is the
466 * correction for exact:
467 */
468 overrun++;
469 }
470
f4781e76 471 alarm->node.expires = ktime_add_safe(alarm->node.expires, interval);
dce75a8c
JS
472 return overrun;
473}
11682a41 474EXPORT_SYMBOL_GPL(alarm_forward);
dce75a8c 475
6cffe00f
TP
476u64 alarm_forward_now(struct alarm *alarm, ktime_t interval)
477{
478 struct alarm_base *base = &alarm_bases[alarm->type];
479
41b3b8df 480 return alarm_forward(alarm, base->get_ktime(), interval);
6cffe00f 481}
11682a41 482EXPORT_SYMBOL_GPL(alarm_forward_now);
dce75a8c 483
d3ba5a9a
CH
484#ifdef CONFIG_POSIX_TIMERS
485
486static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
487{
488 struct alarm_base *base;
489 unsigned long flags;
490 ktime_t delta;
491
492 switch(type) {
493 case ALARM_REALTIME:
494 base = &alarm_bases[ALARM_REALTIME];
495 type = ALARM_REALTIME_FREEZER;
496 break;
497 case ALARM_BOOTTIME:
498 base = &alarm_bases[ALARM_BOOTTIME];
499 type = ALARM_BOOTTIME_FREEZER;
500 break;
501 default:
502 WARN_ONCE(1, "Invalid alarm type: %d\n", type);
503 return;
504 }
505
41b3b8df 506 delta = ktime_sub(absexp, base->get_ktime());
d3ba5a9a
CH
507
508 spin_lock_irqsave(&freezer_delta_lock, flags);
509 if (!freezer_delta || (delta < freezer_delta)) {
510 freezer_delta = delta;
511 freezer_expires = absexp;
512 freezer_alarmtype = type;
513 }
514 spin_unlock_irqrestore(&freezer_delta_lock, flags);
515}
dce75a8c 516
180bf812 517/**
9a7adcf5
JS
518 * clock2alarm - helper that converts from clockid to alarmtypes
519 * @clockid: clockid.
9a7adcf5
JS
520 */
521static enum alarmtimer_type clock2alarm(clockid_t clockid)
522{
523 if (clockid == CLOCK_REALTIME_ALARM)
524 return ALARM_REALTIME;
525 if (clockid == CLOCK_BOOTTIME_ALARM)
526 return ALARM_BOOTTIME;
527 return -1;
528}
529
180bf812 530/**
9a7adcf5
JS
531 * alarm_handle_timer - Callback for posix timers
532 * @alarm: alarm that fired
533 *
534 * Posix timer callback for expired alarm timers.
535 */
4b41308d
JS
536static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm,
537 ktime_t now)
9a7adcf5
JS
538{
539 struct k_itimer *ptr = container_of(alarm, struct k_itimer,
f2c45807 540 it.alarm.alarmtimer);
474e941b 541 enum alarmtimer_restart result = ALARMTIMER_NORESTART;
f2c45807
TG
542 unsigned long flags;
543 int si_private = 0;
474e941b
RL
544
545 spin_lock_irqsave(&ptr->it_lock, flags);
4b41308d 546
f2c45807
TG
547 ptr->it_active = 0;
548 if (ptr->it_interval)
549 si_private = ++ptr->it_requeue_pending;
550
551 if (posix_timer_event(ptr, si_private) && ptr->it_interval) {
552 /*
553 * Handle ignored signals and rearm the timer. This will go
554 * away once we handle ignored signals proper.
555 */
556 ptr->it_overrun += alarm_forward_now(alarm, ptr->it_interval);
557 ++ptr->it_requeue_pending;
558 ptr->it_active = 1;
474e941b 559 result = ALARMTIMER_RESTART;
54da23b7 560 }
474e941b
RL
561 spin_unlock_irqrestore(&ptr->it_lock, flags);
562
563 return result;
9a7adcf5
JS
564}
565
b3db80f7
TG
566/**
567 * alarm_timer_rearm - Posix timer callback for rearming timer
568 * @timr: Pointer to the posixtimer data struct
569 */
570static void alarm_timer_rearm(struct k_itimer *timr)
571{
572 struct alarm *alarm = &timr->it.alarm.alarmtimer;
573
574 timr->it_overrun += alarm_forward_now(alarm, timr->it_interval);
575 alarm_start(alarm, alarm->node.expires);
576}
577
e7561f16
TG
578/**
579 * alarm_timer_forward - Posix timer callback for forwarding timer
580 * @timr: Pointer to the posixtimer data struct
581 * @now: Current time to forward the timer against
582 */
6fec64e1 583static s64 alarm_timer_forward(struct k_itimer *timr, ktime_t now)
e7561f16
TG
584{
585 struct alarm *alarm = &timr->it.alarm.alarmtimer;
586
6fec64e1 587 return alarm_forward(alarm, timr->it_interval, now);
e7561f16
TG
588}
589
d653d845
TG
590/**
591 * alarm_timer_remaining - Posix timer callback to retrieve remaining time
592 * @timr: Pointer to the posixtimer data struct
593 * @now: Current time to calculate against
594 */
595static ktime_t alarm_timer_remaining(struct k_itimer *timr, ktime_t now)
596{
597 struct alarm *alarm = &timr->it.alarm.alarmtimer;
598
07d7e120 599 return ktime_sub(alarm->node.expires, now);
d653d845
TG
600}
601
e344c9e7
TG
602/**
603 * alarm_timer_try_to_cancel - Posix timer callback to cancel a timer
604 * @timr: Pointer to the posixtimer data struct
605 */
606static int alarm_timer_try_to_cancel(struct k_itimer *timr)
607{
608 return alarm_try_to_cancel(&timr->it.alarm.alarmtimer);
609}
610
ec8f954a
TG
611/**
612 * alarm_timer_wait_running - Posix timer callback to wait for a timer
613 * @timr: Pointer to the posixtimer data struct
614 *
615 * Called from the core code when timer cancel detected that the callback
616 * is running. @timr is unlocked and rcu read lock is held to prevent it
617 * from being freed.
618 */
619static void alarm_timer_wait_running(struct k_itimer *timr)
620{
621 hrtimer_cancel_wait_running(&timr->it.alarm.alarmtimer.timer);
622}
623
b3bf6f36
TG
624/**
625 * alarm_timer_arm - Posix timer callback to arm a timer
626 * @timr: Pointer to the posixtimer data struct
627 * @expires: The new expiry time
628 * @absolute: Expiry value is absolute time
629 * @sigev_none: Posix timer does not deliver signals
630 */
631static void alarm_timer_arm(struct k_itimer *timr, ktime_t expires,
632 bool absolute, bool sigev_none)
633{
634 struct alarm *alarm = &timr->it.alarm.alarmtimer;
635 struct alarm_base *base = &alarm_bases[alarm->type];
636
637 if (!absolute)
41b3b8df 638 expires = ktime_add_safe(expires, base->get_ktime());
b3bf6f36
TG
639 if (sigev_none)
640 alarm->node.expires = expires;
641 else
642 alarm_start(&timr->it.alarm.alarmtimer, expires);
643}
644
180bf812 645/**
9a7adcf5
JS
646 * alarm_clock_getres - posix getres interface
647 * @which_clock: clockid
648 * @tp: timespec to fill
649 *
650 * Returns the granularity of underlying alarm base clock
651 */
d2e3e0ca 652static int alarm_clock_getres(const clockid_t which_clock, struct timespec64 *tp)
9a7adcf5 653{
1c6b39ad 654 if (!alarmtimer_get_rtcdev())
98d6f4dd 655 return -EINVAL;
1c6b39ad 656
056a3cac
TG
657 tp->tv_sec = 0;
658 tp->tv_nsec = hrtimer_resolution;
659 return 0;
9a7adcf5
JS
660}
661
662/**
eaf80194 663 * alarm_clock_get_timespec - posix clock_get_timespec interface
9a7adcf5
JS
664 * @which_clock: clockid
665 * @tp: timespec to fill.
666 *
9c71a2e8 667 * Provides the underlying alarm base time in a tasks time namespace.
9a7adcf5 668 */
eaf80194 669static int alarm_clock_get_timespec(clockid_t which_clock, struct timespec64 *tp)
9a7adcf5
JS
670{
671 struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
672
1c6b39ad 673 if (!alarmtimer_get_rtcdev())
98d6f4dd 674 return -EINVAL;
1c6b39ad 675
2f58bf90
AV
676 base->get_timespec(tp);
677
9a7adcf5
JS
678 return 0;
679}
680
9c71a2e8
AV
681/**
682 * alarm_clock_get_ktime - posix clock_get_ktime interface
683 * @which_clock: clockid
684 *
685 * Provides the underlying alarm base time in the root namespace.
686 */
687static ktime_t alarm_clock_get_ktime(clockid_t which_clock)
688{
689 struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
690
691 if (!alarmtimer_get_rtcdev())
692 return -EINVAL;
693
694 return base->get_ktime();
695}
696
9a7adcf5
JS
697/**
698 * alarm_timer_create - posix timer_create interface
699 * @new_timer: k_itimer pointer to manage
700 *
701 * Initializes the k_itimer structure.
702 */
703static int alarm_timer_create(struct k_itimer *new_timer)
704{
705 enum alarmtimer_type type;
9a7adcf5 706
1c6b39ad 707 if (!alarmtimer_get_rtcdev())
f18ddc13 708 return -EOPNOTSUPP;
1c6b39ad 709
9a7adcf5
JS
710 if (!capable(CAP_WAKE_ALARM))
711 return -EPERM;
712
713 type = clock2alarm(new_timer->it_clock);
9e264762 714 alarm_init(&new_timer->it.alarm.alarmtimer, type, alarm_handle_timer);
9a7adcf5
JS
715 return 0;
716}
717
9a7adcf5
JS
718/**
719 * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep
720 * @alarm: ptr to alarm that fired
721 *
722 * Wakes up the task that set the alarmtimer
723 */
4b41308d
JS
724static enum alarmtimer_restart alarmtimer_nsleep_wakeup(struct alarm *alarm,
725 ktime_t now)
9a7adcf5
JS
726{
727 struct task_struct *task = (struct task_struct *)alarm->data;
728
729 alarm->data = NULL;
730 if (task)
731 wake_up_process(task);
4b41308d 732 return ALARMTIMER_NORESTART;
9a7adcf5
JS
733}
734
735/**
736 * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation
737 * @alarm: ptr to alarmtimer
738 * @absexp: absolute expiration time
739 *
740 * Sets the alarm timer and sleeps until it is fired or interrupted.
741 */
15f27ce2
AV
742static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp,
743 enum alarmtimer_type type)
9a7adcf5 744{
edbeda46 745 struct restart_block *restart;
9a7adcf5
JS
746 alarm->data = (void *)current;
747 do {
748 set_current_state(TASK_INTERRUPTIBLE);
9e264762 749 alarm_start(alarm, absexp);
9a7adcf5
JS
750 if (likely(alarm->data))
751 schedule();
752
753 alarm_cancel(alarm);
754 } while (alarm->data && !signal_pending(current));
755
756 __set_current_state(TASK_RUNNING);
757
bd031430
TG
758 destroy_hrtimer_on_stack(&alarm->timer);
759
15f27ce2 760 if (!alarm->data)
9a7adcf5 761 return 0;
9a7adcf5 762
15f27ce2
AV
763 if (freezing(current))
764 alarmtimer_freezerset(absexp, type);
edbeda46
AV
765 restart = &current->restart_block;
766 if (restart->nanosleep.type != TT_NONE) {
c0edd7c9 767 struct timespec64 rmt;
15f27ce2 768 ktime_t rem;
9a7adcf5 769
41b3b8df 770 rem = ktime_sub(absexp, alarm_bases[type].get_ktime());
9a7adcf5 771
15f27ce2
AV
772 if (rem <= 0)
773 return 0;
c0edd7c9 774 rmt = ktime_to_timespec64(rem);
15f27ce2 775
ce41aaf4 776 return nanosleep_copyout(restart, &rmt);
15f27ce2
AV
777 }
778 return -ERESTART_RESTARTBLOCK;
9a7adcf5
JS
779}
780
bd031430
TG
781static void
782alarm_init_on_stack(struct alarm *alarm, enum alarmtimer_type type,
783 enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
784{
785 hrtimer_init_on_stack(&alarm->timer, alarm_bases[type].base_clockid,
786 HRTIMER_MODE_ABS);
787 __alarm_init(alarm, type, function);
788}
789
9a7adcf5
JS
790/**
791 * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep
792 * @restart: ptr to restart block
793 *
794 * Handles restarted clock_nanosleep calls
795 */
796static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
797{
ab8177bc 798 enum alarmtimer_type type = restart->nanosleep.clockid;
15f27ce2 799 ktime_t exp = restart->nanosleep.expires;
9a7adcf5 800 struct alarm alarm;
9a7adcf5 801
bd031430 802 alarm_init_on_stack(&alarm, type, alarmtimer_nsleep_wakeup);
9a7adcf5 803
15f27ce2 804 return alarmtimer_do_nsleep(&alarm, exp, type);
9a7adcf5
JS
805}
806
807/**
808 * alarm_timer_nsleep - alarmtimer nanosleep
809 * @which_clock: clockid
810 * @flags: determins abstime or relative
811 * @tsreq: requested sleep time (abs or rel)
812 * @rmtp: remaining sleep time saved
813 *
814 * Handles clock_nanosleep calls against _ALARM clockids
815 */
816static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
938e7cf2 817 const struct timespec64 *tsreq)
9a7adcf5
JS
818{
819 enum alarmtimer_type type = clock2alarm(which_clock);
15f27ce2 820 struct restart_block *restart = &current->restart_block;
9a7adcf5
JS
821 struct alarm alarm;
822 ktime_t exp;
823 int ret = 0;
9a7adcf5 824
1c6b39ad 825 if (!alarmtimer_get_rtcdev())
f18ddc13 826 return -EOPNOTSUPP;
1c6b39ad 827
16927776
JS
828 if (flags & ~TIMER_ABSTIME)
829 return -EINVAL;
830
9a7adcf5
JS
831 if (!capable(CAP_WAKE_ALARM))
832 return -EPERM;
833
bd031430 834 alarm_init_on_stack(&alarm, type, alarmtimer_nsleep_wakeup);
9a7adcf5 835
ad196384 836 exp = timespec64_to_ktime(*tsreq);
9a7adcf5
JS
837 /* Convert (if necessary) to absolute time */
838 if (flags != TIMER_ABSTIME) {
41b3b8df 839 ktime_t now = alarm_bases[type].get_ktime();
5f936e19
TG
840
841 exp = ktime_add_safe(now, exp);
0b9b9a3b
AV
842 } else {
843 exp = timens_ktime_to_host(which_clock, exp);
9a7adcf5
JS
844 }
845
15f27ce2
AV
846 ret = alarmtimer_do_nsleep(&alarm, exp, type);
847 if (ret != -ERESTART_RESTARTBLOCK)
848 return ret;
9a7adcf5
JS
849
850 /* abs timers don't set remaining time or restart */
15f27ce2
AV
851 if (flags == TIMER_ABSTIME)
852 return -ERESTARTNOHAND;
9a7adcf5 853
9a7adcf5 854 restart->fn = alarm_timer_nsleep_restart;
ab8177bc 855 restart->nanosleep.clockid = type;
2456e855 856 restart->nanosleep.expires = exp;
9a7adcf5
JS
857 return ret;
858}
ff3ead96 859
d3ba5a9a 860const struct k_clock alarm_clock = {
d653d845 861 .clock_getres = alarm_clock_getres,
9c71a2e8 862 .clock_get_ktime = alarm_clock_get_ktime,
eaf80194 863 .clock_get_timespec = alarm_clock_get_timespec,
d653d845 864 .timer_create = alarm_timer_create,
f2c45807
TG
865 .timer_set = common_timer_set,
866 .timer_del = common_timer_del,
867 .timer_get = common_timer_get,
b3bf6f36 868 .timer_arm = alarm_timer_arm,
d653d845
TG
869 .timer_rearm = alarm_timer_rearm,
870 .timer_forward = alarm_timer_forward,
871 .timer_remaining = alarm_timer_remaining,
e344c9e7 872 .timer_try_to_cancel = alarm_timer_try_to_cancel,
ec8f954a 873 .timer_wait_running = alarm_timer_wait_running,
d653d845 874 .nsleep = alarm_timer_nsleep,
d3ba5a9a
CH
875};
876#endif /* CONFIG_POSIX_TIMERS */
877
ff3ead96
JS
878
879/* Suspend hook structures */
880static const struct dev_pm_ops alarmtimer_pm_ops = {
881 .suspend = alarmtimer_suspend,
a0e3213f 882 .resume = alarmtimer_resume,
ff3ead96
JS
883};
884
885static struct platform_driver alarmtimer_driver = {
886 .driver = {
887 .name = "alarmtimer",
888 .pm = &alarmtimer_pm_ops,
889 }
890};
891
5a590f35
AV
892static void get_boottime_timespec(struct timespec64 *tp)
893{
894 ktime_get_boottime_ts64(tp);
895 timens_add_boottime(tp);
896}
897
ff3ead96
JS
898/**
899 * alarmtimer_init - Initialize alarm timer code
900 *
901 * This function initializes the alarm bases and registers
902 * the posix clock ids.
903 */
904static int __init alarmtimer_init(void)
905{
4523f6ad 906 struct platform_device *pdev;
ff3ead96
JS
907 int error = 0;
908 int i;
9a7adcf5 909
c5e14e76 910 alarmtimer_rtc_timer_init();
ad30dfa9 911
ff3ead96
JS
912 /* Initialize alarm bases */
913 alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME;
41b3b8df 914 alarm_bases[ALARM_REALTIME].get_ktime = &ktime_get_real;
2f58bf90 915 alarm_bases[ALARM_REALTIME].get_timespec = ktime_get_real_ts64,
ff3ead96 916 alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
41b3b8df 917 alarm_bases[ALARM_BOOTTIME].get_ktime = &ktime_get_boottime;
5a590f35 918 alarm_bases[ALARM_BOOTTIME].get_timespec = get_boottime_timespec;
ff3ead96
JS
919 for (i = 0; i < ALARM_NUMTYPE; i++) {
920 timerqueue_init_head(&alarm_bases[i].timerqueue);
921 spin_lock_init(&alarm_bases[i].lock);
ff3ead96 922 }
8bc0dafb 923
4523f6ad
TG
924 error = alarmtimer_rtc_interface_setup();
925 if (error)
926 return error;
927
ff3ead96 928 error = platform_driver_register(&alarmtimer_driver);
4523f6ad
TG
929 if (error)
930 goto out_if;
ff3ead96 931
4523f6ad
TG
932 pdev = platform_device_register_simple("alarmtimer", -1, NULL, 0);
933 if (IS_ERR(pdev)) {
934 error = PTR_ERR(pdev);
935 goto out_drv;
936 }
937 return 0;
938
939out_drv:
940 platform_driver_unregister(&alarmtimer_driver);
941out_if:
942 alarmtimer_rtc_interface_remove();
ff3ead96
JS
943 return error;
944}
945device_initcall(alarmtimer_init);