swsusp.c 7.1 KB

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