Skip to content

Commit 95047b0

Browse files
committed
pstore: Refactor compression initialization
This refactors compression initialization slightly to better handle getting potentially called twice (via early pstore_register() calls and later pstore_init()) and improves the comments and reporting to be more verbose. Signed-off-by: Kees Cook <keescook@chromium.org> Tested-by: Guenter Roeck <groeck@chromium.org>
1 parent 4160316 commit 95047b0

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

fs/pstore/platform.c

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -274,36 +274,56 @@ static int pstore_decompress(void *in, void *out,
274274

275275
static void allocate_buf_for_compression(void)
276276
{
277+
struct crypto_comp *ctx;
278+
int size;
279+
char *buf;
280+
281+
/* Skip if not built-in or compression backend not selected yet. */
277282
if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !zbackend)
278283
return;
279284

285+
/* Skip if no pstore backend yet or compression init already done. */
286+
if (!psinfo || tfm)
287+
return;
288+
280289
if (!crypto_has_comp(zbackend->name, 0, 0)) {
281-
pr_err("No %s compression\n", zbackend->name);
290+
pr_err("Unknown compression: %s\n", zbackend->name);
282291
return;
283292
}
284293

285-
big_oops_buf_sz = zbackend->zbufsize(psinfo->bufsize);
286-
if (big_oops_buf_sz <= 0)
294+
size = zbackend->zbufsize(psinfo->bufsize);
295+
if (size <= 0) {
296+
pr_err("Invalid compression size for %s: %d\n",
297+
zbackend->name, size);
287298
return;
299+
}
288300

289-
big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL);
290-
if (!big_oops_buf) {
291-
pr_err("allocate compression buffer error!\n");
301+
buf = kmalloc(size, GFP_KERNEL);
302+
if (!buf) {
303+
pr_err("Failed %d byte compression buffer allocation for: %s\n",
304+
size, zbackend->name);
292305
return;
293306
}
294307

295-
tfm = crypto_alloc_comp(zbackend->name, 0, 0);
296-
if (IS_ERR_OR_NULL(tfm)) {
297-
kfree(big_oops_buf);
298-
big_oops_buf = NULL;
299-
pr_err("crypto_alloc_comp() failed!\n");
308+
ctx = crypto_alloc_comp(zbackend->name, 0, 0);
309+
if (IS_ERR_OR_NULL(ctx)) {
310+
kfree(buf);
311+
pr_err("crypto_alloc_comp('%s') failed: %ld\n", zbackend->name,
312+
PTR_ERR(ctx));
300313
return;
301314
}
315+
316+
/* A non-NULL big_oops_buf indicates compression is available. */
317+
tfm = ctx;
318+
big_oops_buf_sz = size;
319+
big_oops_buf = buf;
320+
321+
pr_info("Using compression: %s\n", zbackend->name);
302322
}
303323

304324
static void free_buf_for_compression(void)
305325
{
306-
if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && !IS_ERR_OR_NULL(tfm))
326+
if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && tfm)
307327
crypto_free_comp(tfm);
308328
kfree(big_oops_buf);
309329
big_oops_buf = NULL;
@@ -774,7 +794,6 @@ void __init pstore_choose_compression(void)
774794
for (step = zbackends; step->name; step++) {
775795
if (!strcmp(compress, step->name)) {
776796
zbackend = step;
777-
pr_info("using %s compression\n", zbackend->name);
778797
return;
779798
}
780799
}
@@ -791,8 +810,7 @@ static int __init pstore_init(void)
791810
* initialize compression because crypto was not ready. If so,
792811
* initialize compression now.
793812
*/
794-
if (psinfo && !tfm)
795-
allocate_buf_for_compression();
813+
allocate_buf_for_compression();
796814

797815
ret = pstore_init_fs();
798816
if (ret)

0 commit comments

Comments
 (0)