swsusp.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 "power.h"
  53. /*
  54. * Preferred image size in bytes (tunable via /sys/power/image_size).
  55. * When it is set to N, swsusp will do its best to ensure the image
  56. * size will not exceed N bytes, but if that is impossible, it will
  57. * try to create the smallest image possible.
  58. */
  59. unsigned long image_size = 500 * 1024 * 1024;
  60. int in_suspend __nosavedata = 0;
  61. #ifdef CONFIG_HIGHMEM
  62. unsigned int count_highmem_pages(void);
  63. int restore_highmem(void);
  64. #else
  65. static inline int restore_highmem(void) { return 0; }
  66. static inline unsigned int count_highmem_pages(void) { return 0; }
  67. #endif
  68. /**
  69. * The following functions are used for tracing the allocated
  70. * swap pages, so that they can be freed in case of an error.
  71. *
  72. * The functions operate on a linked bitmap structure defined
  73. * in power.h
  74. */
  75. void free_bitmap(struct bitmap_page *bitmap)
  76. {
  77. struct bitmap_page *bp;
  78. while (bitmap) {
  79. bp = bitmap->next;
  80. free_page((unsigned long)bitmap);
  81. bitmap = bp;
  82. }
  83. }
  84. struct bitmap_page *alloc_bitmap(unsigned int nr_bits)
  85. {
  86. struct bitmap_page *bitmap, *bp;
  87. unsigned int n;
  88. if (!nr_bits)
  89. return NULL;
  90. bitmap = (struct bitmap_page *)get_zeroed_page(GFP_KERNEL);
  91. bp = bitmap;
  92. for (n = BITMAP_PAGE_BITS; n < nr_bits; n += BITMAP_PAGE_BITS) {
  93. bp->next = (struct bitmap_page *)get_zeroed_page(GFP_KERNEL);
  94. bp = bp->next;
  95. if (!bp) {
  96. free_bitmap(bitmap);
  97. return NULL;
  98. }
  99. }
  100. return bitmap;
  101. }
  102. static int bitmap_set(struct bitmap_page *bitmap, unsigned long bit)
  103. {
  104. unsigned int n;
  105. n = BITMAP_PAGE_BITS;
  106. while (bitmap && n <= bit) {
  107. n += BITMAP_PAGE_BITS;
  108. bitmap = bitmap->next;
  109. }
  110. if (!bitmap)
  111. return -EINVAL;
  112. n -= BITMAP_PAGE_BITS;
  113. bit -= n;
  114. n = 0;
  115. while (bit >= BITS_PER_CHUNK) {
  116. bit -= BITS_PER_CHUNK;
  117. n++;
  118. }
  119. bitmap->chunks[n] |= (1UL << bit);
  120. return 0;
  121. }
  122. sector_t alloc_swapdev_block(int swap, struct bitmap_page *bitmap)
  123. {
  124. unsigned long offset;
  125. offset = swp_offset(get_swap_page_of_type(swap));
  126. if (offset) {
  127. if (bitmap_set(bitmap, offset))
  128. swap_free(swp_entry(swap, offset));
  129. else
  130. return swapdev_block(swap, offset);
  131. }
  132. return 0;
  133. }
  134. void free_all_swap_pages(int swap, struct bitmap_page *bitmap)
  135. {
  136. unsigned int bit, n;
  137. unsigned long test;
  138. bit = 0;
  139. while (bitmap) {
  140. for (n = 0; n < BITMAP_PAGE_CHUNKS; n++)
  141. for (test = 1UL; test; test <<= 1) {
  142. if (bitmap->chunks[n] & test)
  143. swap_free(swp_entry(swap, bit));
  144. bit++;
  145. }
  146. bitmap = bitmap->next;
  147. }
  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("%s %d kbytes in %d.%02d seconds (%d.%02d MB/s)\n", 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("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;
  203. tmp = size;
  204. size += highmem_size;
  205. for_each_zone (zone)
  206. if (populated_zone(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. tmp += snapshot_additional_pages(zone);
  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. int swsusp_suspend(void)
  236. {
  237. int error;
  238. if ((error = arch_prepare_suspend()))
  239. return error;
  240. local_irq_disable();
  241. /* At this point, device_suspend() has been called, but *not*
  242. * device_power_down(). We *must* device_power_down() now.
  243. * Otherwise, drivers for some devices (e.g. interrupt controllers)
  244. * become desynchronized with the actual state of the hardware
  245. * at resume time, and evil weirdness ensues.
  246. */
  247. if ((error = device_power_down(PMSG_FREEZE))) {
  248. printk(KERN_ERR "Some devices failed to power down, aborting suspend\n");
  249. goto Enable_irqs;
  250. }
  251. save_processor_state();
  252. if ((error = swsusp_arch_suspend()))
  253. printk(KERN_ERR "Error %d suspending\n", error);
  254. /* Restore control flow magically appears here */
  255. restore_processor_state();
  256. /* NOTE: device_power_up() is just a resume() for devices
  257. * that suspended with irqs off ... no overall powerup.
  258. */
  259. device_power_up();
  260. Enable_irqs:
  261. local_irq_enable();
  262. return error;
  263. }
  264. int swsusp_resume(void)
  265. {
  266. int error;
  267. local_irq_disable();
  268. /* NOTE: device_power_down() is just a suspend() with irqs off;
  269. * it has no special "power things down" semantics
  270. */
  271. if (device_power_down(PMSG_PRETHAW))
  272. printk(KERN_ERR "Some devices failed to power down, very bad\n");
  273. /* We'll ignore saved state, but this gets preempt count (etc) right */
  274. save_processor_state();
  275. error = restore_highmem();
  276. if (!error) {
  277. error = swsusp_arch_resume();
  278. /* The code below is only ever reached in case of a failure.
  279. * Otherwise execution continues at place where
  280. * swsusp_arch_suspend() was called
  281. */
  282. BUG_ON(!error);
  283. /* This call to restore_highmem() undos the previous one */
  284. restore_highmem();
  285. }
  286. /* The only reason why swsusp_arch_resume() can fail is memory being
  287. * very tight, so we have to free it as soon as we can to avoid
  288. * subsequent failures
  289. */
  290. swsusp_free();
  291. restore_processor_state();
  292. touch_softlockup_watchdog();
  293. device_power_up();
  294. local_irq_enable();
  295. return error;
  296. }