swsusp.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. /*
  2. * linux/kernel/power/swsusp.c
  3. *
  4. * This file provides code to write suspend image to swap and read it back.
  5. *
  6. * Copyright (C) 1998-2001 Gabor Kuti <seasons@fornax.hu>
  7. * Copyright (C) 1998,2001-2005 Pavel Machek <pavel@suse.cz>
  8. *
  9. * This file is released under the GPLv2.
  10. *
  11. * I'd like to thank the following people for their work:
  12. *
  13. * Pavel Machek <pavel@ucw.cz>:
  14. * Modifications, defectiveness pointing, being with me at the very beginning,
  15. * suspend to swap space, stop all tasks. Port to 2.4.18-ac and 2.5.17.
  16. *
  17. * Steve Doddi <dirk@loth.demon.co.uk>:
  18. * Support the possibility of hardware state restoring.
  19. *
  20. * Raph <grey.havens@earthling.net>:
  21. * Support for preserving states of network devices and virtual console
  22. * (including X and svgatextmode)
  23. *
  24. * Kurt Garloff <garloff@suse.de>:
  25. * Straightened the critical function in order to prevent compilers from
  26. * playing tricks with local variables.
  27. *
  28. * Andreas Mohr <a.mohr@mailto.de>
  29. *
  30. * Alex Badea <vampire@go.ro>:
  31. * Fixed runaway init
  32. *
  33. * Andreas Steinmetz <ast@domdv.de>:
  34. * Added encrypted suspend option
  35. *
  36. * More state savers are welcome. Especially for the scsi layer...
  37. *
  38. * For TODOs,FIXMEs also look in Documentation/power/swsusp.txt
  39. */
  40. #include <linux/module.h>
  41. #include <linux/mm.h>
  42. #include <linux/suspend.h>
  43. #include <linux/smp_lock.h>
  44. #include <linux/file.h>
  45. #include <linux/utsname.h>
  46. #include <linux/version.h>
  47. #include <linux/delay.h>
  48. #include <linux/bitops.h>
  49. #include <linux/spinlock.h>
  50. #include <linux/genhd.h>
  51. #include <linux/kernel.h>
  52. #include <linux/major.h>
  53. #include <linux/swap.h>
  54. #include <linux/pm.h>
  55. #include <linux/device.h>
  56. #include <linux/buffer_head.h>
  57. #include <linux/swapops.h>
  58. #include <linux/bootmem.h>
  59. #include <linux/syscalls.h>
  60. #include <linux/highmem.h>
  61. #include <linux/bio.h>
  62. #include <asm/uaccess.h>
  63. #include <asm/mmu_context.h>
  64. #include <asm/pgtable.h>
  65. #include <asm/tlbflush.h>
  66. #include <asm/io.h>
  67. #include <linux/random.h>
  68. #include <linux/crypto.h>
  69. #include <asm/scatterlist.h>
  70. #include "power.h"
  71. #define CIPHER "aes"
  72. #define MAXKEY 32
  73. #define MAXIV 32
  74. extern char resume_file[];
  75. /* Local variables that should not be affected by save */
  76. unsigned int nr_copy_pages __nosavedata = 0;
  77. /* Suspend pagedir is allocated before final copy, therefore it
  78. must be freed after resume
  79. Warning: this is evil. There are actually two pagedirs at time of
  80. resume. One is "pagedir_save", which is empty frame allocated at
  81. time of suspend, that must be freed. Second is "pagedir_nosave",
  82. allocated at time of resume, that travels through memory not to
  83. collide with anything.
  84. Warning: this is even more evil than it seems. Pagedirs this file
  85. talks about are completely different from page directories used by
  86. MMU hardware.
  87. */
  88. suspend_pagedir_t *pagedir_nosave __nosavedata = NULL;
  89. suspend_pagedir_t *pagedir_save;
  90. #define SWSUSP_SIG "S1SUSPEND"
  91. static struct swsusp_header {
  92. char reserved[PAGE_SIZE - 20 - MAXKEY - MAXIV - sizeof(swp_entry_t)];
  93. u8 key_iv[MAXKEY+MAXIV];
  94. swp_entry_t swsusp_info;
  95. char orig_sig[10];
  96. char sig[10];
  97. } __attribute__((packed, aligned(PAGE_SIZE))) swsusp_header;
  98. static struct swsusp_info swsusp_info;
  99. /*
  100. * Saving part...
  101. */
  102. /* We memorize in swapfile_used what swap devices are used for suspension */
  103. #define SWAPFILE_UNUSED 0
  104. #define SWAPFILE_SUSPEND 1 /* This is the suspending device */
  105. #define SWAPFILE_IGNORED 2 /* Those are other swap devices ignored for suspension */
  106. static unsigned short swapfile_used[MAX_SWAPFILES];
  107. static unsigned short root_swap;
  108. static int write_page(unsigned long addr, swp_entry_t * loc);
  109. static int bio_read_page(pgoff_t page_off, void * page);
  110. static u8 key_iv[MAXKEY+MAXIV];
  111. #ifdef CONFIG_SWSUSP_ENCRYPT
  112. static int crypto_init(int mode, void **mem)
  113. {
  114. int error = 0;
  115. int len;
  116. char *modemsg;
  117. struct crypto_tfm *tfm;
  118. modemsg = mode ? "suspend not possible" : "resume not possible";
  119. tfm = crypto_alloc_tfm(CIPHER, CRYPTO_TFM_MODE_CBC);
  120. if(!tfm) {
  121. printk(KERN_ERR "swsusp: no tfm, %s\n", modemsg);
  122. error = -EINVAL;
  123. goto out;
  124. }
  125. if(MAXKEY < crypto_tfm_alg_min_keysize(tfm)) {
  126. printk(KERN_ERR "swsusp: key buffer too small, %s\n", modemsg);
  127. error = -ENOKEY;
  128. goto fail;
  129. }
  130. if (mode)
  131. get_random_bytes(key_iv, MAXKEY+MAXIV);
  132. len = crypto_tfm_alg_max_keysize(tfm);
  133. if (len > MAXKEY)
  134. len = MAXKEY;
  135. if (crypto_cipher_setkey(tfm, key_iv, len)) {
  136. printk(KERN_ERR "swsusp: key setup failure, %s\n", modemsg);
  137. error = -EKEYREJECTED;
  138. goto fail;
  139. }
  140. len = crypto_tfm_alg_ivsize(tfm);
  141. if (MAXIV < len) {
  142. printk(KERN_ERR "swsusp: iv buffer too small, %s\n", modemsg);
  143. error = -EOVERFLOW;
  144. goto fail;
  145. }
  146. crypto_cipher_set_iv(tfm, key_iv+MAXKEY, len);
  147. *mem=(void *)tfm;
  148. goto out;
  149. fail: crypto_free_tfm(tfm);
  150. out: return error;
  151. }
  152. static __inline__ void crypto_exit(void *mem)
  153. {
  154. crypto_free_tfm((struct crypto_tfm *)mem);
  155. }
  156. static __inline__ int crypto_write(struct pbe *p, void *mem)
  157. {
  158. int error = 0;
  159. struct scatterlist src, dst;
  160. src.page = virt_to_page(p->address);
  161. src.offset = 0;
  162. src.length = PAGE_SIZE;
  163. dst.page = virt_to_page((void *)&swsusp_header);
  164. dst.offset = 0;
  165. dst.length = PAGE_SIZE;
  166. error = crypto_cipher_encrypt((struct crypto_tfm *)mem, &dst, &src,
  167. PAGE_SIZE);
  168. if (!error)
  169. error = write_page((unsigned long)&swsusp_header,
  170. &(p->swap_address));
  171. return error;
  172. }
  173. static __inline__ int crypto_read(struct pbe *p, void *mem)
  174. {
  175. int error = 0;
  176. struct scatterlist src, dst;
  177. error = bio_read_page(swp_offset(p->swap_address), (void *)p->address);
  178. if (!error) {
  179. src.offset = 0;
  180. src.length = PAGE_SIZE;
  181. dst.offset = 0;
  182. dst.length = PAGE_SIZE;
  183. src.page = dst.page = virt_to_page((void *)p->address);
  184. error = crypto_cipher_decrypt((struct crypto_tfm *)mem, &dst,
  185. &src, PAGE_SIZE);
  186. }
  187. return error;
  188. }
  189. #else
  190. static __inline__ int crypto_init(int mode, void *mem)
  191. {
  192. return 0;
  193. }
  194. static __inline__ void crypto_exit(void *mem)
  195. {
  196. }
  197. static __inline__ int crypto_write(struct pbe *p, void *mem)
  198. {
  199. return write_page(p->address, &(p->swap_address));
  200. }
  201. static __inline__ int crypto_read(struct pbe *p, void *mem)
  202. {
  203. return bio_read_page(swp_offset(p->swap_address), (void *)p->address);
  204. }
  205. #endif
  206. static int mark_swapfiles(swp_entry_t prev)
  207. {
  208. int error;
  209. rw_swap_page_sync(READ,
  210. swp_entry(root_swap, 0),
  211. virt_to_page((unsigned long)&swsusp_header));
  212. if (!memcmp("SWAP-SPACE",swsusp_header.sig, 10) ||
  213. !memcmp("SWAPSPACE2",swsusp_header.sig, 10)) {
  214. memcpy(swsusp_header.orig_sig,swsusp_header.sig, 10);
  215. memcpy(swsusp_header.sig,SWSUSP_SIG, 10);
  216. memcpy(swsusp_header.key_iv, key_iv, MAXKEY+MAXIV);
  217. swsusp_header.swsusp_info = prev;
  218. error = rw_swap_page_sync(WRITE,
  219. swp_entry(root_swap, 0),
  220. virt_to_page((unsigned long)
  221. &swsusp_header));
  222. } else {
  223. pr_debug("swsusp: Partition is not swap space.\n");
  224. error = -ENODEV;
  225. }
  226. return error;
  227. }
  228. /*
  229. * Check whether the swap device is the specified resume
  230. * device, irrespective of whether they are specified by
  231. * identical names.
  232. *
  233. * (Thus, device inode aliasing is allowed. You can say /dev/hda4
  234. * instead of /dev/ide/host0/bus0/target0/lun0/part4 [if using devfs]
  235. * and they'll be considered the same device. This is *necessary* for
  236. * devfs, since the resume code can only recognize the form /dev/hda4,
  237. * but the suspend code would see the long name.)
  238. */
  239. static int is_resume_device(const struct swap_info_struct *swap_info)
  240. {
  241. struct file *file = swap_info->swap_file;
  242. struct inode *inode = file->f_dentry->d_inode;
  243. return S_ISBLK(inode->i_mode) &&
  244. swsusp_resume_device == MKDEV(imajor(inode), iminor(inode));
  245. }
  246. static int swsusp_swap_check(void) /* This is called before saving image */
  247. {
  248. int i, len;
  249. len=strlen(resume_file);
  250. root_swap = 0xFFFF;
  251. spin_lock(&swap_lock);
  252. for (i=0; i<MAX_SWAPFILES; i++) {
  253. if (!(swap_info[i].flags & SWP_WRITEOK)) {
  254. swapfile_used[i]=SWAPFILE_UNUSED;
  255. } else {
  256. if (!len) {
  257. printk(KERN_WARNING "resume= option should be used to set suspend device" );
  258. if (root_swap == 0xFFFF) {
  259. swapfile_used[i] = SWAPFILE_SUSPEND;
  260. root_swap = i;
  261. } else
  262. swapfile_used[i] = SWAPFILE_IGNORED;
  263. } else {
  264. /* we ignore all swap devices that are not the resume_file */
  265. if (is_resume_device(&swap_info[i])) {
  266. swapfile_used[i] = SWAPFILE_SUSPEND;
  267. root_swap = i;
  268. } else {
  269. swapfile_used[i] = SWAPFILE_IGNORED;
  270. }
  271. }
  272. }
  273. }
  274. spin_unlock(&swap_lock);
  275. return (root_swap != 0xffff) ? 0 : -ENODEV;
  276. }
  277. /**
  278. * This is called after saving image so modification
  279. * will be lost after resume... and that's what we want.
  280. * we make the device unusable. A new call to
  281. * lock_swapdevices can unlock the devices.
  282. */
  283. static void lock_swapdevices(void)
  284. {
  285. int i;
  286. spin_lock(&swap_lock);
  287. for (i = 0; i< MAX_SWAPFILES; i++)
  288. if (swapfile_used[i] == SWAPFILE_IGNORED) {
  289. swap_info[i].flags ^= SWP_WRITEOK;
  290. }
  291. spin_unlock(&swap_lock);
  292. }
  293. /**
  294. * write_page - Write one page to a fresh swap location.
  295. * @addr: Address we're writing.
  296. * @loc: Place to store the entry we used.
  297. *
  298. * Allocate a new swap entry and 'sync' it. Note we discard -EIO
  299. * errors. That is an artifact left over from swsusp. It did not
  300. * check the return of rw_swap_page_sync() at all, since most pages
  301. * written back to swap would return -EIO.
  302. * This is a partial improvement, since we will at least return other
  303. * errors, though we need to eventually fix the damn code.
  304. */
  305. static int write_page(unsigned long addr, swp_entry_t * loc)
  306. {
  307. swp_entry_t entry;
  308. int error = 0;
  309. entry = get_swap_page();
  310. if (swp_offset(entry) &&
  311. swapfile_used[swp_type(entry)] == SWAPFILE_SUSPEND) {
  312. error = rw_swap_page_sync(WRITE, entry,
  313. virt_to_page(addr));
  314. if (error == -EIO)
  315. error = 0;
  316. if (!error)
  317. *loc = entry;
  318. } else
  319. error = -ENOSPC;
  320. return error;
  321. }
  322. /**
  323. * data_free - Free the swap entries used by the saved image.
  324. *
  325. * Walk the list of used swap entries and free each one.
  326. * This is only used for cleanup when suspend fails.
  327. */
  328. static void data_free(void)
  329. {
  330. swp_entry_t entry;
  331. struct pbe * p;
  332. for_each_pbe(p, pagedir_nosave) {
  333. entry = p->swap_address;
  334. if (entry.val)
  335. swap_free(entry);
  336. else
  337. break;
  338. }
  339. }
  340. /**
  341. * data_write - Write saved image to swap.
  342. *
  343. * Walk the list of pages in the image and sync each one to swap.
  344. */
  345. static int data_write(void)
  346. {
  347. int error = 0, i = 0;
  348. unsigned int mod = nr_copy_pages / 100;
  349. struct pbe *p;
  350. void *tfm;
  351. if ((error = crypto_init(1, &tfm)))
  352. return error;
  353. if (!mod)
  354. mod = 1;
  355. printk( "Writing data to swap (%d pages)... ", nr_copy_pages );
  356. for_each_pbe (p, pagedir_nosave) {
  357. if (!(i%mod))
  358. printk( "\b\b\b\b%3d%%", i / mod );
  359. if ((error = crypto_write(p, tfm))) {
  360. crypto_exit(tfm);
  361. return error;
  362. }
  363. i++;
  364. }
  365. printk("\b\b\b\bdone\n");
  366. crypto_exit(tfm);
  367. return error;
  368. }
  369. static void dump_info(void)
  370. {
  371. pr_debug(" swsusp: Version: %u\n",swsusp_info.version_code);
  372. pr_debug(" swsusp: Num Pages: %ld\n",swsusp_info.num_physpages);
  373. pr_debug(" swsusp: UTS Sys: %s\n",swsusp_info.uts.sysname);
  374. pr_debug(" swsusp: UTS Node: %s\n",swsusp_info.uts.nodename);
  375. pr_debug(" swsusp: UTS Release: %s\n",swsusp_info.uts.release);
  376. pr_debug(" swsusp: UTS Version: %s\n",swsusp_info.uts.version);
  377. pr_debug(" swsusp: UTS Machine: %s\n",swsusp_info.uts.machine);
  378. pr_debug(" swsusp: UTS Domain: %s\n",swsusp_info.uts.domainname);
  379. pr_debug(" swsusp: CPUs: %d\n",swsusp_info.cpus);
  380. pr_debug(" swsusp: Image: %ld Pages\n",swsusp_info.image_pages);
  381. pr_debug(" swsusp: Pagedir: %ld Pages\n",swsusp_info.pagedir_pages);
  382. }
  383. static void init_header(void)
  384. {
  385. memset(&swsusp_info, 0, sizeof(swsusp_info));
  386. swsusp_info.version_code = LINUX_VERSION_CODE;
  387. swsusp_info.num_physpages = num_physpages;
  388. memcpy(&swsusp_info.uts, &system_utsname, sizeof(system_utsname));
  389. swsusp_info.suspend_pagedir = pagedir_nosave;
  390. swsusp_info.cpus = num_online_cpus();
  391. swsusp_info.image_pages = nr_copy_pages;
  392. }
  393. static int close_swap(void)
  394. {
  395. swp_entry_t entry;
  396. int error;
  397. dump_info();
  398. error = write_page((unsigned long)&swsusp_info, &entry);
  399. if (!error) {
  400. printk( "S" );
  401. error = mark_swapfiles(entry);
  402. printk( "|\n" );
  403. }
  404. return error;
  405. }
  406. /**
  407. * free_pagedir_entries - Free pages used by the page directory.
  408. *
  409. * This is used during suspend for error recovery.
  410. */
  411. static void free_pagedir_entries(void)
  412. {
  413. int i;
  414. for (i = 0; i < swsusp_info.pagedir_pages; i++)
  415. swap_free(swsusp_info.pagedir[i]);
  416. }
  417. /**
  418. * write_pagedir - Write the array of pages holding the page directory.
  419. * @last: Last swap entry we write (needed for header).
  420. */
  421. static int write_pagedir(void)
  422. {
  423. int error = 0;
  424. unsigned n = 0;
  425. struct pbe * pbe;
  426. printk( "Writing pagedir...");
  427. for_each_pb_page (pbe, pagedir_nosave) {
  428. if ((error = write_page((unsigned long)pbe, &swsusp_info.pagedir[n++])))
  429. return error;
  430. }
  431. swsusp_info.pagedir_pages = n;
  432. printk("done (%u pages)\n", n);
  433. return error;
  434. }
  435. /**
  436. * write_suspend_image - Write entire image and metadata.
  437. *
  438. */
  439. static int write_suspend_image(void)
  440. {
  441. int error;
  442. init_header();
  443. if ((error = data_write()))
  444. goto FreeData;
  445. if ((error = write_pagedir()))
  446. goto FreePagedir;
  447. if ((error = close_swap()))
  448. goto FreePagedir;
  449. Done:
  450. memset(key_iv, 0, MAXKEY+MAXIV);
  451. return error;
  452. FreePagedir:
  453. free_pagedir_entries();
  454. FreeData:
  455. data_free();
  456. goto Done;
  457. }
  458. /**
  459. * enough_swap - Make sure we have enough swap to save the image.
  460. *
  461. * Returns TRUE or FALSE after checking the total amount of swap
  462. * space avaiable.
  463. *
  464. * FIXME: si_swapinfo(&i) returns all swap devices information.
  465. * We should only consider resume_device.
  466. */
  467. int enough_swap(unsigned nr_pages)
  468. {
  469. struct sysinfo i;
  470. si_swapinfo(&i);
  471. pr_debug("swsusp: available swap: %lu pages\n", i.freeswap);
  472. return i.freeswap > (nr_pages + PAGES_FOR_IO +
  473. (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
  474. }
  475. /* It is important _NOT_ to umount filesystems at this point. We want
  476. * them synced (in case something goes wrong) but we DO not want to mark
  477. * filesystem clean: it is not. (And it does not matter, if we resume
  478. * correctly, we'll mark system clean, anyway.)
  479. */
  480. int swsusp_write(void)
  481. {
  482. int error;
  483. lock_swapdevices();
  484. error = write_suspend_image();
  485. /* This will unlock ignored swap devices since writing is finished */
  486. lock_swapdevices();
  487. return error;
  488. }
  489. int swsusp_suspend(void)
  490. {
  491. int error;
  492. if ((error = arch_prepare_suspend()))
  493. return error;
  494. local_irq_disable();
  495. /* At this point, device_suspend() has been called, but *not*
  496. * device_power_down(). We *must* device_power_down() now.
  497. * Otherwise, drivers for some devices (e.g. interrupt controllers)
  498. * become desynchronized with the actual state of the hardware
  499. * at resume time, and evil weirdness ensues.
  500. */
  501. if ((error = device_power_down(PMSG_FREEZE))) {
  502. printk(KERN_ERR "Some devices failed to power down, aborting suspend\n");
  503. local_irq_enable();
  504. return error;
  505. }
  506. if ((error = swsusp_swap_check())) {
  507. printk(KERN_ERR "swsusp: cannot find swap device, try swapon -a.\n");
  508. device_power_up();
  509. local_irq_enable();
  510. return error;
  511. }
  512. save_processor_state();
  513. if ((error = swsusp_arch_suspend()))
  514. printk(KERN_ERR "Error %d suspending\n", error);
  515. /* Restore control flow magically appears here */
  516. restore_processor_state();
  517. restore_highmem();
  518. device_power_up();
  519. local_irq_enable();
  520. return error;
  521. }
  522. int swsusp_resume(void)
  523. {
  524. int error;
  525. local_irq_disable();
  526. if (device_power_down(PMSG_FREEZE))
  527. printk(KERN_ERR "Some devices failed to power down, very bad\n");
  528. /* We'll ignore saved state, but this gets preempt count (etc) right */
  529. save_processor_state();
  530. error = swsusp_arch_resume();
  531. /* Code below is only ever reached in case of failure. Otherwise
  532. * execution continues at place where swsusp_arch_suspend was called
  533. */
  534. BUG_ON(!error);
  535. /* The only reason why swsusp_arch_resume() can fail is memory being
  536. * very tight, so we have to free it as soon as we can to avoid
  537. * subsequent failures
  538. */
  539. swsusp_free();
  540. restore_processor_state();
  541. restore_highmem();
  542. touch_softlockup_watchdog();
  543. device_power_up();
  544. local_irq_enable();
  545. return error;
  546. }
  547. /**
  548. * On resume, for storing the PBE list and the image,
  549. * we can only use memory pages that do not conflict with the pages
  550. * which had been used before suspend.
  551. *
  552. * We don't know which pages are usable until we allocate them.
  553. *
  554. * Allocated but unusable (ie eaten) memory pages are marked so that
  555. * swsusp_free() can release them
  556. */
  557. unsigned long get_safe_page(gfp_t gfp_mask)
  558. {
  559. unsigned long m;
  560. do {
  561. m = get_zeroed_page(gfp_mask);
  562. if (m && PageNosaveFree(virt_to_page(m)))
  563. /* This is for swsusp_free() */
  564. SetPageNosave(virt_to_page(m));
  565. } while (m && PageNosaveFree(virt_to_page(m)));
  566. if (m) {
  567. /* This is for swsusp_free() */
  568. SetPageNosave(virt_to_page(m));
  569. SetPageNosaveFree(virt_to_page(m));
  570. }
  571. return m;
  572. }
  573. /**
  574. * check_pagedir - We ensure here that pages that the PBEs point to
  575. * won't collide with pages where we're going to restore from the loaded
  576. * pages later
  577. */
  578. static int check_pagedir(struct pbe *pblist)
  579. {
  580. struct pbe *p;
  581. /* This is necessary, so that we can free allocated pages
  582. * in case of failure
  583. */
  584. for_each_pbe (p, pblist)
  585. p->address = 0UL;
  586. for_each_pbe (p, pblist) {
  587. p->address = get_safe_page(GFP_ATOMIC);
  588. if (!p->address)
  589. return -ENOMEM;
  590. }
  591. return 0;
  592. }
  593. /**
  594. * swsusp_pagedir_relocate - It is possible, that some memory pages
  595. * occupied by the list of PBEs collide with pages where we're going to
  596. * restore from the loaded pages later. We relocate them here.
  597. */
  598. static struct pbe * swsusp_pagedir_relocate(struct pbe *pblist)
  599. {
  600. struct zone *zone;
  601. unsigned long zone_pfn;
  602. struct pbe *pbpage, *tail, *p;
  603. void *m;
  604. int rel = 0;
  605. if (!pblist) /* a sanity check */
  606. return NULL;
  607. pr_debug("swsusp: Relocating pagedir (%lu pages to check)\n",
  608. swsusp_info.pagedir_pages);
  609. /* Clear page flags */
  610. for_each_zone (zone) {
  611. for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
  612. if (pfn_valid(zone_pfn + zone->zone_start_pfn))
  613. ClearPageNosaveFree(pfn_to_page(zone_pfn +
  614. zone->zone_start_pfn));
  615. }
  616. /* Mark orig addresses */
  617. for_each_pbe (p, pblist)
  618. SetPageNosaveFree(virt_to_page(p->orig_address));
  619. tail = pblist + PB_PAGE_SKIP;
  620. /* Relocate colliding pages */
  621. for_each_pb_page (pbpage, pblist) {
  622. if (PageNosaveFree(virt_to_page((unsigned long)pbpage))) {
  623. m = (void *)get_safe_page(GFP_ATOMIC | __GFP_COLD);
  624. if (!m)
  625. return NULL;
  626. memcpy(m, (void *)pbpage, PAGE_SIZE);
  627. if (pbpage == pblist)
  628. pblist = (struct pbe *)m;
  629. else
  630. tail->next = (struct pbe *)m;
  631. pbpage = (struct pbe *)m;
  632. /* We have to link the PBEs again */
  633. for (p = pbpage; p < pbpage + PB_PAGE_SKIP; p++)
  634. if (p->next) /* needed to save the end */
  635. p->next = p + 1;
  636. rel++;
  637. }
  638. tail = pbpage + PB_PAGE_SKIP;
  639. }
  640. /* This is for swsusp_free() */
  641. for_each_pb_page (pbpage, pblist) {
  642. SetPageNosave(virt_to_page(pbpage));
  643. SetPageNosaveFree(virt_to_page(pbpage));
  644. }
  645. printk("swsusp: Relocated %d pages\n", rel);
  646. return pblist;
  647. }
  648. /*
  649. * Using bio to read from swap.
  650. * This code requires a bit more work than just using buffer heads
  651. * but, it is the recommended way for 2.5/2.6.
  652. * The following are to signal the beginning and end of I/O. Bios
  653. * finish asynchronously, while we want them to happen synchronously.
  654. * A simple atomic_t, and a wait loop take care of this problem.
  655. */
  656. static atomic_t io_done = ATOMIC_INIT(0);
  657. static int end_io(struct bio * bio, unsigned int num, int err)
  658. {
  659. if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
  660. panic("I/O error reading memory image");
  661. atomic_set(&io_done, 0);
  662. return 0;
  663. }
  664. static struct block_device * resume_bdev;
  665. /**
  666. * submit - submit BIO request.
  667. * @rw: READ or WRITE.
  668. * @off physical offset of page.
  669. * @page: page we're reading or writing.
  670. *
  671. * Straight from the textbook - allocate and initialize the bio.
  672. * If we're writing, make sure the page is marked as dirty.
  673. * Then submit it and wait.
  674. */
  675. static int submit(int rw, pgoff_t page_off, void * page)
  676. {
  677. int error = 0;
  678. struct bio * bio;
  679. bio = bio_alloc(GFP_ATOMIC, 1);
  680. if (!bio)
  681. return -ENOMEM;
  682. bio->bi_sector = page_off * (PAGE_SIZE >> 9);
  683. bio_get(bio);
  684. bio->bi_bdev = resume_bdev;
  685. bio->bi_end_io = end_io;
  686. if (bio_add_page(bio, virt_to_page(page), PAGE_SIZE, 0) < PAGE_SIZE) {
  687. printk("swsusp: ERROR: adding page to bio at %ld\n",page_off);
  688. error = -EFAULT;
  689. goto Done;
  690. }
  691. if (rw == WRITE)
  692. bio_set_pages_dirty(bio);
  693. atomic_set(&io_done, 1);
  694. submit_bio(rw | (1 << BIO_RW_SYNC), bio);
  695. while (atomic_read(&io_done))
  696. yield();
  697. Done:
  698. bio_put(bio);
  699. return error;
  700. }
  701. static int bio_read_page(pgoff_t page_off, void * page)
  702. {
  703. return submit(READ, page_off, page);
  704. }
  705. static int bio_write_page(pgoff_t page_off, void * page)
  706. {
  707. return submit(WRITE, page_off, page);
  708. }
  709. /*
  710. * Sanity check if this image makes sense with this kernel/swap context
  711. * I really don't think that it's foolproof but more than nothing..
  712. */
  713. static const char * sanity_check(void)
  714. {
  715. dump_info();
  716. if (swsusp_info.version_code != LINUX_VERSION_CODE)
  717. return "kernel version";
  718. if (swsusp_info.num_physpages != num_physpages)
  719. return "memory size";
  720. if (strcmp(swsusp_info.uts.sysname,system_utsname.sysname))
  721. return "system type";
  722. if (strcmp(swsusp_info.uts.release,system_utsname.release))
  723. return "kernel release";
  724. if (strcmp(swsusp_info.uts.version,system_utsname.version))
  725. return "version";
  726. if (strcmp(swsusp_info.uts.machine,system_utsname.machine))
  727. return "machine";
  728. #if 0
  729. /* We can't use number of online CPUs when we use hotplug to remove them ;-))) */
  730. if (swsusp_info.cpus != num_possible_cpus())
  731. return "number of cpus";
  732. #endif
  733. return NULL;
  734. }
  735. static int check_header(void)
  736. {
  737. const char * reason = NULL;
  738. int error;
  739. if ((error = bio_read_page(swp_offset(swsusp_header.swsusp_info), &swsusp_info)))
  740. return error;
  741. /* Is this same machine? */
  742. if ((reason = sanity_check())) {
  743. printk(KERN_ERR "swsusp: Resume mismatch: %s\n",reason);
  744. return -EPERM;
  745. }
  746. nr_copy_pages = swsusp_info.image_pages;
  747. return error;
  748. }
  749. static int check_sig(void)
  750. {
  751. int error;
  752. memset(&swsusp_header, 0, sizeof(swsusp_header));
  753. if ((error = bio_read_page(0, &swsusp_header)))
  754. return error;
  755. if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) {
  756. memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10);
  757. memcpy(key_iv, swsusp_header.key_iv, MAXKEY+MAXIV);
  758. memset(swsusp_header.key_iv, 0, MAXKEY+MAXIV);
  759. /*
  760. * Reset swap signature now.
  761. */
  762. error = bio_write_page(0, &swsusp_header);
  763. } else {
  764. return -EINVAL;
  765. }
  766. if (!error)
  767. pr_debug("swsusp: Signature found, resuming\n");
  768. return error;
  769. }
  770. /**
  771. * data_read - Read image pages from swap.
  772. *
  773. * You do not need to check for overlaps, check_pagedir()
  774. * already did that.
  775. */
  776. static int data_read(struct pbe *pblist)
  777. {
  778. struct pbe * p;
  779. int error = 0;
  780. int i = 0;
  781. int mod = swsusp_info.image_pages / 100;
  782. void *tfm;
  783. if ((error = crypto_init(0, &tfm)))
  784. return error;
  785. if (!mod)
  786. mod = 1;
  787. printk("swsusp: Reading image data (%lu pages): ",
  788. swsusp_info.image_pages);
  789. for_each_pbe (p, pblist) {
  790. if (!(i % mod))
  791. printk("\b\b\b\b%3d%%", i / mod);
  792. if ((error = crypto_read(p, tfm))) {
  793. crypto_exit(tfm);
  794. return error;
  795. }
  796. i++;
  797. }
  798. printk("\b\b\b\bdone\n");
  799. crypto_exit(tfm);
  800. return error;
  801. }
  802. /**
  803. * read_pagedir - Read page backup list pages from swap
  804. */
  805. static int read_pagedir(struct pbe *pblist)
  806. {
  807. struct pbe *pbpage, *p;
  808. unsigned i = 0;
  809. int error;
  810. if (!pblist)
  811. return -EFAULT;
  812. printk("swsusp: Reading pagedir (%lu pages)\n",
  813. swsusp_info.pagedir_pages);
  814. for_each_pb_page (pbpage, pblist) {
  815. unsigned long offset = swp_offset(swsusp_info.pagedir[i++]);
  816. error = -EFAULT;
  817. if (offset) {
  818. p = (pbpage + PB_PAGE_SKIP)->next;
  819. error = bio_read_page(offset, (void *)pbpage);
  820. (pbpage + PB_PAGE_SKIP)->next = p;
  821. }
  822. if (error)
  823. break;
  824. }
  825. if (!error)
  826. BUG_ON(i != swsusp_info.pagedir_pages);
  827. return error;
  828. }
  829. static int check_suspend_image(void)
  830. {
  831. int error = 0;
  832. if ((error = check_sig()))
  833. return error;
  834. if ((error = check_header()))
  835. return error;
  836. return 0;
  837. }
  838. static int read_suspend_image(void)
  839. {
  840. int error = 0;
  841. struct pbe *p;
  842. if (!(p = alloc_pagedir(nr_copy_pages)))
  843. return -ENOMEM;
  844. if ((error = read_pagedir(p)))
  845. return error;
  846. create_pbe_list(p, nr_copy_pages);
  847. if (!(pagedir_nosave = swsusp_pagedir_relocate(p)))
  848. return -ENOMEM;
  849. /* Allocate memory for the image and read the data from swap */
  850. error = check_pagedir(pagedir_nosave);
  851. if (!error)
  852. error = data_read(pagedir_nosave);
  853. return error;
  854. }
  855. /**
  856. * swsusp_check - Check for saved image in swap
  857. */
  858. int swsusp_check(void)
  859. {
  860. int error;
  861. resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
  862. if (!IS_ERR(resume_bdev)) {
  863. set_blocksize(resume_bdev, PAGE_SIZE);
  864. error = check_suspend_image();
  865. if (error)
  866. blkdev_put(resume_bdev);
  867. } else
  868. error = PTR_ERR(resume_bdev);
  869. if (!error)
  870. pr_debug("swsusp: resume file found\n");
  871. else
  872. pr_debug("swsusp: Error %d check for resume file\n", error);
  873. return error;
  874. }
  875. /**
  876. * swsusp_read - Read saved image from swap.
  877. */
  878. int swsusp_read(void)
  879. {
  880. int error;
  881. if (IS_ERR(resume_bdev)) {
  882. pr_debug("swsusp: block device not initialised\n");
  883. return PTR_ERR(resume_bdev);
  884. }
  885. error = read_suspend_image();
  886. blkdev_put(resume_bdev);
  887. memset(key_iv, 0, MAXKEY+MAXIV);
  888. if (!error)
  889. pr_debug("swsusp: Reading resume file was successful\n");
  890. else
  891. pr_debug("swsusp: Error %d resuming\n", error);
  892. return error;
  893. }
  894. /**
  895. * swsusp_close - close swap device.
  896. */
  897. void swsusp_close(void)
  898. {
  899. if (IS_ERR(resume_bdev)) {
  900. pr_debug("swsusp: block device not initialised\n");
  901. return;
  902. }
  903. blkdev_put(resume_bdev);
  904. }