snapshot.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620
  1. /*
  2. * linux/kernel/power/snapshot.c
  3. *
  4. * This file provides system snapshot/restore functionality for swsusp.
  5. *
  6. * Copyright (C) 1998-2005 Pavel Machek <pavel@ucw.cz>
  7. * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl>
  8. *
  9. * This file is released under the GPLv2.
  10. *
  11. */
  12. #include <linux/version.h>
  13. #include <linux/module.h>
  14. #include <linux/mm.h>
  15. #include <linux/suspend.h>
  16. #include <linux/delay.h>
  17. #include <linux/bitops.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/kernel.h>
  20. #include <linux/pm.h>
  21. #include <linux/device.h>
  22. #include <linux/init.h>
  23. #include <linux/bootmem.h>
  24. #include <linux/syscalls.h>
  25. #include <linux/console.h>
  26. #include <linux/highmem.h>
  27. #include <linux/list.h>
  28. #include <linux/slab.h>
  29. #include <linux/compiler.h>
  30. #include <asm/uaccess.h>
  31. #include <asm/mmu_context.h>
  32. #include <asm/pgtable.h>
  33. #include <asm/tlbflush.h>
  34. #include <asm/io.h>
  35. #include "power.h"
  36. static int swsusp_page_is_free(struct page *);
  37. static void swsusp_set_page_forbidden(struct page *);
  38. static void swsusp_unset_page_forbidden(struct page *);
  39. /*
  40. * Number of bytes to reserve for memory allocations made by device drivers
  41. * from their ->freeze() and ->freeze_noirq() callbacks so that they don't
  42. * cause image creation to fail (tunable via /sys/power/reserved_size).
  43. */
  44. unsigned long reserved_size;
  45. void __init hibernate_reserved_size_init(void)
  46. {
  47. reserved_size = SPARE_PAGES * PAGE_SIZE;
  48. }
  49. /*
  50. * Preferred image size in bytes (tunable via /sys/power/image_size).
  51. * When it is set to N, swsusp will do its best to ensure the image
  52. * size will not exceed N bytes, but if that is impossible, it will
  53. * try to create the smallest image possible.
  54. */
  55. unsigned long image_size;
  56. void __init hibernate_image_size_init(void)
  57. {
  58. image_size = ((totalram_pages * 2) / 5) * PAGE_SIZE;
  59. }
  60. /* List of PBEs needed for restoring the pages that were allocated before
  61. * the suspend and included in the suspend image, but have also been
  62. * allocated by the "resume" kernel, so their contents cannot be written
  63. * directly to their "original" page frames.
  64. */
  65. struct pbe *restore_pblist;
  66. /* Pointer to an auxiliary buffer (1 page) */
  67. static void *buffer;
  68. /**
  69. * @safe_needed - on resume, for storing the PBE list and the image,
  70. * we can only use memory pages that do not conflict with the pages
  71. * used before suspend. The unsafe pages have PageNosaveFree set
  72. * and we count them using unsafe_pages.
  73. *
  74. * Each allocated image page is marked as PageNosave and PageNosaveFree
  75. * so that swsusp_free() can release it.
  76. */
  77. #define PG_ANY 0
  78. #define PG_SAFE 1
  79. #define PG_UNSAFE_CLEAR 1
  80. #define PG_UNSAFE_KEEP 0
  81. static unsigned int allocated_unsafe_pages;
  82. static void *get_image_page(gfp_t gfp_mask, int safe_needed)
  83. {
  84. void *res;
  85. res = (void *)get_zeroed_page(gfp_mask);
  86. if (safe_needed)
  87. while (res && swsusp_page_is_free(virt_to_page(res))) {
  88. /* The page is unsafe, mark it for swsusp_free() */
  89. swsusp_set_page_forbidden(virt_to_page(res));
  90. allocated_unsafe_pages++;
  91. res = (void *)get_zeroed_page(gfp_mask);
  92. }
  93. if (res) {
  94. swsusp_set_page_forbidden(virt_to_page(res));
  95. swsusp_set_page_free(virt_to_page(res));
  96. }
  97. return res;
  98. }
  99. unsigned long get_safe_page(gfp_t gfp_mask)
  100. {
  101. return (unsigned long)get_image_page(gfp_mask, PG_SAFE);
  102. }
  103. static struct page *alloc_image_page(gfp_t gfp_mask)
  104. {
  105. struct page *page;
  106. page = alloc_page(gfp_mask);
  107. if (page) {
  108. swsusp_set_page_forbidden(page);
  109. swsusp_set_page_free(page);
  110. }
  111. return page;
  112. }
  113. /**
  114. * free_image_page - free page represented by @addr, allocated with
  115. * get_image_page (page flags set by it must be cleared)
  116. */
  117. static inline void free_image_page(void *addr, int clear_nosave_free)
  118. {
  119. struct page *page;
  120. BUG_ON(!virt_addr_valid(addr));
  121. page = virt_to_page(addr);
  122. swsusp_unset_page_forbidden(page);
  123. if (clear_nosave_free)
  124. swsusp_unset_page_free(page);
  125. __free_page(page);
  126. }
  127. /* struct linked_page is used to build chains of pages */
  128. #define LINKED_PAGE_DATA_SIZE (PAGE_SIZE - sizeof(void *))
  129. struct linked_page {
  130. struct linked_page *next;
  131. char data[LINKED_PAGE_DATA_SIZE];
  132. } __packed;
  133. static inline void
  134. free_list_of_pages(struct linked_page *list, int clear_page_nosave)
  135. {
  136. while (list) {
  137. struct linked_page *lp = list->next;
  138. free_image_page(list, clear_page_nosave);
  139. list = lp;
  140. }
  141. }
  142. /**
  143. * struct chain_allocator is used for allocating small objects out of
  144. * a linked list of pages called 'the chain'.
  145. *
  146. * The chain grows each time when there is no room for a new object in
  147. * the current page. The allocated objects cannot be freed individually.
  148. * It is only possible to free them all at once, by freeing the entire
  149. * chain.
  150. *
  151. * NOTE: The chain allocator may be inefficient if the allocated objects
  152. * are not much smaller than PAGE_SIZE.
  153. */
  154. struct chain_allocator {
  155. struct linked_page *chain; /* the chain */
  156. unsigned int used_space; /* total size of objects allocated out
  157. * of the current page
  158. */
  159. gfp_t gfp_mask; /* mask for allocating pages */
  160. int safe_needed; /* if set, only "safe" pages are allocated */
  161. };
  162. static void
  163. chain_init(struct chain_allocator *ca, gfp_t gfp_mask, int safe_needed)
  164. {
  165. ca->chain = NULL;
  166. ca->used_space = LINKED_PAGE_DATA_SIZE;
  167. ca->gfp_mask = gfp_mask;
  168. ca->safe_needed = safe_needed;
  169. }
  170. static void *chain_alloc(struct chain_allocator *ca, unsigned int size)
  171. {
  172. void *ret;
  173. if (LINKED_PAGE_DATA_SIZE - ca->used_space < size) {
  174. struct linked_page *lp;
  175. lp = get_image_page(ca->gfp_mask, ca->safe_needed);
  176. if (!lp)
  177. return NULL;
  178. lp->next = ca->chain;
  179. ca->chain = lp;
  180. ca->used_space = 0;
  181. }
  182. ret = ca->chain->data + ca->used_space;
  183. ca->used_space += size;
  184. return ret;
  185. }
  186. /**
  187. * Data types related to memory bitmaps.
  188. *
  189. * Memory bitmap is a structure consiting of many linked lists of
  190. * objects. The main list's elements are of type struct zone_bitmap
  191. * and each of them corresonds to one zone. For each zone bitmap
  192. * object there is a list of objects of type struct bm_block that
  193. * represent each blocks of bitmap in which information is stored.
  194. *
  195. * struct memory_bitmap contains a pointer to the main list of zone
  196. * bitmap objects, a struct bm_position used for browsing the bitmap,
  197. * and a pointer to the list of pages used for allocating all of the
  198. * zone bitmap objects and bitmap block objects.
  199. *
  200. * NOTE: It has to be possible to lay out the bitmap in memory
  201. * using only allocations of order 0. Additionally, the bitmap is
  202. * designed to work with arbitrary number of zones (this is over the
  203. * top for now, but let's avoid making unnecessary assumptions ;-).
  204. *
  205. * struct zone_bitmap contains a pointer to a list of bitmap block
  206. * objects and a pointer to the bitmap block object that has been
  207. * most recently used for setting bits. Additionally, it contains the
  208. * pfns that correspond to the start and end of the represented zone.
  209. *
  210. * struct bm_block contains a pointer to the memory page in which
  211. * information is stored (in the form of a block of bitmap)
  212. * It also contains the pfns that correspond to the start and end of
  213. * the represented memory area.
  214. *
  215. * The memory bitmap is organized as a radix tree to guarantee fast random
  216. * access to the bits. There is one radix tree for each zone (as returned
  217. * from create_mem_extents).
  218. *
  219. * One radix tree is represented by one struct mem_zone_bm_rtree. There are
  220. * two linked lists for the nodes of the tree, one for the inner nodes and
  221. * one for the leave nodes. The linked leave nodes are used for fast linear
  222. * access of the memory bitmap.
  223. *
  224. * The struct rtree_node represents one node of the radix tree.
  225. */
  226. #define BM_END_OF_MAP (~0UL)
  227. #define BM_BITS_PER_BLOCK (PAGE_SIZE * BITS_PER_BYTE)
  228. #define BM_BLOCK_SHIFT (PAGE_SHIFT + 3)
  229. #define BM_BLOCK_MASK ((1UL << BM_BLOCK_SHIFT) - 1)
  230. /*
  231. * struct rtree_node is a wrapper struct to link the nodes
  232. * of the rtree together for easy linear iteration over
  233. * bits and easy freeing
  234. */
  235. struct rtree_node {
  236. struct list_head list;
  237. unsigned long *data;
  238. };
  239. /*
  240. * struct mem_zone_bm_rtree represents a bitmap used for one
  241. * populated memory zone.
  242. */
  243. struct mem_zone_bm_rtree {
  244. struct list_head list; /* Link Zones together */
  245. struct list_head nodes; /* Radix Tree inner nodes */
  246. struct list_head leaves; /* Radix Tree leaves */
  247. unsigned long start_pfn; /* Zone start page frame */
  248. unsigned long end_pfn; /* Zone end page frame + 1 */
  249. struct rtree_node *rtree; /* Radix Tree Root */
  250. int levels; /* Number of Radix Tree Levels */
  251. unsigned int blocks; /* Number of Bitmap Blocks */
  252. };
  253. /* strcut bm_position is used for browsing memory bitmaps */
  254. struct bm_position {
  255. struct mem_zone_bm_rtree *zone;
  256. struct rtree_node *node;
  257. unsigned long node_pfn;
  258. int node_bit;
  259. };
  260. struct memory_bitmap {
  261. struct list_head zones;
  262. struct linked_page *p_list; /* list of pages used to store zone
  263. * bitmap objects and bitmap block
  264. * objects
  265. */
  266. struct bm_position cur; /* most recently used bit position */
  267. };
  268. /* Functions that operate on memory bitmaps */
  269. #define BM_ENTRIES_PER_LEVEL (PAGE_SIZE / sizeof(unsigned long))
  270. #if BITS_PER_LONG == 32
  271. #define BM_RTREE_LEVEL_SHIFT (PAGE_SHIFT - 2)
  272. #else
  273. #define BM_RTREE_LEVEL_SHIFT (PAGE_SHIFT - 3)
  274. #endif
  275. #define BM_RTREE_LEVEL_MASK ((1UL << BM_RTREE_LEVEL_SHIFT) - 1)
  276. /*
  277. * alloc_rtree_node - Allocate a new node and add it to the radix tree.
  278. *
  279. * This function is used to allocate inner nodes as well as the
  280. * leave nodes of the radix tree. It also adds the node to the
  281. * corresponding linked list passed in by the *list parameter.
  282. */
  283. static struct rtree_node *alloc_rtree_node(gfp_t gfp_mask, int safe_needed,
  284. struct chain_allocator *ca,
  285. struct list_head *list)
  286. {
  287. struct rtree_node *node;
  288. node = chain_alloc(ca, sizeof(struct rtree_node));
  289. if (!node)
  290. return NULL;
  291. node->data = get_image_page(gfp_mask, safe_needed);
  292. if (!node->data)
  293. return NULL;
  294. list_add_tail(&node->list, list);
  295. return node;
  296. }
  297. /*
  298. * add_rtree_block - Add a new leave node to the radix tree
  299. *
  300. * The leave nodes need to be allocated in order to keep the leaves
  301. * linked list in order. This is guaranteed by the zone->blocks
  302. * counter.
  303. */
  304. static int add_rtree_block(struct mem_zone_bm_rtree *zone, gfp_t gfp_mask,
  305. int safe_needed, struct chain_allocator *ca)
  306. {
  307. struct rtree_node *node, *block, **dst;
  308. unsigned int levels_needed, block_nr;
  309. int i;
  310. block_nr = zone->blocks;
  311. levels_needed = 0;
  312. /* How many levels do we need for this block nr? */
  313. while (block_nr) {
  314. levels_needed += 1;
  315. block_nr >>= BM_RTREE_LEVEL_SHIFT;
  316. }
  317. /* Make sure the rtree has enough levels */
  318. for (i = zone->levels; i < levels_needed; i++) {
  319. node = alloc_rtree_node(gfp_mask, safe_needed, ca,
  320. &zone->nodes);
  321. if (!node)
  322. return -ENOMEM;
  323. node->data[0] = (unsigned long)zone->rtree;
  324. zone->rtree = node;
  325. zone->levels += 1;
  326. }
  327. /* Allocate new block */
  328. block = alloc_rtree_node(gfp_mask, safe_needed, ca, &zone->leaves);
  329. if (!block)
  330. return -ENOMEM;
  331. /* Now walk the rtree to insert the block */
  332. node = zone->rtree;
  333. dst = &zone->rtree;
  334. block_nr = zone->blocks;
  335. for (i = zone->levels; i > 0; i--) {
  336. int index;
  337. if (!node) {
  338. node = alloc_rtree_node(gfp_mask, safe_needed, ca,
  339. &zone->nodes);
  340. if (!node)
  341. return -ENOMEM;
  342. *dst = node;
  343. }
  344. index = block_nr >> ((i - 1) * BM_RTREE_LEVEL_SHIFT);
  345. index &= BM_RTREE_LEVEL_MASK;
  346. dst = (struct rtree_node **)&((*dst)->data[index]);
  347. node = *dst;
  348. }
  349. zone->blocks += 1;
  350. *dst = block;
  351. return 0;
  352. }
  353. static void free_zone_bm_rtree(struct mem_zone_bm_rtree *zone,
  354. int clear_nosave_free);
  355. /*
  356. * create_zone_bm_rtree - create a radix tree for one zone
  357. *
  358. * Allocated the mem_zone_bm_rtree structure and initializes it.
  359. * This function also allocated and builds the radix tree for the
  360. * zone.
  361. */
  362. static struct mem_zone_bm_rtree *
  363. create_zone_bm_rtree(gfp_t gfp_mask, int safe_needed,
  364. struct chain_allocator *ca,
  365. unsigned long start, unsigned long end)
  366. {
  367. struct mem_zone_bm_rtree *zone;
  368. unsigned int i, nr_blocks;
  369. unsigned long pages;
  370. pages = end - start;
  371. zone = chain_alloc(ca, sizeof(struct mem_zone_bm_rtree));
  372. if (!zone)
  373. return NULL;
  374. INIT_LIST_HEAD(&zone->nodes);
  375. INIT_LIST_HEAD(&zone->leaves);
  376. zone->start_pfn = start;
  377. zone->end_pfn = end;
  378. nr_blocks = DIV_ROUND_UP(pages, BM_BITS_PER_BLOCK);
  379. for (i = 0; i < nr_blocks; i++) {
  380. if (add_rtree_block(zone, gfp_mask, safe_needed, ca)) {
  381. free_zone_bm_rtree(zone, PG_UNSAFE_CLEAR);
  382. return NULL;
  383. }
  384. }
  385. return zone;
  386. }
  387. /*
  388. * free_zone_bm_rtree - Free the memory of the radix tree
  389. *
  390. * Free all node pages of the radix tree. The mem_zone_bm_rtree
  391. * structure itself is not freed here nor are the rtree_node
  392. * structs.
  393. */
  394. static void free_zone_bm_rtree(struct mem_zone_bm_rtree *zone,
  395. int clear_nosave_free)
  396. {
  397. struct rtree_node *node;
  398. list_for_each_entry(node, &zone->nodes, list)
  399. free_image_page(node->data, clear_nosave_free);
  400. list_for_each_entry(node, &zone->leaves, list)
  401. free_image_page(node->data, clear_nosave_free);
  402. }
  403. static void memory_bm_position_reset(struct memory_bitmap *bm)
  404. {
  405. bm->cur.zone = list_entry(bm->zones.next, struct mem_zone_bm_rtree,
  406. list);
  407. bm->cur.node = list_entry(bm->cur.zone->leaves.next,
  408. struct rtree_node, list);
  409. bm->cur.node_pfn = 0;
  410. bm->cur.node_bit = 0;
  411. }
  412. static void memory_bm_free(struct memory_bitmap *bm, int clear_nosave_free);
  413. struct mem_extent {
  414. struct list_head hook;
  415. unsigned long start;
  416. unsigned long end;
  417. };
  418. /**
  419. * free_mem_extents - free a list of memory extents
  420. * @list - list of extents to empty
  421. */
  422. static void free_mem_extents(struct list_head *list)
  423. {
  424. struct mem_extent *ext, *aux;
  425. list_for_each_entry_safe(ext, aux, list, hook) {
  426. list_del(&ext->hook);
  427. kfree(ext);
  428. }
  429. }
  430. /**
  431. * create_mem_extents - create a list of memory extents representing
  432. * contiguous ranges of PFNs
  433. * @list - list to put the extents into
  434. * @gfp_mask - mask to use for memory allocations
  435. */
  436. static int create_mem_extents(struct list_head *list, gfp_t gfp_mask)
  437. {
  438. struct zone *zone;
  439. INIT_LIST_HEAD(list);
  440. for_each_populated_zone(zone) {
  441. unsigned long zone_start, zone_end;
  442. struct mem_extent *ext, *cur, *aux;
  443. zone_start = zone->zone_start_pfn;
  444. zone_end = zone_end_pfn(zone);
  445. list_for_each_entry(ext, list, hook)
  446. if (zone_start <= ext->end)
  447. break;
  448. if (&ext->hook == list || zone_end < ext->start) {
  449. /* New extent is necessary */
  450. struct mem_extent *new_ext;
  451. new_ext = kzalloc(sizeof(struct mem_extent), gfp_mask);
  452. if (!new_ext) {
  453. free_mem_extents(list);
  454. return -ENOMEM;
  455. }
  456. new_ext->start = zone_start;
  457. new_ext->end = zone_end;
  458. list_add_tail(&new_ext->hook, &ext->hook);
  459. continue;
  460. }
  461. /* Merge this zone's range of PFNs with the existing one */
  462. if (zone_start < ext->start)
  463. ext->start = zone_start;
  464. if (zone_end > ext->end)
  465. ext->end = zone_end;
  466. /* More merging may be possible */
  467. cur = ext;
  468. list_for_each_entry_safe_continue(cur, aux, list, hook) {
  469. if (zone_end < cur->start)
  470. break;
  471. if (zone_end < cur->end)
  472. ext->end = cur->end;
  473. list_del(&cur->hook);
  474. kfree(cur);
  475. }
  476. }
  477. return 0;
  478. }
  479. /**
  480. * memory_bm_create - allocate memory for a memory bitmap
  481. */
  482. static int
  483. memory_bm_create(struct memory_bitmap *bm, gfp_t gfp_mask, int safe_needed)
  484. {
  485. struct chain_allocator ca;
  486. struct list_head mem_extents;
  487. struct mem_extent *ext;
  488. int error;
  489. chain_init(&ca, gfp_mask, safe_needed);
  490. INIT_LIST_HEAD(&bm->zones);
  491. error = create_mem_extents(&mem_extents, gfp_mask);
  492. if (error)
  493. return error;
  494. list_for_each_entry(ext, &mem_extents, hook) {
  495. struct mem_zone_bm_rtree *zone;
  496. zone = create_zone_bm_rtree(gfp_mask, safe_needed, &ca,
  497. ext->start, ext->end);
  498. if (!zone) {
  499. error = -ENOMEM;
  500. goto Error;
  501. }
  502. list_add_tail(&zone->list, &bm->zones);
  503. }
  504. bm->p_list = ca.chain;
  505. memory_bm_position_reset(bm);
  506. Exit:
  507. free_mem_extents(&mem_extents);
  508. return error;
  509. Error:
  510. bm->p_list = ca.chain;
  511. memory_bm_free(bm, PG_UNSAFE_CLEAR);
  512. goto Exit;
  513. }
  514. /**
  515. * memory_bm_free - free memory occupied by the memory bitmap @bm
  516. */
  517. static void memory_bm_free(struct memory_bitmap *bm, int clear_nosave_free)
  518. {
  519. struct mem_zone_bm_rtree *zone;
  520. list_for_each_entry(zone, &bm->zones, list)
  521. free_zone_bm_rtree(zone, clear_nosave_free);
  522. free_list_of_pages(bm->p_list, clear_nosave_free);
  523. INIT_LIST_HEAD(&bm->zones);
  524. }
  525. /**
  526. * memory_bm_find_bit - Find the bit for pfn in the memory
  527. * bitmap
  528. *
  529. * Find the bit in the bitmap @bm that corresponds to given pfn.
  530. * The cur.zone, cur.block and cur.node_pfn member of @bm are
  531. * updated.
  532. * It walks the radix tree to find the page which contains the bit for
  533. * pfn and returns the bit position in **addr and *bit_nr.
  534. */
  535. static int memory_bm_find_bit(struct memory_bitmap *bm, unsigned long pfn,
  536. void **addr, unsigned int *bit_nr)
  537. {
  538. struct mem_zone_bm_rtree *curr, *zone;
  539. struct rtree_node *node;
  540. int i, block_nr;
  541. zone = bm->cur.zone;
  542. if (pfn >= zone->start_pfn && pfn < zone->end_pfn)
  543. goto zone_found;
  544. zone = NULL;
  545. /* Find the right zone */
  546. list_for_each_entry(curr, &bm->zones, list) {
  547. if (pfn >= curr->start_pfn && pfn < curr->end_pfn) {
  548. zone = curr;
  549. break;
  550. }
  551. }
  552. if (!zone)
  553. return -EFAULT;
  554. zone_found:
  555. /*
  556. * We have a zone. Now walk the radix tree to find the leave
  557. * node for our pfn.
  558. */
  559. node = bm->cur.node;
  560. if (((pfn - zone->start_pfn) & ~BM_BLOCK_MASK) == bm->cur.node_pfn)
  561. goto node_found;
  562. node = zone->rtree;
  563. block_nr = (pfn - zone->start_pfn) >> BM_BLOCK_SHIFT;
  564. for (i = zone->levels; i > 0; i--) {
  565. int index;
  566. index = block_nr >> ((i - 1) * BM_RTREE_LEVEL_SHIFT);
  567. index &= BM_RTREE_LEVEL_MASK;
  568. BUG_ON(node->data[index] == 0);
  569. node = (struct rtree_node *)node->data[index];
  570. }
  571. node_found:
  572. /* Update last position */
  573. bm->cur.zone = zone;
  574. bm->cur.node = node;
  575. bm->cur.node_pfn = (pfn - zone->start_pfn) & ~BM_BLOCK_MASK;
  576. /* Set return values */
  577. *addr = node->data;
  578. *bit_nr = (pfn - zone->start_pfn) & BM_BLOCK_MASK;
  579. return 0;
  580. }
  581. static void memory_bm_set_bit(struct memory_bitmap *bm, unsigned long pfn)
  582. {
  583. void *addr;
  584. unsigned int bit;
  585. int error;
  586. error = memory_bm_find_bit(bm, pfn, &addr, &bit);
  587. BUG_ON(error);
  588. set_bit(bit, addr);
  589. }
  590. static int mem_bm_set_bit_check(struct memory_bitmap *bm, unsigned long pfn)
  591. {
  592. void *addr;
  593. unsigned int bit;
  594. int error;
  595. error = memory_bm_find_bit(bm, pfn, &addr, &bit);
  596. if (!error)
  597. set_bit(bit, addr);
  598. return error;
  599. }
  600. static void memory_bm_clear_bit(struct memory_bitmap *bm, unsigned long pfn)
  601. {
  602. void *addr;
  603. unsigned int bit;
  604. int error;
  605. error = memory_bm_find_bit(bm, pfn, &addr, &bit);
  606. BUG_ON(error);
  607. clear_bit(bit, addr);
  608. }
  609. static void memory_bm_clear_current(struct memory_bitmap *bm)
  610. {
  611. int bit;
  612. bit = max(bm->cur.node_bit - 1, 0);
  613. clear_bit(bit, bm->cur.node->data);
  614. }
  615. static int memory_bm_test_bit(struct memory_bitmap *bm, unsigned long pfn)
  616. {
  617. void *addr;
  618. unsigned int bit;
  619. int error;
  620. error = memory_bm_find_bit(bm, pfn, &addr, &bit);
  621. BUG_ON(error);
  622. return test_bit(bit, addr);
  623. }
  624. static bool memory_bm_pfn_present(struct memory_bitmap *bm, unsigned long pfn)
  625. {
  626. void *addr;
  627. unsigned int bit;
  628. return !memory_bm_find_bit(bm, pfn, &addr, &bit);
  629. }
  630. /*
  631. * rtree_next_node - Jumps to the next leave node
  632. *
  633. * Sets the position to the beginning of the next node in the
  634. * memory bitmap. This is either the next node in the current
  635. * zone's radix tree or the first node in the radix tree of the
  636. * next zone.
  637. *
  638. * Returns true if there is a next node, false otherwise.
  639. */
  640. static bool rtree_next_node(struct memory_bitmap *bm)
  641. {
  642. bm->cur.node = list_entry(bm->cur.node->list.next,
  643. struct rtree_node, list);
  644. if (&bm->cur.node->list != &bm->cur.zone->leaves) {
  645. bm->cur.node_pfn += BM_BITS_PER_BLOCK;
  646. bm->cur.node_bit = 0;
  647. touch_softlockup_watchdog();
  648. return true;
  649. }
  650. /* No more nodes, goto next zone */
  651. bm->cur.zone = list_entry(bm->cur.zone->list.next,
  652. struct mem_zone_bm_rtree, list);
  653. if (&bm->cur.zone->list != &bm->zones) {
  654. bm->cur.node = list_entry(bm->cur.zone->leaves.next,
  655. struct rtree_node, list);
  656. bm->cur.node_pfn = 0;
  657. bm->cur.node_bit = 0;
  658. return true;
  659. }
  660. /* No more zones */
  661. return false;
  662. }
  663. /**
  664. * memory_bm_rtree_next_pfn - Find the next set bit in the bitmap @bm
  665. *
  666. * Starting from the last returned position this function searches
  667. * for the next set bit in the memory bitmap and returns its
  668. * number. If no more bit is set BM_END_OF_MAP is returned.
  669. *
  670. * It is required to run memory_bm_position_reset() before the
  671. * first call to this function.
  672. */
  673. static unsigned long memory_bm_next_pfn(struct memory_bitmap *bm)
  674. {
  675. unsigned long bits, pfn, pages;
  676. int bit;
  677. do {
  678. pages = bm->cur.zone->end_pfn - bm->cur.zone->start_pfn;
  679. bits = min(pages - bm->cur.node_pfn, BM_BITS_PER_BLOCK);
  680. bit = find_next_bit(bm->cur.node->data, bits,
  681. bm->cur.node_bit);
  682. if (bit < bits) {
  683. pfn = bm->cur.zone->start_pfn + bm->cur.node_pfn + bit;
  684. bm->cur.node_bit = bit + 1;
  685. return pfn;
  686. }
  687. } while (rtree_next_node(bm));
  688. return BM_END_OF_MAP;
  689. }
  690. /**
  691. * This structure represents a range of page frames the contents of which
  692. * should not be saved during the suspend.
  693. */
  694. struct nosave_region {
  695. struct list_head list;
  696. unsigned long start_pfn;
  697. unsigned long end_pfn;
  698. };
  699. static LIST_HEAD(nosave_regions);
  700. /**
  701. * register_nosave_region - register a range of page frames the contents
  702. * of which should not be saved during the suspend (to be used in the early
  703. * initialization code)
  704. */
  705. void __init
  706. __register_nosave_region(unsigned long start_pfn, unsigned long end_pfn,
  707. int use_kmalloc)
  708. {
  709. struct nosave_region *region;
  710. if (start_pfn >= end_pfn)
  711. return;
  712. if (!list_empty(&nosave_regions)) {
  713. /* Try to extend the previous region (they should be sorted) */
  714. region = list_entry(nosave_regions.prev,
  715. struct nosave_region, list);
  716. if (region->end_pfn == start_pfn) {
  717. region->end_pfn = end_pfn;
  718. goto Report;
  719. }
  720. }
  721. if (use_kmalloc) {
  722. /* during init, this shouldn't fail */
  723. region = kmalloc(sizeof(struct nosave_region), GFP_KERNEL);
  724. BUG_ON(!region);
  725. } else
  726. /* This allocation cannot fail */
  727. region = memblock_virt_alloc(sizeof(struct nosave_region), 0);
  728. region->start_pfn = start_pfn;
  729. region->end_pfn = end_pfn;
  730. list_add_tail(&region->list, &nosave_regions);
  731. Report:
  732. printk(KERN_INFO "PM: Registered nosave memory: [mem %#010llx-%#010llx]\n",
  733. (unsigned long long) start_pfn << PAGE_SHIFT,
  734. ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
  735. }
  736. /*
  737. * Set bits in this map correspond to the page frames the contents of which
  738. * should not be saved during the suspend.
  739. */
  740. static struct memory_bitmap *forbidden_pages_map;
  741. /* Set bits in this map correspond to free page frames. */
  742. static struct memory_bitmap *free_pages_map;
  743. /*
  744. * Each page frame allocated for creating the image is marked by setting the
  745. * corresponding bits in forbidden_pages_map and free_pages_map simultaneously
  746. */
  747. void swsusp_set_page_free(struct page *page)
  748. {
  749. if (free_pages_map)
  750. memory_bm_set_bit(free_pages_map, page_to_pfn(page));
  751. }
  752. static int swsusp_page_is_free(struct page *page)
  753. {
  754. return free_pages_map ?
  755. memory_bm_test_bit(free_pages_map, page_to_pfn(page)) : 0;
  756. }
  757. void swsusp_unset_page_free(struct page *page)
  758. {
  759. if (free_pages_map)
  760. memory_bm_clear_bit(free_pages_map, page_to_pfn(page));
  761. }
  762. static void swsusp_set_page_forbidden(struct page *page)
  763. {
  764. if (forbidden_pages_map)
  765. memory_bm_set_bit(forbidden_pages_map, page_to_pfn(page));
  766. }
  767. int swsusp_page_is_forbidden(struct page *page)
  768. {
  769. return forbidden_pages_map ?
  770. memory_bm_test_bit(forbidden_pages_map, page_to_pfn(page)) : 0;
  771. }
  772. static void swsusp_unset_page_forbidden(struct page *page)
  773. {
  774. if (forbidden_pages_map)
  775. memory_bm_clear_bit(forbidden_pages_map, page_to_pfn(page));
  776. }
  777. /**
  778. * mark_nosave_pages - set bits corresponding to the page frames the
  779. * contents of which should not be saved in a given bitmap.
  780. */
  781. static void mark_nosave_pages(struct memory_bitmap *bm)
  782. {
  783. struct nosave_region *region;
  784. if (list_empty(&nosave_regions))
  785. return;
  786. list_for_each_entry(region, &nosave_regions, list) {
  787. unsigned long pfn;
  788. pr_debug("PM: Marking nosave pages: [mem %#010llx-%#010llx]\n",
  789. (unsigned long long) region->start_pfn << PAGE_SHIFT,
  790. ((unsigned long long) region->end_pfn << PAGE_SHIFT)
  791. - 1);
  792. for (pfn = region->start_pfn; pfn < region->end_pfn; pfn++)
  793. if (pfn_valid(pfn)) {
  794. /*
  795. * It is safe to ignore the result of
  796. * mem_bm_set_bit_check() here, since we won't
  797. * touch the PFNs for which the error is
  798. * returned anyway.
  799. */
  800. mem_bm_set_bit_check(bm, pfn);
  801. }
  802. }
  803. }
  804. static bool is_nosave_page(unsigned long pfn)
  805. {
  806. struct nosave_region *region;
  807. list_for_each_entry(region, &nosave_regions, list) {
  808. if (pfn >= region->start_pfn && pfn < region->end_pfn) {
  809. pr_err("PM: %#010llx in e820 nosave region: "
  810. "[mem %#010llx-%#010llx]\n",
  811. (unsigned long long) pfn << PAGE_SHIFT,
  812. (unsigned long long) region->start_pfn << PAGE_SHIFT,
  813. ((unsigned long long) region->end_pfn << PAGE_SHIFT)
  814. - 1);
  815. return true;
  816. }
  817. }
  818. return false;
  819. }
  820. /**
  821. * create_basic_memory_bitmaps - create bitmaps needed for marking page
  822. * frames that should not be saved and free page frames. The pointers
  823. * forbidden_pages_map and free_pages_map are only modified if everything
  824. * goes well, because we don't want the bits to be used before both bitmaps
  825. * are set up.
  826. */
  827. int create_basic_memory_bitmaps(void)
  828. {
  829. struct memory_bitmap *bm1, *bm2;
  830. int error = 0;
  831. if (forbidden_pages_map && free_pages_map)
  832. return 0;
  833. else
  834. BUG_ON(forbidden_pages_map || free_pages_map);
  835. bm1 = kzalloc(sizeof(struct memory_bitmap), GFP_KERNEL);
  836. if (!bm1)
  837. return -ENOMEM;
  838. error = memory_bm_create(bm1, GFP_KERNEL, PG_ANY);
  839. if (error)
  840. goto Free_first_object;
  841. bm2 = kzalloc(sizeof(struct memory_bitmap), GFP_KERNEL);
  842. if (!bm2)
  843. goto Free_first_bitmap;
  844. error = memory_bm_create(bm2, GFP_KERNEL, PG_ANY);
  845. if (error)
  846. goto Free_second_object;
  847. forbidden_pages_map = bm1;
  848. free_pages_map = bm2;
  849. mark_nosave_pages(forbidden_pages_map);
  850. pr_debug("PM: Basic memory bitmaps created\n");
  851. return 0;
  852. Free_second_object:
  853. kfree(bm2);
  854. Free_first_bitmap:
  855. memory_bm_free(bm1, PG_UNSAFE_CLEAR);
  856. Free_first_object:
  857. kfree(bm1);
  858. return -ENOMEM;
  859. }
  860. /**
  861. * free_basic_memory_bitmaps - free memory bitmaps allocated by
  862. * create_basic_memory_bitmaps(). The auxiliary pointers are necessary
  863. * so that the bitmaps themselves are not referred to while they are being
  864. * freed.
  865. */
  866. void free_basic_memory_bitmaps(void)
  867. {
  868. struct memory_bitmap *bm1, *bm2;
  869. if (WARN_ON(!(forbidden_pages_map && free_pages_map)))
  870. return;
  871. bm1 = forbidden_pages_map;
  872. bm2 = free_pages_map;
  873. forbidden_pages_map = NULL;
  874. free_pages_map = NULL;
  875. memory_bm_free(bm1, PG_UNSAFE_CLEAR);
  876. kfree(bm1);
  877. memory_bm_free(bm2, PG_UNSAFE_CLEAR);
  878. kfree(bm2);
  879. pr_debug("PM: Basic memory bitmaps freed\n");
  880. }
  881. /**
  882. * snapshot_additional_pages - estimate the number of additional pages
  883. * be needed for setting up the suspend image data structures for given
  884. * zone (usually the returned value is greater than the exact number)
  885. */
  886. unsigned int snapshot_additional_pages(struct zone *zone)
  887. {
  888. unsigned int rtree, nodes;
  889. rtree = nodes = DIV_ROUND_UP(zone->spanned_pages, BM_BITS_PER_BLOCK);
  890. rtree += DIV_ROUND_UP(rtree * sizeof(struct rtree_node),
  891. LINKED_PAGE_DATA_SIZE);
  892. while (nodes > 1) {
  893. nodes = DIV_ROUND_UP(nodes, BM_ENTRIES_PER_LEVEL);
  894. rtree += nodes;
  895. }
  896. return 2 * rtree;
  897. }
  898. #ifdef CONFIG_HIGHMEM
  899. /**
  900. * count_free_highmem_pages - compute the total number of free highmem
  901. * pages, system-wide.
  902. */
  903. static unsigned int count_free_highmem_pages(void)
  904. {
  905. struct zone *zone;
  906. unsigned int cnt = 0;
  907. for_each_populated_zone(zone)
  908. if (is_highmem(zone))
  909. cnt += zone_page_state(zone, NR_FREE_PAGES);
  910. return cnt;
  911. }
  912. /**
  913. * saveable_highmem_page - Determine whether a highmem page should be
  914. * included in the suspend image.
  915. *
  916. * We should save the page if it isn't Nosave or NosaveFree, or Reserved,
  917. * and it isn't a part of a free chunk of pages.
  918. */
  919. static struct page *saveable_highmem_page(struct zone *zone, unsigned long pfn)
  920. {
  921. struct page *page;
  922. if (!pfn_valid(pfn))
  923. return NULL;
  924. page = pfn_to_page(pfn);
  925. if (page_zone(page) != zone)
  926. return NULL;
  927. BUG_ON(!PageHighMem(page));
  928. if (swsusp_page_is_forbidden(page) || swsusp_page_is_free(page) ||
  929. PageReserved(page))
  930. return NULL;
  931. if (page_is_guard(page))
  932. return NULL;
  933. return page;
  934. }
  935. /**
  936. * count_highmem_pages - compute the total number of saveable highmem
  937. * pages.
  938. */
  939. static unsigned int count_highmem_pages(void)
  940. {
  941. struct zone *zone;
  942. unsigned int n = 0;
  943. for_each_populated_zone(zone) {
  944. unsigned long pfn, max_zone_pfn;
  945. if (!is_highmem(zone))
  946. continue;
  947. mark_free_pages(zone);
  948. max_zone_pfn = zone_end_pfn(zone);
  949. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
  950. if (saveable_highmem_page(zone, pfn))
  951. n++;
  952. }
  953. return n;
  954. }
  955. #else
  956. static inline void *saveable_highmem_page(struct zone *z, unsigned long p)
  957. {
  958. return NULL;
  959. }
  960. #endif /* CONFIG_HIGHMEM */
  961. /**
  962. * saveable_page - Determine whether a non-highmem page should be included
  963. * in the suspend image.
  964. *
  965. * We should save the page if it isn't Nosave, and is not in the range
  966. * of pages statically defined as 'unsaveable', and it isn't a part of
  967. * a free chunk of pages.
  968. */
  969. static struct page *saveable_page(struct zone *zone, unsigned long pfn)
  970. {
  971. struct page *page;
  972. if (!pfn_valid(pfn))
  973. return NULL;
  974. page = pfn_to_page(pfn);
  975. if (page_zone(page) != zone)
  976. return NULL;
  977. BUG_ON(PageHighMem(page));
  978. if (swsusp_page_is_forbidden(page) || swsusp_page_is_free(page))
  979. return NULL;
  980. if (PageReserved(page)
  981. && (!kernel_page_present(page) || pfn_is_nosave(pfn)))
  982. return NULL;
  983. if (page_is_guard(page))
  984. return NULL;
  985. return page;
  986. }
  987. /**
  988. * count_data_pages - compute the total number of saveable non-highmem
  989. * pages.
  990. */
  991. static unsigned int count_data_pages(void)
  992. {
  993. struct zone *zone;
  994. unsigned long pfn, max_zone_pfn;
  995. unsigned int n = 0;
  996. for_each_populated_zone(zone) {
  997. if (is_highmem(zone))
  998. continue;
  999. mark_free_pages(zone);
  1000. max_zone_pfn = zone_end_pfn(zone);
  1001. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
  1002. if (saveable_page(zone, pfn))
  1003. n++;
  1004. }
  1005. return n;
  1006. }
  1007. /* This is needed, because copy_page and memcpy are not usable for copying
  1008. * task structs.
  1009. */
  1010. static inline void do_copy_page(long *dst, long *src)
  1011. {
  1012. int n;
  1013. for (n = PAGE_SIZE / sizeof(long); n; n--)
  1014. *dst++ = *src++;
  1015. }
  1016. /**
  1017. * safe_copy_page - check if the page we are going to copy is marked as
  1018. * present in the kernel page tables (this always is the case if
  1019. * CONFIG_DEBUG_PAGEALLOC is not set and in that case
  1020. * kernel_page_present() always returns 'true').
  1021. */
  1022. static void safe_copy_page(void *dst, struct page *s_page)
  1023. {
  1024. if (kernel_page_present(s_page)) {
  1025. do_copy_page(dst, page_address(s_page));
  1026. } else {
  1027. kernel_map_pages(s_page, 1, 1);
  1028. do_copy_page(dst, page_address(s_page));
  1029. kernel_map_pages(s_page, 1, 0);
  1030. }
  1031. }
  1032. #ifdef CONFIG_HIGHMEM
  1033. static inline struct page *
  1034. page_is_saveable(struct zone *zone, unsigned long pfn)
  1035. {
  1036. return is_highmem(zone) ?
  1037. saveable_highmem_page(zone, pfn) : saveable_page(zone, pfn);
  1038. }
  1039. static void copy_data_page(unsigned long dst_pfn, unsigned long src_pfn)
  1040. {
  1041. struct page *s_page, *d_page;
  1042. void *src, *dst;
  1043. s_page = pfn_to_page(src_pfn);
  1044. d_page = pfn_to_page(dst_pfn);
  1045. if (PageHighMem(s_page)) {
  1046. src = kmap_atomic(s_page);
  1047. dst = kmap_atomic(d_page);
  1048. do_copy_page(dst, src);
  1049. kunmap_atomic(dst);
  1050. kunmap_atomic(src);
  1051. } else {
  1052. if (PageHighMem(d_page)) {
  1053. /* Page pointed to by src may contain some kernel
  1054. * data modified by kmap_atomic()
  1055. */
  1056. safe_copy_page(buffer, s_page);
  1057. dst = kmap_atomic(d_page);
  1058. copy_page(dst, buffer);
  1059. kunmap_atomic(dst);
  1060. } else {
  1061. safe_copy_page(page_address(d_page), s_page);
  1062. }
  1063. }
  1064. }
  1065. #else
  1066. #define page_is_saveable(zone, pfn) saveable_page(zone, pfn)
  1067. static inline void copy_data_page(unsigned long dst_pfn, unsigned long src_pfn)
  1068. {
  1069. safe_copy_page(page_address(pfn_to_page(dst_pfn)),
  1070. pfn_to_page(src_pfn));
  1071. }
  1072. #endif /* CONFIG_HIGHMEM */
  1073. static void
  1074. copy_data_pages(struct memory_bitmap *copy_bm, struct memory_bitmap *orig_bm)
  1075. {
  1076. struct zone *zone;
  1077. unsigned long pfn;
  1078. for_each_populated_zone(zone) {
  1079. unsigned long max_zone_pfn;
  1080. mark_free_pages(zone);
  1081. max_zone_pfn = zone_end_pfn(zone);
  1082. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
  1083. if (page_is_saveable(zone, pfn))
  1084. memory_bm_set_bit(orig_bm, pfn);
  1085. }
  1086. memory_bm_position_reset(orig_bm);
  1087. memory_bm_position_reset(copy_bm);
  1088. for(;;) {
  1089. pfn = memory_bm_next_pfn(orig_bm);
  1090. if (unlikely(pfn == BM_END_OF_MAP))
  1091. break;
  1092. copy_data_page(memory_bm_next_pfn(copy_bm), pfn);
  1093. }
  1094. }
  1095. /* Total number of image pages */
  1096. static unsigned int nr_copy_pages;
  1097. /* Number of pages needed for saving the original pfns of the image pages */
  1098. static unsigned int nr_meta_pages;
  1099. /*
  1100. * Numbers of normal and highmem page frames allocated for hibernation image
  1101. * before suspending devices.
  1102. */
  1103. unsigned int alloc_normal, alloc_highmem;
  1104. /*
  1105. * Memory bitmap used for marking saveable pages (during hibernation) or
  1106. * hibernation image pages (during restore)
  1107. */
  1108. static struct memory_bitmap orig_bm;
  1109. /*
  1110. * Memory bitmap used during hibernation for marking allocated page frames that
  1111. * will contain copies of saveable pages. During restore it is initially used
  1112. * for marking hibernation image pages, but then the set bits from it are
  1113. * duplicated in @orig_bm and it is released. On highmem systems it is next
  1114. * used for marking "safe" highmem pages, but it has to be reinitialized for
  1115. * this purpose.
  1116. */
  1117. static struct memory_bitmap copy_bm;
  1118. /**
  1119. * swsusp_free - free pages allocated for the suspend.
  1120. *
  1121. * Suspend pages are alocated before the atomic copy is made, so we
  1122. * need to release them after the resume.
  1123. */
  1124. void swsusp_free(void)
  1125. {
  1126. unsigned long fb_pfn, fr_pfn;
  1127. memory_bm_position_reset(forbidden_pages_map);
  1128. memory_bm_position_reset(free_pages_map);
  1129. loop:
  1130. fr_pfn = memory_bm_next_pfn(free_pages_map);
  1131. fb_pfn = memory_bm_next_pfn(forbidden_pages_map);
  1132. /*
  1133. * Find the next bit set in both bitmaps. This is guaranteed to
  1134. * terminate when fb_pfn == fr_pfn == BM_END_OF_MAP.
  1135. */
  1136. do {
  1137. if (fb_pfn < fr_pfn)
  1138. fb_pfn = memory_bm_next_pfn(forbidden_pages_map);
  1139. if (fr_pfn < fb_pfn)
  1140. fr_pfn = memory_bm_next_pfn(free_pages_map);
  1141. } while (fb_pfn != fr_pfn);
  1142. if (fr_pfn != BM_END_OF_MAP && pfn_valid(fr_pfn)) {
  1143. struct page *page = pfn_to_page(fr_pfn);
  1144. memory_bm_clear_current(forbidden_pages_map);
  1145. memory_bm_clear_current(free_pages_map);
  1146. __free_page(page);
  1147. goto loop;
  1148. }
  1149. nr_copy_pages = 0;
  1150. nr_meta_pages = 0;
  1151. restore_pblist = NULL;
  1152. buffer = NULL;
  1153. alloc_normal = 0;
  1154. alloc_highmem = 0;
  1155. }
  1156. /* Helper functions used for the shrinking of memory. */
  1157. #define GFP_IMAGE (GFP_KERNEL | __GFP_NOWARN)
  1158. /**
  1159. * preallocate_image_pages - Allocate a number of pages for hibernation image
  1160. * @nr_pages: Number of page frames to allocate.
  1161. * @mask: GFP flags to use for the allocation.
  1162. *
  1163. * Return value: Number of page frames actually allocated
  1164. */
  1165. static unsigned long preallocate_image_pages(unsigned long nr_pages, gfp_t mask)
  1166. {
  1167. unsigned long nr_alloc = 0;
  1168. while (nr_pages > 0) {
  1169. struct page *page;
  1170. page = alloc_image_page(mask);
  1171. if (!page)
  1172. break;
  1173. memory_bm_set_bit(&copy_bm, page_to_pfn(page));
  1174. if (PageHighMem(page))
  1175. alloc_highmem++;
  1176. else
  1177. alloc_normal++;
  1178. nr_pages--;
  1179. nr_alloc++;
  1180. }
  1181. return nr_alloc;
  1182. }
  1183. static unsigned long preallocate_image_memory(unsigned long nr_pages,
  1184. unsigned long avail_normal)
  1185. {
  1186. unsigned long alloc;
  1187. if (avail_normal <= alloc_normal)
  1188. return 0;
  1189. alloc = avail_normal - alloc_normal;
  1190. if (nr_pages < alloc)
  1191. alloc = nr_pages;
  1192. return preallocate_image_pages(alloc, GFP_IMAGE);
  1193. }
  1194. #ifdef CONFIG_HIGHMEM
  1195. static unsigned long preallocate_image_highmem(unsigned long nr_pages)
  1196. {
  1197. return preallocate_image_pages(nr_pages, GFP_IMAGE | __GFP_HIGHMEM);
  1198. }
  1199. /**
  1200. * __fraction - Compute (an approximation of) x * (multiplier / base)
  1201. */
  1202. static unsigned long __fraction(u64 x, u64 multiplier, u64 base)
  1203. {
  1204. x *= multiplier;
  1205. do_div(x, base);
  1206. return (unsigned long)x;
  1207. }
  1208. static unsigned long preallocate_highmem_fraction(unsigned long nr_pages,
  1209. unsigned long highmem,
  1210. unsigned long total)
  1211. {
  1212. unsigned long alloc = __fraction(nr_pages, highmem, total);
  1213. return preallocate_image_pages(alloc, GFP_IMAGE | __GFP_HIGHMEM);
  1214. }
  1215. #else /* CONFIG_HIGHMEM */
  1216. static inline unsigned long preallocate_image_highmem(unsigned long nr_pages)
  1217. {
  1218. return 0;
  1219. }
  1220. static inline unsigned long preallocate_highmem_fraction(unsigned long nr_pages,
  1221. unsigned long highmem,
  1222. unsigned long total)
  1223. {
  1224. return 0;
  1225. }
  1226. #endif /* CONFIG_HIGHMEM */
  1227. /**
  1228. * free_unnecessary_pages - Release preallocated pages not needed for the image
  1229. */
  1230. static void free_unnecessary_pages(void)
  1231. {
  1232. unsigned long save, to_free_normal, to_free_highmem;
  1233. save = count_data_pages();
  1234. if (alloc_normal >= save) {
  1235. to_free_normal = alloc_normal - save;
  1236. save = 0;
  1237. } else {
  1238. to_free_normal = 0;
  1239. save -= alloc_normal;
  1240. }
  1241. save += count_highmem_pages();
  1242. if (alloc_highmem >= save) {
  1243. to_free_highmem = alloc_highmem - save;
  1244. } else {
  1245. to_free_highmem = 0;
  1246. save -= alloc_highmem;
  1247. if (to_free_normal > save)
  1248. to_free_normal -= save;
  1249. else
  1250. to_free_normal = 0;
  1251. }
  1252. memory_bm_position_reset(&copy_bm);
  1253. while (to_free_normal > 0 || to_free_highmem > 0) {
  1254. unsigned long pfn = memory_bm_next_pfn(&copy_bm);
  1255. struct page *page = pfn_to_page(pfn);
  1256. if (PageHighMem(page)) {
  1257. if (!to_free_highmem)
  1258. continue;
  1259. to_free_highmem--;
  1260. alloc_highmem--;
  1261. } else {
  1262. if (!to_free_normal)
  1263. continue;
  1264. to_free_normal--;
  1265. alloc_normal--;
  1266. }
  1267. memory_bm_clear_bit(&copy_bm, pfn);
  1268. swsusp_unset_page_forbidden(page);
  1269. swsusp_unset_page_free(page);
  1270. __free_page(page);
  1271. }
  1272. }
  1273. /**
  1274. * minimum_image_size - Estimate the minimum acceptable size of an image
  1275. * @saveable: Number of saveable pages in the system.
  1276. *
  1277. * We want to avoid attempting to free too much memory too hard, so estimate the
  1278. * minimum acceptable size of a hibernation image to use as the lower limit for
  1279. * preallocating memory.
  1280. *
  1281. * We assume that the minimum image size should be proportional to
  1282. *
  1283. * [number of saveable pages] - [number of pages that can be freed in theory]
  1284. *
  1285. * where the second term is the sum of (1) reclaimable slab pages, (2) active
  1286. * and (3) inactive anonymous pages, (4) active and (5) inactive file pages,
  1287. * minus mapped file pages.
  1288. */
  1289. static unsigned long minimum_image_size(unsigned long saveable)
  1290. {
  1291. unsigned long size;
  1292. size = global_page_state(NR_SLAB_RECLAIMABLE)
  1293. + global_page_state(NR_ACTIVE_ANON)
  1294. + global_page_state(NR_INACTIVE_ANON)
  1295. + global_page_state(NR_ACTIVE_FILE)
  1296. + global_page_state(NR_INACTIVE_FILE)
  1297. - global_page_state(NR_FILE_MAPPED);
  1298. return saveable <= size ? 0 : saveable - size;
  1299. }
  1300. /**
  1301. * hibernate_preallocate_memory - Preallocate memory for hibernation image
  1302. *
  1303. * To create a hibernation image it is necessary to make a copy of every page
  1304. * frame in use. We also need a number of page frames to be free during
  1305. * hibernation for allocations made while saving the image and for device
  1306. * drivers, in case they need to allocate memory from their hibernation
  1307. * callbacks (these two numbers are given by PAGES_FOR_IO (which is a rough
  1308. * estimate) and reserverd_size divided by PAGE_SIZE (which is tunable through
  1309. * /sys/power/reserved_size, respectively). To make this happen, we compute the
  1310. * total number of available page frames and allocate at least
  1311. *
  1312. * ([page frames total] + PAGES_FOR_IO + [metadata pages]) / 2
  1313. * + 2 * DIV_ROUND_UP(reserved_size, PAGE_SIZE)
  1314. *
  1315. * of them, which corresponds to the maximum size of a hibernation image.
  1316. *
  1317. * If image_size is set below the number following from the above formula,
  1318. * the preallocation of memory is continued until the total number of saveable
  1319. * pages in the system is below the requested image size or the minimum
  1320. * acceptable image size returned by minimum_image_size(), whichever is greater.
  1321. */
  1322. int hibernate_preallocate_memory(void)
  1323. {
  1324. struct zone *zone;
  1325. unsigned long saveable, size, max_size, count, highmem, pages = 0;
  1326. unsigned long alloc, save_highmem, pages_highmem, avail_normal;
  1327. struct timeval start, stop;
  1328. int error;
  1329. printk(KERN_INFO "PM: Preallocating image memory... ");
  1330. do_gettimeofday(&start);
  1331. error = memory_bm_create(&orig_bm, GFP_IMAGE, PG_ANY);
  1332. if (error)
  1333. goto err_out;
  1334. error = memory_bm_create(&copy_bm, GFP_IMAGE, PG_ANY);
  1335. if (error)
  1336. goto err_out;
  1337. alloc_normal = 0;
  1338. alloc_highmem = 0;
  1339. /* Count the number of saveable data pages. */
  1340. save_highmem = count_highmem_pages();
  1341. saveable = count_data_pages();
  1342. /*
  1343. * Compute the total number of page frames we can use (count) and the
  1344. * number of pages needed for image metadata (size).
  1345. */
  1346. count = saveable;
  1347. saveable += save_highmem;
  1348. highmem = save_highmem;
  1349. size = 0;
  1350. for_each_populated_zone(zone) {
  1351. size += snapshot_additional_pages(zone);
  1352. if (is_highmem(zone))
  1353. highmem += zone_page_state(zone, NR_FREE_PAGES);
  1354. else
  1355. count += zone_page_state(zone, NR_FREE_PAGES);
  1356. }
  1357. avail_normal = count;
  1358. count += highmem;
  1359. count -= totalreserve_pages;
  1360. /* Add number of pages required for page keys (s390 only). */
  1361. size += page_key_additional_pages(saveable);
  1362. /* Compute the maximum number of saveable pages to leave in memory. */
  1363. max_size = (count - (size + PAGES_FOR_IO)) / 2
  1364. - 2 * DIV_ROUND_UP(reserved_size, PAGE_SIZE);
  1365. /* Compute the desired number of image pages specified by image_size. */
  1366. size = DIV_ROUND_UP(image_size, PAGE_SIZE);
  1367. if (size > max_size)
  1368. size = max_size;
  1369. /*
  1370. * If the desired number of image pages is at least as large as the
  1371. * current number of saveable pages in memory, allocate page frames for
  1372. * the image and we're done.
  1373. */
  1374. if (size >= saveable) {
  1375. pages = preallocate_image_highmem(save_highmem);
  1376. pages += preallocate_image_memory(saveable - pages, avail_normal);
  1377. goto out;
  1378. }
  1379. /* Estimate the minimum size of the image. */
  1380. pages = minimum_image_size(saveable);
  1381. /*
  1382. * To avoid excessive pressure on the normal zone, leave room in it to
  1383. * accommodate an image of the minimum size (unless it's already too
  1384. * small, in which case don't preallocate pages from it at all).
  1385. */
  1386. if (avail_normal > pages)
  1387. avail_normal -= pages;
  1388. else
  1389. avail_normal = 0;
  1390. if (size < pages)
  1391. size = min_t(unsigned long, pages, max_size);
  1392. /*
  1393. * Let the memory management subsystem know that we're going to need a
  1394. * large number of page frames to allocate and make it free some memory.
  1395. * NOTE: If this is not done, performance will be hurt badly in some
  1396. * test cases.
  1397. */
  1398. shrink_all_memory(saveable - size);
  1399. /*
  1400. * The number of saveable pages in memory was too high, so apply some
  1401. * pressure to decrease it. First, make room for the largest possible
  1402. * image and fail if that doesn't work. Next, try to decrease the size
  1403. * of the image as much as indicated by 'size' using allocations from
  1404. * highmem and non-highmem zones separately.
  1405. */
  1406. pages_highmem = preallocate_image_highmem(highmem / 2);
  1407. alloc = count - max_size;
  1408. if (alloc > pages_highmem)
  1409. alloc -= pages_highmem;
  1410. else
  1411. alloc = 0;
  1412. pages = preallocate_image_memory(alloc, avail_normal);
  1413. if (pages < alloc) {
  1414. /* We have exhausted non-highmem pages, try highmem. */
  1415. alloc -= pages;
  1416. pages += pages_highmem;
  1417. pages_highmem = preallocate_image_highmem(alloc);
  1418. if (pages_highmem < alloc)
  1419. goto err_out;
  1420. pages += pages_highmem;
  1421. /*
  1422. * size is the desired number of saveable pages to leave in
  1423. * memory, so try to preallocate (all memory - size) pages.
  1424. */
  1425. alloc = (count - pages) - size;
  1426. pages += preallocate_image_highmem(alloc);
  1427. } else {
  1428. /*
  1429. * There are approximately max_size saveable pages at this point
  1430. * and we want to reduce this number down to size.
  1431. */
  1432. alloc = max_size - size;
  1433. size = preallocate_highmem_fraction(alloc, highmem, count);
  1434. pages_highmem += size;
  1435. alloc -= size;
  1436. size = preallocate_image_memory(alloc, avail_normal);
  1437. pages_highmem += preallocate_image_highmem(alloc - size);
  1438. pages += pages_highmem + size;
  1439. }
  1440. /*
  1441. * We only need as many page frames for the image as there are saveable
  1442. * pages in memory, but we have allocated more. Release the excessive
  1443. * ones now.
  1444. */
  1445. free_unnecessary_pages();
  1446. out:
  1447. do_gettimeofday(&stop);
  1448. printk(KERN_CONT "done (allocated %lu pages)\n", pages);
  1449. swsusp_show_speed(&start, &stop, pages, "Allocated");
  1450. return 0;
  1451. err_out:
  1452. printk(KERN_CONT "\n");
  1453. swsusp_free();
  1454. return -ENOMEM;
  1455. }
  1456. #ifdef CONFIG_HIGHMEM
  1457. /**
  1458. * count_pages_for_highmem - compute the number of non-highmem pages
  1459. * that will be necessary for creating copies of highmem pages.
  1460. */
  1461. static unsigned int count_pages_for_highmem(unsigned int nr_highmem)
  1462. {
  1463. unsigned int free_highmem = count_free_highmem_pages() + alloc_highmem;
  1464. if (free_highmem >= nr_highmem)
  1465. nr_highmem = 0;
  1466. else
  1467. nr_highmem -= free_highmem;
  1468. return nr_highmem;
  1469. }
  1470. #else
  1471. static unsigned int
  1472. count_pages_for_highmem(unsigned int nr_highmem) { return 0; }
  1473. #endif /* CONFIG_HIGHMEM */
  1474. /**
  1475. * enough_free_mem - Make sure we have enough free memory for the
  1476. * snapshot image.
  1477. */
  1478. static int enough_free_mem(unsigned int nr_pages, unsigned int nr_highmem)
  1479. {
  1480. struct zone *zone;
  1481. unsigned int free = alloc_normal;
  1482. for_each_populated_zone(zone)
  1483. if (!is_highmem(zone))
  1484. free += zone_page_state(zone, NR_FREE_PAGES);
  1485. nr_pages += count_pages_for_highmem(nr_highmem);
  1486. pr_debug("PM: Normal pages needed: %u + %u, available pages: %u\n",
  1487. nr_pages, PAGES_FOR_IO, free);
  1488. return free > nr_pages + PAGES_FOR_IO;
  1489. }
  1490. #ifdef CONFIG_HIGHMEM
  1491. /**
  1492. * get_highmem_buffer - if there are some highmem pages in the suspend
  1493. * image, we may need the buffer to copy them and/or load their data.
  1494. */
  1495. static inline int get_highmem_buffer(int safe_needed)
  1496. {
  1497. buffer = get_image_page(GFP_ATOMIC | __GFP_COLD, safe_needed);
  1498. return buffer ? 0 : -ENOMEM;
  1499. }
  1500. /**
  1501. * alloc_highmem_image_pages - allocate some highmem pages for the image.
  1502. * Try to allocate as many pages as needed, but if the number of free
  1503. * highmem pages is lesser than that, allocate them all.
  1504. */
  1505. static inline unsigned int
  1506. alloc_highmem_pages(struct memory_bitmap *bm, unsigned int nr_highmem)
  1507. {
  1508. unsigned int to_alloc = count_free_highmem_pages();
  1509. if (to_alloc > nr_highmem)
  1510. to_alloc = nr_highmem;
  1511. nr_highmem -= to_alloc;
  1512. while (to_alloc-- > 0) {
  1513. struct page *page;
  1514. page = alloc_image_page(__GFP_HIGHMEM);
  1515. memory_bm_set_bit(bm, page_to_pfn(page));
  1516. }
  1517. return nr_highmem;
  1518. }
  1519. #else
  1520. static inline int get_highmem_buffer(int safe_needed) { return 0; }
  1521. static inline unsigned int
  1522. alloc_highmem_pages(struct memory_bitmap *bm, unsigned int n) { return 0; }
  1523. #endif /* CONFIG_HIGHMEM */
  1524. /**
  1525. * swsusp_alloc - allocate memory for the suspend image
  1526. *
  1527. * We first try to allocate as many highmem pages as there are
  1528. * saveable highmem pages in the system. If that fails, we allocate
  1529. * non-highmem pages for the copies of the remaining highmem ones.
  1530. *
  1531. * In this approach it is likely that the copies of highmem pages will
  1532. * also be located in the high memory, because of the way in which
  1533. * copy_data_pages() works.
  1534. */
  1535. static int
  1536. swsusp_alloc(struct memory_bitmap *orig_bm, struct memory_bitmap *copy_bm,
  1537. unsigned int nr_pages, unsigned int nr_highmem)
  1538. {
  1539. if (nr_highmem > 0) {
  1540. if (get_highmem_buffer(PG_ANY))
  1541. goto err_out;
  1542. if (nr_highmem > alloc_highmem) {
  1543. nr_highmem -= alloc_highmem;
  1544. nr_pages += alloc_highmem_pages(copy_bm, nr_highmem);
  1545. }
  1546. }
  1547. if (nr_pages > alloc_normal) {
  1548. nr_pages -= alloc_normal;
  1549. while (nr_pages-- > 0) {
  1550. struct page *page;
  1551. page = alloc_image_page(GFP_ATOMIC | __GFP_COLD);
  1552. if (!page)
  1553. goto err_out;
  1554. memory_bm_set_bit(copy_bm, page_to_pfn(page));
  1555. }
  1556. }
  1557. return 0;
  1558. err_out:
  1559. swsusp_free();
  1560. return -ENOMEM;
  1561. }
  1562. asmlinkage __visible int swsusp_save(void)
  1563. {
  1564. unsigned int nr_pages, nr_highmem;
  1565. printk(KERN_INFO "PM: Creating hibernation image:\n");
  1566. drain_local_pages(NULL);
  1567. nr_pages = count_data_pages();
  1568. nr_highmem = count_highmem_pages();
  1569. printk(KERN_INFO "PM: Need to copy %u pages\n", nr_pages + nr_highmem);
  1570. if (!enough_free_mem(nr_pages, nr_highmem)) {
  1571. printk(KERN_ERR "PM: Not enough free memory\n");
  1572. return -ENOMEM;
  1573. }
  1574. if (swsusp_alloc(&orig_bm, &copy_bm, nr_pages, nr_highmem)) {
  1575. printk(KERN_ERR "PM: Memory allocation failed\n");
  1576. return -ENOMEM;
  1577. }
  1578. /* During allocating of suspend pagedir, new cold pages may appear.
  1579. * Kill them.
  1580. */
  1581. drain_local_pages(NULL);
  1582. copy_data_pages(&copy_bm, &orig_bm);
  1583. /*
  1584. * End of critical section. From now on, we can write to memory,
  1585. * but we should not touch disk. This specially means we must _not_
  1586. * touch swap space! Except we must write out our image of course.
  1587. */
  1588. nr_pages += nr_highmem;
  1589. nr_copy_pages = nr_pages;
  1590. nr_meta_pages = DIV_ROUND_UP(nr_pages * sizeof(long), PAGE_SIZE);
  1591. printk(KERN_INFO "PM: Hibernation image created (%d pages copied)\n",
  1592. nr_pages);
  1593. return 0;
  1594. }
  1595. #ifndef CONFIG_ARCH_HIBERNATION_HEADER
  1596. static int init_header_complete(struct swsusp_info *info)
  1597. {
  1598. memcpy(&info->uts, init_utsname(), sizeof(struct new_utsname));
  1599. info->version_code = LINUX_VERSION_CODE;
  1600. return 0;
  1601. }
  1602. static char *check_image_kernel(struct swsusp_info *info)
  1603. {
  1604. if (info->version_code != LINUX_VERSION_CODE)
  1605. return "kernel version";
  1606. if (strcmp(info->uts.sysname,init_utsname()->sysname))
  1607. return "system type";
  1608. if (strcmp(info->uts.release,init_utsname()->release))
  1609. return "kernel release";
  1610. if (strcmp(info->uts.version,init_utsname()->version))
  1611. return "version";
  1612. if (strcmp(info->uts.machine,init_utsname()->machine))
  1613. return "machine";
  1614. return NULL;
  1615. }
  1616. #endif /* CONFIG_ARCH_HIBERNATION_HEADER */
  1617. unsigned long snapshot_get_image_size(void)
  1618. {
  1619. return nr_copy_pages + nr_meta_pages + 1;
  1620. }
  1621. static int init_header(struct swsusp_info *info)
  1622. {
  1623. memset(info, 0, sizeof(struct swsusp_info));
  1624. info->num_physpages = get_num_physpages();
  1625. info->image_pages = nr_copy_pages;
  1626. info->pages = snapshot_get_image_size();
  1627. info->size = info->pages;
  1628. info->size <<= PAGE_SHIFT;
  1629. return init_header_complete(info);
  1630. }
  1631. /**
  1632. * pack_pfns - pfns corresponding to the set bits found in the bitmap @bm
  1633. * are stored in the array @buf[] (1 page at a time)
  1634. */
  1635. static inline void
  1636. pack_pfns(unsigned long *buf, struct memory_bitmap *bm)
  1637. {
  1638. int j;
  1639. for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
  1640. buf[j] = memory_bm_next_pfn(bm);
  1641. if (unlikely(buf[j] == BM_END_OF_MAP))
  1642. break;
  1643. /* Save page key for data page (s390 only). */
  1644. page_key_read(buf + j);
  1645. }
  1646. }
  1647. /**
  1648. * snapshot_read_next - used for reading the system memory snapshot.
  1649. *
  1650. * On the first call to it @handle should point to a zeroed
  1651. * snapshot_handle structure. The structure gets updated and a pointer
  1652. * to it should be passed to this function every next time.
  1653. *
  1654. * On success the function returns a positive number. Then, the caller
  1655. * is allowed to read up to the returned number of bytes from the memory
  1656. * location computed by the data_of() macro.
  1657. *
  1658. * The function returns 0 to indicate the end of data stream condition,
  1659. * and a negative number is returned on error. In such cases the
  1660. * structure pointed to by @handle is not updated and should not be used
  1661. * any more.
  1662. */
  1663. int snapshot_read_next(struct snapshot_handle *handle)
  1664. {
  1665. if (handle->cur > nr_meta_pages + nr_copy_pages)
  1666. return 0;
  1667. if (!buffer) {
  1668. /* This makes the buffer be freed by swsusp_free() */
  1669. buffer = get_image_page(GFP_ATOMIC, PG_ANY);
  1670. if (!buffer)
  1671. return -ENOMEM;
  1672. }
  1673. if (!handle->cur) {
  1674. int error;
  1675. error = init_header((struct swsusp_info *)buffer);
  1676. if (error)
  1677. return error;
  1678. handle->buffer = buffer;
  1679. memory_bm_position_reset(&orig_bm);
  1680. memory_bm_position_reset(&copy_bm);
  1681. } else if (handle->cur <= nr_meta_pages) {
  1682. clear_page(buffer);
  1683. pack_pfns(buffer, &orig_bm);
  1684. } else {
  1685. struct page *page;
  1686. page = pfn_to_page(memory_bm_next_pfn(&copy_bm));
  1687. if (PageHighMem(page)) {
  1688. /* Highmem pages are copied to the buffer,
  1689. * because we can't return with a kmapped
  1690. * highmem page (we may not be called again).
  1691. */
  1692. void *kaddr;
  1693. kaddr = kmap_atomic(page);
  1694. copy_page(buffer, kaddr);
  1695. kunmap_atomic(kaddr);
  1696. handle->buffer = buffer;
  1697. } else {
  1698. handle->buffer = page_address(page);
  1699. }
  1700. }
  1701. handle->cur++;
  1702. return PAGE_SIZE;
  1703. }
  1704. /**
  1705. * mark_unsafe_pages - mark the pages that cannot be used for storing
  1706. * the image during resume, because they conflict with the pages that
  1707. * had been used before suspend
  1708. */
  1709. static int mark_unsafe_pages(struct memory_bitmap *bm)
  1710. {
  1711. struct zone *zone;
  1712. unsigned long pfn, max_zone_pfn;
  1713. /* Clear page flags */
  1714. for_each_populated_zone(zone) {
  1715. max_zone_pfn = zone_end_pfn(zone);
  1716. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
  1717. if (pfn_valid(pfn))
  1718. swsusp_unset_page_free(pfn_to_page(pfn));
  1719. }
  1720. /* Mark pages that correspond to the "original" pfns as "unsafe" */
  1721. memory_bm_position_reset(bm);
  1722. do {
  1723. pfn = memory_bm_next_pfn(bm);
  1724. if (likely(pfn != BM_END_OF_MAP)) {
  1725. if (likely(pfn_valid(pfn)) && !is_nosave_page(pfn))
  1726. swsusp_set_page_free(pfn_to_page(pfn));
  1727. else
  1728. return -EFAULT;
  1729. }
  1730. } while (pfn != BM_END_OF_MAP);
  1731. allocated_unsafe_pages = 0;
  1732. return 0;
  1733. }
  1734. static void
  1735. duplicate_memory_bitmap(struct memory_bitmap *dst, struct memory_bitmap *src)
  1736. {
  1737. unsigned long pfn;
  1738. memory_bm_position_reset(src);
  1739. pfn = memory_bm_next_pfn(src);
  1740. while (pfn != BM_END_OF_MAP) {
  1741. memory_bm_set_bit(dst, pfn);
  1742. pfn = memory_bm_next_pfn(src);
  1743. }
  1744. }
  1745. static int check_header(struct swsusp_info *info)
  1746. {
  1747. char *reason;
  1748. reason = check_image_kernel(info);
  1749. if (!reason && info->num_physpages != get_num_physpages())
  1750. reason = "memory size";
  1751. if (reason) {
  1752. printk(KERN_ERR "PM: Image mismatch: %s\n", reason);
  1753. return -EPERM;
  1754. }
  1755. return 0;
  1756. }
  1757. /**
  1758. * load header - check the image header and copy data from it
  1759. */
  1760. static int
  1761. load_header(struct swsusp_info *info)
  1762. {
  1763. int error;
  1764. restore_pblist = NULL;
  1765. error = check_header(info);
  1766. if (!error) {
  1767. nr_copy_pages = info->image_pages;
  1768. nr_meta_pages = info->pages - info->image_pages - 1;
  1769. }
  1770. return error;
  1771. }
  1772. /**
  1773. * unpack_orig_pfns - for each element of @buf[] (1 page at a time) set
  1774. * the corresponding bit in the memory bitmap @bm
  1775. */
  1776. static int unpack_orig_pfns(unsigned long *buf, struct memory_bitmap *bm)
  1777. {
  1778. int j;
  1779. for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
  1780. if (unlikely(buf[j] == BM_END_OF_MAP))
  1781. break;
  1782. /* Extract and buffer page key for data page (s390 only). */
  1783. page_key_memorize(buf + j);
  1784. if (memory_bm_pfn_present(bm, buf[j]))
  1785. memory_bm_set_bit(bm, buf[j]);
  1786. else
  1787. return -EFAULT;
  1788. }
  1789. return 0;
  1790. }
  1791. /* List of "safe" pages that may be used to store data loaded from the suspend
  1792. * image
  1793. */
  1794. static struct linked_page *safe_pages_list;
  1795. #ifdef CONFIG_HIGHMEM
  1796. /* struct highmem_pbe is used for creating the list of highmem pages that
  1797. * should be restored atomically during the resume from disk, because the page
  1798. * frames they have occupied before the suspend are in use.
  1799. */
  1800. struct highmem_pbe {
  1801. struct page *copy_page; /* data is here now */
  1802. struct page *orig_page; /* data was here before the suspend */
  1803. struct highmem_pbe *next;
  1804. };
  1805. /* List of highmem PBEs needed for restoring the highmem pages that were
  1806. * allocated before the suspend and included in the suspend image, but have
  1807. * also been allocated by the "resume" kernel, so their contents cannot be
  1808. * written directly to their "original" page frames.
  1809. */
  1810. static struct highmem_pbe *highmem_pblist;
  1811. /**
  1812. * count_highmem_image_pages - compute the number of highmem pages in the
  1813. * suspend image. The bits in the memory bitmap @bm that correspond to the
  1814. * image pages are assumed to be set.
  1815. */
  1816. static unsigned int count_highmem_image_pages(struct memory_bitmap *bm)
  1817. {
  1818. unsigned long pfn;
  1819. unsigned int cnt = 0;
  1820. memory_bm_position_reset(bm);
  1821. pfn = memory_bm_next_pfn(bm);
  1822. while (pfn != BM_END_OF_MAP) {
  1823. if (PageHighMem(pfn_to_page(pfn)))
  1824. cnt++;
  1825. pfn = memory_bm_next_pfn(bm);
  1826. }
  1827. return cnt;
  1828. }
  1829. /**
  1830. * prepare_highmem_image - try to allocate as many highmem pages as
  1831. * there are highmem image pages (@nr_highmem_p points to the variable
  1832. * containing the number of highmem image pages). The pages that are
  1833. * "safe" (ie. will not be overwritten when the suspend image is
  1834. * restored) have the corresponding bits set in @bm (it must be
  1835. * unitialized).
  1836. *
  1837. * NOTE: This function should not be called if there are no highmem
  1838. * image pages.
  1839. */
  1840. static unsigned int safe_highmem_pages;
  1841. static struct memory_bitmap *safe_highmem_bm;
  1842. static int
  1843. prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
  1844. {
  1845. unsigned int to_alloc;
  1846. if (memory_bm_create(bm, GFP_ATOMIC, PG_SAFE))
  1847. return -ENOMEM;
  1848. if (get_highmem_buffer(PG_SAFE))
  1849. return -ENOMEM;
  1850. to_alloc = count_free_highmem_pages();
  1851. if (to_alloc > *nr_highmem_p)
  1852. to_alloc = *nr_highmem_p;
  1853. else
  1854. *nr_highmem_p = to_alloc;
  1855. safe_highmem_pages = 0;
  1856. while (to_alloc-- > 0) {
  1857. struct page *page;
  1858. page = alloc_page(__GFP_HIGHMEM);
  1859. if (!swsusp_page_is_free(page)) {
  1860. /* The page is "safe", set its bit the bitmap */
  1861. memory_bm_set_bit(bm, page_to_pfn(page));
  1862. safe_highmem_pages++;
  1863. }
  1864. /* Mark the page as allocated */
  1865. swsusp_set_page_forbidden(page);
  1866. swsusp_set_page_free(page);
  1867. }
  1868. memory_bm_position_reset(bm);
  1869. safe_highmem_bm = bm;
  1870. return 0;
  1871. }
  1872. /**
  1873. * get_highmem_page_buffer - for given highmem image page find the buffer
  1874. * that suspend_write_next() should set for its caller to write to.
  1875. *
  1876. * If the page is to be saved to its "original" page frame or a copy of
  1877. * the page is to be made in the highmem, @buffer is returned. Otherwise,
  1878. * the copy of the page is to be made in normal memory, so the address of
  1879. * the copy is returned.
  1880. *
  1881. * If @buffer is returned, the caller of suspend_write_next() will write
  1882. * the page's contents to @buffer, so they will have to be copied to the
  1883. * right location on the next call to suspend_write_next() and it is done
  1884. * with the help of copy_last_highmem_page(). For this purpose, if
  1885. * @buffer is returned, @last_highmem page is set to the page to which
  1886. * the data will have to be copied from @buffer.
  1887. */
  1888. static struct page *last_highmem_page;
  1889. static void *
  1890. get_highmem_page_buffer(struct page *page, struct chain_allocator *ca)
  1891. {
  1892. struct highmem_pbe *pbe;
  1893. void *kaddr;
  1894. if (swsusp_page_is_forbidden(page) && swsusp_page_is_free(page)) {
  1895. /* We have allocated the "original" page frame and we can
  1896. * use it directly to store the loaded page.
  1897. */
  1898. last_highmem_page = page;
  1899. return buffer;
  1900. }
  1901. /* The "original" page frame has not been allocated and we have to
  1902. * use a "safe" page frame to store the loaded page.
  1903. */
  1904. pbe = chain_alloc(ca, sizeof(struct highmem_pbe));
  1905. if (!pbe) {
  1906. swsusp_free();
  1907. return ERR_PTR(-ENOMEM);
  1908. }
  1909. pbe->orig_page = page;
  1910. if (safe_highmem_pages > 0) {
  1911. struct page *tmp;
  1912. /* Copy of the page will be stored in high memory */
  1913. kaddr = buffer;
  1914. tmp = pfn_to_page(memory_bm_next_pfn(safe_highmem_bm));
  1915. safe_highmem_pages--;
  1916. last_highmem_page = tmp;
  1917. pbe->copy_page = tmp;
  1918. } else {
  1919. /* Copy of the page will be stored in normal memory */
  1920. kaddr = safe_pages_list;
  1921. safe_pages_list = safe_pages_list->next;
  1922. pbe->copy_page = virt_to_page(kaddr);
  1923. }
  1924. pbe->next = highmem_pblist;
  1925. highmem_pblist = pbe;
  1926. return kaddr;
  1927. }
  1928. /**
  1929. * copy_last_highmem_page - copy the contents of a highmem image from
  1930. * @buffer, where the caller of snapshot_write_next() has place them,
  1931. * to the right location represented by @last_highmem_page .
  1932. */
  1933. static void copy_last_highmem_page(void)
  1934. {
  1935. if (last_highmem_page) {
  1936. void *dst;
  1937. dst = kmap_atomic(last_highmem_page);
  1938. copy_page(dst, buffer);
  1939. kunmap_atomic(dst);
  1940. last_highmem_page = NULL;
  1941. }
  1942. }
  1943. static inline int last_highmem_page_copied(void)
  1944. {
  1945. return !last_highmem_page;
  1946. }
  1947. static inline void free_highmem_data(void)
  1948. {
  1949. if (safe_highmem_bm)
  1950. memory_bm_free(safe_highmem_bm, PG_UNSAFE_CLEAR);
  1951. if (buffer)
  1952. free_image_page(buffer, PG_UNSAFE_CLEAR);
  1953. }
  1954. #else
  1955. static inline int get_safe_write_buffer(void) { return 0; }
  1956. static unsigned int
  1957. count_highmem_image_pages(struct memory_bitmap *bm) { return 0; }
  1958. static inline int
  1959. prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
  1960. {
  1961. return 0;
  1962. }
  1963. static inline void *
  1964. get_highmem_page_buffer(struct page *page, struct chain_allocator *ca)
  1965. {
  1966. return ERR_PTR(-EINVAL);
  1967. }
  1968. static inline void copy_last_highmem_page(void) {}
  1969. static inline int last_highmem_page_copied(void) { return 1; }
  1970. static inline void free_highmem_data(void) {}
  1971. #endif /* CONFIG_HIGHMEM */
  1972. /**
  1973. * prepare_image - use the memory bitmap @bm to mark the pages that will
  1974. * be overwritten in the process of restoring the system memory state
  1975. * from the suspend image ("unsafe" pages) and allocate memory for the
  1976. * image.
  1977. *
  1978. * The idea is to allocate a new memory bitmap first and then allocate
  1979. * as many pages as needed for the image data, but not to assign these
  1980. * pages to specific tasks initially. Instead, we just mark them as
  1981. * allocated and create a lists of "safe" pages that will be used
  1982. * later. On systems with high memory a list of "safe" highmem pages is
  1983. * also created.
  1984. */
  1985. #define PBES_PER_LINKED_PAGE (LINKED_PAGE_DATA_SIZE / sizeof(struct pbe))
  1986. static int
  1987. prepare_image(struct memory_bitmap *new_bm, struct memory_bitmap *bm)
  1988. {
  1989. unsigned int nr_pages, nr_highmem;
  1990. struct linked_page *sp_list, *lp;
  1991. int error;
  1992. /* If there is no highmem, the buffer will not be necessary */
  1993. free_image_page(buffer, PG_UNSAFE_CLEAR);
  1994. buffer = NULL;
  1995. nr_highmem = count_highmem_image_pages(bm);
  1996. error = mark_unsafe_pages(bm);
  1997. if (error)
  1998. goto Free;
  1999. error = memory_bm_create(new_bm, GFP_ATOMIC, PG_SAFE);
  2000. if (error)
  2001. goto Free;
  2002. duplicate_memory_bitmap(new_bm, bm);
  2003. memory_bm_free(bm, PG_UNSAFE_KEEP);
  2004. if (nr_highmem > 0) {
  2005. error = prepare_highmem_image(bm, &nr_highmem);
  2006. if (error)
  2007. goto Free;
  2008. }
  2009. /* Reserve some safe pages for potential later use.
  2010. *
  2011. * NOTE: This way we make sure there will be enough safe pages for the
  2012. * chain_alloc() in get_buffer(). It is a bit wasteful, but
  2013. * nr_copy_pages cannot be greater than 50% of the memory anyway.
  2014. */
  2015. sp_list = NULL;
  2016. /* nr_copy_pages cannot be lesser than allocated_unsafe_pages */
  2017. nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
  2018. nr_pages = DIV_ROUND_UP(nr_pages, PBES_PER_LINKED_PAGE);
  2019. while (nr_pages > 0) {
  2020. lp = get_image_page(GFP_ATOMIC, PG_SAFE);
  2021. if (!lp) {
  2022. error = -ENOMEM;
  2023. goto Free;
  2024. }
  2025. lp->next = sp_list;
  2026. sp_list = lp;
  2027. nr_pages--;
  2028. }
  2029. /* Preallocate memory for the image */
  2030. safe_pages_list = NULL;
  2031. nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
  2032. while (nr_pages > 0) {
  2033. lp = (struct linked_page *)get_zeroed_page(GFP_ATOMIC);
  2034. if (!lp) {
  2035. error = -ENOMEM;
  2036. goto Free;
  2037. }
  2038. if (!swsusp_page_is_free(virt_to_page(lp))) {
  2039. /* The page is "safe", add it to the list */
  2040. lp->next = safe_pages_list;
  2041. safe_pages_list = lp;
  2042. }
  2043. /* Mark the page as allocated */
  2044. swsusp_set_page_forbidden(virt_to_page(lp));
  2045. swsusp_set_page_free(virt_to_page(lp));
  2046. nr_pages--;
  2047. }
  2048. /* Free the reserved safe pages so that chain_alloc() can use them */
  2049. while (sp_list) {
  2050. lp = sp_list->next;
  2051. free_image_page(sp_list, PG_UNSAFE_CLEAR);
  2052. sp_list = lp;
  2053. }
  2054. return 0;
  2055. Free:
  2056. swsusp_free();
  2057. return error;
  2058. }
  2059. /**
  2060. * get_buffer - compute the address that snapshot_write_next() should
  2061. * set for its caller to write to.
  2062. */
  2063. static void *get_buffer(struct memory_bitmap *bm, struct chain_allocator *ca)
  2064. {
  2065. struct pbe *pbe;
  2066. struct page *page;
  2067. unsigned long pfn = memory_bm_next_pfn(bm);
  2068. if (pfn == BM_END_OF_MAP)
  2069. return ERR_PTR(-EFAULT);
  2070. page = pfn_to_page(pfn);
  2071. if (PageHighMem(page))
  2072. return get_highmem_page_buffer(page, ca);
  2073. if (swsusp_page_is_forbidden(page) && swsusp_page_is_free(page))
  2074. /* We have allocated the "original" page frame and we can
  2075. * use it directly to store the loaded page.
  2076. */
  2077. return page_address(page);
  2078. /* The "original" page frame has not been allocated and we have to
  2079. * use a "safe" page frame to store the loaded page.
  2080. */
  2081. pbe = chain_alloc(ca, sizeof(struct pbe));
  2082. if (!pbe) {
  2083. swsusp_free();
  2084. return ERR_PTR(-ENOMEM);
  2085. }
  2086. pbe->orig_address = page_address(page);
  2087. pbe->address = safe_pages_list;
  2088. safe_pages_list = safe_pages_list->next;
  2089. pbe->next = restore_pblist;
  2090. restore_pblist = pbe;
  2091. return pbe->address;
  2092. }
  2093. /**
  2094. * snapshot_write_next - used for writing the system memory snapshot.
  2095. *
  2096. * On the first call to it @handle should point to a zeroed
  2097. * snapshot_handle structure. The structure gets updated and a pointer
  2098. * to it should be passed to this function every next time.
  2099. *
  2100. * On success the function returns a positive number. Then, the caller
  2101. * is allowed to write up to the returned number of bytes to the memory
  2102. * location computed by the data_of() macro.
  2103. *
  2104. * The function returns 0 to indicate the "end of file" condition,
  2105. * and a negative number is returned on error. In such cases the
  2106. * structure pointed to by @handle is not updated and should not be used
  2107. * any more.
  2108. */
  2109. int snapshot_write_next(struct snapshot_handle *handle)
  2110. {
  2111. static struct chain_allocator ca;
  2112. int error = 0;
  2113. /* Check if we have already loaded the entire image */
  2114. if (handle->cur > 1 && handle->cur > nr_meta_pages + nr_copy_pages)
  2115. return 0;
  2116. handle->sync_read = 1;
  2117. if (!handle->cur) {
  2118. if (!buffer)
  2119. /* This makes the buffer be freed by swsusp_free() */
  2120. buffer = get_image_page(GFP_ATOMIC, PG_ANY);
  2121. if (!buffer)
  2122. return -ENOMEM;
  2123. handle->buffer = buffer;
  2124. } else if (handle->cur == 1) {
  2125. error = load_header(buffer);
  2126. if (error)
  2127. return error;
  2128. error = memory_bm_create(&copy_bm, GFP_ATOMIC, PG_ANY);
  2129. if (error)
  2130. return error;
  2131. /* Allocate buffer for page keys. */
  2132. error = page_key_alloc(nr_copy_pages);
  2133. if (error)
  2134. return error;
  2135. } else if (handle->cur <= nr_meta_pages + 1) {
  2136. error = unpack_orig_pfns(buffer, &copy_bm);
  2137. if (error)
  2138. return error;
  2139. if (handle->cur == nr_meta_pages + 1) {
  2140. error = prepare_image(&orig_bm, &copy_bm);
  2141. if (error)
  2142. return error;
  2143. chain_init(&ca, GFP_ATOMIC, PG_SAFE);
  2144. memory_bm_position_reset(&orig_bm);
  2145. restore_pblist = NULL;
  2146. handle->buffer = get_buffer(&orig_bm, &ca);
  2147. handle->sync_read = 0;
  2148. if (IS_ERR(handle->buffer))
  2149. return PTR_ERR(handle->buffer);
  2150. }
  2151. } else {
  2152. copy_last_highmem_page();
  2153. /* Restore page key for data page (s390 only). */
  2154. page_key_write(handle->buffer);
  2155. handle->buffer = get_buffer(&orig_bm, &ca);
  2156. if (IS_ERR(handle->buffer))
  2157. return PTR_ERR(handle->buffer);
  2158. if (handle->buffer != buffer)
  2159. handle->sync_read = 0;
  2160. }
  2161. handle->cur++;
  2162. return PAGE_SIZE;
  2163. }
  2164. /**
  2165. * snapshot_write_finalize - must be called after the last call to
  2166. * snapshot_write_next() in case the last page in the image happens
  2167. * to be a highmem page and its contents should be stored in the
  2168. * highmem. Additionally, it releases the memory that will not be
  2169. * used any more.
  2170. */
  2171. void snapshot_write_finalize(struct snapshot_handle *handle)
  2172. {
  2173. copy_last_highmem_page();
  2174. /* Restore page key for data page (s390 only). */
  2175. page_key_write(handle->buffer);
  2176. page_key_free();
  2177. /* Free only if we have loaded the image entirely */
  2178. if (handle->cur > 1 && handle->cur > nr_meta_pages + nr_copy_pages) {
  2179. memory_bm_free(&orig_bm, PG_UNSAFE_CLEAR);
  2180. free_highmem_data();
  2181. }
  2182. }
  2183. int snapshot_image_loaded(struct snapshot_handle *handle)
  2184. {
  2185. return !(!nr_copy_pages || !last_highmem_page_copied() ||
  2186. handle->cur <= nr_meta_pages + nr_copy_pages);
  2187. }
  2188. #ifdef CONFIG_HIGHMEM
  2189. /* Assumes that @buf is ready and points to a "safe" page */
  2190. static inline void
  2191. swap_two_pages_data(struct page *p1, struct page *p2, void *buf)
  2192. {
  2193. void *kaddr1, *kaddr2;
  2194. kaddr1 = kmap_atomic(p1);
  2195. kaddr2 = kmap_atomic(p2);
  2196. copy_page(buf, kaddr1);
  2197. copy_page(kaddr1, kaddr2);
  2198. copy_page(kaddr2, buf);
  2199. kunmap_atomic(kaddr2);
  2200. kunmap_atomic(kaddr1);
  2201. }
  2202. /**
  2203. * restore_highmem - for each highmem page that was allocated before
  2204. * the suspend and included in the suspend image, and also has been
  2205. * allocated by the "resume" kernel swap its current (ie. "before
  2206. * resume") contents with the previous (ie. "before suspend") one.
  2207. *
  2208. * If the resume eventually fails, we can call this function once
  2209. * again and restore the "before resume" highmem state.
  2210. */
  2211. int restore_highmem(void)
  2212. {
  2213. struct highmem_pbe *pbe = highmem_pblist;
  2214. void *buf;
  2215. if (!pbe)
  2216. return 0;
  2217. buf = get_image_page(GFP_ATOMIC, PG_SAFE);
  2218. if (!buf)
  2219. return -ENOMEM;
  2220. while (pbe) {
  2221. swap_two_pages_data(pbe->copy_page, pbe->orig_page, buf);
  2222. pbe = pbe->next;
  2223. }
  2224. free_image_page(buf, PG_UNSAFE_CLEAR);
  2225. return 0;
  2226. }
  2227. #endif /* CONFIG_HIGHMEM */