swap.c 39 KB

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