swsusp.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 "power.h"
  52. /*
  53. * Preferred image size in bytes (tunable via /sys/power/image_size).
  54. * When it is set to N, swsusp will do its best to ensure the image
  55. * size will not exceed N bytes, but if that is impossible, it will
  56. * try to create the smallest image possible.
  57. */
  58. unsigned long image_size = 500 * 1024 * 1024;
  59. int in_suspend __nosavedata = 0;
  60. #ifdef CONFIG_HIGHMEM
  61. unsigned int count_highmem_pages(void);
  62. int save_highmem(void);
  63. int restore_highmem(void);
  64. #else
  65. static int save_highmem(void) { return 0; }
  66. static int restore_highmem(void) { return 0; }
  67. static unsigned int count_highmem_pages(void) { return 0; }
  68. #endif
  69. /**
  70. * The following functions are used for tracing the allocated
  71. * swap pages, so that they can be freed in case of an error.
  72. *
  73. * The functions operate on a linked bitmap structure defined
  74. * in power.h
  75. */
  76. void free_bitmap(struct bitmap_page *bitmap)
  77. {
  78. struct bitmap_page *bp;
  79. while (bitmap) {
  80. bp = bitmap->next;
  81. free_page((unsigned long)bitmap);
  82. bitmap = bp;
  83. }
  84. }
  85. struct bitmap_page *alloc_bitmap(unsigned int nr_bits)
  86. {
  87. struct bitmap_page *bitmap, *bp;
  88. unsigned int n;
  89. if (!nr_bits)
  90. return NULL;
  91. bitmap = (struct bitmap_page *)get_zeroed_page(GFP_KERNEL);
  92. bp = bitmap;
  93. for (n = BITMAP_PAGE_BITS; n < nr_bits; n += BITMAP_PAGE_BITS) {
  94. bp->next = (struct bitmap_page *)get_zeroed_page(GFP_KERNEL);
  95. bp = bp->next;
  96. if (!bp) {
  97. free_bitmap(bitmap);
  98. return NULL;
  99. }
  100. }
  101. return bitmap;
  102. }
  103. static int bitmap_set(struct bitmap_page *bitmap, unsigned long bit)
  104. {
  105. unsigned int n;
  106. n = BITMAP_PAGE_BITS;
  107. while (bitmap && n <= bit) {
  108. n += BITMAP_PAGE_BITS;
  109. bitmap = bitmap->next;
  110. }
  111. if (!bitmap)
  112. return -EINVAL;
  113. n -= BITMAP_PAGE_BITS;
  114. bit -= n;
  115. n = 0;
  116. while (bit >= BITS_PER_CHUNK) {
  117. bit -= BITS_PER_CHUNK;
  118. n++;
  119. }
  120. bitmap->chunks[n] |= (1UL << bit);
  121. return 0;
  122. }
  123. unsigned long alloc_swap_page(int swap, struct bitmap_page *bitmap)
  124. {
  125. unsigned long offset;
  126. offset = swp_offset(get_swap_page_of_type(swap));
  127. if (offset) {
  128. if (bitmap_set(bitmap, offset)) {
  129. swap_free(swp_entry(swap, offset));
  130. offset = 0;
  131. }
  132. }
  133. return offset;
  134. }
  135. void free_all_swap_pages(int swap, struct bitmap_page *bitmap)
  136. {
  137. unsigned int bit, n;
  138. unsigned long test;
  139. bit = 0;
  140. while (bitmap) {
  141. for (n = 0; n < BITMAP_PAGE_CHUNKS; n++)
  142. for (test = 1UL; test; test <<= 1) {
  143. if (bitmap->chunks[n] & test)
  144. swap_free(swp_entry(swap, bit));
  145. bit++;
  146. }
  147. bitmap = bitmap->next;
  148. }
  149. }
  150. /**
  151. * swsusp_shrink_memory - Try to free as much memory as needed
  152. *
  153. * ... but do not OOM-kill anyone
  154. *
  155. * Notice: all userland should be stopped before it is called, or
  156. * livelock is possible.
  157. */
  158. #define SHRINK_BITE 10000
  159. int swsusp_shrink_memory(void)
  160. {
  161. long size, tmp;
  162. struct zone *zone;
  163. unsigned long pages = 0;
  164. unsigned int i = 0;
  165. char *p = "-\\|/";
  166. printk("Shrinking memory... ");
  167. do {
  168. size = 2 * count_highmem_pages();
  169. size += size / 50 + count_data_pages();
  170. size += (size + PBES_PER_PAGE - 1) / PBES_PER_PAGE +
  171. PAGES_FOR_IO;
  172. tmp = size;
  173. for_each_zone (zone)
  174. if (!is_highmem(zone))
  175. tmp -= zone->free_pages;
  176. if (tmp > 0) {
  177. tmp = shrink_all_memory(SHRINK_BITE);
  178. if (!tmp)
  179. return -ENOMEM;
  180. pages += tmp;
  181. } else if (size > image_size / PAGE_SIZE) {
  182. tmp = shrink_all_memory(SHRINK_BITE);
  183. pages += tmp;
  184. }
  185. printk("\b%c", p[i++%4]);
  186. } while (tmp > 0);
  187. printk("\bdone (%lu pages freed)\n", pages);
  188. return 0;
  189. }
  190. int swsusp_suspend(void)
  191. {
  192. int error;
  193. if ((error = arch_prepare_suspend()))
  194. return error;
  195. local_irq_disable();
  196. /* At this point, device_suspend() has been called, but *not*
  197. * device_power_down(). We *must* device_power_down() now.
  198. * Otherwise, drivers for some devices (e.g. interrupt controllers)
  199. * become desynchronized with the actual state of the hardware
  200. * at resume time, and evil weirdness ensues.
  201. */
  202. if ((error = device_power_down(PMSG_FREEZE))) {
  203. printk(KERN_ERR "Some devices failed to power down, aborting suspend\n");
  204. goto Enable_irqs;
  205. }
  206. if ((error = save_highmem())) {
  207. printk(KERN_ERR "swsusp: Not enough free pages for highmem\n");
  208. goto Restore_highmem;
  209. }
  210. save_processor_state();
  211. if ((error = swsusp_arch_suspend()))
  212. printk(KERN_ERR "Error %d suspending\n", error);
  213. /* Restore control flow magically appears here */
  214. restore_processor_state();
  215. Restore_highmem:
  216. restore_highmem();
  217. device_power_up();
  218. Enable_irqs:
  219. local_irq_enable();
  220. return error;
  221. }
  222. int swsusp_resume(void)
  223. {
  224. int error;
  225. local_irq_disable();
  226. if (device_power_down(PMSG_FREEZE))
  227. printk(KERN_ERR "Some devices failed to power down, very bad\n");
  228. /* We'll ignore saved state, but this gets preempt count (etc) right */
  229. save_processor_state();
  230. error = swsusp_arch_resume();
  231. /* Code below is only ever reached in case of failure. Otherwise
  232. * execution continues at place where swsusp_arch_suspend was called
  233. */
  234. BUG_ON(!error);
  235. /* The only reason why swsusp_arch_resume() can fail is memory being
  236. * very tight, so we have to free it as soon as we can to avoid
  237. * subsequent failures
  238. */
  239. swsusp_free();
  240. restore_processor_state();
  241. restore_highmem();
  242. touch_softlockup_watchdog();
  243. device_power_up();
  244. local_irq_enable();
  245. return error;
  246. }