snapshot.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302
  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. size = DIV_ROUND_UP(image_size, PAGE_SIZE);
  1111. if (size > max_size)
  1112. size = max_size;
  1113. /*
  1114. * If the maximum is not less than the current number of saveable pages
  1115. * in memory, allocate page frames for the image and we're done.
  1116. */
  1117. if (size >= saveable) {
  1118. pages = preallocate_image_highmem(save_highmem);
  1119. pages += preallocate_image_memory(saveable - pages, avail_normal);
  1120. goto out;
  1121. }
  1122. /* Estimate the minimum size of the image. */
  1123. pages = minimum_image_size(saveable);
  1124. /*
  1125. * To avoid excessive pressure on the normal zone, leave room in it to
  1126. * accommodate an image of the minimum size (unless it's already too
  1127. * small, in which case don't preallocate pages from it at all).
  1128. */
  1129. if (avail_normal > pages)
  1130. avail_normal -= pages;
  1131. else
  1132. avail_normal = 0;
  1133. if (size < pages)
  1134. size = min_t(unsigned long, pages, max_size);
  1135. /*
  1136. * Let the memory management subsystem know that we're going to need a
  1137. * large number of page frames to allocate and make it free some memory.
  1138. * NOTE: If this is not done, performance will be hurt badly in some
  1139. * test cases.
  1140. */
  1141. shrink_all_memory(saveable - size);
  1142. /*
  1143. * The number of saveable pages in memory was too high, so apply some
  1144. * pressure to decrease it. First, make room for the largest possible
  1145. * image and fail if that doesn't work. Next, try to decrease the size
  1146. * of the image as much as indicated by 'size' using allocations from
  1147. * highmem and non-highmem zones separately.
  1148. */
  1149. pages_highmem = preallocate_image_highmem(highmem / 2);
  1150. alloc = (count - max_size) - pages_highmem;
  1151. pages = preallocate_image_memory(alloc, avail_normal);
  1152. if (pages < alloc) {
  1153. /* We have exhausted non-highmem pages, try highmem. */
  1154. alloc -= pages;
  1155. pages += pages_highmem;
  1156. pages_highmem = preallocate_image_highmem(alloc);
  1157. if (pages_highmem < alloc)
  1158. goto err_out;
  1159. pages += pages_highmem;
  1160. /*
  1161. * size is the desired number of saveable pages to leave in
  1162. * memory, so try to preallocate (all memory - size) pages.
  1163. */
  1164. alloc = (count - pages) - size;
  1165. pages += preallocate_image_highmem(alloc);
  1166. } else {
  1167. /*
  1168. * There are approximately max_size saveable pages at this point
  1169. * and we want to reduce this number down to size.
  1170. */
  1171. alloc = max_size - size;
  1172. size = preallocate_highmem_fraction(alloc, highmem, count);
  1173. pages_highmem += size;
  1174. alloc -= size;
  1175. size = preallocate_image_memory(alloc, avail_normal);
  1176. pages_highmem += preallocate_image_highmem(alloc - size);
  1177. pages += pages_highmem + size;
  1178. }
  1179. /*
  1180. * We only need as many page frames for the image as there are saveable
  1181. * pages in memory, but we have allocated more. Release the excessive
  1182. * ones now.
  1183. */
  1184. free_unnecessary_pages();
  1185. out:
  1186. do_gettimeofday(&stop);
  1187. printk(KERN_CONT "done (allocated %lu pages)\n", pages);
  1188. swsusp_show_speed(&start, &stop, pages, "Allocated");
  1189. return 0;
  1190. err_out:
  1191. printk(KERN_CONT "\n");
  1192. swsusp_free();
  1193. return -ENOMEM;
  1194. }
  1195. #ifdef CONFIG_HIGHMEM
  1196. /**
  1197. * count_pages_for_highmem - compute the number of non-highmem pages
  1198. * that will be necessary for creating copies of highmem pages.
  1199. */
  1200. static unsigned int count_pages_for_highmem(unsigned int nr_highmem)
  1201. {
  1202. unsigned int free_highmem = count_free_highmem_pages() + alloc_highmem;
  1203. if (free_highmem >= nr_highmem)
  1204. nr_highmem = 0;
  1205. else
  1206. nr_highmem -= free_highmem;
  1207. return nr_highmem;
  1208. }
  1209. #else
  1210. static unsigned int
  1211. count_pages_for_highmem(unsigned int nr_highmem) { return 0; }
  1212. #endif /* CONFIG_HIGHMEM */
  1213. /**
  1214. * enough_free_mem - Make sure we have enough free memory for the
  1215. * snapshot image.
  1216. */
  1217. static int enough_free_mem(unsigned int nr_pages, unsigned int nr_highmem)
  1218. {
  1219. struct zone *zone;
  1220. unsigned int free = alloc_normal;
  1221. for_each_populated_zone(zone)
  1222. if (!is_highmem(zone))
  1223. free += zone_page_state(zone, NR_FREE_PAGES);
  1224. nr_pages += count_pages_for_highmem(nr_highmem);
  1225. pr_debug("PM: Normal pages needed: %u + %u, available pages: %u\n",
  1226. nr_pages, PAGES_FOR_IO, free);
  1227. return free > nr_pages + PAGES_FOR_IO;
  1228. }
  1229. #ifdef CONFIG_HIGHMEM
  1230. /**
  1231. * get_highmem_buffer - if there are some highmem pages in the suspend
  1232. * image, we may need the buffer to copy them and/or load their data.
  1233. */
  1234. static inline int get_highmem_buffer(int safe_needed)
  1235. {
  1236. buffer = get_image_page(GFP_ATOMIC | __GFP_COLD, safe_needed);
  1237. return buffer ? 0 : -ENOMEM;
  1238. }
  1239. /**
  1240. * alloc_highmem_image_pages - allocate some highmem pages for the image.
  1241. * Try to allocate as many pages as needed, but if the number of free
  1242. * highmem pages is lesser than that, allocate them all.
  1243. */
  1244. static inline unsigned int
  1245. alloc_highmem_pages(struct memory_bitmap *bm, unsigned int nr_highmem)
  1246. {
  1247. unsigned int to_alloc = count_free_highmem_pages();
  1248. if (to_alloc > nr_highmem)
  1249. to_alloc = nr_highmem;
  1250. nr_highmem -= to_alloc;
  1251. while (to_alloc-- > 0) {
  1252. struct page *page;
  1253. page = alloc_image_page(__GFP_HIGHMEM);
  1254. memory_bm_set_bit(bm, page_to_pfn(page));
  1255. }
  1256. return nr_highmem;
  1257. }
  1258. #else
  1259. static inline int get_highmem_buffer(int safe_needed) { return 0; }
  1260. static inline unsigned int
  1261. alloc_highmem_pages(struct memory_bitmap *bm, unsigned int n) { return 0; }
  1262. #endif /* CONFIG_HIGHMEM */
  1263. /**
  1264. * swsusp_alloc - allocate memory for the suspend image
  1265. *
  1266. * We first try to allocate as many highmem pages as there are
  1267. * saveable highmem pages in the system. If that fails, we allocate
  1268. * non-highmem pages for the copies of the remaining highmem ones.
  1269. *
  1270. * In this approach it is likely that the copies of highmem pages will
  1271. * also be located in the high memory, because of the way in which
  1272. * copy_data_pages() works.
  1273. */
  1274. static int
  1275. swsusp_alloc(struct memory_bitmap *orig_bm, struct memory_bitmap *copy_bm,
  1276. unsigned int nr_pages, unsigned int nr_highmem)
  1277. {
  1278. int error = 0;
  1279. if (nr_highmem > 0) {
  1280. error = get_highmem_buffer(PG_ANY);
  1281. if (error)
  1282. goto err_out;
  1283. if (nr_highmem > alloc_highmem) {
  1284. nr_highmem -= alloc_highmem;
  1285. nr_pages += alloc_highmem_pages(copy_bm, nr_highmem);
  1286. }
  1287. }
  1288. if (nr_pages > alloc_normal) {
  1289. nr_pages -= alloc_normal;
  1290. while (nr_pages-- > 0) {
  1291. struct page *page;
  1292. page = alloc_image_page(GFP_ATOMIC | __GFP_COLD);
  1293. if (!page)
  1294. goto err_out;
  1295. memory_bm_set_bit(copy_bm, page_to_pfn(page));
  1296. }
  1297. }
  1298. return 0;
  1299. err_out:
  1300. swsusp_free();
  1301. return error;
  1302. }
  1303. asmlinkage int swsusp_save(void)
  1304. {
  1305. unsigned int nr_pages, nr_highmem;
  1306. printk(KERN_INFO "PM: Creating hibernation image:\n");
  1307. drain_local_pages(NULL);
  1308. nr_pages = count_data_pages();
  1309. nr_highmem = count_highmem_pages();
  1310. printk(KERN_INFO "PM: Need to copy %u pages\n", nr_pages + nr_highmem);
  1311. if (!enough_free_mem(nr_pages, nr_highmem)) {
  1312. printk(KERN_ERR "PM: Not enough free memory\n");
  1313. return -ENOMEM;
  1314. }
  1315. if (swsusp_alloc(&orig_bm, &copy_bm, nr_pages, nr_highmem)) {
  1316. printk(KERN_ERR "PM: Memory allocation failed\n");
  1317. return -ENOMEM;
  1318. }
  1319. /* During allocating of suspend pagedir, new cold pages may appear.
  1320. * Kill them.
  1321. */
  1322. drain_local_pages(NULL);
  1323. copy_data_pages(&copy_bm, &orig_bm);
  1324. /*
  1325. * End of critical section. From now on, we can write to memory,
  1326. * but we should not touch disk. This specially means we must _not_
  1327. * touch swap space! Except we must write out our image of course.
  1328. */
  1329. nr_pages += nr_highmem;
  1330. nr_copy_pages = nr_pages;
  1331. nr_meta_pages = DIV_ROUND_UP(nr_pages * sizeof(long), PAGE_SIZE);
  1332. printk(KERN_INFO "PM: Hibernation image created (%d pages copied)\n",
  1333. nr_pages);
  1334. return 0;
  1335. }
  1336. #ifndef CONFIG_ARCH_HIBERNATION_HEADER
  1337. static int init_header_complete(struct swsusp_info *info)
  1338. {
  1339. memcpy(&info->uts, init_utsname(), sizeof(struct new_utsname));
  1340. info->version_code = LINUX_VERSION_CODE;
  1341. return 0;
  1342. }
  1343. static char *check_image_kernel(struct swsusp_info *info)
  1344. {
  1345. if (info->version_code != LINUX_VERSION_CODE)
  1346. return "kernel version";
  1347. if (strcmp(info->uts.sysname,init_utsname()->sysname))
  1348. return "system type";
  1349. if (strcmp(info->uts.release,init_utsname()->release))
  1350. return "kernel release";
  1351. if (strcmp(info->uts.version,init_utsname()->version))
  1352. return "version";
  1353. if (strcmp(info->uts.machine,init_utsname()->machine))
  1354. return "machine";
  1355. return NULL;
  1356. }
  1357. #endif /* CONFIG_ARCH_HIBERNATION_HEADER */
  1358. unsigned long snapshot_get_image_size(void)
  1359. {
  1360. return nr_copy_pages + nr_meta_pages + 1;
  1361. }
  1362. static int init_header(struct swsusp_info *info)
  1363. {
  1364. memset(info, 0, sizeof(struct swsusp_info));
  1365. info->num_physpages = num_physpages;
  1366. info->image_pages = nr_copy_pages;
  1367. info->pages = snapshot_get_image_size();
  1368. info->size = info->pages;
  1369. info->size <<= PAGE_SHIFT;
  1370. return init_header_complete(info);
  1371. }
  1372. /**
  1373. * pack_pfns - pfns corresponding to the set bits found in the bitmap @bm
  1374. * are stored in the array @buf[] (1 page at a time)
  1375. */
  1376. static inline void
  1377. pack_pfns(unsigned long *buf, struct memory_bitmap *bm)
  1378. {
  1379. int j;
  1380. for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
  1381. buf[j] = memory_bm_next_pfn(bm);
  1382. if (unlikely(buf[j] == BM_END_OF_MAP))
  1383. break;
  1384. }
  1385. }
  1386. /**
  1387. * snapshot_read_next - used for reading the system memory snapshot.
  1388. *
  1389. * On the first call to it @handle should point to a zeroed
  1390. * snapshot_handle structure. The structure gets updated and a pointer
  1391. * to it should be passed to this function every next time.
  1392. *
  1393. * On success the function returns a positive number. Then, the caller
  1394. * is allowed to read up to the returned number of bytes from the memory
  1395. * location computed by the data_of() macro.
  1396. *
  1397. * The function returns 0 to indicate the end of data stream condition,
  1398. * and a negative number is returned on error. In such cases the
  1399. * structure pointed to by @handle is not updated and should not be used
  1400. * any more.
  1401. */
  1402. int snapshot_read_next(struct snapshot_handle *handle)
  1403. {
  1404. if (handle->cur > nr_meta_pages + nr_copy_pages)
  1405. return 0;
  1406. if (!buffer) {
  1407. /* This makes the buffer be freed by swsusp_free() */
  1408. buffer = get_image_page(GFP_ATOMIC, PG_ANY);
  1409. if (!buffer)
  1410. return -ENOMEM;
  1411. }
  1412. if (!handle->cur) {
  1413. int error;
  1414. error = init_header((struct swsusp_info *)buffer);
  1415. if (error)
  1416. return error;
  1417. handle->buffer = buffer;
  1418. memory_bm_position_reset(&orig_bm);
  1419. memory_bm_position_reset(&copy_bm);
  1420. } else if (handle->cur <= nr_meta_pages) {
  1421. memset(buffer, 0, PAGE_SIZE);
  1422. pack_pfns(buffer, &orig_bm);
  1423. } else {
  1424. struct page *page;
  1425. page = pfn_to_page(memory_bm_next_pfn(&copy_bm));
  1426. if (PageHighMem(page)) {
  1427. /* Highmem pages are copied to the buffer,
  1428. * because we can't return with a kmapped
  1429. * highmem page (we may not be called again).
  1430. */
  1431. void *kaddr;
  1432. kaddr = kmap_atomic(page, KM_USER0);
  1433. memcpy(buffer, kaddr, PAGE_SIZE);
  1434. kunmap_atomic(kaddr, KM_USER0);
  1435. handle->buffer = buffer;
  1436. } else {
  1437. handle->buffer = page_address(page);
  1438. }
  1439. }
  1440. handle->cur++;
  1441. return PAGE_SIZE;
  1442. }
  1443. /**
  1444. * mark_unsafe_pages - mark the pages that cannot be used for storing
  1445. * the image during resume, because they conflict with the pages that
  1446. * had been used before suspend
  1447. */
  1448. static int mark_unsafe_pages(struct memory_bitmap *bm)
  1449. {
  1450. struct zone *zone;
  1451. unsigned long pfn, max_zone_pfn;
  1452. /* Clear page flags */
  1453. for_each_populated_zone(zone) {
  1454. max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
  1455. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
  1456. if (pfn_valid(pfn))
  1457. swsusp_unset_page_free(pfn_to_page(pfn));
  1458. }
  1459. /* Mark pages that correspond to the "original" pfns as "unsafe" */
  1460. memory_bm_position_reset(bm);
  1461. do {
  1462. pfn = memory_bm_next_pfn(bm);
  1463. if (likely(pfn != BM_END_OF_MAP)) {
  1464. if (likely(pfn_valid(pfn)))
  1465. swsusp_set_page_free(pfn_to_page(pfn));
  1466. else
  1467. return -EFAULT;
  1468. }
  1469. } while (pfn != BM_END_OF_MAP);
  1470. allocated_unsafe_pages = 0;
  1471. return 0;
  1472. }
  1473. static void
  1474. duplicate_memory_bitmap(struct memory_bitmap *dst, struct memory_bitmap *src)
  1475. {
  1476. unsigned long pfn;
  1477. memory_bm_position_reset(src);
  1478. pfn = memory_bm_next_pfn(src);
  1479. while (pfn != BM_END_OF_MAP) {
  1480. memory_bm_set_bit(dst, pfn);
  1481. pfn = memory_bm_next_pfn(src);
  1482. }
  1483. }
  1484. static int check_header(struct swsusp_info *info)
  1485. {
  1486. char *reason;
  1487. reason = check_image_kernel(info);
  1488. if (!reason && info->num_physpages != num_physpages)
  1489. reason = "memory size";
  1490. if (reason) {
  1491. printk(KERN_ERR "PM: Image mismatch: %s\n", reason);
  1492. return -EPERM;
  1493. }
  1494. return 0;
  1495. }
  1496. /**
  1497. * load header - check the image header and copy data from it
  1498. */
  1499. static int
  1500. load_header(struct swsusp_info *info)
  1501. {
  1502. int error;
  1503. restore_pblist = NULL;
  1504. error = check_header(info);
  1505. if (!error) {
  1506. nr_copy_pages = info->image_pages;
  1507. nr_meta_pages = info->pages - info->image_pages - 1;
  1508. }
  1509. return error;
  1510. }
  1511. /**
  1512. * unpack_orig_pfns - for each element of @buf[] (1 page at a time) set
  1513. * the corresponding bit in the memory bitmap @bm
  1514. */
  1515. static int unpack_orig_pfns(unsigned long *buf, struct memory_bitmap *bm)
  1516. {
  1517. int j;
  1518. for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
  1519. if (unlikely(buf[j] == BM_END_OF_MAP))
  1520. break;
  1521. if (memory_bm_pfn_present(bm, buf[j]))
  1522. memory_bm_set_bit(bm, buf[j]);
  1523. else
  1524. return -EFAULT;
  1525. }
  1526. return 0;
  1527. }
  1528. /* List of "safe" pages that may be used to store data loaded from the suspend
  1529. * image
  1530. */
  1531. static struct linked_page *safe_pages_list;
  1532. #ifdef CONFIG_HIGHMEM
  1533. /* struct highmem_pbe is used for creating the list of highmem pages that
  1534. * should be restored atomically during the resume from disk, because the page
  1535. * frames they have occupied before the suspend are in use.
  1536. */
  1537. struct highmem_pbe {
  1538. struct page *copy_page; /* data is here now */
  1539. struct page *orig_page; /* data was here before the suspend */
  1540. struct highmem_pbe *next;
  1541. };
  1542. /* List of highmem PBEs needed for restoring the highmem pages that were
  1543. * allocated before the suspend and included in the suspend image, but have
  1544. * also been allocated by the "resume" kernel, so their contents cannot be
  1545. * written directly to their "original" page frames.
  1546. */
  1547. static struct highmem_pbe *highmem_pblist;
  1548. /**
  1549. * count_highmem_image_pages - compute the number of highmem pages in the
  1550. * suspend image. The bits in the memory bitmap @bm that correspond to the
  1551. * image pages are assumed to be set.
  1552. */
  1553. static unsigned int count_highmem_image_pages(struct memory_bitmap *bm)
  1554. {
  1555. unsigned long pfn;
  1556. unsigned int cnt = 0;
  1557. memory_bm_position_reset(bm);
  1558. pfn = memory_bm_next_pfn(bm);
  1559. while (pfn != BM_END_OF_MAP) {
  1560. if (PageHighMem(pfn_to_page(pfn)))
  1561. cnt++;
  1562. pfn = memory_bm_next_pfn(bm);
  1563. }
  1564. return cnt;
  1565. }
  1566. /**
  1567. * prepare_highmem_image - try to allocate as many highmem pages as
  1568. * there are highmem image pages (@nr_highmem_p points to the variable
  1569. * containing the number of highmem image pages). The pages that are
  1570. * "safe" (ie. will not be overwritten when the suspend image is
  1571. * restored) have the corresponding bits set in @bm (it must be
  1572. * unitialized).
  1573. *
  1574. * NOTE: This function should not be called if there are no highmem
  1575. * image pages.
  1576. */
  1577. static unsigned int safe_highmem_pages;
  1578. static struct memory_bitmap *safe_highmem_bm;
  1579. static int
  1580. prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
  1581. {
  1582. unsigned int to_alloc;
  1583. if (memory_bm_create(bm, GFP_ATOMIC, PG_SAFE))
  1584. return -ENOMEM;
  1585. if (get_highmem_buffer(PG_SAFE))
  1586. return -ENOMEM;
  1587. to_alloc = count_free_highmem_pages();
  1588. if (to_alloc > *nr_highmem_p)
  1589. to_alloc = *nr_highmem_p;
  1590. else
  1591. *nr_highmem_p = to_alloc;
  1592. safe_highmem_pages = 0;
  1593. while (to_alloc-- > 0) {
  1594. struct page *page;
  1595. page = alloc_page(__GFP_HIGHMEM);
  1596. if (!swsusp_page_is_free(page)) {
  1597. /* The page is "safe", set its bit the bitmap */
  1598. memory_bm_set_bit(bm, page_to_pfn(page));
  1599. safe_highmem_pages++;
  1600. }
  1601. /* Mark the page as allocated */
  1602. swsusp_set_page_forbidden(page);
  1603. swsusp_set_page_free(page);
  1604. }
  1605. memory_bm_position_reset(bm);
  1606. safe_highmem_bm = bm;
  1607. return 0;
  1608. }
  1609. /**
  1610. * get_highmem_page_buffer - for given highmem image page find the buffer
  1611. * that suspend_write_next() should set for its caller to write to.
  1612. *
  1613. * If the page is to be saved to its "original" page frame or a copy of
  1614. * the page is to be made in the highmem, @buffer is returned. Otherwise,
  1615. * the copy of the page is to be made in normal memory, so the address of
  1616. * the copy is returned.
  1617. *
  1618. * If @buffer is returned, the caller of suspend_write_next() will write
  1619. * the page's contents to @buffer, so they will have to be copied to the
  1620. * right location on the next call to suspend_write_next() and it is done
  1621. * with the help of copy_last_highmem_page(). For this purpose, if
  1622. * @buffer is returned, @last_highmem page is set to the page to which
  1623. * the data will have to be copied from @buffer.
  1624. */
  1625. static struct page *last_highmem_page;
  1626. static void *
  1627. get_highmem_page_buffer(struct page *page, struct chain_allocator *ca)
  1628. {
  1629. struct highmem_pbe *pbe;
  1630. void *kaddr;
  1631. if (swsusp_page_is_forbidden(page) && swsusp_page_is_free(page)) {
  1632. /* We have allocated the "original" page frame and we can
  1633. * use it directly to store the loaded page.
  1634. */
  1635. last_highmem_page = page;
  1636. return buffer;
  1637. }
  1638. /* The "original" page frame has not been allocated and we have to
  1639. * use a "safe" page frame to store the loaded page.
  1640. */
  1641. pbe = chain_alloc(ca, sizeof(struct highmem_pbe));
  1642. if (!pbe) {
  1643. swsusp_free();
  1644. return ERR_PTR(-ENOMEM);
  1645. }
  1646. pbe->orig_page = page;
  1647. if (safe_highmem_pages > 0) {
  1648. struct page *tmp;
  1649. /* Copy of the page will be stored in high memory */
  1650. kaddr = buffer;
  1651. tmp = pfn_to_page(memory_bm_next_pfn(safe_highmem_bm));
  1652. safe_highmem_pages--;
  1653. last_highmem_page = tmp;
  1654. pbe->copy_page = tmp;
  1655. } else {
  1656. /* Copy of the page will be stored in normal memory */
  1657. kaddr = safe_pages_list;
  1658. safe_pages_list = safe_pages_list->next;
  1659. pbe->copy_page = virt_to_page(kaddr);
  1660. }
  1661. pbe->next = highmem_pblist;
  1662. highmem_pblist = pbe;
  1663. return kaddr;
  1664. }
  1665. /**
  1666. * copy_last_highmem_page - copy the contents of a highmem image from
  1667. * @buffer, where the caller of snapshot_write_next() has place them,
  1668. * to the right location represented by @last_highmem_page .
  1669. */
  1670. static void copy_last_highmem_page(void)
  1671. {
  1672. if (last_highmem_page) {
  1673. void *dst;
  1674. dst = kmap_atomic(last_highmem_page, KM_USER0);
  1675. memcpy(dst, buffer, PAGE_SIZE);
  1676. kunmap_atomic(dst, KM_USER0);
  1677. last_highmem_page = NULL;
  1678. }
  1679. }
  1680. static inline int last_highmem_page_copied(void)
  1681. {
  1682. return !last_highmem_page;
  1683. }
  1684. static inline void free_highmem_data(void)
  1685. {
  1686. if (safe_highmem_bm)
  1687. memory_bm_free(safe_highmem_bm, PG_UNSAFE_CLEAR);
  1688. if (buffer)
  1689. free_image_page(buffer, PG_UNSAFE_CLEAR);
  1690. }
  1691. #else
  1692. static inline int get_safe_write_buffer(void) { return 0; }
  1693. static unsigned int
  1694. count_highmem_image_pages(struct memory_bitmap *bm) { return 0; }
  1695. static inline int
  1696. prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
  1697. {
  1698. return 0;
  1699. }
  1700. static inline void *
  1701. get_highmem_page_buffer(struct page *page, struct chain_allocator *ca)
  1702. {
  1703. return ERR_PTR(-EINVAL);
  1704. }
  1705. static inline void copy_last_highmem_page(void) {}
  1706. static inline int last_highmem_page_copied(void) { return 1; }
  1707. static inline void free_highmem_data(void) {}
  1708. #endif /* CONFIG_HIGHMEM */
  1709. /**
  1710. * prepare_image - use the memory bitmap @bm to mark the pages that will
  1711. * be overwritten in the process of restoring the system memory state
  1712. * from the suspend image ("unsafe" pages) and allocate memory for the
  1713. * image.
  1714. *
  1715. * The idea is to allocate a new memory bitmap first and then allocate
  1716. * as many pages as needed for the image data, but not to assign these
  1717. * pages to specific tasks initially. Instead, we just mark them as
  1718. * allocated and create a lists of "safe" pages that will be used
  1719. * later. On systems with high memory a list of "safe" highmem pages is
  1720. * also created.
  1721. */
  1722. #define PBES_PER_LINKED_PAGE (LINKED_PAGE_DATA_SIZE / sizeof(struct pbe))
  1723. static int
  1724. prepare_image(struct memory_bitmap *new_bm, struct memory_bitmap *bm)
  1725. {
  1726. unsigned int nr_pages, nr_highmem;
  1727. struct linked_page *sp_list, *lp;
  1728. int error;
  1729. /* If there is no highmem, the buffer will not be necessary */
  1730. free_image_page(buffer, PG_UNSAFE_CLEAR);
  1731. buffer = NULL;
  1732. nr_highmem = count_highmem_image_pages(bm);
  1733. error = mark_unsafe_pages(bm);
  1734. if (error)
  1735. goto Free;
  1736. error = memory_bm_create(new_bm, GFP_ATOMIC, PG_SAFE);
  1737. if (error)
  1738. goto Free;
  1739. duplicate_memory_bitmap(new_bm, bm);
  1740. memory_bm_free(bm, PG_UNSAFE_KEEP);
  1741. if (nr_highmem > 0) {
  1742. error = prepare_highmem_image(bm, &nr_highmem);
  1743. if (error)
  1744. goto Free;
  1745. }
  1746. /* Reserve some safe pages for potential later use.
  1747. *
  1748. * NOTE: This way we make sure there will be enough safe pages for the
  1749. * chain_alloc() in get_buffer(). It is a bit wasteful, but
  1750. * nr_copy_pages cannot be greater than 50% of the memory anyway.
  1751. */
  1752. sp_list = NULL;
  1753. /* nr_copy_pages cannot be lesser than allocated_unsafe_pages */
  1754. nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
  1755. nr_pages = DIV_ROUND_UP(nr_pages, PBES_PER_LINKED_PAGE);
  1756. while (nr_pages > 0) {
  1757. lp = get_image_page(GFP_ATOMIC, PG_SAFE);
  1758. if (!lp) {
  1759. error = -ENOMEM;
  1760. goto Free;
  1761. }
  1762. lp->next = sp_list;
  1763. sp_list = lp;
  1764. nr_pages--;
  1765. }
  1766. /* Preallocate memory for the image */
  1767. safe_pages_list = NULL;
  1768. nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
  1769. while (nr_pages > 0) {
  1770. lp = (struct linked_page *)get_zeroed_page(GFP_ATOMIC);
  1771. if (!lp) {
  1772. error = -ENOMEM;
  1773. goto Free;
  1774. }
  1775. if (!swsusp_page_is_free(virt_to_page(lp))) {
  1776. /* The page is "safe", add it to the list */
  1777. lp->next = safe_pages_list;
  1778. safe_pages_list = lp;
  1779. }
  1780. /* Mark the page as allocated */
  1781. swsusp_set_page_forbidden(virt_to_page(lp));
  1782. swsusp_set_page_free(virt_to_page(lp));
  1783. nr_pages--;
  1784. }
  1785. /* Free the reserved safe pages so that chain_alloc() can use them */
  1786. while (sp_list) {
  1787. lp = sp_list->next;
  1788. free_image_page(sp_list, PG_UNSAFE_CLEAR);
  1789. sp_list = lp;
  1790. }
  1791. return 0;
  1792. Free:
  1793. swsusp_free();
  1794. return error;
  1795. }
  1796. /**
  1797. * get_buffer - compute the address that snapshot_write_next() should
  1798. * set for its caller to write to.
  1799. */
  1800. static void *get_buffer(struct memory_bitmap *bm, struct chain_allocator *ca)
  1801. {
  1802. struct pbe *pbe;
  1803. struct page *page;
  1804. unsigned long pfn = memory_bm_next_pfn(bm);
  1805. if (pfn == BM_END_OF_MAP)
  1806. return ERR_PTR(-EFAULT);
  1807. page = pfn_to_page(pfn);
  1808. if (PageHighMem(page))
  1809. return get_highmem_page_buffer(page, ca);
  1810. if (swsusp_page_is_forbidden(page) && swsusp_page_is_free(page))
  1811. /* We have allocated the "original" page frame and we can
  1812. * use it directly to store the loaded page.
  1813. */
  1814. return page_address(page);
  1815. /* The "original" page frame has not been allocated and we have to
  1816. * use a "safe" page frame to store the loaded page.
  1817. */
  1818. pbe = chain_alloc(ca, sizeof(struct pbe));
  1819. if (!pbe) {
  1820. swsusp_free();
  1821. return ERR_PTR(-ENOMEM);
  1822. }
  1823. pbe->orig_address = page_address(page);
  1824. pbe->address = safe_pages_list;
  1825. safe_pages_list = safe_pages_list->next;
  1826. pbe->next = restore_pblist;
  1827. restore_pblist = pbe;
  1828. return pbe->address;
  1829. }
  1830. /**
  1831. * snapshot_write_next - used for writing the system memory snapshot.
  1832. *
  1833. * On the first call to it @handle should point to a zeroed
  1834. * snapshot_handle structure. The structure gets updated and a pointer
  1835. * to it should be passed to this function every next time.
  1836. *
  1837. * On success the function returns a positive number. Then, the caller
  1838. * is allowed to write up to the returned number of bytes to the memory
  1839. * location computed by the data_of() macro.
  1840. *
  1841. * The function returns 0 to indicate the "end of file" condition,
  1842. * and a negative number is returned on error. In such cases the
  1843. * structure pointed to by @handle is not updated and should not be used
  1844. * any more.
  1845. */
  1846. int snapshot_write_next(struct snapshot_handle *handle)
  1847. {
  1848. static struct chain_allocator ca;
  1849. int error = 0;
  1850. /* Check if we have already loaded the entire image */
  1851. if (handle->cur > 1 && handle->cur > nr_meta_pages + nr_copy_pages)
  1852. return 0;
  1853. handle->sync_read = 1;
  1854. if (!handle->cur) {
  1855. if (!buffer)
  1856. /* This makes the buffer be freed by swsusp_free() */
  1857. buffer = get_image_page(GFP_ATOMIC, PG_ANY);
  1858. if (!buffer)
  1859. return -ENOMEM;
  1860. handle->buffer = buffer;
  1861. } else if (handle->cur == 1) {
  1862. error = load_header(buffer);
  1863. if (error)
  1864. return error;
  1865. error = memory_bm_create(&copy_bm, GFP_ATOMIC, PG_ANY);
  1866. if (error)
  1867. return error;
  1868. } else if (handle->cur <= nr_meta_pages + 1) {
  1869. error = unpack_orig_pfns(buffer, &copy_bm);
  1870. if (error)
  1871. return error;
  1872. if (handle->cur == nr_meta_pages + 1) {
  1873. error = prepare_image(&orig_bm, &copy_bm);
  1874. if (error)
  1875. return error;
  1876. chain_init(&ca, GFP_ATOMIC, PG_SAFE);
  1877. memory_bm_position_reset(&orig_bm);
  1878. restore_pblist = NULL;
  1879. handle->buffer = get_buffer(&orig_bm, &ca);
  1880. handle->sync_read = 0;
  1881. if (IS_ERR(handle->buffer))
  1882. return PTR_ERR(handle->buffer);
  1883. }
  1884. } else {
  1885. copy_last_highmem_page();
  1886. handle->buffer = get_buffer(&orig_bm, &ca);
  1887. if (IS_ERR(handle->buffer))
  1888. return PTR_ERR(handle->buffer);
  1889. if (handle->buffer != buffer)
  1890. handle->sync_read = 0;
  1891. }
  1892. handle->cur++;
  1893. return PAGE_SIZE;
  1894. }
  1895. /**
  1896. * snapshot_write_finalize - must be called after the last call to
  1897. * snapshot_write_next() in case the last page in the image happens
  1898. * to be a highmem page and its contents should be stored in the
  1899. * highmem. Additionally, it releases the memory that will not be
  1900. * used any more.
  1901. */
  1902. void snapshot_write_finalize(struct snapshot_handle *handle)
  1903. {
  1904. copy_last_highmem_page();
  1905. /* Free only if we have loaded the image entirely */
  1906. if (handle->cur > 1 && handle->cur > nr_meta_pages + nr_copy_pages) {
  1907. memory_bm_free(&orig_bm, PG_UNSAFE_CLEAR);
  1908. free_highmem_data();
  1909. }
  1910. }
  1911. int snapshot_image_loaded(struct snapshot_handle *handle)
  1912. {
  1913. return !(!nr_copy_pages || !last_highmem_page_copied() ||
  1914. handle->cur <= nr_meta_pages + nr_copy_pages);
  1915. }
  1916. #ifdef CONFIG_HIGHMEM
  1917. /* Assumes that @buf is ready and points to a "safe" page */
  1918. static inline void
  1919. swap_two_pages_data(struct page *p1, struct page *p2, void *buf)
  1920. {
  1921. void *kaddr1, *kaddr2;
  1922. kaddr1 = kmap_atomic(p1, KM_USER0);
  1923. kaddr2 = kmap_atomic(p2, KM_USER1);
  1924. memcpy(buf, kaddr1, PAGE_SIZE);
  1925. memcpy(kaddr1, kaddr2, PAGE_SIZE);
  1926. memcpy(kaddr2, buf, PAGE_SIZE);
  1927. kunmap_atomic(kaddr1, KM_USER0);
  1928. kunmap_atomic(kaddr2, KM_USER1);
  1929. }
  1930. /**
  1931. * restore_highmem - for each highmem page that was allocated before
  1932. * the suspend and included in the suspend image, and also has been
  1933. * allocated by the "resume" kernel swap its current (ie. "before
  1934. * resume") contents with the previous (ie. "before suspend") one.
  1935. *
  1936. * If the resume eventually fails, we can call this function once
  1937. * again and restore the "before resume" highmem state.
  1938. */
  1939. int restore_highmem(void)
  1940. {
  1941. struct highmem_pbe *pbe = highmem_pblist;
  1942. void *buf;
  1943. if (!pbe)
  1944. return 0;
  1945. buf = get_image_page(GFP_ATOMIC, PG_SAFE);
  1946. if (!buf)
  1947. return -ENOMEM;
  1948. while (pbe) {
  1949. swap_two_pages_data(pbe->copy_page, pbe->orig_page, buf);
  1950. pbe = pbe->next;
  1951. }
  1952. free_image_page(buf, PG_UNSAFE_CLEAR);
  1953. return 0;
  1954. }
  1955. #endif /* CONFIG_HIGHMEM */