1 // SPDX-License-Identifier: GPL-2.0
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>
25 static DEFINE_MUTEX(wakelocks_lock);
30 struct wakeup_source *ws;
31 #ifdef CONFIG_PM_WAKELOCKS_GC
36 static struct rb_root wakelocks_tree = RB_ROOT;
38 ssize_t pm_show_wakelocks(char *buf, bool show_active)
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 len += sysfs_emit_at(buf, len, "%s ", wl->name);
52 len += sysfs_emit_at(buf, len, "\n");
54 mutex_unlock(&wakelocks_lock);
58 #if CONFIG_PM_WAKELOCKS_LIMIT > 0
59 static unsigned int number_of_wakelocks;
61 static inline bool wakelocks_limit_exceeded(void)
63 return number_of_wakelocks > CONFIG_PM_WAKELOCKS_LIMIT;
66 static inline void increment_wakelocks_number(void)
68 number_of_wakelocks++;
71 static inline void decrement_wakelocks_number(void)
73 number_of_wakelocks--;
75 #else /* CONFIG_PM_WAKELOCKS_LIMIT = 0 */
76 static inline bool wakelocks_limit_exceeded(void) { return false; }
77 static inline void increment_wakelocks_number(void) {}
78 static inline void decrement_wakelocks_number(void) {}
79 #endif /* CONFIG_PM_WAKELOCKS_LIMIT */
81 #ifdef CONFIG_PM_WAKELOCKS_GC
82 #define WL_GC_COUNT_MAX 100
83 #define WL_GC_TIME_SEC 300
85 static void __wakelocks_gc(struct work_struct *work);
86 static LIST_HEAD(wakelocks_lru_list);
87 static DECLARE_WORK(wakelock_work, __wakelocks_gc);
88 static unsigned int wakelocks_gc_count;
90 static inline void wakelocks_lru_add(struct wakelock *wl)
92 list_add(&wl->lru, &wakelocks_lru_list);
95 static inline void wakelocks_lru_most_recent(struct wakelock *wl)
97 list_move(&wl->lru, &wakelocks_lru_list);
100 static void __wakelocks_gc(struct work_struct *work)
102 struct wakelock *wl, *aux;
105 mutex_lock(&wakelocks_lock);
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_unregister(wl->ws);
122 rb_erase(&wl->node, &wakelocks_tree);
126 decrement_wakelocks_number();
129 wakelocks_gc_count = 0;
131 mutex_unlock(&wakelocks_lock);
134 static void wakelocks_gc(void)
136 if (++wakelocks_gc_count <= WL_GC_COUNT_MAX)
139 schedule_work(&wakelock_work);
141 #else /* !CONFIG_PM_WAKELOCKS_GC */
142 static inline void wakelocks_lru_add(struct wakelock *wl) {}
143 static inline void wakelocks_lru_most_recent(struct wakelock *wl) {}
144 static inline void wakelocks_gc(void) {}
145 #endif /* !CONFIG_PM_WAKELOCKS_GC */
147 static struct wakelock *wakelock_lookup_add(const char *name, size_t len,
148 bool add_if_not_found)
150 struct rb_node **node = &wakelocks_tree.rb_node;
151 struct rb_node *parent = *node;
158 wl = rb_entry(*node, struct wakelock, node);
159 diff = strncmp(name, wl->name, len);
167 node = &(*node)->rb_left;
169 node = &(*node)->rb_right;
171 if (!add_if_not_found)
172 return ERR_PTR(-EINVAL);
174 if (wakelocks_limit_exceeded())
175 return ERR_PTR(-ENOSPC);
177 /* Not found, we have to add a new one. */
178 wl = kzalloc(sizeof(*wl), GFP_KERNEL);
180 return ERR_PTR(-ENOMEM);
182 wl->name = kstrndup(name, len, GFP_KERNEL);
185 return ERR_PTR(-ENOMEM);
188 wl->ws = wakeup_source_register(NULL, wl->name);
192 return ERR_PTR(-ENOMEM);
194 wl->ws->last_time = ktime_get();
196 rb_link_node(&wl->node, parent, node);
197 rb_insert_color(&wl->node, &wakelocks_tree);
198 wakelocks_lru_add(wl);
199 increment_wakelocks_number();
203 int pm_wake_lock(const char *buf)
205 const char *str = buf;
211 if (!capable(CAP_BLOCK_SUSPEND))
214 while (*str && !isspace(*str))
221 if (*str && *str != '\n') {
222 /* Find out if there's a valid timeout string appended. */
223 ret = kstrtou64(skip_spaces(str), 10, &timeout_ns);
228 mutex_lock(&wakelocks_lock);
230 wl = wakelock_lookup_add(buf, len, true);
236 u64 timeout_ms = timeout_ns + NSEC_PER_MSEC - 1;
238 do_div(timeout_ms, NSEC_PER_MSEC);
239 __pm_wakeup_event(wl->ws, timeout_ms);
241 __pm_stay_awake(wl->ws);
244 wakelocks_lru_most_recent(wl);
247 mutex_unlock(&wakelocks_lock);
251 int pm_wake_unlock(const char *buf)
257 if (!capable(CAP_BLOCK_SUSPEND))
264 if (buf[len-1] == '\n')
270 mutex_lock(&wakelocks_lock);
272 wl = wakelock_lookup_add(buf, len, false);
279 wakelocks_lru_most_recent(wl);
283 mutex_unlock(&wakelocks_lock);