swsusp.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. * linux/kernel/power/swsusp.c
  3. *
  4. * This file provides code to write suspend image to swap and read it back.
  5. *
  6. * Copyright (C) 1998-2001 Gabor Kuti <seasons@fornax.hu>
  7. * Copyright (C) 1998,2001-2005 Pavel Machek <pavel@suse.cz>
  8. *
  9. * This file is released under the GPLv2.
  10. *
  11. * I'd like to thank the following people for their work:
  12. *
  13. * Pavel Machek <pavel@ucw.cz>:
  14. * Modifications, defectiveness pointing, being with me at the very beginning,
  15. * suspend to swap space, stop all tasks. Port to 2.4.18-ac and 2.5.17.
  16. *
  17. * Steve Doddi <dirk@loth.demon.co.uk>:
  18. * Support the possibility of hardware state restoring.
  19. *
  20. * Raph <grey.havens@earthling.net>:
  21. * Support for preserving states of network devices and virtual console
  22. * (including X and svgatextmode)
  23. *
  24. * Kurt Garloff <garloff@suse.de>:
  25. * Straightened the critical function in order to prevent compilers from
  26. * playing tricks with local variables.
  27. *
  28. * Andreas Mohr <a.mohr@mailto.de>
  29. *
  30. * Alex Badea <vampire@go.ro>:
  31. * Fixed runaway init
  32. *
  33. * Rafael J. Wysocki <rjw@sisk.pl>
  34. * Reworked the freeing of memory and the handling of swap
  35. *
  36. * More state savers are welcome. Especially for the scsi layer...
  37. *
  38. * For TODOs,FIXMEs also look in Documentation/power/swsusp.txt
  39. */
  40. #include <linux/mm.h>
  41. #include <linux/suspend.h>
  42. #include <linux/spinlock.h>
  43. #include <linux/kernel.h>
  44. #include <linux/major.h>
  45. #include <linux/swap.h>
  46. #include <linux/pm.h>
  47. #include <linux/swapops.h>
  48. #include <linux/bootmem.h>
  49. #include <linux/syscalls.h>
  50. #include <linux/highmem.h>
  51. #include <linux/time.h>
  52. #include <linux/rbtree.h>
  53. #include "power.h"
  54. /*
  55. * Preferred image size in bytes (tunable via /sys/power/image_size).
  56. * When it is set to N, swsusp will do its best to ensure the image
  57. * size will not exceed N bytes, but if that is impossible, it will
  58. * try to create the smallest image possible.
  59. */
  60. unsigned long image_size = 500 * 1024 * 1024;
  61. int in_suspend __nosavedata = 0;
  62. /**
  63. * The following functions are used for tracing the allocated
  64. * swap pages, so that they can be freed in case of an error.
  65. */
  66. struct swsusp_extent {
  67. struct rb_node node;
  68. unsigned long start;
  69. unsigned long end;
  70. };
  71. static struct rb_root swsusp_extents = RB_ROOT;
  72. static int swsusp_extents_insert(unsigned long swap_offset)
  73. {
  74. struct rb_node **new = &(swsusp_extents.rb_node);
  75. struct rb_node *parent = NULL;
  76. struct swsusp_extent *ext;
  77. /* Figure out where to put the new node */
  78. while (*new) {
  79. ext = container_of(*new, struct swsusp_extent, node);
  80. parent = *new;
  81. if (swap_offset < ext->start) {
  82. /* Try to merge */
  83. if (swap_offset == ext->start - 1) {
  84. ext->start--;
  85. return 0;
  86. }
  87. new = &((*new)->rb_left);
  88. } else if (swap_offset > ext->end) {
  89. /* Try to merge */
  90. if (swap_offset == ext->end + 1) {
  91. ext->end++;
  92. return 0;
  93. }
  94. new = &((*new)->rb_right);
  95. } else {
  96. /* It already is in the tree */
  97. return -EINVAL;
  98. }
  99. }
  100. /* Add the new node and rebalance the tree. */
  101. ext = kzalloc(sizeof(struct swsusp_extent), GFP_KERNEL);
  102. if (!ext)
  103. return -ENOMEM;
  104. ext->start = swap_offset;
  105. ext->end = swap_offset;
  106. rb_link_node(&ext->node, parent, new);
  107. rb_insert_color(&ext->node, &swsusp_extents);
  108. return 0;
  109. }
  110. /**
  111. * alloc_swapdev_block - allocate a swap page and register that it has
  112. * been allocated, so that it can be freed in case of an error.
  113. */
  114. sector_t alloc_swapdev_block(int swap)
  115. {
  116. unsigned long offset;
  117. offset = swp_offset(get_swap_page_of_type(swap));
  118. if (offset) {
  119. if (swsusp_extents_insert(offset))
  120. swap_free(swp_entry(swap, offset));
  121. else
  122. return swapdev_block(swap, offset);
  123. }
  124. return 0;
  125. }
  126. /**
  127. * free_all_swap_pages - free swap pages allocated for saving image data.
  128. * It also frees the extents used to register which swap entres had been
  129. * allocated.
  130. */
  131. void free_all_swap_pages(int swap)
  132. {
  133. struct rb_node *node;
  134. while ((node = swsusp_extents.rb_node)) {
  135. struct swsusp_extent *ext;
  136. unsigned long offset;
  137. ext = container_of(node, struct swsusp_extent, node);
  138. rb_erase(node, &swsusp_extents);
  139. for (offset = ext->start; offset <= ext->end; offset++)
  140. swap_free(swp_entry(swap, offset));
  141. kfree(ext);
  142. }
  143. }
  144. int swsusp_swap_in_use(void)
  145. {
  146. return (swsusp_extents.rb_node != NULL);
  147. }
  148. /**
  149. * swsusp_show_speed - print the time elapsed between two events represented by
  150. * @start and @stop
  151. *
  152. * @nr_pages - number of pages processed between @start and @stop
  153. * @msg - introductory message to print
  154. */
  155. void swsusp_show_speed(struct timeval *start, struct timeval *stop,
  156. unsigned nr_pages, char *msg)
  157. {
  158. s64 elapsed_centisecs64;
  159. int centisecs;
  160. int k;
  161. int kps;
  162. elapsed_centisecs64 = timeval_to_ns(stop) - timeval_to_ns(start);
  163. do_div(elapsed_centisecs64, NSEC_PER_SEC / 100);
  164. centisecs = elapsed_centisecs64;
  165. if (centisecs == 0)
  166. centisecs = 1; /* avoid div-by-zero */
  167. k = nr_pages * (PAGE_SIZE / 1024);
  168. kps = (k * 100) / centisecs;
  169. printk(KERN_INFO "PM: %s %d kbytes in %d.%02d seconds (%d.%02d MB/s)\n",
  170. msg, k,
  171. centisecs / 100, centisecs % 100,
  172. kps / 1000, (kps % 1000) / 10);
  173. }
  174. /**
  175. * swsusp_shrink_memory - Try to free as much memory as needed
  176. *
  177. * ... but do not OOM-kill anyone
  178. *
  179. * Notice: all userland should be stopped before it is called, or
  180. * livelock is possible.
  181. */
  182. #define SHRINK_BITE 10000
  183. static inline unsigned long __shrink_memory(long tmp)
  184. {
  185. if (tmp > SHRINK_BITE)
  186. tmp = SHRINK_BITE;
  187. return shrink_all_memory(tmp);
  188. }
  189. int swsusp_shrink_memory(void)
  190. {
  191. long tmp;
  192. struct zone *zone;
  193. unsigned long pages = 0;
  194. unsigned int i = 0;
  195. char *p = "-\\|/";
  196. struct timeval start, stop;
  197. printk(KERN_INFO "PM: Shrinking memory... ");
  198. do_gettimeofday(&start);
  199. do {
  200. long size, highmem_size;
  201. highmem_size = count_highmem_pages();
  202. size = count_data_pages() + PAGES_FOR_IO + SPARE_PAGES;
  203. tmp = size;
  204. size += highmem_size;
  205. for_each_populated_zone(zone) {
  206. tmp += snapshot_additional_pages(zone);
  207. if (is_highmem(zone)) {
  208. highmem_size -=
  209. zone_page_state(zone, NR_FREE_PAGES);
  210. } else {
  211. tmp -= zone_page_state(zone, NR_FREE_PAGES);
  212. tmp += zone->lowmem_reserve[ZONE_NORMAL];
  213. }
  214. }
  215. if (highmem_size < 0)
  216. highmem_size = 0;
  217. tmp += highmem_size;
  218. if (tmp > 0) {
  219. tmp = __shrink_memory(tmp);
  220. if (!tmp)
  221. return -ENOMEM;
  222. pages += tmp;
  223. } else if (size > image_size / PAGE_SIZE) {
  224. tmp = __shrink_memory(size - (image_size / PAGE_SIZE));
  225. pages += tmp;
  226. }
  227. printk("\b%c", p[i++%4]);
  228. } while (tmp > 0);
  229. do_gettimeofday(&stop);
  230. printk("\bdone (%lu pages freed)\n", pages);
  231. swsusp_show_speed(&start, &stop, pages, "Freed");
  232. return 0;
  233. }
  234. /*
  235. * Platforms, like ACPI, may want us to save some memory used by them during
  236. * hibernation and to restore the contents of this memory during the subsequent
  237. * resume. The code below implements a mechanism allowing us to do that.
  238. */
  239. struct nvs_page {
  240. unsigned long phys_start;
  241. unsigned int size;
  242. void *kaddr;
  243. void *data;
  244. struct list_head node;
  245. };
  246. static LIST_HEAD(nvs_list);
  247. /**
  248. * hibernate_nvs_register - register platform NVS memory region to save
  249. * @start - physical address of the region
  250. * @size - size of the region
  251. *
  252. * The NVS region need not be page-aligned (both ends) and we arrange
  253. * things so that the data from page-aligned addresses in this region will
  254. * be copied into separate RAM pages.
  255. */
  256. int hibernate_nvs_register(unsigned long start, unsigned long size)
  257. {
  258. struct nvs_page *entry, *next;
  259. while (size > 0) {
  260. unsigned int nr_bytes;
  261. entry = kzalloc(sizeof(struct nvs_page), GFP_KERNEL);
  262. if (!entry)
  263. goto Error;
  264. list_add_tail(&entry->node, &nvs_list);
  265. entry->phys_start = start;
  266. nr_bytes = PAGE_SIZE - (start & ~PAGE_MASK);
  267. entry->size = (size < nr_bytes) ? size : nr_bytes;
  268. start += entry->size;
  269. size -= entry->size;
  270. }
  271. return 0;
  272. Error:
  273. list_for_each_entry_safe(entry, next, &nvs_list, node) {
  274. list_del(&entry->node);
  275. kfree(entry);
  276. }
  277. return -ENOMEM;
  278. }
  279. /**
  280. * hibernate_nvs_free - free data pages allocated for saving NVS regions
  281. */
  282. void hibernate_nvs_free(void)
  283. {
  284. struct nvs_page *entry;
  285. list_for_each_entry(entry, &nvs_list, node)
  286. if (entry->data) {
  287. free_page((unsigned long)entry->data);
  288. entry->data = NULL;
  289. if (entry->kaddr) {
  290. iounmap(entry->kaddr);
  291. entry->kaddr = NULL;
  292. }
  293. }
  294. }
  295. /**
  296. * hibernate_nvs_alloc - allocate memory necessary for saving NVS regions
  297. */
  298. int hibernate_nvs_alloc(void)
  299. {
  300. struct nvs_page *entry;
  301. list_for_each_entry(entry, &nvs_list, node) {
  302. entry->data = (void *)__get_free_page(GFP_KERNEL);
  303. if (!entry->data) {
  304. hibernate_nvs_free();
  305. return -ENOMEM;
  306. }
  307. }
  308. return 0;
  309. }
  310. /**
  311. * hibernate_nvs_save - save NVS memory regions
  312. */
  313. void hibernate_nvs_save(void)
  314. {
  315. struct nvs_page *entry;
  316. printk(KERN_INFO "PM: Saving platform NVS memory\n");
  317. list_for_each_entry(entry, &nvs_list, node)
  318. if (entry->data) {
  319. entry->kaddr = ioremap(entry->phys_start, entry->size);
  320. memcpy(entry->data, entry->kaddr, entry->size);
  321. }
  322. }
  323. /**
  324. * hibernate_nvs_restore - restore NVS memory regions
  325. *
  326. * This function is going to be called with interrupts disabled, so it
  327. * cannot iounmap the virtual addresses used to access the NVS region.
  328. */
  329. void hibernate_nvs_restore(void)
  330. {
  331. struct nvs_page *entry;
  332. printk(KERN_INFO "PM: Restoring platform NVS memory\n");
  333. list_for_each_entry(entry, &nvs_list, node)
  334. if (entry->data)
  335. memcpy(entry->kaddr, entry->data, entry->size);
  336. }