snapshot.c 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/kernel/power/snapshot.c
  4. *
  5. * This file provides system snapshot/restore functionality for swsusp.
  6. *
  7. * Copyright (C) 1998-2005 Pavel Machek <[email protected]>
  8. * Copyright (C) 2006 Rafael J. Wysocki <[email protected]>
  9. */
  10. #define pr_fmt(fmt) "PM: hibernation: " fmt
  11. #include <linux/version.h>
  12. #include <linux/module.h>
  13. #include <linux/mm.h>
  14. #include <linux/suspend.h>
  15. #include <linux/delay.h>
  16. #include <linux/bitops.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/kernel.h>
  19. #include <linux/pm.h>
  20. #include <linux/device.h>
  21. #include <linux/init.h>
  22. #include <linux/memblock.h>
  23. #include <linux/nmi.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 <linux/compiler.h>
  30. #include <linux/ktime.h>
  31. #include <linux/set_memory.h>
  32. #include <linux/uaccess.h>
  33. #include <asm/mmu_context.h>
  34. #include <asm/tlbflush.h>
  35. #include <asm/io.h>
  36. #include "power.h"
  37. #if defined(CONFIG_STRICT_KERNEL_RWX) && defined(CONFIG_ARCH_HAS_SET_MEMORY)
  38. static bool hibernate_restore_protection;
  39. static bool hibernate_restore_protection_active;
  40. void enable_restore_image_protection(void)
  41. {
  42. hibernate_restore_protection = true;
  43. }
  44. static inline void hibernate_restore_protection_begin(void)
  45. {
  46. hibernate_restore_protection_active = hibernate_restore_protection;
  47. }
  48. static inline void hibernate_restore_protection_end(void)
  49. {
  50. hibernate_restore_protection_active = false;
  51. }
  52. static inline void hibernate_restore_protect_page(void *page_address)
  53. {
  54. if (hibernate_restore_protection_active)
  55. set_memory_ro((unsigned long)page_address, 1);
  56. }
  57. static inline void hibernate_restore_unprotect_page(void *page_address)
  58. {
  59. if (hibernate_restore_protection_active)
  60. set_memory_rw((unsigned long)page_address, 1);
  61. }
  62. #else
  63. static inline void hibernate_restore_protection_begin(void) {}
  64. static inline void hibernate_restore_protection_end(void) {}
  65. static inline void hibernate_restore_protect_page(void *page_address) {}
  66. static inline void hibernate_restore_unprotect_page(void *page_address) {}
  67. #endif /* CONFIG_STRICT_KERNEL_RWX && CONFIG_ARCH_HAS_SET_MEMORY */
  68. /*
  69. * The calls to set_direct_map_*() should not fail because remapping a page
  70. * here means that we only update protection bits in an existing PTE.
  71. * It is still worth to have a warning here if something changes and this
  72. * will no longer be the case.
  73. */
  74. static inline void hibernate_map_page(struct page *page)
  75. {
  76. if (IS_ENABLED(CONFIG_ARCH_HAS_SET_DIRECT_MAP)) {
  77. int ret = set_direct_map_default_noflush(page);
  78. if (ret)
  79. pr_warn_once("Failed to remap page\n");
  80. } else {
  81. debug_pagealloc_map_pages(page, 1);
  82. }
  83. }
  84. static inline void hibernate_unmap_page(struct page *page)
  85. {
  86. if (IS_ENABLED(CONFIG_ARCH_HAS_SET_DIRECT_MAP)) {
  87. unsigned long addr = (unsigned long)page_address(page);
  88. int ret = set_direct_map_invalid_noflush(page);
  89. if (ret)
  90. pr_warn_once("Failed to remap page\n");
  91. flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
  92. } else {
  93. debug_pagealloc_unmap_pages(page, 1);
  94. }
  95. }
  96. static int swsusp_page_is_free(struct page *);
  97. static void swsusp_set_page_forbidden(struct page *);
  98. static void swsusp_unset_page_forbidden(struct page *);
  99. /*
  100. * Number of bytes to reserve for memory allocations made by device drivers
  101. * from their ->freeze() and ->freeze_noirq() callbacks so that they don't
  102. * cause image creation to fail (tunable via /sys/power/reserved_size).
  103. */
  104. unsigned long reserved_size;
  105. void __init hibernate_reserved_size_init(void)
  106. {
  107. reserved_size = SPARE_PAGES * PAGE_SIZE;
  108. }
  109. /*
  110. * Preferred image size in bytes (tunable via /sys/power/image_size).
  111. * When it is set to N, swsusp will do its best to ensure the image
  112. * size will not exceed N bytes, but if that is impossible, it will
  113. * try to create the smallest image possible.
  114. */
  115. unsigned long image_size;
  116. void __init hibernate_image_size_init(void)
  117. {
  118. image_size = ((totalram_pages() * 2) / 5) * PAGE_SIZE;
  119. }
  120. /*
  121. * List of PBEs needed for restoring the pages that were allocated before
  122. * the suspend and included in the suspend image, but have also been
  123. * allocated by the "resume" kernel, so their contents cannot be written
  124. * directly to their "original" page frames.
  125. */
  126. struct pbe *restore_pblist;
  127. /* struct linked_page is used to build chains of pages */
  128. #define LINKED_PAGE_DATA_SIZE (PAGE_SIZE - sizeof(void *))
  129. struct linked_page {
  130. struct linked_page *next;
  131. char data[LINKED_PAGE_DATA_SIZE];
  132. } __packed;
  133. /*
  134. * List of "safe" pages (ie. pages that were not used by the image kernel
  135. * before hibernation) that may be used as temporary storage for image kernel
  136. * memory contents.
  137. */
  138. static struct linked_page *safe_pages_list;
  139. /* Pointer to an auxiliary buffer (1 page) */
  140. static void *buffer;
  141. #define PG_ANY 0
  142. #define PG_SAFE 1
  143. #define PG_UNSAFE_CLEAR 1
  144. #define PG_UNSAFE_KEEP 0
  145. static unsigned int allocated_unsafe_pages;
  146. /**
  147. * get_image_page - Allocate a page for a hibernation image.
  148. * @gfp_mask: GFP mask for the allocation.
  149. * @safe_needed: Get pages that were not used before hibernation (restore only)
  150. *
  151. * During image restoration, for storing the PBE list and the image data, we can
  152. * only use memory pages that do not conflict with the pages used before
  153. * hibernation. The "unsafe" pages have PageNosaveFree set and we count them
  154. * using allocated_unsafe_pages.
  155. *
  156. * Each allocated image page is marked as PageNosave and PageNosaveFree so that
  157. * swsusp_free() can release it.
  158. */
  159. static void *get_image_page(gfp_t gfp_mask, int safe_needed)
  160. {
  161. void *res;
  162. res = (void *)get_zeroed_page(gfp_mask);
  163. if (safe_needed)
  164. while (res && swsusp_page_is_free(virt_to_page(res))) {
  165. /* The page is unsafe, mark it for swsusp_free() */
  166. swsusp_set_page_forbidden(virt_to_page(res));
  167. allocated_unsafe_pages++;
  168. res = (void *)get_zeroed_page(gfp_mask);
  169. }
  170. if (res) {
  171. swsusp_set_page_forbidden(virt_to_page(res));
  172. swsusp_set_page_free(virt_to_page(res));
  173. }
  174. return res;
  175. }
  176. static void *__get_safe_page(gfp_t gfp_mask)
  177. {
  178. if (safe_pages_list) {
  179. void *ret = safe_pages_list;
  180. safe_pages_list = safe_pages_list->next;
  181. memset(ret, 0, PAGE_SIZE);
  182. return ret;
  183. }
  184. return get_image_page(gfp_mask, PG_SAFE);
  185. }
  186. unsigned long get_safe_page(gfp_t gfp_mask)
  187. {
  188. return (unsigned long)__get_safe_page(gfp_mask);
  189. }
  190. static struct page *alloc_image_page(gfp_t gfp_mask)
  191. {
  192. struct page *page;
  193. page = alloc_page(gfp_mask);
  194. if (page) {
  195. swsusp_set_page_forbidden(page);
  196. swsusp_set_page_free(page);
  197. }
  198. return page;
  199. }
  200. static void recycle_safe_page(void *page_address)
  201. {
  202. struct linked_page *lp = page_address;
  203. lp->next = safe_pages_list;
  204. safe_pages_list = lp;
  205. }
  206. /**
  207. * free_image_page - Free a page allocated for hibernation image.
  208. * @addr: Address of the page to free.
  209. * @clear_nosave_free: If set, clear the PageNosaveFree bit for the page.
  210. *
  211. * The page to free should have been allocated by get_image_page() (page flags
  212. * set by it are affected).
  213. */
  214. static inline void free_image_page(void *addr, int clear_nosave_free)
  215. {
  216. struct page *page;
  217. BUG_ON(!virt_addr_valid(addr));
  218. page = virt_to_page(addr);
  219. swsusp_unset_page_forbidden(page);
  220. if (clear_nosave_free)
  221. swsusp_unset_page_free(page);
  222. __free_page(page);
  223. }
  224. static inline void free_list_of_pages(struct linked_page *list,
  225. int clear_page_nosave)
  226. {
  227. while (list) {
  228. struct linked_page *lp = list->next;
  229. free_image_page(list, clear_page_nosave);
  230. list = lp;
  231. }
  232. }
  233. /*
  234. * struct chain_allocator is used for allocating small objects out of
  235. * a linked list of pages called 'the chain'.
  236. *
  237. * The chain grows each time when there is no room for a new object in
  238. * the current page. The allocated objects cannot be freed individually.
  239. * It is only possible to free them all at once, by freeing the entire
  240. * chain.
  241. *
  242. * NOTE: The chain allocator may be inefficient if the allocated objects
  243. * are not much smaller than PAGE_SIZE.
  244. */
  245. struct chain_allocator {
  246. struct linked_page *chain; /* the chain */
  247. unsigned int used_space; /* total size of objects allocated out
  248. of the current page */
  249. gfp_t gfp_mask; /* mask for allocating pages */
  250. int safe_needed; /* if set, only "safe" pages are allocated */
  251. };
  252. static void chain_init(struct chain_allocator *ca, gfp_t gfp_mask,
  253. int safe_needed)
  254. {
  255. ca->chain = NULL;
  256. ca->used_space = LINKED_PAGE_DATA_SIZE;
  257. ca->gfp_mask = gfp_mask;
  258. ca->safe_needed = safe_needed;
  259. }
  260. static void *chain_alloc(struct chain_allocator *ca, unsigned int size)
  261. {
  262. void *ret;
  263. if (LINKED_PAGE_DATA_SIZE - ca->used_space < size) {
  264. struct linked_page *lp;
  265. lp = ca->safe_needed ? __get_safe_page(ca->gfp_mask) :
  266. get_image_page(ca->gfp_mask, PG_ANY);
  267. if (!lp)
  268. return NULL;
  269. lp->next = ca->chain;
  270. ca->chain = lp;
  271. ca->used_space = 0;
  272. }
  273. ret = ca->chain->data + ca->used_space;
  274. ca->used_space += size;
  275. return ret;
  276. }
  277. /*
  278. * Data types related to memory bitmaps.
  279. *
  280. * Memory bitmap is a structure consisting of many linked lists of
  281. * objects. The main list's elements are of type struct zone_bitmap
  282. * and each of them corresponds to one zone. For each zone bitmap
  283. * object there is a list of objects of type struct bm_block that
  284. * represent each blocks of bitmap in which information is stored.
  285. *
  286. * struct memory_bitmap contains a pointer to the main list of zone
  287. * bitmap objects, a struct bm_position used for browsing the bitmap,
  288. * and a pointer to the list of pages used for allocating all of the
  289. * zone bitmap objects and bitmap block objects.
  290. *
  291. * NOTE: It has to be possible to lay out the bitmap in memory
  292. * using only allocations of order 0. Additionally, the bitmap is
  293. * designed to work with arbitrary number of zones (this is over the
  294. * top for now, but let's avoid making unnecessary assumptions ;-).
  295. *
  296. * struct zone_bitmap contains a pointer to a list of bitmap block
  297. * objects and a pointer to the bitmap block object that has been
  298. * most recently used for setting bits. Additionally, it contains the
  299. * PFNs that correspond to the start and end of the represented zone.
  300. *
  301. * struct bm_block contains a pointer to the memory page in which
  302. * information is stored (in the form of a block of bitmap)
  303. * It also contains the pfns that correspond to the start and end of
  304. * the represented memory area.
  305. *
  306. * The memory bitmap is organized as a radix tree to guarantee fast random
  307. * access to the bits. There is one radix tree for each zone (as returned
  308. * from create_mem_extents).
  309. *
  310. * One radix tree is represented by one struct mem_zone_bm_rtree. There are
  311. * two linked lists for the nodes of the tree, one for the inner nodes and
  312. * one for the leave nodes. The linked leave nodes are used for fast linear
  313. * access of the memory bitmap.
  314. *
  315. * The struct rtree_node represents one node of the radix tree.
  316. */
  317. #define BM_END_OF_MAP (~0UL)
  318. #define BM_BITS_PER_BLOCK (PAGE_SIZE * BITS_PER_BYTE)
  319. #define BM_BLOCK_SHIFT (PAGE_SHIFT + 3)
  320. #define BM_BLOCK_MASK ((1UL << BM_BLOCK_SHIFT) - 1)
  321. /*
  322. * struct rtree_node is a wrapper struct to link the nodes
  323. * of the rtree together for easy linear iteration over
  324. * bits and easy freeing
  325. */
  326. struct rtree_node {
  327. struct list_head list;
  328. unsigned long *data;
  329. };
  330. /*
  331. * struct mem_zone_bm_rtree represents a bitmap used for one
  332. * populated memory zone.
  333. */
  334. struct mem_zone_bm_rtree {
  335. struct list_head list; /* Link Zones together */
  336. struct list_head nodes; /* Radix Tree inner nodes */
  337. struct list_head leaves; /* Radix Tree leaves */
  338. unsigned long start_pfn; /* Zone start page frame */
  339. unsigned long end_pfn; /* Zone end page frame + 1 */
  340. struct rtree_node *rtree; /* Radix Tree Root */
  341. int levels; /* Number of Radix Tree Levels */
  342. unsigned int blocks; /* Number of Bitmap Blocks */
  343. };
  344. /* strcut bm_position is used for browsing memory bitmaps */
  345. struct bm_position {
  346. struct mem_zone_bm_rtree *zone;
  347. struct rtree_node *node;
  348. unsigned long node_pfn;
  349. unsigned long cur_pfn;
  350. int node_bit;
  351. };
  352. struct memory_bitmap {
  353. struct list_head zones;
  354. struct linked_page *p_list; /* list of pages used to store zone
  355. bitmap objects and bitmap block
  356. objects */
  357. struct bm_position cur; /* most recently used bit position */
  358. };
  359. /* Functions that operate on memory bitmaps */
  360. #define BM_ENTRIES_PER_LEVEL (PAGE_SIZE / sizeof(unsigned long))
  361. #if BITS_PER_LONG == 32
  362. #define BM_RTREE_LEVEL_SHIFT (PAGE_SHIFT - 2)
  363. #else
  364. #define BM_RTREE_LEVEL_SHIFT (PAGE_SHIFT - 3)
  365. #endif
  366. #define BM_RTREE_LEVEL_MASK ((1UL << BM_RTREE_LEVEL_SHIFT) - 1)
  367. /**
  368. * alloc_rtree_node - Allocate a new node and add it to the radix tree.
  369. * @gfp_mask: GFP mask for the allocation.
  370. * @safe_needed: Get pages not used before hibernation (restore only)
  371. * @ca: Pointer to a linked list of pages ("a chain") to allocate from
  372. * @list: Radix Tree node to add.
  373. *
  374. * This function is used to allocate inner nodes as well as the
  375. * leave nodes of the radix tree. It also adds the node to the
  376. * corresponding linked list passed in by the *list parameter.
  377. */
  378. static struct rtree_node *alloc_rtree_node(gfp_t gfp_mask, int safe_needed,
  379. struct chain_allocator *ca,
  380. struct list_head *list)
  381. {
  382. struct rtree_node *node;
  383. node = chain_alloc(ca, sizeof(struct rtree_node));
  384. if (!node)
  385. return NULL;
  386. node->data = get_image_page(gfp_mask, safe_needed);
  387. if (!node->data)
  388. return NULL;
  389. list_add_tail(&node->list, list);
  390. return node;
  391. }
  392. /**
  393. * add_rtree_block - Add a new leave node to the radix tree.
  394. *
  395. * The leave nodes need to be allocated in order to keep the leaves
  396. * linked list in order. This is guaranteed by the zone->blocks
  397. * counter.
  398. */
  399. static int add_rtree_block(struct mem_zone_bm_rtree *zone, gfp_t gfp_mask,
  400. int safe_needed, struct chain_allocator *ca)
  401. {
  402. struct rtree_node *node, *block, **dst;
  403. unsigned int levels_needed, block_nr;
  404. int i;
  405. block_nr = zone->blocks;
  406. levels_needed = 0;
  407. /* How many levels do we need for this block nr? */
  408. while (block_nr) {
  409. levels_needed += 1;
  410. block_nr >>= BM_RTREE_LEVEL_SHIFT;
  411. }
  412. /* Make sure the rtree has enough levels */
  413. for (i = zone->levels; i < levels_needed; i++) {
  414. node = alloc_rtree_node(gfp_mask, safe_needed, ca,
  415. &zone->nodes);
  416. if (!node)
  417. return -ENOMEM;
  418. node->data[0] = (unsigned long)zone->rtree;
  419. zone->rtree = node;
  420. zone->levels += 1;
  421. }
  422. /* Allocate new block */
  423. block = alloc_rtree_node(gfp_mask, safe_needed, ca, &zone->leaves);
  424. if (!block)
  425. return -ENOMEM;
  426. /* Now walk the rtree to insert the block */
  427. node = zone->rtree;
  428. dst = &zone->rtree;
  429. block_nr = zone->blocks;
  430. for (i = zone->levels; i > 0; i--) {
  431. int index;
  432. if (!node) {
  433. node = alloc_rtree_node(gfp_mask, safe_needed, ca,
  434. &zone->nodes);
  435. if (!node)
  436. return -ENOMEM;
  437. *dst = node;
  438. }
  439. index = block_nr >> ((i - 1) * BM_RTREE_LEVEL_SHIFT);
  440. index &= BM_RTREE_LEVEL_MASK;
  441. dst = (struct rtree_node **)&((*dst)->data[index]);
  442. node = *dst;
  443. }
  444. zone->blocks += 1;
  445. *dst = block;
  446. return 0;
  447. }
  448. static void free_zone_bm_rtree(struct mem_zone_bm_rtree *zone,
  449. int clear_nosave_free);
  450. /**
  451. * create_zone_bm_rtree - Create a radix tree for one zone.
  452. *
  453. * Allocated the mem_zone_bm_rtree structure and initializes it.
  454. * This function also allocated and builds the radix tree for the
  455. * zone.
  456. */
  457. static struct mem_zone_bm_rtree *create_zone_bm_rtree(gfp_t gfp_mask,
  458. int safe_needed,
  459. struct chain_allocator *ca,
  460. unsigned long start,
  461. unsigned long end)
  462. {
  463. struct mem_zone_bm_rtree *zone;
  464. unsigned int i, nr_blocks;
  465. unsigned long pages;
  466. pages = end - start;
  467. zone = chain_alloc(ca, sizeof(struct mem_zone_bm_rtree));
  468. if (!zone)
  469. return NULL;
  470. INIT_LIST_HEAD(&zone->nodes);
  471. INIT_LIST_HEAD(&zone->leaves);
  472. zone->start_pfn = start;
  473. zone->end_pfn = end;
  474. nr_blocks = DIV_ROUND_UP(pages, BM_BITS_PER_BLOCK);
  475. for (i = 0; i < nr_blocks; i++) {
  476. if (add_rtree_block(zone, gfp_mask, safe_needed, ca)) {
  477. free_zone_bm_rtree(zone, PG_UNSAFE_CLEAR);
  478. return NULL;
  479. }
  480. }
  481. return zone;
  482. }
  483. /**
  484. * free_zone_bm_rtree - Free the memory of the radix tree.
  485. *
  486. * Free all node pages of the radix tree. The mem_zone_bm_rtree
  487. * structure itself is not freed here nor are the rtree_node
  488. * structs.
  489. */
  490. static void free_zone_bm_rtree(struct mem_zone_bm_rtree *zone,
  491. int clear_nosave_free)
  492. {
  493. struct rtree_node *node;
  494. list_for_each_entry(node, &zone->nodes, list)
  495. free_image_page(node->data, clear_nosave_free);
  496. list_for_each_entry(node, &zone->leaves, list)
  497. free_image_page(node->data, clear_nosave_free);
  498. }
  499. static void memory_bm_position_reset(struct memory_bitmap *bm)
  500. {
  501. bm->cur.zone = list_entry(bm->zones.next, struct mem_zone_bm_rtree,
  502. list);
  503. bm->cur.node = list_entry(bm->cur.zone->leaves.next,
  504. struct rtree_node, list);
  505. bm->cur.node_pfn = 0;
  506. bm->cur.cur_pfn = BM_END_OF_MAP;
  507. bm->cur.node_bit = 0;
  508. }
  509. static void memory_bm_free(struct memory_bitmap *bm, int clear_nosave_free);
  510. struct mem_extent {
  511. struct list_head hook;
  512. unsigned long start;
  513. unsigned long end;
  514. };
  515. /**
  516. * free_mem_extents - Free a list of memory extents.
  517. * @list: List of extents to free.
  518. */
  519. static void free_mem_extents(struct list_head *list)
  520. {
  521. struct mem_extent *ext, *aux;
  522. list_for_each_entry_safe(ext, aux, list, hook) {
  523. list_del(&ext->hook);
  524. kfree(ext);
  525. }
  526. }
  527. /**
  528. * create_mem_extents - Create a list of memory extents.
  529. * @list: List to put the extents into.
  530. * @gfp_mask: Mask to use for memory allocations.
  531. *
  532. * The extents represent contiguous ranges of PFNs.
  533. */
  534. static int create_mem_extents(struct list_head *list, gfp_t gfp_mask)
  535. {
  536. struct zone *zone;
  537. INIT_LIST_HEAD(list);
  538. for_each_populated_zone(zone) {
  539. unsigned long zone_start, zone_end;
  540. struct mem_extent *ext, *cur, *aux;
  541. zone_start = zone->zone_start_pfn;
  542. zone_end = zone_end_pfn(zone);
  543. list_for_each_entry(ext, list, hook)
  544. if (zone_start <= ext->end)
  545. break;
  546. if (&ext->hook == list || zone_end < ext->start) {
  547. /* New extent is necessary */
  548. struct mem_extent *new_ext;
  549. new_ext = kzalloc(sizeof(struct mem_extent), gfp_mask);
  550. if (!new_ext) {
  551. free_mem_extents(list);
  552. return -ENOMEM;
  553. }
  554. new_ext->start = zone_start;
  555. new_ext->end = zone_end;
  556. list_add_tail(&new_ext->hook, &ext->hook);
  557. continue;
  558. }
  559. /* Merge this zone's range of PFNs with the existing one */
  560. if (zone_start < ext->start)
  561. ext->start = zone_start;
  562. if (zone_end > ext->end)
  563. ext->end = zone_end;
  564. /* More merging may be possible */
  565. cur = ext;
  566. list_for_each_entry_safe_continue(cur, aux, list, hook) {
  567. if (zone_end < cur->start)
  568. break;
  569. if (zone_end < cur->end)
  570. ext->end = cur->end;
  571. list_del(&cur->hook);
  572. kfree(cur);
  573. }
  574. }
  575. return 0;
  576. }
  577. /**
  578. * memory_bm_create - Allocate memory for a memory bitmap.
  579. */
  580. static int memory_bm_create(struct memory_bitmap *bm, gfp_t gfp_mask,
  581. int safe_needed)
  582. {
  583. struct chain_allocator ca;
  584. struct list_head mem_extents;
  585. struct mem_extent *ext;
  586. int error;
  587. chain_init(&ca, gfp_mask, safe_needed);
  588. INIT_LIST_HEAD(&bm->zones);
  589. error = create_mem_extents(&mem_extents, gfp_mask);
  590. if (error)
  591. return error;
  592. list_for_each_entry(ext, &mem_extents, hook) {
  593. struct mem_zone_bm_rtree *zone;
  594. zone = create_zone_bm_rtree(gfp_mask, safe_needed, &ca,
  595. ext->start, ext->end);
  596. if (!zone) {
  597. error = -ENOMEM;
  598. goto Error;
  599. }
  600. list_add_tail(&zone->list, &bm->zones);
  601. }
  602. bm->p_list = ca.chain;
  603. memory_bm_position_reset(bm);
  604. Exit:
  605. free_mem_extents(&mem_extents);
  606. return error;
  607. Error:
  608. bm->p_list = ca.chain;
  609. memory_bm_free(bm, PG_UNSAFE_CLEAR);
  610. goto Exit;
  611. }
  612. /**
  613. * memory_bm_free - Free memory occupied by the memory bitmap.
  614. * @bm: Memory bitmap.
  615. */
  616. static void memory_bm_free(struct memory_bitmap *bm, int clear_nosave_free)
  617. {
  618. struct mem_zone_bm_rtree *zone;
  619. list_for_each_entry(zone, &bm->zones, list)
  620. free_zone_bm_rtree(zone, clear_nosave_free);
  621. free_list_of_pages(bm->p_list, clear_nosave_free);
  622. INIT_LIST_HEAD(&bm->zones);
  623. }
  624. /**
  625. * memory_bm_find_bit - Find the bit for a given PFN in a memory bitmap.
  626. *
  627. * Find the bit in memory bitmap @bm that corresponds to the given PFN.
  628. * The cur.zone, cur.block and cur.node_pfn members of @bm are updated.
  629. *
  630. * Walk the radix tree to find the page containing the bit that represents @pfn
  631. * and return the position of the bit in @addr and @bit_nr.
  632. */
  633. static int memory_bm_find_bit(struct memory_bitmap *bm, unsigned long pfn,
  634. void **addr, unsigned int *bit_nr)
  635. {
  636. struct mem_zone_bm_rtree *curr, *zone;
  637. struct rtree_node *node;
  638. int i, block_nr;
  639. zone = bm->cur.zone;
  640. if (pfn >= zone->start_pfn && pfn < zone->end_pfn)
  641. goto zone_found;
  642. zone = NULL;
  643. /* Find the right zone */
  644. list_for_each_entry(curr, &bm->zones, list) {
  645. if (pfn >= curr->start_pfn && pfn < curr->end_pfn) {
  646. zone = curr;
  647. break;
  648. }
  649. }
  650. if (!zone)
  651. return -EFAULT;
  652. zone_found:
  653. /*
  654. * We have found the zone. Now walk the radix tree to find the leaf node
  655. * for our PFN.
  656. */
  657. /*
  658. * If the zone we wish to scan is the current zone and the
  659. * pfn falls into the current node then we do not need to walk
  660. * the tree.
  661. */
  662. node = bm->cur.node;
  663. if (zone == bm->cur.zone &&
  664. ((pfn - zone->start_pfn) & ~BM_BLOCK_MASK) == bm->cur.node_pfn)
  665. goto node_found;
  666. node = zone->rtree;
  667. block_nr = (pfn - zone->start_pfn) >> BM_BLOCK_SHIFT;
  668. for (i = zone->levels; i > 0; i--) {
  669. int index;
  670. index = block_nr >> ((i - 1) * BM_RTREE_LEVEL_SHIFT);
  671. index &= BM_RTREE_LEVEL_MASK;
  672. BUG_ON(node->data[index] == 0);
  673. node = (struct rtree_node *)node->data[index];
  674. }
  675. node_found:
  676. /* Update last position */
  677. bm->cur.zone = zone;
  678. bm->cur.node = node;
  679. bm->cur.node_pfn = (pfn - zone->start_pfn) & ~BM_BLOCK_MASK;
  680. bm->cur.cur_pfn = pfn;
  681. /* Set return values */
  682. *addr = node->data;
  683. *bit_nr = (pfn - zone->start_pfn) & BM_BLOCK_MASK;
  684. return 0;
  685. }
  686. static void memory_bm_set_bit(struct memory_bitmap *bm, unsigned long pfn)
  687. {
  688. void *addr;
  689. unsigned int bit;
  690. int error;
  691. error = memory_bm_find_bit(bm, pfn, &addr, &bit);
  692. BUG_ON(error);
  693. set_bit(bit, addr);
  694. }
  695. static int mem_bm_set_bit_check(struct memory_bitmap *bm, unsigned long pfn)
  696. {
  697. void *addr;
  698. unsigned int bit;
  699. int error;
  700. error = memory_bm_find_bit(bm, pfn, &addr, &bit);
  701. if (!error)
  702. set_bit(bit, addr);
  703. return error;
  704. }
  705. static void memory_bm_clear_bit(struct memory_bitmap *bm, unsigned long pfn)
  706. {
  707. void *addr;
  708. unsigned int bit;
  709. int error;
  710. error = memory_bm_find_bit(bm, pfn, &addr, &bit);
  711. BUG_ON(error);
  712. clear_bit(bit, addr);
  713. }
  714. static void memory_bm_clear_current(struct memory_bitmap *bm)
  715. {
  716. int bit;
  717. bit = max(bm->cur.node_bit - 1, 0);
  718. clear_bit(bit, bm->cur.node->data);
  719. }
  720. static unsigned long memory_bm_get_current(struct memory_bitmap *bm)
  721. {
  722. return bm->cur.cur_pfn;
  723. }
  724. static int memory_bm_test_bit(struct memory_bitmap *bm, unsigned long pfn)
  725. {
  726. void *addr;
  727. unsigned int bit;
  728. int error;
  729. error = memory_bm_find_bit(bm, pfn, &addr, &bit);
  730. BUG_ON(error);
  731. return test_bit(bit, addr);
  732. }
  733. static bool memory_bm_pfn_present(struct memory_bitmap *bm, unsigned long pfn)
  734. {
  735. void *addr;
  736. unsigned int bit;
  737. return !memory_bm_find_bit(bm, pfn, &addr, &bit);
  738. }
  739. /*
  740. * rtree_next_node - Jump to the next leaf node.
  741. *
  742. * Set the position to the beginning of the next node in the
  743. * memory bitmap. This is either the next node in the current
  744. * zone's radix tree or the first node in the radix tree of the
  745. * next zone.
  746. *
  747. * Return true if there is a next node, false otherwise.
  748. */
  749. static bool rtree_next_node(struct memory_bitmap *bm)
  750. {
  751. if (!list_is_last(&bm->cur.node->list, &bm->cur.zone->leaves)) {
  752. bm->cur.node = list_entry(bm->cur.node->list.next,
  753. struct rtree_node, list);
  754. bm->cur.node_pfn += BM_BITS_PER_BLOCK;
  755. bm->cur.node_bit = 0;
  756. touch_softlockup_watchdog();
  757. return true;
  758. }
  759. /* No more nodes, goto next zone */
  760. if (!list_is_last(&bm->cur.zone->list, &bm->zones)) {
  761. bm->cur.zone = list_entry(bm->cur.zone->list.next,
  762. struct mem_zone_bm_rtree, list);
  763. bm->cur.node = list_entry(bm->cur.zone->leaves.next,
  764. struct rtree_node, list);
  765. bm->cur.node_pfn = 0;
  766. bm->cur.node_bit = 0;
  767. return true;
  768. }
  769. /* No more zones */
  770. return false;
  771. }
  772. /**
  773. * memory_bm_next_pfn - Find the next set bit in a memory bitmap.
  774. * @bm: Memory bitmap.
  775. *
  776. * Starting from the last returned position this function searches for the next
  777. * set bit in @bm and returns the PFN represented by it. If no more bits are
  778. * set, BM_END_OF_MAP is returned.
  779. *
  780. * It is required to run memory_bm_position_reset() before the first call to
  781. * this function for the given memory bitmap.
  782. */
  783. static unsigned long memory_bm_next_pfn(struct memory_bitmap *bm)
  784. {
  785. unsigned long bits, pfn, pages;
  786. int bit;
  787. do {
  788. pages = bm->cur.zone->end_pfn - bm->cur.zone->start_pfn;
  789. bits = min(pages - bm->cur.node_pfn, BM_BITS_PER_BLOCK);
  790. bit = find_next_bit(bm->cur.node->data, bits,
  791. bm->cur.node_bit);
  792. if (bit < bits) {
  793. pfn = bm->cur.zone->start_pfn + bm->cur.node_pfn + bit;
  794. bm->cur.node_bit = bit + 1;
  795. bm->cur.cur_pfn = pfn;
  796. return pfn;
  797. }
  798. } while (rtree_next_node(bm));
  799. bm->cur.cur_pfn = BM_END_OF_MAP;
  800. return BM_END_OF_MAP;
  801. }
  802. /*
  803. * This structure represents a range of page frames the contents of which
  804. * should not be saved during hibernation.
  805. */
  806. struct nosave_region {
  807. struct list_head list;
  808. unsigned long start_pfn;
  809. unsigned long end_pfn;
  810. };
  811. static LIST_HEAD(nosave_regions);
  812. static void recycle_zone_bm_rtree(struct mem_zone_bm_rtree *zone)
  813. {
  814. struct rtree_node *node;
  815. list_for_each_entry(node, &zone->nodes, list)
  816. recycle_safe_page(node->data);
  817. list_for_each_entry(node, &zone->leaves, list)
  818. recycle_safe_page(node->data);
  819. }
  820. static void memory_bm_recycle(struct memory_bitmap *bm)
  821. {
  822. struct mem_zone_bm_rtree *zone;
  823. struct linked_page *p_list;
  824. list_for_each_entry(zone, &bm->zones, list)
  825. recycle_zone_bm_rtree(zone);
  826. p_list = bm->p_list;
  827. while (p_list) {
  828. struct linked_page *lp = p_list;
  829. p_list = lp->next;
  830. recycle_safe_page(lp);
  831. }
  832. }
  833. /**
  834. * register_nosave_region - Register a region of unsaveable memory.
  835. *
  836. * Register a range of page frames the contents of which should not be saved
  837. * during hibernation (to be used in the early initialization code).
  838. */
  839. void __init register_nosave_region(unsigned long start_pfn, unsigned long end_pfn)
  840. {
  841. struct nosave_region *region;
  842. if (start_pfn >= end_pfn)
  843. return;
  844. if (!list_empty(&nosave_regions)) {
  845. /* Try to extend the previous region (they should be sorted) */
  846. region = list_entry(nosave_regions.prev,
  847. struct nosave_region, list);
  848. if (region->end_pfn == start_pfn) {
  849. region->end_pfn = end_pfn;
  850. goto Report;
  851. }
  852. }
  853. /* This allocation cannot fail */
  854. region = memblock_alloc(sizeof(struct nosave_region),
  855. SMP_CACHE_BYTES);
  856. if (!region)
  857. panic("%s: Failed to allocate %zu bytes\n", __func__,
  858. sizeof(struct nosave_region));
  859. region->start_pfn = start_pfn;
  860. region->end_pfn = end_pfn;
  861. list_add_tail(&region->list, &nosave_regions);
  862. Report:
  863. pr_info("Registered nosave memory: [mem %#010llx-%#010llx]\n",
  864. (unsigned long long) start_pfn << PAGE_SHIFT,
  865. ((unsigned long long) end_pfn << PAGE_SHIFT) - 1);
  866. }
  867. /*
  868. * Set bits in this map correspond to the page frames the contents of which
  869. * should not be saved during the suspend.
  870. */
  871. static struct memory_bitmap *forbidden_pages_map;
  872. /* Set bits in this map correspond to free page frames. */
  873. static struct memory_bitmap *free_pages_map;
  874. /*
  875. * Each page frame allocated for creating the image is marked by setting the
  876. * corresponding bits in forbidden_pages_map and free_pages_map simultaneously
  877. */
  878. void swsusp_set_page_free(struct page *page)
  879. {
  880. if (free_pages_map)
  881. memory_bm_set_bit(free_pages_map, page_to_pfn(page));
  882. }
  883. static int swsusp_page_is_free(struct page *page)
  884. {
  885. return free_pages_map ?
  886. memory_bm_test_bit(free_pages_map, page_to_pfn(page)) : 0;
  887. }
  888. void swsusp_unset_page_free(struct page *page)
  889. {
  890. if (free_pages_map)
  891. memory_bm_clear_bit(free_pages_map, page_to_pfn(page));
  892. }
  893. static void swsusp_set_page_forbidden(struct page *page)
  894. {
  895. if (forbidden_pages_map)
  896. memory_bm_set_bit(forbidden_pages_map, page_to_pfn(page));
  897. }
  898. int swsusp_page_is_forbidden(struct page *page)
  899. {
  900. return forbidden_pages_map ?
  901. memory_bm_test_bit(forbidden_pages_map, page_to_pfn(page)) : 0;
  902. }
  903. static void swsusp_unset_page_forbidden(struct page *page)
  904. {
  905. if (forbidden_pages_map)
  906. memory_bm_clear_bit(forbidden_pages_map, page_to_pfn(page));
  907. }
  908. /**
  909. * mark_nosave_pages - Mark pages that should not be saved.
  910. * @bm: Memory bitmap.
  911. *
  912. * Set the bits in @bm that correspond to the page frames the contents of which
  913. * should not be saved.
  914. */
  915. static void mark_nosave_pages(struct memory_bitmap *bm)
  916. {
  917. struct nosave_region *region;
  918. if (list_empty(&nosave_regions))
  919. return;
  920. list_for_each_entry(region, &nosave_regions, list) {
  921. unsigned long pfn;
  922. pr_debug("Marking nosave pages: [mem %#010llx-%#010llx]\n",
  923. (unsigned long long) region->start_pfn << PAGE_SHIFT,
  924. ((unsigned long long) region->end_pfn << PAGE_SHIFT)
  925. - 1);
  926. for (pfn = region->start_pfn; pfn < region->end_pfn; pfn++)
  927. if (pfn_valid(pfn)) {
  928. /*
  929. * It is safe to ignore the result of
  930. * mem_bm_set_bit_check() here, since we won't
  931. * touch the PFNs for which the error is
  932. * returned anyway.
  933. */
  934. mem_bm_set_bit_check(bm, pfn);
  935. }
  936. }
  937. }
  938. /**
  939. * create_basic_memory_bitmaps - Create bitmaps to hold basic page information.
  940. *
  941. * Create bitmaps needed for marking page frames that should not be saved and
  942. * free page frames. The forbidden_pages_map and free_pages_map pointers are
  943. * only modified if everything goes well, because we don't want the bits to be
  944. * touched before both bitmaps are set up.
  945. */
  946. int create_basic_memory_bitmaps(void)
  947. {
  948. struct memory_bitmap *bm1, *bm2;
  949. int error = 0;
  950. if (forbidden_pages_map && free_pages_map)
  951. return 0;
  952. else
  953. BUG_ON(forbidden_pages_map || free_pages_map);
  954. bm1 = kzalloc(sizeof(struct memory_bitmap), GFP_KERNEL);
  955. if (!bm1)
  956. return -ENOMEM;
  957. error = memory_bm_create(bm1, GFP_KERNEL, PG_ANY);
  958. if (error)
  959. goto Free_first_object;
  960. bm2 = kzalloc(sizeof(struct memory_bitmap), GFP_KERNEL);
  961. if (!bm2)
  962. goto Free_first_bitmap;
  963. error = memory_bm_create(bm2, GFP_KERNEL, PG_ANY);
  964. if (error)
  965. goto Free_second_object;
  966. forbidden_pages_map = bm1;
  967. free_pages_map = bm2;
  968. mark_nosave_pages(forbidden_pages_map);
  969. pr_debug("Basic memory bitmaps created\n");
  970. return 0;
  971. Free_second_object:
  972. kfree(bm2);
  973. Free_first_bitmap:
  974. memory_bm_free(bm1, PG_UNSAFE_CLEAR);
  975. Free_first_object:
  976. kfree(bm1);
  977. return -ENOMEM;
  978. }
  979. /**
  980. * free_basic_memory_bitmaps - Free memory bitmaps holding basic information.
  981. *
  982. * Free memory bitmaps allocated by create_basic_memory_bitmaps(). The
  983. * auxiliary pointers are necessary so that the bitmaps themselves are not
  984. * referred to while they are being freed.
  985. */
  986. void free_basic_memory_bitmaps(void)
  987. {
  988. struct memory_bitmap *bm1, *bm2;
  989. if (WARN_ON(!(forbidden_pages_map && free_pages_map)))
  990. return;
  991. bm1 = forbidden_pages_map;
  992. bm2 = free_pages_map;
  993. forbidden_pages_map = NULL;
  994. free_pages_map = NULL;
  995. memory_bm_free(bm1, PG_UNSAFE_CLEAR);
  996. kfree(bm1);
  997. memory_bm_free(bm2, PG_UNSAFE_CLEAR);
  998. kfree(bm2);
  999. pr_debug("Basic memory bitmaps freed\n");
  1000. }
  1001. static void clear_or_poison_free_page(struct page *page)
  1002. {
  1003. if (page_poisoning_enabled_static())
  1004. __kernel_poison_pages(page, 1);
  1005. else if (want_init_on_free())
  1006. clear_highpage(page);
  1007. }
  1008. void clear_or_poison_free_pages(void)
  1009. {
  1010. struct memory_bitmap *bm = free_pages_map;
  1011. unsigned long pfn;
  1012. if (WARN_ON(!(free_pages_map)))
  1013. return;
  1014. if (page_poisoning_enabled() || want_init_on_free()) {
  1015. memory_bm_position_reset(bm);
  1016. pfn = memory_bm_next_pfn(bm);
  1017. while (pfn != BM_END_OF_MAP) {
  1018. if (pfn_valid(pfn))
  1019. clear_or_poison_free_page(pfn_to_page(pfn));
  1020. pfn = memory_bm_next_pfn(bm);
  1021. }
  1022. memory_bm_position_reset(bm);
  1023. pr_info("free pages cleared after restore\n");
  1024. }
  1025. }
  1026. /**
  1027. * snapshot_additional_pages - Estimate the number of extra pages needed.
  1028. * @zone: Memory zone to carry out the computation for.
  1029. *
  1030. * Estimate the number of additional pages needed for setting up a hibernation
  1031. * image data structures for @zone (usually, the returned value is greater than
  1032. * the exact number).
  1033. */
  1034. unsigned int snapshot_additional_pages(struct zone *zone)
  1035. {
  1036. unsigned int rtree, nodes;
  1037. rtree = nodes = DIV_ROUND_UP(zone->spanned_pages, BM_BITS_PER_BLOCK);
  1038. rtree += DIV_ROUND_UP(rtree * sizeof(struct rtree_node),
  1039. LINKED_PAGE_DATA_SIZE);
  1040. while (nodes > 1) {
  1041. nodes = DIV_ROUND_UP(nodes, BM_ENTRIES_PER_LEVEL);
  1042. rtree += nodes;
  1043. }
  1044. return 2 * rtree;
  1045. }
  1046. #ifdef CONFIG_HIGHMEM
  1047. /**
  1048. * count_free_highmem_pages - Compute the total number of free highmem pages.
  1049. *
  1050. * The returned number is system-wide.
  1051. */
  1052. static unsigned int count_free_highmem_pages(void)
  1053. {
  1054. struct zone *zone;
  1055. unsigned int cnt = 0;
  1056. for_each_populated_zone(zone)
  1057. if (is_highmem(zone))
  1058. cnt += zone_page_state(zone, NR_FREE_PAGES);
  1059. return cnt;
  1060. }
  1061. /**
  1062. * saveable_highmem_page - Check if a highmem page is saveable.
  1063. *
  1064. * Determine whether a highmem page should be included in a hibernation image.
  1065. *
  1066. * We should save the page if it isn't Nosave or NosaveFree, or Reserved,
  1067. * and it isn't part of a free chunk of pages.
  1068. */
  1069. static struct page *saveable_highmem_page(struct zone *zone, unsigned long pfn)
  1070. {
  1071. struct page *page;
  1072. if (!pfn_valid(pfn))
  1073. return NULL;
  1074. page = pfn_to_online_page(pfn);
  1075. if (!page || page_zone(page) != zone)
  1076. return NULL;
  1077. BUG_ON(!PageHighMem(page));
  1078. if (swsusp_page_is_forbidden(page) || swsusp_page_is_free(page))
  1079. return NULL;
  1080. if (PageReserved(page) || PageOffline(page))
  1081. return NULL;
  1082. if (page_is_guard(page))
  1083. return NULL;
  1084. return page;
  1085. }
  1086. /**
  1087. * count_highmem_pages - Compute the total number of saveable highmem pages.
  1088. */
  1089. static unsigned int count_highmem_pages(void)
  1090. {
  1091. struct zone *zone;
  1092. unsigned int n = 0;
  1093. for_each_populated_zone(zone) {
  1094. unsigned long pfn, max_zone_pfn;
  1095. if (!is_highmem(zone))
  1096. continue;
  1097. mark_free_pages(zone);
  1098. max_zone_pfn = zone_end_pfn(zone);
  1099. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
  1100. if (saveable_highmem_page(zone, pfn))
  1101. n++;
  1102. }
  1103. return n;
  1104. }
  1105. #else
  1106. static inline void *saveable_highmem_page(struct zone *z, unsigned long p)
  1107. {
  1108. return NULL;
  1109. }
  1110. #endif /* CONFIG_HIGHMEM */
  1111. /**
  1112. * saveable_page - Check if the given page is saveable.
  1113. *
  1114. * Determine whether a non-highmem page should be included in a hibernation
  1115. * image.
  1116. *
  1117. * We should save the page if it isn't Nosave, and is not in the range
  1118. * of pages statically defined as 'unsaveable', and it isn't part of
  1119. * a free chunk of pages.
  1120. */
  1121. static struct page *saveable_page(struct zone *zone, unsigned long pfn)
  1122. {
  1123. struct page *page;
  1124. if (!pfn_valid(pfn))
  1125. return NULL;
  1126. page = pfn_to_online_page(pfn);
  1127. if (!page || page_zone(page) != zone)
  1128. return NULL;
  1129. BUG_ON(PageHighMem(page));
  1130. if (swsusp_page_is_forbidden(page) || swsusp_page_is_free(page))
  1131. return NULL;
  1132. if (PageOffline(page))
  1133. return NULL;
  1134. if (PageReserved(page)
  1135. && (!kernel_page_present(page) || pfn_is_nosave(pfn)))
  1136. return NULL;
  1137. if (page_is_guard(page))
  1138. return NULL;
  1139. return page;
  1140. }
  1141. /**
  1142. * count_data_pages - Compute the total number of saveable non-highmem pages.
  1143. */
  1144. static unsigned int count_data_pages(void)
  1145. {
  1146. struct zone *zone;
  1147. unsigned long pfn, max_zone_pfn;
  1148. unsigned int n = 0;
  1149. for_each_populated_zone(zone) {
  1150. if (is_highmem(zone))
  1151. continue;
  1152. mark_free_pages(zone);
  1153. max_zone_pfn = zone_end_pfn(zone);
  1154. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
  1155. if (saveable_page(zone, pfn))
  1156. n++;
  1157. }
  1158. return n;
  1159. }
  1160. /*
  1161. * This is needed, because copy_page and memcpy are not usable for copying
  1162. * task structs. Returns true if the page was filled with only zeros,
  1163. * otherwise false.
  1164. */
  1165. static inline bool do_copy_page(long *dst, long *src)
  1166. {
  1167. long z = 0;
  1168. int n;
  1169. for (n = PAGE_SIZE / sizeof(long); n; n--) {
  1170. z |= *src;
  1171. *dst++ = *src++;
  1172. }
  1173. return !z;
  1174. }
  1175. /**
  1176. * safe_copy_page - Copy a page in a safe way.
  1177. *
  1178. * Check if the page we are going to copy is marked as present in the kernel
  1179. * page tables. This always is the case if CONFIG_DEBUG_PAGEALLOC or
  1180. * CONFIG_ARCH_HAS_SET_DIRECT_MAP is not set. In that case kernel_page_present()
  1181. * always returns 'true'. Returns true if the page was entirely composed of
  1182. * zeros, otherwise it will return false.
  1183. */
  1184. static bool safe_copy_page(void *dst, struct page *s_page)
  1185. {
  1186. bool zeros_only;
  1187. if (kernel_page_present(s_page)) {
  1188. zeros_only = do_copy_page(dst, page_address(s_page));
  1189. } else {
  1190. hibernate_map_page(s_page);
  1191. zeros_only = do_copy_page(dst, page_address(s_page));
  1192. hibernate_unmap_page(s_page);
  1193. }
  1194. return zeros_only;
  1195. }
  1196. #ifdef CONFIG_HIGHMEM
  1197. static inline struct page *page_is_saveable(struct zone *zone, unsigned long pfn)
  1198. {
  1199. return is_highmem(zone) ?
  1200. saveable_highmem_page(zone, pfn) : saveable_page(zone, pfn);
  1201. }
  1202. static bool copy_data_page(unsigned long dst_pfn, unsigned long src_pfn)
  1203. {
  1204. struct page *s_page, *d_page;
  1205. void *src, *dst;
  1206. bool zeros_only;
  1207. s_page = pfn_to_page(src_pfn);
  1208. d_page = pfn_to_page(dst_pfn);
  1209. if (PageHighMem(s_page)) {
  1210. src = kmap_atomic(s_page);
  1211. dst = kmap_atomic(d_page);
  1212. zeros_only = do_copy_page(dst, src);
  1213. kunmap_atomic(dst);
  1214. kunmap_atomic(src);
  1215. } else {
  1216. if (PageHighMem(d_page)) {
  1217. /*
  1218. * The page pointed to by src may contain some kernel
  1219. * data modified by kmap_atomic()
  1220. */
  1221. zeros_only = safe_copy_page(buffer, s_page);
  1222. dst = kmap_atomic(d_page);
  1223. copy_page(dst, buffer);
  1224. kunmap_atomic(dst);
  1225. } else {
  1226. zeros_only = safe_copy_page(page_address(d_page), s_page);
  1227. }
  1228. }
  1229. return zeros_only;
  1230. }
  1231. #else
  1232. #define page_is_saveable(zone, pfn) saveable_page(zone, pfn)
  1233. static inline int copy_data_page(unsigned long dst_pfn, unsigned long src_pfn)
  1234. {
  1235. return safe_copy_page(page_address(pfn_to_page(dst_pfn)),
  1236. pfn_to_page(src_pfn));
  1237. }
  1238. #endif /* CONFIG_HIGHMEM */
  1239. /*
  1240. * Copy data pages will copy all pages into pages pulled from the copy_bm.
  1241. * If a page was entirely filled with zeros it will be marked in the zero_bm.
  1242. *
  1243. * Returns the number of pages copied.
  1244. */
  1245. static unsigned long copy_data_pages(struct memory_bitmap *copy_bm,
  1246. struct memory_bitmap *orig_bm,
  1247. struct memory_bitmap *zero_bm)
  1248. {
  1249. unsigned long copied_pages = 0;
  1250. struct zone *zone;
  1251. unsigned long pfn, copy_pfn;
  1252. for_each_populated_zone(zone) {
  1253. unsigned long max_zone_pfn;
  1254. mark_free_pages(zone);
  1255. max_zone_pfn = zone_end_pfn(zone);
  1256. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
  1257. if (page_is_saveable(zone, pfn))
  1258. memory_bm_set_bit(orig_bm, pfn);
  1259. }
  1260. memory_bm_position_reset(orig_bm);
  1261. memory_bm_position_reset(copy_bm);
  1262. copy_pfn = memory_bm_next_pfn(copy_bm);
  1263. for(;;) {
  1264. pfn = memory_bm_next_pfn(orig_bm);
  1265. if (unlikely(pfn == BM_END_OF_MAP))
  1266. break;
  1267. if (copy_data_page(copy_pfn, pfn)) {
  1268. memory_bm_set_bit(zero_bm, pfn);
  1269. /* Use this copy_pfn for a page that is not full of zeros */
  1270. continue;
  1271. }
  1272. copied_pages++;
  1273. copy_pfn = memory_bm_next_pfn(copy_bm);
  1274. }
  1275. return copied_pages;
  1276. }
  1277. /* Total number of image pages */
  1278. static unsigned int nr_copy_pages;
  1279. /* Number of pages needed for saving the original pfns of the image pages */
  1280. static unsigned int nr_meta_pages;
  1281. /* Number of zero pages */
  1282. static unsigned int nr_zero_pages;
  1283. /*
  1284. * Numbers of normal and highmem page frames allocated for hibernation image
  1285. * before suspending devices.
  1286. */
  1287. static unsigned int alloc_normal, alloc_highmem;
  1288. /*
  1289. * Memory bitmap used for marking saveable pages (during hibernation) or
  1290. * hibernation image pages (during restore)
  1291. */
  1292. static struct memory_bitmap orig_bm;
  1293. /*
  1294. * Memory bitmap used during hibernation for marking allocated page frames that
  1295. * will contain copies of saveable pages. During restore it is initially used
  1296. * for marking hibernation image pages, but then the set bits from it are
  1297. * duplicated in @orig_bm and it is released. On highmem systems it is next
  1298. * used for marking "safe" highmem pages, but it has to be reinitialized for
  1299. * this purpose.
  1300. */
  1301. static struct memory_bitmap copy_bm;
  1302. /* Memory bitmap which tracks which saveable pages were zero filled. */
  1303. static struct memory_bitmap zero_bm;
  1304. /**
  1305. * swsusp_free - Free pages allocated for hibernation image.
  1306. *
  1307. * Image pages are allocated before snapshot creation, so they need to be
  1308. * released after resume.
  1309. */
  1310. void swsusp_free(void)
  1311. {
  1312. unsigned long fb_pfn, fr_pfn;
  1313. if (!forbidden_pages_map || !free_pages_map)
  1314. goto out;
  1315. memory_bm_position_reset(forbidden_pages_map);
  1316. memory_bm_position_reset(free_pages_map);
  1317. loop:
  1318. fr_pfn = memory_bm_next_pfn(free_pages_map);
  1319. fb_pfn = memory_bm_next_pfn(forbidden_pages_map);
  1320. /*
  1321. * Find the next bit set in both bitmaps. This is guaranteed to
  1322. * terminate when fb_pfn == fr_pfn == BM_END_OF_MAP.
  1323. */
  1324. do {
  1325. if (fb_pfn < fr_pfn)
  1326. fb_pfn = memory_bm_next_pfn(forbidden_pages_map);
  1327. if (fr_pfn < fb_pfn)
  1328. fr_pfn = memory_bm_next_pfn(free_pages_map);
  1329. } while (fb_pfn != fr_pfn);
  1330. if (fr_pfn != BM_END_OF_MAP && pfn_valid(fr_pfn)) {
  1331. struct page *page = pfn_to_page(fr_pfn);
  1332. memory_bm_clear_current(forbidden_pages_map);
  1333. memory_bm_clear_current(free_pages_map);
  1334. hibernate_restore_unprotect_page(page_address(page));
  1335. __free_page(page);
  1336. goto loop;
  1337. }
  1338. out:
  1339. nr_copy_pages = 0;
  1340. nr_meta_pages = 0;
  1341. nr_zero_pages = 0;
  1342. restore_pblist = NULL;
  1343. buffer = NULL;
  1344. alloc_normal = 0;
  1345. alloc_highmem = 0;
  1346. hibernate_restore_protection_end();
  1347. }
  1348. /* Helper functions used for the shrinking of memory. */
  1349. #define GFP_IMAGE (GFP_KERNEL | __GFP_NOWARN)
  1350. /**
  1351. * preallocate_image_pages - Allocate a number of pages for hibernation image.
  1352. * @nr_pages: Number of page frames to allocate.
  1353. * @mask: GFP flags to use for the allocation.
  1354. *
  1355. * Return value: Number of page frames actually allocated
  1356. */
  1357. static unsigned long preallocate_image_pages(unsigned long nr_pages, gfp_t mask)
  1358. {
  1359. unsigned long nr_alloc = 0;
  1360. while (nr_pages > 0) {
  1361. struct page *page;
  1362. page = alloc_image_page(mask);
  1363. if (!page)
  1364. break;
  1365. memory_bm_set_bit(&copy_bm, page_to_pfn(page));
  1366. if (PageHighMem(page))
  1367. alloc_highmem++;
  1368. else
  1369. alloc_normal++;
  1370. nr_pages--;
  1371. nr_alloc++;
  1372. }
  1373. return nr_alloc;
  1374. }
  1375. static unsigned long preallocate_image_memory(unsigned long nr_pages,
  1376. unsigned long avail_normal)
  1377. {
  1378. unsigned long alloc;
  1379. if (avail_normal <= alloc_normal)
  1380. return 0;
  1381. alloc = avail_normal - alloc_normal;
  1382. if (nr_pages < alloc)
  1383. alloc = nr_pages;
  1384. return preallocate_image_pages(alloc, GFP_IMAGE);
  1385. }
  1386. #ifdef CONFIG_HIGHMEM
  1387. static unsigned long preallocate_image_highmem(unsigned long nr_pages)
  1388. {
  1389. return preallocate_image_pages(nr_pages, GFP_IMAGE | __GFP_HIGHMEM);
  1390. }
  1391. /**
  1392. * __fraction - Compute (an approximation of) x * (multiplier / base).
  1393. */
  1394. static unsigned long __fraction(u64 x, u64 multiplier, u64 base)
  1395. {
  1396. return div64_u64(x * multiplier, base);
  1397. }
  1398. static unsigned long preallocate_highmem_fraction(unsigned long nr_pages,
  1399. unsigned long highmem,
  1400. unsigned long total)
  1401. {
  1402. unsigned long alloc = __fraction(nr_pages, highmem, total);
  1403. return preallocate_image_pages(alloc, GFP_IMAGE | __GFP_HIGHMEM);
  1404. }
  1405. #else /* CONFIG_HIGHMEM */
  1406. static inline unsigned long preallocate_image_highmem(unsigned long nr_pages)
  1407. {
  1408. return 0;
  1409. }
  1410. static inline unsigned long preallocate_highmem_fraction(unsigned long nr_pages,
  1411. unsigned long highmem,
  1412. unsigned long total)
  1413. {
  1414. return 0;
  1415. }
  1416. #endif /* CONFIG_HIGHMEM */
  1417. /**
  1418. * free_unnecessary_pages - Release preallocated pages not needed for the image.
  1419. */
  1420. static unsigned long free_unnecessary_pages(void)
  1421. {
  1422. unsigned long save, to_free_normal, to_free_highmem, free;
  1423. save = count_data_pages();
  1424. if (alloc_normal >= save) {
  1425. to_free_normal = alloc_normal - save;
  1426. save = 0;
  1427. } else {
  1428. to_free_normal = 0;
  1429. save -= alloc_normal;
  1430. }
  1431. save += count_highmem_pages();
  1432. if (alloc_highmem >= save) {
  1433. to_free_highmem = alloc_highmem - save;
  1434. } else {
  1435. to_free_highmem = 0;
  1436. save -= alloc_highmem;
  1437. if (to_free_normal > save)
  1438. to_free_normal -= save;
  1439. else
  1440. to_free_normal = 0;
  1441. }
  1442. free = to_free_normal + to_free_highmem;
  1443. memory_bm_position_reset(&copy_bm);
  1444. while (to_free_normal > 0 || to_free_highmem > 0) {
  1445. unsigned long pfn = memory_bm_next_pfn(&copy_bm);
  1446. struct page *page = pfn_to_page(pfn);
  1447. if (PageHighMem(page)) {
  1448. if (!to_free_highmem)
  1449. continue;
  1450. to_free_highmem--;
  1451. alloc_highmem--;
  1452. } else {
  1453. if (!to_free_normal)
  1454. continue;
  1455. to_free_normal--;
  1456. alloc_normal--;
  1457. }
  1458. memory_bm_clear_bit(&copy_bm, pfn);
  1459. swsusp_unset_page_forbidden(page);
  1460. swsusp_unset_page_free(page);
  1461. __free_page(page);
  1462. }
  1463. return free;
  1464. }
  1465. /**
  1466. * minimum_image_size - Estimate the minimum acceptable size of an image.
  1467. * @saveable: Number of saveable pages in the system.
  1468. *
  1469. * We want to avoid attempting to free too much memory too hard, so estimate the
  1470. * minimum acceptable size of a hibernation image to use as the lower limit for
  1471. * preallocating memory.
  1472. *
  1473. * We assume that the minimum image size should be proportional to
  1474. *
  1475. * [number of saveable pages] - [number of pages that can be freed in theory]
  1476. *
  1477. * where the second term is the sum of (1) reclaimable slab pages, (2) active
  1478. * and (3) inactive anonymous pages, (4) active and (5) inactive file pages.
  1479. */
  1480. static unsigned long minimum_image_size(unsigned long saveable)
  1481. {
  1482. unsigned long size;
  1483. size = global_node_page_state_pages(NR_SLAB_RECLAIMABLE_B)
  1484. + global_node_page_state(NR_ACTIVE_ANON)
  1485. + global_node_page_state(NR_INACTIVE_ANON)
  1486. + global_node_page_state(NR_ACTIVE_FILE)
  1487. + global_node_page_state(NR_INACTIVE_FILE);
  1488. return saveable <= size ? 0 : saveable - size;
  1489. }
  1490. /**
  1491. * hibernate_preallocate_memory - Preallocate memory for hibernation image.
  1492. *
  1493. * To create a hibernation image it is necessary to make a copy of every page
  1494. * frame in use. We also need a number of page frames to be free during
  1495. * hibernation for allocations made while saving the image and for device
  1496. * drivers, in case they need to allocate memory from their hibernation
  1497. * callbacks (these two numbers are given by PAGES_FOR_IO (which is a rough
  1498. * estimate) and reserved_size divided by PAGE_SIZE (which is tunable through
  1499. * /sys/power/reserved_size, respectively). To make this happen, we compute the
  1500. * total number of available page frames and allocate at least
  1501. *
  1502. * ([page frames total] - PAGES_FOR_IO - [metadata pages]) / 2
  1503. * - 2 * DIV_ROUND_UP(reserved_size, PAGE_SIZE)
  1504. *
  1505. * of them, which corresponds to the maximum size of a hibernation image.
  1506. *
  1507. * If image_size is set below the number following from the above formula,
  1508. * the preallocation of memory is continued until the total number of saveable
  1509. * pages in the system is below the requested image size or the minimum
  1510. * acceptable image size returned by minimum_image_size(), whichever is greater.
  1511. */
  1512. int hibernate_preallocate_memory(void)
  1513. {
  1514. struct zone *zone;
  1515. unsigned long saveable, size, max_size, count, highmem, pages = 0;
  1516. unsigned long alloc, save_highmem, pages_highmem, avail_normal;
  1517. ktime_t start, stop;
  1518. int error;
  1519. pr_info("Preallocating image memory\n");
  1520. start = ktime_get();
  1521. error = memory_bm_create(&orig_bm, GFP_IMAGE, PG_ANY);
  1522. if (error) {
  1523. pr_err("Cannot allocate original bitmap\n");
  1524. goto err_out;
  1525. }
  1526. error = memory_bm_create(&copy_bm, GFP_IMAGE, PG_ANY);
  1527. if (error) {
  1528. pr_err("Cannot allocate copy bitmap\n");
  1529. goto err_out;
  1530. }
  1531. error = memory_bm_create(&zero_bm, GFP_IMAGE, PG_ANY);
  1532. if (error) {
  1533. pr_err("Cannot allocate zero bitmap\n");
  1534. goto err_out;
  1535. }
  1536. alloc_normal = 0;
  1537. alloc_highmem = 0;
  1538. nr_zero_pages = 0;
  1539. /* Count the number of saveable data pages. */
  1540. save_highmem = count_highmem_pages();
  1541. saveable = count_data_pages();
  1542. /*
  1543. * Compute the total number of page frames we can use (count) and the
  1544. * number of pages needed for image metadata (size).
  1545. */
  1546. count = saveable;
  1547. saveable += save_highmem;
  1548. highmem = save_highmem;
  1549. size = 0;
  1550. for_each_populated_zone(zone) {
  1551. size += snapshot_additional_pages(zone);
  1552. if (is_highmem(zone))
  1553. highmem += zone_page_state(zone, NR_FREE_PAGES);
  1554. else
  1555. count += zone_page_state(zone, NR_FREE_PAGES);
  1556. }
  1557. avail_normal = count;
  1558. count += highmem;
  1559. count -= totalreserve_pages;
  1560. /* Compute the maximum number of saveable pages to leave in memory. */
  1561. max_size = (count - (size + PAGES_FOR_IO)) / 2
  1562. - 2 * DIV_ROUND_UP(reserved_size, PAGE_SIZE);
  1563. /* Compute the desired number of image pages specified by image_size. */
  1564. size = DIV_ROUND_UP(image_size, PAGE_SIZE);
  1565. if (size > max_size)
  1566. size = max_size;
  1567. /*
  1568. * If the desired number of image pages is at least as large as the
  1569. * current number of saveable pages in memory, allocate page frames for
  1570. * the image and we're done.
  1571. */
  1572. if (size >= saveable) {
  1573. pages = preallocate_image_highmem(save_highmem);
  1574. pages += preallocate_image_memory(saveable - pages, avail_normal);
  1575. goto out;
  1576. }
  1577. /* Estimate the minimum size of the image. */
  1578. pages = minimum_image_size(saveable);
  1579. /*
  1580. * To avoid excessive pressure on the normal zone, leave room in it to
  1581. * accommodate an image of the minimum size (unless it's already too
  1582. * small, in which case don't preallocate pages from it at all).
  1583. */
  1584. if (avail_normal > pages)
  1585. avail_normal -= pages;
  1586. else
  1587. avail_normal = 0;
  1588. if (size < pages)
  1589. size = min_t(unsigned long, pages, max_size);
  1590. /*
  1591. * Let the memory management subsystem know that we're going to need a
  1592. * large number of page frames to allocate and make it free some memory.
  1593. * NOTE: If this is not done, performance will be hurt badly in some
  1594. * test cases.
  1595. */
  1596. shrink_all_memory(saveable - size);
  1597. /*
  1598. * The number of saveable pages in memory was too high, so apply some
  1599. * pressure to decrease it. First, make room for the largest possible
  1600. * image and fail if that doesn't work. Next, try to decrease the size
  1601. * of the image as much as indicated by 'size' using allocations from
  1602. * highmem and non-highmem zones separately.
  1603. */
  1604. pages_highmem = preallocate_image_highmem(highmem / 2);
  1605. alloc = count - max_size;
  1606. if (alloc > pages_highmem)
  1607. alloc -= pages_highmem;
  1608. else
  1609. alloc = 0;
  1610. pages = preallocate_image_memory(alloc, avail_normal);
  1611. if (pages < alloc) {
  1612. /* We have exhausted non-highmem pages, try highmem. */
  1613. alloc -= pages;
  1614. pages += pages_highmem;
  1615. pages_highmem = preallocate_image_highmem(alloc);
  1616. if (pages_highmem < alloc) {
  1617. pr_err("Image allocation is %lu pages short\n",
  1618. alloc - pages_highmem);
  1619. goto err_out;
  1620. }
  1621. pages += pages_highmem;
  1622. /*
  1623. * size is the desired number of saveable pages to leave in
  1624. * memory, so try to preallocate (all memory - size) pages.
  1625. */
  1626. alloc = (count - pages) - size;
  1627. pages += preallocate_image_highmem(alloc);
  1628. } else {
  1629. /*
  1630. * There are approximately max_size saveable pages at this point
  1631. * and we want to reduce this number down to size.
  1632. */
  1633. alloc = max_size - size;
  1634. size = preallocate_highmem_fraction(alloc, highmem, count);
  1635. pages_highmem += size;
  1636. alloc -= size;
  1637. size = preallocate_image_memory(alloc, avail_normal);
  1638. pages_highmem += preallocate_image_highmem(alloc - size);
  1639. pages += pages_highmem + size;
  1640. }
  1641. /*
  1642. * We only need as many page frames for the image as there are saveable
  1643. * pages in memory, but we have allocated more. Release the excessive
  1644. * ones now.
  1645. */
  1646. pages -= free_unnecessary_pages();
  1647. out:
  1648. stop = ktime_get();
  1649. pr_info("Allocated %lu pages for snapshot\n", pages);
  1650. swsusp_show_speed(start, stop, pages, "Allocated");
  1651. return 0;
  1652. err_out:
  1653. swsusp_free();
  1654. return -ENOMEM;
  1655. }
  1656. #ifdef CONFIG_HIGHMEM
  1657. /**
  1658. * count_pages_for_highmem - Count non-highmem pages needed for copying highmem.
  1659. *
  1660. * Compute the number of non-highmem pages that will be necessary for creating
  1661. * copies of highmem pages.
  1662. */
  1663. static unsigned int count_pages_for_highmem(unsigned int nr_highmem)
  1664. {
  1665. unsigned int free_highmem = count_free_highmem_pages() + alloc_highmem;
  1666. if (free_highmem >= nr_highmem)
  1667. nr_highmem = 0;
  1668. else
  1669. nr_highmem -= free_highmem;
  1670. return nr_highmem;
  1671. }
  1672. #else
  1673. static unsigned int count_pages_for_highmem(unsigned int nr_highmem) { return 0; }
  1674. #endif /* CONFIG_HIGHMEM */
  1675. /**
  1676. * enough_free_mem - Check if there is enough free memory for the image.
  1677. */
  1678. static int enough_free_mem(unsigned int nr_pages, unsigned int nr_highmem)
  1679. {
  1680. struct zone *zone;
  1681. unsigned int free = alloc_normal;
  1682. for_each_populated_zone(zone)
  1683. if (!is_highmem(zone))
  1684. free += zone_page_state(zone, NR_FREE_PAGES);
  1685. nr_pages += count_pages_for_highmem(nr_highmem);
  1686. pr_debug("Normal pages needed: %u + %u, available pages: %u\n",
  1687. nr_pages, PAGES_FOR_IO, free);
  1688. return free > nr_pages + PAGES_FOR_IO;
  1689. }
  1690. #ifdef CONFIG_HIGHMEM
  1691. /**
  1692. * get_highmem_buffer - Allocate a buffer for highmem pages.
  1693. *
  1694. * If there are some highmem pages in the hibernation image, we may need a
  1695. * buffer to copy them and/or load their data.
  1696. */
  1697. static inline int get_highmem_buffer(int safe_needed)
  1698. {
  1699. buffer = get_image_page(GFP_ATOMIC, safe_needed);
  1700. return buffer ? 0 : -ENOMEM;
  1701. }
  1702. /**
  1703. * alloc_highmem_pages - Allocate some highmem pages for the image.
  1704. *
  1705. * Try to allocate as many pages as needed, but if the number of free highmem
  1706. * pages is less than that, allocate them all.
  1707. */
  1708. static inline unsigned int alloc_highmem_pages(struct memory_bitmap *bm,
  1709. unsigned int nr_highmem)
  1710. {
  1711. unsigned int to_alloc = count_free_highmem_pages();
  1712. if (to_alloc > nr_highmem)
  1713. to_alloc = nr_highmem;
  1714. nr_highmem -= to_alloc;
  1715. while (to_alloc-- > 0) {
  1716. struct page *page;
  1717. page = alloc_image_page(__GFP_HIGHMEM|__GFP_KSWAPD_RECLAIM);
  1718. memory_bm_set_bit(bm, page_to_pfn(page));
  1719. }
  1720. return nr_highmem;
  1721. }
  1722. #else
  1723. static inline int get_highmem_buffer(int safe_needed) { return 0; }
  1724. static inline unsigned int alloc_highmem_pages(struct memory_bitmap *bm,
  1725. unsigned int n) { return 0; }
  1726. #endif /* CONFIG_HIGHMEM */
  1727. /**
  1728. * swsusp_alloc - Allocate memory for hibernation image.
  1729. *
  1730. * We first try to allocate as many highmem pages as there are
  1731. * saveable highmem pages in the system. If that fails, we allocate
  1732. * non-highmem pages for the copies of the remaining highmem ones.
  1733. *
  1734. * In this approach it is likely that the copies of highmem pages will
  1735. * also be located in the high memory, because of the way in which
  1736. * copy_data_pages() works.
  1737. */
  1738. static int swsusp_alloc(struct memory_bitmap *copy_bm,
  1739. unsigned int nr_pages, unsigned int nr_highmem)
  1740. {
  1741. if (nr_highmem > 0) {
  1742. if (get_highmem_buffer(PG_ANY))
  1743. goto err_out;
  1744. if (nr_highmem > alloc_highmem) {
  1745. nr_highmem -= alloc_highmem;
  1746. nr_pages += alloc_highmem_pages(copy_bm, nr_highmem);
  1747. }
  1748. }
  1749. if (nr_pages > alloc_normal) {
  1750. nr_pages -= alloc_normal;
  1751. while (nr_pages-- > 0) {
  1752. struct page *page;
  1753. page = alloc_image_page(GFP_ATOMIC);
  1754. if (!page)
  1755. goto err_out;
  1756. memory_bm_set_bit(copy_bm, page_to_pfn(page));
  1757. }
  1758. }
  1759. return 0;
  1760. err_out:
  1761. swsusp_free();
  1762. return -ENOMEM;
  1763. }
  1764. asmlinkage __visible int swsusp_save(void)
  1765. {
  1766. unsigned int nr_pages, nr_highmem;
  1767. pr_info("Creating image:\n");
  1768. drain_local_pages(NULL);
  1769. nr_pages = count_data_pages();
  1770. nr_highmem = count_highmem_pages();
  1771. pr_info("Need to copy %u pages\n", nr_pages + nr_highmem);
  1772. if (!enough_free_mem(nr_pages, nr_highmem)) {
  1773. pr_err("Not enough free memory\n");
  1774. return -ENOMEM;
  1775. }
  1776. if (swsusp_alloc(&copy_bm, nr_pages, nr_highmem)) {
  1777. pr_err("Memory allocation failed\n");
  1778. return -ENOMEM;
  1779. }
  1780. /*
  1781. * During allocating of suspend pagedir, new cold pages may appear.
  1782. * Kill them.
  1783. */
  1784. drain_local_pages(NULL);
  1785. nr_copy_pages = copy_data_pages(&copy_bm, &orig_bm, &zero_bm);
  1786. /*
  1787. * End of critical section. From now on, we can write to memory,
  1788. * but we should not touch disk. This specially means we must _not_
  1789. * touch swap space! Except we must write out our image of course.
  1790. */
  1791. nr_pages += nr_highmem;
  1792. /* We don't actually copy the zero pages */
  1793. nr_zero_pages = nr_pages - nr_copy_pages;
  1794. nr_meta_pages = DIV_ROUND_UP(nr_pages * sizeof(long), PAGE_SIZE);
  1795. pr_info("Image created (%d pages copied, %d zero pages)\n", nr_copy_pages, nr_zero_pages);
  1796. return 0;
  1797. }
  1798. #ifndef CONFIG_ARCH_HIBERNATION_HEADER
  1799. static int init_header_complete(struct swsusp_info *info)
  1800. {
  1801. memcpy(&info->uts, init_utsname(), sizeof(struct new_utsname));
  1802. info->version_code = LINUX_VERSION_CODE;
  1803. return 0;
  1804. }
  1805. static const char *check_image_kernel(struct swsusp_info *info)
  1806. {
  1807. if (info->version_code != LINUX_VERSION_CODE)
  1808. return "kernel version";
  1809. if (strcmp(info->uts.sysname,init_utsname()->sysname))
  1810. return "system type";
  1811. if (strcmp(info->uts.release,init_utsname()->release))
  1812. return "kernel release";
  1813. if (strcmp(info->uts.version,init_utsname()->version))
  1814. return "version";
  1815. if (strcmp(info->uts.machine,init_utsname()->machine))
  1816. return "machine";
  1817. return NULL;
  1818. }
  1819. #endif /* CONFIG_ARCH_HIBERNATION_HEADER */
  1820. unsigned long snapshot_get_image_size(void)
  1821. {
  1822. return nr_copy_pages + nr_meta_pages + 1;
  1823. }
  1824. EXPORT_SYMBOL_GPL(snapshot_get_image_size);
  1825. static int init_header(struct swsusp_info *info)
  1826. {
  1827. memset(info, 0, sizeof(struct swsusp_info));
  1828. info->num_physpages = get_num_physpages();
  1829. info->image_pages = nr_copy_pages;
  1830. info->pages = snapshot_get_image_size();
  1831. info->size = info->pages;
  1832. info->size <<= PAGE_SHIFT;
  1833. return init_header_complete(info);
  1834. }
  1835. #define ENCODED_PFN_ZERO_FLAG ((unsigned long)1 << (BITS_PER_LONG - 1))
  1836. #define ENCODED_PFN_MASK (~ENCODED_PFN_ZERO_FLAG)
  1837. /**
  1838. * pack_pfns - Prepare PFNs for saving.
  1839. * @bm: Memory bitmap.
  1840. * @buf: Memory buffer to store the PFNs in.
  1841. * @zero_bm: Memory bitmap containing PFNs of zero pages.
  1842. *
  1843. * PFNs corresponding to set bits in @bm are stored in the area of memory
  1844. * pointed to by @buf (1 page at a time). Pages which were filled with only
  1845. * zeros will have the highest bit set in the packed format to distinguish
  1846. * them from PFNs which will be contained in the image file.
  1847. */
  1848. static inline void pack_pfns(unsigned long *buf, struct memory_bitmap *bm,
  1849. struct memory_bitmap *zero_bm)
  1850. {
  1851. int j;
  1852. for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
  1853. buf[j] = memory_bm_next_pfn(bm);
  1854. if (unlikely(buf[j] == BM_END_OF_MAP))
  1855. break;
  1856. if (memory_bm_test_bit(zero_bm, buf[j]))
  1857. buf[j] |= ENCODED_PFN_ZERO_FLAG;
  1858. }
  1859. }
  1860. /**
  1861. * snapshot_read_next - Get the address to read the next image page from.
  1862. * @handle: Snapshot handle to be used for the reading.
  1863. *
  1864. * On the first call, @handle should point to a zeroed snapshot_handle
  1865. * structure. The structure gets populated then and a pointer to it should be
  1866. * passed to this function every next time.
  1867. *
  1868. * On success, the function returns a positive number. Then, the caller
  1869. * is allowed to read up to the returned number of bytes from the memory
  1870. * location computed by the data_of() macro.
  1871. *
  1872. * The function returns 0 to indicate the end of the data stream condition,
  1873. * and negative numbers are returned on errors. If that happens, the structure
  1874. * pointed to by @handle is not updated and should not be used any more.
  1875. */
  1876. int snapshot_read_next(struct snapshot_handle *handle)
  1877. {
  1878. if (handle->cur > nr_meta_pages + nr_copy_pages)
  1879. return 0;
  1880. if (!buffer) {
  1881. /* This makes the buffer be freed by swsusp_free() */
  1882. buffer = get_image_page(GFP_ATOMIC, PG_ANY);
  1883. if (!buffer)
  1884. return -ENOMEM;
  1885. }
  1886. if (!handle->cur) {
  1887. int error;
  1888. error = init_header((struct swsusp_info *)buffer);
  1889. if (error)
  1890. return error;
  1891. handle->buffer = buffer;
  1892. memory_bm_position_reset(&orig_bm);
  1893. memory_bm_position_reset(&copy_bm);
  1894. } else if (handle->cur <= nr_meta_pages) {
  1895. clear_page(buffer);
  1896. pack_pfns(buffer, &orig_bm, &zero_bm);
  1897. } else {
  1898. struct page *page;
  1899. page = pfn_to_page(memory_bm_next_pfn(&copy_bm));
  1900. if (PageHighMem(page)) {
  1901. /*
  1902. * Highmem pages are copied to the buffer,
  1903. * because we can't return with a kmapped
  1904. * highmem page (we may not be called again).
  1905. */
  1906. void *kaddr;
  1907. kaddr = kmap_atomic(page);
  1908. copy_page(buffer, kaddr);
  1909. kunmap_atomic(kaddr);
  1910. handle->buffer = buffer;
  1911. } else {
  1912. handle->buffer = page_address(page);
  1913. }
  1914. }
  1915. handle->cur++;
  1916. return PAGE_SIZE;
  1917. }
  1918. static void duplicate_memory_bitmap(struct memory_bitmap *dst,
  1919. struct memory_bitmap *src)
  1920. {
  1921. unsigned long pfn;
  1922. memory_bm_position_reset(src);
  1923. pfn = memory_bm_next_pfn(src);
  1924. while (pfn != BM_END_OF_MAP) {
  1925. memory_bm_set_bit(dst, pfn);
  1926. pfn = memory_bm_next_pfn(src);
  1927. }
  1928. }
  1929. /**
  1930. * mark_unsafe_pages - Mark pages that were used before hibernation.
  1931. *
  1932. * Mark the pages that cannot be used for storing the image during restoration,
  1933. * because they conflict with the pages that had been used before hibernation.
  1934. */
  1935. static void mark_unsafe_pages(struct memory_bitmap *bm)
  1936. {
  1937. unsigned long pfn;
  1938. /* Clear the "free"/"unsafe" bit for all PFNs */
  1939. memory_bm_position_reset(free_pages_map);
  1940. pfn = memory_bm_next_pfn(free_pages_map);
  1941. while (pfn != BM_END_OF_MAP) {
  1942. memory_bm_clear_current(free_pages_map);
  1943. pfn = memory_bm_next_pfn(free_pages_map);
  1944. }
  1945. /* Mark pages that correspond to the "original" PFNs as "unsafe" */
  1946. duplicate_memory_bitmap(free_pages_map, bm);
  1947. allocated_unsafe_pages = 0;
  1948. }
  1949. static int check_header(struct swsusp_info *info)
  1950. {
  1951. const char *reason;
  1952. reason = check_image_kernel(info);
  1953. if (!reason && info->num_physpages != get_num_physpages())
  1954. reason = "memory size";
  1955. if (reason) {
  1956. pr_err("Image mismatch: %s\n", reason);
  1957. return -EPERM;
  1958. }
  1959. return 0;
  1960. }
  1961. /**
  1962. * load_header - Check the image header and copy the data from it.
  1963. */
  1964. static int load_header(struct swsusp_info *info)
  1965. {
  1966. int error;
  1967. restore_pblist = NULL;
  1968. error = check_header(info);
  1969. if (!error) {
  1970. nr_copy_pages = info->image_pages;
  1971. nr_meta_pages = info->pages - info->image_pages - 1;
  1972. }
  1973. return error;
  1974. }
  1975. /**
  1976. * unpack_orig_pfns - Set bits corresponding to given PFNs in a memory bitmap.
  1977. * @bm: Memory bitmap.
  1978. * @buf: Area of memory containing the PFNs.
  1979. * @zero_bm: Memory bitmap with the zero PFNs marked.
  1980. *
  1981. * For each element of the array pointed to by @buf (1 page at a time), set the
  1982. * corresponding bit in @bm. If the page was originally populated with only
  1983. * zeros then a corresponding bit will also be set in @zero_bm.
  1984. */
  1985. static int unpack_orig_pfns(unsigned long *buf, struct memory_bitmap *bm,
  1986. struct memory_bitmap *zero_bm)
  1987. {
  1988. unsigned long decoded_pfn;
  1989. bool zero;
  1990. int j;
  1991. for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
  1992. if (unlikely(buf[j] == BM_END_OF_MAP))
  1993. break;
  1994. zero = !!(buf[j] & ENCODED_PFN_ZERO_FLAG);
  1995. decoded_pfn = buf[j] & ENCODED_PFN_MASK;
  1996. if (pfn_valid(decoded_pfn) && memory_bm_pfn_present(bm, decoded_pfn)) {
  1997. memory_bm_set_bit(bm, decoded_pfn);
  1998. if (zero) {
  1999. memory_bm_set_bit(zero_bm, decoded_pfn);
  2000. nr_zero_pages++;
  2001. }
  2002. } else {
  2003. if (!pfn_valid(decoded_pfn))
  2004. pr_err(FW_BUG "Memory map mismatch at 0x%llx after hibernation\n",
  2005. (unsigned long long)PFN_PHYS(decoded_pfn));
  2006. return -EFAULT;
  2007. }
  2008. }
  2009. return 0;
  2010. }
  2011. #ifdef CONFIG_HIGHMEM
  2012. /*
  2013. * struct highmem_pbe is used for creating the list of highmem pages that
  2014. * should be restored atomically during the resume from disk, because the page
  2015. * frames they have occupied before the suspend are in use.
  2016. */
  2017. struct highmem_pbe {
  2018. struct page *copy_page; /* data is here now */
  2019. struct page *orig_page; /* data was here before the suspend */
  2020. struct highmem_pbe *next;
  2021. };
  2022. /*
  2023. * List of highmem PBEs needed for restoring the highmem pages that were
  2024. * allocated before the suspend and included in the suspend image, but have
  2025. * also been allocated by the "resume" kernel, so their contents cannot be
  2026. * written directly to their "original" page frames.
  2027. */
  2028. static struct highmem_pbe *highmem_pblist;
  2029. /**
  2030. * count_highmem_image_pages - Compute the number of highmem pages in the image.
  2031. * @bm: Memory bitmap.
  2032. *
  2033. * The bits in @bm that correspond to image pages are assumed to be set.
  2034. */
  2035. static unsigned int count_highmem_image_pages(struct memory_bitmap *bm)
  2036. {
  2037. unsigned long pfn;
  2038. unsigned int cnt = 0;
  2039. memory_bm_position_reset(bm);
  2040. pfn = memory_bm_next_pfn(bm);
  2041. while (pfn != BM_END_OF_MAP) {
  2042. if (PageHighMem(pfn_to_page(pfn)))
  2043. cnt++;
  2044. pfn = memory_bm_next_pfn(bm);
  2045. }
  2046. return cnt;
  2047. }
  2048. static unsigned int safe_highmem_pages;
  2049. static struct memory_bitmap *safe_highmem_bm;
  2050. /**
  2051. * prepare_highmem_image - Allocate memory for loading highmem data from image.
  2052. * @bm: Pointer to an uninitialized memory bitmap structure.
  2053. * @nr_highmem_p: Pointer to the number of highmem image pages.
  2054. *
  2055. * Try to allocate as many highmem pages as there are highmem image pages
  2056. * (@nr_highmem_p points to the variable containing the number of highmem image
  2057. * pages). The pages that are "safe" (ie. will not be overwritten when the
  2058. * hibernation image is restored entirely) have the corresponding bits set in
  2059. * @bm (it must be uninitialized).
  2060. *
  2061. * NOTE: This function should not be called if there are no highmem image pages.
  2062. */
  2063. static int prepare_highmem_image(struct memory_bitmap *bm,
  2064. unsigned int *nr_highmem_p)
  2065. {
  2066. unsigned int to_alloc;
  2067. if (memory_bm_create(bm, GFP_ATOMIC, PG_SAFE))
  2068. return -ENOMEM;
  2069. if (get_highmem_buffer(PG_SAFE))
  2070. return -ENOMEM;
  2071. to_alloc = count_free_highmem_pages();
  2072. if (to_alloc > *nr_highmem_p)
  2073. to_alloc = *nr_highmem_p;
  2074. else
  2075. *nr_highmem_p = to_alloc;
  2076. safe_highmem_pages = 0;
  2077. while (to_alloc-- > 0) {
  2078. struct page *page;
  2079. page = alloc_page(__GFP_HIGHMEM);
  2080. if (!swsusp_page_is_free(page)) {
  2081. /* The page is "safe", set its bit the bitmap */
  2082. memory_bm_set_bit(bm, page_to_pfn(page));
  2083. safe_highmem_pages++;
  2084. }
  2085. /* Mark the page as allocated */
  2086. swsusp_set_page_forbidden(page);
  2087. swsusp_set_page_free(page);
  2088. }
  2089. memory_bm_position_reset(bm);
  2090. safe_highmem_bm = bm;
  2091. return 0;
  2092. }
  2093. static struct page *last_highmem_page;
  2094. /**
  2095. * get_highmem_page_buffer - Prepare a buffer to store a highmem image page.
  2096. *
  2097. * For a given highmem image page get a buffer that suspend_write_next() should
  2098. * return to its caller to write to.
  2099. *
  2100. * If the page is to be saved to its "original" page frame or a copy of
  2101. * the page is to be made in the highmem, @buffer is returned. Otherwise,
  2102. * the copy of the page is to be made in normal memory, so the address of
  2103. * the copy is returned.
  2104. *
  2105. * If @buffer is returned, the caller of suspend_write_next() will write
  2106. * the page's contents to @buffer, so they will have to be copied to the
  2107. * right location on the next call to suspend_write_next() and it is done
  2108. * with the help of copy_last_highmem_page(). For this purpose, if
  2109. * @buffer is returned, @last_highmem_page is set to the page to which
  2110. * the data will have to be copied from @buffer.
  2111. */
  2112. static void *get_highmem_page_buffer(struct page *page,
  2113. struct chain_allocator *ca)
  2114. {
  2115. struct highmem_pbe *pbe;
  2116. void *kaddr;
  2117. if (swsusp_page_is_forbidden(page) && swsusp_page_is_free(page)) {
  2118. /*
  2119. * We have allocated the "original" page frame and we can
  2120. * use it directly to store the loaded page.
  2121. */
  2122. last_highmem_page = page;
  2123. return buffer;
  2124. }
  2125. /*
  2126. * The "original" page frame has not been allocated and we have to
  2127. * use a "safe" page frame to store the loaded page.
  2128. */
  2129. pbe = chain_alloc(ca, sizeof(struct highmem_pbe));
  2130. if (!pbe) {
  2131. swsusp_free();
  2132. return ERR_PTR(-ENOMEM);
  2133. }
  2134. pbe->orig_page = page;
  2135. if (safe_highmem_pages > 0) {
  2136. struct page *tmp;
  2137. /* Copy of the page will be stored in high memory */
  2138. kaddr = buffer;
  2139. tmp = pfn_to_page(memory_bm_next_pfn(safe_highmem_bm));
  2140. safe_highmem_pages--;
  2141. last_highmem_page = tmp;
  2142. pbe->copy_page = tmp;
  2143. } else {
  2144. /* Copy of the page will be stored in normal memory */
  2145. kaddr = __get_safe_page(ca->gfp_mask);
  2146. if (!kaddr)
  2147. return ERR_PTR(-ENOMEM);
  2148. pbe->copy_page = virt_to_page(kaddr);
  2149. }
  2150. pbe->next = highmem_pblist;
  2151. highmem_pblist = pbe;
  2152. return kaddr;
  2153. }
  2154. /**
  2155. * copy_last_highmem_page - Copy most the most recent highmem image page.
  2156. *
  2157. * Copy the contents of a highmem image from @buffer, where the caller of
  2158. * snapshot_write_next() has stored them, to the right location represented by
  2159. * @last_highmem_page .
  2160. */
  2161. static void copy_last_highmem_page(void)
  2162. {
  2163. if (last_highmem_page) {
  2164. void *dst;
  2165. dst = kmap_atomic(last_highmem_page);
  2166. copy_page(dst, buffer);
  2167. kunmap_atomic(dst);
  2168. last_highmem_page = NULL;
  2169. }
  2170. }
  2171. static inline int last_highmem_page_copied(void)
  2172. {
  2173. return !last_highmem_page;
  2174. }
  2175. static inline void free_highmem_data(void)
  2176. {
  2177. if (safe_highmem_bm)
  2178. memory_bm_free(safe_highmem_bm, PG_UNSAFE_CLEAR);
  2179. if (buffer)
  2180. free_image_page(buffer, PG_UNSAFE_CLEAR);
  2181. }
  2182. #else
  2183. static unsigned int count_highmem_image_pages(struct memory_bitmap *bm) { return 0; }
  2184. static inline int prepare_highmem_image(struct memory_bitmap *bm,
  2185. unsigned int *nr_highmem_p) { return 0; }
  2186. static inline void *get_highmem_page_buffer(struct page *page,
  2187. struct chain_allocator *ca)
  2188. {
  2189. return ERR_PTR(-EINVAL);
  2190. }
  2191. static inline void copy_last_highmem_page(void) {}
  2192. static inline int last_highmem_page_copied(void) { return 1; }
  2193. static inline void free_highmem_data(void) {}
  2194. #endif /* CONFIG_HIGHMEM */
  2195. #define PBES_PER_LINKED_PAGE (LINKED_PAGE_DATA_SIZE / sizeof(struct pbe))
  2196. /**
  2197. * prepare_image - Make room for loading hibernation image.
  2198. * @new_bm: Uninitialized memory bitmap structure.
  2199. * @bm: Memory bitmap with unsafe pages marked.
  2200. * @zero_bm: Memory bitmap containing the zero pages.
  2201. *
  2202. * Use @bm to mark the pages that will be overwritten in the process of
  2203. * restoring the system memory state from the suspend image ("unsafe" pages)
  2204. * and allocate memory for the image.
  2205. *
  2206. * The idea is to allocate a new memory bitmap first and then allocate
  2207. * as many pages as needed for image data, but without specifying what those
  2208. * pages will be used for just yet. Instead, we mark them all as allocated and
  2209. * create a lists of "safe" pages to be used later. On systems with high
  2210. * memory a list of "safe" highmem pages is created too.
  2211. *
  2212. * Because it was not known which pages were unsafe when @zero_bm was created,
  2213. * make a copy of it and recreate it within safe pages.
  2214. */
  2215. static int prepare_image(struct memory_bitmap *new_bm, struct memory_bitmap *bm,
  2216. struct memory_bitmap *zero_bm)
  2217. {
  2218. unsigned int nr_pages, nr_highmem;
  2219. struct memory_bitmap tmp;
  2220. struct linked_page *lp;
  2221. int error;
  2222. /* If there is no highmem, the buffer will not be necessary */
  2223. free_image_page(buffer, PG_UNSAFE_CLEAR);
  2224. buffer = NULL;
  2225. nr_highmem = count_highmem_image_pages(bm);
  2226. mark_unsafe_pages(bm);
  2227. error = memory_bm_create(new_bm, GFP_ATOMIC, PG_SAFE);
  2228. if (error)
  2229. goto Free;
  2230. duplicate_memory_bitmap(new_bm, bm);
  2231. memory_bm_free(bm, PG_UNSAFE_KEEP);
  2232. /* Make a copy of zero_bm so it can be created in safe pages */
  2233. error = memory_bm_create(&tmp, GFP_ATOMIC, PG_SAFE);
  2234. if (error)
  2235. goto Free;
  2236. duplicate_memory_bitmap(&tmp, zero_bm);
  2237. memory_bm_free(zero_bm, PG_UNSAFE_KEEP);
  2238. /* Recreate zero_bm in safe pages */
  2239. error = memory_bm_create(zero_bm, GFP_ATOMIC, PG_SAFE);
  2240. if (error)
  2241. goto Free;
  2242. duplicate_memory_bitmap(zero_bm, &tmp);
  2243. memory_bm_free(&tmp, PG_UNSAFE_CLEAR);
  2244. /* At this point zero_bm is in safe pages and it can be used for restoring. */
  2245. if (nr_highmem > 0) {
  2246. error = prepare_highmem_image(bm, &nr_highmem);
  2247. if (error)
  2248. goto Free;
  2249. }
  2250. /*
  2251. * Reserve some safe pages for potential later use.
  2252. *
  2253. * NOTE: This way we make sure there will be enough safe pages for the
  2254. * chain_alloc() in get_buffer(). It is a bit wasteful, but
  2255. * nr_copy_pages cannot be greater than 50% of the memory anyway.
  2256. *
  2257. * nr_copy_pages cannot be less than allocated_unsafe_pages too.
  2258. */
  2259. nr_pages = (nr_zero_pages + nr_copy_pages) - nr_highmem - allocated_unsafe_pages;
  2260. nr_pages = DIV_ROUND_UP(nr_pages, PBES_PER_LINKED_PAGE);
  2261. while (nr_pages > 0) {
  2262. lp = get_image_page(GFP_ATOMIC, PG_SAFE);
  2263. if (!lp) {
  2264. error = -ENOMEM;
  2265. goto Free;
  2266. }
  2267. lp->next = safe_pages_list;
  2268. safe_pages_list = lp;
  2269. nr_pages--;
  2270. }
  2271. /* Preallocate memory for the image */
  2272. nr_pages = (nr_zero_pages + nr_copy_pages) - nr_highmem - allocated_unsafe_pages;
  2273. while (nr_pages > 0) {
  2274. lp = (struct linked_page *)get_zeroed_page(GFP_ATOMIC);
  2275. if (!lp) {
  2276. error = -ENOMEM;
  2277. goto Free;
  2278. }
  2279. if (!swsusp_page_is_free(virt_to_page(lp))) {
  2280. /* The page is "safe", add it to the list */
  2281. lp->next = safe_pages_list;
  2282. safe_pages_list = lp;
  2283. }
  2284. /* Mark the page as allocated */
  2285. swsusp_set_page_forbidden(virt_to_page(lp));
  2286. swsusp_set_page_free(virt_to_page(lp));
  2287. nr_pages--;
  2288. }
  2289. return 0;
  2290. Free:
  2291. swsusp_free();
  2292. return error;
  2293. }
  2294. /**
  2295. * get_buffer - Get the address to store the next image data page.
  2296. *
  2297. * Get the address that snapshot_write_next() should return to its caller to
  2298. * write to.
  2299. */
  2300. static void *get_buffer(struct memory_bitmap *bm, struct chain_allocator *ca)
  2301. {
  2302. struct pbe *pbe;
  2303. struct page *page;
  2304. unsigned long pfn = memory_bm_next_pfn(bm);
  2305. if (pfn == BM_END_OF_MAP)
  2306. return ERR_PTR(-EFAULT);
  2307. page = pfn_to_page(pfn);
  2308. if (PageHighMem(page))
  2309. return get_highmem_page_buffer(page, ca);
  2310. if (swsusp_page_is_forbidden(page) && swsusp_page_is_free(page))
  2311. /*
  2312. * We have allocated the "original" page frame and we can
  2313. * use it directly to store the loaded page.
  2314. */
  2315. return page_address(page);
  2316. /*
  2317. * The "original" page frame has not been allocated and we have to
  2318. * use a "safe" page frame to store the loaded page.
  2319. */
  2320. pbe = chain_alloc(ca, sizeof(struct pbe));
  2321. if (!pbe) {
  2322. swsusp_free();
  2323. return ERR_PTR(-ENOMEM);
  2324. }
  2325. pbe->orig_address = page_address(page);
  2326. pbe->address = __get_safe_page(ca->gfp_mask);
  2327. if (!pbe->address)
  2328. return ERR_PTR(-ENOMEM);
  2329. pbe->next = restore_pblist;
  2330. restore_pblist = pbe;
  2331. return pbe->address;
  2332. }
  2333. /**
  2334. * snapshot_write_next - Get the address to store the next image page.
  2335. * @handle: Snapshot handle structure to guide the writing.
  2336. *
  2337. * On the first call, @handle should point to a zeroed snapshot_handle
  2338. * structure. The structure gets populated then and a pointer to it should be
  2339. * passed to this function every next time.
  2340. *
  2341. * On success, the function returns a positive number. Then, the caller
  2342. * is allowed to write up to the returned number of bytes to the memory
  2343. * location computed by the data_of() macro.
  2344. *
  2345. * The function returns 0 to indicate the "end of file" condition. Negative
  2346. * numbers are returned on errors, in which cases the structure pointed to by
  2347. * @handle is not updated and should not be used any more.
  2348. */
  2349. int snapshot_write_next(struct snapshot_handle *handle)
  2350. {
  2351. static struct chain_allocator ca;
  2352. int error = 0;
  2353. next:
  2354. /* Check if we have already loaded the entire image */
  2355. if (handle->cur > 1 && handle->cur > nr_meta_pages + nr_copy_pages + nr_zero_pages)
  2356. return 0;
  2357. if (!handle->cur) {
  2358. if (!buffer)
  2359. /* This makes the buffer be freed by swsusp_free() */
  2360. buffer = get_image_page(GFP_ATOMIC, PG_ANY);
  2361. if (!buffer)
  2362. return -ENOMEM;
  2363. handle->buffer = buffer;
  2364. } else if (handle->cur == 1) {
  2365. error = load_header(buffer);
  2366. if (error)
  2367. return error;
  2368. safe_pages_list = NULL;
  2369. error = memory_bm_create(&copy_bm, GFP_ATOMIC, PG_ANY);
  2370. if (error)
  2371. return error;
  2372. error = memory_bm_create(&zero_bm, GFP_ATOMIC, PG_ANY);
  2373. if (error)
  2374. return error;
  2375. nr_zero_pages = 0;
  2376. hibernate_restore_protection_begin();
  2377. } else if (handle->cur <= nr_meta_pages + 1) {
  2378. error = unpack_orig_pfns(buffer, &copy_bm, &zero_bm);
  2379. if (error)
  2380. return error;
  2381. if (handle->cur == nr_meta_pages + 1) {
  2382. error = prepare_image(&orig_bm, &copy_bm, &zero_bm);
  2383. if (error)
  2384. return error;
  2385. chain_init(&ca, GFP_ATOMIC, PG_SAFE);
  2386. memory_bm_position_reset(&orig_bm);
  2387. memory_bm_position_reset(&zero_bm);
  2388. restore_pblist = NULL;
  2389. handle->buffer = get_buffer(&orig_bm, &ca);
  2390. if (IS_ERR(handle->buffer))
  2391. return PTR_ERR(handle->buffer);
  2392. }
  2393. } else {
  2394. copy_last_highmem_page();
  2395. hibernate_restore_protect_page(handle->buffer);
  2396. handle->buffer = get_buffer(&orig_bm, &ca);
  2397. if (IS_ERR(handle->buffer))
  2398. return PTR_ERR(handle->buffer);
  2399. }
  2400. handle->sync_read = (handle->buffer == buffer);
  2401. handle->cur++;
  2402. /* Zero pages were not included in the image, memset it and move on. */
  2403. if (handle->cur > nr_meta_pages + 1 &&
  2404. memory_bm_test_bit(&zero_bm, memory_bm_get_current(&orig_bm))) {
  2405. memset(handle->buffer, 0, PAGE_SIZE);
  2406. goto next;
  2407. }
  2408. return PAGE_SIZE;
  2409. }
  2410. /**
  2411. * snapshot_write_finalize - Complete the loading of a hibernation image.
  2412. *
  2413. * Must be called after the last call to snapshot_write_next() in case the last
  2414. * page in the image happens to be a highmem page and its contents should be
  2415. * stored in highmem. Additionally, it recycles bitmap memory that's not
  2416. * necessary any more.
  2417. */
  2418. void snapshot_write_finalize(struct snapshot_handle *handle)
  2419. {
  2420. copy_last_highmem_page();
  2421. hibernate_restore_protect_page(handle->buffer);
  2422. /* Do that only if we have loaded the image entirely */
  2423. if (handle->cur > 1 && handle->cur > nr_meta_pages + nr_copy_pages + nr_zero_pages) {
  2424. memory_bm_recycle(&orig_bm);
  2425. free_highmem_data();
  2426. }
  2427. }
  2428. int snapshot_image_loaded(struct snapshot_handle *handle)
  2429. {
  2430. return !(!nr_copy_pages || !last_highmem_page_copied() ||
  2431. handle->cur <= nr_meta_pages + nr_copy_pages + nr_zero_pages);
  2432. }
  2433. #ifdef CONFIG_HIGHMEM
  2434. /* Assumes that @buf is ready and points to a "safe" page */
  2435. static inline void swap_two_pages_data(struct page *p1, struct page *p2,
  2436. void *buf)
  2437. {
  2438. void *kaddr1, *kaddr2;
  2439. kaddr1 = kmap_atomic(p1);
  2440. kaddr2 = kmap_atomic(p2);
  2441. copy_page(buf, kaddr1);
  2442. copy_page(kaddr1, kaddr2);
  2443. copy_page(kaddr2, buf);
  2444. kunmap_atomic(kaddr2);
  2445. kunmap_atomic(kaddr1);
  2446. }
  2447. /**
  2448. * restore_highmem - Put highmem image pages into their original locations.
  2449. *
  2450. * For each highmem page that was in use before hibernation and is included in
  2451. * the image, and also has been allocated by the "restore" kernel, swap its
  2452. * current contents with the previous (ie. "before hibernation") ones.
  2453. *
  2454. * If the restore eventually fails, we can call this function once again and
  2455. * restore the highmem state as seen by the restore kernel.
  2456. */
  2457. int restore_highmem(void)
  2458. {
  2459. struct highmem_pbe *pbe = highmem_pblist;
  2460. void *buf;
  2461. if (!pbe)
  2462. return 0;
  2463. buf = get_image_page(GFP_ATOMIC, PG_SAFE);
  2464. if (!buf)
  2465. return -ENOMEM;
  2466. while (pbe) {
  2467. swap_two_pages_data(pbe->copy_page, pbe->orig_page, buf);
  2468. pbe = pbe->next;
  2469. }
  2470. free_image_page(buf, PG_UNSAFE_CLEAR);
  2471. return 0;
  2472. }
  2473. #endif /* CONFIG_HIGHMEM */