swsusp.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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_zone (zone)
  206. if (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. }