snapshot.c 11 KB

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