snapshot.c 52 KB

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