Skip to content

Commit b86ff98

Browse files
committed
PM / Sleep: Add user space interface for manipulating wakeup sources, v3
Android allows user space to manipulate wakelocks using two sysfs file located in /sys/power/, wake_lock and wake_unlock. Writing a wakelock name and optionally a timeout to the wake_lock file causes the wakelock whose name was written to be acquired (it is created before is necessary), optionally with the given timeout. Writing the name of a wakelock to wake_unlock causes that wakelock to be released. Implement an analogous interface for user space using wakeup sources. Add the /sys/power/wake_lock and /sys/power/wake_unlock files allowing user space to create, activate and deactivate wakeup sources, such that writing a name and optionally a timeout to wake_lock causes the wakeup source of that name to be activated, optionally with the given timeout. If that wakeup source doesn't exist, it will be created and then activated. Writing a name to wake_unlock causes the wakeup source of that name, if there is one, to be deactivated. Wakeup sources created with the help of wake_lock that haven't been used for more than 5 minutes are garbage collected and destroyed. Moreover, there can be only WL_NUMBER_LIMIT wakeup sources created with the help of wake_lock present at a time. The data type used to track wakeup sources created by user space is called "struct wakelock" to indicate the origins of this feature. This version of the patch includes an rbtree manipulation fix from John Stultz. Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl> Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Reviewed-by: NeilBrown <neilb@suse.de>
1 parent 5585094 commit b86ff98

7 files changed

Lines changed: 317 additions & 0 deletions

File tree

‎Documentation/ABI/testing/sysfs-power‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,45 @@ Description:
189189

190190
Reading from this file causes the last string successfully
191191
written to it to be returned.
192+
193+
What: /sys/power/wake_lock
194+
Date: February 2012
195+
Contact: Rafael J. Wysocki <rjw@sisk.pl>
196+
Description:
197+
The /sys/power/wake_lock file allows user space to create
198+
wakeup source objects and activate them on demand (if one of
199+
those wakeup sources is active, reads from the
200+
/sys/power/wakeup_count file block or return false). When a
201+
string without white space is written to /sys/power/wake_lock,
202+
it will be assumed to represent a wakeup source name. If there
203+
is a wakeup source object with that name, it will be activated
204+
(unless active already). Otherwise, a new wakeup source object
205+
will be registered, assigned the given name and activated.
206+
If a string written to /sys/power/wake_lock contains white
207+
space, the part of the string preceding the white space will be
208+
regarded as a wakeup source name and handled as descrived above.
209+
The other part of the string will be regarded as a timeout (in
210+
nanoseconds) such that the wakeup source will be automatically
211+
deactivated after it has expired. The timeout, if present, is
212+
set regardless of the current state of the wakeup source object
213+
in question.
214+
215+
Reads from this file return a string consisting of the names of
216+
wakeup sources created with the help of it that are active at
217+
the moment, separated with spaces.
218+
219+
220+
What: /sys/power/wake_unlock
221+
Date: February 2012
222+
Contact: Rafael J. Wysocki <rjw@sisk.pl>
223+
Description:
224+
The /sys/power/wake_unlock file allows user space to deactivate
225+
wakeup sources created with the help of /sys/power/wake_lock.
226+
When a string is written to /sys/power/wake_unlock, it will be
227+
assumed to represent the name of a wakeup source to deactivate.
228+
If a wakeup source object of that name exists and is active at
229+
the moment, it will be deactivated.
230+
231+
Reads from this file return a string consisting of the names of
232+
wakeup sources created with the help of /sys/power/wake_lock
233+
that are inactive at the moment, separated with spaces.

‎drivers/base/power/wakeup.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ void wakeup_source_add(struct wakeup_source *ws)
133133
spin_lock_init(&ws->lock);
134134
setup_timer(&ws->timer, pm_wakeup_timer_fn, (unsigned long)ws);
135135
ws->active = false;
136+
ws->last_time = ktime_get();
136137

137138
spin_lock_irq(&events_lock);
138139
list_add_rcu(&ws->entry, &wakeup_sources);

‎kernel/power/Kconfig‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@ config PM_AUTOSLEEP
111111
Allow the kernel to trigger a system transition into a global sleep
112112
state automatically whenever there are no active wakeup sources.
113113

114+
config PM_WAKELOCKS
115+
bool "User space wakeup sources interface"
116+
depends on PM_SLEEP
117+
default n
118+
---help---
119+
Allow user space to create, activate and deactivate wakeup source
120+
objects with the help of a sysfs-based interface.
121+
114122
config PM_RUNTIME
115123
bool "Run-time PM core functionality"
116124
depends on !IA64_HP_SIM

‎kernel/power/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ obj-$(CONFIG_PM_TEST_SUSPEND) += suspend_test.o
1010
obj-$(CONFIG_HIBERNATION) += hibernate.o snapshot.o swap.o user.o \
1111
block_io.o
1212
obj-$(CONFIG_PM_AUTOSLEEP) += autosleep.o
13+
obj-$(CONFIG_PM_WAKELOCKS) += wakelock.o
1314

