snapshot.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. #ifdef CONFIG_HIGHMEM
  32. struct highmem_page {
  33. char *data;
  34. struct page *page;
  35. struct highmem_page *next;
  36. };
  37. static struct highmem_page *highmem_copy;
  38. static int save_highmem_zone(struct zone *zone)
  39. {
  40. unsigned long zone_pfn;
  41. mark_free_pages(zone);
  42. for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
  43. struct page *page;
  44. struct highmem_page *save;
  45. void *kaddr;
  46. unsigned long pfn = zone_pfn + zone->zone_start_pfn;
  47. if (!(pfn%1000))
  48. printk(".");
  49. if (!pfn_valid(pfn))
  50. continue;
  51. page = pfn_to_page(pfn);
  52. /*
  53. * This condition results from rvmalloc() sans vmalloc_32()
  54. * and architectural memory reservations. This should be
  55. * corrected eventually when the cases giving rise to this
  56. * are better understood.
  57. */
  58. if (PageReserved(page)) {
  59. printk("highmem reserved page?!\n");
  60. continue;
  61. }
  62. BUG_ON(PageNosave(page));
  63. if (PageNosaveFree(page))
  64. continue;
  65. save = kmalloc(sizeof(struct highmem_page), GFP_ATOMIC);
  66. if (!save)
  67. return -ENOMEM;
  68. save->next = highmem_copy;
  69. save->page = page;
  70. save->data = (void *) get_zeroed_page(GFP_ATOMIC);
  71. if (!save->data) {
  72. kfree(save);
  73. return -ENOMEM;
  74. }
  75. kaddr = kmap_atomic(page, KM_USER0);
  76. memcpy(save->data, kaddr, PAGE_SIZE);
  77. kunmap_atomic(kaddr, KM_USER0);
  78. highmem_copy = save;
  79. }
  80. return 0;
  81. }
  82. int save_highmem(void)
  83. {
  84. struct zone *zone;
  85. int res = 0;
  86. pr_debug("swsusp: Saving Highmem\n");
  87. for_each_zone (zone) {
  88. if (is_highmem(zone))
  89. res = save_highmem_zone(zone);
  90. if (res)
  91. return res;
  92. }
  93. return 0;
  94. }
  95. int restore_highmem(void)
  96. {
  97. printk("swsusp: Restoring Highmem\n");
  98. while (highmem_copy) {
  99. struct highmem_page *save = highmem_copy;
  100. void *kaddr;
  101. highmem_copy = save->next;
  102. kaddr = kmap_atomic(save->page, KM_USER0);
  103. memcpy(kaddr, save->data, PAGE_SIZE);
  104. kunmap_atomic(kaddr, KM_USER0);
  105. free_page((long) save->data);
  106. kfree(save);
  107. }
  108. return 0;
  109. }
  110. #endif
  111. static int pfn_is_nosave(unsigned long pfn)
  112. {
  113. unsigned long nosave_begin_pfn = __pa(&__nosave_begin) >> PAGE_SHIFT;
  114. unsigned long nosave_end_pfn = PAGE_ALIGN(__pa(&__nosave_end)) >> PAGE_SHIFT;
  115. return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn);
  116. }
  117. /**
  118. * saveable - Determine whether a page should be cloned or not.
  119. * @pfn: The page
  120. *
  121. * We save a page if it's Reserved, and not in the range of pages
  122. * statically defined as 'unsaveable', or if it isn't reserved, and
  123. * isn't part of a free chunk of pages.
  124. */
  125. static int saveable(struct zone *zone, unsigned long *zone_pfn)
  126. {
  127. unsigned long pfn = *zone_pfn + zone->zone_start_pfn;
  128. struct page *page;
  129. if (!pfn_valid(pfn))
  130. return 0;
  131. page = pfn_to_page(pfn);
  132. BUG_ON(PageReserved(page) && PageNosave(page));
  133. if (PageNosave(page))
  134. return 0;
  135. if (PageReserved(page) && pfn_is_nosave(pfn)) {
  136. pr_debug("[nosave pfn 0x%lx]", pfn);
  137. return 0;
  138. }
  139. if (PageNosaveFree(page))
  140. return 0;
  141. return 1;
  142. }
  143. static unsigned count_data_pages(void)
  144. {
  145. struct zone *zone;
  146. unsigned long zone_pfn;
  147. unsigned int n = 0;
  148. for_each_zone (zone) {
  149. if (is_highmem(zone))
  150. continue;
  151. mark_free_pages(zone);
  152. for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
  153. n += saveable(zone, &zone_pfn);
  154. }
  155. return n;
  156. }
  157. static void copy_data_pages(struct pbe *pblist)
  158. {
  159. struct zone *zone;
  160. unsigned long zone_pfn;
  161. struct pbe *pbe, *p;
  162. pbe = pblist;
  163. for_each_zone (zone) {
  164. if (is_highmem(zone))
  165. continue;
  166. mark_free_pages(zone);
  167. /* This is necessary for swsusp_free() */
  168. for_each_pb_page (p, pblist)
  169. SetPageNosaveFree(virt_to_page(p));
  170. for_each_pbe (p, pblist)
  171. SetPageNosaveFree(virt_to_page(p->address));
  172. for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
  173. if (saveable(zone, &zone_pfn)) {
  174. struct page *page;
  175. page = pfn_to_page(zone_pfn + zone->zone_start_pfn);
  176. BUG_ON(!pbe);
  177. pbe->orig_address = (unsigned long)page_address(page);
  178. /* copy_page is not usable for copying task structs. */
  179. memcpy((void *)pbe->address, (void *)pbe->orig_address, PAGE_SIZE);
  180. pbe = pbe->next;
  181. }
  182. }
  183. }
  184. BUG_ON(pbe);
  185. }
  186. /**
  187. * free_pagedir - free pages allocated with alloc_pagedir()
  188. */
  189. void free_pagedir(struct pbe *pblist)
  190. {
  191. struct pbe *pbe;
  192. while (pblist) {
  193. pbe = (pblist + PB_PAGE_SKIP)->next;
  194. ClearPageNosave(virt_to_page(pblist));
  195. ClearPageNosaveFree(virt_to_page(pblist));
  196. free_page((unsigned long)pblist);
  197. pblist = pbe;
  198. }
  199. }
  200. /**
  201. * fill_pb_page - Create a list of PBEs on a given memory page
  202. */
  203. static inline void fill_pb_page(struct pbe *pbpage)
  204. {
  205. struct pbe *p;
  206. p = pbpage;
  207. pbpage += PB_PAGE_SKIP;
  208. do
  209. p->next = p + 1;
  210. while (++p < pbpage);
  211. }
  212. /**
  213. * create_pbe_list - Create a list of PBEs on top of a given chain
  214. * of memory pages allocated with alloc_pagedir()
  215. */
  216. void create_pbe_list(struct pbe *pblist, unsigned int nr_pages)
  217. {
  218. struct pbe *pbpage, *p;
  219. unsigned int num = PBES_PER_PAGE;
  220. for_each_pb_page (pbpage, pblist) {
  221. if (num >= nr_pages)
  222. break;
  223. fill_pb_page(pbpage);
  224. num += PBES_PER_PAGE;
  225. }
  226. if (pbpage) {
  227. for (num -= PBES_PER_PAGE - 1, p = pbpage; num < nr_pages; p++, num++)
  228. p->next = p + 1;
  229. p->next = NULL;
  230. }
  231. pr_debug("create_pbe_list(): initialized %d PBEs\n", num);
  232. }
  233. /**
  234. * @safe_needed - on resume, for storing the PBE list and the image,
  235. * we can only use memory pages that do not conflict with the pages
  236. * which had been used before suspend.
  237. *
  238. * The unsafe pages are marked with the PG_nosave_free flag
  239. *
  240. * Allocated but unusable (ie eaten) memory pages should be marked
  241. * so that swsusp_free() can release them
  242. */
  243. static inline void *alloc_image_page(gfp_t gfp_mask, int safe_needed)
  244. {
  245. void *res;
  246. if (safe_needed)
  247. do {
  248. res = (void *)get_zeroed_page(gfp_mask);
  249. if (res && PageNosaveFree(virt_to_page(res)))
  250. /* This is for swsusp_free() */
  251. SetPageNosave(virt_to_page(res));
  252. } while (res && PageNosaveFree(virt_to_page(res)));
  253. else
  254. res = (void *)get_zeroed_page(gfp_mask);
  255. if (res) {
  256. SetPageNosave(virt_to_page(res));
  257. SetPageNosaveFree(virt_to_page(res));
  258. }
  259. return res;
  260. }
  261. unsigned long get_safe_page(gfp_t gfp_mask)
  262. {
  263. return (unsigned long)alloc_image_page(gfp_mask, 1);
  264. }
  265. /**
  266. * alloc_pagedir - Allocate the page directory.
  267. *
  268. * First, determine exactly how many pages we need and
  269. * allocate them.
  270. *
  271. * We arrange the pages in a chain: each page is an array of PBES_PER_PAGE
  272. * struct pbe elements (pbes) and the last element in the page points
  273. * to the next page.
  274. *
  275. * On each page we set up a list of struct_pbe elements.
  276. */
  277. struct pbe *alloc_pagedir(unsigned int nr_pages, gfp_t gfp_mask, int safe_needed)
  278. {
  279. unsigned int num;
  280. struct pbe *pblist, *pbe;
  281. if (!nr_pages)
  282. return NULL;
  283. pr_debug("alloc_pagedir(): nr_pages = %d\n", nr_pages);
  284. pblist = alloc_image_page(gfp_mask, safe_needed);
  285. /* FIXME: rewrite this ugly loop */
  286. for (pbe = pblist, num = PBES_PER_PAGE; pbe && num < nr_pages;
  287. pbe = pbe->next, num += PBES_PER_PAGE) {
  288. pbe += PB_PAGE_SKIP;
  289. pbe->next = alloc_image_page(gfp_mask, safe_needed);
  290. }
  291. if (!pbe) { /* get_zeroed_page() failed */
  292. free_pagedir(pblist);
  293. pblist = NULL;
  294. }
  295. return pblist;
  296. }
  297. /**
  298. * Free pages we allocated for suspend. Suspend pages are alocated
  299. * before atomic copy, so we need to free them after resume.
  300. */
  301. void swsusp_free(void)
  302. {
  303. struct zone *zone;
  304. unsigned long zone_pfn;
  305. for_each_zone(zone) {
  306. for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
  307. if (pfn_valid(zone_pfn + zone->zone_start_pfn)) {
  308. struct page *page;
  309. page = pfn_to_page(zone_pfn + zone->zone_start_pfn);
  310. if (PageNosave(page) && PageNosaveFree(page)) {
  311. ClearPageNosave(page);
  312. ClearPageNosaveFree(page);
  313. free_page((long) page_address(page));
  314. }
  315. }
  316. }
  317. }
  318. /**
  319. * enough_free_mem - Make sure we enough free memory to snapshot.
  320. *
  321. * Returns TRUE or FALSE after checking the number of available
  322. * free pages.
  323. */
  324. static int enough_free_mem(unsigned int nr_pages)
  325. {
  326. pr_debug("swsusp: available memory: %u pages\n", nr_free_pages());
  327. return nr_free_pages() > (nr_pages + PAGES_FOR_IO +
  328. (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
  329. }
  330. int alloc_data_pages(struct pbe *pblist, gfp_t gfp_mask, int safe_needed)
  331. {
  332. struct pbe *p;
  333. for_each_pbe (p, pblist) {
  334. p->address = (unsigned long)alloc_image_page(gfp_mask, safe_needed);
  335. if (!p->address)
  336. return -ENOMEM;
  337. }
  338. return 0;
  339. }
  340. static struct pbe *swsusp_alloc(unsigned int nr_pages)
  341. {
  342. struct pbe *pblist;
  343. if (!(pblist = alloc_pagedir(nr_pages, GFP_ATOMIC | __GFP_COLD, 0))) {
  344. printk(KERN_ERR "suspend: Allocating pagedir failed.\n");
  345. return NULL;
  346. }
  347. create_pbe_list(pblist, nr_pages);
  348. if (alloc_data_pages(pblist, GFP_ATOMIC | __GFP_COLD, 0)) {
  349. printk(KERN_ERR "suspend: Allocating image pages failed.\n");
  350. swsusp_free();
  351. return NULL;
  352. }
  353. return pblist;
  354. }
  355. asmlinkage int swsusp_save(void)
  356. {
  357. unsigned int nr_pages;
  358. pr_debug("swsusp: critical section: \n");
  359. drain_local_pages();
  360. nr_pages = count_data_pages();
  361. printk("swsusp: Need to copy %u pages\n", nr_pages);
  362. pr_debug("swsusp: pages needed: %u + %lu + %u, free: %u\n",
  363. nr_pages,
  364. (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE,
  365. PAGES_FOR_IO, nr_free_pages());
  366. /* This is needed because of the fixed size of swsusp_info */
  367. if (MAX_PBES < (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE)
  368. return -ENOSPC;
  369. if (!enough_free_mem(nr_pages)) {
  370. printk(KERN_ERR "swsusp: Not enough free memory\n");
  371. return -ENOMEM;
  372. }
  373. pagedir_nosave = swsusp_alloc(nr_pages);
  374. if (!pagedir_nosave)
  375. return -ENOMEM;
  376. /* During allocating of suspend pagedir, new cold pages may appear.
  377. * Kill them.
  378. */
  379. drain_local_pages();
  380. copy_data_pages(pagedir_nosave);
  381. /*
  382. * End of critical section. From now on, we can write to memory,
  383. * but we should not touch disk. This specially means we must _not_
  384. * touch swap space! Except we must write out our image of course.
  385. */
  386. nr_copy_pages = nr_pages;
  387. printk("swsusp: critical section/: done (%d pages copied)\n", nr_pages);
  388. return 0;
  389. }