snapshot.c 51 KB

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