swsusp.c 26 KB

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