hc
2024-05-10 61598093bbdd283a7edc367d900f223070ead8d2
kernel/kernel/power/swap.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * linux/kernel/power/swap.c
34 *
....@@ -7,9 +8,6 @@
78 * Copyright (C) 1998,2001-2005 Pavel Machek <pavel@ucw.cz>
89 * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl>
910 * Copyright (C) 2010-2012 Bojan Smojver <bojan@rexursive.com>
10
- *
11
- * This file is released under the GPLv2.
12
- *
1311 */
1412
1513 #define pr_fmt(fmt) "PM: " fmt
....@@ -228,6 +226,7 @@
228226 atomic_t count;
229227 wait_queue_head_t wait;
230228 blk_status_t error;
229
+ struct blk_plug plug;
231230 };
232231
233232 static void hib_init_batch(struct hib_bio_batch *hb)
....@@ -235,6 +234,12 @@
235234 atomic_set(&hb->count, 0);
236235 init_waitqueue_head(&hb->wait);
237236 hb->error = BLK_STS_OK;
237
+ blk_start_plug(&hb->plug);
238
+}
239
+
240
+static void hib_finish_batch(struct hib_bio_batch *hb)
241
+{
242
+ blk_finish_plug(&hb->plug);
238243 }
239244
240245 static void hib_end_io(struct bio *bio)
....@@ -296,6 +301,10 @@
296301
297302 static int hib_wait_io(struct hib_bio_batch *hb)
298303 {
304
+ /*
305
+ * We are relying on the behavior of blk_plug that a thread with
306
+ * a plug will flush the plug list before sleeping.
307
+ */
299308 wait_event(hb->wait, atomic_read(&hb->count) == 0);
300309 return blk_status_to_errno(hb->error);
301310 }
....@@ -337,26 +346,23 @@
337346 {
338347 int res;
339348
340
- res = swap_type_of(swsusp_resume_device, swsusp_resume_block,
341
- &hib_resume_bdev);
349
+ if (swsusp_resume_device)
350
+ res = swap_type_of(swsusp_resume_device, swsusp_resume_block);
351
+ else
352
+ res = find_first_swap(&swsusp_resume_device);
342353 if (res < 0)
343354 return res;
344
-
345355 root_swap = res;
346
- res = blkdev_get(hib_resume_bdev, FMODE_WRITE, NULL);
347
- if (res)
348
- return res;
356
+
357
+ hib_resume_bdev = blkdev_get_by_dev(swsusp_resume_device, FMODE_WRITE,
358
+ NULL);
359
+ if (IS_ERR(hib_resume_bdev))
360
+ return PTR_ERR(hib_resume_bdev);
349361
350362 res = set_blocksize(hib_resume_bdev, PAGE_SIZE);
351363 if (res < 0)
352364 blkdev_put(hib_resume_bdev, FMODE_WRITE);
353365
354
- /*
355
- * Update the resume device to the one actually used,
356
- * so the test_resume mode can use it in case it is
357
- * invoked from hibernate() to test the snapshot.
358
- */
359
- swsusp_resume_device = hib_resume_bdev->bd_dev;
360366 return res;
361367 }
362368
....@@ -563,6 +569,7 @@
563569 nr_pages++;
564570 }
565571 err2 = hib_wait_io(&hb);
572
+ hib_finish_batch(&hb);
566573 stop = ktime_get();
567574 if (!ret)
568575 ret = err2;
....@@ -856,6 +863,7 @@
856863 pr_info("Image saving done\n");
857864 swsusp_show_speed(start, stop, nr_to_write, "Wrote");
858865 out_clean:
866
+ hib_finish_batch(&hb);
859867 if (crc) {
860868 if (crc->thr)
861869 kthread_stop(crc->thr);
....@@ -976,12 +984,11 @@
976984 last = handle->maps = NULL;
977985 offset = swsusp_header->image;
978986 while (offset) {
979
- tmp = kmalloc(sizeof(*handle->maps), GFP_KERNEL);
987
+ tmp = kzalloc(sizeof(*handle->maps), GFP_KERNEL);
980988 if (!tmp) {
981989 release_swap_reader(handle);
982990 return -ENOMEM;
983991 }
984
- memset(tmp, 0, sizeof(*tmp));
985992 if (!handle->maps)
986993 handle->maps = tmp;
987994 if (last)
....@@ -1087,6 +1094,7 @@
10871094 nr_pages++;
10881095 }
10891096 err2 = hib_wait_io(&hb);
1097
+ hib_finish_batch(&hb);
10901098 stop = ktime_get();
10911099 if (!ret)
10921100 ret = err2;
....@@ -1450,6 +1458,7 @@
14501458 }
14511459 swsusp_show_speed(start, stop, nr_to_read, "Read");
14521460 out_clean:
1461
+ hib_finish_batch(&hb);
14531462 for (i = 0; i < ring_size; i++)
14541463 free_page((unsigned long)page[i]);
14551464 if (crc) {
....@@ -1594,7 +1603,7 @@
15941603 }
15951604 #endif
15961605
1597
-static int swsusp_header_init(void)
1606
+static int __init swsusp_header_init(void)
15981607 {
15991608 swsusp_header = (struct swsusp_header*) __get_free_page(GFP_KERNEL);
16001609 if (!swsusp_header)