PM: hibernate: LZ4 compression support
Subsystems (3)
cover PM: hibernate: LZ4 compression support
This patch series covers the following:
1. Renaming lzo* to generic names, except for lzo_xxx() APIs. This is used in the next patch where we move to crypto based APIs for compression. There are no functional changes introduced by this approach.
2. Replace LZO library calls with crypto generic APIs
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.
3. LZ4 compression
Extend the support for LZ4 compression to be used with hibernation. The main idea is that different compression algorithms
have different characteristics and hibernation may benefit when it uses any of these algorithms: a default algorithm, having higher compression rate but is slower(compression/decompression) and a secondary algorithm, that is faster(compression/decompression) but has lower compression rate.
LZ4 algorithm has better decompression speeds over LZO. This reduces the hibernation image restore time.
As per test results:
LZO LZ4
Size before Compression(bytes) 682696704 682393600 Size after Compression(bytes) 146502402 155993547 Decompression Rate 335.02 MB/s 501.05 MB/s
Restore time 4.4s 3.8s
LZO is the default compression algorithm used for hibernation. Enable CONFIG_HIBERNATION_DEF_COMP_LZ4 to set the default compressor as LZ4.
Compression Benchmarks: https://github.com/lz4/lz4
4. Support to select compression algorithm
Currently the default compression algorithm is selected based on Kconfig. Introduce a kernel command line parameter "hib_compression" to override this behaviour.
Users can set "hib_compression" command line parameter to specify the algorithm.
Usage:
LZO: hib_compression=lzo
LZ4: hib_compression=lz4
LZO is the default compression algorithm used with hibernation.
Nikhil V (4):
PM: hibernate: Rename lzo* to make it generic
PM: hibernate: Move to crypto APIs for LZO compression
PM: hibernate: Add support for LZ4 compression for hibernation
PM: hibernate: Support to select compression algorithm
.../admin-guide/kernel-parameters.txt | 6 + kernel/power/Kconfig | 25 ++- kernel/power/hibernate.c | 83 +++++++- kernel/power/power.h | 19 ++ kernel/power/swap.c | 189 +++++++++++------- 5 files changed, 248 insertions(+), 74 deletions(-)
Signature · 1 line
2.17.1
RFC PATCH 1/4 PM: hibernate: Rename lzo* to make it generic
Renaming lzo* to generic names, except for lzo_xxx() APIs. This is used in the next patch where we move to crypto based APIs for compression. There are no functional changes introduced by this approach.
--- kernel/power/swap.c | 122 ++++++++++++++++++++++---------------------- 1 file changed, 61 insertions(+), 61 deletions(-)
kernel/power/swap.c +61 -61
@@ -514,23 +514,23 @@ static int swap_writer_finish(struct swap_map_handle *handle, }/* We need to remember how much compressed data we need to read. */ -#define LZO_HEADER sizeof(size_t) +#define CMP_HEADER sizeof(size_t)/* Number of pages/bytes we'll compress at one time. */ -#define LZO_UNC_PAGES 32 -#define LZO_UNC_SIZE (LZO_UNC_PAGES * PAGE_SIZE) +#define UNC_PAGES 32 +#define UNC_SIZE (UNC_PAGES * PAGE_SIZE) -/* Number of pages/bytes we need for compressed data (worst case). */ -#define LZO_CMP_PAGES DIV_ROUND_UP(lzo1x_worst_compress(LZO_UNC_SIZE) + \ - LZO_HEADER, PAGE_SIZE) -#define LZO_CMP_SIZE (LZO_CMP_PAGES * PAGE_SIZE) +/* Number of pages we need for compressed data (worst case). */ +#define CMP_PAGES DIV_ROUND_UP(lzo1x_worst_compress(UNC_SIZE) + \ + CMP_HEADER, PAGE_SIZE) +#define CMP_SIZE (CMP_PAGES * PAGE_SIZE)/* Maximum number of threads for compression/decompression. */ -#define LZO_THREADS 3 +#define CMP_THREADS 3/* Minimum/maximum number of pages for read buffering. */ -#define LZO_MIN_RD_PAGES 1024 -#define LZO_MAX_RD_PAGES 8192 +#define CMP_MIN_RD_PAGES 1024 +#define CMP_MAX_RD_PAGES 8192/**
@@ -592,8 +592,8 @@ struct crc_data {wait_queue_head_t go;/* start crc update */ wait_queue_head_t done;/* crc update done */ u32 *crc32;/* points to handle's crc32 */ -size_t *unc_len[LZO_THREADS ];/* uncompressed lengths */ -unsigned char *unc[LZO_THREADS ];/* uncompressed data */ +size_t *unc_len[CMP_THREADS ];/* uncompressed lengths */ +unsigned char *unc[CMP_THREADS ];/* uncompressed data */ };/*
@@ -624,7 +624,7 @@ static int crc32_threadfn(void *data)return 0 ; }/* -* Structure used for LZO data compression. +* Structure used for data compression. */ struct cmp_data {struct task_struct *thr;/* thread */
@@ -635,15 +635,15 @@ struct cmp_data {wait_queue_head_t done;/* compression done */ size_t unc_len;/* uncompressed length */ size_t cmp_len;/* compressed length */ -unsigned char unc[LZO_UNC_SIZE ];/* uncompressed buffer */ -unsigned char cmp[LZO_CMP_SIZE ];/* compressed buffer */ +unsigned char unc[UNC_SIZE ];/* uncompressed buffer */ +unsigned char cmp[CMP_SIZE ];/* compressed buffer */ unsigned char wrk[LZO1X_1_MEM_COMPRESS ];/* compression workspace */ };/* * Compression function that runs in its own thread. */ -static int lzo_compress_threadfn (void *data) +static int compress_threadfn (void *data) {struct cmp_data *d = data;
@@ -660,8 +660,8 @@ static int lzo_compress_threadfn(void *data)atomic_set (&d->ready,0 ); d->ret =lzo1x_1_compress (d->unc, d->unc_len, - d->cmp +LZO_HEADER , &d->cmp_len, - d->wrk); + d->cmp +CMP_HEADER , &d->cmp_len, + d->wrk);atomic_set (&d->stop,1 );wake_up (&d->done); }
@@ -669,14 +669,14 @@ static int lzo_compress_threadfn(void *data) }/** -* save_image_lzo - Save the suspend image data compressed with LZO. +* save_compressed_image - Save the suspend image data after compression. * @handle: Swap map handle to use for saving the image. * @snapshot: Image to read data from. * @nr_to_write: Number of pages to save. */ -static int save_image_lzo (struct swap_map_handle *handle, -struct snapshot_handle *snapshot, -unsigned int nr_to_write) +static int save_compressed_image (struct swap_map_handle *handle, +struct snapshot_handle *snapshot, +unsigned int nr_to_write) {unsigned int m;int ret =0 ;
@@ -698,18 +698,18 @@ static int save_image_lzo(struct swap_map_handle *handle,* footprint. */ nr_threads =num_online_cpus () -1 ; - nr_threads =clamp_val (nr_threads,1 ,LZO_THREADS ); + nr_threads =clamp_val (nr_threads,1 ,CMP_THREADS ); page = (void *)__get_free_page (GFP_NOIO | __GFP_HIGH);if (!page) { -pr_err ("Failed to allocate LZO page\n" ); +pr_err ("Failed to allocate compression page\n" ); ret = -ENOMEM ; goto out_clean; } data =vzalloc (array_size (nr_threads,sizeof (*data)));if (!data) { -pr_err ("Failed to allocate LZO data\n" ); +pr_err ("Failed to allocate compression data\n" ); ret = -ENOMEM ; goto out_clean; }
@@ -728,7 +728,7 @@ static int save_image_lzo(struct swap_map_handle *handle,init_waitqueue_head (&data[thr].go);init_waitqueue_head (&data[thr].done); - data[thr].thr =kthread_run (lzo_compress_threadfn, + data[thr].thr =kthread_run (compress_threadfn, &data[thr],"image_compress/%u" , thr);if (IS_ERR (data[thr].thr)) {
@@ -776,7 +776,7 @@ static int save_image_lzo(struct swap_map_handle *handle, start =ktime_get ();for (;;) {for (thr =0 ; thr < nr_threads; thr++) { -for (off =0 ; off <LZO_UNC_SIZE ; off +=PAGE_SIZE ) { +for (off =0 ; off <UNC_SIZE ; off +=PAGE_SIZE ) { ret =snapshot_read_next (snapshot);if (ret <0 ) goto out_finish;
@@ -816,14 +816,14 @@ static int save_image_lzo(struct swap_map_handle *handle, ret = data[thr].ret;if (ret <0 ) { -pr_err ("LZO compression failed\n" ); +pr_err ("compression failed\n" ); goto out_finish; }if (unlikely (!data[thr].cmp_len || data[thr].cmp_len >lzo1x_worst_compress (data[thr].unc_len))) { -pr_err ("Invalid LZO compressed length\n" ); +pr_err ("Invalid compressed length\n" ); ret =-1 ; goto out_finish; }
@@ -839,7 +839,7 @@ static int save_image_lzo(struct swap_map_handle *handle,* read it. */ for (off =0 ; - off <LZO_HEADER + data[thr].cmp_len; + off <CMP_HEADER + data[thr].cmp_len; off +=PAGE_SIZE ) {memcpy (page, data[thr].cmp + off,PAGE_SIZE );
@@ -941,7 +941,7 @@ int swsusp_write(unsigned int flags)if (!error) { error = (flags &SF_NOCOMPRESS_MODE ) ?save_image (&handle, &snapshot, pages -1 ) : -save_image_lzo (&handle, &snapshot, pages -1 ); +save_compressed_image (&handle, &snapshot, pages -1 ); } out_finish: error =swap_writer_finish (&handle, flags, error);
@@ -1108,7 +1108,7 @@ static int load_image(struct swap_map_handle *handle, }/* -* Structure used for LZO data decompression. +* Structure used for data decompression. */ struct dec_data {struct task_struct *thr;/* thread */
@@ -1119,14 +1119,14 @@ struct dec_data {wait_queue_head_t done;/* decompression done */ size_t unc_len;/* uncompressed length */ size_t cmp_len;/* compressed length */ -unsigned char unc[LZO_UNC_SIZE ];/* uncompressed buffer */ -unsigned char cmp[LZO_CMP_SIZE ];/* compressed buffer */ +unsigned char unc[UNC_SIZE ];/* uncompressed buffer */ +unsigned char cmp[CMP_SIZE ];/* compressed buffer */ };/* * Decompression function that runs in its own thread. */ -static int lzo_decompress_threadfn (void *data) +static int decompress_threadfn (void *data) {struct dec_data *d = data;
@@ -1142,9 +1142,9 @@ static int lzo_decompress_threadfn(void *data) }atomic_set (&d->ready,0 ); - d->unc_len =LZO_UNC_SIZE ; - d->ret =lzo1x_decompress_safe (d->cmp +LZO_HEADER , d->cmp_len, - d->unc, &d->unc_len); + d->unc_len =UNC_SIZE ; + d->ret =lzo1x_decompress_safe (d->cmp +CMP_HEADER , d->cmp_len, + d->unc, &d->unc_len);if (clean_pages_on_decompress)flush_icache_range ((unsigned long )d->unc, (unsigned long )d->unc + d->unc_len);
@@ -1156,14 +1156,14 @@ static int lzo_decompress_threadfn(void *data) }/** -* load_image_lzo - Load compressed image data and decompress them with LZO. +* load_compressed_image - Load compressed image data and decompress it. * @handle: Swap map handle to use for loading data. * @snapshot: Image to copy uncompressed data into. * @nr_to_read: Number of pages to load. */ -static int load_image_lzo (struct swap_map_handle *handle, -struct snapshot_handle *snapshot, -unsigned int nr_to_read) +static int load_compressed_image (struct swap_map_handle *handle, +struct snapshot_handle *snapshot, +unsigned int nr_to_read) {unsigned int m;int ret =0 ;
@@ -1188,18 +1188,18 @@ static int load_image_lzo(struct swap_map_handle *handle,* footprint. */ nr_threads =num_online_cpus () -1 ; - nr_threads =clamp_val (nr_threads,1 ,LZO_THREADS ); + nr_threads =clamp_val (nr_threads,1 ,CMP_THREADS ); - page =vmalloc (array_size (LZO_MAX_RD_PAGES ,sizeof (*page))); + page =vmalloc (array_size (CMP_MAX_RD_PAGES ,sizeof (*page)));if (!page) { -pr_err ("Failed to allocate LZO page\n" ); +pr_err ("Failed to allocate compression page\n" ); ret = -ENOMEM ; goto out_clean; } data =vzalloc (array_size (nr_threads,sizeof (*data)));if (!data) { -pr_err ("Failed to allocate LZO data\n" ); +pr_err ("Failed to allocate compression data\n" ); ret = -ENOMEM ; goto out_clean; }
@@ -1220,7 +1220,7 @@ static int load_image_lzo(struct swap_map_handle *handle,init_waitqueue_head (&data[thr].go);init_waitqueue_head (&data[thr].done); - data[thr].thr =kthread_run (lzo_decompress_threadfn, + data[thr].thr =kthread_run (decompress_threadfn, &data[thr],"image_decompress/%u" , thr);if (IS_ERR (data[thr].thr)) {
@@ -1261,18 +1261,18 @@ static int load_image_lzo(struct swap_map_handle *handle,*/ if (low_free_pages () >snapshot_get_image_size ()) read_pages = (low_free_pages () -snapshot_get_image_size ()) /2 ; - read_pages =clamp_val (read_pages,LZO_MIN_RD_PAGES ,LZO_MAX_RD_PAGES ); + read_pages =clamp_val (read_pages,CMP_MIN_RD_PAGES ,CMP_MAX_RD_PAGES );for (i =0 ; i < read_pages; i++) { - page[i] = (void *)__get_free_page (i <LZO_CMP_PAGES ? + page[i] = (void *)__get_free_page (i <CMP_PAGES ?GFP_NOIO | __GFP_HIGH :GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY);if (!page[i]) { -if (i <LZO_CMP_PAGES ) { +if (i <CMP_PAGES ) { ring_size = i; -pr_err ("Failed to allocate LZO pages\n" ); +pr_err ("Failed to allocate compression pages\n" ); ret = -ENOMEM ; goto out_clean; }else {
@@ -1343,13 +1343,13 @@ static int load_image_lzo(struct swap_map_handle *handle, data[thr].cmp_len = *(size_t *)page[pg];if (unlikely (!data[thr].cmp_len || data[thr].cmp_len > -lzo1x_worst_compress (LZO_UNC_SIZE ))) { -pr_err ("Invalid LZO compressed length\n" ); +lzo1x_worst_compress (UNC_SIZE ))) { +pr_err ("Invalid compressed length\n" ); ret =-1 ; goto out_finish; } - need =DIV_ROUND_UP (data[thr].cmp_len +LZO_HEADER , + need =DIV_ROUND_UP (data[thr].cmp_len +CMP_HEADER ,PAGE_SIZE );if (need > have) {if (eof >1 ) {
@@ -1360,7 +1360,7 @@ static int load_image_lzo(struct swap_map_handle *handle, }for (off =0 ; - off <LZO_HEADER + data[thr].cmp_len; + off <CMP_HEADER + data[thr].cmp_len; off +=PAGE_SIZE ) {memcpy (data[thr].cmp + off, page[pg],PAGE_SIZE );
@@ -1377,7 +1377,7 @@ static int load_image_lzo(struct swap_map_handle *handle,/* * Wait for more data while we are decompressing. */ -if (have <LZO_CMP_PAGES && asked) { +if (have <CMP_PAGES && asked) { ret =hib_wait_io (&hb);if (ret) goto out_finish;
@@ -1395,14 +1395,14 @@ static int load_image_lzo(struct swap_map_handle *handle, ret = data[thr].ret;if (ret <0 ) { -pr_err ("LZO decompression failed\n" ); +pr_err ("decompression failed\n" ); goto out_finish; }if (unlikely (!data[thr].unc_len || - data[thr].unc_len >LZO_UNC_SIZE || - data[thr].unc_len & (PAGE_SIZE -1 ))) { -pr_err ("Invalid LZO uncompressed length\n" ); + data[thr].unc_len >UNC_SIZE || + data[thr].unc_len & (PAGE_SIZE -1 ))) { +pr_err ("Invalid uncompressed length\n" ); ret =-1 ; goto out_finish; }
@@ -1499,7 +1499,7 @@ int swsusp_read(unsigned int *flags_p)if (!error) { error = (*flags_p &SF_NOCOMPRESS_MODE ) ?load_image (&handle, &snapshot, header->pages -1 ) : -load_image_lzo (&handle, &snapshot, header->pages -1 ); +load_compressed_image (&handle, &snapshot, header->pages -1 ); }swap_reader_finish (&handle); end:
Signature · 1 line
2.17.1
RFC PATCH 2/4 PM: hibernate: Move to crypto APIs for LZO compression
In reply to Nikhil VCurrently 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.
--- kernel/power/Kconfig | 20 +++++++- kernel/power/hibernate.c | 33 +++++++++++++ kernel/power/power.h | 5 ++ kernel/power/swap.c | 101 +++++++++++++++++++++++++++++---------- 4 files changed, 131 insertions(+), 28 deletions(-)
kernel/power/Kconfig +18 -2
@@ -39,9 +39,8 @@ config HIBERNATION bool "Hibernation (aka 'suspend to disk')" depends on SWAP && ARCH_HIBERNATION_POSSIBLE select HIBERNATE_CALLBACKS - select LZO_COMPRESS - select LZO_DECOMPRESS select CRC32 + select CRYPTO_LZO help Enable the suspend to disk (STD) functionality, which is usually called "hibernation" in user interfaces. STD checkpoints the
@@ -92,6 +91,23 @@ config HIBERNATION_SNAPSHOT_DEV If in doubt, say Y. +choice + prompt "Default compressor" + default HIBERNATION_COMP_LZO + depends on HIBERNATION + +config HIBERNATION_COMP_LZO + bool "lzo" + depends on CRYPTO_LZO + +endchoice + +config HIBERNATION_DEF_COMP + string + default "lzo" if HIBERNATION_COMP_LZO + help + Default compressor to be used for hibernation. + config PM_STD_PARTITION string "Default resume partition" depends on HIBERNATION
kernel/power/hibernate.c +33 -0
@@ -47,6 +47,15 @@ dev_t swsusp_resume_device;sector_t swsusp_resume_block; __visible int in_suspend __nosavedata; +static const char *default_compressor =CONFIG_HIBERNATION_DEF_COMP ; + +/* +* Compression/decompression algorithm to be used while saving/loading +* image to/from disk. This would later be used in 'kernel/power/swap.c' +* to allocate comp streams. +*/ +char hib_comp_algo[CRYPTO_MAX_ALG_NAME ]; +enum {HIBERNATION_INVALID ,HIBERNATION_PLATFORM ,
@@ -726,6 +735,17 @@ int hibernate(void)return -EPERM ; } +/* +* Query for the compression algorithm support if compression is enabled. +*/ +if (!nocompress) { +strscpy (hib_comp_algo, default_compressor,sizeof (hib_comp_algo)); +if (crypto_has_comp (hib_comp_algo,0 ,0 ) !=1 ) { +pr_err ("%s compression is not available\n" , hib_comp_algo); +return -EOPNOTSUPP ; + } + } + sleep_flags =lock_system_sleep ();/* The snapshot device should not be opened while we're running */ if (!hibernate_acquire ()) {
@@ -949,6 +969,19 @@ static int software_resume(void)if (error) goto Unlock; +/* +* Check if the hibernation image is compressed. If so, query for +* the algorithm support. +*/ +if (!(swsusp_header_flags &SF_NOCOMPRESS_MODE )) { +strscpy (hib_comp_algo, default_compressor,sizeof (hib_comp_algo)); +if (crypto_has_comp (hib_comp_algo,0 ,0 ) !=1 ) { +pr_err ("%s compression is not available\n" , hib_comp_algo); + error = -EOPNOTSUPP ; + goto Unlock; + } + } +/* The snapshot device should not be opened while we're running */ if (!hibernate_acquire ()) { error = -EBUSY ;
kernel/power/power.h +5 -0
@@ -6,6 +6,7 @@#include <linux/compiler.h> #include <linux/cpu.h> #include <linux/cpuidle.h> +#include <linux/crypto.h> struct swsusp_info {struct new_utsname uts;
@@ -54,6 +55,10 @@ asmlinkage int swsusp_save(void);/* kernel/power/hibernate.c */ extern bool freezer_test_done; +extern char hib_comp_algo[CRYPTO_MAX_ALG_NAME ]; + +/* kernel/power/swap.c */ +extern unsigned int swsusp_header_flags;extern int hibernation_snapshot (int platform_mode);extern int hibernation_restore (int platform_mode);
kernel/power/swap.c +75 -26
292 lines not on this page · raw message
Signature · 1 line
2.17.1
RFC PATCH 3/4 PM: hibernate: Add support for LZ4 compression for hibernation
In reply to Nikhil VExtend the support for LZ4 compression to be used with hibernation. The main idea is that different compression algorithms
have different characteristics and hibernation may benefit when it uses any of these algorithms: a default algorithm, having higher compression rate but is slower(compression/decompression) and a secondary algorithm, that is faster(compression/decompression) but has lower compression rate.
LZ4 algorithm has better decompression speeds over LZO. This reduces the hibernation image restore time.
As per test results:
LZO LZ4
Size before Compression(bytes) 682696704 682393600 Size after Compression(bytes) 146502402 155993547 Decompression Rate 335.02 MB/s 501.05 MB/s
Restore time 4.4s 3.8s
LZO is the default compression algorithm used for hibernation. Enable CONFIG_HIBERNATION_COMP_LZ4 to set the default compressor as LZ4.
--- kernel/power/Kconfig | 5 +++++ kernel/power/hibernate.c | 25 ++++++++++++++++++++++--- kernel/power/power.h | 14 ++++++++++++++ 3 files changed, 41 insertions(+), 3 deletions(-)
kernel/power/Kconfig +5 -0
@@ -100,11 +100,16 @@ config HIBERNATION_COMP_LZO bool "lzo" depends on CRYPTO_LZO +config HIBERNATION_COMP_LZ4 + bool "lz4" + depends on CRYPTO_LZ4 + endchoice config HIBERNATION_DEF_COMP string default "lzo" if HIBERNATION_COMP_LZO + default "lz4" if HIBERNATION_COMP_LZ4 help Default compressor to be used for hibernation.
kernel/power/hibernate.c +22 -3
@@ -721,6 +721,9 @@ static int load_image_and_restore(bool snapshot_test)return error; } +#define COMPRESSION_ALGO_LZO "lzo" +#define COMPRESSION_ALGO_LZ4 "lz4" +/** * hibernate - Carry out system hibernation, including saving the image. */
@@ -780,11 +783,24 @@ int hibernate(void)if (hibernation_mode ==HIBERNATION_PLATFORM ) flags |=SF_PLATFORM_MODE ; -if (nocompress) +if (nocompress) { flags |=SF_NOCOMPRESS_MODE ; -else + }else { flags |=SF_CRC32_MODE ; +/* +* By default, LZO compression is enabled. Use SF_COMPRESSION_ALG_LZ4 +* to override this behaviour and use LZ4. +* +* Refer kernel/power/power.h for more details +*/ + +if (!strcmp (hib_comp_algo,COMPRESSION_ALGO_LZ4 )) + flags |=SF_COMPRESSION_ALG_LZ4 ; +else + flags |=SF_COMPRESSION_ALG_LZO ; + } +pm_pr_dbg ("Writing hibernation image.\n" ); error =swsusp_write (flags);swsusp_free ();
@@ -974,7 +990,10 @@ static int software_resume(void)* the algorithm support. */ if (!(swsusp_header_flags &SF_NOCOMPRESS_MODE )) { -strscpy (hib_comp_algo, default_compressor,sizeof (hib_comp_algo)); +if (swsusp_header_flags &SF_COMPRESSION_ALG_LZ4 ) +strscpy (hib_comp_algo,COMPRESSION_ALGO_LZ4 ,sizeof (hib_comp_algo)); +else +strscpy (hib_comp_algo, default_compressor,sizeof (hib_comp_algo));if (crypto_has_comp (hib_comp_algo,0 ,0 ) !=1 ) {pr_err ("%s compression is not available\n" , hib_comp_algo); error = -EOPNOTSUPP ;
kernel/power/power.h +14 -0
@@ -167,11 +167,25 @@ extern int swsusp_swap_in_use(void);* Flags that can be passed from the hibernatig hernel to the "boot" kernel in * the image header. */ +#define SF_COMPRESSION_ALG_LZO 0/* dummy, details given below */ #define SF_PLATFORM_MODE 1#define SF_NOCOMPRESS_MODE 2#define SF_CRC32_MODE 4#define SF_HW_SIG 8 +/* +* Bit to indicate the compression algorithm to be used(for LZ4). The same +* could be checked while saving/loading image to/from disk to use the +* corresponding algorithms. +* +* By default, LZO compression is enabled if SF_CRC32_MODE is set. Use +* SF_COMPRESSION_ALG_LZ4 to override this behaviour and use LZ4. +* +* SF_CRC32_MODE, SF_COMPRESSION_ALG_LZO(dummy) -> Compression, LZO +* SF_CRC32_MODE, SF_COMPRESSION_ALG_LZ4 -> Compression, LZ4 +*/ +#define SF_COMPRESSION_ALG_LZ4 16 +/* kernel/power/hibernate.c */ int swsusp_check (bool exclusive);extern void swsusp_free (void );
Signature · 1 line
2.17.1
RFC PATCH 4/4 PM: hibernate: Support to select compression algorithm
In reply to Nikhil VCurrently the default compression algorithm is selected based on Kconfig. Introduce a kernel command line parameter "hib_compression" to override this behaviour.
Different compression algorithms have different characteristics and hibernation may benefit when it uses any of these algorithms, especially when a secondary algorithm offers better decompression speeds over a default algorithm, which in turn reduces hibernation image restore time.
Users can set "hib_compression" command line parameter to override the default algorithm. Currently LZO and LZ4 are the supported algorithms. Usage:
LZO: hib_compression=lzo
LZ4: hib_compression=lz4
LZO is the default compression algorithm used with hibernation.
--- .../admin-guide/kernel-parameters.txt | 6 ++++ kernel/power/hibernate.c | 29 ++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-)
Documentation/admin-guide/kernel-parameters.txt +6 -0
@@ -1734,6 +1734,12 @@ (that will set all pages holding image data during restoration read-only). + hib_compression= [COMPRESSION ALGORITHM] + lzo Select LZO compression algorithm to compress/decompress + hibernation images. + lz4 Select LZ4 compression algorithm to compress/decompress + hibernation images. + highmem=nn[KMG] [KNL,BOOT] forces the highmem zone to have an exact size of <nn>. This works even on boxes that have no highmem otherwise. This also works to reduce highmem
kernel/power/hibernate.c +28 -1
@@ -742,7 +742,8 @@ int hibernate(void)* Query for the compression algorithm support if compression is enabled. */ if (!nocompress) { -strscpy (hib_comp_algo, default_compressor,sizeof (hib_comp_algo)); +if (!hib_comp_algo[0 ]) +strscpy (hib_comp_algo, default_compressor,sizeof (hib_comp_algo));if (crypto_has_comp (hib_comp_algo,0 ,0 ) !=1 ) {pr_err ("%s compression is not available\n" , hib_comp_algo);return -EOPNOTSUPP ;
@@ -1416,6 +1417,31 @@ static int __init nohibernate_setup(char *str)return 1 ; } +static const char *const comp_alg_enabled[] = { +#if IS_ENABLED(CONFIG_CRYPTO_LZO) +COMPRESSION_ALGO_LZO , +#endif +#if IS_ENABLED(CONFIG_CRYPTO_LZ4) +COMPRESSION_ALGO_LZ4 , +#endif +}; + +static int __init compression_setup (char *str) +{ +int i; + +for (i =0 ; i <ARRAY_SIZE (comp_alg_enabled); i++) { +if (!strcmp (str, comp_alg_enabled[i])) { +strscpy (hib_comp_algo, str,sizeof (hib_comp_algo)); + goto setup_done; + } + } +strscpy (hib_comp_algo, default_compressor,sizeof (hib_comp_algo)); + +setup_done: +return 1 ; +} +__setup ("noresume" , noresume_setup);__setup ("resume_offset=" , resume_offset_setup);__setup ("resume=" , resume_setup);
@@ -1423,3 +1449,4 @@ __setup("hibernate=", hibernate_setup);__setup ("resumewait" , resumewait_setup);__setup ("resumedelay=" , resumedelay_setup);__setup ("nohibernate" , nohibernate_setup); +__setup ("hib_compression=" , compression_setup);
Signature · 1 line
2.17.1
Quoted 62 lines · Nikhil V
This patch series covers the following:
1. Renaming lzo* to generic names, except forlzo_xxx()APIs. This is used in the next patch where we move to crypto based APIs for compression. There are no functional changes introduced by this approach.2. Replace LZO library calls with crypto generic APIs
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.
3. LZ4 compression
Extend the support for LZ4 compression to be used with hibernation. The main idea is that different compression algorithms
have different characteristics and hibernation may benefit when it uses any of these algorithms: a default algorithm, having higher compression rate but is slower(compression/decompression) and a secondary algorithm, that is faster(compression/decompression) but has lower compression rate.LZ4 algorithm has better decompression speeds over LZO. This reduces the hibernation image restore time.
As per test results:
LZO LZ4Size before Compression(bytes) 682696704 682393600 Size after Compression(bytes) 146502402 155993547 Decompression Rate 335.02 MB/s 501.05 MB/sRestore time 4.4s 3.8s
LZO is the default compression algorithm used for hibernation. Enable
CONFIG_HIBERNATION_DEF_COMP_LZ4to set the default compressor as LZ4.Compression Benchmarks: https://github.com/lz4/lz4
4. Support to select compression algorithm
Currently the default compression algorithm is selected based on
Kconfig. Introduce a kernel command line parameter "hib_compression" to override this behaviour.Users can set "
hib_compression" command line parameter to specify the algorithm.
Usage:
LZO: hib_compression=lzo
LZ4: hib_compression=lz4
LZO is the default compression algorithm used with hibernation.Nikhil V (4):
PM: hibernate: Rename lzo* to make it generic
PM: hibernate: Move to crypto APIs for LZO compression
PM: hibernate: Add support for LZ4 compression for hibernation
PM: hibernate: Support to select compression algorithm
Quoted 7 lines
.../admin-guide/kernel-parameters.txt | 6 + kernel/power/Kconfig | 25 ++- kernel/power/hibernate.c | 83 +++++++- kernel/power/power.h | 19 ++ kernel/power/swap.c | 189 +++++++++++------- 5 files changed, 248 insertions(+), 74 deletions(-)
Hi,
Could you please let me know if you have any concerns/comments on this approach? If there are no comments, I could push another patch series with PATCH tag for review.
Thanks
Nikhil V
Quoted 62 lines · Nikhil V
This patch series covers the following:
1. Renaming lzo* to generic names, except forlzo_xxx()APIs. This is used in the next patch where we move to crypto based APIs for compression. There are no functional changes introduced by this approach.2. Replace LZO library calls with crypto generic APIs
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.
3. LZ4 compression
Extend the support for LZ4 compression to be used with hibernation. The main idea is that different compression algorithms
have different characteristics and hibernation may benefit when it uses any of these algorithms: a default algorithm, having higher compression rate but is slower(compression/decompression) and a secondary algorithm, that is faster(compression/decompression) but has lower compression rate.LZ4 algorithm has better decompression speeds over LZO. This reduces the hibernation image restore time.
As per test results:
LZO LZ4Size before Compression(bytes) 682696704 682393600 Size after Compression(bytes) 146502402 155993547 Decompression Rate 335.02 MB/s 501.05 MB/sRestore time 4.4s 3.8s
LZO is the default compression algorithm used for hibernation. Enable
CONFIG_HIBERNATION_DEF_COMP_LZ4to set the default compressor as LZ4.Compression Benchmarks: https://github.com/lz4/lz4
4. Support to select compression algorithm
Currently the default compression algorithm is selected based on
Kconfig. Introduce a kernel command line parameter "hib_compression" to override this behaviour.Users can set "
hib_compression" command line parameter to specify the algorithm.
Usage:
LZO: hib_compression=lzo
LZ4: hib_compression=lz4
LZO is the default compression algorithm used with hibernation.Nikhil V (4):
PM: hibernate: Rename lzo* to make it generic
PM: hibernate: Move to crypto APIs for LZO compression
PM: hibernate: Add support for LZ4 compression for hibernation
PM: hibernate: Support to select compression algorithm
Quoted 7 lines
.../admin-guide/kernel-parameters.txt | 6 + kernel/power/Kconfig | 25 ++- kernel/power/hibernate.c | 83 +++++++- kernel/power/power.h | 19 ++ kernel/power/swap.c | 189 +++++++++++------- 5 files changed, 248 insertions(+), 74 deletions(-)
I have tested this series on x86 QEMU with v6.6-rc3 (plus another patch from pm-next tree i.e "PM: hibernate: use __get_safe_page() rather than touching the list"). It works as advertised.
Thanks,
Pavan
Re: [RFC PATCH 4/4] PM: hibernate: Support to select compression algorithm
In reply to Nikhil VQuoted 30 lines · Nikhil V
kernel/power/hibernate.c +25
-0+static const char *const comp_alg_enabled[] = { +#if IS_ENABLED(CONFIG_CRYPTO_LZO) +COMPRESSION_ALGO_LZO , +#endif +#if IS_ENABLED(CONFIG_CRYPTO_LZ4) +COMPRESSION_ALGO_LZ4 , +#endif +}; + +static int __init compression_setup (char *str) +{ +int i; + +for (i =0 ; i <ARRAY_SIZE (comp_alg_enabled); i++) { +if (!strcmp (str, comp_alg_enabled[i])) { +strscpy (hib_comp_algo, str,sizeof (hib_comp_algo)); + goto setup_done; + } + } +strscpy (hib_comp_algo, default_compressor,sizeof (hib_comp_algo)); + +setup_done: +return 1 ; +} +__setup ("noresume" , noresume_setup);__setup ("resume_offset=" , resume_offset_setup);__setup ("resume=" , resume_setup);
kernel/power/hibernate.c __setup("hibernate=", hibernate_setup); · line 1452
Quoted 4 lines
kernel/power/hibernate.c +1
-0@@ -1423,3 +1449,4 @@ __setup("hibernate=", hibernate_setup);__setup ("resumewait" , resumewait_setup);__setup ("resumedelay=" , resumedelay_setup);__setup ("nohibernate" , nohibernate_setup); +__setup ("hib_compression=" , compression_setup);
If I pass hib_compression=lz4 without enabling CONFIG_CRYPTO_LZ4, It silently makes the compression to lzo which is expected from this code. It would be good if an error/info message is printed in compression_setup() which this happens.
Thanks,
Pavan
kernel test robot
build botRe: [RFC PATCH 2/4] PM: hibernate: Move to crypto APIs for LZO compression
In reply to Nikhil Vkernel test robot noticed the following build errors:
Hi Nikhil,
[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:
[auto build test ERROR on linus/master]
[also build test ERROR on v6.6-rc4 next-20231003]
[cannot apply to pavel-leds/for-next]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Nikhil-V/PM-hibernate-Rename-lzo-to-make-it-generic/20230926-170135 base: linus/master patch link: https://lore.kernel.org/r/486273fb828eb56d1895fee49481f5c035df8300.1695711299.git.quic_nprakash%40quicinc.com patch subject: [RFC PATCH 2/4] PM: hibernate: Move to crypto APIs for LZO compression config: sh-sh7770_generic_defconfig (https://download.01.org/0day-ci/archive/20231004/[email protected]/config) compiler: sh4-linux-gcc (GCC) 13.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231004/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
sh4-linux-ld: kernel/power/hibernate.o: in function `software_resume':
>> hibernate.c:(.text+0xd0c): undefined reference to `crypto_has_alg' sh4-linux-ld: kernel/power/hibernate.o: in function `hibernate': hibernate.c:(.text+0x1218): undefined reference to `crypto_has_alg' sh4-linux-ld: kernel/power/swap.o: in function `compress_threadfn': >> swap.c:(.text+0x5d8): undefined reference to `crypto_comp_compress' sh4-linux-ld: kernel/power/swap.o: in function `decompress_threadfn': >> swap.c:(.text+0x728): undefined reference to `crypto_comp_decompress' sh4-linux-ld: kernel/power/swap.o: in function `load_compressed_image': >> swap.c:(.text+0x9ec): undefined reference to `crypto_alloc_base' >> sh4-linux-ld: swap.c:(.text+0xe7c): undefined reference to `crypto_destroy_tfm' sh4-linux-ld: kernel/power/swap.o: in function `save_compressed_image': swap.c:(.text+0x14f8): undefined reference to `crypto_alloc_base' sh4-linux-ld: swap.c:(.text+0x169c): undefined reference to `crypto_destroy_tfm' sh4-linux-ld: crypto/lzo.o: in function `lzo_decompress': >> lzo.c:(.text+0x50): undefined reference to `lzo1x_decompress_safe' sh4-linux-ld: crypto/lzo.o: in function `lzo_compress': >> lzo.c:(.text+0xb0): undefined reference to `lzo1x_1_compress' sh4-linux-ld: crypto/lzo.o: in function `lzo_sdecompress': lzo.c:(.text+0x144): undefined reference to `lzo1x_decompress_safe' sh4-linux-ld: crypto/lzo.o: in function `lzo_scompress': lzo.c:(.text+0x1a0): undefined reference to `lzo1x_1_compress' sh4-linux-ld: crypto/lzo.o: in function `lzo_mod_fini': >> lzo.c:(.exit.text+0x14): undefined reference to `crypto_unregister_alg' >> sh4-linux-ld: lzo.c:(.exit.text+0x20): undefined reference to `crypto_unregister_scomp' sh4-linux-ld: crypto/lzo.o: in function `lzo_mod_init': >> lzo.c:(.init.text+0x30): undefined reference to `crypto_register_alg' >> sh4-linux-ld: lzo.c:(.init.text+0x38): undefined reference to `crypto_register_scomp' >> sh4-linux-ld: lzo.c:(.init.text+0x40): undefined reference to `crypto_unregister_alg' sh4-linux-ld: crypto/lzo-rle.o: in function `lzorle_decompress': >> lzo-rle.c:(.text+0x50): undefined reference to `lzo1x_decompress_safe' sh4-linux-ld: crypto/lzo-rle.o: in function `lzorle_compress': >> lzo-rle.c:(.text+0xb0): undefined reference to `lzorle1x_1_compress' sh4-linux-ld: crypto/lzo-rle.o: in function `lzorle_sdecompress': lzo-rle.c:(.text+0x144): undefined reference to `lzo1x_decompress_safe' sh4-linux-ld: crypto/lzo-rle.o: in function `lzorle_scompress': lzo-rle.c:(.text+0x1a0): undefined reference to `lzorle1x_1_compress' sh4-linux-ld: crypto/lzo-rle.o: in function `lzorle_mod_fini': >> lzo-rle.c:(.exit.text+0x14): undefined reference to `crypto_unregister_alg' >> sh4-linux-ld: lzo-rle.c:(.exit.text+0x20): undefined reference to `crypto_unregister_scomp' sh4-linux-ld: crypto/lzo-rle.o: in function `lzorle_mod_init': >> lzo-rle.c:(.init.text+0x30): undefined reference to `crypto_register_alg' >> sh4-linux-ld: lzo-rle.c:(.init.text+0x38): undefined reference to `crypto_register_scomp' >> sh4-linux-ld: lzo-rle.c:(.init.text+0x40): undefined reference to `crypto_unregister_alg'
Kconfig warnings: (for reference only)
WARNING: unmet direct dependencies detected for CRYPTO_LZO Depends on [n]: CRYPTO [=n]
Selected by [y]:
- HIBERNATION [=y] && SWAP [=y] && ARCH_HIBERNATION_POSSIBLE [=y]
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki