Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / kernel / power / wakelock.c
blobdfba59be190b6a7d36495b7dcfc225a9fba2b0f6
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * kernel/power/wakelock.c
5 * User space wakeup sources support.
7 * Copyright (C) 2012 Rafael J. Wysocki <rjw@sisk.pl>
9 * This code is based on the analogous interface allowing user space to
10 * manipulate wakelocks on Android.
13 #include <linux/capability.h>
14 #include <linux/ctype.h>
15 #include <linux/device.h>
16 #include <linux/err.h>
17 #include <linux/hrtimer.h>
18 #include <linux/list.h>
19 #include <linux/rbtree.h>
20 #include <linux/slab.h>
21 #include <linux/workqueue.h>
23 #include "power.h"
25 static DEFINE_MUTEX(wakelocks_lock);
27 struct wakelock {
28 char *name;
29 struct rb_node node;
30 struct wakeup_source ws;
31 #ifdef CONFIG_PM_WAKELOCKS_GC
32 struct list_head lru;
33 #endif
36 static struct rb_root wakelocks_tree = RB_ROOT;
38 ssize_t pm_show_wakelocks(char *buf, bool show_active)
40 struct rb_node *node;
41 struct wakelock *wl;
42 char *str = buf;
43 char *end = buf + PAGE_SIZE;
45 mutex_lock(&wakelocks_lock);
47 for (node = rb_first(&wakelocks_tree); node; node = rb_next(node)) {
48 wl = rb_entry(node, struct wakelock, node);
49 if (wl->ws.active == show_active)
50 str += scnprintf(str, end - str, "%s ", wl->name);
52 if (str > buf)
53 str--;
55 str += scnprintf(str, end - str, "\n");
57 mutex_unlock(&wakelocks_lock);
58 return (str - buf);
61 #if CONFIG_PM_WAKELOCKS_LIMIT > 0
62 static unsigned int number_of_wakelocks;
64 static inline bool wakelocks_limit_exceeded(void)
66 return number_of_wakelocks > CONFIG_PM_WAKELOCKS_LIMIT;
69 static inline void increment_wakelocks_number(void)
71 number_of_wakelocks++;
74 static inline void decrement_wakelocks_number(void)
76 number_of_wakelocks--;
78 #else /* CONFIG_PM_WAKELOCKS_LIMIT = 0 */
79 static inline bool wakelocks_limit_exceeded(void) { return false; }
80 static inline void increment_wakelocks_number(void) {}
81 static inline void decrement_wakelocks_number(void) {}
82 #endif /* CONFIG_PM_WAKELOCKS_LIMIT */
84 #ifdef CONFIG_PM_WAKELOCKS_GC
85 #define WL_GC_COUNT_MAX 100
86 #define WL_GC_TIME_SEC 300
88 static void __wakelocks_gc(struct work_struct *work);
89 static LIST_HEAD(wakelocks_lru_list);
90 static DECLARE_WORK(wakelock_work, __wakelocks_gc);
91 static unsigned int wakelocks_gc_count;
93 static inline void wakelocks_lru_add(struct wakelock *wl)
95 list_add(&wl->lru, &wakelocks_lru_list);
98 static inline void wakelocks_lru_most_recent(struct wakelock *wl)
100 list_move(&wl->lru, &wakelocks_lru_list);
103 static void __wakelocks_gc(struct work_struct *work)
105 struct wakelock *wl, *aux;
106 ktime_t now;
108 mutex_lock(&wakelocks_lock);
110 now = ktime_get();
111 list_for_each_entry_safe_reverse(wl, aux, &wakelocks_lru_list, lru) {
112 u64 idle_time_ns;
113 bool active;
115 spin_lock_irq(&wl->ws.lock);
116 idle_time_ns = ktime_to_ns(ktime_sub(now, wl->ws.last_time));
117 active = wl->ws.active;
118 spin_unlock_irq(&wl->ws.lock);
120 if (idle_time_ns < ((u64)WL_GC_TIME_SEC * NSEC_PER_SEC))
121 break;
123 if (!active) {
124 wakeup_source_remove(&wl->ws);
125 rb_erase(&wl->node, &wakelocks_tree);
126 list_del(&wl->lru);
127 kfree(wl->name);
128 kfree(wl);
129 decrement_wakelocks_number();
132 wakelocks_gc_count = 0;
134 mutex_unlock(&wakelocks_lock);
137 static void wakelocks_gc(void)
139 if (++wakelocks_gc_count <= WL_GC_COUNT_MAX)
140 return;
142 schedule_work(&wakelock_work);
144 #else /* !CONFIG_PM_WAKELOCKS_GC */
145 static inline void wakelocks_lru_add(struct wakelock *wl) {}
146 static inline void wakelocks_lru_most_recent(struct wakelock *wl) {}
147 static inline void wakelocks_gc(void) {}
148 #endif /* !CONFIG_PM_WAKELOCKS_GC */
150 static struct wakelock *wakelock_lookup_add(const char *name, size_t len,
151 bool add_if_not_found)
153 struct rb_node **node = &wakelocks_tree.rb_node;
154 struct rb_node *parent = *node;
155 struct wakelock *wl;
157 while (*node) {
158 int diff;
160 parent = *node;
161 wl = rb_entry(*node, struct wakelock, node);
162 diff = strncmp(name, wl->name, len);
163 if (diff == 0) {
164 if (wl->name[len])
165 diff = -1;
166 else
167 return wl;
169 if (diff < 0)
170 node = &(*node)->rb_left;
171 else
172 node = &(*node)->rb_right;
174 if (!add_if_not_found)
175 return ERR_PTR(-EINVAL);
177 if (wakelocks_limit_exceeded())
178 return ERR_PTR(-ENOSPC);
180 /* Not found, we have to add a new one. */
181 wl = kzalloc(sizeof(*wl), GFP_KERNEL);
182 if (!wl)
183 return ERR_PTR(-ENOMEM);
185 wl->name = kstrndup(name, len, GFP_KERNEL);
186 if (!wl->name) {
187 kfree(wl);
188 return ERR_PTR(-ENOMEM);
190 wl->ws.name = wl->name;
191 wakeup_source_add(&wl->ws);
192 rb_link_node(&wl->node, parent, node);
193 rb_insert_color(&wl->node, &wakelocks_tree);
194 wakelocks_lru_add(wl);
195 increment_wakelocks_number();
196 return wl;
199 int pm_wake_lock(const char *buf)
201 const char *str = buf;
202 struct wakelock *wl;
203 u64 timeout_ns = 0;
204 size_t len;
205 int ret = 0;
207 if (!capable(CAP_BLOCK_SUSPEND))
208 return -EPERM;
210 while (*str && !isspace(*str))
211 str++;
213 len = str - buf;
214 if (!len)
215 return -EINVAL;
217 if (*str && *str != '\n') {
218 /* Find out if there's a valid timeout string appended. */
219 ret = kstrtou64(skip_spaces(str), 10, &timeout_ns);
220 if (ret)
221 return -EINVAL;
224 mutex_lock(&wakelocks_lock);
226 wl = wakelock_lookup_add(buf, len, true);
227 if (IS_ERR(wl)) {
228 ret = PTR_ERR(wl);
229 goto out;
231 if (timeout_ns) {
232 u64 timeout_ms = timeout_ns + NSEC_PER_MSEC - 1;
234 do_div(timeout_ms, NSEC_PER_MSEC);
235 __pm_wakeup_event(&wl->ws, timeout_ms);
236 } else {
237 __pm_stay_awake(&wl->ws);
240 wakelocks_lru_most_recent(wl);
242 out:
243 mutex_unlock(&wakelocks_lock);
244 return ret;
247 int pm_wake_unlock(const char *buf)
249 struct wakelock *wl;
250 size_t len;
251 int ret = 0;
253 if (!capable(CAP_BLOCK_SUSPEND))
254 return -EPERM;
256 len = strlen(buf);
257 if (!len)
258 return -EINVAL;
260 if (buf[len-1] == '\n')
261 len--;
263 if (!len)
264 return -EINVAL;
266 mutex_lock(&wakelocks_lock);
268 wl = wakelock_lookup_add(buf, len, false);
269 if (IS_ERR(wl)) {
270 ret = PTR_ERR(wl);
271 goto out;
273 __pm_relax(&wl->ws);
275 wakelocks_lru_most_recent(wl);
276 wakelocks_gc();
278 out:
279 mutex_unlock(&wakelocks_lock);
280 return ret;