snapshot.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948
  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@suse.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/smp_lock.h>
  17. #include <linux/delay.h>
  18. #include <linux/bitops.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/kernel.h>
  21. #include <linux/pm.h>
  22. #include <linux/device.h>
  23. #include <linux/init.h>
  24. #include <linux/bootmem.h>
  25. #include <linux/syscalls.h>
  26. #include <linux/console.h>
  27. #include <linux/highmem.h>
  28. #include <asm/uaccess.h>
  29. #include <asm/mmu_context.h>
  30. #include <asm/pgtable.h>
  31. #include <asm/tlbflush.h>
  32. #include <asm/io.h>
  33. #include "power.h"
  34. static int swsusp_page_is_free(struct page *);
  35. static void swsusp_set_page_forbidden(struct page *);
  36. static void swsusp_unset_page_forbidden(struct page *);
  37. /* List of PBEs needed for restoring the pages that were allocated before
  38. * the suspend and included in the suspend image, but have also been
  39. * allocated by the "resume" kernel, so their contents cannot be written
  40. * directly to their "original" page frames.
  41. */
  42. struct pbe *restore_pblist;
  43. /* Pointer to an auxiliary buffer (1 page) */
  44. static void *buffer;
  45. /**
  46. * @safe_needed - on resume, for storing the PBE list and the image,
  47. * we can only use memory pages that do not conflict with the pages
  48. * used before suspend. The unsafe pages have PageNosaveFree set
  49. * and we count them using unsafe_pages.
  50. *
  51. * Each allocated image page is marked as PageNosave and PageNosaveFree
  52. * so that swsusp_free() can release it.
  53. */
  54. #define PG_ANY 0
  55. #define PG_SAFE 1
  56. #define PG_UNSAFE_CLEAR 1
  57. #define PG_UNSAFE_KEEP 0
  58. static unsigned int allocated_unsafe_pages;
  59. static void *get_image_page(gfp_t gfp_mask, int safe_needed)
  60. {
  61. void *res;
  62. res = (void *)get_zeroed_page(gfp_mask);
  63. if (safe_needed)
  64. while (res && swsusp_page_is_free(virt_to_page(res))) {
  65. /* The page is unsafe, mark it for swsusp_free() */
  66. swsusp_set_page_forbidden(virt_to_page(res));
  67. allocated_unsafe_pages++;
  68. res = (void *)get_zeroed_page(gfp_mask);
  69. }
  70. if (res) {
  71. swsusp_set_page_forbidden(virt_to_page(res));
  72. swsusp_set_page_free(virt_to_page(res));
  73. }
  74. return res;
  75. }
  76. unsigned long get_safe_page(gfp_t gfp_mask)
  77. {
  78. return (unsigned long)get_image_page(gfp_mask, PG_SAFE);
  79. }
  80. static struct page *alloc_image_page(gfp_t gfp_mask)
  81. {
  82. struct page *page;
  83. page = alloc_page(gfp_mask);
  84. if (page) {
  85. swsusp_set_page_forbidden(page);
  86. swsusp_set_page_free(page);
  87. }
  88. return page;
  89. }
  90. /**
  91. * free_image_page - free page represented by @addr, allocated with
  92. * get_image_page (page flags set by it must be cleared)
  93. */
  94. static inline void free_image_page(void *addr, int clear_nosave_free)
  95. {
  96. struct page *page;
  97. BUG_ON(!virt_addr_valid(addr));
  98. page = virt_to_page(addr);
  99. swsusp_unset_page_forbidden(page);
  100. if (clear_nosave_free)
  101. swsusp_unset_page_free(page);
  102. __free_page(page);
  103. }
  104. /* struct linked_page is used to build chains of pages */
  105. #define LINKED_PAGE_DATA_SIZE (PAGE_SIZE - sizeof(void *))
  106. struct linked_page {
  107. struct linked_page *next;
  108. char data[LINKED_PAGE_DATA_SIZE];
  109. } __attribute__((packed));
  110. static inline void
  111. free_list_of_pages(struct linked_page *list, int clear_page_nosave)
  112. {
  113. while (list) {
  114. struct linked_page *lp = list->next;
  115. free_image_page(list, clear_page_nosave);
  116. list = lp;
  117. }
  118. }
  119. /**
  120. * struct chain_allocator is used for allocating small objects out of
  121. * a linked list of pages called 'the chain'.
  122. *
  123. * The chain grows each time when there is no room for a new object in
  124. * the current page. The allocated objects cannot be freed individually.
  125. * It is only possible to free them all at once, by freeing the entire
  126. * chain.
  127. *
  128. * NOTE: The chain allocator may be inefficient if the allocated objects
  129. * are not much smaller than PAGE_SIZE.
  130. */
  131. struct chain_allocator {
  132. struct linked_page *chain; /* the chain */
  133. unsigned int used_space; /* total size of objects allocated out
  134. * of the current page
  135. */
  136. gfp_t gfp_mask; /* mask for allocating pages */
  137. int safe_needed; /* if set, only "safe" pages are allocated */
  138. };
  139. static void
  140. chain_init(struct chain_allocator *ca, gfp_t gfp_mask, int safe_needed)
  141. {
  142. ca->chain = NULL;
  143. ca->used_space = LINKED_PAGE_DATA_SIZE;
  144. ca->gfp_mask = gfp_mask;
  145. ca->safe_needed = safe_needed;
  146. }
  147. static void *chain_alloc(struct chain_allocator *ca, unsigned int size)
  148. {
  149. void *ret;
  150. if (LINKED_PAGE_DATA_SIZE - ca->used_space < size) {
  151. struct linked_page *lp;
  152. lp = get_image_page(ca->gfp_mask, ca->safe_needed);
  153. if (!lp)
  154. return NULL;
  155. lp->next = ca->chain;
  156. ca->chain = lp;
  157. ca->used_space = 0;
  158. }
  159. ret = ca->chain->data + ca->used_space;
  160. ca->used_space += size;
  161. return ret;
  162. }
  163. static void chain_free(struct chain_allocator *ca, int clear_page_nosave)
  164. {
  165. free_list_of_pages(ca->chain, clear_page_nosave);
  166. memset(ca, 0, sizeof(struct chain_allocator));
  167. }
  168. /**
  169. * Data types related to memory bitmaps.
  170. *
  171. * Memory bitmap is a structure consiting of many linked lists of
  172. * objects. The main list's elements are of type struct zone_bitmap
  173. * and each of them corresonds to one zone. For each zone bitmap
  174. * object there is a list of objects of type struct bm_block that
  175. * represent each blocks of bit chunks in which information is
  176. * stored.
  177. *
  178. * struct memory_bitmap contains a pointer to the main list of zone
  179. * bitmap objects, a struct bm_position used for browsing the bitmap,
  180. * and a pointer to the list of pages used for allocating all of the
  181. * zone bitmap objects and bitmap block objects.
  182. *
  183. * NOTE: It has to be possible to lay out the bitmap in memory
  184. * using only allocations of order 0. Additionally, the bitmap is
  185. * designed to work with arbitrary number of zones (this is over the
  186. * top for now, but let's avoid making unnecessary assumptions ;-).
  187. *
  188. * struct zone_bitmap contains a pointer to a list of bitmap block
  189. * objects and a pointer to the bitmap block object that has been
  190. * most recently used for setting bits. Additionally, it contains the
  191. * pfns that correspond to the start and end of the represented zone.
  192. *
  193. * struct bm_block contains a pointer to the memory page in which
  194. * information is stored (in the form of a block of bit chunks
  195. * of type unsigned long each). It also contains the pfns that
  196. * correspond to the start and end of the represented memory area and
  197. * the number of bit chunks in the block.
  198. */
  199. #define BM_END_OF_MAP (~0UL)
  200. #define BM_CHUNKS_PER_BLOCK (PAGE_SIZE / sizeof(long))
  201. #define BM_BITS_PER_CHUNK (sizeof(long) << 3)
  202. #define BM_BITS_PER_BLOCK (PAGE_SIZE << 3)
  203. struct bm_block {
  204. struct bm_block *next; /* next element of the list */
  205. unsigned long start_pfn; /* pfn represented by the first bit */
  206. unsigned long end_pfn; /* pfn represented by the last bit plus 1 */
  207. unsigned int size; /* number of bit chunks */
  208. unsigned long *data; /* chunks of bits representing pages */
  209. };
  210. struct zone_bitmap {
  211. struct zone_bitmap *next; /* next element of the list */
  212. unsigned long start_pfn; /* minimal pfn in this zone */
  213. unsigned long end_pfn; /* maximal pfn in this zone plus 1 */
  214. struct bm_block *bm_blocks; /* list of bitmap blocks */
  215. struct bm_block *cur_block; /* recently used bitmap block */
  216. };
  217. /* strcut bm_position is used for browsing memory bitmaps */
  218. struct bm_position {
  219. struct zone_bitmap *zone_bm;
  220. struct bm_block *block;
  221. int chunk;
  222. int bit;
  223. };
  224. struct memory_bitmap {
  225. struct zone_bitmap *zone_bm_list; /* list of zone bitmaps */
  226. struct linked_page *p_list; /* list of pages used to store zone
  227. * bitmap objects and bitmap block
  228. * objects
  229. */
  230. struct bm_position cur; /* most recently used bit position */
  231. };
  232. /* Functions that operate on memory bitmaps */
  233. static inline void memory_bm_reset_chunk(struct memory_bitmap *bm)
  234. {
  235. bm->cur.chunk = 0;
  236. bm->cur.bit = -1;
  237. }
  238. static void memory_bm_position_reset(struct memory_bitmap *bm)
  239. {
  240. struct zone_bitmap *zone_bm;
  241. zone_bm = bm->zone_bm_list;
  242. bm->cur.zone_bm = zone_bm;
  243. bm->cur.block = zone_bm->bm_blocks;
  244. memory_bm_reset_chunk(bm);
  245. }
  246. static void memory_bm_free(struct memory_bitmap *bm, int clear_nosave_free);
  247. /**
  248. * create_bm_block_list - create a list of block bitmap objects
  249. */
  250. static inline struct bm_block *
  251. create_bm_block_list(unsigned int nr_blocks, struct chain_allocator *ca)
  252. {
  253. struct bm_block *bblist = NULL;
  254. while (nr_blocks-- > 0) {
  255. struct bm_block *bb;
  256. bb = chain_alloc(ca, sizeof(struct bm_block));
  257. if (!bb)
  258. return NULL;
  259. bb->next = bblist;
  260. bblist = bb;
  261. }
  262. return bblist;
  263. }
  264. /**
  265. * create_zone_bm_list - create a list of zone bitmap objects
  266. */
  267. static inline struct zone_bitmap *
  268. create_zone_bm_list(unsigned int nr_zones, struct chain_allocator *ca)
  269. {
  270. struct zone_bitmap *zbmlist = NULL;
  271. while (nr_zones-- > 0) {
  272. struct zone_bitmap *zbm;
  273. zbm = chain_alloc(ca, sizeof(struct zone_bitmap));
  274. if (!zbm)
  275. return NULL;
  276. zbm->next = zbmlist;
  277. zbmlist = zbm;
  278. }
  279. return zbmlist;
  280. }
  281. /**
  282. * memory_bm_create - allocate memory for a memory bitmap
  283. */
  284. static int
  285. memory_bm_create(struct memory_bitmap *bm, gfp_t gfp_mask, int safe_needed)
  286. {
  287. struct chain_allocator ca;
  288. struct zone *zone;
  289. struct zone_bitmap *zone_bm;
  290. struct bm_block *bb;
  291. unsigned int nr;
  292. chain_init(&ca, gfp_mask, safe_needed);
  293. /* Compute the number of zones */
  294. nr = 0;
  295. for_each_zone(zone)
  296. if (populated_zone(zone))
  297. nr++;
  298. /* Allocate the list of zones bitmap objects */
  299. zone_bm = create_zone_bm_list(nr, &ca);
  300. bm->zone_bm_list = zone_bm;
  301. if (!zone_bm) {
  302. chain_free(&ca, PG_UNSAFE_CLEAR);
  303. return -ENOMEM;
  304. }
  305. /* Initialize the zone bitmap objects */
  306. for_each_zone(zone) {
  307. unsigned long pfn;
  308. if (!populated_zone(zone))
  309. continue;
  310. zone_bm->start_pfn = zone->zone_start_pfn;
  311. zone_bm->end_pfn = zone->zone_start_pfn + zone->spanned_pages;
  312. /* Allocate the list of bitmap block objects */
  313. nr = DIV_ROUND_UP(zone->spanned_pages, BM_BITS_PER_BLOCK);
  314. bb = create_bm_block_list(nr, &ca);
  315. zone_bm->bm_blocks = bb;
  316. zone_bm->cur_block = bb;
  317. if (!bb)
  318. goto Free;
  319. nr = zone->spanned_pages;
  320. pfn = zone->zone_start_pfn;
  321. /* Initialize the bitmap block objects */
  322. while (bb) {
  323. unsigned long *ptr;
  324. ptr = get_image_page(gfp_mask, safe_needed);
  325. bb->data = ptr;
  326. if (!ptr)
  327. goto Free;
  328. bb->start_pfn = pfn;
  329. if (nr >= BM_BITS_PER_BLOCK) {
  330. pfn += BM_BITS_PER_BLOCK;
  331. bb->size = BM_CHUNKS_PER_BLOCK;
  332. nr -= BM_BITS_PER_BLOCK;
  333. } else {
  334. /* This is executed only once in the loop */
  335. pfn += nr;
  336. bb->size = DIV_ROUND_UP(nr, BM_BITS_PER_CHUNK);
  337. }
  338. bb->end_pfn = pfn;
  339. bb = bb->next;
  340. }
  341. zone_bm = zone_bm->next;
  342. }
  343. bm->p_list = ca.chain;
  344. memory_bm_position_reset(bm);
  345. return 0;
  346. Free:
  347. bm->p_list = ca.chain;
  348. memory_bm_free(bm, PG_UNSAFE_CLEAR);
  349. return -ENOMEM;
  350. }
  351. /**
  352. * memory_bm_free - free memory occupied by the memory bitmap @bm
  353. */
  354. static void memory_bm_free(struct memory_bitmap *bm, int clear_nosave_free)
  355. {
  356. struct zone_bitmap *zone_bm;
  357. /* Free the list of bit blocks for each zone_bitmap object */
  358. zone_bm = bm->zone_bm_list;
  359. while (zone_bm) {
  360. struct bm_block *bb;
  361. bb = zone_bm->bm_blocks;
  362. while (bb) {
  363. if (bb->data)
  364. free_image_page(bb->data, clear_nosave_free);
  365. bb = bb->next;
  366. }
  367. zone_bm = zone_bm->next;
  368. }
  369. free_list_of_pages(bm->p_list, clear_nosave_free);
  370. bm->zone_bm_list = NULL;
  371. }
  372. /**
  373. * memory_bm_find_bit - find the bit in the bitmap @bm that corresponds
  374. * to given pfn. The cur_zone_bm member of @bm and the cur_block member
  375. * of @bm->cur_zone_bm are updated.
  376. */
  377. static void memory_bm_find_bit(struct memory_bitmap *bm, unsigned long pfn,
  378. void **addr, unsigned int *bit_nr)
  379. {
  380. struct zone_bitmap *zone_bm;
  381. struct bm_block *bb;
  382. /* Check if the pfn is from the current zone */
  383. zone_bm = bm->cur.zone_bm;
  384. if (pfn < zone_bm->start_pfn || pfn >= zone_bm->end_pfn) {
  385. zone_bm = bm->zone_bm_list;
  386. /* We don't assume that the zones are sorted by pfns */
  387. while (pfn < zone_bm->start_pfn || pfn >= zone_bm->end_pfn) {
  388. zone_bm = zone_bm->next;
  389. BUG_ON(!zone_bm);
  390. }
  391. bm->cur.zone_bm = zone_bm;
  392. }
  393. /* Check if the pfn corresponds to the current bitmap block */
  394. bb = zone_bm->cur_block;
  395. if (pfn < bb->start_pfn)
  396. bb = zone_bm->bm_blocks;
  397. while (pfn >= bb->end_pfn) {
  398. bb = bb->next;
  399. BUG_ON(!bb);
  400. }
  401. zone_bm->cur_block = bb;
  402. pfn -= bb->start_pfn;
  403. *bit_nr = pfn % BM_BITS_PER_CHUNK;
  404. *addr = bb->data + pfn / BM_BITS_PER_CHUNK;
  405. }
  406. static void memory_bm_set_bit(struct memory_bitmap *bm, unsigned long pfn)
  407. {
  408. void *addr;
  409. unsigned int bit;
  410. memory_bm_find_bit(bm, pfn, &addr, &bit);
  411. set_bit(bit, addr);
  412. }
  413. static void memory_bm_clear_bit(struct memory_bitmap *bm, unsigned long pfn)
  414. {
  415. void *addr;
  416. unsigned int bit;
  417. memory_bm_find_bit(bm, pfn, &addr, &bit);
  418. clear_bit(bit, addr);
  419. }
  420. static int memory_bm_test_bit(struct memory_bitmap *bm, unsigned long pfn)
  421. {
  422. void *addr;
  423. unsigned int bit;
  424. memory_bm_find_bit(bm, pfn, &addr, &bit);
  425. return test_bit(bit, addr);
  426. }
  427. /* Two auxiliary functions for memory_bm_next_pfn */
  428. /* Find the first set bit in the given chunk, if there is one */
  429. static inline int next_bit_in_chunk(int bit, unsigned long *chunk_p)
  430. {
  431. bit++;
  432. while (bit < BM_BITS_PER_CHUNK) {
  433. if (test_bit(bit, chunk_p))
  434. return bit;
  435. bit++;
  436. }
  437. return -1;
  438. }
  439. /* Find a chunk containing some bits set in given block of bits */
  440. static inline int next_chunk_in_block(int n, struct bm_block *bb)
  441. {
  442. n++;
  443. while (n < bb->size) {
  444. if (bb->data[n])
  445. return n;
  446. n++;
  447. }
  448. return -1;
  449. }
  450. /**
  451. * memory_bm_next_pfn - find the pfn that corresponds to the next set bit
  452. * in the bitmap @bm. If the pfn cannot be found, BM_END_OF_MAP is
  453. * returned.
  454. *
  455. * It is required to run memory_bm_position_reset() before the first call to
  456. * this function.
  457. */
  458. static unsigned long memory_bm_next_pfn(struct memory_bitmap *bm)
  459. {
  460. struct zone_bitmap *zone_bm;
  461. struct bm_block *bb;
  462. int chunk;
  463. int bit;
  464. do {
  465. bb = bm->cur.block;
  466. do {
  467. chunk = bm->cur.chunk;
  468. bit = bm->cur.bit;
  469. do {
  470. bit = next_bit_in_chunk(bit, bb->data + chunk);
  471. if (bit >= 0)
  472. goto Return_pfn;
  473. chunk = next_chunk_in_block(chunk, bb);
  474. bit = -1;
  475. } while (chunk >= 0);
  476. bb = bb->next;
  477. bm->cur.block = bb;
  478. memory_bm_reset_chunk(bm);
  479. } while (bb);
  480. zone_bm = bm->cur.zone_bm->next;
  481. if (zone_bm) {
  482. bm->cur.zone_bm = zone_bm;
  483. bm->cur.block = zone_bm->bm_blocks;
  484. memory_bm_reset_chunk(bm);
  485. }
  486. } while (zone_bm);
  487. memory_bm_position_reset(bm);
  488. return BM_END_OF_MAP;
  489. Return_pfn:
  490. bm->cur.chunk = chunk;
  491. bm->cur.bit = bit;
  492. return bb->start_pfn + chunk * BM_BITS_PER_CHUNK + bit;
  493. }
  494. /**
  495. * This structure represents a range of page frames the contents of which
  496. * should not be saved during the suspend.
  497. */
  498. struct nosave_region {
  499. struct list_head list;
  500. unsigned long start_pfn;
  501. unsigned long end_pfn;
  502. };
  503. static LIST_HEAD(nosave_regions);
  504. /**
  505. * register_nosave_region - register a range of page frames the contents
  506. * of which should not be saved during the suspend (to be used in the early
  507. * initialization code)
  508. */
  509. void __init
  510. register_nosave_region(unsigned long start_pfn, unsigned long end_pfn)
  511. {
  512. struct nosave_region *region;
  513. if (start_pfn >= end_pfn)
  514. return;
  515. if (!list_empty(&nosave_regions)) {
  516. /* Try to extend the previous region (they should be sorted) */
  517. region = list_entry(nosave_regions.prev,
  518. struct nosave_region, list);
  519. if (region->end_pfn == start_pfn) {
  520. region->end_pfn = end_pfn;
  521. goto Report;
  522. }
  523. }
  524. /* This allocation cannot fail */
  525. region = alloc_bootmem_low(sizeof(struct nosave_region));
  526. region->start_pfn = start_pfn;
  527. region->end_pfn = end_pfn;
  528. list_add_tail(&region->list, &nosave_regions);
  529. Report:
  530. printk("swsusp: Registered nosave memory region: %016lx - %016lx\n",
  531. start_pfn << PAGE_SHIFT, end_pfn << PAGE_SHIFT);
  532. }
  533. /*
  534. * Set bits in this map correspond to the page frames the contents of which
  535. * should not be saved during the suspend.
  536. */
  537. static struct memory_bitmap *forbidden_pages_map;
  538. /* Set bits in this map correspond to free page frames. */
  539. static struct memory_bitmap *free_pages_map;
  540. /*
  541. * Each page frame allocated for creating the image is marked by setting the
  542. * corresponding bits in forbidden_pages_map and free_pages_map simultaneously
  543. */
  544. void swsusp_set_page_free(struct page *page)
  545. {
  546. if (free_pages_map)
  547. memory_bm_set_bit(free_pages_map, page_to_pfn(page));
  548. }
  549. static int swsusp_page_is_free(struct page *page)
  550. {
  551. return free_pages_map ?
  552. memory_bm_test_bit(free_pages_map, page_to_pfn(page)) : 0;
  553. }
  554. void swsusp_unset_page_free(struct page *page)
  555. {
  556. if (free_pages_map)
  557. memory_bm_clear_bit(free_pages_map, page_to_pfn(page));
  558. }
  559. static void swsusp_set_page_forbidden(struct page *page)
  560. {
  561. if (forbidden_pages_map)
  562. memory_bm_set_bit(forbidden_pages_map, page_to_pfn(page));
  563. }
  564. int swsusp_page_is_forbidden(struct page *page)
  565. {
  566. return forbidden_pages_map ?
  567. memory_bm_test_bit(forbidden_pages_map, page_to_pfn(page)) : 0;
  568. }
  569. static void swsusp_unset_page_forbidden(struct page *page)
  570. {
  571. if (forbidden_pages_map)
  572. memory_bm_clear_bit(forbidden_pages_map, page_to_pfn(page));
  573. }
  574. /**
  575. * mark_nosave_pages - set bits corresponding to the page frames the
  576. * contents of which should not be saved in a given bitmap.
  577. */
  578. static void mark_nosave_pages(struct memory_bitmap *bm)
  579. {
  580. struct nosave_region *region;
  581. if (list_empty(&nosave_regions))
  582. return;
  583. list_for_each_entry(region, &nosave_regions, list) {
  584. unsigned long pfn;
  585. printk("swsusp: Marking nosave pages: %016lx - %016lx\n",
  586. region->start_pfn << PAGE_SHIFT,
  587. region->end_pfn << PAGE_SHIFT);
  588. for (pfn = region->start_pfn; pfn < region->end_pfn; pfn++)
  589. memory_bm_set_bit(bm, pfn);
  590. }
  591. }
  592. /**
  593. * create_basic_memory_bitmaps - create bitmaps needed for marking page
  594. * frames that should not be saved and free page frames. The pointers
  595. * forbidden_pages_map and free_pages_map are only modified if everything
  596. * goes well, because we don't want the bits to be used before both bitmaps
  597. * are set up.
  598. */
  599. int create_basic_memory_bitmaps(void)
  600. {
  601. struct memory_bitmap *bm1, *bm2;
  602. int error = 0;
  603. BUG_ON(forbidden_pages_map || free_pages_map);
  604. bm1 = kzalloc(sizeof(struct memory_bitmap), GFP_ATOMIC);
  605. if (!bm1)
  606. return -ENOMEM;
  607. error = memory_bm_create(bm1, GFP_ATOMIC | __GFP_COLD, PG_ANY);
  608. if (error)
  609. goto Free_first_object;
  610. bm2 = kzalloc(sizeof(struct memory_bitmap), GFP_ATOMIC);
  611. if (!bm2)
  612. goto Free_first_bitmap;
  613. error = memory_bm_create(bm2, GFP_ATOMIC | __GFP_COLD, PG_ANY);
  614. if (error)
  615. goto Free_second_object;
  616. forbidden_pages_map = bm1;
  617. free_pages_map = bm2;
  618. mark_nosave_pages(forbidden_pages_map);
  619. printk("swsusp: Basic memory bitmaps created\n");
  620. return 0;
  621. Free_second_object:
  622. kfree(bm2);
  623. Free_first_bitmap:
  624. memory_bm_free(bm1, PG_UNSAFE_CLEAR);
  625. Free_first_object:
  626. kfree(bm1);
  627. return -ENOMEM;
  628. }
  629. /**
  630. * free_basic_memory_bitmaps - free memory bitmaps allocated by
  631. * create_basic_memory_bitmaps(). The auxiliary pointers are necessary
  632. * so that the bitmaps themselves are not referred to while they are being
  633. * freed.
  634. */
  635. void free_basic_memory_bitmaps(void)
  636. {
  637. struct memory_bitmap *bm1, *bm2;
  638. BUG_ON(!(forbidden_pages_map && free_pages_map));
  639. bm1 = forbidden_pages_map;
  640. bm2 = free_pages_map;
  641. forbidden_pages_map = NULL;
  642. free_pages_map = NULL;
  643. memory_bm_free(bm1, PG_UNSAFE_CLEAR);
  644. kfree(bm1);
  645. memory_bm_free(bm2, PG_UNSAFE_CLEAR);
  646. kfree(bm2);
  647. printk("swsusp: Basic memory bitmaps freed\n");
  648. }
  649. /**
  650. * snapshot_additional_pages - estimate the number of additional pages
  651. * be needed for setting up the suspend image data structures for given
  652. * zone (usually the returned value is greater than the exact number)
  653. */
  654. unsigned int snapshot_additional_pages(struct zone *zone)
  655. {
  656. unsigned int res;
  657. res = DIV_ROUND_UP(zone->spanned_pages, BM_BITS_PER_BLOCK);
  658. res += DIV_ROUND_UP(res * sizeof(struct bm_block), PAGE_SIZE);
  659. return 2 * res;
  660. }
  661. #ifdef CONFIG_HIGHMEM
  662. /**
  663. * count_free_highmem_pages - compute the total number of free highmem
  664. * pages, system-wide.
  665. */
  666. static unsigned int count_free_highmem_pages(void)
  667. {
  668. struct zone *zone;
  669. unsigned int cnt = 0;
  670. for_each_zone(zone)
  671. if (populated_zone(zone) && is_highmem(zone))
  672. cnt += zone_page_state(zone, NR_FREE_PAGES);
  673. return cnt;
  674. }
  675. /**
  676. * saveable_highmem_page - Determine whether a highmem page should be
  677. * included in the suspend image.
  678. *
  679. * We should save the page if it isn't Nosave or NosaveFree, or Reserved,
  680. * and it isn't a part of a free chunk of pages.
  681. */
  682. static struct page *saveable_highmem_page(unsigned long pfn)
  683. {
  684. struct page *page;
  685. if (!pfn_valid(pfn))
  686. return NULL;
  687. page = pfn_to_page(pfn);
  688. BUG_ON(!PageHighMem(page));
  689. if (swsusp_page_is_forbidden(page) || swsusp_page_is_free(page) ||
  690. PageReserved(page))
  691. return NULL;
  692. return page;
  693. }
  694. /**
  695. * count_highmem_pages - compute the total number of saveable highmem
  696. * pages.
  697. */
  698. unsigned int count_highmem_pages(void)
  699. {
  700. struct zone *zone;
  701. unsigned int n = 0;
  702. for_each_zone(zone) {
  703. unsigned long pfn, max_zone_pfn;
  704. if (!is_highmem(zone))
  705. continue;
  706. mark_free_pages(zone);
  707. max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
  708. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
  709. if (saveable_highmem_page(pfn))
  710. n++;
  711. }
  712. return n;
  713. }
  714. #else
  715. static inline void *saveable_highmem_page(unsigned long pfn) { return NULL; }
  716. static inline unsigned int count_highmem_pages(void) { return 0; }
  717. #endif /* CONFIG_HIGHMEM */
  718. /**
  719. * saveable - Determine whether a non-highmem page should be included in
  720. * the suspend image.
  721. *
  722. * We should save the page if it isn't Nosave, and is not in the range
  723. * of pages statically defined as 'unsaveable', and it isn't a part of
  724. * a free chunk of pages.
  725. */
  726. static struct page *saveable_page(unsigned long pfn)
  727. {
  728. struct page *page;
  729. if (!pfn_valid(pfn))
  730. return NULL;
  731. page = pfn_to_page(pfn);
  732. BUG_ON(PageHighMem(page));
  733. if (swsusp_page_is_forbidden(page) || swsusp_page_is_free(page))
  734. return NULL;
  735. if (PageReserved(page) && pfn_is_nosave(pfn))
  736. return NULL;
  737. return page;
  738. }
  739. /**
  740. * count_data_pages - compute the total number of saveable non-highmem
  741. * pages.
  742. */
  743. unsigned int count_data_pages(void)
  744. {
  745. struct zone *zone;
  746. unsigned long pfn, max_zone_pfn;
  747. unsigned int n = 0;
  748. for_each_zone(zone) {
  749. if (is_highmem(zone))
  750. continue;
  751. mark_free_pages(zone);
  752. max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
  753. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
  754. if(saveable_page(pfn))
  755. n++;
  756. }
  757. return n;
  758. }
  759. /* This is needed, because copy_page and memcpy are not usable for copying
  760. * task structs.
  761. */
  762. static inline void do_copy_page(long *dst, long *src)
  763. {
  764. int n;
  765. for (n = PAGE_SIZE / sizeof(long); n; n--)
  766. *dst++ = *src++;
  767. }
  768. #ifdef CONFIG_HIGHMEM
  769. static inline struct page *
  770. page_is_saveable(struct zone *zone, unsigned long pfn)
  771. {
  772. return is_highmem(zone) ?
  773. saveable_highmem_page(pfn) : saveable_page(pfn);
  774. }
  775. static inline void
  776. copy_data_page(unsigned long dst_pfn, unsigned long src_pfn)
  777. {
  778. struct page *s_page, *d_page;
  779. void *src, *dst;
  780. s_page = pfn_to_page(src_pfn);
  781. d_page = pfn_to_page(dst_pfn);
  782. if (PageHighMem(s_page)) {
  783. src = kmap_atomic(s_page, KM_USER0);
  784. dst = kmap_atomic(d_page, KM_USER1);
  785. do_copy_page(dst, src);
  786. kunmap_atomic(src, KM_USER0);
  787. kunmap_atomic(dst, KM_USER1);
  788. } else {
  789. src = page_address(s_page);
  790. if (PageHighMem(d_page)) {
  791. /* Page pointed to by src may contain some kernel
  792. * data modified by kmap_atomic()
  793. */
  794. do_copy_page(buffer, src);
  795. dst = kmap_atomic(pfn_to_page(dst_pfn), KM_USER0);
  796. memcpy(dst, buffer, PAGE_SIZE);
  797. kunmap_atomic(dst, KM_USER0);
  798. } else {
  799. dst = page_address(d_page);
  800. do_copy_page(dst, src);
  801. }
  802. }
  803. }
  804. #else
  805. #define page_is_saveable(zone, pfn) saveable_page(pfn)
  806. static inline void
  807. copy_data_page(unsigned long dst_pfn, unsigned long src_pfn)
  808. {
  809. do_copy_page(page_address(pfn_to_page(dst_pfn)),
  810. page_address(pfn_to_page(src_pfn)));
  811. }
  812. #endif /* CONFIG_HIGHMEM */
  813. static void
  814. copy_data_pages(struct memory_bitmap *copy_bm, struct memory_bitmap *orig_bm)
  815. {
  816. struct zone *zone;
  817. unsigned long pfn;
  818. for_each_zone(zone) {
  819. unsigned long max_zone_pfn;
  820. mark_free_pages(zone);
  821. max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
  822. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
  823. if (page_is_saveable(zone, pfn))
  824. memory_bm_set_bit(orig_bm, pfn);
  825. }
  826. memory_bm_position_reset(orig_bm);
  827. memory_bm_position_reset(copy_bm);
  828. do {
  829. pfn = memory_bm_next_pfn(orig_bm);
  830. if (likely(pfn != BM_END_OF_MAP))
  831. copy_data_page(memory_bm_next_pfn(copy_bm), pfn);
  832. } while (pfn != BM_END_OF_MAP);
  833. }
  834. /* Total number of image pages */
  835. static unsigned int nr_copy_pages;
  836. /* Number of pages needed for saving the original pfns of the image pages */
  837. static unsigned int nr_meta_pages;
  838. /**
  839. * swsusp_free - free pages allocated for the suspend.
  840. *
  841. * Suspend pages are alocated before the atomic copy is made, so we
  842. * need to release them after the resume.
  843. */
  844. void swsusp_free(void)
  845. {
  846. struct zone *zone;
  847. unsigned long pfn, max_zone_pfn;
  848. for_each_zone(zone) {
  849. max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
  850. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
  851. if (pfn_valid(pfn)) {
  852. struct page *page = pfn_to_page(pfn);
  853. if (swsusp_page_is_forbidden(page) &&
  854. swsusp_page_is_free(page)) {
  855. swsusp_unset_page_forbidden(page);
  856. swsusp_unset_page_free(page);
  857. __free_page(page);
  858. }
  859. }
  860. }
  861. nr_copy_pages = 0;
  862. nr_meta_pages = 0;
  863. restore_pblist = NULL;
  864. buffer = NULL;
  865. }
  866. #ifdef CONFIG_HIGHMEM
  867. /**
  868. * count_pages_for_highmem - compute the number of non-highmem pages
  869. * that will be necessary for creating copies of highmem pages.
  870. */
  871. static unsigned int count_pages_for_highmem(unsigned int nr_highmem)
  872. {
  873. unsigned int free_highmem = count_free_highmem_pages();
  874. if (free_highmem >= nr_highmem)
  875. nr_highmem = 0;
  876. else
  877. nr_highmem -= free_highmem;
  878. return nr_highmem;
  879. }
  880. #else
  881. static unsigned int
  882. count_pages_for_highmem(unsigned int nr_highmem) { return 0; }
  883. #endif /* CONFIG_HIGHMEM */
  884. /**
  885. * enough_free_mem - Make sure we have enough free memory for the
  886. * snapshot image.
  887. */
  888. static int enough_free_mem(unsigned int nr_pages, unsigned int nr_highmem)
  889. {
  890. struct zone *zone;
  891. unsigned int free = 0, meta = 0;
  892. for_each_zone(zone) {
  893. meta += snapshot_additional_pages(zone);
  894. if (!is_highmem(zone))
  895. free += zone_page_state(zone, NR_FREE_PAGES);
  896. }
  897. nr_pages += count_pages_for_highmem(nr_highmem);
  898. pr_debug("swsusp: Normal pages needed: %u + %u + %u, available pages: %u\n",
  899. nr_pages, PAGES_FOR_IO, meta, free);
  900. return free > nr_pages + PAGES_FOR_IO + meta;
  901. }
  902. #ifdef CONFIG_HIGHMEM
  903. /**
  904. * get_highmem_buffer - if there are some highmem pages in the suspend
  905. * image, we may need the buffer to copy them and/or load their data.
  906. */
  907. static inline int get_highmem_buffer(int safe_needed)
  908. {
  909. buffer = get_image_page(GFP_ATOMIC | __GFP_COLD, safe_needed);
  910. return buffer ? 0 : -ENOMEM;
  911. }
  912. /**
  913. * alloc_highmem_image_pages - allocate some highmem pages for the image.
  914. * Try to allocate as many pages as needed, but if the number of free
  915. * highmem pages is lesser than that, allocate them all.
  916. */
  917. static inline unsigned int
  918. alloc_highmem_image_pages(struct memory_bitmap *bm, unsigned int nr_highmem)
  919. {
  920. unsigned int to_alloc = count_free_highmem_pages();
  921. if (to_alloc > nr_highmem)
  922. to_alloc = nr_highmem;
  923. nr_highmem -= to_alloc;
  924. while (to_alloc-- > 0) {
  925. struct page *page;
  926. page = alloc_image_page(__GFP_HIGHMEM);
  927. memory_bm_set_bit(bm, page_to_pfn(page));
  928. }
  929. return nr_highmem;
  930. }
  931. #else
  932. static inline int get_highmem_buffer(int safe_needed) { return 0; }
  933. static inline unsigned int
  934. alloc_highmem_image_pages(struct memory_bitmap *bm, unsigned int n) { return 0; }
  935. #endif /* CONFIG_HIGHMEM */
  936. /**
  937. * swsusp_alloc - allocate memory for the suspend image
  938. *
  939. * We first try to allocate as many highmem pages as there are
  940. * saveable highmem pages in the system. If that fails, we allocate
  941. * non-highmem pages for the copies of the remaining highmem ones.
  942. *
  943. * In this approach it is likely that the copies of highmem pages will
  944. * also be located in the high memory, because of the way in which
  945. * copy_data_pages() works.
  946. */
  947. static int
  948. swsusp_alloc(struct memory_bitmap *orig_bm, struct memory_bitmap *copy_bm,
  949. unsigned int nr_pages, unsigned int nr_highmem)
  950. {
  951. int error;
  952. error = memory_bm_create(orig_bm, GFP_ATOMIC | __GFP_COLD, PG_ANY);
  953. if (error)
  954. goto Free;
  955. error = memory_bm_create(copy_bm, GFP_ATOMIC | __GFP_COLD, PG_ANY);
  956. if (error)
  957. goto Free;
  958. if (nr_highmem > 0) {
  959. error = get_highmem_buffer(PG_ANY);
  960. if (error)
  961. goto Free;
  962. nr_pages += alloc_highmem_image_pages(copy_bm, nr_highmem);
  963. }
  964. while (nr_pages-- > 0) {
  965. struct page *page = alloc_image_page(GFP_ATOMIC | __GFP_COLD);
  966. if (!page)
  967. goto Free;
  968. memory_bm_set_bit(copy_bm, page_to_pfn(page));
  969. }
  970. return 0;
  971. Free:
  972. swsusp_free();
  973. return -ENOMEM;
  974. }
  975. /* Memory bitmap used for marking saveable pages (during suspend) or the
  976. * suspend image pages (during resume)
  977. */
  978. static struct memory_bitmap orig_bm;
  979. /* Memory bitmap used on suspend for marking allocated pages that will contain
  980. * the copies of saveable pages. During resume it is initially used for
  981. * marking the suspend image pages, but then its set bits are duplicated in
  982. * @orig_bm and it is released. Next, on systems with high memory, it may be
  983. * used for marking "safe" highmem pages, but it has to be reinitialized for
  984. * this purpose.
  985. */
  986. static struct memory_bitmap copy_bm;
  987. asmlinkage int swsusp_save(void)
  988. {
  989. unsigned int nr_pages, nr_highmem;
  990. printk("swsusp: critical section: \n");
  991. drain_local_pages();
  992. nr_pages = count_data_pages();
  993. nr_highmem = count_highmem_pages();
  994. printk("swsusp: Need to copy %u pages\n", nr_pages + nr_highmem);
  995. if (!enough_free_mem(nr_pages, nr_highmem)) {
  996. printk(KERN_ERR "swsusp: Not enough free memory\n");
  997. return -ENOMEM;
  998. }
  999. if (swsusp_alloc(&orig_bm, &copy_bm, nr_pages, nr_highmem)) {
  1000. printk(KERN_ERR "swsusp: Memory allocation failed\n");
  1001. return -ENOMEM;
  1002. }
  1003. /* During allocating of suspend pagedir, new cold pages may appear.
  1004. * Kill them.
  1005. */
  1006. drain_local_pages();
  1007. copy_data_pages(&copy_bm, &orig_bm);
  1008. /*
  1009. * End of critical section. From now on, we can write to memory,
  1010. * but we should not touch disk. This specially means we must _not_
  1011. * touch swap space! Except we must write out our image of course.
  1012. */
  1013. nr_pages += nr_highmem;
  1014. nr_copy_pages = nr_pages;
  1015. nr_meta_pages = DIV_ROUND_UP(nr_pages * sizeof(long), PAGE_SIZE);
  1016. printk("swsusp: critical section/: done (%d pages copied)\n", nr_pages);
  1017. return 0;
  1018. }
  1019. static void init_header(struct swsusp_info *info)
  1020. {
  1021. memset(info, 0, sizeof(struct swsusp_info));
  1022. info->version_code = LINUX_VERSION_CODE;
  1023. info->num_physpages = num_physpages;
  1024. memcpy(&info->uts, init_utsname(), sizeof(struct new_utsname));
  1025. info->cpus = num_online_cpus();
  1026. info->image_pages = nr_copy_pages;
  1027. info->pages = nr_copy_pages + nr_meta_pages + 1;
  1028. info->size = info->pages;
  1029. info->size <<= PAGE_SHIFT;
  1030. }
  1031. /**
  1032. * pack_pfns - pfns corresponding to the set bits found in the bitmap @bm
  1033. * are stored in the array @buf[] (1 page at a time)
  1034. */
  1035. static inline void
  1036. pack_pfns(unsigned long *buf, struct memory_bitmap *bm)
  1037. {
  1038. int j;
  1039. for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
  1040. buf[j] = memory_bm_next_pfn(bm);
  1041. if (unlikely(buf[j] == BM_END_OF_MAP))
  1042. break;
  1043. }
  1044. }
  1045. /**
  1046. * snapshot_read_next - used for reading the system memory snapshot.
  1047. *
  1048. * On the first call to it @handle should point to a zeroed
  1049. * snapshot_handle structure. The structure gets updated and a pointer
  1050. * to it should be passed to this function every next time.
  1051. *
  1052. * The @count parameter should contain the number of bytes the caller
  1053. * wants to read from the snapshot. It must not be zero.
  1054. *
  1055. * On success the function returns a positive number. Then, the caller
  1056. * is allowed to read up to the returned number of bytes from the memory
  1057. * location computed by the data_of() macro. The number returned
  1058. * may be smaller than @count, but this only happens if the read would
  1059. * cross a page boundary otherwise.
  1060. *
  1061. * The function returns 0 to indicate the end of data stream condition,
  1062. * and a negative number is returned on error. In such cases the
  1063. * structure pointed to by @handle is not updated and should not be used
  1064. * any more.
  1065. */
  1066. int snapshot_read_next(struct snapshot_handle *handle, size_t count)
  1067. {
  1068. if (handle->cur > nr_meta_pages + nr_copy_pages)
  1069. return 0;
  1070. if (!buffer) {
  1071. /* This makes the buffer be freed by swsusp_free() */
  1072. buffer = get_image_page(GFP_ATOMIC, PG_ANY);
  1073. if (!buffer)
  1074. return -ENOMEM;
  1075. }
  1076. if (!handle->offset) {
  1077. init_header((struct swsusp_info *)buffer);
  1078. handle->buffer = buffer;
  1079. memory_bm_position_reset(&orig_bm);
  1080. memory_bm_position_reset(&copy_bm);
  1081. }
  1082. if (handle->prev < handle->cur) {
  1083. if (handle->cur <= nr_meta_pages) {
  1084. memset(buffer, 0, PAGE_SIZE);
  1085. pack_pfns(buffer, &orig_bm);
  1086. } else {
  1087. struct page *page;
  1088. page = pfn_to_page(memory_bm_next_pfn(&copy_bm));
  1089. if (PageHighMem(page)) {
  1090. /* Highmem pages are copied to the buffer,
  1091. * because we can't return with a kmapped
  1092. * highmem page (we may not be called again).
  1093. */
  1094. void *kaddr;
  1095. kaddr = kmap_atomic(page, KM_USER0);
  1096. memcpy(buffer, kaddr, PAGE_SIZE);
  1097. kunmap_atomic(kaddr, KM_USER0);
  1098. handle->buffer = buffer;
  1099. } else {
  1100. handle->buffer = page_address(page);
  1101. }
  1102. }
  1103. handle->prev = handle->cur;
  1104. }
  1105. handle->buf_offset = handle->cur_offset;
  1106. if (handle->cur_offset + count >= PAGE_SIZE) {
  1107. count = PAGE_SIZE - handle->cur_offset;
  1108. handle->cur_offset = 0;
  1109. handle->cur++;
  1110. } else {
  1111. handle->cur_offset += count;
  1112. }
  1113. handle->offset += count;
  1114. return count;
  1115. }
  1116. /**
  1117. * mark_unsafe_pages - mark the pages that cannot be used for storing
  1118. * the image during resume, because they conflict with the pages that
  1119. * had been used before suspend
  1120. */
  1121. static int mark_unsafe_pages(struct memory_bitmap *bm)
  1122. {
  1123. struct zone *zone;
  1124. unsigned long pfn, max_zone_pfn;
  1125. /* Clear page flags */
  1126. for_each_zone(zone) {
  1127. max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
  1128. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
  1129. if (pfn_valid(pfn))
  1130. swsusp_unset_page_free(pfn_to_page(pfn));
  1131. }
  1132. /* Mark pages that correspond to the "original" pfns as "unsafe" */
  1133. memory_bm_position_reset(bm);
  1134. do {
  1135. pfn = memory_bm_next_pfn(bm);
  1136. if (likely(pfn != BM_END_OF_MAP)) {
  1137. if (likely(pfn_valid(pfn)))
  1138. swsusp_set_page_free(pfn_to_page(pfn));
  1139. else
  1140. return -EFAULT;
  1141. }
  1142. } while (pfn != BM_END_OF_MAP);
  1143. allocated_unsafe_pages = 0;
  1144. return 0;
  1145. }
  1146. static void
  1147. duplicate_memory_bitmap(struct memory_bitmap *dst, struct memory_bitmap *src)
  1148. {
  1149. unsigned long pfn;
  1150. memory_bm_position_reset(src);
  1151. pfn = memory_bm_next_pfn(src);
  1152. while (pfn != BM_END_OF_MAP) {
  1153. memory_bm_set_bit(dst, pfn);
  1154. pfn = memory_bm_next_pfn(src);
  1155. }
  1156. }
  1157. static inline int check_header(struct swsusp_info *info)
  1158. {
  1159. char *reason = NULL;
  1160. if (info->version_code != LINUX_VERSION_CODE)
  1161. reason = "kernel version";
  1162. if (info->num_physpages != num_physpages)
  1163. reason = "memory size";
  1164. if (strcmp(info->uts.sysname,init_utsname()->sysname))
  1165. reason = "system type";
  1166. if (strcmp(info->uts.release,init_utsname()->release))
  1167. reason = "kernel release";
  1168. if (strcmp(info->uts.version,init_utsname()->version))
  1169. reason = "version";
  1170. if (strcmp(info->uts.machine,init_utsname()->machine))
  1171. reason = "machine";
  1172. if (reason) {
  1173. printk(KERN_ERR "swsusp: Resume mismatch: %s\n", reason);
  1174. return -EPERM;
  1175. }
  1176. return 0;
  1177. }
  1178. /**
  1179. * load header - check the image header and copy data from it
  1180. */
  1181. static int
  1182. load_header(struct swsusp_info *info)
  1183. {
  1184. int error;
  1185. restore_pblist = NULL;
  1186. error = check_header(info);
  1187. if (!error) {
  1188. nr_copy_pages = info->image_pages;
  1189. nr_meta_pages = info->pages - info->image_pages - 1;
  1190. }
  1191. return error;
  1192. }
  1193. /**
  1194. * unpack_orig_pfns - for each element of @buf[] (1 page at a time) set
  1195. * the corresponding bit in the memory bitmap @bm
  1196. */
  1197. static inline void
  1198. unpack_orig_pfns(unsigned long *buf, struct memory_bitmap *bm)
  1199. {
  1200. int j;
  1201. for (j = 0; j < PAGE_SIZE / sizeof(long); j++) {
  1202. if (unlikely(buf[j] == BM_END_OF_MAP))
  1203. break;
  1204. memory_bm_set_bit(bm, buf[j]);
  1205. }
  1206. }
  1207. /* List of "safe" pages that may be used to store data loaded from the suspend
  1208. * image
  1209. */
  1210. static struct linked_page *safe_pages_list;
  1211. #ifdef CONFIG_HIGHMEM
  1212. /* struct highmem_pbe is used for creating the list of highmem pages that
  1213. * should be restored atomically during the resume from disk, because the page
  1214. * frames they have occupied before the suspend are in use.
  1215. */
  1216. struct highmem_pbe {
  1217. struct page *copy_page; /* data is here now */
  1218. struct page *orig_page; /* data was here before the suspend */
  1219. struct highmem_pbe *next;
  1220. };
  1221. /* List of highmem PBEs needed for restoring the highmem pages that were
  1222. * allocated before the suspend and included in the suspend image, but have
  1223. * also been allocated by the "resume" kernel, so their contents cannot be
  1224. * written directly to their "original" page frames.
  1225. */
  1226. static struct highmem_pbe *highmem_pblist;
  1227. /**
  1228. * count_highmem_image_pages - compute the number of highmem pages in the
  1229. * suspend image. The bits in the memory bitmap @bm that correspond to the
  1230. * image pages are assumed to be set.
  1231. */
  1232. static unsigned int count_highmem_image_pages(struct memory_bitmap *bm)
  1233. {
  1234. unsigned long pfn;
  1235. unsigned int cnt = 0;
  1236. memory_bm_position_reset(bm);
  1237. pfn = memory_bm_next_pfn(bm);
  1238. while (pfn != BM_END_OF_MAP) {
  1239. if (PageHighMem(pfn_to_page(pfn)))
  1240. cnt++;
  1241. pfn = memory_bm_next_pfn(bm);
  1242. }
  1243. return cnt;
  1244. }
  1245. /**
  1246. * prepare_highmem_image - try to allocate as many highmem pages as
  1247. * there are highmem image pages (@nr_highmem_p points to the variable
  1248. * containing the number of highmem image pages). The pages that are
  1249. * "safe" (ie. will not be overwritten when the suspend image is
  1250. * restored) have the corresponding bits set in @bm (it must be
  1251. * unitialized).
  1252. *
  1253. * NOTE: This function should not be called if there are no highmem
  1254. * image pages.
  1255. */
  1256. static unsigned int safe_highmem_pages;
  1257. static struct memory_bitmap *safe_highmem_bm;
  1258. static int
  1259. prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
  1260. {
  1261. unsigned int to_alloc;
  1262. if (memory_bm_create(bm, GFP_ATOMIC, PG_SAFE))
  1263. return -ENOMEM;
  1264. if (get_highmem_buffer(PG_SAFE))
  1265. return -ENOMEM;
  1266. to_alloc = count_free_highmem_pages();
  1267. if (to_alloc > *nr_highmem_p)
  1268. to_alloc = *nr_highmem_p;
  1269. else
  1270. *nr_highmem_p = to_alloc;
  1271. safe_highmem_pages = 0;
  1272. while (to_alloc-- > 0) {
  1273. struct page *page;
  1274. page = alloc_page(__GFP_HIGHMEM);
  1275. if (!swsusp_page_is_free(page)) {
  1276. /* The page is "safe", set its bit the bitmap */
  1277. memory_bm_set_bit(bm, page_to_pfn(page));
  1278. safe_highmem_pages++;
  1279. }
  1280. /* Mark the page as allocated */
  1281. swsusp_set_page_forbidden(page);
  1282. swsusp_set_page_free(page);
  1283. }
  1284. memory_bm_position_reset(bm);
  1285. safe_highmem_bm = bm;
  1286. return 0;
  1287. }
  1288. /**
  1289. * get_highmem_page_buffer - for given highmem image page find the buffer
  1290. * that suspend_write_next() should set for its caller to write to.
  1291. *
  1292. * If the page is to be saved to its "original" page frame or a copy of
  1293. * the page is to be made in the highmem, @buffer is returned. Otherwise,
  1294. * the copy of the page is to be made in normal memory, so the address of
  1295. * the copy is returned.
  1296. *
  1297. * If @buffer is returned, the caller of suspend_write_next() will write
  1298. * the page's contents to @buffer, so they will have to be copied to the
  1299. * right location on the next call to suspend_write_next() and it is done
  1300. * with the help of copy_last_highmem_page(). For this purpose, if
  1301. * @buffer is returned, @last_highmem page is set to the page to which
  1302. * the data will have to be copied from @buffer.
  1303. */
  1304. static struct page *last_highmem_page;
  1305. static void *
  1306. get_highmem_page_buffer(struct page *page, struct chain_allocator *ca)
  1307. {
  1308. struct highmem_pbe *pbe;
  1309. void *kaddr;
  1310. if (swsusp_page_is_forbidden(page) && swsusp_page_is_free(page)) {
  1311. /* We have allocated the "original" page frame and we can
  1312. * use it directly to store the loaded page.
  1313. */
  1314. last_highmem_page = page;
  1315. return buffer;
  1316. }
  1317. /* The "original" page frame has not been allocated and we have to
  1318. * use a "safe" page frame to store the loaded page.
  1319. */
  1320. pbe = chain_alloc(ca, sizeof(struct highmem_pbe));
  1321. if (!pbe) {
  1322. swsusp_free();
  1323. return NULL;
  1324. }
  1325. pbe->orig_page = page;
  1326. if (safe_highmem_pages > 0) {
  1327. struct page *tmp;
  1328. /* Copy of the page will be stored in high memory */
  1329. kaddr = buffer;
  1330. tmp = pfn_to_page(memory_bm_next_pfn(safe_highmem_bm));
  1331. safe_highmem_pages--;
  1332. last_highmem_page = tmp;
  1333. pbe->copy_page = tmp;
  1334. } else {
  1335. /* Copy of the page will be stored in normal memory */
  1336. kaddr = safe_pages_list;
  1337. safe_pages_list = safe_pages_list->next;
  1338. pbe->copy_page = virt_to_page(kaddr);
  1339. }
  1340. pbe->next = highmem_pblist;
  1341. highmem_pblist = pbe;
  1342. return kaddr;
  1343. }
  1344. /**
  1345. * copy_last_highmem_page - copy the contents of a highmem image from
  1346. * @buffer, where the caller of snapshot_write_next() has place them,
  1347. * to the right location represented by @last_highmem_page .
  1348. */
  1349. static void copy_last_highmem_page(void)
  1350. {
  1351. if (last_highmem_page) {
  1352. void *dst;
  1353. dst = kmap_atomic(last_highmem_page, KM_USER0);
  1354. memcpy(dst, buffer, PAGE_SIZE);
  1355. kunmap_atomic(dst, KM_USER0);
  1356. last_highmem_page = NULL;
  1357. }
  1358. }
  1359. static inline int last_highmem_page_copied(void)
  1360. {
  1361. return !last_highmem_page;
  1362. }
  1363. static inline void free_highmem_data(void)
  1364. {
  1365. if (safe_highmem_bm)
  1366. memory_bm_free(safe_highmem_bm, PG_UNSAFE_CLEAR);
  1367. if (buffer)
  1368. free_image_page(buffer, PG_UNSAFE_CLEAR);
  1369. }
  1370. #else
  1371. static inline int get_safe_write_buffer(void) { return 0; }
  1372. static unsigned int
  1373. count_highmem_image_pages(struct memory_bitmap *bm) { return 0; }
  1374. static inline int
  1375. prepare_highmem_image(struct memory_bitmap *bm, unsigned int *nr_highmem_p)
  1376. {
  1377. return 0;
  1378. }
  1379. static inline void *
  1380. get_highmem_page_buffer(struct page *page, struct chain_allocator *ca)
  1381. {
  1382. return NULL;
  1383. }
  1384. static inline void copy_last_highmem_page(void) {}
  1385. static inline int last_highmem_page_copied(void) { return 1; }
  1386. static inline void free_highmem_data(void) {}
  1387. #endif /* CONFIG_HIGHMEM */
  1388. /**
  1389. * prepare_image - use the memory bitmap @bm to mark the pages that will
  1390. * be overwritten in the process of restoring the system memory state
  1391. * from the suspend image ("unsafe" pages) and allocate memory for the
  1392. * image.
  1393. *
  1394. * The idea is to allocate a new memory bitmap first and then allocate
  1395. * as many pages as needed for the image data, but not to assign these
  1396. * pages to specific tasks initially. Instead, we just mark them as
  1397. * allocated and create a lists of "safe" pages that will be used
  1398. * later. On systems with high memory a list of "safe" highmem pages is
  1399. * also created.
  1400. */
  1401. #define PBES_PER_LINKED_PAGE (LINKED_PAGE_DATA_SIZE / sizeof(struct pbe))
  1402. static int
  1403. prepare_image(struct memory_bitmap *new_bm, struct memory_bitmap *bm)
  1404. {
  1405. unsigned int nr_pages, nr_highmem;
  1406. struct linked_page *sp_list, *lp;
  1407. int error;
  1408. /* If there is no highmem, the buffer will not be necessary */
  1409. free_image_page(buffer, PG_UNSAFE_CLEAR);
  1410. buffer = NULL;
  1411. nr_highmem = count_highmem_image_pages(bm);
  1412. error = mark_unsafe_pages(bm);
  1413. if (error)
  1414. goto Free;
  1415. error = memory_bm_create(new_bm, GFP_ATOMIC, PG_SAFE);
  1416. if (error)
  1417. goto Free;
  1418. duplicate_memory_bitmap(new_bm, bm);
  1419. memory_bm_free(bm, PG_UNSAFE_KEEP);
  1420. if (nr_highmem > 0) {
  1421. error = prepare_highmem_image(bm, &nr_highmem);
  1422. if (error)
  1423. goto Free;
  1424. }
  1425. /* Reserve some safe pages for potential later use.
  1426. *
  1427. * NOTE: This way we make sure there will be enough safe pages for the
  1428. * chain_alloc() in get_buffer(). It is a bit wasteful, but
  1429. * nr_copy_pages cannot be greater than 50% of the memory anyway.
  1430. */
  1431. sp_list = NULL;
  1432. /* nr_copy_pages cannot be lesser than allocated_unsafe_pages */
  1433. nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
  1434. nr_pages = DIV_ROUND_UP(nr_pages, PBES_PER_LINKED_PAGE);
  1435. while (nr_pages > 0) {
  1436. lp = get_image_page(GFP_ATOMIC, PG_SAFE);
  1437. if (!lp) {
  1438. error = -ENOMEM;
  1439. goto Free;
  1440. }
  1441. lp->next = sp_list;
  1442. sp_list = lp;
  1443. nr_pages--;
  1444. }
  1445. /* Preallocate memory for the image */
  1446. safe_pages_list = NULL;
  1447. nr_pages = nr_copy_pages - nr_highmem - allocated_unsafe_pages;
  1448. while (nr_pages > 0) {
  1449. lp = (struct linked_page *)get_zeroed_page(GFP_ATOMIC);
  1450. if (!lp) {
  1451. error = -ENOMEM;
  1452. goto Free;
  1453. }
  1454. if (!swsusp_page_is_free(virt_to_page(lp))) {
  1455. /* The page is "safe", add it to the list */
  1456. lp->next = safe_pages_list;
  1457. safe_pages_list = lp;
  1458. }
  1459. /* Mark the page as allocated */
  1460. swsusp_set_page_forbidden(virt_to_page(lp));
  1461. swsusp_set_page_free(virt_to_page(lp));
  1462. nr_pages--;
  1463. }
  1464. /* Free the reserved safe pages so that chain_alloc() can use them */
  1465. while (sp_list) {
  1466. lp = sp_list->next;
  1467. free_image_page(sp_list, PG_UNSAFE_CLEAR);
  1468. sp_list = lp;
  1469. }
  1470. return 0;
  1471. Free:
  1472. swsusp_free();
  1473. return error;
  1474. }
  1475. /**
  1476. * get_buffer - compute the address that snapshot_write_next() should
  1477. * set for its caller to write to.
  1478. */
  1479. static void *get_buffer(struct memory_bitmap *bm, struct chain_allocator *ca)
  1480. {
  1481. struct pbe *pbe;
  1482. struct page *page = pfn_to_page(memory_bm_next_pfn(bm));
  1483. if (PageHighMem(page))
  1484. return get_highmem_page_buffer(page, ca);
  1485. if (swsusp_page_is_forbidden(page) && swsusp_page_is_free(page))
  1486. /* We have allocated the "original" page frame and we can
  1487. * use it directly to store the loaded page.
  1488. */
  1489. return page_address(page);
  1490. /* The "original" page frame has not been allocated and we have to
  1491. * use a "safe" page frame to store the loaded page.
  1492. */
  1493. pbe = chain_alloc(ca, sizeof(struct pbe));
  1494. if (!pbe) {
  1495. swsusp_free();
  1496. return NULL;
  1497. }
  1498. pbe->orig_address = page_address(page);
  1499. pbe->address = safe_pages_list;
  1500. safe_pages_list = safe_pages_list->next;
  1501. pbe->next = restore_pblist;
  1502. restore_pblist = pbe;
  1503. return pbe->address;
  1504. }
  1505. /**
  1506. * snapshot_write_next - used for writing the system memory snapshot.
  1507. *
  1508. * On the first call to it @handle should point to a zeroed
  1509. * snapshot_handle structure. The structure gets updated and a pointer
  1510. * to it should be passed to this function every next time.
  1511. *
  1512. * The @count parameter should contain the number of bytes the caller
  1513. * wants to write to the image. It must not be zero.
  1514. *
  1515. * On success the function returns a positive number. Then, the caller
  1516. * is allowed to write up to the returned number of bytes to the memory
  1517. * location computed by the data_of() macro. The number returned
  1518. * may be smaller than @count, but this only happens if the write would
  1519. * cross a page boundary otherwise.
  1520. *
  1521. * The function returns 0 to indicate the "end of file" condition,
  1522. * and a negative number is returned on error. In such cases the
  1523. * structure pointed to by @handle is not updated and should not be used
  1524. * any more.
  1525. */
  1526. int snapshot_write_next(struct snapshot_handle *handle, size_t count)
  1527. {
  1528. static struct chain_allocator ca;
  1529. int error = 0;
  1530. /* Check if we have already loaded the entire image */
  1531. if (handle->prev && handle->cur > nr_meta_pages + nr_copy_pages)
  1532. return 0;
  1533. if (handle->offset == 0) {
  1534. if (!buffer)
  1535. /* This makes the buffer be freed by swsusp_free() */
  1536. buffer = get_image_page(GFP_ATOMIC, PG_ANY);
  1537. if (!buffer)
  1538. return -ENOMEM;
  1539. handle->buffer = buffer;
  1540. }
  1541. handle->sync_read = 1;
  1542. if (handle->prev < handle->cur) {
  1543. if (handle->prev == 0) {
  1544. error = load_header(buffer);
  1545. if (error)
  1546. return error;
  1547. error = memory_bm_create(&copy_bm, GFP_ATOMIC, PG_ANY);
  1548. if (error)
  1549. return error;
  1550. } else if (handle->prev <= nr_meta_pages) {
  1551. unpack_orig_pfns(buffer, &copy_bm);
  1552. if (handle->prev == nr_meta_pages) {
  1553. error = prepare_image(&orig_bm, &copy_bm);
  1554. if (error)
  1555. return error;
  1556. chain_init(&ca, GFP_ATOMIC, PG_SAFE);
  1557. memory_bm_position_reset(&orig_bm);
  1558. restore_pblist = NULL;
  1559. handle->buffer = get_buffer(&orig_bm, &ca);
  1560. handle->sync_read = 0;
  1561. if (!handle->buffer)
  1562. return -ENOMEM;
  1563. }
  1564. } else {
  1565. copy_last_highmem_page();
  1566. handle->buffer = get_buffer(&orig_bm, &ca);
  1567. if (handle->buffer != buffer)
  1568. handle->sync_read = 0;
  1569. }
  1570. handle->prev = handle->cur;
  1571. }
  1572. handle->buf_offset = handle->cur_offset;
  1573. if (handle->cur_offset + count >= PAGE_SIZE) {
  1574. count = PAGE_SIZE - handle->cur_offset;
  1575. handle->cur_offset = 0;
  1576. handle->cur++;
  1577. } else {
  1578. handle->cur_offset += count;
  1579. }
  1580. handle->offset += count;
  1581. return count;
  1582. }
  1583. /**
  1584. * snapshot_write_finalize - must be called after the last call to
  1585. * snapshot_write_next() in case the last page in the image happens
  1586. * to be a highmem page and its contents should be stored in the
  1587. * highmem. Additionally, it releases the memory that will not be
  1588. * used any more.
  1589. */
  1590. void snapshot_write_finalize(struct snapshot_handle *handle)
  1591. {
  1592. copy_last_highmem_page();
  1593. /* Free only if we have loaded the image entirely */
  1594. if (handle->prev && handle->cur > nr_meta_pages + nr_copy_pages) {
  1595. memory_bm_free(&orig_bm, PG_UNSAFE_CLEAR);
  1596. free_highmem_data();
  1597. }
  1598. }
  1599. int snapshot_image_loaded(struct snapshot_handle *handle)
  1600. {
  1601. return !(!nr_copy_pages || !last_highmem_page_copied() ||
  1602. handle->cur <= nr_meta_pages + nr_copy_pages);
  1603. }
  1604. #ifdef CONFIG_HIGHMEM
  1605. /* Assumes that @buf is ready and points to a "safe" page */
  1606. static inline void
  1607. swap_two_pages_data(struct page *p1, struct page *p2, void *buf)
  1608. {
  1609. void *kaddr1, *kaddr2;
  1610. kaddr1 = kmap_atomic(p1, KM_USER0);
  1611. kaddr2 = kmap_atomic(p2, KM_USER1);
  1612. memcpy(buf, kaddr1, PAGE_SIZE);
  1613. memcpy(kaddr1, kaddr2, PAGE_SIZE);
  1614. memcpy(kaddr2, buf, PAGE_SIZE);
  1615. kunmap_atomic(kaddr1, KM_USER0);
  1616. kunmap_atomic(kaddr2, KM_USER1);
  1617. }
  1618. /**
  1619. * restore_highmem - for each highmem page that was allocated before
  1620. * the suspend and included in the suspend image, and also has been
  1621. * allocated by the "resume" kernel swap its current (ie. "before
  1622. * resume") contents with the previous (ie. "before suspend") one.
  1623. *
  1624. * If the resume eventually fails, we can call this function once
  1625. * again and restore the "before resume" highmem state.
  1626. */
  1627. int restore_highmem(void)
  1628. {
  1629. struct highmem_pbe *pbe = highmem_pblist;
  1630. void *buf;
  1631. if (!pbe)
  1632. return 0;
  1633. buf = get_image_page(GFP_ATOMIC, PG_SAFE);
  1634. if (!buf)
  1635. return -ENOMEM;
  1636. while (pbe) {
  1637. swap_two_pages_data(pbe->copy_page, pbe->orig_page, buf);
  1638. pbe = pbe->next;
  1639. }
  1640. free_image_page(buf, PG_UNSAFE_CLEAR);
  1641. return 0;
  1642. }
  1643. #endif /* CONFIG_HIGHMEM */