snapshot.c 61 KB

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