diff options
Diffstat (limited to 'kernel/power/user.c')
-rw-r--r-- | kernel/power/user.c | 71 |
1 files changed, 45 insertions, 26 deletions
diff --git a/kernel/power/user.c b/kernel/power/user.c index f5512cb3aa86..a6332a313262 100644 --- a/kernel/power/user.c +++ b/kernel/power/user.c | |||
@@ -23,6 +23,7 @@ | |||
23 | #include <linux/console.h> | 23 | #include <linux/console.h> |
24 | #include <linux/cpu.h> | 24 | #include <linux/cpu.h> |
25 | #include <linux/freezer.h> | 25 | #include <linux/freezer.h> |
26 | #include <linux/smp_lock.h> | ||
26 | 27 | ||
27 | #include <asm/uaccess.h> | 28 | #include <asm/uaccess.h> |
28 | 29 | ||
@@ -69,16 +70,22 @@ static int snapshot_open(struct inode *inode, struct file *filp) | |||
69 | struct snapshot_data *data; | 70 | struct snapshot_data *data; |
70 | int error; | 71 | int error; |
71 | 72 | ||
72 | if (!atomic_add_unless(&snapshot_device_available, -1, 0)) | 73 | mutex_lock(&pm_mutex); |
73 | return -EBUSY; | 74 | |
75 | if (!atomic_add_unless(&snapshot_device_available, -1, 0)) { | ||
76 | error = -EBUSY; | ||
77 | goto Unlock; | ||
78 | } | ||
74 | 79 | ||
75 | if ((filp->f_flags & O_ACCMODE) == O_RDWR) { | 80 | if ((filp->f_flags & O_ACCMODE) == O_RDWR) { |
76 | atomic_inc(&snapshot_device_available); | 81 | atomic_inc(&snapshot_device_available); |
77 | return -ENOSYS; | 82 | error = -ENOSYS; |
83 | goto Unlock; | ||
78 | } | 84 | } |
79 | if(create_basic_memory_bitmaps()) { | 85 | if(create_basic_memory_bitmaps()) { |
80 | atomic_inc(&snapshot_device_available); | 86 | atomic_inc(&snapshot_device_available); |
81 | return -ENOMEM; | 87 | error = -ENOMEM; |
88 | goto Unlock; | ||
82 | } | 89 | } |
83 | nonseekable_open(inode, filp); | 90 | nonseekable_open(inode, filp); |
84 | data = &snapshot_state; | 91 | data = &snapshot_state; |
@@ -98,33 +105,36 @@ static int snapshot_open(struct inode *inode, struct file *filp) | |||
98 | if (error) | 105 | if (error) |
99 | pm_notifier_call_chain(PM_POST_HIBERNATION); | 106 | pm_notifier_call_chain(PM_POST_HIBERNATION); |
100 | } | 107 | } |
101 | if (error) { | 108 | if (error) |
102 | atomic_inc(&snapshot_device_available); | 109 | atomic_inc(&snapshot_device_available); |
103 | return error; | ||
104 | } | ||
105 | data->frozen = 0; | 110 | data->frozen = 0; |
106 | data->ready = 0; | 111 | data->ready = 0; |
107 | data->platform_support = 0; | 112 | data->platform_support = 0; |
108 | 113 | ||
109 | return 0; | 114 | Unlock: |
115 | mutex_unlock(&pm_mutex); | ||
116 | |||
117 | return error; | ||
110 | } | 118 | } |
111 | 119 | ||
112 | static int snapshot_release(struct inode *inode, struct file *filp) | 120 | static int snapshot_release(struct inode *inode, struct file *filp) |
113 | { | 121 | { |
114 | struct snapshot_data *data; | 122 | struct snapshot_data *data; |
115 | 123 | ||
124 | mutex_lock(&pm_mutex); | ||
125 | |||
116 | swsusp_free(); | 126 | swsusp_free(); |
117 | free_basic_memory_bitmaps(); | 127 | free_basic_memory_bitmaps(); |
118 | data = filp->private_data; | 128 | data = filp->private_data; |
119 | free_all_swap_pages(data->swap); | 129 | free_all_swap_pages(data->swap); |
120 | if (data->frozen) { | 130 | if (data->frozen) |
121 | mutex_lock(&pm_mutex); | ||
122 | thaw_processes(); | 131 | thaw_processes(); |
123 | mutex_unlock(&pm_mutex); | ||
124 | } | ||
125 | pm_notifier_call_chain(data->mode == O_WRONLY ? | 132 | pm_notifier_call_chain(data->mode == O_WRONLY ? |
126 | PM_POST_HIBERNATION : PM_POST_RESTORE); | 133 | PM_POST_HIBERNATION : PM_POST_RESTORE); |
127 | atomic_inc(&snapshot_device_available); | 134 | atomic_inc(&snapshot_device_available); |
135 | |||
136 | mutex_unlock(&pm_mutex); | ||
137 | |||
128 | return 0; | 138 | return 0; |
129 | } | 139 | } |
130 | 140 | ||
@@ -134,9 +144,13 @@ static ssize_t snapshot_read(struct file *filp, char __user *buf, | |||
134 | struct snapshot_data *data; | 144 | struct snapshot_data *data; |
135 | ssize_t res; | 145 | ssize_t res; |
136 | 146 | ||
147 | mutex_lock(&pm_mutex); | ||
148 | |||
137 | data = filp->private_data; | 149 | data = filp->private_data; |
138 | if (!data->ready) | 150 | if (!data->ready) { |
139 | return -ENODATA; | 151 | res = -ENODATA; |
152 | goto Unlock; | ||
153 | } | ||
140 | res = snapshot_read_next(&data->handle, count); | 154 | res = snapshot_read_next(&data->handle, count); |
141 | if (res > 0) { | 155 | if (res > 0) { |
142 | if (copy_to_user(buf, data_of(data->handle), res)) | 156 | if (copy_to_user(buf, data_of(data->handle), res)) |
@@ -144,6 +158,10 @@ static ssize_t snapshot_read(struct file *filp, char __user *buf, | |||
144 | else | 158 | else |
145 | *offp = data->handle.offset; | 159 | *offp = data->handle.offset; |
146 | } | 160 | } |
161 | |||
162 | Unlock: | ||
163 | mutex_unlock(&pm_mutex); | ||
164 | |||
147 | return res; | 165 | return res; |
148 | } | 166 | } |
149 | 167 | ||
@@ -153,6 +171,8 @@ static ssize_t snapshot_write(struct file *filp, const char __user *buf, | |||
153 | struct snapshot_data *data; | 171 | struct snapshot_data *data; |
154 | ssize_t res; | 172 | ssize_t res; |
155 | 173 | ||
174 | mutex_lock(&pm_mutex); | ||
175 | |||
156 | data = filp->private_data; | 176 | data = filp->private_data; |
157 | res = snapshot_write_next(&data->handle, count); | 177 | res = snapshot_write_next(&data->handle, count); |
158 | if (res > 0) { | 178 | if (res > 0) { |
@@ -161,11 +181,14 @@ static ssize_t snapshot_write(struct file *filp, const char __user *buf, | |||
161 | else | 181 | else |
162 | *offp = data->handle.offset; | 182 | *offp = data->handle.offset; |
163 | } | 183 | } |
184 | |||
185 | mutex_unlock(&pm_mutex); | ||
186 | |||
164 | return res; | 187 | return res; |
165 | } | 188 | } |
166 | 189 | ||
167 | static int snapshot_ioctl(struct inode *inode, struct file *filp, | 190 | static long snapshot_ioctl(struct file *filp, unsigned int cmd, |
168 | unsigned int cmd, unsigned long arg) | 191 | unsigned long arg) |
169 | { | 192 | { |
170 | int error = 0; | 193 | int error = 0; |
171 | struct snapshot_data *data; | 194 | struct snapshot_data *data; |
@@ -179,6 +202,9 @@ static int snapshot_ioctl(struct inode *inode, struct file *filp, | |||
179 | if (!capable(CAP_SYS_ADMIN)) | 202 | if (!capable(CAP_SYS_ADMIN)) |
180 | return -EPERM; | 203 | return -EPERM; |
181 | 204 | ||
205 | if (!mutex_trylock(&pm_mutex)) | ||
206 | return -EBUSY; | ||
207 | |||
182 | data = filp->private_data; | 208 | data = filp->private_data; |
183 | 209 | ||
184 | switch (cmd) { | 210 | switch (cmd) { |
@@ -186,7 +212,6 @@ static int snapshot_ioctl(struct inode *inode, struct file *filp, | |||
186 | case SNAPSHOT_FREEZE: | 212 | case SNAPSHOT_FREEZE: |
187 | if (data->frozen) | 213 | if (data->frozen) |
188 | break; | 214 | break; |
189 | mutex_lock(&pm_mutex); | ||
190 | printk("Syncing filesystems ... "); | 215 | printk("Syncing filesystems ... "); |
191 | sys_sync(); | 216 | sys_sync(); |
192 | printk("done.\n"); | 217 | printk("done.\n"); |
@@ -194,7 +219,6 @@ static int snapshot_ioctl(struct inode *inode, struct file *filp, | |||
194 | error = freeze_processes(); | 219 | error = freeze_processes(); |
195 | if (error) | 220 | if (error) |
196 | thaw_processes(); | 221 | thaw_processes(); |
197 | mutex_unlock(&pm_mutex); | ||
198 | if (!error) | 222 | if (!error) |
199 | data->frozen = 1; | 223 | data->frozen = 1; |
200 | break; | 224 | break; |
@@ -202,9 +226,7 @@ static int snapshot_ioctl(struct inode *inode, struct file *filp, | |||
202 | case SNAPSHOT_UNFREEZE: | 226 | case SNAPSHOT_UNFREEZE: |
203 | if (!data->frozen || data->ready) | 227 | if (!data->frozen || data->ready) |
204 | break; | 228 | break; |
205 | mutex_lock(&pm_mutex); | ||
206 | thaw_processes(); | 229 | thaw_processes(); |
207 | mutex_unlock(&pm_mutex); | ||
208 | data->frozen = 0; | 230 | data->frozen = 0; |
209 | break; | 231 | break; |
210 | 232 | ||
@@ -307,16 +329,11 @@ static int snapshot_ioctl(struct inode *inode, struct file *filp, | |||
307 | error = -EPERM; | 329 | error = -EPERM; |
308 | break; | 330 | break; |
309 | } | 331 | } |
310 | if (!mutex_trylock(&pm_mutex)) { | ||
311 | error = -EBUSY; | ||
312 | break; | ||
313 | } | ||
314 | /* | 332 | /* |
315 | * Tasks are frozen and the notifiers have been called with | 333 | * Tasks are frozen and the notifiers have been called with |
316 | * PM_HIBERNATION_PREPARE | 334 | * PM_HIBERNATION_PREPARE |
317 | */ | 335 | */ |
318 | error = suspend_devices_and_enter(PM_SUSPEND_MEM); | 336 | error = suspend_devices_and_enter(PM_SUSPEND_MEM); |
319 | mutex_unlock(&pm_mutex); | ||
320 | break; | 337 | break; |
321 | 338 | ||
322 | case SNAPSHOT_PLATFORM_SUPPORT: | 339 | case SNAPSHOT_PLATFORM_SUPPORT: |
@@ -390,6 +407,8 @@ static int snapshot_ioctl(struct inode *inode, struct file *filp, | |||
390 | 407 | ||
391 | } | 408 | } |
392 | 409 | ||
410 | mutex_unlock(&pm_mutex); | ||
411 | |||
393 | return error; | 412 | return error; |
394 | } | 413 | } |
395 | 414 | ||
@@ -399,7 +418,7 @@ static const struct file_operations snapshot_fops = { | |||
399 | .read = snapshot_read, | 418 | .read = snapshot_read, |
400 | .write = snapshot_write, | 419 | .write = snapshot_write, |
401 | .llseek = no_llseek, | 420 | .llseek = no_llseek, |
402 | .ioctl = snapshot_ioctl, | 421 | .unlocked_ioctl = snapshot_ioctl, |
403 | }; | 422 | }; |
404 | 423 | ||
405 | static struct miscdevice snapshot_device = { | 424 | static struct miscdevice snapshot_device = { |