snapshot.c 50 KB

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