Skip to content

Commit fe1d475

Browse files
committed
pstore: Select compression at runtime
To allow for easier build test coverage and run-time testing, this allows multiple compression algorithms to be built into pstore. Still only one is supported to operate at a time (which can be selected at build time or at boot time, similar to how LSMs are selected). Signed-off-by: Kees Cook <keescook@chromium.org>
1 parent 5559740 commit fe1d475

File tree

4 files changed

+151
-84
lines changed

4 files changed

+151
-84
lines changed

fs/pstore/Kconfig

Lines changed: 67 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,55 +12,93 @@ config PSTORE
1212
If you don't have a platform persistent store driver,
1313
say N.
1414

15-
choice
16-
prompt "Choose compression algorithm"
17-
depends on PSTORE
18-
default PSTORE_ZLIB_COMPRESS
19-
help
20-
This option chooses compression algorithm.
21-
22-
Currently, pstore has support for 5 compression algorithms:
23-
zlib, lzo, lz4, lz4hc and 842.
24-
25-
The default compression algorithm is zlib.
26-
2715
config PSTORE_ZLIB_COMPRESS
28-
bool "ZLIB"
29-
select ZLIB_DEFLATE
30-
select ZLIB_INFLATE
31-
help
32-
This option enables ZLIB compression algorithm support.
16+
bool "ZLIB compression"
17+
default y
18+
depends on PSTORE
19+
select ZLIB_DEFLATE
20+
select ZLIB_INFLATE
21+
help
22+
This option enables ZLIB compression algorithm support.
3323

3424
config PSTORE_LZO_COMPRESS
35-
bool "LZO"
36-
select LZO_COMPRESS
37-
select LZO_DECOMPRESS
38-
help
39-
This option enables LZO compression algorithm support.
25+
bool "LZO compression"
26+
depends on PSTORE
27+
select LZO_COMPRESS
28+
select LZO_DECOMPRESS
29+
help
30+
This option enables LZO compression algorithm support.
4031

4132
config PSTORE_LZ4_COMPRESS
42-
bool "LZ4"
43-
select LZ4_COMPRESS
44-
select LZ4_DECOMPRESS
45-
help
46-
This option enables LZ4 compression algorithm support.
33+
bool "LZ4 compression"
34+
depends on PSTORE
35+
select LZ4_COMPRESS
36+
select LZ4_DECOMPRESS
37+
help
38+
This option enables LZ4 compression algorithm support.
4739

4840
config PSTORE_LZ4HC_COMPRESS
49-
bool "LZ4HC"
41+
bool "LZ4HC compression"
42+
depends on PSTORE
5043
select LZ4HC_COMPRESS
5144
select LZ4_DECOMPRESS
5245
help
5346
This option enables LZ4HC (high compression) mode algorithm.
5447

5548
config PSTORE_842_COMPRESS
56-
bool "842"
49+
bool "842 compression"
50+
depends on PSTORE
5751
select 842_COMPRESS
5852
select 842_DECOMPRESS
5953
help
6054
This option enables 842 compression algorithm support.
6155

56+
config PSTORE_COMPRESS
57+
def_bool y
58+
depends on PSTORE
59+
depends on PSTORE_ZLIB_COMPRESS || PSTORE_LZO_COMPRESS || \
60+
PSTORE_LZ4_COMPRESS || PSTORE_LZ4HC_COMPRESS || \
61+
PSTORE_842_COMPRESS
62+
63+
choice
64+
prompt "Default pstore compression algorithm"
65+
depends on PSTORE_COMPRESS
66+
help
67+
This option chooses the default active compression algorithm.
68+
This change be changed at boot with "pstore.compress=..." on
69+
the kernel command line.
70+
71+
Currently, pstore has support for 5 compression algorithms:
72+
zlib, lzo, lz4, lz4hc and 842.
73+
74+
The default compression algorithm is zlib.
75+
76+
config PSTORE_ZLIB_COMPRESS_DEFAULT
77+
bool "zlib" if PSTORE_ZLIB_COMPRESS=y
78+
79+
config PSTORE_LZO_COMPRESS_DEFAULT
80+
bool "lzo" if PSTORE_LZO_COMPRESS=y
81+
82+
config PSTORE_LZ4_COMPRESS_DEFAULT
83+
bool "lz4" if PSTORE_LZ4_COMPRESS=y
84+
85+
config PSTORE_LZ4HC_COMPRESS_DEFAULT
86+
bool "lz4hc" if PSTORE_LZ4HC_COMPRESS=y
87+
88+
config PSTORE_842_COMPRESS_DEFAULT
89+
bool "842" if PSTORE_842_COMPRESS=y
90+
6291
endchoice
6392

