snapshot.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. static 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. static void *alloc_image_page(void)
  237. {
  238. void *res = (void *)get_zeroed_page(GFP_ATOMIC | __GFP_COLD);
  239. if (res) {
  240. SetPageNosave(virt_to_page(res));
  241. SetPageNosaveFree(virt_to_page(res));
  242. }
  243. return res;
  244. }
  245. /**
  246. * alloc_pagedir - Allocate the page directory.
  247. *
  248. * First, determine exactly how many pages we need and
  249. * allocate them.
  250. *
  251. * We arrange the pages in a chain: each page is an array of PBES_PER_PAGE
  252. * struct pbe elements (pbes) and the last element in the page points
  253. * to the next page.
  254. *
  255. * On each page we set up a list of struct_pbe elements.
  256. */
  257. struct pbe *alloc_pagedir(unsigned int nr_pages)
  258. {
  259. unsigned int num;
  260. struct pbe *pblist, *pbe;
  261. if (!nr_pages)
  262. return NULL;
  263. pr_debug("alloc_pagedir(): nr_pages = %d\n", nr_pages);
  264. pblist = alloc_image_page();
  265. /* FIXME: rewrite this ugly loop */
  266. for (pbe = pblist, num = PBES_PER_PAGE; pbe && num < nr_pages;
  267. pbe = pbe->next, num += PBES_PER_PAGE) {
  268. pbe += PB_PAGE_SKIP;
  269. pbe->next = alloc_image_page();
  270. }
  271. if (!pbe) { /* get_zeroed_page() failed */
  272. free_pagedir(pblist);
  273. pblist = NULL;
  274. }
  275. return pblist;
  276. }
  277. /**
  278. * Free pages we allocated for suspend. Suspend pages are alocated
  279. * before atomic copy, so we need to free them after resume.
  280. */
  281. void swsusp_free(void)
  282. {
  283. struct zone *zone;
  284. unsigned long zone_pfn;
  285. for_each_zone(zone) {
  286. for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
  287. if (pfn_valid(zone_pfn + zone->zone_start_pfn)) {
  288. struct page *page;
  289. page = pfn_to_page(zone_pfn + zone->zone_start_pfn);
  290. if (PageNosave(page) && PageNosaveFree(page)) {
  291. ClearPageNosave(page);
  292. ClearPageNosaveFree(page);
  293. free_page((long) page_address(page));
  294. }
  295. }
  296. }
  297. }
  298. /**
  299. * enough_free_mem - Make sure we enough free memory to snapshot.
  300. *
  301. * Returns TRUE or FALSE after checking the number of available
  302. * free pages.
  303. */
  304. static int enough_free_mem(unsigned int nr_pages)
  305. {
  306. pr_debug("swsusp: available memory: %u pages\n", nr_free_pages());
  307. return nr_free_pages() > (nr_pages + PAGES_FOR_IO +
  308. (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
  309. }
  310. static struct pbe *swsusp_alloc(unsigned int nr_pages)
  311. {
  312. struct pbe *pblist, *p;
  313. if (!(pblist = alloc_pagedir(nr_pages))) {
  314. printk(KERN_ERR "suspend: Allocating pagedir failed.\n");
  315. return NULL;
  316. }
  317. create_pbe_list(pblist, nr_pages);
  318. for_each_pbe (p, pblist) {
  319. p->address = (unsigned long)alloc_image_page();
  320. if (!p->address) {
  321. printk(KERN_ERR "suspend: Allocating image pages failed.\n");
  322. swsusp_free();
  323. return NULL;
  324. }
  325. }
  326. return pblist;
  327. }
  328. asmlinkage int swsusp_save(void)
  329. {
  330. unsigned int nr_pages;
  331. pr_debug("swsusp: critical section: \n");
  332. if (save_highmem()) {
  333. printk(KERN_CRIT "swsusp: Not enough free pages for highmem\n");
  334. restore_highmem();
  335. return -ENOMEM;
  336. }
  337. drain_local_pages();
  338. nr_pages = count_data_pages();
  339. printk("swsusp: Need to copy %u pages\n", nr_pages);
  340. pr_debug("swsusp: pages needed: %u + %lu + %u, free: %u\n",
  341. nr_pages,
  342. (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE,
  343. PAGES_FOR_IO, nr_free_pages());
  344. /* This is needed because of the fixed size of swsusp_info */
  345. if (MAX_PBES < (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE)
  346. return -ENOSPC;
  347. if (!enough_free_mem(nr_pages)) {
  348. printk(KERN_ERR "swsusp: Not enough free memory\n");
  349. return -ENOMEM;
  350. }
  351. if (!enough_swap(nr_pages)) {
  352. printk(KERN_ERR "swsusp: Not enough free swap\n");
  353. return -ENOSPC;
  354. }
  355. pagedir_nosave = swsusp_alloc(nr_pages);
  356. if (!pagedir_nosave)
  357. return -ENOMEM;
  358. /* During allocating of suspend pagedir, new cold pages may appear.
  359. * Kill them.
  360. */
  361. drain_local_pages();
  362. copy_data_pages(pagedir_nosave);
  363. /*
  364. * End of critical section. From now on, we can write to memory,
  365. * but we should not touch disk. This specially means we must _not_
  366. * touch swap space! Except we must write out our image of course.
  367. */
  368. nr_copy_pages = nr_pages;
  369. printk("swsusp: critical section/: done (%d pages copied)\n", nr_pages);
  370. return 0;
  371. }