Skip to content

Commit a06c6f5

Browse files
vnikhilprakashrafaeljw
authored andcommitted
PM: hibernate: Move to crypto APIs for LZO compression
Currently for hibernation, LZO is the only compression algorithm available and uses the existing LZO library calls. However, there is no flexibility to switch to other algorithms which provides better results. The main idea is that different compression algorithms have different characteristics and hibernation may benefit when it uses alternate algorithms. By moving to crypto based APIs, it lays a foundation to use other compression algorithms for hibernation. There are no functional changes introduced by this approach. Signed-off-by: Nikhil V <quic_nprakash@quicinc.com> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
1 parent 89a8076 commit a06c6f5

File tree

4 files changed

+132
-28
lines changed

4 files changed

+132
-28
lines changed

kernel/power/Kconfig

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ config HIBERNATION
3939
bool "Hibernation (aka 'suspend to disk')"
4040
depends on SWAP && ARCH_HIBERNATION_POSSIBLE
4141
select HIBERNATE_CALLBACKS
42-
select LZO_COMPRESS
43-
select LZO_DECOMPRESS
4442
select CRC32
43+
select CRYPTO
44+
select CRYPTO_LZO
4545
help
4646
Enable the suspend to disk (STD) functionality, which is usually
4747
called "hibernation" in user interfaces. STD checkpoints the
@@ -92,6 +92,23 @@ config HIBERNATION_SNAPSHOT_DEV
9292

9393
If in doubt, say Y.
9494

95+
choice
96+
prompt "Default compressor"
97+
default HIBERNATION_COMP_LZO
98+
depends on HIBERNATION
99+
100+
config HIBERNATION_COMP_LZO
101+
bool "lzo"
102+
depends on CRYPTO_LZO
103+
104+
endchoice
105+
106+
config HIBERNATION_DEF_COMP
107+
string
108+
default "lzo" if HIBERNATION_COMP_LZO
109+
help
110+
Default compressor to be used for hibernation.
111+
95112
config PM_STD_PARTITION
96113
string "Default resume partition"
97114
depends on HIBERNATION

kernel/power/hibernate.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ dev_t swsusp_resume_device;
4747
sector_t swsusp_resume_block;
4848
__visible int in_suspend __nosavedata;
4949

50+
static const char *default_compressor = CONFIG_HIBERNATION_DEF_COMP;
51+
52+
/*
53+
* Compression/decompression algorithm to be used while saving/loading
54+
* image to/from disk. This would later be used in 'kernel/power/swap.c'
55+
* to allocate comp streams.
56+
*/
57+
char hib_comp_algo[CRYPTO_MAX_ALG_NAME];
58+
5059
enum {
5160
HIBERNATION_INVALID,
5261
HIBERNATION_PLATFORM,
@@ -732,6 +741,17 @@ int hibernate(void)
732741
return -EPERM;
733742
}
734743

744+
/*
745+
* Query for the compression algorithm support if compression is enabled.
746+
*/
747+
if (!nocompress) {
748+
strscpy(hib_comp_algo, default_compressor, sizeof(hib_comp_algo));
749+
if (crypto_has_comp(hib_comp_algo, 0, 0) != 1) {
750+
pr_err("%s compression is not available\n", hib_comp_algo);
751+
return -EOPNOTSUPP;
752+
}
753+
}
754+
735755
sleep_flags = lock_system_sleep();
736756
/* The snapshot device should not be opened while we're running */
737757
if (!hibernate_acquire()) {
@@ -955,6 +975,19 @@ static int software_resume(void)
955975
if (error)
956976
goto Unlock;
957977

978+
/*
979+
* Check if the hibernation image is compressed. If so, query for
980+
* the algorithm support.
981+
*/
982+
if (!(swsusp_header_flags & SF_NOCOMPRESS_MODE)) {
983+
strscpy(hib_comp_algo, default_compressor, sizeof(hib_comp_algo));
984+
if (crypto_has_comp(hib_comp_algo, 0, 0) != 1) {
985+
pr_err("%s compression is not available\n", hib_comp_algo);
986+
error = -EOPNOTSUPP;
987+
goto Unlock;
988+
}
989+
}
990+
958991
/* The snapshot device should not be opened while we're running */
959992
if (!hibernate_acquire()) {
960993
error = -EBUSY;

kernel/power/power.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <linux/compiler.h>
77
#include <linux/cpu.h>
88
#include <linux/cpuidle.h>
9+
#include <linux/crypto.h>
910

1011
struct swsusp_info {
1112
struct new_utsname uts;
@@ -54,6 +55,10 @@ asmlinkage int swsusp_save(void);
5455

5556
/* kernel/power/hibernate.c */
5657
extern bool freezer_test_done;
58+
extern char hib_comp_algo[CRYPTO_MAX_ALG_NAME];
59+
60+
/* kernel/power/swap.c */
61+
extern unsigned int swsusp_header_flags;
5762

5863
extern int hibernation_snapshot(int platform_mode);
5964
extern int hibernation_restore(int platform_mode);

0 commit comments

Comments
 (0)