snapshot.c 59 KB

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