snapshot.c 61 KB

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