-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsuspend.c
2586 lines (2286 loc) · 61.2 KB
/
suspend.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* suspend.c
*
* A simple user space suspend handler for swsusp.
*
* Copyright (C) 2005 Rafael J. Wysocki <[email protected]>
*
* This file is released under the GPLv2.
*
*/
#include "config.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/mount.h>
#include <sys/vt.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <time.h>
#include <linux/kd.h>
#include <linux/tiocl.h>
#include <syscall.h>
#include <libgen.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <termios.h>
#ifdef CONFIG_THREADS
#include <pthread.h>
#endif
#ifdef CONFIG_COMPRESS
#include <lzo/lzo1x.h>
#endif
#include "swsusp.h"
#include "memalloc.h"
#include "config_parser.h"
#include "md5.h"
#include "splash.h"
#include "vt.h"
#include "loglevel.h"
#ifdef CONFIG_BOTH
#include "s2ram.h"
#endif
static char test_file_name[MAX_STR_LEN] = "";
static loff_t test_image_size;
#define suspend_error(msg, args...) \
do { \
fprintf(stderr, "%s: " msg " Reason: %m\n", my_name, ## args); \
} while (0)
#ifdef CONFIG_ARCH_S390
#define suspend_warning(msg)
#else
#define suspend_warning(msg) \
do { \
fprintf(stderr, "%s: " msg "\n", my_name); \
} while (0)
#endif
static char snapshot_dev_name[MAX_STR_LEN] = SNAPSHOT_DEVICE;
static char resume_dev_name[MAX_STR_LEN] = RESUME_DEVICE;
static loff_t resume_offset;
static loff_t pref_image_size = IMAGE_SIZE;
static int suspend_loglevel = SUSPEND_LOGLEVEL;
static char compute_checksum;
#ifdef CONFIG_COMPRESS
static char do_compress;
#else
#define do_compress 0
#endif
#ifdef CONFIG_ENCRYPT
static char do_encrypt;
static char use_RSA;
static char key_name[MAX_STR_LEN] = SUSPEND_KEY_FILE_PATH;
static char password[PASSBUF_SIZE];
static unsigned long encrypt_buf_size;
#else
#define do_encrypt 0
#define key_name NULL
#define encrypt_buf_size 0
#endif
#ifdef CONFIG_BOTH
static char s2ram;
static char s2ram_kms;
#endif
static char early_writeout;
static char splash_param;
#ifdef CONFIG_FBSPLASH
char fbsplash_theme[MAX_STR_LEN] = "";
#endif
#define SHUTDOWN_LEN 16
static char shutdown_method_value[SHUTDOWN_LEN] = "";
static enum {
SHUTDOWN_METHOD_SHUTDOWN,
SHUTDOWN_METHOD_PLATFORM,
SHUTDOWN_METHOD_REBOOT
} shutdown_method = SHUTDOWN_METHOD_PLATFORM;
static int resume_pause;
static char verify_image;
#ifdef CONFIG_THREADS
static char use_threads;
#else
#define use_threads 0
#endif
static int suspend_swappiness = SUSPEND_SWAPPINESS;
static struct vt_mode orig_vtm;
static int vfd;
static struct config_par parameters[] = {
{
.name = "snapshot device",
.fmt = "%s",
.ptr = snapshot_dev_name,
.len = MAX_STR_LEN
},
{
.name = "resume device",
.fmt ="%s",
.ptr = resume_dev_name,
.len = MAX_STR_LEN
},
{
.name = "resume offset",
.fmt = "%llu",
.ptr = &resume_offset,
},
{
.name = "image size",
.fmt = "%lu",
.ptr = &pref_image_size,
},
{
.name = "suspend loglevel",
.fmt = "%d",
.ptr = &suspend_loglevel,
},
{
.name = "max loglevel",
.fmt = "%d",
.ptr = NULL,
},
{
.name = "compute checksum",
.fmt = "%c",
.ptr = &compute_checksum,
},
#ifdef CONFIG_COMPRESS
{
.name = "compress",
.fmt = "%c",
.ptr = &do_compress,
},
#endif
#ifdef CONFIG_ENCRYPT
{
.name = "encrypt",
.fmt = "%c",
.ptr = &do_encrypt,
},
{
.name = "RSA key file",
.fmt = "%s",
.ptr = key_name,
.len = MAX_STR_LEN
},
#endif
{
.name = "early writeout",
.fmt = "%c",
.ptr = &early_writeout,
},
{
.name = "splash",
.fmt = "%c",
.ptr = &splash_param,
},
{
.name = "shutdown method",
.fmt = "%s",
.ptr = shutdown_method_value,
.len = SHUTDOWN_LEN,
},
#ifdef CONFIG_FBSPLASH
{
.name = "fbsplash theme",
.fmt = "%s",
.ptr = fbsplash_theme,
.len = MAX_STR_LEN,
},
#endif
{
.name = "resume pause",
.fmt = "%d",
.ptr = &resume_pause,
},
{
.name = "debug test file",
.fmt = "%s",
.ptr = test_file_name,
.len = MAX_STR_LEN
},
{
.name = "debug verify image",
.fmt = "%c",
.ptr = &verify_image,
},
#ifdef CONFIG_THREADS
{
.name = "threads",
.fmt = "%c",
.ptr = &use_threads,
},
#endif
{
.name = NULL,
.fmt = NULL,
.ptr = NULL,
.len = 0,
}
};
static loff_t check_free_swap(int dev)
{
int error;
loff_t free_swap;
error = ioctl(dev, SNAPSHOT_AVAIL_SWAP_SIZE, &free_swap);
if (error && errno == ENOTTY)
error = ioctl(dev, SNAPSHOT_AVAIL_SWAP, &free_swap);
if (!error)
return free_swap;
suspend_error("check_free_swap failed.");
return 0;
}
static loff_t get_image_size(int dev)
{
int error;
loff_t image_size;
error = ioctl(dev, SNAPSHOT_GET_IMAGE_SIZE, &image_size);
if (!error)
return image_size;
suspend_error("get_image_size failed.");
return 0;
}
static inline loff_t get_swap_page(int dev)
{
int error;
loff_t offset;
error = ioctl(dev, SNAPSHOT_ALLOC_SWAP_PAGE, &offset);
if (error && errno == ENOTTY)
error = ioctl(dev, SNAPSHOT_GET_SWAP_PAGE, &offset);
if (!error)
return offset;
return 0;
}
static inline int free_swap_pages(int dev)
{
return ioctl(dev, SNAPSHOT_FREE_SWAP_PAGES, 0);
}
static int set_swap_file(int dev, u_int32_t blkdev, loff_t offset)
{
struct resume_swap_area swap;
int error;
swap.dev = blkdev;
swap.offset = offset;
error = ioctl(dev, SNAPSHOT_SET_SWAP_AREA, &swap);
if (error && !offset)
error = ioctl(dev, SNAPSHOT_SET_SWAP_FILE, blkdev);
return error;
}
static int atomic_snapshot(int dev, int *in_suspend)
{
int error;
error = ioctl(dev, SNAPSHOT_CREATE_IMAGE, in_suspend);
if (error && errno == ENOTTY)
error = ioctl(dev, SNAPSHOT_ATOMIC_SNAPSHOT, in_suspend);
return error;
}
static inline int free_snapshot(int dev)
{
return ioctl(dev, SNAPSHOT_FREE, 0);
}
static int set_image_size(int dev, loff_t size)
{
int error;
error = ioctl(dev, SNAPSHOT_PREF_IMAGE_SIZE, size);
if (error && errno == ENOTTY)
error = ioctl(dev, SNAPSHOT_SET_IMAGE_SIZE, size);
return error;
}
static inline int suspend_to_ram(int dev)
{
return ioctl(dev, SNAPSHOT_S2RAM, 0);
}
static int platform_enter(int dev)
{
int error;
error = ioctl(dev, SNAPSHOT_POWER_OFF, 0);
if (error && errno == ENOTTY)
error = ioctl(dev, SNAPSHOT_PMOPS, PMOPS_ENTER);
return error;
}
/**
* alloc_swap - allocate a number of swap pages
* @dev: Swap device to use for allocations.
* @extents: Array of extents to track the allocations.
* @nr_extents: Number of extents already in the array.
* @size_p: Points to the number of bytes to allocate, used to
* return the number of allocated bytes.
*
* Allocate the number of swap pages sufficient for saving the number of
* bytes pointed to by @size_p. Use the array @extents to track the
* allocations. This array has to be page_size big and may already
* contain some initial elements (in that case @nr_extents must be the
* number of these elements).
* Each element of the array represents an area of allocated swap space.
* These areas may be extended when swap pages that can be added to them
* are found. They also can be merged with one another.
* The function returns when the requested amount of swap space is
* allocated or if there is no room for more extents. In the latter case
* the last extent created is put at the end of the array and may be passed
* to alloc_swap() as the initial extent when it is invoked next time.
*/
static int
alloc_swap(int dev, struct extent *extents, int nr_extents, loff_t *size_p)
{
const int max_extents = page_size / sizeof(struct extent) - 1;
loff_t size, total_size, offset;
total_size = *size_p;
if (nr_extents <= 0) {
offset = get_swap_page(dev);
if (!offset)
return -ENOSPC;
extents->start = offset;
extents->end = offset + page_size;
nr_extents = 1;
size = page_size;
} else {
size = 0;
}
while (size < total_size && nr_extents <= max_extents) {
int i, j;
offset = get_swap_page(dev);
if (!offset)
return -ENOSPC;
/* Check if we have a matching extent. */
i = 0;
j = nr_extents - 1;
do {
struct extent *ext;
int k = (i + j) / 2;
Repeat:
ext = extents + k;
if (offset == ext->start - page_size) {
ext->start = offset;
/* Check if we can merge extents */
if (k > 0 && extents[k-1].end == offset) {
extents[k-1].end = ext->end;
/* Pull the 'later' extents forward */
memmove(ext, ext + 1,
(nr_extents - k - 1) *
sizeof(*ext));
nr_extents--;
}
offset = 0;
break;
} else if (offset == ext->end) {
ext->end += page_size;
/* Check if we can merge extents */
if (k + 1 < nr_extents
&& ext->end == extents[k+1].start) {
ext->end = extents[k+1].end;
/* Pull the 'later' extents forward */
memmove(ext + 1, ext + 2,
(nr_extents - k - 2) *
sizeof(*ext));
nr_extents--;
}
offset = 0;
break;
} else if (offset > ext->end) {
if (i == k) {
if (i < j) {
/* This means i == j + 1 */
k = j;
i = j;
goto Repeat;
}
} else {
i = k;
}
} else {
/* offset < ext->start - page_size */
j = k;
}
} while (i < j);
if (offset > 0) {
/* No match. Create a new extent. */
struct extent *ext;
if (nr_extents < max_extents) {
ext = extents + i;
/*
* We want to always replace the extent 'i' with
* the new one.
*/
if (offset > ext->end) {
i++;
ext++;
}
/* Push the 'later' extents backwards. */
memmove(ext + 1, ext,
(nr_extents - i) * sizeof(*ext));
} else {
ext = extents + nr_extents;
}
ext->start = offset;
ext->end = offset + page_size;
nr_extents++;
}
size += page_size;
}
*size_p = size;
return nr_extents;
}
/**
* write_page - Write page_size data to given swap location.
* @fd: File handle of the resume partition.
* @buf: Pointer to the area we're writing.
* @offset: Offset of the swap page we're writing to.
*/
static int write_page(int fd, void *buf, loff_t offset)
{
int res = 0;
ssize_t cnt = 0;
if (!offset)
return -EINVAL;
if (lseek64(fd, offset, SEEK_SET) == offset)
cnt = write(fd, buf, page_size);
if (cnt != page_size)
res = -EIO;
return res;
}
/*
* The swap_writer structure is used for handling swap in a file-alike way.
*
* @extents: Array of extents used for trackig swap allocations. It is
* page_size bytes large and holds at most
* (page_size / sizeof(struct extent) - 1) extents. The last slot
* is used to hold the extent that will be used as an initial one
* for the next batch of allocations.
*
* @nr_extents: Number of entries in @extents actually used.
*
* @cur_extent: The extent currently used as the source of swap pages.
*
* @cur_extent_idx: The index of @cur_extent.
*
* @cur_offset: The offset of the swap page that will be used next.
*
* @swap_needed: The amount of swap needed for saving the image.
*
* @written_data: The amount of data actually saved.
*
* @extents_spc: The swap page to which to save @extents.
*
* @buffer: Buffer used for storing image data pages.
*
* @write_buffer: If compression is used, the compressed contents of
* @buffer are stored here. Otherwise, it is equal to
* @buffer.
*
* @page_ptr: Address to write the next image page to.
*
* @dev: Snapshot device handle used for reading image pages and
* invoking ioctls.
*
* @fd: File handle associated with the swap.
*
* @ctx: Used for checksum computing, if so configured.
*
* @lzo_work_buffer: Work buffer used for compression.
*
* @encrypt_buffer: Buffer for storing encrypted data (page_size bytes).
*
* @encrypt_ptr: Address to store the next encrypted page at.
*/
struct swap_writer {
struct extent *extents;
int nr_extents;
struct extent *cur_extent;
int cur_extent_idx;
loff_t cur_offset;
loff_t swap_needed;
loff_t written_data;
loff_t extents_spc;
void *buffer;
void *write_buffer;
void *page_ptr;
int dev, fd, input;
struct md5_ctx ctx;
void *lzo_work_buffer;
void *encrypt_buffer;
void *encrypt_ptr;
};
/**
* free_swap_writer - free memory allocated for saving the image
* @handle: Structure containing pointers to memory buffers to free.
*/
static void free_swap_writer(struct swap_writer *handle)
{
if (handle->write_buffer != handle->buffer)
freemem(handle->write_buffer);
if (do_compress)
freemem(handle->lzo_work_buffer);
if (handle->encrypt_buffer)
freemem(handle->encrypt_buffer);
freemem(handle->buffer);
freemem(handle->extents);
}
/**
* init_swap_writer - initialize the structure used for saving the image
* @handle: Structure to initialize.
* @dev: Special device file to read image pages from.
* @fd: File descriptor associated with the swap.
*
* It doesn't preallocate swap, so preallocate_swap() has to be called on
* @handle after this.
*/
static int init_swap_writer(struct swap_writer *handle, int dev, int fd, int in)
{
loff_t offset;
unsigned int write_buf_size = 0;
handle->extents = getmem(page_size);
handle->buffer = getmem(buffer_size);
handle->page_ptr = handle->buffer;
if (do_encrypt) {
handle->encrypt_buffer = getmem(encrypt_buf_size);
handle->encrypt_ptr = handle->encrypt_buffer;
} else {
handle->encrypt_buffer = NULL;
}
if (do_compress) {
handle->lzo_work_buffer = getmem(LZO1X_1_MEM_COMPRESS);
write_buf_size = compress_buf_size;
if (use_threads)
write_buf_size +=
(WRITE_BUFFERS - 1) * compress_buf_size;
}
if (write_buf_size > 0)
handle->write_buffer = getmem(write_buf_size);
else if (use_threads)
handle->write_buffer = getmem(buffer_size * WRITE_BUFFERS);
else
handle->write_buffer = handle->buffer;
handle->dev = dev;
handle->fd = fd;
handle->input = (in >= 0) ? in : dev;
handle->written_data = 0;
memset(handle->extents, 0, page_size);
handle->nr_extents = 0;
offset = get_swap_page(dev);
if (!offset) {
free_swap_writer(handle);
return -ENOSPC;
}
handle->extents_spc = offset;
if (compute_checksum || verify_image)
md5_init_ctx(&handle->ctx);
return 0;
}
/**
* preallocate_swap - use alloc_swap() to preallocate the number of pages
* given by @handle->swap_needed
* @handle: Pointer to the structure in which to store information
* about the preallocated swap pool.
*
* Returns the offset of the first swap page available from the
* preallocated pool.
*/
static loff_t preallocate_swap(struct swap_writer *handle)
{
const int max = page_size / sizeof(struct extent) - 1;
loff_t size;
int nr_extents;
if (handle->swap_needed < page_size)
return 0;
size = handle->swap_needed;
if (do_compress && size > page_size)
size /= 2;
nr_extents = alloc_swap(handle->dev, handle->extents,
handle->nr_extents, &size);
if (nr_extents <= 0)
return 0;
handle->nr_extents = nr_extents < max ? nr_extents : max;
handle->cur_extent = handle->extents;
handle->cur_extent_idx = 0;
handle->cur_offset = handle->cur_extent->start;
return handle->cur_offset;
}
/**
* save_extents - save the array of extents
* handle: Structure holding the pointer to the array of extents etc.
* finish: If set, the last element of the extents array has to be filled
* with zeros.
*
* Save the buffer (page) holding the array of extents to the swap
* location pointed to by @handle->extents_spc (this must be allocated
* earlier). Before saving the last element of the array is used to store
* the swap offset of the next extents page (we allocate a swap page for
* this purpose).
*/
static int save_extents(struct swap_writer *handle, int finish)
{
loff_t offset = 0;
int error;
if (!finish) {
struct extent *last_extent;
offset = get_swap_page(handle->dev);
if (!offset)
return -ENOSPC;
last_extent = handle->extents +
page_size / sizeof(struct extent) - 1;
last_extent->start = offset;
}
error = write_page(handle->fd, handle->extents, handle->extents_spc);
handle->extents_spc = offset;
return error;
}
/**
* next_swap_page - take one swap page out of the pool allocated using
* alloc_swap() before
* @handle: Pointer to the structure containing information about
* the preallocated swap pool.
*/
static loff_t next_swap_page(struct swap_writer *handle)
{
struct extent ext;
handle->cur_offset += page_size;
if (handle->cur_offset < handle->cur_extent->end)
return handle->cur_offset;
/* We have exhausted the current extent. Forward to the next one */
handle->cur_extent++;
handle->cur_extent_idx++;
if (handle->cur_extent_idx < handle->nr_extents) {
handle->cur_offset = handle->cur_extent->start;
return handle->cur_offset;
}
/* No more extents. Is there anything to pass to alloc_swap()? */
if (handle->cur_extent->start < handle->cur_extent->end) {
ext = *handle->cur_extent;
memset(handle->cur_extent, 0, sizeof(struct extent));
handle->nr_extents = 1;
} else {
memset(&ext, 0, sizeof(struct extent));
handle->nr_extents = 0;
}
if (save_extents(handle, 0))
return 0;
memset(handle->extents, 0, page_size);
*handle->extents = ext;
return preallocate_swap(handle);
}
/**
* save_page - save one page of data to the swap
* @handle: Pointer to the structure containing information about
* the swap.
* @src: Pointer to the data.
*/
static int save_page(struct swap_writer *handle, void *src)
{
loff_t offset;
int error;
offset = next_swap_page(handle);
if (!offset)
return -ENOSPC;
error = write_page(handle->fd, src, offset);
if (error)
return error;
handle->swap_needed -= page_size;
handle->written_data += page_size;
return 0;
}
#ifdef CONFIG_THREADS
/*
* If threads are used for saving the image with compression and encryption,
* there are three of them.
*
* The main one reads image pages from the kernel and puts them into a work
* buffer. When the work buffer is full, it gets compressed, but that's not an
* in-place compression, so the result has to be stored somewhere else. There
* are four so-called "write" buffers for that and the first empty "write"
* buffer is used as the target. If all of the "write" buffers are full, the
* thread has to wait (see the rules below). Otherwise, after placing the
* (compressed) contents of the work buffer into a "write" buffer, the main
* thread regards the work buffer as empty and starts to read more image pages
* from the kernel.
*
* The second thread (call it the "move" thread) encrypts the contents of the
* "write" buffers, one buffer at a time. It really encrypts individual pages
* and the encryption is not in-place, too. The encrypted pages of data are
* placed in yet another buffer (call it the "encrypt" buffer) until it's full,
* in which case the "move" thread has to wait. Of course, it also has to wait
* for data from the main thread if all of the "write" buffers are empty.
* After encrypting an entire "write" buffer, the "move" thread progresses to
* the next "write" buffer, in a round-robin manner.
*
* The synchronization between the main thread and the "move" thread is done
* with the help of two index variables, move_start and move_end. �The rule
* is that:
* (1) the main thread can only put data into write_buffers[move_start],
* (2) after putting data into write_buffers[move_start], the main thread
* increases move_start, modulo the number of "write" buffers, but
* move_start cannot be modified as long as the _next_ "write" buffer is
* write_buffers[move_end] (the thread has to wait if that happens),
* (3) the "move" thread can only read data from write_buffers[move_end] and
* only if move_end != move_start (it has to wait if that's not the case),
* (4) after reading data from write_buffers[move_end], the "move" thread
* increases move_end, modulo the number of "write" buffers.
* This way, move_end always "follows" move_start and the threads don't access
* the same buffer at any time.
*
* The third thread (call it the "save" thread) reads (encrypted) pages of data
* from the "encrypt" buffer and writes them out to the swap. This is done if
* there are some pages to write in the "encrypt" buffer, otherwise the "save"
* thread has to wait for the "move" thread to put more pages in there.
*
* The synchronization between the "move" thread and the "save" thread is done
* with the help of two pointers, save_start and save_end, where save_start
* points to the first empty page and save_end points to the last data page
* that hasn't been written out yet. Thus, the rule is:
* (1) the "move" thread can only put data into the page pointed to by
* save_start,
* (2) after putting data into the page pointed to by save_start, the "move"
* thread increases save_start, modulo the number of pages in the buffer,
* provided that the _next_ page is not the one pointed to by save_end (it
* has to wait if that happens),
* (3) the "save" thread can only read from the page pointed to by save_end,
* as long as save_end != save_start (it has to wait if the two pointers
* are equal),
* (4) after writing data from the page pointed to by save_end, the "save"
* thread increases save_end, modulo the number of pages in the buffer.
* IOW, the "encrypt" buffer is handled as a typical circular buffer with one
* producer (the "move" thread) and one consumer (the "save" thread).
*
* If encryption is not used, the "save" thread is not started and the "move"
* thread writes data to the swap directly out of the "write" buffers.
*/
static int save_ret;
static pthread_mutex_t finish_mutex;
static pthread_cond_t finish_cond;
static char *encrypt_buf;
static char *save_start, *save_end;
static pthread_mutex_t save_mutex;
static pthread_cond_t save_cond;
static pthread_t save_th;
struct write_buffer {
ssize_t size;
void *start;
};
static struct write_buffer write_buffers[WRITE_BUFFERS];
static int move_start, move_end;
static pthread_mutex_t move_mutex;
static pthread_cond_t move_cond;
static pthread_t move_th;
#define FORCE_EXIT 1
static char *save_inc(char *ptr)
{
return encrypt_buf +
(((ptr - encrypt_buf) + page_size) % encrypt_buf_size);
}
static int move_inc(int index)
{
return (index + 1) % WRITE_BUFFERS;
}
static int wait_for_finish(void)
{
pthread_mutex_lock(&finish_mutex);
while((save_end != save_start || move_start != move_end) && !save_ret)
pthread_cond_wait(&finish_cond, &finish_mutex);
pthread_mutex_unlock(&finish_mutex);
return save_ret;
}
static void *save_thread(void *arg)
{
struct swap_writer *handle = arg;
int error = 0;
for (;;) {
/* Wait until there is a buffer ready for processing. */
pthread_mutex_lock(&save_mutex);
while(save_end == save_start && !save_ret)
pthread_cond_wait(&save_cond, &save_mutex);
pthread_mutex_unlock(&save_mutex);
if (save_ret)
return NULL;
error = save_page(handle, save_end);
if (error) {
pthread_mutex_lock(&finish_mutex);
if (!save_ret)
save_ret = error;
pthread_mutex_unlock(&finish_mutex);
pthread_cond_signal(&move_cond);
pthread_cond_signal(&save_cond);
pthread_cond_signal(&finish_cond);
return NULL;
}
/* Go to the next page */
pthread_mutex_lock(&finish_mutex);
pthread_mutex_lock(&save_mutex);
save_end = save_inc(save_end);
pthread_mutex_unlock(&save_mutex);
pthread_mutex_unlock(&finish_mutex);
pthread_cond_signal(&save_cond);
pthread_cond_signal(&finish_cond);
}
return NULL;
}
#ifdef CONFIG_ENCRYPT
static void encrypt_and_save_buffer(void)
{
char *src;
ssize_t buf_size, moved_size;
/*
* The buffer to process is at write_buffers[move_end].start and the
* size of it is write_buffers[move_end].size .
*/
src = write_buffers[move_end].start;
buf_size = write_buffers[move_end].size;
moved_size = 0;
do {
int error;
void *next_start;
/* Encrypt page_size of data. */
error = gcry_cipher_encrypt(cipher_handle,
save_start, page_size,
src, page_size);
if (error) {
pthread_mutex_lock(&finish_mutex);
if (!save_ret)
save_ret = error;
pthread_mutex_unlock(&finish_mutex);
pthread_cond_signal(&move_cond);
pthread_cond_signal(&save_cond);
pthread_cond_signal(&finish_cond);
break;
}
moved_size += page_size;
src += page_size;
pthread_mutex_lock(&save_mutex);
next_start = save_inc(save_start);
while (next_start == save_end && !save_ret)
pthread_cond_wait(&save_cond, &save_mutex);
save_start = next_start;
pthread_mutex_unlock(&save_mutex);
pthread_cond_signal(&save_cond);
} while (moved_size < buf_size && !save_ret);
}
#else /* !CONFIG_ENCRYPT */
static inline void encrypt_and_save_buffer(void) {}
#endif /* !CONFIG_ENCRYPT */
static void save_buffer(struct swap_writer *handle)
{
void *src;
ssize_t size;
/*
* The buffer to process is at write_buffers[move_end].start and the
* size of it is write_buffers[move_end].size .
*/
src = write_buffers[move_end].start;
size = write_buffers[move_end].size;
while (size > 0) {
int error = save_page(handle, src);
if (error) {
pthread_mutex_lock(&finish_mutex);
if (!save_ret)
save_ret = error;
pthread_mutex_unlock(&finish_mutex);
pthread_cond_signal(&move_cond);
pthread_cond_signal(&finish_cond);
break;
}
src += page_size;
size -= page_size;
}
}
static void *move_thread(void *arg)
{
struct swap_writer *handle = arg;
for (;;) {
/* Wait until there is a buffer ready for processing. */
pthread_mutex_lock(&move_mutex);
while(move_end == move_start && !save_ret)
pthread_cond_wait(&move_cond, &move_mutex);
pthread_mutex_unlock(&move_mutex);
if (save_ret)
break;
if (do_encrypt)
encrypt_and_save_buffer();
else
save_buffer(handle);
if (save_ret)
break;
/* Tell the reader thread that we have processed the buffer */
pthread_mutex_lock(&finish_mutex);
pthread_mutex_lock(&move_mutex);
move_end = move_inc(move_end);
pthread_mutex_unlock(&move_mutex);
pthread_mutex_unlock(&finish_mutex);
pthread_cond_signal(&move_cond);
pthread_cond_signal(&finish_cond);