swap.c 38 KB

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