snapshot.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. /*
  2. * linux/kernel/power/snapshot.c
  3. *
  4. * This file provide system snapshot/restore functionality.
  5. *
  6. * Copyright (C) 1998-2005 Pavel Machek <pavel@suse.cz>
  7. *
  8. * This file is released under the GPLv2, and is based on swsusp.c.
  9. *
  10. */
  11. #include <linux/version.h>
  12. #include <linux/module.h>
  13. #include <linux/mm.h>
  14. #include <linux/suspend.h>
  15. #include <linux/smp_lock.h>
  16. #include <linux/delay.h>
  17. #include <linux/bitops.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/kernel.h>
  20. #include <linux/pm.h>
  21. #include <linux/device.h>
  22. #include <linux/bootmem.h>
  23. #include <linux/syscalls.h>
  24. #include <linux/console.h>
  25. #include <linux/highmem.h>
  26. #include <asm/uaccess.h>
  27. #include <asm/mmu_context.h>
  28. #include <asm/pgtable.h>
  29. #include <asm/tlbflush.h>
  30. #include <asm/io.h>
  31. #include "power.h"
  32. struct pbe *pagedir_nosave;
  33. static unsigned int nr_copy_pages;
  34. static unsigned int nr_meta_pages;
  35. static unsigned long *buffer;
  36. #ifdef CONFIG_HIGHMEM
  37. unsigned int count_highmem_pages(void)
  38. {
  39. struct zone *zone;
  40. unsigned long zone_pfn;
  41. unsigned int n = 0;
  42. for_each_zone (zone)
  43. if (is_highmem(zone)) {
  44. mark_free_pages(zone);
  45. for (zone_pfn = 0; zone_pfn < zone->spanned_pages; zone_pfn++) {
  46. struct page *page;
  47. unsigned long pfn = zone_pfn + zone->zone_start_pfn;
  48. if (!pfn_valid(pfn))
  49. continue;
  50. page = pfn_to_page(pfn);
  51. if (PageReserved(page))
  52. continue;
  53. if (PageNosaveFree(page))
  54. continue;
  55. n++;
  56. }
  57. }
  58. return n;
  59. }
  60. struct highmem_page {
  61. char *data;
  62. struct page *page;
  63. struct highmem_page *next;
  64. };
  65. static struct highmem_page *highmem_copy;
  66. static int save_highmem_zone(struct zone *zone)
  67. {
  68. unsigned long zone_pfn;
  69. mark_free_pages(zone);
  70. for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
  71. struct page *page;
  72. struct highmem_page *save;
  73. void *kaddr;
  74. unsigned long pfn = zone_pfn + zone->zone_start_pfn;
  75. if (!(pfn%10000))
  76. printk(".");
  77. if (!pfn_valid(pfn))
  78. continue;
  79. page = pfn_to_page(pfn);
  80. /*
  81. * This condition results from rvmalloc() sans vmalloc_32()
  82. * and architectural memory reservations. This should be
  83. * corrected eventually when the cases giving rise to this
  84. * are better understood.
  85. */
  86. if (PageReserved(page))
  87. continue;
  88. BUG_ON(PageNosave(page));
  89. if (PageNosaveFree(page))
  90. continue;
  91. save = kmalloc(sizeof(struct highmem_page), GFP_ATOMIC);
  92. if (!save)
  93. return -ENOMEM;
  94. save->next = highmem_copy;
  95. save->page = page;
  96. save->data = (void *) get_zeroed_page(GFP_ATOMIC);
  97. if (!save->data) {
  98. kfree(save);
  99. return -ENOMEM;
  100. }
  101. kaddr = kmap_atomic(page, KM_USER0);
  102. memcpy(save->data, kaddr, PAGE_SIZE);
  103. kunmap_atomic(kaddr, KM_USER0);
  104. highmem_copy = save;
  105. }
  106. return 0;
  107. }
  108. int save_highmem(void)
  109. {
  110. struct zone *zone;
  111. int res = 0;
  112. pr_debug("swsusp: Saving Highmem");
  113. drain_local_pages();
  114. for_each_zone (zone) {
  115. if (is_highmem(zone))
  116. res = save_highmem_zone(zone);
  117. if (res)
  118. return res;
  119. }
  120. printk("\n");
  121. return 0;
  122. }
  123. int restore_highmem(void)
  124. {
  125. printk("swsusp: Restoring Highmem\n");
  126. while (highmem_copy) {
  127. struct highmem_page *save = highmem_copy;
  128. void *kaddr;
  129. highmem_copy = save->next;
  130. kaddr = kmap_atomic(save->page, KM_USER0);
  131. memcpy(kaddr, save->data, PAGE_SIZE);
  132. kunmap_atomic(kaddr, KM_USER0);
  133. free_page((long) save->data);
  134. kfree(save);
  135. }
  136. return 0;
  137. }
  138. #endif
  139. static int pfn_is_nosave(unsigned long pfn)
  140. {
  141. unsigned long nosave_begin_pfn = __pa(&__nosave_begin) >> PAGE_SHIFT;
  142. unsigned long nosave_end_pfn = PAGE_ALIGN(__pa(&__nosave_end)) >> PAGE_SHIFT;
  143. return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn);
  144. }
  145. /**
  146. * saveable - Determine whether a page should be cloned or not.
  147. * @pfn: The page
  148. *
  149. * We save a page if it's Reserved, and not in the range of pages
  150. * statically defined as 'unsaveable', or if it isn't reserved, and
  151. * isn't part of a free chunk of pages.
  152. */
  153. static int saveable(struct zone *zone, unsigned long *zone_pfn)
  154. {
  155. unsigned long pfn = *zone_pfn + zone->zone_start_pfn;
  156. struct page *page;
  157. if (!pfn_valid(pfn))
  158. return 0;
  159. page = pfn_to_page(pfn);
  160. BUG_ON(PageReserved(page) && PageNosave(page));
  161. if (PageNosave(page))
  162. return 0;
  163. if (PageReserved(page) && pfn_is_nosave(pfn))
  164. return 0;
  165. if (PageNosaveFree(page))
  166. return 0;
  167. return 1;
  168. }
  169. unsigned int count_data_pages(void)
  170. {
  171. struct zone *zone;
  172. unsigned long zone_pfn;
  173. unsigned int n = 0;
  174. for_each_zone (zone) {
  175. if (is_highmem(zone))
  176. continue;
  177. mark_free_pages(zone);
  178. for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
  179. n += saveable(zone, &zone_pfn);
  180. }
  181. return n;
  182. }
  183. static void copy_data_pages(struct pbe *pblist)
  184. {
  185. struct zone *zone;
  186. unsigned long zone_pfn;
  187. struct pbe *pbe, *p;
  188. pbe = pblist;
  189. for_each_zone (zone) {
  190. if (is_highmem(zone))
  191. continue;
  192. mark_free_pages(zone);
  193. /* This is necessary for swsusp_free() */
  194. for_each_pb_page (p, pblist)
  195. SetPageNosaveFree(virt_to_page(p));
  196. for_each_pbe (p, pblist)
  197. SetPageNosaveFree(virt_to_page(p->address));
  198. for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
  199. if (saveable(zone, &zone_pfn)) {
  200. struct page *page;
  201. page = pfn_to_page(zone_pfn + zone->zone_start_pfn);
  202. BUG_ON(!pbe);
  203. pbe->orig_address = (unsigned long)page_address(page);
  204. /* copy_page is not usable for copying task structs. */
  205. memcpy((void *)pbe->address, (void *)pbe->orig_address, PAGE_SIZE);
  206. pbe = pbe->next;
  207. }
  208. }
  209. }
  210. BUG_ON(pbe);
  211. }
  212. /**
  213. * free_pagedir - free pages allocated with alloc_pagedir()
  214. */
  215. static void free_pagedir(struct pbe *pblist, int clear_nosave_free)
  216. {
  217. struct pbe *pbe;
  218. while (pblist) {
  219. pbe = (pblist + PB_PAGE_SKIP)->next;
  220. ClearPageNosave(virt_to_page(pblist));
  221. if (clear_nosave_free)
  222. ClearPageNosaveFree(virt_to_page(pblist));
  223. free_page((unsigned long)pblist);
  224. pblist = pbe;
  225. }
  226. }
  227. /**
  228. * fill_pb_page - Create a list of PBEs on a given memory page
  229. */
  230. static inline void fill_pb_page(struct pbe *pbpage)
  231. {
  232. struct pbe *p;
  233. p = pbpage;
  234. pbpage += PB_PAGE_SKIP;
  235. do
  236. p->next = p + 1;
  237. while (++p < pbpage);
  238. }
  239. /**
  240. * create_pbe_list - Create a list of PBEs on top of a given chain
  241. * of memory pages allocated with alloc_pagedir()
  242. */
  243. static inline void create_pbe_list(struct pbe *pblist, unsigned int nr_pages)
  244. {
  245. struct pbe *pbpage, *p;
  246. unsigned int num = PBES_PER_PAGE;
  247. for_each_pb_page (pbpage, pblist) {
  248. if (num >= nr_pages)
  249. break;
  250. fill_pb_page(pbpage);
  251. num += PBES_PER_PAGE;
  252. }
  253. if (pbpage) {
  254. for (num -= PBES_PER_PAGE - 1, p = pbpage; num < nr_pages; p++, num++)
  255. p->next = p + 1;
  256. p->next = NULL;
  257. }
  258. }
  259. /**
  260. * On resume it is necessary to trace and eventually free the unsafe
  261. * pages that have been allocated, because they are needed for I/O
  262. * (on x86-64 we likely will "eat" these pages once again while
  263. * creating the temporary page translation tables)
  264. */
  265. struct eaten_page {
  266. struct eaten_page *next;
  267. char padding[PAGE_SIZE - sizeof(void *)];
  268. };
  269. static struct eaten_page *eaten_pages = NULL;
  270. static void release_eaten_pages(void)
  271. {
  272. struct eaten_page *p, *q;
  273. p = eaten_pages;
  274. while (p) {
  275. q = p->next;
  276. /* We don't want swsusp_free() to free this page again */
  277. ClearPageNosave(virt_to_page(p));
  278. free_page((unsigned long)p);
  279. p = q;
  280. }
  281. eaten_pages = NULL;
  282. }
  283. /**
  284. * @safe_needed - on resume, for storing the PBE list and the image,
  285. * we can only use memory pages that do not conflict with the pages
  286. * which had been used before suspend.
  287. *
  288. * The unsafe pages are marked with the PG_nosave_free flag
  289. *
  290. * Allocated but unusable (ie eaten) memory pages should be marked
  291. * so that swsusp_free() can release them
  292. */
  293. static inline void *alloc_image_page(gfp_t gfp_mask, int safe_needed)
  294. {
  295. void *res;
  296. if (safe_needed)
  297. do {
  298. res = (void *)get_zeroed_page(gfp_mask);
  299. if (res && PageNosaveFree(virt_to_page(res))) {
  300. /* This is for swsusp_free() */
  301. SetPageNosave(virt_to_page(res));
  302. ((struct eaten_page *)res)->next = eaten_pages;
  303. eaten_pages = res;
  304. }
  305. } while (res && PageNosaveFree(virt_to_page(res)));
  306. else
  307. res = (void *)get_zeroed_page(gfp_mask);
  308. if (res) {
  309. SetPageNosave(virt_to_page(res));
  310. SetPageNosaveFree(virt_to_page(res));
  311. }
  312. return res;
  313. }
  314. unsigned long get_safe_page(gfp_t gfp_mask)
  315. {
  316. return (unsigned long)alloc_image_page(gfp_mask, 1);
  317. }
  318. /**
  319. * alloc_pagedir - Allocate the page directory.
  320. *
  321. * First, determine exactly how many pages we need and
  322. * allocate them.
  323. *
  324. * We arrange the pages in a chain: each page is an array of PBES_PER_PAGE
  325. * struct pbe elements (pbes) and the last element in the page points
  326. * to the next page.
  327. *
  328. * On each page we set up a list of struct_pbe elements.
  329. */
  330. struct pbe *alloc_pagedir(unsigned int nr_pages, gfp_t gfp_mask, int safe_needed)
  331. {
  332. unsigned int num;
  333. struct pbe *pblist, *pbe;
  334. if (!nr_pages)
  335. return NULL;
  336. pblist = alloc_image_page(gfp_mask, safe_needed);
  337. /* FIXME: rewrite this ugly loop */
  338. for (pbe = pblist, num = PBES_PER_PAGE; pbe && num < nr_pages;
  339. pbe = pbe->next, num += PBES_PER_PAGE) {
  340. pbe += PB_PAGE_SKIP;
  341. pbe->next = alloc_image_page(gfp_mask, safe_needed);
  342. }
  343. if (!pbe) { /* get_zeroed_page() failed */
  344. free_pagedir(pblist, 1);
  345. pblist = NULL;
  346. } else
  347. create_pbe_list(pblist, nr_pages);
  348. return pblist;
  349. }
  350. /**
  351. * Free pages we allocated for suspend. Suspend pages are alocated
  352. * before atomic copy, so we need to free them after resume.
  353. */
  354. void swsusp_free(void)
  355. {
  356. struct zone *zone;
  357. unsigned long zone_pfn;
  358. for_each_zone(zone) {
  359. for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
  360. if (pfn_valid(zone_pfn + zone->zone_start_pfn)) {
  361. struct page *page;
  362. page = pfn_to_page(zone_pfn + zone->zone_start_pfn);
  363. if (PageNosave(page) && PageNosaveFree(page)) {
  364. ClearPageNosave(page);
  365. ClearPageNosaveFree(page);
  366. free_page((long) page_address(page));
  367. }
  368. }
  369. }
  370. nr_copy_pages = 0;
  371. nr_meta_pages = 0;
  372. pagedir_nosave = NULL;
  373. buffer = NULL;
  374. }
  375. /**
  376. * enough_free_mem - Make sure we enough free memory to snapshot.
  377. *
  378. * Returns TRUE or FALSE after checking the number of available
  379. * free pages.
  380. */
  381. static int enough_free_mem(unsigned int nr_pages)
  382. {
  383. struct zone *zone;
  384. unsigned int n = 0;
  385. for_each_zone (zone)
  386. if (!is_highmem(zone))
  387. n += zone->free_pages;
  388. pr_debug("swsusp: available memory: %u pages\n", n);
  389. return n > (nr_pages + PAGES_FOR_IO +
  390. (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
  391. }
  392. static int alloc_data_pages(struct pbe *pblist, gfp_t gfp_mask, int safe_needed)
  393. {
  394. struct pbe *p;
  395. for_each_pbe (p, pblist) {
  396. p->address = (unsigned long)alloc_image_page(gfp_mask, safe_needed);
  397. if (!p->address)
  398. return -ENOMEM;
  399. }
  400. return 0;
  401. }
  402. static struct pbe *swsusp_alloc(unsigned int nr_pages)
  403. {
  404. struct pbe *pblist;
  405. if (!(pblist = alloc_pagedir(nr_pages, GFP_ATOMIC | __GFP_COLD, 0))) {
  406. printk(KERN_ERR "suspend: Allocating pagedir failed.\n");
  407. return NULL;
  408. }
  409. if (alloc_data_pages(pblist, GFP_ATOMIC | __GFP_COLD, 0)) {
  410. printk(KERN_ERR "suspend: Allocating image pages failed.\n");
  411. swsusp_free();
  412. return NULL;
  413. }
  414. return pblist;
  415. }
  416. asmlinkage int swsusp_save(void)
  417. {
  418. unsigned int nr_pages;
  419. pr_debug("swsusp: critical section: \n");
  420. drain_local_pages();
  421. nr_pages = count_data_pages();
  422. printk("swsusp: Need to copy %u pages\n", nr_pages);
  423. pr_debug("swsusp: pages needed: %u + %lu + %u, free: %u\n",
  424. nr_pages,
  425. (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE,
  426. PAGES_FOR_IO, nr_free_pages());
  427. if (!enough_free_mem(nr_pages)) {
  428. printk(KERN_ERR "swsusp: Not enough free memory\n");
  429. return -ENOMEM;
  430. }
  431. pagedir_nosave = swsusp_alloc(nr_pages);
  432. if (!pagedir_nosave)
  433. return -ENOMEM;
  434. /* During allocating of suspend pagedir, new cold pages may appear.
  435. * Kill them.
  436. */
  437. drain_local_pages();
  438. copy_data_pages(pagedir_nosave);
  439. /*
  440. * End of critical section. From now on, we can write to memory,
  441. * but we should not touch disk. This specially means we must _not_
  442. * touch swap space! Except we must write out our image of course.
  443. */
  444. nr_copy_pages = nr_pages;
  445. nr_meta_pages = (nr_pages * sizeof(long) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  446. printk("swsusp: critical section/: done (%d pages copied)\n", nr_pages);
  447. return 0;
  448. }
  449. static void init_header(struct swsusp_info *info)
  450. {
  451. memset(info, 0, sizeof(struct swsusp_info));
  452. info->version_code = LINUX_VERSION_CODE;
  453. info->num_physpages = num_physpages;
  454. memcpy(&info->uts, &system_utsname, sizeof(system_utsname));
  455. info->cpus = num_online_cpus();
  456. info->image_pages = nr_copy_pages;
  457. info->pages = nr_copy_pages + nr_meta_pages + 1;
  458. info->size = info->pages;
  459. info->size <<= PAGE_SHIFT;
  460. }
  461. /**
  462. * pack_orig_addresses - the .orig_address fields of the PBEs from the
  463. * list starting at @pbe are stored in the array @buf[] (1 page)
  464. */
  465. static inline struct pbe *pack_orig_addresses(unsigned long *buf, struct pbe *pbe)
  466. {
  467. int j;
  468. for (j = 0; j < PAGE_SIZE / sizeof(long) && pbe; j++) {
  469. buf[j] = pbe->orig_address;
  470. pbe = pbe->next;
  471. }
  472. if (!pbe)
  473. for (; j < PAGE_SIZE / sizeof(long); j++)
  474. buf[j] = 0;
  475. return pbe;
  476. }
  477. /**
  478. * snapshot_read_next - used for reading the system memory snapshot.
  479. *
  480. * On the first call to it @handle should point to a zeroed
  481. * snapshot_handle structure. The structure gets updated and a pointer
  482. * to it should be passed to this function every next time.
  483. *
  484. * The @count parameter should contain the number of bytes the caller
  485. * wants to read from the snapshot. It must not be zero.
  486. *
  487. * On success the function returns a positive number. Then, the caller
  488. * is allowed to read up to the returned number of bytes from the memory
  489. * location computed by the data_of() macro. The number returned
  490. * may be smaller than @count, but this only happens if the read would
  491. * cross a page boundary otherwise.
  492. *
  493. * The function returns 0 to indicate the end of data stream condition,
  494. * and a negative number is returned on error. In such cases the
  495. * structure pointed to by @handle is not updated and should not be used
  496. * any more.
  497. */
  498. int snapshot_read_next(struct snapshot_handle *handle, size_t count)
  499. {
  500. if (handle->page > nr_meta_pages + nr_copy_pages)
  501. return 0;
  502. if (!buffer) {
  503. /* This makes the buffer be freed by swsusp_free() */
  504. buffer = alloc_image_page(GFP_ATOMIC, 0);
  505. if (!buffer)
  506. return -ENOMEM;
  507. }
  508. if (!handle->offset) {
  509. init_header((struct swsusp_info *)buffer);
  510. handle->buffer = buffer;
  511. handle->pbe = pagedir_nosave;
  512. }
  513. if (handle->prev < handle->page) {
  514. if (handle->page <= nr_meta_pages) {
  515. handle->pbe = pack_orig_addresses(buffer, handle->pbe);
  516. if (!handle->pbe)
  517. handle->pbe = pagedir_nosave;
  518. } else {
  519. handle->buffer = (void *)handle->pbe->address;
  520. handle->pbe = handle->pbe->next;
  521. }
  522. handle->prev = handle->page;
  523. }
  524. handle->buf_offset = handle->page_offset;
  525. if (handle->page_offset + count >= PAGE_SIZE) {
  526. count = PAGE_SIZE - handle->page_offset;
  527. handle->page_offset = 0;
  528. handle->page++;
  529. } else {
  530. handle->page_offset += count;
  531. }
  532. handle->offset += count;
  533. return count;
  534. }
  535. /**
  536. * mark_unsafe_pages - mark the pages that cannot be used for storing
  537. * the image during resume, because they conflict with the pages that
  538. * had been used before suspend
  539. */
  540. static int mark_unsafe_pages(struct pbe *pblist)
  541. {
  542. struct zone *zone;
  543. unsigned long zone_pfn;
  544. struct pbe *p;
  545. if (!pblist) /* a sanity check */
  546. return -EINVAL;
  547. /* Clear page flags */
  548. for_each_zone (zone) {
  549. for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
  550. if (pfn_valid(zone_pfn + zone->zone_start_pfn))
  551. ClearPageNosaveFree(pfn_to_page(zone_pfn +
  552. zone->zone_start_pfn));
  553. }
  554. /* Mark orig addresses */
  555. for_each_pbe (p, pblist) {
  556. if (virt_addr_valid(p->orig_address))
  557. SetPageNosaveFree(virt_to_page(p->orig_address));
  558. else
  559. return -EFAULT;
  560. }
  561. return 0;
  562. }
  563. static void copy_page_backup_list(struct pbe *dst, struct pbe *src)
  564. {
  565. /* We assume both lists contain the same number of elements */
  566. while (src) {
  567. dst->orig_address = src->orig_address;
  568. dst = dst->next;
  569. src = src->next;
  570. }
  571. }
  572. static int check_header(struct swsusp_info *info)
  573. {
  574. char *reason = NULL;
  575. if (info->version_code != LINUX_VERSION_CODE)
  576. reason = "kernel version";
  577. if (info->num_physpages != num_physpages)
  578. reason = "memory size";
  579. if (strcmp(info->uts.sysname,system_utsname.sysname))
  580. reason = "system type";
  581. if (strcmp(info->uts.release,system_utsname.release))
  582. reason = "kernel release";
  583. if (strcmp(info->uts.version,system_utsname.version))
  584. reason = "version";
  585. if (strcmp(info->uts.machine,system_utsname.machine))
  586. reason = "machine";
  587. if (reason) {
  588. printk(KERN_ERR "swsusp: Resume mismatch: %s\n", reason);
  589. return -EPERM;
  590. }
  591. return 0;
  592. }
  593. /**
  594. * load header - check the image header and copy data from it
  595. */
  596. static int load_header(struct snapshot_handle *handle,
  597. struct swsusp_info *info)
  598. {
  599. int error;
  600. struct pbe *pblist;
  601. error = check_header(info);
  602. if (!error) {
  603. pblist = alloc_pagedir(info->image_pages, GFP_ATOMIC, 0);
  604. if (!pblist)
  605. return -ENOMEM;
  606. pagedir_nosave = pblist;
  607. handle->pbe = pblist;
  608. nr_copy_pages = info->image_pages;
  609. nr_meta_pages = info->pages - info->image_pages - 1;
  610. }
  611. return error;
  612. }
  613. /**
  614. * unpack_orig_addresses - copy the elements of @buf[] (1 page) to
  615. * the PBEs in the list starting at @pbe
  616. */
  617. static inline struct pbe *unpack_orig_addresses(unsigned long *buf,
  618. struct pbe *pbe)
  619. {
  620. int j;
  621. for (j = 0; j < PAGE_SIZE / sizeof(long) && pbe; j++) {
  622. pbe->orig_address = buf[j];
  623. pbe = pbe->next;
  624. }
  625. return pbe;
  626. }
  627. /**
  628. * create_image - use metadata contained in the PBE list
  629. * pointed to by pagedir_nosave to mark the pages that will
  630. * be overwritten in the process of restoring the system
  631. * memory state from the image and allocate memory for
  632. * the image avoiding these pages
  633. */
  634. static int create_image(struct snapshot_handle *handle)
  635. {
  636. int error = 0;
  637. struct pbe *p, *pblist;
  638. p = pagedir_nosave;
  639. error = mark_unsafe_pages(p);
  640. if (!error) {
  641. pblist = alloc_pagedir(nr_copy_pages, GFP_ATOMIC, 1);
  642. if (pblist)
  643. copy_page_backup_list(pblist, p);
  644. free_pagedir(p, 0);
  645. if (!pblist)
  646. error = -ENOMEM;
  647. }
  648. if (!error)
  649. error = alloc_data_pages(pblist, GFP_ATOMIC, 1);
  650. if (!error) {
  651. release_eaten_pages();
  652. pagedir_nosave = pblist;
  653. } else {
  654. pagedir_nosave = NULL;
  655. handle->pbe = NULL;
  656. nr_copy_pages = 0;
  657. nr_meta_pages = 0;
  658. }
  659. return error;
  660. }
  661. /**
  662. * snapshot_write_next - used for writing the system memory snapshot.
  663. *
  664. * On the first call to it @handle should point to a zeroed
  665. * snapshot_handle structure. The structure gets updated and a pointer
  666. * to it should be passed to this function every next time.
  667. *
  668. * The @count parameter should contain the number of bytes the caller
  669. * wants to write to the image. It must not be zero.
  670. *
  671. * On success the function returns a positive number. Then, the caller
  672. * is allowed to write up to the returned number of bytes to the memory
  673. * location computed by the data_of() macro. The number returned
  674. * may be smaller than @count, but this only happens if the write would
  675. * cross a page boundary otherwise.
  676. *
  677. * The function returns 0 to indicate the "end of file" condition,
  678. * and a negative number is returned on error. In such cases the
  679. * structure pointed to by @handle is not updated and should not be used
  680. * any more.
  681. */
  682. int snapshot_write_next(struct snapshot_handle *handle, size_t count)
  683. {
  684. int error = 0;
  685. if (handle->prev && handle->page > nr_meta_pages + nr_copy_pages)
  686. return 0;
  687. if (!buffer) {
  688. /* This makes the buffer be freed by swsusp_free() */
  689. buffer = alloc_image_page(GFP_ATOMIC, 0);
  690. if (!buffer)
  691. return -ENOMEM;
  692. }
  693. if (!handle->offset)
  694. handle->buffer = buffer;
  695. if (handle->prev < handle->page) {
  696. if (!handle->prev) {
  697. error = load_header(handle, (struct swsusp_info *)buffer);
  698. if (error)
  699. return error;
  700. } else if (handle->prev <= nr_meta_pages) {
  701. handle->pbe = unpack_orig_addresses(buffer, handle->pbe);
  702. if (!handle->pbe) {
  703. error = create_image(handle);
  704. if (error)
  705. return error;
  706. handle->pbe = pagedir_nosave;
  707. handle->buffer = (void *)handle->pbe->address;
  708. }
  709. } else {
  710. handle->pbe = handle->pbe->next;
  711. handle->buffer = (void *)handle->pbe->address;
  712. }
  713. handle->prev = handle->page;
  714. }
  715. handle->buf_offset = handle->page_offset;
  716. if (handle->page_offset + count >= PAGE_SIZE) {
  717. count = PAGE_SIZE - handle->page_offset;
  718. handle->page_offset = 0;
  719. handle->page++;
  720. } else {
  721. handle->page_offset += count;
  722. }
  723. handle->offset += count;
  724. return count;
  725. }
  726. int snapshot_image_loaded(struct snapshot_handle *handle)
  727. {
  728. return !(!handle->pbe || handle->pbe->next || !nr_copy_pages ||
  729. handle->page <= nr_meta_pages + nr_copy_pages);
  730. }