snapshot.c 61 KB

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