snapshot.c 59 KB

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