snapshot.c 21 KB

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