preerase: Implemented the gc side of preerase

Allocating pre-erased blocks gets quite complicated due to our
restricted flash model, but at least the actual pre-erasing is
relatively straightforward:

- We keep track of known preerased state in lfs3->gbmap.preeraser.

- If LFS3_GC_PREERASE is provided during gc work, we increment the
  preeraser's known window by scanning the gbmap.

- Any BMFREE ranges we find, we erase a block at a time, and store the
  resulting ecksum in a BMERASED range in the gbmap.

- We keep track of how many blocks we erased, and stop early if this
  exceeds cfg.gc_preerase_count. This just lets users tune how many
  blocks to preerase in case something (?) prevents preerased blocks
  from being used.

Some notes:

- We don't really do anything with ranges in lfs3_alloc_preerase. In
  theory we could bulk in erase to minimize the number of commits to the
  gbmap, but we expect erase to dominate, so this probably isn't worth
  it.

  And if erase doesn't dominate, why would you bother pre-erasing
  blocks?

- Preerasing isn't really a traversal operation, and is managed by a
  sort of secondary state machine in lfs3_fs_gc_.

  This also means lfs3_trv_read with LFS3_T_PREERASE does nothing, but I
  guess that is ok? It's tempting to try to make lfs3_trv_read also
  preerase, but it's unclear what block it should return -- it's
  probably the wrong API.

- Introducing ecksums actually went quite a bit smoother than I
  expected. Though it helps ecksums are the only optional payload, no
  type punning or anything.

  Ecksums do muddy the gbmap's design a bit, unfortunately. The main
  issue being that we can only merge BMERASED ranges with equal ecksums.
  This makes BMERASED ranges less compressable than the others, and may
  be one reason to limit cfg.gc_preerase_count.

  However:

  1. This is where I think it's useful to emphasize that the gbmap's
     responsibility is to track _free_ blocks, in-use blocks are
     secondary.

     When allocating, we're going to stop at the first BMFREE/BMERASED,
     but may need to skip over an unbounded number of BMINUSE/BMBAD
     blocks. So the compressability of BMFREE/BMERASED ranges should
     have less of an impact on block allocation.

  2. In practice, most flash uses consistent erase values, so the
     resulting ecksums will probably be compressable. The exceptions are
     noop-erases (SD/eMMC, RAM, NVRAM, etc), and encryption with block
     address permutation?

     Though noop-erases are a pretty big exception.

Code changes:

                    code          stack          ctx
  before:          35116           2136          660
  after:           35116 (+0.0%)   2136 (+0.0%)  660 (+0.0%)

                    code          stack          ctx
  gbmap+np before: 38040           2136          776
  gbmap+np after:  38188 (+0.4%)   2144 (+0.4%)  776 (+0.0%)

                    code          stack          ctx
  gbmap+yp before: 38040           2136          776
  gbmap+yp after:  38608 (+1.5%)   2144 (+0.4%)  796 (+2.6%)
This commit is contained in:
Christopher Haster
2025-12-08 01:51:41 -06:00
parent f05be19d0e
commit 843412cc79
7 changed files with 1049 additions and 201 deletions
+9
View File
@@ -118,6 +118,7 @@ void bench_permutation(size_t i, uint32_t *buffer, size_t size);
BENCH_DEFINE(GC_STEPS, 0 ) \
BENCH_DEFINE(GC_LOOKAHEAD_THRESH, -1 ) \
BENCH_DEFINE(GC_LOOKGBMAP_THRESH, -1 ) \
BENCH_DEFINE(GC_PREERASE_COUNT, -1 ) \
BENCH_DEFINE(GC_COMPACT_THRESH, 0 ) \
BENCH_DEFINE(SHRUB_SIZE, BLOCK_SIZE/4 ) \
BENCH_DEFINE(FRAGMENT_SIZE, LFS3_MIN(BLOCK_SIZE/8, 512) ) \
@@ -148,6 +149,7 @@ void bench_permutation(size_t i, uint32_t *buffer, size_t size);
.fcache_size = FCACHE_SIZE, \
.lookahead_size = LOOKAHEAD_SIZE, \
BENCH_GBMAP_CFG \
BENCH_PREERASE_CFG \
BENCH_GC_CFG \
.gc_lookahead_thresh = GC_LOOKAHEAD_THRESH, \
.gc_compact_thresh = GC_COMPACT_THRESH, \
@@ -163,6 +165,13 @@ void bench_permutation(size_t i, uint32_t *buffer, size_t size);
#define BENCH_GBMAP_CFG
#endif
#if defined(LFS3_GBMAP) && !defined(LFS3_NO_PREERASE)
#define BENCH_PREERASE_CFG \
.gc_preerase_count = GC_PREERASE_COUNT,
#else
#define BENCH_PREERASE_CFG
#endif
#ifdef LFS3_GC
#define BENCH_GC_CFG \
.gc_flags = GC_FLAGS, \