snapshot.c 68 KB

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