snapshot.c 44 KB

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