swsusp.c 32 KB

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