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>
20 #include <linux/workqueue.h>
24 static DEFINE_MUTEX(wakelocks_lock);
29 struct wakeup_source ws;
30 #ifdef CONFIG_PM_WAKELOCKS_GC
35 static struct rb_root wakelocks_tree = RB_ROOT;
37 ssize_t pm_show_wakelocks(char *buf, bool show_active)
42 char *end = buf + PAGE_SIZE;
44 mutex_lock(&wakelocks_lock);
46 for (node = rb_first(&wakelocks_tree); node; node = rb_next(node)) {
47 wl = rb_entry(node, struct wakelock, node);
48 if (wl->ws.active == show_active)
49 str += scnprintf(str, end - str, "%s ", wl->name);
54 str += scnprintf(str, end - str, "\n");
56 mutex_unlock(&wakelocks_lock);
60 #if CONFIG_PM_WAKELOCKS_LIMIT > 0
61 static unsigned int number_of_wakelocks;
63 static inline bool wakelocks_limit_exceeded(void)
65 return number_of_wakelocks > CONFIG_PM_WAKELOCKS_LIMIT;
68 static inline void increment_wakelocks_number(void)
70 number_of_wakelocks++;
73 static inline void decrement_wakelocks_number(void)
75 number_of_wakelocks--;
77 #else /* CONFIG_PM_WAKELOCKS_LIMIT = 0 */
78 static inline bool wakelocks_limit_exceeded(void) { return false; }
79 static inline void increment_wakelocks_number(void) {}
80 static inline void decrement_wakelocks_number(void) {}
81 #endif /* CONFIG_PM_WAKELOCKS_LIMIT */
83 #ifdef CONFIG_PM_WAKELOCKS_GC
84 #define WL_GC_COUNT_MAX 100
85 #define WL_GC_TIME_SEC 300
87 static void __wakelocks_gc(struct work_struct *work);
88 static LIST_HEAD(wakelocks_lru_list);
89 static DECLARE_WORK(wakelock_work, __wakelocks_gc);
90 static unsigned int wakelocks_gc_count;
92 static inline void wakelocks_lru_add(struct wakelock *wl)
94 list_add(&wl->lru, &wakelocks_lru_list);
97 static inline void wakelocks_lru_most_recent(struct wakelock *wl)
99 list_move(&wl->lru, &wakelocks_lru_list);
102 static void __wakelocks_gc(struct work_struct *work)
104 struct wakelock *wl, *aux;
107 mutex_lock(&wakelocks_lock);
110 list_for_each_entry_safe_reverse(wl, aux, &wakelocks_lru_list, lru) {
114 spin_lock_irq(&wl->ws.lock);
115 idle_time_ns = ktime_to_ns(ktime_sub(now, wl->ws.last_time));
116 active = wl->ws.active;
117 spin_unlock_irq(&wl->ws.lock);
119 if (idle_time_ns < ((u64)WL_GC_TIME_SEC * NSEC_PER_SEC))
123 wakeup_source_remove(&wl->ws);
124 rb_erase(&wl->node, &wakelocks_tree);
128 decrement_wakelocks_number();
131 wakelocks_gc_count = 0;
133 mutex_unlock(&wakelocks_lock);
136 static void wakelocks_gc(void)
138 if (++wakelocks_gc_count <= WL_GC_COUNT_MAX)
141 schedule_work(&wakelock_work);
143 #else /* !CONFIG_PM_WAKELOCKS_GC */
144 static inline void wakelocks_lru_add(struct wakelock *wl) {}
145 static inline void wakelocks_lru_most_recent(struct wakelock *wl) {}
146 static inline void wakelocks_gc(void) {}
147 #endif /* !CONFIG_PM_WAKELOCKS_GC */
149 static struct wakelock *wakelock_lookup_add(const char *name, size_t len,
150 bool add_if_not_found)
152 struct rb_node **node = &wakelocks_tree.rb_node;
153 struct rb_node *parent = *node;
160 wl = rb_entry(*node, struct wakelock, node);
161 diff = strncmp(name, wl->name, len);
169 node = &(*node)->rb_left;
171 node = &(*node)->rb_right;
173 if (!add_if_not_found)
174 return ERR_PTR(-EINVAL);
176 if (wakelocks_limit_exceeded())
177 return ERR_PTR(-ENOSPC);
179 /* Not found, we have to add a new one. */
180 wl = kzalloc(sizeof(*wl), GFP_KERNEL);
182 return ERR_PTR(-ENOMEM);
184 wl->name = kstrndup(name, len, GFP_KERNEL);
187 return ERR_PTR(-ENOMEM);
189 wl->ws.name = wl->name;
190 wakeup_source_add(&wl->ws);
191 rb_link_node(&wl->node, parent, node);
192 rb_insert_color(&wl->node, &wakelocks_tree);
193 wakelocks_lru_add(wl);
194 increment_wakelocks_number();
198 int pm_wake_lock(const char *buf)
200 const char *str = buf;
206 if (!capable(CAP_BLOCK_SUSPEND))
209 while (*str && !isspace(*str))
216 if (*str && *str != '\n') {
217 /* Find out if there's a valid timeout string appended. */
218 ret = kstrtou64(skip_spaces(str), 10, &timeout_ns);
223 mutex_lock(&wakelocks_lock);
225 wl = wakelock_lookup_add(buf, len, true);
231 u64 timeout_ms = timeout_ns + NSEC_PER_MSEC - 1;
233 do_div(timeout_ms, NSEC_PER_MSEC);
234 __pm_wakeup_event(&wl->ws, timeout_ms);
236 __pm_stay_awake(&wl->ws);
239 wakelocks_lru_most_recent(wl);
242 mutex_unlock(&wakelocks_lock);
246 int pm_wake_unlock(const char *buf)
252 if (!capable(CAP_BLOCK_SUSPEND))
259 if (buf[len-1] == '\n')
265 mutex_lock(&wakelocks_lock);
267 wl = wakelock_lookup_add(buf, len, false);
274 wakelocks_lru_most_recent(wl);
278 mutex_unlock(&wakelocks_lock);