swsusp.c 7.1 KB

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