snapshot.c 10 KB

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