blob: 1f6aef2e21312942a87264c12f60ae0adc58f255 [file] [log] [blame]
Rafael J. Wysocki61159a32006-03-23 03:00:00 -08001/*
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 *
Pavel Macheka2531292010-07-18 14:27:13 +02007 * Copyright (C) 1998,2001-2005 Pavel Machek <pavel@ucw.cz>
Rafael J. Wysocki61159a32006-03-23 03:00:00 -08008 * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl>
Bojan Smojver5a21d482012-04-29 22:42:06 +02009 * Copyright (C) 2010-2012 Bojan Smojver <bojan@rexursive.com>
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080010 *
11 * This file is released under the GPLv2.
12 *
13 */
14
15#include <linux/module.h>
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080016#include <linux/file.h>
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080017#include <linux/delay.h>
18#include <linux/bitops.h>
19#include <linux/genhd.h>
20#include <linux/device.h>
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080021#include <linux/bio.h>
Andrew Morton546e0d22006-09-25 23:32:44 -070022#include <linux/blkdev.h>
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080023#include <linux/swap.h>
24#include <linux/swapops.h>
25#include <linux/pm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Bojan Smojverf996fc92010-09-09 23:06:23 +020027#include <linux/lzo.h>
28#include <linux/vmalloc.h>
Bojan Smojver081a9d02011-10-13 23:58:07 +020029#include <linux/cpumask.h>
30#include <linux/atomic.h>
31#include <linux/kthread.h>
32#include <linux/crc32.h>
Tina Ruchandanidb597602014-10-30 11:04:53 -070033#include <linux/ktime.h>
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080034
35#include "power.h"
36
Rafael J. Wysockibe8cd642010-12-11 21:46:44 +010037#define HIBERNATE_SIG "S1SUSPEND"
Rafael J. Wysocki61159a32006-03-23 03:00:00 -080038
Jiri Slaby51fb3522010-05-01 23:53:02 +020039/*
James Morsef6cf0542016-04-27 17:47:11 +010040 * When reading an {un,}compressed image, we may restore pages in place,
41 * in which case some architectures need these pages cleaning before they
42 * can be executed. We don't know which pages these may be, so clean the lot.
43 */
44static bool clean_pages_on_read;
45static bool clean_pages_on_decompress;
46
47/*
Jiri Slaby51fb3522010-05-01 23:53:02 +020048 * The swap map is a data structure used for keeping track of each page
49 * written to a swap partition. It consists of many swap_map_page
Cesar Eduardo Barros90133672010-06-07 22:23:12 +020050 * structures that contain each an array of MAP_PAGE_ENTRIES swap entries.
Jiri Slaby51fb3522010-05-01 23:53:02 +020051 * These structures are stored on the swap and linked together with the
52 * help of the .next_swap member.
53 *
54 * The swap map is created during suspend. The swap map pages are
55 * allocated and populated one at a time, so we only need one memory
56 * page to set up the entire structure.
57 *
Bojan Smojver081a9d02011-10-13 23:58:07 +020058 * During resume we pick up all swap_map_page structures into a list.
Jiri Slaby51fb3522010-05-01 23:53:02 +020059 */
60
61#define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(sector_t) - 1)
62
Bojan Smojverf8262d42012-04-24 23:53:28 +020063/*
64 * Number of free pages that are not high.
65 */
66static inline unsigned long low_free_pages(void)
67{
68 return nr_free_pages() - nr_free_highpages();
69}
70
71/*
72 * Number of pages required to be kept free while writing the image. Always
73 * half of all available low pages before the writing starts.
74 */
75static inline unsigned long reqd_free_pages(void)
76{
77 return low_free_pages() / 2;
78}
79
Jiri Slaby51fb3522010-05-01 23:53:02 +020080struct swap_map_page {
81 sector_t entries[MAP_PAGE_ENTRIES];
82 sector_t next_swap;
83};
84
Bojan Smojver081a9d02011-10-13 23:58:07 +020085struct swap_map_page_list {
86 struct swap_map_page *map;
87 struct swap_map_page_list *next;
88};
89
Jiri Slaby51fb3522010-05-01 23:53:02 +020090/**
91 * The swap_map_handle structure is used for handling swap in
92 * a file-alike way
93 */
94
95struct swap_map_handle {
96 struct swap_map_page *cur;
Bojan Smojver081a9d02011-10-13 23:58:07 +020097 struct swap_map_page_list *maps;
Jiri Slaby51fb3522010-05-01 23:53:02 +020098 sector_t cur_swap;
99 sector_t first_sector;
100 unsigned int k;
Bojan Smojverf8262d42012-04-24 23:53:28 +0200101 unsigned long reqd_free_pages;
Bojan Smojver081a9d02011-10-13 23:58:07 +0200102 u32 crc32;
Jiri Slaby51fb3522010-05-01 23:53:02 +0200103};
104
Vivek Goyal1b29c162007-05-02 19:27:07 +0200105struct swsusp_header {
Bojan Smojver081a9d02011-10-13 23:58:07 +0200106 char reserved[PAGE_SIZE - 20 - sizeof(sector_t) - sizeof(int) -
107 sizeof(u32)];
108 u32 crc32;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800109 sector_t image;
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700110 unsigned int flags; /* Flags to pass to the "boot" kernel */
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800111 char orig_sig[10];
112 char sig[10];
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -0700113} __packed;
Vivek Goyal1b29c162007-05-02 19:27:07 +0200114
115static struct swsusp_header *swsusp_header;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800116
Nigel Cunningham0414f2e2009-12-06 16:15:53 +0100117/**
118 * The following functions are used for tracing the allocated
119 * swap pages, so that they can be freed in case of an error.
120 */
121
122struct swsusp_extent {
123 struct rb_node node;
124 unsigned long start;
125 unsigned long end;
126};
127
128static struct rb_root swsusp_extents = RB_ROOT;
129
130static int swsusp_extents_insert(unsigned long swap_offset)
131{
132 struct rb_node **new = &(swsusp_extents.rb_node);
133 struct rb_node *parent = NULL;
134 struct swsusp_extent *ext;
135
136 /* Figure out where to put the new node */
137 while (*new) {
Davidlohr Bueso8316bd72012-10-23 01:21:09 +0200138 ext = rb_entry(*new, struct swsusp_extent, node);
Nigel Cunningham0414f2e2009-12-06 16:15:53 +0100139 parent = *new;
140 if (swap_offset < ext->start) {
141 /* Try to merge */
142 if (swap_offset == ext->start - 1) {
143 ext->start--;
144 return 0;
145 }
146 new = &((*new)->rb_left);
147 } else if (swap_offset > ext->end) {
148 /* Try to merge */
149 if (swap_offset == ext->end + 1) {
150 ext->end++;
151 return 0;
152 }
153 new = &((*new)->rb_right);
154 } else {
155 /* It already is in the tree */
156 return -EINVAL;
157 }
158 }
159 /* Add the new node and rebalance the tree. */
160 ext = kzalloc(sizeof(struct swsusp_extent), GFP_KERNEL);
161 if (!ext)
162 return -ENOMEM;
163
164 ext->start = swap_offset;
165 ext->end = swap_offset;
166 rb_link_node(&ext->node, parent, new);
167 rb_insert_color(&ext->node, &swsusp_extents);
168 return 0;
169}
170
171/**
172 * alloc_swapdev_block - allocate a swap page and register that it has
173 * been allocated, so that it can be freed in case of an error.
174 */
175
176sector_t alloc_swapdev_block(int swap)
177{
178 unsigned long offset;
179
Hugh Dickins910321e2010-09-09 16:38:07 -0700180 offset = swp_offset(get_swap_page_of_type(swap));
Nigel Cunningham0414f2e2009-12-06 16:15:53 +0100181 if (offset) {
182 if (swsusp_extents_insert(offset))
Hugh Dickins910321e2010-09-09 16:38:07 -0700183 swap_free(swp_entry(swap, offset));
Nigel Cunningham0414f2e2009-12-06 16:15:53 +0100184 else
185 return swapdev_block(swap, offset);
186 }
187 return 0;
188}
189
190/**
191 * free_all_swap_pages - free swap pages allocated for saving image data.
Cesar Eduardo Barros90133672010-06-07 22:23:12 +0200192 * It also frees the extents used to register which swap entries had been
Nigel Cunningham0414f2e2009-12-06 16:15:53 +0100193 * allocated.
194 */
195
196void free_all_swap_pages(int swap)
197{
198 struct rb_node *node;
199
200 while ((node = swsusp_extents.rb_node)) {
201 struct swsusp_extent *ext;
202 unsigned long offset;
203
204 ext = container_of(node, struct swsusp_extent, node);
205 rb_erase(node, &swsusp_extents);
206 for (offset = ext->start; offset <= ext->end; offset++)
Hugh Dickins910321e2010-09-09 16:38:07 -0700207 swap_free(swp_entry(swap, offset));
Nigel Cunningham0414f2e2009-12-06 16:15:53 +0100208
209 kfree(ext);
210 }
211}
212
213int swsusp_swap_in_use(void)
214{
215 return (swsusp_extents.rb_node != NULL);
216}
217
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800218/*
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -0800219 * General things
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800220 */
221
222static unsigned short root_swap = 0xffff;
Christoph Hellwig343df3c2015-05-19 09:23:23 +0200223static struct block_device *hib_resume_bdev;
224
225struct hib_bio_batch {
226 atomic_t count;
227 wait_queue_head_t wait;
228 int error;
229};
230
231static void hib_init_batch(struct hib_bio_batch *hb)
232{
233 atomic_set(&hb->count, 0);
234 init_waitqueue_head(&hb->wait);
235 hb->error = 0;
236}
237
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200238static void hib_end_io(struct bio *bio)
Christoph Hellwig343df3c2015-05-19 09:23:23 +0200239{
240 struct hib_bio_batch *hb = bio->bi_private;
Christoph Hellwig343df3c2015-05-19 09:23:23 +0200241 struct page *page = bio->bi_io_vec[0].bv_page;
242
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200243 if (bio->bi_error) {
Christoph Hellwig343df3c2015-05-19 09:23:23 +0200244 printk(KERN_ALERT "Read-error on swap-device (%u:%u:%Lu)\n",
245 imajor(bio->bi_bdev->bd_inode),
246 iminor(bio->bi_bdev->bd_inode),
247 (unsigned long long)bio->bi_iter.bi_sector);
Christoph Hellwig343df3c2015-05-19 09:23:23 +0200248 }
249
250 if (bio_data_dir(bio) == WRITE)
251 put_page(page);
James Morsef6cf0542016-04-27 17:47:11 +0100252 else if (clean_pages_on_read)
253 flush_icache_range((unsigned long)page_address(page),
254 (unsigned long)page_address(page) + PAGE_SIZE);
Christoph Hellwig343df3c2015-05-19 09:23:23 +0200255
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200256 if (bio->bi_error && !hb->error)
257 hb->error = bio->bi_error;
Christoph Hellwig343df3c2015-05-19 09:23:23 +0200258 if (atomic_dec_and_test(&hb->count))
259 wake_up(&hb->wait);
260
261 bio_put(bio);
262}
263
Mike Christie162b99e2016-06-05 14:32:02 -0500264static int hib_submit_io(int op, int op_flags, pgoff_t page_off, void *addr,
Christoph Hellwig343df3c2015-05-19 09:23:23 +0200265 struct hib_bio_batch *hb)
266{
267 struct page *page = virt_to_page(addr);
268 struct bio *bio;
269 int error = 0;
270
Mel Gorman71baba42015-11-06 16:28:28 -0800271 bio = bio_alloc(__GFP_RECLAIM | __GFP_HIGH, 1);
Christoph Hellwig343df3c2015-05-19 09:23:23 +0200272 bio->bi_iter.bi_sector = page_off * (PAGE_SIZE >> 9);
273 bio->bi_bdev = hib_resume_bdev;
Mike Christie162b99e2016-06-05 14:32:02 -0500274 bio_set_op_attrs(bio, op, op_flags);
Christoph Hellwig343df3c2015-05-19 09:23:23 +0200275
276 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
277 printk(KERN_ERR "PM: Adding page to bio failed at %llu\n",
278 (unsigned long long)bio->bi_iter.bi_sector);
279 bio_put(bio);
280 return -EFAULT;
281 }
282
283 if (hb) {
284 bio->bi_end_io = hib_end_io;
285 bio->bi_private = hb;
286 atomic_inc(&hb->count);
Mike Christie4e49ea42016-06-05 14:31:41 -0500287 submit_bio(bio);
Christoph Hellwig343df3c2015-05-19 09:23:23 +0200288 } else {
Mike Christie4e49ea42016-06-05 14:31:41 -0500289 error = submit_bio_wait(bio);
Christoph Hellwig343df3c2015-05-19 09:23:23 +0200290 bio_put(bio);
291 }
292
293 return error;
294}
295
296static int hib_wait_io(struct hib_bio_batch *hb)
297{
298 wait_event(hb->wait, atomic_read(&hb->count) == 0);
299 return hb->error;
300}
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -0800301
Rafael J. Wysocki3fc6b342006-12-06 20:34:09 -0800302/*
303 * Saving part
304 */
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800305
Jiri Slaby51fb3522010-05-01 23:53:02 +0200306static int mark_swapfiles(struct swap_map_handle *handle, unsigned int flags)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800307{
308 int error;
309
Mike Christie162b99e2016-06-05 14:32:02 -0500310 hib_submit_io(REQ_OP_READ, READ_SYNC, swsusp_resume_block,
311 swsusp_header, NULL);
Vivek Goyal1b29c162007-05-02 19:27:07 +0200312 if (!memcmp("SWAP-SPACE",swsusp_header->sig, 10) ||
313 !memcmp("SWAPSPACE2",swsusp_header->sig, 10)) {
314 memcpy(swsusp_header->orig_sig,swsusp_header->sig, 10);
Rafael J. Wysocki3624eb02010-10-04 22:08:12 +0200315 memcpy(swsusp_header->sig, HIBERNATE_SIG, 10);
Jiri Slaby51fb3522010-05-01 23:53:02 +0200316 swsusp_header->image = handle->first_sector;
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700317 swsusp_header->flags = flags;
Bojan Smojver081a9d02011-10-13 23:58:07 +0200318 if (flags & SF_CRC32_MODE)
319 swsusp_header->crc32 = handle->crc32;
Mike Christie162b99e2016-06-05 14:32:02 -0500320 error = hib_submit_io(REQ_OP_WRITE, WRITE_SYNC,
321 swsusp_resume_block, swsusp_header, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800322 } else {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100323 printk(KERN_ERR "PM: Swap header not found!\n");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800324 error = -ENODEV;
325 }
326 return error;
327}
328
329/**
330 * swsusp_swap_check - check if the resume device is a swap device
331 * and get its index (if so)
Jiri Slaby6f612af2010-05-01 23:54:02 +0200332 *
333 * This is called before saving image
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800334 */
Jiri Slaby6f612af2010-05-01 23:54:02 +0200335static int swsusp_swap_check(void)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800336{
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800337 int res;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800338
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800339 res = swap_type_of(swsusp_resume_device, swsusp_resume_block,
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200340 &hib_resume_bdev);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800341 if (res < 0)
342 return res;
343
344 root_swap = res;
Tejun Heoe525fd82010-11-13 11:55:17 +0100345 res = blkdev_get(hib_resume_bdev, FMODE_WRITE, NULL);
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800346 if (res)
347 return res;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800348
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200349 res = set_blocksize(hib_resume_bdev, PAGE_SIZE);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800350 if (res < 0)
Jiri Slaby8a0d6132010-05-01 23:52:34 +0200351 blkdev_put(hib_resume_bdev, FMODE_WRITE);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800352
Chen Yufe12c002016-07-22 10:30:47 +0800353 /*
354 * Update the resume device to the one actually used,
355 * so the test_resume mode can use it in case it is
356 * invoked from hibernate() to test the snapshot.
357 */
358 swsusp_resume_device = hib_resume_bdev->bd_dev;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800359 return res;
360}
361
362/**
363 * write_page - Write one page to given swap location.
364 * @buf: Address we're writing.
365 * @offset: Offset of the swap page we're writing to.
Christoph Hellwig343df3c2015-05-19 09:23:23 +0200366 * @hb: bio completion batch
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800367 */
368
Christoph Hellwig343df3c2015-05-19 09:23:23 +0200369static int write_page(void *buf, sector_t offset, struct hib_bio_batch *hb)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800370{
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800371 void *src;
Bojan Smojver081a9d02011-10-13 23:58:07 +0200372 int ret;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800373
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800374 if (!offset)
375 return -ENOSPC;
Andrew Mortonab954162006-09-25 23:32:42 -0700376
Christoph Hellwig343df3c2015-05-19 09:23:23 +0200377 if (hb) {
Mel Gorman71baba42015-11-06 16:28:28 -0800378 src = (void *)__get_free_page(__GFP_RECLAIM | __GFP_NOWARN |
Bojan Smojver5a21d482012-04-29 22:42:06 +0200379 __GFP_NORETRY);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800380 if (src) {
Jan Beulich3ecb01d2010-10-26 14:22:27 -0700381 copy_page(src, buf);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800382 } else {
Christoph Hellwig343df3c2015-05-19 09:23:23 +0200383 ret = hib_wait_io(hb); /* Free pages */
Bojan Smojver081a9d02011-10-13 23:58:07 +0200384 if (ret)
385 return ret;
Mel Gorman71baba42015-11-06 16:28:28 -0800386 src = (void *)__get_free_page(__GFP_RECLAIM |
Bojan Smojver5a21d482012-04-29 22:42:06 +0200387 __GFP_NOWARN |
388 __GFP_NORETRY);
Bojan Smojver081a9d02011-10-13 23:58:07 +0200389 if (src) {
390 copy_page(src, buf);
391 } else {
392 WARN_ON_ONCE(1);
Christoph Hellwig343df3c2015-05-19 09:23:23 +0200393 hb = NULL; /* Go synchronous */
Bojan Smojver081a9d02011-10-13 23:58:07 +0200394 src = buf;
395 }
Andrew Mortonab954162006-09-25 23:32:42 -0700396 }
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800397 } else {
398 src = buf;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800399 }
Mike Christie162b99e2016-06-05 14:32:02 -0500400 return hib_submit_io(REQ_OP_WRITE, WRITE_SYNC, offset, src, hb);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800401}
402
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800403static void release_swap_writer(struct swap_map_handle *handle)
404{
405 if (handle->cur)
406 free_page((unsigned long)handle->cur);
407 handle->cur = NULL;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800408}
409
410static int get_swap_writer(struct swap_map_handle *handle)
411{
Jiri Slaby6f612af2010-05-01 23:54:02 +0200412 int ret;
413
414 ret = swsusp_swap_check();
415 if (ret) {
416 if (ret != -ENOSPC)
417 printk(KERN_ERR "PM: Cannot find swap device, try "
418 "swapon -a.\n");
419 return ret;
420 }
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800421 handle->cur = (struct swap_map_page *)get_zeroed_page(GFP_KERNEL);
Jiri Slaby6f612af2010-05-01 23:54:02 +0200422 if (!handle->cur) {
423 ret = -ENOMEM;
424 goto err_close;
425 }
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700426 handle->cur_swap = alloc_swapdev_block(root_swap);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800427 if (!handle->cur_swap) {
Jiri Slaby6f612af2010-05-01 23:54:02 +0200428 ret = -ENOSPC;
429 goto err_rel;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800430 }
431 handle->k = 0;
Bojan Smojverf8262d42012-04-24 23:53:28 +0200432 handle->reqd_free_pages = reqd_free_pages();
Jiri Slaby51fb3522010-05-01 23:53:02 +0200433 handle->first_sector = handle->cur_swap;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800434 return 0;
Jiri Slaby6f612af2010-05-01 23:54:02 +0200435err_rel:
436 release_swap_writer(handle);
437err_close:
438 swsusp_close(FMODE_WRITE);
439 return ret;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800440}
441
Andrew Mortonab954162006-09-25 23:32:42 -0700442static int swap_write_page(struct swap_map_handle *handle, void *buf,
Christoph Hellwig343df3c2015-05-19 09:23:23 +0200443 struct hib_bio_batch *hb)
Andrew Mortonab954162006-09-25 23:32:42 -0700444{
445 int error = 0;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800446 sector_t offset;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800447
448 if (!handle->cur)
449 return -EINVAL;
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700450 offset = alloc_swapdev_block(root_swap);
Christoph Hellwig343df3c2015-05-19 09:23:23 +0200451 error = write_page(buf, offset, hb);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800452 if (error)
453 return error;
454 handle->cur->entries[handle->k++] = offset;
455 if (handle->k >= MAP_PAGE_ENTRIES) {
Rafael J. Wysockid1d241c2007-05-06 14:50:47 -0700456 offset = alloc_swapdev_block(root_swap);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800457 if (!offset)
458 return -ENOSPC;
459 handle->cur->next_swap = offset;
Christoph Hellwig343df3c2015-05-19 09:23:23 +0200460 error = write_page(handle->cur, handle->cur_swap, hb);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800461 if (error)
Andrew Mortonab954162006-09-25 23:32:42 -0700462 goto out;
Jan Beulich3ecb01d2010-10-26 14:22:27 -0700463 clear_page(handle->cur);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800464 handle->cur_swap = offset;
465 handle->k = 0;
Bojan Smojver5a21d482012-04-29 22:42:06 +0200466
Christoph Hellwig343df3c2015-05-19 09:23:23 +0200467 if (hb && low_free_pages() <= handle->reqd_free_pages) {
468 error = hib_wait_io(hb);
Bojan Smojver5a21d482012-04-29 22:42:06 +0200469 if (error)
470 goto out;
471 /*
472 * Recalculate the number of required free pages, to
473 * make sure we never take more than half.
474 */
475 handle->reqd_free_pages = reqd_free_pages();
476 }
Bojan Smojver081a9d02011-10-13 23:58:07 +0200477 }
Rafael J. Wysocki59a493352006-12-06 20:34:44 -0800478 out:
Andrew Mortonab954162006-09-25 23:32:42 -0700479 return error;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800480}
481
482static int flush_swap_writer(struct swap_map_handle *handle)
483{
484 if (handle->cur && handle->cur_swap)
Andrew Mortonab954162006-09-25 23:32:42 -0700485 return write_page(handle->cur, handle->cur_swap, NULL);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800486 else
487 return -EINVAL;
488}
489
Jiri Slaby6f612af2010-05-01 23:54:02 +0200490static int swap_writer_finish(struct swap_map_handle *handle,
491 unsigned int flags, int error)
492{
493 if (!error) {
494 flush_swap_writer(handle);
495 printk(KERN_INFO "PM: S");
496 error = mark_swapfiles(handle, flags);
497 printk("|\n");
498 }
499
500 if (error)
501 free_all_swap_pages(root_swap);
502 release_swap_writer(handle);
503 swsusp_close(FMODE_WRITE);
504
505 return error;
506}
507
Bojan Smojverf996fc92010-09-09 23:06:23 +0200508/* We need to remember how much compressed data we need to read. */
509#define LZO_HEADER sizeof(size_t)
510
511/* Number of pages/bytes we'll compress at one time. */
512#define LZO_UNC_PAGES 32
513#define LZO_UNC_SIZE (LZO_UNC_PAGES * PAGE_SIZE)
514
515/* Number of pages/bytes we need for compressed data (worst case). */
516#define LZO_CMP_PAGES DIV_ROUND_UP(lzo1x_worst_compress(LZO_UNC_SIZE) + \
517 LZO_HEADER, PAGE_SIZE)
518#define LZO_CMP_SIZE (LZO_CMP_PAGES * PAGE_SIZE)
519
Bojan Smojver081a9d02011-10-13 23:58:07 +0200520/* Maximum number of threads for compression/decompression. */
521#define LZO_THREADS 3
522
Bojan Smojver5a21d482012-04-29 22:42:06 +0200523/* Minimum/maximum number of pages for read buffering. */
524#define LZO_MIN_RD_PAGES 1024
525#define LZO_MAX_RD_PAGES 8192
Bojan Smojver081a9d02011-10-13 23:58:07 +0200526
527
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800528/**
529 * save_image - save the suspend image data
530 */
531
532static int save_image(struct swap_map_handle *handle,
533 struct snapshot_handle *snapshot,
Andrew Morton3a4f7572006-09-25 23:32:41 -0700534 unsigned int nr_to_write)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800535{
536 unsigned int m;
537 int ret;
Andrew Morton3a4f7572006-09-25 23:32:41 -0700538 int nr_pages;
Andrew Mortonab954162006-09-25 23:32:42 -0700539 int err2;
Christoph Hellwig343df3c2015-05-19 09:23:23 +0200540 struct hib_bio_batch hb;
Tina Ruchandanidb597602014-10-30 11:04:53 -0700541 ktime_t start;
542 ktime_t stop;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800543
Christoph Hellwig343df3c2015-05-19 09:23:23 +0200544 hib_init_batch(&hb);
545
Bojan Smojverd8150d32012-06-21 22:27:24 +0200546 printk(KERN_INFO "PM: Saving image data pages (%u pages)...\n",
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100547 nr_to_write);
Bojan Smojverd8150d32012-06-21 22:27:24 +0200548 m = nr_to_write / 10;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800549 if (!m)
550 m = 1;
551 nr_pages = 0;
Tina Ruchandanidb597602014-10-30 11:04:53 -0700552 start = ktime_get();
Jiri Slaby4ff277f2009-10-28 22:55:33 +0100553 while (1) {
Jiri Slabyd3c1b242010-05-01 23:52:02 +0200554 ret = snapshot_read_next(snapshot);
Jiri Slaby4ff277f2009-10-28 22:55:33 +0100555 if (ret <= 0)
556 break;
Christoph Hellwig343df3c2015-05-19 09:23:23 +0200557 ret = swap_write_page(handle, data_of(*snapshot), &hb);
Jiri Slaby4ff277f2009-10-28 22:55:33 +0100558 if (ret)
559 break;
560 if (!(nr_pages % m))
Bojan Smojverd8150d32012-06-21 22:27:24 +0200561 printk(KERN_INFO "PM: Image saving progress: %3d%%\n",
562 nr_pages / m * 10);
Jiri Slaby4ff277f2009-10-28 22:55:33 +0100563 nr_pages++;
564 }
Christoph Hellwig343df3c2015-05-19 09:23:23 +0200565 err2 = hib_wait_io(&hb);
Tina Ruchandanidb597602014-10-30 11:04:53 -0700566 stop = ktime_get();
Jiri Slaby4ff277f2009-10-28 22:55:33 +0100567 if (!ret)
568 ret = err2;
569 if (!ret)
Bojan Smojverd8150d32012-06-21 22:27:24 +0200570 printk(KERN_INFO "PM: Image saving done.\n");
Tina Ruchandanidb597602014-10-30 11:04:53 -0700571 swsusp_show_speed(start, stop, nr_to_write, "Wrote");
Jiri Slaby4ff277f2009-10-28 22:55:33 +0100572 return ret;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800573}
574
Bojan Smojver081a9d02011-10-13 23:58:07 +0200575/**
576 * Structure used for CRC32.
577 */
578struct crc_data {
579 struct task_struct *thr; /* thread */
580 atomic_t ready; /* ready to start flag */
581 atomic_t stop; /* ready to stop flag */
582 unsigned run_threads; /* nr current threads */
583 wait_queue_head_t go; /* start crc update */
584 wait_queue_head_t done; /* crc update done */
585 u32 *crc32; /* points to handle's crc32 */
586 size_t *unc_len[LZO_THREADS]; /* uncompressed lengths */
587 unsigned char *unc[LZO_THREADS]; /* uncompressed data */
588};
589
590/**
591 * CRC32 update function that runs in its own thread.
592 */
593static int crc32_threadfn(void *data)
594{
595 struct crc_data *d = data;
596 unsigned i;
597
598 while (1) {
599 wait_event(d->go, atomic_read(&d->ready) ||
600 kthread_should_stop());
601 if (kthread_should_stop()) {
602 d->thr = NULL;
603 atomic_set(&d->stop, 1);
604 wake_up(&d->done);
605 break;
606 }
607 atomic_set(&d->ready, 0);
608
Patrick Daly050f2572018-05-07 13:07:50 -0700609 if (!IS_ENABLED(CONFIG_HIBERNATION_SKIP_CRC))
610 for (i = 0; i < d->run_threads; i++)
611 *d->crc32 = crc32_le(*d->crc32,
612 d->unc[i], *d->unc_len[i]);
Bojan Smojver081a9d02011-10-13 23:58:07 +0200613 atomic_set(&d->stop, 1);
614 wake_up(&d->done);
615 }
616 return 0;
617}
618/**
619 * Structure used for LZO data compression.
620 */
621struct cmp_data {
622 struct task_struct *thr; /* thread */
623 atomic_t ready; /* ready to start flag */
624 atomic_t stop; /* ready to stop flag */
625 int ret; /* return code */
626 wait_queue_head_t go; /* start compression */
627 wait_queue_head_t done; /* compression done */
628 size_t unc_len; /* uncompressed length */
629 size_t cmp_len; /* compressed length */
630 unsigned char unc[LZO_UNC_SIZE]; /* uncompressed buffer */
631 unsigned char cmp[LZO_CMP_SIZE]; /* compressed buffer */
632 unsigned char wrk[LZO1X_1_MEM_COMPRESS]; /* compression workspace */
633};
634
635/**
636 * Compression function that runs in its own thread.
637 */
638static int lzo_compress_threadfn(void *data)
639{
640 struct cmp_data *d = data;
641
642 while (1) {
643 wait_event(d->go, atomic_read(&d->ready) ||
644 kthread_should_stop());
645 if (kthread_should_stop()) {
646 d->thr = NULL;
647 d->ret = -1;
648 atomic_set(&d->stop, 1);
649 wake_up(&d->done);
650 break;
651 }
652 atomic_set(&d->ready, 0);
653
654 d->ret = lzo1x_1_compress(d->unc, d->unc_len,
655 d->cmp + LZO_HEADER, &d->cmp_len,
656 d->wrk);
657 atomic_set(&d->stop, 1);
658 wake_up(&d->done);
659 }
660 return 0;
661}
Bojan Smojverf996fc92010-09-09 23:06:23 +0200662
663/**
664 * save_image_lzo - Save the suspend image data compressed with LZO.
Niv Yehezkel057b0a72014-05-31 06:26:01 -0400665 * @handle: Swap map handle to use for saving the image.
Bojan Smojverf996fc92010-09-09 23:06:23 +0200666 * @snapshot: Image to read data from.
667 * @nr_to_write: Number of pages to save.
668 */
669static int save_image_lzo(struct swap_map_handle *handle,
670 struct snapshot_handle *snapshot,
671 unsigned int nr_to_write)
672{
673 unsigned int m;
674 int ret = 0;
675 int nr_pages;
676 int err2;
Christoph Hellwig343df3c2015-05-19 09:23:23 +0200677 struct hib_bio_batch hb;
Tina Ruchandanidb597602014-10-30 11:04:53 -0700678 ktime_t start;
679 ktime_t stop;
Bojan Smojver081a9d02011-10-13 23:58:07 +0200680 size_t off;
681 unsigned thr, run_threads, nr_threads;
682 unsigned char *page = NULL;
683 struct cmp_data *data = NULL;
684 struct crc_data *crc = NULL;
685
Christoph Hellwig343df3c2015-05-19 09:23:23 +0200686 hib_init_batch(&hb);
687
Bojan Smojver081a9d02011-10-13 23:58:07 +0200688 /*
689 * We'll limit the number of threads for compression to limit memory
690 * footprint.
691 */
692 nr_threads = num_online_cpus() - 1;
693 nr_threads = clamp_val(nr_threads, 1, LZO_THREADS);
Bojan Smojverf996fc92010-09-09 23:06:23 +0200694
Mel Gorman71baba42015-11-06 16:28:28 -0800695 page = (void *)__get_free_page(__GFP_RECLAIM | __GFP_HIGH);
Bojan Smojverf996fc92010-09-09 23:06:23 +0200696 if (!page) {
697 printk(KERN_ERR "PM: Failed to allocate LZO page\n");
Bojan Smojver081a9d02011-10-13 23:58:07 +0200698 ret = -ENOMEM;
699 goto out_clean;
Bojan Smojverf996fc92010-09-09 23:06:23 +0200700 }
701
Bojan Smojver081a9d02011-10-13 23:58:07 +0200702 data = vmalloc(sizeof(*data) * nr_threads);
703 if (!data) {
704 printk(KERN_ERR "PM: Failed to allocate LZO data\n");
705 ret = -ENOMEM;
706 goto out_clean;
707 }
708 for (thr = 0; thr < nr_threads; thr++)
709 memset(&data[thr], 0, offsetof(struct cmp_data, go));
710
711 crc = kmalloc(sizeof(*crc), GFP_KERNEL);
712 if (!crc) {
713 printk(KERN_ERR "PM: Failed to allocate crc\n");
714 ret = -ENOMEM;
715 goto out_clean;
716 }
717 memset(crc, 0, offsetof(struct crc_data, go));
718
719 /*
720 * Start the compression threads.
721 */
722 for (thr = 0; thr < nr_threads; thr++) {
723 init_waitqueue_head(&data[thr].go);
724 init_waitqueue_head(&data[thr].done);
725
726 data[thr].thr = kthread_run(lzo_compress_threadfn,
727 &data[thr],
728 "image_compress/%u", thr);
729 if (IS_ERR(data[thr].thr)) {
730 data[thr].thr = NULL;
731 printk(KERN_ERR
732 "PM: Cannot start compression threads\n");
733 ret = -ENOMEM;
734 goto out_clean;
735 }
Bojan Smojverf996fc92010-09-09 23:06:23 +0200736 }
737
Bojan Smojver081a9d02011-10-13 23:58:07 +0200738 /*
Bojan Smojver081a9d02011-10-13 23:58:07 +0200739 * Start the CRC32 thread.
740 */
741 init_waitqueue_head(&crc->go);
742 init_waitqueue_head(&crc->done);
743
744 handle->crc32 = 0;
745 crc->crc32 = &handle->crc32;
746 for (thr = 0; thr < nr_threads; thr++) {
747 crc->unc[thr] = data[thr].unc;
748 crc->unc_len[thr] = &data[thr].unc_len;
Bojan Smojverf996fc92010-09-09 23:06:23 +0200749 }
750
Bojan Smojver081a9d02011-10-13 23:58:07 +0200751 crc->thr = kthread_run(crc32_threadfn, crc, "image_crc32");
752 if (IS_ERR(crc->thr)) {
753 crc->thr = NULL;
754 printk(KERN_ERR "PM: Cannot start CRC32 thread\n");
755 ret = -ENOMEM;
756 goto out_clean;
Bojan Smojverf996fc92010-09-09 23:06:23 +0200757 }
758
Bojan Smojver5a21d482012-04-29 22:42:06 +0200759 /*
760 * Adjust the number of required free pages after all allocations have
761 * been done. We don't want to run out of pages when writing.
762 */
763 handle->reqd_free_pages = reqd_free_pages();
764
Bojan Smojverf996fc92010-09-09 23:06:23 +0200765 printk(KERN_INFO
Bojan Smojver081a9d02011-10-13 23:58:07 +0200766 "PM: Using %u thread(s) for compression.\n"
Bojan Smojverd8150d32012-06-21 22:27:24 +0200767 "PM: Compressing and saving image data (%u pages)...\n",
Bojan Smojver081a9d02011-10-13 23:58:07 +0200768 nr_threads, nr_to_write);
Bojan Smojverd8150d32012-06-21 22:27:24 +0200769 m = nr_to_write / 10;
Bojan Smojverf996fc92010-09-09 23:06:23 +0200770 if (!m)
771 m = 1;
772 nr_pages = 0;
Tina Ruchandanidb597602014-10-30 11:04:53 -0700773 start = ktime_get();
Bojan Smojverf996fc92010-09-09 23:06:23 +0200774 for (;;) {
Bojan Smojver081a9d02011-10-13 23:58:07 +0200775 for (thr = 0; thr < nr_threads; thr++) {
776 for (off = 0; off < LZO_UNC_SIZE; off += PAGE_SIZE) {
777 ret = snapshot_read_next(snapshot);
778 if (ret < 0)
779 goto out_finish;
Bojan Smojverf996fc92010-09-09 23:06:23 +0200780
Bojan Smojver081a9d02011-10-13 23:58:07 +0200781 if (!ret)
782 break;
783
784 memcpy(data[thr].unc + off,
785 data_of(*snapshot), PAGE_SIZE);
786
787 if (!(nr_pages % m))
Bojan Smojverd8150d32012-06-21 22:27:24 +0200788 printk(KERN_INFO
789 "PM: Image saving progress: "
790 "%3d%%\n",
791 nr_pages / m * 10);
Bojan Smojver081a9d02011-10-13 23:58:07 +0200792 nr_pages++;
793 }
794 if (!off)
Bojan Smojverf996fc92010-09-09 23:06:23 +0200795 break;
796
Bojan Smojver081a9d02011-10-13 23:58:07 +0200797 data[thr].unc_len = off;
Bojan Smojverf996fc92010-09-09 23:06:23 +0200798
Bojan Smojver081a9d02011-10-13 23:58:07 +0200799 atomic_set(&data[thr].ready, 1);
800 wake_up(&data[thr].go);
Bojan Smojverf996fc92010-09-09 23:06:23 +0200801 }
802
Bojan Smojver081a9d02011-10-13 23:58:07 +0200803 if (!thr)
Bojan Smojverf996fc92010-09-09 23:06:23 +0200804 break;
805
Bojan Smojver081a9d02011-10-13 23:58:07 +0200806 crc->run_threads = thr;
807 atomic_set(&crc->ready, 1);
808 wake_up(&crc->go);
Bojan Smojverf996fc92010-09-09 23:06:23 +0200809
Bojan Smojver081a9d02011-10-13 23:58:07 +0200810 for (run_threads = thr, thr = 0; thr < run_threads; thr++) {
811 wait_event(data[thr].done,
812 atomic_read(&data[thr].stop));
813 atomic_set(&data[thr].stop, 0);
Bojan Smojverf996fc92010-09-09 23:06:23 +0200814
Bojan Smojver081a9d02011-10-13 23:58:07 +0200815 ret = data[thr].ret;
Bojan Smojverf996fc92010-09-09 23:06:23 +0200816
Bojan Smojver081a9d02011-10-13 23:58:07 +0200817 if (ret < 0) {
818 printk(KERN_ERR "PM: LZO compression failed\n");
Bojan Smojverf996fc92010-09-09 23:06:23 +0200819 goto out_finish;
Bojan Smojver081a9d02011-10-13 23:58:07 +0200820 }
821
822 if (unlikely(!data[thr].cmp_len ||
823 data[thr].cmp_len >
824 lzo1x_worst_compress(data[thr].unc_len))) {
825 printk(KERN_ERR
826 "PM: Invalid LZO compressed length\n");
827 ret = -1;
828 goto out_finish;
829 }
830
831 *(size_t *)data[thr].cmp = data[thr].cmp_len;
832
833 /*
834 * Given we are writing one page at a time to disk, we
835 * copy that much from the buffer, although the last
836 * bit will likely be smaller than full page. This is
837 * OK - we saved the length of the compressed data, so
838 * any garbage at the end will be discarded when we
839 * read it.
840 */
841 for (off = 0;
842 off < LZO_HEADER + data[thr].cmp_len;
843 off += PAGE_SIZE) {
844 memcpy(page, data[thr].cmp + off, PAGE_SIZE);
845
Christoph Hellwig343df3c2015-05-19 09:23:23 +0200846 ret = swap_write_page(handle, page, &hb);
Bojan Smojver081a9d02011-10-13 23:58:07 +0200847 if (ret)
848 goto out_finish;
849 }
Bojan Smojverf996fc92010-09-09 23:06:23 +0200850 }
Bojan Smojver081a9d02011-10-13 23:58:07 +0200851
852 wait_event(crc->done, atomic_read(&crc->stop));
853 atomic_set(&crc->stop, 0);
Bojan Smojverf996fc92010-09-09 23:06:23 +0200854 }
855
856out_finish:
Christoph Hellwig343df3c2015-05-19 09:23:23 +0200857 err2 = hib_wait_io(&hb);
Tina Ruchandanidb597602014-10-30 11:04:53 -0700858 stop = ktime_get();
Bojan Smojverf996fc92010-09-09 23:06:23 +0200859 if (!ret)
860 ret = err2;
Bojan Smojverd8150d32012-06-21 22:27:24 +0200861 if (!ret)
862 printk(KERN_INFO "PM: Image saving done.\n");
Tina Ruchandanidb597602014-10-30 11:04:53 -0700863 swsusp_show_speed(start, stop, nr_to_write, "Wrote");
Bojan Smojver081a9d02011-10-13 23:58:07 +0200864out_clean:
865 if (crc) {
866 if (crc->thr)
867 kthread_stop(crc->thr);
868 kfree(crc);
869 }
870 if (data) {
871 for (thr = 0; thr < nr_threads; thr++)
872 if (data[thr].thr)
873 kthread_stop(data[thr].thr);
874 vfree(data);
875 }
876 if (page) free_page((unsigned long)page);
Bojan Smojverf996fc92010-09-09 23:06:23 +0200877
878 return ret;
879}
880
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800881/**
882 * enough_swap - Make sure we have enough swap to save the image.
883 *
884 * Returns TRUE or FALSE after checking the total amount of swap
885 * space avaiable from the resume partition.
886 */
887
Bojan Smojverf996fc92010-09-09 23:06:23 +0200888static int enough_swap(unsigned int nr_pages, unsigned int flags)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800889{
890 unsigned int free_swap = count_swap_pages(root_swap, 1);
Bojan Smojverf996fc92010-09-09 23:06:23 +0200891 unsigned int required;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800892
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100893 pr_debug("PM: Free swap pages: %u\n", free_swap);
Bojan Smojverf996fc92010-09-09 23:06:23 +0200894
Barry Songee34a372012-01-09 12:56:23 +0800895 required = PAGES_FOR_IO + nr_pages;
Bojan Smojverf996fc92010-09-09 23:06:23 +0200896 return free_swap > required;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800897}
898
899/**
900 * swsusp_write - Write entire image and metadata.
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700901 * @flags: flags to pass to the "boot" kernel in the image header
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800902 *
903 * It is important _NOT_ to umount filesystems at this point. We want
904 * them synced (in case something goes wrong) but we DO not want to mark
905 * filesystem clean: it is not. (And it does not matter, if we resume
906 * correctly, we'll mark system clean, anyway.)
907 */
908
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700909int swsusp_write(unsigned int flags)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800910{
911 struct swap_map_handle handle;
912 struct snapshot_handle snapshot;
913 struct swsusp_info *header;
Jiri Slaby6f612af2010-05-01 23:54:02 +0200914 unsigned long pages;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800915 int error;
916
Jiri Slaby6f612af2010-05-01 23:54:02 +0200917 pages = snapshot_get_image_size();
918 error = get_swap_writer(&handle);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800919 if (error) {
Jiri Slaby6f612af2010-05-01 23:54:02 +0200920 printk(KERN_ERR "PM: Cannot get swap writer\n");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800921 return error;
922 }
Barry Songee34a372012-01-09 12:56:23 +0800923 if (flags & SF_NOCOMPRESS_MODE) {
924 if (!enough_swap(pages, flags)) {
925 printk(KERN_ERR "PM: Not enough free swap\n");
926 error = -ENOSPC;
927 goto out_finish;
928 }
Jiri Slaby6f612af2010-05-01 23:54:02 +0200929 }
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800930 memset(&snapshot, 0, sizeof(struct snapshot_handle));
Jiri Slabyd3c1b242010-05-01 23:52:02 +0200931 error = snapshot_read_next(&snapshot);
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800932 if (error < PAGE_SIZE) {
933 if (error >= 0)
934 error = -EFAULT;
935
Jiri Slaby6f612af2010-05-01 23:54:02 +0200936 goto out_finish;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800937 }
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800938 header = (struct swsusp_info *)data_of(snapshot);
Jiri Slaby6f612af2010-05-01 23:54:02 +0200939 error = swap_write_page(&handle, header, NULL);
Bojan Smojverf996fc92010-09-09 23:06:23 +0200940 if (!error) {
941 error = (flags & SF_NOCOMPRESS_MODE) ?
942 save_image(&handle, &snapshot, pages - 1) :
943 save_image_lzo(&handle, &snapshot, pages - 1);
944 }
Jiri Slaby6f612af2010-05-01 23:54:02 +0200945out_finish:
946 error = swap_writer_finish(&handle, flags, error);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800947 return error;
948}
949
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800950/**
951 * The following functions allow us to read data using a swap map
952 * in a file-alike way
953 */
954
955static void release_swap_reader(struct swap_map_handle *handle)
956{
Bojan Smojver081a9d02011-10-13 23:58:07 +0200957 struct swap_map_page_list *tmp;
958
959 while (handle->maps) {
960 if (handle->maps->map)
961 free_page((unsigned long)handle->maps->map);
962 tmp = handle->maps;
963 handle->maps = handle->maps->next;
964 kfree(tmp);
965 }
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800966 handle->cur = NULL;
967}
968
Jiri Slaby6f612af2010-05-01 23:54:02 +0200969static int get_swap_reader(struct swap_map_handle *handle,
970 unsigned int *flags_p)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800971{
972 int error;
Bojan Smojver081a9d02011-10-13 23:58:07 +0200973 struct swap_map_page_list *tmp, *last;
974 sector_t offset;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800975
Jiri Slaby6f612af2010-05-01 23:54:02 +0200976 *flags_p = swsusp_header->flags;
977
978 if (!swsusp_header->image) /* how can this happen? */
Rafael J. Wysocki61159a32006-03-23 03:00:00 -0800979 return -EINVAL;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800980
Bojan Smojver081a9d02011-10-13 23:58:07 +0200981 handle->cur = NULL;
982 last = handle->maps = NULL;
983 offset = swsusp_header->image;
984 while (offset) {
985 tmp = kmalloc(sizeof(*handle->maps), GFP_KERNEL);
986 if (!tmp) {
987 release_swap_reader(handle);
988 return -ENOMEM;
989 }
990 memset(tmp, 0, sizeof(*tmp));
991 if (!handle->maps)
992 handle->maps = tmp;
993 if (last)
994 last->next = tmp;
995 last = tmp;
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -0800996
Bojan Smojver081a9d02011-10-13 23:58:07 +0200997 tmp->map = (struct swap_map_page *)
Mel Gorman71baba42015-11-06 16:28:28 -0800998 __get_free_page(__GFP_RECLAIM | __GFP_HIGH);
Bojan Smojver081a9d02011-10-13 23:58:07 +0200999 if (!tmp->map) {
1000 release_swap_reader(handle);
1001 return -ENOMEM;
1002 }
1003
Mike Christie162b99e2016-06-05 14:32:02 -05001004 error = hib_submit_io(REQ_OP_READ, READ_SYNC, offset,
1005 tmp->map, NULL);
Bojan Smojver081a9d02011-10-13 23:58:07 +02001006 if (error) {
1007 release_swap_reader(handle);
1008 return error;
1009 }
1010 offset = tmp->map->next_swap;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -08001011 }
1012 handle->k = 0;
Bojan Smojver081a9d02011-10-13 23:58:07 +02001013 handle->cur = handle->maps->map;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -08001014 return 0;
1015}
1016
Andrew Morton546e0d22006-09-25 23:32:44 -07001017static int swap_read_page(struct swap_map_handle *handle, void *buf,
Christoph Hellwig343df3c2015-05-19 09:23:23 +02001018 struct hib_bio_batch *hb)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -08001019{
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -08001020 sector_t offset;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -08001021 int error;
Bojan Smojver081a9d02011-10-13 23:58:07 +02001022 struct swap_map_page_list *tmp;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -08001023
1024 if (!handle->cur)
1025 return -EINVAL;
1026 offset = handle->cur->entries[handle->k];
1027 if (!offset)
1028 return -EFAULT;
Mike Christie162b99e2016-06-05 14:32:02 -05001029 error = hib_submit_io(REQ_OP_READ, READ_SYNC, offset, buf, hb);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -08001030 if (error)
1031 return error;
1032 if (++handle->k >= MAP_PAGE_ENTRIES) {
1033 handle->k = 0;
Bojan Smojver081a9d02011-10-13 23:58:07 +02001034 free_page((unsigned long)handle->maps->map);
1035 tmp = handle->maps;
1036 handle->maps = handle->maps->next;
1037 kfree(tmp);
1038 if (!handle->maps)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -08001039 release_swap_reader(handle);
Bojan Smojver081a9d02011-10-13 23:58:07 +02001040 else
1041 handle->cur = handle->maps->map;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -08001042 }
1043 return error;
1044}
1045
Jiri Slaby6f612af2010-05-01 23:54:02 +02001046static int swap_reader_finish(struct swap_map_handle *handle)
1047{
1048 release_swap_reader(handle);
1049
1050 return 0;
1051}
1052
Rafael J. Wysocki61159a32006-03-23 03:00:00 -08001053/**
1054 * load_image - load the image using the swap map handle
1055 * @handle and the snapshot handle @snapshot
1056 * (assume there are @nr_pages pages to load)
1057 */
1058
1059static int load_image(struct swap_map_handle *handle,
1060 struct snapshot_handle *snapshot,
Andrew Morton546e0d22006-09-25 23:32:44 -07001061 unsigned int nr_to_read)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -08001062{
1063 unsigned int m;
Bojan Smojver081a9d02011-10-13 23:58:07 +02001064 int ret = 0;
Tina Ruchandanidb597602014-10-30 11:04:53 -07001065 ktime_t start;
1066 ktime_t stop;
Christoph Hellwig343df3c2015-05-19 09:23:23 +02001067 struct hib_bio_batch hb;
Andrew Morton546e0d22006-09-25 23:32:44 -07001068 int err2;
1069 unsigned nr_pages;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -08001070
Christoph Hellwig343df3c2015-05-19 09:23:23 +02001071 hib_init_batch(&hb);
1072
James Morsef6cf0542016-04-27 17:47:11 +01001073 clean_pages_on_read = true;
Bojan Smojverd8150d32012-06-21 22:27:24 +02001074 printk(KERN_INFO "PM: Loading image data pages (%u pages)...\n",
Rafael J. Wysocki23976722007-12-08 02:09:43 +01001075 nr_to_read);
Bojan Smojverd8150d32012-06-21 22:27:24 +02001076 m = nr_to_read / 10;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -08001077 if (!m)
1078 m = 1;
1079 nr_pages = 0;
Tina Ruchandanidb597602014-10-30 11:04:53 -07001080 start = ktime_get();
Andrew Morton546e0d22006-09-25 23:32:44 -07001081 for ( ; ; ) {
Bojan Smojver081a9d02011-10-13 23:58:07 +02001082 ret = snapshot_write_next(snapshot);
1083 if (ret <= 0)
Andrew Morton546e0d22006-09-25 23:32:44 -07001084 break;
Christoph Hellwig343df3c2015-05-19 09:23:23 +02001085 ret = swap_read_page(handle, data_of(*snapshot), &hb);
Bojan Smojver081a9d02011-10-13 23:58:07 +02001086 if (ret)
Andrew Morton546e0d22006-09-25 23:32:44 -07001087 break;
1088 if (snapshot->sync_read)
Christoph Hellwig343df3c2015-05-19 09:23:23 +02001089 ret = hib_wait_io(&hb);
Bojan Smojver081a9d02011-10-13 23:58:07 +02001090 if (ret)
Andrew Morton546e0d22006-09-25 23:32:44 -07001091 break;
1092 if (!(nr_pages % m))
Bojan Smojverd8150d32012-06-21 22:27:24 +02001093 printk(KERN_INFO "PM: Image loading progress: %3d%%\n",
1094 nr_pages / m * 10);
Andrew Morton546e0d22006-09-25 23:32:44 -07001095 nr_pages++;
1096 }
Christoph Hellwig343df3c2015-05-19 09:23:23 +02001097 err2 = hib_wait_io(&hb);
Tina Ruchandanidb597602014-10-30 11:04:53 -07001098 stop = ktime_get();
Bojan Smojver081a9d02011-10-13 23:58:07 +02001099 if (!ret)
1100 ret = err2;
1101 if (!ret) {
Bojan Smojverd8150d32012-06-21 22:27:24 +02001102 printk(KERN_INFO "PM: Image loading done.\n");
Rafael J. Wysocki83573762006-12-06 20:34:18 -08001103 snapshot_write_finalize(snapshot);
Con Kolivase655a252006-03-26 01:37:11 -08001104 if (!snapshot_image_loaded(snapshot))
Bojan Smojver081a9d02011-10-13 23:58:07 +02001105 ret = -ENODATA;
Bojan Smojverd8150d32012-06-21 22:27:24 +02001106 }
Tina Ruchandanidb597602014-10-30 11:04:53 -07001107 swsusp_show_speed(start, stop, nr_to_read, "Read");
Bojan Smojver081a9d02011-10-13 23:58:07 +02001108 return ret;
1109}
1110
1111/**
1112 * Structure used for LZO data decompression.
1113 */
1114struct dec_data {
1115 struct task_struct *thr; /* thread */
1116 atomic_t ready; /* ready to start flag */
1117 atomic_t stop; /* ready to stop flag */
1118 int ret; /* return code */
1119 wait_queue_head_t go; /* start decompression */
1120 wait_queue_head_t done; /* decompression done */
1121 size_t unc_len; /* uncompressed length */
1122 size_t cmp_len; /* compressed length */
1123 unsigned char unc[LZO_UNC_SIZE]; /* uncompressed buffer */
1124 unsigned char cmp[LZO_CMP_SIZE]; /* compressed buffer */
1125};
1126
1127/**
1128 * Deompression function that runs in its own thread.
1129 */
1130static int lzo_decompress_threadfn(void *data)
1131{
1132 struct dec_data *d = data;
1133
1134 while (1) {
1135 wait_event(d->go, atomic_read(&d->ready) ||
1136 kthread_should_stop());
1137 if (kthread_should_stop()) {
1138 d->thr = NULL;
1139 d->ret = -1;
1140 atomic_set(&d->stop, 1);
1141 wake_up(&d->done);
1142 break;
1143 }
1144 atomic_set(&d->ready, 0);
1145
1146 d->unc_len = LZO_UNC_SIZE;
1147 d->ret = lzo1x_decompress_safe(d->cmp + LZO_HEADER, d->cmp_len,
1148 d->unc, &d->unc_len);
James Morsef6cf0542016-04-27 17:47:11 +01001149 if (clean_pages_on_decompress)
1150 flush_icache_range((unsigned long)d->unc,
1151 (unsigned long)d->unc + d->unc_len);
1152
Bojan Smojver081a9d02011-10-13 23:58:07 +02001153 atomic_set(&d->stop, 1);
1154 wake_up(&d->done);
1155 }
1156 return 0;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -08001157}
1158
Rafael J. Wysockia634cc12007-07-19 01:47:30 -07001159/**
Bojan Smojverf996fc92010-09-09 23:06:23 +02001160 * load_image_lzo - Load compressed image data and decompress them with LZO.
1161 * @handle: Swap map handle to use for loading data.
1162 * @snapshot: Image to copy uncompressed data into.
1163 * @nr_to_read: Number of pages to load.
1164 */
1165static int load_image_lzo(struct swap_map_handle *handle,
1166 struct snapshot_handle *snapshot,
1167 unsigned int nr_to_read)
1168{
1169 unsigned int m;
Bojan Smojver081a9d02011-10-13 23:58:07 +02001170 int ret = 0;
1171 int eof = 0;
Christoph Hellwig343df3c2015-05-19 09:23:23 +02001172 struct hib_bio_batch hb;
Tina Ruchandanidb597602014-10-30 11:04:53 -07001173 ktime_t start;
1174 ktime_t stop;
Bojan Smojverf996fc92010-09-09 23:06:23 +02001175 unsigned nr_pages;
Bojan Smojver081a9d02011-10-13 23:58:07 +02001176 size_t off;
1177 unsigned i, thr, run_threads, nr_threads;
1178 unsigned ring = 0, pg = 0, ring_size = 0,
1179 have = 0, want, need, asked = 0;
Bojan Smojver5a21d482012-04-29 22:42:06 +02001180 unsigned long read_pages = 0;
Bojan Smojver081a9d02011-10-13 23:58:07 +02001181 unsigned char **page = NULL;
1182 struct dec_data *data = NULL;
1183 struct crc_data *crc = NULL;
Bojan Smojverf996fc92010-09-09 23:06:23 +02001184
Christoph Hellwig343df3c2015-05-19 09:23:23 +02001185 hib_init_batch(&hb);
1186
Bojan Smojver081a9d02011-10-13 23:58:07 +02001187 /*
1188 * We'll limit the number of threads for decompression to limit memory
1189 * footprint.
1190 */
1191 nr_threads = num_online_cpus() - 1;
1192 nr_threads = clamp_val(nr_threads, 1, LZO_THREADS);
Bojan Smojver9f339ca2010-11-25 23:41:39 +01001193
Bojan Smojver5a21d482012-04-29 22:42:06 +02001194 page = vmalloc(sizeof(*page) * LZO_MAX_RD_PAGES);
Bojan Smojver081a9d02011-10-13 23:58:07 +02001195 if (!page) {
1196 printk(KERN_ERR "PM: Failed to allocate LZO page\n");
1197 ret = -ENOMEM;
1198 goto out_clean;
1199 }
Bojan Smojver9f339ca2010-11-25 23:41:39 +01001200
Bojan Smojver081a9d02011-10-13 23:58:07 +02001201 data = vmalloc(sizeof(*data) * nr_threads);
1202 if (!data) {
1203 printk(KERN_ERR "PM: Failed to allocate LZO data\n");
1204 ret = -ENOMEM;
1205 goto out_clean;
1206 }
1207 for (thr = 0; thr < nr_threads; thr++)
1208 memset(&data[thr], 0, offsetof(struct dec_data, go));
1209
1210 crc = kmalloc(sizeof(*crc), GFP_KERNEL);
1211 if (!crc) {
1212 printk(KERN_ERR "PM: Failed to allocate crc\n");
1213 ret = -ENOMEM;
1214 goto out_clean;
1215 }
1216 memset(crc, 0, offsetof(struct crc_data, go));
1217
James Morsef6cf0542016-04-27 17:47:11 +01001218 clean_pages_on_decompress = true;
1219
Bojan Smojver081a9d02011-10-13 23:58:07 +02001220 /*
1221 * Start the decompression threads.
1222 */
1223 for (thr = 0; thr < nr_threads; thr++) {
1224 init_waitqueue_head(&data[thr].go);
1225 init_waitqueue_head(&data[thr].done);
1226
1227 data[thr].thr = kthread_run(lzo_decompress_threadfn,
1228 &data[thr],
1229 "image_decompress/%u", thr);
1230 if (IS_ERR(data[thr].thr)) {
1231 data[thr].thr = NULL;
1232 printk(KERN_ERR
1233 "PM: Cannot start decompression threads\n");
1234 ret = -ENOMEM;
1235 goto out_clean;
Bojan Smojver9f339ca2010-11-25 23:41:39 +01001236 }
Bojan Smojverf996fc92010-09-09 23:06:23 +02001237 }
1238
Bojan Smojver081a9d02011-10-13 23:58:07 +02001239 /*
1240 * Start the CRC32 thread.
1241 */
1242 init_waitqueue_head(&crc->go);
1243 init_waitqueue_head(&crc->done);
Bojan Smojver9f339ca2010-11-25 23:41:39 +01001244
Bojan Smojver081a9d02011-10-13 23:58:07 +02001245 handle->crc32 = 0;
1246 crc->crc32 = &handle->crc32;
1247 for (thr = 0; thr < nr_threads; thr++) {
1248 crc->unc[thr] = data[thr].unc;
1249 crc->unc_len[thr] = &data[thr].unc_len;
Bojan Smojverf996fc92010-09-09 23:06:23 +02001250 }
1251
Bojan Smojver081a9d02011-10-13 23:58:07 +02001252 crc->thr = kthread_run(crc32_threadfn, crc, "image_crc32");
1253 if (IS_ERR(crc->thr)) {
1254 crc->thr = NULL;
1255 printk(KERN_ERR "PM: Cannot start CRC32 thread\n");
1256 ret = -ENOMEM;
1257 goto out_clean;
Bojan Smojverf996fc92010-09-09 23:06:23 +02001258 }
1259
Bojan Smojver081a9d02011-10-13 23:58:07 +02001260 /*
Bojan Smojver5a21d482012-04-29 22:42:06 +02001261 * Set the number of pages for read buffering.
1262 * This is complete guesswork, because we'll only know the real
1263 * picture once prepare_image() is called, which is much later on
1264 * during the image load phase. We'll assume the worst case and
1265 * say that none of the image pages are from high memory.
Bojan Smojver081a9d02011-10-13 23:58:07 +02001266 */
Bojan Smojver5a21d482012-04-29 22:42:06 +02001267 if (low_free_pages() > snapshot_get_image_size())
1268 read_pages = (low_free_pages() - snapshot_get_image_size()) / 2;
1269 read_pages = clamp_val(read_pages, LZO_MIN_RD_PAGES, LZO_MAX_RD_PAGES);
Bojan Smojver081a9d02011-10-13 23:58:07 +02001270
1271 for (i = 0; i < read_pages; i++) {
1272 page[i] = (void *)__get_free_page(i < LZO_CMP_PAGES ?
Mel Gorman71baba42015-11-06 16:28:28 -08001273 __GFP_RECLAIM | __GFP_HIGH :
1274 __GFP_RECLAIM | __GFP_NOWARN |
1275 __GFP_NORETRY);
Bojan Smojver5a21d482012-04-29 22:42:06 +02001276
Bojan Smojver081a9d02011-10-13 23:58:07 +02001277 if (!page[i]) {
1278 if (i < LZO_CMP_PAGES) {
1279 ring_size = i;
1280 printk(KERN_ERR
1281 "PM: Failed to allocate LZO pages\n");
1282 ret = -ENOMEM;
1283 goto out_clean;
1284 } else {
1285 break;
1286 }
1287 }
1288 }
1289 want = ring_size = i;
1290
Bojan Smojverf996fc92010-09-09 23:06:23 +02001291 printk(KERN_INFO
Bojan Smojver081a9d02011-10-13 23:58:07 +02001292 "PM: Using %u thread(s) for decompression.\n"
Bojan Smojverd8150d32012-06-21 22:27:24 +02001293 "PM: Loading and decompressing image data (%u pages)...\n",
Bojan Smojver081a9d02011-10-13 23:58:07 +02001294 nr_threads, nr_to_read);
Bojan Smojverd8150d32012-06-21 22:27:24 +02001295 m = nr_to_read / 10;
Bojan Smojverf996fc92010-09-09 23:06:23 +02001296 if (!m)
1297 m = 1;
1298 nr_pages = 0;
Tina Ruchandanidb597602014-10-30 11:04:53 -07001299 start = ktime_get();
Bojan Smojverf996fc92010-09-09 23:06:23 +02001300
Bojan Smojver081a9d02011-10-13 23:58:07 +02001301 ret = snapshot_write_next(snapshot);
1302 if (ret <= 0)
Bojan Smojverf996fc92010-09-09 23:06:23 +02001303 goto out_finish;
1304
Bojan Smojver081a9d02011-10-13 23:58:07 +02001305 for(;;) {
1306 for (i = 0; !eof && i < want; i++) {
Christoph Hellwig343df3c2015-05-19 09:23:23 +02001307 ret = swap_read_page(handle, page[ring], &hb);
Bojan Smojver081a9d02011-10-13 23:58:07 +02001308 if (ret) {
1309 /*
1310 * On real read error, finish. On end of data,
1311 * set EOF flag and just exit the read loop.
1312 */
1313 if (handle->cur &&
1314 handle->cur->entries[handle->k]) {
1315 goto out_finish;
1316 } else {
1317 eof = 1;
1318 break;
1319 }
1320 }
1321 if (++ring >= ring_size)
1322 ring = 0;
Bojan Smojverf996fc92010-09-09 23:06:23 +02001323 }
Bojan Smojver081a9d02011-10-13 23:58:07 +02001324 asked += i;
1325 want -= i;
Bojan Smojverf996fc92010-09-09 23:06:23 +02001326
Bojan Smojver081a9d02011-10-13 23:58:07 +02001327 /*
1328 * We are out of data, wait for some more.
1329 */
1330 if (!have) {
1331 if (!asked)
1332 break;
1333
Christoph Hellwig343df3c2015-05-19 09:23:23 +02001334 ret = hib_wait_io(&hb);
Bojan Smojver081a9d02011-10-13 23:58:07 +02001335 if (ret)
Bojan Smojverf996fc92010-09-09 23:06:23 +02001336 goto out_finish;
Bojan Smojver081a9d02011-10-13 23:58:07 +02001337 have += asked;
1338 asked = 0;
1339 if (eof)
1340 eof = 2;
Bojan Smojver9f339ca2010-11-25 23:41:39 +01001341 }
Bojan Smojverf996fc92010-09-09 23:06:23 +02001342
Bojan Smojver081a9d02011-10-13 23:58:07 +02001343 if (crc->run_threads) {
1344 wait_event(crc->done, atomic_read(&crc->stop));
1345 atomic_set(&crc->stop, 0);
1346 crc->run_threads = 0;
Bojan Smojverf996fc92010-09-09 23:06:23 +02001347 }
1348
Bojan Smojver081a9d02011-10-13 23:58:07 +02001349 for (thr = 0; have && thr < nr_threads; thr++) {
1350 data[thr].cmp_len = *(size_t *)page[pg];
1351 if (unlikely(!data[thr].cmp_len ||
1352 data[thr].cmp_len >
1353 lzo1x_worst_compress(LZO_UNC_SIZE))) {
1354 printk(KERN_ERR
1355 "PM: Invalid LZO compressed length\n");
1356 ret = -1;
Bojan Smojverf996fc92010-09-09 23:06:23 +02001357 goto out_finish;
Bojan Smojver081a9d02011-10-13 23:58:07 +02001358 }
1359
1360 need = DIV_ROUND_UP(data[thr].cmp_len + LZO_HEADER,
1361 PAGE_SIZE);
1362 if (need > have) {
1363 if (eof > 1) {
1364 ret = -1;
1365 goto out_finish;
1366 }
1367 break;
1368 }
1369
1370 for (off = 0;
1371 off < LZO_HEADER + data[thr].cmp_len;
1372 off += PAGE_SIZE) {
1373 memcpy(data[thr].cmp + off,
1374 page[pg], PAGE_SIZE);
1375 have--;
1376 want++;
1377 if (++pg >= ring_size)
1378 pg = 0;
1379 }
1380
1381 atomic_set(&data[thr].ready, 1);
1382 wake_up(&data[thr].go);
Bojan Smojverf996fc92010-09-09 23:06:23 +02001383 }
Bojan Smojver081a9d02011-10-13 23:58:07 +02001384
1385 /*
1386 * Wait for more data while we are decompressing.
1387 */
1388 if (have < LZO_CMP_PAGES && asked) {
Christoph Hellwig343df3c2015-05-19 09:23:23 +02001389 ret = hib_wait_io(&hb);
Bojan Smojver081a9d02011-10-13 23:58:07 +02001390 if (ret)
1391 goto out_finish;
1392 have += asked;
1393 asked = 0;
1394 if (eof)
1395 eof = 2;
1396 }
1397
1398 for (run_threads = thr, thr = 0; thr < run_threads; thr++) {
1399 wait_event(data[thr].done,
1400 atomic_read(&data[thr].stop));
1401 atomic_set(&data[thr].stop, 0);
1402
1403 ret = data[thr].ret;
1404
1405 if (ret < 0) {
1406 printk(KERN_ERR
1407 "PM: LZO decompression failed\n");
1408 goto out_finish;
1409 }
1410
1411 if (unlikely(!data[thr].unc_len ||
1412 data[thr].unc_len > LZO_UNC_SIZE ||
1413 data[thr].unc_len & (PAGE_SIZE - 1))) {
1414 printk(KERN_ERR
1415 "PM: Invalid LZO uncompressed length\n");
1416 ret = -1;
1417 goto out_finish;
1418 }
1419
1420 for (off = 0;
1421 off < data[thr].unc_len; off += PAGE_SIZE) {
1422 memcpy(data_of(*snapshot),
1423 data[thr].unc + off, PAGE_SIZE);
1424
1425 if (!(nr_pages % m))
Bojan Smojverd8150d32012-06-21 22:27:24 +02001426 printk(KERN_INFO
1427 "PM: Image loading progress: "
1428 "%3d%%\n",
1429 nr_pages / m * 10);
Bojan Smojver081a9d02011-10-13 23:58:07 +02001430 nr_pages++;
1431
1432 ret = snapshot_write_next(snapshot);
1433 if (ret <= 0) {
1434 crc->run_threads = thr + 1;
1435 atomic_set(&crc->ready, 1);
1436 wake_up(&crc->go);
1437 goto out_finish;
1438 }
1439 }
1440 }
1441
1442 crc->run_threads = thr;
1443 atomic_set(&crc->ready, 1);
1444 wake_up(&crc->go);
Bojan Smojverf996fc92010-09-09 23:06:23 +02001445 }
1446
1447out_finish:
Bojan Smojver081a9d02011-10-13 23:58:07 +02001448 if (crc->run_threads) {
1449 wait_event(crc->done, atomic_read(&crc->stop));
1450 atomic_set(&crc->stop, 0);
1451 }
Tina Ruchandanidb597602014-10-30 11:04:53 -07001452 stop = ktime_get();
Bojan Smojver081a9d02011-10-13 23:58:07 +02001453 if (!ret) {
Bojan Smojverd8150d32012-06-21 22:27:24 +02001454 printk(KERN_INFO "PM: Image loading done.\n");
Bojan Smojverf996fc92010-09-09 23:06:23 +02001455 snapshot_write_finalize(snapshot);
1456 if (!snapshot_image_loaded(snapshot))
Bojan Smojver081a9d02011-10-13 23:58:07 +02001457 ret = -ENODATA;
1458 if (!ret) {
Patrick Daly050f2572018-05-07 13:07:50 -07001459 if ((swsusp_header->flags & SF_CRC32_MODE) &&
1460 (!IS_ENABLED(CONFIG_HIBERNATION_SKIP_CRC))) {
Bojan Smojver081a9d02011-10-13 23:58:07 +02001461 if(handle->crc32 != swsusp_header->crc32) {
1462 printk(KERN_ERR
1463 "PM: Invalid image CRC32!\n");
1464 ret = -ENODATA;
1465 }
1466 }
1467 }
Bojan Smojverd8150d32012-06-21 22:27:24 +02001468 }
Tina Ruchandanidb597602014-10-30 11:04:53 -07001469 swsusp_show_speed(start, stop, nr_to_read, "Read");
Bojan Smojver081a9d02011-10-13 23:58:07 +02001470out_clean:
1471 for (i = 0; i < ring_size; i++)
Bojan Smojver9f339ca2010-11-25 23:41:39 +01001472 free_page((unsigned long)page[i]);
Bojan Smojver081a9d02011-10-13 23:58:07 +02001473 if (crc) {
1474 if (crc->thr)
1475 kthread_stop(crc->thr);
1476 kfree(crc);
1477 }
1478 if (data) {
1479 for (thr = 0; thr < nr_threads; thr++)
1480 if (data[thr].thr)
1481 kthread_stop(data[thr].thr);
1482 vfree(data);
1483 }
Markus Elfring6c45de02014-11-16 14:18:28 +01001484 vfree(page);
Bojan Smojverf996fc92010-09-09 23:06:23 +02001485
Bojan Smojver081a9d02011-10-13 23:58:07 +02001486 return ret;
Bojan Smojverf996fc92010-09-09 23:06:23 +02001487}
1488
1489/**
Rafael J. Wysockia634cc12007-07-19 01:47:30 -07001490 * swsusp_read - read the hibernation image.
1491 * @flags_p: flags passed by the "frozen" kernel in the image header should
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001492 * be written into this memory location
Rafael J. Wysockia634cc12007-07-19 01:47:30 -07001493 */
1494
1495int swsusp_read(unsigned int *flags_p)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -08001496{
1497 int error;
1498 struct swap_map_handle handle;
1499 struct snapshot_handle snapshot;
1500 struct swsusp_info *header;
1501
Rafael J. Wysocki61159a32006-03-23 03:00:00 -08001502 memset(&snapshot, 0, sizeof(struct snapshot_handle));
Jiri Slabyd3c1b242010-05-01 23:52:02 +02001503 error = snapshot_write_next(&snapshot);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -08001504 if (error < PAGE_SIZE)
1505 return error < 0 ? error : -EFAULT;
1506 header = (struct swsusp_info *)data_of(snapshot);
Jiri Slaby6f612af2010-05-01 23:54:02 +02001507 error = get_swap_reader(&handle, flags_p);
1508 if (error)
1509 goto end;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -08001510 if (!error)
Andrew Morton546e0d22006-09-25 23:32:44 -07001511 error = swap_read_page(&handle, header, NULL);
Bojan Smojverf996fc92010-09-09 23:06:23 +02001512 if (!error) {
1513 error = (*flags_p & SF_NOCOMPRESS_MODE) ?
1514 load_image(&handle, &snapshot, header->pages - 1) :
1515 load_image_lzo(&handle, &snapshot, header->pages - 1);
1516 }
Jiri Slaby6f612af2010-05-01 23:54:02 +02001517 swap_reader_finish(&handle);
1518end:
Rafael J. Wysocki61159a32006-03-23 03:00:00 -08001519 if (!error)
Rafael J. Wysocki23976722007-12-08 02:09:43 +01001520 pr_debug("PM: Image successfully loaded\n");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -08001521 else
Rafael J. Wysocki23976722007-12-08 02:09:43 +01001522 pr_debug("PM: Error %d resuming\n", error);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -08001523 return error;
1524}
1525
1526/**
1527 * swsusp_check - Check for swsusp signature in the resume device
1528 */
1529
1530int swsusp_check(void)
1531{
1532 int error;
1533
Tejun Heod4d77622010-11-13 11:55:18 +01001534 hib_resume_bdev = blkdev_get_by_dev(swsusp_resume_device,
1535 FMODE_READ, NULL);
Jiri Slaby8a0d6132010-05-01 23:52:34 +02001536 if (!IS_ERR(hib_resume_bdev)) {
1537 set_blocksize(hib_resume_bdev, PAGE_SIZE);
Jan Beulich3ecb01d2010-10-26 14:22:27 -07001538 clear_page(swsusp_header);
Mike Christie162b99e2016-06-05 14:32:02 -05001539 error = hib_submit_io(REQ_OP_READ, READ_SYNC,
1540 swsusp_resume_block,
Vivek Goyal1b29c162007-05-02 19:27:07 +02001541 swsusp_header, NULL);
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -08001542 if (error)
Jiri Slaby76b57e62009-10-07 22:37:35 +02001543 goto put;
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -08001544
Rafael J. Wysocki3624eb02010-10-04 22:08:12 +02001545 if (!memcmp(HIBERNATE_SIG, swsusp_header->sig, 10)) {
Vivek Goyal1b29c162007-05-02 19:27:07 +02001546 memcpy(swsusp_header->sig, swsusp_header->orig_sig, 10);
Patrick Daly018bf892018-03-27 15:27:27 -07001547#ifndef CONFIG_HIBERNATION_IMAGE_REUSE
Rafael J. Wysocki61159a32006-03-23 03:00:00 -08001548 /* Reset swap signature now */
Mike Christie162b99e2016-06-05 14:32:02 -05001549 error = hib_submit_io(REQ_OP_WRITE, WRITE_SYNC,
1550 swsusp_resume_block,
Vivek Goyal1b29c162007-05-02 19:27:07 +02001551 swsusp_header, NULL);
Patrick Daly018bf892018-03-27 15:27:27 -07001552#endif
Rafael J. Wysocki61159a32006-03-23 03:00:00 -08001553 } else {
Jiri Slaby76b57e62009-10-07 22:37:35 +02001554 error = -EINVAL;
Rafael J. Wysocki61159a32006-03-23 03:00:00 -08001555 }
Jiri Slaby76b57e62009-10-07 22:37:35 +02001556
1557put:
Rafael J. Wysocki61159a32006-03-23 03:00:00 -08001558 if (error)
Jiri Slaby8a0d6132010-05-01 23:52:34 +02001559 blkdev_put(hib_resume_bdev, FMODE_READ);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -08001560 else
Rafael J. Wysockid0941ea2010-09-28 23:31:22 +02001561 pr_debug("PM: Image signature found, resuming\n");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -08001562 } else {
Jiri Slaby8a0d6132010-05-01 23:52:34 +02001563 error = PTR_ERR(hib_resume_bdev);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -08001564 }
1565
1566 if (error)
Rafael J. Wysockid0941ea2010-09-28 23:31:22 +02001567 pr_debug("PM: Image not found (code %d)\n", error);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -08001568
1569 return error;
1570}
1571
1572/**
1573 * swsusp_close - close swap device.
1574 */
1575
Al Viroc2dd0da2007-10-08 13:21:10 -04001576void swsusp_close(fmode_t mode)
Rafael J. Wysocki61159a32006-03-23 03:00:00 -08001577{
Jiri Slaby8a0d6132010-05-01 23:52:34 +02001578 if (IS_ERR(hib_resume_bdev)) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +01001579 pr_debug("PM: Image device not initialised\n");
Rafael J. Wysocki61159a32006-03-23 03:00:00 -08001580 return;
1581 }
1582
Jiri Slaby8a0d6132010-05-01 23:52:34 +02001583 blkdev_put(hib_resume_bdev, mode);
Rafael J. Wysocki61159a32006-03-23 03:00:00 -08001584}
Vivek Goyal1b29c162007-05-02 19:27:07 +02001585
Bojan Smojver62c552c2012-06-16 00:09:58 +02001586/**
1587 * swsusp_unmark - Unmark swsusp signature in the resume device
1588 */
1589
1590#ifdef CONFIG_SUSPEND
1591int swsusp_unmark(void)
1592{
1593 int error;
1594
Mike Christie162b99e2016-06-05 14:32:02 -05001595 hib_submit_io(REQ_OP_READ, READ_SYNC, swsusp_resume_block,
1596 swsusp_header, NULL);
Bojan Smojver62c552c2012-06-16 00:09:58 +02001597 if (!memcmp(HIBERNATE_SIG,swsusp_header->sig, 10)) {
1598 memcpy(swsusp_header->sig,swsusp_header->orig_sig, 10);
Mike Christie162b99e2016-06-05 14:32:02 -05001599 error = hib_submit_io(REQ_OP_WRITE, WRITE_SYNC,
1600 swsusp_resume_block,
Bojan Smojver62c552c2012-06-16 00:09:58 +02001601 swsusp_header, NULL);
1602 } else {
1603 printk(KERN_ERR "PM: Cannot find swsusp signature!\n");
1604 error = -ENODEV;
1605 }
1606
1607 /*
1608 * We just returned from suspend, we don't need the image any more.
1609 */
1610 free_all_swap_pages(root_swap);
1611
1612 return error;
1613}
1614#endif
1615
Vivek Goyal1b29c162007-05-02 19:27:07 +02001616static int swsusp_header_init(void)
1617{
1618 swsusp_header = (struct swsusp_header*) __get_free_page(GFP_KERNEL);
1619 if (!swsusp_header)
1620 panic("Could not allocate memory for swsusp_header\n");
1621 return 0;
1622}
1623
1624core_initcall(swsusp_header_init);