snapshot.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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/module.h>
  12. #include <linux/mm.h>
  13. #include <linux/suspend.h>
  14. #include <linux/smp_lock.h>
  15. #include <linux/delay.h>
  16. #include <linux/bitops.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/kernel.h>
  19. #include <linux/pm.h>
  20. #include <linux/device.h>
  21. #include <linux/bootmem.h>
  22. #include <linux/syscalls.h>
  23. #include <linux/console.h>
  24. #include <linux/highmem.h>
  25. #include <asm/uaccess.h>
  26. #include <asm/mmu_context.h>
  27. #include <asm/pgtable.h>
  28. #include <asm/tlbflush.h>
  29. #include <asm/io.h>
  30. #include "power.h"
  31. struct pbe *pagedir_nosave;
  32. unsigned int nr_copy_pages;
  33. #ifdef CONFIG_HIGHMEM
  34. unsigned int count_highmem_pages(void)
  35. {
  36. struct zone *zone;
  37. unsigned long zone_pfn;
  38. unsigned int n = 0;
  39. for_each_zone (zone)
  40. if (is_highmem(zone)) {
  41. mark_free_pages(zone);
  42. for (zone_pfn = 0; zone_pfn < zone->spanned_pages; zone_pfn++) {
  43. struct page *page;
  44. unsigned long pfn = zone_pfn + zone->zone_start_pfn;
  45. if (!pfn_valid(pfn))
  46. continue;
  47. page = pfn_to_page(pfn);
  48. if (PageReserved(page))
  49. continue;
  50. if (PageNosaveFree(page))
  51. continue;
  52. n++;
  53. }
  54. }
  55. return n;
  56. }
  57. struct highmem_page {
  58. char *data;
  59. struct page *page;
  60. struct highmem_page *next;
  61. };
  62. static struct highmem_page *highmem_copy;
  63. static int save_highmem_zone(struct zone *zone)
  64. {
  65. unsigned long zone_pfn;
  66. mark_free_pages(zone);
  67. for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
  68. struct page *page;
  69. struct highmem_page *save;
  70. void *kaddr;
  71. unsigned long pfn = zone_pfn + zone->zone_start_pfn;
  72. if (!(pfn%1000))
  73. printk(".");
  74. if (!pfn_valid(pfn))
  75. continue;
  76. page = pfn_to_page(pfn);
  77. /*
  78. * This condition results from rvmalloc() sans vmalloc_32()
  79. * and architectural memory reservations. This should be
  80. * corrected eventually when the cases giving rise to this
  81. * are better understood.
  82. */
  83. if (PageReserved(page))
  84. continue;
  85. BUG_ON(PageNosave(page));
  86. if (PageNosaveFree(page))
  87. continue;
  88. save = kmalloc(sizeof(struct highmem_page), GFP_ATOMIC);
  89. if (!save)
  90. return -ENOMEM;
  91. save->next = highmem_copy;
  92. save->page = page;
  93. save->data = (void *) get_zeroed_page(GFP_ATOMIC);
  94. if (!save->data) {
  95. kfree(save);
  96. return -ENOMEM;
  97. }
  98. kaddr = kmap_atomic(page, KM_USER0);
  99. memcpy(save->data, kaddr, PAGE_SIZE);
  100. kunmap_atomic(kaddr, KM_USER0);
  101. highmem_copy = save;
  102. }
  103. return 0;
  104. }
  105. int save_highmem(void)
  106. {
  107. struct zone *zone;
  108. int res = 0;
  109. pr_debug("swsusp: Saving Highmem\n");
  110. for_each_zone (zone) {
  111. if (is_highmem(zone))
  112. res = save_highmem_zone(zone);
  113. if (res)
  114. return res;
  115. }
  116. return 0;
  117. }
  118. int restore_highmem(void)
  119. {
  120. printk("swsusp: Restoring Highmem\n");
  121. while (highmem_copy) {
  122. struct highmem_page *save = highmem_copy;
  123. void *kaddr;
  124. highmem_copy = save->next;
  125. kaddr = kmap_atomic(save->page, KM_USER0);
  126. memcpy(kaddr, save->data, PAGE_SIZE);
  127. kunmap_atomic(kaddr, KM_USER0);
  128. free_page((long) save->data);
  129. kfree(save);
  130. }
  131. return 0;
  132. }
  133. #endif
  134. static int pfn_is_nosave(unsigned long pfn)
  135. {
  136. unsigned long nosave_begin_pfn = __pa(&__nosave_begin) >> PAGE_SHIFT;
  137. unsigned long nosave_end_pfn = PAGE_ALIGN(__pa(&__nosave_end)) >> PAGE_SHIFT;
  138. return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn);
  139. }
  140. /**
  141. * saveable - Determine whether a page should be cloned or not.
  142. * @pfn: The page
  143. *
  144. * We save a page if it's Reserved, and not in the range of pages
  145. * statically defined as 'unsaveable', or if it isn't reserved, and
  146. * isn't part of a free chunk of pages.
  147. */
  148. static int saveable(struct zone *zone, unsigned long *zone_pfn)
  149. {
  150. unsigned long pfn = *zone_pfn + zone->zone_start_pfn;
  151. struct page *page;
  152. if (!pfn_valid(pfn))
  153. return 0;
  154. page = pfn_to_page(pfn);
  155. BUG_ON(PageReserved(page) && PageNosave(page));
  156. if (PageNosave(page))
  157. return 0;
  158. if (PageReserved(page) && pfn_is_nosave(pfn))
  159. return 0;
  160. if (PageNosaveFree(page))
  161. return 0;
  162. return 1;
  163. }
  164. unsigned int count_data_pages(void)
  165. {
  166. struct zone *zone;
  167. unsigned long zone_pfn;
  168. unsigned int n = 0;
  169. for_each_zone (zone) {
  170. if (is_highmem(zone))
  171. continue;
  172. mark_free_pages(zone);
  173. for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
  174. n += saveable(zone, &zone_pfn);
  175. }
  176. return n;
  177. }
  178. static void copy_data_pages(struct pbe *pblist)
  179. {
  180. struct zone *zone;
  181. unsigned long zone_pfn;
  182. struct pbe *pbe, *p;
  183. pbe = pblist;
  184. for_each_zone (zone) {
  185. if (is_highmem(zone))
  186. continue;
  187. mark_free_pages(zone);
  188. /* This is necessary for swsusp_free() */
  189. for_each_pb_page (p, pblist)
  190. SetPageNosaveFree(virt_to_page(p));
  191. for_each_pbe (p, pblist)
  192. SetPageNosaveFree(virt_to_page(p->address));
  193. for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
  194. if (saveable(zone, &zone_pfn)) {
  195. struct page *page;
  196. page = pfn_to_page(zone_pfn + zone->zone_start_pfn);
  197. BUG_ON(!pbe);
  198. pbe->orig_address = (unsigned long)page_address(page);
  199. /* copy_page is not usable for copying task structs. */
  200. memcpy((void *)pbe->address, (void *)pbe->orig_address, PAGE_SIZE);
  201. pbe = pbe->next;
  202. }
  203. }
  204. }
  205. BUG_ON(pbe);
  206. }
  207. /**
  208. * free_pagedir - free pages allocated with alloc_pagedir()
  209. */
  210. void free_pagedir(struct pbe *pblist)
  211. {
  212. struct pbe *pbe;
  213. while (pblist) {
  214. pbe = (pblist + PB_PAGE_SKIP)->next;
  215. ClearPageNosave(virt_to_page(pblist));
  216. ClearPageNosaveFree(virt_to_page(pblist));
  217. free_page((unsigned long)pblist);
  218. pblist = pbe;
  219. }
  220. }
  221. /**
  222. * fill_pb_page - Create a list of PBEs on a given memory page
  223. */
  224. static inline void fill_pb_page(struct pbe *pbpage)
  225. {
  226. struct pbe *p;
  227. p = pbpage;
  228. pbpage += PB_PAGE_SKIP;
  229. do
  230. p->next = p + 1;
  231. while (++p < pbpage);
  232. }
  233. /**
  234. * create_pbe_list - Create a list of PBEs on top of a given chain
  235. * of memory pages allocated with alloc_pagedir()
  236. */
  237. static inline void create_pbe_list(struct pbe *pblist, unsigned int nr_pages)
  238. {
  239. struct pbe *pbpage, *p;
  240. unsigned int num = PBES_PER_PAGE;
  241. for_each_pb_page (pbpage, pblist) {
  242. if (num >= nr_pages)
  243. break;
  244. fill_pb_page(pbpage);
  245. num += PBES_PER_PAGE;
  246. }
  247. if (pbpage) {
  248. for (num -= PBES_PER_PAGE - 1, p = pbpage; num < nr_pages; p++, num++)
  249. p->next = p + 1;
  250. p->next = NULL;
  251. }
  252. }
  253. /**
  254. * On resume it is necessary to trace and eventually free the unsafe
  255. * pages that have been allocated, because they are needed for I/O
  256. * (on x86-64 we likely will "eat" these pages once again while
  257. * creating the temporary page translation tables)
  258. */
  259. struct eaten_page {
  260. struct eaten_page *next;
  261. char padding[PAGE_SIZE - sizeof(void *)];
  262. };
  263. static struct eaten_page *eaten_pages = NULL;
  264. void release_eaten_pages(void)
  265. {
  266. struct eaten_page *p, *q;
  267. p = eaten_pages;
  268. while (p) {
  269. q = p->next;
  270. /* We don't want swsusp_free() to free this page again */
  271. ClearPageNosave(virt_to_page(p));
  272. free_page((unsigned long)p);
  273. p = q;
  274. }
  275. eaten_pages = NULL;
  276. }
  277. /**
  278. * @safe_needed - on resume, for storing the PBE list and the image,
  279. * we can only use memory pages that do not conflict with the pages
  280. * which had been used before suspend.
  281. *
  282. * The unsafe pages are marked with the PG_nosave_free flag
  283. *
  284. * Allocated but unusable (ie eaten) memory pages should be marked
  285. * so that swsusp_free() can release them
  286. */
  287. static inline void *alloc_image_page(gfp_t gfp_mask, int safe_needed)
  288. {
  289. void *res;
  290. if (safe_needed)
  291. do {
  292. res = (void *)get_zeroed_page(gfp_mask);
  293. if (res && PageNosaveFree(virt_to_page(res))) {
  294. /* This is for swsusp_free() */
  295. SetPageNosave(virt_to_page(res));
  296. ((struct eaten_page *)res)->next = eaten_pages;
  297. eaten_pages = res;
  298. }
  299. } while (res && PageNosaveFree(virt_to_page(res)));
  300. else
  301. res = (void *)get_zeroed_page(gfp_mask);
  302. if (res) {
  303. SetPageNosave(virt_to_page(res));
  304. SetPageNosaveFree(virt_to_page(res));
  305. }
  306. return res;
  307. }
  308. unsigned long get_safe_page(gfp_t gfp_mask)
  309. {
  310. return (unsigned long)alloc_image_page(gfp_mask, 1);
  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. struct pbe *alloc_pagedir(unsigned int nr_pages, gfp_t gfp_mask, int safe_needed)
  325. {
  326. unsigned int num;
  327. struct pbe *pblist, *pbe;
  328. if (!nr_pages)
  329. return NULL;
  330. pr_debug("alloc_pagedir(): nr_pages = %d\n", nr_pages);
  331. pblist = alloc_image_page(gfp_mask, safe_needed);
  332. /* FIXME: rewrite this ugly loop */
  333. for (pbe = pblist, num = PBES_PER_PAGE; pbe && num < nr_pages;
  334. pbe = pbe->next, num += PBES_PER_PAGE) {
  335. pbe += PB_PAGE_SKIP;
  336. pbe->next = alloc_image_page(gfp_mask, safe_needed);
  337. }
  338. if (!pbe) { /* get_zeroed_page() failed */
  339. free_pagedir(pblist);
  340. pblist = NULL;
  341. } else
  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 zone_pfn;
  353. for_each_zone(zone) {
  354. for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
  355. if (pfn_valid(zone_pfn + zone->zone_start_pfn)) {
  356. struct page *page;
  357. page = pfn_to_page(zone_pfn + zone->zone_start_pfn);
  358. if (PageNosave(page) && PageNosaveFree(page)) {
  359. ClearPageNosave(page);
  360. ClearPageNosaveFree(page);
  361. free_page((long) page_address(page));
  362. }
  363. }
  364. }
  365. }
  366. /**
  367. * enough_free_mem - Make sure we enough free memory to snapshot.
  368. *
  369. * Returns TRUE or FALSE after checking the number of available
  370. * free pages.
  371. */
  372. static int enough_free_mem(unsigned int nr_pages)
  373. {
  374. struct zone *zone;
  375. unsigned int n = 0;
  376. for_each_zone (zone)
  377. if (!is_highmem(zone))
  378. n += zone->free_pages;
  379. pr_debug("swsusp: available memory: %u pages\n", n);
  380. return n > (nr_pages + PAGES_FOR_IO +
  381. (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
  382. }
  383. int alloc_data_pages(struct pbe *pblist, gfp_t gfp_mask, int safe_needed)
  384. {
  385. struct pbe *p;
  386. for_each_pbe (p, pblist) {
  387. p->address = (unsigned long)alloc_image_page(gfp_mask, safe_needed);
  388. if (!p->address)
  389. return -ENOMEM;
  390. }
  391. return 0;
  392. }
  393. static struct pbe *swsusp_alloc(unsigned int nr_pages)
  394. {
  395. struct pbe *pblist;
  396. if (!(pblist = alloc_pagedir(nr_pages, GFP_ATOMIC | __GFP_COLD, 0))) {
  397. printk(KERN_ERR "suspend: Allocating pagedir failed.\n");
  398. return NULL;
  399. }
  400. if (alloc_data_pages(pblist, GFP_ATOMIC | __GFP_COLD, 0)) {
  401. printk(KERN_ERR "suspend: Allocating image pages failed.\n");
  402. swsusp_free();
  403. return NULL;
  404. }
  405. return pblist;
  406. }
  407. asmlinkage int swsusp_save(void)
  408. {
  409. unsigned int nr_pages;
  410. pr_debug("swsusp: critical section: \n");
  411. drain_local_pages();
  412. nr_pages = count_data_pages();
  413. printk("swsusp: Need to copy %u pages\n", nr_pages);
  414. pr_debug("swsusp: pages needed: %u + %lu + %u, free: %u\n",
  415. nr_pages,
  416. (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE,
  417. PAGES_FOR_IO, nr_free_pages());
  418. if (!enough_free_mem(nr_pages)) {
  419. printk(KERN_ERR "swsusp: Not enough free memory\n");
  420. return -ENOMEM;
  421. }
  422. pagedir_nosave = swsusp_alloc(nr_pages);
  423. if (!pagedir_nosave)
  424. return -ENOMEM;
  425. /* During allocating of suspend pagedir, new cold pages may appear.
  426. * Kill them.
  427. */
  428. drain_local_pages();
  429. copy_data_pages(pagedir_nosave);
  430. /*
  431. * End of critical section. From now on, we can write to memory,
  432. * but we should not touch disk. This specially means we must _not_
  433. * touch swap space! Except we must write out our image of course.
  434. */
  435. nr_copy_pages = nr_pages;
  436. printk("swsusp: critical section/: done (%d pages copied)\n", nr_pages);
  437. return 0;
  438. }