1415
obj-$(CONFIG_MAGIC_SYSRQ) += poweroff.o

‎kernel/power/main.c‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,43 @@ static ssize_t autosleep_store(struct kobject *kobj,
431431

432432
power_attr(autosleep);
433433
#endif /* CONFIG_PM_AUTOSLEEP */
434+
435+
#ifdef CONFIG_PM_WAKELOCKS
436+
static ssize_t wake_lock_show(struct kobject *kobj,
437+
struct kobj_attribute *attr,
438+
char *buf)
439+
{
440+
return pm_show_wakelocks(buf, true);
441+
}
442+
443+
static ssize_t wake_lock_store(struct kobject *kobj,
444+
struct kobj_attribute *attr,
445+
const char *buf, size_t n)
446+
{
447+
int error = pm_wake_lock(buf);
448+
return error ? error : n;
449+
}
450+
451+
power_attr(wake_lock);
452+
453+
static ssize_t wake_unlock_show(struct kobject *kobj,
454+
struct kobj_attribute *attr,
455+
char *buf)
456+
{
457+
return pm_show_wakelocks(buf, false);
458+
}
459+
460+
static ssize_t wake_unlock_store(struct kobject *kobj,
461+
struct kobj_attribute *attr,
462+
const char *buf, size_t n)
463+
{
464+
int error = pm_wake_unlock(buf);
465+
return error ? error : n;
466+
}
467+
468+
power_attr(wake_unlock);
469+
470+
#endif /* CONFIG_PM_WAKELOCKS */
434471
#endif /* CONFIG_PM_SLEEP */
435472

436473
#ifdef CONFIG_PM_TRACE
@@ -487,6 +524,10 @@ static struct attribute * g[] = {
487524
#ifdef CONFIG_PM_AUTOSLEEP
488525
&autosleep_attr.attr,
489526
#endif
527+
#ifdef CONFIG_PM_WAKELOCKS
528+
&wake_lock_attr.attr,
529+
&wake_unlock_attr.attr,
530+
#endif
490531
#ifdef CONFIG_PM_DEBUG
491532
&pm_test_attr.attr,
492533
#endif

‎kernel/power/power.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,3 +282,12 @@ static inline void pm_autosleep_unlock(void) {}
282282
static inline suspend_state_t pm_autosleep_state(void) { return PM_SUSPEND_ON; }
283283

284284
#endif /* !CONFIG_PM_AUTOSLEEP */
285+
286+
#ifdef CONFIG_PM_WAKELOCKS
287+
288+
/* kernel/power/wakelock.c */
289+
extern ssize_t pm_show_wakelocks(char *buf, bool show_active);
290+
extern int pm_wake_lock(const char *buf);
291+
extern int pm_wake_unlock(const char *buf);
292+
293+
#endif /* !CONFIG_PM_WAKELOCKS */

‎kernel/power/wakelock.c‎

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
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/ctype.h>
13+
#include <linux/device.h>
14+
#include <linux/err.h>
15+
#include <linux/hrtimer.h>
16+
#include <linux/list.h>
17+
#include <linux/rbtree.h>
18+
#include <linux/slab.h>
19+
20+
#define WL_NUMBER_LIMIT 100
21+
#define WL_GC_COUNT_MAX 100
22+
#define WL_GC_TIME_SEC 300
23+
24+
static DEFINE_MUTEX(wakelocks_lock);
25+
26+
struct wakelock {
27+
char *name;
28+
struct rb_node node;
29+
struct wakeup_source ws;
30+
struct list_head lru;
31+
};
32+
33+
static struct rb_root wakelocks_tree = RB_ROOT;
34+
static LIST_HEAD(wakelocks_lru_list);
35+
static unsigned int number_of_wakelocks;
36+
static unsigned int wakelocks_gc_count;
37+
38+
ssize_t pm_show_wakelocks(char *buf, bool show_active)
39+
{
40+
struct rb_node *node;
41+
struct wakelock *wl;
42+
char *str = buf;
43+
char *end = buf + PAGE_SIZE;
44+
45+
mutex_lock(&wakelocks_lock);
46+
47+
for (node = rb_first(&wakelocks_tree); node; node = rb_next(node)) {
48+
wl = rb_entry(node, struct wakelock, node);
49+
if (wl->ws.active == show_active)
50+
str += scnprintf(str, end - str, "%s ", wl->name);
51+
}
52+
if (str > buf)
53+
str--;
54+
55+
str += scnprintf(str, end - str, "\n");
56+
57+
mutex_unlock(&wakelocks_lock);
58+
return (str - buf);
59+
}
60+
61+
static struct wakelock *wakelock_lookup_add(const char *name, size_t len,
62+
bool add_if_not_found)
63+
{
64+
struct rb_node **node = &wakelocks_tree.rb_node;
65+
struct rb_node *parent = *node;
66+
struct wakelock *wl;
67+
68+
while (*node) {
69+
int diff;
70+
71+
parent = *node;
72+
wl = rb_entry(*node, struct wakelock, node);
73+
diff = strncmp(name, wl->name, len);
74+
if (diff == 0) {
75+
if (wl->name[len])
76+
diff = -1;
77+
else
78+
return wl;
79+
}
80+
if (diff < 0)
81+
node = &(*node)->rb_left;
82+
else
83+
node = &(*node)->rb_right;
84+
}
85+
if (!add_if_not_found)
86+
return ERR_PTR(-EINVAL);
87+
88+
if (number_of_wakelocks > WL_NUMBER_LIMIT)
89+
return ERR_PTR(-ENOSPC);
90+
91+
/* Not found, we have to add a new one. */
92+
wl = kzalloc(sizeof(*wl), GFP_KERNEL);
93+
if (!wl)
94+
return ERR_PTR(-ENOMEM);
95+
96+
wl->name = kstrndup(name, len, GFP_KERNEL);
97+
if (!wl->name) {
98+
kfree(wl);
99+
return ERR_PTR(-ENOMEM);
100+
}
101+
wl->ws.name = wl->name;
102+
wakeup_source_add(&wl->ws);
103+
rb_link_node(&wl->node, parent, node);
104+
rb_insert_color(&wl->node, &wakelocks_tree);
105+
list_add(&wl->lru, &wakelocks_lru_list);
106+
number_of_wakelocks++;
107+
return wl;
108+
}
109+
110+
int pm_wake_lock(const char *buf)
111+
{
112+
const char *str = buf;
113+
struct wakelock *wl;
114+
u64 timeout_ns = 0;
115+
size_t len;
116+
int ret = 0;
117+
118+
while (*str && !isspace(*str))
119+
str++;
120+
121+
len = str - buf;
122+
if (!len)
123+
return -EINVAL;
124+
125+
if (*str && *str != '\n') {
126+
/* Find out if there's a valid timeout string appended. */
127+
ret = kstrtou64(skip_spaces(str), 10, &timeout_ns);
128+
if (ret)
129+
return -EINVAL;
130+
}
131+
132+
mutex_lock(&wakelocks_lock);
133+
134+
wl = wakelock_lookup_add(buf, len, true);
135+
if (IS_ERR(wl)) {
136+
ret = PTR_ERR(wl);
137+
goto out;
138+
}
139+
if (timeout_ns) {
140+
u64 timeout_ms = timeout_ns + NSEC_PER_MSEC - 1;
141+
142+
do_div(timeout_ms, NSEC_PER_MSEC);
143+
__pm_wakeup_event(&wl->ws, timeout_ms);
144+
} else {
145+
__pm_stay_awake(&wl->ws);
146+
}
147+
148+
list_move(&wl->lru, &wakelocks_lru_list);
149+
150+
out:
151+
mutex_unlock(&wakelocks_lock);
152+
return ret;
153+
}
154+
155+
static void wakelocks_gc(void)
156+
{
157+
struct wakelock *wl, *aux;
158+
ktime_t now = ktime_get();
159+
160+
list_for_each_entry_safe_reverse(wl, aux, &wakelocks_lru_list, lru) {
161+
u64 idle_time_ns;
162+
bool active;
163+
164+
spin_lock_irq(&wl->ws.lock);
165+
idle_time_ns = ktime_to_ns(ktime_sub(now, wl->ws.last_time));
166+
active = wl->ws.active;
167+
spin_unlock_irq(&wl->ws.lock);
168+
169+
if (idle_time_ns < ((u64)WL_GC_TIME_SEC * NSEC_PER_SEC))
170+
break;
171+
172+
if (!active) {
173+
wakeup_source_remove(&wl->ws);
174+
rb_erase(&wl->node, &wakelocks_tree);
175+
list_del(&wl->lru);
176+
kfree(wl->name);
177+
kfree(wl);
178+
number_of_wakelocks--;
179+
}
180+
}
181+
wakelocks_gc_count = 0;
182+
}
183+
184+
int pm_wake_unlock(const char *buf)
185+
{
186+
struct wakelock *wl;
187+
size_t len;
188+
int ret = 0;
189+
190+
len = strlen(buf);
191+
if (!len)
192+
return -EINVAL;
193+
194+
if (buf[len-1] == '\n')
195+
len--;
196+
197+
if (!len)
198+
return -EINVAL;
199+
200+
mutex_lock(&wakelocks_lock);
201+
202+
wl = wakelock_lookup_add(buf, len, false);
203+
if (IS_ERR(wl)) {
204+
ret = PTR_ERR(wl);
205+
goto out;
206+
}
207+
__pm_relax(&wl->ws);
208+
list_move(&wl->lru, &wakelocks_lru_list);
209+
if (++wakelocks_gc_count > WL_GC_COUNT_MAX)
210+
wakelocks_gc();
211+
212+
out:
213+
mutex_unlock(&wakelocks_lock);
214+
return ret;
215+
}

0 commit comments

Comments
 (0)