swsusp.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408
  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-2004 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. * More state savers are welcome. Especially for the scsi layer...
  35. *
  36. * For TODOs,FIXMEs also look in Documentation/power/swsusp.txt
  37. */
  38. #include <linux/module.h>
  39. #include <linux/mm.h>
  40. #include <linux/suspend.h>
  41. #include <linux/smp_lock.h>
  42. #include <linux/file.h>
  43. #include <linux/utsname.h>
  44. #include <linux/version.h>
  45. #include <linux/delay.h>
  46. #include <linux/reboot.h>
  47. #include <linux/bitops.h>
  48. #include <linux/vt_kern.h>
  49. #include <linux/kbd_kern.h>
  50. #include <linux/keyboard.h>
  51. #include <linux/spinlock.h>
  52. #include <linux/genhd.h>
  53. #include <linux/kernel.h>
  54. #include <linux/major.h>
  55. #include <linux/swap.h>
  56. #include <linux/pm.h>
  57. #include <linux/device.h>
  58. #include <linux/buffer_head.h>
  59. #include <linux/swapops.h>
  60. #include <linux/bootmem.h>
  61. #include <linux/syscalls.h>
  62. #include <linux/console.h>
  63. #include <linux/highmem.h>
  64. #include <linux/bio.h>
  65. #include <linux/mount.h>
  66. #include <asm/uaccess.h>
  67. #include <asm/mmu_context.h>
  68. #include <asm/pgtable.h>
  69. #include <asm/tlbflush.h>
  70. #include <asm/io.h>
  71. #include "power.h"
  72. /* References to section boundaries */
  73. extern const void __nosave_begin, __nosave_end;
  74. /* Variables to be preserved over suspend */
  75. static int nr_copy_pages_check;
  76. extern char resume_file[];
  77. /* Local variables that should not be affected by save */
  78. static unsigned int nr_copy_pages __nosavedata = 0;
  79. /* Suspend pagedir is allocated before final copy, therefore it
  80. must be freed after resume
  81. Warning: this is evil. There are actually two pagedirs at time of
  82. resume. One is "pagedir_save", which is empty frame allocated at
  83. time of suspend, that must be freed. Second is "pagedir_nosave",
  84. allocated at time of resume, that travels through memory not to
  85. collide with anything.
  86. Warning: this is even more evil than it seems. Pagedirs this file
  87. talks about are completely different from page directories used by
  88. MMU hardware.
  89. */
  90. suspend_pagedir_t *pagedir_nosave __nosavedata = NULL;
  91. static suspend_pagedir_t *pagedir_save;
  92. #define SWSUSP_SIG "S1SUSPEND"
  93. static struct swsusp_header {
  94. char reserved[PAGE_SIZE - 20 - sizeof(swp_entry_t)];
  95. swp_entry_t swsusp_info;
  96. char orig_sig[10];
  97. char sig[10];
  98. } __attribute__((packed, aligned(PAGE_SIZE))) swsusp_header;
  99. static struct swsusp_info swsusp_info;
  100. /*
  101. * XXX: We try to keep some more pages free so that I/O operations succeed
  102. * without paging. Might this be more?
  103. */
  104. #define PAGES_FOR_IO 512
  105. /*
  106. * Saving part...
  107. */
  108. /* We memorize in swapfile_used what swap devices are used for suspension */
  109. #define SWAPFILE_UNUSED 0
  110. #define SWAPFILE_SUSPEND 1 /* This is the suspending device */
  111. #define SWAPFILE_IGNORED 2 /* Those are other swap devices ignored for suspension */
  112. static unsigned short swapfile_used[MAX_SWAPFILES];
  113. static unsigned short root_swap;
  114. static int mark_swapfiles(swp_entry_t prev)
  115. {
  116. int error;
  117. rw_swap_page_sync(READ,
  118. swp_entry(root_swap, 0),
  119. virt_to_page((unsigned long)&swsusp_header));
  120. if (!memcmp("SWAP-SPACE",swsusp_header.sig, 10) ||
  121. !memcmp("SWAPSPACE2",swsusp_header.sig, 10)) {
  122. memcpy(swsusp_header.orig_sig,swsusp_header.sig, 10);
  123. memcpy(swsusp_header.sig,SWSUSP_SIG, 10);
  124. swsusp_header.swsusp_info = prev;
  125. error = rw_swap_page_sync(WRITE,
  126. swp_entry(root_swap, 0),
  127. virt_to_page((unsigned long)
  128. &swsusp_header));
  129. } else {
  130. pr_debug("swsusp: Partition is not swap space.\n");
  131. error = -ENODEV;
  132. }
  133. return error;
  134. }
  135. /*
  136. * Check whether the swap device is the specified resume
  137. * device, irrespective of whether they are specified by
  138. * identical names.
  139. *
  140. * (Thus, device inode aliasing is allowed. You can say /dev/hda4
  141. * instead of /dev/ide/host0/bus0/target0/lun0/part4 [if using devfs]
  142. * and they'll be considered the same device. This is *necessary* for
  143. * devfs, since the resume code can only recognize the form /dev/hda4,
  144. * but the suspend code would see the long name.)
  145. */
  146. static int is_resume_device(const struct swap_info_struct *swap_info)
  147. {
  148. struct file *file = swap_info->swap_file;
  149. struct inode *inode = file->f_dentry->d_inode;
  150. return S_ISBLK(inode->i_mode) &&
  151. swsusp_resume_device == MKDEV(imajor(inode), iminor(inode));
  152. }
  153. static int swsusp_swap_check(void) /* This is called before saving image */
  154. {
  155. int i, len;
  156. len=strlen(resume_file);
  157. root_swap = 0xFFFF;
  158. swap_list_lock();
  159. for (i=0; i<MAX_SWAPFILES; i++) {
  160. if (swap_info[i].flags == 0) {
  161. swapfile_used[i]=SWAPFILE_UNUSED;
  162. } else {
  163. if (!len) {
  164. printk(KERN_WARNING "resume= option should be used to set suspend device" );
  165. if (root_swap == 0xFFFF) {
  166. swapfile_used[i] = SWAPFILE_SUSPEND;
  167. root_swap = i;
  168. } else
  169. swapfile_used[i] = SWAPFILE_IGNORED;
  170. } else {
  171. /* we ignore all swap devices that are not the resume_file */
  172. if (is_resume_device(&swap_info[i])) {
  173. swapfile_used[i] = SWAPFILE_SUSPEND;
  174. root_swap = i;
  175. } else {
  176. swapfile_used[i] = SWAPFILE_IGNORED;
  177. }
  178. }
  179. }
  180. }
  181. swap_list_unlock();
  182. return (root_swap != 0xffff) ? 0 : -ENODEV;
  183. }
  184. /**
  185. * This is called after saving image so modification
  186. * will be lost after resume... and that's what we want.
  187. * we make the device unusable. A new call to
  188. * lock_swapdevices can unlock the devices.
  189. */
  190. static void lock_swapdevices(void)
  191. {
  192. int i;
  193. swap_list_lock();
  194. for (i = 0; i< MAX_SWAPFILES; i++)
  195. if (swapfile_used[i] == SWAPFILE_IGNORED) {
  196. swap_info[i].flags ^= 0xFF;
  197. }
  198. swap_list_unlock();
  199. }
  200. /**
  201. * write_swap_page - Write one page to a fresh swap location.
  202. * @addr: Address we're writing.
  203. * @loc: Place to store the entry we used.
  204. *
  205. * Allocate a new swap entry and 'sync' it. Note we discard -EIO
  206. * errors. That is an artifact left over from swsusp. It did not
  207. * check the return of rw_swap_page_sync() at all, since most pages
  208. * written back to swap would return -EIO.
  209. * This is a partial improvement, since we will at least return other
  210. * errors, though we need to eventually fix the damn code.
  211. */
  212. static int write_page(unsigned long addr, swp_entry_t * loc)
  213. {
  214. swp_entry_t entry;
  215. int error = 0;
  216. entry = get_swap_page();
  217. if (swp_offset(entry) &&
  218. swapfile_used[swp_type(entry)] == SWAPFILE_SUSPEND) {
  219. error = rw_swap_page_sync(WRITE, entry,
  220. virt_to_page(addr));
  221. if (error == -EIO)
  222. error = 0;
  223. if (!error)
  224. *loc = entry;
  225. } else
  226. error = -ENOSPC;
  227. return error;
  228. }
  229. /**
  230. * data_free - Free the swap entries used by the saved image.
  231. *
  232. * Walk the list of used swap entries and free each one.
  233. * This is only used for cleanup when suspend fails.
  234. */
  235. static void data_free(void)
  236. {
  237. swp_entry_t entry;
  238. int i;
  239. for (i = 0; i < nr_copy_pages; i++) {
  240. entry = (pagedir_nosave + i)->swap_address;
  241. if (entry.val)
  242. swap_free(entry);
  243. else
  244. break;
  245. (pagedir_nosave + i)->swap_address = (swp_entry_t){0};
  246. }
  247. }
  248. /**
  249. * data_write - Write saved image to swap.
  250. *
  251. * Walk the list of pages in the image and sync each one to swap.
  252. */
  253. static int data_write(void)
  254. {
  255. int error = 0, i = 0;
  256. unsigned int mod = nr_copy_pages / 100;
  257. struct pbe *p;
  258. if (!mod)
  259. mod = 1;
  260. printk( "Writing data to swap (%d pages)... ", nr_copy_pages );
  261. for_each_pbe (p, pagedir_nosave) {
  262. if (!(i%mod))
  263. printk( "\b\b\b\b%3d%%", i / mod );
  264. if ((error = write_page(p->address, &(p->swap_address))))
  265. return error;
  266. i++;
  267. }
  268. printk("\b\b\b\bdone\n");
  269. return error;
  270. }
  271. static void dump_info(void)
  272. {
  273. pr_debug(" swsusp: Version: %u\n",swsusp_info.version_code);
  274. pr_debug(" swsusp: Num Pages: %ld\n",swsusp_info.num_physpages);
  275. pr_debug(" swsusp: UTS Sys: %s\n",swsusp_info.uts.sysname);
  276. pr_debug(" swsusp: UTS Node: %s\n",swsusp_info.uts.nodename);
  277. pr_debug(" swsusp: UTS Release: %s\n",swsusp_info.uts.release);
  278. pr_debug(" swsusp: UTS Version: %s\n",swsusp_info.uts.version);
  279. pr_debug(" swsusp: UTS Machine: %s\n",swsusp_info.uts.machine);
  280. pr_debug(" swsusp: UTS Domain: %s\n",swsusp_info.uts.domainname);
  281. pr_debug(" swsusp: CPUs: %d\n",swsusp_info.cpus);
  282. pr_debug(" swsusp: Image: %ld Pages\n",swsusp_info.image_pages);
  283. pr_debug(" swsusp: Pagedir: %ld Pages\n",swsusp_info.pagedir_pages);
  284. }
  285. static void init_header(void)
  286. {
  287. memset(&swsusp_info, 0, sizeof(swsusp_info));
  288. swsusp_info.version_code = LINUX_VERSION_CODE;
  289. swsusp_info.num_physpages = num_physpages;
  290. memcpy(&swsusp_info.uts, &system_utsname, sizeof(system_utsname));
  291. swsusp_info.suspend_pagedir = pagedir_nosave;
  292. swsusp_info.cpus = num_online_cpus();
  293. swsusp_info.image_pages = nr_copy_pages;
  294. }
  295. static int close_swap(void)
  296. {
  297. swp_entry_t entry;
  298. int error;
  299. dump_info();
  300. error = write_page((unsigned long)&swsusp_info, &entry);
  301. if (!error) {
  302. printk( "S" );
  303. error = mark_swapfiles(entry);
  304. printk( "|\n" );
  305. }
  306. return error;
  307. }
  308. /**
  309. * free_pagedir_entries - Free pages used by the page directory.
  310. *
  311. * This is used during suspend for error recovery.
  312. */
  313. static void free_pagedir_entries(void)
  314. {
  315. int i;
  316. for (i = 0; i < swsusp_info.pagedir_pages; i++)
  317. swap_free(swsusp_info.pagedir[i]);
  318. }
  319. /**
  320. * write_pagedir - Write the array of pages holding the page directory.
  321. * @last: Last swap entry we write (needed for header).
  322. */
  323. static int write_pagedir(void)
  324. {
  325. int error = 0;
  326. unsigned n = 0;
  327. struct pbe * pbe;
  328. printk( "Writing pagedir...");
  329. for_each_pb_page (pbe, pagedir_nosave) {
  330. if ((error = write_page((unsigned long)pbe, &swsusp_info.pagedir[n++])))
  331. return error;
  332. }
  333. swsusp_info.pagedir_pages = n;
  334. printk("done (%u pages)\n", n);
  335. return error;
  336. }
  337. /**
  338. * write_suspend_image - Write entire image and metadata.
  339. *
  340. */
  341. static int write_suspend_image(void)
  342. {
  343. int error;
  344. init_header();
  345. if ((error = data_write()))
  346. goto FreeData;
  347. if ((error = write_pagedir()))
  348. goto FreePagedir;
  349. if ((error = close_swap()))
  350. goto FreePagedir;
  351. Done:
  352. return error;
  353. FreePagedir:
  354. free_pagedir_entries();
  355. FreeData:
  356. data_free();
  357. goto Done;
  358. }
  359. #ifdef CONFIG_HIGHMEM
  360. struct highmem_page {
  361. char *data;
  362. struct page *page;
  363. struct highmem_page *next;
  364. };
  365. static struct highmem_page *highmem_copy;
  366. static int save_highmem_zone(struct zone *zone)
  367. {
  368. unsigned long zone_pfn;
  369. mark_free_pages(zone);
  370. for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
  371. struct page *page;
  372. struct highmem_page *save;
  373. void *kaddr;
  374. unsigned long pfn = zone_pfn + zone->zone_start_pfn;
  375. if (!(pfn%1000))
  376. printk(".");
  377. if (!pfn_valid(pfn))
  378. continue;
  379. page = pfn_to_page(pfn);
  380. /*
  381. * This condition results from rvmalloc() sans vmalloc_32()
  382. * and architectural memory reservations. This should be
  383. * corrected eventually when the cases giving rise to this
  384. * are better understood.
  385. */
  386. if (PageReserved(page)) {
  387. printk("highmem reserved page?!\n");
  388. continue;
  389. }
  390. BUG_ON(PageNosave(page));
  391. if (PageNosaveFree(page))
  392. continue;
  393. save = kmalloc(sizeof(struct highmem_page), GFP_ATOMIC);
  394. if (!save)
  395. return -ENOMEM;
  396. save->next = highmem_copy;
  397. save->page = page;
  398. save->data = (void *) get_zeroed_page(GFP_ATOMIC);
  399. if (!save->data) {
  400. kfree(save);
  401. return -ENOMEM;
  402. }
  403. kaddr = kmap_atomic(page, KM_USER0);
  404. memcpy(save->data, kaddr, PAGE_SIZE);
  405. kunmap_atomic(kaddr, KM_USER0);
  406. highmem_copy = save;
  407. }
  408. return 0;
  409. }
  410. #endif /* CONFIG_HIGHMEM */
  411. static int save_highmem(void)
  412. {
  413. #ifdef CONFIG_HIGHMEM
  414. struct zone *zone;
  415. int res = 0;
  416. pr_debug("swsusp: Saving Highmem\n");
  417. for_each_zone (zone) {
  418. if (is_highmem(zone))
  419. res = save_highmem_zone(zone);
  420. if (res)
  421. return res;
  422. }
  423. #endif
  424. return 0;
  425. }
  426. static int restore_highmem(void)
  427. {
  428. #ifdef CONFIG_HIGHMEM
  429. printk("swsusp: Restoring Highmem\n");
  430. while (highmem_copy) {
  431. struct highmem_page *save = highmem_copy;
  432. void *kaddr;
  433. highmem_copy = save->next;
  434. kaddr = kmap_atomic(save->page, KM_USER0);
  435. memcpy(kaddr, save->data, PAGE_SIZE);
  436. kunmap_atomic(kaddr, KM_USER0);
  437. free_page((long) save->data);
  438. kfree(save);
  439. }
  440. #endif
  441. return 0;
  442. }
  443. static int pfn_is_nosave(unsigned long pfn)
  444. {
  445. unsigned long nosave_begin_pfn = __pa(&__nosave_begin) >> PAGE_SHIFT;
  446. unsigned long nosave_end_pfn = PAGE_ALIGN(__pa(&__nosave_end)) >> PAGE_SHIFT;
  447. return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn);
  448. }
  449. /**
  450. * saveable - Determine whether a page should be cloned or not.
  451. * @pfn: The page
  452. *
  453. * We save a page if it's Reserved, and not in the range of pages
  454. * statically defined as 'unsaveable', or if it isn't reserved, and
  455. * isn't part of a free chunk of pages.
  456. */
  457. static int saveable(struct zone * zone, unsigned long * zone_pfn)
  458. {
  459. unsigned long pfn = *zone_pfn + zone->zone_start_pfn;
  460. struct page * page;
  461. if (!pfn_valid(pfn))
  462. return 0;
  463. page = pfn_to_page(pfn);
  464. BUG_ON(PageReserved(page) && PageNosave(page));
  465. if (PageNosave(page))
  466. return 0;
  467. if (PageReserved(page) && pfn_is_nosave(pfn)) {
  468. pr_debug("[nosave pfn 0x%lx]", pfn);
  469. return 0;
  470. }
  471. if (PageNosaveFree(page))
  472. return 0;
  473. return 1;
  474. }
  475. static void count_data_pages(void)
  476. {
  477. struct zone *zone;
  478. unsigned long zone_pfn;
  479. nr_copy_pages = 0;
  480. for_each_zone (zone) {
  481. if (is_highmem(zone))
  482. continue;
  483. mark_free_pages(zone);
  484. for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
  485. nr_copy_pages += saveable(zone, &zone_pfn);
  486. }
  487. }
  488. static void copy_data_pages(void)
  489. {
  490. struct zone *zone;
  491. unsigned long zone_pfn;
  492. struct pbe * pbe = pagedir_nosave;
  493. pr_debug("copy_data_pages(): pages to copy: %d\n", nr_copy_pages);
  494. for_each_zone (zone) {
  495. if (is_highmem(zone))
  496. continue;
  497. mark_free_pages(zone);
  498. for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
  499. if (saveable(zone, &zone_pfn)) {
  500. struct page * page;
  501. page = pfn_to_page(zone_pfn + zone->zone_start_pfn);
  502. BUG_ON(!pbe);
  503. pbe->orig_address = (long) page_address(page);
  504. /* copy_page is not usable for copying task structs. */
  505. memcpy((void *)pbe->address, (void *)pbe->orig_address, PAGE_SIZE);
  506. pbe = pbe->next;
  507. }
  508. }
  509. }
  510. BUG_ON(pbe);
  511. }
  512. /**
  513. * calc_nr - Determine the number of pages needed for a pbe list.
  514. */
  515. static int calc_nr(int nr_copy)
  516. {
  517. int extra = 0;
  518. int mod = !!(nr_copy % PBES_PER_PAGE);
  519. int diff = (nr_copy / PBES_PER_PAGE) + mod;
  520. do {
  521. extra += diff;
  522. nr_copy += diff;
  523. mod = !!(nr_copy % PBES_PER_PAGE);
  524. diff = (nr_copy / PBES_PER_PAGE) + mod - extra;
  525. } while (diff > 0);
  526. return nr_copy;
  527. }
  528. /**
  529. * free_pagedir - free pages allocated with alloc_pagedir()
  530. */
  531. static inline void free_pagedir(struct pbe *pblist)
  532. {
  533. struct pbe *pbe;
  534. while (pblist) {
  535. pbe = (pblist + PB_PAGE_SKIP)->next;
  536. free_page((unsigned long)pblist);
  537. pblist = pbe;
  538. }
  539. }
  540. /**
  541. * fill_pb_page - Create a list of PBEs on a given memory page
  542. */
  543. static inline void fill_pb_page(struct pbe *pbpage)
  544. {
  545. struct pbe *p;
  546. p = pbpage;
  547. pbpage += PB_PAGE_SKIP;
  548. do
  549. p->next = p + 1;
  550. while (++p < pbpage);
  551. }
  552. /**
  553. * create_pbe_list - Create a list of PBEs on top of a given chain
  554. * of memory pages allocated with alloc_pagedir()
  555. */
  556. static void create_pbe_list(struct pbe *pblist, unsigned nr_pages)
  557. {
  558. struct pbe *pbpage, *p;
  559. unsigned num = PBES_PER_PAGE;
  560. for_each_pb_page (pbpage, pblist) {
  561. if (num >= nr_pages)
  562. break;
  563. fill_pb_page(pbpage);
  564. num += PBES_PER_PAGE;
  565. }
  566. if (pbpage) {
  567. for (num -= PBES_PER_PAGE - 1, p = pbpage; num < nr_pages; p++, num++)
  568. p->next = p + 1;
  569. p->next = NULL;
  570. }
  571. pr_debug("create_pbe_list(): initialized %d PBEs\n", num);
  572. }
  573. /**
  574. * alloc_pagedir - Allocate the page directory.
  575. *
  576. * First, determine exactly how many pages we need and
  577. * allocate them.
  578. *
  579. * We arrange the pages in a chain: each page is an array of PBES_PER_PAGE
  580. * struct pbe elements (pbes) and the last element in the page points
  581. * to the next page.
  582. *
  583. * On each page we set up a list of struct_pbe elements.
  584. */
  585. static struct pbe * alloc_pagedir(unsigned nr_pages)
  586. {
  587. unsigned num;
  588. struct pbe *pblist, *pbe;
  589. if (!nr_pages)
  590. return NULL;
  591. pr_debug("alloc_pagedir(): nr_pages = %d\n", nr_pages);
  592. pblist = (struct pbe *)get_zeroed_page(GFP_ATOMIC | __GFP_COLD);
  593. for (pbe = pblist, num = PBES_PER_PAGE; pbe && num < nr_pages;
  594. pbe = pbe->next, num += PBES_PER_PAGE) {
  595. pbe += PB_PAGE_SKIP;
  596. pbe->next = (struct pbe *)get_zeroed_page(GFP_ATOMIC | __GFP_COLD);
  597. }
  598. if (!pbe) { /* get_zeroed_page() failed */
  599. free_pagedir(pblist);
  600. pblist = NULL;
  601. }
  602. return pblist;
  603. }
  604. /**
  605. * free_image_pages - Free pages allocated for snapshot
  606. */
  607. static void free_image_pages(void)
  608. {
  609. struct pbe * p;
  610. for_each_pbe (p, pagedir_save) {
  611. if (p->address) {
  612. ClearPageNosave(virt_to_page(p->address));
  613. free_page(p->address);
  614. p->address = 0;
  615. }
  616. }
  617. }
  618. /**
  619. * alloc_image_pages - Allocate pages for the snapshot.
  620. */
  621. static int alloc_image_pages(void)
  622. {
  623. struct pbe * p;
  624. for_each_pbe (p, pagedir_save) {
  625. p->address = get_zeroed_page(GFP_ATOMIC | __GFP_COLD);
  626. if (!p->address)
  627. return -ENOMEM;
  628. SetPageNosave(virt_to_page(p->address));
  629. }
  630. return 0;
  631. }
  632. void swsusp_free(void)
  633. {
  634. BUG_ON(PageNosave(virt_to_page(pagedir_save)));
  635. BUG_ON(PageNosaveFree(virt_to_page(pagedir_save)));
  636. free_image_pages();
  637. free_pagedir(pagedir_save);
  638. }
  639. /**
  640. * enough_free_mem - Make sure we enough free memory to snapshot.
  641. *
  642. * Returns TRUE or FALSE after checking the number of available
  643. * free pages.
  644. */
  645. static int enough_free_mem(void)
  646. {
  647. if (nr_free_pages() < (nr_copy_pages + PAGES_FOR_IO)) {
  648. pr_debug("swsusp: Not enough free pages: Have %d\n",
  649. nr_free_pages());
  650. return 0;
  651. }
  652. return 1;
  653. }
  654. /**
  655. * enough_swap - Make sure we have enough swap to save the image.
  656. *
  657. * Returns TRUE or FALSE after checking the total amount of swap
  658. * space avaiable.
  659. *
  660. * FIXME: si_swapinfo(&i) returns all swap devices information.
  661. * We should only consider resume_device.
  662. */
  663. static int enough_swap(void)
  664. {
  665. struct sysinfo i;
  666. si_swapinfo(&i);
  667. if (i.freeswap < (nr_copy_pages + PAGES_FOR_IO)) {
  668. pr_debug("swsusp: Not enough swap. Need %ld\n",i.freeswap);
  669. return 0;
  670. }
  671. return 1;
  672. }
  673. static int swsusp_alloc(void)
  674. {
  675. int error;
  676. pagedir_nosave = NULL;
  677. nr_copy_pages = calc_nr(nr_copy_pages);
  678. pr_debug("suspend: (pages needed: %d + %d free: %d)\n",
  679. nr_copy_pages, PAGES_FOR_IO, nr_free_pages());
  680. if (!enough_free_mem())
  681. return -ENOMEM;
  682. if (!enough_swap())
  683. return -ENOSPC;
  684. if (!(pagedir_save = alloc_pagedir(nr_copy_pages))) {
  685. printk(KERN_ERR "suspend: Allocating pagedir failed.\n");
  686. return -ENOMEM;
  687. }
  688. create_pbe_list(pagedir_save, nr_copy_pages);
  689. pagedir_nosave = pagedir_save;
  690. if ((error = alloc_image_pages())) {
  691. printk(KERN_ERR "suspend: Allocating image pages failed.\n");
  692. swsusp_free();
  693. return error;
  694. }
  695. nr_copy_pages_check = nr_copy_pages;
  696. return 0;
  697. }
  698. static int suspend_prepare_image(void)
  699. {
  700. int error;
  701. pr_debug("swsusp: critical section: \n");
  702. if (save_highmem()) {
  703. printk(KERN_CRIT "Suspend machine: Not enough free pages for highmem\n");
  704. restore_highmem();
  705. return -ENOMEM;
  706. }
  707. drain_local_pages();
  708. count_data_pages();
  709. printk("swsusp: Need to copy %u pages\n", nr_copy_pages);
  710. error = swsusp_alloc();
  711. if (error)
  712. return error;
  713. /* During allocating of suspend pagedir, new cold pages may appear.
  714. * Kill them.
  715. */
  716. drain_local_pages();
  717. copy_data_pages();
  718. /*
  719. * End of critical section. From now on, we can write to memory,
  720. * but we should not touch disk. This specially means we must _not_
  721. * touch swap space! Except we must write out our image of course.
  722. */
  723. printk("swsusp: critical section/: done (%d pages copied)\n", nr_copy_pages );
  724. return 0;
  725. }
  726. /* It is important _NOT_ to umount filesystems at this point. We want
  727. * them synced (in case something goes wrong) but we DO not want to mark
  728. * filesystem clean: it is not. (And it does not matter, if we resume
  729. * correctly, we'll mark system clean, anyway.)
  730. */
  731. int swsusp_write(void)
  732. {
  733. int error;
  734. device_resume();
  735. lock_swapdevices();
  736. error = write_suspend_image();
  737. /* This will unlock ignored swap devices since writing is finished */
  738. lock_swapdevices();
  739. return error;
  740. }
  741. extern asmlinkage int swsusp_arch_suspend(void);
  742. extern asmlinkage int swsusp_arch_resume(void);
  743. asmlinkage int swsusp_save(void)
  744. {
  745. return suspend_prepare_image();
  746. }
  747. int swsusp_suspend(void)
  748. {
  749. int error;
  750. if ((error = arch_prepare_suspend()))
  751. return error;
  752. local_irq_disable();
  753. /* At this point, device_suspend() has been called, but *not*
  754. * device_power_down(). We *must* device_power_down() now.
  755. * Otherwise, drivers for some devices (e.g. interrupt controllers)
  756. * become desynchronized with the actual state of the hardware
  757. * at resume time, and evil weirdness ensues.
  758. */
  759. if ((error = device_power_down(PMSG_FREEZE))) {
  760. local_irq_enable();
  761. return error;
  762. }
  763. if ((error = swsusp_swap_check())) {
  764. printk(KERN_ERR "swsusp: FATAL: cannot find swap device, try "
  765. "swapon -a!\n");
  766. local_irq_enable();
  767. return error;
  768. }
  769. save_processor_state();
  770. if ((error = swsusp_arch_suspend()))
  771. printk("Error %d suspending\n", error);
  772. /* Restore control flow magically appears here */
  773. restore_processor_state();
  774. BUG_ON (nr_copy_pages_check != nr_copy_pages);
  775. restore_highmem();
  776. device_power_up();
  777. local_irq_enable();
  778. return error;
  779. }
  780. int swsusp_resume(void)
  781. {
  782. int error;
  783. local_irq_disable();
  784. if (device_power_down(PMSG_FREEZE))
  785. printk(KERN_ERR "Some devices failed to power down, very bad\n");
  786. /* We'll ignore saved state, but this gets preempt count (etc) right */
  787. save_processor_state();
  788. error = swsusp_arch_resume();
  789. /* Code below is only ever reached in case of failure. Otherwise
  790. * execution continues at place where swsusp_arch_suspend was called
  791. */
  792. BUG_ON(!error);
  793. restore_processor_state();
  794. restore_highmem();
  795. device_power_up();
  796. local_irq_enable();
  797. return error;
  798. }
  799. /**
  800. * On resume, for storing the PBE list and the image,
  801. * we can only use memory pages that do not conflict with the pages
  802. * which had been used before suspend.
  803. *
  804. * We don't know which pages are usable until we allocate them.
  805. *
  806. * Allocated but unusable (ie eaten) memory pages are linked together
  807. * to create a list, so that we can free them easily
  808. *
  809. * We could have used a type other than (void *)
  810. * for this purpose, but ...
  811. */
  812. static void **eaten_memory = NULL;
  813. static inline void eat_page(void *page)
  814. {
  815. void **c;
  816. c = eaten_memory;
  817. eaten_memory = page;
  818. *eaten_memory = c;
  819. }
  820. static unsigned long get_usable_page(unsigned gfp_mask)
  821. {
  822. unsigned long m;
  823. m = get_zeroed_page(gfp_mask);
  824. while (!PageNosaveFree(virt_to_page(m))) {
  825. eat_page((void *)m);
  826. m = get_zeroed_page(gfp_mask);
  827. if (!m)
  828. break;
  829. }
  830. return m;
  831. }
  832. static void free_eaten_memory(void)
  833. {
  834. unsigned long m;
  835. void **c;
  836. int i = 0;
  837. c = eaten_memory;
  838. while (c) {
  839. m = (unsigned long)c;
  840. c = *c;
  841. free_page(m);
  842. i++;
  843. }
  844. eaten_memory = NULL;
  845. pr_debug("swsusp: %d unused pages freed\n", i);
  846. }
  847. /**
  848. * check_pagedir - We ensure here that pages that the PBEs point to
  849. * won't collide with pages where we're going to restore from the loaded
  850. * pages later
  851. */
  852. static int check_pagedir(struct pbe *pblist)
  853. {
  854. struct pbe *p;
  855. /* This is necessary, so that we can free allocated pages
  856. * in case of failure
  857. */
  858. for_each_pbe (p, pblist)
  859. p->address = 0UL;
  860. for_each_pbe (p, pblist) {
  861. p->address = get_usable_page(GFP_ATOMIC);
  862. if (!p->address)
  863. return -ENOMEM;
  864. }
  865. return 0;
  866. }
  867. /**
  868. * swsusp_pagedir_relocate - It is possible, that some memory pages
  869. * occupied by the list of PBEs collide with pages where we're going to
  870. * restore from the loaded pages later. We relocate them here.
  871. */
  872. static struct pbe * swsusp_pagedir_relocate(struct pbe *pblist)
  873. {
  874. struct zone *zone;
  875. unsigned long zone_pfn;
  876. struct pbe *pbpage, *tail, *p;
  877. void *m;
  878. int rel = 0, error = 0;
  879. if (!pblist) /* a sanity check */
  880. return NULL;
  881. pr_debug("swsusp: Relocating pagedir (%lu pages to check)\n",
  882. swsusp_info.pagedir_pages);
  883. /* Set page flags */
  884. for_each_zone (zone) {
  885. for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
  886. SetPageNosaveFree(pfn_to_page(zone_pfn +
  887. zone->zone_start_pfn));
  888. }
  889. /* Clear orig addresses */
  890. for_each_pbe (p, pblist)
  891. ClearPageNosaveFree(virt_to_page(p->orig_address));
  892. tail = pblist + PB_PAGE_SKIP;
  893. /* Relocate colliding pages */
  894. for_each_pb_page (pbpage, pblist) {
  895. if (!PageNosaveFree(virt_to_page((unsigned long)pbpage))) {
  896. m = (void *)get_usable_page(GFP_ATOMIC | __GFP_COLD);
  897. if (!m) {
  898. error = -ENOMEM;
  899. break;
  900. }
  901. memcpy(m, (void *)pbpage, PAGE_SIZE);
  902. if (pbpage == pblist)
  903. pblist = (struct pbe *)m;
  904. else
  905. tail->next = (struct pbe *)m;
  906. eat_page((void *)pbpage);
  907. pbpage = (struct pbe *)m;
  908. /* We have to link the PBEs again */
  909. for (p = pbpage; p < pbpage + PB_PAGE_SKIP; p++)
  910. if (p->next) /* needed to save the end */
  911. p->next = p + 1;
  912. rel++;
  913. }
  914. tail = pbpage + PB_PAGE_SKIP;
  915. }
  916. if (error) {
  917. printk("\nswsusp: Out of memory\n\n");
  918. free_pagedir(pblist);
  919. free_eaten_memory();
  920. pblist = NULL;
  921. }
  922. else
  923. printk("swsusp: Relocated %d pages\n", rel);
  924. return pblist;
  925. }
  926. /*
  927. * Using bio to read from swap.
  928. * This code requires a bit more work than just using buffer heads
  929. * but, it is the recommended way for 2.5/2.6.
  930. * The following are to signal the beginning and end of I/O. Bios
  931. * finish asynchronously, while we want them to happen synchronously.
  932. * A simple atomic_t, and a wait loop take care of this problem.
  933. */
  934. static atomic_t io_done = ATOMIC_INIT(0);
  935. static int end_io(struct bio * bio, unsigned int num, int err)
  936. {
  937. if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
  938. panic("I/O error reading memory image");
  939. atomic_set(&io_done, 0);
  940. return 0;
  941. }
  942. static struct block_device * resume_bdev;
  943. /**
  944. * submit - submit BIO request.
  945. * @rw: READ or WRITE.
  946. * @off physical offset of page.
  947. * @page: page we're reading or writing.
  948. *
  949. * Straight from the textbook - allocate and initialize the bio.
  950. * If we're writing, make sure the page is marked as dirty.
  951. * Then submit it and wait.
  952. */
  953. static int submit(int rw, pgoff_t page_off, void * page)
  954. {
  955. int error = 0;
  956. struct bio * bio;
  957. bio = bio_alloc(GFP_ATOMIC, 1);
  958. if (!bio)
  959. return -ENOMEM;
  960. bio->bi_sector = page_off * (PAGE_SIZE >> 9);
  961. bio_get(bio);
  962. bio->bi_bdev = resume_bdev;
  963. bio->bi_end_io = end_io;
  964. if (bio_add_page(bio, virt_to_page(page), PAGE_SIZE, 0) < PAGE_SIZE) {
  965. printk("swsusp: ERROR: adding page to bio at %ld\n",page_off);
  966. error = -EFAULT;
  967. goto Done;
  968. }
  969. if (rw == WRITE)
  970. bio_set_pages_dirty(bio);
  971. atomic_set(&io_done, 1);
  972. submit_bio(rw | (1 << BIO_RW_SYNC), bio);
  973. while (atomic_read(&io_done))
  974. yield();
  975. Done:
  976. bio_put(bio);
  977. return error;
  978. }
  979. static int bio_read_page(pgoff_t page_off, void * page)
  980. {
  981. return submit(READ, page_off, page);
  982. }
  983. static int bio_write_page(pgoff_t page_off, void * page)
  984. {
  985. return submit(WRITE, page_off, page);
  986. }
  987. /*
  988. * Sanity check if this image makes sense with this kernel/swap context
  989. * I really don't think that it's foolproof but more than nothing..
  990. */
  991. static const char * sanity_check(void)
  992. {
  993. dump_info();
  994. if (swsusp_info.version_code != LINUX_VERSION_CODE)
  995. return "kernel version";
  996. if (swsusp_info.num_physpages != num_physpages)
  997. return "memory size";
  998. if (strcmp(swsusp_info.uts.sysname,system_utsname.sysname))
  999. return "system type";
  1000. if (strcmp(swsusp_info.uts.release,system_utsname.release))
  1001. return "kernel release";
  1002. if (strcmp(swsusp_info.uts.version,system_utsname.version))
  1003. return "version";
  1004. if (strcmp(swsusp_info.uts.machine,system_utsname.machine))
  1005. return "machine";
  1006. #if 0
  1007. if(swsusp_info.cpus != num_online_cpus())
  1008. return "number of cpus";
  1009. #endif
  1010. return NULL;
  1011. }
  1012. static int check_header(void)
  1013. {
  1014. const char * reason = NULL;
  1015. int error;
  1016. if ((error = bio_read_page(swp_offset(swsusp_header.swsusp_info), &swsusp_info)))
  1017. return error;
  1018. /* Is this same machine? */
  1019. if ((reason = sanity_check())) {
  1020. printk(KERN_ERR "swsusp: Resume mismatch: %s\n",reason);
  1021. return -EPERM;
  1022. }
  1023. nr_copy_pages = swsusp_info.image_pages;
  1024. return error;
  1025. }
  1026. static int check_sig(void)
  1027. {
  1028. int error;
  1029. memset(&swsusp_header, 0, sizeof(swsusp_header));
  1030. if ((error = bio_read_page(0, &swsusp_header)))
  1031. return error;
  1032. if (!memcmp(SWSUSP_SIG, swsusp_header.sig, 10)) {
  1033. memcpy(swsusp_header.sig, swsusp_header.orig_sig, 10);
  1034. /*
  1035. * Reset swap signature now.
  1036. */
  1037. error = bio_write_page(0, &swsusp_header);
  1038. } else {
  1039. printk(KERN_ERR "swsusp: Suspend partition has wrong signature?\n");
  1040. return -EINVAL;
  1041. }
  1042. if (!error)
  1043. pr_debug("swsusp: Signature found, resuming\n");
  1044. return error;
  1045. }
  1046. /**
  1047. * data_read - Read image pages from swap.
  1048. *
  1049. * You do not need to check for overlaps, check_pagedir()
  1050. * already did that.
  1051. */
  1052. static int data_read(struct pbe *pblist)
  1053. {
  1054. struct pbe * p;
  1055. int error = 0;
  1056. int i = 0;
  1057. int mod = swsusp_info.image_pages / 100;
  1058. if (!mod)
  1059. mod = 1;
  1060. printk("swsusp: Reading image data (%lu pages): ",
  1061. swsusp_info.image_pages);
  1062. for_each_pbe (p, pblist) {
  1063. if (!(i % mod))
  1064. printk("\b\b\b\b%3d%%", i / mod);
  1065. error = bio_read_page(swp_offset(p->swap_address),
  1066. (void *)p->address);
  1067. if (error)
  1068. return error;
  1069. i++;
  1070. }
  1071. printk("\b\b\b\bdone\n");
  1072. return error;
  1073. }
  1074. /**
  1075. * read_pagedir - Read page backup list pages from swap
  1076. */
  1077. static int read_pagedir(struct pbe *pblist)
  1078. {
  1079. struct pbe *pbpage, *p;
  1080. unsigned i = 0;
  1081. int error;
  1082. if (!pblist)
  1083. return -EFAULT;
  1084. printk("swsusp: Reading pagedir (%lu pages)\n",
  1085. swsusp_info.pagedir_pages);
  1086. for_each_pb_page (pbpage, pblist) {
  1087. unsigned long offset = swp_offset(swsusp_info.pagedir[i++]);
  1088. error = -EFAULT;
  1089. if (offset) {
  1090. p = (pbpage + PB_PAGE_SKIP)->next;
  1091. error = bio_read_page(offset, (void *)pbpage);
  1092. (pbpage + PB_PAGE_SKIP)->next = p;
  1093. }
  1094. if (error)
  1095. break;
  1096. }
  1097. if (error)
  1098. free_page((unsigned long)pblist);
  1099. BUG_ON(i != swsusp_info.pagedir_pages);
  1100. return error;
  1101. }
  1102. static int check_suspend_image(void)
  1103. {
  1104. int error = 0;
  1105. if ((error = check_sig()))
  1106. return error;
  1107. if ((error = check_header()))
  1108. return error;
  1109. return 0;
  1110. }
  1111. static int read_suspend_image(void)
  1112. {
  1113. int error = 0;
  1114. struct pbe *p;
  1115. if (!(p = alloc_pagedir(nr_copy_pages)))
  1116. return -ENOMEM;
  1117. if ((error = read_pagedir(p)))
  1118. return error;
  1119. create_pbe_list(p, nr_copy_pages);
  1120. if (!(pagedir_nosave = swsusp_pagedir_relocate(p)))
  1121. return -ENOMEM;
  1122. /* Allocate memory for the image and read the data from swap */
  1123. error = check_pagedir(pagedir_nosave);
  1124. free_eaten_memory();
  1125. if (!error)
  1126. error = data_read(pagedir_nosave);
  1127. if (error) { /* We fail cleanly */
  1128. for_each_pbe (p, pagedir_nosave)
  1129. if (p->address) {
  1130. free_page(p->address);
  1131. p->address = 0UL;
  1132. }
  1133. free_pagedir(pagedir_nosave);
  1134. }
  1135. return error;
  1136. }
  1137. /**
  1138. * swsusp_check - Check for saved image in swap
  1139. */
  1140. int swsusp_check(void)
  1141. {
  1142. int error;
  1143. resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ);
  1144. if (!IS_ERR(resume_bdev)) {
  1145. set_blocksize(resume_bdev, PAGE_SIZE);
  1146. error = check_suspend_image();
  1147. if (error)
  1148. blkdev_put(resume_bdev);
  1149. } else
  1150. error = PTR_ERR(resume_bdev);
  1151. if (!error)
  1152. pr_debug("swsusp: resume file found\n");
  1153. else
  1154. pr_debug("swsusp: Error %d check for resume file\n", error);
  1155. return error;
  1156. }
  1157. /**
  1158. * swsusp_read - Read saved image from swap.
  1159. */
  1160. int swsusp_read(void)
  1161. {
  1162. int error;
  1163. if (IS_ERR(resume_bdev)) {
  1164. pr_debug("swsusp: block device not initialised\n");
  1165. return PTR_ERR(resume_bdev);
  1166. }
  1167. error = read_suspend_image();
  1168. blkdev_put(resume_bdev);
  1169. if (!error)
  1170. pr_debug("swsusp: Reading resume file was successful\n");
  1171. else
  1172. pr_debug("swsusp: Error %d resuming\n", error);
  1173. return error;
  1174. }
  1175. /**
  1176. * swsusp_close - close swap device.
  1177. */
  1178. void swsusp_close(void)
  1179. {
  1180. if (IS_ERR(resume_bdev)) {
  1181. pr_debug("swsusp: block device not initialised\n");
  1182. return;
  1183. }
  1184. blkdev_put(resume_bdev);
  1185. }