swsusp.c 6.6 KB

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