snapshot.c 21 KB

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