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