hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/kernel/time/alarmtimer.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0
12 /*
23 * Alarmtimer interface
34 *
....@@ -10,10 +11,6 @@
1011 * Copyright (C) 2010 IBM Corperation
1112 *
1213 * Author: John Stultz <john.stultz@linaro.org>
13
- *
14
- * This program is free software; you can redistribute it and/or modify
15
- * it under the terms of the GNU General Public License version 2 as
16
- * published by the Free Software Foundation.
1714 */
1815 #include <linux/time.h>
1916 #include <linux/hrtimer.h>
....@@ -29,23 +26,28 @@
2926 #include <linux/freezer.h>
3027 #include <linux/compat.h>
3128 #include <linux/module.h>
29
+#include <linux/time_namespace.h>
3230
3331 #include "posix-timers.h"
3432
3533 #define CREATE_TRACE_POINTS
3634 #include <trace/events/alarmtimer.h>
3735
36
+#undef CREATE_TRACE_POINTS
37
+#include <trace/hooks/wakeupbypass.h>
3838 /**
3939 * struct alarm_base - Alarm timer bases
4040 * @lock: Lock for syncrhonized access to the base
4141 * @timerqueue: Timerqueue head managing the list of events
42
- * @gettime: Function to read the time correlating to the base
42
+ * @get_ktime: Function to read the time correlating to the base
43
+ * @get_timespec: Function to read the namespace time correlating to the base
4344 * @base_clockid: clockid for the base
4445 */
4546 static struct alarm_base {
4647 spinlock_t lock;
4748 struct timerqueue_head timerqueue;
48
- ktime_t (*gettime)(void);
49
+ ktime_t (*get_ktime)(void);
50
+ void (*get_timespec)(struct timespec64 *tp);
4951 clockid_t base_clockid;
5052 } alarm_bases[ALARM_NUMTYPE];
5153
....@@ -58,8 +60,6 @@
5860 #endif
5961
6062 #ifdef CONFIG_RTC_CLASS
61
-static struct wakeup_source *ws;
62
-
6363 /* rtc timer and device for setting alarm wakeups at suspend */
6464 static struct rtc_timer rtctimer;
6565 static struct rtc_device *rtcdev;
....@@ -69,8 +69,6 @@
6969 * alarmtimer_get_rtcdev - Return selected rtcdevice
7070 *
7171 * This function returns the rtc device to use for wakealarms.
72
- * If one has not already been chosen, it checks to see if a
73
- * functional rtc device is available.
7472 */
7573 struct rtc_device *alarmtimer_get_rtcdev(void)
7674 {
....@@ -90,7 +88,7 @@
9088 {
9189 unsigned long flags;
9290 struct rtc_device *rtc = to_rtc_device(dev);
93
- struct wakeup_source *__ws;
91
+ struct platform_device *pdev;
9492 int ret = 0;
9593
9694 if (rtcdev)
....@@ -101,10 +99,13 @@
10199 if (!device_may_wakeup(rtc->dev.parent))
102100 return -1;
103101
104
- __ws = wakeup_source_register(dev, "alarmtimer");
102
+ pdev = platform_device_register_data(dev, "alarmtimer",
103
+ PLATFORM_DEVID_AUTO, NULL, 0);
104
+ if (!IS_ERR(pdev))
105
+ device_init_wakeup(&pdev->dev, true);
105106
106107 spin_lock_irqsave(&rtcdev_lock, flags);
107
- if (!rtcdev) {
108
+ if (!IS_ERR(pdev) && !rtcdev) {
108109 if (!try_module_get(rtc->owner)) {
109110 ret = -1;
110111 goto unlock;
....@@ -113,13 +114,14 @@
113114 rtcdev = rtc;
114115 /* hold a reference so it doesn't go away */
115116 get_device(dev);
116
- ws = __ws;
117
- __ws = NULL;
117
+ pdev = NULL;
118
+ } else {
119
+ ret = -1;
118120 }
119121 unlock:
120122 spin_unlock_irqrestore(&rtcdev_lock, flags);
121123
122
- wakeup_source_unregister(__ws);
124
+ platform_device_unregister(pdev);
123125
124126 return ret;
125127 }
....@@ -143,11 +145,6 @@
143145 class_interface_unregister(&alarmtimer_rtc_interface);
144146 }
145147 #else
146
-struct rtc_device *alarmtimer_get_rtcdev(void)
147
-{
148
- return NULL;
149
-}
150
-#define rtcdev (NULL)
151148 static inline int alarmtimer_rtc_interface_setup(void) { return 0; }
152149 static inline void alarmtimer_rtc_interface_remove(void) { }
153150 static inline void alarmtimer_rtc_timer_init(void) { }
....@@ -197,7 +194,7 @@
197194 * When a alarm timer fires, this runs through the timerqueue to
198195 * see which alarms expired, and runs those. If there are more alarm
199196 * timers queued for the future, we set the hrtimer to fire when
200
- * when the next future alarm timer expires.
197
+ * the next future alarm timer expires.
201198 */
202199 static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
203200 {
....@@ -212,7 +209,7 @@
212209 spin_unlock_irqrestore(&base->lock, flags);
213210
214211 if (alarm->function)
215
- restart = alarm->function(alarm, base->gettime());
212
+ restart = alarm->function(alarm, base->get_ktime());
216213
217214 spin_lock_irqsave(&base->lock, flags);
218215 if (restart != ALARMTIMER_NORESTART) {
....@@ -222,7 +219,7 @@
222219 }
223220 spin_unlock_irqrestore(&base->lock, flags);
224221
225
- trace_alarmtimer_fired(alarm, base->gettime());
222
+ trace_alarmtimer_fired(alarm, base->get_ktime());
226223 return ret;
227224
228225 }
....@@ -230,7 +227,7 @@
230227 ktime_t alarm_expires_remaining(const struct alarm *alarm)
231228 {
232229 struct alarm_base *base = &alarm_bases[alarm->type];
233
- return ktime_sub(alarm->node.expires, base->gettime());
230
+ return ktime_sub(alarm->node.expires, base->get_ktime());
234231 }
235232 EXPORT_SYMBOL_GPL(alarm_expires_remaining);
236233
....@@ -238,7 +235,6 @@
238235 /**
239236 * alarmtimer_suspend - Suspend time callback
240237 * @dev: unused
241
- * @state: unused
242238 *
243239 * When we are going into suspend, we look through the bases
244240 * to see which is the soonest timer to expire. We then
....@@ -252,6 +248,7 @@
252248 struct rtc_device *rtc;
253249 unsigned long flags;
254250 struct rtc_time tm;
251
+ int wakeup_bypass_enabled = 0;
255252
256253 spin_lock_irqsave(&freezer_delta_lock, flags);
257254 min = freezer_delta;
....@@ -259,6 +256,10 @@
259256 type = freezer_alarmtype;
260257 freezer_delta = 0;
261258 spin_unlock_irqrestore(&freezer_delta_lock, flags);
259
+
260
+ trace_android_vh_wakeup_bypass(&wakeup_bypass_enabled);
261
+ if (wakeup_bypass_enabled)
262
+ return 0;
262263
263264 rtc = alarmtimer_get_rtcdev();
264265 /* If we have no rtcdev, just return */
....@@ -276,7 +277,7 @@
276277 spin_unlock_irqrestore(&base->lock, flags);
277278 if (!next)
278279 continue;
279
- delta = ktime_sub(next->expires, base->gettime());
280
+ delta = ktime_sub(next->expires, base->get_ktime());
280281 if (!min || (delta < min)) {
281282 expires = next->expires;
282283 min = delta;
....@@ -287,7 +288,7 @@
287288 return 0;
288289
289290 if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) {
290
- __pm_wakeup_event(ws, 2 * MSEC_PER_SEC);
291
+ pm_wakeup_event(dev, 2 * MSEC_PER_SEC);
291292 return -EBUSY;
292293 }
293294
....@@ -302,7 +303,7 @@
302303 /* Set alarm, if in the past reject suspend briefly to handle */
303304 ret = rtc_timer_start(rtc, &rtctimer, now, 0);
304305 if (ret < 0)
305
- __pm_wakeup_event(ws, MSEC_PER_SEC);
306
+ pm_wakeup_event(dev, MSEC_PER_SEC);
306307 return ret;
307308 }
308309
....@@ -370,7 +371,7 @@
370371 hrtimer_start(&alarm->timer, alarm->node.expires, HRTIMER_MODE_ABS);
371372 spin_unlock_irqrestore(&base->lock, flags);
372373
373
- trace_alarmtimer_start(alarm, base->gettime());
374
+ trace_alarmtimer_start(alarm, base->get_ktime());
374375 }
375376 EXPORT_SYMBOL_GPL(alarm_start);
376377
....@@ -383,7 +384,7 @@
383384 {
384385 struct alarm_base *base = &alarm_bases[alarm->type];
385386
386
- start = ktime_add_safe(start, base->gettime());
387
+ start = ktime_add_safe(start, base->get_ktime());
387388 alarm_start(alarm, start);
388389 }
389390 EXPORT_SYMBOL_GPL(alarm_start_relative);
....@@ -420,7 +421,7 @@
420421 alarmtimer_dequeue(base, alarm);
421422 spin_unlock_irqrestore(&base->lock, flags);
422423
423
- trace_alarmtimer_cancel(alarm, base->gettime());
424
+ trace_alarmtimer_cancel(alarm, base->get_ktime());
424425 return ret;
425426 }
426427 EXPORT_SYMBOL_GPL(alarm_try_to_cancel);
....@@ -438,7 +439,7 @@
438439 int ret = alarm_try_to_cancel(alarm);
439440 if (ret >= 0)
440441 return ret;
441
- hrtimer_grab_expiry_lock(&alarm->timer);
442
+ hrtimer_cancel_wait_running(&alarm->timer);
442443 }
443444 }
444445 EXPORT_SYMBOL_GPL(alarm_cancel);
....@@ -476,11 +477,35 @@
476477 }
477478 EXPORT_SYMBOL_GPL(alarm_forward);
478479
479
-u64 alarm_forward_now(struct alarm *alarm, ktime_t interval)
480
+static u64 __alarm_forward_now(struct alarm *alarm, ktime_t interval, bool throttle)
480481 {
481482 struct alarm_base *base = &alarm_bases[alarm->type];
483
+ ktime_t now = base->get_ktime();
482484
483
- return alarm_forward(alarm, base->gettime(), interval);
485
+ if (IS_ENABLED(CONFIG_HIGH_RES_TIMERS) && throttle) {
486
+ /*
487
+ * Same issue as with posix_timer_fn(). Timers which are
488
+ * periodic but the signal is ignored can starve the system
489
+ * with a very small interval. The real fix which was
490
+ * promised in the context of posix_timer_fn() never
491
+ * materialized, but someone should really work on it.
492
+ *
493
+ * To prevent DOS fake @now to be 1 jiffie out which keeps
494
+ * the overrun accounting correct but creates an
495
+ * inconsistency vs. timer_gettime(2).
496
+ */
497
+ ktime_t kj = NSEC_PER_SEC / HZ;
498
+
499
+ if (interval < kj)
500
+ now = ktime_add(now, kj);
501
+ }
502
+
503
+ return alarm_forward(alarm, now, interval);
504
+}
505
+
506
+u64 alarm_forward_now(struct alarm *alarm, ktime_t interval)
507
+{
508
+ return __alarm_forward_now(alarm, interval, false);
484509 }
485510 EXPORT_SYMBOL_GPL(alarm_forward_now);
486511
....@@ -506,7 +531,7 @@
506531 return;
507532 }
508533
509
- delta = ktime_sub(absexp, base->gettime());
534
+ delta = ktime_sub(absexp, base->get_ktime());
510535
511536 spin_lock_irqsave(&freezer_delta_lock, flags);
512537 if (!freezer_delta || (delta < freezer_delta)) {
....@@ -554,9 +579,10 @@
554579 if (posix_timer_event(ptr, si_private) && ptr->it_interval) {
555580 /*
556581 * Handle ignored signals and rearm the timer. This will go
557
- * away once we handle ignored signals proper.
582
+ * away once we handle ignored signals proper. Ensure that
583
+ * small intervals cannot starve the system.
558584 */
559
- ptr->it_overrun += alarm_forward_now(alarm, ptr->it_interval);
585
+ ptr->it_overrun += __alarm_forward_now(alarm, ptr->it_interval, true);
560586 ++ptr->it_requeue_pending;
561587 ptr->it_active = 1;
562588 result = ALARMTIMER_RESTART;
....@@ -612,6 +638,19 @@
612638 }
613639
614640 /**
641
+ * alarm_timer_wait_running - Posix timer callback to wait for a timer
642
+ * @timr: Pointer to the posixtimer data struct
643
+ *
644
+ * Called from the core code when timer cancel detected that the callback
645
+ * is running. @timr is unlocked and rcu read lock is held to prevent it
646
+ * from being freed.
647
+ */
648
+static void alarm_timer_wait_running(struct k_itimer *timr)
649
+{
650
+ hrtimer_cancel_wait_running(&timr->it.alarm.alarmtimer.timer);
651
+}
652
+
653
+/**
615654 * alarm_timer_arm - Posix timer callback to arm a timer
616655 * @timr: Pointer to the posixtimer data struct
617656 * @expires: The new expiry time
....@@ -625,7 +664,7 @@
625664 struct alarm_base *base = &alarm_bases[alarm->type];
626665
627666 if (!absolute)
628
- expires = ktime_add_safe(expires, base->gettime());
667
+ expires = ktime_add_safe(expires, base->get_ktime());
629668 if (sigev_none)
630669 alarm->node.expires = expires;
631670 else
....@@ -650,21 +689,38 @@
650689 }
651690
652691 /**
653
- * alarm_clock_get - posix clock_get interface
692
+ * alarm_clock_get_timespec - posix clock_get_timespec interface
654693 * @which_clock: clockid
655694 * @tp: timespec to fill.
656695 *
657
- * Provides the underlying alarm base time.
696
+ * Provides the underlying alarm base time in a tasks time namespace.
658697 */
659
-static int alarm_clock_get(clockid_t which_clock, struct timespec64 *tp)
698
+static int alarm_clock_get_timespec(clockid_t which_clock, struct timespec64 *tp)
660699 {
661700 struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
662701
663702 if (!alarmtimer_get_rtcdev())
664703 return -EINVAL;
665704
666
- *tp = ktime_to_timespec64(base->gettime());
705
+ base->get_timespec(tp);
706
+
667707 return 0;
708
+}
709
+
710
+/**
711
+ * alarm_clock_get_ktime - posix clock_get_ktime interface
712
+ * @which_clock: clockid
713
+ *
714
+ * Provides the underlying alarm base time in the root namespace.
715
+ */
716
+static ktime_t alarm_clock_get_ktime(clockid_t which_clock)
717
+{
718
+ struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
719
+
720
+ if (!alarmtimer_get_rtcdev())
721
+ return -EINVAL;
722
+
723
+ return base->get_ktime();
668724 }
669725
670726 /**
....@@ -740,7 +796,7 @@
740796 struct timespec64 rmt;
741797 ktime_t rem;
742798
743
- rem = ktime_sub(absexp, alarm_bases[type].gettime());
799
+ rem = ktime_sub(absexp, alarm_bases[type].get_ktime());
744800
745801 if (rem <= 0)
746802 return 0;
....@@ -809,9 +865,11 @@
809865 exp = timespec64_to_ktime(*tsreq);
810866 /* Convert (if necessary) to absolute time */
811867 if (flags != TIMER_ABSTIME) {
812
- ktime_t now = alarm_bases[type].gettime();
868
+ ktime_t now = alarm_bases[type].get_ktime();
813869
814870 exp = ktime_add_safe(now, exp);
871
+ } else {
872
+ exp = timens_ktime_to_host(which_clock, exp);
815873 }
816874
817875 ret = alarmtimer_do_nsleep(&alarm, exp, type);
....@@ -830,7 +888,8 @@
830888
831889 const struct k_clock alarm_clock = {
832890 .clock_getres = alarm_clock_getres,
833
- .clock_get = alarm_clock_get,
891
+ .clock_get_ktime = alarm_clock_get_ktime,
892
+ .clock_get_timespec = alarm_clock_get_timespec,
834893 .timer_create = alarm_timer_create,
835894 .timer_set = common_timer_set,
836895 .timer_del = common_timer_del,
....@@ -840,6 +899,7 @@
840899 .timer_forward = alarm_timer_forward,
841900 .timer_remaining = alarm_timer_remaining,
842901 .timer_try_to_cancel = alarm_timer_try_to_cancel,
902
+ .timer_wait_running = alarm_timer_wait_running,
843903 .nsleep = alarm_timer_nsleep,
844904 };
845905 #endif /* CONFIG_POSIX_TIMERS */
....@@ -858,6 +918,12 @@
858918 }
859919 };
860920
921
+static void get_boottime_timespec(struct timespec64 *tp)
922
+{
923
+ ktime_get_boottime_ts64(tp);
924
+ timens_add_boottime(tp);
925
+}
926
+
861927 /**
862928 * alarmtimer_init - Initialize alarm timer code
863929 *
....@@ -866,17 +932,18 @@
866932 */
867933 static int __init alarmtimer_init(void)
868934 {
869
- struct platform_device *pdev;
870
- int error = 0;
935
+ int error;
871936 int i;
872937
873938 alarmtimer_rtc_timer_init();
874939
875940 /* Initialize alarm bases */
876941 alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME;
877
- alarm_bases[ALARM_REALTIME].gettime = &ktime_get_real;
942
+ alarm_bases[ALARM_REALTIME].get_ktime = &ktime_get_real;
943
+ alarm_bases[ALARM_REALTIME].get_timespec = ktime_get_real_ts64;
878944 alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
879
- alarm_bases[ALARM_BOOTTIME].gettime = &ktime_get_boottime;
945
+ alarm_bases[ALARM_BOOTTIME].get_ktime = &ktime_get_boottime;
946
+ alarm_bases[ALARM_BOOTTIME].get_timespec = get_boottime_timespec;
880947 for (i = 0; i < ALARM_NUMTYPE; i++) {
881948 timerqueue_init_head(&alarm_bases[i].timerqueue);
882949 spin_lock_init(&alarm_bases[i].lock);
....@@ -890,15 +957,7 @@
890957 if (error)
891958 goto out_if;
892959
893
- pdev = platform_device_register_simple("alarmtimer", -1, NULL, 0);
894
- if (IS_ERR(pdev)) {
895
- error = PTR_ERR(pdev);
896
- goto out_drv;
897
- }
898960 return 0;
899
-
900
-out_drv:
901
- platform_driver_unregister(&alarmtimer_driver);
902961 out_if:
903962 alarmtimer_rtc_interface_remove();
904963 return error;