2 * kernel/power/wakelock.c
4 * User space wakeup sources support.
6 * Copyright (C) 2012 Rafael J. Wysocki <rjw@sisk.pl>
8 * This code is based on the analogous interface allowing user space to
9 * manipulate wakelocks on Android.
12 #include <linux/capability.h>
13 #include <linux/ctype.h>
14 #include <linux/device.h>
15 #include <linux/err.h>
16 #include <linux/hrtimer.h>
17 #include <linux/list.h>
18 #include <linux/rbtree.h>
19 #include <linux/slab.h>
21 static DEFINE_MUTEX(wakelocks_lock
);
26 struct wakeup_source ws
;
27 #ifdef CONFIG_PM_WAKELOCKS_GC
32 static struct rb_root wakelocks_tree
= RB_ROOT
;
34 ssize_t
pm_show_wakelocks(char *buf
, bool show_active
)
39 char *end
= buf
+ PAGE_SIZE
;
41 mutex_lock(&wakelocks_lock
);
43 for (node
= rb_first(&wakelocks_tree
); node
; node
= rb_next(node
)) {
44 wl
= rb_entry(node
, struct wakelock
, node
);
45 if (wl
->ws
.active
== show_active
)
46 str
+= scnprintf(str
, end
- str
, "%s ", wl
->name
);
51 str
+= scnprintf(str
, end
- str
, "\n");
53 mutex_unlock(&wakelocks_lock
);
57 #if CONFIG_PM_WAKELOCKS_LIMIT > 0
58 static unsigned int number_of_wakelocks
;
60 static inline bool wakelocks_limit_exceeded(void)
62 return number_of_wakelocks
> CONFIG_PM_WAKELOCKS_LIMIT
;
65 static inline void increment_wakelocks_number(void)
67 number_of_wakelocks
++;
70 static inline void decrement_wakelocks_number(void)
72 number_of_wakelocks
--;
74 #else /* CONFIG_PM_WAKELOCKS_LIMIT = 0 */
75 static inline bool wakelocks_limit_exceeded(void) { return false; }
76 static inline void increment_wakelocks_number(void) {}
77 static inline void decrement_wakelocks_number(void) {}
78 #endif /* CONFIG_PM_WAKELOCKS_LIMIT */
80 #ifdef CONFIG_PM_WAKELOCKS_GC
81 #define WL_GC_COUNT_MAX 100
82 #define WL_GC_TIME_SEC 300
84 static LIST_HEAD(wakelocks_lru_list
);
85 static unsigned int wakelocks_gc_count
;
87 static inline void wakelocks_lru_add(struct wakelock
*wl
)
89 list_add(&wl
->lru
, &wakelocks_lru_list
);
92 static inline void wakelocks_lru_most_recent(struct wakelock
*wl
)
94 list_move(&wl
->lru
, &wakelocks_lru_list
);
97 static void wakelocks_gc(void)
99 struct wakelock
*wl
, *aux
;
102 if (++wakelocks_gc_count
<= WL_GC_COUNT_MAX
)
106 list_for_each_entry_safe_reverse(wl
, aux
, &wakelocks_lru_list
, lru
) {
110 spin_lock_irq(&wl
->ws
.lock
);
111 idle_time_ns
= ktime_to_ns(ktime_sub(now
, wl
->ws
.last_time
));
112 active
= wl
->ws
.active
;
113 spin_unlock_irq(&wl
->ws
.lock
);
115 if (idle_time_ns
< ((u64
)WL_GC_TIME_SEC
* NSEC_PER_SEC
))
119 wakeup_source_remove(&wl
->ws
);
120 rb_erase(&wl
->node
, &wakelocks_tree
);
124 decrement_wakelocks_number();
127 wakelocks_gc_count
= 0;
129 #else /* !CONFIG_PM_WAKELOCKS_GC */
130 static inline void wakelocks_lru_add(struct wakelock
*wl
) {}
131 static inline void wakelocks_lru_most_recent(struct wakelock
*wl
) {}
132 static inline void wakelocks_gc(void) {}
133 #endif /* !CONFIG_PM_WAKELOCKS_GC */
135 static struct wakelock
*wakelock_lookup_add(const char *name
, size_t len
,
136 bool add_if_not_found
)
138 struct rb_node
**node
= &wakelocks_tree
.rb_node
;
139 struct rb_node
*parent
= *node
;
146 wl
= rb_entry(*node
, struct wakelock
, node
);
147 diff
= strncmp(name
, wl
->name
, len
);
155 node
= &(*node
)->rb_left
;
157 node
= &(*node
)->rb_right
;
159 if (!add_if_not_found
)
160 return ERR_PTR(-EINVAL
);
162 if (wakelocks_limit_exceeded())
163 return ERR_PTR(-ENOSPC
);
165 /* Not found, we have to add a new one. */
166 wl
= kzalloc(sizeof(*wl
), GFP_KERNEL
);
168 return ERR_PTR(-ENOMEM
);
170 wl
->name
= kstrndup(name
, len
, GFP_KERNEL
);
173 return ERR_PTR(-ENOMEM
);
175 wl
->ws
.name
= wl
->name
;
176 wakeup_source_add(&wl
->ws
);
177 rb_link_node(&wl
->node
, parent
, node
);
178 rb_insert_color(&wl
->node
, &wakelocks_tree
);
179 wakelocks_lru_add(wl
);
180 increment_wakelocks_number();
184 int pm_wake_lock(const char *buf
)
186 const char *str
= buf
;
192 if (!capable(CAP_BLOCK_SUSPEND
))
195 while (*str
&& !isspace(*str
))
202 if (*str
&& *str
!= '\n') {
203 /* Find out if there's a valid timeout string appended. */
204 ret
= kstrtou64(skip_spaces(str
), 10, &timeout_ns
);
209 mutex_lock(&wakelocks_lock
);
211 wl
= wakelock_lookup_add(buf
, len
, true);
217 u64 timeout_ms
= timeout_ns
+ NSEC_PER_MSEC
- 1;
219 do_div(timeout_ms
, NSEC_PER_MSEC
);
220 __pm_wakeup_event(&wl
->ws
, timeout_ms
);
222 __pm_stay_awake(&wl
->ws
);
225 wakelocks_lru_most_recent(wl
);
228 mutex_unlock(&wakelocks_lock
);
232 int pm_wake_unlock(const char *buf
)
238 if (!capable(CAP_BLOCK_SUSPEND
))
245 if (buf
[len
-1] == '\n')
251 mutex_lock(&wakelocks_lock
);
253 wl
= wakelock_lookup_add(buf
, len
, false);
260 wakelocks_lru_most_recent(wl
);
264 mutex_unlock(&wakelocks_lock
);