Linux Audio

Check our new training course

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