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
+69
View File
@@ -211,6 +211,9 @@ enum lfs3_type {
#define LFS3_F_LOOKAHEAD \
0x00000200 // Repopulate lookahead/gbmap
#endif
#if !defined(LFS3_RDONLY) && defined(LFS3_GBMAP) && !defined(LFS3_NO_PREERASE)
#define LFS3_F_PREERASE 0x00000400 // Try to pre-erase free blocks
#endif
#ifndef LFS3_RDONLY
#define LFS3_F_COMPACT 0x00000800 // Compact metadata logs
#endif
@@ -228,6 +231,9 @@ enum lfs3_type {
#define LFS3_F_GC ( \
LFS3_IFDEF_RDONLY(0, LFS3_F_MKCONSISTENT) \
| LFS3_IFDEF_RDONLY(0, LFS3_F_LOOKAHEAD) \
| LFS3_IFDEF_RDONLY(0, \
LFS3_IFDEF_GBMAP( \
LFS3_IFDEF_PREERASE(LFS3_F_PREERASE, 0), 0)) \
| LFS3_IFDEF_RDONLY(0, LFS3_F_COMPACT) \
| LFS3_F_CKMETA \
| LFS3_F_CKDATA)
@@ -269,6 +275,9 @@ enum lfs3_type {
#define LFS3_M_LOOKAHEAD \
0x00000200 // Repopulate lookahead/gbmap
#endif
#if !defined(LFS3_RDONLY) && defined(LFS3_GBMAP) && !defined(LFS3_NO_PREERASE)
#define LFS3_M_PREERASE 0x00000400 // Try to pre-erase free blocks
#endif
#ifndef LFS3_RDONLY
#define LFS3_M_COMPACT 0x00000800 // Compact metadata logs
#endif
@@ -282,6 +291,9 @@ enum lfs3_type {
#define LFS3_M_GC ( \
LFS3_IFDEF_RDONLY(0, LFS3_M_MKCONSISTENT) \
| LFS3_IFDEF_RDONLY(0, LFS3_M_LOOKAHEAD) \
| LFS3_IFDEF_RDONLY(0, \
LFS3_IFDEF_GBMAP( \
LFS3_IFDEF_PREERASE(LFS3_M_PREERASE, 0), 0)) \
| LFS3_IFDEF_RDONLY(0, LFS3_M_COMPACT) \
| LFS3_M_CKMETA \
| LFS3_M_CKDATA)
@@ -322,6 +334,9 @@ enum lfs3_type {
#define LFS3_I_LOOKAHEAD \
0x00000200 // Lookahead/gbmap is not full
#endif
#if !defined(LFS3_RDONLY) && defined(LFS3_GBMAP) && !defined(LFS3_NO_PREERASE)
#define LFS3_I_PREERASE 0x00000400 // Blocks can be pre-erased
#endif
#ifndef LFS3_RDONLY
#define LFS3_I_COMPACT 0x00000800 // Filesystem may have uncompacted metadata
#endif
@@ -352,6 +367,9 @@ enum lfs3_btype {
#define LFS3_T_LOOKAHEAD \
0x00000200 // Repopulate lookahead/gbmap
#endif
#if !defined(LFS3_RDONLY) && defined(LFS3_GBMAP) && !defined(LFS3_NO_PREERASE)
#define LFS3_T_PREERASE 0x00000400 // Try to pre-erase free blocks
#endif
#ifndef LFS3_RDONLY
#define LFS3_T_COMPACT 0x00000800 // Compact metadata logs
#endif
@@ -374,6 +392,9 @@ enum lfs3_btype {
#define LFS3_T_GC ( \
LFS3_IFDEF_RDONLY(0, LFS3_T_MKCONSISTENT) \
| LFS3_IFDEF_RDONLY(0, LFS3_T_LOOKAHEAD) \
| LFS3_IFDEF_RDONLY(0, \
LFS3_IFDEF_GBMAP( \
LFS3_IFDEF_PREERASE(LFS3_T_PREERASE, 0), 0)) \
| LFS3_IFDEF_RDONLY(0, LFS3_T_COMPACT) \
| LFS3_T_CKMETA \
| LFS3_T_CKDATA)
@@ -387,6 +408,10 @@ enum lfs3_btype {
#define LFS3_CK_LOOKAHEAD \
0x00000200 // Repopulate lookahead/gbmap
#endif
#if !defined(LFS3_RDONLY) && defined(LFS3_GBMAP) && !defined(LFS3_NO_PREERASE)
#define LFS3_CK_PREERASE \
0x00000400 // Try to pre-erase free blocks
#endif
#ifndef LFS3_RDONLY
#define LFS3_CK_COMPACT 0x00000800 // Compact metadata logs
#endif
@@ -400,6 +425,9 @@ enum lfs3_btype {
#define LFS3_CK_GC ( \
LFS3_IFDEF_RDONLY(0, LFS3_CK_MKCONSISTENT) \
| LFS3_IFDEF_RDONLY(0, LFS3_CK_LOOKAHEAD) \
| LFS3_IFDEF_RDONLY(0, \
LFS3_IFDEF_GBMAP( \
LFS3_IFDEF_PREERASE(LFS3_CK_PREERASE, 0), 0)) \
| LFS3_IFDEF_RDONLY(0, LFS3_CK_COMPACT) \
| LFS3_CK_CKMETA \
| LFS3_CK_CKDATA)
@@ -413,6 +441,10 @@ enum lfs3_btype {
#define LFS3_GC_LOOKAHEAD \
0x00000200 // Repopulate lookahead/gbmap
#endif
#if !defined(LFS3_RDONLY) && defined(LFS3_GBMAP) && !defined(LFS3_NO_PREERASE)
#define LFS3_GC_PREERASE \
0x00000400 // Try to pre-erase free blocks
#endif
#ifndef LFS3_RDONLY
#define LFS3_GC_COMPACT 0x00000800 // Compact metadata logs
#endif
@@ -426,6 +458,9 @@ enum lfs3_btype {
#define LFS3_GC_GC ( \
LFS3_IFDEF_RDONLY(0, LFS3_GC_MKCONSISTENT) \
| LFS3_IFDEF_RDONLY(0, LFS3_GC_LOOKAHEAD) \
| LFS3_IFDEF_RDONLY(0, \
LFS3_IFDEF_GBMAP( \
LFS3_IFDEF_PREERASE(LFS3_GC_PREERASE, 0), 0)) \
| LFS3_IFDEF_RDONLY(0, LFS3_GC_COMPACT) \
| LFS3_GC_CKMETA \
| LFS3_GC_CKDATA)
@@ -577,6 +612,22 @@ struct lfs3_cfg {
lfs3_block_t gc_lookgbmap_thresh;
#endif
// Number of blocks to try to pre-erase during gc. When erase is
// expensive (flash), pre-erasing blocks can help reduce the latency
// of block allocation.
//
// Requires the gbmap to track pre-erased blocks.
//
// 0 only erases blocks immediately before prog, while -1 or any
// value >= block_count attempts to pre-erase all known free blocks
// during gc.
//
#if !defined(LFS3_RDONLY) \
&& defined(LFS3_GBMAP) \
&& !defined(LFS3_NO_PREERASE)
lfs3_block_t gc_preerase_count;
#endif
// Threshold for metadata compaction during gc in bytes.
//
// Metadata logs that exceed this threshold will be compacted during
@@ -1083,6 +1134,15 @@ typedef struct lfs3_bptr {
#endif
} lfs3_bptr_t;
// erased-state checksum
#ifndef LFS3_RDONLY
typedef struct lfs3_ecksum {
// cksize=-1 indicates no ecksum
lfs3_ssize_t cksize;
uint32_t cksum;
} lfs3_ecksum_t;
#endif
// littlefs's core metadata log type
typedef struct lfs3_rbyd {
lfs3_rid_t weight;
@@ -1317,7 +1377,16 @@ typedef struct lfs3 {
struct lfs3_gbmap {
lfs3_block_t window;
lfs3_block_t known;
#if !defined(LFS3_RDONLY)
lfs3_sblock_t free;
#endif
#if !defined(LFS3_RDONLY) && !defined(LFS3_NO_PREERASE)
lfs3_ecksum_t ecksum;
struct lfs3_preeraser {
lfs3_block_t known;
lfs3_block_t count;
} preeraser;
#endif
lfs3_btree_t b;
lfs3_btree_t b_p;
} gbmap;