snapshot.c 61 KB

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