swsusp.c 28 KB

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