snapshot.c 72 KB

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