snapshot.c 22 KB

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