swsusp.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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("%s %d kbytes in %d.%02d seconds (%d.%02d MB/s)\n", msg, k,
  170. centisecs / 100, centisecs % 100,
  171. kps / 1000, (kps % 1000) / 10);
  172. }
  173. /**
  174. * swsusp_shrink_memory - Try to free as much memory as needed
  175. *
  176. * ... but do not OOM-kill anyone
  177. *
  178. * Notice: all userland should be stopped before it is called, or
  179. * livelock is possible.
  180. */
  181. #define SHRINK_BITE 10000
  182. static inline unsigned long __shrink_memory(long tmp)
  183. {
  184. if (tmp > SHRINK_BITE)
  185. tmp = SHRINK_BITE;
  186. return shrink_all_memory(tmp);
  187. }
  188. int swsusp_shrink_memory(void)
  189. {
  190. long tmp;
  191. struct zone *zone;
  192. unsigned long pages = 0;
  193. unsigned int i = 0;
  194. char *p = "-\\|/";
  195. struct timeval start, stop;
  196. printk("Shrinking memory... ");
  197. do_gettimeofday(&start);
  198. do {
  199. long size, highmem_size;
  200. highmem_size = count_highmem_pages();
  201. size = count_data_pages() + PAGES_FOR_IO + SPARE_PAGES;
  202. tmp = size;
  203. size += highmem_size;
  204. for_each_zone (zone)
  205. if (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. int swsusp_resume(void)
  235. {
  236. int error;
  237. local_irq_disable();
  238. /* NOTE: device_power_down() is just a suspend() with irqs off;
  239. * it has no special "power things down" semantics
  240. */
  241. if (device_power_down(PMSG_PRETHAW))
  242. printk(KERN_ERR "Some devices failed to power down, very bad\n");
  243. /* We'll ignore saved state, but this gets preempt count (etc) right */
  244. save_processor_state();
  245. error = restore_highmem();
  246. if (!error) {
  247. error = swsusp_arch_resume();
  248. /* The code below is only ever reached in case of a failure.
  249. * Otherwise execution continues at place where
  250. * swsusp_arch_suspend() was called
  251. */
  252. BUG_ON(!error);
  253. /* This call to restore_highmem() undos the previous one */
  254. restore_highmem();
  255. }
  256. /* The only reason why swsusp_arch_resume() can fail is memory being
  257. * very tight, so we have to free it as soon as we can to avoid
  258. * subsequent failures
  259. */
  260. swsusp_free();
  261. restore_processor_state();
  262. touch_softlockup_watchdog();
  263. device_power_up();
  264. local_irq_enable();
  265. return error;
  266. }