snapshot.c 20 KB

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