GNU Linux-libre 4.19.264-gnu1
[releases.git] / kernel / power / wakelock.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * kernel/power/wakelock.c
4  *
5  * User space wakeup sources support.
6  *
7  * Copyright (C) 2012 Rafael J. Wysocki <rjw@sisk.pl>
8  *
9  * This code is based on the analogous interface allowing user space to
10  * manipulate wakelocks on Android.
11  */
12
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>
22
23 #include "power.h"
24
25 static DEFINE_MUTEX(wakelocks_lock);
26
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
34 };
35
36 static struct rb_root wakelocks_tree = RB_ROOT;
37
38 ssize_t pm_show_wakelocks(char *buf, bool show_active)
39 {
40         struct rb_node *node;
41         struct wakelock *wl;
42         int len = 0;
43
44         mutex_lock(&wakelocks_lock);
45
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);
50         }
51         len += sysfs_emit_at(buf, len, "\n");
52
53         mutex_unlock(&wakelocks_lock);
54         return len;
55 }
56
57 #if CONFIG_PM_WAKELOCKS_LIMIT > 0
58 static unsigned int number_of_wakelocks;
59
60 static inline bool wakelocks_limit_exceeded(void)
61 {
62         return number_of_wakelocks > CONFIG_PM_WAKELOCKS_LIMIT;
63 }
64
65 static inline void increment_wakelocks_number(void)
66 {
67         number_of_wakelocks++;
68 }
69
70 static inline void decrement_wakelocks_number(void)
71 {
72         number_of_wakelocks--;
73 }
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 */
79
80 #ifdef CONFIG_PM_WAKELOCKS_GC
81 #define WL_GC_COUNT_MAX 100
82 #define WL_GC_TIME_SEC  300
83
84 static void __wakelocks_gc(struct work_struct *work);
85 static LIST_HEAD(wakelocks_lru_list);
86 static DECLARE_WORK(wakelock_work, __wakelocks_gc);
87 static unsigned int wakelocks_gc_count;
88
89 static inline void wakelocks_lru_add(struct wakelock *wl)
90 {
91         list_add(&wl->lru, &wakelocks_lru_list);
92 }
93
94 static inline void wakelocks_lru_most_recent(struct wakelock *wl)
95 {
96         list_move(&wl->lru, &wakelocks_lru_list);
97 }
98
99 static void __wakelocks_gc(struct work_struct *work)
100 {
101         struct wakelock *wl, *aux;
102         ktime_t now;
103
104         mutex_lock(&wakelocks_lock);
105
106         now = ktime_get();
107         list_for_each_entry_safe_reverse(wl, aux, &wakelocks_lru_list, lru) {
108                 u64 idle_time_ns;
109                 bool active;
110
111                 spin_lock_irq(&wl->ws.lock);
112                 idle_time_ns = ktime_to_ns(ktime_sub(now, wl->ws.last_time));
113                 active = wl->ws.active;
114                 spin_unlock_irq(&wl->ws.lock);
115
116                 if (idle_time_ns < ((u64)WL_GC_TIME_SEC * NSEC_PER_SEC))
117                         break;
118
119                 if (!active) {
120                         wakeup_source_remove(&wl->ws);
121                         rb_erase(&wl->node, &wakelocks_tree);
122                         list_del(&wl->lru);
123                         kfree(wl->name);
124                         kfree(wl);
125                         decrement_wakelocks_number();
126                 }
127         }
128         wakelocks_gc_count = 0;
129
130         mutex_unlock(&wakelocks_lock);
131 }
132
133 static void wakelocks_gc(void)
134 {
135         if (++wakelocks_gc_count <= WL_GC_COUNT_MAX)
136                 return;
137
138         schedule_work(&wakelock_work);
139 }
140 #else /* !CONFIG_PM_WAKELOCKS_GC */
141 static inline void wakelocks_lru_add(struct wakelock *wl) {}
142 static inline void wakelocks_lru_most_recent(struct wakelock *wl) {}
143 static inline void wakelocks_gc(void) {}
144 #endif /* !CONFIG_PM_WAKELOCKS_GC */
145
146 static struct wakelock *wakelock_lookup_add(const char *name, size_t len,
147                                             bool add_if_not_found)
148 {
149         struct rb_node **node = &wakelocks_tree.rb_node;
150         struct rb_node *parent = *node;
151         struct wakelock *wl;
152
153         while (*node) {
154                 int diff;
155
156                 parent = *node;
157                 wl = rb_entry(*node, struct wakelock, node);
158                 diff = strncmp(name, wl->name, len);
159                 if (diff == 0) {
160                         if (wl->name[len])
161                                 diff = -1;
162                         else
163                                 return wl;
164                 }
165                 if (diff < 0)
166                         node = &(*node)->rb_left;
167                 else
168                         node = &(*node)->rb_right;
169         }
170         if (!add_if_not_found)
171                 return ERR_PTR(-EINVAL);
172
173         if (wakelocks_limit_exceeded())
174                 return ERR_PTR(-ENOSPC);
175
176         /* Not found, we have to add a new one. */
177         wl = kzalloc(sizeof(*wl), GFP_KERNEL);
178         if (!wl)
179                 return ERR_PTR(-ENOMEM);
180
181         wl->name = kstrndup(name, len, GFP_KERNEL);
182         if (!wl->name) {
183                 kfree(wl);
184                 return ERR_PTR(-ENOMEM);
185         }
186         wl->ws.name = wl->name;
187         wl->ws.last_time = ktime_get();
188         wakeup_source_add(&wl->ws);
189         rb_link_node(&wl->node, parent, node);
190         rb_insert_color(&wl->node, &wakelocks_tree);
191         wakelocks_lru_add(wl);
192         increment_wakelocks_number();
193         return wl;
194 }
195
196 int pm_wake_lock(const char *buf)
197 {
198         const char *str = buf;
199         struct wakelock *wl;
200         u64 timeout_ns = 0;
201         size_t len;
202         int ret = 0;
203
204         if (!capable(CAP_BLOCK_SUSPEND))
205                 return -EPERM;
206
207         while (*str && !isspace(*str))
208                 str++;
209
210         len = str - buf;
211         if (!len)
212                 return -EINVAL;
213
214         if (*str && *str != '\n') {
215                 /* Find out if there's a valid timeout string appended. */
216                 ret = kstrtou64(skip_spaces(str), 10, &timeout_ns);
217                 if (ret)
218                         return -EINVAL;
219         }
220
221         mutex_lock(&wakelocks_lock);
222
223         wl = wakelock_lookup_add(buf, len, true);
224         if (IS_ERR(wl)) {
225                 ret = PTR_ERR(wl);
226                 goto out;
227         }
228         if (timeout_ns) {
229                 u64 timeout_ms = timeout_ns + NSEC_PER_MSEC - 1;
230
231                 do_div(timeout_ms, NSEC_PER_MSEC);
232                 __pm_wakeup_event(&wl->ws, timeout_ms);
233         } else {
234                 __pm_stay_awake(&wl->ws);
235         }
236
237         wakelocks_lru_most_recent(wl);
238
239  out:
240         mutex_unlock(&wakelocks_lock);
241         return ret;
242 }
243
244 int pm_wake_unlock(const char *buf)
245 {
246         struct wakelock *wl;
247         size_t len;
248         int ret = 0;
249
250         if (!capable(CAP_BLOCK_SUSPEND))
251                 return -EPERM;
252
253         len = strlen(buf);
254         if (!len)
255                 return -EINVAL;
256
257         if (buf[len-1] == '\n')
258                 len--;
259
260         if (!len)
261                 return -EINVAL;
262
263         mutex_lock(&wakelocks_lock);
264
265         wl = wakelock_lookup_add(buf, len, false);
266         if (IS_ERR(wl)) {
267                 ret = PTR_ERR(wl);
268                 goto out;
269         }
270         __pm_relax(&wl->ws);
271
272         wakelocks_lru_most_recent(wl);
273         wakelocks_gc();
274
275  out:
276         mutex_unlock(&wakelocks_lock);
277         return ret;
278 }