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