snapshot.c 78 KB

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