| 1 | // SPDX-License-Identifier: GPL-2.0-only | 
|---|
| 2 | /* | 
|---|
| 3 | * kernel/power/hibernate.c - Hibernation (a.k.a suspend-to-disk) support. | 
|---|
| 4 | * | 
|---|
| 5 | * Copyright (c) 2003 Patrick Mochel | 
|---|
| 6 | * Copyright (c) 2003 Open Source Development Lab | 
|---|
| 7 | * Copyright (c) 2004 Pavel Machek <pavel@ucw.cz> | 
|---|
| 8 | * Copyright (c) 2009 Rafael J. Wysocki, Novell Inc. | 
|---|
| 9 | * Copyright (C) 2012 Bojan Smojver <bojan@rexursive.com> | 
|---|
| 10 | */ | 
|---|
| 11 |  | 
|---|
| 12 | #define pr_fmt(fmt) "PM: hibernation: " fmt | 
|---|
| 13 |  | 
|---|
| 14 | #include <crypto/acompress.h> | 
|---|
| 15 | #include <linux/blkdev.h> | 
|---|
| 16 | #include <linux/export.h> | 
|---|
| 17 | #include <linux/suspend.h> | 
|---|
| 18 | #include <linux/reboot.h> | 
|---|
| 19 | #include <linux/string.h> | 
|---|
| 20 | #include <linux/device.h> | 
|---|
| 21 | #include <linux/async.h> | 
|---|
| 22 | #include <linux/delay.h> | 
|---|
| 23 | #include <linux/fs.h> | 
|---|
| 24 | #include <linux/mount.h> | 
|---|
| 25 | #include <linux/pm.h> | 
|---|
| 26 | #include <linux/nmi.h> | 
|---|
| 27 | #include <linux/console.h> | 
|---|
| 28 | #include <linux/cpu.h> | 
|---|
| 29 | #include <linux/freezer.h> | 
|---|
| 30 | #include <linux/gfp.h> | 
|---|
| 31 | #include <linux/syscore_ops.h> | 
|---|
| 32 | #include <linux/ctype.h> | 
|---|
| 33 | #include <linux/ktime.h> | 
|---|
| 34 | #include <linux/security.h> | 
|---|
| 35 | #include <linux/secretmem.h> | 
|---|
| 36 | #include <trace/events/power.h> | 
|---|
| 37 |  | 
|---|
| 38 | #include "power.h" | 
|---|
| 39 |  | 
|---|
| 40 |  | 
|---|
| 41 | static int nocompress; | 
|---|
| 42 | static int noresume; | 
|---|
| 43 | static int nohibernate; | 
|---|
| 44 | static int resume_wait; | 
|---|
| 45 | static unsigned int resume_delay; | 
|---|
| 46 | static char resume_file[256] = CONFIG_PM_STD_PARTITION; | 
|---|
| 47 | dev_t swsusp_resume_device; | 
|---|
| 48 | sector_t swsusp_resume_block; | 
|---|
| 49 | __visible int in_suspend __nosavedata; | 
|---|
| 50 |  | 
|---|
| 51 | static char hibernate_compressor[CRYPTO_MAX_ALG_NAME] = CONFIG_HIBERNATION_DEF_COMP; | 
|---|
| 52 |  | 
|---|
| 53 | /* | 
|---|
| 54 | * Compression/decompression algorithm to be used while saving/loading | 
|---|
| 55 | * image to/from disk. This would later be used in 'kernel/power/swap.c' | 
|---|
| 56 | * to allocate comp streams. | 
|---|
| 57 | */ | 
|---|
| 58 | char hib_comp_algo[CRYPTO_MAX_ALG_NAME]; | 
|---|
| 59 |  | 
|---|
| 60 | enum { | 
|---|
| 61 | HIBERNATION_INVALID, | 
|---|
| 62 | HIBERNATION_PLATFORM, | 
|---|
| 63 | HIBERNATION_SHUTDOWN, | 
|---|
| 64 | HIBERNATION_REBOOT, | 
|---|
| 65 | #ifdef CONFIG_SUSPEND | 
|---|
| 66 | HIBERNATION_SUSPEND, | 
|---|
| 67 | #endif | 
|---|
| 68 | HIBERNATION_TEST_RESUME, | 
|---|
| 69 | /* keep last */ | 
|---|
| 70 | __HIBERNATION_AFTER_LAST | 
|---|
| 71 | }; | 
|---|
| 72 | #define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1) | 
|---|
| 73 | #define HIBERNATION_FIRST (HIBERNATION_INVALID + 1) | 
|---|
| 74 |  | 
|---|
| 75 | static int hibernation_mode = HIBERNATION_SHUTDOWN; | 
|---|
| 76 |  | 
|---|
| 77 | bool freezer_test_done; | 
|---|
| 78 |  | 
|---|
| 79 | static const struct platform_hibernation_ops *hibernation_ops; | 
|---|
| 80 |  | 
|---|
| 81 | static atomic_t hibernate_atomic = ATOMIC_INIT(1); | 
|---|
| 82 |  | 
|---|
| 83 | #ifdef CONFIG_SUSPEND | 
|---|
| 84 | /** | 
|---|
| 85 | * pm_hibernation_mode_is_suspend - Check if hibernation has been set to suspend | 
|---|
| 86 | */ | 
|---|
| 87 | bool pm_hibernation_mode_is_suspend(void) | 
|---|
| 88 | { | 
|---|
| 89 | return hibernation_mode == HIBERNATION_SUSPEND; | 
|---|
| 90 | } | 
|---|
| 91 | EXPORT_SYMBOL_GPL(pm_hibernation_mode_is_suspend); | 
|---|
| 92 | #endif | 
|---|
| 93 |  | 
|---|
| 94 | bool hibernate_acquire(void) | 
|---|
| 95 | { | 
|---|
| 96 | return atomic_add_unless(v: &hibernate_atomic, a: -1, u: 0); | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | void hibernate_release(void) | 
|---|
| 100 | { | 
|---|
| 101 | atomic_inc(v: &hibernate_atomic); | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | bool hibernation_in_progress(void) | 
|---|
| 105 | { | 
|---|
| 106 | return !atomic_read(v: &hibernate_atomic); | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | bool hibernation_available(void) | 
|---|
| 110 | { | 
|---|
| 111 | return nohibernate == 0 && | 
|---|
| 112 | !security_locked_down(what: LOCKDOWN_HIBERNATION) && | 
|---|
| 113 | !secretmem_active() && !cxl_mem_active(); | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | /** | 
|---|
| 117 | * hibernation_set_ops - Set the global hibernate operations. | 
|---|
| 118 | * @ops: Hibernation operations to use in subsequent hibernation transitions. | 
|---|
| 119 | */ | 
|---|
| 120 | void hibernation_set_ops(const struct platform_hibernation_ops *ops) | 
|---|
| 121 | { | 
|---|
| 122 | unsigned int sleep_flags; | 
|---|
| 123 |  | 
|---|
| 124 | if (ops && !(ops->begin && ops->end &&  ops->pre_snapshot | 
|---|
| 125 | && ops->prepare && ops->finish && ops->enter && ops->pre_restore | 
|---|
| 126 | && ops->restore_cleanup && ops->leave)) { | 
|---|
| 127 | WARN_ON(1); | 
|---|
| 128 | return; | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | sleep_flags = lock_system_sleep(); | 
|---|
| 132 |  | 
|---|
| 133 | hibernation_ops = ops; | 
|---|
| 134 | if (ops) | 
|---|
| 135 | hibernation_mode = HIBERNATION_PLATFORM; | 
|---|
| 136 | else if (hibernation_mode == HIBERNATION_PLATFORM) | 
|---|
| 137 | hibernation_mode = HIBERNATION_SHUTDOWN; | 
|---|
| 138 |  | 
|---|
| 139 | unlock_system_sleep(sleep_flags); | 
|---|
| 140 | } | 
|---|
| 141 | EXPORT_SYMBOL_GPL(hibernation_set_ops); | 
|---|
| 142 |  | 
|---|
| 143 | static bool entering_platform_hibernation; | 
|---|
| 144 |  | 
|---|
| 145 | bool system_entering_hibernation(void) | 
|---|
| 146 | { | 
|---|
| 147 | return entering_platform_hibernation; | 
|---|
| 148 | } | 
|---|
| 149 | EXPORT_SYMBOL(system_entering_hibernation); | 
|---|
| 150 |  | 
|---|
| 151 | #ifdef CONFIG_PM_DEBUG | 
|---|
| 152 | static unsigned int pm_test_delay = 5; | 
|---|
| 153 | module_param(pm_test_delay, uint, 0644); | 
|---|
| 154 | MODULE_PARM_DESC(pm_test_delay, | 
|---|
| 155 | "Number of seconds to wait before resuming from hibernation test"); | 
|---|
| 156 | static void hibernation_debug_sleep(void) | 
|---|
| 157 | { | 
|---|
| 158 | pr_info( "hibernation debug: Waiting for %d second(s).\n", | 
|---|
| 159 | pm_test_delay); | 
|---|
| 160 | mdelay(pm_test_delay * 1000); | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | static int hibernation_test(int level) | 
|---|
| 164 | { | 
|---|
| 165 | if (pm_test_level == level) { | 
|---|
| 166 | hibernation_debug_sleep(); | 
|---|
| 167 | return 1; | 
|---|
| 168 | } | 
|---|
| 169 | return 0; | 
|---|
| 170 | } | 
|---|
| 171 | #else /* !CONFIG_PM_DEBUG */ | 
|---|
| 172 | static int hibernation_test(int level) { return 0; } | 
|---|
| 173 | #endif /* !CONFIG_PM_DEBUG */ | 
|---|
| 174 |  | 
|---|
| 175 | /** | 
|---|
| 176 | * platform_begin - Call platform to start hibernation. | 
|---|
| 177 | * @platform_mode: Whether or not to use the platform driver. | 
|---|
| 178 | */ | 
|---|
| 179 | static int platform_begin(int platform_mode) | 
|---|
| 180 | { | 
|---|
| 181 | return (platform_mode && hibernation_ops) ? | 
|---|
| 182 | hibernation_ops->begin(PMSG_FREEZE) : 0; | 
|---|
| 183 | } | 
|---|
| 184 |  | 
|---|
| 185 | /** | 
|---|
| 186 | * platform_end - Call platform to finish transition to the working state. | 
|---|
| 187 | * @platform_mode: Whether or not to use the platform driver. | 
|---|
| 188 | */ | 
|---|
| 189 | static void platform_end(int platform_mode) | 
|---|
| 190 | { | 
|---|
| 191 | if (platform_mode && hibernation_ops) | 
|---|
| 192 | hibernation_ops->end(); | 
|---|
| 193 | } | 
|---|
| 194 |  | 
|---|
| 195 | /** | 
|---|
| 196 | * platform_pre_snapshot - Call platform to prepare the machine for hibernation. | 
|---|
| 197 | * @platform_mode: Whether or not to use the platform driver. | 
|---|
| 198 | * | 
|---|
| 199 | * Use the platform driver to prepare the system for creating a hibernate image, | 
|---|
| 200 | * if so configured, and return an error code if that fails. | 
|---|
| 201 | */ | 
|---|
| 202 |  | 
|---|
| 203 | static int platform_pre_snapshot(int platform_mode) | 
|---|
| 204 | { | 
|---|
| 205 | return (platform_mode && hibernation_ops) ? | 
|---|
| 206 | hibernation_ops->pre_snapshot() : 0; | 
|---|
| 207 | } | 
|---|
| 208 |  | 
|---|
| 209 | /** | 
|---|
| 210 | * platform_leave - Call platform to prepare a transition to the working state. | 
|---|
| 211 | * @platform_mode: Whether or not to use the platform driver. | 
|---|
| 212 | * | 
|---|
| 213 | * Use the platform driver prepare to prepare the machine for switching to the | 
|---|
| 214 | * normal mode of operation. | 
|---|
| 215 | * | 
|---|
| 216 | * This routine is called on one CPU with interrupts disabled. | 
|---|
| 217 | */ | 
|---|
| 218 | static void platform_leave(int platform_mode) | 
|---|
| 219 | { | 
|---|
| 220 | if (platform_mode && hibernation_ops) | 
|---|
| 221 | hibernation_ops->leave(); | 
|---|
| 222 | } | 
|---|
| 223 |  | 
|---|
| 224 | /** | 
|---|
| 225 | * platform_finish - Call platform to switch the system to the working state. | 
|---|
| 226 | * @platform_mode: Whether or not to use the platform driver. | 
|---|
| 227 | * | 
|---|
| 228 | * Use the platform driver to switch the machine to the normal mode of | 
|---|
| 229 | * operation. | 
|---|
| 230 | * | 
|---|
| 231 | * This routine must be called after platform_prepare(). | 
|---|
| 232 | */ | 
|---|
| 233 | static void platform_finish(int platform_mode) | 
|---|
| 234 | { | 
|---|
| 235 | if (platform_mode && hibernation_ops) | 
|---|
| 236 | hibernation_ops->finish(); | 
|---|
| 237 | } | 
|---|
| 238 |  | 
|---|
| 239 | /** | 
|---|
| 240 | * platform_pre_restore - Prepare for hibernate image restoration. | 
|---|
| 241 | * @platform_mode: Whether or not to use the platform driver. | 
|---|
| 242 | * | 
|---|
| 243 | * Use the platform driver to prepare the system for resume from a hibernation | 
|---|
| 244 | * image. | 
|---|
| 245 | * | 
|---|
| 246 | * If the restore fails after this function has been called, | 
|---|
| 247 | * platform_restore_cleanup() must be called. | 
|---|
| 248 | */ | 
|---|
| 249 | static int platform_pre_restore(int platform_mode) | 
|---|
| 250 | { | 
|---|
| 251 | return (platform_mode && hibernation_ops) ? | 
|---|
| 252 | hibernation_ops->pre_restore() : 0; | 
|---|
| 253 | } | 
|---|
| 254 |  | 
|---|
| 255 | /** | 
|---|
| 256 | * platform_restore_cleanup - Switch to the working state after failing restore. | 
|---|
| 257 | * @platform_mode: Whether or not to use the platform driver. | 
|---|
| 258 | * | 
|---|
| 259 | * Use the platform driver to switch the system to the normal mode of operation | 
|---|
| 260 | * after a failing restore. | 
|---|
| 261 | * | 
|---|
| 262 | * If platform_pre_restore() has been called before the failing restore, this | 
|---|
| 263 | * function must be called too, regardless of the result of | 
|---|
| 264 | * platform_pre_restore(). | 
|---|
| 265 | */ | 
|---|
| 266 | static void platform_restore_cleanup(int platform_mode) | 
|---|
| 267 | { | 
|---|
| 268 | if (platform_mode && hibernation_ops) | 
|---|
| 269 | hibernation_ops->restore_cleanup(); | 
|---|
| 270 | } | 
|---|
| 271 |  | 
|---|
| 272 | /** | 
|---|
| 273 | * platform_recover - Recover from a failure to suspend devices. | 
|---|
| 274 | * @platform_mode: Whether or not to use the platform driver. | 
|---|
| 275 | */ | 
|---|
| 276 | static void platform_recover(int platform_mode) | 
|---|
| 277 | { | 
|---|
| 278 | if (platform_mode && hibernation_ops && hibernation_ops->recover) | 
|---|
| 279 | hibernation_ops->recover(); | 
|---|
| 280 | } | 
|---|
| 281 |  | 
|---|
| 282 | /** | 
|---|
| 283 | * swsusp_show_speed - Print time elapsed between two events during hibernation. | 
|---|
| 284 | * @start: Starting event. | 
|---|
| 285 | * @stop: Final event. | 
|---|
| 286 | * @nr_pages: Number of memory pages processed between @start and @stop. | 
|---|
| 287 | * @msg: Additional diagnostic message to print. | 
|---|
| 288 | */ | 
|---|
| 289 | void swsusp_show_speed(ktime_t start, ktime_t stop, | 
|---|
| 290 | unsigned nr_pages, char *msg) | 
|---|
| 291 | { | 
|---|
| 292 | ktime_t diff; | 
|---|
| 293 | u64 elapsed_centisecs64; | 
|---|
| 294 | unsigned int centisecs; | 
|---|
| 295 | unsigned int k; | 
|---|
| 296 | unsigned int kps; | 
|---|
| 297 |  | 
|---|
| 298 | diff = ktime_sub(stop, start); | 
|---|
| 299 | elapsed_centisecs64 = ktime_divns(kt: diff, div: 10*NSEC_PER_MSEC); | 
|---|
| 300 | centisecs = elapsed_centisecs64; | 
|---|
| 301 | if (centisecs == 0) | 
|---|
| 302 | centisecs = 1;	/* avoid div-by-zero */ | 
|---|
| 303 | k = nr_pages * (PAGE_SIZE / 1024); | 
|---|
| 304 | kps = (k * 100) / centisecs; | 
|---|
| 305 | pr_info( "%s %u kbytes in %u.%02u seconds (%u.%02u MB/s)\n", | 
|---|
| 306 | msg, k, centisecs / 100, centisecs % 100, kps / 1000, | 
|---|
| 307 | (kps % 1000) / 10); | 
|---|
| 308 | } | 
|---|
| 309 |  | 
|---|
| 310 | __weak int arch_resume_nosmt(void) | 
|---|
| 311 | { | 
|---|
| 312 | return 0; | 
|---|
| 313 | } | 
|---|
| 314 |  | 
|---|
| 315 | /** | 
|---|
| 316 | * create_image - Create a hibernation image. | 
|---|
| 317 | * @platform_mode: Whether or not to use the platform driver. | 
|---|
| 318 | * | 
|---|
| 319 | * Execute device drivers' "late" and "noirq" freeze callbacks, create a | 
|---|
| 320 | * hibernation image and run the drivers' "noirq" and "early" thaw callbacks. | 
|---|
| 321 | * | 
|---|
| 322 | * Control reappears in this routine after the subsequent restore. | 
|---|
| 323 | */ | 
|---|
| 324 | static int create_image(int platform_mode) | 
|---|
| 325 | { | 
|---|
| 326 | int error; | 
|---|
| 327 |  | 
|---|
| 328 | error = dpm_suspend_end(PMSG_FREEZE); | 
|---|
| 329 | if (error) { | 
|---|
| 330 | pr_err( "Some devices failed to power down, aborting\n"); | 
|---|
| 331 | return error; | 
|---|
| 332 | } | 
|---|
| 333 |  | 
|---|
| 334 | error = platform_pre_snapshot(platform_mode); | 
|---|
| 335 | if (error || hibernation_test(level: TEST_PLATFORM)) | 
|---|
| 336 | goto Platform_finish; | 
|---|
| 337 |  | 
|---|
| 338 | error = pm_sleep_disable_secondary_cpus(); | 
|---|
| 339 | if (error || hibernation_test(level: TEST_CPUS)) | 
|---|
| 340 | goto Enable_cpus; | 
|---|
| 341 |  | 
|---|
| 342 | local_irq_disable(); | 
|---|
| 343 |  | 
|---|
| 344 | system_state = SYSTEM_SUSPEND; | 
|---|
| 345 |  | 
|---|
| 346 | error = syscore_suspend(); | 
|---|
| 347 | if (error) { | 
|---|
| 348 | pr_err( "Some system devices failed to power down, aborting\n"); | 
|---|
| 349 | goto Enable_irqs; | 
|---|
| 350 | } | 
|---|
| 351 |  | 
|---|
| 352 | if (hibernation_test(level: TEST_CORE) || pm_wakeup_pending()) | 
|---|
| 353 | goto Power_up; | 
|---|
| 354 |  | 
|---|
| 355 | in_suspend = 1; | 
|---|
| 356 | save_processor_state(); | 
|---|
| 357 | trace_suspend_resume(TPS( "machine_suspend"), PM_EVENT_HIBERNATE, start: true); | 
|---|
| 358 | error = swsusp_arch_suspend(); | 
|---|
| 359 | /* Restore control flow magically appears here */ | 
|---|
| 360 | restore_processor_state(); | 
|---|
| 361 | trace_suspend_resume(TPS( "machine_suspend"), PM_EVENT_HIBERNATE, start: false); | 
|---|
| 362 | if (error) | 
|---|
| 363 | pr_err( "Error %d creating image\n", error); | 
|---|
| 364 |  | 
|---|
| 365 | if (!in_suspend) { | 
|---|
| 366 | events_check_enabled = false; | 
|---|
| 367 | clear_or_poison_free_pages(); | 
|---|
| 368 | } | 
|---|
| 369 |  | 
|---|
| 370 | platform_leave(platform_mode); | 
|---|
| 371 |  | 
|---|
| 372 | Power_up: | 
|---|
| 373 | syscore_resume(); | 
|---|
| 374 |  | 
|---|
| 375 | Enable_irqs: | 
|---|
| 376 | system_state = SYSTEM_RUNNING; | 
|---|
| 377 | local_irq_enable(); | 
|---|
| 378 |  | 
|---|
| 379 | Enable_cpus: | 
|---|
| 380 | pm_sleep_enable_secondary_cpus(); | 
|---|
| 381 |  | 
|---|
| 382 | /* Allow architectures to do nosmt-specific post-resume dances */ | 
|---|
| 383 | if (!in_suspend) | 
|---|
| 384 | error = arch_resume_nosmt(); | 
|---|
| 385 |  | 
|---|
| 386 | Platform_finish: | 
|---|
| 387 | platform_finish(platform_mode); | 
|---|
| 388 |  | 
|---|
| 389 | dpm_resume_start(state: in_suspend ? | 
|---|
| 390 | (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE); | 
|---|
| 391 |  | 
|---|
| 392 | return error; | 
|---|
| 393 | } | 
|---|
| 394 |  | 
|---|
| 395 | static void shrink_shmem_memory(void) | 
|---|
| 396 | { | 
|---|
| 397 | struct sysinfo info; | 
|---|
| 398 | unsigned long nr_shmem_pages, nr_freed_pages; | 
|---|
| 399 |  | 
|---|
| 400 | si_meminfo(val: &info); | 
|---|
| 401 | nr_shmem_pages = info.sharedram; /* current page count used for shmem */ | 
|---|
| 402 | /* | 
|---|
| 403 | * The intent is to reclaim all shmem pages. Though shrink_all_memory() can | 
|---|
| 404 | * only reclaim about half of them, it's enough for creating the hibernation | 
|---|
| 405 | * image. | 
|---|
| 406 | */ | 
|---|
| 407 | nr_freed_pages = shrink_all_memory(nr_pages: nr_shmem_pages); | 
|---|
| 408 | pr_debug( "requested to reclaim %lu shmem pages, actually freed %lu pages\n", | 
|---|
| 409 | nr_shmem_pages, nr_freed_pages); | 
|---|
| 410 | } | 
|---|
| 411 |  | 
|---|
| 412 | /** | 
|---|
| 413 | * hibernation_snapshot - Quiesce devices and create a hibernation image. | 
|---|
| 414 | * @platform_mode: If set, use platform driver to prepare for the transition. | 
|---|
| 415 | * | 
|---|
| 416 | * This routine must be called with system_transition_mutex held. | 
|---|
| 417 | */ | 
|---|
| 418 | int hibernation_snapshot(int platform_mode) | 
|---|
| 419 | { | 
|---|
| 420 | pm_message_t msg; | 
|---|
| 421 | int error; | 
|---|
| 422 |  | 
|---|
| 423 | pm_suspend_clear_flags(); | 
|---|
| 424 | error = platform_begin(platform_mode); | 
|---|
| 425 | if (error) | 
|---|
| 426 | goto Close; | 
|---|
| 427 |  | 
|---|
| 428 | /* Preallocate image memory before shutting down devices. */ | 
|---|
| 429 | error = hibernate_preallocate_memory(); | 
|---|
| 430 | if (error) | 
|---|
| 431 | goto Close; | 
|---|
| 432 |  | 
|---|
| 433 | error = freeze_kernel_threads(); | 
|---|
| 434 | if (error) | 
|---|
| 435 | goto Cleanup; | 
|---|
| 436 |  | 
|---|
| 437 | if (hibernation_test(level: TEST_FREEZER)) { | 
|---|
| 438 |  | 
|---|
| 439 | /* | 
|---|
| 440 | * Indicate to the caller that we are returning due to a | 
|---|
| 441 | * successful freezer test. | 
|---|
| 442 | */ | 
|---|
| 443 | freezer_test_done = true; | 
|---|
| 444 | goto Thaw; | 
|---|
| 445 | } | 
|---|
| 446 |  | 
|---|
| 447 | error = dpm_prepare(PMSG_FREEZE); | 
|---|
| 448 | if (error) { | 
|---|
| 449 | dpm_complete(PMSG_RECOVER); | 
|---|
| 450 | goto Thaw; | 
|---|
| 451 | } | 
|---|
| 452 |  | 
|---|
| 453 | /* | 
|---|
| 454 | * Device drivers may move lots of data to shmem in dpm_prepare(). The shmem | 
|---|
| 455 | * pages will use lots of system memory, causing hibernation image creation | 
|---|
| 456 | * fail due to insufficient free memory. | 
|---|
| 457 | * This call is to force flush the shmem pages to swap disk and reclaim | 
|---|
| 458 | * the system memory so that image creation can succeed. | 
|---|
| 459 | */ | 
|---|
| 460 | shrink_shmem_memory(); | 
|---|
| 461 |  | 
|---|
| 462 | console_suspend_all(); | 
|---|
| 463 | pm_restrict_gfp_mask(); | 
|---|
| 464 |  | 
|---|
| 465 | error = dpm_suspend(PMSG_FREEZE); | 
|---|
| 466 |  | 
|---|
| 467 | if (error || hibernation_test(level: TEST_DEVICES)) | 
|---|
| 468 | platform_recover(platform_mode); | 
|---|
| 469 | else | 
|---|
| 470 | error = create_image(platform_mode); | 
|---|
| 471 |  | 
|---|
| 472 | /* | 
|---|
| 473 | * In the case that we call create_image() above, the control | 
|---|
| 474 | * returns here (1) after the image has been created or the | 
|---|
| 475 | * image creation has failed and (2) after a successful restore. | 
|---|
| 476 | */ | 
|---|
| 477 |  | 
|---|
| 478 | /* We may need to release the preallocated image pages here. */ | 
|---|
| 479 | if (error || !in_suspend) | 
|---|
| 480 | swsusp_free(); | 
|---|
| 481 |  | 
|---|
| 482 | msg = in_suspend ? (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE; | 
|---|
| 483 | dpm_resume(state: msg); | 
|---|
| 484 |  | 
|---|
| 485 | if (error || !in_suspend) | 
|---|
| 486 | pm_restore_gfp_mask(); | 
|---|
| 487 |  | 
|---|
| 488 | console_resume_all(); | 
|---|
| 489 | dpm_complete(state: msg); | 
|---|
| 490 |  | 
|---|
| 491 | Close: | 
|---|
| 492 | platform_end(platform_mode); | 
|---|
| 493 | return error; | 
|---|
| 494 |  | 
|---|
| 495 | Thaw: | 
|---|
| 496 | thaw_kernel_threads(); | 
|---|
| 497 | Cleanup: | 
|---|
| 498 | swsusp_free(); | 
|---|
| 499 | goto Close; | 
|---|
| 500 | } | 
|---|
| 501 |  | 
|---|
| 502 | int __weak hibernate_resume_nonboot_cpu_disable(void) | 
|---|
| 503 | { | 
|---|
| 504 | return suspend_disable_secondary_cpus(); | 
|---|
| 505 | } | 
|---|
| 506 |  | 
|---|
| 507 | /** | 
|---|
| 508 | * resume_target_kernel - Restore system state from a hibernation image. | 
|---|
| 509 | * @platform_mode: Whether or not to use the platform driver. | 
|---|
| 510 | * | 
|---|
| 511 | * Execute device drivers' "noirq" and "late" freeze callbacks, restore the | 
|---|
| 512 | * contents of highmem that have not been restored yet from the image and run | 
|---|
| 513 | * the low-level code that will restore the remaining contents of memory and | 
|---|
| 514 | * switch to the just restored target kernel. | 
|---|
| 515 | */ | 
|---|
| 516 | static int resume_target_kernel(bool platform_mode) | 
|---|
| 517 | { | 
|---|
| 518 | int error; | 
|---|
| 519 |  | 
|---|
| 520 | error = dpm_suspend_end(PMSG_QUIESCE); | 
|---|
| 521 | if (error) { | 
|---|
| 522 | pr_err( "Some devices failed to power down, aborting resume\n"); | 
|---|
| 523 | return error; | 
|---|
| 524 | } | 
|---|
| 525 |  | 
|---|
| 526 | error = platform_pre_restore(platform_mode); | 
|---|
| 527 | if (error) | 
|---|
| 528 | goto Cleanup; | 
|---|
| 529 |  | 
|---|
| 530 | cpuidle_pause(); | 
|---|
| 531 |  | 
|---|
| 532 | error = hibernate_resume_nonboot_cpu_disable(); | 
|---|
| 533 | if (error) | 
|---|
| 534 | goto Enable_cpus; | 
|---|
| 535 |  | 
|---|
| 536 | local_irq_disable(); | 
|---|
| 537 | system_state = SYSTEM_SUSPEND; | 
|---|
| 538 |  | 
|---|
| 539 | error = syscore_suspend(); | 
|---|
| 540 | if (error) | 
|---|
| 541 | goto Enable_irqs; | 
|---|
| 542 |  | 
|---|
| 543 | save_processor_state(); | 
|---|
| 544 | error = restore_highmem(); | 
|---|
| 545 | if (!error) { | 
|---|
| 546 | error = swsusp_arch_resume(); | 
|---|
| 547 | /* | 
|---|
| 548 | * The code below is only ever reached in case of a failure. | 
|---|
| 549 | * Otherwise, execution continues at the place where | 
|---|
| 550 | * swsusp_arch_suspend() was called. | 
|---|
| 551 | */ | 
|---|
| 552 | BUG_ON(!error); | 
|---|
| 553 | /* | 
|---|
| 554 | * This call to restore_highmem() reverts the changes made by | 
|---|
| 555 | * the previous one. | 
|---|
| 556 | */ | 
|---|
| 557 | restore_highmem(); | 
|---|
| 558 | } | 
|---|
| 559 | /* | 
|---|
| 560 | * The only reason why swsusp_arch_resume() can fail is memory being | 
|---|
| 561 | * very tight, so we have to free it as soon as we can to avoid | 
|---|
| 562 | * subsequent failures. | 
|---|
| 563 | */ | 
|---|
| 564 | swsusp_free(); | 
|---|
| 565 | restore_processor_state(); | 
|---|
| 566 | touch_softlockup_watchdog(); | 
|---|
| 567 |  | 
|---|
| 568 | syscore_resume(); | 
|---|
| 569 |  | 
|---|
| 570 | Enable_irqs: | 
|---|
| 571 | system_state = SYSTEM_RUNNING; | 
|---|
| 572 | local_irq_enable(); | 
|---|
| 573 |  | 
|---|
| 574 | Enable_cpus: | 
|---|
| 575 | pm_sleep_enable_secondary_cpus(); | 
|---|
| 576 |  | 
|---|
| 577 | Cleanup: | 
|---|
| 578 | platform_restore_cleanup(platform_mode); | 
|---|
| 579 |  | 
|---|
| 580 | dpm_resume_start(PMSG_RECOVER); | 
|---|
| 581 |  | 
|---|
| 582 | return error; | 
|---|
| 583 | } | 
|---|
| 584 |  | 
|---|
| 585 | /** | 
|---|
| 586 | * hibernation_restore - Quiesce devices and restore from a hibernation image. | 
|---|
| 587 | * @platform_mode: If set, use platform driver to prepare for the transition. | 
|---|
| 588 | * | 
|---|
| 589 | * This routine must be called with system_transition_mutex held.  If it is | 
|---|
| 590 | * successful, control reappears in the restored target kernel in | 
|---|
| 591 | * hibernation_snapshot(). | 
|---|
| 592 | */ | 
|---|
| 593 | int hibernation_restore(int platform_mode) | 
|---|
| 594 | { | 
|---|
| 595 | int error; | 
|---|
| 596 |  | 
|---|
| 597 | pm_prepare_console(); | 
|---|
| 598 | console_suspend_all(); | 
|---|
| 599 | error = dpm_suspend_start(PMSG_QUIESCE); | 
|---|
| 600 | if (!error) { | 
|---|
| 601 | error = resume_target_kernel(platform_mode); | 
|---|
| 602 | /* | 
|---|
| 603 | * The above should either succeed and jump to the new kernel, | 
|---|
| 604 | * or return with an error. Otherwise things are just | 
|---|
| 605 | * undefined, so let's be paranoid. | 
|---|
| 606 | */ | 
|---|
| 607 | BUG_ON(!error); | 
|---|
| 608 | } | 
|---|
| 609 | dpm_resume_end(PMSG_RECOVER); | 
|---|
| 610 | console_resume_all(); | 
|---|
| 611 | pm_restore_console(); | 
|---|
| 612 | return error; | 
|---|
| 613 | } | 
|---|
| 614 |  | 
|---|
| 615 | /** | 
|---|
| 616 | * hibernation_platform_enter - Power off the system using the platform driver. | 
|---|
| 617 | */ | 
|---|
| 618 | int hibernation_platform_enter(void) | 
|---|
| 619 | { | 
|---|
| 620 | int error; | 
|---|
| 621 |  | 
|---|
| 622 | if (!hibernation_ops) | 
|---|
| 623 | return -ENOSYS; | 
|---|
| 624 |  | 
|---|
| 625 | /* | 
|---|
| 626 | * We have cancelled the power transition by running | 
|---|
| 627 | * hibernation_ops->finish() before saving the image, so we should let | 
|---|
| 628 | * the firmware know that we're going to enter the sleep state after all | 
|---|
| 629 | */ | 
|---|
| 630 | error = hibernation_ops->begin(PMSG_HIBERNATE); | 
|---|
| 631 | if (error) | 
|---|
| 632 | goto Close; | 
|---|
| 633 |  | 
|---|
| 634 | entering_platform_hibernation = true; | 
|---|
| 635 | console_suspend_all(); | 
|---|
| 636 | error = dpm_suspend_start(PMSG_HIBERNATE); | 
|---|
| 637 | if (error) { | 
|---|
| 638 | if (hibernation_ops->recover) | 
|---|
| 639 | hibernation_ops->recover(); | 
|---|
| 640 | goto Resume_devices; | 
|---|
| 641 | } | 
|---|
| 642 |  | 
|---|
| 643 | error = dpm_suspend_end(PMSG_HIBERNATE); | 
|---|
| 644 | if (error) | 
|---|
| 645 | goto Resume_devices; | 
|---|
| 646 |  | 
|---|
| 647 | error = hibernation_ops->prepare(); | 
|---|
| 648 | if (error) | 
|---|
| 649 | goto Platform_finish; | 
|---|
| 650 |  | 
|---|
| 651 | error = pm_sleep_disable_secondary_cpus(); | 
|---|
| 652 | if (error) | 
|---|
| 653 | goto Enable_cpus; | 
|---|
| 654 |  | 
|---|
| 655 | local_irq_disable(); | 
|---|
| 656 | system_state = SYSTEM_SUSPEND; | 
|---|
| 657 |  | 
|---|
| 658 | error = syscore_suspend(); | 
|---|
| 659 | if (error) | 
|---|
| 660 | goto Enable_irqs; | 
|---|
| 661 |  | 
|---|
| 662 | if (pm_wakeup_pending()) { | 
|---|
| 663 | error = -EAGAIN; | 
|---|
| 664 | goto Power_up; | 
|---|
| 665 | } | 
|---|
| 666 |  | 
|---|
| 667 | hibernation_ops->enter(); | 
|---|
| 668 | /* We should never get here */ | 
|---|
| 669 | while (1); | 
|---|
| 670 |  | 
|---|
| 671 | Power_up: | 
|---|
| 672 | syscore_resume(); | 
|---|
| 673 | Enable_irqs: | 
|---|
| 674 | system_state = SYSTEM_RUNNING; | 
|---|
| 675 | local_irq_enable(); | 
|---|
| 676 |  | 
|---|
| 677 | Enable_cpus: | 
|---|
| 678 | pm_sleep_enable_secondary_cpus(); | 
|---|
| 679 |  | 
|---|
| 680 | Platform_finish: | 
|---|
| 681 | hibernation_ops->finish(); | 
|---|
| 682 |  | 
|---|
| 683 | dpm_resume_start(PMSG_RESTORE); | 
|---|
| 684 |  | 
|---|
| 685 | Resume_devices: | 
|---|
| 686 | entering_platform_hibernation = false; | 
|---|
| 687 | dpm_resume_end(PMSG_RESTORE); | 
|---|
| 688 | console_resume_all(); | 
|---|
| 689 |  | 
|---|
| 690 | Close: | 
|---|
| 691 | hibernation_ops->end(); | 
|---|
| 692 |  | 
|---|
| 693 | return error; | 
|---|
| 694 | } | 
|---|
| 695 |  | 
|---|
| 696 | /** | 
|---|
| 697 | * power_down - Shut the machine down for hibernation. | 
|---|
| 698 | * | 
|---|
| 699 | * Use the platform driver, if configured, to put the system into the sleep | 
|---|
| 700 | * state corresponding to hibernation, or try to power it off or reboot, | 
|---|
| 701 | * depending on the value of hibernation_mode. | 
|---|
| 702 | */ | 
|---|
| 703 | static void power_down(void) | 
|---|
| 704 | { | 
|---|
| 705 | int error; | 
|---|
| 706 |  | 
|---|
| 707 | #ifdef CONFIG_SUSPEND | 
|---|
| 708 | if (hibernation_mode == HIBERNATION_SUSPEND) { | 
|---|
| 709 | pm_restore_gfp_mask(); | 
|---|
| 710 | error = suspend_devices_and_enter(state: mem_sleep_current); | 
|---|
| 711 | if (!error) | 
|---|
| 712 | goto exit; | 
|---|
| 713 |  | 
|---|
| 714 | hibernation_mode = hibernation_ops ? HIBERNATION_PLATFORM : | 
|---|
| 715 | HIBERNATION_SHUTDOWN; | 
|---|
| 716 | } | 
|---|
| 717 | #endif | 
|---|
| 718 |  | 
|---|
| 719 | switch (hibernation_mode) { | 
|---|
| 720 | case HIBERNATION_REBOOT: | 
|---|
| 721 | kernel_restart(NULL); | 
|---|
| 722 | break; | 
|---|
| 723 | case HIBERNATION_PLATFORM: | 
|---|
| 724 | error = hibernation_platform_enter(); | 
|---|
| 725 | if (error == -EAGAIN || error == -EBUSY) { | 
|---|
| 726 | events_check_enabled = false; | 
|---|
| 727 | pr_info( "Wakeup event detected during hibernation, rolling back.\n"); | 
|---|
| 728 | goto exit; | 
|---|
| 729 | } | 
|---|
| 730 | fallthrough; | 
|---|
| 731 | case HIBERNATION_SHUTDOWN: | 
|---|
| 732 | if (kernel_can_power_off()) { | 
|---|
| 733 | entering_platform_hibernation = true; | 
|---|
| 734 | kernel_power_off(); | 
|---|
| 735 | entering_platform_hibernation = false; | 
|---|
| 736 | } | 
|---|
| 737 | break; | 
|---|
| 738 | } | 
|---|
| 739 | kernel_halt(); | 
|---|
| 740 | /* | 
|---|
| 741 | * Valid image is on the disk, if we continue we risk serious data | 
|---|
| 742 | * corruption after resume. | 
|---|
| 743 | */ | 
|---|
| 744 | pr_crit( "Power down manually\n"); | 
|---|
| 745 | while (1) | 
|---|
| 746 | cpu_relax(); | 
|---|
| 747 |  | 
|---|
| 748 | exit: | 
|---|
| 749 | /* Match the pm_restore_gfp_mask() call in hibernate(). */ | 
|---|
| 750 | pm_restrict_gfp_mask(); | 
|---|
| 751 |  | 
|---|
| 752 | /* Restore swap signature. */ | 
|---|
| 753 | error = swsusp_unmark(); | 
|---|
| 754 | if (error) | 
|---|
| 755 | pr_err( "Swap will be unusable! Try swapon -a.\n"); | 
|---|
| 756 | } | 
|---|
| 757 |  | 
|---|
| 758 | static int load_image_and_restore(void) | 
|---|
| 759 | { | 
|---|
| 760 | int error; | 
|---|
| 761 | unsigned int flags; | 
|---|
| 762 |  | 
|---|
| 763 | pm_pr_dbg( "Loading hibernation image.\n"); | 
|---|
| 764 |  | 
|---|
| 765 | lock_device_hotplug(); | 
|---|
| 766 | error = create_basic_memory_bitmaps(); | 
|---|
| 767 | if (error) { | 
|---|
| 768 | swsusp_close(); | 
|---|
| 769 | goto Unlock; | 
|---|
| 770 | } | 
|---|
| 771 |  | 
|---|
| 772 | error = swsusp_read(flags_p: &flags); | 
|---|
| 773 | swsusp_close(); | 
|---|
| 774 | if (!error) | 
|---|
| 775 | error = hibernation_restore(platform_mode: flags & SF_PLATFORM_MODE); | 
|---|
| 776 |  | 
|---|
| 777 | pr_err( "Failed to load image, recovering.\n"); | 
|---|
| 778 | swsusp_free(); | 
|---|
| 779 | free_basic_memory_bitmaps(); | 
|---|
| 780 | Unlock: | 
|---|
| 781 | unlock_device_hotplug(); | 
|---|
| 782 |  | 
|---|
| 783 | return error; | 
|---|
| 784 | } | 
|---|
| 785 |  | 
|---|
| 786 | #define COMPRESSION_ALGO_LZO "lzo" | 
|---|
| 787 | #define COMPRESSION_ALGO_LZ4 "lz4" | 
|---|
| 788 |  | 
|---|
| 789 | /** | 
|---|
| 790 | * hibernate - Carry out system hibernation, including saving the image. | 
|---|
| 791 | */ | 
|---|
| 792 | int hibernate(void) | 
|---|
| 793 | { | 
|---|
| 794 | bool snapshot_test = false; | 
|---|
| 795 | unsigned int sleep_flags; | 
|---|
| 796 | int error; | 
|---|
| 797 |  | 
|---|
| 798 | if (!hibernation_available()) { | 
|---|
| 799 | pm_pr_dbg( "Hibernation not available.\n"); | 
|---|
| 800 | return -EPERM; | 
|---|
| 801 | } | 
|---|
| 802 |  | 
|---|
| 803 | /* | 
|---|
| 804 | * Query for the compression algorithm support if compression is enabled. | 
|---|
| 805 | */ | 
|---|
| 806 | if (!nocompress) { | 
|---|
| 807 | strscpy(hib_comp_algo, hibernate_compressor); | 
|---|
| 808 | if (!crypto_has_acomp(alg_name: hib_comp_algo, type: 0, CRYPTO_ALG_ASYNC)) { | 
|---|
| 809 | pr_err( "%s compression is not available\n", hib_comp_algo); | 
|---|
| 810 | return -EOPNOTSUPP; | 
|---|
| 811 | } | 
|---|
| 812 | } | 
|---|
| 813 |  | 
|---|
| 814 | sleep_flags = lock_system_sleep(); | 
|---|
| 815 | /* The snapshot device should not be opened while we're running */ | 
|---|
| 816 | if (!hibernate_acquire()) { | 
|---|
| 817 | error = -EBUSY; | 
|---|
| 818 | goto Unlock; | 
|---|
| 819 | } | 
|---|
| 820 |  | 
|---|
| 821 | pr_info( "hibernation entry\n"); | 
|---|
| 822 | pm_prepare_console(); | 
|---|
| 823 | error = pm_notifier_call_chain_robust(PM_HIBERNATION_PREPARE, PM_POST_HIBERNATION); | 
|---|
| 824 | if (error) | 
|---|
| 825 | goto Restore; | 
|---|
| 826 |  | 
|---|
| 827 | ksys_sync_helper(); | 
|---|
| 828 | if (filesystem_freeze_enabled) | 
|---|
| 829 | filesystems_freeze(); | 
|---|
| 830 |  | 
|---|
| 831 | error = freeze_processes(); | 
|---|
| 832 | if (error) | 
|---|
| 833 | goto Exit; | 
|---|
| 834 |  | 
|---|
| 835 | lock_device_hotplug(); | 
|---|
| 836 | /* Allocate memory management structures */ | 
|---|
| 837 | error = create_basic_memory_bitmaps(); | 
|---|
| 838 | if (error) | 
|---|
| 839 | goto Thaw; | 
|---|
| 840 |  | 
|---|
| 841 | error = hibernation_snapshot(platform_mode: hibernation_mode == HIBERNATION_PLATFORM); | 
|---|
| 842 | if (error || freezer_test_done) | 
|---|
| 843 | goto Free_bitmaps; | 
|---|
| 844 |  | 
|---|
| 845 | if (in_suspend) { | 
|---|
| 846 | unsigned int flags = 0; | 
|---|
| 847 |  | 
|---|
| 848 | if (hibernation_mode == HIBERNATION_PLATFORM) | 
|---|
| 849 | flags |= SF_PLATFORM_MODE; | 
|---|
| 850 | if (nocompress) { | 
|---|
| 851 | flags |= SF_NOCOMPRESS_MODE; | 
|---|
| 852 | } else { | 
|---|
| 853 | flags |= SF_CRC32_MODE; | 
|---|
| 854 |  | 
|---|
| 855 | /* | 
|---|
| 856 | * By default, LZO compression is enabled. Use SF_COMPRESSION_ALG_LZ4 | 
|---|
| 857 | * to override this behaviour and use LZ4. | 
|---|
| 858 | * | 
|---|
| 859 | * Refer kernel/power/power.h for more details | 
|---|
| 860 | */ | 
|---|
| 861 |  | 
|---|
| 862 | if (!strcmp(hib_comp_algo, COMPRESSION_ALGO_LZ4)) | 
|---|
| 863 | flags |= SF_COMPRESSION_ALG_LZ4; | 
|---|
| 864 | else | 
|---|
| 865 | flags |= SF_COMPRESSION_ALG_LZO; | 
|---|
| 866 | } | 
|---|
| 867 |  | 
|---|
| 868 | pm_pr_dbg( "Writing hibernation image.\n"); | 
|---|
| 869 | error = swsusp_write(flags); | 
|---|
| 870 | swsusp_free(); | 
|---|
| 871 | if (!error) { | 
|---|
| 872 | if (hibernation_mode == HIBERNATION_TEST_RESUME) | 
|---|
| 873 | snapshot_test = true; | 
|---|
| 874 | else | 
|---|
| 875 | power_down(); | 
|---|
| 876 | } | 
|---|
| 877 | in_suspend = 0; | 
|---|
| 878 | pm_restore_gfp_mask(); | 
|---|
| 879 | } else { | 
|---|
| 880 | pm_pr_dbg( "Hibernation image restored successfully.\n"); | 
|---|
| 881 | } | 
|---|
| 882 |  | 
|---|
| 883 | Free_bitmaps: | 
|---|
| 884 | free_basic_memory_bitmaps(); | 
|---|
| 885 | Thaw: | 
|---|
| 886 | unlock_device_hotplug(); | 
|---|
| 887 | if (snapshot_test) { | 
|---|
| 888 | pm_pr_dbg( "Checking hibernation image\n"); | 
|---|
| 889 | error = swsusp_check(exclusive: false); | 
|---|
| 890 | if (!error) | 
|---|
| 891 | error = load_image_and_restore(); | 
|---|
| 892 | } | 
|---|
| 893 | thaw_processes(); | 
|---|
| 894 |  | 
|---|
| 895 | /* Don't bother checking whether freezer_test_done is true */ | 
|---|
| 896 | freezer_test_done = false; | 
|---|
| 897 | Exit: | 
|---|
| 898 | filesystems_thaw(); | 
|---|
| 899 | pm_notifier_call_chain(PM_POST_HIBERNATION); | 
|---|
| 900 | Restore: | 
|---|
| 901 | pm_restore_console(); | 
|---|
| 902 | hibernate_release(); | 
|---|
| 903 | Unlock: | 
|---|
| 904 | unlock_system_sleep(sleep_flags); | 
|---|
| 905 | pr_info( "hibernation exit\n"); | 
|---|
| 906 |  | 
|---|
| 907 | return error; | 
|---|
| 908 | } | 
|---|
| 909 |  | 
|---|
| 910 | /** | 
|---|
| 911 | * hibernate_quiet_exec - Execute a function with all devices frozen. | 
|---|
| 912 | * @func: Function to execute. | 
|---|
| 913 | * @data: Data pointer to pass to @func. | 
|---|
| 914 | * | 
|---|
| 915 | * Return the @func return value or an error code if it cannot be executed. | 
|---|
| 916 | */ | 
|---|
| 917 | int hibernate_quiet_exec(int (*func)(void *data), void *data) | 
|---|
| 918 | { | 
|---|
| 919 | unsigned int sleep_flags; | 
|---|
| 920 | int error; | 
|---|
| 921 |  | 
|---|
| 922 | sleep_flags = lock_system_sleep(); | 
|---|
| 923 |  | 
|---|
| 924 | if (!hibernate_acquire()) { | 
|---|
| 925 | error = -EBUSY; | 
|---|
| 926 | goto unlock; | 
|---|
| 927 | } | 
|---|
| 928 |  | 
|---|
| 929 | pm_prepare_console(); | 
|---|
| 930 |  | 
|---|
| 931 | error = pm_notifier_call_chain_robust(PM_HIBERNATION_PREPARE, PM_POST_HIBERNATION); | 
|---|
| 932 | if (error) | 
|---|
| 933 | goto restore; | 
|---|
| 934 |  | 
|---|
| 935 | if (filesystem_freeze_enabled) | 
|---|
| 936 | filesystems_freeze(); | 
|---|
| 937 |  | 
|---|
| 938 | error = freeze_processes(); | 
|---|
| 939 | if (error) | 
|---|
| 940 | goto exit; | 
|---|
| 941 |  | 
|---|
| 942 | lock_device_hotplug(); | 
|---|
| 943 |  | 
|---|
| 944 | pm_suspend_clear_flags(); | 
|---|
| 945 |  | 
|---|
| 946 | error = platform_begin(platform_mode: true); | 
|---|
| 947 | if (error) | 
|---|
| 948 | goto thaw; | 
|---|
| 949 |  | 
|---|
| 950 | error = freeze_kernel_threads(); | 
|---|
| 951 | if (error) | 
|---|
| 952 | goto thaw; | 
|---|
| 953 |  | 
|---|
| 954 | error = dpm_prepare(PMSG_FREEZE); | 
|---|
| 955 | if (error) | 
|---|
| 956 | goto dpm_complete; | 
|---|
| 957 |  | 
|---|
| 958 | console_suspend_all(); | 
|---|
| 959 |  | 
|---|
| 960 | error = dpm_suspend(PMSG_FREEZE); | 
|---|
| 961 | if (error) | 
|---|
| 962 | goto dpm_resume; | 
|---|
| 963 |  | 
|---|
| 964 | error = dpm_suspend_end(PMSG_FREEZE); | 
|---|
| 965 | if (error) | 
|---|
| 966 | goto dpm_resume; | 
|---|
| 967 |  | 
|---|
| 968 | error = platform_pre_snapshot(platform_mode: true); | 
|---|
| 969 | if (error) | 
|---|
| 970 | goto skip; | 
|---|
| 971 |  | 
|---|
| 972 | error = func(data); | 
|---|
| 973 |  | 
|---|
| 974 | skip: | 
|---|
| 975 | platform_finish(platform_mode: true); | 
|---|
| 976 |  | 
|---|
| 977 | dpm_resume_start(PMSG_THAW); | 
|---|
| 978 |  | 
|---|
| 979 | dpm_resume: | 
|---|
| 980 | dpm_resume(PMSG_THAW); | 
|---|
| 981 |  | 
|---|
| 982 | console_resume_all(); | 
|---|
| 983 |  | 
|---|
| 984 | dpm_complete: | 
|---|
| 985 | dpm_complete(PMSG_THAW); | 
|---|
| 986 |  | 
|---|
| 987 | thaw_kernel_threads(); | 
|---|
| 988 |  | 
|---|
| 989 | thaw: | 
|---|
| 990 | platform_end(platform_mode: true); | 
|---|
| 991 |  | 
|---|
| 992 | unlock_device_hotplug(); | 
|---|
| 993 |  | 
|---|
| 994 | thaw_processes(); | 
|---|
| 995 |  | 
|---|
| 996 | exit: | 
|---|
| 997 | filesystems_thaw(); | 
|---|
| 998 | pm_notifier_call_chain(PM_POST_HIBERNATION); | 
|---|
| 999 |  | 
|---|
| 1000 | restore: | 
|---|
| 1001 | pm_restore_console(); | 
|---|
| 1002 |  | 
|---|
| 1003 | hibernate_release(); | 
|---|
| 1004 |  | 
|---|
| 1005 | unlock: | 
|---|
| 1006 | unlock_system_sleep(sleep_flags); | 
|---|
| 1007 |  | 
|---|
| 1008 | return error; | 
|---|
| 1009 | } | 
|---|
| 1010 | EXPORT_SYMBOL_GPL(hibernate_quiet_exec); | 
|---|
| 1011 |  | 
|---|
| 1012 | static int __init find_resume_device(void) | 
|---|
| 1013 | { | 
|---|
| 1014 | if (!strlen(resume_file)) | 
|---|
| 1015 | return -ENOENT; | 
|---|
| 1016 |  | 
|---|
| 1017 | pm_pr_dbg( "Checking hibernation image partition %s\n", resume_file); | 
|---|
| 1018 |  | 
|---|
| 1019 | if (resume_delay) { | 
|---|
| 1020 | pr_info( "Waiting %dsec before reading resume device ...\n", | 
|---|
| 1021 | resume_delay); | 
|---|
| 1022 | ssleep(seconds: resume_delay); | 
|---|
| 1023 | } | 
|---|
| 1024 |  | 
|---|
| 1025 | /* Check if the device is there */ | 
|---|
| 1026 | if (!early_lookup_bdev(pathname: resume_file, dev: &swsusp_resume_device)) | 
|---|
| 1027 | return 0; | 
|---|
| 1028 |  | 
|---|
| 1029 | /* | 
|---|
| 1030 | * Some device discovery might still be in progress; we need to wait for | 
|---|
| 1031 | * this to finish. | 
|---|
| 1032 | */ | 
|---|
| 1033 | wait_for_device_probe(); | 
|---|
| 1034 | if (resume_wait) { | 
|---|
| 1035 | while (early_lookup_bdev(pathname: resume_file, dev: &swsusp_resume_device)) | 
|---|
| 1036 | msleep(msecs: 10); | 
|---|
| 1037 | async_synchronize_full(); | 
|---|
| 1038 | } | 
|---|
| 1039 |  | 
|---|
| 1040 | return early_lookup_bdev(pathname: resume_file, dev: &swsusp_resume_device); | 
|---|
| 1041 | } | 
|---|
| 1042 |  | 
|---|
| 1043 | static int software_resume(void) | 
|---|
| 1044 | { | 
|---|
| 1045 | int error; | 
|---|
| 1046 |  | 
|---|
| 1047 | pm_pr_dbg( "Hibernation image partition %d:%d present\n", | 
|---|
| 1048 | MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device)); | 
|---|
| 1049 |  | 
|---|
| 1050 | pm_pr_dbg( "Looking for hibernation image.\n"); | 
|---|
| 1051 |  | 
|---|
| 1052 | mutex_lock(lock: &system_transition_mutex); | 
|---|
| 1053 | error = swsusp_check(exclusive: true); | 
|---|
| 1054 | if (error) | 
|---|
| 1055 | goto Unlock; | 
|---|
| 1056 |  | 
|---|
| 1057 | /* | 
|---|
| 1058 | * Check if the hibernation image is compressed. If so, query for | 
|---|
| 1059 | * the algorithm support. | 
|---|
| 1060 | */ | 
|---|
| 1061 | if (!(swsusp_header_flags & SF_NOCOMPRESS_MODE)) { | 
|---|
| 1062 | if (swsusp_header_flags & SF_COMPRESSION_ALG_LZ4) | 
|---|
| 1063 | strscpy(hib_comp_algo, COMPRESSION_ALGO_LZ4); | 
|---|
| 1064 | else | 
|---|
| 1065 | strscpy(hib_comp_algo, COMPRESSION_ALGO_LZO); | 
|---|
| 1066 | if (!crypto_has_acomp(alg_name: hib_comp_algo, type: 0, CRYPTO_ALG_ASYNC)) { | 
|---|
| 1067 | pr_err( "%s compression is not available\n", hib_comp_algo); | 
|---|
| 1068 | error = -EOPNOTSUPP; | 
|---|
| 1069 | goto Unlock; | 
|---|
| 1070 | } | 
|---|
| 1071 | } | 
|---|
| 1072 |  | 
|---|
| 1073 | /* The snapshot device should not be opened while we're running */ | 
|---|
| 1074 | if (!hibernate_acquire()) { | 
|---|
| 1075 | error = -EBUSY; | 
|---|
| 1076 | swsusp_close(); | 
|---|
| 1077 | goto Unlock; | 
|---|
| 1078 | } | 
|---|
| 1079 |  | 
|---|
| 1080 | pr_info( "resume from hibernation\n"); | 
|---|
| 1081 | pm_prepare_console(); | 
|---|
| 1082 | error = pm_notifier_call_chain_robust(PM_RESTORE_PREPARE, PM_POST_RESTORE); | 
|---|
| 1083 | if (error) | 
|---|
| 1084 | goto Restore; | 
|---|
| 1085 |  | 
|---|
| 1086 | if (filesystem_freeze_enabled) | 
|---|
| 1087 | filesystems_freeze(); | 
|---|
| 1088 |  | 
|---|
| 1089 | pm_pr_dbg( "Preparing processes for hibernation restore.\n"); | 
|---|
| 1090 | error = freeze_processes(); | 
|---|
| 1091 | if (error) { | 
|---|
| 1092 | filesystems_thaw(); | 
|---|
| 1093 | goto Close_Finish; | 
|---|
| 1094 | } | 
|---|
| 1095 |  | 
|---|
| 1096 | error = freeze_kernel_threads(); | 
|---|
| 1097 | if (error) { | 
|---|
| 1098 | thaw_processes(); | 
|---|
| 1099 | filesystems_thaw(); | 
|---|
| 1100 | goto Close_Finish; | 
|---|
| 1101 | } | 
|---|
| 1102 |  | 
|---|
| 1103 | error = load_image_and_restore(); | 
|---|
| 1104 | thaw_processes(); | 
|---|
| 1105 | filesystems_thaw(); | 
|---|
| 1106 | Finish: | 
|---|
| 1107 | pm_notifier_call_chain(PM_POST_RESTORE); | 
|---|
| 1108 | Restore: | 
|---|
| 1109 | pm_restore_console(); | 
|---|
| 1110 | pr_info( "resume failed (%d)\n", error); | 
|---|
| 1111 | hibernate_release(); | 
|---|
| 1112 | /* For success case, the suspend path will release the lock */ | 
|---|
| 1113 | Unlock: | 
|---|
| 1114 | mutex_unlock(lock: &system_transition_mutex); | 
|---|
| 1115 | pm_pr_dbg( "Hibernation image not present or could not be loaded.\n"); | 
|---|
| 1116 | return error; | 
|---|
| 1117 | Close_Finish: | 
|---|
| 1118 | swsusp_close(); | 
|---|
| 1119 | goto Finish; | 
|---|
| 1120 | } | 
|---|
| 1121 |  | 
|---|
| 1122 | /** | 
|---|
| 1123 | * software_resume_initcall - Resume from a saved hibernation image. | 
|---|
| 1124 | * | 
|---|
| 1125 | * This routine is called as a late initcall, when all devices have been | 
|---|
| 1126 | * discovered and initialized already. | 
|---|
| 1127 | * | 
|---|
| 1128 | * The image reading code is called to see if there is a hibernation image | 
|---|
| 1129 | * available for reading.  If that is the case, devices are quiesced and the | 
|---|
| 1130 | * contents of memory is restored from the saved image. | 
|---|
| 1131 | * | 
|---|
| 1132 | * If this is successful, control reappears in the restored target kernel in | 
|---|
| 1133 | * hibernation_snapshot() which returns to hibernate().  Otherwise, the routine | 
|---|
| 1134 | * attempts to recover gracefully and make the kernel return to the normal mode | 
|---|
| 1135 | * of operation. | 
|---|
| 1136 | */ | 
|---|
| 1137 | static int __init software_resume_initcall(void) | 
|---|
| 1138 | { | 
|---|
| 1139 | /* | 
|---|
| 1140 | * If the user said "noresume".. bail out early. | 
|---|
| 1141 | */ | 
|---|
| 1142 | if (noresume || !hibernation_available()) | 
|---|
| 1143 | return 0; | 
|---|
| 1144 |  | 
|---|
| 1145 | if (!swsusp_resume_device) { | 
|---|
| 1146 | int error = find_resume_device(); | 
|---|
| 1147 |  | 
|---|
| 1148 | if (error) | 
|---|
| 1149 | return error; | 
|---|
| 1150 | } | 
|---|
| 1151 |  | 
|---|
| 1152 | return software_resume(); | 
|---|
| 1153 | } | 
|---|
| 1154 | late_initcall_sync(software_resume_initcall); | 
|---|
| 1155 |  | 
|---|
| 1156 |  | 
|---|
| 1157 | static const char * const hibernation_modes[] = { | 
|---|
| 1158 | [HIBERNATION_PLATFORM]	= "platform", | 
|---|
| 1159 | [HIBERNATION_SHUTDOWN]	= "shutdown", | 
|---|
| 1160 | [HIBERNATION_REBOOT]	= "reboot", | 
|---|
| 1161 | #ifdef CONFIG_SUSPEND | 
|---|
| 1162 | [HIBERNATION_SUSPEND]	= "suspend", | 
|---|
| 1163 | #endif | 
|---|
| 1164 | [HIBERNATION_TEST_RESUME]	= "test_resume", | 
|---|
| 1165 | }; | 
|---|
| 1166 |  | 
|---|
| 1167 | /* | 
|---|
| 1168 | * /sys/power/disk - Control hibernation mode. | 
|---|
| 1169 | * | 
|---|
| 1170 | * Hibernation can be handled in several ways.  There are a few different ways | 
|---|
| 1171 | * to put the system into the sleep state: using the platform driver (e.g. ACPI | 
|---|
| 1172 | * or other hibernation_ops), powering it off or rebooting it (for testing | 
|---|
| 1173 | * mostly). | 
|---|
| 1174 | * | 
|---|
| 1175 | * The sysfs file /sys/power/disk provides an interface for selecting the | 
|---|
| 1176 | * hibernation mode to use.  Reading from this file causes the available modes | 
|---|
| 1177 | * to be printed.  There are 3 modes that can be supported: | 
|---|
| 1178 | * | 
|---|
| 1179 | *	'platform' | 
|---|
| 1180 | *	'shutdown' | 
|---|
| 1181 | *	'reboot' | 
|---|
| 1182 | * | 
|---|
| 1183 | * If a platform hibernation driver is in use, 'platform' will be supported | 
|---|
| 1184 | * and will be used by default.  Otherwise, 'shutdown' will be used by default. | 
|---|
| 1185 | * The selected option (i.e. the one corresponding to the current value of | 
|---|
| 1186 | * hibernation_mode) is enclosed by a square bracket. | 
|---|
| 1187 | * | 
|---|
| 1188 | * To select a given hibernation mode it is necessary to write the mode's | 
|---|
| 1189 | * string representation (as returned by reading from /sys/power/disk) back | 
|---|
| 1190 | * into /sys/power/disk. | 
|---|
| 1191 | */ | 
|---|
| 1192 |  | 
|---|
| 1193 | static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr, | 
|---|
| 1194 | char *buf) | 
|---|
| 1195 | { | 
|---|
| 1196 | ssize_t count = 0; | 
|---|
| 1197 | int i; | 
|---|
| 1198 |  | 
|---|
| 1199 | if (!hibernation_available()) | 
|---|
| 1200 | return sysfs_emit(buf, fmt: "[disabled]\n"); | 
|---|
| 1201 |  | 
|---|
| 1202 | for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) { | 
|---|
| 1203 | if (!hibernation_modes[i]) | 
|---|
| 1204 | continue; | 
|---|
| 1205 | switch (i) { | 
|---|
| 1206 | case HIBERNATION_SHUTDOWN: | 
|---|
| 1207 | case HIBERNATION_REBOOT: | 
|---|
| 1208 | #ifdef CONFIG_SUSPEND | 
|---|
| 1209 | case HIBERNATION_SUSPEND: | 
|---|
| 1210 | #endif | 
|---|
| 1211 | case HIBERNATION_TEST_RESUME: | 
|---|
| 1212 | break; | 
|---|
| 1213 | case HIBERNATION_PLATFORM: | 
|---|
| 1214 | if (hibernation_ops) | 
|---|
| 1215 | break; | 
|---|
| 1216 | /* not a valid mode, continue with loop */ | 
|---|
| 1217 | continue; | 
|---|
| 1218 | } | 
|---|
| 1219 | if (i == hibernation_mode) | 
|---|
| 1220 | count += sysfs_emit_at(buf, at: count, fmt: "[%s] ", hibernation_modes[i]); | 
|---|
| 1221 | else | 
|---|
| 1222 | count += sysfs_emit_at(buf, at: count, fmt: "%s ", hibernation_modes[i]); | 
|---|
| 1223 | } | 
|---|
| 1224 |  | 
|---|
| 1225 | /* Convert the last space to a newline if needed. */ | 
|---|
| 1226 | if (count > 0) | 
|---|
| 1227 | buf[count - 1] = '\n'; | 
|---|
| 1228 |  | 
|---|
| 1229 | return count; | 
|---|
| 1230 | } | 
|---|
| 1231 |  | 
|---|
| 1232 | static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr, | 
|---|
| 1233 | const char *buf, size_t n) | 
|---|
| 1234 | { | 
|---|
| 1235 | int mode = HIBERNATION_INVALID; | 
|---|
| 1236 | unsigned int sleep_flags; | 
|---|
| 1237 | int error = 0; | 
|---|
| 1238 | int len; | 
|---|
| 1239 | char *p; | 
|---|
| 1240 | int i; | 
|---|
| 1241 |  | 
|---|
| 1242 | if (!hibernation_available()) | 
|---|
| 1243 | return -EPERM; | 
|---|
| 1244 |  | 
|---|
| 1245 | p = memchr(buf, '\n', n); | 
|---|
| 1246 | len = p ? p - buf : n; | 
|---|
| 1247 |  | 
|---|
| 1248 | sleep_flags = lock_system_sleep(); | 
|---|
| 1249 | for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) { | 
|---|
| 1250 | if (len == strlen(hibernation_modes[i]) | 
|---|
| 1251 | && !strncmp(buf, hibernation_modes[i], len)) { | 
|---|
| 1252 | mode = i; | 
|---|
| 1253 | break; | 
|---|
| 1254 | } | 
|---|
| 1255 | } | 
|---|
| 1256 | if (mode != HIBERNATION_INVALID) { | 
|---|
| 1257 | switch (mode) { | 
|---|
| 1258 | case HIBERNATION_SHUTDOWN: | 
|---|
| 1259 | case HIBERNATION_REBOOT: | 
|---|
| 1260 | #ifdef CONFIG_SUSPEND | 
|---|
| 1261 | case HIBERNATION_SUSPEND: | 
|---|
| 1262 | #endif | 
|---|
| 1263 | case HIBERNATION_TEST_RESUME: | 
|---|
| 1264 | hibernation_mode = mode; | 
|---|
| 1265 | break; | 
|---|
| 1266 | case HIBERNATION_PLATFORM: | 
|---|
| 1267 | if (hibernation_ops) | 
|---|
| 1268 | hibernation_mode = mode; | 
|---|
| 1269 | else | 
|---|
| 1270 | error = -EINVAL; | 
|---|
| 1271 | } | 
|---|
| 1272 | } else | 
|---|
| 1273 | error = -EINVAL; | 
|---|
| 1274 |  | 
|---|
| 1275 | if (!error) | 
|---|
| 1276 | pm_pr_dbg( "Hibernation mode set to '%s'\n", | 
|---|
| 1277 | hibernation_modes[mode]); | 
|---|
| 1278 | unlock_system_sleep(sleep_flags); | 
|---|
| 1279 | return error ? error : n; | 
|---|
| 1280 | } | 
|---|
| 1281 |  | 
|---|
| 1282 | power_attr(disk); | 
|---|
| 1283 |  | 
|---|
| 1284 | static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr, | 
|---|
| 1285 | char *buf) | 
|---|
| 1286 | { | 
|---|
| 1287 | return sysfs_emit(buf, fmt: "%d:%d\n", MAJOR(swsusp_resume_device), | 
|---|
| 1288 | MINOR(swsusp_resume_device)); | 
|---|
| 1289 | } | 
|---|
| 1290 |  | 
|---|
| 1291 | static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr, | 
|---|
| 1292 | const char *buf, size_t n) | 
|---|
| 1293 | { | 
|---|
| 1294 | unsigned int sleep_flags; | 
|---|
| 1295 | int len = n; | 
|---|
| 1296 | char *name; | 
|---|
| 1297 | dev_t dev; | 
|---|
| 1298 | int error; | 
|---|
| 1299 |  | 
|---|
| 1300 | if (!hibernation_available()) | 
|---|
| 1301 | return n; | 
|---|
| 1302 |  | 
|---|
| 1303 | if (len && buf[len-1] == '\n') | 
|---|
| 1304 | len--; | 
|---|
| 1305 | name = kstrndup(s: buf, len, GFP_KERNEL); | 
|---|
| 1306 | if (!name) | 
|---|
| 1307 | return -ENOMEM; | 
|---|
| 1308 |  | 
|---|
| 1309 | error = lookup_bdev(pathname: name, dev: &dev); | 
|---|
| 1310 | if (error) { | 
|---|
| 1311 | unsigned maj, min, offset; | 
|---|
| 1312 | char *p, dummy; | 
|---|
| 1313 |  | 
|---|
| 1314 | error = 0; | 
|---|
| 1315 | if (sscanf(name, "%u:%u%c", &maj, &min, &dummy) == 2 || | 
|---|
| 1316 | sscanf(name, "%u:%u:%u:%c", &maj, &min, &offset, | 
|---|
| 1317 | &dummy) == 3) { | 
|---|
| 1318 | dev = MKDEV(maj, min); | 
|---|
| 1319 | if (maj != MAJOR(dev) || min != MINOR(dev)) | 
|---|
| 1320 | error = -EINVAL; | 
|---|
| 1321 | } else { | 
|---|
| 1322 | dev = new_decode_dev(dev: simple_strtoul(name, &p, 16)); | 
|---|
| 1323 | if (*p) | 
|---|
| 1324 | error = -EINVAL; | 
|---|
| 1325 | } | 
|---|
| 1326 | } | 
|---|
| 1327 | kfree(objp: name); | 
|---|
| 1328 | if (error) | 
|---|
| 1329 | return error; | 
|---|
| 1330 |  | 
|---|
| 1331 | sleep_flags = lock_system_sleep(); | 
|---|
| 1332 | swsusp_resume_device = dev; | 
|---|
| 1333 | unlock_system_sleep(sleep_flags); | 
|---|
| 1334 |  | 
|---|
| 1335 | pm_pr_dbg( "Configured hibernation resume from disk to %u\n", | 
|---|
| 1336 | swsusp_resume_device); | 
|---|
| 1337 | noresume = 0; | 
|---|
| 1338 | software_resume(); | 
|---|
| 1339 | return n; | 
|---|
| 1340 | } | 
|---|
| 1341 |  | 
|---|
| 1342 | power_attr(resume); | 
|---|
| 1343 |  | 
|---|
| 1344 | static ssize_t resume_offset_show(struct kobject *kobj, | 
|---|
| 1345 | struct kobj_attribute *attr, char *buf) | 
|---|
| 1346 | { | 
|---|
| 1347 | return sysfs_emit(buf, fmt: "%llu\n", (unsigned long long)swsusp_resume_block); | 
|---|
| 1348 | } | 
|---|
| 1349 |  | 
|---|
| 1350 | static ssize_t resume_offset_store(struct kobject *kobj, | 
|---|
| 1351 | struct kobj_attribute *attr, const char *buf, | 
|---|
| 1352 | size_t n) | 
|---|
| 1353 | { | 
|---|
| 1354 | unsigned long long offset; | 
|---|
| 1355 | int rc; | 
|---|
| 1356 |  | 
|---|
| 1357 | rc = kstrtoull(s: buf, base: 0, res: &offset); | 
|---|
| 1358 | if (rc) | 
|---|
| 1359 | return rc; | 
|---|
| 1360 | swsusp_resume_block = offset; | 
|---|
| 1361 |  | 
|---|
| 1362 | return n; | 
|---|
| 1363 | } | 
|---|
| 1364 |  | 
|---|
| 1365 | power_attr(resume_offset); | 
|---|
| 1366 |  | 
|---|
| 1367 | static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr, | 
|---|
| 1368 | char *buf) | 
|---|
| 1369 | { | 
|---|
| 1370 | return sysfs_emit(buf, fmt: "%lu\n", image_size); | 
|---|
| 1371 | } | 
|---|
| 1372 |  | 
|---|
| 1373 | static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr, | 
|---|
| 1374 | const char *buf, size_t n) | 
|---|
| 1375 | { | 
|---|
| 1376 | unsigned long size; | 
|---|
| 1377 |  | 
|---|
| 1378 | if (sscanf(buf, "%lu", &size) == 1) { | 
|---|
| 1379 | image_size = size; | 
|---|
| 1380 | return n; | 
|---|
| 1381 | } | 
|---|
| 1382 |  | 
|---|
| 1383 | return -EINVAL; | 
|---|
| 1384 | } | 
|---|
| 1385 |  | 
|---|
| 1386 | power_attr(image_size); | 
|---|
| 1387 |  | 
|---|
| 1388 | static ssize_t reserved_size_show(struct kobject *kobj, | 
|---|
| 1389 | struct kobj_attribute *attr, char *buf) | 
|---|
| 1390 | { | 
|---|
| 1391 | return sysfs_emit(buf, fmt: "%lu\n", reserved_size); | 
|---|
| 1392 | } | 
|---|
| 1393 |  | 
|---|
| 1394 | static ssize_t reserved_size_store(struct kobject *kobj, | 
|---|
| 1395 | struct kobj_attribute *attr, | 
|---|
| 1396 | const char *buf, size_t n) | 
|---|
| 1397 | { | 
|---|
| 1398 | unsigned long size; | 
|---|
| 1399 |  | 
|---|
| 1400 | if (sscanf(buf, "%lu", &size) == 1) { | 
|---|
| 1401 | reserved_size = size; | 
|---|
| 1402 | return n; | 
|---|
| 1403 | } | 
|---|
| 1404 |  | 
|---|
| 1405 | return -EINVAL; | 
|---|
| 1406 | } | 
|---|
| 1407 |  | 
|---|
| 1408 | power_attr(reserved_size); | 
|---|
| 1409 |  | 
|---|
| 1410 | static struct attribute *g[] = { | 
|---|
| 1411 | &disk_attr.attr, | 
|---|
| 1412 | &resume_offset_attr.attr, | 
|---|
| 1413 | &resume_attr.attr, | 
|---|
| 1414 | &image_size_attr.attr, | 
|---|
| 1415 | &reserved_size_attr.attr, | 
|---|
| 1416 | NULL, | 
|---|
| 1417 | }; | 
|---|
| 1418 |  | 
|---|
| 1419 |  | 
|---|
| 1420 | static const struct attribute_group attr_group = { | 
|---|
| 1421 | .attrs = g, | 
|---|
| 1422 | }; | 
|---|
| 1423 |  | 
|---|
| 1424 |  | 
|---|
| 1425 | static int __init pm_disk_init(void) | 
|---|
| 1426 | { | 
|---|
| 1427 | return sysfs_create_group(kobj: power_kobj, grp: &attr_group); | 
|---|
| 1428 | } | 
|---|
| 1429 |  | 
|---|
| 1430 | core_initcall(pm_disk_init); | 
|---|
| 1431 |  | 
|---|
| 1432 |  | 
|---|
| 1433 | static int __init resume_setup(char *str) | 
|---|
| 1434 | { | 
|---|
| 1435 | if (noresume) | 
|---|
| 1436 | return 1; | 
|---|
| 1437 |  | 
|---|
| 1438 | strscpy(resume_file, str); | 
|---|
| 1439 | return 1; | 
|---|
| 1440 | } | 
|---|
| 1441 |  | 
|---|
| 1442 | static int __init resume_offset_setup(char *str) | 
|---|
| 1443 | { | 
|---|
| 1444 | unsigned long long offset; | 
|---|
| 1445 |  | 
|---|
| 1446 | if (noresume) | 
|---|
| 1447 | return 1; | 
|---|
| 1448 |  | 
|---|
| 1449 | if (sscanf(str, "%llu", &offset) == 1) | 
|---|
| 1450 | swsusp_resume_block = offset; | 
|---|
| 1451 |  | 
|---|
| 1452 | return 1; | 
|---|
| 1453 | } | 
|---|
| 1454 |  | 
|---|
| 1455 | static int __init hibernate_setup(char *str) | 
|---|
| 1456 | { | 
|---|
| 1457 | if (!strncmp(str, "noresume", 8)) { | 
|---|
| 1458 | noresume = 1; | 
|---|
| 1459 | } else if (!strncmp(str, "nocompress", 10)) { | 
|---|
| 1460 | nocompress = 1; | 
|---|
| 1461 | } else if (!strncmp(str, "no", 2)) { | 
|---|
| 1462 | noresume = 1; | 
|---|
| 1463 | nohibernate = 1; | 
|---|
| 1464 | } else if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX) | 
|---|
| 1465 | && !strncmp(str, "protect_image", 13)) { | 
|---|
| 1466 | enable_restore_image_protection(); | 
|---|
| 1467 | } | 
|---|
| 1468 | return 1; | 
|---|
| 1469 | } | 
|---|
| 1470 |  | 
|---|
| 1471 | static int __init noresume_setup(char *str) | 
|---|
| 1472 | { | 
|---|
| 1473 | noresume = 1; | 
|---|
| 1474 | return 1; | 
|---|
| 1475 | } | 
|---|
| 1476 |  | 
|---|
| 1477 | static int __init resumewait_setup(char *str) | 
|---|
| 1478 | { | 
|---|
| 1479 | resume_wait = 1; | 
|---|
| 1480 | return 1; | 
|---|
| 1481 | } | 
|---|
| 1482 |  | 
|---|
| 1483 | static int __init resumedelay_setup(char *str) | 
|---|
| 1484 | { | 
|---|
| 1485 | int rc = kstrtouint(s: str, base: 0, res: &resume_delay); | 
|---|
| 1486 |  | 
|---|
| 1487 | if (rc) | 
|---|
| 1488 | pr_warn( "resumedelay: bad option string '%s'\n", str); | 
|---|
| 1489 | return 1; | 
|---|
| 1490 | } | 
|---|
| 1491 |  | 
|---|
| 1492 | static int __init nohibernate_setup(char *str) | 
|---|
| 1493 | { | 
|---|
| 1494 | noresume = 1; | 
|---|
| 1495 | nohibernate = 1; | 
|---|
| 1496 | return 1; | 
|---|
| 1497 | } | 
|---|
| 1498 |  | 
|---|
| 1499 | static const char * const comp_alg_enabled[] = { | 
|---|
| 1500 | #if IS_ENABLED(CONFIG_CRYPTO_LZO) | 
|---|
| 1501 | COMPRESSION_ALGO_LZO, | 
|---|
| 1502 | #endif | 
|---|
| 1503 | #if IS_ENABLED(CONFIG_CRYPTO_LZ4) | 
|---|
| 1504 | COMPRESSION_ALGO_LZ4, | 
|---|
| 1505 | #endif | 
|---|
| 1506 | }; | 
|---|
| 1507 |  | 
|---|
| 1508 | static int hibernate_compressor_param_set(const char *compressor, | 
|---|
| 1509 | const struct kernel_param *kp) | 
|---|
| 1510 | { | 
|---|
| 1511 | int index, ret; | 
|---|
| 1512 |  | 
|---|
| 1513 | if (!mutex_trylock(lock: &system_transition_mutex)) | 
|---|
| 1514 | return -EBUSY; | 
|---|
| 1515 |  | 
|---|
| 1516 | index = sysfs_match_string(comp_alg_enabled, compressor); | 
|---|
| 1517 | if (index >= 0) { | 
|---|
| 1518 | ret = param_set_copystring(val: comp_alg_enabled[index], kp); | 
|---|
| 1519 | if (!ret) | 
|---|
| 1520 | strscpy(hib_comp_algo, comp_alg_enabled[index]); | 
|---|
| 1521 | } else { | 
|---|
| 1522 | ret = index; | 
|---|
| 1523 | } | 
|---|
| 1524 |  | 
|---|
| 1525 | mutex_unlock(lock: &system_transition_mutex); | 
|---|
| 1526 |  | 
|---|
| 1527 | if (ret) | 
|---|
| 1528 | pr_debug( "Cannot set specified compressor %s\n", | 
|---|
| 1529 | compressor); | 
|---|
| 1530 |  | 
|---|
| 1531 | return ret; | 
|---|
| 1532 | } | 
|---|
| 1533 |  | 
|---|
| 1534 | static const struct kernel_param_ops hibernate_compressor_param_ops = { | 
|---|
| 1535 | .set    = hibernate_compressor_param_set, | 
|---|
| 1536 | .get    = param_get_string, | 
|---|
| 1537 | }; | 
|---|
| 1538 |  | 
|---|
| 1539 | static struct kparam_string hibernate_compressor_param_string = { | 
|---|
| 1540 | .maxlen = sizeof(hibernate_compressor), | 
|---|
| 1541 | .string = hibernate_compressor, | 
|---|
| 1542 | }; | 
|---|
| 1543 |  | 
|---|
| 1544 | module_param_cb(compressor, &hibernate_compressor_param_ops, | 
|---|
| 1545 | &hibernate_compressor_param_string, 0644); | 
|---|
| 1546 | MODULE_PARM_DESC(compressor, | 
|---|
| 1547 | "Compression algorithm to be used with hibernation"); | 
|---|
| 1548 |  | 
|---|
| 1549 | __setup( "noresume", noresume_setup); | 
|---|
| 1550 | __setup( "resume_offset=", resume_offset_setup); | 
|---|
| 1551 | __setup( "resume=", resume_setup); | 
|---|
| 1552 | __setup( "hibernate=", hibernate_setup); | 
|---|
| 1553 | __setup( "resumewait", resumewait_setup); | 
|---|
| 1554 | __setup( "resumedelay=", resumedelay_setup); | 
|---|
| 1555 | __setup( "nohibernate", nohibernate_setup); | 
|---|
| 1556 |  | 
|---|