swsusp.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 inline int save_highmem(void) { return 0; }
  66. static inline int restore_highmem(void) { return 0; }
  67. static inline 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. static inline unsigned long __shrink_memory(long tmp)
  160. {
  161. if (tmp > SHRINK_BITE)
  162. tmp = SHRINK_BITE;
  163. return shrink_all_memory(tmp);
  164. }
  165. int swsusp_shrink_memory(void)
  166. {
  167. long size, tmp;
  168. struct zone *zone;
  169. unsigned long pages = 0;
  170. unsigned int i = 0;
  171. char *p = "-\\|/";
  172. printk("Shrinking memory... ");
  173. do {
  174. size = 2 * count_highmem_pages();
  175. size += size / 50 + count_data_pages();
  176. size += (size + PBES_PER_PAGE - 1) / PBES_PER_PAGE +
  177. PAGES_FOR_IO;
  178. tmp = size;
  179. for_each_zone (zone)
  180. if (!is_highmem(zone) && populated_zone(zone)) {
  181. tmp -= zone->free_pages;
  182. tmp += zone->lowmem_reserve[ZONE_NORMAL];
  183. }
  184. if (tmp > 0) {
  185. tmp = __shrink_memory(tmp);
  186. if (!tmp)
  187. return -ENOMEM;
  188. pages += tmp;
  189. } else if (size > image_size / PAGE_SIZE) {
  190. tmp = __shrink_memory(size - (image_size / PAGE_SIZE));
  191. pages += tmp;
  192. }
  193. printk("\b%c", p[i++%4]);
  194. } while (tmp > 0);
  195. printk("\bdone (%lu pages freed)\n", pages);
  196. return 0;
  197. }
  198. int swsusp_suspend(void)
  199. {
  200. int error;
  201. if ((error = arch_prepare_suspend()))
  202. return error;
  203. local_irq_disable();
  204. /* At this point, device_suspend() has been called, but *not*
  205. * device_power_down(). We *must* device_power_down() now.
  206. * Otherwise, drivers for some devices (e.g. interrupt controllers)
  207. * become desynchronized with the actual state of the hardware
  208. * at resume time, and evil weirdness ensues.
  209. */
  210. if ((error = device_power_down(PMSG_FREEZE))) {
  211. printk(KERN_ERR "Some devices failed to power down, aborting suspend\n");
  212. goto Enable_irqs;
  213. }
  214. if ((error = save_highmem())) {
  215. printk(KERN_ERR "swsusp: Not enough free pages for highmem\n");
  216. goto Restore_highmem;
  217. }
  218. save_processor_state();
  219. if ((error = swsusp_arch_suspend()))
  220. printk(KERN_ERR "Error %d suspending\n", error);
  221. /* Restore control flow magically appears here */
  222. restore_processor_state();
  223. Restore_highmem:
  224. restore_highmem();
  225. device_power_up();
  226. Enable_irqs:
  227. local_irq_enable();
  228. return error;
  229. }
  230. int swsusp_resume(void)
  231. {
  232. int error;
  233. local_irq_disable();
  234. if (device_power_down(PMSG_FREEZE))
  235. printk(KERN_ERR "Some devices failed to power down, very bad\n");
  236. /* We'll ignore saved state, but this gets preempt count (etc) right */
  237. save_processor_state();
  238. error = swsusp_arch_resume();
  239. /* Code below is only ever reached in case of failure. Otherwise
  240. * execution continues at place where swsusp_arch_suspend was called
  241. */
  242. BUG_ON(!error);
  243. /* The only reason why swsusp_arch_resume() can fail is memory being
  244. * very tight, so we have to free it as soon as we can to avoid
  245. * subsequent failures
  246. */
  247. swsusp_free();
  248. restore_processor_state();
  249. restore_highmem();
  250. touch_softlockup_watchdog();
  251. device_power_up();
  252. local_irq_enable();
  253. return error;
  254. }