linux/fs/pstore
Ard Biesheuvel 9416006239 pstore: Base compression input buffer size on estimated compressed size
Commit 1756ddea69 ("pstore: Remove worst-case compression size logic")
removed some clunky per-algorithm worst case size estimation routines on
the basis that we can always store pstore records uncompressed, and
these worst case estimations are about how much the size might
inadvertently *increase* due to encapsulation overhead when the input
cannot be compressed at all. So if compression results in a size
increase, we just store the original data instead.

However, it seems that the original code was misinterpreting these
calculations as an estimation of how much uncompressed data might fit
into a compressed buffer of a given size, and it was using the results
to consume the input data in larger chunks than the pstore record size,
relying on the compression to ensure that what ultimately gets stored
fits into the available space.

One result of this, as observed and reported by Linus, is that upgrading
to a newer kernel that includes the given commit may result in pstore
decompression errors reported in the kernel log. This is due to the fact
that the existing records may unexpectedly decompress to a size that is
larger than the pstore record size.

Another potential problem caused by this change is that we may
underutilize the fixed sized records on pstore backends such as ramoops.
And on pstore backends with variable sized records such as EFI, we will
end up creating many more entries than before to store the same amount
of compressed data.

So let's fix both issues, by bringing back the typical case estimation of
how much ASCII text captured from the dmesg log might fit into a pstore
record of a given size after compression. The original implementation
used the computation given below for zlib:

  switch (size) {
  /* buffer range for efivars */
  case 1000 ... 2000:
  	cmpr = 56;
  	break;
  case 2001 ... 3000:
  	cmpr = 54;
  	break;
  case 3001 ... 3999:
  	cmpr = 52;
  	break;
  /* buffer range for nvram, erst */
  case 4000 ... 10000:
  	cmpr = 45;
  	break;
  default:
  	cmpr = 60;
  	break;
  }

  return (size * 100) / cmpr;

We will use the previous worst-case of 60% for compression. For
decompression go extra large (3x) so we make sure there's enough space
for anything.

While at it, rate limit the error message so we don't flood the log
unnecessarily on systems that have accumulated a lot of pstore history.

Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Eric Biggers <ebiggers@kernel.org>
Cc: Kees Cook <keescook@chromium.org>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Link: https://lore.kernel.org/r/20230830212238.135900-1-ardb@kernel.org
Co-developed-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Kees Cook <keescook@chromium.org>
2023-08-31 13:58:49 -07:00
..
blk.c init: improve the name_to_dev_t interface 2023-06-05 10:56:46 -06:00
ftrace.c pstore/ftrace: Allow immediate recording 2021-11-18 10:29:52 -08:00
inode.c pstore: Support record sizes larger than kmalloc() limit 2023-08-17 15:18:24 -07:00
internal.h pstore: Move kmsg_bytes default into Kconfig 2020-12-01 12:09:17 -08:00
Kconfig pstore: Replace crypto API compression with zlib_deflate library calls 2023-07-17 16:08:58 -07:00
Makefile pstore/blk: Introduce backend for block devices 2020-05-30 10:34:03 -07:00
platform.c pstore: Base compression input buffer size on estimated compressed size 2023-08-31 13:58:49 -07:00
pmsg.c pstore update for v6.4-rc1 2023-04-27 17:03:40 -07:00
ram_core.c pstore: Fix kernel-doc warning 2023-08-18 13:27:28 -07:00
ram_internal.h pstore/ram: Set freed addresses to NULL 2022-10-19 09:25:39 -07:00
ram.c pstore: Support record sizes larger than kmalloc() limit 2023-08-17 15:18:24 -07:00
zone.c pstore/zone: Use GFP_ATOMIC to allocate zone buffer 2022-10-17 13:14:10 -07:00