snapshot.c 23 KB

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