xref: /openbmc/linux/kernel/power/user.c (revision bd4af432)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * linux/kernel/power/user.c
4  *
5  * This file provides the user space interface for software suspend/resume.
6  *
7  * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl>
8  */
9 
10 #include <linux/suspend.h>
11 #include <linux/reboot.h>
12 #include <linux/string.h>
13 #include <linux/device.h>
14 #include <linux/miscdevice.h>
15 #include <linux/mm.h>
16 #include <linux/swap.h>
17 #include <linux/swapops.h>
18 #include <linux/pm.h>
19 #include <linux/fs.h>
20 #include <linux/compat.h>
21 #include <linux/console.h>
22 #include <linux/cpu.h>
23 #include <linux/freezer.h>
24 
25 #include <linux/uaccess.h>
26 
27 #include "power.h"
28 
29 
30 static struct snapshot_data {
31 	struct snapshot_handle handle;
32 	int swap;
33 	int mode;
34 	bool frozen;
35 	bool ready;
36 	bool platform_support;
37 	bool free_bitmaps;
38 } snapshot_state;
39 
40 atomic_t snapshot_device_available = ATOMIC_INIT(1);
41 
42 static int snapshot_open(struct inode *inode, struct file *filp)
43 {
44 	struct snapshot_data *data;
45 	int error, nr_calls = 0;
46 
47 	if (!hibernation_available())
48 		return -EPERM;
49 
50 	lock_system_sleep();
51 
52 	if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
53 		error = -EBUSY;
54 		goto Unlock;
55 	}
56 
57 	if ((filp->f_flags & O_ACCMODE) == O_RDWR) {
58 		atomic_inc(&snapshot_device_available);
59 		error = -ENOSYS;
60 		goto Unlock;
61 	}
62 	nonseekable_open(inode, filp);
63 	data = &snapshot_state;
64 	filp->private_data = data;
65 	memset(&data->handle, 0, sizeof(struct snapshot_handle));
66 	if ((filp->f_flags & O_ACCMODE) == O_RDONLY) {
67 		/* Hibernating.  The image device should be accessible. */
68 		data->swap = swsusp_resume_device ?
69 			swap_type_of(swsusp_resume_device, 0, NULL) : -1;
70 		data->mode = O_RDONLY;
71 		data->free_bitmaps = false;
72 		error = __pm_notifier_call_chain(PM_HIBERNATION_PREPARE, -1, &nr_calls);
73 		if (error)
74 			__pm_notifier_call_chain(PM_POST_HIBERNATION, --nr_calls, NULL);
75 	} else {
76 		/*
77 		 * Resuming.  We may need to wait for the image device to
78 		 * appear.
79 		 */
80 		wait_for_device_probe();
81 
82 		data->swap = -1;
83 		data->mode = O_WRONLY;
84 		error = __pm_notifier_call_chain(PM_RESTORE_PREPARE, -1, &nr_calls);
85 		if (!error) {
86 			error = create_basic_memory_bitmaps();
87 			data->free_bitmaps = !error;
88 		} else
89 			nr_calls--;
90 
91 		if (error)
92 			__pm_notifier_call_chain(PM_POST_RESTORE, nr_calls, NULL);
93 	}
94 	if (error)
95 		atomic_inc(&snapshot_device_available);
96 
97 	data->frozen = false;
98 	data->ready = false;
99 	data->platform_support = false;
100 
101  Unlock:
102 	unlock_system_sleep();
103 
104 	return error;
105 }
106 
107 static int snapshot_release(struct inode *inode, struct file *filp)
108 {
109 	struct snapshot_data *data;
110 
111 	lock_system_sleep();
112 
113 	swsusp_free();
114 	data = filp->private_data;
115 	free_all_swap_pages(data->swap);
116 	if (data->frozen) {
117 		pm_restore_gfp_mask();
118 		free_basic_memory_bitmaps();
119 		thaw_processes();
120 	} else if (data->free_bitmaps) {
121 		free_basic_memory_bitmaps();
122 	}
123 	pm_notifier_call_chain(data->mode == O_RDONLY ?
124 			PM_POST_HIBERNATION : PM_POST_RESTORE);
125 	atomic_inc(&snapshot_device_available);
126 
127 	unlock_system_sleep();
128 
129 	return 0;
130 }
131 
132 static ssize_t snapshot_read(struct file *filp, char __user *buf,
133                              size_t count, loff_t *offp)
134 {
135 	struct snapshot_data *data;
136 	ssize_t res;
137 	loff_t pg_offp = *offp & ~PAGE_MASK;
138 
139 	lock_system_sleep();
140 
141 	data = filp->private_data;
142 	if (!data->ready) {
143 		res = -ENODATA;
144 		goto Unlock;
145 	}
146 	if (!pg_offp) { /* on page boundary? */
147 		res = snapshot_read_next(&data->handle);
148 		if (res <= 0)
149 			goto Unlock;
150 	} else {
151 		res = PAGE_SIZE - pg_offp;
152 	}
153 
154 	res = simple_read_from_buffer(buf, count, &pg_offp,
155 			data_of(data->handle), res);
156 	if (res > 0)
157 		*offp += res;
158 
159  Unlock:
160 	unlock_system_sleep();
161 
162 	return res;
163 }
164 
165 static ssize_t snapshot_write(struct file *filp, const char __user *buf,
166                               size_t count, loff_t *offp)
167 {
168 	struct snapshot_data *data;
169 	ssize_t res;
170 	loff_t pg_offp = *offp & ~PAGE_MASK;
171 
172 	lock_system_sleep();
173 
174 	data = filp->private_data;
175 
176 	if (!pg_offp) {
177 		res = snapshot_write_next(&data->handle);
178 		if (res <= 0)
179 			goto unlock;
180 	} else {
181 		res = PAGE_SIZE - pg_offp;
182 	}
183 
184 	if (!data_of(data->handle)) {
185 		res = -EINVAL;
186 		goto unlock;
187 	}
188 
189 	res = simple_write_to_buffer(data_of(data->handle), res, &pg_offp,
190 			buf, count);
191 	if (res > 0)
192 		*offp += res;
193 unlock:
194 	unlock_system_sleep();
195 
196 	return res;
197 }
198 
199 struct compat_resume_swap_area {
200 	compat_loff_t offset;
201 	u32 dev;
202 } __packed;
203 
204 static int snapshot_set_swap_area(struct snapshot_data *data,
205 		void __user *argp)
206 {
207 	sector_t offset;
208 	dev_t swdev;
209 
210 	if (swsusp_swap_in_use())
211 		return -EPERM;
212 
213 	if (in_compat_syscall()) {
214 		struct compat_resume_swap_area swap_area;
215 
216 		if (copy_from_user(&swap_area, argp, sizeof(swap_area)))
217 			return -EFAULT;
218 		swdev = new_decode_dev(swap_area.dev);
219 		offset = swap_area.offset;
220 	} else {
221 		struct resume_swap_area swap_area;
222 
223 		if (copy_from_user(&swap_area, argp, sizeof(swap_area)))
224 			return -EFAULT;
225 		swdev = new_decode_dev(swap_area.dev);
226 		offset = swap_area.offset;
227 	}
228 
229 	/*
230 	 * User space encodes device types as two-byte values,
231 	 * so we need to recode them
232 	 */
233 	if (!swdev) {
234 		data->swap = -1;
235 		return -EINVAL;
236 	}
237 	data->swap = swap_type_of(swdev, offset, NULL);
238 	if (data->swap < 0)
239 		return -ENODEV;
240 	return 0;
241 }
242 
243 static long snapshot_ioctl(struct file *filp, unsigned int cmd,
244 							unsigned long arg)
245 {
246 	int error = 0;
247 	struct snapshot_data *data;
248 	loff_t size;
249 	sector_t offset;
250 
251 	if (_IOC_TYPE(cmd) != SNAPSHOT_IOC_MAGIC)
252 		return -ENOTTY;
253 	if (_IOC_NR(cmd) > SNAPSHOT_IOC_MAXNR)
254 		return -ENOTTY;
255 	if (!capable(CAP_SYS_ADMIN))
256 		return -EPERM;
257 
258 	if (!mutex_trylock(&system_transition_mutex))
259 		return -EBUSY;
260 
261 	lock_device_hotplug();
262 	data = filp->private_data;
263 
264 	switch (cmd) {
265 
266 	case SNAPSHOT_FREEZE:
267 		if (data->frozen)
268 			break;
269 
270 		ksys_sync_helper();
271 
272 		error = freeze_processes();
273 		if (error)
274 			break;
275 
276 		error = create_basic_memory_bitmaps();
277 		if (error)
278 			thaw_processes();
279 		else
280 			data->frozen = true;
281 
282 		break;
283 
284 	case SNAPSHOT_UNFREEZE:
285 		if (!data->frozen || data->ready)
286 			break;
287 		pm_restore_gfp_mask();
288 		free_basic_memory_bitmaps();
289 		data->free_bitmaps = false;
290 		thaw_processes();
291 		data->frozen = false;
292 		break;
293 
294 	case SNAPSHOT_CREATE_IMAGE:
295 		if (data->mode != O_RDONLY || !data->frozen  || data->ready) {
296 			error = -EPERM;
297 			break;
298 		}
299 		pm_restore_gfp_mask();
300 		error = hibernation_snapshot(data->platform_support);
301 		if (!error) {
302 			error = put_user(in_suspend, (int __user *)arg);
303 			data->ready = !freezer_test_done && !error;
304 			freezer_test_done = false;
305 		}
306 		break;
307 
308 	case SNAPSHOT_ATOMIC_RESTORE:
309 		snapshot_write_finalize(&data->handle);
310 		if (data->mode != O_WRONLY || !data->frozen ||
311 		    !snapshot_image_loaded(&data->handle)) {
312 			error = -EPERM;
313 			break;
314 		}
315 		error = hibernation_restore(data->platform_support);
316 		break;
317 
318 	case SNAPSHOT_FREE:
319 		swsusp_free();
320 		memset(&data->handle, 0, sizeof(struct snapshot_handle));
321 		data->ready = false;
322 		/*
323 		 * It is necessary to thaw kernel threads here, because
324 		 * SNAPSHOT_CREATE_IMAGE may be invoked directly after
325 		 * SNAPSHOT_FREE.  In that case, if kernel threads were not
326 		 * thawed, the preallocation of memory carried out by
327 		 * hibernation_snapshot() might run into problems (i.e. it
328 		 * might fail or even deadlock).
329 		 */
330 		thaw_kernel_threads();
331 		break;
332 
333 	case SNAPSHOT_PREF_IMAGE_SIZE:
334 		image_size = arg;
335 		break;
336 
337 	case SNAPSHOT_GET_IMAGE_SIZE:
338 		if (!data->ready) {
339 			error = -ENODATA;
340 			break;
341 		}
342 		size = snapshot_get_image_size();
343 		size <<= PAGE_SHIFT;
344 		error = put_user(size, (loff_t __user *)arg);
345 		break;
346 
347 	case SNAPSHOT_AVAIL_SWAP_SIZE:
348 		size = count_swap_pages(data->swap, 1);
349 		size <<= PAGE_SHIFT;
350 		error = put_user(size, (loff_t __user *)arg);
351 		break;
352 
353 	case SNAPSHOT_ALLOC_SWAP_PAGE:
354 		if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
355 			error = -ENODEV;
356 			break;
357 		}
358 		offset = alloc_swapdev_block(data->swap);
359 		if (offset) {
360 			offset <<= PAGE_SHIFT;
361 			error = put_user(offset, (loff_t __user *)arg);
362 		} else {
363 			error = -ENOSPC;
364 		}
365 		break;
366 
367 	case SNAPSHOT_FREE_SWAP_PAGES:
368 		if (data->swap < 0 || data->swap >= MAX_SWAPFILES) {
369 			error = -ENODEV;
370 			break;
371 		}
372 		free_all_swap_pages(data->swap);
373 		break;
374 
375 	case SNAPSHOT_S2RAM:
376 		if (!data->frozen) {
377 			error = -EPERM;
378 			break;
379 		}
380 		/*
381 		 * Tasks are frozen and the notifiers have been called with
382 		 * PM_HIBERNATION_PREPARE
383 		 */
384 		error = suspend_devices_and_enter(PM_SUSPEND_MEM);
385 		data->ready = false;
386 		break;
387 
388 	case SNAPSHOT_PLATFORM_SUPPORT:
389 		data->platform_support = !!arg;
390 		break;
391 
392 	case SNAPSHOT_POWER_OFF:
393 		if (data->platform_support)
394 			error = hibernation_platform_enter();
395 		break;
396 
397 	case SNAPSHOT_SET_SWAP_AREA:
398 		error = snapshot_set_swap_area(data, (void __user *)arg);
399 		break;
400 
401 	default:
402 		error = -ENOTTY;
403 
404 	}
405 
406 	unlock_device_hotplug();
407 	mutex_unlock(&system_transition_mutex);
408 
409 	return error;
410 }
411 
412 #ifdef CONFIG_COMPAT
413 static long
414 snapshot_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
415 {
416 	BUILD_BUG_ON(sizeof(loff_t) != sizeof(compat_loff_t));
417 
418 	switch (cmd) {
419 	case SNAPSHOT_GET_IMAGE_SIZE:
420 	case SNAPSHOT_AVAIL_SWAP_SIZE:
421 	case SNAPSHOT_ALLOC_SWAP_PAGE:
422 	case SNAPSHOT_CREATE_IMAGE:
423 	case SNAPSHOT_SET_SWAP_AREA:
424 		return snapshot_ioctl(file, cmd,
425 				      (unsigned long) compat_ptr(arg));
426 	default:
427 		return snapshot_ioctl(file, cmd, arg);
428 	}
429 }
430 #endif /* CONFIG_COMPAT */
431 
432 static const struct file_operations snapshot_fops = {
433 	.open = snapshot_open,
434 	.release = snapshot_release,
435 	.read = snapshot_read,
436 	.write = snapshot_write,
437 	.llseek = no_llseek,
438 	.unlocked_ioctl = snapshot_ioctl,
439 #ifdef CONFIG_COMPAT
440 	.compat_ioctl = snapshot_compat_ioctl,
441 #endif
442 };
443 
444 static struct miscdevice snapshot_device = {
445 	.minor = SNAPSHOT_MINOR,
446 	.name = "snapshot",
447 	.fops = &snapshot_fops,
448 };
449 
450 static int __init snapshot_device_init(void)
451 {
452 	return misc_register(&snapshot_device);
453 };
454 
455 device_initcall(snapshot_device_init);
456