snapshot.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  1. /*
  2. * linux/kernel/power/snapshot.c
  3. *
  4. * This file provide system snapshot/restore functionality.
  5. *
  6. * Copyright (C) 1998-2005 Pavel Machek <pavel@suse.cz>
  7. *
  8. * This file is released under the GPLv2, and is based on swsusp.c.
  9. *
  10. */
  11. #include <linux/version.h>
  12. #include <linux/module.h>
  13. #include <linux/mm.h>
  14. #include <linux/suspend.h>
  15. #include <linux/smp_lock.h>
  16. #include <linux/delay.h>
  17. #include <linux/bitops.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/kernel.h>
  20. #include <linux/pm.h>
  21. #include <linux/device.h>
  22. #include <linux/bootmem.h>
  23. #include <linux/syscalls.h>
  24. #include <linux/console.h>
  25. #include <linux/highmem.h>
  26. #include <asm/uaccess.h>
  27. #include <asm/mmu_context.h>
  28. #include <asm/pgtable.h>
  29. #include <asm/tlbflush.h>
  30. #include <asm/io.h>
  31. #include "power.h"
  32. /* List of PBEs used for creating and restoring the suspend image */
  33. struct pbe *restore_pblist;
  34. static unsigned int nr_copy_pages;
  35. static unsigned int nr_meta_pages;
  36. static void *buffer;
  37. #ifdef CONFIG_HIGHMEM
  38. unsigned int count_highmem_pages(void)
  39. {
  40. struct zone *zone;
  41. unsigned long zone_pfn;
  42. unsigned int n = 0;
  43. for_each_zone (zone)
  44. if (is_highmem(zone)) {
  45. mark_free_pages(zone);
  46. for (zone_pfn = 0; zone_pfn < zone->spanned_pages; zone_pfn++) {
  47. struct page *page;
  48. unsigned long pfn = zone_pfn + zone->zone_start_pfn;
  49. if (!pfn_valid(pfn))
  50. continue;
  51. page = pfn_to_page(pfn);
  52. if (PageReserved(page))
  53. continue;
  54. if (PageNosaveFree(page))
  55. continue;
  56. n++;
  57. }
  58. }
  59. return n;
  60. }
  61. struct highmem_page {
  62. char *data;
  63. struct page *page;
  64. struct highmem_page *next;
  65. };
  66. static struct highmem_page *highmem_copy;
  67. static int save_highmem_zone(struct zone *zone)
  68. {
  69. unsigned long zone_pfn;
  70. mark_free_pages(zone);
  71. for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn) {
  72. struct page *page;
  73. struct highmem_page *save;
  74. void *kaddr;
  75. unsigned long pfn = zone_pfn + zone->zone_start_pfn;
  76. if (!(pfn%10000))
  77. printk(".");
  78. if (!pfn_valid(pfn))
  79. continue;
  80. page = pfn_to_page(pfn);
  81. /*
  82. * This condition results from rvmalloc() sans vmalloc_32()
  83. * and architectural memory reservations. This should be
  84. * corrected eventually when the cases giving rise to this
  85. * are better understood.
  86. */
  87. if (PageReserved(page))
  88. continue;
  89. BUG_ON(PageNosave(page));
  90. if (PageNosaveFree(page))
  91. continue;
  92. save = kmalloc(sizeof(struct highmem_page), GFP_ATOMIC);
  93. if (!save)
  94. return -ENOMEM;
  95. save->next = highmem_copy;
  96. save->page = page;
  97. save->data = (void *) get_zeroed_page(GFP_ATOMIC);
  98. if (!save->data) {
  99. kfree(save);
  100. return -ENOMEM;
  101. }
  102. kaddr = kmap_atomic(page, KM_USER0);
  103. memcpy(save->data, kaddr, PAGE_SIZE);
  104. kunmap_atomic(kaddr, KM_USER0);
  105. highmem_copy = save;
  106. }
  107. return 0;
  108. }
  109. int save_highmem(void)
  110. {
  111. struct zone *zone;
  112. int res = 0;
  113. pr_debug("swsusp: Saving Highmem");
  114. drain_local_pages();
  115. for_each_zone (zone) {
  116. if (is_highmem(zone))
  117. res = save_highmem_zone(zone);
  118. if (res)
  119. return res;
  120. }
  121. printk("\n");
  122. return 0;
  123. }
  124. int restore_highmem(void)
  125. {
  126. printk("swsusp: Restoring Highmem\n");
  127. while (highmem_copy) {
  128. struct highmem_page *save = highmem_copy;
  129. void *kaddr;
  130. highmem_copy = save->next;
  131. kaddr = kmap_atomic(save->page, KM_USER0);
  132. memcpy(kaddr, save->data, PAGE_SIZE);
  133. kunmap_atomic(kaddr, KM_USER0);
  134. free_page((long) save->data);
  135. kfree(save);
  136. }
  137. return 0;
  138. }
  139. #else
  140. static inline unsigned int count_highmem_pages(void) {return 0;}
  141. static inline int save_highmem(void) {return 0;}
  142. static inline int restore_highmem(void) {return 0;}
  143. #endif
  144. /**
  145. * @safe_needed - on resume, for storing the PBE list and the image,
  146. * we can only use memory pages that do not conflict with the pages
  147. * used before suspend.
  148. *
  149. * The unsafe pages are marked with the PG_nosave_free flag
  150. * and we count them using unsafe_pages
  151. */
  152. #define PG_ANY 0
  153. #define PG_SAFE 1
  154. #define PG_UNSAFE_CLEAR 1
  155. #define PG_UNSAFE_KEEP 0
  156. static unsigned int allocated_unsafe_pages;
  157. static void *alloc_image_page(gfp_t gfp_mask, int safe_needed)
  158. {
  159. void *res;
  160. res = (void *)get_zeroed_page(gfp_mask);
  161. if (safe_needed)
  162. while (res && PageNosaveFree(virt_to_page(res))) {
  163. /* The page is unsafe, mark it for swsusp_free() */
  164. SetPageNosave(virt_to_page(res));
  165. allocated_unsafe_pages++;
  166. res = (void *)get_zeroed_page(gfp_mask);
  167. }
  168. if (res) {
  169. SetPageNosave(virt_to_page(res));
  170. SetPageNosaveFree(virt_to_page(res));
  171. }
  172. return res;
  173. }
  174. unsigned long get_safe_page(gfp_t gfp_mask)
  175. {
  176. return (unsigned long)alloc_image_page(gfp_mask, PG_SAFE);
  177. }
  178. /**
  179. * free_image_page - free page represented by @addr, allocated with
  180. * alloc_image_page (page flags set by it must be cleared)
  181. */
  182. static inline void free_image_page(void *addr, int clear_nosave_free)
  183. {
  184. ClearPageNosave(virt_to_page(addr));
  185. if (clear_nosave_free)
  186. ClearPageNosaveFree(virt_to_page(addr));
  187. free_page((unsigned long)addr);
  188. }
  189. /* struct linked_page is used to build chains of pages */
  190. #define LINKED_PAGE_DATA_SIZE (PAGE_SIZE - sizeof(void *))
  191. struct linked_page {
  192. struct linked_page *next;
  193. char data[LINKED_PAGE_DATA_SIZE];
  194. } __attribute__((packed));
  195. static inline void
  196. free_list_of_pages(struct linked_page *list, int clear_page_nosave)
  197. {
  198. while (list) {
  199. struct linked_page *lp = list->next;
  200. free_image_page(list, clear_page_nosave);
  201. list = lp;
  202. }
  203. }
  204. /**
  205. * struct chain_allocator is used for allocating small objects out of
  206. * a linked list of pages called 'the chain'.
  207. *
  208. * The chain grows each time when there is no room for a new object in
  209. * the current page. The allocated objects cannot be freed individually.
  210. * It is only possible to free them all at once, by freeing the entire
  211. * chain.
  212. *
  213. * NOTE: The chain allocator may be inefficient if the allocated objects
  214. * are not much smaller than PAGE_SIZE.
  215. */
  216. struct chain_allocator {
  217. struct linked_page *chain; /* the chain */
  218. unsigned int used_space; /* total size of objects allocated out
  219. * of the current page
  220. */
  221. gfp_t gfp_mask; /* mask for allocating pages */
  222. int safe_needed; /* if set, only "safe" pages are allocated */
  223. };
  224. static void
  225. chain_init(struct chain_allocator *ca, gfp_t gfp_mask, int safe_needed)
  226. {
  227. ca->chain = NULL;
  228. ca->used_space = LINKED_PAGE_DATA_SIZE;
  229. ca->gfp_mask = gfp_mask;
  230. ca->safe_needed = safe_needed;
  231. }
  232. static void *chain_alloc(struct chain_allocator *ca, unsigned int size)
  233. {
  234. void *ret;
  235. if (LINKED_PAGE_DATA_SIZE - ca->used_space < size) {
  236. struct linked_page *lp;
  237. lp = alloc_image_page(ca->gfp_mask, ca->safe_needed);
  238. if (!lp)
  239. return NULL;
  240. lp->next = ca->chain;
  241. ca->chain = lp;
  242. ca->used_space = 0;
  243. }
  244. ret = ca->chain->data + ca->used_space;
  245. ca->used_space += size;
  246. return ret;
  247. }
  248. static void chain_free(struct chain_allocator *ca, int clear_page_nosave)
  249. {
  250. free_list_of_pages(ca->chain, clear_page_nosave);
  251. memset(ca, 0, sizeof(struct chain_allocator));
  252. }
  253. /**
  254. * Data types related to memory bitmaps.
  255. *
  256. * Memory bitmap is a structure consiting of many linked lists of
  257. * objects. The main list's elements are of type struct zone_bitmap
  258. * and each of them corresonds to one zone. For each zone bitmap
  259. * object there is a list of objects of type struct bm_block that
  260. * represent each blocks of bit chunks in which information is
  261. * stored.
  262. *
  263. * struct memory_bitmap contains a pointer to the main list of zone
  264. * bitmap objects, a struct bm_position used for browsing the bitmap,
  265. * and a pointer to the list of pages used for allocating all of the
  266. * zone bitmap objects and bitmap block objects.
  267. *
  268. * NOTE: It has to be possible to lay out the bitmap in memory
  269. * using only allocations of order 0. Additionally, the bitmap is
  270. * designed to work with arbitrary number of zones (this is over the
  271. * top for now, but let's avoid making unnecessary assumptions ;-).
  272. *
  273. * struct zone_bitmap contains a pointer to a list of bitmap block
  274. * objects and a pointer to the bitmap block object that has been
  275. * most recently used for setting bits. Additionally, it contains the
  276. * pfns that correspond to the start and end of the represented zone.
  277. *
  278. * struct bm_block contains a pointer to the memory page in which
  279. * information is stored (in the form of a block of bit chunks
  280. * of type unsigned long each). It also contains the pfns that
  281. * correspond to the start and end of the represented memory area and
  282. * the number of bit chunks in the block.
  283. *
  284. * NOTE: Memory bitmaps are used for two types of operations only:
  285. * "set a bit" and "find the next bit set". Moreover, the searching
  286. * is always carried out after all of the "set a bit" operations
  287. * on given bitmap.
  288. */
  289. #define BM_END_OF_MAP (~0UL)
  290. #define BM_CHUNKS_PER_BLOCK (PAGE_SIZE / sizeof(long))
  291. #define BM_BITS_PER_CHUNK (sizeof(long) << 3)
  292. #define BM_BITS_PER_BLOCK (PAGE_SIZE << 3)
  293. struct bm_block {
  294. struct bm_block *next; /* next element of the list */
  295. unsigned long start_pfn; /* pfn represented by the first bit */
  296. unsigned long end_pfn; /* pfn represented by the last bit plus 1 */
  297. unsigned int size; /* number of bit chunks */
  298. unsigned long *data; /* chunks of bits representing pages */
  299. };
  300. struct zone_bitmap {
  301. struct zone_bitmap *next; /* next element of the list */
  302. unsigned long start_pfn; /* minimal pfn in this zone */
  303. unsigned long end_pfn; /* maximal pfn in this zone plus 1 */
  304. struct bm_block *bm_blocks; /* list of bitmap blocks */
  305. struct bm_block *cur_block; /* recently used bitmap block */
  306. };
  307. /* strcut bm_position is used for browsing memory bitmaps */
  308. struct bm_position {
  309. struct zone_bitmap *zone_bm;
  310. struct bm_block *block;
  311. int chunk;
  312. int bit;
  313. };
  314. struct memory_bitmap {
  315. struct zone_bitmap *zone_bm_list; /* list of zone bitmaps */
  316. struct linked_page *p_list; /* list of pages used to store zone
  317. * bitmap objects and bitmap block
  318. * objects
  319. */
  320. struct bm_position cur; /* most recently used bit position */
  321. };
  322. /* Functions that operate on memory bitmaps */
  323. static inline void memory_bm_reset_chunk(struct memory_bitmap *bm)
  324. {
  325. bm->cur.chunk = 0;
  326. bm->cur.bit = -1;
  327. }
  328. static void memory_bm_position_reset(struct memory_bitmap *bm)
  329. {
  330. struct zone_bitmap *zone_bm;
  331. zone_bm = bm->zone_bm_list;
  332. bm->cur.zone_bm = zone_bm;
  333. bm->cur.block = zone_bm->bm_blocks;
  334. memory_bm_reset_chunk(bm);
  335. }
  336. static void memory_bm_free(struct memory_bitmap *bm, int clear_nosave_free);
  337. /**
  338. * create_bm_block_list - create a list of block bitmap objects
  339. */
  340. static inline struct bm_block *
  341. create_bm_block_list(unsigned int nr_blocks, struct chain_allocator *ca)
  342. {
  343. struct bm_block *bblist = NULL;
  344. while (nr_blocks-- > 0) {
  345. struct bm_block *bb;
  346. bb = chain_alloc(ca, sizeof(struct bm_block));
  347. if (!bb)
  348. return NULL;
  349. bb->next = bblist;
  350. bblist = bb;
  351. }
  352. return bblist;
  353. }
  354. /**
  355. * create_zone_bm_list - create a list of zone bitmap objects
  356. */
  357. static inline struct zone_bitmap *
  358. create_zone_bm_list(unsigned int nr_zones, struct chain_allocator *ca)
  359. {
  360. struct zone_bitmap *zbmlist = NULL;
  361. while (nr_zones-- > 0) {
  362. struct zone_bitmap *zbm;
  363. zbm = chain_alloc(ca, sizeof(struct zone_bitmap));
  364. if (!zbm)
  365. return NULL;
  366. zbm->next = zbmlist;
  367. zbmlist = zbm;
  368. }
  369. return zbmlist;
  370. }
  371. /**
  372. * memory_bm_create - allocate memory for a memory bitmap
  373. */
  374. static int
  375. memory_bm_create(struct memory_bitmap *bm, gfp_t gfp_mask, int safe_needed)
  376. {
  377. struct chain_allocator ca;
  378. struct zone *zone;
  379. struct zone_bitmap *zone_bm;
  380. struct bm_block *bb;
  381. unsigned int nr;
  382. chain_init(&ca, gfp_mask, safe_needed);
  383. /* Compute the number of zones */
  384. nr = 0;
  385. for_each_zone (zone)
  386. if (populated_zone(zone) && !is_highmem(zone))
  387. nr++;
  388. /* Allocate the list of zones bitmap objects */
  389. zone_bm = create_zone_bm_list(nr, &ca);
  390. bm->zone_bm_list = zone_bm;
  391. if (!zone_bm) {
  392. chain_free(&ca, PG_UNSAFE_CLEAR);
  393. return -ENOMEM;
  394. }
  395. /* Initialize the zone bitmap objects */
  396. for_each_zone (zone) {
  397. unsigned long pfn;
  398. if (!populated_zone(zone) || is_highmem(zone))
  399. continue;
  400. zone_bm->start_pfn = zone->zone_start_pfn;
  401. zone_bm->end_pfn = zone->zone_start_pfn + zone->spanned_pages;
  402. /* Allocate the list of bitmap block objects */
  403. nr = DIV_ROUND_UP(zone->spanned_pages, BM_BITS_PER_BLOCK);
  404. bb = create_bm_block_list(nr, &ca);
  405. zone_bm->bm_blocks = bb;
  406. zone_bm->cur_block = bb;
  407. if (!bb)
  408. goto Free;
  409. nr = zone->spanned_pages;
  410. pfn = zone->zone_start_pfn;
  411. /* Initialize the bitmap block objects */
  412. while (bb) {
  413. unsigned long *ptr;
  414. ptr = alloc_image_page(gfp_mask, safe_needed);
  415. bb->data = ptr;
  416. if (!ptr)
  417. goto Free;
  418. bb->start_pfn = pfn;
  419. if (nr >= BM_BITS_PER_BLOCK) {
  420. pfn += BM_BITS_PER_BLOCK;
  421. bb->size = BM_CHUNKS_PER_BLOCK;
  422. nr -= BM_BITS_PER_BLOCK;
  423. } else {
  424. /* This is executed only once in the loop */
  425. pfn += nr;
  426. bb->size = DIV_ROUND_UP(nr, BM_BITS_PER_CHUNK);
  427. }
  428. bb->end_pfn = pfn;
  429. bb = bb->next;
  430. }
  431. zone_bm = zone_bm->next;
  432. }
  433. bm->p_list = ca.chain;
  434. memory_bm_position_reset(bm);
  435. return 0;
  436. Free:
  437. bm->p_list = ca.chain;
  438. memory_bm_free(bm, PG_UNSAFE_CLEAR);
  439. return -ENOMEM;
  440. }
  441. /**
  442. * memory_bm_free - free memory occupied by the memory bitmap @bm
  443. */
  444. static void memory_bm_free(struct memory_bitmap *bm, int clear_nosave_free)
  445. {
  446. struct zone_bitmap *zone_bm;
  447. /* Free the list of bit blocks for each zone_bitmap object */
  448. zone_bm = bm->zone_bm_list;
  449. while (zone_bm) {
  450. struct bm_block *bb;
  451. bb = zone_bm->bm_blocks;
  452. while (bb) {
  453. if (bb->data)
  454. free_image_page(bb->data, clear_nosave_free);
  455. bb = bb->next;
  456. }
  457. zone_bm = zone_bm->next;
  458. }
  459. free_list_of_pages(bm->p_list, clear_nosave_free);
  460. bm->zone_bm_list = NULL;
  461. }
  462. /**
  463. * memory_bm_set_bit - set the bit in the bitmap @bm that corresponds
  464. * to given pfn. The cur_zone_bm member of @bm and the cur_block member
  465. * of @bm->cur_zone_bm are updated.
  466. *
  467. * If the bit cannot be set, the function returns -EINVAL .
  468. */
  469. static int
  470. memory_bm_set_bit(struct memory_bitmap *bm, unsigned long pfn)
  471. {
  472. struct zone_bitmap *zone_bm;
  473. struct bm_block *bb;
  474. /* Check if the pfn is from the current zone */
  475. zone_bm = bm->cur.zone_bm;
  476. if (pfn < zone_bm->start_pfn || pfn >= zone_bm->end_pfn) {
  477. zone_bm = bm->zone_bm_list;
  478. /* We don't assume that the zones are sorted by pfns */
  479. while (pfn < zone_bm->start_pfn || pfn >= zone_bm->end_pfn) {
  480. zone_bm = zone_bm->next;
  481. if (unlikely(!zone_bm))
  482. return -EINVAL;
  483. }
  484. bm->cur.zone_bm = zone_bm;
  485. }
  486. /* Check if the pfn corresponds to the current bitmap block */
  487. bb = zone_bm->cur_block;
  488. if (pfn < bb->start_pfn)
  489. bb = zone_bm->bm_blocks;
  490. while (pfn >= bb->end_pfn) {
  491. bb = bb->next;
  492. if (unlikely(!bb))
  493. return -EINVAL;
  494. }
  495. zone_bm->cur_block = bb;
  496. pfn -= bb->start_pfn;
  497. set_bit(pfn % BM_BITS_PER_CHUNK, bb->data + pfn / BM_BITS_PER_CHUNK);
  498. return 0;
  499. }
  500. /* Two auxiliary functions for memory_bm_next_pfn */
  501. /* Find the first set bit in the given chunk, if there is one */
  502. static inline int next_bit_in_chunk(int bit, unsigned long *chunk_p)
  503. {
  504. bit++;
  505. while (bit < BM_BITS_PER_CHUNK) {
  506. if (test_bit(bit, chunk_p))
  507. return bit;
  508. bit++;
  509. }
  510. return -1;
  511. }
  512. /* Find a chunk containing some bits set in given block of bits */
  513. static inline int next_chunk_in_block(int n, struct bm_block *bb)
  514. {
  515. n++;
  516. while (n < bb->size) {
  517. if (bb->data[n])
  518. return n;
  519. n++;
  520. }
  521. return -1;
  522. }
  523. /**
  524. * memory_bm_next_pfn - find the pfn that corresponds to the next set bit
  525. * in the bitmap @bm. If the pfn cannot be found, BM_END_OF_MAP is
  526. * returned.
  527. *
  528. * It is required to run memory_bm_position_reset() before the first call to
  529. * this function.
  530. */
  531. static unsigned long memory_bm_next_pfn(struct memory_bitmap *bm)
  532. {
  533. struct zone_bitmap *zone_bm;
  534. struct bm_block *bb;
  535. int chunk;
  536. int bit;
  537. do {
  538. bb = bm->cur.block;
  539. do {
  540. chunk = bm->cur.chunk;
  541. bit = bm->cur.bit;
  542. do {
  543. bit = next_bit_in_chunk(bit, bb->data + chunk);
  544. if (bit >= 0)
  545. goto Return_pfn;
  546. chunk = next_chunk_in_block(chunk, bb);
  547. bit = -1;
  548. } while (chunk >= 0);
  549. bb = bb->next;
  550. bm->cur.block = bb;
  551. memory_bm_reset_chunk(bm);
  552. } while (bb);
  553. zone_bm = bm->cur.zone_bm->next;
  554. if (zone_bm) {
  555. bm->cur.zone_bm = zone_bm;
  556. bm->cur.block = zone_bm->bm_blocks;
  557. memory_bm_reset_chunk(bm);
  558. }
  559. } while (zone_bm);
  560. memory_bm_position_reset(bm);
  561. return BM_END_OF_MAP;
  562. Return_pfn:
  563. bm->cur.chunk = chunk;
  564. bm->cur.bit = bit;
  565. return bb->start_pfn + chunk * BM_BITS_PER_CHUNK + bit;
  566. }
  567. /**
  568. * snapshot_additional_pages - estimate the number of additional pages
  569. * be needed for setting up the suspend image data structures for given
  570. * zone (usually the returned value is greater than the exact number)
  571. */
  572. unsigned int snapshot_additional_pages(struct zone *zone)
  573. {
  574. unsigned int res;
  575. res = DIV_ROUND_UP(zone->spanned_pages, BM_BITS_PER_BLOCK);
  576. res += DIV_ROUND_UP(res * sizeof(struct bm_block), PAGE_SIZE);
  577. return res;
  578. }
  579. /**
  580. * pfn_is_nosave - check if given pfn is in the 'nosave' section
  581. */
  582. static inline int pfn_is_nosave(unsigned long pfn)
  583. {
  584. unsigned long nosave_begin_pfn = __pa(&__nosave_begin) >> PAGE_SHIFT;
  585. unsigned long nosave_end_pfn = PAGE_ALIGN(__pa(&__nosave_end)) >> PAGE_SHIFT;
  586. return (pfn >= nosave_begin_pfn) && (pfn < nosave_end_pfn);
  587. }
  588. /**
  589. * saveable - Determine whether a page should be cloned or not.
  590. * @pfn: The page
  591. *
  592. * We save a page if it isn't Nosave, and is not in the range of pages
  593. * statically defined as 'unsaveable', and it
  594. * isn't a part of a free chunk of pages.
  595. */
  596. static struct page *saveable_page(unsigned long pfn)
  597. {
  598. struct page *page;
  599. if (!pfn_valid(pfn))
  600. return NULL;
  601. page = pfn_to_page(pfn);
  602. if (PageNosave(page))
  603. return NULL;
  604. if (PageReserved(page) && pfn_is_nosave(pfn))
  605. return NULL;
  606. if (PageNosaveFree(page))
  607. return NULL;
  608. return page;
  609. }
  610. unsigned int count_data_pages(void)
  611. {
  612. struct zone *zone;
  613. unsigned long pfn, max_zone_pfn;
  614. unsigned int n = 0;
  615. for_each_zone (zone) {
  616. if (is_highmem(zone))
  617. continue;
  618. mark_free_pages(zone);
  619. max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
  620. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
  621. n += !!saveable_page(pfn);
  622. }
  623. return n;
  624. }
  625. static inline void copy_data_page(long *dst, long *src)
  626. {
  627. int n;
  628. /* copy_page and memcpy are not usable for copying task structs. */
  629. for (n = PAGE_SIZE / sizeof(long); n; n--)
  630. *dst++ = *src++;
  631. }
  632. static void
  633. copy_data_pages(struct memory_bitmap *copy_bm, struct memory_bitmap *orig_bm)
  634. {
  635. struct zone *zone;
  636. unsigned long pfn;
  637. for_each_zone (zone) {
  638. unsigned long max_zone_pfn;
  639. if (is_highmem(zone))
  640. continue;
  641. mark_free_pages(zone);
  642. max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
  643. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
  644. if (saveable_page(pfn))
  645. memory_bm_set_bit(orig_bm, pfn);
  646. }
  647. memory_bm_position_reset(orig_bm);
  648. memory_bm_position_reset(copy_bm);
  649. do {
  650. pfn = memory_bm_next_pfn(orig_bm);
  651. if (likely(pfn != BM_END_OF_MAP)) {
  652. struct page *page;
  653. void *src;
  654. page = pfn_to_page(pfn);
  655. src = page_address(page);
  656. page = pfn_to_page(memory_bm_next_pfn(copy_bm));
  657. copy_data_page(page_address(page), src);
  658. }
  659. } while (pfn != BM_END_OF_MAP);
  660. }
  661. /**
  662. * swsusp_free - free pages allocated for the suspend.
  663. *
  664. * Suspend pages are alocated before the atomic copy is made, so we
  665. * need to release them after the resume.
  666. */
  667. void swsusp_free(void)
  668. {
  669. struct zone *zone;
  670. unsigned long pfn, max_zone_pfn;
  671. for_each_zone(zone) {
  672. max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
  673. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
  674. if (pfn_valid(pfn)) {
  675. struct page *page = pfn_to_page(pfn);
  676. if (PageNosave(page) && PageNosaveFree(page)) {
  677. ClearPageNosave(page);
  678. ClearPageNosaveFree(page);
  679. free_page((long) page_address(page));
  680. }
  681. }
  682. }
  683. nr_copy_pages = 0;
  684. nr_meta_pages = 0;
  685. restore_pblist = NULL;
  686. buffer = NULL;
  687. }
  688. /**
  689. * enough_free_mem - Make sure we enough free memory to snapshot.
  690. *
  691. * Returns TRUE or FALSE after checking the number of available
  692. * free pages.
  693. */
  694. static int enough_free_mem(unsigned int nr_pages)
  695. {
  696. struct zone *zone;
  697. unsigned int free = 0, meta = 0;
  698. for_each_zone (zone)
  699. if (!is_highmem(zone)) {
  700. free += zone->free_pages;
  701. meta += snapshot_additional_pages(zone);
  702. }
  703. pr_debug("swsusp: pages needed: %u + %u + %u, available pages: %u\n",
  704. nr_pages, PAGES_FOR_IO, meta, free);
  705. return free > nr_pages + PAGES_FOR_IO + meta;
  706. }
  707. static int
  708. swsusp_alloc(struct memory_bitmap *orig_bm, struct memory_bitmap *copy_bm,
  709. unsigned int nr_pages)
  710. {
  711. int error;
  712. error = memory_bm_create(orig_bm, GFP_ATOMIC | __GFP_COLD, PG_ANY);
  713. if (error)
  714. goto Free;
  715. error = memory_bm_create(copy_bm, GFP_ATOMIC | __GFP_COLD, PG_ANY);
  716. if (error)
  717. goto Free;
  718. while (nr_pages-- > 0) {
  719. struct page *page = alloc_page(GFP_ATOMIC | __GFP_COLD);
  720. if (!page)
  721. goto Free;
  722. SetPageNosave(page);
  723. SetPageNosaveFree(page);
  724. memory_bm_set_bit(copy_bm, page_to_pfn(page));
  725. }
  726. return 0;
  727. Free:
  728. swsusp_free();
  729. return -ENOMEM;
  730. }
  731. /* Memory bitmap used for marking saveable pages */
  732. static struct memory_bitmap orig_bm;
  733. /* Memory bitmap used for marking allocated pages that will contain the copies
  734. * of saveable pages
  735. */
  736. static struct memory_bitmap copy_bm;
  737. asmlinkage int swsusp_save(void)
  738. {
  739. unsigned int nr_pages;
  740. pr_debug("swsusp: critical section: \n");
  741. drain_local_pages();
  742. nr_pages = count_data_pages();
  743. printk("swsusp: Need to copy %u pages\n", nr_pages);
  744. if (!enough_free_mem(nr_pages)) {
  745. printk(KERN_ERR "swsusp: Not enough free memory\n");
  746. return -ENOMEM;
  747. }
  748. if (swsusp_alloc(&orig_bm, &copy_bm, nr_pages))
  749. return -ENOMEM;
  750. /* During allocating of suspend pagedir, new cold pages may appear.
  751. * Kill them.
  752. */
  753. drain_local_pages();
  754. copy_data_pages(&copy_bm, &orig_bm);
  755. /*
  756. * End of critical section. From now on, we can write to memory,
  757. * but we should not touch disk. This specially means we must _not_
  758. * touch swap space! Except we must write out our image of course.
  759. */
  760. nr_copy_pages = nr_pages;
  761. nr_meta_pages = (nr_pages * sizeof(long) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  762. printk("swsusp: critical section/: done (%d pages copied)\n", nr_pages);
  763. return 0;
  764. }
  765. static void init_header(struct swsusp_info *info)
  766. {
  767. memset(info, 0, sizeof(struct swsusp_info));
  768. info->version_code = LINUX_VERSION_CODE;
  769. info->num_physpages = num_physpages;
  770. memcpy(&info->uts, &system_utsname, sizeof(system_utsname));
  771. info->cpus = num_online_cpus();
  772. info->image_pages = nr_copy_pages;
  773. info->pages = nr_copy_pages + nr_meta_pages + 1;
  774. info->size = info->pages;
  775. info->size <<= PAGE_SHIFT;
  776. }
  777. /**
  778. * pack_pfns - pfns corresponding to the set bits found in the bitmap @bm
  779. * are stored in the array @buf[] (1 page at a time)
  780. */
  781. static inline void
  782. pack_pfns(unsigned long *buf, struct memory_bitmap *bm)
  783. {
  784. int j;
  785. for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
  786. buf[j] = memory_bm_next_pfn(bm);
  787. if (unlikely(buf[j] == BM_END_OF_MAP))
  788. break;
  789. }
  790. }
  791. /**
  792. * snapshot_read_next - used for reading the system memory snapshot.
  793. *
  794. * On the first call to it @handle should point to a zeroed
  795. * snapshot_handle structure. The structure gets updated and a pointer
  796. * to it should be passed to this function every next time.
  797. *
  798. * The @count parameter should contain the number of bytes the caller
  799. * wants to read from the snapshot. It must not be zero.
  800. *
  801. * On success the function returns a positive number. Then, the caller
  802. * is allowed to read up to the returned number of bytes from the memory
  803. * location computed by the data_of() macro. The number returned
  804. * may be smaller than @count, but this only happens if the read would
  805. * cross a page boundary otherwise.
  806. *
  807. * The function returns 0 to indicate the end of data stream condition,
  808. * and a negative number is returned on error. In such cases the
  809. * structure pointed to by @handle is not updated and should not be used
  810. * any more.
  811. */
  812. int snapshot_read_next(struct snapshot_handle *handle, size_t count)
  813. {
  814. if (handle->cur > nr_meta_pages + nr_copy_pages)
  815. return 0;
  816. if (!buffer) {
  817. /* This makes the buffer be freed by swsusp_free() */
  818. buffer = alloc_image_page(GFP_ATOMIC, PG_ANY);
  819. if (!buffer)
  820. return -ENOMEM;
  821. }
  822. if (!handle->offset) {
  823. init_header((struct swsusp_info *)buffer);
  824. handle->buffer = buffer;
  825. memory_bm_position_reset(&orig_bm);
  826. memory_bm_position_reset(&copy_bm);
  827. }
  828. if (handle->prev < handle->cur) {
  829. if (handle->cur <= nr_meta_pages) {
  830. memset(buffer, 0, PAGE_SIZE);
  831. pack_pfns(buffer, &orig_bm);
  832. } else {
  833. unsigned long pfn = memory_bm_next_pfn(&copy_bm);
  834. handle->buffer = page_address(pfn_to_page(pfn));
  835. }
  836. handle->prev = handle->cur;
  837. }
  838. handle->buf_offset = handle->cur_offset;
  839. if (handle->cur_offset + count >= PAGE_SIZE) {
  840. count = PAGE_SIZE - handle->cur_offset;
  841. handle->cur_offset = 0;
  842. handle->cur++;
  843. } else {
  844. handle->cur_offset += count;
  845. }
  846. handle->offset += count;
  847. return count;
  848. }
  849. /**
  850. * mark_unsafe_pages - mark the pages that cannot be used for storing
  851. * the image during resume, because they conflict with the pages that
  852. * had been used before suspend
  853. */
  854. static int mark_unsafe_pages(struct memory_bitmap *bm)
  855. {
  856. struct zone *zone;
  857. unsigned long pfn, max_zone_pfn;
  858. /* Clear page flags */
  859. for_each_zone (zone) {
  860. max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
  861. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
  862. if (pfn_valid(pfn))
  863. ClearPageNosaveFree(pfn_to_page(pfn));
  864. }
  865. /* Mark pages that correspond to the "original" pfns as "unsafe" */
  866. memory_bm_position_reset(bm);
  867. do {
  868. pfn = memory_bm_next_pfn(bm);
  869. if (likely(pfn != BM_END_OF_MAP)) {
  870. if (likely(pfn_valid(pfn)))
  871. SetPageNosaveFree(pfn_to_page(pfn));
  872. else
  873. return -EFAULT;
  874. }
  875. } while (pfn != BM_END_OF_MAP);
  876. allocated_unsafe_pages = 0;
  877. return 0;
  878. }
  879. static void
  880. duplicate_memory_bitmap(struct memory_bitmap *dst, struct memory_bitmap *src)
  881. {
  882. unsigned long pfn;
  883. memory_bm_position_reset(src);
  884. pfn = memory_bm_next_pfn(src);
  885. while (pfn != BM_END_OF_MAP) {
  886. memory_bm_set_bit(dst, pfn);
  887. pfn = memory_bm_next_pfn(src);
  888. }
  889. }
  890. static inline int check_header(struct swsusp_info *info)
  891. {
  892. char *reason = NULL;
  893. if (info->version_code != LINUX_VERSION_CODE)
  894. reason = "kernel version";
  895. if (info->num_physpages != num_physpages)
  896. reason = "memory size";
  897. if (strcmp(info->uts.sysname,system_utsname.sysname))
  898. reason = "system type";
  899. if (strcmp(info->uts.release,system_utsname.release))
  900. reason = "kernel release";
  901. if (strcmp(info->uts.version,system_utsname.version))
  902. reason = "version";
  903. if (strcmp(info->uts.machine,system_utsname.machine))
  904. reason = "machine";
  905. if (reason) {
  906. printk(KERN_ERR "swsusp: Resume mismatch: %s\n", reason);
  907. return -EPERM;
  908. }
  909. return 0;
  910. }
  911. /**
  912. * load header - check the image header and copy data from it
  913. */
  914. static int
  915. load_header(struct swsusp_info *info)
  916. {
  917. int error;
  918. restore_pblist = NULL;
  919. error = check_header(info);
  920. if (!error) {
  921. nr_copy_pages = info->image_pages;
  922. nr_meta_pages = info->pages - info->image_pages - 1;
  923. }
  924. return error;
  925. }
  926. /**
  927. * unpack_orig_pfns - for each element of @buf[] (1 page at a time) set
  928. * the corresponding bit in the memory bitmap @bm
  929. */
  930. static inline void
  931. unpack_orig_pfns(unsigned long *buf, struct memory_bitmap *bm)
  932. {
  933. int j;
  934. for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
  935. if (unlikely(buf[j] == BM_END_OF_MAP))
  936. break;
  937. memory_bm_set_bit(bm, buf[j]);
  938. }
  939. }
  940. /**
  941. * prepare_image - use the memory bitmap @bm to mark the pages that will
  942. * be overwritten in the process of restoring the system memory state
  943. * from the suspend image ("unsafe" pages) and allocate memory for the
  944. * image.
  945. *
  946. * The idea is to allocate a new memory bitmap first and then allocate
  947. * as many pages as needed for the image data, but not to assign these
  948. * pages to specific tasks initially. Instead, we just mark them as
  949. * allocated and create a list of "safe" pages that will be used later.
  950. */
  951. #define PBES_PER_LINKED_PAGE (LINKED_PAGE_DATA_SIZE / sizeof(struct pbe))
  952. static struct linked_page *safe_pages_list;
  953. static int
  954. prepare_image(struct memory_bitmap *new_bm, struct memory_bitmap *bm)
  955. {
  956. unsigned int nr_pages;
  957. struct linked_page *sp_list, *lp;
  958. int error;
  959. error = mark_unsafe_pages(bm);
  960. if (error)
  961. goto Free;
  962. error = memory_bm_create(new_bm, GFP_ATOMIC, PG_SAFE);
  963. if (error)
  964. goto Free;
  965. duplicate_memory_bitmap(new_bm, bm);
  966. memory_bm_free(bm, PG_UNSAFE_KEEP);
  967. /* Reserve some safe pages for potential later use.
  968. *
  969. * NOTE: This way we make sure there will be enough safe pages for the
  970. * chain_alloc() in get_buffer(). It is a bit wasteful, but
  971. * nr_copy_pages cannot be greater than 50% of the memory anyway.
  972. */
  973. sp_list = NULL;
  974. /* nr_copy_pages cannot be lesser than allocated_unsafe_pages */
  975. nr_pages = nr_copy_pages - allocated_unsafe_pages;
  976. nr_pages = DIV_ROUND_UP(nr_pages, PBES_PER_LINKED_PAGE);
  977. while (nr_pages > 0) {
  978. lp = alloc_image_page(GFP_ATOMIC, PG_SAFE);
  979. if (!lp) {
  980. error = -ENOMEM;
  981. goto Free;
  982. }
  983. lp->next = sp_list;
  984. sp_list = lp;
  985. nr_pages--;
  986. }
  987. /* Preallocate memory for the image */
  988. safe_pages_list = NULL;
  989. nr_pages = nr_copy_pages - allocated_unsafe_pages;
  990. while (nr_pages > 0) {
  991. lp = (struct linked_page *)get_zeroed_page(GFP_ATOMIC);
  992. if (!lp) {
  993. error = -ENOMEM;
  994. goto Free;
  995. }
  996. if (!PageNosaveFree(virt_to_page(lp))) {
  997. /* The page is "safe", add it to the list */
  998. lp->next = safe_pages_list;
  999. safe_pages_list = lp;
  1000. }
  1001. /* Mark the page as allocated */
  1002. SetPageNosave(virt_to_page(lp));
  1003. SetPageNosaveFree(virt_to_page(lp));
  1004. nr_pages--;
  1005. }
  1006. /* Free the reserved safe pages so that chain_alloc() can use them */
  1007. while (sp_list) {
  1008. lp = sp_list->next;
  1009. free_image_page(sp_list, PG_UNSAFE_CLEAR);
  1010. sp_list = lp;
  1011. }
  1012. return 0;
  1013. Free:
  1014. swsusp_free();
  1015. return error;
  1016. }
  1017. /**
  1018. * get_buffer - compute the address that snapshot_write_next() should
  1019. * set for its caller to write to.
  1020. */
  1021. static void *get_buffer(struct memory_bitmap *bm, struct chain_allocator *ca)
  1022. {
  1023. struct pbe *pbe;
  1024. struct page *page = pfn_to_page(memory_bm_next_pfn(bm));
  1025. if (PageNosave(page) && PageNosaveFree(page))
  1026. /* We have allocated the "original" page frame and we can
  1027. * use it directly to store the loaded page.
  1028. */
  1029. return page_address(page);
  1030. /* The "original" page frame has not been allocated and we have to
  1031. * use a "safe" page frame to store the loaded page.
  1032. */
  1033. pbe = chain_alloc(ca, sizeof(struct pbe));
  1034. if (!pbe) {
  1035. swsusp_free();
  1036. return NULL;
  1037. }
  1038. pbe->orig_address = (unsigned long)page_address(page);
  1039. pbe->address = (unsigned long)safe_pages_list;
  1040. safe_pages_list = safe_pages_list->next;
  1041. pbe->next = restore_pblist;
  1042. restore_pblist = pbe;
  1043. return (void *)pbe->address;
  1044. }
  1045. /**
  1046. * snapshot_write_next - used for writing the system memory snapshot.
  1047. *
  1048. * On the first call to it @handle should point to a zeroed
  1049. * snapshot_handle structure. The structure gets updated and a pointer
  1050. * to it should be passed to this function every next time.
  1051. *
  1052. * The @count parameter should contain the number of bytes the caller
  1053. * wants to write to the image. It must not be zero.
  1054. *
  1055. * On success the function returns a positive number. Then, the caller
  1056. * is allowed to write up to the returned number of bytes to the memory
  1057. * location computed by the data_of() macro. The number returned
  1058. * may be smaller than @count, but this only happens if the write would
  1059. * cross a page boundary otherwise.
  1060. *
  1061. * The function returns 0 to indicate the "end of file" condition,
  1062. * and a negative number is returned on error. In such cases the
  1063. * structure pointed to by @handle is not updated and should not be used
  1064. * any more.
  1065. */
  1066. int snapshot_write_next(struct snapshot_handle *handle, size_t count)
  1067. {
  1068. static struct chain_allocator ca;
  1069. int error = 0;
  1070. /* Check if we have already loaded the entire image */
  1071. if (handle->prev && handle->cur > nr_meta_pages + nr_copy_pages)
  1072. return 0;
  1073. if (!buffer) {
  1074. /* This makes the buffer be freed by swsusp_free() */
  1075. buffer = alloc_image_page(GFP_ATOMIC, PG_ANY);
  1076. if (!buffer)
  1077. return -ENOMEM;
  1078. }
  1079. if (!handle->offset)
  1080. handle->buffer = buffer;
  1081. handle->sync_read = 1;
  1082. if (handle->prev < handle->cur) {
  1083. if (handle->prev == 0) {
  1084. error = load_header(buffer);
  1085. if (error)
  1086. return error;
  1087. error = memory_bm_create(&copy_bm, GFP_ATOMIC, PG_ANY);
  1088. if (error)
  1089. return error;
  1090. } else if (handle->prev <= nr_meta_pages) {
  1091. unpack_orig_pfns(buffer, &copy_bm);
  1092. if (handle->prev == nr_meta_pages) {
  1093. error = prepare_image(&orig_bm, &copy_bm);
  1094. if (error)
  1095. return error;
  1096. chain_init(&ca, GFP_ATOMIC, PG_SAFE);
  1097. memory_bm_position_reset(&orig_bm);
  1098. restore_pblist = NULL;
  1099. handle->buffer = get_buffer(&orig_bm, &ca);
  1100. handle->sync_read = 0;
  1101. if (!handle->buffer)
  1102. return -ENOMEM;
  1103. }
  1104. } else {
  1105. handle->buffer = get_buffer(&orig_bm, &ca);
  1106. handle->sync_read = 0;
  1107. }
  1108. handle->prev = handle->cur;
  1109. }
  1110. handle->buf_offset = handle->cur_offset;
  1111. if (handle->cur_offset + count >= PAGE_SIZE) {
  1112. count = PAGE_SIZE - handle->cur_offset;
  1113. handle->cur_offset = 0;
  1114. handle->cur++;
  1115. } else {
  1116. handle->cur_offset += count;
  1117. }
  1118. handle->offset += count;
  1119. return count;
  1120. }
  1121. int snapshot_image_loaded(struct snapshot_handle *handle)
  1122. {
  1123. return !(!nr_copy_pages ||
  1124. handle->cur <= nr_meta_pages + nr_copy_pages);
  1125. }
  1126. void snapshot_free_unused_memory(struct snapshot_handle *handle)
  1127. {
  1128. /* Free only if we have loaded the image entirely */
  1129. if (handle->prev && handle->cur > nr_meta_pages + nr_copy_pages)
  1130. memory_bm_free(&orig_bm, PG_UNSAFE_CLEAR);
  1131. }