snapshot.c 70 KB

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