snapshot.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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 n;
  151. n = 0;
  152. for_each_zone (zone) {
  153. if (is_highmem(zone))
  154. continue;
  155. mark_free_pages(zone);
  156. for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
  157. n += saveable(zone, &zone_pfn);
  158. }
  159. return n;
  160. }
  161. static void copy_data_pages(struct pbe *pblist)
  162. {
  163. struct zone *zone;
  164. unsigned long zone_pfn;
  165. struct pbe *pbe, *p;
  166. pbe = pblist;
  167. for_each_zone (zone) {
  168. if (is_highmem(zone))
  169. continue;
  170. mark_free_pages(zone);
  171. /* This is necessary for swsusp_free() */
  172. for_each_pb_page (p, pblist)
  173. SetPageNosaveFree(virt_to_page(p));
  174. for_each_pbe (p, pblist)
  175. SetPageNosaveFree(virt_to_page(p->address));
  176. for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
  177. if (saveable(zone, &zone_pfn)) {
  178. struct page *page;
  179. page = pfn_to_page(zone_pfn + zone->zone_start_pfn);
  180. BUG_ON(!pbe);
  181. pbe->orig_address = (unsigned long)page_address(page);
  182. /* copy_page is not usable for copying task structs. */
  183. memcpy((void *)pbe->address, (void *)pbe->orig_address, PAGE_SIZE);
  184. pbe = pbe->next;
  185. }
  186. }
  187. }
  188. BUG_ON(pbe);
  189. }
  190. /**
  191. * free_pagedir - free pages allocated with alloc_pagedir()
  192. */
  193. static void free_pagedir(struct pbe *pblist)
  194. {
  195. struct pbe *pbe;
  196. while (pblist) {
  197. pbe = (pblist + PB_PAGE_SKIP)->next;
  198. ClearPageNosave(virt_to_page(pblist));
  199. ClearPageNosaveFree(virt_to_page(pblist));
  200. free_page((unsigned long)pblist);
  201. pblist = pbe;
  202. }
  203. }
  204. /**
  205. * fill_pb_page - Create a list of PBEs on a given memory page
  206. */
  207. static inline void fill_pb_page(struct pbe *pbpage)
  208. {
  209. struct pbe *p;
  210. p = pbpage;
  211. pbpage += PB_PAGE_SKIP;
  212. do
  213. p->next = p + 1;
  214. while (++p < pbpage);
  215. }
  216. /**
  217. * create_pbe_list - Create a list of PBEs on top of a given chain
  218. * of memory pages allocated with alloc_pagedir()
  219. */
  220. void create_pbe_list(struct pbe *pblist, unsigned nr_pages)
  221. {
  222. struct pbe *pbpage, *p;
  223. unsigned num = PBES_PER_PAGE;
  224. for_each_pb_page (pbpage, pblist) {
  225. if (num >= nr_pages)
  226. break;
  227. fill_pb_page(pbpage);
  228. num += PBES_PER_PAGE;
  229. }
  230. if (pbpage) {
  231. for (num -= PBES_PER_PAGE - 1, p = pbpage; num < nr_pages; p++, num++)
  232. p->next = p + 1;
  233. p->next = NULL;
  234. }
  235. pr_debug("create_pbe_list(): initialized %d PBEs\n", num);
  236. }
  237. static void *alloc_image_page(void)
  238. {
  239. void *res = (void *)get_zeroed_page(GFP_ATOMIC | __GFP_COLD);
  240. if (res) {
  241. SetPageNosave(virt_to_page(res));
  242. SetPageNosaveFree(virt_to_page(res));
  243. }
  244. return res;
  245. }
  246. /**
  247. * alloc_pagedir - Allocate the page directory.
  248. *
  249. * First, determine exactly how many pages we need and
  250. * allocate them.
  251. *
  252. * We arrange the pages in a chain: each page is an array of PBES_PER_PAGE
  253. * struct pbe elements (pbes) and the last element in the page points
  254. * to the next page.
  255. *
  256. * On each page we set up a list of struct_pbe elements.
  257. */
  258. struct pbe *alloc_pagedir(unsigned nr_pages)
  259. {
  260. unsigned num;
  261. struct pbe *pblist, *pbe;
  262. if (!nr_pages)
  263. return NULL;
  264. pr_debug("alloc_pagedir(): nr_pages = %d\n", nr_pages);
  265. pblist = alloc_image_page();
  266. /* FIXME: rewrite this ugly loop */
  267. for (pbe = pblist, num = PBES_PER_PAGE; pbe && num < nr_pages;
  268. pbe = pbe->next, num += PBES_PER_PAGE) {
  269. pbe += PB_PAGE_SKIP;
  270. pbe->next = alloc_image_page();
  271. }
  272. if (!pbe) { /* get_zeroed_page() failed */
  273. free_pagedir(pblist);
  274. pblist = NULL;
  275. }
  276. return pblist;
  277. }
  278. /**
  279. * Free pages we allocated for suspend. Suspend pages are alocated
  280. * before atomic copy, so we need to free them after resume.
  281. */
  282. void swsusp_free(void)
  283. {
  284. struct zone *zone;
  285. unsigned long zone_pfn;
  286. for_each_zone(zone) {
  287. for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
  288. if (pfn_valid(zone_pfn + zone->zone_start_pfn)) {
  289. struct page * page;
  290. page = pfn_to_page(zone_pfn + zone->zone_start_pfn);
  291. if (PageNosave(page) && PageNosaveFree(page)) {
  292. ClearPageNosave(page);
  293. ClearPageNosaveFree(page);
  294. free_page((long) page_address(page));
  295. }
  296. }
  297. }
  298. }
  299. /**
  300. * enough_free_mem - Make sure we enough free memory to snapshot.
  301. *
  302. * Returns TRUE or FALSE after checking the number of available
  303. * free pages.
  304. */
  305. static int enough_free_mem(unsigned nr_pages)
  306. {
  307. pr_debug("swsusp: available memory: %u pages\n", nr_free_pages());
  308. return nr_free_pages() > (nr_pages + PAGES_FOR_IO +
  309. (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE);
  310. }
  311. static struct pbe *swsusp_alloc(unsigned nr_pages)
  312. {
  313. struct pbe *pblist, *p;
  314. if (!(pblist = alloc_pagedir(nr_pages))) {
  315. printk(KERN_ERR "suspend: Allocating pagedir failed.\n");
  316. return NULL;
  317. }
  318. create_pbe_list(pblist, nr_pages);
  319. for_each_pbe (p, pblist) {
  320. p->address = (unsigned long)alloc_image_page();
  321. if (!p->address) {
  322. printk(KERN_ERR "suspend: Allocating image pages failed.\n");
  323. swsusp_free();
  324. return NULL;
  325. }
  326. }
  327. return pblist;
  328. }
  329. asmlinkage int swsusp_save(void)
  330. {
  331. unsigned nr_pages;
  332. pr_debug("swsusp: critical section: \n");
  333. if (save_highmem()) {
  334. printk(KERN_CRIT "swsusp: Not enough free pages for highmem\n");
  335. restore_highmem();
  336. return -ENOMEM;
  337. }
  338. drain_local_pages();
  339. nr_pages = count_data_pages();
  340. printk("swsusp: Need to copy %u pages\n", nr_pages);
  341. pr_debug("swsusp: pages needed: %u + %lu + %u, free: %u\n",
  342. nr_pages,
  343. (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE,
  344. PAGES_FOR_IO, nr_free_pages());
  345. /* This is needed because of the fixed size of swsusp_info */
  346. if (MAX_PBES < (nr_pages + PBES_PER_PAGE - 1) / PBES_PER_PAGE)
  347. return -ENOSPC;
  348. if (!enough_free_mem(nr_pages)) {
  349. printk(KERN_ERR "swsusp: Not enough free memory\n");
  350. return -ENOMEM;
  351. }
  352. if (!enough_swap(nr_pages)) {
  353. printk(KERN_ERR "swsusp: Not enough free swap\n");
  354. return -ENOSPC;
  355. }
  356. pagedir_nosave = swsusp_alloc(nr_pages);
  357. if (!pagedir_nosave)
  358. return -ENOMEM;
  359. /* During allocating of suspend pagedir, new cold pages may appear.
  360. * Kill them.
  361. */
  362. drain_local_pages();
  363. copy_data_pages(pagedir_nosave);
  364. /*
  365. * End of critical section. From now on, we can write to memory,
  366. * but we should not touch disk. This specially means we must _not_
  367. * touch swap space! Except we must write out our image of course.
  368. */
  369. nr_copy_pages = nr_pages;
  370. printk("swsusp: critical section/: done (%d pages copied)\n", nr_pages);
  371. return 0;
  372. }