snapshot.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601
  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@ucw.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 <linux/list.h>
  28. #include <linux/slab.h>
  29. #include <linux/compiler.h>
  30. #include <asm/uaccess.h>
  31. #include <asm/mmu_context.h>
  32. #include <asm/pgtable.h>
  33. #include <asm/tlbflush.h>
  34. #include <asm/io.h>
  35. #include "power.h"
  36. static int swsusp_page_is_free(struct page *);
  37. static void swsusp_set_page_forbidden(struct page *);
  38. static void swsusp_unset_page_forbidden(struct page *);
  39. /*
  40. * Number of bytes to reserve for memory allocations made by device drivers
  41. * from their ->freeze() and ->freeze_noirq() callbacks so that they don't
  42. * cause image creation to fail (tunable via /sys/power/reserved_size).
  43. */
  44. unsigned long reserved_size;
  45. void __init hibernate_reserved_size_init(void)
  46. {
  47. reserved_size = SPARE_PAGES * PAGE_SIZE;
  48. }
  49. /*
  50. * Preferred image size in bytes (tunable via /sys/power/image_size).
  51. * When it is set to N, swsusp will do its best to ensure the image
  52. * size will not exceed N bytes, but if that is impossible, it will
  53. * try to create the smallest image possible.
  54. */
  55. unsigned long image_size;
  56. void __init hibernate_image_size_init(void)
  57. {
  58. image_size = ((totalram_pages * 2) / 5) * PAGE_SIZE;
  59. }
  60. /* List of PBEs needed for restoring the pages that were allocated before
  61. * the suspend and included in the suspend image, but have also been
  62. * allocated by the "resume" kernel, so their contents cannot be written
  63. * directly to their "original" page frames.
  64. */
  65. struct pbe *restore_pblist;
  66. /* Pointer to an auxiliary buffer (1 page) */
  67. static void *buffer;
  68. /**
  69. * @safe_needed - on resume, for storing the PBE list and the image,
  70. * we can only use memory pages that do not conflict with the pages
  71. * used before suspend. The unsafe pages have PageNosaveFree set
  72. * and we count them using unsafe_pages.
  73. *
  74. * Each allocated image page is marked as PageNosave and PageNosaveFree
  75. * so that swsusp_free() can release it.
  76. */
  77. #define PG_ANY 0
  78. #define PG_SAFE 1
  79. #define PG_UNSAFE_CLEAR 1
  80. #define PG_UNSAFE_KEEP 0
  81. static unsigned int allocated_unsafe_pages;
  82. static void *get_image_page(gfp_t gfp_mask, int safe_needed)
  83. {
  84. void *res;
  85. res = (void *)get_zeroed_page(gfp_mask);
  86. if (safe_needed)
  87. while (res && swsusp_page_is_free(virt_to_page(res))) {
  88. /* The page is unsafe, mark it for swsusp_free() */
  89. swsusp_set_page_forbidden(virt_to_page(res));
  90. allocated_unsafe_pages++;
  91. res = (void *)get_zeroed_page(gfp_mask);
  92. }
  93. if (res) {
  94. swsusp_set_page_forbidden(virt_to_page(res));
  95. swsusp_set_page_free(virt_to_page(res));
  96. }
  97. return res;
  98. }
  99. unsigned long get_safe_page(gfp_t gfp_mask)
  100. {
  101. return (unsigned long)get_image_page(gfp_mask, PG_SAFE);
  102. }
  103. static struct page *alloc_image_page(gfp_t gfp_mask)
  104. {
  105. struct page *page;
  106. page = alloc_page(gfp_mask);
  107. if (page) {
  108. swsusp_set_page_forbidden(page);
  109. swsusp_set_page_free(page);
  110. }
  111. return page;
  112. }
  113. /**
  114. * free_image_page - free page represented by @addr, allocated with
  115. * get_image_page (page flags set by it must be cleared)
  116. */
  117. static inline void free_image_page(void *addr, int clear_nosave_free)
  118. {
  119. struct page *page;
  120. BUG_ON(!virt_addr_valid(addr));
  121. page = virt_to_page(addr);
  122. swsusp_unset_page_forbidden(page);
  123. if (clear_nosave_free)
  124. swsusp_unset_page_free(page);
  125. __free_page(page);
  126. }
  127. /* struct linked_page is used to build chains of pages */
  128. #define LINKED_PAGE_DATA_SIZE (PAGE_SIZE - sizeof(void *))
  129. struct linked_page {
  130. struct linked_page *next;
  131. char data[LINKED_PAGE_DATA_SIZE];
  132. } __packed;
  133. static inline void
  134. free_list_of_pages(struct linked_page *list, int clear_page_nosave)
  135. {
  136. while (list) {
  137. struct linked_page *lp = list->next;
  138. free_image_page(list, clear_page_nosave);
  139. list = lp;
  140. }
  141. }
  142. /**
  143. * struct chain_allocator is used for allocating small objects out of
  144. * a linked list of pages called 'the chain'.
  145. *
  146. * The chain grows each time when there is no room for a new object in
  147. * the current page. The allocated objects cannot be freed individually.
  148. * It is only possible to free them all at once, by freeing the entire
  149. * chain.
  150. *
  151. * NOTE: The chain allocator may be inefficient if the allocated objects
  152. * are not much smaller than PAGE_SIZE.
  153. */
  154. struct chain_allocator {
  155. struct linked_page *chain; /* the chain */
  156. unsigned int used_space; /* total size of objects allocated out
  157. * of the current page
  158. */
  159. gfp_t gfp_mask; /* mask for allocating pages */
  160. int safe_needed; /* if set, only "safe" pages are allocated */
  161. };
  162. static void
  163. chain_init(struct chain_allocator *ca, gfp_t gfp_mask, int safe_needed)
  164. {
  165. ca->chain = NULL;
  166. ca->used_space = LINKED_PAGE_DATA_SIZE;
  167. ca->gfp_mask = gfp_mask;
  168. ca->safe_needed = safe_needed;
  169. }
  170. static void *chain_alloc(struct chain_allocator *ca, unsigned int size)
  171. {
  172. void *ret;
  173. if (LINKED_PAGE_DATA_SIZE - ca->used_space < size) {
  174. struct linked_page *lp;
  175. lp = get_image_page(ca->gfp_mask, ca->safe_needed);
  176. if (!lp)
  177. return NULL;
  178. lp->next = ca->chain;
  179. ca->chain = lp;
  180. ca->used_space = 0;
  181. }
  182. ret = ca->chain->data + ca->used_space;
  183. ca->used_space += size;
  184. return ret;
  185. }
  186. /**
  187. * Data types related to memory bitmaps.
  188. *
  189. * Memory bitmap is a structure consiting of many linked lists of
  190. * objects. The main list's elements are of type struct zone_bitmap
  191. * and each of them corresonds to one zone. For each zone bitmap
  192. * object there is a list of objects of type struct bm_block that
  193. * represent each blocks of bitmap in which information is stored.
  194. *
  195. * struct memory_bitmap contains a pointer to the main list of zone
  196. * bitmap objects, a struct bm_position used for browsing the bitmap,
  197. * and a pointer to the list of pages used for allocating all of the
  198. * zone bitmap objects and bitmap block objects.
  199. *
  200. * NOTE: It has to be possible to lay out the bitmap in memory
  201. * using only allocations of order 0. Additionally, the bitmap is
  202. * designed to work with arbitrary number of zones (this is over the
  203. * top for now, but let's avoid making unnecessary assumptions ;-).
  204. *
  205. * struct zone_bitmap contains a pointer to a list of bitmap block
  206. * objects and a pointer to the bitmap block object that has been
  207. * most recently used for setting bits. Additionally, it contains the
  208. * pfns that correspond to the start and end of the represented zone.
  209. *
  210. * struct bm_block contains a pointer to the memory page in which
  211. * information is stored (in the form of a block of bitmap)
  212. * It also contains the pfns that correspond to the start and end of
  213. * the represented memory area.
  214. *
  215. * The memory bitmap is organized as a radix tree to guarantee fast random
  216. * access to the bits. There is one radix tree for each zone (as returned
  217. * from create_mem_extents).
  218. *
  219. * One radix tree is represented by one struct mem_zone_bm_rtree. There are
  220. * two linked lists for the nodes of the tree, one for the inner nodes and
  221. * one for the leave nodes. The linked leave nodes are used for fast linear
  222. * access of the memory bitmap.
  223. *
  224. * The struct rtree_node represents one node of the radix tree.
  225. */
  226. #define BM_END_OF_MAP (~0UL)
  227. #define BM_BITS_PER_BLOCK (PAGE_SIZE * BITS_PER_BYTE)
  228. #define BM_BLOCK_SHIFT (PAGE_SHIFT + 3)
  229. #define BM_BLOCK_MASK ((1UL << BM_BLOCK_SHIFT) - 1)
  230. /*
  231. * struct rtree_node is a wrapper struct to link the nodes
  232. * of the rtree together for easy linear iteration over
  233. * bits and easy freeing
  234. */
  235. struct rtree_node {
  236. struct list_head list;
  237. unsigned long *data;
  238. };
  239. /*
  240. * struct mem_zone_bm_rtree represents a bitmap used for one
  241. * populated memory zone.
  242. */
  243. struct mem_zone_bm_rtree {
  244. struct list_head list; /* Link Zones together */
  245. struct list_head nodes; /* Radix Tree inner nodes */
  246. struct list_head leaves; /* Radix Tree leaves */
  247. unsigned long start_pfn; /* Zone start page frame */
  248. unsigned long end_pfn; /* Zone end page frame + 1 */
  249. struct rtree_node *rtree; /* Radix Tree Root */
  250. int levels; /* Number of Radix Tree Levels */
  251. unsigned int blocks; /* Number of Bitmap Blocks */
  252. };
  253. /* strcut bm_position is used for browsing memory bitmaps */
  254. struct bm_position {
  255. struct mem_zone_bm_rtree *zone;
  256. struct rtree_node *node;
  257. unsigned long node_pfn;
  258. int node_bit;
  259. };
  260. struct memory_bitmap {
  261. struct list_head zones;
  262. struct linked_page *p_list; /* list of pages used to store zone
  263. * bitmap objects and bitmap block
  264. * objects
  265. */
  266. struct bm_position cur; /* most recently used bit position */
  267. };
  268. /* Functions that operate on memory bitmaps */
  269. #define BM_ENTRIES_PER_LEVEL (PAGE_SIZE / sizeof(unsigned long))
  270. #if BITS_PER_LONG == 32
  271. #define BM_RTREE_LEVEL_SHIFT (PAGE_SHIFT - 2)
  272. #else
  273. #define BM_RTREE_LEVEL_SHIFT (PAGE_SHIFT - 3)
  274. #endif
  275. #define BM_RTREE_LEVEL_MASK ((1UL << BM_RTREE_LEVEL_SHIFT) - 1)
  276. /*
  277. * alloc_rtree_node - Allocate a new node and add it to the radix tree.
  278. *
  279. * This function is used to allocate inner nodes as well as the
  280. * leave nodes of the radix tree. It also adds the node to the
  281. * corresponding linked list passed in by the *list parameter.
  282. */
  283. static struct rtree_node *alloc_rtree_node(gfp_t gfp_mask, int safe_needed,
  284. struct chain_allocator *ca,
  285. struct list_head *list)
  286. {
  287. struct rtree_node *node;
  288. node = chain_alloc(ca, sizeof(struct rtree_node));
  289. if (!node)
  290. return NULL;
  291. node->data = get_image_page(gfp_mask, safe_needed);
  292. if (!node->data)
  293. return NULL;
  294. list_add_tail(&node->list, list);
  295. return node;
  296. }
  297. /*
  298. * add_rtree_block - Add a new leave node to the radix tree
  299. *
  300. * The leave nodes need to be allocated in order to keep the leaves
  301. * linked list in order. This is guaranteed by the zone->blocks
  302. * counter.
  303. */
  304. static int add_rtree_block(struct mem_zone_bm_rtree *zone, gfp_t gfp_mask,
  305. int safe_needed, struct chain_allocator *ca)
  306. {
  307. struct rtree_node *node, *block, **dst;
  308. unsigned int levels_needed, block_nr;
  309. int i;
  310. block_nr = zone->blocks;
  311. levels_needed = 0;
  312. /* How many levels do we need for this block nr? */
  313. while (block_nr) {
  314. levels_needed += 1;
  315. block_nr >>= BM_RTREE_LEVEL_SHIFT;
  316. }
  317. /* Make sure the rtree has enough levels */
  318. for (i = zone->levels; i < levels_needed; i++) {
  319. node = alloc_rtree_node(gfp_mask, safe_needed, ca,
  320. &zone->nodes);
  321. if (!node)
  322. return -ENOMEM;
  323. node->data[0] = (unsigned long)zone->rtree;
  324. zone->rtree = node;
  325. zone->levels += 1;
  326. }
  327. /* Allocate new block */
  328. block = alloc_rtree_node(gfp_mask, safe_needed, ca, &zone->leaves);
  329. if (!block)
  330. return -ENOMEM;
  331. /* Now walk the rtree to insert the block */
  332. node = zone->rtree;
  333. dst = &zone->rtree;
  334. block_nr = zone->blocks;
  335. for (i = zone->levels; i > 0; i--) {
  336. int index;
  337. if (!node) {
  338. node = alloc_rtree_node(gfp_mask, safe_needed, ca,
  339. &zone->nodes);
  340. if (!node)
  341. return -ENOMEM;
  342. *dst = node;
  343. }
  344. index = block_nr >> ((i - 1) * BM_RTREE_LEVEL_SHIFT);
  345. index &= BM_RTREE_LEVEL_MASK;
  346. dst = (struct rtree_node **)&((*dst)->data[index]);
  347. node = *dst;
  348. }
  349. zone->blocks += 1;
  350. *dst = block;
  351. return 0;
  352. }
  353. static void free_zone_bm_rtree(struct mem_zone_bm_rtree *zone,
  354. int clear_nosave_free);
  355. /*
  356. * create_zone_bm_rtree - create a radix tree for one zone
  357. *
  358. * Allocated the mem_zone_bm_rtree structure and initializes it.
  359. * This function also allocated and builds the radix tree for the
  360. * zone.
  361. */
  362. static struct mem_zone_bm_rtree *
  363. create_zone_bm_rtree(gfp_t gfp_mask, int safe_needed,
  364. struct chain_allocator *ca,
  365. unsigned long start, unsigned long end)
  366. {
  367. struct mem_zone_bm_rtree *zone;
  368. unsigned int i, nr_blocks;
  369. unsigned long pages;
  370. pages = end - start;
  371. zone = chain_alloc(ca, sizeof(struct mem_zone_bm_rtree));
  372. if (!zone)
  373. return NULL;
  374. INIT_LIST_HEAD(&zone->nodes);
  375. INIT_LIST_HEAD(&zone->leaves);
  376. zone->start_pfn = start;
  377. zone->end_pfn = end;
  378. nr_blocks = DIV_ROUND_UP(pages, BM_BITS_PER_BLOCK);
  379. for (i = 0; i < nr_blocks; i++) {
  380. if (add_rtree_block(zone, gfp_mask, safe_needed, ca)) {
  381. free_zone_bm_rtree(zone, PG_UNSAFE_CLEAR);
  382. return NULL;
  383. }
  384. }
  385. return zone;
  386. }
  387. /*
  388. * free_zone_bm_rtree - Free the memory of the radix tree
  389. *
  390. * Free all node pages of the radix tree. The mem_zone_bm_rtree
  391. * structure itself is not freed here nor are the rtree_node
  392. * structs.
  393. */
  394. static void free_zone_bm_rtree(struct mem_zone_bm_rtree *zone,
  395. int clear_nosave_free)
  396. {
  397. struct rtree_node *node;
  398. list_for_each_entry(node, &zone->nodes, list)
  399. free_image_page(node->data, clear_nosave_free);
  400. list_for_each_entry(node, &zone->leaves, list)
  401. free_image_page(node->data, clear_nosave_free);
  402. }
  403. static void memory_bm_position_reset(struct memory_bitmap *bm)
  404. {
  405. bm->cur.zone = list_entry(bm->zones.next, struct mem_zone_bm_rtree,
  406. list);
  407. bm->cur.node = list_entry(bm->cur.zone->leaves.next,
  408. struct rtree_node, list);
  409. bm->cur.node_pfn = 0;
  410. bm->cur.node_bit = 0;
  411. }
  412. static void memory_bm_free(struct memory_bitmap *bm, int clear_nosave_free);
  413. struct mem_extent {
  414. struct list_head hook;
  415. unsigned long start;
  416. unsigned long end;
  417. };
  418. /**
  419. * free_mem_extents - free a list of memory extents
  420. * @list - list of extents to empty
  421. */
  422. static void free_mem_extents(struct list_head *list)
  423. {
  424. struct mem_extent *ext, *aux;
  425. list_for_each_entry_safe(ext, aux, list, hook) {
  426. list_del(&ext->hook);
  427. kfree(ext);
  428. }
  429. }
  430. /**
  431. * create_mem_extents - create a list of memory extents representing
  432. * contiguous ranges of PFNs
  433. * @list - list to put the extents into
  434. * @gfp_mask - mask to use for memory allocations
  435. */
  436. static int create_mem_extents(struct list_head *list, gfp_t gfp_mask)
  437. {
  438. struct zone *zone;
  439. INIT_LIST_HEAD(list);
  440. for_each_populated_zone(zone) {
  441. unsigned long zone_start, zone_end;
  442. struct mem_extent *ext, *cur, *aux;
  443. zone_start = zone->zone_start_pfn;
  444. zone_end = zone_end_pfn(zone);
  445. list_for_each_entry(ext, list, hook)
  446. if (zone_start <= ext->end)
  447. break;
  448. if (&ext->hook == list || zone_end < ext->start) {
  449. /* New extent is necessary */
  450. struct mem_extent *new_ext;
  451. new_ext = kzalloc(sizeof(struct mem_extent), gfp_mask);
  452. if (!new_ext) {
  453. free_mem_extents(list);
  454. return -ENOMEM;
  455. }
  456. new_ext->start = zone_start;
  457. new_ext->end = zone_end;
  458. list_add_tail(&new_ext->hook, &ext->hook);
  459. continue;
  460. }
  461. /* Merge this zone's range of PFNs with the existing one */
  462. if (zone_start < ext->start)
  463. ext->start = zone_start;
  464. if (zone_end > ext->end)
  465. ext->end = zone_end;
  466. /* More merging may be possible */
  467. cur = ext;
  468. list_for_each_entry_safe_continue(cur, aux, list, hook) {
  469. if (zone_end < cur->start)
  470. break;
  471. if (zone_end < cur->end)
  472. ext->end = cur->end;
  473. list_del(&cur->hook);
  474. kfree(cur);
  475. }
  476. }
  477. return 0;
  478. }
  479. /**
  480. * memory_bm_create - allocate memory for a memory bitmap
  481. */
  482. static int
  483. memory_bm_create(struct memory_bitmap *bm, gfp_t gfp_mask, int safe_needed)
  484. {
  485. struct chain_allocator ca;
  486. struct list_head mem_extents;
  487. struct mem_extent *ext;
  488. int error;
  489. chain_init(&ca, gfp_mask, safe_needed);
  490. INIT_LIST_HEAD(&bm->zones);
  491. error = create_mem_extents(&mem_extents, gfp_mask);
  492. if (error)
  493. return error;
  494. list_for_each_entry(ext, &mem_extents, hook) {
  495. struct mem_zone_bm_rtree *zone;
  496. zone = create_zone_bm_rtree(gfp_mask, safe_needed, &ca,
  497. ext->start, ext->end);
  498. if (!zone) {
  499. error = -ENOMEM;
  500. goto Error;
  501. }
  502. list_add_tail(&zone->list, &bm->zones);
  503. }
  504. bm->p_list = ca.chain;
  505. memory_bm_position_reset(bm);
  506. Exit:
  507. free_mem_extents(&mem_extents);
  508. return error;
  509. Error:
  510. bm->p_list = ca.chain;
  511. memory_bm_free(bm, PG_UNSAFE_CLEAR);
  512. goto Exit;
  513. }
  514. /**
  515. * memory_bm_free - free memory occupied by the memory bitmap @bm
  516. */
  517. static void memory_bm_free(struct memory_bitmap *bm, int clear_nosave_free)
  518. {
  519. struct mem_zone_bm_rtree *zone;
  520. list_for_each_entry(zone, &bm->zones, list)
  521. free_zone_bm_rtree(zone, clear_nosave_free);
  522. free_list_of_pages(bm->p_list, clear_nosave_free);
  523. INIT_LIST_HEAD(&bm->zones);
  524. }
  525. /**
  526. * memory_bm_find_bit - Find the bit for pfn in the memory
  527. * bitmap
  528. *
  529. * Find the bit in the bitmap @bm that corresponds to given pfn.
  530. * The cur.zone, cur.block and cur.node_pfn member of @bm are
  531. * updated.
  532. * It walks the radix tree to find the page which contains the bit for
  533. * pfn and returns the bit position in **addr and *bit_nr.
  534. */
  535. static int memory_bm_find_bit(struct memory_bitmap *bm, unsigned long pfn,
  536. void **addr, unsigned int *bit_nr)
  537. {
  538. struct mem_zone_bm_rtree *curr, *zone;
  539. struct rtree_node *node;
  540. int i, block_nr;
  541. zone = bm->cur.zone;
  542. if (pfn >= zone->start_pfn && pfn < zone->end_pfn)
  543. goto zone_found;
  544. zone = NULL;
  545. /* Find the right zone */
  546. list_for_each_entry(curr, &bm->zones, list) {
  547. if (pfn >= curr->start_pfn && pfn < curr->end_pfn) {
  548. zone = curr;
  549. break;
  550. }
  551. }
  552. if (!zone)
  553. return -EFAULT;
  554. zone_found:
  555. /*
  556. * We have a zone. Now walk the radix tree to find the leave
  557. * node for our pfn.
  558. */
  559. node = bm->cur.node;
  560. if (((pfn - zone->start_pfn) & ~BM_BLOCK_MASK) == bm->cur.node_pfn)
  561. goto node_found;
  562. node = zone->rtree;
  563. block_nr = (pfn - zone->start_pfn) >> BM_BLOCK_SHIFT;
  564. for (i = zone->levels; i > 0; i--) {
  565. int index;
  566. index = block_nr >> ((i - 1) * BM_RTREE_LEVEL_SHIFT);
  567. index &= BM_RTREE_LEVEL_MASK;
  568. BUG_ON(node->data[index] == 0);
  569. node = (struct rtree_node *)node->data[index];
  570. }
  571. node_found:
  572. /* Update last position */
  573. bm->cur.zone = zone;
  574. bm->cur.node = node;
  575. bm->cur.node_pfn = (pfn - zone->start_pfn) & ~BM_BLOCK_MASK;
  576. /* Set return values */
  577. *addr = node->data;
  578. *bit_nr = (pfn - zone->start_pfn) & BM_BLOCK_MASK;
  579. return 0;
  580. }
  581. static void memory_bm_set_bit(struct memory_bitmap *bm, unsigned long pfn)
  582. {
  583. void *addr;
  584. unsigned int bit;
  585. int error;
  586. error = memory_bm_find_bit(bm, pfn, &addr, &bit);
  587. BUG_ON(error);
  588. set_bit(bit, addr);
  589. }
  590. static int mem_bm_set_bit_check(struct memory_bitmap *bm, unsigned long pfn)
  591. {
  592. void *addr;
  593. unsigned int bit;
  594. int error;
  595. error = memory_bm_find_bit(bm, pfn, &addr, &bit);
  596. if (!error)
  597. set_bit(bit, addr);
  598. return error;
  599. }
  600. static void memory_bm_clear_bit(struct memory_bitmap *bm, unsigned long pfn)
  601. {
  602. void *addr;
  603. unsigned int bit;
  604. int error;
  605. error = memory_bm_find_bit(bm, pfn, &addr, &bit);
  606. BUG_ON(error);
  607. clear_bit(bit, addr);
  608. }
  609. static void memory_bm_clear_current(struct memory_bitmap *bm)
  610. {
  611. int bit;
  612. bit = max(bm->cur.node_bit - 1, 0);
  613. clear_bit(bit, bm->cur.node->data);
  614. }
  615. static int memory_bm_test_bit(struct memory_bitmap *bm, unsigned long pfn)
  616. {
  617. void *addr;
  618. unsigned int bit;
  619. int error;
  620. error = memory_bm_find_bit(bm, pfn, &addr, &bit);
  621. BUG_ON(error);
  622. return test_bit(bit, addr);
  623. }
  624. static bool memory_bm_pfn_present(struct memory_bitmap *bm, unsigned long pfn)
  625. {
  626. void *addr;
  627. unsigned int bit;
  628. return !memory_bm_find_bit(bm, pfn, &addr, &bit);
  629. }
  630. /*
  631. * rtree_next_node - Jumps to the next leave node
  632. *
  633. * Sets the position to the beginning of the next node in the
  634. * memory bitmap. This is either the next node in the current
  635. * zone's radix tree or the first node in the radix tree of the
  636. * next zone.
  637. *
  638. * Returns true if there is a next node, false otherwise.
  639. */
  640. static bool rtree_next_node(struct memory_bitmap *bm)
  641. {
  642. bm->cur.node = list_entry(bm->cur.node->list.next,
  643. struct rtree_node, list);
  644. if (&bm->cur.node->list != &bm->cur.zone->leaves) {
  645. bm->cur.node_pfn += BM_BITS_PER_BLOCK;
  646. bm->cur.node_bit = 0;
  647. touch_softlockup_watchdog();
  648. return true;
  649. }
  650. /* No more nodes, goto next zone */
  651. bm->cur.zone = list_entry(bm->cur.zone->list.next,
  652. struct mem_zone_bm_rtree, list);
  653. if (&bm->cur.zone->list != &bm->zones) {
  654. bm->cur.node = list_entry(bm->cur.zone->leaves.next,
  655. struct rtree_node, list);
  656. bm->cur.node_pfn = 0;
  657. bm->cur.node_bit = 0;
  658. return true;
  659. }
  660. /* No more zones */
  661. return false;
  662. }
  663. /**
  664. * memory_bm_rtree_next_pfn - Find the next set bit in the bitmap @bm
  665. *
  666. * Starting from the last returned position this function searches
  667. * for the next set bit in the memory bitmap and returns its
  668. * number. If no more bit is set BM_END_OF_MAP is returned.
  669. *
  670. * It is required to run memory_bm_position_reset() before the
  671. * first call to this function.
  672. */
  673. static unsigned long memory_bm_next_pfn(struct memory_bitmap *bm)
  674. {
  675. unsigned long bits, pfn, pages;
  676. int bit;
  677. do {
  678. pages = bm->cur.zone->end_pfn - bm->cur.zone->start_pfn;
  679. bits = min(pages - bm->cur.node_pfn, BM_BITS_PER_BLOCK);
  680. bit = find_next_bit(bm->cur.node->data, bits,
  681. bm->cur.node_bit);
  682. if (bit < bits) {
  683. pfn = bm->cur.zone->start_pfn + bm->cur.node_pfn + bit;
  684. bm->cur.node_bit = bit + 1;
  685. return pfn;
  686. }
  687. } while (rtree_next_node(bm));
  688. return BM_END_OF_MAP;
  689. }
  690. /**
  691. * This structure represents a range of page frames the contents of which
  692. * should not be saved during the suspend.
  693. */
  694. struct nosave_region {
  695. struct list_head list;
  696. unsigned long start_pfn;
  697. unsigned long end_pfn;
  698. };
  699. static LIST_HEAD(nosave_regions);
  700. /**
  701. * register_nosave_region - register a range of page frames the contents
  702. * of which should not be saved during the suspend (to be used in the early
  703. * initialization code)
  704. */
  705. void __init
  706. __register_nosave_region(unsigned long start_pfn, unsigned long end_pfn,
  707. int use_kmalloc)
  708. {
  709. struct nosave_region *region;
  710. if (start_pfn >= end_pfn)
  711. return;
  712. if (!list_empty(&nosave_regions)) {
  713. /* Try to extend the previous region (they should be sorted) */
  714. region = list_entry(nosave_regions.prev,
  715. struct nosave_region, list);
  716. if (region->end_pfn == start_pfn) {
  717. region->end_pfn = end_pfn;
  718. goto Report;
  719. }
  720. }
  721. if (use_kmalloc) {
  722. /* during init, this shouldn't fail */
  723. region = kmalloc(sizeof(struct nosave_region), GFP_KERNEL);
  724. BUG_ON(!region);
  725. } else
  726. /* This allocation cannot fail */
  727. region = memblock_virt_alloc(sizeof(struct nosave_region), 0);
  728. region->start_pfn = start_pfn;
  729. region->end_pfn = end_pfn;
  730. list_add_tail(&region->list, &nosave_regions);
  731. Report:
  732. printk(KERN_INFO "PM: Registered nosave memory: [mem %#010llx-%#010llx]\n",
  733. (unsigned long long) start_pfn << PAGE_SHIFT,
  734. ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
  735. }
  736. /*
  737. * Set bits in this map correspond to the page frames the contents of which
  738. * should not be saved during the suspend.
  739. */
  740. static struct memory_bitmap *forbidden_pages_map;
  741. /* Set bits in this map correspond to free page frames. */
  742. static struct memory_bitmap *free_pages_map;
  743. /*
  744. * Each page frame allocated for creating the image is marked by setting the
  745. * corresponding bits in forbidden_pages_map and free_pages_map simultaneously
  746. */
  747. void swsusp_set_page_free(struct page *page)
  748. {
  749. if (free_pages_map)
  750. memory_bm_set_bit(free_pages_map, page_to_pfn(page));
  751. }
  752. static int swsusp_page_is_free(struct page *page)
  753. {
  754. return free_pages_map ?
  755. memory_bm_test_bit(free_pages_map, page_to_pfn(page)) : 0;
  756. }
  757. void swsusp_unset_page_free(struct page *page)
  758. {
  759. if (free_pages_map)
  760. memory_bm_clear_bit(free_pages_map, page_to_pfn(page));
  761. }
  762. static void swsusp_set_page_forbidden(struct page *page)
  763. {
  764. if (forbidden_pages_map)
  765. memory_bm_set_bit(forbidden_pages_map, page_to_pfn(page));
  766. }
  767. int swsusp_page_is_forbidden(struct page *page)
  768. {
  769. return forbidden_pages_map ?
  770. memory_bm_test_bit(forbidden_pages_map, page_to_pfn(page)) : 0;
  771. }
  772. static void swsusp_unset_page_forbidden(struct page *page)
  773. {
  774. if (forbidden_pages_map)
  775. memory_bm_clear_bit(forbidden_pages_map, page_to_pfn(page));
  776. }
  777. /**
  778. * mark_nosave_pages - set bits corresponding to the page frames the
  779. * contents of which should not be saved in a given bitmap.
  780. */
  781. static void mark_nosave_pages(struct memory_bitmap *bm)
  782. {
  783. struct nosave_region *region;
  784. if (list_empty(&nosave_regions))
  785. return;
  786. list_for_each_entry(region, &nosave_regions, list) {
  787. unsigned long pfn;
  788. pr_debug("PM: Marking nosave pages: [mem %#010llx-%#010llx]\n",
  789. (unsigned long long) region->start_pfn << PAGE_SHIFT,
  790. ((unsigned long long) region->end_pfn << PAGE_SHIFT)
  791. - 1);
  792. for (pfn = region->start_pfn; pfn < region->end_pfn; pfn++)
  793. if (pfn_valid(pfn)) {
  794. /*
  795. * It is safe to ignore the result of
  796. * mem_bm_set_bit_check() here, since we won't
  797. * touch the PFNs for which the error is
  798. * returned anyway.
  799. */
  800. mem_bm_set_bit_check(bm, pfn);
  801. }
  802. }
  803. }
  804. /**
  805. * create_basic_memory_bitmaps - create bitmaps needed for marking page
  806. * frames that should not be saved and free page frames. The pointers
  807. * forbidden_pages_map and free_pages_map are only modified if everything
  808. * goes well, because we don't want the bits to be used before both bitmaps
  809. * are set up.
  810. */
  811. int create_basic_memory_bitmaps(void)
  812. {
  813. struct memory_bitmap *bm1, *bm2;
  814. int error = 0;
  815. if (forbidden_pages_map && free_pages_map)
  816. return 0;
  817. else
  818. BUG_ON(forbidden_pages_map || free_pages_map);
  819. bm1 = kzalloc(sizeof(struct memory_bitmap), GFP_KERNEL);
  820. if (!bm1)
  821. return -ENOMEM;
  822. error = memory_bm_create(bm1, GFP_KERNEL, PG_ANY);
  823. if (error)
  824. goto Free_first_object;
  825. bm2 = kzalloc(sizeof(struct memory_bitmap), GFP_KERNEL);
  826. if (!bm2)
  827. goto Free_first_bitmap;
  828. error = memory_bm_create(bm2, GFP_KERNEL, PG_ANY);
  829. if (error)
  830. goto Free_second_object;
  831. forbidden_pages_map = bm1;
  832. free_pages_map = bm2;
  833. mark_nosave_pages(forbidden_pages_map);
  834. pr_debug("PM: Basic memory bitmaps created\n");
  835. return 0;
  836. Free_second_object:
  837. kfree(bm2);
  838. Free_first_bitmap:
  839. memory_bm_free(bm1, PG_UNSAFE_CLEAR);
  840. Free_first_object:
  841. kfree(bm1);
  842. return -ENOMEM;
  843. }
  844. /**
  845. * free_basic_memory_bitmaps - free memory bitmaps allocated by
  846. * create_basic_memory_bitmaps(). The auxiliary pointers are necessary
  847. * so that the bitmaps themselves are not referred to while they are being
  848. * freed.
  849. */
  850. void free_basic_memory_bitmaps(void)
  851. {
  852. struct memory_bitmap *bm1, *bm2;
  853. if (WARN_ON(!(forbidden_pages_map && free_pages_map)))
  854. return;
  855. bm1 = forbidden_pages_map;
  856. bm2 = free_pages_map;
  857. forbidden_pages_map = NULL;
  858. free_pages_map = NULL;
  859. memory_bm_free(bm1, PG_UNSAFE_CLEAR);
  860. kfree(bm1);
  861. memory_bm_free(bm2, PG_UNSAFE_CLEAR);
  862. kfree(bm2);
  863. pr_debug("PM: Basic memory bitmaps freed\n");
  864. }
  865. /**
  866. * snapshot_additional_pages - estimate the number of additional pages
  867. * be needed for setting up the suspend image data structures for given
  868. * zone (usually the returned value is greater than the exact number)
  869. */
  870. unsigned int snapshot_additional_pages(struct zone *zone)
  871. {
  872. unsigned int rtree, nodes;
  873. rtree = nodes = DIV_ROUND_UP(zone->spanned_pages, BM_BITS_PER_BLOCK);
  874. rtree += DIV_ROUND_UP(rtree * sizeof(struct rtree_node),
  875. LINKED_PAGE_DATA_SIZE);
  876. while (nodes > 1) {
  877. nodes = DIV_ROUND_UP(nodes, BM_ENTRIES_PER_LEVEL);
  878. rtree += nodes;
  879. }
  880. return 2 * rtree;
  881. }
  882. #ifdef CONFIG_HIGHMEM
  883. /**
  884. * count_free_highmem_pages - compute the total number of free highmem
  885. * pages, system-wide.
  886. */
  887. static unsigned int count_free_highmem_pages(void)
  888. {
  889. struct zone *zone;
  890. unsigned int cnt = 0;
  891. for_each_populated_zone(zone)
  892. if (is_highmem(zone))
  893. cnt += zone_page_state(zone, NR_FREE_PAGES);
  894. return cnt;
  895. }
  896. /**
  897. * saveable_highmem_page - Determine whether a highmem page should be
  898. * included in the suspend image.
  899. *
  900. * We should save the page if it isn't Nosave or NosaveFree, or Reserved,
  901. * and it isn't a part of a free chunk of pages.
  902. */
  903. static struct page *saveable_highmem_page(struct zone *zone, unsigned long pfn)
  904. {
  905. struct page *page;
  906. if (!pfn_valid(pfn))
  907. return NULL;
  908. page = pfn_to_page(pfn);
  909. if (page_zone(page) != zone)
  910. return NULL;
  911. BUG_ON(!PageHighMem(page));
  912. if (swsusp_page_is_forbidden(page) || swsusp_page_is_free(page) ||
  913. PageReserved(page))
  914. return NULL;
  915. if (page_is_guard(page))
  916. return NULL;
  917. return page;
  918. }
  919. /**
  920. * count_highmem_pages - compute the total number of saveable highmem
  921. * pages.
  922. */
  923. static unsigned int count_highmem_pages(void)
  924. {
  925. struct zone *zone;
  926. unsigned int n = 0;
  927. for_each_populated_zone(zone) {
  928. unsigned long pfn, max_zone_pfn;
  929. if (!is_highmem(zone))
  930. continue;
  931. mark_free_pages(zone);
  932. max_zone_pfn = zone_end_pfn(zone);
  933. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
  934. if (saveable_highmem_page(zone, pfn))
  935. n++;
  936. }
  937. return n;
  938. }
  939. #else
  940. static inline void *saveable_highmem_page(struct zone *z, unsigned long p)
  941. {
  942. return NULL;
  943. }
  944. #endif /* CONFIG_HIGHMEM */
  945. /**
  946. * saveable_page - Determine whether a non-highmem page should be included
  947. * in the suspend image.
  948. *
  949. * We should save the page if it isn't Nosave, and is not in the range
  950. * of pages statically defined as 'unsaveable', and it isn't a part of
  951. * a free chunk of pages.
  952. */
  953. static struct page *saveable_page(struct zone *zone, unsigned long pfn)
  954. {
  955. struct page *page;
  956. if (!pfn_valid(pfn))
  957. return NULL;
  958. page = pfn_to_page(pfn);
  959. if (page_zone(page) != zone)
  960. return NULL;
  961. BUG_ON(PageHighMem(page));
  962. if (swsusp_page_is_forbidden(page) || swsusp_page_is_free(page))
  963. return NULL;
  964. if (PageReserved(page)
  965. && (!kernel_page_present(page) || pfn_is_nosave(pfn)))
  966. return NULL;
  967. if (page_is_guard(page))
  968. return NULL;
  969. return page;
  970. }
  971. /**
  972. * count_data_pages - compute the total number of saveable non-highmem
  973. * pages.
  974. */
  975. static unsigned int count_data_pages(void)
  976. {
  977. struct zone *zone;
  978. unsigned long pfn, max_zone_pfn;
  979. unsigned int n = 0;
  980. for_each_populated_zone(zone) {
  981. if (is_highmem(zone))
  982. continue;
  983. mark_free_pages(zone);
  984. max_zone_pfn = zone_end_pfn(zone);
  985. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
  986. if (saveable_page(zone, pfn))
  987. n++;
  988. }
  989. return n;
  990. }
  991. /* This is needed, because copy_page and memcpy are not usable for copying
  992. * task structs.
  993. */
  994. static inline void do_copy_page(long *dst, long *src)
  995. {
  996. int n;
  997. for (n = PAGE_SIZE / sizeof(long); n; n--)
  998. *dst++ = *src++;
  999. }
  1000. /**
  1001. * safe_copy_page - check if the page we are going to copy is marked as
  1002. * present in the kernel page tables (this always is the case if
  1003. * CONFIG_DEBUG_PAGEALLOC is not set and in that case
  1004. * kernel_page_present() always returns 'true').
  1005. */
  1006. static void safe_copy_page(void *dst, struct page *s_page)
  1007. {
  1008. if (kernel_page_present(s_page)) {
  1009. do_copy_page(dst, page_address(s_page));
  1010. } else {
  1011. kernel_map_pages(s_page, 1, 1);
  1012. do_copy_page(dst, page_address(s_page));
  1013. kernel_map_pages(s_page, 1, 0);
  1014. }
  1015. }
  1016. #ifdef CONFIG_HIGHMEM
  1017. static inline struct page *
  1018. page_is_saveable(struct zone *zone, unsigned long pfn)
  1019. {
  1020. return is_highmem(zone) ?
  1021. saveable_highmem_page(zone, pfn) : saveable_page(zone, pfn);
  1022. }
  1023. static void copy_data_page(unsigned long dst_pfn, unsigned long src_pfn)
  1024. {
  1025. struct page *s_page, *d_page;
  1026. void *src, *dst;
  1027. s_page = pfn_to_page(src_pfn);
  1028. d_page = pfn_to_page(dst_pfn);
  1029. if (PageHighMem(s_page)) {
  1030. src = kmap_atomic(s_page);
  1031. dst = kmap_atomic(d_page);
  1032. do_copy_page(dst, src);
  1033. kunmap_atomic(dst);
  1034. kunmap_atomic(src);
  1035. } else {
  1036. if (PageHighMem(d_page)) {
  1037. /* Page pointed to by src may contain some kernel
  1038. * data modified by kmap_atomic()
  1039. */
  1040. safe_copy_page(buffer, s_page);
  1041. dst = kmap_atomic(d_page);
  1042. copy_page(dst, buffer);
  1043. kunmap_atomic(dst);
  1044. } else {
  1045. safe_copy_page(page_address(d_page), s_page);
  1046. }
  1047. }
  1048. }
  1049. #else
  1050. #define page_is_saveable(zone, pfn) saveable_page(zone, pfn)
  1051. static inline void copy_data_page(unsigned long dst_pfn, unsigned long src_pfn)
  1052. {
  1053. safe_copy_page(page_address(pfn_to_page(dst_pfn)),
  1054. pfn_to_page(src_pfn));
  1055. }
  1056. #endif /* CONFIG_HIGHMEM */
  1057. static void
  1058. copy_data_pages(struct memory_bitmap *copy_bm, struct memory_bitmap *orig_bm)
  1059. {
  1060. struct zone *zone;
  1061. unsigned long pfn;
  1062. for_each_populated_zone(zone) {
  1063. unsigned long max_zone_pfn;
  1064. mark_free_pages(zone);
  1065. max_zone_pfn = zone_end_pfn(zone);
  1066. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
  1067. if (page_is_saveable(zone, pfn))
  1068. memory_bm_set_bit(orig_bm, pfn);
  1069. }
  1070. memory_bm_position_reset(orig_bm);
  1071. memory_bm_position_reset(copy_bm);
  1072. for(;;) {
  1073. pfn = memory_bm_next_pfn(orig_bm);
  1074. if (unlikely(pfn == BM_END_OF_MAP))
  1075. break;
  1076. copy_data_page(memory_bm_next_pfn(copy_bm), pfn);
  1077. }
  1078. }
  1079. /* Total number of image pages */
  1080. static unsigned int nr_copy_pages;
  1081. /* Number of pages needed for saving the original pfns of the image pages */
  1082. static unsigned int nr_meta_pages;
  1083. /*
  1084. * Numbers of normal and highmem page frames allocated for hibernation image
  1085. * before suspending devices.
  1086. */
  1087. unsigned int alloc_normal, alloc_highmem;
  1088. /*
  1089. * Memory bitmap used for marking saveable pages (during hibernation) or
  1090. * hibernation image pages (during restore)
  1091. */
  1092. static struct memory_bitmap orig_bm;
  1093. /*
  1094. * Memory bitmap used during hibernation for marking allocated page frames that
  1095. * will contain copies of saveable pages. During restore it is initially used
  1096. * for marking hibernation image pages, but then the set bits from it are
  1097. * duplicated in @orig_bm and it is released. On highmem systems it is next
  1098. * used for marking "safe" highmem pages, but it has to be reinitialized for
  1099. * this purpose.
  1100. */
  1101. static struct memory_bitmap copy_bm;
  1102. /**
  1103. * swsusp_free - free pages allocated for the suspend.
  1104. *
  1105. * Suspend pages are alocated before the atomic copy is made, so we
  1106. * need to release them after the resume.
  1107. */
  1108. void swsusp_free(void)
  1109. {
  1110. unsigned long fb_pfn, fr_pfn;
  1111. memory_bm_position_reset(forbidden_pages_map);
  1112. memory_bm_position_reset(free_pages_map);
  1113. loop:
  1114. fr_pfn = memory_bm_next_pfn(free_pages_map);
  1115. fb_pfn = memory_bm_next_pfn(forbidden_pages_map);
  1116. /*
  1117. * Find the next bit set in both bitmaps. This is guaranteed to
  1118. * terminate when fb_pfn == fr_pfn == BM_END_OF_MAP.
  1119. */
  1120. do {
  1121. if (fb_pfn < fr_pfn)
  1122. fb_pfn = memory_bm_next_pfn(forbidden_pages_map);
  1123. if (fr_pfn < fb_pfn)
  1124. fr_pfn = memory_bm_next_pfn(free_pages_map);
  1125. } while (fb_pfn != fr_pfn);
  1126. if (fr_pfn != BM_END_OF_MAP && pfn_valid(fr_pfn)) {
  1127. struct page *page = pfn_to_page(fr_pfn);
  1128. memory_bm_clear_current(forbidden_pages_map);
  1129. memory_bm_clear_current(free_pages_map);
  1130. __free_page(page);
  1131. goto loop;
  1132. }
  1133. nr_copy_pages = 0;
  1134. nr_meta_pages = 0;
  1135. restore_pblist = NULL;
  1136. buffer = NULL;
  1137. alloc_normal = 0;
  1138. alloc_highmem = 0;
  1139. }
  1140. /* Helper functions used for the shrinking of memory. */
  1141. #define GFP_IMAGE (GFP_KERNEL | __GFP_NOWARN)
  1142. /**
  1143. * preallocate_image_pages - Allocate a number of pages for hibernation image
  1144. * @nr_pages: Number of page frames to allocate.
  1145. * @mask: GFP flags to use for the allocation.
  1146. *
  1147. * Return value: Number of page frames actually allocated
  1148. */
  1149. static unsigned long preallocate_image_pages(unsigned long nr_pages, gfp_t mask)
  1150. {
  1151. unsigned long nr_alloc = 0;
  1152. while (nr_pages > 0) {
  1153. struct page *page;
  1154. page = alloc_image_page(mask);
  1155. if (!page)
  1156. break;
  1157. memory_bm_set_bit(&copy_bm, page_to_pfn(page));
  1158. if (PageHighMem(page))
  1159. alloc_highmem++;
  1160. else
  1161. alloc_normal++;
  1162. nr_pages--;
  1163. nr_alloc++;
  1164. }
  1165. return nr_alloc;
  1166. }
  1167. static unsigned long preallocate_image_memory(unsigned long nr_pages,
  1168. unsigned long avail_normal)
  1169. {
  1170. unsigned long alloc;
  1171. if (avail_normal <= alloc_normal)
  1172. return 0;
  1173. alloc = avail_normal - alloc_normal;
  1174. if (nr_pages < alloc)
  1175. alloc = nr_pages;
  1176. return preallocate_image_pages(alloc, GFP_IMAGE);
  1177. }
  1178. #ifdef CONFIG_HIGHMEM
  1179. static unsigned long preallocate_image_highmem(unsigned long nr_pages)
  1180. {
  1181. return preallocate_image_pages(nr_pages, GFP_IMAGE | __GFP_HIGHMEM);
  1182. }
  1183. /**
  1184. * __fraction - Compute (an approximation of) x * (multiplier / base)
  1185. */
  1186. static unsigned long __fraction(u64 x, u64 multiplier, u64 base)
  1187. {
  1188. x *= multiplier;
  1189. do_div(x, base);
  1190. return (unsigned long)x;
  1191. }
  1192. static unsigned long preallocate_highmem_fraction(unsigned long nr_pages,
  1193. unsigned long highmem,
  1194. unsigned long total)
  1195. {
  1196. unsigned long alloc = __fraction(nr_pages, highmem, total);
  1197. return preallocate_image_pages(alloc, GFP_IMAGE | __GFP_HIGHMEM);
  1198. }
  1199. #else /* CONFIG_HIGHMEM */
  1200. static inline unsigned long preallocate_image_highmem(unsigned long nr_pages)
  1201. {
  1202. return 0;
  1203. }
  1204. static inline unsigned long preallocate_highmem_fraction(unsigned long nr_pages,
  1205. unsigned long highmem,
  1206. unsigned long total)
  1207. {
  1208. return 0;
  1209. }
  1210. #endif /* CONFIG_HIGHMEM */
  1211. /**
  1212. * free_unnecessary_pages - Release preallocated pages not needed for the image
  1213. */
  1214. static void free_unnecessary_pages(void)
  1215. {
  1216. unsigned long save, to_free_normal, to_free_highmem;
  1217. save = count_data_pages();
  1218. if (alloc_normal >= save) {
  1219. to_free_normal = alloc_normal - save;
  1220. save = 0;
  1221. } else {
  1222. to_free_normal = 0;
  1223. save -= alloc_normal;
  1224. }
  1225. save += count_highmem_pages();
  1226. if (alloc_highmem >= save) {
  1227. to_free_highmem = alloc_highmem - save;
  1228. } else {
  1229. to_free_highmem = 0;
  1230. save -= alloc_highmem;
  1231. if (to_free_normal > save)
  1232. to_free_normal -= save;
  1233. else
  1234. to_free_normal = 0;
  1235. }
  1236. memory_bm_position_reset(&copy_bm);
  1237. while (to_free_normal > 0 || to_free_highmem > 0) {
  1238. unsigned long pfn = memory_bm_next_pfn(&copy_bm);
  1239. struct page *page = pfn_to_page(pfn);
  1240. if (PageHighMem(page)) {
  1241. if (!to_free_highmem)
  1242. continue;
  1243. to_free_highmem--;
  1244. alloc_highmem--;
  1245. } else {
  1246. if (!to_free_normal)
  1247. continue;
  1248. to_free_normal--;
  1249. alloc_normal--;
  1250. }
  1251. memory_bm_clear_bit(&copy_bm, pfn);
  1252. swsusp_unset_page_forbidden(page);
  1253. swsusp_unset_page_free(page);
  1254. __free_page(page);
  1255. }
  1256. }
  1257. /**
  1258. * minimum_image_size - Estimate the minimum acceptable size of an image
  1259. * @saveable: Number of saveable pages in the system.
  1260. *
  1261. * We want to avoid attempting to free too much memory too hard, so estimate the
  1262. * minimum acceptable size of a hibernation image to use as the lower limit for
  1263. * preallocating memory.
  1264. *
  1265. * We assume that the minimum image size should be proportional to
  1266. *
  1267. * [number of saveable pages] - [number of pages that can be freed in theory]
  1268. *
  1269. * where the second term is the sum of (1) reclaimable slab pages, (2) active
  1270. * and (3) inactive anonymous pages, (4) active and (5) inactive file pages,
  1271. * minus mapped file pages.
  1272. */
  1273. static unsigned long minimum_image_size(unsigned long saveable)
  1274. {
  1275. unsigned long size;
  1276. size = global_page_state(NR_SLAB_RECLAIMABLE)
  1277. + global_page_state(NR_ACTIVE_ANON)
  1278. + global_page_state(NR_INACTIVE_ANON)
  1279. + global_page_state(NR_ACTIVE_FILE)
  1280. + global_page_state(NR_INACTIVE_FILE)
  1281. - global_page_state(NR_FILE_MAPPED);
  1282. return saveable <= size ? 0 : saveable - size;
  1283. }
  1284. /**
  1285. * hibernate_preallocate_memory - Preallocate memory for hibernation image
  1286. *
  1287. * To create a hibernation image it is necessary to make a copy of every page
  1288. * frame in use. We also need a number of page frames to be free during
  1289. * hibernation for allocations made while saving the image and for device
  1290. * drivers, in case they need to allocate memory from their hibernation
  1291. * callbacks (these two numbers are given by PAGES_FOR_IO (which is a rough
  1292. * estimate) and reserverd_size divided by PAGE_SIZE (which is tunable through
  1293. * /sys/power/reserved_size, respectively). To make this happen, we compute the
  1294. * total number of available page frames and allocate at least
  1295. *
  1296. * ([page frames total] + PAGES_FOR_IO + [metadata pages]) / 2
  1297. * + 2 * DIV_ROUND_UP(reserved_size, PAGE_SIZE)
  1298. *
  1299. * of them, which corresponds to the maximum size of a hibernation image.
  1300. *
  1301. * If image_size is set below the number following from the above formula,
  1302. * the preallocation of memory is continued until the total number of saveable
  1303. * pages in the system is below the requested image size or the minimum
  1304. * acceptable image size returned by minimum_image_size(), whichever is greater.
  1305. */
  1306. int hibernate_preallocate_memory(void)
  1307. {
  1308. struct zone *zone;
  1309. unsigned long saveable, size, max_size, count, highmem, pages = 0;
  1310. unsigned long alloc, save_highmem, pages_highmem, avail_normal;
  1311. struct timeval start, stop;
  1312. int error;
  1313. printk(KERN_INFO "PM: Preallocating image memory... ");
  1314. do_gettimeofday(&start);
  1315. error = memory_bm_create(&orig_bm, GFP_IMAGE, PG_ANY);
  1316. if (error)
  1317. goto err_out;
  1318. error = memory_bm_create(&copy_bm, GFP_IMAGE, PG_ANY);
  1319. if (error)
  1320. goto err_out;
  1321. alloc_normal = 0;
  1322. alloc_highmem = 0;
  1323. /* Count the number of saveable data pages. */
  1324. save_highmem = count_highmem_pages();
  1325. saveable = count_data_pages();
  1326. /*
  1327. * Compute the total number of page frames we can use (count) and the
  1328. * number of pages needed for image metadata (size).
  1329. */
  1330. count = saveable;
  1331. saveable += save_highmem;
  1332. highmem = save_highmem;
  1333. size = 0;
  1334. for_each_populated_zone(zone) {
  1335. size += snapshot_additional_pages(zone);
  1336. if (is_highmem(zone))
  1337. highmem += zone_page_state(zone, NR_FREE_PAGES);
  1338. else
  1339. count += zone_page_state(zone, NR_FREE_PAGES);
  1340. }
  1341. avail_normal = count;
  1342. count += highmem;
  1343. count -= totalreserve_pages;
  1344. /* Add number of pages required for page keys (s390 only). */
  1345. size += page_key_additional_pages(saveable);
  1346. /* Compute the maximum number of saveable pages to leave in memory. */
  1347. max_size = (count - (size + PAGES_FOR_IO)) / 2
  1348. - 2 * DIV_ROUND_UP(reserved_size, PAGE_SIZE);
  1349. /* Compute the desired number of image pages specified by image_size. */
  1350. size = DIV_ROUND_UP(image_size, PAGE_SIZE);
  1351. if (size > max_size)
  1352. size = max_size;
  1353. /*
  1354. * If the desired number of image pages is at least as large as the
  1355. * current number of saveable pages in memory, allocate page frames for
  1356. * the image and we're done.
  1357. */
  1358. if (size >= saveable) {
  1359. pages = preallocate_image_highmem(save_highmem);
  1360. pages += preallocate_image_memory(saveable - pages, avail_normal);
  1361. goto out;
  1362. }
  1363. /* Estimate the minimum size of the image. */
  1364. pages = minimum_image_size(saveable);
  1365. /*
  1366. * To avoid excessive pressure on the normal zone, leave room in it to
  1367. * accommodate an image of the minimum size (unless it's already too
  1368. * small, in which case don't preallocate pages from it at all).
  1369. */
  1370. if (avail_normal > pages)
  1371. avail_normal -= pages;
  1372. else
  1373. avail_normal = 0;
  1374. if (size < pages)
  1375. size = min_t(unsigned long, pages, max_size);
  1376. /*
  1377. * Let the memory management subsystem know that we're going to need a
  1378. * large number of page frames to allocate and make it free some memory.
  1379. * NOTE: If this is not done, performance will be hurt badly in some
  1380. * test cases.
  1381. */
  1382. shrink_all_memory(saveable - size);
  1383. /*
  1384. * The number of saveable pages in memory was too high, so apply some
  1385. * pressure to decrease it. First, make room for the largest possible
  1386. * image and fail if that doesn't work. Next, try to decrease the size
  1387. * of the image as much as indicated by 'size' using allocations from
  1388. * highmem and non-highmem zones separately.
  1389. */
  1390. pages_highmem = preallocate_image_highmem(highmem / 2);
  1391. alloc = count - max_size;
  1392. if (alloc > pages_highmem)
  1393. alloc -= pages_highmem;
  1394. else
  1395. alloc = 0;
  1396. pages = preallocate_image_memory(alloc, avail_normal);
  1397. if (pages < alloc) {
  1398. /* We have exhausted non-highmem pages, try highmem. */
  1399. alloc -= pages;
  1400. pages += pages_highmem;
  1401. pages_highmem = preallocate_image_highmem(alloc);
  1402. if (pages_highmem < alloc)
  1403. goto err_out;
  1404. pages += pages_highmem;
  1405. /*
  1406. * size is the desired number of saveable pages to leave in
  1407. * memory, so try to preallocate (all memory - size) pages.
  1408. */
  1409. alloc = (count - pages) - size;
  1410. pages += preallocate_image_highmem(alloc);
  1411. } else {
  1412. /*
  1413. * There are approximately max_size saveable pages at this point
  1414. * and we want to reduce this number down to size.
  1415. */
  1416. alloc = max_size - size;
  1417. size = preallocate_highmem_fraction(alloc, highmem, count);
  1418. pages_highmem += size;
  1419. alloc -= size;
  1420. size = preallocate_image_memory(alloc, avail_normal);
  1421. pages_highmem += preallocate_image_highmem(alloc - size);
  1422. pages += pages_highmem + size;
  1423. }
  1424. /*
  1425. * We only need as many page frames for the image as there are saveable
  1426. * pages in memory, but we have allocated more. Release the excessive
  1427. * ones now.
  1428. */
  1429. free_unnecessary_pages();
  1430. out:
  1431. do_gettimeofday(&stop);
  1432. printk(KERN_CONT "done (allocated %lu pages)\n", pages);
  1433. swsusp_show_speed(&start, &stop, pages, "Allocated");
  1434. return 0;
  1435. err_out:
  1436. printk(KERN_CONT "\n");
  1437. swsusp_free();
  1438. return -ENOMEM;
  1439. }
  1440. #ifdef CONFIG_HIGHMEM
  1441. /**
  1442. * count_pages_for_highmem - compute the number of non-highmem pages
  1443. * that will be necessary for creating copies of highmem pages.
  1444. */
  1445. static unsigned int count_pages_for_highmem(unsigned int nr_highmem)
  1446. {
  1447. unsigned int free_highmem = count_free_highmem_pages() + alloc_highmem;
  1448. if (free_highmem >= nr_highmem)
  1449. nr_highmem = 0;
  1450. else
  1451. nr_highmem -= free_highmem;
  1452. return nr_highmem;
  1453. }
  1454. #else
  1455. static unsigned int
  1456. count_pages_for_highmem(unsigned int nr_highmem) { return 0; }
  1457. #endif /* CONFIG_HIGHMEM */
  1458. /**
  1459. * enough_free_mem - Make sure we have enough free memory for the
  1460. * snapshot image.
  1461. */
  1462. static int enough_free_mem(unsigned int nr_pages, unsigned int nr_highmem)
  1463. {
  1464. struct zone *zone;
  1465. unsigned int free = alloc_normal;
  1466. for_each_populated_zone(zone)
  1467. if (!is_highmem(zone))
  1468. free += zone_page_state(zone, NR_FREE_PAGES);
  1469. nr_pages += count_pages_for_highmem(nr_highmem);
  1470. pr_debug("PM: Normal pages needed: %u + %u, available pages: %u\n",
  1471. nr_pages, PAGES_FOR_IO, free);
  1472. return free > nr_pages + PAGES_FOR_IO;
  1473. }
  1474. #ifdef CONFIG_HIGHMEM
  1475. /**
  1476. * get_highmem_buffer - if there are some highmem pages in the suspend
  1477. * image, we may need the buffer to copy them and/or load their data.
  1478. */
  1479. static inline int get_highmem_buffer(int safe_needed)
  1480. {
  1481. buffer = get_image_page(GFP_ATOMIC | __GFP_COLD, safe_needed);
  1482. return buffer ? 0 : -ENOMEM;
  1483. }
  1484. /**
  1485. * alloc_highmem_image_pages - allocate some highmem pages for the image.
  1486. * Try to allocate as many pages as needed, but if the number of free
  1487. * highmem pages is lesser than that, allocate them all.
  1488. */
  1489. static inline unsigned int
  1490. alloc_highmem_pages(struct memory_bitmap *bm, unsigned int nr_highmem)
  1491. {
  1492. unsigned int to_alloc = count_free_highmem_pages();
  1493. if (to_alloc > nr_highmem)
  1494. to_alloc = nr_highmem;
  1495. nr_highmem -= to_alloc;
  1496. while (to_alloc-- > 0) {
  1497. struct page *page;
  1498. page = alloc_image_page(__GFP_HIGHMEM);
  1499. memory_bm_set_bit(bm, page_to_pfn(page));
  1500. }
  1501. return nr_highmem;
  1502. }
  1503. #else
  1504. static inline int get_highmem_buffer(int safe_needed) { return 0; }
  1505. static inline unsigned int
  1506. alloc_highmem_pages(struct memory_bitmap *bm, unsigned int n) { return 0; }
  1507. #endif /* CONFIG_HIGHMEM */
  1508. /**
  1509. * swsusp_alloc - allocate memory for the suspend image
  1510. *
  1511. * We first try to allocate as many highmem pages as there are
  1512. * saveable highmem pages in the system. If that fails, we allocate
  1513. * non-highmem pages for the copies of the remaining highmem ones.
  1514. *
  1515. * In this approach it is likely that the copies of highmem pages will
  1516. * also be located in the high memory, because of the way in which
  1517. * copy_data_pages() works.
  1518. */
  1519. static int
  1520. swsusp_alloc(struct memory_bitmap *orig_bm, struct memory_bitmap *copy_bm,
  1521. unsigned int nr_pages, unsigned int nr_highmem)
  1522. {
  1523. if (nr_highmem > 0) {
  1524. if (get_highmem_buffer(PG_ANY))
  1525. goto err_out;
  1526. if (nr_highmem > alloc_highmem) {
  1527. nr_highmem -= alloc_highmem;
  1528. nr_pages += alloc_highmem_pages(copy_bm, nr_highmem);
  1529. }
  1530. }
  1531. if (nr_pages > alloc_normal) {
  1532. nr_pages -= alloc_normal;
  1533. while (nr_pages-- > 0) {
  1534. struct page *page;
  1535. page = alloc_image_page(GFP_ATOMIC | __GFP_COLD);
  1536. if (!page)
  1537. goto err_out;
  1538. memory_bm_set_bit(copy_bm, page_to_pfn(page));
  1539. }
  1540. }
  1541. return 0;
  1542. err_out:
  1543. swsusp_free();
  1544. return -ENOMEM;
  1545. }
  1546. asmlinkage __visible int swsusp_save(void)
  1547. {
  1548. unsigned int nr_pages, nr_highmem;
  1549. printk(KERN_INFO "PM: Creating hibernation image:\n");
  1550. drain_local_pages(NULL);
  1551. nr_pages = count_data_pages();
  1552. nr_highmem = count_highmem_pages();
  1553. printk(KERN_INFO "PM: Need to copy %u pages\n", nr_pages + nr_highmem);
  1554. if (!enough_free_mem(nr_pages, nr_highmem)) {
  1555. printk(KERN_ERR "PM: Not enough free memory\n");
  1556. return -ENOMEM;
  1557. }
  1558. if (swsusp_alloc(&orig_bm, &copy_bm, nr_pages, nr_highmem)) {
  1559. printk(KERN_ERR "PM: Memory allocation failed\n");
  1560. return -ENOMEM;
  1561. }
  1562. /* During allocating of suspend pagedir, new cold pages may appear.
  1563. * Kill them.
  1564. */
  1565. drain_local_pages(NULL);
  1566. copy_data_pages(&copy_bm, &orig_bm);
  1567. /*
  1568. * End of critical section. From now on, we can write to memory,
  1569. * but we should not touch disk. This specially means we must _not_
  1570. * touch swap space! Except we must write out our image of course.
  1571. */
  1572. nr_pages += nr_highmem;
  1573. nr_copy_pages = nr_pages;
  1574. nr_meta_pages = DIV_ROUND_UP(nr_pages * sizeof(long), PAGE_SIZE);
  1575. printk(KERN_INFO "PM: Hibernation image created (%d pages copied)\n",
  1576. nr_pages);
  1577. return 0;
  1578. }
  1579. #ifndef CONFIG_ARCH_HIBERNATION_HEADER
  1580. static int init_header_complete(struct swsusp_info *info)
  1581. {
  1582. memcpy(&info->uts, init_utsname(), sizeof(struct new_utsname));
  1583. info->version_code = LINUX_VERSION_CODE;
  1584. return 0;
  1585. }
  1586. static char *check_image_kernel(struct swsusp_info *info)
  1587. {
  1588. if (info->version_code != LINUX_VERSION_CODE)
  1589. return "kernel version";
  1590. if (strcmp(info->uts.sysname,init_utsname()->sysname))
  1591. return "system type";
  1592. if (strcmp(info->uts.release,init_utsname()->release))
  1593. return "kernel release";
  1594. if (strcmp(info->uts.version,init_utsname()->version))
  1595. return "version";
  1596. if (strcmp(info->uts.machine,init_utsname()->machine))
  1597. return "machine";
  1598. return NULL;
  1599. }
  1600. #endif /* CONFIG_ARCH_HIBERNATION_HEADER */
  1601. unsigned long snapshot_get_image_size(void)
  1602. {
  1603. return nr_copy_pages + nr_meta_pages + 1;
  1604. }
  1605. static int init_header(struct swsusp_info *info)
  1606. {
  1607. memset(info, 0, sizeof(struct swsusp_info));
  1608. info->num_physpages = get_num_physpages();
  1609. info->image_pages = nr_copy_pages;
  1610. info->pages = snapshot_get_image_size();
  1611. info->size = info->pages;
  1612. info->size <<= PAGE_SHIFT;
  1613. return init_header_complete(info);
  1614. }
  1615. /**
  1616. * pack_pfns - pfns corresponding to the set bits found in the bitmap @bm
  1617. * are stored in the array @buf[] (1 page at a time)
  1618. */
  1619. static inline void
  1620. pack_pfns(unsigned long *buf, struct memory_bitmap *bm)
  1621. {
  1622. int j;
  1623. for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
  1624. buf[j] = memory_bm_next_pfn(bm);
  1625. if (unlikely(buf[j] == BM_END_OF_MAP))
  1626. break;
  1627. /* Save page key for data page (s390 only). */
  1628. page_key_read(buf + j);
  1629. }
  1630. }
  1631. /**
  1632. * snapshot_read_next - used for reading the system memory snapshot.
  1633. *
  1634. * On the first call to it @handle should point to a zeroed
  1635. * snapshot_handle structure. The structure gets updated and a pointer
  1636. * to it should be passed to this function every next time.
  1637. *
  1638. * On success the function returns a positive number. Then, the caller
  1639. * is allowed to read up to the returned number of bytes from the memory
  1640. * location computed by the data_of() macro.
  1641. *
  1642. * The function returns 0 to indicate the end of data stream condition,
  1643. * and a negative number is returned on error. In such cases the
  1644. * structure pointed to by @handle is not updated and should not be used
  1645. * any more.
  1646. */
  1647. int snapshot_read_next(struct snapshot_handle *handle)
  1648. {
  1649. if (handle->cur > nr_meta_pages + nr_copy_pages)
  1650. return 0;
  1651. if (!buffer) {
  1652. /* This makes the buffer be freed by swsusp_free() */
  1653. buffer = get_image_page(GFP_ATOMIC, PG_ANY);
  1654. if (!buffer)
  1655. return -ENOMEM;
  1656. }
  1657. if (!handle->cur) {
  1658. int error;
  1659. error = init_header((struct swsusp_info *)buffer);
  1660. if (error)
  1661. return error;
  1662. handle->buffer = buffer;
  1663. memory_bm_position_reset(&orig_bm);
  1664. memory_bm_position_reset(&copy_bm);
  1665. } else if (handle->cur <= nr_meta_pages) {
  1666. clear_page(buffer);
  1667. pack_pfns(buffer, &orig_bm);
  1668. } else {
  1669. struct page *page;
  1670. page = pfn_to_page(memory_bm_next_pfn(&copy_bm));
  1671. if (PageHighMem(page)) {
  1672. /* Highmem pages are copied to the buffer,
  1673. * because we can't return with a kmapped
  1674. * highmem page (we may not be called again).
  1675. */
  1676. void *kaddr;
  1677. kaddr = kmap_atomic(page);
  1678. copy_page(buffer, kaddr);
  1679. kunmap_atomic(kaddr);
  1680. handle->buffer = buffer;
  1681. } else {
  1682. handle->buffer = page_address(page);
  1683. }
  1684. }
  1685. handle->cur++;
  1686. return PAGE_SIZE;
  1687. }
  1688. /**
  1689. * mark_unsafe_pages - mark the pages that cannot be used for storing
  1690. * the image during resume, because they conflict with the pages that
  1691. * had been used before suspend
  1692. */
  1693. static int mark_unsafe_pages(struct memory_bitmap *bm)
  1694. {
  1695. struct zone *zone;
  1696. unsigned long pfn, max_zone_pfn;
  1697. /* Clear page flags */
  1698. for_each_populated_zone(zone) {
  1699. max_zone_pfn = zone_end_pfn(zone);
  1700. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
  1701. if (pfn_valid(pfn))
  1702. swsusp_unset_page_free(pfn_to_page(pfn));
  1703. }
  1704. /* Mark pages that correspond to the "original" pfns as "unsafe" */
  1705. memory_bm_position_reset(bm);
  1706. do {
  1707. pfn = memory_bm_next_pfn(bm);
  1708. if (likely(pfn != BM_END_OF_MAP)) {
  1709. if (likely(pfn_valid(pfn)))
  1710. swsusp_set_page_free(pfn_to_page(pfn));
  1711. else
  1712. return -EFAULT;
  1713. }
  1714. } while (pfn != BM_END_OF_MAP);
  1715. allocated_unsafe_pages = 0;
  1716. return 0;
  1717. }
  1718. static void
  1719. duplicate_memory_bitmap(struct memory_bitmap *dst, struct memory_bitmap *src)
  1720. {
  1721. unsigned long pfn;
  1722. memory_bm_position_reset(src);
  1723. pfn = memory_bm_next_pfn(src);
  1724. while (pfn != BM_END_OF_MAP) {
  1725. memory_bm_set_bit(dst, pfn);
  1726. pfn = memory_bm_next_pfn(src);
  1727. }
  1728. }
  1729. static int check_header(struct swsusp_info *info)
  1730. {
  1731. char *reason;
  1732. reason = check_image_kernel(info);
  1733. if (!reason && info->num_physpages != get_num_physpages())
  1734. reason = "memory size";
  1735. if (reason) {
  1736. printk(KERN_ERR "PM: Image mismatch: %s\n", reason);
  1737. return -EPERM;
  1738. }
  1739. return 0;
  1740. }
  1741. /**
  1742. * load header - check the image header and copy data from it
  1743. */
  1744. static int
  1745. load_header(struct swsusp_info *info)
  1746. {
  1747. int error;
  1748. restore_pblist = NULL;
  1749. error = check_header(info);
  1750. if (!error) {
  1751. nr_copy_pages = info->image_pages;
  1752. nr_meta_pages = info->pages - info->image_pages - 1;
  1753. }
  1754. return error;
  1755. }
  1756. /**
  1757. * unpack_orig_pfns - for each element of @buf[] (1 page at a time) set
  1758. * the corresponding bit in the memory bitmap @bm
  1759. */
  1760. static int unpack_orig_pfns(unsigned long *buf, struct memory_bitmap *bm)
  1761. {
  1762. int j;
  1763. for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
  1764. if (unlikely(buf[j] == BM_END_OF_MAP))
  1765. break;
  1766. /* Extract and buffer page key for data page (s390 only). */
  1767. page_key_memorize(buf + j);
  1768. if (memory_bm_pfn_present(bm, buf[j]))
  1769. memory_bm_set_bit(bm, buf[j]);
  1770. else
  1771. return -EFAULT;
  1772. }
  1773. return 0;
  1774. }
  1775. /* List of "safe" pages that may be used to store data loaded from the suspend
  1776. * image
  1777. */
  1778. static struct linked_page *safe_pages_list;
  1779. #ifdef CONFIG_HIGHMEM
  1780. /* struct highmem_pbe is used for creating the list of highmem pages that
  1781. * should be restored atomically during the resume from disk, because the page
  1782. * frames they have occupied before the suspend are in use.
  1783. */
  1784. struct highmem_pbe {
  1785. struct page *copy_page; /* data is here now */
  1786. struct page *orig_page; /* data was here before the suspend */
  1787. struct highmem_pbe *next;
  1788. };
  1789. /* List of highmem PBEs needed for restoring the highmem pages that were
  1790. * allocated before the suspend and included in the suspend image, but have
  1791. * also been allocated by the "resume" kernel, so their contents cannot be
  1792. * written directly to their "original" page frames.
  1793. */
  1794. static struct highmem_pbe *highmem_pblist;
  1795. /**
  1796. * count_highmem_image_pages - compute the number of highmem pages in the
  1797. * suspend image. The bits in the memory bitmap @bm that correspond to the
  1798. * image pages are assumed to be set.
  1799. */
  1800. static unsigned int count_highmem_image_pages(struct memory_bitmap *bm)
  1801. {
  1802. unsigned long pfn;
  1803. unsigned int cnt = 0;
  1804. memory_bm_position_reset(bm);
  1805. pfn = memory_bm_next_pfn(bm);
  1806. while (pfn != BM_END_OF_MAP) {
  1807. if (PageHighMem(pfn_to_page(pfn)))
  1808. cnt++;
  1809. pfn = memory_bm_next_pfn(bm);
  1810. }
  1811. return cnt;
  1812. }
  1813. /**
  1814. * prepare_highmem_image - try to allocate as many highmem pages as
  1815. * there are highmem image pages (@nr_highmem_p points to the variable
  1816. * containing the number of highmem image pages). The pages that are
  1817. * "safe" (ie. will not be overwritten when the suspend image is
  1818. * restored) have the corresponding bits set in @bm (it must be
  1819. * unitialized).
  1820. *
  1821. * NOTE: This function should not be called if there are no highmem
  1822. * image pages.
  1823. */
  1824. static unsigned int safe_highmem_pages;
  1825. static struct memory_bitmap *safe_highmem_bm;
  1826. static int
  1827. prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
  1828. {
  1829. unsigned int to_alloc;
  1830. if (memory_bm_create(bm, GFP_ATOMIC, PG_SAFE))
  1831. return -ENOMEM;
  1832. if (get_highmem_buffer(PG_SAFE))
  1833. return -ENOMEM;
  1834. to_alloc = count_free_highmem_pages();
  1835. if (to_alloc > *nr_highmem_p)
  1836. to_alloc = *nr_highmem_p;
  1837. else
  1838. *nr_highmem_p = to_alloc;
  1839. safe_highmem_pages = 0;
  1840. while (to_alloc-- > 0) {
  1841. struct page *page;
  1842. page = alloc_page(__GFP_HIGHMEM);
  1843. if (!swsusp_page_is_free(page)) {
  1844. /* The page is "safe", set its bit the bitmap */
  1845. memory_bm_set_bit(bm, page_to_pfn(page));
  1846. safe_highmem_pages++;
  1847. }
  1848. /* Mark the page as allocated */
  1849. swsusp_set_page_forbidden(page);
  1850. swsusp_set_page_free(page);
  1851. }
  1852. memory_bm_position_reset(bm);
  1853. safe_highmem_bm = bm;
  1854. return 0;
  1855. }
  1856. /**
  1857. * get_highmem_page_buffer - for given highmem image page find the buffer
  1858. * that suspend_write_next() should set for its caller to write to.
  1859. *
  1860. * If the page is to be saved to its "original" page frame or a copy of
  1861. * the page is to be made in the highmem, @buffer is returned. Otherwise,
  1862. * the copy of the page is to be made in normal memory, so the address of
  1863. * the copy is returned.
  1864. *
  1865. * If @buffer is returned, the caller of suspend_write_next() will write
  1866. * the page's contents to @buffer, so they will have to be copied to the
  1867. * right location on the next call to suspend_write_next() and it is done
  1868. * with the help of copy_last_highmem_page(). For this purpose, if
  1869. * @buffer is returned, @last_highmem page is set to the page to which
  1870. * the data will have to be copied from @buffer.
  1871. */
  1872. static struct page *last_highmem_page;
  1873. static void *
  1874. get_highmem_page_buffer(struct page *page, struct chain_allocator *ca)
  1875. {
  1876. struct highmem_pbe *pbe;
  1877. void *kaddr;
  1878. if (swsusp_page_is_forbidden(page) && swsusp_page_is_free(page)) {
  1879. /* We have allocated the "original" page frame and we can
  1880. * use it directly to store the loaded page.
  1881. */
  1882. last_highmem_page = page;
  1883. return buffer;
  1884. }
  1885. /* The "original" page frame has not been allocated and we have to
  1886. * use a "safe" page frame to store the loaded page.
  1887. */
  1888. pbe = chain_alloc(ca, sizeof(struct highmem_pbe));
  1889. if (!pbe) {
  1890. swsusp_free();
  1891. return ERR_PTR(-ENOMEM);
  1892. }
  1893. pbe->orig_page = page;
  1894. if (safe_highmem_pages > 0) {
  1895. struct page *tmp;
  1896. /* Copy of the page will be stored in high memory */
  1897. kaddr = buffer;
  1898. tmp = pfn_to_page(memory_bm_next_pfn(safe_highmem_bm));
  1899. safe_highmem_pages--;
  1900. last_highmem_page = tmp;
  1901. pbe->copy_page = tmp;
  1902. } else {
  1903. /* Copy of the page will be stored in normal memory */
  1904. kaddr = safe_pages_list;
  1905. safe_pages_list = safe_pages_list->next;
  1906. pbe->copy_page = virt_to_page(kaddr);
  1907. }
  1908. pbe->next = highmem_pblist;
  1909. highmem_pblist = pbe;
  1910. return kaddr;
  1911. }
  1912. /**
  1913. * copy_last_highmem_page - copy the contents of a highmem image from
  1914. * @buffer, where the caller of snapshot_write_next() has place them,
  1915. * to the right location represented by @last_highmem_page .
  1916. */
  1917. static void copy_last_highmem_page(void)
  1918. {
  1919. if (last_highmem_page) {
  1920. void *dst;
  1921. dst = kmap_atomic(last_highmem_page);
  1922. copy_page(dst, buffer);
  1923. kunmap_atomic(dst);
  1924. last_highmem_page = NULL;
  1925. }
  1926. }
  1927. static inline int last_highmem_page_copied(void)
  1928. {
  1929. return !last_highmem_page;
  1930. }
  1931. static inline void free_highmem_data(void)
  1932. {
  1933. if (safe_highmem_bm)
  1934. memory_bm_free(safe_highmem_bm, PG_UNSAFE_CLEAR);
  1935. if (buffer)
  1936. free_image_page(buffer, PG_UNSAFE_CLEAR);
  1937. }
  1938. #else
  1939. static inline int get_safe_write_buffer(void) { return 0; }
  1940. static unsigned int
  1941. count_highmem_image_pages(struct memory_bitmap *bm) { return 0; }
  1942. static inline int
  1943. prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
  1944. {
  1945. return 0;
  1946. }
  1947. static inline void *
  1948. get_highmem_page_buffer(struct page *page, struct chain_allocator *ca)
  1949. {
  1950. return ERR_PTR(-EINVAL);
  1951. }
  1952. static inline void copy_last_highmem_page(void) {}
  1953. static inline int last_highmem_page_copied(void) { return 1; }
  1954. static inline void free_highmem_data(void) {}
  1955. #endif /* CONFIG_HIGHMEM */
  1956. /**
  1957. * prepare_image - use the memory bitmap @bm to mark the pages that will
  1958. * be overwritten in the process of restoring the system memory state
  1959. * from the suspend image ("unsafe" pages) and allocate memory for the
  1960. * image.
  1961. *
  1962. * The idea is to allocate a new memory bitmap first and then allocate
  1963. * as many pages as needed for the image data, but not to assign these
  1964. * pages to specific tasks initially. Instead, we just mark them as
  1965. * allocated and create a lists of "safe" pages that will be used
  1966. * later. On systems with high memory a list of "safe" highmem pages is
  1967. * also created.
  1968. */
  1969. #define PBES_PER_LINKED_PAGE (LINKED_PAGE_DATA_SIZE / sizeof(struct pbe))
  1970. static int
  1971. prepare_image(struct memory_bitmap *new_bm, struct memory_bitmap *bm)
  1972. {
  1973. unsigned int nr_pages, nr_highmem;
  1974. struct linked_page *sp_list, *lp;
  1975. int error;
  1976. /* If there is no highmem, the buffer will not be necessary */
  1977. free_image_page(buffer, PG_UNSAFE_CLEAR);
  1978. buffer = NULL;
  1979. nr_highmem = count_highmem_image_pages(bm);
  1980. error = mark_unsafe_pages(bm);
  1981. if (error)
  1982. goto Free;
  1983. error = memory_bm_create(new_bm, GFP_ATOMIC, PG_SAFE);
  1984. if (error)
  1985. goto Free;
  1986. duplicate_memory_bitmap(new_bm, bm);
  1987. memory_bm_free(bm, PG_UNSAFE_KEEP);
  1988. if (nr_highmem > 0) {
  1989. error = prepare_highmem_image(bm, &nr_highmem);
  1990. if (error)
  1991. goto Free;
  1992. }
  1993. /* Reserve some safe pages for potential later use.
  1994. *
  1995. * NOTE: This way we make sure there will be enough safe pages for the
  1996. * chain_alloc() in get_buffer(). It is a bit wasteful, but
  1997. * nr_copy_pages cannot be greater than 50% of the memory anyway.
  1998. */
  1999. sp_list = NULL;
  2000. /* nr_copy_pages cannot be lesser than allocated_unsafe_pages */
  2001. nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
  2002. nr_pages = DIV_ROUND_UP(nr_pages, PBES_PER_LINKED_PAGE);
  2003. while (nr_pages > 0) {
  2004. lp = get_image_page(GFP_ATOMIC, PG_SAFE);
  2005. if (!lp) {
  2006. error = -ENOMEM;
  2007. goto Free;
  2008. }
  2009. lp->next = sp_list;
  2010. sp_list = lp;
  2011. nr_pages--;
  2012. }
  2013. /* Preallocate memory for the image */
  2014. safe_pages_list = NULL;
  2015. nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
  2016. while (nr_pages > 0) {
  2017. lp = (struct linked_page *)get_zeroed_page(GFP_ATOMIC);
  2018. if (!lp) {
  2019. error = -ENOMEM;
  2020. goto Free;
  2021. }
  2022. if (!swsusp_page_is_free(virt_to_page(lp))) {
  2023. /* The page is "safe", add it to the list */
  2024. lp->next = safe_pages_list;
  2025. safe_pages_list = lp;
  2026. }
  2027. /* Mark the page as allocated */
  2028. swsusp_set_page_forbidden(virt_to_page(lp));
  2029. swsusp_set_page_free(virt_to_page(lp));
  2030. nr_pages--;
  2031. }
  2032. /* Free the reserved safe pages so that chain_alloc() can use them */
  2033. while (sp_list) {
  2034. lp = sp_list->next;
  2035. free_image_page(sp_list, PG_UNSAFE_CLEAR);
  2036. sp_list = lp;
  2037. }
  2038. return 0;
  2039. Free:
  2040. swsusp_free();
  2041. return error;
  2042. }
  2043. /**
  2044. * get_buffer - compute the address that snapshot_write_next() should
  2045. * set for its caller to write to.
  2046. */
  2047. static void *get_buffer(struct memory_bitmap *bm, struct chain_allocator *ca)
  2048. {
  2049. struct pbe *pbe;
  2050. struct page *page;
  2051. unsigned long pfn = memory_bm_next_pfn(bm);
  2052. if (pfn == BM_END_OF_MAP)
  2053. return ERR_PTR(-EFAULT);
  2054. page = pfn_to_page(pfn);
  2055. if (PageHighMem(page))
  2056. return get_highmem_page_buffer(page, ca);
  2057. if (swsusp_page_is_forbidden(page) && swsusp_page_is_free(page))
  2058. /* We have allocated the "original" page frame and we can
  2059. * use it directly to store the loaded page.
  2060. */
  2061. return page_address(page);
  2062. /* The "original" page frame has not been allocated and we have to
  2063. * use a "safe" page frame to store the loaded page.
  2064. */
  2065. pbe = chain_alloc(ca, sizeof(struct pbe));
  2066. if (!pbe) {
  2067. swsusp_free();
  2068. return ERR_PTR(-ENOMEM);
  2069. }
  2070. pbe->orig_address = page_address(page);
  2071. pbe->address = safe_pages_list;
  2072. safe_pages_list = safe_pages_list->next;
  2073. pbe->next = restore_pblist;
  2074. restore_pblist = pbe;
  2075. return pbe->address;
  2076. }
  2077. /**
  2078. * snapshot_write_next - used for writing the system memory snapshot.
  2079. *
  2080. * On the first call to it @handle should point to a zeroed
  2081. * snapshot_handle structure. The structure gets updated and a pointer
  2082. * to it should be passed to this function every next time.
  2083. *
  2084. * On success the function returns a positive number. Then, the caller
  2085. * is allowed to write up to the returned number of bytes to the memory
  2086. * location computed by the data_of() macro.
  2087. *
  2088. * The function returns 0 to indicate the "end of file" condition,
  2089. * and a negative number is returned on error. In such cases the
  2090. * structure pointed to by @handle is not updated and should not be used
  2091. * any more.
  2092. */
  2093. int snapshot_write_next(struct snapshot_handle *handle)
  2094. {
  2095. static struct chain_allocator ca;
  2096. int error = 0;
  2097. /* Check if we have already loaded the entire image */
  2098. if (handle->cur > 1 && handle->cur > nr_meta_pages + nr_copy_pages)
  2099. return 0;
  2100. handle->sync_read = 1;
  2101. if (!handle->cur) {
  2102. if (!buffer)
  2103. /* This makes the buffer be freed by swsusp_free() */
  2104. buffer = get_image_page(GFP_ATOMIC, PG_ANY);
  2105. if (!buffer)
  2106. return -ENOMEM;
  2107. handle->buffer = buffer;
  2108. } else if (handle->cur == 1) {
  2109. error = load_header(buffer);
  2110. if (error)
  2111. return error;
  2112. error = memory_bm_create(&copy_bm, GFP_ATOMIC, PG_ANY);
  2113. if (error)
  2114. return error;
  2115. /* Allocate buffer for page keys. */
  2116. error = page_key_alloc(nr_copy_pages);
  2117. if (error)
  2118. return error;
  2119. } else if (handle->cur <= nr_meta_pages + 1) {
  2120. error = unpack_orig_pfns(buffer, &copy_bm);
  2121. if (error)
  2122. return error;
  2123. if (handle->cur == nr_meta_pages + 1) {
  2124. error = prepare_image(&orig_bm, &copy_bm);
  2125. if (error)
  2126. return error;
  2127. chain_init(&ca, GFP_ATOMIC, PG_SAFE);
  2128. memory_bm_position_reset(&orig_bm);
  2129. restore_pblist = NULL;
  2130. handle->buffer = get_buffer(&orig_bm, &ca);
  2131. handle->sync_read = 0;
  2132. if (IS_ERR(handle->buffer))
  2133. return PTR_ERR(handle->buffer);
  2134. }
  2135. } else {
  2136. copy_last_highmem_page();
  2137. /* Restore page key for data page (s390 only). */
  2138. page_key_write(handle->buffer);
  2139. handle->buffer = get_buffer(&orig_bm, &ca);
  2140. if (IS_ERR(handle->buffer))
  2141. return PTR_ERR(handle->buffer);
  2142. if (handle->buffer != buffer)
  2143. handle->sync_read = 0;
  2144. }
  2145. handle->cur++;
  2146. return PAGE_SIZE;
  2147. }
  2148. /**
  2149. * snapshot_write_finalize - must be called after the last call to
  2150. * snapshot_write_next() in case the last page in the image happens
  2151. * to be a highmem page and its contents should be stored in the
  2152. * highmem. Additionally, it releases the memory that will not be
  2153. * used any more.
  2154. */
  2155. void snapshot_write_finalize(struct snapshot_handle *handle)
  2156. {
  2157. copy_last_highmem_page();
  2158. /* Restore page key for data page (s390 only). */
  2159. page_key_write(handle->buffer);
  2160. page_key_free();
  2161. /* Free only if we have loaded the image entirely */
  2162. if (handle->cur > 1 && handle->cur > nr_meta_pages + nr_copy_pages) {
  2163. memory_bm_free(&orig_bm, PG_UNSAFE_CLEAR);
  2164. free_highmem_data();
  2165. }
  2166. }
  2167. int snapshot_image_loaded(struct snapshot_handle *handle)
  2168. {
  2169. return !(!nr_copy_pages || !last_highmem_page_copied() ||
  2170. handle->cur <= nr_meta_pages + nr_copy_pages);
  2171. }
  2172. #ifdef CONFIG_HIGHMEM
  2173. /* Assumes that @buf is ready and points to a "safe" page */
  2174. static inline void
  2175. swap_two_pages_data(struct page *p1, struct page *p2, void *buf)
  2176. {
  2177. void *kaddr1, *kaddr2;
  2178. kaddr1 = kmap_atomic(p1);
  2179. kaddr2 = kmap_atomic(p2);
  2180. copy_page(buf, kaddr1);
  2181. copy_page(kaddr1, kaddr2);
  2182. copy_page(kaddr2, buf);
  2183. kunmap_atomic(kaddr2);
  2184. kunmap_atomic(kaddr1);
  2185. }
  2186. /**
  2187. * restore_highmem - for each highmem page that was allocated before
  2188. * the suspend and included in the suspend image, and also has been
  2189. * allocated by the "resume" kernel swap its current (ie. "before
  2190. * resume") contents with the previous (ie. "before suspend") one.
  2191. *
  2192. * If the resume eventually fails, we can call this function once
  2193. * again and restore the "before resume" highmem state.
  2194. */
  2195. int restore_highmem(void)
  2196. {
  2197. struct highmem_pbe *pbe = highmem_pblist;
  2198. void *buf;
  2199. if (!pbe)
  2200. return 0;
  2201. buf = get_image_page(GFP_ATOMIC, PG_SAFE);
  2202. if (!buf)
  2203. return -ENOMEM;
  2204. while (pbe) {
  2205. swap_two_pages_data(pbe->copy_page, pbe->orig_page, buf);
  2206. pbe = pbe->next;
  2207. }
  2208. free_image_page(buf, PG_UNSAFE_CLEAR);
  2209. return 0;
  2210. }
  2211. #endif /* CONFIG_HIGHMEM */