swap.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605
  1. /*
  2. * linux/kernel/power/swap.c
  3. *
  4. * This file provides functions for reading the suspend image from
  5. * and writing it to a swap partition.
  6. *
  7. * Copyright (C) 1998,2001-2005 Pavel Machek <pavel@ucw.cz>
  8. * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl>
  9. * Copyright (C) 2010-2012 Bojan Smojver <bojan@rexursive.com>
  10. *
  11. * This file is released under the GPLv2.
  12. *
  13. */
  14. #define pr_fmt(fmt) "PM: " fmt
  15. #include <linux/module.h>
  16. #include <linux/file.h>
  17. #include <linux/delay.h>
  18. #include <linux/bitops.h>
  19. #include <linux/genhd.h>
  20. #include <linux/device.h>
  21. #include <linux/bio.h>
  22. #include <linux/blkdev.h>
  23. #include <linux/swap.h>
  24. #include <linux/swapops.h>
  25. #include <linux/pm.h>
  26. #include <linux/slab.h>
  27. #include <linux/lzo.h>
  28. #include <linux/vmalloc.h>
  29. #include <linux/cpumask.h>
  30. #include <linux/atomic.h>
  31. #include <linux/kthread.h>
  32. #include <linux/crc32.h>
  33. #include <linux/ktime.h>
  34. #include "power.h"
  35. #define HIBERNATE_SIG "S1SUSPEND"
  36. /*
  37. * When reading an {un,}compressed image, we may restore pages in place,
  38. * in which case some architectures need these pages cleaning before they
  39. * can be executed. We don't know which pages these may be, so clean the lot.
  40. */
  41. static bool clean_pages_on_read;
  42. static bool clean_pages_on_decompress;
  43. /*
  44. * The swap map is a data structure used for keeping track of each page
  45. * written to a swap partition. It consists of many swap_map_page
  46. * structures that contain each an array of MAP_PAGE_ENTRIES swap entries.
  47. * These structures are stored on the swap and linked together with the
  48. * help of the .next_swap member.
  49. *
  50. * The swap map is created during suspend. The swap map pages are
  51. * allocated and populated one at a time, so we only need one memory
  52. * page to set up the entire structure.
  53. *
  54. * During resume we pick up all swap_map_page structures into a list.
  55. */
  56. #define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(sector_t) - 1)
  57. /*
  58. * Number of free pages that are not high.
  59. */
  60. static inline unsigned long low_free_pages(void)
  61. {
  62. return nr_free_pages() - nr_free_highpages();
  63. }
  64. /*
  65. * Number of pages required to be kept free while writing the image. Always
  66. * half of all available low pages before the writing starts.
  67. */
  68. static inline unsigned long reqd_free_pages(void)
  69. {
  70. return low_free_pages() / 2;
  71. }
  72. struct swap_map_page {
  73. sector_t entries[MAP_PAGE_ENTRIES];
  74. sector_t next_swap;
  75. };
  76. struct swap_map_page_list {
  77. struct swap_map_page *map;
  78. struct swap_map_page_list *next;
  79. };
  80. /**
  81. * The swap_map_handle structure is used for handling swap in
  82. * a file-alike way
  83. */
  84. struct swap_map_handle {
  85. struct swap_map_page *cur;
  86. struct swap_map_page_list *maps;
  87. sector_t cur_swap;
  88. sector_t first_sector;
  89. unsigned int k;
  90. unsigned long reqd_free_pages;
  91. u32 crc32;
  92. };
  93. struct swsusp_header {
  94. char reserved[PAGE_SIZE - 20 - sizeof(sector_t) - sizeof(int) -
  95. sizeof(u32)];
  96. u32 crc32;
  97. sector_t image;
  98. unsigned int flags; /* Flags to pass to the "boot" kernel */
  99. char orig_sig[10];
  100. char sig[10];
  101. } __packed;
  102. static struct swsusp_header *swsusp_header;
  103. /**
  104. * The following functions are used for tracing the allocated
  105. * swap pages, so that they can be freed in case of an error.
  106. */
  107. struct swsusp_extent {
  108. struct rb_node node;
  109. unsigned long start;
  110. unsigned long end;
  111. };
  112. static struct rb_root swsusp_extents = RB_ROOT;
  113. static int swsusp_extents_insert(unsigned long swap_offset)
  114. {
  115. struct rb_node **new = &(swsusp_extents.rb_node);
  116. struct rb_node *parent = NULL;
  117. struct swsusp_extent *ext;
  118. /* Figure out where to put the new node */
  119. while (*new) {
  120. ext = rb_entry(*new, struct swsusp_extent, node);
  121. parent = *new;
  122. if (swap_offset < ext->start) {
  123. /* Try to merge */
  124. if (swap_offset == ext->start - 1) {
  125. ext->start--;
  126. return 0;
  127. }
  128. new = &((*new)->rb_left);
  129. } else if (swap_offset > ext->end) {
  130. /* Try to merge */
  131. if (swap_offset == ext->end + 1) {
  132. ext->end++;
  133. return 0;
  134. }
  135. new = &((*new)->rb_right);
  136. } else {
  137. /* It already is in the tree */
  138. return -EINVAL;
  139. }
  140. }
  141. /* Add the new node and rebalance the tree. */
  142. ext = kzalloc(sizeof(struct swsusp_extent), GFP_KERNEL);
  143. if (!ext)
  144. return -ENOMEM;
  145. ext->start = swap_offset;
  146. ext->end = swap_offset;
  147. rb_link_node(&ext->node, parent, new);
  148. rb_insert_color(&ext->node, &swsusp_extents);
  149. return 0;
  150. }
  151. /**
  152. * alloc_swapdev_block - allocate a swap page and register that it has
  153. * been allocated, so that it can be freed in case of an error.
  154. */
  155. sector_t alloc_swapdev_block(int swap)
  156. {
  157. unsigned long offset;
  158. offset = swp_offset(get_swap_page_of_type(swap));
  159. if (offset) {
  160. if (swsusp_extents_insert(offset))
  161. swap_free(swp_entry(swap, offset));
  162. else
  163. return swapdev_block(swap, offset);
  164. }
  165. return 0;
  166. }
  167. /**
  168. * free_all_swap_pages - free swap pages allocated for saving image data.
  169. * It also frees the extents used to register which swap entries had been
  170. * allocated.
  171. */
  172. void free_all_swap_pages(int swap)
  173. {
  174. struct rb_node *node;
  175. while ((node = swsusp_extents.rb_node)) {
  176. struct swsusp_extent *ext;
  177. unsigned long offset;
  178. ext = rb_entry(node, struct swsusp_extent, node);
  179. rb_erase(node, &swsusp_extents);
  180. for (offset = ext->start; offset <= ext->end; offset++)
  181. swap_free(swp_entry(swap, offset));
  182. kfree(ext);
  183. }
  184. }
  185. int swsusp_swap_in_use(void)
  186. {
  187. return (swsusp_extents.rb_node != NULL);
  188. }
  189. /*
  190. * General things
  191. */
  192. static unsigned short root_swap = 0xffff;
  193. static struct block_device *hib_resume_bdev;
  194. struct hib_bio_batch {
  195. atomic_t count;
  196. wait_queue_head_t wait;
  197. blk_status_t error;
  198. };
  199. static void hib_init_batch(struct hib_bio_batch *hb)
  200. {
  201. atomic_set(&hb->count, 0);
  202. init_waitqueue_head(&hb->wait);
  203. hb->error = BLK_STS_OK;
  204. }
  205. static void hib_end_io(struct bio *bio)
  206. {
  207. struct hib_bio_batch *hb = bio->bi_private;
  208. struct page *page = bio_first_page_all(bio);
  209. if (bio->bi_status) {
  210. pr_alert("Read-error on swap-device (%u:%u:%Lu)\n",
  211. MAJOR(bio_dev(bio)), MINOR(bio_dev(bio)),
  212. (unsigned long long)bio->bi_iter.bi_sector);
  213. }
  214. if (bio_data_dir(bio) == WRITE)
  215. put_page(page);
  216. else if (clean_pages_on_read)
  217. flush_icache_range((unsigned long)page_address(page),
  218. (unsigned long)page_address(page) + PAGE_SIZE);
  219. if (bio->bi_status && !hb->error)
  220. hb->error = bio->bi_status;
  221. if (atomic_dec_and_test(&hb->count))
  222. wake_up(&hb->wait);
  223. bio_put(bio);
  224. }
  225. static int hib_submit_io(int op, int op_flags, pgoff_t page_off, void *addr,
  226. struct hib_bio_batch *hb)
  227. {
  228. struct page *page = virt_to_page(addr);
  229. struct bio *bio;
  230. int error = 0;
  231. bio = bio_alloc(GFP_NOIO | __GFP_HIGH, 1);
  232. bio->bi_iter.bi_sector = page_off * (PAGE_SIZE >> 9);
  233. bio_set_dev(bio, hib_resume_bdev);
  234. bio_set_op_attrs(bio, op, op_flags);
  235. if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
  236. pr_err("Adding page to bio failed at %llu\n",
  237. (unsigned long long)bio->bi_iter.bi_sector);
  238. bio_put(bio);
  239. return -EFAULT;
  240. }
  241. if (hb) {
  242. bio->bi_end_io = hib_end_io;
  243. bio->bi_private = hb;
  244. atomic_inc(&hb->count);
  245. submit_bio(bio);
  246. } else {
  247. error = submit_bio_wait(bio);
  248. bio_put(bio);
  249. }
  250. return error;
  251. }
  252. static blk_status_t hib_wait_io(struct hib_bio_batch *hb)
  253. {
  254. wait_event(hb->wait, atomic_read(&hb->count) == 0);
  255. return blk_status_to_errno(hb->error);
  256. }
  257. /*
  258. * Saving part
  259. */
  260. static int mark_swapfiles(struct swap_map_handle *handle, unsigned int flags)
  261. {
  262. int error;
  263. hib_submit_io(REQ_OP_READ, 0, swsusp_resume_block,
  264. swsusp_header, NULL);
  265. if (!memcmp("SWAP-SPACE",swsusp_header->sig, 10) ||
  266. !memcmp("SWAPSPACE2",swsusp_header->sig, 10)) {
  267. memcpy(swsusp_header->orig_sig,swsusp_header->sig, 10);
  268. memcpy(swsusp_header->sig, HIBERNATE_SIG, 10);
  269. swsusp_header->image = handle->first_sector;
  270. swsusp_header->flags = flags;
  271. if (flags & SF_CRC32_MODE)
  272. swsusp_header->crc32 = handle->crc32;
  273. error = hib_submit_io(REQ_OP_WRITE, REQ_SYNC,
  274. swsusp_resume_block, swsusp_header, NULL);
  275. } else {
  276. pr_err("Swap header not found!\n");
  277. error = -ENODEV;
  278. }
  279. return error;
  280. }
  281. /**
  282. * swsusp_swap_check - check if the resume device is a swap device
  283. * and get its index (if so)
  284. *
  285. * This is called before saving image
  286. */
  287. static int swsusp_swap_check(void)
  288. {
  289. int res;
  290. res = swap_type_of(swsusp_resume_device, swsusp_resume_block,
  291. &hib_resume_bdev);
  292. if (res < 0)
  293. return res;
  294. root_swap = res;
  295. res = blkdev_get(hib_resume_bdev, FMODE_WRITE, NULL);
  296. if (res)
  297. return res;
  298. res = set_blocksize(hib_resume_bdev, PAGE_SIZE);
  299. if (res < 0)
  300. blkdev_put(hib_resume_bdev, FMODE_WRITE);
  301. /*
  302. * Update the resume device to the one actually used,
  303. * so the test_resume mode can use it in case it is
  304. * invoked from hibernate() to test the snapshot.
  305. */
  306. swsusp_resume_device = hib_resume_bdev->bd_dev;
  307. return res;
  308. }
  309. /**
  310. * write_page - Write one page to given swap location.
  311. * @buf: Address we're writing.
  312. * @offset: Offset of the swap page we're writing to.
  313. * @hb: bio completion batch
  314. */
  315. static int write_page(void *buf, sector_t offset, struct hib_bio_batch *hb)
  316. {
  317. void *src;
  318. int ret;
  319. if (!offset)
  320. return -ENOSPC;
  321. if (hb) {
  322. src = (void *)__get_free_page(GFP_NOIO | __GFP_NOWARN |
  323. __GFP_NORETRY);
  324. if (src) {
  325. copy_page(src, buf);
  326. } else {
  327. ret = hib_wait_io(hb); /* Free pages */
  328. if (ret)
  329. return ret;
  330. src = (void *)__get_free_page(GFP_NOIO |
  331. __GFP_NOWARN |
  332. __GFP_NORETRY);
  333. if (src) {
  334. copy_page(src, buf);
  335. } else {
  336. WARN_ON_ONCE(1);
  337. hb = NULL; /* Go synchronous */
  338. src = buf;
  339. }
  340. }
  341. } else {
  342. src = buf;
  343. }
  344. return hib_submit_io(REQ_OP_WRITE, REQ_SYNC, offset, src, hb);
  345. }
  346. static void release_swap_writer(struct swap_map_handle *handle)
  347. {
  348. if (handle->cur)
  349. free_page((unsigned long)handle->cur);
  350. handle->cur = NULL;
  351. }
  352. static int get_swap_writer(struct swap_map_handle *handle)
  353. {
  354. int ret;
  355. ret = swsusp_swap_check();
  356. if (ret) {
  357. if (ret != -ENOSPC)
  358. pr_err("Cannot find swap device, try swapon -a\n");
  359. return ret;
  360. }
  361. handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL);
  362. if (!handle->cur) {
  363. ret = -ENOMEM;
  364. goto err_close;
  365. }
  366. handle->cur_swap = alloc_swapdev_block(root_swap);
  367. if (!handle->cur_swap) {
  368. ret = -ENOSPC;
  369. goto err_rel;
  370. }
  371. handle->k = 0;
  372. handle->reqd_free_pages = reqd_free_pages();
  373. handle->first_sector = handle->cur_swap;
  374. return 0;
  375. err_rel:
  376. release_swap_writer(handle);
  377. err_close:
  378. swsusp_close(FMODE_WRITE);
  379. return ret;
  380. }
  381. static int swap_write_page(struct swap_map_handle *handle, void *buf,
  382. struct hib_bio_batch *hb)
  383. {
  384. int error = 0;
  385. sector_t offset;
  386. if (!handle->cur)
  387. return -EINVAL;
  388. offset = alloc_swapdev_block(root_swap);
  389. error = write_page(buf, offset, hb);
  390. if (error)
  391. return error;
  392. handle->cur->entries[handle->k++] = offset;
  393. if (handle->k >= MAP_PAGE_ENTRIES) {
  394. offset = alloc_swapdev_block(root_swap);
  395. if (!offset)
  396. return -ENOSPC;
  397. handle->cur->next_swap = offset;
  398. error = write_page(handle->cur, handle->cur_swap, hb);
  399. if (error)
  400. goto out;
  401. clear_page(handle->cur);
  402. handle->cur_swap = offset;
  403. handle->k = 0;
  404. if (hb && low_free_pages() <= handle->reqd_free_pages) {
  405. error = hib_wait_io(hb);
  406. if (error)
  407. goto out;
  408. /*
  409. * Recalculate the number of required free pages, to
  410. * make sure we never take more than half.
  411. */
  412. handle->reqd_free_pages = reqd_free_pages();
  413. }
  414. }
  415. out:
  416. return error;
  417. }
  418. static int flush_swap_writer(struct swap_map_handle *handle)
  419. {
  420. if (handle->cur && handle->cur_swap)
  421. return write_page(handle->cur, handle->cur_swap, NULL);
  422. else
  423. return -EINVAL;
  424. }
  425. static int swap_writer_finish(struct swap_map_handle *handle,
  426. unsigned int flags, int error)
  427. {
  428. if (!error) {
  429. flush_swap_writer(handle);
  430. pr_info("S");
  431. error = mark_swapfiles(handle, flags);
  432. pr_cont("|\n");
  433. }
  434. if (error)
  435. free_all_swap_pages(root_swap);
  436. release_swap_writer(handle);
  437. swsusp_close(FMODE_WRITE);
  438. return error;
  439. }
  440. /* We need to remember how much compressed data we need to read. */
  441. #define LZO_HEADER sizeof(size_t)
  442. /* Number of pages/bytes we'll compress at one time. */
  443. #define LZO_UNC_PAGES 32
  444. #define LZO_UNC_SIZE (LZO_UNC_PAGES * PAGE_SIZE)
  445. /* Number of pages/bytes we need for compressed data (worst case). */
  446. #define LZO_CMP_PAGES DIV_ROUND_UP(lzo1x_worst_compress(LZO_UNC_SIZE) + \
  447. LZO_HEADER, PAGE_SIZE)
  448. #define LZO_CMP_SIZE (LZO_CMP_PAGES * PAGE_SIZE)
  449. /* Maximum number of threads for compression/decompression. */
  450. #define LZO_THREADS 3
  451. /* Minimum/maximum number of pages for read buffering. */
  452. #define LZO_MIN_RD_PAGES 1024
  453. #define LZO_MAX_RD_PAGES 8192
  454. /**
  455. * save_image - save the suspend image data
  456. */
  457. static int save_image(struct swap_map_handle *handle,
  458. struct snapshot_handle *snapshot,
  459. unsigned int nr_to_write)
  460. {
  461. unsigned int m;
  462. int ret;
  463. int nr_pages;
  464. int err2;
  465. struct hib_bio_batch hb;
  466. ktime_t start;
  467. ktime_t stop;
  468. hib_init_batch(&hb);
  469. pr_info("Saving image data pages (%u pages)...\n",
  470. nr_to_write);
  471. m = nr_to_write / 10;
  472. if (!m)
  473. m = 1;
  474. nr_pages = 0;
  475. start = ktime_get();
  476. while (1) {
  477. ret = snapshot_read_next(snapshot);
  478. if (ret <= 0)
  479. break;
  480. ret = swap_write_page(handle, data_of(*snapshot), &hb);
  481. if (ret)
  482. break;
  483. if (!(nr_pages % m))
  484. pr_info("Image saving progress: %3d%%\n",
  485. nr_pages / m * 10);
  486. nr_pages++;
  487. }
  488. err2 = hib_wait_io(&hb);
  489. stop = ktime_get();
  490. if (!ret)
  491. ret = err2;
  492. if (!ret)
  493. pr_info("Image saving done\n");
  494. swsusp_show_speed(start, stop, nr_to_write, "Wrote");
  495. return ret;
  496. }
  497. /**
  498. * Structure used for CRC32.
  499. */
  500. struct crc_data {
  501. struct task_struct *thr; /* thread */
  502. atomic_t ready; /* ready to start flag */
  503. atomic_t stop; /* ready to stop flag */
  504. unsigned run_threads; /* nr current threads */
  505. wait_queue_head_t go; /* start crc update */
  506. wait_queue_head_t done; /* crc update done */
  507. u32 *crc32; /* points to handle's crc32 */
  508. size_t *unc_len[LZO_THREADS]; /* uncompressed lengths */
  509. unsigned char *unc[LZO_THREADS]; /* uncompressed data */
  510. };
  511. /**
  512. * CRC32 update function that runs in its own thread.
  513. */
  514. static int crc32_threadfn(void *data)
  515. {
  516. struct crc_data *d = data;
  517. unsigned i;
  518. while (1) {
  519. wait_event(d->go, atomic_read(&d->ready) ||
  520. kthread_should_stop());
  521. if (kthread_should_stop()) {
  522. d->thr = NULL;
  523. atomic_set(&d->stop, 1);
  524. wake_up(&d->done);
  525. break;
  526. }
  527. atomic_set(&d->ready, 0);
  528. for (i = 0; i < d->run_threads; i++)
  529. *d->crc32 = crc32_le(*d->crc32,
  530. d->unc[i], *d->unc_len[i]);
  531. atomic_set(&d->stop, 1);
  532. wake_up(&d->done);
  533. }
  534. return 0;
  535. }
  536. /**
  537. * Structure used for LZO data compression.
  538. */
  539. struct cmp_data {
  540. struct task_struct *thr; /* thread */
  541. atomic_t ready; /* ready to start flag */
  542. atomic_t stop; /* ready to stop flag */
  543. int ret; /* return code */
  544. wait_queue_head_t go; /* start compression */
  545. wait_queue_head_t done; /* compression done */
  546. size_t unc_len; /* uncompressed length */
  547. size_t cmp_len; /* compressed length */
  548. unsigned char unc[LZO_UNC_SIZE]; /* uncompressed buffer */
  549. unsigned char cmp[LZO_CMP_SIZE]; /* compressed buffer */
  550. unsigned char wrk[LZO1X_1_MEM_COMPRESS]; /* compression workspace */
  551. };
  552. /**
  553. * Compression function that runs in its own thread.
  554. */
  555. static int lzo_compress_threadfn(void *data)
  556. {
  557. struct cmp_data *d = data;
  558. while (1) {
  559. wait_event(d->go, atomic_read(&d->ready) ||
  560. kthread_should_stop());
  561. if (kthread_should_stop()) {
  562. d->thr = NULL;
  563. d->ret = -1;
  564. atomic_set(&d->stop, 1);
  565. wake_up(&d->done);
  566. break;
  567. }
  568. atomic_set(&d->ready, 0);
  569. d->ret = lzo1x_1_compress(d->unc, d->unc_len,
  570. d->cmp + LZO_HEADER, &d->cmp_len,
  571. d->wrk);
  572. atomic_set(&d->stop, 1);
  573. wake_up(&d->done);
  574. }
  575. return 0;
  576. }
  577. /**
  578. * save_image_lzo - Save the suspend image data compressed with LZO.
  579. * @handle: Swap map handle to use for saving the image.
  580. * @snapshot: Image to read data from.
  581. * @nr_to_write: Number of pages to save.
  582. */
  583. static int save_image_lzo(struct swap_map_handle *handle,
  584. struct snapshot_handle *snapshot,
  585. unsigned int nr_to_write)
  586. {
  587. unsigned int m;
  588. int ret = 0;
  589. int nr_pages;
  590. int err2;
  591. struct hib_bio_batch hb;
  592. ktime_t start;
  593. ktime_t stop;
  594. size_t off;
  595. unsigned thr, run_threads, nr_threads;
  596. unsigned char *page = NULL;
  597. struct cmp_data *data = NULL;
  598. struct crc_data *crc = NULL;
  599. hib_init_batch(&hb);
  600. /*
  601. * We'll limit the number of threads for compression to limit memory
  602. * footprint.
  603. */
  604. nr_threads = num_online_cpus() - 1;
  605. nr_threads = clamp_val(nr_threads, 1, LZO_THREADS);
  606. page = (void *)__get_free_page(GFP_NOIO | __GFP_HIGH);
  607. if (!page) {
  608. pr_err("Failed to allocate LZO page\n");
  609. ret = -ENOMEM;
  610. goto out_clean;
  611. }
  612. data = vmalloc(array_size(nr_threads, sizeof(*data)));
  613. if (!data) {
  614. pr_err("Failed to allocate LZO data\n");
  615. ret = -ENOMEM;
  616. goto out_clean;
  617. }
  618. for (thr = 0; thr < nr_threads; thr++)
  619. memset(&data[thr], 0, offsetof(struct cmp_data, go));
  620. crc = kmalloc(sizeof(*crc), GFP_KERNEL);
  621. if (!crc) {
  622. pr_err("Failed to allocate crc\n");
  623. ret = -ENOMEM;
  624. goto out_clean;
  625. }
  626. memset(crc, 0, offsetof(struct crc_data, go));
  627. /*
  628. * Start the compression threads.
  629. */
  630. for (thr = 0; thr < nr_threads; thr++) {
  631. init_waitqueue_head(&data[thr].go);
  632. init_waitqueue_head(&data[thr].done);
  633. data[thr].thr = kthread_run(lzo_compress_threadfn,
  634. &data[thr],
  635. "image_compress/%u", thr);
  636. if (IS_ERR(data[thr].thr)) {
  637. data[thr].thr = NULL;
  638. pr_err("Cannot start compression threads\n");
  639. ret = -ENOMEM;
  640. goto out_clean;
  641. }
  642. }
  643. /*
  644. * Start the CRC32 thread.
  645. */
  646. init_waitqueue_head(&crc->go);
  647. init_waitqueue_head(&crc->done);
  648. handle->crc32 = 0;
  649. crc->crc32 = &handle->crc32;
  650. for (thr = 0; thr < nr_threads; thr++) {
  651. crc->unc[thr] = data[thr].unc;
  652. crc->unc_len[thr] = &data[thr].unc_len;
  653. }
  654. crc->thr = kthread_run(crc32_threadfn, crc, "image_crc32");
  655. if (IS_ERR(crc->thr)) {
  656. crc->thr = NULL;
  657. pr_err("Cannot start CRC32 thread\n");
  658. ret = -ENOMEM;
  659. goto out_clean;
  660. }
  661. /*
  662. * Adjust the number of required free pages after all allocations have
  663. * been done. We don't want to run out of pages when writing.
  664. */
  665. handle->reqd_free_pages = reqd_free_pages();
  666. pr_info("Using %u thread(s) for compression\n", nr_threads);
  667. pr_info("Compressing and saving image data (%u pages)...\n",
  668. nr_to_write);
  669. m = nr_to_write / 10;
  670. if (!m)
  671. m = 1;
  672. nr_pages = 0;
  673. start = ktime_get();
  674. for (;;) {
  675. for (thr = 0; thr < nr_threads; thr++) {
  676. for (off = 0; off < LZO_UNC_SIZE; off += PAGE_SIZE) {
  677. ret = snapshot_read_next(snapshot);
  678. if (ret < 0)
  679. goto out_finish;
  680. if (!ret)
  681. break;
  682. memcpy(data[thr].unc + off,
  683. data_of(*snapshot), PAGE_SIZE);
  684. if (!(nr_pages % m))
  685. pr_info("Image saving progress: %3d%%\n",
  686. nr_pages / m * 10);
  687. nr_pages++;
  688. }
  689. if (!off)
  690. break;
  691. data[thr].unc_len = off;
  692. atomic_set(&data[thr].ready, 1);
  693. wake_up(&data[thr].go);
  694. }
  695. if (!thr)
  696. break;
  697. crc->run_threads = thr;
  698. atomic_set(&crc->ready, 1);
  699. wake_up(&crc->go);
  700. for (run_threads = thr, thr = 0; thr < run_threads; thr++) {
  701. wait_event(data[thr].done,
  702. atomic_read(&data[thr].stop));
  703. atomic_set(&data[thr].stop, 0);
  704. ret = data[thr].ret;
  705. if (ret < 0) {
  706. pr_err("LZO compression failed\n");
  707. goto out_finish;
  708. }
  709. if (unlikely(!data[thr].cmp_len ||
  710. data[thr].cmp_len >
  711. lzo1x_worst_compress(data[thr].unc_len))) {
  712. pr_err("Invalid LZO compressed length\n");
  713. ret = -1;
  714. goto out_finish;
  715. }
  716. *(size_t *)data[thr].cmp = data[thr].cmp_len;
  717. /*
  718. * Given we are writing one page at a time to disk, we
  719. * copy that much from the buffer, although the last
  720. * bit will likely be smaller than full page. This is
  721. * OK - we saved the length of the compressed data, so
  722. * any garbage at the end will be discarded when we
  723. * read it.
  724. */
  725. for (off = 0;
  726. off < LZO_HEADER + data[thr].cmp_len;
  727. off += PAGE_SIZE) {
  728. memcpy(page, data[thr].cmp + off, PAGE_SIZE);
  729. ret = swap_write_page(handle, page, &hb);
  730. if (ret)
  731. goto out_finish;
  732. }
  733. }
  734. wait_event(crc->done, atomic_read(&crc->stop));
  735. atomic_set(&crc->stop, 0);
  736. }
  737. out_finish:
  738. err2 = hib_wait_io(&hb);
  739. stop = ktime_get();
  740. if (!ret)
  741. ret = err2;
  742. if (!ret)
  743. pr_info("Image saving done\n");
  744. swsusp_show_speed(start, stop, nr_to_write, "Wrote");
  745. out_clean:
  746. if (crc) {
  747. if (crc->thr)
  748. kthread_stop(crc->thr);
  749. kfree(crc);
  750. }
  751. if (data) {
  752. for (thr = 0; thr < nr_threads; thr++)
  753. if (data[thr].thr)
  754. kthread_stop(data[thr].thr);
  755. vfree(data);
  756. }
  757. if (page) free_page((unsigned long)page);
  758. return ret;
  759. }
  760. /**
  761. * enough_swap - Make sure we have enough swap to save the image.
  762. *
  763. * Returns TRUE or FALSE after checking the total amount of swap
  764. * space avaiable from the resume partition.
  765. */
  766. static int enough_swap(unsigned int nr_pages)
  767. {
  768. unsigned int free_swap = count_swap_pages(root_swap, 1);
  769. unsigned int required;
  770. pr_debug("Free swap pages: %u\n", free_swap);
  771. required = PAGES_FOR_IO + nr_pages;
  772. return free_swap > required;
  773. }
  774. /**
  775. * swsusp_write - Write entire image and metadata.
  776. * @flags: flags to pass to the "boot" kernel in the image header
  777. *
  778. * It is important _NOT_ to umount filesystems at this point. We want
  779. * them synced (in case something goes wrong) but we DO not want to mark
  780. * filesystem clean: it is not. (And it does not matter, if we resume
  781. * correctly, we'll mark system clean, anyway.)
  782. */
  783. int swsusp_write(unsigned int flags)
  784. {
  785. struct swap_map_handle handle;
  786. struct snapshot_handle snapshot;
  787. struct swsusp_info *header;
  788. unsigned long pages;
  789. int error;
  790. pages = snapshot_get_image_size();
  791. error = get_swap_writer(&handle);
  792. if (error) {
  793. pr_err("Cannot get swap writer\n");
  794. return error;
  795. }
  796. if (flags & SF_NOCOMPRESS_MODE) {
  797. if (!enough_swap(pages)) {
  798. pr_err("Not enough free swap\n");
  799. error = -ENOSPC;
  800. goto out_finish;
  801. }
  802. }
  803. memset(&snapshot, 0, sizeof(struct snapshot_handle));
  804. error = snapshot_read_next(&snapshot);
  805. if (error < (int)PAGE_SIZE) {
  806. if (error >= 0)
  807. error = -EFAULT;
  808. goto out_finish;
  809. }
  810. header = (struct swsusp_info *)data_of(snapshot);
  811. error = swap_write_page(&handle, header, NULL);
  812. if (!error) {
  813. error = (flags & SF_NOCOMPRESS_MODE) ?
  814. save_image(&handle, &snapshot, pages - 1) :
  815. save_image_lzo(&handle, &snapshot, pages - 1);
  816. }
  817. out_finish:
  818. error = swap_writer_finish(&handle, flags, error);
  819. return error;
  820. }
  821. /**
  822. * The following functions allow us to read data using a swap map
  823. * in a file-alike way
  824. */
  825. static void release_swap_reader(struct swap_map_handle *handle)
  826. {
  827. struct swap_map_page_list *tmp;
  828. while (handle->maps) {
  829. if (handle->maps->map)
  830. free_page((unsigned long)handle->maps->map);
  831. tmp = handle->maps;
  832. handle->maps = handle->maps->next;
  833. kfree(tmp);
  834. }
  835. handle->cur = NULL;
  836. }
  837. static int get_swap_reader(struct swap_map_handle *handle,
  838. unsigned int *flags_p)
  839. {
  840. int error;
  841. struct swap_map_page_list *tmp, *last;
  842. sector_t offset;
  843. *flags_p = swsusp_header->flags;
  844. if (!swsusp_header->image) /* how can this happen? */
  845. return -EINVAL;
  846. handle->cur = NULL;
  847. last = handle->maps = NULL;
  848. offset = swsusp_header->image;
  849. while (offset) {
  850. tmp = kmalloc(sizeof(*handle->maps), GFP_KERNEL);
  851. if (!tmp) {
  852. release_swap_reader(handle);
  853. return -ENOMEM;
  854. }
  855. memset(tmp, 0, sizeof(*tmp));
  856. if (!handle->maps)
  857. handle->maps = tmp;
  858. if (last)
  859. last->next = tmp;
  860. last = tmp;
  861. tmp->map = (struct swap_map_page *)
  862. __get_free_page(GFP_NOIO | __GFP_HIGH);
  863. if (!tmp->map) {
  864. release_swap_reader(handle);
  865. return -ENOMEM;
  866. }
  867. error = hib_submit_io(REQ_OP_READ, 0, offset, tmp->map, NULL);
  868. if (error) {
  869. release_swap_reader(handle);
  870. return error;
  871. }
  872. offset = tmp->map->next_swap;
  873. }
  874. handle->k = 0;
  875. handle->cur = handle->maps->map;
  876. return 0;
  877. }
  878. static int swap_read_page(struct swap_map_handle *handle, void *buf,
  879. struct hib_bio_batch *hb)
  880. {
  881. sector_t offset;
  882. int error;
  883. struct swap_map_page_list *tmp;
  884. if (!handle->cur)
  885. return -EINVAL;
  886. offset = handle->cur->entries[handle->k];
  887. if (!offset)
  888. return -EFAULT;
  889. error = hib_submit_io(REQ_OP_READ, 0, offset, buf, hb);
  890. if (error)
  891. return error;
  892. if (++handle->k >= MAP_PAGE_ENTRIES) {
  893. handle->k = 0;
  894. free_page((unsigned long)handle->maps->map);
  895. tmp = handle->maps;
  896. handle->maps = handle->maps->next;
  897. kfree(tmp);
  898. if (!handle->maps)
  899. release_swap_reader(handle);
  900. else
  901. handle->cur = handle->maps->map;
  902. }
  903. return error;
  904. }
  905. static int swap_reader_finish(struct swap_map_handle *handle)
  906. {
  907. release_swap_reader(handle);
  908. return 0;
  909. }
  910. /**
  911. * load_image - load the image using the swap map handle
  912. * @handle and the snapshot handle @snapshot
  913. * (assume there are @nr_pages pages to load)
  914. */
  915. static int load_image(struct swap_map_handle *handle,
  916. struct snapshot_handle *snapshot,
  917. unsigned int nr_to_read)
  918. {
  919. unsigned int m;
  920. int ret = 0;
  921. ktime_t start;
  922. ktime_t stop;
  923. struct hib_bio_batch hb;
  924. int err2;
  925. unsigned nr_pages;
  926. hib_init_batch(&hb);
  927. clean_pages_on_read = true;
  928. pr_info("Loading image data pages (%u pages)...\n", nr_to_read);
  929. m = nr_to_read / 10;
  930. if (!m)
  931. m = 1;
  932. nr_pages = 0;
  933. start = ktime_get();
  934. for ( ; ; ) {
  935. ret = snapshot_write_next(snapshot);
  936. if (ret <= 0)
  937. break;
  938. ret = swap_read_page(handle, data_of(*snapshot), &hb);
  939. if (ret)
  940. break;
  941. if (snapshot->sync_read)
  942. ret = hib_wait_io(&hb);
  943. if (ret)
  944. break;
  945. if (!(nr_pages % m))
  946. pr_info("Image loading progress: %3d%%\n",
  947. nr_pages / m * 10);
  948. nr_pages++;
  949. }
  950. err2 = hib_wait_io(&hb);
  951. stop = ktime_get();
  952. if (!ret)
  953. ret = err2;
  954. if (!ret) {
  955. pr_info("Image loading done\n");
  956. snapshot_write_finalize(snapshot);
  957. if (!snapshot_image_loaded(snapshot))
  958. ret = -ENODATA;
  959. }
  960. swsusp_show_speed(start, stop, nr_to_read, "Read");
  961. return ret;
  962. }
  963. /**
  964. * Structure used for LZO data decompression.
  965. */
  966. struct dec_data {
  967. struct task_struct *thr; /* thread */
  968. atomic_t ready; /* ready to start flag */
  969. atomic_t stop; /* ready to stop flag */
  970. int ret; /* return code */
  971. wait_queue_head_t go; /* start decompression */
  972. wait_queue_head_t done; /* decompression done */
  973. size_t unc_len; /* uncompressed length */
  974. size_t cmp_len; /* compressed length */
  975. unsigned char unc[LZO_UNC_SIZE]; /* uncompressed buffer */
  976. unsigned char cmp[LZO_CMP_SIZE]; /* compressed buffer */
  977. };
  978. /**
  979. * Deompression function that runs in its own thread.
  980. */
  981. static int lzo_decompress_threadfn(void *data)
  982. {
  983. struct dec_data *d = data;
  984. while (1) {
  985. wait_event(d->go, atomic_read(&d->ready) ||
  986. kthread_should_stop());
  987. if (kthread_should_stop()) {
  988. d->thr = NULL;
  989. d->ret = -1;
  990. atomic_set(&d->stop, 1);
  991. wake_up(&d->done);
  992. break;
  993. }
  994. atomic_set(&d->ready, 0);
  995. d->unc_len = LZO_UNC_SIZE;
  996. d->ret = lzo1x_decompress_safe(d->cmp + LZO_HEADER, d->cmp_len,
  997. d->unc, &d->unc_len);
  998. if (clean_pages_on_decompress)
  999. flush_icache_range((unsigned long)d->unc,
  1000. (unsigned long)d->unc + d->unc_len);
  1001. atomic_set(&d->stop, 1);
  1002. wake_up(&d->done);
  1003. }
  1004. return 0;
  1005. }
  1006. /**
  1007. * load_image_lzo - Load compressed image data and decompress them with LZO.
  1008. * @handle: Swap map handle to use for loading data.
  1009. * @snapshot: Image to copy uncompressed data into.
  1010. * @nr_to_read: Number of pages to load.
  1011. */
  1012. static int load_image_lzo(struct swap_map_handle *handle,
  1013. struct snapshot_handle *snapshot,
  1014. unsigned int nr_to_read)
  1015. {
  1016. unsigned int m;
  1017. int ret = 0;
  1018. int eof = 0;
  1019. struct hib_bio_batch hb;
  1020. ktime_t start;
  1021. ktime_t stop;
  1022. unsigned nr_pages;
  1023. size_t off;
  1024. unsigned i, thr, run_threads, nr_threads;
  1025. unsigned ring = 0, pg = 0, ring_size = 0,
  1026. have = 0, want, need, asked = 0;
  1027. unsigned long read_pages = 0;
  1028. unsigned char **page = NULL;
  1029. struct dec_data *data = NULL;
  1030. struct crc_data *crc = NULL;
  1031. hib_init_batch(&hb);
  1032. /*
  1033. * We'll limit the number of threads for decompression to limit memory
  1034. * footprint.
  1035. */
  1036. nr_threads = num_online_cpus() - 1;
  1037. nr_threads = clamp_val(nr_threads, 1, LZO_THREADS);
  1038. page = vmalloc(array_size(LZO_MAX_RD_PAGES, sizeof(*page)));
  1039. if (!page) {
  1040. pr_err("Failed to allocate LZO page\n");
  1041. ret = -ENOMEM;
  1042. goto out_clean;
  1043. }
  1044. data = vmalloc(array_size(nr_threads, sizeof(*data)));
  1045. if (!data) {
  1046. pr_err("Failed to allocate LZO data\n");
  1047. ret = -ENOMEM;
  1048. goto out_clean;
  1049. }
  1050. for (thr = 0; thr < nr_threads; thr++)
  1051. memset(&data[thr], 0, offsetof(struct dec_data, go));
  1052. crc = kmalloc(sizeof(*crc), GFP_KERNEL);
  1053. if (!crc) {
  1054. pr_err("Failed to allocate crc\n");
  1055. ret = -ENOMEM;
  1056. goto out_clean;
  1057. }
  1058. memset(crc, 0, offsetof(struct crc_data, go));
  1059. clean_pages_on_decompress = true;
  1060. /*
  1061. * Start the decompression threads.
  1062. */
  1063. for (thr = 0; thr < nr_threads; thr++) {
  1064. init_waitqueue_head(&data[thr].go);
  1065. init_waitqueue_head(&data[thr].done);
  1066. data[thr].thr = kthread_run(lzo_decompress_threadfn,
  1067. &data[thr],
  1068. "image_decompress/%u", thr);
  1069. if (IS_ERR(data[thr].thr)) {
  1070. data[thr].thr = NULL;
  1071. pr_err("Cannot start decompression threads\n");
  1072. ret = -ENOMEM;
  1073. goto out_clean;
  1074. }
  1075. }
  1076. /*
  1077. * Start the CRC32 thread.
  1078. */
  1079. init_waitqueue_head(&crc->go);
  1080. init_waitqueue_head(&crc->done);
  1081. handle->crc32 = 0;
  1082. crc->crc32 = &handle->crc32;
  1083. for (thr = 0; thr < nr_threads; thr++) {
  1084. crc->unc[thr] = data[thr].unc;
  1085. crc->unc_len[thr] = &data[thr].unc_len;
  1086. }
  1087. crc->thr = kthread_run(crc32_threadfn, crc, "image_crc32");
  1088. if (IS_ERR(crc->thr)) {
  1089. crc->thr = NULL;
  1090. pr_err("Cannot start CRC32 thread\n");
  1091. ret = -ENOMEM;
  1092. goto out_clean;
  1093. }
  1094. /*
  1095. * Set the number of pages for read buffering.
  1096. * This is complete guesswork, because we'll only know the real
  1097. * picture once prepare_image() is called, which is much later on
  1098. * during the image load phase. We'll assume the worst case and
  1099. * say that none of the image pages are from high memory.
  1100. */
  1101. if (low_free_pages() > snapshot_get_image_size())
  1102. read_pages = (low_free_pages() - snapshot_get_image_size()) / 2;
  1103. read_pages = clamp_val(read_pages, LZO_MIN_RD_PAGES, LZO_MAX_RD_PAGES);
  1104. for (i = 0; i < read_pages; i++) {
  1105. page[i] = (void *)__get_free_page(i < LZO_CMP_PAGES ?
  1106. GFP_NOIO | __GFP_HIGH :
  1107. GFP_NOIO | __GFP_NOWARN |
  1108. __GFP_NORETRY);
  1109. if (!page[i]) {
  1110. if (i < LZO_CMP_PAGES) {
  1111. ring_size = i;
  1112. pr_err("Failed to allocate LZO pages\n");
  1113. ret = -ENOMEM;
  1114. goto out_clean;
  1115. } else {
  1116. break;
  1117. }
  1118. }
  1119. }
  1120. want = ring_size = i;
  1121. pr_info("Using %u thread(s) for decompression\n", nr_threads);
  1122. pr_info("Loading and decompressing image data (%u pages)...\n",
  1123. nr_to_read);
  1124. m = nr_to_read / 10;
  1125. if (!m)
  1126. m = 1;
  1127. nr_pages = 0;
  1128. start = ktime_get();
  1129. ret = snapshot_write_next(snapshot);
  1130. if (ret <= 0)
  1131. goto out_finish;
  1132. for(;;) {
  1133. for (i = 0; !eof && i < want; i++) {
  1134. ret = swap_read_page(handle, page[ring], &hb);
  1135. if (ret) {
  1136. /*
  1137. * On real read error, finish. On end of data,
  1138. * set EOF flag and just exit the read loop.
  1139. */
  1140. if (handle->cur &&
  1141. handle->cur->entries[handle->k]) {
  1142. goto out_finish;
  1143. } else {
  1144. eof = 1;
  1145. break;
  1146. }
  1147. }
  1148. if (++ring >= ring_size)
  1149. ring = 0;
  1150. }
  1151. asked += i;
  1152. want -= i;
  1153. /*
  1154. * We are out of data, wait for some more.
  1155. */
  1156. if (!have) {
  1157. if (!asked)
  1158. break;
  1159. ret = hib_wait_io(&hb);
  1160. if (ret)
  1161. goto out_finish;
  1162. have += asked;
  1163. asked = 0;
  1164. if (eof)
  1165. eof = 2;
  1166. }
  1167. if (crc->run_threads) {
  1168. wait_event(crc->done, atomic_read(&crc->stop));
  1169. atomic_set(&crc->stop, 0);
  1170. crc->run_threads = 0;
  1171. }
  1172. for (thr = 0; have && thr < nr_threads; thr++) {
  1173. data[thr].cmp_len = *(size_t *)page[pg];
  1174. if (unlikely(!data[thr].cmp_len ||
  1175. data[thr].cmp_len >
  1176. lzo1x_worst_compress(LZO_UNC_SIZE))) {
  1177. pr_err("Invalid LZO compressed length\n");
  1178. ret = -1;
  1179. goto out_finish;
  1180. }
  1181. need = DIV_ROUND_UP(data[thr].cmp_len + LZO_HEADER,
  1182. PAGE_SIZE);
  1183. if (need > have) {
  1184. if (eof > 1) {
  1185. ret = -1;
  1186. goto out_finish;
  1187. }
  1188. break;
  1189. }
  1190. for (off = 0;
  1191. off < LZO_HEADER + data[thr].cmp_len;
  1192. off += PAGE_SIZE) {
  1193. memcpy(data[thr].cmp + off,
  1194. page[pg], PAGE_SIZE);
  1195. have--;
  1196. want++;
  1197. if (++pg >= ring_size)
  1198. pg = 0;
  1199. }
  1200. atomic_set(&data[thr].ready, 1);
  1201. wake_up(&data[thr].go);
  1202. }
  1203. /*
  1204. * Wait for more data while we are decompressing.
  1205. */
  1206. if (have < LZO_CMP_PAGES && asked) {
  1207. ret = hib_wait_io(&hb);
  1208. if (ret)
  1209. goto out_finish;
  1210. have += asked;
  1211. asked = 0;
  1212. if (eof)
  1213. eof = 2;
  1214. }
  1215. for (run_threads = thr, thr = 0; thr < run_threads; thr++) {
  1216. wait_event(data[thr].done,
  1217. atomic_read(&data[thr].stop));
  1218. atomic_set(&data[thr].stop, 0);
  1219. ret = data[thr].ret;
  1220. if (ret < 0) {
  1221. pr_err("LZO decompression failed\n");
  1222. goto out_finish;
  1223. }
  1224. if (unlikely(!data[thr].unc_len ||
  1225. data[thr].unc_len > LZO_UNC_SIZE ||
  1226. data[thr].unc_len & (PAGE_SIZE - 1))) {
  1227. pr_err("Invalid LZO uncompressed length\n");
  1228. ret = -1;
  1229. goto out_finish;
  1230. }
  1231. for (off = 0;
  1232. off < data[thr].unc_len; off += PAGE_SIZE) {
  1233. memcpy(data_of(*snapshot),
  1234. data[thr].unc + off, PAGE_SIZE);
  1235. if (!(nr_pages % m))
  1236. pr_info("Image loading progress: %3d%%\n",
  1237. nr_pages / m * 10);
  1238. nr_pages++;
  1239. ret = snapshot_write_next(snapshot);
  1240. if (ret <= 0) {
  1241. crc->run_threads = thr + 1;
  1242. atomic_set(&crc->ready, 1);
  1243. wake_up(&crc->go);
  1244. goto out_finish;
  1245. }
  1246. }
  1247. }
  1248. crc->run_threads = thr;
  1249. atomic_set(&crc->ready, 1);
  1250. wake_up(&crc->go);
  1251. }
  1252. out_finish:
  1253. if (crc->run_threads) {
  1254. wait_event(crc->done, atomic_read(&crc->stop));
  1255. atomic_set(&crc->stop, 0);
  1256. }
  1257. stop = ktime_get();
  1258. if (!ret) {
  1259. pr_info("Image loading done\n");
  1260. snapshot_write_finalize(snapshot);
  1261. if (!snapshot_image_loaded(snapshot))
  1262. ret = -ENODATA;
  1263. if (!ret) {
  1264. if (swsusp_header->flags & SF_CRC32_MODE) {
  1265. if(handle->crc32 != swsusp_header->crc32) {
  1266. pr_err("Invalid image CRC32!\n");
  1267. ret = -ENODATA;
  1268. }
  1269. }
  1270. }
  1271. }
  1272. swsusp_show_speed(start, stop, nr_to_read, "Read");
  1273. out_clean:
  1274. for (i = 0; i < ring_size; i++)
  1275. free_page((unsigned long)page[i]);
  1276. if (crc) {
  1277. if (crc->thr)
  1278. kthread_stop(crc->thr);
  1279. kfree(crc);
  1280. }
  1281. if (data) {
  1282. for (thr = 0; thr < nr_threads; thr++)
  1283. if (data[thr].thr)
  1284. kthread_stop(data[thr].thr);
  1285. vfree(data);
  1286. }
  1287. vfree(page);
  1288. return ret;
  1289. }
  1290. /**
  1291. * swsusp_read - read the hibernation image.
  1292. * @flags_p: flags passed by the "frozen" kernel in the image header should
  1293. * be written into this memory location
  1294. */
  1295. int swsusp_read(unsigned int *flags_p)
  1296. {
  1297. int error;
  1298. struct swap_map_handle handle;
  1299. struct snapshot_handle snapshot;
  1300. struct swsusp_info *header;
  1301. memset(&snapshot, 0, sizeof(struct snapshot_handle));
  1302. error = snapshot_write_next(&snapshot);
  1303. if (error < (int)PAGE_SIZE)
  1304. return error < 0 ? error : -EFAULT;
  1305. header = (struct swsusp_info *)data_of(snapshot);
  1306. error = get_swap_reader(&handle, flags_p);
  1307. if (error)
  1308. goto end;
  1309. if (!error)
  1310. error = swap_read_page(&handle, header, NULL);
  1311. if (!error) {
  1312. error = (*flags_p & SF_NOCOMPRESS_MODE) ?
  1313. load_image(&handle, &snapshot, header->pages - 1) :
  1314. load_image_lzo(&handle, &snapshot, header->pages - 1);
  1315. }
  1316. swap_reader_finish(&handle);
  1317. end:
  1318. if (!error)
  1319. pr_debug("Image successfully loaded\n");
  1320. else
  1321. pr_debug("Error %d resuming\n", error);
  1322. return error;
  1323. }
  1324. /**
  1325. * swsusp_check - Check for swsusp signature in the resume device
  1326. */
  1327. int swsusp_check(void)
  1328. {
  1329. int error;
  1330. hib_resume_bdev = blkdev_get_by_dev(swsusp_resume_device,
  1331. FMODE_READ, NULL);
  1332. if (!IS_ERR(hib_resume_bdev)) {
  1333. set_blocksize(hib_resume_bdev, PAGE_SIZE);
  1334. clear_page(swsusp_header);
  1335. error = hib_submit_io(REQ_OP_READ, 0,
  1336. swsusp_resume_block,
  1337. swsusp_header, NULL);
  1338. if (error)
  1339. goto put;
  1340. if (!memcmp(HIBERNATE_SIG, swsusp_header->sig, 10)) {
  1341. memcpy(swsusp_header->sig, swsusp_header->orig_sig, 10);
  1342. /* Reset swap signature now */
  1343. error = hib_submit_io(REQ_OP_WRITE, REQ_SYNC,
  1344. swsusp_resume_block,
  1345. swsusp_header, NULL);
  1346. } else {
  1347. error = -EINVAL;
  1348. }
  1349. put:
  1350. if (error)
  1351. blkdev_put(hib_resume_bdev, FMODE_READ);
  1352. else
  1353. pr_debug("Image signature found, resuming\n");
  1354. } else {
  1355. error = PTR_ERR(hib_resume_bdev);
  1356. }
  1357. if (error)
  1358. pr_debug("Image not found (code %d)\n", error);
  1359. return error;
  1360. }
  1361. /**
  1362. * swsusp_close - close swap device.
  1363. */
  1364. void swsusp_close(fmode_t mode)
  1365. {
  1366. if (IS_ERR(hib_resume_bdev)) {
  1367. pr_debug("Image device not initialised\n");
  1368. return;
  1369. }
  1370. blkdev_put(hib_resume_bdev, mode);
  1371. }
  1372. /**
  1373. * swsusp_unmark - Unmark swsusp signature in the resume device
  1374. */
  1375. #ifdef CONFIG_SUSPEND
  1376. int swsusp_unmark(void)
  1377. {
  1378. int error;
  1379. hib_submit_io(REQ_OP_READ, 0, swsusp_resume_block,
  1380. swsusp_header, NULL);
  1381. if (!memcmp(HIBERNATE_SIG,swsusp_header->sig, 10)) {
  1382. memcpy(swsusp_header->sig,swsusp_header->orig_sig, 10);
  1383. error = hib_submit_io(REQ_OP_WRITE, REQ_SYNC,
  1384. swsusp_resume_block,
  1385. swsusp_header, NULL);
  1386. } else {
  1387. pr_err("Cannot find swsusp signature!\n");
  1388. error = -ENODEV;
  1389. }
  1390. /*
  1391. * We just returned from suspend, we don't need the image any more.
  1392. */
  1393. free_all_swap_pages(root_swap);
  1394. return error;
  1395. }
  1396. #endif
  1397. static int swsusp_header_init(void)
  1398. {
  1399. swsusp_header = (struct swsusp_header*) __get_free_page(GFP_KERNEL);
  1400. if (!swsusp_header)
  1401. panic("Could not allocate memory for swsusp_header\n");
  1402. return 0;
  1403. }
  1404. core_initcall(swsusp_header_init);