snapshot.c 59 KB

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