snapshot.c 22 KB

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