snapshot.c 51 KB

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