snapshot.c 59 KB

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