swap.c 38 KB

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