snapshot.c 68 KB

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