93+
config PSTORE_COMPRESS_DEFAULT
94+
string
95+
depends on PSTORE_COMPRESS
96+
default "zlib" if PSTORE_ZLIB_COMPRESS_DEFAULT
97+
default "lzo" if PSTORE_LZO_COMPRESS_DEFAULT
98+
default "lz4" if PSTORE_LZ4_COMPRESS_DEFAULT
99+
default "lz4hc" if PSTORE_LZ4HC_COMPRESS_DEFAULT
100+
default "842" if PSTORE_842_COMPRESS_DEFAULT
101+
64102
config PSTORE_CONSOLE
65103
bool "Log kernel console messages"
66104
depends on PSTORE

fs/pstore/inode.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,8 @@ static int __init init_pstore_fs(void)
486486
{
487487
int err;
488488

489+
pstore_choose_compression();
490+
489491
/* Create a convenient mount point for people to access pstore */
490492
err = sysfs_create_mount_point(fs_kobj, "pstore");
491493
if (err)

fs/pstore/internal.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,7 @@ extern bool pstore_is_mounted(void);
3737
extern void pstore_record_init(struct pstore_record *record,
3838
struct pstore_info *psi);
3939

40+
/* Called during module_init() */
41+
extern void __init pstore_choose_compression(void);
42+
4043
#endif

fs/pstore/platform.c

Lines changed: 79 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,24 @@ static DEFINE_SPINLOCK(pstore_lock);
7777
struct pstore_info *psinfo;
7878

7979
static char *backend;
80+
static char *compress =
81+
#ifdef CONFIG_PSTORE_COMPRESS_DEFAULT
82+
CONFIG_PSTORE_COMPRESS_DEFAULT;
83+
#else
84+
NULL;
85+
#endif
8086

8187
/* Compression parameters */
8288
#ifdef CONFIG_PSTORE_ZLIB_COMPRESS
8389
#define COMPR_LEVEL 6
8490
#define WINDOW_BITS 12
8591
#define MEM_LEVEL 4
8692
static struct z_stream_s stream;
87-
#else
93+
#endif
94+
#if defined(CONFIG_PSTORE_LZO_COMPRESS) || \
95+
defined(CONFIG_PSTORE_LZ4_COMPRESS) || \
96+
defined(CONFIG_PSTORE_LZ4HC_COMPRESS) || \
97+
defined(CONFIG_PSTORE_842_COMPRESS)
8898
static unsigned char *workspace;
8999
#endif
90100

@@ -268,14 +278,6 @@ static void free_zlib(void)
268278
big_oops_buf = NULL;
269279
big_oops_buf_sz = 0;
270280
}
271-
272-
static const struct pstore_zbackend backend_zlib = {
273-
.compress = compress_zlib,
274-
.decompress = decompress_zlib,
275-
.allocate = allocate_zlib,
276-
.free = free_zlib,
277-
.name = "zlib",
278-
};
279281
#endif
280282

281283
#ifdef CONFIG_PSTORE_LZO_COMPRESS
@@ -329,14 +331,6 @@ static void free_lzo(void)
329331
big_oops_buf = NULL;
330332
big_oops_buf_sz = 0;
331333
}
332-
333-
static const struct pstore_zbackend backend_lzo = {
334-
.compress = compress_lzo,
335-
.decompress = decompress_lzo,
336-
.allocate = allocate_lzo,
337-
.free = free_lzo,
338-
.name = "lzo",
339-
};
340334
#endif
341335

342336
#if defined(CONFIG_PSTORE_LZ4_COMPRESS) || defined(CONFIG_PSTORE_LZ4HC_COMPRESS)
@@ -396,14 +390,6 @@ static void allocate_lz4(void)
396390
workspace = NULL;
397391
}
398392
}
399-
400-
static const struct pstore_zbackend backend_lz4 = {
401-
.compress = compress_lz4,
402-
.decompress = decompress_lz4,
403-
.allocate = allocate_lz4,
404-
.free = free_lz4,
405-
.name = "lz4",
406-
};
407393
#endif
408394

409395
#ifdef CONFIG_PSTORE_LZ4HC_COMPRESS
@@ -438,14 +424,6 @@ static void allocate_lz4hc(void)
438424
workspace = NULL;
439425
}
440426
}
441-
442-
static const struct pstore_zbackend backend_lz4hc = {
443-
.compress = compress_lz4hc,
444-
.decompress = decompress_lz4,
445-
.allocate = allocate_lz4hc,
446-
.free = free_lz4,
447-
.name = "lz4hc",
448-
};
449427
#endif
450428

