snapshot.c 72 KB

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