snapshot.c 68 KB

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