451429
#ifdef CONFIG_PSTORE_842_COMPRESS
@@ -508,30 +486,58 @@ static void free_842(void)
508486
big_oops_buf = NULL;
509487
big_oops_buf_sz = 0;
510488
}
511-
512-
static const struct pstore_zbackend backend_842 = {
513-
.compress = compress_842,
514-
.decompress = decompress_842,
515-
.allocate = allocate_842,
516-
.free = free_842,
517-
.name = "842",
518-
};
519489
#endif
520490

521-
static const struct pstore_zbackend *zbackend =
522-
#if defined(CONFIG_PSTORE_ZLIB_COMPRESS)
523-
&backend_zlib;
524-
#elif defined(CONFIG_PSTORE_LZO_COMPRESS)
525-
&backend_lzo;
526-
#elif defined(CONFIG_PSTORE_LZ4_COMPRESS)
527-
&backend_lz4;
528-
#elif defined(CONFIG_PSTORE_LZ4HC_COMPRESS)
529-
&backend_lz4hc;
530-
#elif defined(CONFIG_PSTORE_842_COMPRESS)
531-
&backend_842;
532-
#else
533-
NULL;
491+
static const struct pstore_zbackend *zbackend __ro_after_init;
492+
493+
static const struct pstore_zbackend zbackends[] = {
494+
#ifdef CONFIG_PSTORE_ZLIB_COMPRESS
495+
{
496+
.compress = compress_zlib,
497+
.decompress = decompress_zlib,
498+
.allocate = allocate_zlib,
499+
.free = free_zlib,
500+
.name = "zlib",
501+
},
502+
#endif
503+
#ifdef CONFIG_PSTORE_LZO_COMPRESS
504+
{
505+
.compress = compress_lzo,
506+
.decompress = decompress_lzo,
507+
.allocate = allocate_lzo,
508+
.free = free_lzo,
509+
.name = "lzo",
510+
},
511+
#endif
512+
#ifdef CONFIG_PSTORE_LZ4_COMPRESS
513+
{
514+
.compress = compress_lz4,
515+
.decompress = decompress_lz4,
516+
.allocate = allocate_lz4,
517+
.free = free_lz4,
518+
.name = "lz4",
519+
},
520+
#endif
521+
#ifdef CONFIG_PSTORE_LZ4HC_COMPRESS
522+
{
523+
.compress = compress_lz4hc,
524+
.decompress = decompress_lz4,
525+
.allocate = allocate_lz4hc,
526+
.free = free_lz4,
527+
.name = "lz4hc",
528+
},
534529
#endif
530+
#ifdef CONFIG_PSTORE_842_COMPRESS
531+
{
532+
.compress = compress_842,
533+
.decompress = decompress_842,
534+
.allocate = allocate_842,
535+
.free = free_842,
536+
.name = "842",
537+
},
538+
#endif
539+
{ }
540+
};
535541

536542
static int pstore_compress(const void *in, void *out,
537543
size_t inlen, size_t outlen)
@@ -553,7 +559,6 @@ static int pstore_decompress(void *in, void *out, size_t inlen, size_t outlen)
553559
static void allocate_buf_for_compression(void)
554560
{
555561
if (zbackend) {
556-
pr_info("using %s compression\n", zbackend->name);
557562
zbackend->allocate();
558563
} else {
559564
pr_err("allocate compression buffer error!\n");
@@ -1022,5 +1027,24 @@ static void pstore_timefunc(struct timer_list *unused)
10221027
jiffies + msecs_to_jiffies(pstore_update_ms));
10231028
}
10241029

1030+
void __init pstore_choose_compression(void)
1031+
{
1032+
const struct pstore_zbackend *step;
1033+
1034+
if (!compress)
1035+
return;
1036+
1037+
for (step = zbackends; step->name; step++) {
1038+
if (!strcmp(compress, step->name)) {
1039+
zbackend = step;
1040+
pr_info("using %s compression\n", zbackend->name);
1041+
return;
1042+
}
1043+
}
1044+
}
1045+
1046+
module_param(compress, charp, 0444);
1047+
MODULE_PARM_DESC(compress, "Pstore compression to use");
1048+
10251049
module_param(backend, charp, 0444);
10261050
MODULE_PARM_DESC(backend, "Pstore backend to use");

0 commit comments

Comments
 (0)