Skip to content

Added opt-in lazy sector release to reduce O(N²) deletion (issue #70)#71

Merged
fdesbiens merged 1 commit into
eclipse-threadx:devfrom
fdesbiens:fix/issue-70-lazy-sector-release
Jun 2, 2026
Merged

Added opt-in lazy sector release to reduce O(N²) deletion (issue #70)#71
fdesbiens merged 1 commit into
eclipse-threadx:devfrom
fdesbiens:fix/issue-70-lazy-sector-release

Conversation

@fdesbiens
Copy link
Copy Markdown
Contributor

WARNING: This feature is brand new and has not been tested beyond the existing regression suite. It should be thoroughly tested — including stress, power-loss, and wear-levelling scenarios — before being used in any production system.

When LX_NAND_FLASH_ENABLE_LAZY_SECTOR_RELEASE is defined and free blocks exceed LX_NAND_FLASH_SECTOR_RELEASE_LAZY_THRESHOLD (default 10), releasing a sector from a full block defers the copy+erase. A tombstone page is written to a freshly-allocated block; the old full block is flagged COMPACTION_PENDING in its block status and recorded in a new per-LG compaction table. Compaction (merge + erase) is triggered lazily:

  • sector_write: before writing to a full block with a pending source
  • block_data_move: intercepts wear-levelling moves for pending LGs
  • defragment: iterates all pending LGs (now actually implemented)
  • block_allocate: emergency compaction when the free list is empty

Reads transparently fall back to the compaction-pending block when the primary block does not contain the requested sector.

Crash recovery is handled in open_extended:

  • Phase 1: detects COMPACTION_PENDING blocks and either clears the flag (crash before mapping update — abort) or rebuilds compaction_table (crash after mapping update — resume deferred compaction).
  • Phase 2: erases orphaned ALLOCATED blocks not present in any mapping.

Without the flag, all paths remain unchanged and defragment returns LX_NOT_SUPPORTED as before.

Fixes: #70

…pse-threadx#70)

WARNING: This feature is brand new and has not been tested beyond the
existing regression suite.  It should be thoroughly tested — including
stress, power-loss, and wear-levelling scenarios — before being used in
any production system.

When LX_NAND_FLASH_ENABLE_LAZY_SECTOR_RELEASE is defined and free blocks
exceed LX_NAND_FLASH_SECTOR_RELEASE_LAZY_THRESHOLD (default 10), releasing
a sector from a full block defers the copy+erase.  A tombstone page is
written to a freshly-allocated block; the old full block is flagged
COMPACTION_PENDING in its block status and recorded in a new per-LG
compaction table.  Compaction (merge + erase) is triggered lazily:

  * sector_write: before writing to a full block with a pending source
  * block_data_move: intercepts wear-levelling moves for pending LGs
  * defragment: iterates all pending LGs (now actually implemented)
  * block_allocate: emergency compaction when the free list is empty

Reads transparently fall back to the compaction-pending block when the
primary block does not contain the requested sector.

Crash recovery is handled in open_extended:
  - Phase 1: detects COMPACTION_PENDING blocks and either clears the flag
    (crash before mapping update — abort) or rebuilds compaction_table
    (crash after mapping update — resume deferred compaction).
  - Phase 2: erases orphaned ALLOCATED blocks not present in any mapping.

Without the flag, all paths remain unchanged and defragment returns
LX_NOT_SUPPORTED as before.

Fixes: eclipse-threadx#70

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@fdesbiens fdesbiens changed the base branch from master to dev June 2, 2026 15:24
@fdesbiens fdesbiens merged commit bfb3967 into eclipse-threadx:dev Jun 2, 2026
2 checks passed
@fdesbiens fdesbiens deleted the fix/issue-70-lazy-sector-release branch June 2, 2026 15:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Behavioral change/Performance issue in lx_nand_flash_sector_release.

1 participant