snapshot.c 72 KB

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