snapshot.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728
  1. /*
  2. * linux/kernel/power/snapshot.c
  3. *
  4. * This file provides system snapshot/restore functionality for swsusp.
  5. *
  6. * Copyright (C) 1998-2005 Pavel Machek <pavel@suse.cz>
  7. * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl>
  8. *
  9. * This file is released under the GPLv2.
  10. *
  11. */
  12. #include <linux/version.h>
  13. #include <linux/module.h>
  14. #include <linux/mm.h>
  15. #include <linux/suspend.h>
  16. #include <linux/smp_lock.h>
  17. #include <linux/delay.h>
  18. #include <linux/bitops.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/kernel.h>
  21. #include <linux/pm.h>
  22. #include <linux/device.h>
  23. #include <linux/bootmem.h>
  24. #include <linux/syscalls.h>
  25. #include <linux/console.h>
  26. #include <linux/highmem.h>
  27. #include <asm/uaccess.h>
  28. #include <asm/mmu_context.h>
  29. #include <asm/pgtable.h>
  30. #include <asm/tlbflush.h>
  31. #include <asm/io.h>
  32. #include "power.h"
  33. /* List of PBEs needed for restoring the pages that were allocated before
  34. * the suspend and included in the suspend image, but have also been
  35. * allocated by the "resume" kernel, so their contents cannot be written
  36. * directly to their "original" page frames.
  37. */
  38. struct pbe *restore_pblist;
  39. /* Pointer to an auxiliary buffer (1 page) */
  40. static void *buffer;
  41. /**
  42. * @safe_needed - on resume, for storing the PBE list and the image,
  43. * we can only use memory pages that do not conflict with the pages
  44. * used before suspend. The unsafe pages have PageNosaveFree set
  45. * and we count them using unsafe_pages.
  46. *
  47. * Each allocated image page is marked as PageNosave and PageNosaveFree
  48. * so that swsusp_free() can release it.
  49. */
  50. #define PG_ANY 0
  51. #define PG_SAFE 1
  52. #define PG_UNSAFE_CLEAR 1
  53. #define PG_UNSAFE_KEEP 0
  54. static unsigned int allocated_unsafe_pages;
  55. static void *get_image_page(gfp_t gfp_mask, int safe_needed)
  56. {
  57. void *res;
  58. res = (void *)get_zeroed_page(gfp_mask);
  59. if (safe_needed)
  60. while (res && PageNosaveFree(virt_to_page(res))) {
  61. /* The page is unsafe, mark it for swsusp_free() */
  62. SetPageNosave(virt_to_page(res));
  63. allocated_unsafe_pages++;
  64. res = (void *)get_zeroed_page(gfp_mask);
  65. }
  66. if (res) {
  67. SetPageNosave(virt_to_page(res));
  68. SetPageNosaveFree(virt_to_page(res));
  69. }
  70. return res;
  71. }
  72. unsigned long get_safe_page(gfp_t gfp_mask)
  73. {
  74. return (unsigned long)get_image_page(gfp_mask, PG_SAFE);
  75. }
  76. static struct page *alloc_image_page(gfp_t gfp_mask)
  77. {
  78. struct page *page;
  79. page = alloc_page(gfp_mask);
  80. if (page) {
  81. SetPageNosave(page);
  82. SetPageNosaveFree(page);
  83. }
  84. return page;
  85. }
  86. /**
  87. * free_image_page - free page represented by @addr, allocated with
  88. * get_image_page (page flags set by it must be cleared)
  89. */
  90. static inline void free_image_page(void *addr, int clear_nosave_free)
  91. {
  92. struct page *page;
  93. BUG_ON(!virt_addr_valid(addr));
  94. page = virt_to_page(addr);
  95. ClearPageNosave(page);
  96. if (clear_nosave_free)
  97. ClearPageNosaveFree(page);
  98. __free_page(page);
  99. }
  100. /* struct linked_page is used to build chains of pages */
  101. #define LINKED_PAGE_DATA_SIZE (PAGE_SIZE - sizeof(void *))
  102. struct linked_page {
  103. struct linked_page *next;
  104. char data[LINKED_PAGE_DATA_SIZE];
  105. } __attribute__((packed));
  106. static inline void
  107. free_list_of_pages(struct linked_page *list, int clear_page_nosave)
  108. {
  109. while (list) {
  110. struct linked_page *lp = list->next;
  111. free_image_page(list, clear_page_nosave);
  112. list = lp;
  113. }
  114. }
  115. /**
  116. * struct chain_allocator is used for allocating small objects out of
  117. * a linked list of pages called 'the chain'.
  118. *
  119. * The chain grows each time when there is no room for a new object in
  120. * the current page. The allocated objects cannot be freed individually.
  121. * It is only possible to free them all at once, by freeing the entire
  122. * chain.
  123. *
  124. * NOTE: The chain allocator may be inefficient if the allocated objects
  125. * are not much smaller than PAGE_SIZE.
  126. */
  127. struct chain_allocator {
  128. struct linked_page *chain; /* the chain */
  129. unsigned int used_space; /* total size of objects allocated out
  130. * of the current page
  131. */
  132. gfp_t gfp_mask; /* mask for allocating pages */
  133. int safe_needed; /* if set, only "safe" pages are allocated */
  134. };
  135. static void
  136. chain_init(struct chain_allocator *ca, gfp_t gfp_mask, int safe_needed)
  137. {
  138. ca->chain = NULL;
  139. ca->used_space = LINKED_PAGE_DATA_SIZE;
  140. ca->gfp_mask = gfp_mask;
  141. ca->safe_needed = safe_needed;
  142. }
  143. static void *chain_alloc(struct chain_allocator *ca, unsigned int size)
  144. {
  145. void *ret;
  146. if (LINKED_PAGE_DATA_SIZE - ca->used_space < size) {
  147. struct linked_page *lp;
  148. lp = get_image_page(ca->gfp_mask, ca->safe_needed);
  149. if (!lp)
  150. return NULL;
  151. lp->next = ca->chain;
  152. ca->chain = lp;
  153. ca->used_space = 0;
  154. }
  155. ret = ca->chain->data + ca->used_space;
  156. ca->used_space += size;
  157. return ret;
  158. }
  159. static void chain_free(struct chain_allocator *ca, int clear_page_nosave)
  160. {
  161. free_list_of_pages(ca->chain, clear_page_nosave);
  162. memset(ca, 0, sizeof(struct chain_allocator));
  163. }
  164. /**
  165. * Data types related to memory bitmaps.
  166. *
  167. * Memory bitmap is a structure consiting of many linked lists of
  168. * objects. The main list's elements are of type struct zone_bitmap
  169. * and each of them corresonds to one zone. For each zone bitmap
  170. * object there is a list of objects of type struct bm_block that
  171. * represent each blocks of bit chunks in which information is
  172. * stored.
  173. *
  174. * struct memory_bitmap contains a pointer to the main list of zone
  175. * bitmap objects, a struct bm_position used for browsing the bitmap,
  176. * and a pointer to the list of pages used for allocating all of the
  177. * zone bitmap objects and bitmap block objects.
  178. *
  179. * NOTE: It has to be possible to lay out the bitmap in memory
  180. * using only allocations of order 0. Additionally, the bitmap is
  181. * designed to work with arbitrary number of zones (this is over the
  182. * top for now, but let's avoid making unnecessary assumptions ;-).
  183. *
  184. * struct zone_bitmap contains a pointer to a list of bitmap block
  185. * objects and a pointer to the bitmap block object that has been
  186. * most recently used for setting bits. Additionally, it contains the
  187. * pfns that correspond to the start and end of the represented zone.
  188. *
  189. * struct bm_block contains a pointer to the memory page in which
  190. * information is stored (in the form of a block of bit chunks
  191. * of type unsigned long each). It also contains the pfns that
  192. * correspond to the start and end of the represented memory area and
  193. * the number of bit chunks in the block.
  194. *
  195. * NOTE: Memory bitmaps are used for two types of operations only:
  196. * "set a bit" and "find the next bit set". Moreover, the searching
  197. * is always carried out after all of the "set a bit" operations
  198. * on given bitmap.
  199. */
  200. #define BM_END_OF_MAP (~0UL)
  201. #define BM_CHUNKS_PER_BLOCK (PAGE_SIZE / sizeof(long))
  202. #define BM_BITS_PER_CHUNK (sizeof(long) << 3)
  203. #define BM_BITS_PER_BLOCK (PAGE_SIZE << 3)
  204. struct bm_block {
  205. struct bm_block *next; /* next element of the list */
  206. unsigned long start_pfn; /* pfn represented by the first bit */
  207. unsigned long end_pfn; /* pfn represented by the last bit plus 1 */
  208. unsigned int size; /* number of bit chunks */
  209. unsigned long *data; /* chunks of bits representing pages */
  210. };
  211. struct zone_bitmap {
  212. struct zone_bitmap *next; /* next element of the list */
  213. unsigned long start_pfn; /* minimal pfn in this zone */
  214. unsigned long end_pfn; /* maximal pfn in this zone plus 1 */
  215. struct bm_block *bm_blocks; /* list of bitmap blocks */
  216. struct bm_block *cur_block; /* recently used bitmap block */
  217. };
  218. /* strcut bm_position is used for browsing memory bitmaps */
  219. struct bm_position {
  220. struct zone_bitmap *zone_bm;
  221. struct bm_block *block;
  222. int chunk;
  223. int bit;
  224. };
  225. struct memory_bitmap {
  226. struct zone_bitmap *zone_bm_list; /* list of zone bitmaps */
  227. struct linked_page *p_list; /* list of pages used to store zone
  228. * bitmap objects and bitmap block
  229. * objects
  230. */
  231. struct bm_position cur; /* most recently used bit position */
  232. };
  233. /* Functions that operate on memory bitmaps */
  234. static inline void memory_bm_reset_chunk(struct memory_bitmap *bm)
  235. {
  236. bm->cur.chunk = 0;
  237. bm->cur.bit = -1;
  238. }
  239. static void memory_bm_position_reset(struct memory_bitmap *bm)
  240. {
  241. struct zone_bitmap *zone_bm;
  242. zone_bm = bm->zone_bm_list;
  243. bm->cur.zone_bm = zone_bm;
  244. bm->cur.block = zone_bm->bm_blocks;
  245. memory_bm_reset_chunk(bm);
  246. }
  247. static void memory_bm_free(struct memory_bitmap *bm, int clear_nosave_free);
  248. /**
  249. * create_bm_block_list - create a list of block bitmap objects
  250. */
  251. static inline struct bm_block *
  252. create_bm_block_list(unsigned int nr_blocks, struct chain_allocator *ca)
  253. {
  254. struct bm_block *bblist = NULL;
  255. while (nr_blocks-- > 0) {
  256. struct bm_block *bb;
  257. bb = chain_alloc(ca, sizeof(struct bm_block));
  258. if (!bb)
  259. return NULL;
  260. bb->next = bblist;
  261. bblist = bb;
  262. }
  263. return bblist;
  264. }
  265. /**
  266. * create_zone_bm_list - create a list of zone bitmap objects
  267. */
  268. static inline struct zone_bitmap *
  269. create_zone_bm_list(unsigned int nr_zones, struct chain_allocator *ca)
  270. {
  271. struct zone_bitmap *zbmlist = NULL;
  272. while (nr_zones-- > 0) {
  273. struct zone_bitmap *zbm;
  274. zbm = chain_alloc(ca, sizeof(struct zone_bitmap));
  275. if (!zbm)
  276. return NULL;
  277. zbm->next = zbmlist;
  278. zbmlist = zbm;
  279. }
  280. return zbmlist;
  281. }
  282. /**
  283. * memory_bm_create - allocate memory for a memory bitmap
  284. */
  285. static int
  286. memory_bm_create(struct memory_bitmap *bm, gfp_t gfp_mask, int safe_needed)
  287. {
  288. struct chain_allocator ca;
  289. struct zone *zone;
  290. struct zone_bitmap *zone_bm;
  291. struct bm_block *bb;
  292. unsigned int nr;
  293. chain_init(&ca, gfp_mask, safe_needed);
  294. /* Compute the number of zones */
  295. nr = 0;
  296. for_each_zone(zone)
  297. if (populated_zone(zone))
  298. nr++;
  299. /* Allocate the list of zones bitmap objects */
  300. zone_bm = create_zone_bm_list(nr, &ca);
  301. bm->zone_bm_list = zone_bm;
  302. if (!zone_bm) {
  303. chain_free(&ca, PG_UNSAFE_CLEAR);
  304. return -ENOMEM;
  305. }
  306. /* Initialize the zone bitmap objects */
  307. for_each_zone(zone) {
  308. unsigned long pfn;
  309. if (!populated_zone(zone))
  310. continue;
  311. zone_bm->start_pfn = zone->zone_start_pfn;
  312. zone_bm->end_pfn = zone->zone_start_pfn + zone->spanned_pages;
  313. /* Allocate the list of bitmap block objects */
  314. nr = DIV_ROUND_UP(zone->spanned_pages, BM_BITS_PER_BLOCK);
  315. bb = create_bm_block_list(nr, &ca);
  316. zone_bm->bm_blocks = bb;
  317. zone_bm->cur_block = bb;
  318. if (!bb)
  319. goto Free;
  320. nr = zone->spanned_pages;
  321. pfn = zone->zone_start_pfn;
  322. /* Initialize the bitmap block objects */
  323. while (bb) {
  324. unsigned long *ptr;
  325. ptr = get_image_page(gfp_mask, safe_needed);
  326. bb->data = ptr;
  327. if (!ptr)
  328. goto Free;
  329. bb->start_pfn = pfn;
  330. if (nr >= BM_BITS_PER_BLOCK) {
  331. pfn += BM_BITS_PER_BLOCK;
  332. bb->size = BM_CHUNKS_PER_BLOCK;
  333. nr -= BM_BITS_PER_BLOCK;
  334. } else {
  335. /* This is executed only once in the loop */
  336. pfn += nr;
  337. bb->size = DIV_ROUND_UP(nr, BM_BITS_PER_CHUNK);
  338. }
  339. bb->end_pfn = pfn;
  340. bb = bb->next;
  341. }
  342. zone_bm = zone_bm->next;
  343. }
  344. bm->p_list = ca.chain;
  345. memory_bm_position_reset(bm);
  346. return 0;
  347. Free:
  348. bm->p_list = ca.chain;
  349. memory_bm_free(bm, PG_UNSAFE_CLEAR);
  350. return -ENOMEM;
  351. }
  352. /**
  353. * memory_bm_free - free memory occupied by the memory bitmap @bm
  354. */
  355. static void memory_bm_free(struct memory_bitmap *bm, int clear_nosave_free)
  356. {
  357. struct zone_bitmap *zone_bm;
  358. /* Free the list of bit blocks for each zone_bitmap object */
  359. zone_bm = bm->zone_bm_list;
  360. while (zone_bm) {
  361. struct bm_block *bb;
  362. bb = zone_bm->bm_blocks;
  363. while (bb) {
  364. if (bb->data)
  365. free_image_page(bb->data, clear_nosave_free);
  366. bb = bb->next;
  367. }
  368. zone_bm = zone_bm->next;
  369. }
  370. free_list_of_pages(bm->p_list, clear_nosave_free);
  371. bm->zone_bm_list = NULL;
  372. }
  373. /**
  374. * memory_bm_set_bit - set the bit in the bitmap @bm that corresponds
  375. * to given pfn. The cur_zone_bm member of @bm and the cur_block member
  376. * of @bm->cur_zone_bm are updated.
  377. *
  378. * If the bit cannot be set, the function returns -EINVAL .
  379. */
  380. static int
  381. memory_bm_set_bit(struct memory_bitmap *bm, unsigned long pfn)
  382. {
  383. struct zone_bitmap *zone_bm;
  384. struct bm_block *bb;
  385. /* Check if the pfn is from the current zone */
  386. zone_bm = bm->cur.zone_bm;
  387. if (pfn < zone_bm->start_pfn || pfn >= zone_bm->end_pfn) {
  388. zone_bm = bm->zone_bm_list;
  389. /* We don't assume that the zones are sorted by pfns */
  390. while (pfn < zone_bm->start_pfn || pfn >= zone_bm->end_pfn) {
  391. zone_bm = zone_bm->next;
  392. if (unlikely(!zone_bm))
  393. return -EINVAL;
  394. }
  395. bm->cur.zone_bm = zone_bm;
  396. }
  397. /* Check if the pfn corresponds to the current bitmap block */
  398. bb = zone_bm->cur_block;
  399. if (pfn < bb->start_pfn)
  400. bb = zone_bm->bm_blocks;
  401. while (pfn >= bb->end_pfn) {
  402. bb = bb->next;
  403. if (unlikely(!bb))
  404. return -EINVAL;
  405. }
  406. zone_bm->cur_block = bb;
  407. pfn -= bb->start_pfn;
  408. set_bit(pfn % BM_BITS_PER_CHUNK, bb->data + pfn / BM_BITS_PER_CHUNK);
  409. return 0;
  410. }
  411. /* Two auxiliary functions for memory_bm_next_pfn */
  412. /* Find the first set bit in the given chunk, if there is one */
  413. static inline int next_bit_in_chunk(int bit, unsigned long *chunk_p)
  414. {
  415. bit++;
  416. while (bit < BM_BITS_PER_CHUNK) {
  417. if (test_bit(bit, chunk_p))
  418. return bit;
  419. bit++;
  420. }
  421. return -1;
  422. }
  423. /* Find a chunk containing some bits set in given block of bits */
  424. static inline int next_chunk_in_block(int n, struct bm_block *bb)
  425. {
  426. n++;
  427. while (n < bb->size) {
  428. if (bb->data[n])
  429. return n;
  430. n++;
  431. }
  432. return -1;
  433. }
  434. /**
  435. * memory_bm_next_pfn - find the pfn that corresponds to the next set bit
  436. * in the bitmap @bm. If the pfn cannot be found, BM_END_OF_MAP is
  437. * returned.
  438. *
  439. * It is required to run memory_bm_position_reset() before the first call to
  440. * this function.
  441. */
  442. static unsigned long memory_bm_next_pfn(struct memory_bitmap *bm)
  443. {
  444. struct zone_bitmap *zone_bm;
  445. struct bm_block *bb;
  446. int chunk;
  447. int bit;
  448. do {
  449. bb = bm->cur.block;
  450. do {
  451. chunk = bm->cur.chunk;
  452. bit = bm->cur.bit;
  453. do {
  454. bit = next_bit_in_chunk(bit, bb->data + chunk);
  455. if (bit >= 0)
  456. goto Return_pfn;
  457. chunk = next_chunk_in_block(chunk, bb);
  458. bit = -1;
  459. } while (chunk >= 0);
  460. bb = bb->next;
  461. bm->cur.block = bb;
  462. memory_bm_reset_chunk(bm);
  463. } while (bb);
  464. zone_bm = bm->cur.zone_bm->next;
  465. if (zone_bm) {
  466. bm->cur.zone_bm = zone_bm;
  467. bm->cur.block = zone_bm->bm_blocks;
  468. memory_bm_reset_chunk(bm);
  469. }
  470. } while (zone_bm);
  471. memory_bm_position_reset(bm);
  472. return BM_END_OF_MAP;
  473. Return_pfn:
  474. bm->cur.chunk = chunk;
  475. bm->cur.bit = bit;
  476. return bb->start_pfn + chunk * BM_BITS_PER_CHUNK + bit;
  477. }
  478. /**
  479. * snapshot_additional_pages - estimate the number of additional pages
  480. * be needed for setting up the suspend image data structures for given
  481. * zone (usually the returned value is greater than the exact number)
  482. */
  483. unsigned int snapshot_additional_pages(struct zone *zone)
  484. {
  485. unsigned int res;
  486. res = DIV_ROUND_UP(zone->spanned_pages, BM_BITS_PER_BLOCK);
  487. res += DIV_ROUND_UP(res * sizeof(struct bm_block), PAGE_SIZE);
  488. return 2 * res;
  489. }
  490. #ifdef CONFIG_HIGHMEM
  491. /**
  492. * count_free_highmem_pages - compute the total number of free highmem
  493. * pages, system-wide.
  494. */
  495. static unsigned int count_free_highmem_pages(void)
  496. {
  497. struct zone *zone;
  498. unsigned int cnt = 0;
  499. for_each_zone(zone)
  500. if (populated_zone(zone) && is_highmem(zone))
  501. cnt += zone_page_state(zone, NR_FREE_PAGES);
  502. return cnt;
  503. }
  504. /**
  505. * saveable_highmem_page - Determine whether a highmem page should be
  506. * included in the suspend image.
  507. *
  508. * We should save the page if it isn't Nosave or NosaveFree, or Reserved,
  509. * and it isn't a part of a free chunk of pages.
  510. */
  511. static struct page *saveable_highmem_page(unsigned long pfn)
  512. {
  513. struct page *page;
  514. if (!pfn_valid(pfn))
  515. return NULL;
  516. page = pfn_to_page(pfn);
  517. BUG_ON(!PageHighMem(page));
  518. if (PageNosave(page) || PageReserved(page) || PageNosaveFree(page))
  519. return NULL;
  520. return page;
  521. }
  522. /**
  523. * count_highmem_pages - compute the total number of saveable highmem
  524. * pages.
  525. */
  526. unsigned int count_highmem_pages(void)
  527. {
  528. struct zone *zone;
  529. unsigned int n = 0;
  530. for_each_zone(zone) {
  531. unsigned long pfn, max_zone_pfn;
  532. if (!is_highmem(zone))
  533. continue;
  534. mark_free_pages(zone);
  535. max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
  536. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
  537. if (saveable_highmem_page(pfn))
  538. n++;
  539. }
  540. return n;
  541. }
  542. #else
  543. static inline void *saveable_highmem_page(unsigned long pfn) { return NULL; }
  544. static inline unsigned int count_highmem_pages(void) { return 0; }
  545. #endif /* CONFIG_HIGHMEM */
  546. /**
  547. * saveable - Determine whether a non-highmem page should be included in
  548. * the suspend image.
  549. *
  550. * We should save the page if it isn't Nosave, and is not in the range
  551. * of pages statically defined as 'unsaveable', and it isn't a part of
  552. * a free chunk of pages.
  553. */
  554. static struct page *saveable_page(unsigned long pfn)
  555. {
  556. struct page *page;
  557. if (!pfn_valid(pfn))
  558. return NULL;
  559. page = pfn_to_page(pfn);
  560. BUG_ON(PageHighMem(page));
  561. if (PageNosave(page) || PageNosaveFree(page))
  562. return NULL;
  563. if (PageReserved(page) && pfn_is_nosave(pfn))
  564. return NULL;
  565. return page;
  566. }
  567. /**
  568. * count_data_pages - compute the total number of saveable non-highmem
  569. * pages.
  570. */
  571. unsigned int count_data_pages(void)
  572. {
  573. struct zone *zone;
  574. unsigned long pfn, max_zone_pfn;
  575. unsigned int n = 0;
  576. for_each_zone(zone) {
  577. if (is_highmem(zone))
  578. continue;
  579. mark_free_pages(zone);
  580. max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
  581. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
  582. if(saveable_page(pfn))
  583. n++;
  584. }
  585. return n;
  586. }
  587. /* This is needed, because copy_page and memcpy are not usable for copying
  588. * task structs.
  589. */
  590. static inline void do_copy_page(long *dst, long *src)
  591. {
  592. int n;
  593. for (n = PAGE_SIZE / sizeof(long); n; n--)
  594. *dst++ = *src++;
  595. }
  596. #ifdef CONFIG_HIGHMEM
  597. static inline struct page *
  598. page_is_saveable(struct zone *zone, unsigned long pfn)
  599. {
  600. return is_highmem(zone) ?
  601. saveable_highmem_page(pfn) : saveable_page(pfn);
  602. }
  603. static inline void
  604. copy_data_page(unsigned long dst_pfn, unsigned long src_pfn)
  605. {
  606. struct page *s_page, *d_page;
  607. void *src, *dst;
  608. s_page = pfn_to_page(src_pfn);
  609. d_page = pfn_to_page(dst_pfn);
  610. if (PageHighMem(s_page)) {
  611. src = kmap_atomic(s_page, KM_USER0);
  612. dst = kmap_atomic(d_page, KM_USER1);
  613. do_copy_page(dst, src);
  614. kunmap_atomic(src, KM_USER0);
  615. kunmap_atomic(dst, KM_USER1);
  616. } else {
  617. src = page_address(s_page);
  618. if (PageHighMem(d_page)) {
  619. /* Page pointed to by src may contain some kernel
  620. * data modified by kmap_atomic()
  621. */
  622. do_copy_page(buffer, src);
  623. dst = kmap_atomic(pfn_to_page(dst_pfn), KM_USER0);
  624. memcpy(dst, buffer, PAGE_SIZE);
  625. kunmap_atomic(dst, KM_USER0);
  626. } else {
  627. dst = page_address(d_page);
  628. do_copy_page(dst, src);
  629. }
  630. }
  631. }
  632. #else
  633. #define page_is_saveable(zone, pfn) saveable_page(pfn)
  634. static inline void
  635. copy_data_page(unsigned long dst_pfn, unsigned long src_pfn)
  636. {
  637. do_copy_page(page_address(pfn_to_page(dst_pfn)),
  638. page_address(pfn_to_page(src_pfn)));
  639. }
  640. #endif /* CONFIG_HIGHMEM */
  641. static void
  642. copy_data_pages(struct memory_bitmap *copy_bm, struct memory_bitmap *orig_bm)
  643. {
  644. struct zone *zone;
  645. unsigned long pfn;
  646. for_each_zone(zone) {
  647. unsigned long max_zone_pfn;
  648. mark_free_pages(zone);
  649. max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
  650. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
  651. if (page_is_saveable(zone, pfn))
  652. memory_bm_set_bit(orig_bm, pfn);
  653. }
  654. memory_bm_position_reset(orig_bm);
  655. memory_bm_position_reset(copy_bm);
  656. do {
  657. pfn = memory_bm_next_pfn(orig_bm);
  658. if (likely(pfn != BM_END_OF_MAP))
  659. copy_data_page(memory_bm_next_pfn(copy_bm), pfn);
  660. } while (pfn != BM_END_OF_MAP);
  661. }
  662. /* Total number of image pages */
  663. static unsigned int nr_copy_pages;
  664. /* Number of pages needed for saving the original pfns of the image pages */
  665. static unsigned int nr_meta_pages;
  666. /**
  667. * swsusp_free - free pages allocated for the suspend.
  668. *
  669. * Suspend pages are alocated before the atomic copy is made, so we
  670. * need to release them after the resume.
  671. */
  672. void swsusp_free(void)
  673. {
  674. struct zone *zone;
  675. unsigned long pfn, max_zone_pfn;
  676. for_each_zone(zone) {
  677. max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
  678. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
  679. if (pfn_valid(pfn)) {
  680. struct page *page = pfn_to_page(pfn);
  681. if (PageNosave(page) && PageNosaveFree(page)) {
  682. ClearPageNosave(page);
  683. ClearPageNosaveFree(page);
  684. __free_page(page);
  685. }
  686. }
  687. }
  688. nr_copy_pages = 0;
  689. nr_meta_pages = 0;
  690. restore_pblist = NULL;
  691. buffer = NULL;
  692. }
  693. #ifdef CONFIG_HIGHMEM
  694. /**
  695. * count_pages_for_highmem - compute the number of non-highmem pages
  696. * that will be necessary for creating copies of highmem pages.
  697. */
  698. static unsigned int count_pages_for_highmem(unsigned int nr_highmem)
  699. {
  700. unsigned int free_highmem = count_free_highmem_pages();
  701. if (free_highmem >= nr_highmem)
  702. nr_highmem = 0;
  703. else
  704. nr_highmem -= free_highmem;
  705. return nr_highmem;
  706. }
  707. #else
  708. static unsigned int
  709. count_pages_for_highmem(unsigned int nr_highmem) { return 0; }
  710. #endif /* CONFIG_HIGHMEM */
  711. /**
  712. * enough_free_mem - Make sure we have enough free memory for the
  713. * snapshot image.
  714. */
  715. static int enough_free_mem(unsigned int nr_pages, unsigned int nr_highmem)
  716. {
  717. struct zone *zone;
  718. unsigned int free = 0, meta = 0;
  719. for_each_zone(zone) {
  720. meta += snapshot_additional_pages(zone);
  721. if (!is_highmem(zone))
  722. free += zone_page_state(zone, NR_FREE_PAGES);
  723. }
  724. nr_pages += count_pages_for_highmem(nr_highmem);
  725. pr_debug("swsusp: Normal pages needed: %u + %u + %u, available pages: %u\n",
  726. nr_pages, PAGES_FOR_IO, meta, free);
  727. return free > nr_pages + PAGES_FOR_IO + meta;
  728. }
  729. #ifdef CONFIG_HIGHMEM
  730. /**
  731. * get_highmem_buffer - if there are some highmem pages in the suspend
  732. * image, we may need the buffer to copy them and/or load their data.
  733. */
  734. static inline int get_highmem_buffer(int safe_needed)
  735. {
  736. buffer = get_image_page(GFP_ATOMIC | __GFP_COLD, safe_needed);
  737. return buffer ? 0 : -ENOMEM;
  738. }
  739. /**
  740. * alloc_highmem_image_pages - allocate some highmem pages for the image.
  741. * Try to allocate as many pages as needed, but if the number of free
  742. * highmem pages is lesser than that, allocate them all.
  743. */
  744. static inline unsigned int
  745. alloc_highmem_image_pages(struct memory_bitmap *bm, unsigned int nr_highmem)
  746. {
  747. unsigned int to_alloc = count_free_highmem_pages();
  748. if (to_alloc > nr_highmem)
  749. to_alloc = nr_highmem;
  750. nr_highmem -= to_alloc;
  751. while (to_alloc-- > 0) {
  752. struct page *page;
  753. page = alloc_image_page(__GFP_HIGHMEM);
  754. memory_bm_set_bit(bm, page_to_pfn(page));
  755. }
  756. return nr_highmem;
  757. }
  758. #else
  759. static inline int get_highmem_buffer(int safe_needed) { return 0; }
  760. static inline unsigned int
  761. alloc_highmem_image_pages(struct memory_bitmap *bm, unsigned int n) { return 0; }
  762. #endif /* CONFIG_HIGHMEM */
  763. /**
  764. * swsusp_alloc - allocate memory for the suspend image
  765. *
  766. * We first try to allocate as many highmem pages as there are
  767. * saveable highmem pages in the system. If that fails, we allocate
  768. * non-highmem pages for the copies of the remaining highmem ones.
  769. *
  770. * In this approach it is likely that the copies of highmem pages will
  771. * also be located in the high memory, because of the way in which
  772. * copy_data_pages() works.
  773. */
  774. static int
  775. swsusp_alloc(struct memory_bitmap *orig_bm, struct memory_bitmap *copy_bm,
  776. unsigned int nr_pages, unsigned int nr_highmem)
  777. {
  778. int error;
  779. error = memory_bm_create(orig_bm, GFP_ATOMIC | __GFP_COLD, PG_ANY);
  780. if (error)
  781. goto Free;
  782. error = memory_bm_create(copy_bm, GFP_ATOMIC | __GFP_COLD, PG_ANY);
  783. if (error)
  784. goto Free;
  785. if (nr_highmem > 0) {
  786. error = get_highmem_buffer(PG_ANY);
  787. if (error)
  788. goto Free;
  789. nr_pages += alloc_highmem_image_pages(copy_bm, nr_highmem);
  790. }
  791. while (nr_pages-- > 0) {
  792. struct page *page = alloc_image_page(GFP_ATOMIC | __GFP_COLD);
  793. if (!page)
  794. goto Free;
  795. memory_bm_set_bit(copy_bm, page_to_pfn(page));
  796. }
  797. return 0;
  798. Free:
  799. swsusp_free();
  800. return -ENOMEM;
  801. }
  802. /* Memory bitmap used for marking saveable pages (during suspend) or the
  803. * suspend image pages (during resume)
  804. */
  805. static struct memory_bitmap orig_bm;
  806. /* Memory bitmap used on suspend for marking allocated pages that will contain
  807. * the copies of saveable pages. During resume it is initially used for
  808. * marking the suspend image pages, but then its set bits are duplicated in
  809. * @orig_bm and it is released. Next, on systems with high memory, it may be
  810. * used for marking "safe" highmem pages, but it has to be reinitialized for
  811. * this purpose.
  812. */
  813. static struct memory_bitmap copy_bm;
  814. asmlinkage int swsusp_save(void)
  815. {
  816. unsigned int nr_pages, nr_highmem;
  817. printk("swsusp: critical section: \n");
  818. drain_local_pages();
  819. nr_pages = count_data_pages();
  820. nr_highmem = count_highmem_pages();
  821. printk("swsusp: Need to copy %u pages\n", nr_pages + nr_highmem);
  822. if (!enough_free_mem(nr_pages, nr_highmem)) {
  823. printk(KERN_ERR "swsusp: Not enough free memory\n");
  824. return -ENOMEM;
  825. }
  826. if (swsusp_alloc(&orig_bm, &copy_bm, nr_pages, nr_highmem)) {
  827. printk(KERN_ERR "swsusp: Memory allocation failed\n");
  828. return -ENOMEM;
  829. }
  830. /* During allocating of suspend pagedir, new cold pages may appear.
  831. * Kill them.
  832. */
  833. drain_local_pages();
  834. copy_data_pages(&copy_bm, &orig_bm);
  835. /*
  836. * End of critical section. From now on, we can write to memory,
  837. * but we should not touch disk. This specially means we must _not_
  838. * touch swap space! Except we must write out our image of course.
  839. */
  840. nr_pages += nr_highmem;
  841. nr_copy_pages = nr_pages;
  842. nr_meta_pages = DIV_ROUND_UP(nr_pages * sizeof(long), PAGE_SIZE);
  843. printk("swsusp: critical section/: done (%d pages copied)\n", nr_pages);
  844. return 0;
  845. }
  846. static void init_header(struct swsusp_info *info)
  847. {
  848. memset(info, 0, sizeof(struct swsusp_info));
  849. info->version_code = LINUX_VERSION_CODE;
  850. info->num_physpages = num_physpages;
  851. memcpy(&info->uts, init_utsname(), sizeof(struct new_utsname));
  852. info->cpus = num_online_cpus();
  853. info->image_pages = nr_copy_pages;
  854. info->pages = nr_copy_pages + nr_meta_pages + 1;
  855. info->size = info->pages;
  856. info->size <<= PAGE_SHIFT;
  857. }
  858. /**
  859. * pack_pfns - pfns corresponding to the set bits found in the bitmap @bm
  860. * are stored in the array @buf[] (1 page at a time)
  861. */
  862. static inline void
  863. pack_pfns(unsigned long *buf, struct memory_bitmap *bm)
  864. {
  865. int j;
  866. for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
  867. buf[j] = memory_bm_next_pfn(bm);
  868. if (unlikely(buf[j] == BM_END_OF_MAP))
  869. break;
  870. }
  871. }
  872. /**
  873. * snapshot_read_next - used for reading the system memory snapshot.
  874. *
  875. * On the first call to it @handle should point to a zeroed
  876. * snapshot_handle structure. The structure gets updated and a pointer
  877. * to it should be passed to this function every next time.
  878. *
  879. * The @count parameter should contain the number of bytes the caller
  880. * wants to read from the snapshot. It must not be zero.
  881. *
  882. * On success the function returns a positive number. Then, the caller
  883. * is allowed to read up to the returned number of bytes from the memory
  884. * location computed by the data_of() macro. The number returned
  885. * may be smaller than @count, but this only happens if the read would
  886. * cross a page boundary otherwise.
  887. *
  888. * The function returns 0 to indicate the end of data stream condition,
  889. * and a negative number is returned on error. In such cases the
  890. * structure pointed to by @handle is not updated and should not be used
  891. * any more.
  892. */
  893. int snapshot_read_next(struct snapshot_handle *handle, size_t count)
  894. {
  895. if (handle->cur > nr_meta_pages + nr_copy_pages)
  896. return 0;
  897. if (!buffer) {
  898. /* This makes the buffer be freed by swsusp_free() */
  899. buffer = get_image_page(GFP_ATOMIC, PG_ANY);
  900. if (!buffer)
  901. return -ENOMEM;
  902. }
  903. if (!handle->offset) {
  904. init_header((struct swsusp_info *)buffer);
  905. handle->buffer = buffer;
  906. memory_bm_position_reset(&orig_bm);
  907. memory_bm_position_reset(&copy_bm);
  908. }
  909. if (handle->prev < handle->cur) {
  910. if (handle->cur <= nr_meta_pages) {
  911. memset(buffer, 0, PAGE_SIZE);
  912. pack_pfns(buffer, &orig_bm);
  913. } else {
  914. struct page *page;
  915. page = pfn_to_page(memory_bm_next_pfn(&copy_bm));
  916. if (PageHighMem(page)) {
  917. /* Highmem pages are copied to the buffer,
  918. * because we can't return with a kmapped
  919. * highmem page (we may not be called again).
  920. */
  921. void *kaddr;
  922. kaddr = kmap_atomic(page, KM_USER0);
  923. memcpy(buffer, kaddr, PAGE_SIZE);
  924. kunmap_atomic(kaddr, KM_USER0);
  925. handle->buffer = buffer;
  926. } else {
  927. handle->buffer = page_address(page);
  928. }
  929. }
  930. handle->prev = handle->cur;
  931. }
  932. handle->buf_offset = handle->cur_offset;
  933. if (handle->cur_offset + count >= PAGE_SIZE) {
  934. count = PAGE_SIZE - handle->cur_offset;
  935. handle->cur_offset = 0;
  936. handle->cur++;
  937. } else {
  938. handle->cur_offset += count;
  939. }
  940. handle->offset += count;
  941. return count;
  942. }
  943. /**
  944. * mark_unsafe_pages - mark the pages that cannot be used for storing
  945. * the image during resume, because they conflict with the pages that
  946. * had been used before suspend
  947. */
  948. static int mark_unsafe_pages(struct memory_bitmap *bm)
  949. {
  950. struct zone *zone;
  951. unsigned long pfn, max_zone_pfn;
  952. /* Clear page flags */
  953. for_each_zone(zone) {
  954. max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
  955. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
  956. if (pfn_valid(pfn))
  957. ClearPageNosaveFree(pfn_to_page(pfn));
  958. }
  959. /* Mark pages that correspond to the "original" pfns as "unsafe" */
  960. memory_bm_position_reset(bm);
  961. do {
  962. pfn = memory_bm_next_pfn(bm);
  963. if (likely(pfn != BM_END_OF_MAP)) {
  964. if (likely(pfn_valid(pfn)))
  965. SetPageNosaveFree(pfn_to_page(pfn));
  966. else
  967. return -EFAULT;
  968. }
  969. } while (pfn != BM_END_OF_MAP);
  970. allocated_unsafe_pages = 0;
  971. return 0;
  972. }
  973. static void
  974. duplicate_memory_bitmap(struct memory_bitmap *dst, struct memory_bitmap *src)
  975. {
  976. unsigned long pfn;
  977. memory_bm_position_reset(src);
  978. pfn = memory_bm_next_pfn(src);
  979. while (pfn != BM_END_OF_MAP) {
  980. memory_bm_set_bit(dst, pfn);
  981. pfn = memory_bm_next_pfn(src);
  982. }
  983. }
  984. static inline int check_header(struct swsusp_info *info)
  985. {
  986. char *reason = NULL;
  987. if (info->version_code != LINUX_VERSION_CODE)
  988. reason = "kernel version";
  989. if (info->num_physpages != num_physpages)
  990. reason = "memory size";
  991. if (strcmp(info->uts.sysname,init_utsname()->sysname))
  992. reason = "system type";
  993. if (strcmp(info->uts.release,init_utsname()->release))
  994. reason = "kernel release";
  995. if (strcmp(info->uts.version,init_utsname()->version))
  996. reason = "version";
  997. if (strcmp(info->uts.machine,init_utsname()->machine))
  998. reason = "machine";
  999. if (reason) {
  1000. printk(KERN_ERR "swsusp: Resume mismatch: %s\n", reason);
  1001. return -EPERM;
  1002. }
  1003. return 0;
  1004. }
  1005. /**
  1006. * load header - check the image header and copy data from it
  1007. */
  1008. static int
  1009. load_header(struct swsusp_info *info)
  1010. {
  1011. int error;
  1012. restore_pblist = NULL;
  1013. error = check_header(info);
  1014. if (!error) {
  1015. nr_copy_pages = info->image_pages;
  1016. nr_meta_pages = info->pages - info->image_pages - 1;
  1017. }
  1018. return error;
  1019. }
  1020. /**
  1021. * unpack_orig_pfns - for each element of @buf[] (1 page at a time) set
  1022. * the corresponding bit in the memory bitmap @bm
  1023. */
  1024. static inline void
  1025. unpack_orig_pfns(unsigned long *buf, struct memory_bitmap *bm)
  1026. {
  1027. int j;
  1028. for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
  1029. if (unlikely(buf[j] == BM_END_OF_MAP))
  1030. break;
  1031. memory_bm_set_bit(bm, buf[j]);
  1032. }
  1033. }
  1034. /* List of "safe" pages that may be used to store data loaded from the suspend
  1035. * image
  1036. */
  1037. static struct linked_page *safe_pages_list;
  1038. #ifdef CONFIG_HIGHMEM
  1039. /* struct highmem_pbe is used for creating the list of highmem pages that
  1040. * should be restored atomically during the resume from disk, because the page
  1041. * frames they have occupied before the suspend are in use.
  1042. */
  1043. struct highmem_pbe {
  1044. struct page *copy_page; /* data is here now */
  1045. struct page *orig_page; /* data was here before the suspend */
  1046. struct highmem_pbe *next;
  1047. };
  1048. /* List of highmem PBEs needed for restoring the highmem pages that were
  1049. * allocated before the suspend and included in the suspend image, but have
  1050. * also been allocated by the "resume" kernel, so their contents cannot be
  1051. * written directly to their "original" page frames.
  1052. */
  1053. static struct highmem_pbe *highmem_pblist;
  1054. /**
  1055. * count_highmem_image_pages - compute the number of highmem pages in the
  1056. * suspend image. The bits in the memory bitmap @bm that correspond to the
  1057. * image pages are assumed to be set.
  1058. */
  1059. static unsigned int count_highmem_image_pages(struct memory_bitmap *bm)
  1060. {
  1061. unsigned long pfn;
  1062. unsigned int cnt = 0;
  1063. memory_bm_position_reset(bm);
  1064. pfn = memory_bm_next_pfn(bm);
  1065. while (pfn != BM_END_OF_MAP) {
  1066. if (PageHighMem(pfn_to_page(pfn)))
  1067. cnt++;
  1068. pfn = memory_bm_next_pfn(bm);
  1069. }
  1070. return cnt;
  1071. }
  1072. /**
  1073. * prepare_highmem_image - try to allocate as many highmem pages as
  1074. * there are highmem image pages (@nr_highmem_p points to the variable
  1075. * containing the number of highmem image pages). The pages that are
  1076. * "safe" (ie. will not be overwritten when the suspend image is
  1077. * restored) have the corresponding bits set in @bm (it must be
  1078. * unitialized).
  1079. *
  1080. * NOTE: This function should not be called if there are no highmem
  1081. * image pages.
  1082. */
  1083. static unsigned int safe_highmem_pages;
  1084. static struct memory_bitmap *safe_highmem_bm;
  1085. static int
  1086. prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
  1087. {
  1088. unsigned int to_alloc;
  1089. if (memory_bm_create(bm, GFP_ATOMIC, PG_SAFE))
  1090. return -ENOMEM;
  1091. if (get_highmem_buffer(PG_SAFE))
  1092. return -ENOMEM;
  1093. to_alloc = count_free_highmem_pages();
  1094. if (to_alloc > *nr_highmem_p)
  1095. to_alloc = *nr_highmem_p;
  1096. else
  1097. *nr_highmem_p = to_alloc;
  1098. safe_highmem_pages = 0;
  1099. while (to_alloc-- > 0) {
  1100. struct page *page;
  1101. page = alloc_page(__GFP_HIGHMEM);
  1102. if (!PageNosaveFree(page)) {
  1103. /* The page is "safe", set its bit the bitmap */
  1104. memory_bm_set_bit(bm, page_to_pfn(page));
  1105. safe_highmem_pages++;
  1106. }
  1107. /* Mark the page as allocated */
  1108. SetPageNosave(page);
  1109. SetPageNosaveFree(page);
  1110. }
  1111. memory_bm_position_reset(bm);
  1112. safe_highmem_bm = bm;
  1113. return 0;
  1114. }
  1115. /**
  1116. * get_highmem_page_buffer - for given highmem image page find the buffer
  1117. * that suspend_write_next() should set for its caller to write to.
  1118. *
  1119. * If the page is to be saved to its "original" page frame or a copy of
  1120. * the page is to be made in the highmem, @buffer is returned. Otherwise,
  1121. * the copy of the page is to be made in normal memory, so the address of
  1122. * the copy is returned.
  1123. *
  1124. * If @buffer is returned, the caller of suspend_write_next() will write
  1125. * the page's contents to @buffer, so they will have to be copied to the
  1126. * right location on the next call to suspend_write_next() and it is done
  1127. * with the help of copy_last_highmem_page(). For this purpose, if
  1128. * @buffer is returned, @last_highmem page is set to the page to which
  1129. * the data will have to be copied from @buffer.
  1130. */
  1131. static struct page *last_highmem_page;
  1132. static void *
  1133. get_highmem_page_buffer(struct page *page, struct chain_allocator *ca)
  1134. {
  1135. struct highmem_pbe *pbe;
  1136. void *kaddr;
  1137. if (PageNosave(page) && PageNosaveFree(page)) {
  1138. /* We have allocated the "original" page frame and we can
  1139. * use it directly to store the loaded page.
  1140. */
  1141. last_highmem_page = page;
  1142. return buffer;
  1143. }
  1144. /* The "original" page frame has not been allocated and we have to
  1145. * use a "safe" page frame to store the loaded page.
  1146. */
  1147. pbe = chain_alloc(ca, sizeof(struct highmem_pbe));
  1148. if (!pbe) {
  1149. swsusp_free();
  1150. return NULL;
  1151. }
  1152. pbe->orig_page = page;
  1153. if (safe_highmem_pages > 0) {
  1154. struct page *tmp;
  1155. /* Copy of the page will be stored in high memory */
  1156. kaddr = buffer;
  1157. tmp = pfn_to_page(memory_bm_next_pfn(safe_highmem_bm));
  1158. safe_highmem_pages--;
  1159. last_highmem_page = tmp;
  1160. pbe->copy_page = tmp;
  1161. } else {
  1162. /* Copy of the page will be stored in normal memory */
  1163. kaddr = safe_pages_list;
  1164. safe_pages_list = safe_pages_list->next;
  1165. pbe->copy_page = virt_to_page(kaddr);
  1166. }
  1167. pbe->next = highmem_pblist;
  1168. highmem_pblist = pbe;
  1169. return kaddr;
  1170. }
  1171. /**
  1172. * copy_last_highmem_page - copy the contents of a highmem image from
  1173. * @buffer, where the caller of snapshot_write_next() has place them,
  1174. * to the right location represented by @last_highmem_page .
  1175. */
  1176. static void copy_last_highmem_page(void)
  1177. {
  1178. if (last_highmem_page) {
  1179. void *dst;
  1180. dst = kmap_atomic(last_highmem_page, KM_USER0);
  1181. memcpy(dst, buffer, PAGE_SIZE);
  1182. kunmap_atomic(dst, KM_USER0);
  1183. last_highmem_page = NULL;
  1184. }
  1185. }
  1186. static inline int last_highmem_page_copied(void)
  1187. {
  1188. return !last_highmem_page;
  1189. }
  1190. static inline void free_highmem_data(void)
  1191. {
  1192. if (safe_highmem_bm)
  1193. memory_bm_free(safe_highmem_bm, PG_UNSAFE_CLEAR);
  1194. if (buffer)
  1195. free_image_page(buffer, PG_UNSAFE_CLEAR);
  1196. }
  1197. #else
  1198. static inline int get_safe_write_buffer(void) { return 0; }
  1199. static unsigned int
  1200. count_highmem_image_pages(struct memory_bitmap *bm) { return 0; }
  1201. static inline int
  1202. prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
  1203. {
  1204. return 0;
  1205. }
  1206. static inline void *
  1207. get_highmem_page_buffer(struct page *page, struct chain_allocator *ca)
  1208. {
  1209. return NULL;
  1210. }
  1211. static inline void copy_last_highmem_page(void) {}
  1212. static inline int last_highmem_page_copied(void) { return 1; }
  1213. static inline void free_highmem_data(void) {}
  1214. #endif /* CONFIG_HIGHMEM */
  1215. /**
  1216. * prepare_image - use the memory bitmap @bm to mark the pages that will
  1217. * be overwritten in the process of restoring the system memory state
  1218. * from the suspend image ("unsafe" pages) and allocate memory for the
  1219. * image.
  1220. *
  1221. * The idea is to allocate a new memory bitmap first and then allocate
  1222. * as many pages as needed for the image data, but not to assign these
  1223. * pages to specific tasks initially. Instead, we just mark them as
  1224. * allocated and create a lists of "safe" pages that will be used
  1225. * later. On systems with high memory a list of "safe" highmem pages is
  1226. * also created.
  1227. */
  1228. #define PBES_PER_LINKED_PAGE (LINKED_PAGE_DATA_SIZE / sizeof(struct pbe))
  1229. static int
  1230. prepare_image(struct memory_bitmap *new_bm, struct memory_bitmap *bm)
  1231. {
  1232. unsigned int nr_pages, nr_highmem;
  1233. struct linked_page *sp_list, *lp;
  1234. int error;
  1235. /* If there is no highmem, the buffer will not be necessary */
  1236. free_image_page(buffer, PG_UNSAFE_CLEAR);
  1237. buffer = NULL;
  1238. nr_highmem = count_highmem_image_pages(bm);
  1239. error = mark_unsafe_pages(bm);
  1240. if (error)
  1241. goto Free;
  1242. error = memory_bm_create(new_bm, GFP_ATOMIC, PG_SAFE);
  1243. if (error)
  1244. goto Free;
  1245. duplicate_memory_bitmap(new_bm, bm);
  1246. memory_bm_free(bm, PG_UNSAFE_KEEP);
  1247. if (nr_highmem > 0) {
  1248. error = prepare_highmem_image(bm, &nr_highmem);
  1249. if (error)
  1250. goto Free;
  1251. }
  1252. /* Reserve some safe pages for potential later use.
  1253. *
  1254. * NOTE: This way we make sure there will be enough safe pages for the
  1255. * chain_alloc() in get_buffer(). It is a bit wasteful, but
  1256. * nr_copy_pages cannot be greater than 50% of the memory anyway.
  1257. */
  1258. sp_list = NULL;
  1259. /* nr_copy_pages cannot be lesser than allocated_unsafe_pages */
  1260. nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
  1261. nr_pages = DIV_ROUND_UP(nr_pages, PBES_PER_LINKED_PAGE);
  1262. while (nr_pages > 0) {
  1263. lp = get_image_page(GFP_ATOMIC, PG_SAFE);
  1264. if (!lp) {
  1265. error = -ENOMEM;
  1266. goto Free;
  1267. }
  1268. lp->next = sp_list;
  1269. sp_list = lp;
  1270. nr_pages--;
  1271. }
  1272. /* Preallocate memory for the image */
  1273. safe_pages_list = NULL;
  1274. nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
  1275. while (nr_pages > 0) {
  1276. lp = (struct linked_page *)get_zeroed_page(GFP_ATOMIC);
  1277. if (!lp) {
  1278. error = -ENOMEM;
  1279. goto Free;
  1280. }
  1281. if (!PageNosaveFree(virt_to_page(lp))) {
  1282. /* The page is "safe", add it to the list */
  1283. lp->next = safe_pages_list;
  1284. safe_pages_list = lp;
  1285. }
  1286. /* Mark the page as allocated */
  1287. SetPageNosave(virt_to_page(lp));
  1288. SetPageNosaveFree(virt_to_page(lp));
  1289. nr_pages--;
  1290. }
  1291. /* Free the reserved safe pages so that chain_alloc() can use them */
  1292. while (sp_list) {
  1293. lp = sp_list->next;
  1294. free_image_page(sp_list, PG_UNSAFE_CLEAR);
  1295. sp_list = lp;
  1296. }
  1297. return 0;
  1298. Free:
  1299. swsusp_free();
  1300. return error;
  1301. }
  1302. /**
  1303. * get_buffer - compute the address that snapshot_write_next() should
  1304. * set for its caller to write to.
  1305. */
  1306. static void *get_buffer(struct memory_bitmap *bm, struct chain_allocator *ca)
  1307. {
  1308. struct pbe *pbe;
  1309. struct page *page = pfn_to_page(memory_bm_next_pfn(bm));
  1310. if (PageHighMem(page))
  1311. return get_highmem_page_buffer(page, ca);
  1312. if (PageNosave(page) && PageNosaveFree(page))
  1313. /* We have allocated the "original" page frame and we can
  1314. * use it directly to store the loaded page.
  1315. */
  1316. return page_address(page);
  1317. /* The "original" page frame has not been allocated and we have to
  1318. * use a "safe" page frame to store the loaded page.
  1319. */
  1320. pbe = chain_alloc(ca, sizeof(struct pbe));
  1321. if (!pbe) {
  1322. swsusp_free();
  1323. return NULL;
  1324. }
  1325. pbe->orig_address = page_address(page);
  1326. pbe->address = safe_pages_list;
  1327. safe_pages_list = safe_pages_list->next;
  1328. pbe->next = restore_pblist;
  1329. restore_pblist = pbe;
  1330. return pbe->address;
  1331. }
  1332. /**
  1333. * snapshot_write_next - used for writing the system memory snapshot.
  1334. *
  1335. * On the first call to it @handle should point to a zeroed
  1336. * snapshot_handle structure. The structure gets updated and a pointer
  1337. * to it should be passed to this function every next time.
  1338. *
  1339. * The @count parameter should contain the number of bytes the caller
  1340. * wants to write to the image. It must not be zero.
  1341. *
  1342. * On success the function returns a positive number. Then, the caller
  1343. * is allowed to write up to the returned number of bytes to the memory
  1344. * location computed by the data_of() macro. The number returned
  1345. * may be smaller than @count, but this only happens if the write would
  1346. * cross a page boundary otherwise.
  1347. *
  1348. * The function returns 0 to indicate the "end of file" condition,
  1349. * and a negative number is returned on error. In such cases the
  1350. * structure pointed to by @handle is not updated and should not be used
  1351. * any more.
  1352. */
  1353. int snapshot_write_next(struct snapshot_handle *handle, size_t count)
  1354. {
  1355. static struct chain_allocator ca;
  1356. int error = 0;
  1357. /* Check if we have already loaded the entire image */
  1358. if (handle->prev && handle->cur > nr_meta_pages + nr_copy_pages)
  1359. return 0;
  1360. if (handle->offset == 0) {
  1361. if (!buffer)
  1362. /* This makes the buffer be freed by swsusp_free() */
  1363. buffer = get_image_page(GFP_ATOMIC, PG_ANY);
  1364. if (!buffer)
  1365. return -ENOMEM;
  1366. handle->buffer = buffer;
  1367. }
  1368. handle->sync_read = 1;
  1369. if (handle->prev < handle->cur) {
  1370. if (handle->prev == 0) {
  1371. error = load_header(buffer);
  1372. if (error)
  1373. return error;
  1374. error = memory_bm_create(&copy_bm, GFP_ATOMIC, PG_ANY);
  1375. if (error)
  1376. return error;
  1377. } else if (handle->prev <= nr_meta_pages) {
  1378. unpack_orig_pfns(buffer, &copy_bm);
  1379. if (handle->prev == nr_meta_pages) {
  1380. error = prepare_image(&orig_bm, &copy_bm);
  1381. if (error)
  1382. return error;
  1383. chain_init(&ca, GFP_ATOMIC, PG_SAFE);
  1384. memory_bm_position_reset(&orig_bm);
  1385. restore_pblist = NULL;
  1386. handle->buffer = get_buffer(&orig_bm, &ca);
  1387. handle->sync_read = 0;
  1388. if (!handle->buffer)
  1389. return -ENOMEM;
  1390. }
  1391. } else {
  1392. copy_last_highmem_page();
  1393. handle->buffer = get_buffer(&orig_bm, &ca);
  1394. if (handle->buffer != buffer)
  1395. handle->sync_read = 0;
  1396. }
  1397. handle->prev = handle->cur;
  1398. }
  1399. handle->buf_offset = handle->cur_offset;
  1400. if (handle->cur_offset + count >= PAGE_SIZE) {
  1401. count = PAGE_SIZE - handle->cur_offset;
  1402. handle->cur_offset = 0;
  1403. handle->cur++;
  1404. } else {
  1405. handle->cur_offset += count;
  1406. }
  1407. handle->offset += count;
  1408. return count;
  1409. }
  1410. /**
  1411. * snapshot_write_finalize - must be called after the last call to
  1412. * snapshot_write_next() in case the last page in the image happens
  1413. * to be a highmem page and its contents should be stored in the
  1414. * highmem. Additionally, it releases the memory that will not be
  1415. * used any more.
  1416. */
  1417. void snapshot_write_finalize(struct snapshot_handle *handle)
  1418. {
  1419. copy_last_highmem_page();
  1420. /* Free only if we have loaded the image entirely */
  1421. if (handle->prev && handle->cur > nr_meta_pages + nr_copy_pages) {
  1422. memory_bm_free(&orig_bm, PG_UNSAFE_CLEAR);
  1423. free_highmem_data();
  1424. }
  1425. }
  1426. int snapshot_image_loaded(struct snapshot_handle *handle)
  1427. {
  1428. return !(!nr_copy_pages || !last_highmem_page_copied() ||
  1429. handle->cur <= nr_meta_pages + nr_copy_pages);
  1430. }
  1431. #ifdef CONFIG_HIGHMEM
  1432. /* Assumes that @buf is ready and points to a "safe" page */
  1433. static inline void
  1434. swap_two_pages_data(struct page *p1, struct page *p2, void *buf)
  1435. {
  1436. void *kaddr1, *kaddr2;
  1437. kaddr1 = kmap_atomic(p1, KM_USER0);
  1438. kaddr2 = kmap_atomic(p2, KM_USER1);
  1439. memcpy(buf, kaddr1, PAGE_SIZE);
  1440. memcpy(kaddr1, kaddr2, PAGE_SIZE);
  1441. memcpy(kaddr2, buf, PAGE_SIZE);
  1442. kunmap_atomic(kaddr1, KM_USER0);
  1443. kunmap_atomic(kaddr2, KM_USER1);
  1444. }
  1445. /**
  1446. * restore_highmem - for each highmem page that was allocated before
  1447. * the suspend and included in the suspend image, and also has been
  1448. * allocated by the "resume" kernel swap its current (ie. "before
  1449. * resume") contents with the previous (ie. "before suspend") one.
  1450. *
  1451. * If the resume eventually fails, we can call this function once
  1452. * again and restore the "before resume" highmem state.
  1453. */
  1454. int restore_highmem(void)
  1455. {
  1456. struct highmem_pbe *pbe = highmem_pblist;
  1457. void *buf;
  1458. if (!pbe)
  1459. return 0;
  1460. buf = get_image_page(GFP_ATOMIC, PG_SAFE);
  1461. if (!buf)
  1462. return -ENOMEM;
  1463. while (pbe) {
  1464. swap_two_pages_data(pbe->copy_page, pbe->orig_page, buf);
  1465. pbe = pbe->next;
  1466. }
  1467. free_image_page(buf, PG_UNSAFE_CLEAR);
  1468. return 0;
  1469. }
  1470. #endif /* CONFIG_HIGHMEM */