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