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  */
alarmtimer_get_rtcdev(void)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 
alarmtimer_rtc_add_device(struct device * dev)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 
alarmtimer_rtc_timer_init(void)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 
alarmtimer_rtc_interface_setup(void)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 }
alarmtimer_rtc_interface_remove(void)140 static void alarmtimer_rtc_interface_remove(void)
141 {
142 	class_interface_unregister(&alarmtimer_rtc_interface);
143 }
144 #else
alarmtimer_rtc_interface_setup(void)145 static inline int alarmtimer_rtc_interface_setup(void) { return 0; }
alarmtimer_rtc_interface_remove(void)146 static inline void alarmtimer_rtc_interface_remove(void) { }
alarmtimer_rtc_timer_init(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  */
alarmtimer_enqueue(struct alarm_base * base,struct alarm * alarm)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  */
alarmtimer_dequeue(struct alarm_base * base,struct alarm * alarm)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  */
alarmtimer_fired(struct hrtimer * timer)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 
alarm_expires_remaining(const struct alarm * alarm)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  */
alarmtimer_suspend(struct device * dev)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 
alarmtimer_resume(struct device * dev)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
alarmtimer_suspend(struct device * dev)324 static int alarmtimer_suspend(struct device *dev)
325 {
326 	return 0;
327 }
328 
alarmtimer_resume(struct device * dev)329 static int alarmtimer_resume(struct device *dev)
330 {
331 	return 0;
332 }
333 #endif
334 
335 static void
__alarm_init(struct alarm * alarm,enum alarmtimer_type type,enum alarmtimer_restart (* function)(struct alarm *,ktime_t))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  */
alarm_init(struct alarm * alarm,enum alarmtimer_type type,enum alarmtimer_restart (* function)(struct alarm *,ktime_t))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  */
alarm_start(struct alarm * alarm,ktime_t start)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  */
alarm_start_relative(struct alarm * alarm,ktime_t start)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 
alarm_restart(struct alarm * alarm)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  */
alarm_try_to_cancel(struct alarm * alarm)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  */
alarm_cancel(struct alarm * alarm)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 
alarm_forward(struct alarm * alarm,ktime_t now,ktime_t interval)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 
__alarm_forward_now(struct alarm * alarm,ktime_t interval,bool throttle)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 jiffy 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 
alarm_forward_now(struct alarm * alarm,ktime_t interval)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 
alarmtimer_freezerset(ktime_t absexp,enum alarmtimer_type type)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  */
clock2alarm(clockid_t clockid)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  */
alarm_handle_timer(struct alarm * alarm,ktime_t now)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 
578 	spin_lock_irqsave(&ptr->it_lock, flags);
579 
580 	if (posix_timer_queue_signal(ptr) && ptr->it_interval) {
581 		/*
582 		 * Handle ignored signals and rearm the timer. This will go
583 		 * away once we handle ignored signals proper. Ensure that
584 		 * small intervals cannot starve the system.
585 		 */
586 		ptr->it_overrun += __alarm_forward_now(alarm, ptr->it_interval, true);
587 		++ptr->it_requeue_pending;
588 		ptr->it_active = 1;
589 		result = ALARMTIMER_RESTART;
590 	}
591 	spin_unlock_irqrestore(&ptr->it_lock, flags);
592 
593 	return result;
594 }
595 
596 /**
597  * alarm_timer_rearm - Posix timer callback for rearming timer
598  * @timr:	Pointer to the posixtimer data struct
599  */
alarm_timer_rearm(struct k_itimer * timr)600 static void alarm_timer_rearm(struct k_itimer *timr)
601 {
602 	struct alarm *alarm = &timr->it.alarm.alarmtimer;
603 
604 	timr->it_overrun += alarm_forward_now(alarm, timr->it_interval);
605 	alarm_start(alarm, alarm->node.expires);
606 }
607 
608 /**
609  * alarm_timer_forward - Posix timer callback for forwarding timer
610  * @timr:	Pointer to the posixtimer data struct
611  * @now:	Current time to forward the timer against
612  */
alarm_timer_forward(struct k_itimer * timr,ktime_t now)613 static s64 alarm_timer_forward(struct k_itimer *timr, ktime_t now)
614 {
615 	struct alarm *alarm = &timr->it.alarm.alarmtimer;
616 
617 	return alarm_forward(alarm, timr->it_interval, now);
618 }
619 
620 /**
621  * alarm_timer_remaining - Posix timer callback to retrieve remaining time
622  * @timr:	Pointer to the posixtimer data struct
623  * @now:	Current time to calculate against
624  */
alarm_timer_remaining(struct k_itimer * timr,ktime_t now)625 static ktime_t alarm_timer_remaining(struct k_itimer *timr, ktime_t now)
626 {
627 	struct alarm *alarm = &timr->it.alarm.alarmtimer;
628 
629 	return ktime_sub(alarm->node.expires, now);
630 }
631 
632 /**
633  * alarm_timer_try_to_cancel - Posix timer callback to cancel a timer
634  * @timr:	Pointer to the posixtimer data struct
635  */
alarm_timer_try_to_cancel(struct k_itimer * timr)636 static int alarm_timer_try_to_cancel(struct k_itimer *timr)
637 {
638 	return alarm_try_to_cancel(&timr->it.alarm.alarmtimer);
639 }
640 
641 /**
642  * alarm_timer_wait_running - Posix timer callback to wait for a timer
643  * @timr:	Pointer to the posixtimer data struct
644  *
645  * Called from the core code when timer cancel detected that the callback
646  * is running. @timr is unlocked and rcu read lock is held to prevent it
647  * from being freed.
648  */
alarm_timer_wait_running(struct k_itimer * timr)649 static void alarm_timer_wait_running(struct k_itimer *timr)
650 {
651 	hrtimer_cancel_wait_running(&timr->it.alarm.alarmtimer.timer);
652 }
653 
654 /**
655  * alarm_timer_arm - Posix timer callback to arm a timer
656  * @timr:	Pointer to the posixtimer data struct
657  * @expires:	The new expiry time
658  * @absolute:	Expiry value is absolute time
659  * @sigev_none:	Posix timer does not deliver signals
660  */
alarm_timer_arm(struct k_itimer * timr,ktime_t expires,bool absolute,bool sigev_none)661 static void alarm_timer_arm(struct k_itimer *timr, ktime_t expires,
662 			    bool absolute, bool sigev_none)
663 {
664 	struct alarm *alarm = &timr->it.alarm.alarmtimer;
665 	struct alarm_base *base = &alarm_bases[alarm->type];
666 
667 	if (!absolute)
668 		expires = ktime_add_safe(expires, base->get_ktime());
669 	if (sigev_none)
670 		alarm->node.expires = expires;
671 	else
672 		alarm_start(&timr->it.alarm.alarmtimer, expires);
673 }
674 
675 /**
676  * alarm_clock_getres - posix getres interface
677  * @which_clock: clockid
678  * @tp: timespec to fill
679  *
680  * Returns the granularity of underlying alarm base clock
681  */
alarm_clock_getres(const clockid_t which_clock,struct timespec64 * tp)682 static int alarm_clock_getres(const clockid_t which_clock, struct timespec64 *tp)
683 {
684 	if (!alarmtimer_get_rtcdev())
685 		return -EINVAL;
686 
687 	tp->tv_sec = 0;
688 	tp->tv_nsec = hrtimer_resolution;
689 	return 0;
690 }
691 
692 /**
693  * alarm_clock_get_timespec - posix clock_get_timespec interface
694  * @which_clock: clockid
695  * @tp: timespec to fill.
696  *
697  * Provides the underlying alarm base time in a tasks time namespace.
698  */
alarm_clock_get_timespec(clockid_t which_clock,struct timespec64 * tp)699 static int alarm_clock_get_timespec(clockid_t which_clock, struct timespec64 *tp)
700 {
701 	struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
702 
703 	if (!alarmtimer_get_rtcdev())
704 		return -EINVAL;
705 
706 	base->get_timespec(tp);
707 
708 	return 0;
709 }
710 
711 /**
712  * alarm_clock_get_ktime - posix clock_get_ktime interface
713  * @which_clock: clockid
714  *
715  * Provides the underlying alarm base time in the root namespace.
716  */
alarm_clock_get_ktime(clockid_t which_clock)717 static ktime_t alarm_clock_get_ktime(clockid_t which_clock)
718 {
719 	struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
720 
721 	if (!alarmtimer_get_rtcdev())
722 		return -EINVAL;
723 
724 	return base->get_ktime();
725 }
726 
727 /**
728  * alarm_timer_create - posix timer_create interface
729  * @new_timer: k_itimer pointer to manage
730  *
731  * Initializes the k_itimer structure.
732  */
alarm_timer_create(struct k_itimer * new_timer)733 static int alarm_timer_create(struct k_itimer *new_timer)
734 {
735 	enum  alarmtimer_type type;
736 
737 	if (!alarmtimer_get_rtcdev())
738 		return -EOPNOTSUPP;
739 
740 	if (!capable(CAP_WAKE_ALARM))
741 		return -EPERM;
742 
743 	type = clock2alarm(new_timer->it_clock);
744 	alarm_init(&new_timer->it.alarm.alarmtimer, type, alarm_handle_timer);
745 	return 0;
746 }
747 
748 /**
749  * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep
750  * @alarm: ptr to alarm that fired
751  * @now: time at the timer expiration
752  *
753  * Wakes up the task that set the alarmtimer
754  *
755  * Return: ALARMTIMER_NORESTART
756  */
alarmtimer_nsleep_wakeup(struct alarm * alarm,ktime_t now)757 static enum alarmtimer_restart alarmtimer_nsleep_wakeup(struct alarm *alarm,
758 								ktime_t now)
759 {
760 	struct task_struct *task = alarm->data;
761 
762 	alarm->data = NULL;
763 	if (task)
764 		wake_up_process(task);
765 	return ALARMTIMER_NORESTART;
766 }
767 
768 /**
769  * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation
770  * @alarm: ptr to alarmtimer
771  * @absexp: absolute expiration time
772  * @type: alarm type (BOOTTIME/REALTIME).
773  *
774  * Sets the alarm timer and sleeps until it is fired or interrupted.
775  */
alarmtimer_do_nsleep(struct alarm * alarm,ktime_t absexp,enum alarmtimer_type type)776 static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp,
777 				enum alarmtimer_type type)
778 {
779 	struct restart_block *restart;
780 	alarm->data = (void *)current;
781 	do {
782 		set_current_state(TASK_INTERRUPTIBLE);
783 		alarm_start(alarm, absexp);
784 		if (likely(alarm->data))
785 			schedule();
786 
787 		alarm_cancel(alarm);
788 	} while (alarm->data && !signal_pending(current));
789 
790 	__set_current_state(TASK_RUNNING);
791 
792 	destroy_hrtimer_on_stack(&alarm->timer);
793 
794 	if (!alarm->data)
795 		return 0;
796 
797 	if (freezing(current))
798 		alarmtimer_freezerset(absexp, type);
799 	restart = &current->restart_block;
800 	if (restart->nanosleep.type != TT_NONE) {
801 		struct timespec64 rmt;
802 		ktime_t rem;
803 
804 		rem = ktime_sub(absexp, alarm_bases[type].get_ktime());
805 
806 		if (rem <= 0)
807 			return 0;
808 		rmt = ktime_to_timespec64(rem);
809 
810 		return nanosleep_copyout(restart, &rmt);
811 	}
812 	return -ERESTART_RESTARTBLOCK;
813 }
814 
815 static void
alarm_init_on_stack(struct alarm * alarm,enum alarmtimer_type type,enum alarmtimer_restart (* function)(struct alarm *,ktime_t))816 alarm_init_on_stack(struct alarm *alarm, enum alarmtimer_type type,
817 		    enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
818 {
819 	hrtimer_init_on_stack(&alarm->timer, alarm_bases[type].base_clockid,
820 			      HRTIMER_MODE_ABS);
821 	__alarm_init(alarm, type, function);
822 }
823 
824 /**
825  * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep
826  * @restart: ptr to restart block
827  *
828  * Handles restarted clock_nanosleep calls
829  */
alarm_timer_nsleep_restart(struct restart_block * restart)830 static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
831 {
832 	enum  alarmtimer_type type = restart->nanosleep.clockid;
833 	ktime_t exp = restart->nanosleep.expires;
834 	struct alarm alarm;
835 
836 	alarm_init_on_stack(&alarm, type, alarmtimer_nsleep_wakeup);
837 
838 	return alarmtimer_do_nsleep(&alarm, exp, type);
839 }
840 
841 /**
842  * alarm_timer_nsleep - alarmtimer nanosleep
843  * @which_clock: clockid
844  * @flags: determines abstime or relative
845  * @tsreq: requested sleep time (abs or rel)
846  *
847  * Handles clock_nanosleep calls against _ALARM clockids
848  */
alarm_timer_nsleep(const clockid_t which_clock,int flags,const struct timespec64 * tsreq)849 static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
850 			      const struct timespec64 *tsreq)
851 {
852 	enum  alarmtimer_type type = clock2alarm(which_clock);
853 	struct restart_block *restart = &current->restart_block;
854 	struct alarm alarm;
855 	ktime_t exp;
856 	int ret;
857 
858 	if (!alarmtimer_get_rtcdev())
859 		return -EOPNOTSUPP;
860 
861 	if (flags & ~TIMER_ABSTIME)
862 		return -EINVAL;
863 
864 	if (!capable(CAP_WAKE_ALARM))
865 		return -EPERM;
866 
867 	alarm_init_on_stack(&alarm, type, alarmtimer_nsleep_wakeup);
868 
869 	exp = timespec64_to_ktime(*tsreq);
870 	/* Convert (if necessary) to absolute time */
871 	if (flags != TIMER_ABSTIME) {
872 		ktime_t now = alarm_bases[type].get_ktime();
873 
874 		exp = ktime_add_safe(now, exp);
875 	} else {
876 		exp = timens_ktime_to_host(which_clock, exp);
877 	}
878 
879 	ret = alarmtimer_do_nsleep(&alarm, exp, type);
880 	if (ret != -ERESTART_RESTARTBLOCK)
881 		return ret;
882 
883 	/* abs timers don't set remaining time or restart */
884 	if (flags == TIMER_ABSTIME)
885 		return -ERESTARTNOHAND;
886 
887 	restart->nanosleep.clockid = type;
888 	restart->nanosleep.expires = exp;
889 	set_restart_fn(restart, alarm_timer_nsleep_restart);
890 	return ret;
891 }
892 
893 const struct k_clock alarm_clock = {
894 	.clock_getres		= alarm_clock_getres,
895 	.clock_get_ktime	= alarm_clock_get_ktime,
896 	.clock_get_timespec	= alarm_clock_get_timespec,
897 	.timer_create		= alarm_timer_create,
898 	.timer_set		= common_timer_set,
899 	.timer_del		= common_timer_del,
900 	.timer_get		= common_timer_get,
901 	.timer_arm		= alarm_timer_arm,
902 	.timer_rearm		= alarm_timer_rearm,
903 	.timer_forward		= alarm_timer_forward,
904 	.timer_remaining	= alarm_timer_remaining,
905 	.timer_try_to_cancel	= alarm_timer_try_to_cancel,
906 	.timer_wait_running	= alarm_timer_wait_running,
907 	.nsleep			= alarm_timer_nsleep,
908 };
909 #endif /* CONFIG_POSIX_TIMERS */
910 
911 
912 /* Suspend hook structures */
913 static const struct dev_pm_ops alarmtimer_pm_ops = {
914 	.suspend = alarmtimer_suspend,
915 	.resume = alarmtimer_resume,
916 };
917 
918 static struct platform_driver alarmtimer_driver = {
919 	.driver = {
920 		.name = "alarmtimer",
921 		.pm = &alarmtimer_pm_ops,
922 	}
923 };
924 
get_boottime_timespec(struct timespec64 * tp)925 static void get_boottime_timespec(struct timespec64 *tp)
926 {
927 	ktime_get_boottime_ts64(tp);
928 	timens_add_boottime(tp);
929 }
930 
931 /**
932  * alarmtimer_init - Initialize alarm timer code
933  *
934  * This function initializes the alarm bases and registers
935  * the posix clock ids.
936  */
alarmtimer_init(void)937 static int __init alarmtimer_init(void)
938 {
939 	int error;
940 	int i;
941 
942 	alarmtimer_rtc_timer_init();
943 
944 	/* Initialize alarm bases */
945 	alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME;
946 	alarm_bases[ALARM_REALTIME].get_ktime = &ktime_get_real;
947 	alarm_bases[ALARM_REALTIME].get_timespec = ktime_get_real_ts64;
948 	alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
949 	alarm_bases[ALARM_BOOTTIME].get_ktime = &ktime_get_boottime;
950 	alarm_bases[ALARM_BOOTTIME].get_timespec = get_boottime_timespec;
951 	for (i = 0; i < ALARM_NUMTYPE; i++) {
952 		timerqueue_init_head(&alarm_bases[i].timerqueue);
953 		spin_lock_init(&alarm_bases[i].lock);
954 	}
955 
956 	error = alarmtimer_rtc_interface_setup();
957 	if (error)
958 		return error;
959 
960 	error = platform_driver_register(&alarmtimer_driver);
961 	if (error)
962 		goto out_if;
963 
964 	return 0;
965 out_if:
966 	alarmtimer_rtc_interface_remove();
967 	return error;
968 }
969 device_initcall(alarmtimer_init);
970