snapshot.c 61 KB

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