snapshot.c 21 KB

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