]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/base/power/wakeup.c
PM / Sleep: Fix race conditions related to wakeup source timer function
[mirror_ubuntu-artful-kernel.git] / drivers / base / power / wakeup.c
CommitLineData
c125e96f
RW
1/*
2 * drivers/base/power/wakeup.c - System wakeup events framework
3 *
4 * Copyright (c) 2010 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
5 *
6 * This file is released under the GPLv2.
7 */
8
9#include <linux/device.h>
10#include <linux/slab.h>
11#include <linux/sched.h>
12#include <linux/capability.h>
1b6bc32f 13#include <linux/export.h>
c125e96f 14#include <linux/suspend.h>
9c034392
RW
15#include <linux/seq_file.h>
16#include <linux/debugfs.h>
074037ec
RW
17
18#include "power.h"
19
20#define TIMEOUT 100
c125e96f
RW
21
22/*
23 * If set, the suspend/hibernate code will abort transitions to a sleep state
24 * if wakeup events are registered during or immediately before the transition.
25 */
26bool events_check_enabled;
27
023d3779
RW
28/*
29 * Combined counters of registered wakeup events and wakeup events in progress.
30 * They need to be modified together atomically, so it's better to use one
31 * atomic variable to hold them both.
32 */
33static atomic_t combined_event_count = ATOMIC_INIT(0);
34
35#define IN_PROGRESS_BITS (sizeof(int) * 4)
36#define MAX_IN_PROGRESS ((1 << IN_PROGRESS_BITS) - 1)
37
38static void split_counters(unsigned int *cnt, unsigned int *inpr)
39{
40 unsigned int comb = atomic_read(&combined_event_count);
41
42 *cnt = (comb >> IN_PROGRESS_BITS);
43 *inpr = comb & MAX_IN_PROGRESS;
44}
45
46/* A preserved old value of the events counter. */
074037ec 47static unsigned int saved_count;
c125e96f
RW
48
49static DEFINE_SPINLOCK(events_lock);
50
4eb241e5
RW
51static void pm_wakeup_timer_fn(unsigned long data);
52
074037ec
RW
53static LIST_HEAD(wakeup_sources);
54
55/**
56 * wakeup_source_create - Create a struct wakeup_source object.
57 * @name: Name of the new wakeup source.
58 */
59struct wakeup_source *wakeup_source_create(const char *name)
60{
61 struct wakeup_source *ws;
62
63 ws = kzalloc(sizeof(*ws), GFP_KERNEL);
64 if (!ws)
65 return NULL;
66
074037ec
RW
67 if (name)
68 ws->name = kstrdup(name, GFP_KERNEL);
69
70 return ws;
71}
72EXPORT_SYMBOL_GPL(wakeup_source_create);
73
74/**
75 * wakeup_source_destroy - Destroy a struct wakeup_source object.
76 * @ws: Wakeup source to destroy.
d94aff87
RW
77 *
78 * Callers must ensure that __pm_stay_awake() or __pm_wakeup_event() will never
79 * be run in parallel with this function for the same wakeup source object.
074037ec
RW
80 */
81void wakeup_source_destroy(struct wakeup_source *ws)
82{
83 if (!ws)
84 return;
85
d94aff87
RW
86 del_timer_sync(&ws->timer);
87 __pm_relax(ws);
074037ec
RW
88 kfree(ws->name);
89 kfree(ws);
90}
91EXPORT_SYMBOL_GPL(wakeup_source_destroy);
92
93/**
94 * wakeup_source_add - Add given object to the list of wakeup sources.
95 * @ws: Wakeup source object to add to the list.
96 */
97void wakeup_source_add(struct wakeup_source *ws)
98{
99 if (WARN_ON(!ws))
100 return;
101
7c95149b 102 spin_lock_init(&ws->lock);
074037ec
RW
103 setup_timer(&ws->timer, pm_wakeup_timer_fn, (unsigned long)ws);
104 ws->active = false;
105
106 spin_lock_irq(&events_lock);
107 list_add_rcu(&ws->entry, &wakeup_sources);
108 spin_unlock_irq(&events_lock);
074037ec
RW
109}
110EXPORT_SYMBOL_GPL(wakeup_source_add);
111
112/**
113 * wakeup_source_remove - Remove given object from the wakeup sources list.
114 * @ws: Wakeup source object to remove from the list.
115 */
116void wakeup_source_remove(struct wakeup_source *ws)
117{
118 if (WARN_ON(!ws))
119 return;
120
121 spin_lock_irq(&events_lock);
122 list_del_rcu(&ws->entry);
123 spin_unlock_irq(&events_lock);
124 synchronize_rcu();
125}
126EXPORT_SYMBOL_GPL(wakeup_source_remove);
127
128/**
129 * wakeup_source_register - Create wakeup source and add it to the list.
130 * @name: Name of the wakeup source to register.
131 */
132struct wakeup_source *wakeup_source_register(const char *name)
133{
134 struct wakeup_source *ws;
135
136 ws = wakeup_source_create(name);
137 if (ws)
138 wakeup_source_add(ws);
139
140 return ws;
141}
142EXPORT_SYMBOL_GPL(wakeup_source_register);
143
144/**
145 * wakeup_source_unregister - Remove wakeup source from the list and remove it.
146 * @ws: Wakeup source object to unregister.
147 */
148void wakeup_source_unregister(struct wakeup_source *ws)
149{
150 wakeup_source_remove(ws);
151 wakeup_source_destroy(ws);
152}
153EXPORT_SYMBOL_GPL(wakeup_source_unregister);
154
155/**
156 * device_wakeup_attach - Attach a wakeup source object to a device object.
157 * @dev: Device to handle.
158 * @ws: Wakeup source object to attach to @dev.
159 *
160 * This causes @dev to be treated as a wakeup device.
161 */
162static int device_wakeup_attach(struct device *dev, struct wakeup_source *ws)
163{
164 spin_lock_irq(&dev->power.lock);
165 if (dev->power.wakeup) {
166 spin_unlock_irq(&dev->power.lock);
167 return -EEXIST;
168 }
169 dev->power.wakeup = ws;
170 spin_unlock_irq(&dev->power.lock);
171 return 0;
172}
173
174/**
175 * device_wakeup_enable - Enable given device to be a wakeup source.
176 * @dev: Device to handle.
177 *
178 * Create a wakeup source object, register it and attach it to @dev.
179 */
180int device_wakeup_enable(struct device *dev)
181{
182 struct wakeup_source *ws;
183 int ret;
184
185 if (!dev || !dev->power.can_wakeup)
186 return -EINVAL;
187
188 ws = wakeup_source_register(dev_name(dev));
189 if (!ws)
190 return -ENOMEM;
191
192 ret = device_wakeup_attach(dev, ws);
193 if (ret)
194 wakeup_source_unregister(ws);
195
196 return ret;
197}
198EXPORT_SYMBOL_GPL(device_wakeup_enable);
199
200/**
201 * device_wakeup_detach - Detach a device's wakeup source object from it.
202 * @dev: Device to detach the wakeup source object from.
203 *
204 * After it returns, @dev will not be treated as a wakeup device any more.
205 */
206static struct wakeup_source *device_wakeup_detach(struct device *dev)
207{
208 struct wakeup_source *ws;
209
210 spin_lock_irq(&dev->power.lock);
211 ws = dev->power.wakeup;
212 dev->power.wakeup = NULL;
213 spin_unlock_irq(&dev->power.lock);
214 return ws;
215}
216
217/**
218 * device_wakeup_disable - Do not regard a device as a wakeup source any more.
219 * @dev: Device to handle.
220 *
221 * Detach the @dev's wakeup source object from it, unregister this wakeup source
222 * object and destroy it.
223 */
224int device_wakeup_disable(struct device *dev)
225{
226 struct wakeup_source *ws;
227
228 if (!dev || !dev->power.can_wakeup)
229 return -EINVAL;
230
231 ws = device_wakeup_detach(dev);
232 if (ws)
233 wakeup_source_unregister(ws);
234
235 return 0;
236}
237EXPORT_SYMBOL_GPL(device_wakeup_disable);
238
cb8f51bd
RW
239/**
240 * device_set_wakeup_capable - Set/reset device wakeup capability flag.
241 * @dev: Device to handle.
242 * @capable: Whether or not @dev is capable of waking up the system from sleep.
243 *
244 * If @capable is set, set the @dev's power.can_wakeup flag and add its
245 * wakeup-related attributes to sysfs. Otherwise, unset the @dev's
246 * power.can_wakeup flag and remove its wakeup-related attributes from sysfs.
247 *
248 * This function may sleep and it can't be called from any context where
249 * sleeping is not allowed.
250 */
251void device_set_wakeup_capable(struct device *dev, bool capable)
252{
253 if (!!dev->power.can_wakeup == !!capable)
254 return;
255
22110faf 256 if (device_is_registered(dev) && !list_empty(&dev->power.entry)) {
cb8f51bd
RW
257 if (capable) {
258 if (wakeup_sysfs_add(dev))
259 return;
260 } else {
261 wakeup_sysfs_remove(dev);
262 }
263 }
264 dev->power.can_wakeup = capable;
265}
266EXPORT_SYMBOL_GPL(device_set_wakeup_capable);
267
074037ec
RW
268/**
269 * device_init_wakeup - Device wakeup initialization.
270 * @dev: Device to handle.
271 * @enable: Whether or not to enable @dev as a wakeup device.
272 *
273 * By default, most devices should leave wakeup disabled. The exceptions are
274 * devices that everyone expects to be wakeup sources: keyboards, power buttons,
8f88893c
AS
275 * possibly network interfaces, etc. Also, devices that don't generate their
276 * own wakeup requests but merely forward requests from one bus to another
277 * (like PCI bridges) should have wakeup enabled by default.
074037ec
RW
278 */
279int device_init_wakeup(struct device *dev, bool enable)
280{
281 int ret = 0;
282
283 if (enable) {
284 device_set_wakeup_capable(dev, true);
285 ret = device_wakeup_enable(dev);
286 } else {
287 device_set_wakeup_capable(dev, false);
288 }
289
290 return ret;
291}
292EXPORT_SYMBOL_GPL(device_init_wakeup);
293
294/**
295 * device_set_wakeup_enable - Enable or disable a device to wake up the system.
296 * @dev: Device to handle.
297 */
298int device_set_wakeup_enable(struct device *dev, bool enable)
299{
300 if (!dev || !dev->power.can_wakeup)
301 return -EINVAL;
302
303 return enable ? device_wakeup_enable(dev) : device_wakeup_disable(dev);
304}
305EXPORT_SYMBOL_GPL(device_set_wakeup_enable);
4eb241e5 306
c125e96f
RW
307/*
308 * The functions below use the observation that each wakeup event starts a
309 * period in which the system should not be suspended. The moment this period
310 * will end depends on how the wakeup event is going to be processed after being
311 * detected and all of the possible cases can be divided into two distinct
312 * groups.
313 *
314 * First, a wakeup event may be detected by the same functional unit that will
315 * carry out the entire processing of it and possibly will pass it to user space
316 * for further processing. In that case the functional unit that has detected
317 * the event may later "close" the "no suspend" period associated with it
318 * directly as soon as it has been dealt with. The pair of pm_stay_awake() and
319 * pm_relax(), balanced with each other, is supposed to be used in such
320 * situations.
321 *
322 * Second, a wakeup event may be detected by one functional unit and processed
323 * by another one. In that case the unit that has detected it cannot really
324 * "close" the "no suspend" period associated with it, unless it knows in
325 * advance what's going to happen to the event during processing. This
326 * knowledge, however, may not be available to it, so it can simply specify time
327 * to wait before the system can be suspended and pass it as the second
328 * argument of pm_wakeup_event().
074037ec
RW
329 *
330 * It is valid to call pm_relax() after pm_wakeup_event(), in which case the
331 * "no suspend" period will be ended either by the pm_relax(), or by the timer
332 * function executed when the timer expires, whichever comes first.
c125e96f
RW
333 */
334
074037ec
RW
335/**
336 * wakup_source_activate - Mark given wakeup source as active.
337 * @ws: Wakeup source to handle.
338 *
339 * Update the @ws' statistics and, if @ws has just been activated, notify the PM
340 * core of the event by incrementing the counter of of wakeup events being
341 * processed.
342 */
343static void wakeup_source_activate(struct wakeup_source *ws)
344{
345 ws->active = true;
346 ws->active_count++;
347 ws->timer_expires = jiffies;
348 ws->last_time = ktime_get();
349
023d3779
RW
350 /* Increment the counter of events in progress. */
351 atomic_inc(&combined_event_count);
074037ec
RW
352}
353
354/**
355 * __pm_stay_awake - Notify the PM core of a wakeup event.
356 * @ws: Wakeup source object associated with the source of the event.
357 *
358 * It is safe to call this function from interrupt context.
359 */
360void __pm_stay_awake(struct wakeup_source *ws)
361{
362 unsigned long flags;
363
364 if (!ws)
365 return;
366
367 spin_lock_irqsave(&ws->lock, flags);
368 ws->event_count++;
369 if (!ws->active)
370 wakeup_source_activate(ws);
371 spin_unlock_irqrestore(&ws->lock, flags);
372}
373EXPORT_SYMBOL_GPL(__pm_stay_awake);
374
c125e96f
RW
375/**
376 * pm_stay_awake - Notify the PM core that a wakeup event is being processed.
377 * @dev: Device the wakeup event is related to.
378 *
074037ec
RW
379 * Notify the PM core of a wakeup event (signaled by @dev) by calling
380 * __pm_stay_awake for the @dev's wakeup source object.
c125e96f
RW
381 *
382 * Call this function after detecting of a wakeup event if pm_relax() is going
383 * to be called directly after processing the event (and possibly passing it to
384 * user space for further processing).
c125e96f
RW
385 */
386void pm_stay_awake(struct device *dev)
387{
388 unsigned long flags;
389
074037ec
RW
390 if (!dev)
391 return;
c125e96f 392
074037ec
RW
393 spin_lock_irqsave(&dev->power.lock, flags);
394 __pm_stay_awake(dev->power.wakeup);
395 spin_unlock_irqrestore(&dev->power.lock, flags);
c125e96f 396}
074037ec 397EXPORT_SYMBOL_GPL(pm_stay_awake);
c125e96f
RW
398
399/**
074037ec
RW
400 * wakup_source_deactivate - Mark given wakeup source as inactive.
401 * @ws: Wakeup source to handle.
c125e96f 402 *
074037ec
RW
403 * Update the @ws' statistics and notify the PM core that the wakeup source has
404 * become inactive by decrementing the counter of wakeup events being processed
405 * and incrementing the counter of registered wakeup events.
406 */
407static void wakeup_source_deactivate(struct wakeup_source *ws)
408{
409 ktime_t duration;
410 ktime_t now;
411
412 ws->relax_count++;
413 /*
414 * __pm_relax() may be called directly or from a timer function.
415 * If it is called directly right after the timer function has been
416 * started, but before the timer function calls __pm_relax(), it is
417 * possible that __pm_stay_awake() will be called in the meantime and
418 * will set ws->active. Then, ws->active may be cleared immediately
419 * by the __pm_relax() called from the timer function, but in such a
420 * case ws->relax_count will be different from ws->active_count.
421 */
422 if (ws->relax_count != ws->active_count) {
423 ws->relax_count--;
424 return;
425 }
426
427 ws->active = false;
428
429 now = ktime_get();
430 duration = ktime_sub(now, ws->last_time);
431 ws->total_time = ktime_add(ws->total_time, duration);
432 if (ktime_to_ns(duration) > ktime_to_ns(ws->max_time))
433 ws->max_time = duration;
434
435 del_timer(&ws->timer);
da863cdd 436 ws->timer_expires = 0;
074037ec
RW
437
438 /*
023d3779
RW
439 * Increment the counter of registered wakeup events and decrement the
440 * couter of wakeup events in progress simultaneously.
074037ec 441 */
023d3779 442 atomic_add(MAX_IN_PROGRESS, &combined_event_count);
074037ec
RW
443}
444
445/**
446 * __pm_relax - Notify the PM core that processing of a wakeup event has ended.
447 * @ws: Wakeup source object associated with the source of the event.
c125e96f
RW
448 *
449 * Call this function for wakeup events whose processing started with calling
074037ec 450 * __pm_stay_awake().
c125e96f
RW
451 *
452 * It is safe to call it from interrupt context.
453 */
074037ec 454void __pm_relax(struct wakeup_source *ws)
c125e96f
RW
455{
456 unsigned long flags;
457
074037ec
RW
458 if (!ws)
459 return;
460
461 spin_lock_irqsave(&ws->lock, flags);
462 if (ws->active)
463 wakeup_source_deactivate(ws);
464 spin_unlock_irqrestore(&ws->lock, flags);
465}
466EXPORT_SYMBOL_GPL(__pm_relax);
467
468/**
469 * pm_relax - Notify the PM core that processing of a wakeup event has ended.
470 * @dev: Device that signaled the event.
471 *
472 * Execute __pm_relax() for the @dev's wakeup source object.
473 */
474void pm_relax(struct device *dev)
475{
476 unsigned long flags;
477
478 if (!dev)
479 return;
480
481 spin_lock_irqsave(&dev->power.lock, flags);
482 __pm_relax(dev->power.wakeup);
483 spin_unlock_irqrestore(&dev->power.lock, flags);
c125e96f 484}
074037ec 485EXPORT_SYMBOL_GPL(pm_relax);
c125e96f
RW
486
487/**
4eb241e5 488 * pm_wakeup_timer_fn - Delayed finalization of a wakeup event.
074037ec 489 * @data: Address of the wakeup source object associated with the event source.
c125e96f 490 *
da863cdd
RW
491 * Call wakeup_source_deactivate() for the wakeup source whose address is stored
492 * in @data if it is currently active and its timer has not been canceled and
493 * the expiration time of the timer is not in future.
c125e96f 494 */
4eb241e5 495static void pm_wakeup_timer_fn(unsigned long data)
074037ec 496{
da863cdd
RW
497 struct wakeup_source *ws = (struct wakeup_source *)data;
498 unsigned long flags;
499
500 spin_lock_irqsave(&ws->lock, flags);
501
502 if (ws->active && ws->timer_expires
503 && time_after_eq(jiffies, ws->timer_expires))
504 wakeup_source_deactivate(ws);
505
506 spin_unlock_irqrestore(&ws->lock, flags);
074037ec
RW
507}
508
509/**
510 * __pm_wakeup_event - Notify the PM core of a wakeup event.
511 * @ws: Wakeup source object associated with the event source.
512 * @msec: Anticipated event processing time (in milliseconds).
513 *
514 * Notify the PM core of a wakeup event whose source is @ws that will take
515 * approximately @msec milliseconds to be processed by the kernel. If @ws is
516 * not active, activate it. If @msec is nonzero, set up the @ws' timer to
517 * execute pm_wakeup_timer_fn() in future.
518 *
519 * It is safe to call this function from interrupt context.
520 */
521void __pm_wakeup_event(struct wakeup_source *ws, unsigned int msec)
c125e96f 522{
4eb241e5 523 unsigned long flags;
074037ec 524 unsigned long expires;
c125e96f 525
074037ec
RW
526 if (!ws)
527 return;
528
529 spin_lock_irqsave(&ws->lock, flags);
530
531 ws->event_count++;
532 if (!ws->active)
533 wakeup_source_activate(ws);
534
535 if (!msec) {
536 wakeup_source_deactivate(ws);
537 goto unlock;
4eb241e5 538 }
074037ec
RW
539
540 expires = jiffies + msecs_to_jiffies(msec);
541 if (!expires)
542 expires = 1;
543
544 if (time_after(expires, ws->timer_expires)) {
545 mod_timer(&ws->timer, expires);
546 ws->timer_expires = expires;
547 }
548
549 unlock:
550 spin_unlock_irqrestore(&ws->lock, flags);
c125e96f 551}
074037ec
RW
552EXPORT_SYMBOL_GPL(__pm_wakeup_event);
553
c125e96f
RW
554
555/**
556 * pm_wakeup_event - Notify the PM core of a wakeup event.
557 * @dev: Device the wakeup event is related to.
558 * @msec: Anticipated event processing time (in milliseconds).
559 *
074037ec 560 * Call __pm_wakeup_event() for the @dev's wakeup source object.
c125e96f
RW
561 */
562void pm_wakeup_event(struct device *dev, unsigned int msec)
563{
564 unsigned long flags;
c125e96f 565
074037ec
RW
566 if (!dev)
567 return;
c125e96f 568
074037ec
RW
569 spin_lock_irqsave(&dev->power.lock, flags);
570 __pm_wakeup_event(dev->power.wakeup, msec);
571 spin_unlock_irqrestore(&dev->power.lock, flags);
572}
573EXPORT_SYMBOL_GPL(pm_wakeup_event);
4eb241e5 574
074037ec
RW
575/**
576 * pm_wakeup_update_hit_counts - Update hit counts of all active wakeup sources.
577 */
578static void pm_wakeup_update_hit_counts(void)
579{
580 unsigned long flags;
581 struct wakeup_source *ws;
4eb241e5 582
074037ec
RW
583 rcu_read_lock();
584 list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
585 spin_lock_irqsave(&ws->lock, flags);
586 if (ws->active)
587 ws->hit_count++;
588 spin_unlock_irqrestore(&ws->lock, flags);
c125e96f 589 }
074037ec 590 rcu_read_unlock();
c125e96f
RW
591}
592
593/**
a2867e08 594 * pm_wakeup_pending - Check if power transition in progress should be aborted.
c125e96f
RW
595 *
596 * Compare the current number of registered wakeup events with its preserved
a2867e08
RW
597 * value from the past and return true if new wakeup events have been registered
598 * since the old value was stored. Also return true if the current number of
599 * wakeup events being processed is different from zero.
c125e96f 600 */
a2867e08 601bool pm_wakeup_pending(void)
c125e96f
RW
602{
603 unsigned long flags;
a2867e08 604 bool ret = false;
c125e96f
RW
605
606 spin_lock_irqsave(&events_lock, flags);
607 if (events_check_enabled) {
023d3779
RW
608 unsigned int cnt, inpr;
609
610 split_counters(&cnt, &inpr);
611 ret = (cnt != saved_count || inpr > 0);
a2867e08 612 events_check_enabled = !ret;
c125e96f
RW
613 }
614 spin_unlock_irqrestore(&events_lock, flags);
a2867e08 615 if (ret)
074037ec 616 pm_wakeup_update_hit_counts();
c125e96f
RW
617 return ret;
618}
619
620/**
621 * pm_get_wakeup_count - Read the number of registered wakeup events.
622 * @count: Address to store the value at.
623 *
624 * Store the number of registered wakeup events at the address in @count. Block
625 * if the current number of wakeup events being processed is nonzero.
626 *
790c7885 627 * Return 'false' if the wait for the number of wakeup events being processed to
c125e96f 628 * drop down to zero has been interrupted by a signal (and the current number
790c7885 629 * of wakeup events being processed is still nonzero). Otherwise return 'true'.
c125e96f 630 */
074037ec 631bool pm_get_wakeup_count(unsigned int *count)
c125e96f 632{
023d3779 633 unsigned int cnt, inpr;
c125e96f 634
023d3779
RW
635 for (;;) {
636 split_counters(&cnt, &inpr);
637 if (inpr == 0 || signal_pending(current))
638 break;
074037ec
RW
639 pm_wakeup_update_hit_counts();
640 schedule_timeout_interruptible(msecs_to_jiffies(TIMEOUT));
c125e96f 641 }
074037ec 642
023d3779
RW
643 split_counters(&cnt, &inpr);
644 *count = cnt;
645 return !inpr;
c125e96f
RW
646}
647
648/**
649 * pm_save_wakeup_count - Save the current number of registered wakeup events.
650 * @count: Value to compare with the current number of registered wakeup events.
651 *
652 * If @count is equal to the current number of registered wakeup events and the
653 * current number of wakeup events being processed is zero, store @count as the
378eef99
RW
654 * old number of registered wakeup events for pm_check_wakeup_events(), enable
655 * wakeup events detection and return 'true'. Otherwise disable wakeup events
656 * detection and return 'false'.
c125e96f 657 */
074037ec 658bool pm_save_wakeup_count(unsigned int count)
c125e96f 659{
023d3779 660 unsigned int cnt, inpr;
c125e96f 661
378eef99 662 events_check_enabled = false;
c125e96f 663 spin_lock_irq(&events_lock);
023d3779
RW
664 split_counters(&cnt, &inpr);
665 if (cnt == count && inpr == 0) {
074037ec 666 saved_count = count;
c125e96f 667 events_check_enabled = true;
c125e96f
RW
668 }
669 spin_unlock_irq(&events_lock);
378eef99 670 if (!events_check_enabled)
074037ec 671 pm_wakeup_update_hit_counts();
378eef99 672 return events_check_enabled;
c125e96f 673}
9c034392
RW
674
675static struct dentry *wakeup_sources_stats_dentry;
676
677/**
678 * print_wakeup_source_stats - Print wakeup source statistics information.
679 * @m: seq_file to print the statistics into.
680 * @ws: Wakeup source object to print the statistics for.
681 */
682static int print_wakeup_source_stats(struct seq_file *m,
683 struct wakeup_source *ws)
684{
685 unsigned long flags;
686 ktime_t total_time;
687 ktime_t max_time;
688 unsigned long active_count;
689 ktime_t active_time;
690 int ret;
691
692 spin_lock_irqsave(&ws->lock, flags);
693
694 total_time = ws->total_time;
695 max_time = ws->max_time;
696 active_count = ws->active_count;
697 if (ws->active) {
698 active_time = ktime_sub(ktime_get(), ws->last_time);
699 total_time = ktime_add(total_time, active_time);
700 if (active_time.tv64 > max_time.tv64)
701 max_time = active_time;
702 } else {
703 active_time = ktime_set(0, 0);
704 }
705
706 ret = seq_printf(m, "%-12s\t%lu\t\t%lu\t\t%lu\t\t"
707 "%lld\t\t%lld\t\t%lld\t\t%lld\n",
708 ws->name, active_count, ws->event_count, ws->hit_count,
709 ktime_to_ms(active_time), ktime_to_ms(total_time),
710 ktime_to_ms(max_time), ktime_to_ms(ws->last_time));
711
712 spin_unlock_irqrestore(&ws->lock, flags);
713
714 return ret;
715}
716
717/**
718 * wakeup_sources_stats_show - Print wakeup sources statistics information.
719 * @m: seq_file to print the statistics into.
720 */
721static int wakeup_sources_stats_show(struct seq_file *m, void *unused)
722{
723 struct wakeup_source *ws;
724
725 seq_puts(m, "name\t\tactive_count\tevent_count\thit_count\t"
726 "active_since\ttotal_time\tmax_time\tlast_change\n");
727
728 rcu_read_lock();
729 list_for_each_entry_rcu(ws, &wakeup_sources, entry)
730 print_wakeup_source_stats(m, ws);
731 rcu_read_unlock();
732
733 return 0;
734}
735
736static int wakeup_sources_stats_open(struct inode *inode, struct file *file)
737{
738 return single_open(file, wakeup_sources_stats_show, NULL);
739}
740
741static const struct file_operations wakeup_sources_stats_fops = {
742 .owner = THIS_MODULE,
743 .open = wakeup_sources_stats_open,
744 .read = seq_read,
745 .llseek = seq_lseek,
746 .release = single_release,
747};
748
749static int __init wakeup_sources_debugfs_init(void)
750{
751 wakeup_sources_stats_dentry = debugfs_create_file("wakeup_sources",
752 S_IRUGO, NULL, NULL, &wakeup_sources_stats_fops);
753 return 0;
754}
755
756postcore_initcall(wakeup_sources_debugfs_init);