snapshot.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258
  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. {
  940. return preallocate_image_pages(nr_pages, GFP_IMAGE);
  941. }
  942. #ifdef CONFIG_HIGHMEM
  943. static unsigned long preallocate_image_highmem(unsigned long nr_pages)
  944. {
  945. return preallocate_image_pages(nr_pages, GFP_IMAGE | __GFP_HIGHMEM);
  946. }
  947. /**
  948. * __fraction - Compute (an approximation of) x * (multiplier / base)
  949. */
  950. static unsigned long __fraction(u64 x, u64 multiplier, u64 base)
  951. {
  952. x *= multiplier;
  953. do_div(x, base);
  954. return (unsigned long)x;
  955. }
  956. static unsigned long preallocate_highmem_fraction(unsigned long nr_pages,
  957. unsigned long highmem,
  958. unsigned long total)
  959. {
  960. unsigned long alloc = __fraction(nr_pages, highmem, total);
  961. return preallocate_image_pages(alloc, GFP_IMAGE | __GFP_HIGHMEM);
  962. }
  963. #else /* CONFIG_HIGHMEM */
  964. static inline unsigned long preallocate_image_highmem(unsigned long nr_pages)
  965. {
  966. return 0;
  967. }
  968. static inline unsigned long preallocate_highmem_fraction(unsigned long nr_pages,
  969. unsigned long highmem,
  970. unsigned long total)
  971. {
  972. return 0;
  973. }
  974. #endif /* CONFIG_HIGHMEM */
  975. /**
  976. * free_unnecessary_pages - Release preallocated pages not needed for the image
  977. */
  978. static void free_unnecessary_pages(void)
  979. {
  980. unsigned long save_highmem, to_free_normal, to_free_highmem;
  981. to_free_normal = alloc_normal - count_data_pages();
  982. save_highmem = count_highmem_pages();
  983. if (alloc_highmem > save_highmem) {
  984. to_free_highmem = alloc_highmem - save_highmem;
  985. } else {
  986. to_free_highmem = 0;
  987. to_free_normal -= save_highmem - alloc_highmem;
  988. }
  989. memory_bm_position_reset(&copy_bm);
  990. while (to_free_normal > 0 || to_free_highmem > 0) {
  991. unsigned long pfn = memory_bm_next_pfn(&copy_bm);
  992. struct page *page = pfn_to_page(pfn);
  993. if (PageHighMem(page)) {
  994. if (!to_free_highmem)
  995. continue;
  996. to_free_highmem--;
  997. alloc_highmem--;
  998. } else {
  999. if (!to_free_normal)
  1000. continue;
  1001. to_free_normal--;
  1002. alloc_normal--;
  1003. }
  1004. memory_bm_clear_bit(&copy_bm, pfn);
  1005. swsusp_unset_page_forbidden(page);
  1006. swsusp_unset_page_free(page);
  1007. __free_page(page);
  1008. }
  1009. }
  1010. /**
  1011. * minimum_image_size - Estimate the minimum acceptable size of an image
  1012. * @saveable: Number of saveable pages in the system.
  1013. *
  1014. * We want to avoid attempting to free too much memory too hard, so estimate the
  1015. * minimum acceptable size of a hibernation image to use as the lower limit for
  1016. * preallocating memory.
  1017. *
  1018. * We assume that the minimum image size should be proportional to
  1019. *
  1020. * [number of saveable pages] - [number of pages that can be freed in theory]
  1021. *
  1022. * where the second term is the sum of (1) reclaimable slab pages, (2) active
  1023. * and (3) inactive anonymouns pages, (4) active and (5) inactive file pages,
  1024. * minus mapped file pages.
  1025. */
  1026. static unsigned long minimum_image_size(unsigned long saveable)
  1027. {
  1028. unsigned long size;
  1029. size = global_page_state(NR_SLAB_RECLAIMABLE)
  1030. + global_page_state(NR_ACTIVE_ANON)
  1031. + global_page_state(NR_INACTIVE_ANON)
  1032. + global_page_state(NR_ACTIVE_FILE)
  1033. + global_page_state(NR_INACTIVE_FILE)
  1034. - global_page_state(NR_FILE_MAPPED);
  1035. return saveable <= size ? 0 : saveable - size;
  1036. }
  1037. /**
  1038. * hibernate_preallocate_memory - Preallocate memory for hibernation image
  1039. *
  1040. * To create a hibernation image it is necessary to make a copy of every page
  1041. * frame in use. We also need a number of page frames to be free during
  1042. * hibernation for allocations made while saving the image and for device
  1043. * drivers, in case they need to allocate memory from their hibernation
  1044. * callbacks (these two numbers are given by PAGES_FOR_IO and SPARE_PAGES,
  1045. * respectively, both of which are rough estimates). To make this happen, we
  1046. * compute the total number of available page frames and allocate at least
  1047. *
  1048. * ([page frames total] + PAGES_FOR_IO + [metadata pages]) / 2 + 2 * SPARE_PAGES
  1049. *
  1050. * of them, which corresponds to the maximum size of a hibernation image.
  1051. *
  1052. * If image_size is set below the number following from the above formula,
  1053. * the preallocation of memory is continued until the total number of saveable
  1054. * pages in the system is below the requested image size or the minimum
  1055. * acceptable image size returned by minimum_image_size(), whichever is greater.
  1056. */
  1057. int hibernate_preallocate_memory(void)
  1058. {
  1059. struct zone *zone;
  1060. unsigned long saveable, size, max_size, count, highmem, pages = 0;
  1061. unsigned long alloc, save_highmem, pages_highmem;
  1062. struct timeval start, stop;
  1063. int error;
  1064. printk(KERN_INFO "PM: Preallocating image memory... ");
  1065. do_gettimeofday(&start);
  1066. error = memory_bm_create(&orig_bm, GFP_IMAGE, PG_ANY);
  1067. if (error)
  1068. goto err_out;
  1069. error = memory_bm_create(&copy_bm, GFP_IMAGE, PG_ANY);
  1070. if (error)
  1071. goto err_out;
  1072. alloc_normal = 0;
  1073. alloc_highmem = 0;
  1074. /* Count the number of saveable data pages. */
  1075. save_highmem = count_highmem_pages();
  1076. saveable = count_data_pages();
  1077. /*
  1078. * Compute the total number of page frames we can use (count) and the
  1079. * number of pages needed for image metadata (size).
  1080. */
  1081. count = saveable;
  1082. saveable += save_highmem;
  1083. highmem = save_highmem;
  1084. size = 0;
  1085. for_each_populated_zone(zone) {
  1086. size += snapshot_additional_pages(zone);
  1087. if (is_highmem(zone))
  1088. highmem += zone_page_state(zone, NR_FREE_PAGES);
  1089. else
  1090. count += zone_page_state(zone, NR_FREE_PAGES);
  1091. }
  1092. count += highmem;
  1093. count -= totalreserve_pages;
  1094. /* Compute the maximum number of saveable pages to leave in memory. */
  1095. max_size = (count - (size + PAGES_FOR_IO)) / 2 - 2 * SPARE_PAGES;
  1096. size = DIV_ROUND_UP(image_size, PAGE_SIZE);
  1097. if (size > max_size)
  1098. size = max_size;
  1099. /*
  1100. * If the maximum is not less than the current number of saveable pages
  1101. * in memory, allocate page frames for the image and we're done.
  1102. */
  1103. if (size >= saveable) {
  1104. pages = preallocate_image_highmem(save_highmem);
  1105. pages += preallocate_image_memory(saveable - pages);
  1106. goto out;
  1107. }
  1108. /* Estimate the minimum size of the image. */
  1109. pages = minimum_image_size(saveable);
  1110. if (size < pages)
  1111. size = min_t(unsigned long, pages, max_size);
  1112. /*
  1113. * Let the memory management subsystem know that we're going to need a
  1114. * large number of page frames to allocate and make it free some memory.
  1115. * NOTE: If this is not done, performance will be hurt badly in some
  1116. * test cases.
  1117. */
  1118. shrink_all_memory(saveable - size);
  1119. /*
  1120. * The number of saveable pages in memory was too high, so apply some
  1121. * pressure to decrease it. First, make room for the largest possible
  1122. * image and fail if that doesn't work. Next, try to decrease the size
  1123. * of the image as much as indicated by 'size' using allocations from
  1124. * highmem and non-highmem zones separately.
  1125. */
  1126. pages_highmem = preallocate_image_highmem(highmem / 2);
  1127. alloc = (count - max_size) - pages_highmem;
  1128. pages = preallocate_image_memory(alloc);
  1129. if (pages < alloc)
  1130. goto err_out;
  1131. size = max_size - size;
  1132. alloc = size;
  1133. size = preallocate_highmem_fraction(size, highmem, count);
  1134. pages_highmem += size;
  1135. alloc -= size;
  1136. pages += preallocate_image_memory(alloc);
  1137. pages += pages_highmem;
  1138. /*
  1139. * We only need as many page frames for the image as there are saveable
  1140. * pages in memory, but we have allocated more. Release the excessive
  1141. * ones now.
  1142. */
  1143. free_unnecessary_pages();
  1144. out:
  1145. do_gettimeofday(&stop);
  1146. printk(KERN_CONT "done (allocated %lu pages)\n", pages);
  1147. swsusp_show_speed(&start, &stop, pages, "Allocated");
  1148. return 0;
  1149. err_out:
  1150. printk(KERN_CONT "\n");
  1151. swsusp_free();
  1152. return -ENOMEM;
  1153. }
  1154. #ifdef CONFIG_HIGHMEM
  1155. /**
  1156. * count_pages_for_highmem - compute the number of non-highmem pages
  1157. * that will be necessary for creating copies of highmem pages.
  1158. */
  1159. static unsigned int count_pages_for_highmem(unsigned int nr_highmem)
  1160. {
  1161. unsigned int free_highmem = count_free_highmem_pages() + alloc_highmem;
  1162. if (free_highmem >= nr_highmem)
  1163. nr_highmem = 0;
  1164. else
  1165. nr_highmem -= free_highmem;
  1166. return nr_highmem;
  1167. }
  1168. #else
  1169. static unsigned int
  1170. count_pages_for_highmem(unsigned int nr_highmem) { return 0; }
  1171. #endif /* CONFIG_HIGHMEM */
  1172. /**
  1173. * enough_free_mem - Make sure we have enough free memory for the
  1174. * snapshot image.
  1175. */
  1176. static int enough_free_mem(unsigned int nr_pages, unsigned int nr_highmem)
  1177. {
  1178. struct zone *zone;
  1179. unsigned int free = alloc_normal;
  1180. for_each_populated_zone(zone)
  1181. if (!is_highmem(zone))
  1182. free += zone_page_state(zone, NR_FREE_PAGES);
  1183. nr_pages += count_pages_for_highmem(nr_highmem);
  1184. pr_debug("PM: Normal pages needed: %u + %u, available pages: %u\n",
  1185. nr_pages, PAGES_FOR_IO, free);
  1186. return free > nr_pages + PAGES_FOR_IO;
  1187. }
  1188. #ifdef CONFIG_HIGHMEM
  1189. /**
  1190. * get_highmem_buffer - if there are some highmem pages in the suspend
  1191. * image, we may need the buffer to copy them and/or load their data.
  1192. */
  1193. static inline int get_highmem_buffer(int safe_needed)
  1194. {
  1195. buffer = get_image_page(GFP_ATOMIC | __GFP_COLD, safe_needed);
  1196. return buffer ? 0 : -ENOMEM;
  1197. }
  1198. /**
  1199. * alloc_highmem_image_pages - allocate some highmem pages for the image.
  1200. * Try to allocate as many pages as needed, but if the number of free
  1201. * highmem pages is lesser than that, allocate them all.
  1202. */
  1203. static inline unsigned int
  1204. alloc_highmem_pages(struct memory_bitmap *bm, unsigned int nr_highmem)
  1205. {
  1206. unsigned int to_alloc = count_free_highmem_pages();
  1207. if (to_alloc > nr_highmem)
  1208. to_alloc = nr_highmem;
  1209. nr_highmem -= to_alloc;
  1210. while (to_alloc-- > 0) {
  1211. struct page *page;
  1212. page = alloc_image_page(__GFP_HIGHMEM);
  1213. memory_bm_set_bit(bm, page_to_pfn(page));
  1214. }
  1215. return nr_highmem;
  1216. }
  1217. #else
  1218. static inline int get_highmem_buffer(int safe_needed) { return 0; }
  1219. static inline unsigned int
  1220. alloc_highmem_pages(struct memory_bitmap *bm, unsigned int n) { return 0; }
  1221. #endif /* CONFIG_HIGHMEM */
  1222. /**
  1223. * swsusp_alloc - allocate memory for the suspend image
  1224. *
  1225. * We first try to allocate as many highmem pages as there are
  1226. * saveable highmem pages in the system. If that fails, we allocate
  1227. * non-highmem pages for the copies of the remaining highmem ones.
  1228. *
  1229. * In this approach it is likely that the copies of highmem pages will
  1230. * also be located in the high memory, because of the way in which
  1231. * copy_data_pages() works.
  1232. */
  1233. static int
  1234. swsusp_alloc(struct memory_bitmap *orig_bm, struct memory_bitmap *copy_bm,
  1235. unsigned int nr_pages, unsigned int nr_highmem)
  1236. {
  1237. int error = 0;
  1238. if (nr_highmem > 0) {
  1239. error = get_highmem_buffer(PG_ANY);
  1240. if (error)
  1241. goto err_out;
  1242. if (nr_highmem > alloc_highmem) {
  1243. nr_highmem -= alloc_highmem;
  1244. nr_pages += alloc_highmem_pages(copy_bm, nr_highmem);
  1245. }
  1246. }
  1247. if (nr_pages > alloc_normal) {
  1248. nr_pages -= alloc_normal;
  1249. while (nr_pages-- > 0) {
  1250. struct page *page;
  1251. page = alloc_image_page(GFP_ATOMIC | __GFP_COLD);
  1252. if (!page)
  1253. goto err_out;
  1254. memory_bm_set_bit(copy_bm, page_to_pfn(page));
  1255. }
  1256. }
  1257. return 0;
  1258. err_out:
  1259. swsusp_free();
  1260. return error;
  1261. }
  1262. asmlinkage int swsusp_save(void)
  1263. {
  1264. unsigned int nr_pages, nr_highmem;
  1265. printk(KERN_INFO "PM: Creating hibernation image:\n");
  1266. drain_local_pages(NULL);
  1267. nr_pages = count_data_pages();
  1268. nr_highmem = count_highmem_pages();
  1269. printk(KERN_INFO "PM: Need to copy %u pages\n", nr_pages + nr_highmem);
  1270. if (!enough_free_mem(nr_pages, nr_highmem)) {
  1271. printk(KERN_ERR "PM: Not enough free memory\n");
  1272. return -ENOMEM;
  1273. }
  1274. if (swsusp_alloc(&orig_bm, &copy_bm, nr_pages, nr_highmem)) {
  1275. printk(KERN_ERR "PM: Memory allocation failed\n");
  1276. return -ENOMEM;
  1277. }
  1278. /* During allocating of suspend pagedir, new cold pages may appear.
  1279. * Kill them.
  1280. */
  1281. drain_local_pages(NULL);
  1282. copy_data_pages(&copy_bm, &orig_bm);
  1283. /*
  1284. * End of critical section. From now on, we can write to memory,
  1285. * but we should not touch disk. This specially means we must _not_
  1286. * touch swap space! Except we must write out our image of course.
  1287. */
  1288. nr_pages += nr_highmem;
  1289. nr_copy_pages = nr_pages;
  1290. nr_meta_pages = DIV_ROUND_UP(nr_pages * sizeof(long), PAGE_SIZE);
  1291. printk(KERN_INFO "PM: Hibernation image created (%d pages copied)\n",
  1292. nr_pages);
  1293. return 0;
  1294. }
  1295. #ifndef CONFIG_ARCH_HIBERNATION_HEADER
  1296. static int init_header_complete(struct swsusp_info *info)
  1297. {
  1298. memcpy(&info->uts, init_utsname(), sizeof(struct new_utsname));
  1299. info->version_code = LINUX_VERSION_CODE;
  1300. return 0;
  1301. }
  1302. static char *check_image_kernel(struct swsusp_info *info)
  1303. {
  1304. if (info->version_code != LINUX_VERSION_CODE)
  1305. return "kernel version";
  1306. if (strcmp(info->uts.sysname,init_utsname()->sysname))
  1307. return "system type";
  1308. if (strcmp(info->uts.release,init_utsname()->release))
  1309. return "kernel release";
  1310. if (strcmp(info->uts.version,init_utsname()->version))
  1311. return "version";
  1312. if (strcmp(info->uts.machine,init_utsname()->machine))
  1313. return "machine";
  1314. return NULL;
  1315. }
  1316. #endif /* CONFIG_ARCH_HIBERNATION_HEADER */
  1317. unsigned long snapshot_get_image_size(void)
  1318. {
  1319. return nr_copy_pages + nr_meta_pages + 1;
  1320. }
  1321. static int init_header(struct swsusp_info *info)
  1322. {
  1323. memset(info, 0, sizeof(struct swsusp_info));
  1324. info->num_physpages = num_physpages;
  1325. info->image_pages = nr_copy_pages;
  1326. info->pages = snapshot_get_image_size();
  1327. info->size = info->pages;
  1328. info->size <<= PAGE_SHIFT;
  1329. return init_header_complete(info);
  1330. }
  1331. /**
  1332. * pack_pfns - pfns corresponding to the set bits found in the bitmap @bm
  1333. * are stored in the array @buf[] (1 page at a time)
  1334. */
  1335. static inline void
  1336. pack_pfns(unsigned long *buf, struct memory_bitmap *bm)
  1337. {
  1338. int j;
  1339. for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
  1340. buf[j] = memory_bm_next_pfn(bm);
  1341. if (unlikely(buf[j] == BM_END_OF_MAP))
  1342. break;
  1343. }
  1344. }
  1345. /**
  1346. * snapshot_read_next - used for reading the system memory snapshot.
  1347. *
  1348. * On the first call to it @handle should point to a zeroed
  1349. * snapshot_handle structure. The structure gets updated and a pointer
  1350. * to it should be passed to this function every next time.
  1351. *
  1352. * On success the function returns a positive number. Then, the caller
  1353. * is allowed to read up to the returned number of bytes from the memory
  1354. * location computed by the data_of() macro.
  1355. *
  1356. * The function returns 0 to indicate the end of data stream condition,
  1357. * and a negative number is returned on error. In such cases the
  1358. * structure pointed to by @handle is not updated and should not be used
  1359. * any more.
  1360. */
  1361. int snapshot_read_next(struct snapshot_handle *handle)
  1362. {
  1363. if (handle->cur > nr_meta_pages + nr_copy_pages)
  1364. return 0;
  1365. if (!buffer) {
  1366. /* This makes the buffer be freed by swsusp_free() */
  1367. buffer = get_image_page(GFP_ATOMIC, PG_ANY);
  1368. if (!buffer)
  1369. return -ENOMEM;
  1370. }
  1371. if (!handle->cur) {
  1372. int error;
  1373. error = init_header((struct swsusp_info *)buffer);
  1374. if (error)
  1375. return error;
  1376. handle->buffer = buffer;
  1377. memory_bm_position_reset(&orig_bm);
  1378. memory_bm_position_reset(&copy_bm);
  1379. } else if (handle->cur <= nr_meta_pages) {
  1380. memset(buffer, 0, PAGE_SIZE);
  1381. pack_pfns(buffer, &orig_bm);
  1382. } else {
  1383. struct page *page;
  1384. page = pfn_to_page(memory_bm_next_pfn(&copy_bm));
  1385. if (PageHighMem(page)) {
  1386. /* Highmem pages are copied to the buffer,
  1387. * because we can't return with a kmapped
  1388. * highmem page (we may not be called again).
  1389. */
  1390. void *kaddr;
  1391. kaddr = kmap_atomic(page, KM_USER0);
  1392. memcpy(buffer, kaddr, PAGE_SIZE);
  1393. kunmap_atomic(kaddr, KM_USER0);
  1394. handle->buffer = buffer;
  1395. } else {
  1396. handle->buffer = page_address(page);
  1397. }
  1398. }
  1399. handle->cur++;
  1400. return PAGE_SIZE;
  1401. }
  1402. /**
  1403. * mark_unsafe_pages - mark the pages that cannot be used for storing
  1404. * the image during resume, because they conflict with the pages that
  1405. * had been used before suspend
  1406. */
  1407. static int mark_unsafe_pages(struct memory_bitmap *bm)
  1408. {
  1409. struct zone *zone;
  1410. unsigned long pfn, max_zone_pfn;
  1411. /* Clear page flags */
  1412. for_each_populated_zone(zone) {
  1413. max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
  1414. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
  1415. if (pfn_valid(pfn))
  1416. swsusp_unset_page_free(pfn_to_page(pfn));
  1417. }
  1418. /* Mark pages that correspond to the "original" pfns as "unsafe" */
  1419. memory_bm_position_reset(bm);
  1420. do {
  1421. pfn = memory_bm_next_pfn(bm);
  1422. if (likely(pfn != BM_END_OF_MAP)) {
  1423. if (likely(pfn_valid(pfn)))
  1424. swsusp_set_page_free(pfn_to_page(pfn));
  1425. else
  1426. return -EFAULT;
  1427. }
  1428. } while (pfn != BM_END_OF_MAP);
  1429. allocated_unsafe_pages = 0;
  1430. return 0;
  1431. }
  1432. static void
  1433. duplicate_memory_bitmap(struct memory_bitmap *dst, struct memory_bitmap *src)
  1434. {
  1435. unsigned long pfn;
  1436. memory_bm_position_reset(src);
  1437. pfn = memory_bm_next_pfn(src);
  1438. while (pfn != BM_END_OF_MAP) {
  1439. memory_bm_set_bit(dst, pfn);
  1440. pfn = memory_bm_next_pfn(src);
  1441. }
  1442. }
  1443. static int check_header(struct swsusp_info *info)
  1444. {
  1445. char *reason;
  1446. reason = check_image_kernel(info);
  1447. if (!reason && info->num_physpages != num_physpages)
  1448. reason = "memory size";
  1449. if (reason) {
  1450. printk(KERN_ERR "PM: Image mismatch: %s\n", reason);
  1451. return -EPERM;
  1452. }
  1453. return 0;
  1454. }
  1455. /**
  1456. * load header - check the image header and copy data from it
  1457. */
  1458. static int
  1459. load_header(struct swsusp_info *info)
  1460. {
  1461. int error;
  1462. restore_pblist = NULL;
  1463. error = check_header(info);
  1464. if (!error) {
  1465. nr_copy_pages = info->image_pages;
  1466. nr_meta_pages = info->pages - info->image_pages - 1;
  1467. }
  1468. return error;
  1469. }
  1470. /**
  1471. * unpack_orig_pfns - for each element of @buf[] (1 page at a time) set
  1472. * the corresponding bit in the memory bitmap @bm
  1473. */
  1474. static int unpack_orig_pfns(unsigned long *buf, struct memory_bitmap *bm)
  1475. {
  1476. int j;
  1477. for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
  1478. if (unlikely(buf[j] == BM_END_OF_MAP))
  1479. break;
  1480. if (memory_bm_pfn_present(bm, buf[j]))
  1481. memory_bm_set_bit(bm, buf[j]);
  1482. else
  1483. return -EFAULT;
  1484. }
  1485. return 0;
  1486. }
  1487. /* List of "safe" pages that may be used to store data loaded from the suspend
  1488. * image
  1489. */
  1490. static struct linked_page *safe_pages_list;
  1491. #ifdef CONFIG_HIGHMEM
  1492. /* struct highmem_pbe is used for creating the list of highmem pages that
  1493. * should be restored atomically during the resume from disk, because the page
  1494. * frames they have occupied before the suspend are in use.
  1495. */
  1496. struct highmem_pbe {
  1497. struct page *copy_page; /* data is here now */
  1498. struct page *orig_page; /* data was here before the suspend */
  1499. struct highmem_pbe *next;
  1500. };
  1501. /* List of highmem PBEs needed for restoring the highmem pages that were
  1502. * allocated before the suspend and included in the suspend image, but have
  1503. * also been allocated by the "resume" kernel, so their contents cannot be
  1504. * written directly to their "original" page frames.
  1505. */
  1506. static struct highmem_pbe *highmem_pblist;
  1507. /**
  1508. * count_highmem_image_pages - compute the number of highmem pages in the
  1509. * suspend image. The bits in the memory bitmap @bm that correspond to the
  1510. * image pages are assumed to be set.
  1511. */
  1512. static unsigned int count_highmem_image_pages(struct memory_bitmap *bm)
  1513. {
  1514. unsigned long pfn;
  1515. unsigned int cnt = 0;
  1516. memory_bm_position_reset(bm);
  1517. pfn = memory_bm_next_pfn(bm);
  1518. while (pfn != BM_END_OF_MAP) {
  1519. if (PageHighMem(pfn_to_page(pfn)))
  1520. cnt++;
  1521. pfn = memory_bm_next_pfn(bm);
  1522. }
  1523. return cnt;
  1524. }
  1525. /**
  1526. * prepare_highmem_image - try to allocate as many highmem pages as
  1527. * there are highmem image pages (@nr_highmem_p points to the variable
  1528. * containing the number of highmem image pages). The pages that are
  1529. * "safe" (ie. will not be overwritten when the suspend image is
  1530. * restored) have the corresponding bits set in @bm (it must be
  1531. * unitialized).
  1532. *
  1533. * NOTE: This function should not be called if there are no highmem
  1534. * image pages.
  1535. */
  1536. static unsigned int safe_highmem_pages;
  1537. static struct memory_bitmap *safe_highmem_bm;
  1538. static int
  1539. prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
  1540. {
  1541. unsigned int to_alloc;
  1542. if (memory_bm_create(bm, GFP_ATOMIC, PG_SAFE))
  1543. return -ENOMEM;
  1544. if (get_highmem_buffer(PG_SAFE))
  1545. return -ENOMEM;
  1546. to_alloc = count_free_highmem_pages();
  1547. if (to_alloc > *nr_highmem_p)
  1548. to_alloc = *nr_highmem_p;
  1549. else
  1550. *nr_highmem_p = to_alloc;
  1551. safe_highmem_pages = 0;
  1552. while (to_alloc-- > 0) {
  1553. struct page *page;
  1554. page = alloc_page(__GFP_HIGHMEM);
  1555. if (!swsusp_page_is_free(page)) {
  1556. /* The page is "safe", set its bit the bitmap */
  1557. memory_bm_set_bit(bm, page_to_pfn(page));
  1558. safe_highmem_pages++;
  1559. }
  1560. /* Mark the page as allocated */
  1561. swsusp_set_page_forbidden(page);
  1562. swsusp_set_page_free(page);
  1563. }
  1564. memory_bm_position_reset(bm);
  1565. safe_highmem_bm = bm;
  1566. return 0;
  1567. }
  1568. /**
  1569. * get_highmem_page_buffer - for given highmem image page find the buffer
  1570. * that suspend_write_next() should set for its caller to write to.
  1571. *
  1572. * If the page is to be saved to its "original" page frame or a copy of
  1573. * the page is to be made in the highmem, @buffer is returned. Otherwise,
  1574. * the copy of the page is to be made in normal memory, so the address of
  1575. * the copy is returned.
  1576. *
  1577. * If @buffer is returned, the caller of suspend_write_next() will write
  1578. * the page's contents to @buffer, so they will have to be copied to the
  1579. * right location on the next call to suspend_write_next() and it is done
  1580. * with the help of copy_last_highmem_page(). For this purpose, if
  1581. * @buffer is returned, @last_highmem page is set to the page to which
  1582. * the data will have to be copied from @buffer.
  1583. */
  1584. static struct page *last_highmem_page;
  1585. static void *
  1586. get_highmem_page_buffer(struct page *page, struct chain_allocator *ca)
  1587. {
  1588. struct highmem_pbe *pbe;
  1589. void *kaddr;
  1590. if (swsusp_page_is_forbidden(page) && swsusp_page_is_free(page)) {
  1591. /* We have allocated the "original" page frame and we can
  1592. * use it directly to store the loaded page.
  1593. */
  1594. last_highmem_page = page;
  1595. return buffer;
  1596. }
  1597. /* The "original" page frame has not been allocated and we have to
  1598. * use a "safe" page frame to store the loaded page.
  1599. */
  1600. pbe = chain_alloc(ca, sizeof(struct highmem_pbe));
  1601. if (!pbe) {
  1602. swsusp_free();
  1603. return ERR_PTR(-ENOMEM);
  1604. }
  1605. pbe->orig_page = page;
  1606. if (safe_highmem_pages > 0) {
  1607. struct page *tmp;
  1608. /* Copy of the page will be stored in high memory */
  1609. kaddr = buffer;
  1610. tmp = pfn_to_page(memory_bm_next_pfn(safe_highmem_bm));
  1611. safe_highmem_pages--;
  1612. last_highmem_page = tmp;
  1613. pbe->copy_page = tmp;
  1614. } else {
  1615. /* Copy of the page will be stored in normal memory */
  1616. kaddr = safe_pages_list;
  1617. safe_pages_list = safe_pages_list->next;
  1618. pbe->copy_page = virt_to_page(kaddr);
  1619. }
  1620. pbe->next = highmem_pblist;
  1621. highmem_pblist = pbe;
  1622. return kaddr;
  1623. }
  1624. /**
  1625. * copy_last_highmem_page - copy the contents of a highmem image from
  1626. * @buffer, where the caller of snapshot_write_next() has place them,
  1627. * to the right location represented by @last_highmem_page .
  1628. */
  1629. static void copy_last_highmem_page(void)
  1630. {
  1631. if (last_highmem_page) {
  1632. void *dst;
  1633. dst = kmap_atomic(last_highmem_page, KM_USER0);
  1634. memcpy(dst, buffer, PAGE_SIZE);
  1635. kunmap_atomic(dst, KM_USER0);
  1636. last_highmem_page = NULL;
  1637. }
  1638. }
  1639. static inline int last_highmem_page_copied(void)
  1640. {
  1641. return !last_highmem_page;
  1642. }
  1643. static inline void free_highmem_data(void)
  1644. {
  1645. if (safe_highmem_bm)
  1646. memory_bm_free(safe_highmem_bm, PG_UNSAFE_CLEAR);
  1647. if (buffer)
  1648. free_image_page(buffer, PG_UNSAFE_CLEAR);
  1649. }
  1650. #else
  1651. static inline int get_safe_write_buffer(void) { return 0; }
  1652. static unsigned int
  1653. count_highmem_image_pages(struct memory_bitmap *bm) { return 0; }
  1654. static inline int
  1655. prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
  1656. {
  1657. return 0;
  1658. }
  1659. static inline void *
  1660. get_highmem_page_buffer(struct page *page, struct chain_allocator *ca)
  1661. {
  1662. return ERR_PTR(-EINVAL);
  1663. }
  1664. static inline void copy_last_highmem_page(void) {}
  1665. static inline int last_highmem_page_copied(void) { return 1; }
  1666. static inline void free_highmem_data(void) {}
  1667. #endif /* CONFIG_HIGHMEM */
  1668. /**
  1669. * prepare_image - use the memory bitmap @bm to mark the pages that will
  1670. * be overwritten in the process of restoring the system memory state
  1671. * from the suspend image ("unsafe" pages) and allocate memory for the
  1672. * image.
  1673. *
  1674. * The idea is to allocate a new memory bitmap first and then allocate
  1675. * as many pages as needed for the image data, but not to assign these
  1676. * pages to specific tasks initially. Instead, we just mark them as
  1677. * allocated and create a lists of "safe" pages that will be used
  1678. * later. On systems with high memory a list of "safe" highmem pages is
  1679. * also created.
  1680. */
  1681. #define PBES_PER_LINKED_PAGE (LINKED_PAGE_DATA_SIZE / sizeof(struct pbe))
  1682. static int
  1683. prepare_image(struct memory_bitmap *new_bm, struct memory_bitmap *bm)
  1684. {
  1685. unsigned int nr_pages, nr_highmem;
  1686. struct linked_page *sp_list, *lp;
  1687. int error;
  1688. /* If there is no highmem, the buffer will not be necessary */
  1689. free_image_page(buffer, PG_UNSAFE_CLEAR);
  1690. buffer = NULL;
  1691. nr_highmem = count_highmem_image_pages(bm);
  1692. error = mark_unsafe_pages(bm);
  1693. if (error)
  1694. goto Free;
  1695. error = memory_bm_create(new_bm, GFP_ATOMIC, PG_SAFE);
  1696. if (error)
  1697. goto Free;
  1698. duplicate_memory_bitmap(new_bm, bm);
  1699. memory_bm_free(bm, PG_UNSAFE_KEEP);
  1700. if (nr_highmem > 0) {
  1701. error = prepare_highmem_image(bm, &nr_highmem);
  1702. if (error)
  1703. goto Free;
  1704. }
  1705. /* Reserve some safe pages for potential later use.
  1706. *
  1707. * NOTE: This way we make sure there will be enough safe pages for the
  1708. * chain_alloc() in get_buffer(). It is a bit wasteful, but
  1709. * nr_copy_pages cannot be greater than 50% of the memory anyway.
  1710. */
  1711. sp_list = NULL;
  1712. /* nr_copy_pages cannot be lesser than allocated_unsafe_pages */
  1713. nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
  1714. nr_pages = DIV_ROUND_UP(nr_pages, PBES_PER_LINKED_PAGE);
  1715. while (nr_pages > 0) {
  1716. lp = get_image_page(GFP_ATOMIC, PG_SAFE);
  1717. if (!lp) {
  1718. error = -ENOMEM;
  1719. goto Free;
  1720. }
  1721. lp->next = sp_list;
  1722. sp_list = lp;
  1723. nr_pages--;
  1724. }
  1725. /* Preallocate memory for the image */
  1726. safe_pages_list = NULL;
  1727. nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
  1728. while (nr_pages > 0) {
  1729. lp = (struct linked_page *)get_zeroed_page(GFP_ATOMIC);
  1730. if (!lp) {
  1731. error = -ENOMEM;
  1732. goto Free;
  1733. }
  1734. if (!swsusp_page_is_free(virt_to_page(lp))) {
  1735. /* The page is "safe", add it to the list */
  1736. lp->next = safe_pages_list;
  1737. safe_pages_list = lp;
  1738. }
  1739. /* Mark the page as allocated */
  1740. swsusp_set_page_forbidden(virt_to_page(lp));
  1741. swsusp_set_page_free(virt_to_page(lp));
  1742. nr_pages--;
  1743. }
  1744. /* Free the reserved safe pages so that chain_alloc() can use them */
  1745. while (sp_list) {
  1746. lp = sp_list->next;
  1747. free_image_page(sp_list, PG_UNSAFE_CLEAR);
  1748. sp_list = lp;
  1749. }
  1750. return 0;
  1751. Free:
  1752. swsusp_free();
  1753. return error;
  1754. }
  1755. /**
  1756. * get_buffer - compute the address that snapshot_write_next() should
  1757. * set for its caller to write to.
  1758. */
  1759. static void *get_buffer(struct memory_bitmap *bm, struct chain_allocator *ca)
  1760. {
  1761. struct pbe *pbe;
  1762. struct page *page;
  1763. unsigned long pfn = memory_bm_next_pfn(bm);
  1764. if (pfn == BM_END_OF_MAP)
  1765. return ERR_PTR(-EFAULT);
  1766. page = pfn_to_page(pfn);
  1767. if (PageHighMem(page))
  1768. return get_highmem_page_buffer(page, ca);
  1769. if (swsusp_page_is_forbidden(page) && swsusp_page_is_free(page))
  1770. /* We have allocated the "original" page frame and we can
  1771. * use it directly to store the loaded page.
  1772. */
  1773. return page_address(page);
  1774. /* The "original" page frame has not been allocated and we have to
  1775. * use a "safe" page frame to store the loaded page.
  1776. */
  1777. pbe = chain_alloc(ca, sizeof(struct pbe));
  1778. if (!pbe) {
  1779. swsusp_free();
  1780. return ERR_PTR(-ENOMEM);
  1781. }
  1782. pbe->orig_address = page_address(page);
  1783. pbe->address = safe_pages_list;
  1784. safe_pages_list = safe_pages_list->next;
  1785. pbe->next = restore_pblist;
  1786. restore_pblist = pbe;
  1787. return pbe->address;
  1788. }
  1789. /**
  1790. * snapshot_write_next - used for writing the system memory snapshot.
  1791. *
  1792. * On the first call to it @handle should point to a zeroed
  1793. * snapshot_handle structure. The structure gets updated and a pointer
  1794. * to it should be passed to this function every next time.
  1795. *
  1796. * On success the function returns a positive number. Then, the caller
  1797. * is allowed to write up to the returned number of bytes to the memory
  1798. * location computed by the data_of() macro.
  1799. *
  1800. * The function returns 0 to indicate the "end of file" condition,
  1801. * and a negative number is returned on error. In such cases the
  1802. * structure pointed to by @handle is not updated and should not be used
  1803. * any more.
  1804. */
  1805. int snapshot_write_next(struct snapshot_handle *handle)
  1806. {
  1807. static struct chain_allocator ca;
  1808. int error = 0;
  1809. /* Check if we have already loaded the entire image */
  1810. if (handle->cur > 1 && handle->cur > nr_meta_pages + nr_copy_pages)
  1811. return 0;
  1812. handle->sync_read = 1;
  1813. if (!handle->cur) {
  1814. if (!buffer)
  1815. /* This makes the buffer be freed by swsusp_free() */
  1816. buffer = get_image_page(GFP_ATOMIC, PG_ANY);
  1817. if (!buffer)
  1818. return -ENOMEM;
  1819. handle->buffer = buffer;
  1820. } else if (handle->cur == 1) {
  1821. error = load_header(buffer);
  1822. if (error)
  1823. return error;
  1824. error = memory_bm_create(&copy_bm, GFP_ATOMIC, PG_ANY);
  1825. if (error)
  1826. return error;
  1827. } else if (handle->cur <= nr_meta_pages + 1) {
  1828. error = unpack_orig_pfns(buffer, &copy_bm);
  1829. if (error)
  1830. return error;
  1831. if (handle->cur == nr_meta_pages + 1) {
  1832. error = prepare_image(&orig_bm, &copy_bm);
  1833. if (error)
  1834. return error;
  1835. chain_init(&ca, GFP_ATOMIC, PG_SAFE);
  1836. memory_bm_position_reset(&orig_bm);
  1837. restore_pblist = NULL;
  1838. handle->buffer = get_buffer(&orig_bm, &ca);
  1839. handle->sync_read = 0;
  1840. if (IS_ERR(handle->buffer))
  1841. return PTR_ERR(handle->buffer);
  1842. }
  1843. } else {
  1844. copy_last_highmem_page();
  1845. handle->buffer = get_buffer(&orig_bm, &ca);
  1846. if (IS_ERR(handle->buffer))
  1847. return PTR_ERR(handle->buffer);
  1848. if (handle->buffer != buffer)
  1849. handle->sync_read = 0;
  1850. }
  1851. handle->cur++;
  1852. return PAGE_SIZE;
  1853. }
  1854. /**
  1855. * snapshot_write_finalize - must be called after the last call to
  1856. * snapshot_write_next() in case the last page in the image happens
  1857. * to be a highmem page and its contents should be stored in the
  1858. * highmem. Additionally, it releases the memory that will not be
  1859. * used any more.
  1860. */
  1861. void snapshot_write_finalize(struct snapshot_handle *handle)
  1862. {
  1863. copy_last_highmem_page();
  1864. /* Free only if we have loaded the image entirely */
  1865. if (handle->cur > 1 && handle->cur > nr_meta_pages + nr_copy_pages) {
  1866. memory_bm_free(&orig_bm, PG_UNSAFE_CLEAR);
  1867. free_highmem_data();
  1868. }
  1869. }
  1870. int snapshot_image_loaded(struct snapshot_handle *handle)
  1871. {
  1872. return !(!nr_copy_pages || !last_highmem_page_copied() ||
  1873. handle->cur <= nr_meta_pages + nr_copy_pages);
  1874. }
  1875. #ifdef CONFIG_HIGHMEM
  1876. /* Assumes that @buf is ready and points to a "safe" page */
  1877. static inline void
  1878. swap_two_pages_data(struct page *p1, struct page *p2, void *buf)
  1879. {
  1880. void *kaddr1, *kaddr2;
  1881. kaddr1 = kmap_atomic(p1, KM_USER0);
  1882. kaddr2 = kmap_atomic(p2, KM_USER1);
  1883. memcpy(buf, kaddr1, PAGE_SIZE);
  1884. memcpy(kaddr1, kaddr2, PAGE_SIZE);
  1885. memcpy(kaddr2, buf, PAGE_SIZE);
  1886. kunmap_atomic(kaddr1, KM_USER0);
  1887. kunmap_atomic(kaddr2, KM_USER1);
  1888. }
  1889. /**
  1890. * restore_highmem - for each highmem page that was allocated before
  1891. * the suspend and included in the suspend image, and also has been
  1892. * allocated by the "resume" kernel swap its current (ie. "before
  1893. * resume") contents with the previous (ie. "before suspend") one.
  1894. *
  1895. * If the resume eventually fails, we can call this function once
  1896. * again and restore the "before resume" highmem state.
  1897. */
  1898. int restore_highmem(void)
  1899. {
  1900. struct highmem_pbe *pbe = highmem_pblist;
  1901. void *buf;
  1902. if (!pbe)
  1903. return 0;
  1904. buf = get_image_page(GFP_ATOMIC, PG_SAFE);
  1905. if (!buf)
  1906. return -ENOMEM;
  1907. while (pbe) {
  1908. swap_two_pages_data(pbe->copy_page, pbe->orig_page, buf);
  1909. pbe = pbe->next;
  1910. }
  1911. free_image_page(buf, PG_UNSAFE_CLEAR);
  1912. return 0;
  1913. }
  1914. #endif /* CONFIG_HIGHMEM */