nfnetlink_log: fix use after free
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / power / main.c
blobff3a6182f5f0df3fb479cbd56092dd0ee8e1ff2e
1 /*
2 * kernel/power/main.c - PM subsystem core functionality.
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
6 *
7 * This file is released under the GPLv2
9 */
11 #include <linux/module.h>
12 #include <linux/suspend.h>
13 #include <linux/kobject.h>
14 #include <linux/string.h>
15 #include <linux/delay.h>
16 #include <linux/errno.h>
17 #include <linux/init.h>
18 #include <linux/pm.h>
19 #include <linux/console.h>
20 #include <linux/cpu.h>
21 #include <linux/resume-trace.h>
22 #include <linux/freezer.h>
24 #include "power.h"
26 /*This is just an arbitrary number */
27 #define FREE_PAGE_NUMBER (100)
29 DEFINE_MUTEX(pm_mutex);
31 struct pm_ops *pm_ops;
32 suspend_disk_method_t pm_disk_mode = PM_DISK_PLATFORM;
34 /**
35 * pm_set_ops - Set the global power method table.
36 * @ops: Pointer to ops structure.
39 void pm_set_ops(struct pm_ops * ops)
41 mutex_lock(&pm_mutex);
42 pm_ops = ops;
43 mutex_unlock(&pm_mutex);
47 /**
48 * suspend_prepare - Do prep work before entering low-power state.
49 * @state: State we're entering.
51 * This is common code that is called for each state that we're
52 * entering. Allocate a console, stop all processes, then make sure
53 * the platform can enter the requested state.
56 static int suspend_prepare(suspend_state_t state)
58 int error;
59 unsigned int free_pages;
61 if (!pm_ops || !pm_ops->enter)
62 return -EPERM;
64 pm_prepare_console();
66 error = disable_nonboot_cpus();
67 if (error)
68 goto Enable_cpu;
70 if (freeze_processes()) {
71 error = -EAGAIN;
72 goto Thaw;
75 if ((free_pages = nr_free_pages()) < FREE_PAGE_NUMBER) {
76 pr_debug("PM: free some memory\n");
77 shrink_all_memory(FREE_PAGE_NUMBER - free_pages);
78 if (nr_free_pages() < FREE_PAGE_NUMBER) {
79 error = -ENOMEM;
80 printk(KERN_ERR "PM: No enough memory\n");
81 goto Thaw;
85 if (pm_ops->prepare) {
86 if ((error = pm_ops->prepare(state)))
87 goto Thaw;
90 suspend_console();
91 if ((error = device_suspend(PMSG_SUSPEND))) {
92 printk(KERN_ERR "Some devices failed to suspend\n");
93 goto Finish;
95 return 0;
96 Finish:
97 if (pm_ops->finish)
98 pm_ops->finish(state);
99 Thaw:
100 thaw_processes();
101 Enable_cpu:
102 enable_nonboot_cpus();
103 pm_restore_console();
104 return error;
108 int suspend_enter(suspend_state_t state)
110 int error = 0;
111 unsigned long flags;
113 local_irq_save(flags);
115 if ((error = device_power_down(PMSG_SUSPEND))) {
116 printk(KERN_ERR "Some devices failed to power down\n");
117 goto Done;
119 error = pm_ops->enter(state);
120 device_power_up();
121 Done:
122 local_irq_restore(flags);
123 return error;
128 * suspend_finish - Do final work before exiting suspend sequence.
129 * @state: State we're coming out of.
131 * Call platform code to clean up, restart processes, and free the
132 * console that we've allocated. This is not called for suspend-to-disk.
135 static void suspend_finish(suspend_state_t state)
137 device_resume();
138 resume_console();
139 thaw_processes();
140 enable_nonboot_cpus();
141 if (pm_ops && pm_ops->finish)
142 pm_ops->finish(state);
143 pm_restore_console();
149 static const char * const pm_states[PM_SUSPEND_MAX] = {
150 [PM_SUSPEND_STANDBY] = "standby",
151 [PM_SUSPEND_MEM] = "mem",
152 #ifdef CONFIG_SOFTWARE_SUSPEND
153 [PM_SUSPEND_DISK] = "disk",
154 #endif
157 static inline int valid_state(suspend_state_t state)
159 /* Suspend-to-disk does not really need low-level support.
160 * It can work with reboot if needed. */
161 if (state == PM_SUSPEND_DISK)
162 return 1;
164 if (pm_ops && pm_ops->valid && !pm_ops->valid(state))
165 return 0;
166 return 1;
171 * enter_state - Do common work of entering low-power state.
172 * @state: pm_state structure for state we're entering.
174 * Make sure we're the only ones trying to enter a sleep state. Fail
175 * if someone has beat us to it, since we don't want anything weird to
176 * happen when we wake up.
177 * Then, do the setup for suspend, enter the state, and cleaup (after
178 * we've woken up).
181 static int enter_state(suspend_state_t state)
183 int error;
185 if (!valid_state(state))
186 return -ENODEV;
187 if (!mutex_trylock(&pm_mutex))
188 return -EBUSY;
190 if (state == PM_SUSPEND_DISK) {
191 error = pm_suspend_disk();
192 goto Unlock;
195 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
196 if ((error = suspend_prepare(state)))
197 goto Unlock;
199 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
200 error = suspend_enter(state);
202 pr_debug("PM: Finishing wakeup.\n");
203 suspend_finish(state);
204 Unlock:
205 mutex_unlock(&pm_mutex);
206 return error;
210 * This is main interface to the outside world. It needs to be
211 * called from process context.
213 int software_suspend(void)
215 return enter_state(PM_SUSPEND_DISK);
220 * pm_suspend - Externally visible function for suspending system.
221 * @state: Enumarted value of state to enter.
223 * Determine whether or not value is within range, get state
224 * structure, and enter (above).
227 int pm_suspend(suspend_state_t state)
229 if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
230 return enter_state(state);
231 return -EINVAL;
234 EXPORT_SYMBOL(pm_suspend);
236 decl_subsys(power,NULL,NULL);
240 * state - control system power state.
242 * show() returns what states are supported, which is hard-coded to
243 * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
244 * 'disk' (Suspend-to-Disk).
246 * store() accepts one of those strings, translates it into the
247 * proper enumerated value, and initiates a suspend transition.
250 static ssize_t state_show(struct subsystem * subsys, char * buf)
252 int i;
253 char * s = buf;
255 for (i = 0; i < PM_SUSPEND_MAX; i++) {
256 if (pm_states[i] && valid_state(i))
257 s += sprintf(s,"%s ", pm_states[i]);
259 s += sprintf(s,"\n");
260 return (s - buf);
263 static ssize_t state_store(struct subsystem * subsys, const char * buf, size_t n)
265 suspend_state_t state = PM_SUSPEND_STANDBY;
266 const char * const *s;
267 char *p;
268 int error;
269 int len;
271 p = memchr(buf, '\n', n);
272 len = p ? p - buf : n;
274 for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
275 if (*s && !strncmp(buf, *s, len))
276 break;
278 if (state < PM_SUSPEND_MAX && *s)
279 error = enter_state(state);
280 else
281 error = -EINVAL;
282 return error ? error : n;
285 power_attr(state);
287 #ifdef CONFIG_PM_TRACE
288 int pm_trace_enabled;
290 static ssize_t pm_trace_show(struct subsystem * subsys, char * buf)
292 return sprintf(buf, "%d\n", pm_trace_enabled);
295 static ssize_t
296 pm_trace_store(struct subsystem * subsys, const char * buf, size_t n)
298 int val;
300 if (sscanf(buf, "%d", &val) == 1) {
301 pm_trace_enabled = !!val;
302 return n;
304 return -EINVAL;
307 power_attr(pm_trace);
309 static struct attribute * g[] = {
310 &state_attr.attr,
311 &pm_trace_attr.attr,
312 NULL,
314 #else
315 static struct attribute * g[] = {
316 &state_attr.attr,
317 NULL,
319 #endif /* CONFIG_PM_TRACE */
321 static struct attribute_group attr_group = {
322 .attrs = g,
326 static int __init pm_init(void)
328 int error = subsystem_register(&power_subsys);
329 if (!error)
330 error = sysfs_create_group(&power_subsys.kset.kobj,&attr_group);
331 return error;
334 core_initcall(pm_init);