2 * kernel/power/wakelock.c
4 * User space wakeup sources support.
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>
23 static DEFINE_MUTEX(wakelocks_lock);
28 struct wakeup_source ws;
29 #ifdef CONFIG_PM_WAKELOCKS_GC
34 static struct rb_root wakelocks_tree = RB_ROOT;
36 ssize_t pm_show_wakelocks(char *buf, bool show_active)
41 char *end = buf + PAGE_SIZE;
43 mutex_lock(&wakelocks_lock);
45 for (node = rb_first(&wakelocks_tree); node; node = rb_next(node)) {
46 wl = rb_entry(node, struct wakelock, node);
47 if (wl->ws.active == show_active)
48 str += scnprintf(str, end - str, "%s ", wl->name);
53 str += scnprintf(str, end - str, "\n");
55 mutex_unlock(&wakelocks_lock);
59 #if CONFIG_PM_WAKELOCKS_LIMIT > 0
60 static unsigned int number_of_wakelocks;
62 static inline bool wakelocks_limit_exceeded(void)
64 return number_of_wakelocks > CONFIG_PM_WAKELOCKS_LIMIT;
67 static inline void increment_wakelocks_number(void)
69 number_of_wakelocks++;
72 static inline void decrement_wakelocks_number(void)
74 number_of_wakelocks--;
76 #else /* CONFIG_PM_WAKELOCKS_LIMIT = 0 */
77 static inline bool wakelocks_limit_exceeded(void) { return false; }
78 static inline void increment_wakelocks_number(void) {}
79 static inline void decrement_wakelocks_number(void) {}
80 #endif /* CONFIG_PM_WAKELOCKS_LIMIT */
82 #ifdef CONFIG_PM_WAKELOCKS_GC
83 #define WL_GC_COUNT_MAX 100
84 #define WL_GC_TIME_SEC 300
86 static LIST_HEAD(wakelocks_lru_list);
87 static unsigned int wakelocks_gc_count;
89 static inline void wakelocks_lru_add(struct wakelock *wl)
91 list_add(&wl->lru, &wakelocks_lru_list);
94 static inline void wakelocks_lru_most_recent(struct wakelock *wl)
96 list_move(&wl->lru, &wakelocks_lru_list);
99 static void wakelocks_gc(void)
101 struct wakelock *wl, *aux;
104 if (++wakelocks_gc_count <= WL_GC_COUNT_MAX)
108 list_for_each_entry_safe_reverse(wl, aux, &wakelocks_lru_list, lru) {
112 spin_lock_irq(&wl->ws.lock);
113 idle_time_ns = ktime_to_ns(ktime_sub(now, wl->ws.last_time));
114 active = wl->ws.active;
115 spin_unlock_irq(&wl->ws.lock);
117 if (idle_time_ns < ((u64)WL_GC_TIME_SEC * NSEC_PER_SEC))
121 wakeup_source_remove(&wl->ws);
122 rb_erase(&wl->node, &wakelocks_tree);
126 decrement_wakelocks_number();
129 wakelocks_gc_count = 0;
131 #else /* !CONFIG_PM_WAKELOCKS_GC */
132 static inline void wakelocks_lru_add(struct wakelock *wl) {}
133 static inline void wakelocks_lru_most_recent(struct wakelock *wl) {}
134 static inline void wakelocks_gc(void) {}
135 #endif /* !CONFIG_PM_WAKELOCKS_GC */
137 static struct wakelock *wakelock_lookup_add(const char *name, size_t len,
138 bool add_if_not_found)
140 struct rb_node **node = &wakelocks_tree.rb_node;
141 struct rb_node *parent = *node;
148 wl = rb_entry(*node, struct wakelock, node);
149 diff = strncmp(name, wl->name, len);
157 node = &(*node)->rb_left;
159 node = &(*node)->rb_right;
161 if (!add_if_not_found)
162 return ERR_PTR(-EINVAL);
164 if (wakelocks_limit_exceeded())
165 return ERR_PTR(-ENOSPC);
167 /* Not found, we have to add a new one. */
168 wl = kzalloc(sizeof(*wl), GFP_KERNEL);
170 return ERR_PTR(-ENOMEM);
172 wl->name = kstrndup(name, len, GFP_KERNEL);
175 return ERR_PTR(-ENOMEM);
177 wl->ws.name = wl->name;
178 wakeup_source_add(&wl->ws);
179 rb_link_node(&wl->node, parent, node);
180 rb_insert_color(&wl->node, &wakelocks_tree);
181 wakelocks_lru_add(wl);
182 increment_wakelocks_number();
186 int pm_wake_lock(const char *buf)
188 const char *str = buf;
194 if (!capable(CAP_BLOCK_SUSPEND))
197 while (*str && !isspace(*str))
204 if (*str && *str != '\n') {
205 /* Find out if there's a valid timeout string appended. */
206 ret = kstrtou64(skip_spaces(str), 10, &timeout_ns);
211 mutex_lock(&wakelocks_lock);
213 wl = wakelock_lookup_add(buf, len, true);
219 u64 timeout_ms = timeout_ns + NSEC_PER_MSEC - 1;
221 do_div(timeout_ms, NSEC_PER_MSEC);
222 __pm_wakeup_event(&wl->ws, timeout_ms);
224 __pm_stay_awake(&wl->ws);
227 wakelocks_lru_most_recent(wl);
230 mutex_unlock(&wakelocks_lock);
234 int pm_wake_unlock(const char *buf)
240 if (!capable(CAP_BLOCK_SUSPEND))
247 if (buf[len-1] == '\n')
253 mutex_lock(&wakelocks_lock);
255 wl = wakelock_lookup_add(buf, len, false);
262 wakelocks_lru_most_recent(wl);
266 mutex_unlock(&wakelocks_lock);