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