snapshot.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607
  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. if (!list_is_last(&bm->cur.node->list, &bm->cur.zone->leaves)) {
  644. bm->cur.node = list_entry(bm->cur.node->list.next,
  645. struct rtree_node, list);
  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. if (!list_is_last(&bm->cur.zone->list, &bm->zones)) {
  653. bm->cur.zone = list_entry(bm->cur.zone->list.next,
  654. struct mem_zone_bm_rtree, list);
  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. /**
  806. * create_basic_memory_bitmaps - create bitmaps needed for marking page
  807. * frames that should not be saved and free page frames. The pointers
  808. * forbidden_pages_map and free_pages_map are only modified if everything
  809. * goes well, because we don't want the bits to be used before both bitmaps
  810. * are set up.
  811. */
  812. int create_basic_memory_bitmaps(void)
  813. {
  814. struct memory_bitmap *bm1, *bm2;
  815. int error = 0;
  816. if (forbidden_pages_map && free_pages_map)
  817. return 0;
  818. else
  819. BUG_ON(forbidden_pages_map || free_pages_map);
  820. bm1 = kzalloc(sizeof(struct memory_bitmap), GFP_KERNEL);
  821. if (!bm1)
  822. return -ENOMEM;
  823. error = memory_bm_create(bm1, GFP_KERNEL, PG_ANY);
  824. if (error)
  825. goto Free_first_object;
  826. bm2 = kzalloc(sizeof(struct memory_bitmap), GFP_KERNEL);
  827. if (!bm2)
  828. goto Free_first_bitmap;
  829. error = memory_bm_create(bm2, GFP_KERNEL, PG_ANY);
  830. if (error)
  831. goto Free_second_object;
  832. forbidden_pages_map = bm1;
  833. free_pages_map = bm2;
  834. mark_nosave_pages(forbidden_pages_map);
  835. pr_debug("PM: Basic memory bitmaps created\n");
  836. return 0;
  837. Free_second_object:
  838. kfree(bm2);
  839. Free_first_bitmap:
  840. memory_bm_free(bm1, PG_UNSAFE_CLEAR);
  841. Free_first_object:
  842. kfree(bm1);
  843. return -ENOMEM;
  844. }
  845. /**
  846. * free_basic_memory_bitmaps - free memory bitmaps allocated by
  847. * create_basic_memory_bitmaps(). The auxiliary pointers are necessary
  848. * so that the bitmaps themselves are not referred to while they are being
  849. * freed.
  850. */
  851. void free_basic_memory_bitmaps(void)
  852. {
  853. struct memory_bitmap *bm1, *bm2;
  854. if (WARN_ON(!(forbidden_pages_map && free_pages_map)))
  855. return;
  856. bm1 = forbidden_pages_map;
  857. bm2 = free_pages_map;
  858. forbidden_pages_map = NULL;
  859. free_pages_map = NULL;
  860. memory_bm_free(bm1, PG_UNSAFE_CLEAR);
  861. kfree(bm1);
  862. memory_bm_free(bm2, PG_UNSAFE_CLEAR);
  863. kfree(bm2);
  864. pr_debug("PM: Basic memory bitmaps freed\n");
  865. }
  866. /**
  867. * snapshot_additional_pages - estimate the number of additional pages
  868. * be needed for setting up the suspend image data structures for given
  869. * zone (usually the returned value is greater than the exact number)
  870. */
  871. unsigned int snapshot_additional_pages(struct zone *zone)
  872. {
  873. unsigned int rtree, nodes;
  874. rtree = nodes = DIV_ROUND_UP(zone->spanned_pages, BM_BITS_PER_BLOCK);
  875. rtree += DIV_ROUND_UP(rtree * sizeof(struct rtree_node),
  876. LINKED_PAGE_DATA_SIZE);
  877. while (nodes > 1) {
  878. nodes = DIV_ROUND_UP(nodes, BM_ENTRIES_PER_LEVEL);
  879. rtree += nodes;
  880. }
  881. return 2 * rtree;
  882. }
  883. #ifdef CONFIG_HIGHMEM
  884. /**
  885. * count_free_highmem_pages - compute the total number of free highmem
  886. * pages, system-wide.
  887. */
  888. static unsigned int count_free_highmem_pages(void)
  889. {
  890. struct zone *zone;
  891. unsigned int cnt = 0;
  892. for_each_populated_zone(zone)
  893. if (is_highmem(zone))
  894. cnt += zone_page_state(zone, NR_FREE_PAGES);
  895. return cnt;
  896. }
  897. /**
  898. * saveable_highmem_page - Determine whether a highmem page should be
  899. * included in the suspend image.
  900. *
  901. * We should save the page if it isn't Nosave or NosaveFree, or Reserved,
  902. * and it isn't a part of a free chunk of pages.
  903. */
  904. static struct page *saveable_highmem_page(struct zone *zone, unsigned long pfn)
  905. {
  906. struct page *page;
  907. if (!pfn_valid(pfn))
  908. return NULL;
  909. page = pfn_to_page(pfn);
  910. if (page_zone(page) != zone)
  911. return NULL;
  912. BUG_ON(!PageHighMem(page));
  913. if (swsusp_page_is_forbidden(page) || swsusp_page_is_free(page) ||
  914. PageReserved(page))
  915. return NULL;
  916. if (page_is_guard(page))
  917. return NULL;
  918. return page;
  919. }
  920. /**
  921. * count_highmem_pages - compute the total number of saveable highmem
  922. * pages.
  923. */
  924. static unsigned int count_highmem_pages(void)
  925. {
  926. struct zone *zone;
  927. unsigned int n = 0;
  928. for_each_populated_zone(zone) {
  929. unsigned long pfn, max_zone_pfn;
  930. if (!is_highmem(zone))
  931. continue;
  932. mark_free_pages(zone);
  933. max_zone_pfn = zone_end_pfn(zone);
  934. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
  935. if (saveable_highmem_page(zone, pfn))
  936. n++;
  937. }
  938. return n;
  939. }
  940. #else
  941. static inline void *saveable_highmem_page(struct zone *z, unsigned long p)
  942. {
  943. return NULL;
  944. }
  945. #endif /* CONFIG_HIGHMEM */
  946. /**
  947. * saveable_page - Determine whether a non-highmem page should be included
  948. * in the suspend image.
  949. *
  950. * We should save the page if it isn't Nosave, and is not in the range
  951. * of pages statically defined as 'unsaveable', and it isn't a part of
  952. * a free chunk of pages.
  953. */
  954. static struct page *saveable_page(struct zone *zone, unsigned long pfn)
  955. {
  956. struct page *page;
  957. if (!pfn_valid(pfn))
  958. return NULL;
  959. page = pfn_to_page(pfn);
  960. if (page_zone(page) != zone)
  961. return NULL;
  962. BUG_ON(PageHighMem(page));
  963. if (swsusp_page_is_forbidden(page) || swsusp_page_is_free(page))
  964. return NULL;
  965. if (PageReserved(page)
  966. && (!kernel_page_present(page) || pfn_is_nosave(pfn)))
  967. return NULL;
  968. if (page_is_guard(page))
  969. return NULL;
  970. return page;
  971. }
  972. /**
  973. * count_data_pages - compute the total number of saveable non-highmem
  974. * pages.
  975. */
  976. static unsigned int count_data_pages(void)
  977. {
  978. struct zone *zone;
  979. unsigned long pfn, max_zone_pfn;
  980. unsigned int n = 0;
  981. for_each_populated_zone(zone) {
  982. if (is_highmem(zone))
  983. continue;
  984. mark_free_pages(zone);
  985. max_zone_pfn = zone_end_pfn(zone);
  986. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
  987. if (saveable_page(zone, pfn))
  988. n++;
  989. }
  990. return n;
  991. }
  992. /* This is needed, because copy_page and memcpy are not usable for copying
  993. * task structs.
  994. */
  995. static inline void do_copy_page(long *dst, long *src)
  996. {
  997. int n;
  998. for (n = PAGE_SIZE / sizeof(long); n; n--)
  999. *dst++ = *src++;
  1000. }
  1001. /**
  1002. * safe_copy_page - check if the page we are going to copy is marked as
  1003. * present in the kernel page tables (this always is the case if
  1004. * CONFIG_DEBUG_PAGEALLOC is not set and in that case
  1005. * kernel_page_present() always returns 'true').
  1006. */
  1007. static void safe_copy_page(void *dst, struct page *s_page)
  1008. {
  1009. if (kernel_page_present(s_page)) {
  1010. do_copy_page(dst, page_address(s_page));
  1011. } else {
  1012. kernel_map_pages(s_page, 1, 1);
  1013. do_copy_page(dst, page_address(s_page));
  1014. kernel_map_pages(s_page, 1, 0);
  1015. }
  1016. }
  1017. #ifdef CONFIG_HIGHMEM
  1018. static inline struct page *
  1019. page_is_saveable(struct zone *zone, unsigned long pfn)
  1020. {
  1021. return is_highmem(zone) ?
  1022. saveable_highmem_page(zone, pfn) : saveable_page(zone, pfn);
  1023. }
  1024. static void copy_data_page(unsigned long dst_pfn, unsigned long src_pfn)
  1025. {
  1026. struct page *s_page, *d_page;
  1027. void *src, *dst;
  1028. s_page = pfn_to_page(src_pfn);
  1029. d_page = pfn_to_page(dst_pfn);
  1030. if (PageHighMem(s_page)) {
  1031. src = kmap_atomic(s_page);
  1032. dst = kmap_atomic(d_page);
  1033. do_copy_page(dst, src);
  1034. kunmap_atomic(dst);
  1035. kunmap_atomic(src);
  1036. } else {
  1037. if (PageHighMem(d_page)) {
  1038. /* Page pointed to by src may contain some kernel
  1039. * data modified by kmap_atomic()
  1040. */
  1041. safe_copy_page(buffer, s_page);
  1042. dst = kmap_atomic(d_page);
  1043. copy_page(dst, buffer);
  1044. kunmap_atomic(dst);
  1045. } else {
  1046. safe_copy_page(page_address(d_page), s_page);
  1047. }
  1048. }
  1049. }
  1050. #else
  1051. #define page_is_saveable(zone, pfn) saveable_page(zone, pfn)
  1052. static inline void copy_data_page(unsigned long dst_pfn, unsigned long src_pfn)
  1053. {
  1054. safe_copy_page(page_address(pfn_to_page(dst_pfn)),
  1055. pfn_to_page(src_pfn));
  1056. }
  1057. #endif /* CONFIG_HIGHMEM */
  1058. static void
  1059. copy_data_pages(struct memory_bitmap *copy_bm, struct memory_bitmap *orig_bm)
  1060. {
  1061. struct zone *zone;
  1062. unsigned long pfn;
  1063. for_each_populated_zone(zone) {
  1064. unsigned long max_zone_pfn;
  1065. mark_free_pages(zone);
  1066. max_zone_pfn = zone_end_pfn(zone);
  1067. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
  1068. if (page_is_saveable(zone, pfn))
  1069. memory_bm_set_bit(orig_bm, pfn);
  1070. }
  1071. memory_bm_position_reset(orig_bm);
  1072. memory_bm_position_reset(copy_bm);
  1073. for(;;) {
  1074. pfn = memory_bm_next_pfn(orig_bm);
  1075. if (unlikely(pfn == BM_END_OF_MAP))
  1076. break;
  1077. copy_data_page(memory_bm_next_pfn(copy_bm), pfn);
  1078. }
  1079. }
  1080. /* Total number of image pages */
  1081. static unsigned int nr_copy_pages;
  1082. /* Number of pages needed for saving the original pfns of the image pages */
  1083. static unsigned int nr_meta_pages;
  1084. /*
  1085. * Numbers of normal and highmem page frames allocated for hibernation image
  1086. * before suspending devices.
  1087. */
  1088. unsigned int alloc_normal, alloc_highmem;
  1089. /*
  1090. * Memory bitmap used for marking saveable pages (during hibernation) or
  1091. * hibernation image pages (during restore)
  1092. */
  1093. static struct memory_bitmap orig_bm;
  1094. /*
  1095. * Memory bitmap used during hibernation for marking allocated page frames that
  1096. * will contain copies of saveable pages. During restore it is initially used
  1097. * for marking hibernation image pages, but then the set bits from it are
  1098. * duplicated in @orig_bm and it is released. On highmem systems it is next
  1099. * used for marking "safe" highmem pages, but it has to be reinitialized for
  1100. * this purpose.
  1101. */
  1102. static struct memory_bitmap copy_bm;
  1103. /**
  1104. * swsusp_free - free pages allocated for the suspend.
  1105. *
  1106. * Suspend pages are alocated before the atomic copy is made, so we
  1107. * need to release them after the resume.
  1108. */
  1109. void swsusp_free(void)
  1110. {
  1111. unsigned long fb_pfn, fr_pfn;
  1112. if (!forbidden_pages_map || !free_pages_map)
  1113. goto out;
  1114. memory_bm_position_reset(forbidden_pages_map);
  1115. memory_bm_position_reset(free_pages_map);
  1116. loop:
  1117. fr_pfn = memory_bm_next_pfn(free_pages_map);
  1118. fb_pfn = memory_bm_next_pfn(forbidden_pages_map);
  1119. /*
  1120. * Find the next bit set in both bitmaps. This is guaranteed to
  1121. * terminate when fb_pfn == fr_pfn == BM_END_OF_MAP.
  1122. */
  1123. do {
  1124. if (fb_pfn < fr_pfn)
  1125. fb_pfn = memory_bm_next_pfn(forbidden_pages_map);
  1126. if (fr_pfn < fb_pfn)
  1127. fr_pfn = memory_bm_next_pfn(free_pages_map);
  1128. } while (fb_pfn != fr_pfn);
  1129. if (fr_pfn != BM_END_OF_MAP && pfn_valid(fr_pfn)) {
  1130. struct page *page = pfn_to_page(fr_pfn);
  1131. memory_bm_clear_current(forbidden_pages_map);
  1132. memory_bm_clear_current(free_pages_map);
  1133. __free_page(page);
  1134. goto loop;
  1135. }
  1136. out:
  1137. nr_copy_pages = 0;
  1138. nr_meta_pages = 0;
  1139. restore_pblist = NULL;
  1140. buffer = NULL;
  1141. alloc_normal = 0;
  1142. alloc_highmem = 0;
  1143. }
  1144. /* Helper functions used for the shrinking of memory. */
  1145. #define GFP_IMAGE (GFP_KERNEL | __GFP_NOWARN)
  1146. /**
  1147. * preallocate_image_pages - Allocate a number of pages for hibernation image
  1148. * @nr_pages: Number of page frames to allocate.
  1149. * @mask: GFP flags to use for the allocation.
  1150. *
  1151. * Return value: Number of page frames actually allocated
  1152. */
  1153. static unsigned long preallocate_image_pages(unsigned long nr_pages, gfp_t mask)
  1154. {
  1155. unsigned long nr_alloc = 0;
  1156. while (nr_pages > 0) {
  1157. struct page *page;
  1158. page = alloc_image_page(mask);
  1159. if (!page)
  1160. break;
  1161. memory_bm_set_bit(&copy_bm, page_to_pfn(page));
  1162. if (PageHighMem(page))
  1163. alloc_highmem++;
  1164. else
  1165. alloc_normal++;
  1166. nr_pages--;
  1167. nr_alloc++;
  1168. }
  1169. return nr_alloc;
  1170. }
  1171. static unsigned long preallocate_image_memory(unsigned long nr_pages,
  1172. unsigned long avail_normal)
  1173. {
  1174. unsigned long alloc;
  1175. if (avail_normal <= alloc_normal)
  1176. return 0;
  1177. alloc = avail_normal - alloc_normal;
  1178. if (nr_pages < alloc)
  1179. alloc = nr_pages;
  1180. return preallocate_image_pages(alloc, GFP_IMAGE);
  1181. }
  1182. #ifdef CONFIG_HIGHMEM
  1183. static unsigned long preallocate_image_highmem(unsigned long nr_pages)
  1184. {
  1185. return preallocate_image_pages(nr_pages, GFP_IMAGE | __GFP_HIGHMEM);
  1186. }
  1187. /**
  1188. * __fraction - Compute (an approximation of) x * (multiplier / base)
  1189. */
  1190. static unsigned long __fraction(u64 x, u64 multiplier, u64 base)
  1191. {
  1192. x *= multiplier;
  1193. do_div(x, base);
  1194. return (unsigned long)x;
  1195. }
  1196. static unsigned long preallocate_highmem_fraction(unsigned long nr_pages,
  1197. unsigned long highmem,
  1198. unsigned long total)
  1199. {
  1200. unsigned long alloc = __fraction(nr_pages, highmem, total);
  1201. return preallocate_image_pages(alloc, GFP_IMAGE | __GFP_HIGHMEM);
  1202. }
  1203. #else /* CONFIG_HIGHMEM */
  1204. static inline unsigned long preallocate_image_highmem(unsigned long nr_pages)
  1205. {
  1206. return 0;
  1207. }
  1208. static inline unsigned long preallocate_highmem_fraction(unsigned long nr_pages,
  1209. unsigned long highmem,
  1210. unsigned long total)
  1211. {
  1212. return 0;
  1213. }
  1214. #endif /* CONFIG_HIGHMEM */
  1215. /**
  1216. * free_unnecessary_pages - Release preallocated pages not needed for the image
  1217. */
  1218. static unsigned long free_unnecessary_pages(void)
  1219. {
  1220. unsigned long save, to_free_normal, to_free_highmem, free;
  1221. save = count_data_pages();
  1222. if (alloc_normal >= save) {
  1223. to_free_normal = alloc_normal - save;
  1224. save = 0;
  1225. } else {
  1226. to_free_normal = 0;
  1227. save -= alloc_normal;
  1228. }
  1229. save += count_highmem_pages();
  1230. if (alloc_highmem >= save) {
  1231. to_free_highmem = alloc_highmem - save;
  1232. } else {
  1233. to_free_highmem = 0;
  1234. save -= alloc_highmem;
  1235. if (to_free_normal > save)
  1236. to_free_normal -= save;
  1237. else
  1238. to_free_normal = 0;
  1239. }
  1240. free = to_free_normal + to_free_highmem;
  1241. memory_bm_position_reset(&copy_bm);
  1242. while (to_free_normal > 0 || to_free_highmem > 0) {
  1243. unsigned long pfn = memory_bm_next_pfn(&copy_bm);
  1244. struct page *page = pfn_to_page(pfn);
  1245. if (PageHighMem(page)) {
  1246. if (!to_free_highmem)
  1247. continue;
  1248. to_free_highmem--;
  1249. alloc_highmem--;
  1250. } else {
  1251. if (!to_free_normal)
  1252. continue;
  1253. to_free_normal--;
  1254. alloc_normal--;
  1255. }
  1256. memory_bm_clear_bit(&copy_bm, pfn);
  1257. swsusp_unset_page_forbidden(page);
  1258. swsusp_unset_page_free(page);
  1259. __free_page(page);
  1260. }
  1261. return free;
  1262. }
  1263. /**
  1264. * minimum_image_size - Estimate the minimum acceptable size of an image
  1265. * @saveable: Number of saveable pages in the system.
  1266. *
  1267. * We want to avoid attempting to free too much memory too hard, so estimate the
  1268. * minimum acceptable size of a hibernation image to use as the lower limit for
  1269. * preallocating memory.
  1270. *
  1271. * We assume that the minimum image size should be proportional to
  1272. *
  1273. * [number of saveable pages] - [number of pages that can be freed in theory]
  1274. *
  1275. * where the second term is the sum of (1) reclaimable slab pages, (2) active
  1276. * and (3) inactive anonymous pages, (4) active and (5) inactive file pages,
  1277. * minus mapped file pages.
  1278. */
  1279. static unsigned long minimum_image_size(unsigned long saveable)
  1280. {
  1281. unsigned long size;
  1282. size = global_page_state(NR_SLAB_RECLAIMABLE)
  1283. + global_page_state(NR_ACTIVE_ANON)
  1284. + global_page_state(NR_INACTIVE_ANON)
  1285. + global_page_state(NR_ACTIVE_FILE)
  1286. + global_page_state(NR_INACTIVE_FILE)
  1287. - global_page_state(NR_FILE_MAPPED);
  1288. return saveable <= size ? 0 : saveable - size;
  1289. }
  1290. /**
  1291. * hibernate_preallocate_memory - Preallocate memory for hibernation image
  1292. *
  1293. * To create a hibernation image it is necessary to make a copy of every page
  1294. * frame in use. We also need a number of page frames to be free during
  1295. * hibernation for allocations made while saving the image and for device
  1296. * drivers, in case they need to allocate memory from their hibernation
  1297. * callbacks (these two numbers are given by PAGES_FOR_IO (which is a rough
  1298. * estimate) and reserverd_size divided by PAGE_SIZE (which is tunable through
  1299. * /sys/power/reserved_size, respectively). To make this happen, we compute the
  1300. * total number of available page frames and allocate at least
  1301. *
  1302. * ([page frames total] + PAGES_FOR_IO + [metadata pages]) / 2
  1303. * + 2 * DIV_ROUND_UP(reserved_size, PAGE_SIZE)
  1304. *
  1305. * of them, which corresponds to the maximum size of a hibernation image.
  1306. *
  1307. * If image_size is set below the number following from the above formula,
  1308. * the preallocation of memory is continued until the total number of saveable
  1309. * pages in the system is below the requested image size or the minimum
  1310. * acceptable image size returned by minimum_image_size(), whichever is greater.
  1311. */
  1312. int hibernate_preallocate_memory(void)
  1313. {
  1314. struct zone *zone;
  1315. unsigned long saveable, size, max_size, count, highmem, pages = 0;
  1316. unsigned long alloc, save_highmem, pages_highmem, avail_normal;
  1317. ktime_t start, stop;
  1318. int error;
  1319. printk(KERN_INFO "PM: Preallocating image memory... ");
  1320. start = ktime_get();
  1321. error = memory_bm_create(&orig_bm, GFP_IMAGE, PG_ANY);
  1322. if (error)
  1323. goto err_out;
  1324. error = memory_bm_create(&copy_bm, GFP_IMAGE, PG_ANY);
  1325. if (error)
  1326. goto err_out;
  1327. alloc_normal = 0;
  1328. alloc_highmem = 0;
  1329. /* Count the number of saveable data pages. */
  1330. save_highmem = count_highmem_pages();
  1331. saveable = count_data_pages();
  1332. /*
  1333. * Compute the total number of page frames we can use (count) and the
  1334. * number of pages needed for image metadata (size).
  1335. */
  1336. count = saveable;
  1337. saveable += save_highmem;
  1338. highmem = save_highmem;
  1339. size = 0;
  1340. for_each_populated_zone(zone) {
  1341. size += snapshot_additional_pages(zone);
  1342. if (is_highmem(zone))
  1343. highmem += zone_page_state(zone, NR_FREE_PAGES);
  1344. else
  1345. count += zone_page_state(zone, NR_FREE_PAGES);
  1346. }
  1347. avail_normal = count;
  1348. count += highmem;
  1349. count -= totalreserve_pages;
  1350. /* Add number of pages required for page keys (s390 only). */
  1351. size += page_key_additional_pages(saveable);
  1352. /* Compute the maximum number of saveable pages to leave in memory. */
  1353. max_size = (count - (size + PAGES_FOR_IO)) / 2
  1354. - 2 * DIV_ROUND_UP(reserved_size, PAGE_SIZE);
  1355. /* Compute the desired number of image pages specified by image_size. */
  1356. size = DIV_ROUND_UP(image_size, PAGE_SIZE);
  1357. if (size > max_size)
  1358. size = max_size;
  1359. /*
  1360. * If the desired number of image pages is at least as large as the
  1361. * current number of saveable pages in memory, allocate page frames for
  1362. * the image and we're done.
  1363. */
  1364. if (size >= saveable) {
  1365. pages = preallocate_image_highmem(save_highmem);
  1366. pages += preallocate_image_memory(saveable - pages, avail_normal);
  1367. goto out;
  1368. }
  1369. /* Estimate the minimum size of the image. */
  1370. pages = minimum_image_size(saveable);
  1371. /*
  1372. * To avoid excessive pressure on the normal zone, leave room in it to
  1373. * accommodate an image of the minimum size (unless it's already too
  1374. * small, in which case don't preallocate pages from it at all).
  1375. */
  1376. if (avail_normal > pages)
  1377. avail_normal -= pages;
  1378. else
  1379. avail_normal = 0;
  1380. if (size < pages)
  1381. size = min_t(unsigned long, pages, max_size);
  1382. /*
  1383. * Let the memory management subsystem know that we're going to need a
  1384. * large number of page frames to allocate and make it free some memory.
  1385. * NOTE: If this is not done, performance will be hurt badly in some
  1386. * test cases.
  1387. */
  1388. shrink_all_memory(saveable - size);
  1389. /*
  1390. * The number of saveable pages in memory was too high, so apply some
  1391. * pressure to decrease it. First, make room for the largest possible
  1392. * image and fail if that doesn't work. Next, try to decrease the size
  1393. * of the image as much as indicated by 'size' using allocations from
  1394. * highmem and non-highmem zones separately.
  1395. */
  1396. pages_highmem = preallocate_image_highmem(highmem / 2);
  1397. alloc = count - max_size;
  1398. if (alloc > pages_highmem)
  1399. alloc -= pages_highmem;
  1400. else
  1401. alloc = 0;
  1402. pages = preallocate_image_memory(alloc, avail_normal);
  1403. if (pages < alloc) {
  1404. /* We have exhausted non-highmem pages, try highmem. */
  1405. alloc -= pages;
  1406. pages += pages_highmem;
  1407. pages_highmem = preallocate_image_highmem(alloc);
  1408. if (pages_highmem < alloc)
  1409. goto err_out;
  1410. pages += pages_highmem;
  1411. /*
  1412. * size is the desired number of saveable pages to leave in
  1413. * memory, so try to preallocate (all memory - size) pages.
  1414. */
  1415. alloc = (count - pages) - size;
  1416. pages += preallocate_image_highmem(alloc);
  1417. } else {
  1418. /*
  1419. * There are approximately max_size saveable pages at this point
  1420. * and we want to reduce this number down to size.
  1421. */
  1422. alloc = max_size - size;
  1423. size = preallocate_highmem_fraction(alloc, highmem, count);
  1424. pages_highmem += size;
  1425. alloc -= size;
  1426. size = preallocate_image_memory(alloc, avail_normal);
  1427. pages_highmem += preallocate_image_highmem(alloc - size);
  1428. pages += pages_highmem + size;
  1429. }
  1430. /*
  1431. * We only need as many page frames for the image as there are saveable
  1432. * pages in memory, but we have allocated more. Release the excessive
  1433. * ones now.
  1434. */
  1435. pages -= free_unnecessary_pages();
  1436. out:
  1437. stop = ktime_get();
  1438. printk(KERN_CONT "done (allocated %lu pages)\n", pages);
  1439. swsusp_show_speed(start, stop, pages, "Allocated");
  1440. return 0;
  1441. err_out:
  1442. printk(KERN_CONT "\n");
  1443. swsusp_free();
  1444. return -ENOMEM;
  1445. }
  1446. #ifdef CONFIG_HIGHMEM
  1447. /**
  1448. * count_pages_for_highmem - compute the number of non-highmem pages
  1449. * that will be necessary for creating copies of highmem pages.
  1450. */
  1451. static unsigned int count_pages_for_highmem(unsigned int nr_highmem)
  1452. {
  1453. unsigned int free_highmem = count_free_highmem_pages() + alloc_highmem;
  1454. if (free_highmem >= nr_highmem)
  1455. nr_highmem = 0;
  1456. else
  1457. nr_highmem -= free_highmem;
  1458. return nr_highmem;
  1459. }
  1460. #else
  1461. static unsigned int
  1462. count_pages_for_highmem(unsigned int nr_highmem) { return 0; }
  1463. #endif /* CONFIG_HIGHMEM */
  1464. /**
  1465. * enough_free_mem - Make sure we have enough free memory for the
  1466. * snapshot image.
  1467. */
  1468. static int enough_free_mem(unsigned int nr_pages, unsigned int nr_highmem)
  1469. {
  1470. struct zone *zone;
  1471. unsigned int free = alloc_normal;
  1472. for_each_populated_zone(zone)
  1473. if (!is_highmem(zone))
  1474. free += zone_page_state(zone, NR_FREE_PAGES);
  1475. nr_pages += count_pages_for_highmem(nr_highmem);
  1476. pr_debug("PM: Normal pages needed: %u + %u, available pages: %u\n",
  1477. nr_pages, PAGES_FOR_IO, free);
  1478. return free > nr_pages + PAGES_FOR_IO;
  1479. }
  1480. #ifdef CONFIG_HIGHMEM
  1481. /**
  1482. * get_highmem_buffer - if there are some highmem pages in the suspend
  1483. * image, we may need the buffer to copy them and/or load their data.
  1484. */
  1485. static inline int get_highmem_buffer(int safe_needed)
  1486. {
  1487. buffer = get_image_page(GFP_ATOMIC | __GFP_COLD, safe_needed);
  1488. return buffer ? 0 : -ENOMEM;
  1489. }
  1490. /**
  1491. * alloc_highmem_image_pages - allocate some highmem pages for the image.
  1492. * Try to allocate as many pages as needed, but if the number of free
  1493. * highmem pages is lesser than that, allocate them all.
  1494. */
  1495. static inline unsigned int
  1496. alloc_highmem_pages(struct memory_bitmap *bm, unsigned int nr_highmem)
  1497. {
  1498. unsigned int to_alloc = count_free_highmem_pages();
  1499. if (to_alloc > nr_highmem)
  1500. to_alloc = nr_highmem;
  1501. nr_highmem -= to_alloc;
  1502. while (to_alloc-- > 0) {
  1503. struct page *page;
  1504. page = alloc_image_page(__GFP_HIGHMEM|__GFP_KSWAPD_RECLAIM);
  1505. memory_bm_set_bit(bm, page_to_pfn(page));
  1506. }
  1507. return nr_highmem;
  1508. }
  1509. #else
  1510. static inline int get_highmem_buffer(int safe_needed) { return 0; }
  1511. static inline unsigned int
  1512. alloc_highmem_pages(struct memory_bitmap *bm, unsigned int n) { return 0; }
  1513. #endif /* CONFIG_HIGHMEM */
  1514. /**
  1515. * swsusp_alloc - allocate memory for the suspend image
  1516. *
  1517. * We first try to allocate as many highmem pages as there are
  1518. * saveable highmem pages in the system. If that fails, we allocate
  1519. * non-highmem pages for the copies of the remaining highmem ones.
  1520. *
  1521. * In this approach it is likely that the copies of highmem pages will
  1522. * also be located in the high memory, because of the way in which
  1523. * copy_data_pages() works.
  1524. */
  1525. static int
  1526. swsusp_alloc(struct memory_bitmap *orig_bm, struct memory_bitmap *copy_bm,
  1527. unsigned int nr_pages, unsigned int nr_highmem)
  1528. {
  1529. if (nr_highmem > 0) {
  1530. if (get_highmem_buffer(PG_ANY))
  1531. goto err_out;
  1532. if (nr_highmem > alloc_highmem) {
  1533. nr_highmem -= alloc_highmem;
  1534. nr_pages += alloc_highmem_pages(copy_bm, nr_highmem);
  1535. }
  1536. }
  1537. if (nr_pages > alloc_normal) {
  1538. nr_pages -= alloc_normal;
  1539. while (nr_pages-- > 0) {
  1540. struct page *page;
  1541. page = alloc_image_page(GFP_ATOMIC | __GFP_COLD);
  1542. if (!page)
  1543. goto err_out;
  1544. memory_bm_set_bit(copy_bm, page_to_pfn(page));
  1545. }
  1546. }
  1547. return 0;
  1548. err_out:
  1549. swsusp_free();
  1550. return -ENOMEM;
  1551. }
  1552. asmlinkage __visible int swsusp_save(void)
  1553. {
  1554. unsigned int nr_pages, nr_highmem;
  1555. printk(KERN_INFO "PM: Creating hibernation image:\n");
  1556. drain_local_pages(NULL);
  1557. nr_pages = count_data_pages();
  1558. nr_highmem = count_highmem_pages();
  1559. printk(KERN_INFO "PM: Need to copy %u pages\n", nr_pages + nr_highmem);
  1560. if (!enough_free_mem(nr_pages, nr_highmem)) {
  1561. printk(KERN_ERR "PM: Not enough free memory\n");
  1562. return -ENOMEM;
  1563. }
  1564. if (swsusp_alloc(&orig_bm, &copy_bm, nr_pages, nr_highmem)) {
  1565. printk(KERN_ERR "PM: Memory allocation failed\n");
  1566. return -ENOMEM;
  1567. }
  1568. /* During allocating of suspend pagedir, new cold pages may appear.
  1569. * Kill them.
  1570. */
  1571. drain_local_pages(NULL);
  1572. copy_data_pages(&copy_bm, &orig_bm);
  1573. /*
  1574. * End of critical section. From now on, we can write to memory,
  1575. * but we should not touch disk. This specially means we must _not_
  1576. * touch swap space! Except we must write out our image of course.
  1577. */
  1578. nr_pages += nr_highmem;
  1579. nr_copy_pages = nr_pages;
  1580. nr_meta_pages = DIV_ROUND_UP(nr_pages * sizeof(long), PAGE_SIZE);
  1581. printk(KERN_INFO "PM: Hibernation image created (%d pages copied)\n",
  1582. nr_pages);
  1583. return 0;
  1584. }
  1585. #ifndef CONFIG_ARCH_HIBERNATION_HEADER
  1586. static int init_header_complete(struct swsusp_info *info)
  1587. {
  1588. memcpy(&info->uts, init_utsname(), sizeof(struct new_utsname));
  1589. info->version_code = LINUX_VERSION_CODE;
  1590. return 0;
  1591. }
  1592. static char *check_image_kernel(struct swsusp_info *info)
  1593. {
  1594. if (info->version_code != LINUX_VERSION_CODE)
  1595. return "kernel version";
  1596. if (strcmp(info->uts.sysname,init_utsname()->sysname))
  1597. return "system type";
  1598. if (strcmp(info->uts.release,init_utsname()->release))
  1599. return "kernel release";
  1600. if (strcmp(info->uts.version,init_utsname()->version))
  1601. return "version";
  1602. if (strcmp(info->uts.machine,init_utsname()->machine))
  1603. return "machine";
  1604. return NULL;
  1605. }
  1606. #endif /* CONFIG_ARCH_HIBERNATION_HEADER */
  1607. unsigned long snapshot_get_image_size(void)
  1608. {
  1609. return nr_copy_pages + nr_meta_pages + 1;
  1610. }
  1611. static int init_header(struct swsusp_info *info)
  1612. {
  1613. memset(info, 0, sizeof(struct swsusp_info));
  1614. info->num_physpages = get_num_physpages();
  1615. info->image_pages = nr_copy_pages;
  1616. info->pages = snapshot_get_image_size();
  1617. info->size = info->pages;
  1618. info->size <<= PAGE_SHIFT;
  1619. return init_header_complete(info);
  1620. }
  1621. /**
  1622. * pack_pfns - pfns corresponding to the set bits found in the bitmap @bm
  1623. * are stored in the array @buf[] (1 page at a time)
  1624. */
  1625. static inline void
  1626. pack_pfns(unsigned long *buf, struct memory_bitmap *bm)
  1627. {
  1628. int j;
  1629. for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
  1630. buf[j] = memory_bm_next_pfn(bm);
  1631. if (unlikely(buf[j] == BM_END_OF_MAP))
  1632. break;
  1633. /* Save page key for data page (s390 only). */
  1634. page_key_read(buf + j);
  1635. }
  1636. }
  1637. /**
  1638. * snapshot_read_next - used for reading the system memory snapshot.
  1639. *
  1640. * On the first call to it @handle should point to a zeroed
  1641. * snapshot_handle structure. The structure gets updated and a pointer
  1642. * to it should be passed to this function every next time.
  1643. *
  1644. * On success the function returns a positive number. Then, the caller
  1645. * is allowed to read up to the returned number of bytes from the memory
  1646. * location computed by the data_of() macro.
  1647. *
  1648. * The function returns 0 to indicate the end of data stream condition,
  1649. * and a negative number is returned on error. In such cases the
  1650. * structure pointed to by @handle is not updated and should not be used
  1651. * any more.
  1652. */
  1653. int snapshot_read_next(struct snapshot_handle *handle)
  1654. {
  1655. if (handle->cur > nr_meta_pages + nr_copy_pages)
  1656. return 0;
  1657. if (!buffer) {
  1658. /* This makes the buffer be freed by swsusp_free() */
  1659. buffer = get_image_page(GFP_ATOMIC, PG_ANY);
  1660. if (!buffer)
  1661. return -ENOMEM;
  1662. }
  1663. if (!handle->cur) {
  1664. int error;
  1665. error = init_header((struct swsusp_info *)buffer);
  1666. if (error)
  1667. return error;
  1668. handle->buffer = buffer;
  1669. memory_bm_position_reset(&orig_bm);
  1670. memory_bm_position_reset(&copy_bm);
  1671. } else if (handle->cur <= nr_meta_pages) {
  1672. clear_page(buffer);
  1673. pack_pfns(buffer, &orig_bm);
  1674. } else {
  1675. struct page *page;
  1676. page = pfn_to_page(memory_bm_next_pfn(&copy_bm));
  1677. if (PageHighMem(page)) {
  1678. /* Highmem pages are copied to the buffer,
  1679. * because we can't return with a kmapped
  1680. * highmem page (we may not be called again).
  1681. */
  1682. void *kaddr;
  1683. kaddr = kmap_atomic(page);
  1684. copy_page(buffer, kaddr);
  1685. kunmap_atomic(kaddr);
  1686. handle->buffer = buffer;
  1687. } else {
  1688. handle->buffer = page_address(page);
  1689. }
  1690. }
  1691. handle->cur++;
  1692. return PAGE_SIZE;
  1693. }
  1694. /**
  1695. * mark_unsafe_pages - mark the pages that cannot be used for storing
  1696. * the image during resume, because they conflict with the pages that
  1697. * had been used before suspend
  1698. */
  1699. static int mark_unsafe_pages(struct memory_bitmap *bm)
  1700. {
  1701. struct zone *zone;
  1702. unsigned long pfn, max_zone_pfn;
  1703. /* Clear page flags */
  1704. for_each_populated_zone(zone) {
  1705. max_zone_pfn = zone_end_pfn(zone);
  1706. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
  1707. if (pfn_valid(pfn))
  1708. swsusp_unset_page_free(pfn_to_page(pfn));
  1709. }
  1710. /* Mark pages that correspond to the "original" pfns as "unsafe" */
  1711. memory_bm_position_reset(bm);
  1712. do {
  1713. pfn = memory_bm_next_pfn(bm);
  1714. if (likely(pfn != BM_END_OF_MAP)) {
  1715. if (likely(pfn_valid(pfn)))
  1716. swsusp_set_page_free(pfn_to_page(pfn));
  1717. else
  1718. return -EFAULT;
  1719. }
  1720. } while (pfn != BM_END_OF_MAP);
  1721. allocated_unsafe_pages = 0;
  1722. return 0;
  1723. }
  1724. static void
  1725. duplicate_memory_bitmap(struct memory_bitmap *dst, struct memory_bitmap *src)
  1726. {
  1727. unsigned long pfn;
  1728. memory_bm_position_reset(src);
  1729. pfn = memory_bm_next_pfn(src);
  1730. while (pfn != BM_END_OF_MAP) {
  1731. memory_bm_set_bit(dst, pfn);
  1732. pfn = memory_bm_next_pfn(src);
  1733. }
  1734. }
  1735. static int check_header(struct swsusp_info *info)
  1736. {
  1737. char *reason;
  1738. reason = check_image_kernel(info);
  1739. if (!reason && info->num_physpages != get_num_physpages())
  1740. reason = "memory size";
  1741. if (reason) {
  1742. printk(KERN_ERR "PM: Image mismatch: %s\n", reason);
  1743. return -EPERM;
  1744. }
  1745. return 0;
  1746. }
  1747. /**
  1748. * load header - check the image header and copy data from it
  1749. */
  1750. static int
  1751. load_header(struct swsusp_info *info)
  1752. {
  1753. int error;
  1754. restore_pblist = NULL;
  1755. error = check_header(info);
  1756. if (!error) {
  1757. nr_copy_pages = info->image_pages;
  1758. nr_meta_pages = info->pages - info->image_pages - 1;
  1759. }
  1760. return error;
  1761. }
  1762. /**
  1763. * unpack_orig_pfns - for each element of @buf[] (1 page at a time) set
  1764. * the corresponding bit in the memory bitmap @bm
  1765. */
  1766. static int unpack_orig_pfns(unsigned long *buf, struct memory_bitmap *bm)
  1767. {
  1768. int j;
  1769. for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
  1770. if (unlikely(buf[j] == BM_END_OF_MAP))
  1771. break;
  1772. /* Extract and buffer page key for data page (s390 only). */
  1773. page_key_memorize(buf + j);
  1774. if (memory_bm_pfn_present(bm, buf[j]))
  1775. memory_bm_set_bit(bm, buf[j]);
  1776. else
  1777. return -EFAULT;
  1778. }
  1779. return 0;
  1780. }
  1781. /* List of "safe" pages that may be used to store data loaded from the suspend
  1782. * image
  1783. */
  1784. static struct linked_page *safe_pages_list;
  1785. #ifdef CONFIG_HIGHMEM
  1786. /* struct highmem_pbe is used for creating the list of highmem pages that
  1787. * should be restored atomically during the resume from disk, because the page
  1788. * frames they have occupied before the suspend are in use.
  1789. */
  1790. struct highmem_pbe {
  1791. struct page *copy_page; /* data is here now */
  1792. struct page *orig_page; /* data was here before the suspend */
  1793. struct highmem_pbe *next;
  1794. };
  1795. /* List of highmem PBEs needed for restoring the highmem pages that were
  1796. * allocated before the suspend and included in the suspend image, but have
  1797. * also been allocated by the "resume" kernel, so their contents cannot be
  1798. * written directly to their "original" page frames.
  1799. */
  1800. static struct highmem_pbe *highmem_pblist;
  1801. /**
  1802. * count_highmem_image_pages - compute the number of highmem pages in the
  1803. * suspend image. The bits in the memory bitmap @bm that correspond to the
  1804. * image pages are assumed to be set.
  1805. */
  1806. static unsigned int count_highmem_image_pages(struct memory_bitmap *bm)
  1807. {
  1808. unsigned long pfn;
  1809. unsigned int cnt = 0;
  1810. memory_bm_position_reset(bm);
  1811. pfn = memory_bm_next_pfn(bm);
  1812. while (pfn != BM_END_OF_MAP) {
  1813. if (PageHighMem(pfn_to_page(pfn)))
  1814. cnt++;
  1815. pfn = memory_bm_next_pfn(bm);
  1816. }
  1817. return cnt;
  1818. }
  1819. /**
  1820. * prepare_highmem_image - try to allocate as many highmem pages as
  1821. * there are highmem image pages (@nr_highmem_p points to the variable
  1822. * containing the number of highmem image pages). The pages that are
  1823. * "safe" (ie. will not be overwritten when the suspend image is
  1824. * restored) have the corresponding bits set in @bm (it must be
  1825. * unitialized).
  1826. *
  1827. * NOTE: This function should not be called if there are no highmem
  1828. * image pages.
  1829. */
  1830. static unsigned int safe_highmem_pages;
  1831. static struct memory_bitmap *safe_highmem_bm;
  1832. static int
  1833. prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
  1834. {
  1835. unsigned int to_alloc;
  1836. if (memory_bm_create(bm, GFP_ATOMIC, PG_SAFE))
  1837. return -ENOMEM;
  1838. if (get_highmem_buffer(PG_SAFE))
  1839. return -ENOMEM;
  1840. to_alloc = count_free_highmem_pages();
  1841. if (to_alloc > *nr_highmem_p)
  1842. to_alloc = *nr_highmem_p;
  1843. else
  1844. *nr_highmem_p = to_alloc;
  1845. safe_highmem_pages = 0;
  1846. while (to_alloc-- > 0) {
  1847. struct page *page;
  1848. page = alloc_page(__GFP_HIGHMEM);
  1849. if (!swsusp_page_is_free(page)) {
  1850. /* The page is "safe", set its bit the bitmap */
  1851. memory_bm_set_bit(bm, page_to_pfn(page));
  1852. safe_highmem_pages++;
  1853. }
  1854. /* Mark the page as allocated */
  1855. swsusp_set_page_forbidden(page);
  1856. swsusp_set_page_free(page);
  1857. }
  1858. memory_bm_position_reset(bm);
  1859. safe_highmem_bm = bm;
  1860. return 0;
  1861. }
  1862. /**
  1863. * get_highmem_page_buffer - for given highmem image page find the buffer
  1864. * that suspend_write_next() should set for its caller to write to.
  1865. *
  1866. * If the page is to be saved to its "original" page frame or a copy of
  1867. * the page is to be made in the highmem, @buffer is returned. Otherwise,
  1868. * the copy of the page is to be made in normal memory, so the address of
  1869. * the copy is returned.
  1870. *
  1871. * If @buffer is returned, the caller of suspend_write_next() will write
  1872. * the page's contents to @buffer, so they will have to be copied to the
  1873. * right location on the next call to suspend_write_next() and it is done
  1874. * with the help of copy_last_highmem_page(). For this purpose, if
  1875. * @buffer is returned, @last_highmem page is set to the page to which
  1876. * the data will have to be copied from @buffer.
  1877. */
  1878. static struct page *last_highmem_page;
  1879. static void *
  1880. get_highmem_page_buffer(struct page *page, struct chain_allocator *ca)
  1881. {
  1882. struct highmem_pbe *pbe;
  1883. void *kaddr;
  1884. if (swsusp_page_is_forbidden(page) && swsusp_page_is_free(page)) {
  1885. /* We have allocated the "original" page frame and we can
  1886. * use it directly to store the loaded page.
  1887. */
  1888. last_highmem_page = page;
  1889. return buffer;
  1890. }
  1891. /* The "original" page frame has not been allocated and we have to
  1892. * use a "safe" page frame to store the loaded page.
  1893. */
  1894. pbe = chain_alloc(ca, sizeof(struct highmem_pbe));
  1895. if (!pbe) {
  1896. swsusp_free();
  1897. return ERR_PTR(-ENOMEM);
  1898. }
  1899. pbe->orig_page = page;
  1900. if (safe_highmem_pages > 0) {
  1901. struct page *tmp;
  1902. /* Copy of the page will be stored in high memory */
  1903. kaddr = buffer;
  1904. tmp = pfn_to_page(memory_bm_next_pfn(safe_highmem_bm));
  1905. safe_highmem_pages--;
  1906. last_highmem_page = tmp;
  1907. pbe->copy_page = tmp;
  1908. } else {
  1909. /* Copy of the page will be stored in normal memory */
  1910. kaddr = safe_pages_list;
  1911. safe_pages_list = safe_pages_list->next;
  1912. pbe->copy_page = virt_to_page(kaddr);
  1913. }
  1914. pbe->next = highmem_pblist;
  1915. highmem_pblist = pbe;
  1916. return kaddr;
  1917. }
  1918. /**
  1919. * copy_last_highmem_page - copy the contents of a highmem image from
  1920. * @buffer, where the caller of snapshot_write_next() has place them,
  1921. * to the right location represented by @last_highmem_page .
  1922. */
  1923. static void copy_last_highmem_page(void)
  1924. {
  1925. if (last_highmem_page) {
  1926. void *dst;
  1927. dst = kmap_atomic(last_highmem_page);
  1928. copy_page(dst, buffer);
  1929. kunmap_atomic(dst);
  1930. last_highmem_page = NULL;
  1931. }
  1932. }
  1933. static inline int last_highmem_page_copied(void)
  1934. {
  1935. return !last_highmem_page;
  1936. }
  1937. static inline void free_highmem_data(void)
  1938. {
  1939. if (safe_highmem_bm)
  1940. memory_bm_free(safe_highmem_bm, PG_UNSAFE_CLEAR);
  1941. if (buffer)
  1942. free_image_page(buffer, PG_UNSAFE_CLEAR);
  1943. }
  1944. #else
  1945. static unsigned int
  1946. count_highmem_image_pages(struct memory_bitmap *bm) { return 0; }
  1947. static inline int
  1948. prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
  1949. {
  1950. return 0;
  1951. }
  1952. static inline void *
  1953. get_highmem_page_buffer(struct page *page, struct chain_allocator *ca)
  1954. {
  1955. return ERR_PTR(-EINVAL);
  1956. }
  1957. static inline void copy_last_highmem_page(void) {}
  1958. static inline int last_highmem_page_copied(void) { return 1; }
  1959. static inline void free_highmem_data(void) {}
  1960. #endif /* CONFIG_HIGHMEM */
  1961. /**
  1962. * prepare_image - use the memory bitmap @bm to mark the pages that will
  1963. * be overwritten in the process of restoring the system memory state
  1964. * from the suspend image ("unsafe" pages) and allocate memory for the
  1965. * image.
  1966. *
  1967. * The idea is to allocate a new memory bitmap first and then allocate
  1968. * as many pages as needed for the image data, but not to assign these
  1969. * pages to specific tasks initially. Instead, we just mark them as
  1970. * allocated and create a lists of "safe" pages that will be used
  1971. * later. On systems with high memory a list of "safe" highmem pages is
  1972. * also created.
  1973. */
  1974. #define PBES_PER_LINKED_PAGE (LINKED_PAGE_DATA_SIZE / sizeof(struct pbe))
  1975. static int
  1976. prepare_image(struct memory_bitmap *new_bm, struct memory_bitmap *bm)
  1977. {
  1978. unsigned int nr_pages, nr_highmem;
  1979. struct linked_page *sp_list, *lp;
  1980. int error;
  1981. /* If there is no highmem, the buffer will not be necessary */
  1982. free_image_page(buffer, PG_UNSAFE_CLEAR);
  1983. buffer = NULL;
  1984. nr_highmem = count_highmem_image_pages(bm);
  1985. error = mark_unsafe_pages(bm);
  1986. if (error)
  1987. goto Free;
  1988. error = memory_bm_create(new_bm, GFP_ATOMIC, PG_SAFE);
  1989. if (error)
  1990. goto Free;
  1991. duplicate_memory_bitmap(new_bm, bm);
  1992. memory_bm_free(bm, PG_UNSAFE_KEEP);
  1993. if (nr_highmem > 0) {
  1994. error = prepare_highmem_image(bm, &nr_highmem);
  1995. if (error)
  1996. goto Free;
  1997. }
  1998. /* Reserve some safe pages for potential later use.
  1999. *
  2000. * NOTE: This way we make sure there will be enough safe pages for the
  2001. * chain_alloc() in get_buffer(). It is a bit wasteful, but
  2002. * nr_copy_pages cannot be greater than 50% of the memory anyway.
  2003. */
  2004. sp_list = NULL;
  2005. /* nr_copy_pages cannot be lesser than allocated_unsafe_pages */
  2006. nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
  2007. nr_pages = DIV_ROUND_UP(nr_pages, PBES_PER_LINKED_PAGE);
  2008. while (nr_pages > 0) {
  2009. lp = get_image_page(GFP_ATOMIC, PG_SAFE);
  2010. if (!lp) {
  2011. error = -ENOMEM;
  2012. goto Free;
  2013. }
  2014. lp->next = sp_list;
  2015. sp_list = lp;
  2016. nr_pages--;
  2017. }
  2018. /* Preallocate memory for the image */
  2019. safe_pages_list = NULL;
  2020. nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
  2021. while (nr_pages > 0) {
  2022. lp = (struct linked_page *)get_zeroed_page(GFP_ATOMIC);
  2023. if (!lp) {
  2024. error = -ENOMEM;
  2025. goto Free;
  2026. }
  2027. if (!swsusp_page_is_free(virt_to_page(lp))) {
  2028. /* The page is "safe", add it to the list */
  2029. lp->next = safe_pages_list;
  2030. safe_pages_list = lp;
  2031. }
  2032. /* Mark the page as allocated */
  2033. swsusp_set_page_forbidden(virt_to_page(lp));
  2034. swsusp_set_page_free(virt_to_page(lp));
  2035. nr_pages--;
  2036. }
  2037. /* Free the reserved safe pages so that chain_alloc() can use them */
  2038. while (sp_list) {
  2039. lp = sp_list->next;
  2040. free_image_page(sp_list, PG_UNSAFE_CLEAR);
  2041. sp_list = lp;
  2042. }
  2043. return 0;
  2044. Free:
  2045. swsusp_free();
  2046. return error;
  2047. }
  2048. /**
  2049. * get_buffer - compute the address that snapshot_write_next() should
  2050. * set for its caller to write to.
  2051. */
  2052. static void *get_buffer(struct memory_bitmap *bm, struct chain_allocator *ca)
  2053. {
  2054. struct pbe *pbe;
  2055. struct page *page;
  2056. unsigned long pfn = memory_bm_next_pfn(bm);
  2057. if (pfn == BM_END_OF_MAP)
  2058. return ERR_PTR(-EFAULT);
  2059. page = pfn_to_page(pfn);
  2060. if (PageHighMem(page))
  2061. return get_highmem_page_buffer(page, ca);
  2062. if (swsusp_page_is_forbidden(page) && swsusp_page_is_free(page))
  2063. /* We have allocated the "original" page frame and we can
  2064. * use it directly to store the loaded page.
  2065. */
  2066. return page_address(page);
  2067. /* The "original" page frame has not been allocated and we have to
  2068. * use a "safe" page frame to store the loaded page.
  2069. */
  2070. pbe = chain_alloc(ca, sizeof(struct pbe));
  2071. if (!pbe) {
  2072. swsusp_free();
  2073. return ERR_PTR(-ENOMEM);
  2074. }
  2075. pbe->orig_address = page_address(page);
  2076. pbe->address = safe_pages_list;
  2077. safe_pages_list = safe_pages_list->next;
  2078. pbe->next = restore_pblist;
  2079. restore_pblist = pbe;
  2080. return pbe->address;
  2081. }
  2082. /**
  2083. * snapshot_write_next - used for writing the system memory snapshot.
  2084. *
  2085. * On the first call to it @handle should point to a zeroed
  2086. * snapshot_handle structure. The structure gets updated and a pointer
  2087. * to it should be passed to this function every next time.
  2088. *
  2089. * On success the function returns a positive number. Then, the caller
  2090. * is allowed to write up to the returned number of bytes to the memory
  2091. * location computed by the data_of() macro.
  2092. *
  2093. * The function returns 0 to indicate the "end of file" condition,
  2094. * and a negative number is returned on error. In such cases the
  2095. * structure pointed to by @handle is not updated and should not be used
  2096. * any more.
  2097. */
  2098. int snapshot_write_next(struct snapshot_handle *handle)
  2099. {
  2100. static struct chain_allocator ca;
  2101. int error = 0;
  2102. /* Check if we have already loaded the entire image */
  2103. if (handle->cur > 1 && handle->cur > nr_meta_pages + nr_copy_pages)
  2104. return 0;
  2105. handle->sync_read = 1;
  2106. if (!handle->cur) {
  2107. if (!buffer)
  2108. /* This makes the buffer be freed by swsusp_free() */
  2109. buffer = get_image_page(GFP_ATOMIC, PG_ANY);
  2110. if (!buffer)
  2111. return -ENOMEM;
  2112. handle->buffer = buffer;
  2113. } else if (handle->cur == 1) {
  2114. error = load_header(buffer);
  2115. if (error)
  2116. return error;
  2117. error = memory_bm_create(&copy_bm, GFP_ATOMIC, PG_ANY);
  2118. if (error)
  2119. return error;
  2120. /* Allocate buffer for page keys. */
  2121. error = page_key_alloc(nr_copy_pages);
  2122. if (error)
  2123. return error;
  2124. } else if (handle->cur <= nr_meta_pages + 1) {
  2125. error = unpack_orig_pfns(buffer, &copy_bm);
  2126. if (error)
  2127. return error;
  2128. if (handle->cur == nr_meta_pages + 1) {
  2129. error = prepare_image(&orig_bm, &copy_bm);
  2130. if (error)
  2131. return error;
  2132. chain_init(&ca, GFP_ATOMIC, PG_SAFE);
  2133. memory_bm_position_reset(&orig_bm);
  2134. restore_pblist = NULL;
  2135. handle->buffer = get_buffer(&orig_bm, &ca);
  2136. handle->sync_read = 0;
  2137. if (IS_ERR(handle->buffer))
  2138. return PTR_ERR(handle->buffer);
  2139. }
  2140. } else {
  2141. copy_last_highmem_page();
  2142. /* Restore page key for data page (s390 only). */
  2143. page_key_write(handle->buffer);
  2144. handle->buffer = get_buffer(&orig_bm, &ca);
  2145. if (IS_ERR(handle->buffer))
  2146. return PTR_ERR(handle->buffer);
  2147. if (handle->buffer != buffer)
  2148. handle->sync_read = 0;
  2149. }
  2150. handle->cur++;
  2151. return PAGE_SIZE;
  2152. }
  2153. /**
  2154. * snapshot_write_finalize - must be called after the last call to
  2155. * snapshot_write_next() in case the last page in the image happens
  2156. * to be a highmem page and its contents should be stored in the
  2157. * highmem. Additionally, it releases the memory that will not be
  2158. * used any more.
  2159. */
  2160. void snapshot_write_finalize(struct snapshot_handle *handle)
  2161. {
  2162. copy_last_highmem_page();
  2163. /* Restore page key for data page (s390 only). */
  2164. page_key_write(handle->buffer);
  2165. page_key_free();
  2166. /* Free only if we have loaded the image entirely */
  2167. if (handle->cur > 1 && handle->cur > nr_meta_pages + nr_copy_pages) {
  2168. memory_bm_free(&orig_bm, PG_UNSAFE_CLEAR);
  2169. free_highmem_data();
  2170. }
  2171. }
  2172. int snapshot_image_loaded(struct snapshot_handle *handle)
  2173. {
  2174. return !(!nr_copy_pages || !last_highmem_page_copied() ||
  2175. handle->cur <= nr_meta_pages + nr_copy_pages);
  2176. }
  2177. #ifdef CONFIG_HIGHMEM
  2178. /* Assumes that @buf is ready and points to a "safe" page */
  2179. static inline void
  2180. swap_two_pages_data(struct page *p1, struct page *p2, void *buf)
  2181. {
  2182. void *kaddr1, *kaddr2;
  2183. kaddr1 = kmap_atomic(p1);
  2184. kaddr2 = kmap_atomic(p2);
  2185. copy_page(buf, kaddr1);
  2186. copy_page(kaddr1, kaddr2);
  2187. copy_page(kaddr2, buf);
  2188. kunmap_atomic(kaddr2);
  2189. kunmap_atomic(kaddr1);
  2190. }
  2191. /**
  2192. * restore_highmem - for each highmem page that was allocated before
  2193. * the suspend and included in the suspend image, and also has been
  2194. * allocated by the "resume" kernel swap its current (ie. "before
  2195. * resume") contents with the previous (ie. "before suspend") one.
  2196. *
  2197. * If the resume eventually fails, we can call this function once
  2198. * again and restore the "before resume" highmem state.
  2199. */
  2200. int restore_highmem(void)
  2201. {
  2202. struct highmem_pbe *pbe = highmem_pblist;
  2203. void *buf;
  2204. if (!pbe)
  2205. return 0;
  2206. buf = get_image_page(GFP_ATOMIC, PG_SAFE);
  2207. if (!buf)
  2208. return -ENOMEM;
  2209. while (pbe) {
  2210. swap_two_pages_data(pbe->copy_page, pbe->orig_page, buf);
  2211. pbe = pbe->next;
  2212. }
  2213. free_image_page(buf, PG_UNSAFE_CLEAR);
  2214. return 0;
  2215. }
  2216. #endif /* CONFIG_HIGHMEM */