snapshot.c 21 KB

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