swsusp.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 <linux/io.h>
  54. #include "power.h"
  55. /*
  56. * Preferred image size in bytes (tunable via /sys/power/image_size).
  57. * When it is set to N, swsusp will do its best to ensure the image
  58. * size will not exceed N bytes, but if that is impossible, it will
  59. * try to create the smallest image possible.
  60. */
  61. unsigned long image_size = 500 * 1024 * 1024;
  62. int in_suspend __nosavedata = 0;
  63. /**
  64. * The following functions are used for tracing the allocated
  65. * swap pages, so that they can be freed in case of an error.
  66. */
  67. struct swsusp_extent {
  68. struct rb_node node;
  69. unsigned long start;
  70. unsigned long end;
  71. };
  72. static struct rb_root swsusp_extents = RB_ROOT;
  73. static int swsusp_extents_insert(unsigned long swap_offset)
  74. {
  75. struct rb_node **new = &(swsusp_extents.rb_node);
  76. struct rb_node *parent = NULL;
  77. struct swsusp_extent *ext;
  78. /* Figure out where to put the new node */
  79. while (*new) {
  80. ext = container_of(*new, struct swsusp_extent, node);
  81. parent = *new;
  82. if (swap_offset < ext->start) {
  83. /* Try to merge */
  84. if (swap_offset == ext->start - 1) {
  85. ext->start--;
  86. return 0;
  87. }
  88. new = &((*new)->rb_left);
  89. } else if (swap_offset > ext->end) {
  90. /* Try to merge */
  91. if (swap_offset == ext->end + 1) {
  92. ext->end++;
  93. return 0;
  94. }
  95. new = &((*new)->rb_right);
  96. } else {
  97. /* It already is in the tree */
  98. return -EINVAL;
  99. }
  100. }
  101. /* Add the new node and rebalance the tree. */
  102. ext = kzalloc(sizeof(struct swsusp_extent), GFP_KERNEL);
  103. if (!ext)
  104. return -ENOMEM;
  105. ext->start = swap_offset;
  106. ext->end = swap_offset;
  107. rb_link_node(&ext->node, parent, new);
  108. rb_insert_color(&ext->node, &swsusp_extents);
  109. return 0;
  110. }
  111. /**
  112. * alloc_swapdev_block - allocate a swap page and register that it has
  113. * been allocated, so that it can be freed in case of an error.
  114. */
  115. sector_t alloc_swapdev_block(int swap)
  116. {
  117. unsigned long offset;
  118. offset = swp_offset(get_swap_page_of_type(swap));
  119. if (offset) {
  120. if (swsusp_extents_insert(offset))
  121. swap_free(swp_entry(swap, offset));
  122. else
  123. return swapdev_block(swap, offset);
  124. }
  125. return 0;
  126. }
  127. /**
  128. * free_all_swap_pages - free swap pages allocated for saving image data.
  129. * It also frees the extents used to register which swap entres had been
  130. * allocated.
  131. */
  132. void free_all_swap_pages(int swap)
  133. {
  134. struct rb_node *node;
  135. while ((node = swsusp_extents.rb_node)) {
  136. struct swsusp_extent *ext;
  137. unsigned long offset;
  138. ext = container_of(node, struct swsusp_extent, node);
  139. rb_erase(node, &swsusp_extents);
  140. for (offset = ext->start; offset <= ext->end; offset++)
  141. swap_free(swp_entry(swap, offset));
  142. kfree(ext);
  143. }
  144. }
  145. int swsusp_swap_in_use(void)
  146. {
  147. return (swsusp_extents.rb_node != NULL);
  148. }
  149. /**
  150. * swsusp_show_speed - print the time elapsed between two events represented by
  151. * @start and @stop
  152. *
  153. * @nr_pages - number of pages processed between @start and @stop
  154. * @msg - introductory message to print
  155. */
  156. void swsusp_show_speed(struct timeval *start, struct timeval *stop,
  157. unsigned nr_pages, char *msg)
  158. {
  159. s64 elapsed_centisecs64;
  160. int centisecs;
  161. int k;
  162. int kps;
  163. elapsed_centisecs64 = timeval_to_ns(stop) - timeval_to_ns(start);
  164. do_div(elapsed_centisecs64, NSEC_PER_SEC / 100);
  165. centisecs = elapsed_centisecs64;
  166. if (centisecs == 0)
  167. centisecs = 1; /* avoid div-by-zero */
  168. k = nr_pages * (PAGE_SIZE / 1024);
  169. kps = (k * 100) / centisecs;
  170. printk(KERN_INFO "PM: %s %d kbytes in %d.%02d seconds (%d.%02d MB/s)\n",
  171. msg, k,
  172. centisecs / 100, centisecs % 100,
  173. kps / 1000, (kps % 1000) / 10);
  174. }
  175. /**
  176. * swsusp_shrink_memory - Try to free as much memory as needed
  177. *
  178. * ... but do not OOM-kill anyone
  179. *
  180. * Notice: all userland should be stopped before it is called, or
  181. * livelock is possible.
  182. */
  183. #define SHRINK_BITE 10000
  184. static inline unsigned long __shrink_memory(long tmp)
  185. {
  186. if (tmp > SHRINK_BITE)
  187. tmp = SHRINK_BITE;
  188. return shrink_all_memory(tmp);
  189. }
  190. int swsusp_shrink_memory(void)
  191. {
  192. long tmp;
  193. struct zone *zone;
  194. unsigned long pages = 0;
  195. unsigned int i = 0;
  196. char *p = "-\\|/";
  197. struct timeval start, stop;
  198. printk(KERN_INFO "PM: Shrinking memory... ");
  199. do_gettimeofday(&start);
  200. do {
  201. long size, highmem_size;
  202. highmem_size = count_highmem_pages();
  203. size = count_data_pages() + PAGES_FOR_IO + SPARE_PAGES;
  204. tmp = size;
  205. size += highmem_size;
  206. for_each_populated_zone(zone) {
  207. tmp += snapshot_additional_pages(zone);
  208. if (is_highmem(zone)) {
  209. highmem_size -=
  210. zone_page_state(zone, NR_FREE_PAGES);
  211. } else {
  212. tmp -= zone_page_state(zone, NR_FREE_PAGES);
  213. tmp += zone->lowmem_reserve[ZONE_NORMAL];
  214. }
  215. }
  216. if (highmem_size < 0)
  217. highmem_size = 0;
  218. tmp += highmem_size;
  219. if (tmp > 0) {
  220. tmp = __shrink_memory(tmp);
  221. if (!tmp)
  222. return -ENOMEM;
  223. pages += tmp;
  224. } else if (size > image_size / PAGE_SIZE) {
  225. tmp = __shrink_memory(size - (image_size / PAGE_SIZE));
  226. pages += tmp;
  227. }
  228. printk("\b%c", p[i++%4]);
  229. } while (tmp > 0);
  230. do_gettimeofday(&stop);
  231. printk("\bdone (%lu pages freed)\n", pages);
  232. swsusp_show_speed(&start, &stop, pages, "Freed");
  233. return 0;
  234. }
  235. /*
  236. * Platforms, like ACPI, may want us to save some memory used by them during
  237. * hibernation and to restore the contents of this memory during the subsequent
  238. * resume. The code below implements a mechanism allowing us to do that.
  239. */
  240. struct nvs_page {
  241. unsigned long phys_start;
  242. unsigned int size;
  243. void *kaddr;
  244. void *data;
  245. struct list_head node;
  246. };
  247. static LIST_HEAD(nvs_list);
  248. /**
  249. * hibernate_nvs_register - register platform NVS memory region to save
  250. * @start - physical address of the region
  251. * @size - size of the region
  252. *
  253. * The NVS region need not be page-aligned (both ends) and we arrange
  254. * things so that the data from page-aligned addresses in this region will
  255. * be copied into separate RAM pages.
  256. */
  257. int hibernate_nvs_register(unsigned long start, unsigned long size)
  258. {
  259. struct nvs_page *entry, *next;
  260. while (size > 0) {
  261. unsigned int nr_bytes;
  262. entry = kzalloc(sizeof(struct nvs_page), GFP_KERNEL);
  263. if (!entry)
  264. goto Error;
  265. list_add_tail(&entry->node, &nvs_list);
  266. entry->phys_start = start;
  267. nr_bytes = PAGE_SIZE - (start & ~PAGE_MASK);
  268. entry->size = (size < nr_bytes) ? size : nr_bytes;
  269. start += entry->size;
  270. size -= entry->size;
  271. }
  272. return 0;
  273. Error:
  274. list_for_each_entry_safe(entry, next, &nvs_list, node) {
  275. list_del(&entry->node);
  276. kfree(entry);
  277. }
  278. return -ENOMEM;
  279. }
  280. /**
  281. * hibernate_nvs_free - free data pages allocated for saving NVS regions
  282. */
  283. void hibernate_nvs_free(void)
  284. {
  285. struct nvs_page *entry;
  286. list_for_each_entry(entry, &nvs_list, node)
  287. if (entry->data) {
  288. free_page((unsigned long)entry->data);
  289. entry->data = NULL;
  290. if (entry->kaddr) {
  291. iounmap(entry->kaddr);
  292. entry->kaddr = NULL;
  293. }
  294. }
  295. }
  296. /**
  297. * hibernate_nvs_alloc - allocate memory necessary for saving NVS regions
  298. */
  299. int hibernate_nvs_alloc(void)
  300. {
  301. struct nvs_page *entry;
  302. list_for_each_entry(entry, &nvs_list, node) {
  303. entry->data = (void *)__get_free_page(GFP_KERNEL);
  304. if (!entry->data) {
  305. hibernate_nvs_free();
  306. return -ENOMEM;
  307. }
  308. }
  309. return 0;
  310. }
  311. /**
  312. * hibernate_nvs_save - save NVS memory regions
  313. */
  314. void hibernate_nvs_save(void)
  315. {
  316. struct nvs_page *entry;
  317. printk(KERN_INFO "PM: Saving platform NVS memory\n");
  318. list_for_each_entry(entry, &nvs_list, node)
  319. if (entry->data) {
  320. entry->kaddr = ioremap(entry->phys_start, entry->size);
  321. memcpy(entry->data, entry->kaddr, entry->size);
  322. }
  323. }
  324. /**
  325. * hibernate_nvs_restore - restore NVS memory regions
  326. *
  327. * This function is going to be called with interrupts disabled, so it
  328. * cannot iounmap the virtual addresses used to access the NVS region.
  329. */
  330. void hibernate_nvs_restore(void)
  331. {
  332. struct nvs_page *entry;
  333. printk(KERN_INFO "PM: Restoring platform NVS memory\n");
  334. list_for_each_entry(entry, &nvs_list, node)
  335. if (entry->data)
  336. memcpy(entry->kaddr, entry->data, entry->size);
  337. }