From 3aff97d647f17573016fbddbddb6dcb3334cdd17 Mon Sep 17 00:00:00 2001 From: Shayne <8741557+shaynelegault@users.noreply.github.com> Date: Wed, 18 Nov 2026 21:38:07 -0600 Subject: [PATCH 01/11] mtd: port the P105 checkpoint planner to Orchard ARMv7 Export the offline SwissPPNFTL checkpoint and reclamation work from the merged ARMv7 append writer. Carry the kernel changes in the ARMv7 MTD series and document the current append-only behavior and remaining runtime work. Assisted-by: LLM --- chips/s5l8942x.toml | 1 + devices/p105/device.toml | 1 + docs/bring-up/p105-sftl-checkpoint-gc.md | 50 + ...dd-offline-checkpoint-encoding-and-G.patch | 1303 +++++++++++++++++ ...ve-PPN-erase-addressing-and-VFL-rota.patch | 59 + 5 files changed, 1414 insertions(+) create mode 100644 docs/bring-up/p105-sftl-checkpoint-gc.md create mode 100644 kernel/patches/armv7/mtd/0006-mtd-apple-sftl-Add-offline-checkpoint-encoding-and-G.patch create mode 100644 kernel/patches/armv7/mtd/0007-docs-Record-native-PPN-erase-addressing-and-VFL-rota.patch diff --git a/chips/s5l8942x.toml b/chips/s5l8942x.toml index f8d63b5c..6c46e961 100644 --- a/chips/s5l8942x.toml +++ b/chips/s5l8942x.toml @@ -17,4 +17,5 @@ stages = "Project Cascadia P105 boot chain" reference_version = "Project Cascadia builds its A5 boot chain from 8.4.1 / 12H321" nand_reads = "Measured P105 64 GB PPN geometry has eight CAUs per bus, 2088 blocks per CAU and 128 normal pages per block; exact ARMv7 read-topic qualification is pending (docs/bring-up/p105-nand-reads.md)." nand_program = "The H2FMI driver programs complete PPN pages only into erased destinations, verifies full readback and has no erase operation; hardware qualification of this revision is pending (docs/bring-up/p105-nand-program.md)." +nand_checkpoint_gc = "The 64 GB P105 SwissPPNFTL planner checks relocation and checkpoint results offline. Physical erase and reuse await qualification (docs/bring-up/p105-sftl-checkpoint-gc.md)." usb_phy = "P105 uses a 24 MHz reference; the proposed S5L8942X cold-handoff callbacks await an exact-revision boot (docs/bring-up/p105-usb-handoff.md)." diff --git a/devices/p105/device.toml b/devices/p105/device.toml index 97fc3301..59eff59a 100644 --- a/devices/p105/device.toml +++ b/devices/p105/device.toml @@ -29,6 +29,7 @@ recipe = "firmware.json" # (OrchardLinux/orchard#93), freed space is never reused and writes fail once # the blank flash runs out. Drop this table when it lands; the installer # warns from it and allows the install either way. +# The offline checkpoint planner is described in docs/bring-up/p105-sftl-checkpoint-gc.md. [storage] reclaims = false diff --git a/docs/bring-up/p105-sftl-checkpoint-gc.md b/docs/bring-up/p105-sftl-checkpoint-gc.md new file mode 100644 index 00000000..debbe707 --- /dev/null +++ b/docs/bring-up/p105-sftl-checkpoint-gc.md @@ -0,0 +1,50 @@ +# P105 SwissPPNFTL checkpoint encoder and reclamation planner + +The 64 GB P105 shares one flash translation layer across the native iOS and +Linux partitions. The append writer returns `ENOSPC` after it exhausts +verified blank blocks. Orchard's installer warns about that limit and allows +installation. See the [append writer](p105-sftl-append.md) and +[P105 device profile](../../devices/p105/device.toml). + +## Kernel source + +The port uses the merged append writer on +[OrchardLinux/linux armv7 at 0c650327](https://github.com/OrchardLinux/linux/commit/0c6503279c702b44a340c37f072cd9170d6bf4a4). +The [ported kernel source](https://github.com/shaynelegault/linux/commit/e823a434a766b09c5e5b2ad4586b7a27085b1828) +contains the planner and its fixtures. The kernel patches live in `kernel/patches/armv7/mtd/`, selected by +`kernel/series/apple-armv7`. The original planner and native rotation +notes are in [the source draft at c504eae2](https://github.com/shaynelegault/linux/commit/c504eae2a214915dcf5d2c4856c52e7a6ac79d2d). + +The snapshot covers all 15,625,016 logical keys, including the sixteen +internal keys beyond the exported disk. The encoder preserves the opaque +accounting records, updates the context-block list and serializes range +fragments with 48-bit sequence numbers. The planner budgets destination +space for live data and the block footer. It checks copy and checkpoint +readback results supplied by its caller, then compares the whole map from +a fresh recovery against the planned map. These helpers operate offline. + +## Open work + +- The runtime writer needs authoritative VFL and context ownership data to + select blocks and lanes, verify relocated data, and return an erased block + to the allocator. +- Installing a checkpoint requires native updates to accounting fields. + The reader needs to select the previous complete checkpoint and replay + interrupted saves when the newest checkpoint is incomplete. +- The native VFL rotation switches banks in two persisted steps. An initial + checkpoint retains the old table references while the tables are copied. + A final checkpoint publishes the new references. Recovery after a torn + copy or a block retirement still needs qualification. +- A controller erase and reuse cycle still needs a test on the measured + hardware, including the physical erase extent, the relationship between + normal and SLC views, and recovery after partial completion. The P105 + profile records both storage reads and writes as `untested`. + +The native format evidence comes from IOFlashStorage-730 on build 13G36 +and the [pinned Cascadia NAND research](https://github.com/rzxvx/Project-Cascadia/blob/c20b85ebb951c704d7e3a664c378dde8276e3928/docs/research/p105-nand.md). +On the ported source, the host sanitizer run passed 22 reader/writer cases +and eight planner cases. The differential tests matched 24 native metadata +vectors and round-tripped 209 encoded maps. Five ARMv7 objects compiled +with warnings treated as errors, including both KUnit fixtures. These checks +used synthetic pages and covered encoding, receipt validation and cold map +recovery. Physical erase and reboot recovery remain untested. diff --git a/kernel/patches/armv7/mtd/0006-mtd-apple-sftl-Add-offline-checkpoint-encoding-and-G.patch b/kernel/patches/armv7/mtd/0006-mtd-apple-sftl-Add-offline-checkpoint-encoding-and-G.patch new file mode 100644 index 00000000..88b5abcc --- /dev/null +++ b/kernel/patches/armv7/mtd/0006-mtd-apple-sftl-Add-offline-checkpoint-encoding-and-G.patch @@ -0,0 +1,1303 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Shayne <8741557+shaynelegault@users.noreply.github.com> +Date: Wed, 18 Nov 2026 21:34:11 -0600 +Subject: [PATCH] mtd: apple-sftl: Add offline checkpoint encoding and GC + planning + +Snapshot the recovered whole-device map, encode native context and range records, and check relocation, checkpoint readback and cold-recovery receipts in a bounded offline transaction. Add synthetic KUnit cases for the planner and a round trip through the built-in reader. + +Port the source from c8fb01811bc8 to the upstream ARMv7 append writer and its existing KUnit fixture. The provider still performs no erase and the allocator still uses append-only writes. + +Assisted-by: LLM +--- + .../driver-api/mtd/apple-sftl-reclamation.rst | 85 +++ + Documentation/driver-api/mtd/index.rst | 4 +- + drivers/mtd/Kconfig | 5 +- + drivers/mtd/Makefile | 3 +- + drivers/mtd/apple-sftl-core.c | 91 ++++ + drivers/mtd/apple-sftl-reclaim-test.c | 329 ++++++++++++ + drivers/mtd/apple-sftl-reclaim.c | 498 ++++++++++++++++++ + drivers/mtd/apple-sftl-reclaim.h | 88 ++++ + drivers/mtd/apple-sftl-test.c | 46 +- + 9 files changed, 1144 insertions(+), 5 deletions(-) + create mode 100644 Documentation/driver-api/mtd/apple-sftl-reclamation.rst + create mode 100644 drivers/mtd/apple-sftl-reclaim-test.c + create mode 100644 drivers/mtd/apple-sftl-reclaim.c + create mode 100644 drivers/mtd/apple-sftl-reclaim.h + +diff --git a/Documentation/driver-api/mtd/apple-sftl-reclamation.rst b/Documentation/driver-api/mtd/apple-sftl-reclamation.rst +new file mode 100644 +index 000000000..e286c6935 +--- /dev/null ++++ b/Documentation/driver-api/mtd/apple-sftl-reclamation.rst +@@ -0,0 +1,85 @@ ++.. SPDX-License-Identifier: GPL-2.0-only ++ ++Apple SwissPPNFTL checkpoint and reclamation draft ++================================================= ++ ++The measured 64 GB PPN layout shares one FTL across the logical partitions. ++A Linux partition therefore provides no exclusive ownership of a physical ++block. Reclamation must preserve the recovered map for the whole device. ++ ++The code in ``apple-sftl-reclaim.c`` serializes checkpoint images and plans ++transactions from supplied snapshots and completion receipts. It registers ++no media operation. The block device retains its existing access policy. ++ ++Checkpoint images ++----------------- ++ ++``apple_sftl_snapshot()`` takes an owned copy of the whole recovered range ++map. It rereads the selected context's non-map records under the map lock, ++checks their saved stamps and preserves their payloads. The caller supplies ++a memory budget. Changed media stops further reads through that instance. ++ ++``apple_sftl_checkpoint_build()`` emits a bounded array of canonical 4 KiB ++records. The metadata follows the 13G36 IOFlashStorage-730 formatter at ++``0x81092408``: type 0x1f, subtype, a 48-bit sequence, an array index/count, ++and a trailing 0xffffffff. The base record's decoded context-block list is ++updated. Other payload bytes survive unchanged. Mapping records use subtype ++4 with the native cursor/range encoding. Padding brings the image to full ++16 KiB pages and consumes sequence values through subtype-4 padding records. ++The resulting bytes round-trip through the current recovery decoder. ++ ++Opaque statistics, superblock state, user-sequence and read-count payloads ++still describe the source checkpoint. Preserving them prevents accidental ++data loss during format work; a native writer must update the fields whose ++meaning depends on relocation. These images have not been qualified for ++native checkpoint installation. ++ ++Offline transaction ++------------------- ++ ++``apple_sftl_gc_plan()`` consumes a whole-device snapshot plus descriptions ++of a closed victim and a fully checked blank destination. It rejects reserved ++blocks, references into the victim's footer or inactive space, a destination ++still referenced by the map, insufficient destination capacity and a working ++plan buffers exceeding the caller's budget. A source with an allocation ++floor above the last replayed record is excluded under the current recovery ++contract. Copies retain the logical keys while ++moving their provider addresses. Destination space leaves eight pages for ++a future native summary. ++ ++The transaction advances through explicit receipt checks: ++ ++* Each copy receipt must match its planned logical range, source, ++ destination and final sequence. The caller must issue and verify that IO. ++* A complete checkpoint image is built only after the copy receipts arrive. ++ Its readback must match every metadata and payload byte. ++* A fresh recovery snapshot must contain the planned whole-device mapping ++ and the new checkpoint sequence. Equivalent coalescing is accepted. ++ ++A mismatched receipt moves the transaction to its failed state. Even a victim ++with zero current references must pass checkpoint and cold-recovery stages, ++since the current replay contract can still require its obsolete history. ++The final VERIFIED state means that these supplied receipts passed. It does ++not certify native accounting or VFL commit durability. ++ ++Outstanding native protocol work ++------------------------------- ++ ++VFL recovery uses two context groups with four copies per group on this ++profile. The native rotation routine at ``0x810a1b68`` checks references into ++the target group before reuse, copies required lane tables and writes a final ++checkpoint page. Its interruption and replacement paths require further ++tracing before a runtime writer can reproduce them. ++ ++Offline execution of the pinned native iBEC erase queue establishes queued ++67/D7 commands, final-row 6A/D7, 79 polling and 77/7D completion. Linking its ++row construction to the 64 GB erase granule remains open, along with timeout, ++retirement and partial completion across CAUs. The provider exposes no erase ++callback. Enabling physical reclamation requires resolving those semantics ++and qualifying the exact candidate with device recovery and interruption ++checks. ++ ++The source research began with `Cascadia's NAND notes ++`_. ++The private native differential fixtures retain source hashes and execute ++against synthetic RAM. Public tests contain synthetic media only. +diff --git a/Documentation/driver-api/mtd/index.rst b/Documentation/driver-api/mtd/index.rst +index e91da2aa5..64342a68b 100644 +--- a/Documentation/driver-api/mtd/index.rst ++++ b/Documentation/driver-api/mtd/index.rst +@@ -9,4 +9,6 @@ Memory Technology Device (MTD) + + spi-intel + nand_ecc +- spi-nor apple-sftl ++ spi-nor ++ apple-sftl ++ apple-sftl-reclamation +diff --git a/drivers/mtd/Kconfig b/drivers/mtd/Kconfig +index 7771393e2..e4bc9ed60 100644 +--- a/drivers/mtd/Kconfig ++++ b/drivers/mtd/Kconfig +@@ -226,8 +226,9 @@ config MTD_APPLE_SFTL_KUNIT_TEST + default KUNIT_ALL_TESTS + help + Synthesize complete VFL/SFTL media and exercise checkpoint recovery, +- native block summaries and open-block journal replay. Malformed +- replicas and unsupported layouts must fail without storage writes. ++ native block summaries, open-block journal replay, checkpoint encoding ++ and offline reclamation transactions. Malformed replicas and unsupported ++ layouts must fail without storage writes. + Also test complete programs, interrupted summaries, allocation + limits and fresh-loader replay. + All test media and provider operations are synthetic. +diff --git a/drivers/mtd/Makefile b/drivers/mtd/Makefile +index fa6180a62..8a05767a1 100644 +--- a/drivers/mtd/Makefile ++++ b/drivers/mtd/Makefile +@@ -33,5 +33,6 @@ obj-$(CONFIG_MTD_UBI) += ubi/ + obj-$(CONFIG_MTD_HYPERBUS) += hyperbus/ + + obj-$(CONFIG_MTD_APPLE_SFTL) += apple-sftl.o +-apple-sftl-y := apple-sftl-core.o apple-sftl-block.o ++apple-sftl-y := apple-sftl-core.o apple-sftl-block.o apple-sftl-reclaim.o + obj-$(CONFIG_MTD_APPLE_SFTL_KUNIT_TEST) += apple-sftl-test.o ++obj-$(CONFIG_MTD_APPLE_SFTL_KUNIT_TEST) += apple-sftl-reclaim-test.o +diff --git a/drivers/mtd/apple-sftl-core.c b/drivers/mtd/apple-sftl-core.c +index 5d10d72e5..6d77d5552 100644 +--- a/drivers/mtd/apple-sftl-core.c ++++ b/drivers/mtd/apple-sftl-core.c +@@ -21,6 +21,7 @@ + #include + + #include "apple-sftl.h" ++#include "apple-sftl-reclaim.h" + + #define SFTL_LPAGES 15625016U + #define SFTL_DISK_PAGES 15625000U +@@ -63,6 +64,7 @@ struct sftl_member { + struct apple_sftl { + struct apple_ppn *ppn; + struct rb_root runs; ++ struct sftl_records context; + u32 nr_runs; + u16 lanes[SFTL_BLOCKS]; + DECLARE_BITMAP(reserved, SFTL_BLOCKS); +@@ -620,6 +622,17 @@ static int sftl_context(struct apple_sftl *sftl, const u8 *heads) + } + if (cursor != SFTL_LPAGES || !fragment_sequence) + goto out; ++ /* Retain compact references so an explicit snapshot can preserve opaque data. */ ++ for (i = first; i < records.count; i++) { ++ const struct sftl_record *record = &records.items[i]; ++ ++ if (sftl_flags(record) == 4) ++ continue; ++ ret = sftl_record_add(&sftl->context, record->stamp, record->key, ++ record->address); ++ if (ret) ++ goto out; ++ } + sftl->checkpoint_sequence = fragment_sequence; + sftl->sequence = fragment_sequence; + ret = 0; +@@ -955,6 +968,7 @@ void apple_sftl_free(struct apple_sftl *sftl) + kfree(rb_entry(node, struct sftl_run, node)); + } + kvfree(sftl->footer); ++ kvfree(sftl->context.items); + kfree(sftl->page); + kfree(sftl); + } +@@ -1249,3 +1263,80 @@ void apple_sftl_get_info(struct apple_sftl *sftl, struct apple_sftl_info *info) + info->journal_sequence = sftl->journal_sequence; + } + EXPORT_SYMBOL_GPL(apple_sftl_get_info); ++ ++int apple_sftl_snapshot(struct apple_sftl *sftl, size_t max_bytes, ++ struct apple_sftl_snapshot **out) ++{ ++ struct apple_sftl_snapshot *snapshot; ++ struct rb_node *node; ++ size_t map_bytes, context_bytes; ++ u32 i = 0; ++ unsigned int chunk; ++ int ret = 0; ++ ++ if (!out) ++ return -EINVAL; ++ *out = NULL; ++ if (!sftl) ++ return -EINVAL; ++ mutex_lock(&sftl->lock); ++ if (sftl->failed) { ++ ret = -EIO; ++ goto unlock; ++ } ++ map_bytes = array_size(sftl->nr_runs, sizeof(*snapshot->runs)); ++ context_bytes = array_size(sftl->context.count, sizeof(*snapshot->opaque)); ++ if (map_bytes > max_bytes || context_bytes > max_bytes - map_bytes) { ++ ret = -ENOSPC; ++ goto unlock; ++ } ++ snapshot = kzalloc(sizeof(*snapshot), GFP_KERNEL); ++ if (!snapshot) { ++ ret = -ENOMEM; ++ goto unlock; ++ } ++ snapshot->runs = kvzalloc(map_bytes, GFP_KERNEL); ++ snapshot->opaque = kvzalloc(context_bytes, GFP_KERNEL); ++ if (!snapshot->runs || !snapshot->opaque) { ++ ret = -ENOMEM; ++ goto failed; ++ } ++ for (node = rb_first(&sftl->runs); node; node = rb_next(node)) { ++ const struct sftl_run *run = rb_entry(node, struct sftl_run, node); ++ ++ snapshot->runs[i++] = (struct apple_sftl_extent) { ++ .logical = run->start, .length = run->end - run->start, ++ .provider = run->provider, ++ }; ++ } ++ for (i = 0; i < sftl->context.count; i++) { ++ const struct sftl_record *reference = &sftl->context.items[i]; ++ struct apple_sftl_context_record *record = &snapshot->opaque[i]; ++ ++ ret = sftl_read_address(sftl, reference->address, &chunk); ++ if (ret) { ++ sftl->failed = true; ++ goto failed; ++ } ++ if (sftl_stamp(sftl->oob + chunk * SFTL_META) != reference->stamp) { ++ ret = -EBADMSG; ++ sftl->failed = true; ++ goto failed; ++ } ++ memcpy(record->meta, sftl->oob + chunk * SFTL_META, SFTL_META); ++ memcpy(record->data, sftl->page + chunk * SFTL_CHUNK, SFTL_CHUNK); ++ } ++ snapshot->nr_runs = sftl->nr_runs; ++ snapshot->nr_opaque = sftl->context.count; ++ snapshot->sequence = sftl->sequence; ++ snapshot->journal_sequence = sftl->journal_sequence; ++ snapshot->checkpoint_sequence = sftl->checkpoint_sequence; ++ *out = snapshot; ++ goto unlock; ++failed: ++ apple_sftl_snapshot_free(snapshot); ++unlock: ++ mutex_unlock(&sftl->lock); ++ return ret; ++} ++EXPORT_SYMBOL_GPL(apple_sftl_snapshot); +diff --git a/drivers/mtd/apple-sftl-reclaim-test.c b/drivers/mtd/apple-sftl-reclaim-test.c +new file mode 100644 +index 000000000..c26d8b43a +--- /dev/null ++++ b/drivers/mtd/apple-sftl-reclaim-test.c +@@ -0,0 +1,329 @@ ++// SPDX-License-Identifier: GPL-2.0-only ++/* Synthetic checkpoints and supplied IO receipts; no media mutations. */ ++#include ++#include ++#include ++#include ++#include ++ ++#include "apple-sftl-reclaim.h" ++ ++static void reclaim_source(struct apple_sftl_snapshot *snapshot, ++ struct apple_sftl_context_record *opaque, ++ struct apple_sftl_extent *runs, u32 count) ++{ ++ unsigned int i; ++ ++ memset(opaque, 0x6b, sizeof(*opaque)); ++ apple_sftl_context_meta(opaque->meta, 1, 100, 17, 23); ++ put_unaligned_le32(1, opaque->data); ++ put_unaligned_le32(1, opaque->data + 4); ++ for (i = 0; i < count; i++) ++ runs[i] = (struct apple_sftl_extent) { ++ .logical = i, .length = 1, ++ .provider = i & 1 ? APPLE_SFTL_NO_ADDRESS : 4 * APPLE_SFTL_STRIDE + i, ++ }; ++ runs[count - 1].length = APPLE_SFTL_LOGICAL_PAGES - count + 1; ++ runs[count - 1].provider = APPLE_SFTL_NO_ADDRESS; ++ *snapshot = (struct apple_sftl_snapshot) { ++ .runs = runs, .nr_runs = count, .opaque = opaque, .nr_opaque = 1, ++ .sequence = 200, .checkpoint_sequence = 101, .journal_sequence = 200, ++ }; ++} ++ ++static void reclaim_metadata_vector(struct kunit *test) ++{ ++ /* Native 0x81092408 output, with low-word sequence carry. */ ++ static const u8 expected[16] = { ++ 0x1f, 0x06, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, ++ 0x00, 0x02, 0x09, 0x00, 0xff, 0xff, 0xff, 0xff, ++ }; ++ u8 meta[16]; ++ ++ KUNIT_ASSERT_EQ(test, apple_sftl_context_meta(meta, 6, 0x100000000ULL, 512, 9), 0); ++ KUNIT_EXPECT_MEMEQ(test, meta, expected, sizeof(meta)); ++ memset(meta, 0x5a, sizeof(meta)); ++ KUNIT_EXPECT_EQ(test, apple_sftl_context_meta(meta, 0, 1, 0, 0), -EINVAL); ++ KUNIT_EXPECT_EQ(test, meta[0], 0x5a); ++ KUNIT_EXPECT_EQ(test, apple_sftl_context_meta(meta, 4, 1ULL << 48, 0, 0), -EINVAL); ++} ++ ++static void reclaim_checkpoint_fragments(struct kunit *test) ++{ ++ struct apple_sftl_context_record *opaque = kunit_kmalloc(test, sizeof(*opaque), GFP_KERNEL); ++ struct apple_sftl_extent *runs = kunit_kmalloc(test, 514 * sizeof(*runs), GFP_KERNEL); ++ struct apple_sftl_checkpoint *image = NULL; ++ struct apple_sftl_snapshot snapshot; ++ u32 blocks[] = { 5, 6 }; ++ u32 i; ++ ++ KUNIT_ASSERT_NOT_NULL(test, opaque); ++ KUNIT_ASSERT_NOT_NULL(test, runs); ++ reclaim_source(&snapshot, opaque, runs, 514); ++ KUNIT_ASSERT_EQ(test, apple_sftl_checkpoint_build(&snapshot, blocks, 2, 4, &image), 0); ++ KUNIT_ASSERT_NOT_NULL(test, image); ++ KUNIT_EXPECT_EQ(test, image->count, 4U); ++ KUNIT_EXPECT_EQ(test, image->first_sequence, 201ULL); ++ KUNIT_EXPECT_EQ(test, image->sequence, 204ULL); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le32(image->records[0].data), 2U); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le32(image->records[0].data + 4), 5U); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le32(image->records[0].data + 8), 6U); ++ KUNIT_EXPECT_MEMEQ(test, image->records[0].data + 12, opaque->data + 12, 4084); ++ KUNIT_EXPECT_MEMEQ(test, image->records[0].meta + 8, opaque->meta + 8, 8); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le32(image->records[1].data), 0U); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le32(image->records[2].data), 511U); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le32(image->records[3].data + 4), (u32)~0U); ++ for (i = 1; i < 4; i++) { ++ KUNIT_EXPECT_EQ(test, image->records[i].meta[0], 0x1f); ++ KUNIT_EXPECT_EQ(test, image->records[i].meta[1], 4); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le32(image->records[i].meta + 8), 0U); ++ } ++ apple_sftl_checkpoint_free(image); ++ image = NULL; ++ KUNIT_EXPECT_EQ(test, ++ apple_sftl_checkpoint_build(&snapshot, blocks, 2, 3, &image), -ENOSPC); ++ KUNIT_EXPECT_PTR_EQ(test, image, NULL); ++ snapshot.sequence = (1ULL << 48) - 3; ++ KUNIT_EXPECT_EQ(test, ++ apple_sftl_checkpoint_build(&snapshot, blocks, 2, 4, &image), -EOVERFLOW); ++} ++ ++static void reclaim_gc_transaction(struct kunit *test) ++{ ++ struct apple_sftl_context_record *opaque = kunit_kmalloc(test, sizeof(*opaque), GFP_KERNEL); ++ struct apple_sftl_extent runs[3]; ++ struct apple_sftl_extent recovered_runs[3]; ++ struct apple_sftl_snapshot snapshot, recovered; ++ struct apple_sftl_gc_block victim = { ++ .block = 4, .lanes = 3, .closed = true, .last_sequence = 199, ++ }; ++ struct apple_sftl_gc_block destination = { .block = 7, .lanes = 3, .blank = true }; ++ const struct apple_sftl_gc_copy *copy; ++ struct apple_sftl_checkpoint *image = NULL; ++ struct apple_sftl_gc *gc = NULL; ++ u32 blocks[] = { 5 }; ++ ++ KUNIT_ASSERT_NOT_NULL(test, opaque); ++ reclaim_source(&snapshot, opaque, runs, 3); ++ runs[0].length = 2; ++ runs[1] = (struct apple_sftl_extent) { ++ .logical = 2, .length = 1, .provider = 6 * APPLE_SFTL_STRIDE, ++ }; ++ runs[2] = (struct apple_sftl_extent) { ++ .logical = 3, .length = APPLE_SFTL_LOGICAL_PAGES - 3, ++ .provider = APPLE_SFTL_NO_ADDRESS, ++ }; ++ KUNIT_ASSERT_EQ(test, apple_sftl_gc_plan(&snapshot, &victim, &destination, 4, ++ 65536, &gc), 0); ++ KUNIT_EXPECT_EQ(test, apple_sftl_gc_stage(gc), APPLE_SFTL_GC_COPY); ++ KUNIT_EXPECT_EQ(test, apple_sftl_gc_checkpoint(gc, blocks, 1, &image), -EINVAL); ++ copy = apple_sftl_gc_next_copy(gc); ++ KUNIT_ASSERT_NOT_NULL(test, copy); ++ KUNIT_EXPECT_EQ(test, copy->logical, 0U); ++ KUNIT_EXPECT_EQ(test, copy->length, 2U); ++ KUNIT_EXPECT_EQ(test, copy->destination, 7 * APPLE_SFTL_STRIDE); ++ KUNIT_ASSERT_EQ(test, apple_sftl_gc_copy_done(gc, copy, 202), 0); ++ KUNIT_EXPECT_EQ(test, apple_sftl_gc_stage(gc), APPLE_SFTL_GC_CHECKPOINT); ++ KUNIT_ASSERT_EQ(test, apple_sftl_gc_checkpoint(gc, blocks, 1, &image), 0); ++ KUNIT_ASSERT_EQ(test, apple_sftl_gc_checkpoint_done(gc, image), 0); ++ KUNIT_EXPECT_EQ(test, apple_sftl_gc_stage(gc), APPLE_SFTL_GC_RECOVERY); ++ memcpy(recovered_runs, runs, sizeof(runs)); ++ recovered_runs[0].provider = 7 * APPLE_SFTL_STRIDE; ++ recovered = snapshot; ++ recovered.runs = recovered_runs; ++ recovered.sequence = image->sequence; ++ recovered.journal_sequence = image->sequence; ++ recovered.checkpoint_sequence = image->sequence; ++ KUNIT_ASSERT_EQ(test, apple_sftl_gc_recovered(gc, &recovered), 0); ++ KUNIT_EXPECT_EQ(test, apple_sftl_gc_stage(gc), APPLE_SFTL_GC_VERIFIED); ++ /* The caller's original map and opaque accounting bytes remain intact. */ ++ KUNIT_EXPECT_EQ(test, runs[0].provider, 4 * APPLE_SFTL_STRIDE); ++ KUNIT_EXPECT_EQ(test, opaque->data[200], 0x6b); ++ apple_sftl_checkpoint_free(image); ++ apple_sftl_gc_free(gc); ++} ++ ++static void reclaim_gc_fail_stop(struct kunit *test) ++{ ++ struct apple_sftl_context_record *opaque = kunit_kmalloc(test, sizeof(*opaque), GFP_KERNEL); ++ struct apple_sftl_extent runs[2]; ++ struct apple_sftl_snapshot snapshot; ++ struct apple_sftl_gc_block victim = { ++ .block = 4, .lanes = 3, .closed = true, .last_sequence = 199, ++ }; ++ struct apple_sftl_gc_block destination = { .block = 7, .lanes = 3, .blank = true }; ++ struct apple_sftl_gc *gc = NULL; ++ struct apple_sftl_gc_copy receipt; ++ struct apple_sftl_checkpoint *image = NULL; ++ u32 blocks[] = { 5 }; ++ ++ KUNIT_ASSERT_NOT_NULL(test, opaque); ++ reclaim_source(&snapshot, opaque, runs, 2); ++ KUNIT_ASSERT_EQ(test, apple_sftl_gc_plan(&snapshot, &victim, &destination, 4, ++ 65536, &gc), 0); ++ receipt = *apple_sftl_gc_next_copy(gc); ++ receipt.destination++; ++ KUNIT_EXPECT_EQ(test, apple_sftl_gc_copy_done(gc, &receipt, 201), -EBADMSG); ++ KUNIT_EXPECT_EQ(test, apple_sftl_gc_stage(gc), APPLE_SFTL_GC_FAILED); ++ KUNIT_EXPECT_EQ(test, apple_sftl_gc_checkpoint(gc, blocks, 1, &image), -EINVAL); ++ apple_sftl_gc_free(gc); ++ gc = NULL; ++ victim.reserved = true; ++ KUNIT_EXPECT_EQ(test, ++ apple_sftl_gc_plan(&snapshot, &victim, &destination, 4, ++ 65536, &gc), -EINVAL); ++ victim.reserved = false; ++ destination.block = 4; ++ KUNIT_EXPECT_EQ(test, ++ apple_sftl_gc_plan(&snapshot, &victim, &destination, 4, ++ 65536, &gc), -EINVAL); ++ KUNIT_EXPECT_PTR_EQ(test, gc, NULL); ++} ++ ++static void reclaim_obsolete_history_needs_checkpoint(struct kunit *test) ++{ ++ struct apple_sftl_context_record *opaque = kunit_kmalloc(test, sizeof(*opaque), GFP_KERNEL); ++ struct apple_sftl_extent runs[1]; ++ struct apple_sftl_snapshot snapshot; ++ struct apple_sftl_gc_block victim = { ++ .block = 4, .lanes = 3, .closed = true, .last_sequence = 199, ++ }; ++ struct apple_sftl_gc_block destination = { .block = 7, .lanes = 3, .blank = true }; ++ struct apple_sftl_gc *gc = NULL; ++ ++ KUNIT_ASSERT_NOT_NULL(test, opaque); ++ reclaim_source(&snapshot, opaque, runs, 1); ++ KUNIT_ASSERT_EQ(test, apple_sftl_gc_plan(&snapshot, &victim, &destination, 4, ++ 65536, &gc), 0); ++ KUNIT_EXPECT_PTR_EQ(test, apple_sftl_gc_next_copy(gc), NULL); ++ KUNIT_EXPECT_EQ(test, apple_sftl_gc_stage(gc), APPLE_SFTL_GC_CHECKPOINT); ++ KUNIT_EXPECT_EQ(test, apple_sftl_gc_recovered(gc, &snapshot), -EINVAL); ++ apple_sftl_gc_free(gc); ++} ++ ++static void reclaim_checkpoint_opaque_and_invalid(struct kunit *test) ++{ ++ static const u8 subtypes[] = { 1, 2, 3, 5, 6 }; ++ struct apple_sftl_context_record *opaque; ++ struct apple_sftl_extent runs[1]; ++ struct apple_sftl_snapshot snapshot; ++ struct apple_sftl_checkpoint *image = NULL; ++ u32 blocks[] = { 5, 5 }; ++ u32 i; ++ ++ opaque = kunit_kmalloc(test, 5 * sizeof(*opaque), GFP_KERNEL); ++ KUNIT_ASSERT_NOT_NULL(test, opaque); ++ reclaim_source(&snapshot, opaque, runs, 1); ++ for (i = 1; i < 5; i++) { ++ memset(&opaque[i], 0x50 + i, sizeof(opaque[i])); ++ apple_sftl_context_meta(opaque[i].meta, subtypes[i], 100 + i, i * 16, 16); ++ } ++ snapshot.nr_opaque = 5; ++ KUNIT_EXPECT_EQ(test, ++ apple_sftl_checkpoint_build(&snapshot, blocks, 2, 8, &image), -EINVAL); ++ KUNIT_ASSERT_EQ(test, apple_sftl_checkpoint_build(&snapshot, blocks, 1, 8, &image), 0); ++ for (i = 1; i < 5; i++) { ++ KUNIT_EXPECT_MEMEQ(test, image->records[i].data, opaque[i].data, 4096); ++ KUNIT_EXPECT_MEMEQ(test, image->records[i].meta + 8, opaque[i].meta + 8, 8); ++ } ++ apple_sftl_checkpoint_free(image); ++ image = NULL; ++ opaque[2].meta[1] = 1; ++ KUNIT_EXPECT_EQ(test, ++ apple_sftl_checkpoint_build(&snapshot, blocks, 1, 8, &image), -EBADMSG); ++ KUNIT_EXPECT_PTR_EQ(test, image, NULL); ++} ++ ++static void reclaim_checkpoint_torn_receipts(struct kunit *test) ++{ ++ struct apple_sftl_context_record *opaque = kunit_kmalloc(test, sizeof(*opaque), GFP_KERNEL); ++ struct apple_sftl_extent runs[1]; ++ struct apple_sftl_snapshot snapshot, recovered; ++ struct apple_sftl_gc_block victim = { ++ .block = 4, .lanes = 3, .closed = true, .last_sequence = 199, ++ }; ++ struct apple_sftl_gc_block destination = { .block = 7, .lanes = 3, .blank = true }; ++ struct apple_sftl_checkpoint *image; ++ struct apple_sftl_gc *gc; ++ u32 blocks[] = { 5 }, i; ++ ++ KUNIT_ASSERT_NOT_NULL(test, opaque); ++ reclaim_source(&snapshot, opaque, runs, 1); ++ for (i = 0; i < 4; i++) { ++ KUNIT_ASSERT_EQ(test, ++ apple_sftl_gc_plan(&snapshot, &victim, &destination, 4, ++ 65536, &gc), 0); ++ KUNIT_ASSERT_EQ(test, apple_sftl_gc_checkpoint(gc, blocks, 1, &image), 0); ++ image->records[i].meta[2] ^= 1; ++ KUNIT_EXPECT_EQ(test, apple_sftl_gc_checkpoint_done(gc, image), -EBADMSG); ++ KUNIT_EXPECT_EQ(test, apple_sftl_gc_stage(gc), APPLE_SFTL_GC_FAILED); ++ apple_sftl_checkpoint_free(image); ++ apple_sftl_gc_free(gc); ++ } ++ KUNIT_ASSERT_EQ(test, ++ apple_sftl_gc_plan(&snapshot, &victim, &destination, 4, 65536, &gc), 0); ++ KUNIT_ASSERT_EQ(test, apple_sftl_gc_checkpoint(gc, blocks, 1, &image), 0); ++ KUNIT_ASSERT_EQ(test, apple_sftl_gc_checkpoint_done(gc, image), 0); ++ recovered = snapshot; ++ recovered.sequence = image->sequence; ++ recovered.journal_sequence = image->sequence; ++ recovered.checkpoint_sequence = image->sequence - 1; ++ KUNIT_EXPECT_EQ(test, apple_sftl_gc_recovered(gc, &recovered), -EBADMSG); ++ KUNIT_EXPECT_EQ(test, apple_sftl_gc_stage(gc), APPLE_SFTL_GC_FAILED); ++ apple_sftl_checkpoint_free(image); ++ apple_sftl_gc_free(gc); ++} ++ ++static void reclaim_gc_boundaries(struct kunit *test) ++{ ++ struct apple_sftl_context_record *opaque = kunit_kmalloc(test, sizeof(*opaque), GFP_KERNEL); ++ struct apple_sftl_extent runs[2]; ++ struct apple_sftl_snapshot snapshot; ++ struct apple_sftl_gc_block victim = { ++ .block = 4, .lanes = 3, .closed = true, .last_sequence = 199, ++ }; ++ struct apple_sftl_gc_block destination = { .block = 7, .lanes = 1, .blank = true }; ++ struct apple_sftl_gc *gc = NULL; ++ int ret; ++ ++ KUNIT_ASSERT_NOT_NULL(test, opaque); ++ reclaim_source(&snapshot, opaque, runs, 2); ++ snapshot.journal_sequence--; ++ ret = apple_sftl_gc_plan(&snapshot, &victim, &destination, 4, 65536, &gc); ++ KUNIT_EXPECT_EQ(test, ret, -EOPNOTSUPP); ++ snapshot.journal_sequence++; ++ ret = apple_sftl_gc_plan(&snapshot, &victim, &destination, 4, 1, &gc); ++ KUNIT_EXPECT_EQ(test, ret, -ENOSPC); ++ runs[0].length = 481; ++ runs[1].logical = 481; ++ runs[1].length = APPLE_SFTL_LOGICAL_PAGES - 481; ++ ret = apple_sftl_gc_plan(&snapshot, &victim, &destination, 4, 65536, &gc); ++ KUNIT_EXPECT_EQ(test, ret, -ENOSPC); ++ runs[0].length = 1; ++ runs[0].provider += 1000; ++ runs[1].logical = 1; ++ runs[1].length = APPLE_SFTL_LOGICAL_PAGES - 1; ++ ret = apple_sftl_gc_plan(&snapshot, &victim, &destination, 4, 65536, &gc); ++ KUNIT_EXPECT_EQ(test, ret, -EBADMSG); ++ KUNIT_EXPECT_PTR_EQ(test, gc, NULL); ++} ++ ++static struct kunit_case reclaim_cases[] = { ++ KUNIT_CASE(reclaim_metadata_vector), ++ KUNIT_CASE(reclaim_checkpoint_opaque_and_invalid), ++ KUNIT_CASE(reclaim_checkpoint_torn_receipts), ++ KUNIT_CASE(reclaim_gc_boundaries), ++ KUNIT_CASE(reclaim_checkpoint_fragments), ++ KUNIT_CASE(reclaim_gc_transaction), ++ KUNIT_CASE(reclaim_gc_fail_stop), ++ KUNIT_CASE(reclaim_obsolete_history_needs_checkpoint), ++ {} ++}; ++ ++static struct kunit_suite reclaim_suite = { ++ .name = "apple-sftl-reclaim", ++ .test_cases = reclaim_cases, ++}; ++ ++kunit_test_suite(reclaim_suite); ++MODULE_LICENSE("GPL"); +diff --git a/drivers/mtd/apple-sftl-reclaim.c b/drivers/mtd/apple-sftl-reclaim.c +new file mode 100644 +index 000000000..100e92b57 +--- /dev/null ++++ b/drivers/mtd/apple-sftl-reclaim.c +@@ -0,0 +1,498 @@ ++// SPDX-License-Identifier: GPL-2.0-only ++/* ++ * SwissPPNFTL checkpoint serialization and offline reclamation transactions. ++ * Native metadata: 13G36 IOFlashStorage-730 formatter at 0x81092408. ++ * Range payload: cxtNextBatch at 0x8108f07c and the recovery decoder. ++ * ++ * Opaque accounting payloads survive byte-for-byte. Updating their native ++ * accounting and committing VFL bank rotation remain separate work. No erase ++ * callback or runtime GC worker is registered by this file. ++ */ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#include "apple-sftl-reclaim.h" ++ ++#define SFTL_SEQUENCE GENMASK_ULL(47, 0) ++#define SFTL_PROVIDER (APPLE_SFTL_SUBBLOCKS * APPLE_SFTL_STRIDE) ++#define SFTL_PAIRS ((APPLE_SFTL_CHUNK - 8) / 8) ++#define SFTL_MAX_RECORDS (16U * APPLE_SFTL_STRIDE) ++#define SFTL_UNMAPPED 0x1ffffffU ++ ++struct apple_sftl_gc { ++ struct apple_sftl_snapshot snapshot; ++ struct apple_sftl_gc_copy *copies; ++ struct apple_sftl_checkpoint *checkpoint; ++ u32 nr_copies, copied, max_records, victim, destination; ++ u64 retire_sequence, copied_sequence; ++ enum apple_sftl_gc_stage stage; ++}; ++ ++int apple_sftl_context_meta(u8 *meta, u8 subtype, u64 sequence, ++ u16 index, u16 entries) ++{ ++ if (!meta || !subtype || subtype > 6 || sequence > SFTL_SEQUENCE) ++ return -EINVAL; ++ put_unaligned_le64(sequence << 16 | (u16)subtype << 8 | 0x1f, meta); ++ put_unaligned_le16(index, meta + 8); ++ put_unaligned_le16(entries, meta + 10); ++ put_unaligned_le32(U32_MAX, meta + 12); ++ return 0; ++} ++EXPORT_SYMBOL_GPL(apple_sftl_context_meta); ++ ++static int sftl_check_map(const struct apple_sftl_snapshot *snapshot) ++{ ++ u32 cursor = 0, i; ++ ++ if (!snapshot || !snapshot->runs || !snapshot->nr_runs || ++ snapshot->nr_runs > APPLE_SFTL_LOGICAL_PAGES || ++ snapshot->checkpoint_sequence > snapshot->sequence || ++ snapshot->journal_sequence > snapshot->sequence || ++ snapshot->sequence > SFTL_SEQUENCE) ++ return -EINVAL; ++ for (i = 0; i < snapshot->nr_runs; i++) { ++ const struct apple_sftl_extent *run = &snapshot->runs[i]; ++ ++ if (run->logical != cursor || !run->length || ++ run->length > APPLE_SFTL_LOGICAL_PAGES - cursor || ++ (run->provider != APPLE_SFTL_NO_ADDRESS && ++ (run->provider >= SFTL_PROVIDER || ++ run->length > SFTL_PROVIDER - run->provider))) ++ return -EBADMSG; ++ cursor += run->length; ++ } ++ return cursor == APPLE_SFTL_LOGICAL_PAGES ? 0 : -EBADMSG; ++} ++ ++static int sftl_check_context(const struct apple_sftl_snapshot *snapshot) ++{ ++ u32 i, previous = 0, names; ++ ++ for (i = 0; i < snapshot->nr_opaque; i++) { ++ u8 subtype = snapshot->opaque[i].meta[1]; ++ u32 order = subtype >= 5 ? subtype - 1 : subtype; ++ ++ if (snapshot->opaque[i].meta[0] != 0x1f || !subtype || ++ subtype == 4 || subtype > 6 || (!i && subtype != 1) || ++ order < previous) ++ return -EBADMSG; ++ previous = order; ++ } ++ names = get_unaligned_le32(snapshot->opaque[0].data); ++ if (!names || names > 16) ++ return -EBADMSG; ++ return 0; ++} ++ ++static int sftl_checkpoint_count(const struct apple_sftl_snapshot *snapshot, ++ u32 max_records, u32 *count) ++{ ++ u32 records; ++ int ret = sftl_check_map(snapshot); ++ ++ if (ret) ++ return ret; ++ if (!snapshot->opaque || !snapshot->nr_opaque || ++ snapshot->nr_opaque > SFTL_MAX_RECORDS || ++ !max_records || max_records > SFTL_MAX_RECORDS) ++ return -EINVAL; ++ ret = sftl_check_context(snapshot); ++ if (ret) ++ return ret; ++ records = snapshot->nr_opaque + (snapshot->nr_runs + SFTL_PAIRS - 1) / SFTL_PAIRS; ++ records = (records + 3) & ~3U; ++ if (records > max_records) ++ return -ENOSPC; ++ if (records > SFTL_SEQUENCE - snapshot->sequence) ++ return -EOVERFLOW; ++ *count = records; ++ return 0; ++} ++ ++void apple_sftl_snapshot_free(struct apple_sftl_snapshot *snapshot) ++{ ++ if (!snapshot) ++ return; ++ kvfree(snapshot->runs); ++ kvfree(snapshot->opaque); ++ kfree(snapshot); ++} ++EXPORT_SYMBOL_GPL(apple_sftl_snapshot_free); ++ ++void apple_sftl_checkpoint_free(struct apple_sftl_checkpoint *checkpoint) ++{ ++ if (!checkpoint) ++ return; ++ kvfree(checkpoint->records); ++ kfree(checkpoint); ++} ++EXPORT_SYMBOL_GPL(apple_sftl_checkpoint_free); ++ ++int apple_sftl_checkpoint_build(const struct apple_sftl_snapshot *snapshot, ++ const u32 *blocks, u32 nr_blocks, ++ u32 max_records, struct apple_sftl_checkpoint **out) ++{ ++ struct apple_sftl_checkpoint *checkpoint; ++ u32 count, i, j, cursor = 0, run = 0, old_names; ++ int ret; ++ ++ if (!out) ++ return -EINVAL; ++ *out = NULL; ++ ret = sftl_checkpoint_count(snapshot, max_records, &count); ++ if (ret) ++ return ret; ++ if (!blocks || !nr_blocks || nr_blocks > 16) ++ return -EINVAL; ++ for (i = 0; i < nr_blocks; i++) { ++ if (!blocks[i] || blocks[i] >= APPLE_SFTL_SUBBLOCKS) ++ return -EINVAL; ++ for (j = 0; j < i; j++) ++ if (blocks[j] == blocks[i]) ++ return -EINVAL; ++ } ++ old_names = get_unaligned_le32(snapshot->opaque[0].data); ++ checkpoint = kzalloc(sizeof(*checkpoint), GFP_KERNEL); ++ if (!checkpoint) ++ return -ENOMEM; ++ checkpoint->records = kvzalloc(array_size(count, sizeof(*checkpoint->records)), GFP_KERNEL); ++ if (!checkpoint->records) { ++ kfree(checkpoint); ++ return -ENOMEM; ++ } ++ checkpoint->count = count; ++ checkpoint->first_sequence = snapshot->sequence + 1; ++ checkpoint->sequence = snapshot->sequence + count; ++ for (i = 0; i < snapshot->nr_opaque; i++) { ++ struct apple_sftl_context_record *record = &checkpoint->records[i]; ++ ++ *record = snapshot->opaque[i]; ++ apple_sftl_context_meta(record->meta, record->meta[1], ++ checkpoint->first_sequence + i, ++ get_unaligned_le16(record->meta + 8), ++ get_unaligned_le16(record->meta + 10)); ++ } ++ /* Only the decoded base list changes; its remaining native bytes survive. */ ++ memset(checkpoint->records[0].data + 4, 0, max(old_names, nr_blocks) * sizeof(u32)); ++ put_unaligned_le32(nr_blocks, checkpoint->records[0].data); ++ for (i = 0; i < nr_blocks; i++) ++ put_unaligned_le32(blocks[i], checkpoint->records[0].data + 4 + i * 4); ++ for (i = snapshot->nr_opaque; i < count; i++) { ++ struct apple_sftl_context_record *record = &checkpoint->records[i]; ++ ++ memset(record->data, 0xff, sizeof(record->data)); ++ apple_sftl_context_meta(record->meta, 4, ++ checkpoint->first_sequence + i, 0, 0); ++ if (run == snapshot->nr_runs) ++ continue; ++ put_unaligned_le32(cursor, record->data); ++ put_unaligned_le32(0xfffffff0, record->data + 4); ++ for (j = 0; j < SFTL_PAIRS && run < snapshot->nr_runs; j++, run++) { ++ const struct apple_sftl_extent *extent = &snapshot->runs[run]; ++ u32 address = extent->provider; ++ ++ if (address == APPLE_SFTL_NO_ADDRESS) ++ address = SFTL_UNMAPPED; ++ put_unaligned_le32(address, record->data + 8 + j * 8); ++ put_unaligned_le32(extent->length, record->data + 12 + j * 8); ++ cursor += extent->length; ++ } ++ } ++ *out = checkpoint; ++ return 0; ++} ++EXPORT_SYMBOL_GPL(apple_sftl_checkpoint_build); ++ ++static bool sftl_overlap(u32 first, u32 length, u32 start, u32 end) ++{ ++ return first < end && start < first + length; ++} ++ ++static void sftl_gc_run(struct apple_sftl_snapshot *map, u32 logical, ++ u32 length, u32 provider) ++{ ++ struct apple_sftl_extent *run = &map->runs[map->nr_runs++]; ++ ++ *run = (struct apple_sftl_extent) { ++ .logical = logical, .length = length, .provider = provider, ++ }; ++} ++ ++static bool sftl_gc_block_valid(const struct apple_sftl_gc_block *block) ++{ ++ return block && block->block && block->block < APPLE_SFTL_SUBBLOCKS && ++ block->lanes && !block->reserved && block->last_sequence <= SFTL_SEQUENCE; ++} ++ ++int apple_sftl_gc_plan(const struct apple_sftl_snapshot *snapshot, ++ const struct apple_sftl_gc_block *victim, ++ const struct apple_sftl_gc_block *destination, ++ u32 max_records, size_t max_bytes, struct apple_sftl_gc **out) ++{ ++ struct apple_sftl_gc *gc; ++ u32 start, end, dest, capacity, copied = 0, i, count; ++ size_t map_bytes, copy_bytes, opaque_bytes; ++ int ret; ++ ++ if (!out) ++ return -EINVAL; ++ *out = NULL; ++ ret = sftl_checkpoint_count(snapshot, max_records, &count); ++ if (ret) ++ return ret; ++ if (snapshot->sequence != snapshot->journal_sequence) ++ return -EOPNOTSUPP; ++ if (!sftl_gc_block_valid(victim) || !sftl_gc_block_valid(destination) || ++ victim->blank || !victim->closed || !destination->blank || ++ destination->last_sequence || victim->block == destination->block || ++ victim->last_sequence > snapshot->sequence) ++ return -EINVAL; ++ start = victim->block * APPLE_SFTL_STRIDE; ++ end = start + hweight16(victim->lanes) * 512; ++ dest = destination->block * APPLE_SFTL_STRIDE; ++ /* Eight native footer pages remain available when the destination closes. */ ++ capacity = hweight16(destination->lanes) * 512 - 32; ++ for (i = 0; i < snapshot->nr_runs; i++) { ++ const struct apple_sftl_extent *run = &snapshot->runs[i]; ++ ++ if (run->provider != APPLE_SFTL_NO_ADDRESS && ++ sftl_overlap(run->provider, run->length, dest, dest + APPLE_SFTL_STRIDE)) ++ return -EBADMSG; ++ } ++ map_bytes = array_size(snapshot->nr_runs * 3, sizeof(*gc->snapshot.runs)); ++ copy_bytes = array_size(snapshot->nr_runs, sizeof(*gc->copies)); ++ opaque_bytes = array_size(snapshot->nr_opaque, sizeof(*snapshot->opaque)); ++ if (map_bytes > max_bytes || copy_bytes > max_bytes - map_bytes || ++ opaque_bytes > max_bytes - map_bytes - copy_bytes) ++ return -ENOSPC; ++ gc = kzalloc(sizeof(*gc), GFP_KERNEL); ++ if (!gc) ++ return -ENOMEM; ++ gc->snapshot.sequence = snapshot->sequence; ++ gc->snapshot.checkpoint_sequence = snapshot->checkpoint_sequence; ++ gc->snapshot.journal_sequence = snapshot->journal_sequence; ++ gc->max_records = max_records; ++ gc->victim = victim->block; ++ gc->destination = destination->block; ++ gc->retire_sequence = max(victim->last_sequence, snapshot->sequence); ++ gc->copied_sequence = snapshot->sequence; ++ gc->snapshot.runs = kvzalloc(map_bytes, GFP_KERNEL); ++ gc->copies = kvzalloc(copy_bytes, GFP_KERNEL); ++ gc->snapshot.opaque = kvzalloc(opaque_bytes, GFP_KERNEL); ++ if (!gc->snapshot.runs || !gc->copies || !gc->snapshot.opaque) { ++ ret = -ENOMEM; ++ goto failed; ++ } ++ memcpy(gc->snapshot.opaque, snapshot->opaque, ++ snapshot->nr_opaque * sizeof(*snapshot->opaque)); ++ gc->snapshot.nr_opaque = snapshot->nr_opaque; ++ for (i = 0; i < snapshot->nr_runs; i++) { ++ const struct apple_sftl_extent *run = &snapshot->runs[i]; ++ u32 before, live, offset, address = run->provider; ++ ++ if (address != APPLE_SFTL_NO_ADDRESS && ++ sftl_overlap(address, run->length, start, start + APPLE_SFTL_STRIDE) && ++ min(address + run->length, start + APPLE_SFTL_STRIDE) > end - 32) { ++ ret = -EBADMSG; ++ goto failed; ++ } ++ if (address == APPLE_SFTL_NO_ADDRESS || ++ !sftl_overlap(address, run->length, start, end)) { ++ sftl_gc_run(&gc->snapshot, run->logical, run->length, address); ++ continue; ++ } ++ before = start > address ? start - address : 0; ++ live = min(address + run->length, end) - max(address, start); ++ if (live > capacity - copied) { ++ ret = -ENOSPC; ++ goto failed; ++ } ++ if (before) ++ sftl_gc_run(&gc->snapshot, run->logical, before, address); ++ sftl_gc_run(&gc->snapshot, run->logical + before, live, dest + copied); ++ gc->copies[gc->nr_copies++] = (struct apple_sftl_gc_copy) { ++ .logical = run->logical + before, .length = live, ++ .source = address + before, .destination = dest + copied, ++ }; ++ copied += live; ++ offset = before + live; ++ if (offset < run->length) ++ sftl_gc_run(&gc->snapshot, run->logical + offset, ++ run->length - offset, address + offset); ++ } ++ /* Copy records consume sequences before checkpoint serialization. */ ++ if (copied > SFTL_SEQUENCE - snapshot->sequence) { ++ ret = -EOVERFLOW; ++ goto failed; ++ } ++ gc->snapshot.sequence += copied; ++ gc->snapshot.journal_sequence += copied; ++ ret = sftl_checkpoint_count(&gc->snapshot, max_records, &count); ++ if (ret) ++ goto failed; ++ gc->stage = gc->nr_copies ? APPLE_SFTL_GC_COPY : APPLE_SFTL_GC_CHECKPOINT; ++ *out = gc; ++ return 0; ++failed: ++ apple_sftl_gc_free(gc); ++ return ret; ++} ++EXPORT_SYMBOL_GPL(apple_sftl_gc_plan); ++ ++void apple_sftl_gc_free(struct apple_sftl_gc *gc) ++{ ++ if (!gc) ++ return; ++ apple_sftl_checkpoint_free(gc->checkpoint); ++ kvfree(gc->snapshot.runs); ++ kvfree(gc->snapshot.opaque); ++ kvfree(gc->copies); ++ kfree(gc); ++} ++EXPORT_SYMBOL_GPL(apple_sftl_gc_free); ++ ++enum apple_sftl_gc_stage apple_sftl_gc_stage(const struct apple_sftl_gc *gc) ++{ ++ return gc ? gc->stage : APPLE_SFTL_GC_FAILED; ++} ++EXPORT_SYMBOL_GPL(apple_sftl_gc_stage); ++ ++const struct apple_sftl_gc_copy *apple_sftl_gc_next_copy(const struct apple_sftl_gc *gc) ++{ ++ if (!gc || gc->stage != APPLE_SFTL_GC_COPY) ++ return NULL; ++ return &gc->copies[gc->copied]; ++} ++EXPORT_SYMBOL_GPL(apple_sftl_gc_next_copy); ++ ++int apple_sftl_gc_copy_done(struct apple_sftl_gc *gc, ++ const struct apple_sftl_gc_copy *receipt, u64 sequence) ++{ ++ const struct apple_sftl_gc_copy *expected = apple_sftl_gc_next_copy(gc); ++ ++ if (!expected || !receipt) ++ return -EINVAL; ++ if (memcmp(expected, receipt, sizeof(*receipt)) || ++ sequence != gc->copied_sequence + expected->length) { ++ gc->stage = APPLE_SFTL_GC_FAILED; ++ return -EBADMSG; ++ } ++ gc->copied_sequence = sequence; ++ if (++gc->copied == gc->nr_copies) ++ gc->stage = APPLE_SFTL_GC_CHECKPOINT; ++ return 0; ++} ++EXPORT_SYMBOL_GPL(apple_sftl_gc_copy_done); ++ ++int apple_sftl_gc_checkpoint(struct apple_sftl_gc *gc, const u32 *blocks, ++ u32 nr_blocks, struct apple_sftl_checkpoint **out) ++{ ++ struct apple_sftl_checkpoint *image; ++ u32 i; ++ int ret; ++ ++ if (!out) ++ return -EINVAL; ++ *out = NULL; ++ if (!gc || gc->stage != APPLE_SFTL_GC_CHECKPOINT || gc->checkpoint) ++ return -EINVAL; ++ if (!blocks || !nr_blocks || nr_blocks > 16) ++ return -EINVAL; ++ for (i = 0; i < nr_blocks; i++) ++ if (blocks[i] == gc->victim || blocks[i] == gc->destination) ++ return -EINVAL; ++ ret = apple_sftl_checkpoint_build(&gc->snapshot, blocks, nr_blocks, ++ gc->max_records, &gc->checkpoint); ++ if (ret) ++ return ret; ++ ret = apple_sftl_checkpoint_build(&gc->snapshot, blocks, nr_blocks, ++ gc->max_records, &image); ++ if (ret) { ++ apple_sftl_checkpoint_free(gc->checkpoint); ++ gc->checkpoint = NULL; ++ return ret; ++ } ++ *out = image; ++ return 0; ++} ++EXPORT_SYMBOL_GPL(apple_sftl_gc_checkpoint); ++ ++int apple_sftl_gc_checkpoint_done(struct apple_sftl_gc *gc, ++ const struct apple_sftl_checkpoint *readback) ++{ ++ const struct apple_sftl_checkpoint *expected; ++ ++ if (!gc || gc->stage != APPLE_SFTL_GC_CHECKPOINT || !gc->checkpoint || !readback) ++ return -EINVAL; ++ expected = gc->checkpoint; ++ if (!readback->records || readback->count != expected->count || ++ readback->first_sequence != expected->first_sequence || ++ readback->sequence != expected->sequence || ++ memcmp(readback->records, expected->records, ++ expected->count * sizeof(*expected->records))) { ++ gc->stage = APPLE_SFTL_GC_FAILED; ++ return -EBADMSG; ++ } ++ gc->stage = APPLE_SFTL_GC_RECOVERY; ++ return 0; ++} ++EXPORT_SYMBOL_GPL(apple_sftl_gc_checkpoint_done); ++ ++int apple_sftl_gc_recovered(struct apple_sftl_gc *gc, ++ const struct apple_sftl_snapshot *recovered) ++{ ++ u32 wanted = 0, found = 0, offset = 0, length; ++ int ret; ++ ++ if (!gc || gc->stage != APPLE_SFTL_GC_RECOVERY) ++ return -EINVAL; ++ ret = sftl_check_map(recovered); ++ if (ret) ++ goto failed; ++ if (recovered->checkpoint_sequence != gc->checkpoint->sequence || ++ recovered->sequence != gc->checkpoint->sequence || ++ recovered->journal_sequence != recovered->sequence || ++ recovered->checkpoint_sequence <= gc->retire_sequence) { ++ ret = -EBADMSG; ++ goto failed; ++ } ++ /* Compare logical mappings even when the cold reader coalesces ranges. */ ++ while (wanted < gc->snapshot.nr_runs && found < recovered->nr_runs) { ++ const struct apple_sftl_extent *a = &gc->snapshot.runs[wanted]; ++ const struct apple_sftl_extent *b = &recovered->runs[found]; ++ u32 a_offset = offset - a->logical, b_offset = offset - b->logical; ++ u32 a_address = a->provider, b_address = b->provider; ++ ++ if (a_address != APPLE_SFTL_NO_ADDRESS) ++ a_address += a_offset; ++ if (b_address != APPLE_SFTL_NO_ADDRESS) ++ b_address += b_offset; ++ if (a_address != b_address) { ++ ret = -EBADMSG; ++ goto failed; ++ } ++ length = min(a->length - a_offset, b->length - b_offset); ++ offset += length; ++ if (offset == a->logical + a->length) ++ wanted++; ++ if (offset == b->logical + b->length) ++ found++; ++ } ++ if (offset != APPLE_SFTL_LOGICAL_PAGES) { ++ ret = -EBADMSG; ++ goto failed; ++ } ++ gc->stage = APPLE_SFTL_GC_VERIFIED; ++ return 0; ++failed: ++ gc->stage = APPLE_SFTL_GC_FAILED; ++ return ret; ++} ++EXPORT_SYMBOL_GPL(apple_sftl_gc_recovered); +diff --git a/drivers/mtd/apple-sftl-reclaim.h b/drivers/mtd/apple-sftl-reclaim.h +new file mode 100644 +index 000000000..e7b110ab5 +--- /dev/null ++++ b/drivers/mtd/apple-sftl-reclaim.h +@@ -0,0 +1,88 @@ ++/* SPDX-License-Identifier: GPL-2.0-only */ ++#ifndef _APPLE_SFTL_RECLAIM_H ++#define _APPLE_SFTL_RECLAIM_H ++ ++#include ++ ++#define APPLE_SFTL_CHUNK 4096U ++#define APPLE_SFTL_META 16U ++#define APPLE_SFTL_STRIDE 8192U ++#define APPLE_SFTL_SUBBLOCKS 2086U ++#define APPLE_SFTL_LOGICAL_PAGES 15625016U ++#define APPLE_SFTL_NO_ADDRESS ((u32)~0U) ++ ++struct apple_sftl; ++ ++struct apple_sftl_extent { ++ u32 logical, length, provider; ++}; ++ ++struct apple_sftl_context_record { ++ u8 meta[APPLE_SFTL_META]; ++ u8 data[APPLE_SFTL_CHUNK]; ++}; ++ ++/* Owned whole-device map and unchanged non-map context payloads. */ ++struct apple_sftl_snapshot { ++ struct apple_sftl_extent *runs; ++ struct apple_sftl_context_record *opaque; ++ u32 nr_runs, nr_opaque; ++ u64 sequence, checkpoint_sequence, journal_sequence; ++}; ++ ++struct apple_sftl_checkpoint { ++ struct apple_sftl_context_record *records; ++ u32 count; ++ u64 first_sequence, sequence; ++}; ++ ++int apple_sftl_snapshot(struct apple_sftl *sftl, size_t max_bytes, ++ struct apple_sftl_snapshot **out); ++void apple_sftl_snapshot_free(struct apple_sftl_snapshot *snapshot); ++int apple_sftl_checkpoint_build(const struct apple_sftl_snapshot *snapshot, ++ const u32 *blocks, u32 nr_blocks, ++ u32 max_records, struct apple_sftl_checkpoint **out); ++void apple_sftl_checkpoint_free(struct apple_sftl_checkpoint *checkpoint); ++int apple_sftl_context_meta(u8 *meta, u8 subtype, u64 sequence, ++ u16 index, u16 entries); ++ ++/* A blank receipt describes an extent checked in full by the caller. */ ++struct apple_sftl_gc_block { ++ u32 block; ++ u16 lanes; ++ bool blank, closed, reserved; ++ u64 last_sequence; ++}; ++ ++struct apple_sftl_gc_copy { ++ u32 logical, length, source, destination; ++}; ++ ++enum apple_sftl_gc_stage { ++ APPLE_SFTL_GC_COPY, ++ APPLE_SFTL_GC_CHECKPOINT, ++ APPLE_SFTL_GC_RECOVERY, ++ APPLE_SFTL_GC_VERIFIED, ++ APPLE_SFTL_GC_FAILED, ++}; ++ ++struct apple_sftl_gc; ++ ++/* These routines plan and validate supplied bytes. They perform no media IO. */ ++int apple_sftl_gc_plan(const struct apple_sftl_snapshot *snapshot, ++ const struct apple_sftl_gc_block *victim, ++ const struct apple_sftl_gc_block *destination, ++ u32 max_records, size_t max_bytes, struct apple_sftl_gc **out); ++void apple_sftl_gc_free(struct apple_sftl_gc *gc); ++enum apple_sftl_gc_stage apple_sftl_gc_stage(const struct apple_sftl_gc *gc); ++const struct apple_sftl_gc_copy *apple_sftl_gc_next_copy(const struct apple_sftl_gc *gc); ++int apple_sftl_gc_copy_done(struct apple_sftl_gc *gc, ++ const struct apple_sftl_gc_copy *receipt, u64 sequence); ++int apple_sftl_gc_checkpoint(struct apple_sftl_gc *gc, const u32 *blocks, ++ u32 nr_blocks, struct apple_sftl_checkpoint **out); ++int apple_sftl_gc_checkpoint_done(struct apple_sftl_gc *gc, ++ const struct apple_sftl_checkpoint *readback); ++int apple_sftl_gc_recovered(struct apple_sftl_gc *gc, ++ const struct apple_sftl_snapshot *recovered); ++ ++#endif +diff --git a/drivers/mtd/apple-sftl-test.c b/drivers/mtd/apple-sftl-test.c +index 91454af2d..1ffcc7477 100644 +--- a/drivers/mtd/apple-sftl-test.c ++++ b/drivers/mtd/apple-sftl-test.c +@@ -8,6 +8,7 @@ + #include + + #include "apple-sftl.h" ++#include "apple-sftl-reclaim.h" + + #define TEST_LOGICAL_PAGES 15625016U + #define TEST_STRIDE 8192U +@@ -58,6 +59,7 @@ struct sftl_fixture { + u16 program_lanes; + u64 sequence_bias; + struct sftl_test_page **written[5]; ++ struct apple_sftl_checkpoint *checkpoint; + }; + + static void sftl_test_meta(u8 *meta, u8 kind, u8 flags, u64 seq, u32 key) +@@ -270,7 +272,18 @@ static int sftl_test_read_page(void *priv, unsigned int bus, unsigned int cau, + } + if (lane > 1) + return 0; +- if (block == 1 && !position) { ++ if (block == 1 && f->checkpoint && position < f->checkpoint->count / 4) { ++ unsigned int i; ++ ++ for (i = 0; i < 4; i++) { ++ const struct apple_sftl_context_record *r; ++ ++ r = &f->checkpoint->records[position * 4 + i]; ++ ++ memcpy(data + i * 4096, r->data, 4096); ++ memcpy(oob + i * 16, r->meta, 16); ++ } ++ } else if (block == 1 && !position) { + sftl_test_checkpoint(f, data, oob); + } else if (block == 2 && !position) { + sftl_test_open(f, data, oob); +@@ -964,6 +977,36 @@ static void sftl_test_append_rejects_invalid_window(struct kunit *test) + apple_sftl_free(sftl); + } + ++static void sftl_test_checkpoint_cold_roundtrip(struct kunit *test) ++{ ++ struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); ++ struct apple_sftl_snapshot *before = NULL, *after = NULL; ++ struct apple_sftl_checkpoint *checkpoint = NULL; ++ struct apple_sftl *sftl = NULL; ++ u32 blocks[] = { 1 }; ++ ++ KUNIT_ASSERT_NOT_NULL(test, f); ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ KUNIT_EXPECT_EQ(test, apple_sftl_snapshot(sftl, 1, &before), -ENOSPC); ++ KUNIT_EXPECT_PTR_EQ(test, before, NULL); ++ KUNIT_ASSERT_EQ(test, apple_sftl_snapshot(sftl, 65536, &before), 0); ++ apple_sftl_free(sftl); ++ sftl = NULL; ++ KUNIT_ASSERT_EQ(test, apple_sftl_checkpoint_build(before, blocks, 1, 16, &checkpoint), 0); ++ f->checkpoint = checkpoint; ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ KUNIT_ASSERT_EQ(test, apple_sftl_snapshot(sftl, 65536, &after), 0); ++ KUNIT_EXPECT_EQ(test, after->checkpoint_sequence, checkpoint->sequence); ++ KUNIT_EXPECT_EQ(test, after->nr_runs, before->nr_runs); ++ KUNIT_EXPECT_MEMEQ(test, after->runs, before->runs, ++ before->nr_runs * sizeof(*before->runs)); ++ KUNIT_EXPECT_MEMEQ(test, after->opaque[0].data, before->opaque[0].data, 4096); ++ apple_sftl_free(sftl); ++ apple_sftl_snapshot_free(after); ++ apple_sftl_snapshot_free(before); ++ apple_sftl_checkpoint_free(checkpoint); ++} ++ + static struct kunit_case apple_sftl_cases[] = { + KUNIT_CASE(sftl_test_append_replay_open), + KUNIT_CASE(sftl_test_append_replay_closed), +@@ -979,6 +1022,7 @@ static struct kunit_case apple_sftl_cases[] = { + KUNIT_CASE(sftl_test_append_refuses_sequence_floor_gap), + KUNIT_CASE(sftl_test_append_rejects_invalid_window), + KUNIT_CASE(sftl_test_complete_medium), ++ KUNIT_CASE(sftl_test_checkpoint_cold_roundtrip), + KUNIT_CASE(sftl_test_scan_after_open_lane_hole), + KUNIT_CASE(sftl_test_runtime_failure_stops_reads), + KUNIT_CASE(sftl_test_reject_incomplete_or_ambiguous), diff --git a/kernel/patches/armv7/mtd/0007-docs-Record-native-PPN-erase-addressing-and-VFL-rota.patch b/kernel/patches/armv7/mtd/0007-docs-Record-native-PPN-erase-addressing-and-VFL-rota.patch new file mode 100644 index 00000000..3fa56e4f --- /dev/null +++ b/kernel/patches/armv7/mtd/0007-docs-Record-native-PPN-erase-addressing-and-VFL-rota.patch @@ -0,0 +1,59 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Shayne <8741557+shaynelegault@users.noreply.github.com> +Date: Wed, 18 Nov 2026 21:34:34 -0600 +Subject: [PATCH] docs: Record native PPN erase addressing and VFL rotation + order + +Document the normal erase geometry and two persisted VFL checkpoints around lane-table copying from source c504eae2a214. Describe the physical ownership and recovery evidence required before the planner can drive destructive I/O. + +Assisted-by: LLM +--- + .../driver-api/mtd/apple-sftl-reclamation.rst | 26 ++++++++++++++----- + 1 file changed, 20 insertions(+), 6 deletions(-) + +diff --git a/Documentation/driver-api/mtd/apple-sftl-reclamation.rst b/Documentation/driver-api/mtd/apple-sftl-reclamation.rst +index e286c6935..31ac3e636 100644 +--- a/Documentation/driver-api/mtd/apple-sftl-reclamation.rst ++++ b/Documentation/driver-api/mtd/apple-sftl-reclamation.rst +@@ -56,6 +56,11 @@ The transaction advances through explicit receipt checks: + * A fresh recovery snapshot must contain the planned whole-device mapping + and the new checkpoint sequence. Equivalent coalescing is accepted. + ++The supplied blank, reserved and lane descriptions are trusted inputs. They ++must come from authoritative VFL and context ownership checks before any ++future destructive IO can use a plan. The present receipt checks cannot ++establish those physical ownership facts. ++ + A mismatched receipt moves the transaction to its failed state. Even a victim + with zero current references must pass checkpoint and cold-recovery stages, + since the current replay contract can still require its obsolete history. +@@ -67,14 +72,23 @@ Outstanding native protocol work + + VFL recovery uses two context groups with four copies per group on this + profile. The native rotation routine at ``0x810a1b68`` checks references into +-the target group before reuse, copies required lane tables and writes a final +-checkpoint page. Its interruption and replacement paths require further +-tracing before a runtime writer can reproduce them. ++the target group before reuse. On its healthy path, it erases the target ++members in SLC view and writes an initial ff/ff checkpoint retaining references ++to the old lane tables. It then copies those tables, updates the references ++and writes a final ff/ff checkpoint. The old source bank survives this path. ++The initial write at 0x810a1e5a and final write at 0x810a1e34 are separate ++persisted steps. Torn-copy selection, retirement and replacement paths still ++require tracing before a runtime writer can reproduce them. + + Offline execution of the pinned native iBEC erase queue establishes queued +-67/D7 commands, final-row 6A/D7, 79 polling and 77/7D completion. Linking its +-row construction to the 64 GB erase granule remains open, along with timeout, +-retirement and partial completion across CAUs. The provider exposes no erase ++67/D7 commands, final-row 6A/D7, 79 polling and 77/7D completion. The native single-block ++wrapper at 0x9ff09a0c shifts the block coordinate by the package's page-address ++bit count. The 13G36 provider builder at 0x8109de94 also supplies CAU and view ++fields. Both saved parameter copies agree: page bits 7, block bits 12 and CAU ++bits 3. A normal block's page-0 row is ``(cau << 19) | (block << 7)``; the SLC ++view adds bit 22. The normal view reports 128 pages per block. Physical erase ++effects, the SLC alias relationship, timeout, retirement and partial completion ++across CAUs remain unqualified. The provider exposes no erase + callback. Enabling physical reclamation requires resolving those semantics + and qualifying the exact candidate with device recovery and interruption + checks. From 1879f8dda67756bd2f2a5fceea89507e1681b47e Mon Sep 17 00:00:00 2001 From: Shayne <8741557+shaynelegault@users.noreply.github.com> Date: Wed, 18 Nov 2026 22:07:25 -0600 Subject: [PATCH 02/11] armv7: Exercise P105 NAND reclamation in the normal write path Export the verified H2FMI erase operation, live page copier and SFTL allocator from the ARMv7 kernel at ccd56d3c440c. Record the synthetic end-to-end tests and the native checkpoint and hardware qualification still required for P105. Assisted-by: LLM --- chips/s5l8942x.toml | 4 +- devices/p105/device.toml | 3 +- docs/bring-up/p105-sftl-checkpoint-gc.md | 119 +- ...Verify-normal-view-block-erase-befor.patch | 336 +++ ...opy-and-verify-live-pages-during-rec.patch | 587 +++++ ...eclaim-closed-data-blocks-in-the-wri.patch | 1960 +++++++++++++++++ 6 files changed, 2959 insertions(+), 50 deletions(-) create mode 100644 kernel/patches/armv7/mtd/0008-mtd-apple-h2fmi-Verify-normal-view-block-erase-befor.patch create mode 100644 kernel/patches/armv7/mtd/0009-mtd-apple-sftl-Copy-and-verify-live-pages-during-rec.patch create mode 100644 kernel/patches/armv7/mtd/0010-mtd-apple-sftl-Reclaim-closed-data-blocks-in-the-wri.patch diff --git a/chips/s5l8942x.toml b/chips/s5l8942x.toml index 6c46e961..2677cc0f 100644 --- a/chips/s5l8942x.toml +++ b/chips/s5l8942x.toml @@ -16,6 +16,6 @@ platform = "P105 on a Cascadia-based build" stages = "Project Cascadia P105 boot chain" reference_version = "Project Cascadia builds its A5 boot chain from 8.4.1 / 12H321" nand_reads = "Measured P105 64 GB PPN geometry has eight CAUs per bus, 2088 blocks per CAU and 128 normal pages per block; exact ARMv7 read-topic qualification is pending (docs/bring-up/p105-nand-reads.md)." -nand_program = "The H2FMI driver programs complete PPN pages only into erased destinations, verifies full readback and has no erase operation; hardware qualification of this revision is pending (docs/bring-up/p105-nand-program.md)." -nand_checkpoint_gc = "The 64 GB P105 SwissPPNFTL planner checks relocation and checkpoint results offline. Physical erase and reuse await qualification (docs/bring-up/p105-sftl-checkpoint-gc.md)." +nand_program = "The H2FMI driver programs complete PPN pages only into erased destinations, verifies full readback and leaves physical erase disabled pending qualification (docs/bring-up/p105-nand-program.md)." +nand_checkpoint_gc = "The 64 GB SwissPPNFTL writer verifies ownership before recycling a retired block on a qualified provider. Reclamation is tested on synthetic media. Physical P105 erase and reuse await qualification (docs/bring-up/p105-sftl-checkpoint-gc.md)." usb_phy = "P105 uses a 24 MHz reference; the proposed S5L8942X cold-handoff callbacks await an exact-revision boot (docs/bring-up/p105-usb-handoff.md)." diff --git a/devices/p105/device.toml b/devices/p105/device.toml index 59eff59a..4467457d 100644 --- a/devices/p105/device.toml +++ b/devices/p105/device.toml @@ -29,7 +29,8 @@ recipe = "firmware.json" # (OrchardLinux/orchard#93), freed space is never reused and writes fail once # the blank flash runs out. Drop this table when it lands; the installer # warns from it and allows the install either way. -# The offline checkpoint planner is described in docs/bring-up/p105-sftl-checkpoint-gc.md. +# The P105 does not advertise verified erase. Synthetic reclamation results +# are recorded in docs/bring-up/p105-sftl-checkpoint-gc.md. [storage] reclaims = false diff --git a/docs/bring-up/p105-sftl-checkpoint-gc.md b/docs/bring-up/p105-sftl-checkpoint-gc.md index debbe707..20745fe3 100644 --- a/docs/bring-up/p105-sftl-checkpoint-gc.md +++ b/docs/bring-up/p105-sftl-checkpoint-gc.md @@ -1,50 +1,75 @@ -# P105 SwissPPNFTL checkpoint encoder and reclamation planner +# P105 SwissPPNFTL checkpoint and reclamation -The 64 GB P105 shares one flash translation layer across the native iOS and -Linux partitions. The append writer returns `ENOSPC` after it exhausts -verified blank blocks. Orchard's installer warns about that limit and allows -installation. See the [append writer](p105-sftl-append.md) and +The measured 64 GB P105 shares one flash translation layer across iOS and +Linux. Its H2FMI profile still appends to verified blank blocks and returns +`ENOSPC` when they run out. The installer continues to warn about this +limit. See the [append writer](p105-sftl-append.md) and [P105 device profile](../../devices/p105/device.toml). -## Kernel source - -The port uses the merged append writer on -[OrchardLinux/linux armv7 at 0c650327](https://github.com/OrchardLinux/linux/commit/0c6503279c702b44a340c37f072cd9170d6bf4a4). -The [ported kernel source](https://github.com/shaynelegault/linux/commit/e823a434a766b09c5e5b2ad4586b7a27085b1828) -contains the planner and its fixtures. The kernel patches live in `kernel/patches/armv7/mtd/`, selected by -`kernel/series/apple-armv7`. The original planner and native rotation -notes are in [the source draft at c504eae2](https://github.com/shaynelegault/linux/commit/c504eae2a214915dcf5d2c4856c52e7a6ac79d2d). - -The snapshot covers all 15,625,016 logical keys, including the sixteen -internal keys beyond the exported disk. The encoder preserves the opaque -accounting records, updates the context-block list and serializes range -fragments with 48-bit sequence numbers. The planner budgets destination -space for live data and the block footer. It checks copy and checkpoint -readback results supplied by its caller, then compares the whole map from -a fresh recovery against the planned map. These helpers operate offline. - -## Open work - -- The runtime writer needs authoritative VFL and context ownership data to - select blocks and lanes, verify relocated data, and return an erased block - to the allocator. -- Installing a checkpoint requires native updates to accounting fields. - The reader needs to select the previous complete checkpoint and replay - interrupted saves when the newest checkpoint is incomplete. -- The native VFL rotation switches banks in two persisted steps. An initial - checkpoint retains the old table references while the tables are copied. - A final checkpoint publishes the new references. Recovery after a torn - copy or a block retirement still needs qualification. -- A controller erase and reuse cycle still needs a test on the measured - hardware, including the physical erase extent, the relationship between - normal and SLC views, and recovery after partial completion. The P105 - profile records both storage reads and writes as `untested`. - -The native format evidence comes from IOFlashStorage-730 on build 13G36 -and the [pinned Cascadia NAND research](https://github.com/rzxvx/Project-Cascadia/blob/c20b85ebb951c704d7e3a664c378dde8276e3928/docs/research/p105-nand.md). -On the ported source, the host sanitizer run passed 22 reader/writer cases -and eight planner cases. The differential tests matched 24 native metadata -vectors and round-tripped 209 encoded maps. Five ARMv7 objects compiled -with warnings treated as errors, including both KUnit fixtures. These checks -used synthetic pages and covered encoding, receipt validation and cold map -recovery. Physical erase and reboot recovery remain untested. +## Write path + +The allocator checks the map for all 15,625,016 logical keys, including the +16 internal keys beyond the exported disk. On a provider that supports +verified erase, it can recycle a fully obsolete block. The block must have a +valid native footer, predate the saved checkpoint, and be absent from both +the checkpoint map and the current map. The allocator verifies each record +against its footer and compares the active map with a fresh load. It excludes +bootstrap and context blocks, unknown lane flags, ambiguous checkpoint +generations and blocks that may still be needed for journal replay. + +A writer with an atomic checkpoint installer can also compact a partially +live block. The allocator keeps a verified blank landing block while space +is available. Under space pressure it copies live records into that block, +checks the data and out-of-band metadata after each program, and allows room +for the pending write and block footer. It installs the new checkpoint, +compares the checkpoint readback, then reloads the medium and verifies the +entire mapping. Only then can it erase the old block. The waiting write +uses the free tail of the landing block. A subsequent write can claim +the erased block. Without a checkpoint installer, foreground writes retain +the available blank capacity. + +The H2FMI erase operation checks both controller status replies and reads +all data and out-of-band bytes from all 128 pages of each affected lane. +The allocator also verifies the whole logical block before reuse. An +indeterminate erase or failed verification stops the writer instance. + +## Source and verification + +The ARMv7 patches in `kernel/patches/armv7/mtd/` build on the merged +[ARMv7 append writer](https://github.com/OrchardLinux/linux/commit/0c6503279c702b44a340c37f072cd9170d6bf4a4). +The [kernel source](https://github.com/shaynelegault/linux/commit/ccd56d3c440c0e84d1581667d43a0437b3e221f8) +contains KUnit fixtures for both the ordinary write path and the planner. + +The mixed-block fixture leaves five live 4 KiB records in a closed block. +A normal write triggers relocation and a synthetic two-slot checkpoint +installation. The test reloads that checkpoint through the real reader, +checks all five records and the triggering write, then writes into the +reclaimed block and reloads again. Other fixtures exercise incomplete +footers, corruption, failed programs, partial erase and a checkpoint that +was never published. A simulated controller transaction checks every byte of +the erased lane, including the last byte of the last page. + +On this revision, 31 reader/writer cases and nine planner cases passed under +ASan and UBSan. Differential checks matched 24 native metadata vectors, +round-tripped 209 maps and verified 66 relocation sizes. Eleven ARMv7 object +configurations compiled with warnings treated as errors. + +The checkpoint metadata and footer evidence comes from IOFlashStorage-730 on +build 13G36 and the [pinned Cascadia NAND research](https://github.com/rzxvx/Project-Cascadia/blob/c20b85ebb951c704d7e3a664c378dde8276e3928/docs/research/p105-nand.md). +The original [planner and rotation notes](https://github.com/shaynelegault/linux/commit/c504eae2a214915dcf5d2c4856c52e7a6ac79d2d) +document the starting evidence. + +## Remaining before hardware enablement + +The checkpoint installer used by the tests models an atomic switch. A native +P105 installer must update the accounting fields, keep the old table +references during the first VFL save, publish the new references in the +second save, and recover correctly if power fails during either save or +during table copying. The current encoder preserves the opaque native +accounting records. + +Physical qualification must establish the exact erase footprint in both +normal and SLC views, exclusive ownership across raw MTD and SFTL operations, +and recovery after a partial erase or block retirement. End-to-end testing +on the measured P105 must verify cold-boot recovery and preservation of +existing iOS data before the device can advertise reclamation. diff --git a/kernel/patches/armv7/mtd/0008-mtd-apple-h2fmi-Verify-normal-view-block-erase-befor.patch b/kernel/patches/armv7/mtd/0008-mtd-apple-h2fmi-Verify-normal-view-block-erase-befor.patch new file mode 100644 index 00000000..6ce40f2a --- /dev/null +++ b/kernel/patches/armv7/mtd/0008-mtd-apple-h2fmi-Verify-normal-view-block-erase-befor.patch @@ -0,0 +1,336 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Shayne <8741557+shaynelegault@users.noreply.github.com> +Date: Wed, 18 Nov 2026 21:58:40 -0600 +Subject: [PATCH] mtd: apple-h2fmi: Verify normal-view block erase before reuse + +Add a provider erase operation for a single bus, CAU and physical block. Check the queue and operation status, then read all 128 physical pages and their OOB before returning success. Stop the bus on ambiguous completion. Exclude bootstrap blocks and keep the P105 match data disabled pending hardware qualification. + +Assisted-by: LLM +--- + drivers/mtd/devices/mtd-apple-h2fmi-test.c | 43 ++++++++ + drivers/mtd/devices/mtd-apple-h2fmi.c | 108 ++++++++++++++++++--- + drivers/mtd/devices/mtd-apple-h2fmi.h | 21 ++++ + include/linux/mtd/apple-ppn.h | 7 ++ + 4 files changed, 166 insertions(+), 13 deletions(-) + +diff --git a/drivers/mtd/devices/mtd-apple-h2fmi-test.c b/drivers/mtd/devices/mtd-apple-h2fmi-test.c +index 419c04cc8..b22c500e3 100644 +--- a/drivers/mtd/devices/mtd-apple-h2fmi-test.c ++++ b/drivers/mtd/devices/mtd-apple-h2fmi-test.c +@@ -101,6 +101,30 @@ static void h2fmi_row_test(struct kunit *test) + KUNIT_EXPECT_EQ(test, row, 0xc27ffU); + } + ++static void h2fmi_erase_row_test(struct kunit *test) ++{ ++ u8 params[H2FMI_PARAMS_SIZE]; ++ struct h2fmi_geometry g; ++ u32 row = 0xdeadbeef; ++ ++ h2fmi_test_params(params, ppn_16gb); ++ KUNIT_ASSERT_EQ(test, h2fmi_parse_geometry(params, sizeof(params), &g), 0); ++ KUNIT_EXPECT_EQ(test, h2fmi_erase_row(&g, 0, 1, &row), -EOPNOTSUPP); ++ h2fmi_test_params(params, ppn_64gb); ++ KUNIT_ASSERT_EQ(test, h2fmi_parse_geometry(params, sizeof(params), &g), 0); ++ KUNIT_EXPECT_EQ(test, h2fmi_erase_row(&g, 8, 1, &row), -EINVAL); ++ KUNIT_EXPECT_EQ(test, h2fmi_erase_row(&g, 0, 2088, &row), -EINVAL); ++ KUNIT_EXPECT_EQ(test, h2fmi_erase_row(&g, 0, 0, &row), -EPERM); ++ KUNIT_EXPECT_EQ(test, h2fmi_erase_row(&g, 7, 2056, &row), -EPERM); ++ KUNIT_EXPECT_EQ(test, h2fmi_erase_row(&g, 7, 2086, &row), -EPERM); ++ KUNIT_EXPECT_EQ(test, h2fmi_erase_row(&g, 7, 2087, &row), -EPERM); ++ KUNIT_EXPECT_EQ(test, row, 0xdeadbeefU); ++ KUNIT_ASSERT_EQ(test, h2fmi_erase_row(&g, 0, 2048, &row), 0); ++ KUNIT_EXPECT_EQ(test, row, 0x40000U); ++ KUNIT_ASSERT_EQ(test, h2fmi_erase_row(&g, 1, 1, &row), 0); ++ KUNIT_EXPECT_EQ(test, row, 0x80080U); ++} ++ + static void h2fmi_slc_row_test(struct kunit *test) + { + u8 params[H2FMI_PARAMS_SIZE]; +@@ -137,6 +161,23 @@ static void h2fmi_slc_row_test(struct kunit *test) + KUNIT_EXPECT_EQ(test, h2fmi_slc_row(&g, 0, 0, 128, &row), -EINVAL); + } + ++static void h2fmi_raw_page_erased_test(struct kunit *test) ++{ ++ u8 *raw = kunit_kmalloc(test, H2FMI_PAGE_RAW, GFP_KERNEL); ++ unsigned int status; ++ ++ KUNIT_ASSERT_NOT_NULL(test, raw); ++ memset(raw, 0xff, H2FMI_PAGE_RAW); ++ for (status = 0; status <= 0xff; status++) ++ KUNIT_EXPECT_EQ(test, h2fmi_raw_page_erased(status, raw), ++ status == PPN_ST_ERASED); ++ raw[H2FMI_CHUNK_LO] = 0xfe; ++ KUNIT_EXPECT_FALSE(test, h2fmi_raw_page_erased(PPN_ST_ERASED, raw)); ++ raw[H2FMI_CHUNK_LO] = 0xff; ++ raw[H2FMI_PAGE_RAW - 1] = 0x7f; ++ KUNIT_EXPECT_FALSE(test, h2fmi_raw_page_erased(PPN_ST_ERASED, raw)); ++} ++ + static void h2fmi_status_test(struct kunit *test) + { + unsigned int status; +@@ -170,6 +211,8 @@ static struct kunit_case h2fmi_test_cases[] = { + KUNIT_CASE(h2fmi_geometry_test), + KUNIT_CASE(h2fmi_invalid_geometry_test), + KUNIT_CASE(h2fmi_row_test), ++ KUNIT_CASE(h2fmi_erase_row_test), ++ KUNIT_CASE(h2fmi_raw_page_erased_test), + KUNIT_CASE(h2fmi_status_test), + KUNIT_CASE(h2fmi_slc_row_test), + KUNIT_CASE(h2fmi_program_status_test), +diff --git a/drivers/mtd/devices/mtd-apple-h2fmi.c b/drivers/mtd/devices/mtd-apple-h2fmi.c +index 46a6bc603..661bd0d1f 100644 +--- a/drivers/mtd/devices/mtd-apple-h2fmi.c ++++ b/drivers/mtd/devices/mtd-apple-h2fmi.c +@@ -22,8 +22,8 @@ + * CAU. Raw MTD offsets are independent of the iOS logical block addresses. + * + * Pages are read and programmed whole, data and metadata together. The +- * driver has no erase: a page is only programmed when it reads back erased, +- * and every program is read back and compared before it is reported done. ++ * raw MTD devices provide no erase. A page is only programmed when it ++ * reads back erased, and every program is read back and compared. + * Logical-address translation and allocation belong to the FTL above. + */ + +@@ -38,6 +38,7 @@ + #include + #include + #include ++#include + #include + + #include "mtd-apple-h2fmi.h" +@@ -73,7 +74,7 @@ + #define FMC_STATUS_MASK 0x4c + + /* Every command sequence the driver issues. A sequence that is not listed +- * does not run; in particular there is no erase. */ ++ * does not run. Only the single-row final erase command is needed. */ + static const u8 h2fmi_ops[][2] = { + { 0x70, 0x00 }, /* read status */ + { 0x92, 0x97 }, /* PPN device parameters */ +@@ -83,7 +84,8 @@ static const u8 h2fmi_ops[][2] = { + { 0x0a, 0x37 }, /* PPN page read */ + { 0x8a, 0x00 }, /* PPN program, address phase */ + { 0x17, 0x00 }, /* PPN program confirm */ +- { 0x79, 0x00 }, /* PPN program status */ ++ { 0x79, 0x00 }, /* PPN mutation status */ ++ { 0x6a, 0xd7 }, /* PPN erase, one normal row */ + }; + + struct h2fmi_bus { +@@ -102,6 +104,10 @@ struct h2fmi_die { + u8 cau; + }; + ++struct h2fmi_variant { ++ bool normal_erase_qualified; ++}; ++ + struct h2fmi { + struct device *dev; + struct h2fmi_bus bus[H2FMI_BUSES]; +@@ -109,6 +115,7 @@ struct h2fmi { + struct h2fmi_die *die; + unsigned int ndies; + struct platform_device *sftl; ++ struct apple_ppn_ops ppn_ops; + }; + + /* +@@ -418,7 +425,7 @@ static int h2fmi_data_in(struct h2fmi *fmi, struct h2fmi_bus *b, + return 0; + } + +-/* Program status uses 79, as in iBEC's write_bootpage path. */ ++/* Both page programming and the iBEC erase queue use status command 79. */ + static int h2fmi_write_status(struct h2fmi *fmi, struct h2fmi_bus *b, u8 *status) + { + int ret = h2fmi_cmd1(fmi, b, 0x79); +@@ -508,8 +515,7 @@ static int h2fmi_program_page(struct h2fmi *fmi, struct h2fmi_bus *b, + b->page, H2FMI_PAGE_RAW, &status); + if (ret) + goto out; +- if (status != PPN_ST_ERASED || +- memchr_inv(b->page, 0xff, H2FMI_PAGE_RAW)) { ++ if (!h2fmi_raw_page_erased(status, b->page)) { + ret = -EEXIST; + goto out; + } +@@ -647,17 +653,84 @@ static int h2fmi_ppn_program(void *priv, unsigned int bus, unsigned int cau, + return h2fmi_program_page(fmi, &fmi->bus[bus], cau, block, page, data, oob); + } + +-static const struct apple_ppn_ops h2fmi_ppn_ops = { +- .read_page = h2fmi_ppn_read, +- .program_page = h2fmi_ppn_program, +-}; ++/* ++ * The single-row iBEC erase queue sends 6A/D7, checks queue status (79) ++ * and operation status (77/7D), then ends the operation (77). Do not ++ * retry an uncertain erase. The caller holds the bus lock through readback. ++ */ ++static int h2fmi_erase_transaction(struct h2fmi *fmi, struct h2fmi_bus *b, u32 row) ++{ ++ u32 on, if_ctrl; ++ u8 queued = 0, completed = 0; ++ int ret; ++ ++ if (b->failed) ++ return -EIO; ++ on = readl(b->fmc + FMC_ON); ++ if_ctrl = readl(b->fmc + FMC_IF_CTRL); ++ if (on != 5) ++ return -EIO; ++ ++ writel(1u << H2FMI_CE, b->fmc + FMC_CE_CTRL); ++ h2fmi_clear_irqs(b); ++ ret = h2fmi_cmd_addr_cmd(fmi, b, 0x6a, 0xd7, row, 4); ++ if (!ret) ++ ret = h2fmi_write_status(fmi, b, &queued); ++ if (!ret) ++ ret = h2fmi_status(fmi, b, &completed); ++ if (!ret) ++ ret = h2fmi_cmd1(fmi, b, 0x77); ++ writel(0, b->fmc + FMC_CE_CTRL); ++ h2fmi_device_reset(b, on, if_ctrl); ++ if (!ret && (queued != PPN_ST_GOOD || completed != PPN_ST_GOOD)) { ++ dev_err(fmi->dev, "PPN erase row %08x: queue %02x, completion %02x\n", ++ row, queued, completed); ++ ret = -EIO; ++ } ++ return ret; ++} ++ ++static int h2fmi_ppn_erase(void *priv, unsigned int bus, unsigned int cau, ++ unsigned int block) ++{ ++ struct h2fmi *fmi = priv; ++ struct h2fmi_bus *b; ++ unsigned int page; ++ u32 row; ++ u8 status; ++ int ret; ++ ++ if (bus >= H2FMI_BUSES) ++ return -EINVAL; ++ b = &fmi->bus[bus]; ++ ret = h2fmi_erase_row(&b->geometry, cau, block, &row); ++ if (ret) ++ return ret; ++ ++ mutex_lock(&b->lock); ++ ret = h2fmi_erase_transaction(fmi, b, row); ++ for (page = 0; !ret && page < b->geometry.pages; page++) { ++ ret = h2fmi_read_transaction(fmi, b, 0x0a, 0x37, row + page, 4, ++ b->page, H2FMI_PAGE_RAW, &status); ++ if (!ret && !h2fmi_raw_page_erased(status, b->page)) { ++ dev_err(fmi->dev, "PPN erase readback row %08x: status %02x\n", ++ row + page, status); ++ ret = -EIO; ++ } ++ } ++ if (ret) ++ b->failed = true; ++ mutex_unlock(&b->lock); ++ return ret; ++} + + static int h2fmi_register_sftl(struct h2fmi *fmi) + { ++ const struct h2fmi_variant *variant = device_get_match_data(fmi->dev); + const struct h2fmi_geometry *g = &fmi->bus[0].geometry; + struct apple_ppn provider = { + .dev = fmi->dev, +- .ops = &h2fmi_ppn_ops, ++ .ops = &fmi->ppn_ops, + .priv = fmi, + .buses = H2FMI_BUSES, + .caus = g->caus, +@@ -669,6 +742,10 @@ static int h2fmi_register_sftl(struct h2fmi *fmi) + if (!IS_ENABLED(CONFIG_MTD_APPLE_SFTL) || g->caus != 8 || + g->blocks != 2088 || g->pages != 128 || g->slc_pages != 64) + return 0; ++ fmi->ppn_ops.read_page = h2fmi_ppn_read; ++ fmi->ppn_ops.program_page = h2fmi_ppn_program; ++ if (variant && variant->normal_erase_qualified) ++ fmi->ppn_ops.erase_block = h2fmi_ppn_erase; + fmi->sftl = platform_device_register_data(fmi->dev, "apple-sftl", + PLATFORM_DEVID_AUTO, + &provider, sizeof(provider)); +@@ -947,8 +1024,13 @@ static void h2fmi_remove(struct platform_device *pdev) + mtd_device_unregister(&fmi->mtd[i]); + } + ++/* Normal/SLC aliasing and the physical erase extent remain unqualified. */ ++static const struct h2fmi_variant s5l8940x_ppn = { ++ .normal_erase_qualified = false, ++}; ++ + static const struct of_device_id h2fmi_of_match[] = { +- { .compatible = "apple,s5l8940x-fmi" }, ++ { .compatible = "apple,s5l8940x-fmi", .data = &s5l8940x_ppn }, + { } + }; + MODULE_DEVICE_TABLE(of, h2fmi_of_match); +diff --git a/drivers/mtd/devices/mtd-apple-h2fmi.h b/drivers/mtd/devices/mtd-apple-h2fmi.h +index aeb812f33..1ebcade9d 100644 +--- a/drivers/mtd/devices/mtd-apple-h2fmi.h ++++ b/drivers/mtd/devices/mtd-apple-h2fmi.h +@@ -86,6 +86,22 @@ static inline int h2fmi_page_row(const struct h2fmi_geometry *g, + return 0; + } + ++/* The single-row erase trace is available only for the 64 GB package. */ ++static inline int h2fmi_erase_row(const struct h2fmi_geometry *g, ++ unsigned int cau, unsigned int block, u32 *row) ++{ ++ if (g->caus != 8 || g->blocks != 2088 || g->pages != 128 || ++ g->slc_pages != 64 || g->page_bits != 7 || ++ g->block_bits != 12 || g->cau_bits != 3) ++ return -EOPNOTSUPP; ++ if (cau >= g->caus || block >= g->blocks) ++ return -EINVAL; ++ /* SLC-view bootstrap members may occupy any of the last 32 blocks. */ ++ if (!block || block >= g->blocks - 32) ++ return -EPERM; ++ return h2fmi_page_row(g, cau, block, 0, row); ++} ++ + /* The VFL bootstrap's packed requests set the bit above the CAU field. + * Keep this view separate from normal MTD offsets. Its address encoding + * follows the saved firmware; live context selection is a separate step. +@@ -120,6 +136,11 @@ static inline int h2fmi_page_status(u8 status) + } + } + ++static inline bool h2fmi_raw_page_erased(u8 status, const u8 *raw) ++{ ++ return status == PPN_ST_ERASED && !memchr_inv(raw, 0xff, H2FMI_PAGE_RAW); ++} ++ + /* CMD 79 reports ready at bit 6 and program failure at bits 0 and 4. */ + static inline int h2fmi_program_status(u8 status) + { +diff --git a/include/linux/mtd/apple-ppn.h b/include/linux/mtd/apple-ppn.h +index 73fba467d..04b4b935a 100644 +--- a/include/linux/mtd/apple-ppn.h ++++ b/include/linux/mtd/apple-ppn.h +@@ -18,6 +18,13 @@ struct apple_ppn_ops { + int (*read_page)(void *priv, unsigned int bus, unsigned int cau, + unsigned int block, unsigned int page, bool context, + u8 *data, u8 *oob, u8 *status); ++ /* Optional. Caller owns the complete physical erase extent in all views. ++ * Success means one normal-view CAU/block was erased and every byte ++ * of data and OOB read back blank. On failure the extent may have ++ * changed and must not be returned to the allocator. ++ */ ++ int (*erase_block)(void *priv, unsigned int bus, unsigned int cau, ++ unsigned int block); + }; + + struct apple_ppn { diff --git a/kernel/patches/armv7/mtd/0009-mtd-apple-sftl-Copy-and-verify-live-pages-during-rec.patch b/kernel/patches/armv7/mtd/0009-mtd-apple-sftl-Copy-and-verify-live-pages-during-rec.patch new file mode 100644 index 00000000..38b6fbe5 --- /dev/null +++ b/kernel/patches/armv7/mtd/0009-mtd-apple-sftl-Copy-and-verify-live-pages-during-rec.patch @@ -0,0 +1,587 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Shayne <8741557+shaynelegault@users.noreply.github.com> +Date: Wed, 18 Nov 2026 22:02:08 -0600 +Subject: [PATCH] mtd: apple-sftl: Copy and verify live pages during + reclamation + +Pack live ordinary data into complete pages with native sequence metadata. Check destination blankness and read back the full data and OOB after each program. Preserve mapping order when a page needs padding, honor the measured source footer width, and stop after any uncertain I/O. Compare live and cold maps before authorizing a retired block for reuse. + +Assisted-by: LLM +--- + drivers/mtd/apple-sftl-reclaim-test.c | 152 ++++++++++++++++- + drivers/mtd/apple-sftl-reclaim.c | 228 ++++++++++++++++++++++---- + drivers/mtd/apple-sftl-reclaim.h | 27 ++- + 3 files changed, 374 insertions(+), 33 deletions(-) + +diff --git a/drivers/mtd/apple-sftl-reclaim-test.c b/drivers/mtd/apple-sftl-reclaim-test.c +index c26d8b43a..d451b5370 100644 +--- a/drivers/mtd/apple-sftl-reclaim-test.c ++++ b/drivers/mtd/apple-sftl-reclaim-test.c +@@ -1,5 +1,5 @@ + // SPDX-License-Identifier: GPL-2.0-only +-/* Synthetic checkpoints and supplied IO receipts; no media mutations. */ ++/* Checkpoint and reclamation tests on synthetic flash. */ + #include + #include + #include +@@ -121,14 +121,14 @@ static void reclaim_gc_transaction(struct kunit *test) + KUNIT_ASSERT_NOT_NULL(test, copy); + KUNIT_EXPECT_EQ(test, copy->logical, 0U); + KUNIT_EXPECT_EQ(test, copy->length, 2U); +- KUNIT_EXPECT_EQ(test, copy->destination, 7 * APPLE_SFTL_STRIDE); +- KUNIT_ASSERT_EQ(test, apple_sftl_gc_copy_done(gc, copy, 202), 0); ++ KUNIT_EXPECT_EQ(test, copy->destination, 7 * APPLE_SFTL_STRIDE + 2); ++ KUNIT_ASSERT_EQ(test, apple_sftl_gc_copy_done(gc, copy, 204), 0); + KUNIT_EXPECT_EQ(test, apple_sftl_gc_stage(gc), APPLE_SFTL_GC_CHECKPOINT); + KUNIT_ASSERT_EQ(test, apple_sftl_gc_checkpoint(gc, blocks, 1, &image), 0); + KUNIT_ASSERT_EQ(test, apple_sftl_gc_checkpoint_done(gc, image), 0); + KUNIT_EXPECT_EQ(test, apple_sftl_gc_stage(gc), APPLE_SFTL_GC_RECOVERY); + memcpy(recovered_runs, runs, sizeof(runs)); +- recovered_runs[0].provider = 7 * APPLE_SFTL_STRIDE; ++ recovered_runs[0].provider = 7 * APPLE_SFTL_STRIDE + 2; + recovered = snapshot; + recovered.runs = recovered_runs; + recovered.sequence = image->sequence; +@@ -306,9 +306,153 @@ static void reclaim_gc_boundaries(struct kunit *test) + ret = apple_sftl_gc_plan(&snapshot, &victim, &destination, 4, 65536, &gc); + KUNIT_EXPECT_EQ(test, ret, -EBADMSG); + KUNIT_EXPECT_PTR_EQ(test, gc, NULL); ++ /* A short native footer leaves extra source chunks available for data. */ ++ runs[0].provider = victim.block * APPLE_SFTL_STRIDE + 1024 - 5; ++ victim.footer_chunks = 4; ++ KUNIT_ASSERT_EQ(test, apple_sftl_gc_plan(&snapshot, &victim, &destination, ++ 4, 65536, &gc), 0); ++ apple_sftl_gc_free(gc); ++ runs[0].provider++; ++ KUNIT_EXPECT_EQ(test, apple_sftl_gc_plan(&snapshot, &victim, &destination, ++ 4, 65536, &gc), -EBADMSG); ++ victim.footer_chunks = 5; ++ KUNIT_EXPECT_EQ(test, apple_sftl_gc_plan(&snapshot, &victim, &destination, ++ 4, 65536, &gc), -EINVAL); ++} ++ ++enum copy_fault { ++ COPY_GOOD, COPY_BAD_KEY, COPY_FLAGGED, COPY_KIND2, COPY_ZERO_STAMP, ++ COPY_BUSY, COPY_PROGRAM_ERROR, ++ COPY_DATA_MISMATCH, COPY_OOB_MISMATCH, ++}; ++ ++struct copy_medium { ++ struct apple_sftl_gc_page pages[2]; ++ enum copy_fault fault; ++ unsigned int programs; ++}; ++ ++static int copy_read(void *priv, u32 addr, struct apple_sftl_gc_page *page) ++{ ++ struct copy_medium *m = priv; ++ unsigned int i; ++ ++ if (addr == 4 * APPLE_SFTL_STRIDE || addr == 4 * APPLE_SFTL_STRIDE + 4) { ++ for (i = 0; i < 4; i++) { ++ u8 *meta = page->oob + i * APPLE_SFTL_META; ++ u32 chunk = addr % APPLE_SFTL_STRIDE + i; ++ ++ memset(page->data + i * APPLE_SFTL_CHUNK, chunk, APPLE_SFTL_CHUNK); ++ put_unaligned_le64((m->fault == COPY_ZERO_STAMP ? 0ULL : 80ULL + chunk) << 16, meta); ++ meta[0] = m->fault == COPY_KIND2 ? 2 : 1; ++ meta[1] = m->fault == COPY_FLAGGED; ++ put_unaligned_le32(m->fault == COPY_BAD_KEY ? 99 : chunk - 1, meta + 8); ++ put_unaligned_le32(0, meta + 12); ++ } ++ return 0; ++ } ++ if (addr != 7 * APPLE_SFTL_STRIDE && addr != 7 * APPLE_SFTL_STRIDE + 4) ++ return -ERANGE; ++ i = (addr % APPLE_SFTL_STRIDE) / 4; ++ memcpy(page, &m->pages[i], sizeof(*page)); ++ if (i == 1 && m->programs == 2) { ++ if (m->fault == COPY_DATA_MISMATCH) ++ page->data[0] ^= 1; ++ if (m->fault == COPY_OOB_MISMATCH) ++ page->oob[63] ^= 1; ++ } ++ return 0; ++} ++ ++static int copy_program(void *priv, u32 addr, const struct apple_sftl_gc_page *page) ++{ ++ struct copy_medium *m = priv; ++ unsigned int i = m->programs; ++ ++ if (i >= 2 || addr != 7 * APPLE_SFTL_STRIDE + 4 * i) ++ return -EINVAL; ++ memcpy(&m->pages[i], page, sizeof(*page)); ++ m->programs++; ++ return i && m->fault == COPY_PROGRAM_ERROR ? -EIO : 0; ++} ++ ++static void reclaim_copy_live_pages(struct kunit *test) ++{ ++ static const struct apple_sftl_gc_io io = { .read = copy_read, .program = copy_program }; ++ static const u32 keys[] = { 0, 0, 0, 0, 1, 2, 4, 5 }; ++ const u32 dest = 7 * APPLE_SFTL_STRIDE; ++ struct apple_sftl_context_record *opaque = kunit_kmalloc(test, sizeof(*opaque), GFP_KERNEL); ++ struct copy_medium *m = kunit_kmalloc(test, sizeof(*m), GFP_KERNEL); ++ struct apple_sftl_extent runs[4], relocated[4]; ++ struct apple_sftl_snapshot snapshot, recovered; ++ struct apple_sftl_gc_block victim = { ++ .block = 4, .lanes = 3, .closed = true, .last_sequence = 199, ++ }; ++ struct apple_sftl_gc_block destination = { .block = 7, .lanes = 3, .blank = true }; ++ u32 context_block = 5; ++ unsigned int fault, i; ++ ++ KUNIT_ASSERT_NOT_NULL(test, opaque); ++ KUNIT_ASSERT_NOT_NULL(test, m); ++ reclaim_source(&snapshot, opaque, runs, 4); ++ runs[0] = (struct apple_sftl_extent) { 0, 3, 4 * APPLE_SFTL_STRIDE + 1 }; ++ runs[1] = (struct apple_sftl_extent) { 3, 1, 6 * APPLE_SFTL_STRIDE }; ++ runs[2] = (struct apple_sftl_extent) { 4, 2, 4 * APPLE_SFTL_STRIDE + 5 }; ++ runs[3] = (struct apple_sftl_extent) { ++ 6, APPLE_SFTL_LOGICAL_PAGES - 6, APPLE_SFTL_NO_ADDRESS, ++ }; ++ for (fault = COPY_GOOD; fault <= COPY_OOB_MISMATCH; fault++) { ++ struct apple_sftl_gc *gc; ++ struct apple_sftl_checkpoint *image; ++ int ret; ++ ++ memset(m->pages, 0xff, sizeof(m->pages)); ++ m->fault = fault; ++ m->programs = 0; ++ if (fault == COPY_BUSY) ++ m->pages[0].oob[63] = 0; ++ KUNIT_ASSERT_EQ(test, apple_sftl_gc_plan(&snapshot, &victim, &destination, ++ 4, 65536, &gc), 0); ++ ret = apple_sftl_gc_relocate(gc, &io, m); ++ if (fault != COPY_GOOD) { ++ KUNIT_EXPECT_EQ(test, ret, fault == COPY_PROGRAM_ERROR ? -EIO : -EBADMSG); ++ KUNIT_EXPECT_EQ(test, apple_sftl_gc_stage(gc), APPLE_SFTL_GC_FAILED); ++ KUNIT_EXPECT_EQ(test, m->programs, ++ fault <= COPY_BUSY ? 0U : 2U); ++ KUNIT_EXPECT_EQ(test, apple_sftl_gc_relocate(gc, &io, m), -EINVAL); ++ apple_sftl_gc_free(gc); ++ continue; ++ } ++ KUNIT_ASSERT_EQ(test, ret, 0); ++ KUNIT_EXPECT_EQ(test, m->programs, 2U); ++ for (i = 0; i < ARRAY_SIZE(keys); i++) { ++ const u8 *meta = &m->pages[i / 4].oob[(i % 4) * APPLE_SFTL_META]; ++ const u8 *data = &m->pages[i / 4].data[(i % 4) * APPLE_SFTL_CHUNK]; ++ ++ KUNIT_EXPECT_EQ(test, get_unaligned_le64(meta) >> 16, 201ULL + i); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le32(meta + 8), keys[i]); ++ KUNIT_EXPECT_EQ(test, data[0], (u8)(keys[i] + 1)); ++ KUNIT_EXPECT_EQ(test, data[APPLE_SFTL_CHUNK - 1], data[0]); ++ } ++ KUNIT_ASSERT_EQ(test, apple_sftl_gc_checkpoint(gc, &context_block, 1, &image), 0); ++ KUNIT_EXPECT_EQ(test, image->first_sequence, 209ULL); ++ KUNIT_ASSERT_EQ(test, apple_sftl_gc_checkpoint_done(gc, image), 0); ++ memcpy(relocated, runs, sizeof(runs)); ++ relocated[0].provider = dest + 3; ++ relocated[2].provider = dest + 6; ++ recovered = snapshot; ++ recovered.runs = relocated; ++ recovered.sequence = image->sequence; ++ recovered.journal_sequence = image->sequence; ++ recovered.checkpoint_sequence = image->sequence; ++ KUNIT_EXPECT_EQ(test, apple_sftl_gc_recovered(gc, &recovered), 0); ++ apple_sftl_checkpoint_free(image); ++ apple_sftl_gc_free(gc); ++ } + } + + static struct kunit_case reclaim_cases[] = { ++ KUNIT_CASE(reclaim_copy_live_pages), + KUNIT_CASE(reclaim_metadata_vector), + KUNIT_CASE(reclaim_checkpoint_opaque_and_invalid), + KUNIT_CASE(reclaim_checkpoint_torn_receipts), +diff --git a/drivers/mtd/apple-sftl-reclaim.c b/drivers/mtd/apple-sftl-reclaim.c +index 100e92b57..1565c3bdf 100644 +--- a/drivers/mtd/apple-sftl-reclaim.c ++++ b/drivers/mtd/apple-sftl-reclaim.c +@@ -1,12 +1,11 @@ + // SPDX-License-Identifier: GPL-2.0-only + /* +- * SwissPPNFTL checkpoint serialization and offline reclamation transactions. ++ * SwissPPNFTL checkpoint serialization and verified reclamation transactions. + * Native metadata: 13G36 IOFlashStorage-730 formatter at 0x81092408. + * Range payload: cxtNextBatch at 0x8108f07c and the recovery decoder. + * + * Opaque accounting payloads survive byte-for-byte. Updating their native +- * accounting and committing VFL bank rotation remain separate work. No erase +- * callback or runtime GC worker is registered by this file. ++ * accounting and committing VFL bank rotation remain separate work. + */ + #include + #include +@@ -30,7 +29,8 @@ struct apple_sftl_gc { + struct apple_sftl_gc_copy *copies; + struct apple_sftl_checkpoint *checkpoint; + u32 nr_copies, copied, max_records, victim, destination; +- u64 retire_sequence, copied_sequence; ++ u64 retire_sequence, copied_sequence, victim_sequence; ++ u32 padding; + enum apple_sftl_gc_stage stage; + }; + +@@ -237,7 +237,7 @@ int apple_sftl_gc_plan(const struct apple_sftl_snapshot *snapshot, + u32 max_records, size_t max_bytes, struct apple_sftl_gc **out) + { + struct apple_sftl_gc *gc; +- u32 start, end, dest, capacity, copied = 0, i, count; ++ u32 start, end, dest, capacity, copied = 0, i, count, footer; + size_t map_bytes, copy_bytes, opaque_bytes; + int ret; + +@@ -254,6 +254,9 @@ int apple_sftl_gc_plan(const struct apple_sftl_snapshot *snapshot, + destination->last_sequence || victim->block == destination->block || + victim->last_sequence > snapshot->sequence) + return -EINVAL; ++ footer = victim->footer_chunks ? victim->footer_chunks : 32; ++ if (footer < 4 || footer > 32 || footer % 4) ++ return -EINVAL; + start = victim->block * APPLE_SFTL_STRIDE; + end = start + hweight16(victim->lanes) * 512; + dest = destination->block * APPLE_SFTL_STRIDE; +@@ -282,6 +285,7 @@ int apple_sftl_gc_plan(const struct apple_sftl_snapshot *snapshot, + gc->victim = victim->block; + gc->destination = destination->block; + gc->retire_sequence = max(victim->last_sequence, snapshot->sequence); ++ gc->victim_sequence = victim->last_sequence; + gc->copied_sequence = snapshot->sequence; + gc->snapshot.runs = kvzalloc(map_bytes, GFP_KERNEL); + gc->copies = kvzalloc(copy_bytes, GFP_KERNEL); +@@ -299,7 +303,7 @@ int apple_sftl_gc_plan(const struct apple_sftl_snapshot *snapshot, + + if (address != APPLE_SFTL_NO_ADDRESS && + sftl_overlap(address, run->length, start, start + APPLE_SFTL_STRIDE) && +- min(address + run->length, start + APPLE_SFTL_STRIDE) > end - 32) { ++ min(address + run->length, start + APPLE_SFTL_STRIDE) > end - footer) { + ret = -EBADMSG; + goto failed; + } +@@ -327,6 +331,21 @@ int apple_sftl_gc_plan(const struct apple_sftl_snapshot *snapshot, + sftl_gc_run(&gc->snapshot, run->logical + offset, + run->length - offset, address + offset); + } ++ /* Program complete pages. Repeat the first key in the leading slots: ++ * its last copy then begins an uninterrupted run of live mappings. ++ */ ++ gc->padding = (4 - copied % 4) % 4; ++ if (gc->padding) { ++ for (i = 0; i < gc->nr_copies; i++) ++ gc->copies[i].destination += gc->padding; ++ for (i = 0; i < gc->snapshot.nr_runs; i++) { ++ struct apple_sftl_extent *run = &gc->snapshot.runs[i]; ++ ++ if (run->provider >= dest && run->provider < dest + capacity) ++ run->provider += gc->padding; ++ } ++ copied += gc->padding; ++ } + /* Copy records consume sequences before checkpoint serialization. */ + if (copied > SFTL_SEQUENCE - snapshot->sequence) { + ret = -EOVERFLOW; +@@ -364,6 +383,13 @@ enum apple_sftl_gc_stage apple_sftl_gc_stage(const struct apple_sftl_gc *gc) + } + EXPORT_SYMBOL_GPL(apple_sftl_gc_stage); + ++u32 apple_sftl_gc_copy_chunks(const struct apple_sftl_gc *gc) ++{ ++ if (!gc || gc->stage != APPLE_SFTL_GC_COPY || gc->copied) ++ return 0; ++ return gc->snapshot.sequence - gc->copied_sequence; ++} ++ + const struct apple_sftl_gc_copy *apple_sftl_gc_next_copy(const struct apple_sftl_gc *gc) + { + if (!gc || gc->stage != APPLE_SFTL_GC_COPY) +@@ -380,7 +406,8 @@ int apple_sftl_gc_copy_done(struct apple_sftl_gc *gc, + if (!expected || !receipt) + return -EINVAL; + if (memcmp(expected, receipt, sizeof(*receipt)) || +- sequence != gc->copied_sequence + expected->length) { ++ sequence != gc->copied_sequence + expected->length + ++ (gc->copied ? 0 : gc->padding)) { + gc->stage = APPLE_SFTL_GC_FAILED; + return -EBADMSG; + } +@@ -391,6 +418,106 @@ int apple_sftl_gc_copy_done(struct apple_sftl_gc *gc, + } + EXPORT_SYMBOL_GPL(apple_sftl_gc_copy_done); + ++static int sftl_gc_program(struct apple_sftl_gc_page *buffer, ++ const struct apple_sftl_gc_page *page, ++ const struct apple_sftl_gc_io *io, void *priv, u32 address) ++{ ++ int ret; ++ ++ ret = io->read(priv, address, buffer); ++ if (ret) ++ return ret; ++ if (memchr_inv(buffer, 0xff, sizeof(*buffer))) ++ return -EBADMSG; ++ ret = io->program(priv, address, page); ++ if (ret) ++ return ret; ++ ret = io->read(priv, address, buffer); ++ if (ret) ++ return ret; ++ return memcmp(page, buffer, sizeof(*page)) ? -EBADMSG : 0; ++} ++ ++/* No erase or map publication occurs here. Even an indeterminate program ++ * consumes the transaction: the caller must recover before writing again. ++ */ ++int apple_sftl_gc_relocate(struct apple_sftl_gc *gc, ++ const struct apple_sftl_gc_io *io, void *priv) ++{ ++ struct apple_sftl_gc_page *pages, *src, *dst; ++ u32 i, offset, slot = 0, cached = U32_MAX, repeat, address; ++ u64 sequence; ++ int ret = 0; ++ ++ if (!gc || gc->stage != APPLE_SFTL_GC_COPY || gc->copied || ++ !io || !io->read || !io->program) ++ return -EINVAL; ++ pages = kvmalloc(2 * sizeof(*pages), GFP_NOIO); ++ if (!pages) ++ return -ENOMEM; ++ src = &pages[0]; ++ dst = &pages[1]; ++ address = gc->destination * APPLE_SFTL_STRIDE; ++ sequence = gc->copied_sequence; ++ repeat = 1 + gc->padding; ++ for (i = 0; i < gc->nr_copies; i++) { ++ const struct apple_sftl_gc_copy *copy = &gc->copies[i]; ++ ++ for (offset = 0; offset < copy->length; offset++) { ++ u32 from = copy->source + offset, key = copy->logical + offset; ++ const u8 *meta; ++ u64 stamp; ++ ++ if (cached != (from & ~3U)) { ++ cached = from & ~3U; ++ ret = io->read(priv, cached, src); ++ if (ret) ++ goto failed; ++ } ++ meta = src->oob + (from & 3) * APPLE_SFTL_META; ++ stamp = get_unaligned_le64(meta) >> 16; ++ /* Only rewrite ordinary unflagged data in its native class. */ ++ if (meta[0] != 1 || meta[1] || !stamp || ++ get_unaligned_le32(meta + 8) != key || ++ get_unaligned_le32(meta + 12) != stamp >> 16 || ++ stamp > gc->victim_sequence) { ++ ret = -EBADMSG; ++ goto failed; ++ } ++ do { ++ u8 *out = dst->oob + slot * APPLE_SFTL_META; ++ ++ memcpy(dst->data + slot * APPLE_SFTL_CHUNK, ++ src->data + (from & 3) * APPLE_SFTL_CHUNK, ++ APPLE_SFTL_CHUNK); ++ sequence++; ++ put_unaligned_le64(sequence << 16 | 1, out); ++ put_unaligned_le32(key, out + 8); ++ put_unaligned_le32(sequence >> 16, out + 12); ++ if (++slot == 4) { ++ ret = sftl_gc_program(src, dst, io, priv, address); ++ if (ret) ++ goto failed; ++ cached = U32_MAX; ++ slot = 0; ++ address += 4; ++ } ++ } while (--repeat); ++ repeat = 1; ++ } ++ } ++ gc->copied = gc->nr_copies; ++ gc->copied_sequence = sequence; ++ gc->stage = APPLE_SFTL_GC_CHECKPOINT; ++ goto out; ++failed: ++ gc->stage = APPLE_SFTL_GC_FAILED; ++out: ++ kvfree(pages); ++ return ret; ++} ++EXPORT_SYMBOL_GPL(apple_sftl_gc_relocate); ++ + int apple_sftl_gc_checkpoint(struct apple_sftl_gc *gc, const u32 *blocks, + u32 nr_blocks, struct apple_sftl_checkpoint **out) + { +@@ -445,27 +572,14 @@ int apple_sftl_gc_checkpoint_done(struct apple_sftl_gc *gc, + } + EXPORT_SYMBOL_GPL(apple_sftl_gc_checkpoint_done); + +-int apple_sftl_gc_recovered(struct apple_sftl_gc *gc, ++static bool sftl_maps_equal(const struct apple_sftl_snapshot *expected, + const struct apple_sftl_snapshot *recovered) + { + u32 wanted = 0, found = 0, offset = 0, length; +- int ret; + +- if (!gc || gc->stage != APPLE_SFTL_GC_RECOVERY) +- return -EINVAL; +- ret = sftl_check_map(recovered); +- if (ret) +- goto failed; +- if (recovered->checkpoint_sequence != gc->checkpoint->sequence || +- recovered->sequence != gc->checkpoint->sequence || +- recovered->journal_sequence != recovered->sequence || +- recovered->checkpoint_sequence <= gc->retire_sequence) { +- ret = -EBADMSG; +- goto failed; +- } + /* Compare logical mappings even when the cold reader coalesces ranges. */ +- while (wanted < gc->snapshot.nr_runs && found < recovered->nr_runs) { +- const struct apple_sftl_extent *a = &gc->snapshot.runs[wanted]; ++ while (wanted < expected->nr_runs && found < recovered->nr_runs) { ++ const struct apple_sftl_extent *a = &expected->runs[wanted]; + const struct apple_sftl_extent *b = &recovered->runs[found]; + u32 a_offset = offset - a->logical, b_offset = offset - b->logical; + u32 a_address = a->provider, b_address = b->provider; +@@ -474,10 +588,8 @@ int apple_sftl_gc_recovered(struct apple_sftl_gc *gc, + a_address += a_offset; + if (b_address != APPLE_SFTL_NO_ADDRESS) + b_address += b_offset; +- if (a_address != b_address) { +- ret = -EBADMSG; +- goto failed; +- } ++ if (a_address != b_address) ++ return false; + length = min(a->length - a_offset, b->length - b_offset); + offset += length; + if (offset == a->logical + a->length) +@@ -485,7 +597,67 @@ int apple_sftl_gc_recovered(struct apple_sftl_gc *gc, + if (offset == b->logical + b->length) + found++; + } +- if (offset != APPLE_SFTL_LOGICAL_PAGES) { ++ return offset == APPLE_SFTL_LOGICAL_PAGES; ++} ++ ++/* ++ * The durable checkpoint already supersedes every record in this closed ++ * block. Its own map must exclude the block (checked by the caller before ++ * replay), as must the full map independently recovered after replay. ++ */ ++int apple_sftl_gc_plan_retired(const struct apple_sftl_snapshot *active, ++ const struct apple_sftl_snapshot *cold, ++ const struct apple_sftl_gc_block *victim, ++ u64 checkpoint_start) ++{ ++ u32 i, first; ++ int ret; ++ ++ ret = sftl_check_map(active); ++ if (ret) ++ return ret; ++ ret = sftl_check_map(cold); ++ if (ret) ++ return ret; ++ if (!sftl_gc_block_valid(victim) || !victim->closed || victim->blank || ++ !checkpoint_start || checkpoint_start > cold->checkpoint_sequence || ++ victim->last_sequence >= checkpoint_start) ++ return -EOPNOTSUPP; ++ if (active->sequence != cold->sequence || ++ active->checkpoint_sequence != cold->checkpoint_sequence || ++ active->journal_sequence != active->sequence || ++ cold->journal_sequence != cold->sequence || ++ !sftl_maps_equal(active, cold)) ++ return -EBADMSG; ++ first = victim->block * APPLE_SFTL_STRIDE; ++ for (i = 0; i < cold->nr_runs; i++) { ++ const struct apple_sftl_extent *run = &cold->runs[i]; ++ ++ if (run->provider != APPLE_SFTL_NO_ADDRESS && ++ sftl_overlap(run->provider, run->length, first, first + APPLE_SFTL_STRIDE)) ++ return -EOPNOTSUPP; ++ } ++ return 0; ++} ++ ++int apple_sftl_gc_recovered(struct apple_sftl_gc *gc, ++ const struct apple_sftl_snapshot *recovered) ++{ ++ int ret; ++ ++ if (!gc || gc->stage != APPLE_SFTL_GC_RECOVERY) ++ return -EINVAL; ++ ret = sftl_check_map(recovered); ++ if (ret) ++ goto failed; ++ if (recovered->checkpoint_sequence != gc->checkpoint->sequence || ++ recovered->sequence != gc->checkpoint->sequence || ++ recovered->journal_sequence != recovered->sequence || ++ recovered->checkpoint_sequence <= gc->retire_sequence) { ++ ret = -EBADMSG; ++ goto failed; ++ } ++ if (!sftl_maps_equal(&gc->snapshot, recovered)) { + ret = -EBADMSG; + goto failed; + } +diff --git a/drivers/mtd/apple-sftl-reclaim.h b/drivers/mtd/apple-sftl-reclaim.h +index e7b110ab5..f26d7fe1d 100644 +--- a/drivers/mtd/apple-sftl-reclaim.h ++++ b/drivers/mtd/apple-sftl-reclaim.h +@@ -50,6 +50,8 @@ int apple_sftl_context_meta(u8 *meta, u8 subtype, u64 sequence, + struct apple_sftl_gc_block { + u32 block; + u16 lanes; ++ /* A zero source footer width uses the writer's conservative 32 chunks. */ ++ u16 footer_chunks; + bool blank, closed, reserved; + u64 last_sequence; + }; +@@ -68,20 +70,43 @@ enum apple_sftl_gc_stage { + + struct apple_sftl_gc; + +-/* These routines plan and validate supplied bytes. They perform no media IO. */ ++/* ++ * Page-aligned provider addresses use the same 4 KiB units as the map. ++ * The caller owns the checked blank destination and excludes all other ++ * writers, including raw MTD, until the transaction finishes. ++ * Reads must report media errors. A program failure can be indeterminate. ++ */ ++struct apple_sftl_gc_page { ++ u8 data[4 * APPLE_SFTL_CHUNK]; ++ u8 oob[4 * APPLE_SFTL_META]; ++}; ++ ++struct apple_sftl_gc_io { ++ int (*read)(void *priv, u32 address, struct apple_sftl_gc_page *page); ++ int (*program)(void *priv, u32 address, const struct apple_sftl_gc_page *page); ++}; ++ ++/* Planning consumes supplied bytes. Relocation performs verified page I/O. */ + int apple_sftl_gc_plan(const struct apple_sftl_snapshot *snapshot, + const struct apple_sftl_gc_block *victim, + const struct apple_sftl_gc_block *destination, + u32 max_records, size_t max_bytes, struct apple_sftl_gc **out); + void apple_sftl_gc_free(struct apple_sftl_gc *gc); + enum apple_sftl_gc_stage apple_sftl_gc_stage(const struct apple_sftl_gc *gc); ++u32 apple_sftl_gc_copy_chunks(const struct apple_sftl_gc *gc); + const struct apple_sftl_gc_copy *apple_sftl_gc_next_copy(const struct apple_sftl_gc *gc); + int apple_sftl_gc_copy_done(struct apple_sftl_gc *gc, + const struct apple_sftl_gc_copy *receipt, u64 sequence); ++int apple_sftl_gc_relocate(struct apple_sftl_gc *gc, ++ const struct apple_sftl_gc_io *io, void *priv); + int apple_sftl_gc_checkpoint(struct apple_sftl_gc *gc, const u32 *blocks, + u32 nr_blocks, struct apple_sftl_checkpoint **out); + int apple_sftl_gc_checkpoint_done(struct apple_sftl_gc *gc, + const struct apple_sftl_checkpoint *readback); ++int apple_sftl_gc_plan_retired(const struct apple_sftl_snapshot *active, ++ const struct apple_sftl_snapshot *cold, ++ const struct apple_sftl_gc_block *victim, ++ u64 checkpoint_start); + int apple_sftl_gc_recovered(struct apple_sftl_gc *gc, + const struct apple_sftl_snapshot *recovered); + diff --git a/kernel/patches/armv7/mtd/0010-mtd-apple-sftl-Reclaim-closed-data-blocks-in-the-wri.patch b/kernel/patches/armv7/mtd/0010-mtd-apple-sftl-Reclaim-closed-data-blocks-in-the-wri.patch new file mode 100644 index 00000000..cd65f5fa --- /dev/null +++ b/kernel/patches/armv7/mtd/0010-mtd-apple-sftl-Reclaim-closed-data-blocks-in-the-wri.patch @@ -0,0 +1,1960 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Shayne <8741557+shaynelegault@users.noreply.github.com> +Date: Wed, 18 Nov 2026 22:06:10 -0600 +Subject: [PATCH] mtd: apple-sftl: Reclaim closed data blocks in the write path + +Track checkpoint and replay ownership across the whole device. Recover the map independently and verify native footer records before recycling an obsolete block. Reserve a blank landing block for mixed victims only when a checkpoint backend can install and retire a generation. Verify relocation, checkpoint readback and fresh recovery before erasing the donor, then make its capacity available to normal writes. + +Exercise write pressure, cold recovery, interrupted summaries, ambiguous erase, retryable reservation failure and sequence exhaustion on synthetic media. Keep the P105 append-only until native checkpoint rotation and physical erase are qualified. + +Assisted-by: LLM +--- + .../driver-api/mtd/apple-sftl-reclamation.rst | 107 ++- + drivers/mtd/Kconfig | 11 +- + drivers/mtd/apple-sftl-core.c | 721 ++++++++++++++++-- + drivers/mtd/apple-sftl-reclaim.c | 9 + + drivers/mtd/apple-sftl-reclaim.h | 1 + + drivers/mtd/apple-sftl-test.c | 688 ++++++++++++++++- + drivers/mtd/apple-sftl.h | 15 + + drivers/mtd/devices/Kconfig | 7 +- + 8 files changed, 1438 insertions(+), 121 deletions(-) + +diff --git a/Documentation/driver-api/mtd/apple-sftl-reclamation.rst b/Documentation/driver-api/mtd/apple-sftl-reclamation.rst +index 31ac3e636..b164bdce0 100644 +--- a/Documentation/driver-api/mtd/apple-sftl-reclamation.rst ++++ b/Documentation/driver-api/mtd/apple-sftl-reclamation.rst +@@ -7,9 +7,37 @@ The measured 64 GB PPN layout shares one FTL across the logical partitions. + A Linux partition therefore provides no exclusive ownership of a physical + block. Reclamation must preserve the recovered map for the whole device. + +-The code in ``apple-sftl-reclaim.c`` serializes checkpoint images and plans +-transactions from supplied snapshots and completion receipts. It registers +-no media operation. The block device retains its existing access policy. ++The block writer allocates from verified blank blocks. An optional, verified ++``erase_block`` provider operation lets the allocator also reclaim obsolete ++blocks. A checkpoint backend with atomic installation and retirement of the ++previous fallback additionally permits relocation of live data. P105 H2FMI ++provides neither facility on the current hardware profile. When verified ++blank blocks run out on that profile, a write returns ``-ENOSPC``. ++ ++Reclaiming an obsolete block ++---------------------------- ++ ++The allocator records the blocks referenced by the saved checkpoint before ++replaying the journal. It requires a single visible checkpoint generation ++for erase eligibility. If an older generation is also visible, its fallback ++map can still own blocks. An obsolete candidate must be absent from both ++the saved map and the active map, including the logical keys outside the ++exported disk. The allocator excludes VFL and FTL context blocks, the ++bootstrap range and blocks with unknown active-lane flags. It loads the ++medium again and compares the whole recovered map and its sequence numbers ++with the active instance. ++ ++The allocator reads the native footer and checks its runs against the ++logical key and sequence in every data record. Its highest declared ++sequence must precede the first record in the checkpoint. Even an obsolete ++write or trim after the checkpoint can be needed for contiguous replay. ++ ++For an eligible block, the provider erases one active lane at a time. ++The allocator checks every normal-view data and OOB byte for the erased ++value before it hands the block to the waiting write. An indeterminate ++erase or failed blank verification stops I/O on that writer instance. ++Synthetic KUnit media exercise exhaustion, reclamation, reuse and cold ++recovery through the block writer's allocation path. + + Checkpoint images + ----------------- +@@ -34,38 +62,50 @@ data loss during format work; a native writer must update the fields whose + meaning depends on relocation. These images have not been qualified for + native checkpoint installation. + +-Offline transaction +-------------------- ++Relocating live data ++-------------------- + + ``apple_sftl_gc_plan()`` consumes a whole-device snapshot plus descriptions + of a closed victim and a fully checked blank destination. It rejects reserved + blocks, references into the victim's footer or inactive space, a destination +-still referenced by the map, insufficient destination capacity and a working +-plan buffers exceeding the caller's budget. A source with an allocation ++still referenced by the map, insufficient destination capacity and plan ++buffers exceeding the caller's budget. A source with an allocation + floor above the last replayed record is excluded under the current recovery +-contract. Copies retain the logical keys while +-moving their provider addresses. Destination space leaves eight pages for +-a future native summary. +- +-The transaction advances through explicit receipt checks: +- +-* Each copy receipt must match its planned logical range, source, +- destination and final sequence. The caller must issue and verify that IO. +-* A complete checkpoint image is built only after the copy receipts arrive. +- Its readback must match every metadata and payload byte. ++contract. Copies retain the logical keys while moving their provider ++addresses. Destination space leaves eight pages for a future native summary. ++ ++With both callbacks installed, allocation can use the last verified blank ++block to compact a closed source still referenced by the saved checkpoint. ++The allocator checks the source footer and data records before writing, ++accepts unflagged type 1 data for relocation, and budgets enough destination ++space and sequence numbers for the incoming write. The public KUnit tests ++exercise this through the normal write path with a synthetic checkpoint ++backend. ++ ++* ``apple_sftl_gc_relocate()`` reads each planned source key and checks its ++ metadata. It assembles four 4 KiB records per program unit and repeats the ++ first key when padding is needed. It checks that each destination page is ++ blank, programs it, then compares all data and OOB bytes with readback. ++* The checkpoint backend reserves context blocks before copying. After the ++ copies complete it installs the new checkpoint, retires the previous ++ fallback, and returns independent readback. Every metadata and payload ++ byte must match the submitted image. + * A fresh recovery snapshot must contain the planned whole-device mapping +- and the new checkpoint sequence. Equivalent coalescing is accepted. +- +-The supplied blank, reserved and lane descriptions are trusted inputs. They +-must come from authoritative VFL and context ownership checks before any +-future destructive IO can use a plan. The present receipt checks cannot +-establish those physical ownership facts. +- +-A mismatched receipt moves the transaction to its failed state. Even a victim +-with zero current references must pass checkpoint and cold-recovery stages, +-since the current replay contract can still require its obsolete history. +-The final VERIFIED state means that these supplied receipts passed. It does +-not certify native accounting or VFL commit durability. ++ and the new checkpoint sequence. Equivalent coalescing is accepted. The ++ allocator checks ownership and lane assignments again before erasing the ++ source. The pending write uses the free tail of the destination, and the ++ verified erased source becomes the next spare. ++ ++Standalone planner callers supply the blank, reserved and lane descriptions. ++At runtime the allocator derives these from its VFL and context state and ++from a scan of the candidate. Exposing the erase operation requires the ++caller to own the complete physical erase extent across all affected views. ++ ++A copy I/O failure or mismatched receipt moves the transaction to its failed ++state. A victim with live data or required replay history must survive ++until a new checkpoint is durably installed and recovered. VERIFIED records ++successful checks against the supplied checkpoint and recovery receipts. ++Native accounting and VFL rotation still require qualification. + + Outstanding native protocol work + ------------------------------- +@@ -88,10 +128,11 @@ fields. Both saved parameter copies agree: page bits 7, block bits 12 and CAU + bits 3. A normal block's page-0 row is ``(cau << 19) | (block << 7)``; the SLC + view adds bit 22. The normal view reports 128 pages per block. Physical erase + effects, the SLC alias relationship, timeout, retirement and partial completion +-across CAUs remain unqualified. The provider exposes no erase +-callback. Enabling physical reclamation requires resolving those semantics +-and qualifying the exact candidate with device recovery and interruption +-checks. ++across CAUs remain unqualified. The H2FMI implementation issues the ++single-row command and checks the erased status, data and OOB on all 128 ++normal-view pages. Enabling its callback on hardware requires a qualified ++physical erase extent, exclusive ownership across all affected views, and ++device recovery and interruption checks. + + The source research began with `Cascadia's NAND notes + `_. +diff --git a/drivers/mtd/Kconfig b/drivers/mtd/Kconfig +index e4bc9ed60..e95303396 100644 +--- a/drivers/mtd/Kconfig ++++ b/drivers/mtd/Kconfig +@@ -216,9 +216,10 @@ config MTD_APPLE_SFTL + Unsupported checkpoints, missing journal sequences and refresh + requirements stop recovery before a disk is registered. + +- Writes are appended to verified blank blocks. There is no erase or +- garbage collection yet, so writes fail with ENOSPC once the blank +- blocks are used up; reads keep working. ++ Writes are appended to verified blank blocks. A provider with a ++ qualified erase operation can also recycle verified retired blocks. ++ The physical P105 provider remains append-only and returns ENOSPC ++ when its supply of blank blocks runs out. + + config MTD_APPLE_SFTL_KUNIT_TEST + tristate "KUnit tests for Apple VFL/SwissPPNFTL" if !KUNIT_ALL_TESTS +@@ -227,8 +228,8 @@ config MTD_APPLE_SFTL_KUNIT_TEST + help + Synthesize complete VFL/SFTL media and exercise checkpoint recovery, + native block summaries, open-block journal replay, checkpoint encoding +- and offline reclamation transactions. Malformed replicas and unsupported +- layouts must fail without storage writes. ++ and verified reclamation during normal writes. Exercise failed erase, ++ readback and recovery using synthetic flash. + Also test complete programs, interrupted summaries, allocation + limits and fresh-loader replay. + All test media and provider operations are synthetic. +diff --git a/drivers/mtd/apple-sftl-core.c b/drivers/mtd/apple-sftl-core.c +index 6d77d5552..3c28e7b27 100644 +--- a/drivers/mtd/apple-sftl-core.c ++++ b/drivers/mtd/apple-sftl-core.c +@@ -39,6 +39,7 @@ + #define SFTL_FOOTER_CHUNKS 32U + #define SFTL_CLAIM_TIMEOUT (500U * HZ) + #define SFTL_FOOTER_BYTES (SFTL_FOOTER_CHUNKS * SFTL_CHUNK) ++#define SFTL_GC_MAP_BYTES (8U * 1024 * 1024) /* per map, on the measured 512 MiB P105 */ + + struct sftl_run { + struct rb_node node; +@@ -69,10 +70,16 @@ struct apple_sftl { + u16 lanes[SFTL_BLOCKS]; + DECLARE_BITMAP(reserved, SFTL_BLOCKS); + DECLARE_BITMAP(vfl_reserved, SFTL_BLOCKS); ++ DECLARE_BITMAP(unknown_lane_flags, SFTL_BLOCKS); ++ DECLARE_BITMAP(checkpoint_used, SFTL_BLOCKS); ++ DECLARE_BITMAP(old_closed, SFTL_BLOCKS); ++ u64 checkpoint_start; + u64 checkpoint_sequence, sequence, journal_sequence; + DECLARE_BITMAP(candidates, SFTL_BLOCKS); + DECLARE_BITMAP(used, SFTL_BLOCKS); +- u32 next_candidate, append_block, append_position, append_capacity; ++ u32 next_candidate, append_block, append_position, append_capacity, spare; ++ const struct apple_sftl_checkpoint_ops *checkpoint_ops; ++ void *checkpoint_priv; + u32 footer_runs; + u64 append_base; + u8 *footer; +@@ -467,9 +474,15 @@ static int sftl_vfl(struct apple_sftl *sftl) + goto out; + } + } +- for (block = 0; block < SFTL_BLOCKS; block++) +- if (!(get_unaligned_le16(table + block * 2) & BIT(15))) ++ for (block = 0; block < SFTL_BLOCKS; block++) { ++ u16 value = get_unaligned_le16(table + block * 2); ++ ++ if (!(value & BIT(15))) { + sftl->lanes[block] |= BIT(lane); ++ if (value) ++ __set_bit(block, sftl->unknown_lane_flags); ++ } ++ } + } + ret = 0; + out: +@@ -633,6 +646,9 @@ static int sftl_context(struct apple_sftl *sftl, const u8 *heads) + if (ret) + goto out; + } ++ /* Older saved generations have separate maps we have not validated. */ ++ if (!first) ++ sftl->checkpoint_start = records.items[0].stamp & SFTL_SEQUENCE; + sftl->checkpoint_sequence = fragment_sequence; + sftl->sequence = fragment_sequence; + ret = 0; +@@ -675,8 +691,11 @@ static int sftl_footer(struct apple_sftl *sftl, const u8 *head, u32 block, + base != sftl_sequence(head) || !span || base + span > SFTL_SEQUENCE + 1) + return -EBADMSG; + *floor = max(*floor, base + span - 1); +- if (base + span - 1 <= sftl->checkpoint_sequence) ++ if (base + span - 1 <= sftl->checkpoint_sequence) { ++ if (head[0] == 1 && base && base + span - 1 < sftl->checkpoint_start) ++ __set_bit(block, sftl->old_closed); + return 0; ++ } + footer = kmalloc(chunks * SFTL_CHUNK, GFP_KERNEL); + if (!footer) + return -ENOMEM; +@@ -749,7 +768,7 @@ out: + } + + static bool sftl_partial_footer(struct apple_sftl *sftl, const u8 *head, +- u32 position, u32 pages) ++ u32 position, u32 pages, u64 span) + { + const u8 *meta = sftl->oob; + unsigned int chunk; +@@ -757,7 +776,7 @@ static bool sftl_partial_footer(struct apple_sftl *sftl, const u8 *head, + if (position < pages - SFTL_FOOTER_CHUNKS / 4 || + meta[0] != 0x1c || meta[1] != SFTL_FOOTER_CHUNKS * 2 || + sftl_sequence(meta) != sftl_sequence(head) || +- get_unaligned_le32(meta + 8) != pages * 4 - SFTL_FOOTER_CHUNKS || ++ get_unaligned_le32(meta + 8) != span || + get_unaligned_le16(meta + 12) != SFTL_FOOTER_CHUNKS || + get_unaligned_le16(meta + 14) != 0xffff) + return false; +@@ -771,6 +790,7 @@ static int sftl_journal(struct apple_sftl *sftl, const u8 *heads) + { + struct sftl_records journal = {}; + u64 floor = sftl->checkpoint_sequence, expected = floor + 1; ++ u64 checkpoint_first = sftl->context.items[0].stamp & SFTL_SEQUENCE; + u32 block, position, i, pages; + unsigned int chunk; + int ret; +@@ -778,6 +798,7 @@ static int sftl_journal(struct apple_sftl *sftl, const u8 *heads) + for (block = 1; block < SFTL_SUBBLOCKS; block++) { + const u8 *head = heads + block * SFTL_META; + bool complete_prefix = true, partial_footer = false; ++ u64 base = sftl_sequence(head), next = base; + + if (head[0] != 1 && head[0] != 2) + continue; +@@ -808,20 +829,27 @@ static int sftl_journal(struct apple_sftl *sftl, const u8 *heads) + if (position < pages - SFTL_FOOTER_CHUNKS / 4) { + for (chunk = 0; chunk < 4; chunk++) { + const u8 *meta = sftl->oob + chunk * SFTL_META; +- u64 sequence = sftl_sequence(head) + position * 4 + chunk; ++ u64 sequence = sftl_sequence(meta); + +- if (meta[0] != 1 || meta[1] || +- sftl_sequence(meta) != sequence || ++ if (meta[0] != 1 || meta[1] || sequence < next || + get_unaligned_le32(meta + 12) != sequence >> 16) + complete_prefix = false; ++ /* A checkpoint occupies its own sequence numbers between pages. */ ++ if (sequence > next && ++ (!position || chunk || next != checkpoint_first || ++ sequence != sftl->checkpoint_sequence + 1)) ++ complete_prefix = false; ++ next = sequence + 1; + } + } +- /* An incomplete final summary leaves the last page erased. +- * Its full contiguous data prefix must still validate directly. +- * The partially written summary payload is never trusted. ++ /* A closing footer may follow a checkpoint that consumed ++ * sequences between two data pages in the same block. Match ++ * its span to the observed data, including that interval. + */ +- if (sftl_partial_footer(sftl, head, position, pages)) { +- if (!complete_prefix) { ++ if (sftl->oob[0] == 0x1c) { ++ if (!complete_prefix || ++ !sftl_partial_footer(sftl, head, position, pages, ++ next - base)) { + ret = -EBADMSG; + goto out; + } +@@ -889,6 +917,23 @@ out: + return ret; + } + ++static void sftl_mark_mapped(struct apple_sftl *sftl, unsigned long *blocks) ++{ ++ struct rb_node *node; ++ u32 block; ++ ++ for (node = rb_first(&sftl->runs); node; node = rb_next(node)) { ++ struct sftl_run *run = rb_entry(node, struct sftl_run, node); ++ u32 end; ++ ++ if (run->provider == SFTL_UNMAPPED) ++ continue; ++ end = (run->provider + run->end - run->start - 1) / SFTL_STRIDE; ++ for (block = run->provider / SFTL_STRIDE; block <= end; block++) ++ __set_bit(block, blocks); ++ } ++} ++ + int apple_sftl_load(struct apple_ppn *provider, struct apple_sftl **out) + { + struct apple_sftl *sftl; +@@ -911,6 +956,7 @@ int apple_sftl_load(struct apple_ppn *provider, struct apple_sftl **out) + sftl->runs = RB_ROOT; + sftl->append_block = U32_MAX; + sftl->next_candidate = 1; ++ sftl->spare = U32_MAX; + mutex_init(&sftl->lock); + sftl->page = kmalloc(APPLE_PPN_PAGE_SIZE, GFP_KERNEL); + heads = kcalloc(SFTL_BLOCKS, SFTL_META, GFP_KERNEL); +@@ -944,6 +990,8 @@ int apple_sftl_load(struct apple_ppn *provider, struct apple_sftl **out) + ret = sftl_context(sftl, heads); + if (ret) + goto failed; ++ /* A map entry superseded by journal replay still belongs to the saved map. */ ++ sftl_mark_mapped(sftl, sftl->checkpoint_used); + ret = sftl_journal(sftl, heads); + if (ret) + goto failed; +@@ -1020,7 +1068,6 @@ EXPORT_SYMBOL_GPL(apple_sftl_read); + /* Caller holds sftl->lock. Every candidate is checked in full before use. */ + static int sftl_writer_prepare(struct apple_sftl *sftl) + { +- struct rb_node *node; + u32 block; + + if (sftl->writer_prepared) +@@ -1038,67 +1085,289 @@ static int sftl_writer_prepare(struct apple_sftl *sftl) + __set_bit(0, sftl->used); + __set_bit(SFTL_SUBBLOCKS, sftl->used); + __set_bit(SFTL_SUBBLOCKS + 1, sftl->used); ++ sftl_mark_mapped(sftl, sftl->used); ++ sftl->writer_prepared = true; ++ return 0; ++} ++ ++/* Caller holds the live map lock or owns an unpublished cold loader. */ ++static void sftl_copy_map(struct apple_sftl *sftl, struct apple_sftl_snapshot *snapshot) ++{ ++ struct rb_node *node; ++ u32 i = 0; ++ + for (node = rb_first(&sftl->runs); node; node = rb_next(node)) { +- struct sftl_run *run = rb_entry(node, struct sftl_run, node); +- u32 end; ++ const struct sftl_run *run = rb_entry(node, struct sftl_run, node); + +- if (run->provider == SFTL_UNMAPPED) +- continue; +- end = (run->provider + run->end - run->start - 1) / SFTL_STRIDE; +- for (block = run->provider / SFTL_STRIDE; block <= end; block++) +- __set_bit(block, sftl->used); ++ snapshot->runs[i++] = (struct apple_sftl_extent) { ++ .logical = run->start, .length = run->end - run->start, ++ .provider = run->provider, ++ }; + } +- sftl->writer_prepared = true; ++ snapshot->nr_runs = sftl->nr_runs; ++ snapshot->sequence = sftl->sequence; ++ snapshot->journal_sequence = sftl->journal_sequence; ++ snapshot->checkpoint_sequence = sftl->checkpoint_sequence; ++} ++ ++/* Verify the native summary against every chunk it describes. A cold loader ++ * can skip old footers, but their claimed sequence ceiling alone cannot ++ * authorize erase. Unknown record kinds remain on media. ++ */ ++static int sftl_verify_closed(struct apple_sftl *sftl, struct apple_sftl_gc_block *victim, ++ unsigned long deadline, bool relocate) ++{ ++ u32 first = victim->block * SFTL_STRIDE, capacity = hweight16(victim->lanes) * 512; ++ u32 chunks, span, i, position = 0, previous_end = 0; ++ unsigned int chunk; ++ u8 trailer[SFTL_META]; ++ u64 base; ++ int ret; ++ ++ ret = sftl_read_address(sftl, first + capacity - 4, &chunk); ++ if (ret) ++ return ret; ++ memcpy(trailer, sftl->oob, SFTL_META); ++ chunks = get_unaligned_le16(trailer + 12); ++ span = get_unaligned_le32(trailer + 8); ++ base = sftl_sequence(trailer); ++ if (trailer[0] != 0x1c || chunks < 4 || chunks > SFTL_FOOTER_CHUNKS || ++ chunks % 4 || trailer[1] >> 1 != chunks || ++ get_unaligned_le16(trailer + 14) != 0xffff || !base || !span || ++ base + span - 1 >= sftl->checkpoint_start) ++ return -EOPNOTSUPP; ++ for (i = 0; i < chunks; i += 4) { ++ ret = sftl_read_address(sftl, first + capacity - chunks + i, &chunk); ++ if (ret) ++ return ret; ++ for (chunk = 0; chunk < 4; chunk++) ++ if (memcmp(sftl->oob + chunk * SFTL_META, trailer, SFTL_META)) ++ return -EBADMSG; ++ memcpy(sftl->footer + i * SFTL_CHUNK, sftl->page, APPLE_PPN_PAGE_SIZE); ++ } ++ for (i = 0; i < chunks * SFTL_CHUNK; i += SFTL_META) { ++ const u8 *entry = sftl->footer + i; ++ u32 relative = get_unaligned_le32(entry), high = get_unaligned_le32(entry + 4); ++ u32 key = get_unaligned_le32(entry + 8), length = get_unaligned_le32(entry + 12); ++ u32 j; ++ ++ if (!length) { ++ if (position != capacity - chunks) ++ return -EBADMSG; ++ victim->last_sequence = base + span - 1; ++ victim->footer_chunks = chunks; ++ return 0; ++ } ++ if ((!position && relative) || relative < previous_end || relative >= span || ++ length > span - relative || length > capacity - chunks - position) ++ return -EBADMSG; ++ if (key >= SFTL_LPAGES || length > SFTL_LPAGES - key) ++ return -EOPNOTSUPP; ++ /* Some native runs cross a high-word boundary with the initial ++ * high word in the footer. Their per-chunk semantics are unverified. ++ */ ++ if (high == (base + relative) >> 16 && ++ high != (base + relative + length - 1) >> 16) ++ return -EOPNOTSUPP; ++ for (j = 0; j < length; j++, position++) { ++ u64 seq = base + relative + j; ++ const u8 *meta; ++ ++ if (!(position % 4)) { ++ if (time_after_eq(jiffies, deadline)) ++ return -ETIMEDOUT; ++ ret = sftl_read_address(sftl, first + position, &chunk); ++ if (ret) ++ return ret; ++ } ++ meta = sftl->oob + (position % 4) * SFTL_META; ++ /* Recognized native classes outside this operation are ineligible. */ ++ if ((meta[0] == 2 && !meta[1]) || ++ (relocate && meta[0] == 1 && meta[1] == 1)) ++ return -EOPNOTSUPP; ++ if (meta[0] != 1 || meta[1] > 1 || ++ sftl_sequence(meta) != seq || high != seq >> 16 || ++ get_unaligned_le32(meta + 8) != key + j || ++ get_unaligned_le32(meta + 12) != high) ++ return -EBADMSG; ++ } ++ previous_end = relative + length; ++ } ++ return -EBADMSG; ++} ++ ++static int sftl_block_blank(struct apple_sftl *sftl, u32 block, unsigned long deadline, ++ bool *blank) ++{ ++ u32 capacity = hweight16(sftl->lanes[block]) * 512, pos; ++ unsigned int chunk; ++ int ret; ++ ++ *blank = false; ++ for (pos = 0; pos < capacity; pos += 4) { ++ if (time_after_eq(jiffies, deadline)) ++ return -ETIMEDOUT; ++ ret = sftl_read_address(sftl, block * SFTL_STRIDE + pos, &chunk); ++ if (ret) ++ return ret; ++ if (!sftl_erased(sftl->page, APPLE_PPN_PAGE_SIZE) || ++ !sftl_erased(sftl->oob, APPLE_PPN_OOB_SIZE)) ++ return 0; ++ } ++ *blank = true; + return 0; + } + +-static int sftl_claim_block(struct apple_sftl *sftl) ++static bool sftl_closed_candidate(struct apple_sftl *sftl, u32 block) + { +- unsigned long deadline = jiffies + SFTL_CLAIM_TIMEOUT; +- u32 block; ++ return sftl->checkpoint_start && test_bit(block, sftl->old_closed) && ++ !test_bit(block, sftl->reserved) && !test_bit(block, sftl->vfl_reserved) && ++ !test_bit(block, sftl->unknown_lane_flags); ++} + +- while (sftl->next_candidate < SFTL_SUBBLOCKS) { +- u32 capacity, position; +- unsigned int chunk; +- bool blank = true; +- int ret; ++static bool sftl_retired_candidate(struct apple_sftl *sftl, u32 block) ++{ ++ return sftl_closed_candidate(sftl, block) && !test_bit(block, sftl->checkpoint_used); ++} + +- block = sftl->next_candidate++; +- if (!test_bit(block, sftl->candidates) || +- test_bit(block, sftl->used) || !sftl->lanes[block]) ++static int sftl_erase_verified(struct apple_sftl *sftl, u32 block, unsigned long deadline) ++{ ++ unsigned int lane; ++ bool blank; ++ int ret; ++ ++ for (lane = 0; lane < SFTL_LANES; lane++) { ++ if (!(sftl->lanes[block] & BIT(lane))) + continue; +- /* A checked or rejected candidate is never reused in this instance. */ +- __set_bit(block, sftl->used); +- capacity = hweight16(sftl->lanes[block]) * 128 * 4; +- for (position = 0; position < capacity; position += 4) { +- if (time_after_eq(jiffies, deadline)) { +- sftl->failed = true; +- return -ETIMEDOUT; +- } +- ret = sftl_read_address(sftl, block * SFTL_STRIDE + position, &chunk); +- if (ret) { +- sftl->failed = true; +- return ret; ++ if (time_after_eq(jiffies, deadline)) ++ return -ETIMEDOUT; ++ ret = sftl->ppn->ops->erase_block(sftl->ppn->priv, lane & 1, lane >> 1, block); ++ if (ret) ++ return ret; ++ } ++ ret = sftl_block_blank(sftl, block, deadline, &blank); ++ return ret ? ret : (blank ? 0 : -EBADMSG); ++} ++ ++static int sftl_reclaim_retired(struct apple_sftl *sftl, u32 *claimed, ++ unsigned long deadline) ++{ ++ struct apple_sftl_snapshot active = {}, saved = {}; ++ struct apple_sftl *cold = NULL; ++ size_t active_bytes, cold_bytes; ++ u32 block; ++ int ret = -ENOSPC; ++ ++ if (!sftl->ppn->ops->erase_block) ++ return -ENOSPC; ++ /* The top 32 physical blocks can contain native bootstrap members. */ ++ for (block = 1; block < SFTL_BLOCKS - 32; block++) { ++ struct apple_sftl_gc_block victim = { ++ .block = block, .lanes = sftl->lanes[block], .closed = true, ++ }; ++ ++ if (test_bit(block, sftl->used) || !sftl_retired_candidate(sftl, block)) ++ continue; ++ if (!cold) { ++ ret = apple_sftl_load(sftl->ppn, &cold); ++ if (ret) ++ break; ++ active_bytes = array_size(sftl->nr_runs, sizeof(*active.runs)); ++ cold_bytes = array_size(cold->nr_runs, sizeof(*saved.runs)); ++ if (active_bytes > SFTL_GC_MAP_BYTES || cold_bytes > SFTL_GC_MAP_BYTES) { ++ ret = -ENOMEM; ++ break; + } +- if (!sftl_erased(sftl->page, APPLE_PPN_PAGE_SIZE) || +- !sftl_erased(sftl->oob, APPLE_PPN_OOB_SIZE)) { +- blank = false; ++ active.runs = kvzalloc(active_bytes, GFP_KERNEL); ++ saved.runs = kvzalloc(cold_bytes, GFP_KERNEL); ++ if (!active.runs || !saved.runs) { ++ ret = -ENOMEM; + break; + } ++ sftl_copy_map(sftl, &active); ++ sftl_copy_map(cold, &saved); + } +- if (!blank) ++ if (!sftl_retired_candidate(cold, block) || ++ cold->checkpoint_start != sftl->checkpoint_start || ++ cold->lanes[block] != victim.lanes) { ++ ret = -EBADMSG; ++ break; ++ } ++ ret = sftl_verify_closed(sftl, &victim, deadline, false); ++ if (!ret) ++ ret = apple_sftl_gc_plan_retired(&active, &saved, &victim, ++ cold->checkpoint_start); ++ if (ret == -EOPNOTSUPP) { ++ ret = -ENOSPC; + continue; +- sftl->append_block = block; +- sftl->append_capacity = capacity - SFTL_FOOTER_CHUNKS; +- sftl->append_position = 0; +- sftl->append_base = sftl->sequence + 1; +- sftl->footer_runs = 0; +- memset(sftl->footer, 0, SFTL_FOOTER_BYTES); +- return 0; ++ } ++ if (ret) ++ break; ++ __set_bit(block, sftl->used); ++ ret = sftl_erase_verified(sftl, block, deadline); ++ if (ret) { ++ /* Never reissue an ambiguous, possibly partial erase. */ ++ sftl->failed = true; ++ break; ++ } ++ *claimed = block; ++ break; ++ } ++ kvfree(saved.runs); ++ kvfree(active.runs); ++ apple_sftl_free(cold); ++ return ret; ++} ++ ++static int sftl_check_landing(struct apple_sftl *sftl, u32 block, unsigned long deadline) ++{ ++ bool blank; ++ int ret; ++ ++ if (!block || block >= SFTL_SUBBLOCKS || !sftl->lanes[block] || ++ test_bit(block, sftl->reserved) || test_bit(block, sftl->vfl_reserved) || ++ test_bit(block, sftl->checkpoint_used)) ++ return -EBADMSG; ++ ret = sftl_block_blank(sftl, block, deadline, &blank); ++ return ret ? ret : (blank ? 0 : -EBADMSG); ++} ++ ++static int sftl_take_blank(struct apple_sftl *sftl, u32 *out, unsigned long deadline) ++{ ++ u32 block; ++ bool blank; ++ int ret; ++ ++ while (sftl->next_candidate < SFTL_SUBBLOCKS) { ++ block = sftl->next_candidate++; ++ if (!test_bit(block, sftl->candidates) || test_bit(block, sftl->used) || ++ (sftl->checkpoint_ops && test_bit(block, sftl->checkpoint_used)) || ++ !sftl->lanes[block]) ++ continue; ++ __set_bit(block, sftl->used); ++ ret = sftl_block_blank(sftl, block, deadline, &blank); ++ if (ret) { ++ sftl->failed = true; ++ return ret; ++ } ++ if (blank) { ++ *out = block; ++ return 0; ++ } + } + return -ENOSPC; + } + ++static void sftl_open_block(struct apple_sftl *sftl, u32 block) ++{ ++ sftl->append_block = block; ++ sftl->append_capacity = hweight16(sftl->lanes[block]) * 512 - SFTL_FOOTER_CHUNKS; ++ sftl->append_position = 0; ++ sftl->append_base = sftl->sequence + 1; ++ sftl->footer_runs = 0; ++ memset(sftl->footer, 0, SFTL_FOOTER_BYTES); ++} ++ + static int sftl_program_address(struct apple_sftl *sftl, u32 address, + const u8 *data, const u8 *oob) + { +@@ -1147,14 +1416,315 @@ static void sftl_append_summary(struct apple_sftl *sftl, u64 sequence, u32 key) + put_unaligned_le32(1, entry + 12); + } + ++static int sftl_snapshot_locked(struct apple_sftl *sftl, size_t max_bytes, ++ struct apple_sftl_snapshot **out); ++ ++struct sftl_move { ++ struct apple_sftl *sftl; ++ unsigned long deadline; ++}; ++ ++static int sftl_move_read(void *priv, u32 address, struct apple_sftl_gc_page *page) ++{ ++ struct sftl_move *move = priv; ++ struct apple_sftl *sftl = move->sftl; ++ unsigned int chunk; ++ int ret; ++ ++ if (time_after_eq(jiffies, move->deadline)) ++ return -ETIMEDOUT; ++ ret = sftl_read_address(sftl, address, &chunk); ++ if (ret) ++ return ret; ++ memcpy(page->data, sftl->page, sizeof(page->data)); ++ memcpy(page->oob, sftl->oob, sizeof(page->oob)); ++ return 0; ++} ++ ++static int sftl_move_program(void *priv, u32 address, const struct apple_sftl_gc_page *page) ++{ ++ struct sftl_move *move = priv; ++ struct apple_sftl *sftl = move->sftl; ++ unsigned int chunk; ++ int ret; ++ ++ if (time_after_eq(jiffies, move->deadline)) ++ return -ETIMEDOUT; ++ if (address != sftl->append_block * SFTL_STRIDE + sftl->append_position || ++ sftl->append_position + 4 > sftl->append_capacity) ++ return -ERANGE; ++ ret = sftl_program_address(sftl, address, page->data, page->oob); ++ if (ret) ++ return ret; ++ for (chunk = 0; chunk < 4; chunk++) { ++ const u8 *meta = page->oob + chunk * SFTL_META; ++ ++ sftl_append_summary(sftl, sftl_sequence(meta), get_unaligned_le32(meta + 8)); ++ } ++ sftl->append_position += 4; ++ return 0; ++} ++ ++static const struct apple_sftl_gc_io sftl_move_ops = { ++ .read = sftl_move_read, ++ .program = sftl_move_program, ++}; ++ ++/* Adopt the state independently decoded from media; keep the landing block's ++ * verified summary and cursor so subsequent writes can use its free tail. ++ */ ++static void sftl_adopt_checkpoint(struct apple_sftl *sftl, struct apple_sftl *cold) ++{ ++ struct rb_root old_runs = sftl->runs; ++ struct sftl_records old_context = sftl->context; ++ u32 block; ++ ++ sftl->runs = cold->runs; ++ cold->runs = old_runs; ++ sftl->nr_runs = cold->nr_runs; ++ sftl->context = cold->context; ++ cold->context = old_context; ++ sftl->sequence = cold->sequence; ++ sftl->journal_sequence = cold->journal_sequence; ++ sftl->checkpoint_sequence = cold->checkpoint_sequence; ++ sftl->checkpoint_start = cold->checkpoint_start; ++ memcpy(sftl->reserved, cold->reserved, sizeof(sftl->reserved)); ++ memcpy(sftl->vfl_reserved, cold->vfl_reserved, sizeof(sftl->vfl_reserved)); ++ memcpy(sftl->checkpoint_used, cold->checkpoint_used, sizeof(sftl->checkpoint_used)); ++ memcpy(sftl->old_closed, cold->old_closed, sizeof(sftl->old_closed)); ++ memcpy(sftl->unknown_lane_flags, cold->unknown_lane_flags, sizeof(sftl->unknown_lane_flags)); ++ for (block = 0; block < SFTL_BLOCKS; block++) ++ if (test_bit(block, sftl->reserved) || test_bit(block, sftl->vfl_reserved) || ++ test_bit(block, sftl->checkpoint_used)) ++ __set_bit(block, sftl->used); ++} ++ ++static int sftl_compact(struct apple_sftl *sftl, u32 landing, unsigned long deadline) ++{ ++ struct apple_sftl_gc_block destination = { ++ .block = landing, .lanes = sftl->lanes[landing], .blank = true, ++ }; ++ struct sftl_move move = { .sftl = sftl, .deadline = deadline }; ++ struct apple_sftl_checkpoint *image = NULL, *readback = NULL; ++ struct apple_sftl_snapshot *before = NULL, *after = NULL; ++ struct apple_sftl_gc *gc = NULL; ++ struct apple_sftl *cold = NULL; ++ u32 blocks[SFTL_MAX_CONTEXTS], nr = ARRAY_SIZE(blocks), block, slots, i; ++ int ret = -ENOSPC; ++ ++ for (block = 1; block < SFTL_BLOCKS - 32; block++) { ++ struct apple_sftl_gc_block victim = { ++ .block = block, .lanes = sftl->lanes[block], .closed = true, ++ }; ++ ++ if (!sftl_closed_candidate(sftl, block) || !test_bit(block, sftl->checkpoint_used)) ++ continue; ++ if (!before) { ++ ret = sftl_snapshot_locked(sftl, SFTL_GC_MAP_BYTES, &before); ++ if (ret) ++ goto out; ++ } ++ ret = sftl_verify_closed(sftl, &victim, deadline, true); ++ if (ret == -EOPNOTSUPP) { ++ ret = -ENOSPC; ++ continue; ++ } ++ if (ret) ++ goto out; ++ ret = apple_sftl_gc_plan(before, &victim, &destination, SFTL_MAX_CONTEXTS * SFTL_STRIDE, ++ SFTL_GC_MAP_BYTES, &gc); ++ if (ret == -ENOSPC || ret == -EOVERFLOW) { ++ ret = -ENOSPC; ++ continue; ++ } ++ if (ret) ++ goto out; ++ slots = apple_sftl_gc_copy_chunks(gc); ++ /* Keep at least a page for this request, and gain usable capacity. */ ++ if (!slots || !apple_sftl_gc_has_sequence_room(gc, 4) || ++ slots + 4 > hweight16(destination.lanes) * 512 - SFTL_FOOTER_CHUNKS || ++ slots >= hweight16(victim.lanes) * 512 - SFTL_FOOTER_CHUNKS) { ++ apple_sftl_gc_free(gc); ++ gc = NULL; ++ ret = -ENOSPC; ++ continue; ++ } ++ ret = sftl->checkpoint_ops->reserve(sftl->checkpoint_priv, blocks, &nr); ++ if (ret) ++ goto out; ++ if (!nr || nr > ARRAY_SIZE(blocks)) { ++ ret = -EINVAL; ++ goto out; ++ } ++ for (i = 0; i < nr; i++) { ++ u32 k; ++ ++ if (!blocks[i] || blocks[i] >= SFTL_SUBBLOCKS || blocks[i] == block || ++ blocks[i] == landing || !test_bit(blocks[i], sftl->reserved)) { ++ ret = -EINVAL; ++ goto out; ++ } ++ for (k = 0; k < i; k++) ++ if (blocks[k] == blocks[i]) { ++ ret = -EINVAL; ++ goto out; ++ } ++ } ++ ret = sftl_check_landing(sftl, landing, deadline); ++ if (ret) ++ goto failed; ++ sftl_open_block(sftl, landing); ++ ret = apple_sftl_gc_relocate(gc, &sftl_move_ops, &move); ++ if (ret) { ++ if (apple_sftl_gc_stage(gc) == APPLE_SFTL_GC_COPY) { ++ /* The executor could not start. No page was programmed. */ ++ sftl->append_block = U32_MAX; ++ goto out; ++ } ++ goto failed; ++ } ++ ret = apple_sftl_gc_checkpoint(gc, blocks, nr, &image); ++ if (ret) ++ goto failed; ++ ret = sftl->checkpoint_ops->install(sftl->checkpoint_priv, image, &readback); ++ if (readback == image) { ++ readback = NULL; ++ ret = -EBADMSG; ++ } else if (readback && readback->records == image->records) { ++ readback->records = NULL; ++ ret = -EBADMSG; ++ } ++ if (ret) ++ goto failed; ++ ret = apple_sftl_gc_checkpoint_done(gc, readback); ++ if (ret) ++ goto failed; ++ ret = apple_sftl_load(sftl->ppn, &cold); ++ if (ret) ++ goto failed; ++ ret = sftl_snapshot_locked(cold, SFTL_GC_MAP_BYTES, &after); ++ if (ret) ++ goto failed; ++ ret = apple_sftl_gc_recovered(gc, after); ++ if (ret) ++ goto failed; ++ if (!sftl_retired_candidate(cold, block) || test_bit(landing, cold->unknown_lane_flags) || ++ test_bit(landing, cold->reserved) || test_bit(landing, cold->vfl_reserved) || ++ memcmp(sftl->lanes, cold->lanes, sizeof(sftl->lanes))) { ++ ret = -EBADMSG; ++ goto failed; ++ } ++ ret = sftl_erase_verified(sftl, block, deadline); ++ if (ret) ++ goto failed; ++ sftl_adopt_checkpoint(sftl, cold); ++ sftl->spare = block; ++ goto out; ++ } ++ goto out; ++failed: ++ sftl->failed = true; ++out: ++ apple_sftl_free(cold); ++ apple_sftl_snapshot_free(after); ++ apple_sftl_snapshot_free(before); ++ apple_sftl_checkpoint_free(readback); ++ apple_sftl_checkpoint_free(image); ++ apple_sftl_gc_free(gc); ++ return ret; ++} ++ ++static bool sftl_can_compact(struct apple_sftl *sftl) ++{ ++ u32 block; ++ ++ if (!sftl->checkpoint_ops || !sftl->ppn->ops->erase_block) ++ return false; ++ for (block = 1; block < SFTL_BLOCKS - 32; block++) ++ if (sftl_closed_candidate(sftl, block) && test_bit(block, sftl->checkpoint_used)) ++ return true; ++ return false; ++} ++ ++static int sftl_claim_block(struct apple_sftl *sftl) ++{ ++ unsigned long deadline = jiffies + SFTL_CLAIM_TIMEOUT; ++ u32 block, spare; ++ int ret; ++ ++ if (sftl->spare != U32_MAX) { ++ block = sftl->spare; ++ sftl->spare = U32_MAX; ++ ret = sftl_check_landing(sftl, block, deadline); ++ if (ret) { ++ sftl->failed = true; ++ return ret; ++ } ++ } else { ++ ret = sftl_take_blank(sftl, &block, deadline); ++ if (ret == -ENOSPC && !sftl->failed) ++ ret = sftl_reclaim_retired(sftl, &block, deadline); ++ if (ret) ++ return ret; ++ } ++ /* Only reserve landing space when a complete commit backend exists. */ ++ if (!test_bit(block, sftl->unknown_lane_flags) && sftl_can_compact(sftl)) { ++ ret = sftl_take_blank(sftl, &spare, deadline); ++ if (!ret) ++ sftl->spare = spare; ++ else if (ret == -ENOSPC && !sftl->failed) { ++ /* Use already-retired space ahead of copying live records. ++ * Keep the checked landing available for a later transaction. ++ */ ++ ret = sftl_reclaim_retired(sftl, &spare, deadline); ++ if (!ret) { ++ sftl->spare = block; ++ sftl_open_block(sftl, spare); ++ return 0; ++ } ++ if (ret != -ENOSPC || sftl->failed) { ++ if (!sftl->failed) ++ sftl->spare = block; ++ return ret; ++ } ++ ret = sftl_compact(sftl, block, deadline); ++ if (!ret || sftl->failed) ++ return ret; ++ if (ret != -ENOSPC && ret != -EOPNOTSUPP) { ++ sftl->spare = block; ++ return ret; ++ } ++ } else { ++ if (!sftl->failed) ++ sftl->spare = block; ++ return ret; ++ } ++ } ++ sftl_open_block(sftl, block); ++ return 0; ++} ++ ++void apple_sftl_set_checkpoint_ops(struct apple_sftl *sftl, ++ const struct apple_sftl_checkpoint_ops *ops, void *priv) ++{ ++ mutex_lock(&sftl->lock); ++ sftl->checkpoint_ops = ops && ops->reserve && ops->install ? ops : NULL; ++ sftl->checkpoint_priv = priv; ++ mutex_unlock(&sftl->lock); ++} ++EXPORT_SYMBOL_GPL(apple_sftl_set_checkpoint_ops); ++ + static int sftl_close_block(struct apple_sftl *sftl) + { ++ u64 span = sftl->sequence - sftl->append_base + 1; + u8 meta[SFTL_META]; + unsigned int chunk, index; + int ret; + ++ if (!span || span > U32_MAX) ++ return -EOVERFLOW; + put_unaligned_le64(sftl->append_base << 16 | 0x401c, meta); +- put_unaligned_le32(sftl->append_position, meta + 8); ++ put_unaligned_le32(span, meta + 8); + put_unaligned_le16(SFTL_FOOTER_CHUNKS, meta + 12); + put_unaligned_le16(0xffff, meta + 14); + for (chunk = 0; chunk < 4; chunk++) +@@ -1201,6 +1771,10 @@ int apple_sftl_write(struct apple_sftl *sftl, u32 logical_page, + if (ret) + goto out; + } ++ if (sftl->sequence > SFTL_SEQUENCE - 4) { ++ ret = -EOVERFLOW; ++ goto out; ++ } + address = sftl->append_block * SFTL_STRIDE + sftl->append_position; + first_sequence = sftl->sequence + 1; + for (chunk = 0; chunk < 4; chunk++) { +@@ -1264,11 +1838,10 @@ void apple_sftl_get_info(struct apple_sftl *sftl, struct apple_sftl_info *info) + } + EXPORT_SYMBOL_GPL(apple_sftl_get_info); + +-int apple_sftl_snapshot(struct apple_sftl *sftl, size_t max_bytes, ++static int sftl_snapshot_locked(struct apple_sftl *sftl, size_t max_bytes, + struct apple_sftl_snapshot **out) + { + struct apple_sftl_snapshot *snapshot; +- struct rb_node *node; + size_t map_bytes, context_bytes; + u32 i = 0; + unsigned int chunk; +@@ -1279,7 +1852,6 @@ int apple_sftl_snapshot(struct apple_sftl *sftl, size_t max_bytes, + *out = NULL; + if (!sftl) + return -EINVAL; +- mutex_lock(&sftl->lock); + if (sftl->failed) { + ret = -EIO; + goto unlock; +@@ -1301,14 +1873,7 @@ int apple_sftl_snapshot(struct apple_sftl *sftl, size_t max_bytes, + ret = -ENOMEM; + goto failed; + } +- for (node = rb_first(&sftl->runs); node; node = rb_next(node)) { +- const struct sftl_run *run = rb_entry(node, struct sftl_run, node); +- +- snapshot->runs[i++] = (struct apple_sftl_extent) { +- .logical = run->start, .length = run->end - run->start, +- .provider = run->provider, +- }; +- } ++ sftl_copy_map(sftl, snapshot); + for (i = 0; i < sftl->context.count; i++) { + const struct sftl_record *reference = &sftl->context.items[i]; + struct apple_sftl_context_record *record = &snapshot->opaque[i]; +@@ -1326,16 +1891,26 @@ int apple_sftl_snapshot(struct apple_sftl *sftl, size_t max_bytes, + memcpy(record->meta, sftl->oob + chunk * SFTL_META, SFTL_META); + memcpy(record->data, sftl->page + chunk * SFTL_CHUNK, SFTL_CHUNK); + } +- snapshot->nr_runs = sftl->nr_runs; + snapshot->nr_opaque = sftl->context.count; +- snapshot->sequence = sftl->sequence; +- snapshot->journal_sequence = sftl->journal_sequence; +- snapshot->checkpoint_sequence = sftl->checkpoint_sequence; + *out = snapshot; + goto unlock; + failed: + apple_sftl_snapshot_free(snapshot); + unlock: ++ return ret; ++} ++int apple_sftl_snapshot(struct apple_sftl *sftl, size_t max_bytes, ++ struct apple_sftl_snapshot **out) ++{ ++ int ret; ++ ++ if (!sftl || !out) { ++ if (out) ++ *out = NULL; ++ return -EINVAL; ++ } ++ mutex_lock(&sftl->lock); ++ ret = sftl_snapshot_locked(sftl, max_bytes, out); + mutex_unlock(&sftl->lock); + return ret; + } +diff --git a/drivers/mtd/apple-sftl-reclaim.c b/drivers/mtd/apple-sftl-reclaim.c +index 1565c3bdf..f85a5ec7b 100644 +--- a/drivers/mtd/apple-sftl-reclaim.c ++++ b/drivers/mtd/apple-sftl-reclaim.c +@@ -390,6 +390,15 @@ u32 apple_sftl_gc_copy_chunks(const struct apple_sftl_gc *gc) + return gc->snapshot.sequence - gc->copied_sequence; + } + ++bool apple_sftl_gc_has_sequence_room(const struct apple_sftl_gc *gc, u32 pending) ++{ ++ u32 count; ++ ++ if (!gc || sftl_checkpoint_count(&gc->snapshot, gc->max_records, &count)) ++ return false; ++ return pending <= SFTL_SEQUENCE - gc->snapshot.sequence - count; ++} ++ + const struct apple_sftl_gc_copy *apple_sftl_gc_next_copy(const struct apple_sftl_gc *gc) + { + if (!gc || gc->stage != APPLE_SFTL_GC_COPY) +diff --git a/drivers/mtd/apple-sftl-reclaim.h b/drivers/mtd/apple-sftl-reclaim.h +index f26d7fe1d..6b6780c8b 100644 +--- a/drivers/mtd/apple-sftl-reclaim.h ++++ b/drivers/mtd/apple-sftl-reclaim.h +@@ -94,6 +94,7 @@ int apple_sftl_gc_plan(const struct apple_sftl_snapshot *snapshot, + void apple_sftl_gc_free(struct apple_sftl_gc *gc); + enum apple_sftl_gc_stage apple_sftl_gc_stage(const struct apple_sftl_gc *gc); + u32 apple_sftl_gc_copy_chunks(const struct apple_sftl_gc *gc); ++bool apple_sftl_gc_has_sequence_room(const struct apple_sftl_gc *gc, u32 pending); + const struct apple_sftl_gc_copy *apple_sftl_gc_next_copy(const struct apple_sftl_gc *gc); + int apple_sftl_gc_copy_done(struct apple_sftl_gc *gc, + const struct apple_sftl_gc_copy *receipt, u64 sequence); +diff --git a/drivers/mtd/apple-sftl-test.c b/drivers/mtd/apple-sftl-test.c +index 1ffcc7477..89b9ae498 100644 +--- a/drivers/mtd/apple-sftl-test.c ++++ b/drivers/mtd/apple-sftl-test.c +@@ -37,6 +37,13 @@ enum sftl_fault { + SFTL_RUNTIME_FAILURE, + }; + ++enum sftl_test_commit { ++ SFTL_COMMIT_OK, ++ SFTL_COMMIT_IO_ERROR, ++ SFTL_COMMIT_BAD_READBACK, ++ SFTL_COMMIT_UNPUBLISHED, ++}; ++ + struct sftl_test_page { + u8 data[APPLE_PPN_PAGE_SIZE]; + u8 oob[APPLE_PPN_OOB_SIZE]; +@@ -45,6 +52,7 @@ struct sftl_test_page { + struct sftl_fixture { + struct kunit *test; + struct apple_ppn provider; ++ struct apple_ppn_ops ops; + enum sftl_fault fault; + unsigned int reads; + unsigned int programs; +@@ -52,7 +60,14 @@ struct sftl_fixture { + unsigned int tear_program; + unsigned int program_outside; + bool uncertain_program; +- bool no_candidates; ++ unsigned int append_candidates; ++ unsigned int erases; ++ bool fail_erase; ++ unsigned int erase_pages; ++ bool checkpoint_slot, show_staged, checkpoint_published; ++ bool fail_reserve_once; ++ unsigned int installs, recovery_reads; ++ enum sftl_test_commit commit; + bool dirty_candidate; + bool vfl_candidate; + bool full_lanes; +@@ -117,10 +132,12 @@ static int sftl_test_context(struct sftl_fixture *f, unsigned int lane, + if (page <= 2) + for (i = 1; i <= 4; i++) + put_unaligned_le16(0, data + i * 2); +- /* Three initially blank append candidates, single-lane by default. */ +- if (page == 1 && !f->no_candidates) +- for (i = 5; i <= 7; i++) ++ /* Initially blank append candidates, single-lane by default. */ ++ if (page == 1) ++ for (i = 5; i < 5 + f->append_candidates; i++) + put_unaligned_le16(0, data + i * 2); ++ if (page == 1 && f->checkpoint_slot) ++ put_unaligned_le16(0, data + 7 * 2); + if (f->full_lanes) + put_unaligned_le16(0, data + 5 * 2); + if (page == 1 && f->vfl_candidate) { +@@ -147,6 +164,10 @@ static void sftl_test_checkpoint(struct sftl_fixture *f, u8 *data, u8 *oob) + memset(data, 0, 4096); + put_unaligned_le32(1, data); + put_unaligned_le32(1, data + 4); ++ if (f->checkpoint_slot) { ++ put_unaligned_le32(2, data); ++ put_unaligned_le32(7, data + 8); ++ } + sftl_test_meta(oob, 0x1f, 1, 100 + f->sequence_bias, 0); + if (f->fault == SFTL_INCOMPLETE_CONTEXT) + return; +@@ -254,6 +275,11 @@ static int sftl_test_read_page(void *priv, unsigned int bus, unsigned int cau, + return sftl_test_context(f, lane, page, data, oob, status); + return 0; + } ++ if (f->checkpoint_slot && ((block == 1 && f->checkpoint_published) || ++ (block == 7 && !f->checkpoint_published && !f->show_staged))) ++ return 0; ++ if (block == 7 && f->checkpoint_published && !page) ++ f->recovery_reads++; + if (block == 3 && (f->fault == SFTL_RUNTIME_FAILURE || + (!position && f->fault == SFTL_READ_FAILURE))) + return -EIO; +@@ -350,6 +376,23 @@ static int sftl_test_program_page(void *priv, unsigned int bus, unsigned int cau + return failure ? -EIO : 0; + } + ++/* The GC scenarios own one normal lane of block 5. Other blocks hold ++ * the saved context, open journal, or mapped data and must survive. ++ */ ++static int sftl_test_erase_block(void *priv, unsigned int bus, unsigned int cau, ++ unsigned int block) ++{ ++ struct sftl_fixture *f = priv; ++ ++ f->erases++; ++ if (block != 5 || bus || cau || !f->written[0] || ++ (f->checkpoint_slot && !f->recovery_reads)) ++ return -EPERM; ++ memset(f->written[0] + 128 - f->erase_pages, 0, ++ f->erase_pages * sizeof(*f->written[0])); ++ return f->fail_erase ? -EIO : 0; ++} ++ + static const struct apple_ppn_ops sftl_test_ops = { + .read_page = sftl_test_read_page, + .program_page = sftl_test_program_page, +@@ -364,7 +407,10 @@ static struct sftl_fixture *sftl_test_fixture(struct kunit *test, + return NULL; + f->test = test; + f->fault = fault; +- f->provider.ops = &sftl_test_ops; ++ f->ops = sftl_test_ops; ++ f->provider.ops = &f->ops; ++ f->append_candidates = 3; ++ f->erase_pages = 128; + f->provider.priv = f; + f->provider.buses = 2; + f->provider.caus = 8; +@@ -872,7 +918,7 @@ static void sftl_test_append_reserves_vfl_members(struct kunit *test) + KUNIT_ASSERT_NOT_NULL(test, f); + KUNIT_ASSERT_NOT_NULL(test, data); + f->vfl_candidate = true; +- f->no_candidates = true; ++ f->append_candidates = 0; + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); + memset(data, 'V', 4096); + KUNIT_ASSERT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE, data, 1), 0); +@@ -929,7 +975,7 @@ static void sftl_test_append_no_space_keeps_reads(struct kunit *test) + + KUNIT_ASSERT_NOT_NULL(test, f); + KUNIT_ASSERT_NOT_NULL(test, data); +- f->no_candidates = true; ++ f->append_candidates = 0; + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); + memset(data, 'N', 4096); + KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE, data, 1), -ENOSPC); +@@ -1007,7 +1053,635 @@ static void sftl_test_checkpoint_cold_roundtrip(struct kunit *test) + apple_sftl_checkpoint_free(checkpoint); + } + ++/* Place the final internal key in the old block. Exported disk reads ++ * cannot see this key, but the allocation map still has to protect it. ++ */ ++static int sftl_test_gc_internal_key(struct sftl_fixture *f, ++ struct apple_sftl_snapshot *map) ++{ ++ struct sftl_test_page *page = sftl_test_written(f, 0, 0, 0); ++ struct sftl_test_page *footer = sftl_test_written(f, 0, 0, 120); ++ struct apple_sftl_extent *runs, *last; ++ ++ if (!page || !footer || get_unaligned_le32(footer->data + 12) != 480) ++ return -EINVAL; ++ put_unaligned_le32(TEST_LOGICAL_PAGES - 1, page->oob + 8); ++ memcpy(footer->data + 16, footer->data, 16); ++ put_unaligned_le32(TEST_LOGICAL_PAGES - 1, footer->data + 8); ++ put_unaligned_le32(1, footer->data + 12); ++ put_unaligned_le32(1, footer->data + 16); ++ put_unaligned_le32(TEST_APPEND_BASE + 1, footer->data + 24); ++ put_unaligned_le32(479, footer->data + 28); ++ ++ runs = kunit_kcalloc(f->test, map->nr_runs + 1, sizeof(*runs), GFP_KERNEL); ++ if (!runs) ++ return -ENOMEM; ++ memcpy(runs, map->runs, map->nr_runs * sizeof(*runs)); ++ last = &runs[map->nr_runs - 1]; ++ if (last->provider != APPLE_SFTL_NO_ADDRESS || last->length < 2) ++ return -EINVAL; ++ last->length--; ++ runs[map->nr_runs++] = (struct apple_sftl_extent) { ++ .logical = TEST_LOGICAL_PAGES - 1, .length = 1, .provider = 5 * TEST_STRIDE, ++ }; ++ map->runs = runs; ++ return 0; ++} ++ ++enum sftl_test_gc_checkpoint { ++ GC_NO_CHECKPOINT, ++ GC_CHECKPOINT_BEFORE_OVERWRITE, ++ GC_CHECKPOINT_AFTER_OVERWRITE, ++ GC_CHECKPOINT_WITH_INTERNAL_KEY, ++}; ++ ++static int sftl_test_gc_save(struct sftl_fixture *f, struct apple_sftl *sftl, ++ bool internal_key) ++{ ++ struct apple_sftl_snapshot *snapshot = NULL, map; ++ u32 blocks[] = { 1, 7 }; ++ int ret; ++ ++ ret = apple_sftl_snapshot(sftl, 65536, &snapshot); ++ if (ret) ++ return ret; ++ map = *snapshot; ++ if (internal_key) ++ ret = sftl_test_gc_internal_key(f, &map); ++ if (!ret) ++ ret = apple_sftl_checkpoint_build(&map, blocks, f->checkpoint_slot ? 2 : 1, 16, ++ &f->checkpoint); ++ apple_sftl_snapshot_free(snapshot); ++ return ret; ++} ++ ++/* Write two complete generations onto the only two available blocks. ++ * Block 6 supersedes all the user data in block 5. The saved checkpoint is ++ * an input to the test; normal writes perform the subsequent erase and reuse. ++ */ ++static int sftl_test_gc_medium(struct sftl_fixture *f, u8 *data, ++ enum sftl_test_gc_checkpoint checkpoint) ++{ ++ struct apple_sftl *sftl = NULL; ++ unsigned int i; ++ int ret; ++ ++ f->append_candidates = 2; ++ ret = apple_sftl_load(&f->provider, &sftl); ++ if (ret) ++ return ret; ++ ret = sftl_test_fill_append(sftl, data); ++ if (ret) ++ goto out; ++ if (checkpoint == GC_CHECKPOINT_BEFORE_OVERWRITE) { ++ ret = sftl_test_gc_save(f, sftl, false); ++ if (ret) ++ goto out; ++ apple_sftl_free(sftl); ++ ret = apple_sftl_load(&f->provider, &sftl); ++ if (ret) ++ return ret; ++ } ++ memset(data, 'L', APPLE_PPN_PAGE_SIZE); ++ for (i = 0; i < TEST_APPEND_PROGRAMS; i++) { ++ ret = apple_sftl_write(sftl, TEST_APPEND_BASE + 4 * i, data, 4); ++ if (ret) ++ goto out; ++ } ++ if (checkpoint == GC_CHECKPOINT_AFTER_OVERWRITE || checkpoint == GC_CHECKPOINT_WITH_INTERNAL_KEY) ++ ret = sftl_test_gc_save(f, sftl, checkpoint == GC_CHECKPOINT_WITH_INTERNAL_KEY); ++out: ++ apple_sftl_free(sftl); ++ return ret; ++} ++ ++static void sftl_test_gc_reuses_obsolete_block(struct kunit *test) ++{ ++ struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); ++ struct apple_sftl *sftl = NULL; ++ u8 *data = kunit_kmalloc(test, APPLE_PPN_PAGE_SIZE, GFP_KERNEL); ++ u32 new_key = TEST_APPEND_BASE + 480; ++ unsigned int page; ++ ++ KUNIT_ASSERT_NOT_NULL(test, f); ++ KUNIT_ASSERT_NOT_NULL(test, data); ++ KUNIT_ASSERT_EQ(test, sftl_test_gc_medium(f, data, GC_CHECKPOINT_AFTER_OVERWRITE), 0); ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ memset(data, 'R', 4096); ++ KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, new_key, data, 1), -ENOSPC); ++ apple_sftl_free(sftl); ++ f->ops.erase_block = sftl_test_erase_block; ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ KUNIT_ASSERT_EQ(test, apple_sftl_write(sftl, new_key, data, 1), 0); ++ KUNIT_EXPECT_EQ(test, f->erases, 1U); ++ KUNIT_ASSERT_NOT_NULL(test, sftl_test_written(f, 0, 0, 0)); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le32(sftl_test_written(f, 0, 0, 0)->oob + 8), ++ new_key); ++ KUNIT_EXPECT_EQ(test, apple_sftl_flush(sftl), 0); ++ apple_sftl_free(sftl); ++ ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ KUNIT_ASSERT_EQ(test, apple_sftl_read(sftl, new_key, data), 0); ++ sftl_test_expect_fill(test, data, 'R'); ++ for (page = 0; page < 480; page++) { ++ KUNIT_ASSERT_EQ(test, apple_sftl_read(sftl, TEST_APPEND_BASE + page, data), 0); ++ sftl_test_expect_fill(test, data, 'L'); ++ } ++ KUNIT_ASSERT_EQ(test, apple_sftl_read(sftl, 0, data), 0); ++ sftl_test_expect_fill(test, data, 'C'); ++ KUNIT_ASSERT_EQ(test, apple_sftl_read(sftl, 2, data), 0); ++ sftl_test_expect_fill(test, data, 'G'); ++ apple_sftl_free(sftl); ++ apple_sftl_checkpoint_free(f->checkpoint); ++} ++ ++/* Apple also writes a one-page footer with sparse sequence allocation. ++ * Model an older generation, followed later by all the mapped data. ++ */ ++static void sftl_test_gc_short_native_footer(struct kunit *test) ++{ ++ struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); ++ struct apple_sftl *sftl = NULL; ++ struct sftl_test_page *p; ++ u8 *data = kunit_kmalloc(test, APPLE_PPN_PAGE_SIZE, GFP_KERNEL); ++ unsigned int page, chunk; ++ u64 base = 10; ++ ++ KUNIT_ASSERT_NOT_NULL(test, f); ++ KUNIT_ASSERT_NOT_NULL(test, data); ++ f->sequence_bias = 2048; ++ KUNIT_ASSERT_EQ(test, sftl_test_gc_medium(f, data, GC_CHECKPOINT_AFTER_OVERWRITE), 0); ++ for (page = 0; page < 127; page++) { ++ p = sftl_test_written(f, 0, 0, page); ++ KUNIT_ASSERT_NOT_NULL(test, p); ++ for (chunk = 0; chunk < 4; chunk++) { ++ u32 slot = page * 4 + chunk; ++ u64 sequence = base + slot + (slot >= 480 ? 8 : 0); ++ ++ sftl_test_meta(p->oob + 16 * chunk, 1, 0, sequence, ++ TEST_APPEND_BASE + slot % 480); ++ } ++ } ++ p = sftl_test_written(f, 0, 0, 127); ++ KUNIT_ASSERT_NOT_NULL(test, p); ++ memset(p->data, 0, sizeof(p->data)); ++ put_unaligned_le32(TEST_APPEND_BASE, p->data + 8); ++ put_unaligned_le32(480, p->data + 12); ++ put_unaligned_le32(488, p->data + 16); ++ put_unaligned_le32(TEST_APPEND_BASE, p->data + 24); ++ put_unaligned_le32(28, p->data + 28); ++ for (chunk = 0; chunk < 4; chunk++) { ++ u8 *meta = p->oob + 16 * chunk; ++ ++ sftl_test_meta(meta, 0x1c, 8, base, 516); ++ put_unaligned_le16(4, meta + 12); ++ put_unaligned_le16(0xffff, meta + 14); ++ } ++ f->ops.erase_block = sftl_test_erase_block; ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ memset(data, 'S', 4096); ++ KUNIT_ASSERT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 480, data, 1), 0); ++ KUNIT_EXPECT_EQ(test, f->erases, 1U); ++ apple_sftl_free(sftl); ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ KUNIT_ASSERT_EQ(test, apple_sftl_read(sftl, TEST_APPEND_BASE + 480, data), 0); ++ sftl_test_expect_fill(test, data, 'S'); ++ apple_sftl_free(sftl); ++ apple_sftl_checkpoint_free(f->checkpoint); ++} ++ ++static void sftl_test_gc_preserves_protected_blocks(struct kunit *test) ++{ ++ static const enum sftl_test_gc_checkpoint cases[] = { ++ GC_NO_CHECKPOINT, /* Needed to replay the journal */ ++ GC_CHECKPOINT_BEFORE_OVERWRITE, /* Still live in the durable map */ ++ GC_CHECKPOINT_WITH_INTERNAL_KEY, ++ GC_CHECKPOINT_AFTER_OVERWRITE, /* Closed footer hides a newer record */ ++ }; ++ unsigned int i; ++ ++ for (i = 0; i < ARRAY_SIZE(cases); i++) { ++ struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); ++ struct apple_sftl *sftl = NULL; ++ u8 *data = kunit_kmalloc(test, APPLE_PPN_PAGE_SIZE, GFP_KERNEL); ++ unsigned int programs; ++ ++ KUNIT_ASSERT_NOT_NULL(test, f); ++ KUNIT_ASSERT_NOT_NULL(test, data); ++ KUNIT_ASSERT_EQ(test, sftl_test_gc_medium(f, data, cases[i]), 0); ++ if (cases[i] == GC_CHECKPOINT_AFTER_OVERWRITE) { ++ struct sftl_test_page *p = sftl_test_written(f, 0, 0, 64); ++ ++ KUNIT_ASSERT_NOT_NULL(test, p); ++ sftl_test_meta(p->oob, 2, 0, f->checkpoint->sequence + 1, 0xffff0001); ++ } ++ f->ops.erase_block = sftl_test_erase_block; ++ programs = f->programs; ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ KUNIT_EXPECT_LT(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 480, data, 1), 0); ++ KUNIT_EXPECT_EQ(test, f->erases, 0U); ++ KUNIT_EXPECT_EQ(test, f->programs, programs); ++ apple_sftl_free(sftl); ++ apple_sftl_checkpoint_free(f->checkpoint); ++ } ++} ++ ++static void sftl_test_gc_erase_failure(struct kunit *test) ++{ ++ static const struct { ++ unsigned int pages; ++ bool fails; ++ } outcome[] = { ++ { 0, true }, { 64, true }, { 128, true }, ++ { 64, false }, /* A false success cannot bypass blank readback. */ ++ }; ++ unsigned int i; ++ ++ for (i = 0; i < ARRAY_SIZE(outcome); i++) { ++ struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); ++ struct apple_sftl *sftl = NULL; ++ u8 *data = kunit_kmalloc(test, APPLE_PPN_PAGE_SIZE, GFP_KERNEL); ++ unsigned int programs; ++ ++ KUNIT_ASSERT_NOT_NULL(test, f); ++ KUNIT_ASSERT_NOT_NULL(test, data); ++ KUNIT_ASSERT_EQ(test, sftl_test_gc_medium(f, data, GC_CHECKPOINT_AFTER_OVERWRITE), 0); ++ f->ops.erase_block = sftl_test_erase_block; ++ f->fail_erase = outcome[i].fails; ++ f->erase_pages = outcome[i].pages; ++ programs = f->programs; ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ KUNIT_EXPECT_LT(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 480, data, 1), 0); ++ KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 480, data, 1), -EIO); ++ KUNIT_EXPECT_EQ(test, f->erases, 1U); ++ KUNIT_EXPECT_EQ(test, f->programs, programs); ++ apple_sftl_free(sftl); ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ KUNIT_ASSERT_EQ(test, apple_sftl_read(sftl, TEST_APPEND_BASE + 480, data), 0); ++ sftl_test_expect_fill(test, data, 0); ++ KUNIT_ASSERT_EQ(test, apple_sftl_read(sftl, TEST_APPEND_BASE, data), 0); ++ sftl_test_expect_fill(test, data, 'L'); ++ if (outcome[i].pages == 64) { ++ f->fail_erase = false; ++ KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 480, ++ data, 1), -ENOSPC); ++ KUNIT_EXPECT_EQ(test, f->erases, 1U); ++ KUNIT_EXPECT_EQ(test, f->programs, programs); ++ } ++ apple_sftl_free(sftl); ++ apple_sftl_checkpoint_free(f->checkpoint); ++ } ++} ++ ++/* The saved map still owns five slots at the end of closed block 5. ++ * Block 6 is the only remaining blank block. ++ */ ++static int sftl_test_gc_live_medium(struct sftl_fixture *f, u8 *data) ++{ ++ struct apple_sftl *sftl = NULL; ++ unsigned int i; ++ int ret; ++ ++ f->append_candidates = 2; ++ f->checkpoint_slot = true; ++ ret = apple_sftl_load(&f->provider, &sftl); ++ if (ret) ++ return ret; ++ for (i = 0; i < TEST_APPEND_PROGRAMS - 1; i++) { ++ memset(data, i + 1, 4096); ++ ret = apple_sftl_write(sftl, TEST_APPEND_BASE, data, 1); ++ if (ret) ++ goto out; ++ } ++ for (i = 0; i < 4; i++) ++ memset(data + i * 4096, 120 + i, 4096); ++ ret = apple_sftl_write(sftl, TEST_APPEND_BASE + 1, data, 4); ++ if (!ret) ++ ret = sftl_test_gc_save(f, sftl, false); ++out: ++ apple_sftl_free(sftl); ++ return ret; ++} ++ ++static void sftl_test_gc_preserves_last_blank_capacity(struct kunit *test) ++{ ++ struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); ++ struct apple_sftl *sftl = NULL; ++ u8 *data = kunit_kmalloc(test, APPLE_PPN_PAGE_SIZE, GFP_KERNEL); ++ unsigned int i; ++ ++ KUNIT_ASSERT_NOT_NULL(test, f); ++ KUNIT_ASSERT_NOT_NULL(test, data); ++ KUNIT_ASSERT_EQ(test, sftl_test_gc_live_medium(f, data), 0); ++ f->ops.erase_block = sftl_test_erase_block; ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ /* Without a checkpoint writer, all 480 foreground pages must fit. */ ++ memset(data, 'W', APPLE_PPN_PAGE_SIZE); ++ for (i = 0; i < TEST_APPEND_PROGRAMS; i++) ++ KUNIT_ASSERT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 5 + 4 * i, ++ data, 4), 0); ++ KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 485, data, 1), -ENOSPC); ++ KUNIT_EXPECT_EQ(test, f->programs, 256U); ++ KUNIT_EXPECT_EQ(test, f->erases, 0U); ++ apple_sftl_free(sftl); ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ for (i = 0; i < 5; i++) { ++ KUNIT_ASSERT_EQ(test, apple_sftl_read(sftl, TEST_APPEND_BASE + i, data), 0); ++ sftl_test_expect_fill(test, data, 119 + i); ++ } ++ KUNIT_ASSERT_EQ(test, apple_sftl_read(sftl, TEST_APPEND_BASE + 484, data), 0); ++ sftl_test_expect_fill(test, data, 'W'); ++ apple_sftl_free(sftl); ++ apple_sftl_checkpoint_free(f->checkpoint); ++} ++ ++static int sftl_test_gc_reserve(void *priv, u32 *blocks, u32 *nr_blocks) ++{ ++ struct sftl_fixture *f = priv; ++ ++ if (f->fail_reserve_once) { ++ f->fail_reserve_once = false; ++ return -ENOMEM; ++ } ++ if (!f->checkpoint_slot || f->checkpoint_published || !*nr_blocks) ++ return -ENOSPC; ++ blocks[0] = 7; ++ *nr_blocks = 1; ++ return 0; ++} ++ ++/* This fixture has two context slots with an atomic selection switch. Read ++ * the new slot through the NAND provider, independently of the submitted image. ++ */ ++static int sftl_test_gc_install(void *priv, const struct apple_sftl_checkpoint *image, ++ struct apple_sftl_checkpoint **out) ++{ ++ struct sftl_fixture *f = priv; ++ struct apple_sftl_checkpoint *readback; ++ struct sftl_test_page *page; ++ unsigned int i; ++ u8 status; ++ int ret; ++ ++ f->installs++; ++ *out = NULL; ++ if (image->count != 4 || get_unaligned_le32(image->records[0].data + 4) != 7) ++ return -EINVAL; ++ page = kunit_kmalloc(f->test, sizeof(*page), GFP_KERNEL); ++ if (!page) ++ return -ENOMEM; ++ for (i = 0; i < 4; i++) { ++ memcpy(page->data + i * 4096, image->records[i].data, 4096); ++ memcpy(page->oob + i * 16, image->records[i].meta, 16); ++ } ++ ret = sftl_test_program_page(f, 0, 0, 7, 0, page->data, page->oob); ++ if (ret || f->commit == SFTL_COMMIT_IO_ERROR) ++ return ret ? ret : -EIO; ++ f->show_staged = true; ++ ret = sftl_test_read_page(f, 0, 0, 7, 0, false, page->data, page->oob, &status); ++ f->show_staged = false; ++ if (ret || status != 0x40) ++ return ret ? ret : -EIO; ++ readback = kzalloc(sizeof(*readback), GFP_KERNEL); ++ if (!readback) ++ return -ENOMEM; ++ readback->records = kcalloc(4, sizeof(*readback->records), GFP_KERNEL); ++ if (!readback->records) { ++ kfree(readback); ++ return -ENOMEM; ++ } ++ readback->count = 4; ++ readback->first_sequence = get_unaligned_le64(page->oob) >> 16; ++ readback->sequence = get_unaligned_le64(page->oob + 48) >> 16; ++ for (i = 0; i < 4; i++) { ++ memcpy(readback->records[i].data, page->data + i * 4096, 4096); ++ memcpy(readback->records[i].meta, page->oob + i * 16, 16); ++ } ++ if (f->commit == SFTL_COMMIT_BAD_READBACK) ++ readback->records[0].data[64] ^= 1; ++ f->checkpoint_published = f->commit == SFTL_COMMIT_OK; ++ *out = readback; ++ return 0; ++} ++ ++static const struct apple_sftl_checkpoint_ops sftl_test_gc_ops = { ++ .reserve = sftl_test_gc_reserve, ++ .install = sftl_test_gc_install, ++}; ++ ++static void sftl_test_gc_relocated_data(struct kunit *test, struct apple_sftl *sftl, ++ u8 *data, u8 foreground) ++{ ++ unsigned int i; ++ ++ for (i = 0; i < 5; i++) { ++ KUNIT_ASSERT_EQ(test, apple_sftl_read(sftl, TEST_APPEND_BASE + i, data), 0); ++ sftl_test_expect_fill(test, data, 119 + i); ++ } ++ KUNIT_ASSERT_EQ(test, apple_sftl_read(sftl, TEST_APPEND_BASE + 5, data), 0); ++ sftl_test_expect_fill(test, data, foreground); ++} ++ ++static void sftl_test_gc_retries_reservation(struct kunit *test) ++{ ++ struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); ++ struct apple_sftl *sftl = NULL; ++ u8 *data = kunit_kmalloc(test, APPLE_PPN_PAGE_SIZE, GFP_KERNEL); ++ unsigned int programs; ++ ++ KUNIT_ASSERT_NOT_NULL(test, f); ++ KUNIT_ASSERT_NOT_NULL(test, data); ++ KUNIT_ASSERT_EQ(test, sftl_test_gc_live_medium(f, data), 0); ++ f->ops.erase_block = sftl_test_erase_block; ++ f->fail_reserve_once = true; ++ programs = f->programs; ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ apple_sftl_set_checkpoint_ops(sftl, &sftl_test_gc_ops, f); ++ memset(data, 'R', 4096); ++ KUNIT_ASSERT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 5, data, 1), -ENOMEM); ++ KUNIT_EXPECT_EQ(test, f->programs, programs); ++ KUNIT_EXPECT_EQ(test, f->erases, 0U); ++ KUNIT_EXPECT_EQ(test, f->installs, 0U); ++ KUNIT_ASSERT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 5, data, 1), 0); ++ KUNIT_EXPECT_EQ(test, f->erases, 1U); ++ KUNIT_EXPECT_EQ(test, f->installs, 1U); ++ apple_sftl_free(sftl); ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ sftl_test_gc_relocated_data(test, sftl, data, 'R'); ++ apple_sftl_free(sftl); ++ apple_sftl_checkpoint_free(f->checkpoint); ++} ++ ++static void sftl_test_gc_relocates_on_write(struct kunit *test) ++{ ++ struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); ++ struct apple_sftl *sftl = NULL, *cold = NULL; ++ u8 *data = kunit_kmalloc(test, APPLE_PPN_PAGE_SIZE, GFP_KERNEL); ++ struct sftl_test_page *page, *copy; ++ u8 saved[APPLE_PPN_OOB_SIZE]; ++ unsigned int i; ++ ++ KUNIT_ASSERT_NOT_NULL(test, f); ++ KUNIT_ASSERT_NOT_NULL(test, data); ++ KUNIT_ASSERT_EQ(test, sftl_test_gc_live_medium(f, data), 0); ++ f->ops.erase_block = sftl_test_erase_block; ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ apple_sftl_set_checkpoint_ops(sftl, &sftl_test_gc_ops, f); ++ memset(data, 'W', 4096); ++ KUNIT_ASSERT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 5, data, 1), 0); ++ KUNIT_EXPECT_EQ(test, f->installs, 1U); ++ KUNIT_EXPECT_EQ(test, f->erases, 1U); ++ KUNIT_EXPECT_PTR_EQ(test, sftl_test_written(f, 0, 0, 0), NULL); ++ for (i = 0; i < 5; i++) { ++ unsigned int slot = 3 + i; /* Head padding repeats the first key. */ ++ ++ page = sftl_test_written(f, 1, 0, slot / 4); ++ KUNIT_ASSERT_NOT_NULL(test, page); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le32(page->oob + (slot % 4) * 16 + 8), ++ TEST_APPEND_BASE + i); ++ sftl_test_expect_fill(test, page->data + slot % 4 * 4096, 119 + i); ++ } ++ page = sftl_test_written(f, 1, 0, 2); ++ KUNIT_ASSERT_NOT_NULL(test, page); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le32(page->oob + 8), TEST_APPEND_BASE + 5); ++ sftl_test_gc_relocated_data(test, sftl, data, 'W'); ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &cold), 0); ++ sftl_test_gc_relocated_data(test, cold, data, 'W'); ++ apple_sftl_free(cold); ++ ++ /* Close the same block across the checkpoint's sequence gap. */ ++ memset(data, 'Z', APPLE_PPN_PAGE_SIZE); ++ for (i = 3; i < TEST_APPEND_PROGRAMS; i++) ++ KUNIT_ASSERT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 4 * i, data, 4), 0); ++ page = sftl_test_written(f, 1, 0, 127); ++ KUNIT_ASSERT_NOT_NULL(test, page); ++ KUNIT_EXPECT_EQ(test, page->oob[0], (u8)0x1c); ++ /* A power cut before the last footer program leaves a recoverable prefix. */ ++ f->written[1][127] = NULL; ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &cold), 0); ++ sftl_test_gc_relocated_data(test, cold, data, 'W'); ++ apple_sftl_free(cold); ++ /* Move one sequence gap outside the checkpoint, keeping the post- ++ * checkpoint journal and partial footer intact. Recovery must reject it. ++ */ ++ copy = sftl_test_written(f, 1, 0, 1); ++ memcpy(saved, copy->oob, sizeof(saved)); ++ for (i = 0; i < 4; i++) { ++ u8 *meta = copy->oob + i * 16; ++ u64 seq = get_unaligned_le64(meta) >> 16; ++ ++ sftl_test_meta(meta, 1, 0, seq + 1, get_unaligned_le32(meta + 8)); ++ } ++ KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &cold), -EBADMSG); ++ apple_sftl_free(cold); ++ memcpy(copy->oob, saved, sizeof(saved)); ++ f->written[1][127] = page; ++ memset(data, 'Z', APPLE_PPN_PAGE_SIZE); ++ KUNIT_ASSERT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 480, data, 1), 0); ++ KUNIT_ASSERT_NOT_NULL(test, sftl_test_written(f, 0, 0, 0)); ++ KUNIT_EXPECT_EQ(test, f->erases, 1U); ++ apple_sftl_free(sftl); ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &cold), 0); ++ sftl_test_gc_relocated_data(test, cold, data, 'W'); ++ KUNIT_ASSERT_EQ(test, apple_sftl_read(cold, TEST_APPEND_BASE + 480, data), 0); ++ sftl_test_expect_fill(test, data, 'Z'); ++ apple_sftl_free(cold); ++ apple_sftl_checkpoint_free(f->checkpoint); ++} ++ ++static void sftl_test_gc_relocation_stops_before_erase(struct kunit *test) ++{ ++ static const struct { ++ enum sftl_test_commit commit; ++ bool copy_error; ++ } modes[] = { ++ { .copy_error = true }, ++ { .commit = SFTL_COMMIT_IO_ERROR }, ++ { .commit = SFTL_COMMIT_BAD_READBACK }, ++ { .commit = SFTL_COMMIT_UNPUBLISHED }, ++ }; ++ unsigned int mode; ++ ++ for (mode = 0; mode < ARRAY_SIZE(modes); mode++) { ++ struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); ++ struct apple_sftl *sftl = NULL; ++ u8 *data = kunit_kmalloc(test, APPLE_PPN_PAGE_SIZE, GFP_KERNEL); ++ unsigned int i, programs; ++ ++ KUNIT_ASSERT_NOT_NULL(test, f); ++ KUNIT_ASSERT_NOT_NULL(test, data); ++ KUNIT_ASSERT_EQ(test, sftl_test_gc_live_medium(f, data), 0); ++ f->ops.erase_block = sftl_test_erase_block; ++ f->commit = modes[mode].commit; ++ if (modes[mode].copy_error) { ++ /* The second copied page lands, but completion reports a failure. */ ++ f->fail_program = f->programs + 2; ++ f->uncertain_program = true; ++ } ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ apple_sftl_set_checkpoint_ops(sftl, &sftl_test_gc_ops, f); ++ KUNIT_EXPECT_LT(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 5, data, 1), 0); ++ KUNIT_EXPECT_EQ(test, f->installs, modes[mode].copy_error ? 0U : 1U); ++ KUNIT_EXPECT_EQ(test, f->erases, 0U); ++ for (i = 0; i < 128; i++) ++ KUNIT_ASSERT_NOT_NULL(test, sftl_test_written(f, 0, 0, i)); ++ KUNIT_EXPECT_PTR_EQ(test, sftl_test_written(f, 1, 0, 2), NULL); ++ programs = f->programs; ++ KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 5, data, 1), -EIO); ++ KUNIT_EXPECT_EQ(test, f->programs, programs); ++ apple_sftl_free(sftl); ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ sftl_test_gc_relocated_data(test, sftl, data, 0); ++ apple_sftl_free(sftl); ++ apple_sftl_checkpoint_free(f->checkpoint); ++ } ++} ++ ++static void sftl_test_gc_preserves_last_sequences(struct kunit *test) ++{ ++ struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); ++ struct apple_sftl *sftl = NULL; ++ struct apple_sftl_info info; ++ u8 *data = kunit_kmalloc(test, APPLE_PPN_PAGE_SIZE, GFP_KERNEL); ++ const u64 limit = (1ULL << 48) - 1; ++ unsigned int i; ++ ++ KUNIT_ASSERT_NOT_NULL(test, f); ++ KUNIT_ASSERT_NOT_NULL(test, data); ++ /* Copy (8) and checkpoint (4) would exhaust the remaining sequence IDs. */ ++ f->sequence_bias = limit - (TEST_APPEND_FIRST + 479 + 4) - 12; ++ KUNIT_ASSERT_EQ(test, sftl_test_gc_live_medium(f, data), 0); ++ f->ops.erase_block = sftl_test_erase_block; ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ apple_sftl_get_info(sftl, &info); ++ KUNIT_ASSERT_EQ(test, info.sequence, limit - 12); ++ apple_sftl_set_checkpoint_ops(sftl, &sftl_test_gc_ops, f); ++ memset(data, 'B', 4096); ++ for (i = 0; i < 3; i++) ++ KUNIT_ASSERT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 5 + i, data, 1), 0); ++ KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 8, data, 1), -EOVERFLOW); ++ KUNIT_EXPECT_EQ(test, f->erases, 0U); ++ KUNIT_EXPECT_EQ(test, f->installs, 0U); ++ KUNIT_EXPECT_EQ(test, f->programs, 131U); ++ apple_sftl_free(sftl); ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ apple_sftl_get_info(sftl, &info); ++ KUNIT_EXPECT_EQ(test, info.sequence, limit); ++ sftl_test_gc_relocated_data(test, sftl, data, 'B'); ++ apple_sftl_free(sftl); ++ apple_sftl_checkpoint_free(f->checkpoint); ++} ++ + static struct kunit_case apple_sftl_cases[] = { ++ KUNIT_CASE(sftl_test_gc_retries_reservation), ++ KUNIT_CASE(sftl_test_gc_preserves_last_sequences), ++ KUNIT_CASE(sftl_test_gc_relocates_on_write), ++ KUNIT_CASE(sftl_test_gc_relocation_stops_before_erase), ++ KUNIT_CASE(sftl_test_gc_preserves_last_blank_capacity), ++ KUNIT_CASE(sftl_test_gc_reuses_obsolete_block), ++ KUNIT_CASE(sftl_test_gc_short_native_footer), ++ KUNIT_CASE(sftl_test_gc_preserves_protected_blocks), ++ KUNIT_CASE(sftl_test_gc_erase_failure), + KUNIT_CASE(sftl_test_append_replay_open), + KUNIT_CASE(sftl_test_append_replay_closed), + KUNIT_CASE(sftl_test_append_replay_full_lanes), +diff --git a/drivers/mtd/apple-sftl.h b/drivers/mtd/apple-sftl.h +index 0babcebb0..e8c9ffbf9 100644 +--- a/drivers/mtd/apple-sftl.h ++++ b/drivers/mtd/apple-sftl.h +@@ -6,6 +6,21 @@ + #include + + struct apple_sftl; ++struct apple_sftl_checkpoint; ++ ++/* Internal checkpoint backend. reserve selects owned context blocks without ++ * modifying media. install atomically publishes a native checkpoint, retires ++ * the previous fallback, and returns independently read, caller-owned bytes. ++ * An error may be indeterminate. The caller stops the instance on failure. ++ */ ++struct apple_sftl_checkpoint_ops { ++ int (*reserve)(void *priv, u32 *blocks, u32 *nr_blocks); ++ int (*install)(void *priv, const struct apple_sftl_checkpoint *image, ++ struct apple_sftl_checkpoint **readback); ++}; ++ ++void apple_sftl_set_checkpoint_ops(struct apple_sftl *sftl, ++ const struct apple_sftl_checkpoint_ops *ops, void *priv); + + struct apple_sftl_info { + u32 logical_pages; +diff --git a/drivers/mtd/devices/Kconfig b/drivers/mtd/devices/Kconfig +index 22782e845..a43defee2 100644 +--- a/drivers/mtd/devices/Kconfig ++++ b/drivers/mtd/devices/Kconfig +@@ -216,8 +216,9 @@ config MTD_APPLE_H2FMI + driver. One MTD per CAU; geometry is read from each package. + + Pages are programmed whole, only when they read back erased, and +- each program is verified by reading it back. The driver has no +- erase operation. ++ each program is verified by reading it back. The aggregate PPN ++ provider can verify a single-block erase on qualified hardware. ++ Erase is disabled on the P105. + + If unsure, say N. + +@@ -226,7 +227,7 @@ config MTD_APPLE_H2FMI_KUNIT_TEST + depends on KUNIT + default KUNIT_ALL_TESTS + help +- Test PPN parameter parsing, row encoding and page-status handling. ++ Test PPN parameter parsing, erase row bounds and page-status handling. + Both measured PPN package profiles are covered. + These tests use synthetic parameter pages and do not access hardware. + From 59d744416acc9d1788f7183afcdede6caf01b4c1 Mon Sep 17 00:00:00 2001 From: Shayne <8741557+shaynelegault@users.noreply.github.com> Date: Wed, 18 Nov 2026 22:45:20 -0600 Subject: [PATCH 03/11] P105: recover and verify native flash checkpoints Export the native-format save and recovery path, healthy VFL rotation and block-accounting guards to the ARMv7 MTD series. Record the captured-media and interruption results, and the provider accounting still needed before enabling physical erase and reuse. Assisted-by: LLM --- chips/s5l8942x.toml | 2 +- devices/p105/device.toml | 4 +- docs/bring-up/p105-sftl-checkpoint-gc.md | 106 +- ...otate-the-healthy-replicated-PPN-VFL.patch | 576 +++ ...ncode-and-verify-native-context-page.patch | 1063 ++++++ ...ecover-interrupted-saves-and-verify-.patch | 3183 +++++++++++++++++ 6 files changed, 4905 insertions(+), 29 deletions(-) create mode 100644 kernel/patches/armv7/mtd/0011-mtd-apple-sftl-Rotate-the-healthy-replicated-PPN-VFL.patch create mode 100644 kernel/patches/armv7/mtd/0012-mtd-apple-sftl-Encode-and-verify-native-context-page.patch create mode 100644 kernel/patches/armv7/mtd/0013-mtd-apple-sftl-Recover-interrupted-saves-and-verify-.patch diff --git a/chips/s5l8942x.toml b/chips/s5l8942x.toml index 2677cc0f..2d28a6ca 100644 --- a/chips/s5l8942x.toml +++ b/chips/s5l8942x.toml @@ -17,5 +17,5 @@ stages = "Project Cascadia P105 boot chain" reference_version = "Project Cascadia builds its A5 boot chain from 8.4.1 / 12H321" nand_reads = "Measured P105 64 GB PPN geometry has eight CAUs per bus, 2088 blocks per CAU and 128 normal pages per block; exact ARMv7 read-topic qualification is pending (docs/bring-up/p105-nand-reads.md)." nand_program = "The H2FMI driver programs complete PPN pages only into erased destinations, verifies full readback and leaves physical erase disabled pending qualification (docs/bring-up/p105-nand-program.md)." -nand_checkpoint_gc = "The 64 GB SwissPPNFTL writer verifies ownership before recycling a retired block on a qualified provider. Reclamation is tested on synthetic media. Physical P105 erase and reuse await qualification (docs/bring-up/p105-sftl-checkpoint-gc.md)." +nand_checkpoint_gc = "The 64 GB SwissPPNFTL reader recovers supported torn checkpoints read-only. Native-format saves and healthy VFL rotation are verified against synthetic and captured media. P105 accounting and physical erase qualification remain open (docs/bring-up/p105-sftl-checkpoint-gc.md)." usb_phy = "P105 uses a 24 MHz reference; the proposed S5L8942X cold-handoff callbacks await an exact-revision boot (docs/bring-up/p105-usb-handoff.md)." diff --git a/devices/p105/device.toml b/devices/p105/device.toml index 4467457d..753a23ba 100644 --- a/devices/p105/device.toml +++ b/devices/p105/device.toml @@ -29,8 +29,8 @@ recipe = "firmware.json" # (OrchardLinux/orchard#93), freed space is never reused and writes fail once # the blank flash runs out. Drop this table when it lands; the installer # warns from it and allows the install either way. -# The P105 does not advertise verified erase. Synthetic reclamation results -# are recorded in docs/bring-up/p105-sftl-checkpoint-gc.md. +# Torn native checkpoints can recover read-only. P105 checkpoint writes and +# physical erase remain disabled (docs/bring-up/p105-sftl-checkpoint-gc.md). [storage] reclaims = false diff --git a/docs/bring-up/p105-sftl-checkpoint-gc.md b/docs/bring-up/p105-sftl-checkpoint-gc.md index 20745fe3..5b6fce87 100644 --- a/docs/bring-up/p105-sftl-checkpoint-gc.md +++ b/docs/bring-up/p105-sftl-checkpoint-gc.md @@ -15,9 +15,18 @@ valid native footer, predate the saved checkpoint, and be absent from both the checkpoint map and the current map. The allocator verifies each record against its footer and compares the active map with a fresh load. It excludes bootstrap and context blocks, unknown lane flags, ambiguous checkpoint -generations and blocks that may still be needed for journal replay. +generations and blocks that may still be needed for journal replay. To +erase an obsolete native donor directly, the saved block role must already +be dirty-free with zero live pages. -A writer with an atomic checkpoint installer can also compact a partially +For the native layout, a new append block must also have the clean-free +class and zero saved mappings. Two of the five such blocks in the captured +checkpoint were consumed by later writes. The other three had erased heads +and no live mappings. Allocation still verifies the entire block. The +native role table also protects a third context block that the two-entry +checkpoint header does not name. + +A writer with a qualified checkpoint accounting policy can also compact a partially live block. The allocator keeps a verified blank landing block while space is available. Under space pressure it copies live records into that block, checks the data and out-of-band metadata after each program, and allows room @@ -33,26 +42,60 @@ all data and out-of-band bytes from all 128 pages of each affected lane. The allocator also verifies the whole logical block before reuse. An indeterminate erase or failed verification stops the writer instance. +## Checkpoint publication and recovery + +The two-block SwissPPNFTL save and VFL bank rotation operate on different +parts of the medium. + +| Operation | Write order | Recovery evidence | +| --- | --- | --- | +| SwissPPNFTL checkpoint | Write the named blank alternate in normal view, verify each page, reload with both generations visible, retire the old context, reload again, then erase the data donor. | The native base and accounting prefix contains 32 records. The map is padded in batches of 64 records. Coverage of the last logical key can occur many pages before the save finishes. | +| Healthy VFL rotation | Erase the four target SLC members, save a checkpoint referencing the source tables, copy all 16 lane tables, then save the final checkpoint with the target references. | Each page has four replicas. The source bank can contain a table update after its last complete checkpoint. The executor checks that tail before changing the target. | + +Recovery from an incomplete native batch preserves confirmed journal writes +and exposes the disk read-only when the next safe sequence is uncertain. +If retirement stops between lanes, the new generation must name the old +context site and every surviving record there must predate the selected +generation. Conflicting references, missing live records or unsupported +geometry can still prevent a mount. + +The encoder derives per-superblock valid logical-page counts and the global +valid-page statistic from the whole map. These values matched every one of +the 2,086 superblocks in the archived native checkpoint. The writer also +rebuilds the per-block user sequence statistics from closed footers. A live +page copy receives a new physical write sequence and keeps the high 32 bits +of the original user sequence in its tag. The tests supply the remaining +allocation policy for synthetic media. The P105 driver keeps native +checkpoint writes disabled. + ## Source and verification -The ARMv7 patches in `kernel/patches/armv7/mtd/` build on the merged +The patches in `kernel/patches/armv7/mtd/` apply to the merged [ARMv7 append writer](https://github.com/OrchardLinux/linux/commit/0c6503279c702b44a340c37f072cd9170d6bf4a4). -The [kernel source](https://github.com/shaynelegault/linux/commit/ccd56d3c440c0e84d1581667d43a0437b3e221f8) -contains KUnit fixtures for both the ordinary write path and the planner. - -The mixed-block fixture leaves five live 4 KiB records in a closed block. -A normal write triggers relocation and a synthetic two-slot checkpoint -installation. The test reloads that checkpoint through the real reader, -checks all five records and the triggering write, then writes into the -reclaimed block and reloads again. Other fixtures exercise incomplete -footers, corruption, failed programs, partial erase and a checkpoint that -was never published. A simulated controller transaction checks every byte of -the erased lane, including the last byte of the last page. - -On this revision, 31 reader/writer cases and nine planner cases passed under -ASan and UBSan. Differential checks matched 24 native metadata vectors, -round-tripped 209 maps and verified 66 relocation sizes. Eleven ARMv7 object -configurations compiled with warnings treated as errors. +The [kernel source](https://github.com/shaynelegault/linux/commit/64e248acc722bbf6a1171caf07d3858853de6ad3) +contains the synthetic KUnit fixtures. + +- A normal write moved five live records, programmed a 24-page native-format + checkpoint and reused the donor. Cold loads verified the map with both + contexts visible and again after retirement. The fixtures cover all 25 + save boundaries, readback faults, and power cuts between the old context's + lane erases. +- The C reader replayed captured device pages through journal sequence + 187831212. All 15,625,016 keys agreed with the archived map. All 95 + incomplete prefixes of a 96-record second generation preserved the map, + including interruptions inside pages and during map padding. + Erasing 1–15 of the 16 old lanes preserved the new map read-only. A fully + retired old context permitted healthy recovery. +- Replaying the recorded healthy VFL rotation reproduced all 72 target + replica pages, including their out-of-band metadata. All 256 source pages + remained intact. Synthetic tests also interrupted the operation after + each of its 76 member erases or page programs. +- The reader rejects a historical 283-FTL-superblock checkpoint for this + 2,086-superblock volume. The native loader and setter treat this field as + the size of the FTL address space. +- All 59 host sanitizer cases passed, along with 209 range-map round trips, + 66 relocation cases and 24 native metadata vectors. The ARMv7 object + checks covered 17 configurations. The checkpoint metadata and footer evidence comes from IOFlashStorage-730 on build 13G36 and the [pinned Cascadia NAND research](https://github.com/rzxvx/Project-Cascadia/blob/c20b85ebb951c704d7e3a664c378dde8276e3928/docs/research/p105-nand.md). @@ -61,15 +104,26 @@ document the starting evidence. ## Remaining before hardware enablement -The checkpoint installer used by the tests models an atomic switch. A native -P105 installer must update the accounting fields, keep the old table -references during the first VFL save, publish the new references in the -second save, and recover correctly if power fails during either save or -during table copying. The current encoder preserves the opaque native -accounting records. +The remaining software work is to account for allocation of the landing +block, open write streams, wear and the timing of native counter updates. +On all 341 qualifying captured closed donors, native finalization changes +class 3 to class 9 and clears the read count. After a statistics refresh, +the free count increases and the data count decreases. The native writer +can save the previously cached counters before that refresh. Applying the +donor delta directly to the old checkpoint could therefore publish the +wrong counts. Each save also requests two statistics payloads from the +native provider. ID 255 reads 88 bytes of VFL state (13G36 handler +`0x810a048c`), and ID 254 reads 464 bytes of lower-provider state +(`0x8108ac18`). The archive contains one version of this pair. A pair of +checkpoints bracketing a known native relocation, or an independently +validated replay of those provider events, would allow a byte-for-byte +comparison of the resulting state. Scrub and bad-member replacement also +need qualification. Physical qualification must establish the exact erase footprint in both normal and SLC views, exclusive ownership across raw MTD and SFTL operations, -and recovery after a partial erase or block retirement. End-to-end testing +and recovery after a partial erase or block retirement. Before a physical +donor can be returned for writing, the driver must also reconcile the +reported erase outcome with native wear and free-block state. End-to-end testing on the measured P105 must verify cold-boot recovery and preservation of existing iOS data before the device can advertise reclamation. diff --git a/kernel/patches/armv7/mtd/0011-mtd-apple-sftl-Rotate-the-healthy-replicated-PPN-VFL.patch b/kernel/patches/armv7/mtd/0011-mtd-apple-sftl-Rotate-the-healthy-replicated-PPN-VFL.patch new file mode 100644 index 00000000..f00a0c29 --- /dev/null +++ b/kernel/patches/armv7/mtd/0011-mtd-apple-sftl-Rotate-the-healthy-replicated-PPN-VFL.patch @@ -0,0 +1,576 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Shayne <8741557+shaynelegault@users.noreply.github.com> +Date: Wed, 18 Nov 2026 22:25:39 -0600 +Subject: [PATCH] mtd: apple-sftl: Rotate the healthy replicated PPN VFL banks + +Verify the source checkpoint, all lane tables and any later table updates before touching the alternate bank. Erase and verify its four members, publish the checkpoint that references the old tables, copy the tables, then publish their new references. Read back every replica and retain the source on every error. Exercise both directions and interrupted operations with a qualified synthetic SLC backend. + +Assisted-by: LLM +--- + drivers/mtd/Makefile | 3 +- + drivers/mtd/apple-sftl-vfl-test.c | 206 ++++++++++++++++++++++ + drivers/mtd/apple-sftl-vfl.c | 282 ++++++++++++++++++++++++++++++ + drivers/mtd/apple-sftl-vfl.h | 39 +++++ + 4 files changed, 529 insertions(+), 1 deletion(-) + create mode 100644 drivers/mtd/apple-sftl-vfl-test.c + create mode 100644 drivers/mtd/apple-sftl-vfl.c + create mode 100644 drivers/mtd/apple-sftl-vfl.h + +diff --git a/drivers/mtd/Makefile b/drivers/mtd/Makefile +index 8a05767a1..c22c89937 100644 +--- a/drivers/mtd/Makefile ++++ b/drivers/mtd/Makefile +@@ -33,6 +33,7 @@ obj-$(CONFIG_MTD_UBI) += ubi/ + obj-$(CONFIG_MTD_HYPERBUS) += hyperbus/ + + obj-$(CONFIG_MTD_APPLE_SFTL) += apple-sftl.o +-apple-sftl-y := apple-sftl-core.o apple-sftl-block.o apple-sftl-reclaim.o ++apple-sftl-y := apple-sftl-core.o apple-sftl-block.o apple-sftl-reclaim.o apple-sftl-vfl.o + obj-$(CONFIG_MTD_APPLE_SFTL_KUNIT_TEST) += apple-sftl-test.o + obj-$(CONFIG_MTD_APPLE_SFTL_KUNIT_TEST) += apple-sftl-reclaim-test.o ++obj-$(CONFIG_MTD_APPLE_SFTL_KUNIT_TEST) += apple-sftl-vfl-test.o +diff --git a/drivers/mtd/apple-sftl-vfl-test.c b/drivers/mtd/apple-sftl-vfl-test.c +new file mode 100644 +index 000000000..403ae52df +--- /dev/null ++++ b/drivers/mtd/apple-sftl-vfl-test.c +@@ -0,0 +1,206 @@ ++// SPDX-License-Identifier: GPL-2.0-only ++/* Single-fragment, two-bank synthetic VFL media with physical replica cuts. */ ++#include ++#include ++#include ++#include ++#include ++ ++#include "apple-sftl-vfl.h" ++ ++#define SOURCE_AGE (U32_MAX - 128) ++#define TARGET_AGE (SOURCE_AGE - 64) ++ ++struct vfl_fixture { ++ struct apple_sftl_vfl_source source; ++ struct apple_ppn ppn; ++ u8 *expected; ++ u8 next[4]; ++ bool erased[4], bad_tail, target_ref, stale, bad_readback, short_erase; ++ unsigned int mutations, cut; ++}; ++ ++static void test_tag(u8 *oob, u8 lane, u32 age) ++{ ++ memset(oob, 0xff, 64); ++ put_unaligned_le32(lane == 0xff ? 0x00ffff20 : 0x20 | (u32)lane << 8, oob); ++ put_unaligned_le32(age, oob + 4); ++ put_unaligned_le64(0, oob + 8); ++} ++ ++static void test_context(struct vfl_fixture *f, u8 *data, bool initial, bool final) ++{ ++ unsigned int i; ++ ++ memset(data, 0, APPLE_PPN_PAGE_SIZE); ++ put_unaligned_le16(16, data + 10); ++ put_unaligned_le16(2086, data + 12); ++ put_unaligned_le16(2086, data + 14); ++ for (i = 0; i < 8; i++) { ++ put_unaligned_le16(f->source.members[i].block, data + 0x100 + i * 4); ++ data[0x102 + i * 4] = f->source.members[i].lane; ++ } ++ for (i = 0; i < 16; i++) { ++ u16 ref = initial && i == 9 ? 63 : i + 1; ++ bool other = final || (f->target_ref && !i); ++ ++ ref |= (f->source.group ^ other) << 15; ++ put_unaligned_le16(ref, data + 0x200 + 32 * i); ++ } ++} ++ ++static void test_target_page(struct vfl_fixture *f, unsigned int page, u8 *data, u8 *oob) ++{ ++ if (!page || page == 17) { ++ test_context(f, data, !page, page == 17); ++ test_tag(oob, 0xff, TARGET_AGE - page); ++ } else { ++ /* Lane 9 received its last table update in source page 63. */ ++ memset(data, page == 10 ? 63 : page, APPLE_PPN_PAGE_SIZE); ++ test_tag(oob, page - 1, TARGET_AGE - page); ++ } ++} ++ ++static int test_read(void *priv, unsigned int bus, unsigned int cau, unsigned int block, ++ unsigned int page, bool context, u8 *data, u8 *oob, u8 *status) ++{ ++ struct vfl_fixture *f = priv; ++ unsigned int member = 2 * cau + bus; ++ ++ if (!context || member >= 8 || block != 2087 || page >= 64) ++ return -ERANGE; ++ *status = 0x40; ++ if (member >= 4) { ++ member -= 4; ++ if (page < f->next[member]) { ++ test_target_page(f, page, data, oob); ++ if (f->bad_readback && page == 16 && member == 3) ++ data[APPLE_PPN_PAGE_SIZE - 1] ^= 1; ++ } else if (!page && !f->erased[member]) { ++ test_context(f, data, false, false); ++ test_tag(oob, 0xff, f->stale ? SOURCE_AGE - 64 : U32_MAX); ++ } else { ++ *status = 0x49; ++ memset(data, 0xff, APPLE_PPN_PAGE_SIZE); ++ memset(oob, 0xff, APPLE_PPN_OOB_SIZE); ++ } ++ } else if (page == 62) { ++ test_context(f, data, false, false); ++ test_tag(oob, 0xff, SOURCE_AGE - page); ++ } else if ((page >= 1 && page <= 16) || page == 63) { ++ memset(data, page, APPLE_PPN_PAGE_SIZE); ++ test_tag(oob, page == 63 ? 9 : page - 1, SOURCE_AGE - page); ++ if (f->bad_tail && member == 3 && page == 63) ++ data[17]++; ++ } else { ++ return -ERANGE; ++ } ++ return 0; ++} ++ ++static int test_erase(void *priv, const struct apple_sftl_vfl_member *m) ++{ ++ struct vfl_fixture *f = priv; ++ ++ if (m->block != 2087 || m->lane < 4 || m->lane >= 8 || f->erased[m->lane - 4]) ++ return -EINVAL; ++ if (!f->short_erase) ++ f->erased[m->lane - 4] = true; ++ return ++f->mutations == f->cut ? -EIO : 0; ++} ++ ++static int test_program(void *priv, const struct apple_sftl_vfl_member *m, u8 page, ++ const u8 *data, const u8 *oob) ++{ ++ struct vfl_fixture *f = priv; ++ u8 expected_oob[APPLE_PPN_OOB_SIZE]; ++ ++ if (m->block != 2087 || m->lane < 4 || m->lane >= 8 || !f->erased[m->lane - 4] || ++ page > 17 || f->next[m->lane - 4] != page || f->mutations != 4U * page + m->lane) ++ return -EINVAL; ++ test_target_page(f, page, f->expected, expected_oob); ++ if (memcmp(f->expected, data, APPLE_PPN_PAGE_SIZE) || ++ memcmp(expected_oob, oob, sizeof(expected_oob))) ++ return -EBADMSG; ++ f->next[m->lane - 4]++; ++ return ++f->mutations == f->cut ? -EIO : 0; ++} ++ ++static const struct apple_ppn_ops test_read_ops = { .read_page = test_read }; ++static const struct apple_sftl_vfl_ops test_write_ops = { ++ .erase = test_erase, .program = test_program, ++}; ++ ++static void vfl_rotate_test(struct kunit *test) ++{ ++ struct vfl_fixture f = { ++ .source = { .page = 62, .age = SOURCE_AGE - 62 }, ++ .ppn = { .ops = &test_read_ops, .buses = 2, .caus = 8, .blocks = 2088, .pages = 128 }, ++ }; ++ unsigned int i, cut; ++ ++ f.ppn.priv = &f; ++ f.expected = kunit_kmalloc(test, APPLE_PPN_PAGE_SIZE, GFP_KERNEL); ++ KUNIT_ASSERT_NOT_NULL(test, f.expected); ++ for (i = 0; i < 8; i++) { ++ f.source.members[i].block = 2087; ++ f.source.members[i].lane = i; ++ } ++ ++ /* Exercise every durable operation including a completed but unacknowledged ++ * last replica. No failure permits another program or a source erase. ++ */ ++ for (cut = 0; cut <= 76; cut++) { ++ f.cut = cut; ++ f.mutations = 0; ++ memset(f.erased, 0, sizeof(f.erased)); ++ memset(f.next, 0, sizeof(f.next)); ++ KUNIT_ASSERT_EQ(test, apple_sftl_vfl_rotate(&f.ppn, &f.source, &test_write_ops, &f), ++ cut ? -EIO : 0); ++ KUNIT_EXPECT_EQ(test, f.mutations, cut ? cut : 76U); ++ } ++ memset(f.erased, 0, sizeof(f.erased)); ++ memset(f.next, 0, sizeof(f.next)); ++ f.cut = 0; ++ f.mutations = 0; ++ f.bad_tail = true; ++ KUNIT_EXPECT_EQ(test, apple_sftl_vfl_rotate(&f.ppn, &f.source, &test_write_ops, &f), -EBADMSG); ++ f.bad_tail = false; ++ f.target_ref = true; ++ KUNIT_EXPECT_EQ(test, apple_sftl_vfl_rotate(&f.ppn, &f.source, &test_write_ops, &f), -EOPNOTSUPP); ++ f.target_ref = false; ++ f.stale = true; ++ KUNIT_EXPECT_EQ(test, apple_sftl_vfl_rotate(&f.ppn, &f.source, &test_write_ops, &f), -EBADMSG); ++ KUNIT_EXPECT_EQ(test, f.mutations, 0U); ++ f.stale = false; ++ f.short_erase = true; ++ KUNIT_EXPECT_EQ(test, apple_sftl_vfl_rotate(&f.ppn, &f.source, &test_write_ops, &f), -EBADMSG); ++ KUNIT_EXPECT_EQ(test, f.mutations, 1U); ++ f.short_erase = false; ++ f.mutations = 0; ++ f.bad_readback = true; ++ KUNIT_EXPECT_EQ(test, apple_sftl_vfl_rotate(&f.ppn, &f.source, &test_write_ops, &f), -EBADMSG); ++ KUNIT_EXPECT_EQ(test, f.mutations, 72U); ++ f.bad_readback = false; ++ f.mutations = 0; ++ memset(f.erased, 0, sizeof(f.erased)); ++ memset(f.next, 0, sizeof(f.next)); ++ f.source.group = 1; ++ for (i = 0; i < 8; i++) ++ f.source.members[i].lane = i ^ 4; ++ KUNIT_EXPECT_EQ(test, apple_sftl_vfl_rotate(&f.ppn, &f.source, &test_write_ops, &f), 0); ++ KUNIT_EXPECT_EQ(test, f.mutations, 76U); ++} ++ ++static struct kunit_case vfl_cases[] = { ++ KUNIT_CASE(vfl_rotate_test), ++ {} ++}; ++ ++static struct kunit_suite vfl_suite = { ++ .name = "apple-sftl-vfl-rotate", ++ .test_cases = vfl_cases, ++}; ++kunit_test_suite(vfl_suite); ++ ++MODULE_LICENSE("GPL"); +diff --git a/drivers/mtd/apple-sftl-vfl.c b/drivers/mtd/apple-sftl-vfl.c +new file mode 100644 +index 000000000..9ab992bfe +--- /dev/null ++++ b/drivers/mtd/apple-sftl-vfl.c +@@ -0,0 +1,282 @@ ++// SPDX-License-Identifier: GPL-2.0-only ++/* Healthy single-fragment VFL bank rotation on the measured 64 GB geometry. */ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#include "apple-sftl-vfl.h" ++ ++#define VFL_LANES 16 ++#define VFL_COPIES 4 ++#define VFL_PAGES 64 ++#define VFL_GLOBAL 0xff ++ ++struct vfl_page { ++ u8 data[APPLE_PPN_PAGE_SIZE]; ++ u8 oob[APPLE_PPN_OOB_SIZE]; ++}; ++ ++struct vfl_rotation { ++ struct apple_ppn *ppn; ++ const struct apple_sftl_vfl_source *source; ++ const struct apple_sftl_vfl_ops *ops; ++ void *priv; ++ struct vfl_page checkpoint, tables[VFL_LANES], first, other; ++}; ++ ++static int vfl_read(struct vfl_rotation *r, unsigned int group, unsigned int member, ++ unsigned int page, struct vfl_page *out) ++{ ++ const struct apple_sftl_vfl_member *m = &r->source->members[group * VFL_COPIES + member]; ++ u8 status; ++ int ret; ++ ++ ret = r->ppn->ops->read_page(r->ppn->priv, m->lane & 1, m->lane >> 1, ++ m->block, page, true, out->data, out->oob, &status); ++ if (ret) ++ return ret; ++ if (status == 0x40) ++ return 0; ++ if (status == 0x49 && !memchr_inv(out, 0xff, sizeof(*out))) ++ return 0; ++ return -EIO; ++} ++ ++static int vfl_read_copies(struct vfl_rotation *r, unsigned int group, ++ unsigned int page, struct vfl_page *out) ++{ ++ unsigned int i; ++ int ret; ++ ++ for (i = 0; i < VFL_COPIES; i++) { ++ ret = vfl_read(r, group, i, page, i ? &r->other : out); ++ if (ret) ++ return ret; ++ if (i && memcmp(out, &r->other, sizeof(*out))) ++ return -EBADMSG; ++ } ++ return 0; ++} ++ ++/* 13G36 _programVFLCxt emits a single 16-byte tag for this geometry. */ ++static void vfl_tag(u8 *oob, u8 lane, u32 age) ++{ ++ memset(oob, 0xff, APPLE_PPN_OOB_SIZE); ++ memset(oob, 0, 16); ++ oob[0] = 0x20; ++ oob[1] = lane; ++ oob[2] = lane == VFL_GLOBAL ? 0xff : 0; ++ put_unaligned_le32(age, oob + 4); ++} ++ ++static bool vfl_tag_matches(const struct vfl_page *p, u8 lane, u32 age) ++{ ++ u8 expected[APPLE_PPN_OOB_SIZE]; ++ ++ vfl_tag(expected, lane, age); ++ return !memcmp(p->oob, expected, sizeof(expected)); ++} ++ ++static bool vfl_members_valid(const struct apple_sftl_vfl_source *s) ++{ ++ unsigned int i, j; ++ ++ for (i = 0; i < ARRAY_SIZE(s->members); i++) { ++ const struct apple_sftl_vfl_member *m = &s->members[i]; ++ ++ if (m->block < 2056 || m->block >= 2088 || m->lane >= VFL_LANES) ++ return false; ++ for (j = 0; j < i; j++) ++ if (m->block == s->members[j].block && m->lane == s->members[j].lane) ++ return false; ++ } ++ return true; ++} ++ ++static bool vfl_members_match(const struct apple_sftl_vfl_source *s, const u8 *data) ++{ ++ unsigned int i; ++ ++ for (i = 0; i < ARRAY_SIZE(s->members); i++) { ++ const u8 *entry = data + 0x100 + i * 4; ++ const struct apple_sftl_vfl_member *m = &s->members[i]; ++ ++ if (get_unaligned_le16(entry) != m->block || entry[2] != m->lane || entry[3]) ++ return false; ++ } ++ return true; ++} ++ ++static u8 *vfl_reference(struct vfl_page *p, unsigned int lane) ++{ ++ return p->data + 0x200 + lane * 32; ++} ++ ++/* The selected checkpoint and every older table it names must outlive erase. */ ++static int vfl_source_pages(struct vfl_rotation *r) ++{ ++ const struct apple_sftl_vfl_source *s = r->source; ++ struct vfl_page *cp = &r->checkpoint; ++ unsigned int lane, page; ++ int ret; ++ ++ ret = vfl_read_copies(r, s->group, s->page, cp); ++ if (ret) ++ return ret; ++ if (!vfl_tag_matches(cp, VFL_GLOBAL, s->age) || ++ !vfl_members_match(s, cp->data) || ++ get_unaligned_le16(cp->data + 10) != VFL_LANES || ++ get_unaligned_le16(cp->data + 14) != 2086 || ++ memchr_inv(cp->data + 0x400, 0, sizeof(cp->data) - 0x400)) ++ return -EOPNOTSUPP; ++ for (lane = 0; lane < VFL_LANES; lane++) { ++ u8 *field = vfl_reference(cp, lane); ++ u16 ref = get_unaligned_le16(field); ++ ++ page = ref & 0x7fff; ++ if (ref >> 15 != s->group || page >= s->page || ++ memchr_inv(field + 2, 0, 30)) ++ return -EOPNOTSUPP; ++ ret = vfl_read_copies(r, s->group, page, &r->tables[lane]); ++ if (ret) ++ return ret; ++ if (!vfl_tag_matches(&r->tables[lane], lane, s->age + (s->page - page))) ++ return -EBADMSG; ++ } ++ /* A full bank can end with a table update after its last global save. */ ++ for (page = s->page + 1; page < VFL_PAGES; page++) { ++ ret = vfl_read_copies(r, s->group, page, &r->first); ++ if (ret) ++ return ret; ++ lane = r->first.oob[1]; ++ if (lane >= VFL_LANES || ++ !vfl_tag_matches(&r->first, lane, s->age - (page - s->page))) ++ return -EOPNOTSUPP; ++ memcpy(&r->tables[lane], &r->first, sizeof(r->first)); ++ put_unaligned_le16((s->group << 15) | page, vfl_reference(cp, lane)); ++ } ++ return 0; ++} ++ ++/* Refuse a stale source receipt or an interrupted/replaced target bank. */ ++static int vfl_check_target(struct vfl_rotation *r) ++{ ++ const struct apple_sftl_vfl_source *s = r->source; ++ unsigned int page, lane; ++ u32 first_age = 0; ++ bool ended = false; ++ int ret; ++ ++ for (page = 0; page < VFL_PAGES; page++) { ++ ret = vfl_read_copies(r, !s->group, page, &r->first); ++ if (ret) ++ return ret; ++ if (!memchr_inv(&r->first, 0xff, sizeof(r->first))) { ++ ended = true; ++ continue; ++ } ++ lane = r->first.oob[1]; ++ if (!page) { ++ first_age = get_unaligned_le32(r->first.oob + 4); ++ if (lane != VFL_GLOBAL) ++ return -EOPNOTSUPP; ++ } ++ if (ended || (lane != VFL_GLOBAL && lane >= VFL_LANES) || ++ (lane == VFL_GLOBAL && !vfl_members_match(s, r->first.data)) || ++ first_age < page || first_age - page <= s->age + s->page || ++ !vfl_tag_matches(&r->first, lane, first_age - page)) ++ return -EBADMSG; ++ } ++ return 0; ++} ++ ++static int vfl_erase_target(struct vfl_rotation *r) ++{ ++ unsigned int group = !r->source->group, i, page; ++ int ret; ++ ++ for (i = 0; i < VFL_COPIES; i++) { ++ ret = r->ops->erase(r->priv, &r->source->members[group * VFL_COPIES + i]); ++ if (ret) ++ return ret; ++ for (page = 0; page < VFL_PAGES; page++) { ++ ret = vfl_read(r, group, i, page, &r->first); ++ if (ret) ++ return ret; ++ if (memchr_inv(&r->first, 0xff, sizeof(r->first))) ++ return -EBADMSG; ++ } ++ } ++ return 0; ++} ++ ++static int vfl_program(struct vfl_rotation *r, unsigned int page, struct vfl_page *p, ++ u8 lane, u32 age) ++{ ++ unsigned int group = !r->source->group, i; ++ int ret; ++ ++ vfl_tag(p->oob, lane, age); ++ for (i = 0; i < VFL_COPIES; i++) { ++ ret = r->ops->program(r->priv, &r->source->members[group * VFL_COPIES + i], ++ page, p->data, p->oob); ++ if (ret) ++ return ret; ++ ret = vfl_read(r, group, i, page, &r->first); ++ if (ret) ++ return ret; ++ if (memcmp(&r->first, p, sizeof(*p))) ++ return -EBADMSG; ++ } ++ return 0; ++} ++ ++int apple_sftl_vfl_rotate(struct apple_ppn *ppn, const struct apple_sftl_vfl_source *s, ++ const struct apple_sftl_vfl_ops *ops, void *priv) ++{ ++ struct vfl_rotation *r; ++ u32 age; ++ unsigned int lane; ++ int ret; ++ ++ if (!ppn || !ppn->ops || !ppn->ops->read_page || !s || ++ !ops || !ops->erase || !ops->program) ++ return -EOPNOTSUPP; ++ if (ppn->buses != 2 || ppn->caus != 8 || ppn->blocks != 2088 || ppn->pages != 128 || ++ !vfl_members_valid(s) || s->group > 1 || s->page >= VFL_PAGES || ++ s->age > U32_MAX - s->page || ++ s->age < BIT(31) + (VFL_PAGES - s->page) + VFL_LANES + 1) ++ return -EOPNOTSUPP; ++ r = kvzalloc(sizeof(*r), GFP_NOIO); ++ if (!r) ++ return -ENOMEM; ++ r->ppn = ppn; ++ r->source = s; ++ r->ops = ops; ++ r->priv = priv; ++ age = s->age - (VFL_PAGES - s->page); ++ ret = vfl_source_pages(r); ++ if (!ret) ++ ret = vfl_check_target(r); ++ if (!ret) ++ ret = vfl_erase_target(r); ++ if (!ret) ++ ret = vfl_program(r, 0, &r->checkpoint, VFL_GLOBAL, age); ++ for (lane = 0; !ret && lane < VFL_LANES; lane++) { ++ ret = vfl_program(r, lane + 1, &r->tables[lane], lane, age - lane - 1); ++ if (ret) ++ break; ++ put_unaligned_le16((!s->group << 15) | (lane + 1), ++ vfl_reference(&r->checkpoint, lane)); ++ } ++ if (!ret) ++ ret = vfl_program(r, VFL_LANES + 1, &r->checkpoint, ++ VFL_GLOBAL, age - VFL_LANES - 1); ++ kvfree(r); ++ return ret; ++} ++EXPORT_SYMBOL_GPL(apple_sftl_vfl_rotate); +diff --git a/drivers/mtd/apple-sftl-vfl.h b/drivers/mtd/apple-sftl-vfl.h +new file mode 100644 +index 000000000..58a62d7f2 +--- /dev/null ++++ b/drivers/mtd/apple-sftl-vfl.h +@@ -0,0 +1,39 @@ ++/* SPDX-License-Identifier: GPL-2.0-only */ ++#ifndef _APPLE_SFTL_VFL_H ++#define _APPLE_SFTL_VFL_H ++ ++#include ++ ++struct apple_sftl; ++ ++struct apple_sftl_vfl_member { ++ u16 block; ++ u8 lane; ++}; ++ ++/* Location of the recovered checkpoint. Newer table records can follow it. */ ++struct apple_sftl_vfl_source { ++ struct apple_sftl_vfl_member members[8]; ++ u32 age; ++ u8 group, page; ++}; ++ ++int apple_sftl_vfl_source(struct apple_sftl *sftl, ++ struct apple_sftl_vfl_source *out); ++ ++/* ++ * Only a backend with exclusive ownership of all eight SLC members, including ++ * their physical aliases, may supply these operations. They must never remap ++ * a bad member silently. On any error, reload before attempting more writes. ++ */ ++struct apple_sftl_vfl_ops { ++ int (*erase)(void *priv, const struct apple_sftl_vfl_member *member); ++ int (*program)(void *priv, const struct apple_sftl_vfl_member *member, ++ u8 page, const u8 *data, const u8 *oob); ++}; ++ ++int apple_sftl_vfl_rotate(struct apple_ppn *ppn, ++ const struct apple_sftl_vfl_source *source, ++ const struct apple_sftl_vfl_ops *ops, void *priv); ++ ++#endif diff --git a/kernel/patches/armv7/mtd/0012-mtd-apple-sftl-Encode-and-verify-native-context-page.patch b/kernel/patches/armv7/mtd/0012-mtd-apple-sftl-Encode-and-verify-native-context-page.patch new file mode 100644 index 00000000..d7eacdf3 --- /dev/null +++ b/kernel/patches/armv7/mtd/0012-mtd-apple-sftl-Encode-and-verify-native-context-page.patch @@ -0,0 +1,1063 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Shayne <8741557+shaynelegault@users.noreply.github.com> +Date: Wed, 18 Nov 2026 22:36:36 -0600 +Subject: [PATCH] mtd: apple-sftl: Encode and verify native context pages + +Pad complete native map batches, account for the whole logical map and +retain the original user age when relocating ordinary data. Reconstruct +per-block age summaries from native footer runs. Separate healthy donor +finalization from the later refresh of cached statistics. + +Prebuild the planned checkpoint before any copy and program the verified +alternate context in whole pages with independent readback. + +Assisted-by: LLM +--- + drivers/mtd/Makefile | 4 +- + drivers/mtd/apple-sftl-account-test.c | 189 ++++++++++++++++ + drivers/mtd/apple-sftl-account.c | 311 ++++++++++++++++++++++++++ + drivers/mtd/apple-sftl-account.h | 36 +++ + drivers/mtd/apple-sftl-reclaim-test.c | 15 +- + drivers/mtd/apple-sftl-reclaim.c | 196 ++++++++++++++-- + drivers/mtd/apple-sftl-reclaim.h | 21 ++ + drivers/mtd/apple-sftl-test-fixture.h | 50 +++++ + 8 files changed, 797 insertions(+), 25 deletions(-) + create mode 100644 drivers/mtd/apple-sftl-account-test.c + create mode 100644 drivers/mtd/apple-sftl-account.c + create mode 100644 drivers/mtd/apple-sftl-account.h + create mode 100644 drivers/mtd/apple-sftl-test-fixture.h + +diff --git a/drivers/mtd/Makefile b/drivers/mtd/Makefile +index c22c89937..3acbdbac5 100644 +--- a/drivers/mtd/Makefile ++++ b/drivers/mtd/Makefile +@@ -33,7 +33,7 @@ obj-$(CONFIG_MTD_UBI) += ubi/ + obj-$(CONFIG_MTD_HYPERBUS) += hyperbus/ + + obj-$(CONFIG_MTD_APPLE_SFTL) += apple-sftl.o +-apple-sftl-y := apple-sftl-core.o apple-sftl-block.o apple-sftl-reclaim.o apple-sftl-vfl.o ++apple-sftl-y := apple-sftl-core.o apple-sftl-block.o apple-sftl-reclaim.o apple-sftl-account.o apple-sftl-vfl.o + obj-$(CONFIG_MTD_APPLE_SFTL_KUNIT_TEST) += apple-sftl-test.o + obj-$(CONFIG_MTD_APPLE_SFTL_KUNIT_TEST) += apple-sftl-reclaim-test.o +-obj-$(CONFIG_MTD_APPLE_SFTL_KUNIT_TEST) += apple-sftl-vfl-test.o ++obj-$(CONFIG_MTD_APPLE_SFTL_KUNIT_TEST) += apple-sftl-account-test.o apple-sftl-vfl-test.o +diff --git a/drivers/mtd/apple-sftl-account-test.c b/drivers/mtd/apple-sftl-account-test.c +new file mode 100644 +index 000000000..ee60d3f9c +--- /dev/null ++++ b/drivers/mtd/apple-sftl-account-test.c +@@ -0,0 +1,189 @@ ++// SPDX-License-Identifier: GPL-2.0-only ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#include "apple-sftl-account.h" ++#include "apple-sftl-test-fixture.h" ++ ++#define TEST_ACCOUNT_RECORDS 36 ++#define TEST_VALID_STAT_OFFSET (4 + 8 * 16 + 8) ++ ++static void account_fixture(struct kunit *test, struct apple_sftl_checkpoint *image) ++{ ++ u32 i; ++ ++ image->count = TEST_ACCOUNT_RECORDS; ++ image->records = kunit_kcalloc(test, image->count, sizeof(*image->records), ++ GFP_KERNEL); ++ if (!image->records) ++ return; ++ apple_sftl_test_native_prefix(image->records); ++ for (i = 32; i < image->count; i++) { ++ image->records[i].meta[0] = 0x1f; ++ image->records[i].meta[1] = 4; ++ put_unaligned_le32(U32_MAX, image->records[i].meta + 12); ++ } ++} ++ ++static void account_recompute_valid_lbas(struct kunit *test) ++{ ++ struct apple_sftl_extent runs[] = { ++ { 0, 3, 512 * APPLE_SFTL_STRIDE - 1 }, ++ { 3, APPLE_SFTL_LOGICAL_PAGES - 4, APPLE_SFTL_NO_ADDRESS }, ++ { APPLE_SFTL_LOGICAL_PAGES - 1, 1, 2085 * APPLE_SFTL_STRIDE + 1 }, ++ }; ++ struct apple_sftl_snapshot map = { .runs = runs, .nr_runs = ARRAY_SIZE(runs) }; ++ struct apple_sftl_checkpoint image = {}; ++ u8 *stats, *entry; ++ ++ account_fixture(test, &image); ++ KUNIT_ASSERT_NOT_NULL(test, image.records); ++ stats = image.records[4].data; ++ entry = image.records[8].data; ++ /* A previously referenced donor has a stale count of four. */ ++ put_unaligned_le16(4, entry + 2); ++ put_unaligned_le64(4, stats + TEST_VALID_STAT_OFFSET); ++ entry[0] = 3; ++ entry[1] = 16; ++ put_unaligned_le16(111, entry + 6); ++ stats[12] = 0xac; ++ image.records[16].data[7] = 0x76; ++ KUNIT_ASSERT_EQ(test, apple_sftl_account_map(&image, &map), 0); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le16(entry + 2), 0); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le16(entry + 511 * 8 + 2), 1); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le16(image.records[9].data + 2), 2); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le16(image.records[12].data + 37 * 8 + 2), 1); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le64(stats + TEST_VALID_STAT_OFFSET), 4ULL); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le16(entry), 0x1003); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le16(entry + 6), 111); ++ KUNIT_EXPECT_EQ(test, stats[12], (u8)0xac); ++ KUNIT_EXPECT_EQ(test, image.records[16].data[7], (u8)0x76); ++ runs[2].provider = APPLE_SFTL_NO_ADDRESS; ++ KUNIT_ASSERT_EQ(test, apple_sftl_account_map(&image, &map), 0); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le64(stats + TEST_VALID_STAT_OFFSET), 3ULL); ++} ++ ++static void account_invalid_layout_or_map(struct kunit *test) ++{ ++ struct apple_sftl_extent runs[] = { ++ { 0, 1, APPLE_SFTL_STRIDE }, { 1, APPLE_SFTL_LOGICAL_PAGES - 1, APPLE_SFTL_NO_ADDRESS }, ++ }; ++ struct apple_sftl_snapshot map = { .runs = runs, .nr_runs = ARRAY_SIZE(runs) }; ++ struct apple_sftl_checkpoint image = {}; ++ u8 *stats; ++ ++ account_fixture(test, &image); ++ KUNIT_ASSERT_NOT_NULL(test, image.records); ++ stats = image.records[4].data; ++ put_unaligned_le16(1, image.records[9].meta + 8); ++ KUNIT_EXPECT_EQ(test, apple_sftl_account_map(&image, &map), -EOPNOTSUPP); ++ put_unaligned_le16(512, image.records[9].meta + 8); ++ put_unaligned_le32(U32_MAX, stats + 8); ++ KUNIT_EXPECT_EQ(test, apple_sftl_account_map(&image, &map), -EOPNOTSUPP); ++ put_unaligned_le32(2, stats + 8); ++ put_unaligned_le64(1, stats + TEST_VALID_STAT_OFFSET); ++ KUNIT_EXPECT_EQ(test, apple_sftl_account_map(&image, &map), -EBADMSG); ++ put_unaligned_le64(0, stats + TEST_VALID_STAT_OFFSET); ++ runs[1].length--; ++ KUNIT_EXPECT_EQ(test, apple_sftl_account_map(&image, &map), -EBADMSG); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le64(stats + TEST_VALID_STAT_OFFSET), 0ULL); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le16(image.records[8].data + 8 + 2), 0); ++} ++ ++static void account_footer_user_sequence(struct kunit *test) ++{ ++ struct apple_sftl_user_seq summary = {}; ++ u8 footer[APPLE_SFTL_META * 4] = {}; ++ u32 high[] = { U32_MAX - 15, U32_MAX, 10, U32_MAX - 1 }; ++ u32 i; ++ ++ for (i = 0; i < ARRAY_SIZE(high); i++) { ++ put_unaligned_le32(high[i], footer + i * APPLE_SFTL_META + 4); ++ put_unaligned_le32(i + 1, footer + i * APPLE_SFTL_META + 12); ++ } ++ apple_sftl_summarize_user_seq(&summary, footer, ARRAY_SIZE(high)); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le32(summary.data), 10U); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le32(summary.data + 4), U32_MAX - 1); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le32(summary.data + 8), 2863311528U); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le32(summary.data + 12), 3U); ++ apple_sftl_summarize_user_seq(&summary, footer + APPLE_SFTL_META, 1); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le32(summary.data + 8), 0U); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le32(summary.data + 12), 1U); ++ apple_sftl_summarize_user_seq(&summary, footer, 0); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le32(summary.data), 0U); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le32(summary.data + 4), 0U); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le32(summary.data + 12), 1U); ++} ++ ++static void account_context_and_retired_donor(struct kunit *test) ++{ ++ struct apple_sftl_checkpoint image = {}; ++ struct apple_sftl_snapshot snapshot = {}; ++ u32 blocks[] = { 4, 1100 }; ++ u8 footer[APPLE_SFTL_META] = { 0x1c, 8 }; ++ u8 *p, *donor, *reads, *free, *data; ++ ++ account_fixture(test, &image); ++ KUNIT_ASSERT_NOT_NULL(test, image.records); ++ snapshot.nr_opaque = APPLE_SFTL_ACCOUNT_RECORDS; ++ snapshot.opaque = image.records; ++ p = image.records[10].data + 76 * 8; ++ image.records[8].data[4 * 8] = 7; ++ KUNIT_EXPECT_FALSE(test, apple_sftl_account_context_pair(&snapshot, blocks)); ++ p[0] = 7; ++ KUNIT_EXPECT_TRUE(test, apple_sftl_account_context_pair(&snapshot, blocks)); ++ put_unaligned_le16(1, p + 2); ++ KUNIT_EXPECT_FALSE(test, apple_sftl_account_context_pair(&snapshot, blocks)); ++ ++ donor = image.records[10].data + 77 * 8; /* Block 1101. */ ++ reads = image.records[29].data + 77 * 4; ++ free = image.records[4].data + TEST_VALID_STAT_OFFSET + 16; ++ data = free + 16; ++ donor[0] = 3; ++ donor[1] = 8; ++ put_unaligned_le16(47, donor + 6); ++ put_unaligned_le16(4, footer + 12); ++ put_unaligned_le16(0xffff, footer + 14); ++ put_unaligned_le32(517, reads); ++ put_unaligned_le64(10, free); ++ put_unaligned_le64(12, data); ++ put_unaligned_le16(5, donor + 2); ++ KUNIT_EXPECT_EQ(test, apple_sftl_account_retire(&image, 1101, footer), -EOPNOTSUPP); ++ put_unaligned_le16(0, donor + 2); ++ footer[1] = 9; ++ KUNIT_EXPECT_EQ(test, apple_sftl_account_retire(&image, 1101, footer), -EOPNOTSUPP); ++ footer[1] = 8; ++ put_unaligned_le64(0, data); ++ KUNIT_EXPECT_EQ(test, apple_sftl_account_retire(&image, 1101, footer), -EBADMSG); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le32(reads), 517U); ++ KUNIT_EXPECT_EQ(test, donor[0], (u8)3); ++ put_unaligned_le64(12, data); ++ KUNIT_ASSERT_EQ(test, apple_sftl_account_retire(&image, 1101, footer), 0); ++ KUNIT_EXPECT_EQ(test, donor[0], (u8)9); ++ KUNIT_EXPECT_EQ(test, donor[1], (u8)8); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le16(donor + 6), 47); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le32(reads), 0U); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le64(free), 11ULL); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le64(data), 11ULL); ++ KUNIT_EXPECT_EQ(test, apple_sftl_account_retire(&image, 1101, footer), -EOPNOTSUPP); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le64(free), 11ULL); ++} ++ ++static struct kunit_case account_cases[] = { ++ KUNIT_CASE(account_recompute_valid_lbas), ++ KUNIT_CASE(account_invalid_layout_or_map), ++ KUNIT_CASE(account_footer_user_sequence), ++ KUNIT_CASE(account_context_and_retired_donor), ++ {} ++}; ++ ++static struct kunit_suite account_suite = { ++ .name = "apple-sftl-accounting", ++ .test_cases = account_cases, ++}; ++kunit_test_suite(account_suite); ++MODULE_LICENSE("GPL"); +diff --git a/drivers/mtd/apple-sftl-account.c b/drivers/mtd/apple-sftl-account.c +new file mode 100644 +index 000000000..56115e81c +--- /dev/null ++++ b/drivers/mtd/apple-sftl-account.c +@@ -0,0 +1,311 @@ ++// SPDX-License-Identifier: GPL-2.0-only ++/* ++ * SwissPPNFTL accounting on 13G36 64 GB media. cxtSaveSB at 0x8108e7e4 ++ * serializes the resident valid-LBA count into bytes 2..3 of each entry. ++ * Stat 9 counts all valid LBAs, including internal keys past the disk end. ++ */ ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#include "apple-sftl-account.h" ++#include "apple-sftl-reclaim.h" ++ ++#define SFTL_STATS_START 4 ++#define SFTL_SB_START 8 ++#define SFTL_USER_SEQ_START 16 ++#define SFTL_READ_COUNT_START 28 ++#define SFTL_SB_ENTRY_BYTES 8 ++#define SFTL_SB_PER_RECORD (APPLE_SFTL_CHUNK / SFTL_SB_ENTRY_BYTES) ++ ++bool apple_sftl_native_context_tag(u32 index, const u8 *meta) ++{ ++ u32 start = 0, entries = 0, stride = 0; ++ u8 subtype; ++ ++ if (index < SFTL_STATS_START) ++ subtype = 1; ++ else if (index < SFTL_SB_START) ++ subtype = 2; ++ else if (index < SFTL_USER_SEQ_START) { ++ subtype = 3; ++ stride = SFTL_SB_PER_RECORD; ++ start = (index - SFTL_SB_START) * stride; ++ } else if (index < SFTL_READ_COUNT_START) { ++ subtype = 5; ++ stride = APPLE_SFTL_CHUNK / 16; ++ start = (index - SFTL_USER_SEQ_START) * stride; ++ } else if (index < APPLE_SFTL_ACCOUNT_RECORDS) { ++ subtype = 6; ++ stride = APPLE_SFTL_CHUNK / 4; ++ start = (index - SFTL_READ_COUNT_START) * stride; ++ } else { ++ subtype = 4; ++ } ++ if (start < APPLE_SFTL_SUBBLOCKS) ++ entries = min(stride, APPLE_SFTL_SUBBLOCKS - start); ++ return meta && meta[0] == 0x1f && meta[1] == subtype && ++ get_unaligned_le16(meta + 8) == start && ++ get_unaligned_le16(meta + 10) == entries && ++ get_unaligned_le32(meta + 12) == U32_MAX; ++} ++ ++/* Native close (0x8108bd60) samples the user sequence high word once per ++ * footer run. Lengths and skipped U32_MAX values do not enter the mean. ++ */ ++void apple_sftl_summarize_user_seq(struct apple_sftl_user_seq *out, const u8 *runs, u32 count) ++{ ++ u32 i, low = U32_MAX, high = 0, samples = 0; ++ u64 sum = 0; ++ ++ for (i = 0; i < count; i++, runs += APPLE_SFTL_META) { ++ u32 seq = get_unaligned_le32(runs + 4); ++ ++ if (seq == U32_MAX) ++ continue; ++ low = min(low, seq); ++ high = max(high, seq); ++ sum += seq; ++ samples++; ++ } ++ if (!samples) { ++ low = 0; ++ samples = 1; ++ } ++ put_unaligned_le32(low, out->data); ++ put_unaligned_le32(high, out->data + 4); ++ put_unaligned_le32(div_u64(sum, samples), out->data + 8); ++ put_unaligned_le32(samples, out->data + 12); ++} ++EXPORT_SYMBOL_GPL(apple_sftl_summarize_user_seq); ++ ++static bool sftl_native_prefix(const struct apple_sftl_context_record *records, u32 count) ++{ ++ u32 i; ++ ++ if (!records || count < APPLE_SFTL_ACCOUNT_RECORDS || ++ get_unaligned_le32(records[3].data + APPLE_SFTL_CHUNK - 4) != APPLE_SFTL_SUBBLOCKS) ++ return false; ++ for (i = 0; i < APPLE_SFTL_ACCOUNT_RECORDS; i++) ++ if (!apple_sftl_native_context_tag(i, records[i].meta)) ++ return false; ++ return true; ++} ++ ++static u8 *sftl_sb_entry(struct apple_sftl_context_record *records, u32 block) ++{ ++ u32 record = SFTL_SB_START + block / SFTL_SB_PER_RECORD; ++ u32 offset = block % SFTL_SB_PER_RECORD * SFTL_SB_ENTRY_BYTES; ++ ++ return records[record].data + offset; ++} ++ ++bool apple_sftl_account_context_pair(const struct apple_sftl_snapshot *snapshot, ++ const u32 blocks[2]) ++{ ++ u32 i; ++ ++ if (!snapshot || snapshot->nr_opaque != APPLE_SFTL_ACCOUNT_RECORDS || !blocks || ++ blocks[0] == blocks[1] || !sftl_native_prefix(snapshot->opaque, snapshot->nr_opaque)) ++ return false; ++ for (i = 0; i < 2; i++) { ++ const u8 *entry; ++ ++ if (!blocks[i] || blocks[i] >= APPLE_SFTL_SUBBLOCKS) ++ return false; ++ entry = sftl_sb_entry(snapshot->opaque, blocks[i]); ++ /* Recovery assigns class 7 to context (0x1f) at 0x8108d762. ++ * The context allocator independently assigns it at 0x810937a4. ++ */ ++ if (entry[0] != 7 || get_unaligned_le16(entry + 2)) ++ return false; ++ } ++ return true; ++} ++EXPORT_SYMBOL_GPL(apple_sftl_account_context_pair); ++ ++int apple_sftl_account_user_seq(struct apple_sftl_snapshot *snapshot, ++ const struct apple_sftl_user_seq *updates) ++{ ++ u32 block, per_record = APPLE_SFTL_CHUNK / sizeof(*updates); ++ ++ if (snapshot->nr_opaque != APPLE_SFTL_ACCOUNT_RECORDS || ++ !sftl_native_prefix(snapshot->opaque, snapshot->nr_opaque)) ++ return -EBADMSG; ++ for (block = 0; block < APPLE_SFTL_SUBBLOCKS; block++) { ++ u32 record = SFTL_USER_SEQ_START + block / per_record; ++ u32 offset = block % per_record * sizeof(*updates); ++ ++ /* A native close always has a nonzero sample count. */ ++ if (get_unaligned_le32(updates[block].data + 12)) ++ memcpy(snapshot->opaque[record].data + offset, ++ updates[block].data, sizeof(*updates)); ++ } ++ return 0; ++} ++ ++static int sftl_account_layout(const struct apple_sftl_checkpoint *image) ++{ ++ u32 i; ++ ++ if (image->count <= APPLE_SFTL_ACCOUNT_RECORDS || image->count % 4 || ++ !sftl_native_prefix(image->records, image->count)) ++ return -EOPNOTSUPP; ++ for (i = APPLE_SFTL_ACCOUNT_RECORDS; i < image->count; i++) ++ if (!apple_sftl_native_context_tag(i, image->records[i].meta)) ++ return -EOPNOTSUPP; ++ return 0; ++} ++ ++static u8 *sftl_stats(struct apple_sftl_checkpoint *image) ++{ ++ u8 *payload = image->records[SFTL_STATS_START].data; ++ u32 i, offset = sizeof(u32); ++ ++ /* The measured stats payload is a word-counted TLV list. IDs 1..43 ++ * carry 64-bit counters, followed by two private variable-size blobs. ++ */ ++ if (get_unaligned_le32(payload) != 45) ++ return NULL; ++ for (i = 0; i < 45; i++) { ++ u32 id, words; ++ ++ if (offset > APPLE_SFTL_CHUNK - 8) ++ return NULL; ++ id = get_unaligned_le32(payload + offset); ++ words = get_unaligned_le32(payload + offset + 4); ++ offset += 8; ++ if (words > (APPLE_SFTL_CHUNK - offset) / 4 || ++ (i < 43 && (id != i + 1 || words != 2)) || ++ (i == 43 && (id != 255 || words != 22)) || ++ (i == 44 && (id != 254 || words != 116))) ++ return NULL; ++ offset += words * 4; ++ } ++ return payload; ++} ++ ++/* sftl_stats has checked all of the fixed-size leading counters. */ ++static u8 *sftl_stat(u8 *stats, u32 id) ++{ ++ return stats + sizeof(u32) + (id - 1) * 16 + 8; ++} ++ ++/* Apply the native post-GC state of a verified, presently unmapped donor. ++ * It is dirty/free (9) at this point. Erase success and wear are later events. ++ */ ++int apple_sftl_account_retire(struct apple_sftl_checkpoint *image, u32 block, const u8 *footer_meta) ++{ ++ u32 reads_per_record = APPLE_SFTL_CHUNK / sizeof(u32); ++ u8 *entry, *reads, *stats; ++ u64 data, free; ++ u16 chunks; ++ int ret; ++ ++ if (!image || !image->records || !footer_meta || !block || block >= APPLE_SFTL_SUBBLOCKS) ++ return -EINVAL; ++ ret = sftl_account_layout(image); ++ if (ret) ++ return ret; ++ chunks = get_unaligned_le16(footer_meta + 12); ++ if (footer_meta[0] != 0x1c || chunks < 4 || chunks > 32 || chunks % 4 || ++ footer_meta[1] >> 1 != chunks || get_unaligned_le16(footer_meta + 14) != 0xffff) ++ return -EOPNOTSUPP; ++ entry = sftl_sb_entry(image->records, block); ++ if (entry[0] != 3 || entry[1] != footer_meta[1] || get_unaligned_le16(entry + 2)) ++ return -EOPNOTSUPP; ++ stats = sftl_stats(image); ++ if (!stats) ++ return -EOPNOTSUPP; ++ free = get_unaligned_le64(sftl_stat(stats, 10)); ++ data = get_unaligned_le64(sftl_stat(stats, 11)); ++ if (free >= APPLE_SFTL_SUBBLOCKS || !data || data > APPLE_SFTL_SUBBLOCKS - free) ++ return -EBADMSG; ++ reads = image->records[SFTL_READ_COUNT_START + block / reads_per_record].data + ++ (block % reads_per_record) * sizeof(u32); ++ /* cxt's stats IDs 10/11 copy native free/data counts (0x81094be2). ++ * Final invalidation clears reads and moves one block at 0x81090ade. ++ */ ++ entry[0] = 9; ++ put_unaligned_le32(0, reads); ++ put_unaligned_le64(free + 1, sftl_stat(stats, 10)); ++ put_unaligned_le64(data - 1, sftl_stat(stats, 11)); ++ return 0; ++} ++EXPORT_SYMBOL_GPL(apple_sftl_account_retire); ++ ++static int sftl_account_runs(const struct apple_sftl_snapshot *map, u16 *counts, u32 *total) ++{ ++ u32 cursor = 0, i; ++ ++ for (i = 0; i < map->nr_runs; i++) { ++ const struct apple_sftl_extent *run = &map->runs[i]; ++ u32 length = run->length, addr = run->provider; ++ ++ if (run->logical != cursor || !length || length > APPLE_SFTL_LOGICAL_PAGES - cursor) ++ return -EBADMSG; ++ cursor += length; ++ if (addr == APPLE_SFTL_NO_ADDRESS) ++ continue; ++ if (addr >= APPLE_SFTL_SUBBLOCKS * APPLE_SFTL_STRIDE || ++ length > APPLE_SFTL_SUBBLOCKS * APPLE_SFTL_STRIDE - addr) ++ return -EBADMSG; ++ *total += length; ++ while (length) { ++ u32 block = addr / APPLE_SFTL_STRIDE; ++ u32 n = min(length, APPLE_SFTL_STRIDE - addr % APPLE_SFTL_STRIDE); ++ ++ if (counts[block] > APPLE_SFTL_STRIDE - n) ++ return -EBADMSG; ++ counts[block] += n; ++ addr += n; ++ length -= n; ++ } ++ } ++ return cursor == APPLE_SFTL_LOGICAL_PAGES ? 0 : -EBADMSG; ++} ++ ++int apple_sftl_account_map(struct apple_sftl_checkpoint *image, ++ const struct apple_sftl_snapshot *map) ++{ ++ u32 old_total = 0, total = 0, block; ++ u8 *valid; ++ u16 *counts; ++ int ret; ++ ++ if (!image || !image->records || !map || !map->runs || !map->nr_runs || ++ map->nr_runs > APPLE_SFTL_LOGICAL_PAGES) ++ return -EINVAL; ++ ret = sftl_account_layout(image); ++ if (ret) ++ return ret; ++ valid = sftl_stats(image); ++ if (!valid) ++ return -EOPNOTSUPP; ++ valid = sftl_stat(valid, 9); ++ for (block = 0; block < APPLE_SFTL_SUBBLOCKS; block++) { ++ u16 n = get_unaligned_le16(sftl_sb_entry(image->records, block) + 2); ++ ++ if (n > APPLE_SFTL_STRIDE) ++ return -EBADMSG; ++ old_total += n; ++ } ++ if (get_unaligned_le64(valid) != old_total) ++ return -EBADMSG; ++ ++ counts = kcalloc(APPLE_SFTL_SUBBLOCKS, sizeof(*counts), GFP_NOIO); ++ if (!counts) ++ return -ENOMEM; ++ ret = sftl_account_runs(map, counts, &total); ++ if (!ret) { ++ for (block = 0; block < APPLE_SFTL_SUBBLOCKS; block++) ++ put_unaligned_le16(counts[block], sftl_sb_entry(image->records, block) + 2); ++ put_unaligned_le64(total, valid); ++ } ++ kfree(counts); ++ return ret; ++} ++EXPORT_SYMBOL_GPL(apple_sftl_account_map); +diff --git a/drivers/mtd/apple-sftl-account.h b/drivers/mtd/apple-sftl-account.h +new file mode 100644 +index 000000000..864311c43 +--- /dev/null ++++ b/drivers/mtd/apple-sftl-account.h +@@ -0,0 +1,36 @@ ++/* SPDX-License-Identifier: GPL-2.0-only */ ++#ifndef _APPLE_SFTL_ACCOUNT_H ++#define _APPLE_SFTL_ACCOUNT_H ++ ++#include ++ ++#define APPLE_SFTL_ACCOUNT_RECORDS 32U ++ ++/* Native subtype 5 keeps one little-endian tuple per closed block. */ ++struct apple_sftl_user_seq { ++ u8 data[16]; ++}; ++ ++struct apple_sftl_checkpoint; ++struct apple_sftl_snapshot; ++ ++bool apple_sftl_account_context_pair(const struct apple_sftl_snapshot *snapshot, const u32 blocks[2]); ++/* Donor component of a post-GC state. The caller has validated its footer, ++ * removed its final mapping and knows the counters represent the baseline ++ * immediately before this retirement. A native save can precede stats refresh. ++ */ ++int apple_sftl_account_retire(struct apple_sftl_checkpoint *image, u32 block, ++ const u8 *footer_meta); ++bool apple_sftl_native_context_tag(u32 index, const u8 *meta); ++ ++void apple_sftl_summarize_user_seq(struct apple_sftl_user_seq *out, const u8 *runs, u32 count); ++int apple_sftl_account_user_seq(struct apple_sftl_snapshot *snapshot, ++ const struct apple_sftl_user_seq *updates); ++ ++/* Rebuild the map-derived fields of a 13G36 accounting image. A successful ++ * update does not certify the other native accounting or block ownership. ++ */ ++int apple_sftl_account_map(struct apple_sftl_checkpoint *image, ++ const struct apple_sftl_snapshot *map); ++ ++#endif +diff --git a/drivers/mtd/apple-sftl-reclaim-test.c b/drivers/mtd/apple-sftl-reclaim-test.c +index d451b5370..969af6857 100644 +--- a/drivers/mtd/apple-sftl-reclaim-test.c ++++ b/drivers/mtd/apple-sftl-reclaim-test.c +@@ -322,7 +322,7 @@ static void reclaim_gc_boundaries(struct kunit *test) + + enum copy_fault { + COPY_GOOD, COPY_BAD_KEY, COPY_FLAGGED, COPY_KIND2, COPY_ZERO_STAMP, +- COPY_BUSY, COPY_PROGRAM_ERROR, ++ COPY_FUTURE_USER_EPOCH, COPY_BUSY, COPY_PROGRAM_ERROR, + COPY_DATA_MISMATCH, COPY_OOB_MISMATCH, + }; + +@@ -341,13 +341,14 @@ static int copy_read(void *priv, u32 addr, struct apple_sftl_gc_page *page) + for (i = 0; i < 4; i++) { + u8 *meta = page->oob + i * APPLE_SFTL_META; + u32 chunk = addr % APPLE_SFTL_STRIDE + i; ++ u64 weave = m->fault == COPY_ZERO_STAMP ? 0 : 0x10050ULL + chunk; + + memset(page->data + i * APPLE_SFTL_CHUNK, chunk, APPLE_SFTL_CHUNK); +- put_unaligned_le64((m->fault == COPY_ZERO_STAMP ? 0ULL : 80ULL + chunk) << 16, meta); ++ put_unaligned_le64(weave << 16, meta); + meta[0] = m->fault == COPY_KIND2 ? 2 : 1; + meta[1] = m->fault == COPY_FLAGGED; + put_unaligned_le32(m->fault == COPY_BAD_KEY ? 99 : chunk - 1, meta + 8); +- put_unaligned_le32(0, meta + 12); ++ put_unaligned_le32(m->fault == COPY_FUTURE_USER_EPOCH ? 2 : 0, meta + 12); + } + return 0; + } +@@ -395,6 +396,9 @@ static void reclaim_copy_live_pages(struct kunit *test) + KUNIT_ASSERT_NOT_NULL(test, opaque); + KUNIT_ASSERT_NOT_NULL(test, m); + reclaim_source(&snapshot, opaque, runs, 4); ++ /* Copy historical data across a second weave epoch, including head padding. */ ++ snapshot.sequence = snapshot.journal_sequence = 0x1fffd; ++ victim.last_sequence = 0x100c7; + runs[0] = (struct apple_sftl_extent) { 0, 3, 4 * APPLE_SFTL_STRIDE + 1 }; + runs[1] = (struct apple_sftl_extent) { 3, 1, 6 * APPLE_SFTL_STRIDE }; + runs[2] = (struct apple_sftl_extent) { 4, 2, 4 * APPLE_SFTL_STRIDE + 5 }; +@@ -429,13 +433,14 @@ static void reclaim_copy_live_pages(struct kunit *test) + const u8 *meta = &m->pages[i / 4].oob[(i % 4) * APPLE_SFTL_META]; + const u8 *data = &m->pages[i / 4].data[(i % 4) * APPLE_SFTL_CHUNK]; + +- KUNIT_EXPECT_EQ(test, get_unaligned_le64(meta) >> 16, 201ULL + i); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le64(meta) >> 16, 0x1fffeULL + i); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(meta + 8), keys[i]); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le32(meta + 12), 0U); + KUNIT_EXPECT_EQ(test, data[0], (u8)(keys[i] + 1)); + KUNIT_EXPECT_EQ(test, data[APPLE_SFTL_CHUNK - 1], data[0]); + } + KUNIT_ASSERT_EQ(test, apple_sftl_gc_checkpoint(gc, &context_block, 1, &image), 0); +- KUNIT_EXPECT_EQ(test, image->first_sequence, 209ULL); ++ KUNIT_EXPECT_EQ(test, image->first_sequence, 0x20006ULL); + KUNIT_ASSERT_EQ(test, apple_sftl_gc_checkpoint_done(gc, image), 0); + memcpy(relocated, runs, sizeof(runs)); + relocated[0].provider = dest + 3; +diff --git a/drivers/mtd/apple-sftl-reclaim.c b/drivers/mtd/apple-sftl-reclaim.c +index f85a5ec7b..cf30529b3 100644 +--- a/drivers/mtd/apple-sftl-reclaim.c ++++ b/drivers/mtd/apple-sftl-reclaim.c +@@ -4,8 +4,8 @@ + * Native metadata: 13G36 IOFlashStorage-730 formatter at 0x81092408. + * Range payload: cxtNextBatch at 0x8108f07c and the recovery decoder. + * +- * Opaque accounting payloads survive byte-for-byte. Updating their native +- * accounting and committing VFL bank rotation remain separate work. ++ * Map-derived accounting is rebuilt for the measured native envelope. ++ * The platform must account for the other native state before publication. + */ + #include + #include +@@ -17,6 +17,7 @@ + #include + + #include "apple-sftl-reclaim.h" ++#include "apple-sftl-account.h" + + #define SFTL_SEQUENCE GENMASK_ULL(47, 0) + #define SFTL_PROVIDER (APPLE_SFTL_SUBBLOCKS * APPLE_SFTL_STRIDE) +@@ -28,6 +29,7 @@ struct apple_sftl_gc { + struct apple_sftl_snapshot snapshot; + struct apple_sftl_gc_copy *copies; + struct apple_sftl_checkpoint *checkpoint; ++ bool issued; + u32 nr_copies, copied, max_records, victim, destination; + u64 retire_sequence, copied_sequence, victim_sequence; + u32 padding; +@@ -85,12 +87,29 @@ static int sftl_check_context(const struct apple_sftl_snapshot *snapshot) + return -EBADMSG; + previous = order; + } ++ /* A four-chunk native base cannot be downgraded to the compact form ++ * when its geometry or accounting prefix is damaged. ++ */ ++ if (snapshot->nr_opaque >= 4 && snapshot->opaque[3].meta[1] == 1 && ++ (snapshot->nr_opaque != APPLE_SFTL_ACCOUNT_RECORDS || ++ get_unaligned_le32(snapshot->opaque[3].data + APPLE_SFTL_CHUNK - 4) != ++ APPLE_SFTL_SUBBLOCKS)) ++ return -EOPNOTSUPP; + names = get_unaligned_le32(snapshot->opaque[0].data); + if (!names || names > 16) + return -EBADMSG; + return 0; + } + ++static bool sftl_native_context(const struct apple_sftl_snapshot *snapshot) ++{ ++ const struct apple_sftl_context_record *base = snapshot->opaque; ++ ++ return snapshot->nr_opaque >= 4 && base[0].meta[1] == 1 && ++ base[1].meta[1] == 1 && base[2].meta[1] == 1 && base[3].meta[1] == 1 && ++ get_unaligned_le32(base[3].data + APPLE_SFTL_CHUNK - 4) == APPLE_SFTL_SUBBLOCKS; ++} ++ + static int sftl_checkpoint_count(const struct apple_sftl_snapshot *snapshot, + u32 max_records, u32 *count) + { +@@ -106,7 +125,12 @@ static int sftl_checkpoint_count(const struct apple_sftl_snapshot *snapshot, + ret = sftl_check_context(snapshot); + if (ret) + return ret; +- records = snapshot->nr_opaque + (snapshot->nr_runs + SFTL_PAIRS - 1) / SFTL_PAIRS; ++ records = (snapshot->nr_runs + SFTL_PAIRS - 1) / SFTL_PAIRS; ++ /* cxtNextBatch pads the measured 64 GB range stream to whole batches. */ ++ if (sftl_native_context(snapshot)) ++ records = (records + APPLE_SFTL_NATIVE_MAP_BATCH - 1) & ++ ~(APPLE_SFTL_NATIVE_MAP_BATCH - 1); ++ records += snapshot->nr_opaque; + records = (records + 3) & ~3U; + if (records > max_records) + return -ENOSPC; +@@ -205,6 +229,13 @@ int apple_sftl_checkpoint_build(const struct apple_sftl_snapshot *snapshot, + cursor += extent->length; + } + } ++ if (sftl_native_context(snapshot)) { ++ ret = apple_sftl_account_map(checkpoint, snapshot); ++ if (ret) { ++ apple_sftl_checkpoint_free(checkpoint); ++ return ret; ++ } ++ } + *out = checkpoint; + return 0; + } +@@ -447,6 +478,101 @@ static int sftl_gc_program(struct apple_sftl_gc_page *buffer, + return memcmp(page, buffer, sizeof(*page)) ? -EBADMSG : 0; + } + ++int apple_sftl_checkpoint_stage(const struct apple_sftl_checkpoint *image, ++ u32 block, u16 lanes, const struct apple_sftl_gc_io *io, ++ void *priv, struct apple_sftl_checkpoint **readback) ++{ ++ struct apple_sftl_checkpoint *copy; ++ struct apple_sftl_gc_page *pages; ++ u32 capacity = hweight16(lanes) * 512, start = block * APPLE_SFTL_STRIDE; ++ u32 i, j, order = 0; ++ bool native; ++ int ret; ++ ++ if (!readback) ++ return -EINVAL; ++ *readback = NULL; ++ if (!image || !image->records || !image->count || image->count % 4 || ++ !block || block >= APPLE_SFTL_SUBBLOCKS || image->count > capacity || ++ !image->first_sequence || image->sequence > SFTL_SEQUENCE || ++ image->first_sequence > image->sequence || ++ image->sequence - image->first_sequence != image->count - 1 || ++ !io || !io->read || !io->program) ++ return -EINVAL; ++ if (image->records[0].meta[1] != 1 || ++ image->records[image->count - 1].meta[1] != 4 || ++ !get_unaligned_le32(image->records[0].data) || ++ get_unaligned_le32(image->records[0].data) > 16 || ++ get_unaligned_le32(image->records[0].data + 4) != block) ++ return -EBADMSG; ++ native = image->records[3].meta[1] == 1; ++ if (native && (image->count <= APPLE_SFTL_ACCOUNT_RECORDS || ++ (image->count - APPLE_SFTL_ACCOUNT_RECORDS) % APPLE_SFTL_NATIVE_MAP_BATCH || ++ get_unaligned_le32(image->records[3].data + APPLE_SFTL_CHUNK - 4) != ++ APPLE_SFTL_SUBBLOCKS)) ++ return -EBADMSG; ++ for (i = 0; i < image->count; i++) { ++ const u8 *meta = image->records[i].meta; ++ u32 group = meta[1] == 4 ? 7 : meta[1]; ++ ++ if (group < order || (native && !apple_sftl_native_context_tag(i, meta))) ++ return -EBADMSG; ++ order = group; ++ if (meta[0] != 0x1f || !meta[1] || meta[1] > 6 || ++ get_unaligned_le64(meta) >> 16 != image->first_sequence + i || ++ get_unaligned_le32(meta + 12) != U32_MAX) ++ return -EBADMSG; ++ } ++ copy = kzalloc(sizeof(*copy), GFP_NOIO); ++ if (!copy) ++ return -ENOMEM; ++ copy->records = kvzalloc(array_size(image->count, sizeof(*copy->records)), GFP_NOIO); ++ pages = kvmalloc(2 * sizeof(*pages), GFP_NOIO); ++ if (!copy->records || !pages) { ++ ret = -ENOMEM; ++ goto out; ++ } ++ /* Check the whole block before the first program, including the space ++ * past the new generation where a loader might find older records. ++ */ ++ for (i = 0; i < capacity; i += 4) { ++ ret = io->read(priv, start + i, &pages[0]); ++ if (ret) ++ goto out; ++ if (memchr_inv(&pages[0], 0xff, sizeof(*pages))) { ++ ret = -EBADMSG; ++ goto out; ++ } ++ } ++ for (i = 0; i < image->count; i += 4) { ++ for (j = 0; j < 4; j++) { ++ memcpy(pages[1].data + j * APPLE_SFTL_CHUNK, ++ image->records[i + j].data, APPLE_SFTL_CHUNK); ++ memcpy(pages[1].oob + j * APPLE_SFTL_META, ++ image->records[i + j].meta, APPLE_SFTL_META); ++ } ++ ret = sftl_gc_program(&pages[0], &pages[1], io, priv, start + i); ++ if (ret) ++ goto out; ++ for (j = 0; j < 4; j++) { ++ memcpy(copy->records[i + j].data, ++ pages[0].data + j * APPLE_SFTL_CHUNK, APPLE_SFTL_CHUNK); ++ memcpy(copy->records[i + j].meta, ++ pages[0].oob + j * APPLE_SFTL_META, APPLE_SFTL_META); ++ } ++ } ++ copy->count = image->count; ++ copy->first_sequence = get_unaligned_le64(copy->records[0].meta) >> 16; ++ copy->sequence = get_unaligned_le64(copy->records[copy->count - 1].meta) >> 16; ++ *readback = copy; ++ copy = NULL; ++out: ++ kvfree(pages); ++ apple_sftl_checkpoint_free(copy); ++ return ret; ++} ++EXPORT_SYMBOL_GPL(apple_sftl_checkpoint_stage); ++ + /* No erase or map publication occurs here. Even an indeterminate program + * consumes the transaction: the caller must recover before writing again. + */ +@@ -476,6 +602,7 @@ int apple_sftl_gc_relocate(struct apple_sftl_gc *gc, + u32 from = copy->source + offset, key = copy->logical + offset; + const u8 *meta; + u64 stamp; ++ u32 user_epoch; + + if (cached != (from & ~3U)) { + cached = from & ~3U; +@@ -485,10 +612,11 @@ int apple_sftl_gc_relocate(struct apple_sftl_gc *gc, + } + meta = src->oob + (from & 3) * APPLE_SFTL_META; + stamp = get_unaligned_le64(meta) >> 16; ++ user_epoch = get_unaligned_le32(meta + 12); + /* Only rewrite ordinary unflagged data in its native class. */ + if (meta[0] != 1 || meta[1] || !stamp || + get_unaligned_le32(meta + 8) != key || +- get_unaligned_le32(meta + 12) != stamp >> 16 || ++ user_epoch > stamp >> 16 || + stamp > gc->victim_sequence) { + ret = -EBADMSG; + goto failed; +@@ -502,7 +630,7 @@ int apple_sftl_gc_relocate(struct apple_sftl_gc *gc, + sequence++; + put_unaligned_le64(sequence << 16 | 1, out); + put_unaligned_le32(key, out + 8); +- put_unaligned_le32(sequence >> 16, out + 12); ++ put_unaligned_le32(user_epoch, out + 12); + if (++slot == 4) { + ret = sftl_gc_program(src, dst, io, priv, address); + if (ret) +@@ -527,6 +655,23 @@ out: + } + EXPORT_SYMBOL_GPL(apple_sftl_gc_relocate); + ++int apple_sftl_gc_prepare_native(struct apple_sftl_gc *gc, ++ apple_sftl_prepare_accounting prepare, void *priv, ++ const u32 blocks[2], u32 capacity) ++{ ++ int ret; ++ ++ if (!gc || !prepare || !blocks || !capacity || gc->stage != APPLE_SFTL_GC_COPY || ++ gc->copied || gc->checkpoint) ++ return -EINVAL; ++ ret = prepare(priv, gc->snapshot.opaque, gc->snapshot.nr_opaque, &gc->snapshot, ++ gc->victim, gc->destination, blocks); ++ if (ret) ++ return ret; ++ return apple_sftl_checkpoint_build(&gc->snapshot, blocks, 2, ++ min(gc->max_records, capacity), &gc->checkpoint); ++} ++ + int apple_sftl_gc_checkpoint(struct apple_sftl_gc *gc, const u32 *blocks, + u32 nr_blocks, struct apple_sftl_checkpoint **out) + { +@@ -537,24 +682,39 @@ int apple_sftl_gc_checkpoint(struct apple_sftl_gc *gc, const u32 *blocks, + if (!out) + return -EINVAL; + *out = NULL; +- if (!gc || gc->stage != APPLE_SFTL_GC_CHECKPOINT || gc->checkpoint) ++ if (!gc || gc->stage != APPLE_SFTL_GC_CHECKPOINT || gc->issued) + return -EINVAL; + if (!blocks || !nr_blocks || nr_blocks > 16) + return -EINVAL; + for (i = 0; i < nr_blocks; i++) + if (blocks[i] == gc->victim || blocks[i] == gc->destination) + return -EINVAL; +- ret = apple_sftl_checkpoint_build(&gc->snapshot, blocks, nr_blocks, +- gc->max_records, &gc->checkpoint); +- if (ret) +- return ret; +- ret = apple_sftl_checkpoint_build(&gc->snapshot, blocks, nr_blocks, +- gc->max_records, &image); +- if (ret) { +- apple_sftl_checkpoint_free(gc->checkpoint); +- gc->checkpoint = NULL; +- return ret; ++ if (!gc->checkpoint) { ++ ret = apple_sftl_checkpoint_build(&gc->snapshot, blocks, nr_blocks, ++ gc->max_records, &gc->checkpoint); ++ if (ret) ++ return ret; ++ } else { ++ const u8 *base = gc->checkpoint->records[0].data; ++ ++ if (get_unaligned_le32(base) != nr_blocks) ++ return -EINVAL; ++ for (i = 0; i < nr_blocks; i++) ++ if (get_unaligned_le32(base + 4 + 4 * i) != blocks[i]) ++ return -EINVAL; ++ } ++ image = kmalloc(sizeof(*image), GFP_NOIO); ++ if (!image) ++ return -ENOMEM; ++ *image = *gc->checkpoint; ++ image->records = kvmalloc(array_size(image->count, sizeof(*image->records)), GFP_NOIO); ++ if (!image->records) { ++ kfree(image); ++ return -ENOMEM; + } ++ memcpy(image->records, gc->checkpoint->records, ++ array_size(image->count, sizeof(*image->records))); ++ gc->issued = true; + *out = image; + return 0; + } +@@ -565,7 +725,7 @@ int apple_sftl_gc_checkpoint_done(struct apple_sftl_gc *gc, + { + const struct apple_sftl_checkpoint *expected; + +- if (!gc || gc->stage != APPLE_SFTL_GC_CHECKPOINT || !gc->checkpoint || !readback) ++ if (!gc || gc->stage != APPLE_SFTL_GC_CHECKPOINT || !gc->issued || !readback) + return -EINVAL; + expected = gc->checkpoint; + if (!readback->records || readback->count != expected->count || +@@ -654,7 +814,7 @@ int apple_sftl_gc_recovered(struct apple_sftl_gc *gc, + { + int ret; + +- if (!gc || gc->stage != APPLE_SFTL_GC_RECOVERY) ++ if (!gc || (gc->stage != APPLE_SFTL_GC_RECOVERY && gc->stage != APPLE_SFTL_GC_VERIFIED)) + return -EINVAL; + ret = sftl_check_map(recovered); + if (ret) +diff --git a/drivers/mtd/apple-sftl-reclaim.h b/drivers/mtd/apple-sftl-reclaim.h +index 6b6780c8b..b74d53bcd 100644 +--- a/drivers/mtd/apple-sftl-reclaim.h ++++ b/drivers/mtd/apple-sftl-reclaim.h +@@ -6,6 +6,7 @@ + + #define APPLE_SFTL_CHUNK 4096U + #define APPLE_SFTL_META 16U ++#define APPLE_SFTL_NATIVE_MAP_BATCH 64U + #define APPLE_SFTL_STRIDE 8192U + #define APPLE_SFTL_SUBBLOCKS 2086U + #define APPLE_SFTL_LOGICAL_PAGES 15625016U +@@ -86,6 +87,26 @@ struct apple_sftl_gc_io { + int (*program)(void *priv, u32 address, const struct apple_sftl_gc_page *page); + }; + ++/* Write a prepared context to an exclusively owned, entirely blank block. ++ * All addresses and lengths are in 4 KiB units. A failure may leave a prefix. ++ * The previous generation remains intact throughout this operation. ++ */ ++int apple_sftl_checkpoint_stage(const struct apple_sftl_checkpoint *image, ++ u32 block, u16 lanes, const struct apple_sftl_gc_io *io, ++ void *priv, struct apple_sftl_checkpoint **readback); ++ ++/* Platform policy must account for the native roles, streams and counters of ++ * the transition, or refuse it before any live data is moved. The two ++ * context blocks are in new-active, old-active order. ++ */ ++typedef int (*apple_sftl_prepare_accounting)(void *priv, ++ struct apple_sftl_context_record *opaque, u32 nr_opaque, ++ const struct apple_sftl_snapshot *map, u32 victim, u32 landing, ++ const u32 blocks[2]); ++int apple_sftl_gc_prepare_native(struct apple_sftl_gc *gc, ++ apple_sftl_prepare_accounting prepare, void *priv, ++ const u32 blocks[2], u32 capacity); ++ + /* Planning consumes supplied bytes. Relocation performs verified page I/O. */ + int apple_sftl_gc_plan(const struct apple_sftl_snapshot *snapshot, + const struct apple_sftl_gc_block *victim, +diff --git a/drivers/mtd/apple-sftl-test-fixture.h b/drivers/mtd/apple-sftl-test-fixture.h +new file mode 100644 +index 000000000..16d65b6a2 +--- /dev/null ++++ b/drivers/mtd/apple-sftl-test-fixture.h +@@ -0,0 +1,50 @@ ++/* SPDX-License-Identifier: GPL-2.0-only */ ++#ifndef _APPLE_SFTL_TEST_FIXTURE_H ++#define _APPLE_SFTL_TEST_FIXTURE_H ++ ++#include ++#include ++#include "apple-sftl-reclaim.h" ++ ++/* Populate the measured layout in 32 zeroed synthetic records. Callers place ++ * these in their own generation and supply its context block addresses. ++ */ ++static inline void apple_sftl_test_native_prefix(struct apple_sftl_context_record *records) ++{ ++ static const struct { ++ u8 subtype, chunks; ++ u16 entries; ++ } groups[] = { { 1, 4, 0 }, { 2, 4, 0 }, { 3, 8, 512 }, ++ { 5, 12, 256 }, { 6, 4, 1024 } }; ++ u32 i, pos, j, cursor = 0; ++ u8 *stats; ++ ++ for (i = 0; i < ARRAY_SIZE(groups); i++) { ++ for (j = 0; j < groups[i].chunks; j++) { ++ u8 *meta = records[cursor++].meta; ++ u32 start = j * groups[i].entries; ++ u16 n = start >= APPLE_SFTL_SUBBLOCKS ? 0 : ++ min_t(u32, groups[i].entries, APPLE_SFTL_SUBBLOCKS - start); ++ ++ meta[0] = 0x1f; ++ meta[1] = groups[i].subtype; ++ put_unaligned_le16(start, meta + 8); ++ put_unaligned_le16(n, meta + 10); ++ put_unaligned_le32(U32_MAX, meta + 12); ++ } ++ } ++ put_unaligned_le32(1, records[0].data); ++ put_unaligned_le32(1, records[0].data + 4); ++ put_unaligned_le32(APPLE_SFTL_SUBBLOCKS, records[3].data + APPLE_SFTL_CHUNK - 4); ++ stats = records[4].data; ++ put_unaligned_le32(45, stats); ++ pos = 4; ++ for (i = 0; i < 45; i++) { ++ u32 words = i < 43 ? 2 : (i == 43 ? 22 : 116); ++ ++ put_unaligned_le32(i < 43 ? i + 1 : (i == 43 ? 255 : 254), stats + pos); ++ put_unaligned_le32(words, stats + pos + 4); ++ pos += 8 + words * 4; ++ } ++} ++#endif diff --git a/kernel/patches/armv7/mtd/0013-mtd-apple-sftl-Recover-interrupted-saves-and-verify-.patch b/kernel/patches/armv7/mtd/0013-mtd-apple-sftl-Recover-interrupted-saves-and-verify-.patch new file mode 100644 index 00000000..ccd45d7a --- /dev/null +++ b/kernel/patches/armv7/mtd/0013-mtd-apple-sftl-Recover-interrupted-saves-and-verify-.patch @@ -0,0 +1,3183 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Shayne <8741557+shaynelegault@users.noreply.github.com> +Date: Wed, 18 Nov 2026 22:44:09 -0600 +Subject: [PATCH] mtd: apple-sftl: Recover interrupted saves and verify native + reclamation + +Select complete native checkpoints and require durable allocation roles +before allocating or retiring blocks. Recover interrupted context saves +and partial lane retirement read-only when the newer mapping is provable. +Reconcile replicated VFL banks and revalidate lane ownership on rotation. + +Stage the alternate checkpoint from the normal writer, reload the whole +map on both sides of context retirement, and fail on ambiguous I/O. +Exercise interrupted operations on synthetic media. Keep physical P105 +erase gated on native accounting and device qualification. + +Assisted-by: LLM +--- + .../driver-api/mtd/apple-sftl-reclamation.rst | 101 +- + drivers/mtd/Kconfig | 9 +- + drivers/mtd/apple-sftl-account.c | 55 +- + drivers/mtd/apple-sftl-account.h | 3 + + drivers/mtd/apple-sftl-block.c | 6 +- + drivers/mtd/apple-sftl-core.c | 1159 +++++++++++++---- + drivers/mtd/apple-sftl-reclaim.c | 36 +- + drivers/mtd/apple-sftl-test.c | 926 ++++++++++++- + drivers/mtd/apple-sftl-vfl.c | 20 +- + drivers/mtd/apple-sftl-vfl.h | 8 + + drivers/mtd/apple-sftl.h | 18 +- + 11 files changed, 2056 insertions(+), 285 deletions(-) + +diff --git a/Documentation/driver-api/mtd/apple-sftl-reclamation.rst b/Documentation/driver-api/mtd/apple-sftl-reclamation.rst +index b164bdce0..38abdab54 100644 +--- a/Documentation/driver-api/mtd/apple-sftl-reclamation.rst ++++ b/Documentation/driver-api/mtd/apple-sftl-reclamation.rst +@@ -9,9 +9,9 @@ block. Reclamation must preserve the recovered map for the whole device. + + The block writer allocates from verified blank blocks. An optional, verified + ``erase_block`` provider operation lets the allocator also reclaim obsolete +-blocks. A checkpoint backend with atomic installation and retirement of the +-previous fallback additionally permits relocation of live data. P105 H2FMI +-provides neither facility on the current hardware profile. When verified ++blocks. With a complete native accounting policy the allocator can also stage ++a checkpoint in the alternate context block and relocate live data. ++P105 H2FMI does not enable physical erase or supply this policy. When verified + blank blocks run out on that profile, a write returns ``-ENOSPC``. + + Reclaiming an obsolete block +@@ -25,7 +25,10 @@ the saved map and the active map, including the logical keys outside the + exported disk. The allocator excludes VFL and FTL context blocks, the + bootstrap range and blocks with unknown active-lane flags. It loads the + medium again and compares the whole recovered map and its sequence numbers +-with the active instance. ++with the active instance. On the measured native layout the saved accounting ++must already mark an obsolete donor dirty-free with zero valid pages. A ++closed data block that has lost its last reference is still ineligible for ++immediate erase while its saved role is closed data. + + The allocator reads the native footer and checks its runs against the + logical key and sequence in every data record. Its highest declared +@@ -51,16 +54,45 @@ a memory budget. Changed media stops further reads through that instance. + records. The metadata follows the 13G36 IOFlashStorage-730 formatter at + ``0x81092408``: type 0x1f, subtype, a 48-bit sequence, an array index/count, + and a trailing 0xffffffff. The base record's decoded context-block list is +-updated. Other payload bytes survive unchanged. Mapping records use subtype +-4 with the native cursor/range encoding. Padding brings the image to full +-16 KiB pages and consumes sequence values through subtype-4 padding records. +-The resulting bytes round-trip through the current recovery decoder. +- +-Opaque statistics, superblock state, user-sequence and read-count payloads +-still describe the source checkpoint. Preserving them prevents accidental +-data loss during format work; a native writer must update the fields whose +-meaning depends on relocation. These images have not been qualified for +-native checkpoint installation. ++updated. Mapping records use subtype 4 with the native cursor/range encoding. ++On the measured native format, the encoder pads the range stream to a ++multiple of 64 records with subtype-4 padding. Each padding record ++consumes a sequence value. Full logical coverage alone cannot identify the ++last durable page of a native save. ++ ++The encoder recomputes the valid logical-page count of every superblock and ++the associated global statistic (ID 9) from the whole planned map. Closing ++a block or replaying a closed footer also updates its subtype-5 user ++sequence statistics. Each footer run supplies one sample. ++ ++For a qualified closed donor, native finalization changes its class from ++3 to 9 and clears its read count. A subsequent statistics refresh transfers ++one block from the data count to the free count. The native checkpoint ++writer can serialize the previous cached counts before that refresh. A ++policy must establish the point of serialization as well as the allocation, ++stream and wear transitions before allowing the first relocation write. ++The native formatter also queries its provider for two statistics payloads ++(IDs 255 and 254) on each save. ++ ++Saving and recovering a native context ++-------------------------------------- ++ ++The measured two-block format names its active and alternate contexts in the ++base record. The writer verifies exclusive ownership of both and checks the ++old generation's complete extent and the alternate's erased contents. It ++programs the new context in 16 KiB units, reads back each page's data and ++metadata, then cold-loads the medium with both generations visible. The ++expected checkpoint sequence, target location, mapping and lane ownership ++must agree. It erases the old context one lane at a time, verifies it blank ++and cold-loads again before erasing the data donor. ++ ++Recovery selects a complete generation and replays later writes. A partially ++written native map batch can expose sequence values without revealing how ++many further values the interrupted operation reserved. Recovery from that ++state is read-only. When an interrupted retirement leaves older context ++fragments on some lanes, the complete newer generation must own the old ++site and the remaining records must be provably older. Ambiguous fragments, ++conflicting metadata and unaccounted journal gaps prevent recovery. + + Relocating live data + -------------------- +@@ -74,22 +106,22 @@ floor above the last replayed record is excluded under the current recovery + contract. Copies retain the logical keys while moving their provider + addresses. Destination space leaves eight pages for a future native summary. + +-With both callbacks installed, allocation can use the last verified blank ++With checkpoint and erase support, allocation can use the last verified blank + block to compact a closed source still referenced by the saved checkpoint. + The allocator checks the source footer and data records before writing, + accepts unflagged type 1 data for relocation, and budgets enough destination + space and sequence numbers for the incoming write. The public KUnit tests +-exercise this through the normal write path with a synthetic checkpoint +-backend. ++exercise the normal write path with native-format context pages, synthetic ++media and a test accounting policy. + + * ``apple_sftl_gc_relocate()`` reads each planned source key and checks its +- metadata. It assembles four 4 KiB records per program unit and repeats the +- first key when padding is needed. It checks that each destination page is +- blank, programs it, then compares all data and OOB bytes with readback. +-* The checkpoint backend reserves context blocks before copying. After the +- copies complete it installs the new checkpoint, retires the previous +- fallback, and returns independent readback. Every metadata and payload +- byte must match the submitted image. ++ metadata. A copy gets a new physical write sequence and keeps the original ++ user sequence's high 32 bits from its data tag. It assembles four 4 KiB ++ records per program unit and repeats the first key when padding is needed. ++ It checks that each destination page is blank, programs it, then compares ++ all data and OOB bytes with readback. ++* The writer reserves context blocks before copying. Each installed metadata ++ and payload byte must match the submitted image. + * A fresh recovery snapshot must contain the planned whole-device mapping + and the new checkpoint sequence. Equivalent coalescing is accepted. The + allocator checks ownership and lane assignments again before erasing the +@@ -105,10 +137,10 @@ A copy I/O failure or mismatched receipt moves the transaction to its failed + state. A victim with live data or required replay history must survive + until a new checkpoint is durably installed and recovered. VERIFIED records + successful checks against the supplied checkpoint and recovery receipts. +-Native accounting and VFL rotation still require qualification. ++Native accounting and physical mutation still require qualification. + +-Outstanding native protocol work +-------------------------------- ++Native VFL bank rotation ++------------------------ + + VFL recovery uses two context groups with four copies per group on this + profile. The native rotation routine at ``0x810a1b68`` checks references into +@@ -117,8 +149,19 @@ members in SLC view and writes an initial ff/ff checkpoint retaining references + to the old lane tables. It then copies those tables, updates the references + and writes a final ff/ff checkpoint. The old source bank survives this path. + The initial write at 0x810a1e5a and final write at 0x810a1e34 are separate +-persisted steps. Torn-copy selection, retirement and replacement paths still +-require tracing before a runtime writer can reproduce them. ++persisted steps. The healthy-path executor reads and verifies all four copies of ++each source page, incorporates table updates after the last complete ++checkpoint, verifies target erasure, and reads back the initial checkpoint, ++all 16 tables and the final checkpoint. It uses separately qualified SLC ++erase/program callbacks. Physical P105 supplies neither callback. VFL bank ++rotation is independent of alternating the normal-view SwissPPNFTL context ++blocks. ++ ++A fallback to older VFL replicas requires a complete agreeing quartet, ++validated references and unchanged addressable ownership. Recovery reports ++ambiguous media or unsupported FTL geometries and restricts degraded media ++to reads. Scrub lists, bad-member replacement and physical aliasing require ++additional protocol and hardware evidence. + + Offline execution of the pinned native iBEC erase queue establishes queued + 67/D7 commands, final-row 6A/D7, 79 polling and 77/7D completion. The native single-block +diff --git a/drivers/mtd/Kconfig b/drivers/mtd/Kconfig +index e95303396..d1bba09ee 100644 +--- a/drivers/mtd/Kconfig ++++ b/drivers/mtd/Kconfig +@@ -213,8 +213,9 @@ config MTD_APPLE_SFTL + Recover the logical block medium stored in Apple VFL/SwissPPNFTL + metadata on the measured 64 GB Hynix PPN layout. The H2FMI driver + supplies canonical page reads through its aggregate PPN provider. +- Unsupported checkpoints, missing journal sequences and refresh +- requirements stop recovery before a disk is registered. ++ Recovery can expose a complete older generation read-only after an ++ interrupted native save. Unsupported geometry, conflicting replicas ++ and unaccounted journal gaps stop recovery. + + Writes are appended to verified blank blocks. A provider with a + qualified erase operation can also recycle verified retired blocks. +@@ -230,8 +231,8 @@ config MTD_APPLE_SFTL_KUNIT_TEST + native block summaries, open-block journal replay, checkpoint encoding + and verified reclamation during normal writes. Exercise failed erase, + readback and recovery using synthetic flash. +- Also test complete programs, interrupted summaries, allocation +- limits and fresh-loader replay. ++ Exercise native accounting, checkpoint page cuts, interrupted lane ++ retirement, both replicated VFL banks and fresh-loader replay. + All test media and provider operations are synthetic. + + source "drivers/mtd/chips/Kconfig" +diff --git a/drivers/mtd/apple-sftl-account.c b/drivers/mtd/apple-sftl-account.c +index 56115e81c..b6153bfd4 100644 +--- a/drivers/mtd/apple-sftl-account.c ++++ b/drivers/mtd/apple-sftl-account.c +@@ -103,20 +103,19 @@ static u8 *sftl_sb_entry(struct apple_sftl_context_record *records, u32 block) + return records[record].data + offset; + } + +-bool apple_sftl_account_context_pair(const struct apple_sftl_snapshot *snapshot, +- const u32 blocks[2]) ++static bool sftl_account_context_pair(struct apple_sftl_context_record *records, u32 count, ++ const u32 blocks[2]) + { + u32 i; + +- if (!snapshot || snapshot->nr_opaque != APPLE_SFTL_ACCOUNT_RECORDS || !blocks || +- blocks[0] == blocks[1] || !sftl_native_prefix(snapshot->opaque, snapshot->nr_opaque)) ++ if (!blocks || blocks[0] == blocks[1] || !sftl_native_prefix(records, count)) + return false; + for (i = 0; i < 2; i++) { + const u8 *entry; + + if (!blocks[i] || blocks[i] >= APPLE_SFTL_SUBBLOCKS) + return false; +- entry = sftl_sb_entry(snapshot->opaque, blocks[i]); ++ entry = sftl_sb_entry(records, blocks[i]); + /* Recovery assigns class 7 to context (0x1f) at 0x8108d762. + * The context allocator independently assigns it at 0x810937a4. + */ +@@ -125,8 +124,54 @@ bool apple_sftl_account_context_pair(const struct apple_sftl_snapshot *snapshot, + } + return true; + } ++ ++bool apple_sftl_account_context_pair(const struct apple_sftl_snapshot *snapshot, ++ const u32 blocks[2]) ++{ ++ return snapshot && snapshot->nr_opaque == APPLE_SFTL_ACCOUNT_RECORDS && ++ sftl_account_context_pair(snapshot->opaque, snapshot->nr_opaque, blocks); ++} + EXPORT_SYMBOL_GPL(apple_sftl_account_context_pair); + ++/* The next generation is saved after verified ordinary copies, before source ++ * erase and before the caller's next write. Source footer reads have already ++ * established its width. This writer emits the ordinary (stream zero) tags. ++ */ ++bool apple_sftl_account_gc_image(const struct apple_sftl_snapshot *before, ++ const struct apple_sftl_checkpoint *image, const u32 blocks[2], ++ u32 donor, u32 landing, u32 live, u16 footer_chunks) ++{ ++ u32 block, seen = 0, reads_per_record = APPLE_SFTL_CHUNK / sizeof(u32); ++ const u8 *old_source, *source, *destination, *reads; ++ ++ if (image->count <= APPLE_SFTL_ACCOUNT_RECORDS || ++ !sftl_account_context_pair(image->records, image->count, blocks) || ++ !donor || donor >= APPLE_SFTL_SUBBLOCKS || !landing || landing >= APPLE_SFTL_SUBBLOCKS || ++ !live || footer_chunks < 4 || footer_chunks > 32 || footer_chunks % 4) ++ return false; ++ old_source = sftl_sb_entry(before->opaque, donor); ++ source = sftl_sb_entry(image->records, donor); ++ destination = sftl_sb_entry(image->records, landing); ++ reads = image->records[SFTL_READ_COUNT_START + donor / reads_per_record].data + ++ (donor % reads_per_record) * sizeof(u32); ++ if (old_source[0] != 3 || source[0] != 9 || source[1] != old_source[1] || ++ source[1] >> 1 != footer_chunks || ++ get_unaligned_le16(source + 6) != get_unaligned_le16(old_source + 6) || ++ get_unaligned_le16(source + 2) || get_unaligned_le32(reads) || ++ destination[0] != 4 || destination[1] || get_unaligned_le16(destination + 2) != live) ++ return false; ++ for (block = 0; block < APPLE_SFTL_SUBBLOCKS; block++) { ++ const u8 *entry = sftl_sb_entry(image->records, block); ++ ++ if (entry[0] != 4) ++ continue; ++ if (entry[1] > 1 || (seen & (1U << entry[1]))) ++ return false; ++ seen |= 1U << entry[1]; ++ } ++ return true; ++} ++ + int apple_sftl_account_user_seq(struct apple_sftl_snapshot *snapshot, + const struct apple_sftl_user_seq *updates) + { +diff --git a/drivers/mtd/apple-sftl-account.h b/drivers/mtd/apple-sftl-account.h +index 864311c43..a5045c63f 100644 +--- a/drivers/mtd/apple-sftl-account.h ++++ b/drivers/mtd/apple-sftl-account.h +@@ -15,6 +15,9 @@ struct apple_sftl_checkpoint; + struct apple_sftl_snapshot; + + bool apple_sftl_account_context_pair(const struct apple_sftl_snapshot *snapshot, const u32 blocks[2]); ++bool apple_sftl_account_gc_image(const struct apple_sftl_snapshot *before, ++ const struct apple_sftl_checkpoint *image, const u32 blocks[2], ++ u32 donor, u32 landing, u32 live, u16 footer_chunks); + /* Donor component of a post-GC state. The caller has validated its footer, + * removed its final mapping and knows the counters represent the baseline + * immediately before this retirement. A native save can precede stats refresh. +diff --git a/drivers/mtd/apple-sftl-block.c b/drivers/mtd/apple-sftl-block.c +index f1998b324..31f1dfa14 100644 +--- a/drivers/mtd/apple-sftl-block.c ++++ b/drivers/mtd/apple-sftl-block.c +@@ -206,9 +206,11 @@ static int sftl_disk_probe(struct platform_device *pdev) + goto free_ftl; + } + /* Appending continues the journal; it must end where the map does. */ +- sftl->writable = provider->ops->program_page && ++ sftl->writable = provider->ops->program_page && !info.read_only && + info.sequence == info.journal_sequence; +- if (provider->ops->program_page && !sftl->writable) ++ if (info.read_only) ++ dev_warn(&pdev->dev, "incomplete metadata publication, read-only\n"); ++ else if (provider->ops->program_page && !sftl->writable) + dev_warn(&pdev->dev, "journal ends before the allocation floor, read-only\n"); + if (sftl->writable) + limits.features |= BLK_FEAT_FUA; +diff --git a/drivers/mtd/apple-sftl-core.c b/drivers/mtd/apple-sftl-core.c +index 3c28e7b27..723837295 100644 +--- a/drivers/mtd/apple-sftl-core.c ++++ b/drivers/mtd/apple-sftl-core.c +@@ -21,7 +21,9 @@ + #include + + #include "apple-sftl.h" ++#include "apple-sftl-vfl.h" + #include "apple-sftl-reclaim.h" ++#include "apple-sftl-account.h" + + #define SFTL_LPAGES 15625016U + #define SFTL_DISK_PAGES 15625000U +@@ -36,6 +38,14 @@ + #define SFTL_META 16U + #define SFTL_CHUNK 4096U + #define SFTL_TRIM 0xffff0001U ++/* cxtSaveSB serializes the role in byte zero of each 8-byte block entry. */ ++enum sftl_block_role { ++ SFTL_CLEAN_FREE = 1, ++ SFTL_CLOSED_DATA = 3, ++ SFTL_OPEN_DATA = 4, ++ SFTL_DIRTY_FREE = 9, ++}; ++ + #define SFTL_FOOTER_CHUNKS 32U + #define SFTL_CLAIM_TIMEOUT (500U * HZ) + #define SFTL_FOOTER_BYTES (SFTL_FOOTER_CHUNKS * SFTL_CHUNK) +@@ -57,15 +67,14 @@ struct sftl_records { + u32 count, capacity; + }; + +-struct sftl_member { +- u16 block; +- u8 lane; +-}; +- + struct apple_sftl { + struct apple_ppn *ppn; ++ struct apple_sftl_vfl_source vfl_source; ++ bool vfl_degraded, context_degraded, native_context; ++ u8 roles[SFTL_SUBBLOCKS], block_flags[SFTL_SUBBLOCKS]; + struct rb_root runs; + struct sftl_records context; ++ struct apple_sftl_user_seq *closed_user_seq; + u32 nr_runs; + u16 lanes[SFTL_BLOCKS]; + DECLARE_BITMAP(reserved, SFTL_BLOCKS); +@@ -76,6 +85,7 @@ struct apple_sftl { + u64 checkpoint_start; + u64 checkpoint_sequence, sequence, journal_sequence; + DECLARE_BITMAP(candidates, SFTL_BLOCKS); ++ DECLARE_BITMAP(fallback_occupied, SFTL_SUBBLOCKS); + DECLARE_BITMAP(used, SFTL_BLOCKS); + u32 next_candidate, append_block, append_position, append_capacity, spare; + const struct apple_sftl_checkpoint_ops *checkpoint_ops; +@@ -357,125 +367,333 @@ static int sftl_record_compare(const void *a, const void *b) + return left < right ? -1 : left > right; + } + +-static int sftl_vfl(struct apple_sftl *sftl) ++#define SFTL_VFL_PAGES 64 ++#define SFTL_VFL_COPIES 4 ++#define SFTL_VFL_HEADER 0xff ++ ++struct sftl_vfl_slot { ++ u32 age; ++ u8 tag, copies; ++}; ++ ++struct sftl_vfl_scan { ++ struct sftl_vfl_slot slots[2][SFTL_VFL_PAGES]; ++ u8 sample[APPLE_PPN_PAGE_SIZE + APPLE_PPN_OOB_SIZE]; ++ u8 reference_data[APPLE_PPN_PAGE_SIZE]; ++}; ++ ++static int sftl_vfl_members(struct apple_sftl *sftl) + { +- struct sftl_member members[8]; +- u8 list[32], best_oob[64], best_members = 0; +- u8 *best, *table; +- u32 best_age = U32_MAX; +- unsigned int member, page, latest_page = 0, lane, block; ++ struct apple_sftl_vfl_member *members = sftl->vfl_source.members; ++ unsigned int lane, i, j; ++ u8 status; + int ret; + +- best = kmalloc(APPLE_PPN_PAGE_SIZE, GFP_KERNEL); +- table = kmalloc(APPLE_PPN_PAGE_SIZE + APPLE_PPN_OOB_SIZE, GFP_KERNEL); +- if (!best || !table) { +- ret = -ENOMEM; +- goto out; ++ /* An interrupted erase can remove the usual lane-zero seed. Search only ++ * page zero in the measured bootstrap block for a surviving member list. ++ */ ++ for (lane = 0; lane < SFTL_LANES; lane++) { ++ ret = sftl->ppn->ops->read_page(sftl->ppn->priv, lane & 1, lane >> 1, ++ 2087, 0, true, sftl->page, sftl->oob, &status); ++ if (ret) ++ return ret; ++ /* Some nonmembers report 45 at the bootstrap coordinate. Once ++ * discovered, every actual member is reread with strict status. ++ */ ++ if (status == 0x45) ++ continue; ++ if (status != 0x40 && status != 0x49) ++ return -EIO; ++ if (status == 0x40 && get_unaligned_le32(sftl->oob) == 0x00ffff20) ++ break; ++ if (!sftl_erased(sftl->page, APPLE_PPN_PAGE_SIZE) || ++ !sftl_erased(sftl->oob, sizeof(sftl->oob))) ++ return -EBADMSG; + } +- ret = sftl_read_page(sftl, 0, 0, 2087, 0, true, sftl->page, sftl->oob); +- if (ret) +- goto out; +- if (get_unaligned_le32(sftl->oob) != 0x00ffff20) { +- ret = -EBADMSG; +- goto out; ++ if (lane == SFTL_LANES) ++ return -EBADMSG; ++ for (i = 0; i < 8; i++) { ++ const u8 *row = sftl->page + 0x100 + 4 * i; ++ ++ members[i].block = get_unaligned_le16(row); ++ members[i].lane = row[2]; ++ if (members[i].block < 2056 || members[i].block >= SFTL_BLOCKS || ++ members[i].lane >= SFTL_LANES || row[3]) ++ return -EBADMSG; ++ for (j = 0; j < i; j++) ++ if (members[j].block == members[i].block && members[j].lane == members[i].lane) ++ return -EBADMSG; ++ __set_bit(members[i].block, sftl->vfl_reserved); + } +- memcpy(list, sftl->page + 0x100, sizeof(list)); +- for (member = 0; member < 8; member++) { +- unsigned int other; ++ if (lane) { ++ for (i = 0; i < 8; i++) ++ if (members[i].lane == lane && members[i].block == 2087) ++ return 0; ++ return -EBADMSG; ++ } ++ return 0; ++} + +- members[member].block = get_unaligned_le16(list + member * 4); +- members[member].lane = list[member * 4 + 2]; +- if (members[member].block < 2056 || members[member].block >= SFTL_BLOCKS || +- members[member].lane >= SFTL_LANES || list[member * 4 + 3]) { +- ret = -EBADMSG; +- goto out; ++static int sftl_vfl_read(struct apple_sftl *sftl, unsigned int group, unsigned int copy, ++ unsigned int page) ++{ ++ const struct apple_sftl_vfl_member *member = &sftl->vfl_source.members[group * 4 + copy]; ++ ++ return sftl_read_page(sftl, member->lane & 1, member->lane >> 1, member->block, ++ page, true, sftl->page, sftl->oob); ++} ++ ++static int sftl_vfl_read_slot(struct apple_sftl *sftl, struct sftl_vfl_scan *scan, ++ unsigned int group, unsigned int page, struct sftl_vfl_slot *slot) ++{ ++ const struct apple_sftl_vfl_source *source = &sftl->vfl_source; ++ unsigned int copy, i; ++ int ret; ++ ++ for (copy = 0; copy < SFTL_VFL_COPIES; copy++) { ++ ret = sftl_vfl_read(sftl, group, copy, page); ++ if (ret) ++ return ret; ++ if (sftl_erased(sftl->oob, sizeof(sftl->oob))) { ++ if (!sftl_erased(sftl->page, APPLE_PPN_PAGE_SIZE)) ++ return -EBADMSG; ++ continue; + } +- for (other = 0; other < member; other++) +- if (members[other].block == members[member].block && +- members[other].lane == members[member].lane) { +- ret = -EBADMSG; +- goto out; ++ if (slot->copies) { ++ if (memcmp(scan->sample, sftl->page, APPLE_PPN_PAGE_SIZE) || ++ memcmp(scan->sample + APPLE_PPN_PAGE_SIZE, sftl->oob, ++ sizeof(sftl->oob))) ++ return -EBADMSG; ++ } else { ++ if (sftl->oob[0] != 0x20 || sftl->oob[3]) ++ return -EBADMSG; ++ slot->tag = sftl->oob[1]; ++ slot->age = get_unaligned_le32(sftl->oob + 4); ++ if (slot->age < BIT(31)) ++ return -EOPNOTSUPP; ++ if (slot->tag == SFTL_VFL_HEADER) { ++ if (sftl->oob[2] != 0xff) ++ return -EBADMSG; ++ for (i = 0; i < 8; i++) { ++ const u8 *row = sftl->page + 0x100 + 4 * i; ++ const struct apple_sftl_vfl_member *m = &source->members[i]; ++ ++ if (get_unaligned_le16(row) != m->block || ++ row[2] != m->lane || row[3]) ++ return -EBADMSG; ++ } ++ } else if (!page || slot->tag >= SFTL_LANES || sftl->oob[2]) { ++ return -EBADMSG; + } +- __set_bit(members[member].block, sftl->vfl_reserved); ++ memcpy(scan->sample, sftl->page, APPLE_PPN_PAGE_SIZE); ++ memcpy(scan->sample + APPLE_PPN_PAGE_SIZE, sftl->oob, ++ sizeof(sftl->oob)); ++ } ++ slot->copies |= BIT(copy); + } +- for (member = 0; member < 8; member++) { +- for (page = 0; page < 64; page++) { +- u32 age; ++ return 0; ++} ++ ++/* Verify the replica set again when a checkpoint or table is consumed. ++ * sample then contains the complete first programmed page and its tag. ++ */ ++static int sftl_vfl_reread(struct apple_sftl *sftl, struct sftl_vfl_scan *scan, ++ unsigned int group, unsigned int page) ++{ ++ const struct sftl_vfl_slot *expected = &scan->slots[group][page]; ++ struct sftl_vfl_slot actual = {}; ++ int ret = sftl_vfl_read_slot(sftl, scan, group, page, &actual); ++ ++ if (ret) ++ return ret; ++ if (!actual.copies || actual.copies != expected->copies || ++ actual.age != expected->age || actual.tag != expected->tag) ++ return -EBADMSG; ++ return 0; ++} ++ ++static int sftl_vfl_index(struct apple_sftl *sftl, struct sftl_vfl_scan *scan) ++{ ++ struct apple_sftl_vfl_source *source = &sftl->vfl_source; ++ unsigned int group, page; ++ bool have_checkpoint = false; ++ int ret; ++ ++ for (group = 0; group < 2; group++) { ++ for (page = 0; page < SFTL_VFL_PAGES; page++) { ++ struct sftl_vfl_slot *slot = &scan->slots[group][page]; + +- ret = sftl_read_page(sftl, members[member].lane & 1, +- members[member].lane >> 1, members[member].block, +- page, true, sftl->page, sftl->oob); ++ ret = sftl_vfl_read_slot(sftl, scan, group, page, slot); + if (ret) +- goto out; +- if (!page && memcmp(sftl->page + 0x100, list, sizeof(list))) { +- ret = -EBADMSG; +- goto out; +- } +- if (get_unaligned_le32(sftl->oob) != 0x00ffff20) ++ return ret; ++ if (!slot->copies) + continue; +- age = get_unaligned_le32(sftl->oob + 4); +- if (age < BIT(31)) { +- ret = -EOPNOTSUPP; +- goto out; ++ if (!scan->slots[group][0].copies || ++ (u64)slot->age + page != scan->slots[group][0].age) ++ return -EBADMSG; ++ if (group && slot->age <= scan->slots[0][0].age) { ++ u32 other = scan->slots[0][0].age - slot->age; ++ ++ if (other < SFTL_VFL_PAGES && scan->slots[0][other].copies) ++ return -EBADMSG; + } +- if (age > best_age) ++ if (slot->copies != 0xf) { ++ sftl->vfl_degraded = true; + continue; +- if (!best_members || age < best_age) { +- best_age = age; +- latest_page = page; +- memcpy(best, sftl->page, APPLE_PPN_PAGE_SIZE); +- memcpy(best_oob, sftl->oob, sizeof(best_oob)); +- best_members = BIT(member); +- } else { +- if (page != latest_page || best_members & BIT(member) || +- memcmp(best, sftl->page, APPLE_PPN_PAGE_SIZE) || +- memcmp(best_oob, sftl->oob, sizeof(best_oob))) { +- ret = -EBADMSG; +- goto out; +- } +- best_members |= BIT(member); ++ } ++ if (slot->tag == SFTL_VFL_HEADER && ++ (!have_checkpoint || slot->age < source->age)) { ++ have_checkpoint = true; ++ source->age = slot->age; ++ source->group = group; ++ source->page = page; + } + } ++ if (scan->slots[group][0].copies != 0xf) ++ sftl->vfl_degraded = true; + } +- if ((best_members != 0x0f && best_members != 0xf0) || +- get_unaligned_le16(best + 10) != 16 || +- get_unaligned_le16(best + 12) != SFTL_SUBBLOCKS || +- get_unaligned_le16(best + 14) != SFTL_SUBBLOCKS) { +- ret = -EBADMSG; +- goto out; +- } ++ return have_checkpoint ? 0 : -EBADMSG; ++} ++ ++static int sftl_vfl_load_refs(struct apple_sftl *sftl, struct sftl_vfl_scan *scan, ++ unsigned int group, unsigned int page, u16 *refs) ++{ ++ const struct sftl_vfl_slot *header = &scan->slots[group][page]; ++ unsigned int lane; ++ int ret; ++ ++ ret = sftl_vfl_reread(sftl, scan, group, page); ++ if (ret) ++ return ret; ++ /* The 13G36 loader compares FTLSuBlks (+12) with VFLSuBlks ++ * (+14). An older, smaller FTL is a different address space. ++ */ ++ if (get_unaligned_le16(scan->sample + 10) != 16 || ++ get_unaligned_le16(scan->sample + 12) != SFTL_SUBBLOCKS || ++ get_unaligned_le16(scan->sample + 14) != SFTL_SUBBLOCKS) ++ return -EOPNOTSUPP; + for (lane = 0; lane < SFTL_LANES; lane++) { +- u16 reference = get_unaligned_le16(best + 0x200 + lane * 32); +- unsigned int group = reference >> 15; ++ const struct sftl_vfl_slot *table; ++ u16 ref = get_unaligned_le16(scan->sample + 0x200 + 32 * lane); ++ u16 row = ref & 0x7fff; + +- page = reference & 0x7fff; +- if (page >= 64) { +- ret = -EBADMSG; +- goto out; +- } +- for (member = group * 4; member < group * 4 + 4; member++) { +- ret = sftl_read_page(sftl, members[member].lane & 1, +- members[member].lane >> 1, members[member].block, +- page, true, sftl->page, sftl->oob); +- if (ret) +- goto out; +- if (sftl->oob[0] != 0x20 || sftl->oob[1] != lane || +- sftl->oob[2] || sftl->oob[3]) { +- ret = -EBADMSG; +- goto out; ++ if (row >= SFTL_VFL_PAGES) ++ return -EBADMSG; ++ table = &scan->slots[ref >> 15][row]; ++ /* A stale reference can point at the same lane in a later bank cycle. */ ++ if (table->copies != 0xf || table->tag != lane || table->age <= header->age) ++ return -EBADMSG; ++ refs[lane] = ref; ++ } ++ return 0; ++} ++ ++static int sftl_vfl_same_table(struct apple_sftl *sftl, struct sftl_vfl_scan *scan, ++ u16 ref, unsigned int group, unsigned int page) ++{ ++ unsigned int block; ++ int ret; ++ ++ ret = sftl_vfl_reread(sftl, scan, ref >> 15, ref & 0x7fff); ++ if (ret) ++ return ret; ++ memcpy(scan->reference_data, scan->sample, APPLE_PPN_PAGE_SIZE); ++ ret = sftl_vfl_reread(sftl, scan, group, page); ++ if (ret) ++ return ret; ++ if (memcmp(scan->reference_data, scan->sample, 2 * SFTL_SUBBLOCKS) || ++ memcmp(scan->reference_data + 2 * SFTL_BLOCKS, scan->sample + 2 * SFTL_BLOCKS, ++ APPLE_PPN_PAGE_SIZE - 2 * SFTL_BLOCKS)) ++ return -EBADMSG; ++ /* Two trailing physical indices are outside every supported FTL ++ * address. Older inactive marker flags can differ during rotation. ++ */ ++ for (block = SFTL_SUBBLOCKS; block < SFTL_BLOCKS; block++) { ++ u16 old = get_unaligned_le16(scan->reference_data + 2 * block); ++ u16 new = get_unaligned_le16(scan->sample + 2 * block); ++ ++ if (old != new && !(old & new & BIT(15))) ++ return -EBADMSG; ++ } ++ return 0; ++} ++ ++/* During healthy rotation, unpublished table copies reproduce the source ++ * payload. An updated table that the selected checkpoint does not name can ++ * change ownership: a previous map alone is then insufficient for recovery. ++ */ ++static int sftl_vfl_check_tail(struct apple_sftl *sftl, struct sftl_vfl_scan *scan, ++ const u16 *refs) ++{ ++ const struct apple_sftl_vfl_source *source = &sftl->vfl_source; ++ u16 other[SFTL_LANES]; ++ unsigned int group, page, lane; ++ int ret; ++ ++ for (group = 0; group < 2; group++) ++ for (page = 0; page < SFTL_VFL_PAGES; page++) { ++ const struct sftl_vfl_slot *slot = &scan->slots[group][page]; ++ ++ if (!slot->copies || slot->age > source->age || ++ (group == source->group && page == source->page)) ++ continue; ++ if (slot->age == source->age) ++ return -EBADMSG; ++ sftl->vfl_degraded = true; ++ if (slot->tag != SFTL_VFL_HEADER) { ++ ret = sftl_vfl_same_table(sftl, scan, refs[slot->tag], group, page); ++ if (ret) ++ return ret; ++ continue; + } +- if (member == group * 4) { +- memcpy(table, sftl->page, APPLE_PPN_PAGE_SIZE); +- memcpy(table + APPLE_PPN_PAGE_SIZE, sftl->oob, sizeof(sftl->oob)); +- } else if (memcmp(table, sftl->page, APPLE_PPN_PAGE_SIZE) || +- memcmp(table + APPLE_PPN_PAGE_SIZE, +- sftl->oob, sizeof(sftl->oob))) { +- ret = -EBADMSG; +- goto out; ++ ret = sftl_vfl_load_refs(sftl, scan, group, page, other); ++ if (ret) ++ return ret; ++ for (lane = 0; lane < SFTL_LANES; lane++) { ++ if (other[lane] == refs[lane]) ++ continue; ++ ret = sftl_vfl_same_table(sftl, scan, refs[lane], ++ other[lane] >> 15, other[lane] & 0x7fff); ++ if (ret) ++ return ret; + } + } ++ return 0; ++} ++ ++static int sftl_vfl(struct apple_sftl *sftl) ++{ ++ const struct apple_sftl_vfl_source *source = &sftl->vfl_source; ++ struct sftl_vfl_scan *scan; ++ u16 refs[SFTL_LANES]; ++ unsigned int lane, block; ++ int ret; ++ ++ ret = sftl_vfl_members(sftl); ++ if (ret) ++ return ret; ++ scan = kvzalloc(sizeof(*scan), GFP_KERNEL); ++ if (!scan) ++ return -ENOMEM; ++ ret = sftl_vfl_index(sftl, scan); ++ if (ret) ++ goto out; ++ ret = sftl_vfl_load_refs(sftl, scan, source->group, source->page, refs); ++ if (ret) ++ goto out; ++ ret = sftl_vfl_check_tail(sftl, scan, refs); ++ if (ret) ++ goto out; ++ for (lane = 0; lane < SFTL_LANES; lane++) { ++ u16 ref = refs[lane]; ++ ++ if (!source->page && (ref >> 15) != source->group) ++ sftl->vfl_degraded = true; ++ ret = sftl_vfl_reread(sftl, scan, ref >> 15, ref & 0x7fff); ++ if (ret) ++ goto out; + for (block = 0; block < SFTL_BLOCKS; block++) { +- u16 value = get_unaligned_le16(table + block * 2); ++ u16 value = get_unaligned_le16(scan->sample + block * 2); + + if (!(value & BIT(15))) { + sftl->lanes[block] |= BIT(lane); +@@ -484,12 +702,79 @@ static int sftl_vfl(struct apple_sftl *sftl) + } + } + } +- ret = 0; + out: +- kfree(table); +- kfree(best); ++ kvfree(scan); ++ return ret; ++} ++ ++int apple_sftl_vfl_source(struct apple_sftl *sftl, struct apple_sftl_vfl_source *out) ++{ ++ int ret = 0; ++ ++ if (!sftl || !out) ++ return -EINVAL; ++ mutex_lock(&sftl->lock); ++ if (sftl->vfl_degraded || sftl->context_degraded || sftl->failed) ++ ret = -EROFS; ++ else ++ *out = sftl->vfl_source; ++ mutex_unlock(&sftl->lock); + return ret; + } ++EXPORT_SYMBOL_GPL(apple_sftl_vfl_source); ++ ++/* Qualified SLC operations also exclude writes through raw MTD. The mount's ++ * mutex keeps foreground writes, checkpoints and bank rotation serialized. ++ */ ++int apple_sftl_rotate_vfl(struct apple_sftl *sftl, const struct apple_sftl_vfl_ops *ops, ++ void *priv) ++{ ++ struct apple_sftl *cold; ++ bool started = false; ++ int ret; ++ ++ if (!sftl || !ops || !ops->erase || !ops->program) ++ return -EOPNOTSUPP; ++ cold = kvzalloc(sizeof(*cold), GFP_NOIO); ++ if (!cold) ++ return -ENOMEM; ++ mutex_lock(&sftl->lock); ++ if (sftl->failed) { ++ ret = -EIO; ++ goto out; ++ } ++ if (sftl->vfl_degraded || sftl->context_degraded) { ++ ret = -EROFS; ++ goto out; ++ } ++ ret = apple_sftl_vfl_rotate_checked(sftl->ppn, &sftl->vfl_source, ops, priv, &started); ++ if (ret) ++ goto out; ++ cold->ppn = sftl->ppn; ++ cold->page = sftl->page; ++ ret = sftl_vfl(cold); ++ if (ret) ++ goto out; ++ if (cold->vfl_degraded || cold->vfl_source.group == sftl->vfl_source.group || ++ cold->vfl_source.page != SFTL_LANES + 1 || ++ cold->vfl_source.age >= sftl->vfl_source.age || ++ memcmp(cold->vfl_source.members, sftl->vfl_source.members, sizeof(sftl->vfl_source.members)) || ++ memcmp(cold->lanes, sftl->lanes, sizeof(sftl->lanes)) || ++ memcmp(cold->vfl_reserved, sftl->vfl_reserved, sizeof(sftl->vfl_reserved)) || ++ memcmp(cold->unknown_lane_flags, sftl->unknown_lane_flags, sizeof(sftl->unknown_lane_flags))) { ++ ret = -EBADMSG; ++ goto out; ++ } ++ sftl->vfl_source = cold->vfl_source; ++out: ++ if (ret && started) ++ sftl->failed = true; ++ mutex_unlock(&sftl->lock); ++ kvfree(cold); ++ return ret; ++} ++EXPORT_SYMBOL_GPL(apple_sftl_rotate_vfl); ++ + + static int sftl_context_names(struct apple_sftl *sftl, const u8 *payload, + u16 *pending, u32 *count) +@@ -513,12 +798,167 @@ static int sftl_context_names(struct apple_sftl *sftl, const u8 *payload, + return 0; + } + +-static int sftl_context(struct apple_sftl *sftl, const u8 *heads) ++static void sftl_clear_runs(struct apple_sftl *sftl) ++{ ++ struct rb_node *node; ++ ++ while ((node = rb_first(&sftl->runs))) { ++ rb_erase(node, &sftl->runs); ++ kfree(rb_entry(node, struct sftl_run, node)); ++ } ++ sftl->nr_runs = 0; ++} ++ ++static int sftl_load_ranges(struct apple_sftl *sftl, const u8 *payload, u32 *cursor, ++ bool *short_range) ++{ ++ u32 value = get_unaligned_le32(payload + 4), offset; ++ ++ if (value == U32_MAX) ++ return *cursor == SFTL_LPAGES && get_unaligned_le32(payload) == U32_MAX ? ++ 0 : -EBADMSG; ++ if (value != 0xfffffff0 || get_unaligned_le32(payload) != *cursor || ++ *cursor == SFTL_LPAGES) ++ return -EBADMSG; ++ ++ for (offset = 8; offset < SFTL_CHUNK; offset += 8) { ++ u32 length = get_unaligned_le32(payload + offset + 4); ++ int ret; ++ ++ value = get_unaligned_le32(payload + offset); ++ if (value == U32_MAX) ++ break; ++ if (!length || length > SFTL_LPAGES - *cursor) ++ return -EBADMSG; ++ if (value == BIT(fls(SFTL_PROVIDER * 2 - 1) - 1) - 1) ++ value = SFTL_UNMAPPED; ++ ret = sftl_add_run(sftl, *cursor, *cursor + length, value); ++ if (ret) ++ return ret; ++ *cursor += length; ++ } ++ /* A save interrupted between programs can end only after complete ++ * 511-pair fragments until the whole logical address space is covered. ++ */ ++ if (offset != SFTL_CHUNK && *cursor < SFTL_LPAGES) ++ *short_range = true; ++ return offset == 8 ? -EBADMSG : 0; ++} ++ ++/* Native initial allocation takes an erased free block. A blank head alone ++ * can also describe an unnamed context spare, a reserved block or a stream. ++ * An older saved role can veto a new allocation while that fallback exists. ++ */ ++static void sftl_load_block_roles(struct apple_sftl *sftl, const u8 *meta, const u8 *payload, ++ bool selected) ++{ ++ u32 block = get_unaligned_le16(meta + 8), count = get_unaligned_le16(meta + 10); ++ ++ while (count--) { ++ u8 role = payload[0]; ++ u16 live = get_unaligned_le16(payload + 2); ++ ++ if (selected) { ++ if ((role == SFTL_CLEAN_FREE || role == SFTL_DIRTY_FREE) && live) ++ role = 0; ++ sftl->roles[block] = role; ++ sftl->block_flags[block] = payload[1]; ++ } else if (role != SFTL_CLEAN_FREE || live) { ++ __set_bit(block, sftl->fallback_occupied); ++ } ++ block++; ++ payload += 8; ++ } ++} ++ ++/* -ENODATA distinguishes a valid prefix from malformed records or a read error. */ ++static int sftl_decode_generation(struct apple_sftl *sftl, const struct sftl_records *records, ++ u32 first, u32 end, unsigned long *named) ++{ ++ u32 cursor = 0, map_records = 0, i, previous_order = 0; ++ u64 first_sequence = records->items[first].stamp & SFTL_SEQUENCE; ++ bool base_tags = true, native = false, short_range = false; ++ ++ for (i = first; i < end; i++) { ++ const struct sftl_record *record = &records->items[i]; ++ u32 index = i - first, subtype = sftl_flags(record), chunk; ++ u32 order = subtype == 4 ? 7 : subtype; ++ const u8 *meta, *payload; ++ int ret; ++ ++ if (!first_sequence || (record->stamp & SFTL_SEQUENCE) != first_sequence + index || ++ order < previous_order || (record->address & 3) != (index & 3)) ++ return -EBADMSG; ++ previous_order = order; ++ ret = sftl_read_address(sftl, record->address, &chunk); ++ if (ret) ++ return ret; ++ meta = sftl->oob + chunk * SFTL_META; ++ payload = sftl->page + chunk * SFTL_CHUNK; ++ if (sftl_stamp(meta) != record->stamp) ++ return -EBADMSG; ++ if (!index) { ++ u32 names = get_unaligned_le32(payload), k; ++ ++ if (subtype != 1 || !names || names > SFTL_MAX_CONTEXTS) ++ return -EBADMSG; ++ for (k = 0; k < names; k++) { ++ u32 block = get_unaligned_le32(payload + 4 + k * 4); ++ ++ if (!block || block >= SFTL_SUBBLOCKS || ++ !test_bit(block, sftl->reserved) || test_bit(block, named)) ++ return -EBADMSG; ++ __set_bit(block, named); ++ } ++ } ++ if (!test_bit(record->address / SFTL_STRIDE, named)) ++ return -EBADMSG; ++ if (index < 4 && subtype == 1) { ++ base_tags &= apple_sftl_native_context_tag(index, meta); ++ if (index == 3) { ++ if (!base_tags || ++ get_unaligned_le32(payload + SFTL_CHUNK - 4) != SFTL_SUBBLOCKS) ++ return -EBADMSG; ++ native = true; ++ } ++ } ++ if (native && !apple_sftl_native_context_tag(index, meta)) ++ return -EBADMSG; ++ if (native && subtype == 3) ++ sftl_load_block_roles(sftl, meta, payload, true); ++ if (subtype == 4) { ++ ret = sftl_load_ranges(sftl, payload, &cursor, &short_range); ++ if (ret) ++ return ret; ++ map_records++; ++ } ++ } ++ ++ /* cxtNextBatch pads the measured native map in groups of 64. Coverage ++ * alone can become complete many pages before the final batch does. ++ */ ++ if (cursor != SFTL_LPAGES || !map_records || ++ (native && map_records % APPLE_SFTL_NATIVE_MAP_BATCH)) { ++ if (short_range) ++ return -EBADMSG; ++ /* A partial program or a native aggregate that lost its tail does ++ * not establish the next safe write sequence. Recover for reads. ++ */ ++ if (native || (records->items[end - 1].address & 3) != 3) ++ sftl->context_degraded = true; ++ return -ENODATA; ++ } ++ sftl->native_context = native; ++ return 0; ++} ++ ++static int sftl_context(struct apple_sftl *sftl, const u8 *heads, struct sftl_records *journal) + { + struct sftl_records records = {}; ++ DECLARE_BITMAP(gapped, SFTL_SUBBLOCKS) = {}; ++ DECLARE_BITMAP(named, SFTL_SUBBLOCKS); + u16 pending[SFTL_MAX_CONTEXTS]; +- u32 count = 0, block, at, i, first = 0, cursor = 0, chunk; +- u64 previous = 0, fragment_sequence = 0; ++ u32 count = 0, block, at, i, first = 0, end, chunk, oldest; + int ret = -EBADMSG; + + for (block = 1; block < SFTL_SUBBLOCKS; block++) { +@@ -530,15 +970,21 @@ static int sftl_context(struct apple_sftl *sftl, const u8 *heads) + __set_bit(block, sftl->reserved); + } + for (at = 0; at < count; at++) { +- u32 position, pages; ++ u16 blank_lanes = 0, populated = 0; ++ u32 position, pages, lanes; ++ bool gap = false; + + block = pending[at]; +- pages = hweight16(sftl->lanes[block]) * 128; ++ lanes = hweight16(sftl->lanes[block]); ++ pages = lanes * 128; + if (!pages) { + ret = -EBADMSG; + goto out; + } + for (position = 0; position < pages; position++) { ++ u16 lane = BIT(position % lanes); ++ bool empty = false; ++ + ret = sftl_read_address(sftl, block * SFTL_STRIDE + position * 4, &chunk); + if (ret) + goto out; +@@ -546,15 +992,30 @@ static int sftl_context(struct apple_sftl *sftl, const u8 *heads) + ret = sftl_erased(sftl->page, APPLE_PPN_PAGE_SIZE) ? 0 : -EBADMSG; + if (ret) + goto out; +- break; ++ gap = true; ++ blank_lanes |= lane; ++ continue; + } ++ if (blank_lanes & lane) { ++ ret = -EBADMSG; ++ goto out; ++ } ++ populated |= lane; ++ if (gap) ++ __set_bit(block, gapped); + for (chunk = 0; chunk < 4; chunk++) { + const u8 *meta = sftl->oob + chunk * SFTL_META; + const u8 *payload = sftl->page + chunk * SFTL_CHUNK; + +- if (sftl_erased(meta, SFTL_META)) ++ if (sftl_erased(meta, SFTL_META)) { ++ if (!sftl_erased(payload, SFTL_CHUNK)) { ++ ret = -EBADMSG; ++ goto out; ++ } ++ empty = true; + continue; +- if (meta[0] != 0x1f || !meta[1] || meta[1] > 6) { ++ } ++ if (empty || meta[0] != 0x1f || !meta[1] || meta[1] > 6) { + ret = -EBADMSG; + goto out; + } +@@ -569,94 +1030,125 @@ static int sftl_context(struct apple_sftl *sftl, const u8 *heads) + goto out; + } + } ++ if (test_bit(block, gapped) && hweight16(populated) == lanes) { ++ ret = -EBADMSG; ++ goto out; ++ } + } + ret = -EBADMSG; + if (!records.count) + goto out; + sort(records.items, records.count, sizeof(*records.items), sftl_record_compare, NULL); +- if (sftl_flags(&records.items[0]) != 1) ++ /* An interrupted lane-by-lane retirement can leave older fragments ++ * without their base. They cannot provide a recovery root. ++ */ ++ for (oldest = 0; oldest < records.count; oldest++) { ++ const struct sftl_record *r = &records.items[oldest]; ++ ++ if (sftl_flags(r) == 1 && !(r->address & 3)) ++ break; ++ if (!test_bit(r->address / SFTL_STRIDE, gapped)) ++ goto out; ++ } ++ if (oldest == records.count) + goto out; + for (i = 1; i < records.count; i++) +- if (sftl_flags(&records.items[i]) == 1 && sftl_flags(&records.items[i - 1]) != 1) +- first = i; +- if (sftl_flags(&records.items[records.count - 1]) != 4) +- goto out; +- for (i = first; i < records.count; i++) { +- const struct sftl_record *record = &records.items[i]; +- u64 sequence = record->stamp & SFTL_SEQUENCE; +- const u8 *payload; +- u32 offset, value, length, names; +- +- if (i != first && sequence != previous + 1) ++ if (!sftl_record_compare(&records.items[i - 1], &records.items[i])) + goto out; +- previous = sequence; +- ret = sftl_read_address(sftl, record->address, &chunk); +- if (ret) ++ for (end = records.count; end > oldest; end = first) { ++ first = end - 1; ++ while (first > oldest && (sftl_flags(&records.items[first]) != 1 || ++ (records.items[first].address & 3))) ++ first--; ++ memset(named, 0, sizeof(named)); ++ ret = sftl_decode_generation(sftl, &records, first, end, named); ++ if (!ret) ++ break; ++ if (ret != -ENODATA) + goto out; ++ sftl_clear_runs(sftl); ++ } ++ if (end == oldest) { + ret = -EBADMSG; +- if (sftl_stamp(sftl->oob + chunk * SFTL_META) != record->stamp) +- goto out; +- payload = sftl->page + chunk * SFTL_CHUNK; +- if (i == first) { +- names = get_unaligned_le32(payload); +- if (!names || names > SFTL_MAX_CONTEXTS) ++ goto out; ++ } ++ /* Retired lanes may be wholly erased while old lanes still hold ++ * verified context records. Require a newer intact generation which ++ * names that site, and keep the writer off until reconciliation. ++ */ ++ for (i = 0; i < records.count; i++) { ++ block = records.items[i].address / SFTL_STRIDE; ++ if (test_bit(block, gapped)) { ++ if (i >= first || !test_bit(block, named)) { ++ ret = -EBADMSG; + goto out; +- for (offset = 0; offset < names; offset++) { +- value = get_unaligned_le32(payload + 4 + offset * 4); +- if (value >= SFTL_BLOCKS || !test_bit(value, sftl->reserved)) +- goto out; + } ++ sftl->context_degraded = true; + } +- if (sftl_flags(record) != 4) +- continue; +- if (fragment_sequence && sequence != fragment_sequence + 1) +- goto out; +- fragment_sequence = sequence; +- value = get_unaligned_le32(payload + 4); +- if (value == U32_MAX) ++ } ++ /* Only the selected generation can establish a data role. Canonical ++ * block tables from an older fallback can still veto initial allocation. ++ */ ++ for (i = 0; sftl->native_context && !sftl->context_degraded && i < first; i++) { ++ const struct sftl_record *r = &records.items[i]; ++ const u8 *meta; ++ u32 start; ++ ++ if (sftl_flags(r) != 3) + continue; +- if (value != 0xfffffff0 || get_unaligned_le32(payload) != cursor) ++ ret = sftl_read_address(sftl, r->address, &chunk); ++ if (ret) + goto out; +- for (offset = 8; offset < SFTL_CHUNK; offset += 8) { +- value = get_unaligned_le32(payload + offset); +- length = get_unaligned_le32(payload + offset + 4); +- if (value == U32_MAX) +- break; +- if (!length || length > SFTL_LPAGES - cursor) +- goto out; +- if (value == BIT(fls(SFTL_PROVIDER * 2 - 1) - 1) - 1) +- value = SFTL_UNMAPPED; +- ret = sftl_add_run(sftl, cursor, cursor + length, value); +- if (ret) +- goto out; +- cursor += length; ++ meta = sftl->oob + chunk * SFTL_META; ++ if (sftl_stamp(meta) != r->stamp) { + ret = -EBADMSG; ++ goto out; + } ++ start = get_unaligned_le16(meta + 8); ++ if (start < SFTL_SUBBLOCKS && !(start % (SFTL_CHUNK / 8)) && ++ apple_sftl_native_context_tag(8 + start / (SFTL_CHUNK / 8), meta)) ++ sftl_load_block_roles(sftl, meta, sftl->page + chunk * SFTL_CHUNK, false); + } +- if (cursor != SFTL_LPAGES || !fragment_sequence) +- goto out; +- /* Retain compact references so an explicit snapshot can preserve opaque data. */ +- for (i = first; i < records.count; i++) { ++ /* Retain the selected generation's opaque records for later snapshots. */ ++ for (i = first; i < end; i++) { + const struct sftl_record *record = &records.items[i]; + + if (sftl_flags(record) == 4) + continue; +- ret = sftl_record_add(&sftl->context, record->stamp, record->key, +- record->address); ++ ret = sftl_record_add(&sftl->context, record->stamp, record->key, record->address); + if (ret) + goto out; + } +- /* Older saved generations have separate maps we have not validated. */ +- if (!first) ++ /* Keep every possible fallback out of the erase path. */ ++ if (!first && end == records.count) + sftl->checkpoint_start = records.items[0].stamp & SFTL_SEQUENCE; +- sftl->checkpoint_sequence = fragment_sequence; +- sftl->sequence = fragment_sequence; ++ sftl->checkpoint_sequence = records.items[end - 1].stamp & SFTL_SEQUENCE; ++ sftl->sequence = sftl->checkpoint_sequence; ++ if (end < records.count) { ++ /* Account for observed context sequences in normal journal order. ++ * Only their own IDs may bridge a gap between two data records. ++ */ ++ records.count -= end; ++ memmove(records.items, records.items + end, records.count * sizeof(*records.items)); ++ *journal = records; ++ records.items = NULL; ++ } + ret = 0; + out: + kvfree(records.items); + return ret; + } + ++static bool sftl_user_epoch_valid(const u8 *meta, u64 sequence) ++{ ++ u32 user_epoch = get_unaligned_le32(meta + 12); ++ ++ /* Relocation restamps the weave sequence and retains the original user epoch. */ ++ if (meta[0] == 1 && !meta[1] && get_unaligned_le32(meta + 8) < SFTL_LPAGES) ++ return user_epoch <= sequence >> 16; ++ return user_epoch == sequence >> 16; ++} ++ + static int sftl_add_journal(struct sftl_records *journal, const u8 *meta, + u32 address, u64 checkpoint) + { +@@ -665,12 +1157,28 @@ static int sftl_add_journal(struct sftl_records *journal, const u8 *meta, + if (sequence <= checkpoint || sftl_erased(meta, SFTL_META)) + return 0; + if ((meta[0] != 1 && meta[0] != 2) || +- get_unaligned_le32(meta + 12) != sequence >> 16 || ++ !sftl_user_epoch_valid(meta, sequence) || + (meta[0] == 1 ? meta[1] > 1 : meta[1] != 0)) + return -EBADMSG; + return sftl_record_add(journal, sftl_stamp(meta), get_unaligned_le32(meta + 8), address); + } + ++static int sftl_remember_user_seq(struct apple_sftl *sftl, u32 block, const u8 *footer, u32 runs) ++{ ++ /* Four base chunks identify the layout validated by sftl_decode_generation. */ ++ if (sftl->context.count != APPLE_SFTL_ACCOUNT_RECORDS || ++ sftl_flags(&sftl->context.items[3]) != 1) ++ return 0; ++ if (!sftl->closed_user_seq) { ++ sftl->closed_user_seq = kvzalloc(SFTL_SUBBLOCKS * sizeof(*sftl->closed_user_seq), ++ GFP_NOIO); ++ if (!sftl->closed_user_seq) ++ return -ENOMEM; ++ } ++ apple_sftl_summarize_user_seq(&sftl->closed_user_seq[block], footer, runs); ++ return 0; ++} ++ + static int sftl_footer(struct apple_sftl *sftl, const u8 *head, u32 block, + u32 pages, struct sftl_records *journal, u64 *floor) + { +@@ -754,14 +1262,20 @@ static int sftl_footer(struct apple_sftl *sftl, const u8 *head, u32 block, + meta[1] = head[1]; + put_unaligned_le64(sequence << 16 | get_unaligned_le16(meta), meta); + put_unaligned_le32(actual_key, meta + 8); +- put_unaligned_le32(sequence >> 16, meta + 12); ++ if (!meta[1] && actual_key < SFTL_LPAGES) ++ memcpy(meta + 12, footer + index + 4, sizeof(u32)); ++ else ++ put_unaligned_le32(sequence >> 16, meta + 12); + } + ret = sftl_add_journal(journal, meta, address, sftl->checkpoint_sequence); + if (ret) + goto out; + } + } +- ret = terminated && position == pages * 4 - chunks ? 0 : -EBADMSG; ++ if (terminated && position == pages * 4 - chunks) ++ ret = sftl_remember_user_seq(sftl, block, footer, index / SFTL_META); ++ else ++ ret = -EBADMSG; + out: + kfree(footer); + return ret; +@@ -786,11 +1300,38 @@ static bool sftl_partial_footer(struct apple_sftl *sftl, const u8 *head, + return true; + } + +-static int sftl_journal(struct apple_sftl *sftl, const u8 *heads) ++/* A Linux block may close across an observed checkpoint save. For a torn ++ * suffix, admit only the exact sequence IDs actually read from context OOB. ++ * The leading portion of journal is sorted until all data has been gathered. ++ */ ++static bool sftl_context_gap(const struct apple_sftl *sftl, const struct sftl_records *journal, ++ u32 nr_context, u64 first, u64 next) ++{ ++ u32 low = 0, high = nr_context; ++ u64 count = next - first; ++ ++ if (first == (sftl->context.items[0].stamp & SFTL_SEQUENCE) && ++ next == sftl->checkpoint_sequence + 1) ++ return true; ++ if (count > nr_context) ++ return false; ++ while (low < high) { ++ u32 mid = low + (high - low) / 2; ++ ++ if ((journal->items[mid].stamp & SFTL_SEQUENCE) < first) ++ low = mid + 1; ++ else ++ high = mid; ++ } ++ return count && count <= nr_context - low && ++ (journal->items[low].stamp & SFTL_SEQUENCE) == first && ++ (journal->items[low + count - 1].stamp & SFTL_SEQUENCE) == next - 1; ++} ++ ++static int sftl_journal(struct apple_sftl *sftl, const u8 *heads, struct sftl_records *journal) + { +- struct sftl_records journal = {}; + u64 floor = sftl->checkpoint_sequence, expected = floor + 1; +- u64 checkpoint_first = sftl->context.items[0].stamp & SFTL_SEQUENCE; ++ u32 nr_context = journal->count; + u32 block, position, i, pages; + unsigned int chunk; + int ret; +@@ -807,7 +1348,7 @@ static int sftl_journal(struct apple_sftl *sftl, const u8 *heads) + if (ret) + goto out; + if (sftl->oob[0] == 0x1c) { +- ret = sftl_footer(sftl, head, block, pages, &journal, &floor); ++ ret = sftl_footer(sftl, head, block, pages, journal, &floor); + if (ret) + goto out; + continue; +@@ -832,12 +1373,12 @@ static int sftl_journal(struct apple_sftl *sftl, const u8 *heads) + u64 sequence = sftl_sequence(meta); + + if (meta[0] != 1 || meta[1] || sequence < next || +- get_unaligned_le32(meta + 12) != sequence >> 16) ++ !sftl_user_epoch_valid(meta, sequence)) + complete_prefix = false; + /* A checkpoint occupies its own sequence numbers between pages. */ +- if (sequence > next && +- (!position || chunk || next != checkpoint_first || +- sequence != sftl->checkpoint_sequence + 1)) ++ if (complete_prefix && sequence > next && ++ (!position || chunk || !sftl_context_gap(sftl, journal, ++ nr_context, next, sequence))) + complete_prefix = false; + next = sequence + 1; + } +@@ -861,7 +1402,7 @@ static int sftl_journal(struct apple_sftl *sftl, const u8 *heads) + goto out; + } + for (chunk = 0; chunk < 4; chunk++) { +- ret = sftl_add_journal(&journal, sftl->oob + chunk * SFTL_META, ++ ret = sftl_add_journal(journal, sftl->oob + chunk * SFTL_META, + block * SFTL_STRIDE + position * 4 + chunk, + sftl->checkpoint_sequence); + if (ret) +@@ -869,14 +1410,16 @@ static int sftl_journal(struct apple_sftl *sftl, const u8 *heads) + } + } + } +- sort(journal.items, journal.count, sizeof(*journal.items), sftl_record_compare, NULL); +- for (i = 0; i < journal.count; i++) { +- const struct sftl_record *record = &journal.items[i]; ++ sort(journal->items, journal->count, sizeof(*journal->items), sftl_record_compare, NULL); ++ for (i = 0; i < journal->count; i++) { ++ const struct sftl_record *record = &journal->items[i]; + u64 sequence = record->stamp & SFTL_SEQUENCE; + + ret = -EBADMSG; + if (sequence != expected++) + goto out; ++ if (sftl_kind(record) == 0x1f) ++ continue; + if (sftl_kind(record) == 2 && record->key == SFTL_TRIM) { + const u8 *payload; + u32 count, offset; +@@ -910,10 +1453,9 @@ static int sftl_journal(struct apple_sftl *sftl, const u8 *heads) + sftl->sequence = sequence; + } + sftl->journal_sequence = expected - 1; +- sftl->sequence = max(sftl->sequence, floor); ++ sftl->sequence = max(sftl->journal_sequence, floor); + ret = 0; + out: +- kvfree(journal.items); + return ret; + } + +@@ -937,6 +1479,7 @@ static void sftl_mark_mapped(struct apple_sftl *sftl, unsigned long *blocks) + int apple_sftl_load(struct apple_ppn *provider, struct apple_sftl **out) + { + struct apple_sftl *sftl; ++ struct sftl_records journal = {}; + u8 *heads; + u32 block; + unsigned int chunk; +@@ -987,18 +1530,20 @@ int apple_sftl_load(struct apple_ppn *provider, struct apple_sftl **out) + goto failed; + } + } +- ret = sftl_context(sftl, heads); ++ ret = sftl_context(sftl, heads, &journal); + if (ret) + goto failed; + /* A map entry superseded by journal replay still belongs to the saved map. */ + sftl_mark_mapped(sftl, sftl->checkpoint_used); +- ret = sftl_journal(sftl, heads); ++ ret = sftl_journal(sftl, heads, &journal); + if (ret) + goto failed; ++ kvfree(journal.items); + kfree(heads); + *out = sftl; + return 0; + failed: ++ kvfree(journal.items); + kfree(heads); + apple_sftl_free(sftl); + return ret; +@@ -1007,16 +1552,12 @@ EXPORT_SYMBOL_GPL(apple_sftl_load); + + void apple_sftl_free(struct apple_sftl *sftl) + { +- struct rb_node *node; +- + if (!sftl) + return; +- while ((node = rb_first(&sftl->runs))) { +- rb_erase(node, &sftl->runs); +- kfree(rb_entry(node, struct sftl_run, node)); +- } ++ sftl_clear_runs(sftl); + kvfree(sftl->footer); + kvfree(sftl->context.items); ++ kvfree(sftl->closed_user_seq); + kfree(sftl->page); + kfree(sftl); + } +@@ -1070,6 +1611,8 @@ static int sftl_writer_prepare(struct apple_sftl *sftl) + { + u32 block; + ++ if (sftl->vfl_degraded || sftl->context_degraded) ++ return -EROFS; + if (sftl->writer_prepared) + return 0; + if (sftl->journal_sequence != sftl->sequence) +@@ -1133,6 +1676,7 @@ static int sftl_verify_closed(struct apple_sftl *sftl, struct apple_sftl_gc_bloc + base = sftl_sequence(trailer); + if (trailer[0] != 0x1c || chunks < 4 || chunks > SFTL_FOOTER_CHUNKS || + chunks % 4 || trailer[1] >> 1 != chunks || ++ (sftl->native_context && ((trailer[1] & 1) || sftl->block_flags[victim->block] != trailer[1])) || + get_unaligned_le16(trailer + 14) != 0xffff || !base || !span || + base + span - 1 >= sftl->checkpoint_start) + return -EOPNOTSUPP; +@@ -1163,12 +1707,6 @@ static int sftl_verify_closed(struct apple_sftl *sftl, struct apple_sftl_gc_bloc + return -EBADMSG; + if (key >= SFTL_LPAGES || length > SFTL_LPAGES - key) + return -EOPNOTSUPP; +- /* Some native runs cross a high-word boundary with the initial +- * high word in the footer. Their per-chunk semantics are unverified. +- */ +- if (high == (base + relative) >> 16 && +- high != (base + relative + length - 1) >> 16) +- return -EOPNOTSUPP; + for (j = 0; j < length; j++, position++) { + u64 seq = base + relative + j; + const u8 *meta; +@@ -1186,10 +1724,14 @@ static int sftl_verify_closed(struct apple_sftl *sftl, struct apple_sftl_gc_bloc + (relocate && meta[0] == 1 && meta[1] == 1)) + return -EOPNOTSUPP; + if (meta[0] != 1 || meta[1] > 1 || +- sftl_sequence(meta) != seq || high != seq >> 16 || +- get_unaligned_le32(meta + 8) != key + j || +- get_unaligned_le32(meta + 12) != high) ++ sftl_sequence(meta) != seq || !sftl_user_epoch_valid(meta, seq) || ++ get_unaligned_le32(meta + 8) != key + j || high > seq >> 16) + return -EBADMSG; ++ /* Native media also has runs whose summary differs from individual ++ * user epochs. Leave them intact until that encoding is understood. ++ */ ++ if (get_unaligned_le32(meta + 12) != high) ++ return -EOPNOTSUPP; + } + previous_end = relative + length; + } +@@ -1221,13 +1763,17 @@ static int sftl_block_blank(struct apple_sftl *sftl, u32 block, unsigned long de + static bool sftl_closed_candidate(struct apple_sftl *sftl, u32 block) + { + return sftl->checkpoint_start && test_bit(block, sftl->old_closed) && ++ (!sftl->native_context || sftl->roles[block] == SFTL_CLOSED_DATA || ++ sftl->roles[block] == SFTL_DIRTY_FREE) && + !test_bit(block, sftl->reserved) && !test_bit(block, sftl->vfl_reserved) && + !test_bit(block, sftl->unknown_lane_flags); + } + + static bool sftl_retired_candidate(struct apple_sftl *sftl, u32 block) + { +- return sftl_closed_candidate(sftl, block) && !test_bit(block, sftl->checkpoint_used); ++ return !sftl->vfl_degraded && !sftl->context_degraded && ++ (!sftl->native_context || sftl->roles[block] == SFTL_DIRTY_FREE) && ++ sftl_closed_candidate(sftl, block) && !test_bit(block, sftl->checkpoint_used); + } + + static int sftl_erase_verified(struct apple_sftl *sftl, u32 block, unsigned long deadline) +@@ -1324,7 +1870,12 @@ static int sftl_check_landing(struct apple_sftl *sftl, u32 block, unsigned long + bool blank; + int ret; + ++ /* A cached spare was either positively free or was erased and checked ++ * in this transaction after verifying an ordinary closed donor. ++ */ + if (!block || block >= SFTL_SUBBLOCKS || !sftl->lanes[block] || ++ (sftl->native_context && sftl->roles[block] != SFTL_CLEAN_FREE && ++ sftl->roles[block] != SFTL_CLOSED_DATA && sftl->roles[block] != SFTL_DIRTY_FREE) || + test_bit(block, sftl->reserved) || test_bit(block, sftl->vfl_reserved) || + test_bit(block, sftl->checkpoint_used)) + return -EBADMSG; +@@ -1341,6 +1892,8 @@ static int sftl_take_blank(struct apple_sftl *sftl, u32 *out, unsigned long dead + while (sftl->next_candidate < SFTL_SUBBLOCKS) { + block = sftl->next_candidate++; + if (!test_bit(block, sftl->candidates) || test_bit(block, sftl->used) || ++ (sftl->native_context && (sftl->roles[block] != SFTL_CLEAN_FREE || ++ test_bit(block, sftl->fallback_occupied))) || + (sftl->checkpoint_ops && test_bit(block, sftl->checkpoint_used)) || + !sftl->lanes[block]) + continue; +@@ -1391,10 +1944,10 @@ static void sftl_data_meta(u8 *meta, u64 sequence, u32 logical_page) + put_unaligned_le32(sequence >> 16, meta + 12); + } + +-static void sftl_append_summary(struct apple_sftl *sftl, u64 sequence, u32 key) ++static void sftl_append_summary(struct apple_sftl *sftl, const u8 *meta) + { +- u32 relative = sequence - sftl->append_base; +- u32 high = sequence >> 16; ++ u32 relative = sftl_sequence(meta) - sftl->append_base; ++ u32 high = get_unaligned_le32(meta + 12), key = get_unaligned_le32(meta + 8); + u8 *entry; + + if (sftl->footer_runs) { +@@ -1456,11 +2009,8 @@ static int sftl_move_program(void *priv, u32 address, const struct apple_sftl_gc + ret = sftl_program_address(sftl, address, page->data, page->oob); + if (ret) + return ret; +- for (chunk = 0; chunk < 4; chunk++) { +- const u8 *meta = page->oob + chunk * SFTL_META; +- +- sftl_append_summary(sftl, sftl_sequence(meta), get_unaligned_le32(meta + 8)); +- } ++ for (chunk = 0; chunk < 4; chunk++) ++ sftl_append_summary(sftl, page->oob + chunk * SFTL_META); + sftl->append_position += 4; + return 0; + } +@@ -1484,10 +2034,22 @@ static void sftl_adopt_checkpoint(struct apple_sftl *sftl, struct apple_sftl *co + sftl->nr_runs = cold->nr_runs; + sftl->context = cold->context; + cold->context = old_context; ++ kvfree(sftl->closed_user_seq); ++ sftl->closed_user_seq = cold->closed_user_seq; ++ cold->closed_user_seq = NULL; + sftl->sequence = cold->sequence; + sftl->journal_sequence = cold->journal_sequence; + sftl->checkpoint_sequence = cold->checkpoint_sequence; + sftl->checkpoint_start = cold->checkpoint_start; ++ sftl->vfl_source = cold->vfl_source; ++ sftl->vfl_degraded = cold->vfl_degraded; ++ sftl->context_degraded = cold->context_degraded; ++ sftl->native_context = cold->native_context; ++ memcpy(sftl->roles, cold->roles, sizeof(sftl->roles)); ++ memcpy(sftl->block_flags, cold->block_flags, sizeof(sftl->block_flags)); ++ for (block = 0; block < ARRAY_SIZE(sftl->candidates); block++) ++ sftl->candidates[block] &= cold->candidates[block]; ++ memcpy(sftl->fallback_occupied, cold->fallback_occupied, sizeof(sftl->fallback_occupied)); + memcpy(sftl->reserved, cold->reserved, sizeof(sftl->reserved)); + memcpy(sftl->vfl_reserved, cold->vfl_reserved, sizeof(sftl->vfl_reserved)); + memcpy(sftl->checkpoint_used, cold->checkpoint_used, sizeof(sftl->checkpoint_used)); +@@ -1499,6 +2061,112 @@ static void sftl_adopt_checkpoint(struct apple_sftl *sftl, struct apple_sftl *co + __set_bit(block, sftl->used); + } + ++/* A native save on this profile uses two named normal-view context blocks. ++ * Only accept a complete generation starting at page zero of the active block, ++ * with a blank second block. The old generation remains readable while the ++ * alternate is programmed. Both blocks must be exclusively context-owned. ++ */ ++static bool sftl_owns_context_block(struct apple_sftl *sftl, u32 block) ++{ ++ return !sftl->vfl_degraded && !sftl->context_degraded && ++ block && block < SFTL_BLOCKS - 32 && sftl->lanes[block] && ++ test_bit(block, sftl->reserved) && !test_bit(block, sftl->vfl_reserved) && ++ !test_bit(block, sftl->unknown_lane_flags) && !test_bit(block, sftl->checkpoint_used); ++} ++ ++static int sftl_native_pair(struct apple_sftl *sftl, const struct apple_sftl_snapshot *saved, ++ u32 blocks[2], unsigned long deadline) ++{ ++ const u8 *base = saved->opaque[0].data; ++ u64 length; ++ u32 slots, i, j; ++ unsigned int chunk; ++ bool blank; ++ int ret; ++ ++ if (get_unaligned_le32(base) != 2 || !sftl->checkpoint_start) ++ return -EOPNOTSUPP; ++ blocks[0] = get_unaligned_le32(base + 8); ++ blocks[1] = get_unaligned_le32(base + 4); ++ if (!apple_sftl_account_context_pair(saved, blocks)) ++ return -EOPNOTSUPP; ++ if (blocks[0] == blocks[1] || !sftl_owns_context_block(sftl, blocks[0]) || ++ !sftl_owns_context_block(sftl, blocks[1]) || ++ sftl->context.items[0].address != blocks[1] * SFTL_STRIDE) ++ return -EOPNOTSUPP; ++ for (i = 0; i < saved->nr_runs; i++) { ++ const struct apple_sftl_extent *r = &saved->runs[i]; ++ u32 end; ++ ++ if (r->provider == SFTL_UNMAPPED) ++ continue; ++ end = (r->provider + r->length - 1) / SFTL_STRIDE; ++ for (j = 0; j < 2; j++) ++ if (r->provider / SFTL_STRIDE <= blocks[j] && blocks[j] <= end) ++ return -EBADMSG; ++ } ++ length = sftl->checkpoint_sequence - sftl->checkpoint_start + 1; ++ slots = hweight16(sftl->lanes[blocks[1]]) * 512; ++ if (!length || length % 4 || length > slots) ++ return -EOPNOTSUPP; ++ for (i = 0; i < slots; i += 4) { ++ if (time_after_eq(jiffies, deadline)) ++ return -ETIMEDOUT; ++ ret = sftl_read_address(sftl, blocks[1] * SFTL_STRIDE + i, &chunk); ++ if (ret) ++ return ret; ++ if (i >= length) { ++ if (!sftl_erased(sftl->page, APPLE_PPN_PAGE_SIZE) || ++ !sftl_erased(sftl->oob, APPLE_PPN_OOB_SIZE)) ++ return -EOPNOTSUPP; ++ continue; ++ } ++ for (j = 0; j < 4; j++) { ++ const u8 *meta = sftl->oob + j * SFTL_META; ++ ++ if (meta[0] != 0x1f || !meta[1] || meta[1] > 6 || ++ sftl_sequence(meta) != sftl->checkpoint_start + i + j) ++ return -EOPNOTSUPP; ++ } ++ } ++ ret = sftl_block_blank(sftl, blocks[0], deadline, &blank); ++ return ret ? ret : (blank ? 0 : -EOPNOTSUPP); ++} ++ ++static int sftl_context_program(void *priv, u32 address, const struct apple_sftl_gc_page *page) ++{ ++ struct sftl_move *move = priv; ++ ++ if (time_after_eq(jiffies, move->deadline)) ++ return -ETIMEDOUT; ++ return sftl_program_address(move->sftl, address, page->data, page->oob); ++} ++ ++static const struct apple_sftl_gc_io sftl_context_io = { ++ .read = sftl_move_read, ++ .program = sftl_context_program, ++}; ++ ++static int sftl_reload_plan(struct apple_sftl *sftl, struct apple_sftl_gc *gc, ++ struct apple_sftl **out) ++{ ++ struct apple_sftl_snapshot *snapshot = NULL; ++ int ret; ++ ++ apple_sftl_free(*out); ++ *out = NULL; ++ ret = apple_sftl_load(sftl->ppn, out); ++ if (ret) ++ return ret; ++ ret = sftl_snapshot_locked(*out, SFTL_GC_MAP_BYTES, &snapshot); ++ if (!ret) ++ ret = apple_sftl_gc_recovered(gc, snapshot); ++ apple_sftl_snapshot_free(snapshot); ++ if (!ret && memcmp(sftl->lanes, (*out)->lanes, sizeof(sftl->lanes))) ++ return -EBADMSG; ++ return ret; ++} ++ + static int sftl_compact(struct apple_sftl *sftl, u32 landing, unsigned long deadline) + { + struct apple_sftl_gc_block destination = { +@@ -1506,7 +2174,7 @@ static int sftl_compact(struct apple_sftl *sftl, u32 landing, unsigned long dead + }; + struct sftl_move move = { .sftl = sftl, .deadline = deadline }; + struct apple_sftl_checkpoint *image = NULL, *readback = NULL; +- struct apple_sftl_snapshot *before = NULL, *after = NULL; ++ struct apple_sftl_snapshot *before = NULL; + struct apple_sftl_gc *gc = NULL; + struct apple_sftl *cold = NULL; + u32 blocks[SFTL_MAX_CONTEXTS], nr = ARRAY_SIZE(blocks), block, slots, i; +@@ -1517,7 +2185,8 @@ static int sftl_compact(struct apple_sftl *sftl, u32 landing, unsigned long dead + .block = block, .lanes = sftl->lanes[block], .closed = true, + }; + +- if (!sftl_closed_candidate(sftl, block) || !test_bit(block, sftl->checkpoint_used)) ++ if (!sftl_closed_candidate(sftl, block) || !test_bit(block, sftl->checkpoint_used) || ++ (sftl->native_context && sftl->roles[block] != SFTL_CLOSED_DATA)) + continue; + if (!before) { + ret = sftl_snapshot_locked(sftl, SFTL_GC_MAP_BYTES, &before); +@@ -1549,7 +2218,15 @@ static int sftl_compact(struct apple_sftl *sftl, u32 landing, unsigned long dead + ret = -ENOSPC; + continue; + } +- ret = sftl->checkpoint_ops->reserve(sftl->checkpoint_priv, blocks, &nr); ++ if (sftl->checkpoint_ops->prepare_native) { ++ nr = 2; ++ ret = sftl_native_pair(sftl, before, blocks, deadline); ++ if (!ret) ++ ret = apple_sftl_gc_prepare_native(gc, sftl->checkpoint_ops->prepare_native, ++ sftl->checkpoint_priv, blocks, hweight16(sftl->lanes[blocks[0]]) * 512); ++ } else { ++ ret = sftl->checkpoint_ops->reserve(sftl->checkpoint_priv, blocks, &nr); ++ } + if (ret) + goto out; + if (!nr || nr > ARRAY_SIZE(blocks)) { +@@ -1586,7 +2263,11 @@ static int sftl_compact(struct apple_sftl *sftl, u32 landing, unsigned long dead + ret = apple_sftl_gc_checkpoint(gc, blocks, nr, &image); + if (ret) + goto failed; +- ret = sftl->checkpoint_ops->install(sftl->checkpoint_priv, image, &readback); ++ if (sftl->checkpoint_ops->prepare_native) ++ ret = apple_sftl_checkpoint_stage(image, blocks[0], sftl->lanes[blocks[0]], ++ &sftl_context_io, &move, &readback); ++ else ++ ret = sftl->checkpoint_ops->install(sftl->checkpoint_priv, image, &readback); + if (readback == image) { + readback = NULL; + ret = -EBADMSG; +@@ -1599,18 +2280,33 @@ static int sftl_compact(struct apple_sftl *sftl, u32 landing, unsigned long dead + ret = apple_sftl_gc_checkpoint_done(gc, readback); + if (ret) + goto failed; +- ret = apple_sftl_load(sftl->ppn, &cold); +- if (ret) +- goto failed; +- ret = sftl_snapshot_locked(cold, SFTL_GC_MAP_BYTES, &after); +- if (ret) +- goto failed; +- ret = apple_sftl_gc_recovered(gc, after); ++ ret = sftl_reload_plan(sftl, gc, &cold); + if (ret) + goto failed; ++ if (sftl->checkpoint_ops->prepare_native) { ++ /* The new map has now been recovered with the fallback intact. ++ * Verify its physical location and both reservations before ++ * retiring the old context. Reload again before releasing data. ++ */ ++ if (!cold->native_context || cold->roles[landing] != SFTL_OPEN_DATA || ++ cold->roles[block] != SFTL_DIRTY_FREE || ++ cold->context.items[0].address != blocks[0] * SFTL_STRIDE || ++ !sftl_owns_context_block(cold, blocks[0]) || ++ !sftl_owns_context_block(cold, blocks[1])) { ++ ret = -EBADMSG; ++ goto failed; ++ } ++ ret = sftl_erase_verified(sftl, blocks[1], deadline); ++ if (ret) ++ goto failed; ++ ret = sftl_reload_plan(sftl, gc, &cold); ++ if (ret) ++ goto failed; ++ } + if (!sftl_retired_candidate(cold, block) || test_bit(landing, cold->unknown_lane_flags) || + test_bit(landing, cold->reserved) || test_bit(landing, cold->vfl_reserved) || +- memcmp(sftl->lanes, cold->lanes, sizeof(sftl->lanes))) { ++ (cold->native_context && (cold->roles[landing] != SFTL_OPEN_DATA || ++ cold->roles[block] != SFTL_DIRTY_FREE))) { + ret = -EBADMSG; + goto failed; + } +@@ -1626,7 +2322,6 @@ failed: + sftl->failed = true; + out: + apple_sftl_free(cold); +- apple_sftl_snapshot_free(after); + apple_sftl_snapshot_free(before); + apple_sftl_checkpoint_free(readback); + apple_sftl_checkpoint_free(image); +@@ -1708,7 +2403,10 @@ void apple_sftl_set_checkpoint_ops(struct apple_sftl *sftl, + const struct apple_sftl_checkpoint_ops *ops, void *priv) + { + mutex_lock(&sftl->lock); +- sftl->checkpoint_ops = ops && ops->reserve && ops->install ? ops : NULL; ++ sftl->checkpoint_ops = NULL; ++ if (ops && ((sftl->native_context && ops->prepare_native && !ops->reserve && !ops->install) || ++ (!sftl->native_context && !ops->prepare_native && ops->reserve && ops->install))) ++ sftl->checkpoint_ops = ops; + sftl->checkpoint_priv = priv; + mutex_unlock(&sftl->lock); + } +@@ -1739,6 +2437,9 @@ static int sftl_close_block(struct apple_sftl *sftl) + if (ret) + return ret; + } ++ ret = sftl_remember_user_seq(sftl, sftl->append_block, sftl->footer, sftl->footer_runs); ++ if (ret) ++ return ret; + sftl->append_block = U32_MAX; + return 0; + } +@@ -1794,7 +2495,7 @@ int apple_sftl_write(struct apple_sftl *sftl, u32 logical_page, + ret = sftl_replace(sftl, key, 1, address + chunk); + if (ret) + goto failed; +- sftl_append_summary(sftl, first_sequence + chunk, key); ++ sftl_append_summary(sftl, sftl->oob + chunk * SFTL_META); + } + sftl->sequence += 4; + sftl->journal_sequence = sftl->sequence; +@@ -1829,6 +2530,7 @@ EXPORT_SYMBOL_GPL(apple_sftl_flush); + + void apple_sftl_get_info(struct apple_sftl *sftl, struct apple_sftl_info *info) + { ++ info->read_only = sftl->vfl_degraded || sftl->context_degraded; + info->logical_pages = SFTL_LPAGES; + info->disk_pages = SFTL_DISK_PAGES; + info->nr_runs = sftl->nr_runs; +@@ -1892,6 +2594,11 @@ static int sftl_snapshot_locked(struct apple_sftl *sftl, size_t max_bytes, + memcpy(record->data, sftl->page + chunk * SFTL_CHUNK, SFTL_CHUNK); + } + snapshot->nr_opaque = sftl->context.count; ++ if (sftl->closed_user_seq) { ++ ret = apple_sftl_account_user_seq(snapshot, sftl->closed_user_seq); ++ if (ret) ++ goto failed; ++ } + *out = snapshot; + goto unlock; + failed: +diff --git a/drivers/mtd/apple-sftl-reclaim.c b/drivers/mtd/apple-sftl-reclaim.c +index cf30529b3..e642acd59 100644 +--- a/drivers/mtd/apple-sftl-reclaim.c ++++ b/drivers/mtd/apple-sftl-reclaim.c +@@ -33,6 +33,7 @@ struct apple_sftl_gc { + u32 nr_copies, copied, max_records, victim, destination; + u64 retire_sequence, copied_sequence, victim_sequence; + u32 padding; ++ u16 footer_chunks; + enum apple_sftl_gc_stage stage; + }; + +@@ -314,6 +315,7 @@ int apple_sftl_gc_plan(const struct apple_sftl_snapshot *snapshot, + gc->snapshot.journal_sequence = snapshot->journal_sequence; + gc->max_records = max_records; + gc->victim = victim->block; ++ gc->footer_chunks = victim->footer_chunks; + gc->destination = destination->block; + gc->retire_sequence = max(victim->last_sequence, snapshot->sequence); + gc->victim_sequence = victim->last_sequence; +@@ -659,17 +661,41 @@ int apple_sftl_gc_prepare_native(struct apple_sftl_gc *gc, + apple_sftl_prepare_accounting prepare, void *priv, + const u32 blocks[2], u32 capacity) + { ++ struct apple_sftl_snapshot staged; ++ struct apple_sftl_checkpoint *image = NULL; ++ size_t size; + int ret; + + if (!gc || !prepare || !blocks || !capacity || gc->stage != APPLE_SFTL_GC_COPY || + gc->copied || gc->checkpoint) + return -EINVAL; +- ret = prepare(priv, gc->snapshot.opaque, gc->snapshot.nr_opaque, &gc->snapshot, ++ if (!apple_sftl_account_context_pair(&gc->snapshot, blocks)) ++ return -EBADMSG; ++ /* The policy may change several fields before refusing a transition. ++ * Keep its edits private so retries always begin with the original state. ++ */ ++ staged = gc->snapshot; ++ size = array_size(staged.nr_opaque, sizeof(*staged.opaque)); ++ staged.opaque = kvmalloc(size, GFP_NOIO); ++ if (!staged.opaque) ++ return -ENOMEM; ++ memcpy(staged.opaque, gc->snapshot.opaque, size); ++ ret = prepare(priv, staged.opaque, staged.nr_opaque, &staged, + gc->victim, gc->destination, blocks); +- if (ret) +- return ret; +- return apple_sftl_checkpoint_build(&gc->snapshot, blocks, 2, +- min(gc->max_records, capacity), &gc->checkpoint); ++ if (!ret) ++ ret = apple_sftl_checkpoint_build(&staged, blocks, 2, ++ min(gc->max_records, capacity), &image); ++ if (!ret && !apple_sftl_account_gc_image(&gc->snapshot, image, blocks, ++ gc->victim, gc->destination, ++ apple_sftl_gc_copy_chunks(gc) - gc->padding, ++ gc->footer_chunks)) { ++ apple_sftl_checkpoint_free(image); ++ ret = -EBADMSG; ++ } ++ if (!ret) ++ gc->checkpoint = image; ++ kvfree(staged.opaque); ++ return ret; + } + + int apple_sftl_gc_checkpoint(struct apple_sftl_gc *gc, const u32 *blocks, +diff --git a/drivers/mtd/apple-sftl-test.c b/drivers/mtd/apple-sftl-test.c +index 89b9ae498..b44cca5c6 100644 +--- a/drivers/mtd/apple-sftl-test.c ++++ b/drivers/mtd/apple-sftl-test.c +@@ -6,9 +6,12 @@ + #include + #include + #include ++#include + + #include "apple-sftl.h" + #include "apple-sftl-reclaim.h" ++#include "apple-sftl-test-fixture.h" ++#include "apple-sftl-vfl.h" + + #define TEST_LOGICAL_PAGES 15625016U + #define TEST_STRIDE 8192U +@@ -55,6 +58,12 @@ struct sftl_fixture { + struct apple_ppn_ops ops; + enum sftl_fault fault; + unsigned int reads; ++ bool rotating_vfl, vfl_table_changed, vfl_bad_age, vfl_stale_ref, vfl_age_collision; ++ unsigned int vfl_cut, vfl_target; ++ bool vfl_mount, slc_erased[8]; ++ unsigned int slc_mutations, slc_fail; ++ struct sftl_test_page *slc[4][18]; ++ u16 vfl_tail_marker; + unsigned int programs; + unsigned int fail_program; + unsigned int tear_program; +@@ -66,6 +75,12 @@ struct sftl_fixture { + unsigned int erase_pages; + bool checkpoint_slot, show_staged, checkpoint_published; + bool fail_reserve_once; ++ unsigned int corrupt_stage_readback; ++ unsigned int native_erase_cut, native_policy_calls; ++ u16 erased_context_lanes; ++ bool loaded_before_retirement, loaded_after_retirement; ++ int native_policy_error; ++ bool drop_context_role, keep_donor_role, duplicate_stream; + unsigned int installs, recovery_reads; + enum sftl_test_commit commit; + bool dirty_candidate; +@@ -107,24 +122,43 @@ static void sftl_test_members(struct sftl_fixture *f, u8 *data) + static int sftl_test_context(struct sftl_fixture *f, unsigned int lane, + unsigned int page, u8 *data, u8 *oob, u8 *status) + { +- unsigned int i; ++ unsigned int i, group = lane / 4, step = page * 4 + lane % 4; ++ bool target = f->rotating_vfl && group == f->vfl_target; ++ u32 origin = f->rotating_vfl ? (target ? 0xfffffe00 : 0xffffff00) : ++ (group ? 0xffffff00 : 0xfffffe00); ++ u16 ref_group = target && !page ? 1 - group : group; ++ ++ if (target && f->vfl_age_collision) ++ origin = 0xffffff00; ++ if (lane >= 8 || (target && step >= f->vfl_cut)) ++ return 0; ++ if (f->vfl_mount && f->slc_erased[lane]) { ++ struct sftl_test_page *row = lane >= 4 && page < 18 ? f->slc[lane - 4][page] : NULL; + +- if (lane >= 8) ++ if (row) { ++ memcpy(data, row->data, APPLE_PPN_PAGE_SIZE); ++ memcpy(oob, row->oob, APPLE_PPN_OOB_SIZE); ++ *status = 0x40; ++ } + return 0; +- if (!page) { ++ } ++ if (!page || page == 17 || (f->vfl_mount && !group && page > 17)) { + memset(data, 0, 16384); + put_unaligned_le16(16, data + 10); + put_unaligned_le16(2086, data + 12); + put_unaligned_le16(2086, data + 14); + sftl_test_members(f, data); + for (i = 0; i < 16; i++) +- put_unaligned_le16(i + 1, data + 0x200 + i * 32); ++ put_unaligned_le16((ref_group << 15) | (i + 1), data + 0x200 + i * 32); ++ memset(oob, 0, 16); + oob[0] = 0x20; + oob[1] = 0xff; + oob[2] = 0xff; + oob[3] = 0; +- put_unaligned_le32(lane < 4 ? 0xfffffff0 : 0xfffffff1, oob + 4); +- if (f->fault == SFTL_CHECKPOINT_COPY && lane == 1) ++ put_unaligned_le32(origin - page, oob + 4); ++ if (f->vfl_stale_ref && target && !page) ++ put_unaligned_le16((f->vfl_target << 15) | 1, data + 0x200); ++ if (f->fault == SFTL_CHECKPOINT_COPY && lane == 1 && page == 17) + data[0] ^= 1; + *status = 0x40; + } else if (page <= 16) { +@@ -144,8 +178,14 @@ static int sftl_test_context(struct sftl_fixture *f, unsigned int lane, + put_unaligned_le16(0, data + 2056 * 2); + put_unaligned_le16(0, data + 2057 * 2); + } ++ memset(oob, 0, 16); + oob[0] = 0x20; + oob[1] = page - 1; ++ put_unaligned_le32(origin - page + (target && f->vfl_bad_age), oob + 4); ++ if (target && f->vfl_tail_marker && page == 1) ++ put_unaligned_le16(f->vfl_tail_marker, data + 2 * 2086); ++ if (target && f->vfl_table_changed && page == 1) ++ put_unaligned_le16(0x8000, data + 2); + oob[2] = 0; + oob[3] = 0; + if (f->fault == SFTL_LANE_COPY && lane == 1 && page == 1) +@@ -270,6 +310,14 @@ static int sftl_test_read_page(void *priv, unsigned int bus, unsigned int cau, + memset(data, 0xff, 16384); + memset(oob, 0xff, 64); + *status = 0x49; ++ if (context && block == 2087 && !lane && !page && sftl_test_written(f, 2, 0, 23)) { ++ if (!f->erased_context_lanes) ++ f->loaded_before_retirement = true; ++ if (f->erased_context_lanes == 3) ++ f->loaded_after_retirement = true; ++ } ++ if (!context && block == 1 && (f->erased_context_lanes & (1U << lane))) ++ return 0; + if (context) { + if (block == 2087 || (f->vfl_candidate && block == 2056 && !lane)) + return sftl_test_context(f, lane, page, data, oob, status); +@@ -484,6 +532,171 @@ static void sftl_test_reject_incomplete_or_ambiguous(struct kunit *test) + } + } + ++static int sftl_test_slc_erase(void *priv, const struct apple_sftl_vfl_member *m) ++{ ++ struct sftl_fixture *f = priv; ++ ++ if (m->block != 2087 || m->lane < 4 || m->lane >= 8 || f->slc_erased[m->lane]) ++ return -EINVAL; ++ f->slc_erased[m->lane] = true; ++ return ++f->slc_mutations == f->slc_fail ? -EIO : 0; ++} ++ ++static int sftl_test_slc_program(void *priv, const struct apple_sftl_vfl_member *m, ++ u8 page, const u8 *data, const u8 *oob) ++{ ++ struct sftl_fixture *f = priv; ++ struct sftl_test_page *row; ++ ++ if (m->block != 2087 || m->lane < 4 || m->lane >= 8 || page >= 18 || ++ !f->slc_erased[m->lane] || f->slc[m->lane - 4][page]) ++ return -EINVAL; ++ row = kunit_kmalloc(f->test, sizeof(*row), GFP_KERNEL); ++ if (!row) ++ return -ENOMEM; ++ memcpy(row->data, data, APPLE_PPN_PAGE_SIZE); ++ memcpy(row->oob, oob, APPLE_PPN_OOB_SIZE); ++ f->slc[m->lane - 4][page] = row; ++ return ++f->slc_mutations == f->slc_fail ? -EIO : 0; ++} ++ ++static void sftl_test_rotate_mounted(struct kunit *test) ++{ ++ const struct apple_sftl_vfl_ops ops = { ++ .erase = sftl_test_slc_erase, .program = sftl_test_slc_program, ++ }; ++ unsigned int fault; ++ u8 *data = kunit_kmalloc(test, 4096, GFP_KERNEL); ++ ++ KUNIT_ASSERT_NOT_NULL(test, data); ++ for (fault = 0; fault < 2; fault++) { ++ struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); ++ struct apple_sftl_vfl_source source; ++ struct apple_sftl *sftl = NULL; ++ struct apple_sftl_info info; ++ ++ KUNIT_ASSERT_NOT_NULL(test, f); ++ f->vfl_mount = true; ++ f->append_candidates = 1; ++ f->slc_fail = fault ? 2 : 0; ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ memset(data, 'N', 4096); ++ KUNIT_ASSERT_EQ(test, apple_sftl_write(sftl, 8, data, 1), 0); ++ if (fault) { ++ KUNIT_EXPECT_EQ(test, apple_sftl_rotate_vfl(sftl, &ops, f), -EIO); ++ KUNIT_EXPECT_EQ(test, f->slc_mutations, 2U); ++ KUNIT_EXPECT_EQ(test, apple_sftl_read(sftl, 8, data), -EIO); ++ KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, 9, data, 1), -EIO); ++ } else { ++ KUNIT_ASSERT_EQ(test, apple_sftl_rotate_vfl(sftl, &ops, f), 0); ++ KUNIT_EXPECT_EQ(test, f->slc_mutations, 76U); ++ KUNIT_ASSERT_EQ(test, apple_sftl_vfl_source(sftl, &source), 0); ++ KUNIT_EXPECT_EQ(test, source.group, (u8)1); ++ KUNIT_EXPECT_EQ(test, source.page, (u8)17); ++ /* This bank has unprogrammed space. A refused second ++ * rotation must leave the mounted writer usable. ++ */ ++ KUNIT_EXPECT_EQ(test, apple_sftl_rotate_vfl(sftl, &ops, f), -EOPNOTSUPP); ++ KUNIT_EXPECT_EQ(test, f->slc_mutations, 76U); ++ KUNIT_ASSERT_EQ(test, apple_sftl_read(sftl, 8, data), 0); ++ KUNIT_EXPECT_EQ(test, data[0], (u8)'N'); ++ memset(data, 'O', 4096); ++ KUNIT_ASSERT_EQ(test, apple_sftl_write(sftl, 9, data, 1), 0); ++ } ++ apple_sftl_free(sftl); ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ apple_sftl_get_info(sftl, &info); ++ KUNIT_EXPECT_EQ(test, info.read_only, !!fault); ++ KUNIT_ASSERT_EQ(test, apple_sftl_read(sftl, 8, data), 0); ++ KUNIT_EXPECT_EQ(test, data[0], (u8)'N'); ++ if (!fault) { ++ KUNIT_ASSERT_EQ(test, apple_sftl_read(sftl, 9, data), 0); ++ KUNIT_EXPECT_EQ(test, data[0], (u8)'O'); ++ } ++ apple_sftl_free(sftl); ++ } ++} ++ ++/* Each native page has four independently programmed SLC replicas. */ ++static void sftl_test_vfl_rotation_cuts(struct kunit *test) ++{ ++ /* Initial header, sixteen lane tables and final header, four copies each. */ ++ const unsigned int programs = 18 * 4; ++ struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); ++ u8 *data = kunit_kmalloc(test, 4096, GFP_KERNEL); ++ unsigned int group, cut; ++ ++ KUNIT_ASSERT_NOT_NULL(test, f); ++ KUNIT_ASSERT_NOT_NULL(test, data); ++ f->rotating_vfl = true; ++ for (group = 0; group < 2; group++) { ++ f->vfl_target = group; ++ for (cut = 0; cut <= programs; cut++) { ++ struct apple_sftl_vfl_source source; ++ struct apple_sftl_info info; ++ struct apple_sftl *sftl = NULL; ++ ++ f->vfl_cut = cut; ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ apple_sftl_get_info(sftl, &info); ++ KUNIT_EXPECT_EQ(test, info.read_only, cut < programs); ++ KUNIT_ASSERT_EQ(test, apple_sftl_read(sftl, 0, data), 0); ++ KUNIT_EXPECT_EQ(test, data[0], (u8)'C'); ++ if (cut < programs) { ++ KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, 0, data, 1), -EROFS); ++ KUNIT_EXPECT_EQ(test, apple_sftl_vfl_source(sftl, &source), -EROFS); ++ } else { ++ KUNIT_ASSERT_EQ(test, apple_sftl_vfl_source(sftl, &source), 0); ++ KUNIT_EXPECT_EQ(test, source.group, (u8)group); ++ KUNIT_EXPECT_EQ(test, source.page, (u8)17); ++ } ++ apple_sftl_free(sftl); ++ } ++ } ++ KUNIT_EXPECT_EQ(test, f->programs, 0U); ++ KUNIT_EXPECT_EQ(test, f->erases, 0U); ++} ++ ++static void sftl_test_vfl_rejects_ambiguous_tables(struct kunit *test) ++{ ++ struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); ++ struct apple_sftl *sftl = NULL; ++ struct apple_sftl_info info; ++ ++ KUNIT_ASSERT_NOT_NULL(test, f); ++ f->rotating_vfl = true; ++ f->vfl_target = 1; ++ f->vfl_cut = 8; ++ f->vfl_age_collision = true; ++ KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &sftl), -EBADMSG); ++ KUNIT_EXPECT_PTR_EQ(test, sftl, NULL); ++ f->vfl_age_collision = false; ++ f->vfl_tail_marker = 0x8003; ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ apple_sftl_get_info(sftl, &info); ++ KUNIT_EXPECT_EQ(test, info.read_only, true); ++ apple_sftl_free(sftl); ++ f->vfl_tail_marker = 3; ++ KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &sftl), -EBADMSG); ++ KUNIT_EXPECT_PTR_EQ(test, sftl, NULL); ++ f->vfl_tail_marker = 0; ++ f->vfl_table_changed = true; ++ KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &sftl), -EBADMSG); ++ KUNIT_EXPECT_PTR_EQ(test, sftl, NULL); ++ f->vfl_table_changed = false; ++ f->vfl_bad_age = true; ++ KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &sftl), -EBADMSG); ++ KUNIT_EXPECT_PTR_EQ(test, sftl, NULL); ++ /* Four agreeing tables at the right coordinate but from a newer cycle. */ ++ f->vfl_bad_age = false; ++ f->vfl_stale_ref = true; ++ f->vfl_cut = 8; ++ KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &sftl), -EBADMSG); ++ KUNIT_EXPECT_PTR_EQ(test, sftl, NULL); ++ KUNIT_EXPECT_EQ(test, f->programs, 0U); ++ KUNIT_EXPECT_EQ(test, f->erases, 0U); ++} ++ + static void sftl_test_trim_then_overwrite(struct kunit *test) + { + struct sftl_fixture *f = sftl_test_fixture(test, SFTL_TRIM); +@@ -1023,6 +1236,271 @@ static void sftl_test_append_rejects_invalid_window(struct kunit *test) + apple_sftl_free(sftl); + } + ++/* Block 7 is the single-lane staging destination in this fixture. */ ++static int sftl_test_stage_read(void *priv, u32 addr, struct apple_sftl_gc_page *page) ++{ ++ struct sftl_fixture *f = priv; ++ u32 chunk = addr - 7 * TEST_STRIDE; ++ u8 status; ++ int ret; ++ ++ if (chunk >= 512 || (chunk & 3)) ++ return -ERANGE; ++ ret = sftl_test_read_page(f, 0, 0, 7, chunk / 4, false, page->data, page->oob, &status); ++ if (!ret && status == 0x40 && f->corrupt_stage_readback == chunk / 4 + 1) ++ page->data[APPLE_PPN_PAGE_SIZE - 1] ^= 1; ++ return ret; ++} ++ ++static int sftl_test_stage_program(void *priv, u32 addr, const struct apple_sftl_gc_page *page) ++{ ++ u32 chunk = addr - 7 * TEST_STRIDE; ++ ++ if (chunk >= 512 || (chunk & 3)) ++ return -ERANGE; ++ return sftl_test_program_page(priv, 0, 0, 7, chunk / 4, page->data, page->oob); ++} ++ ++static const struct apple_sftl_gc_io sftl_test_stage_io = { ++ .read = sftl_test_stage_read, ++ .program = sftl_test_stage_program, ++}; ++ ++/* Make the saved map span two physical program units by splitting its ++ * unmapped tail. The first three map fragments leave logical space uncovered. ++ * Both checkpoint generations are visible to every subsequent cold load. ++ */ ++static int sftl_test_second_generation(struct sftl_fixture *f) ++{ ++ struct apple_sftl_snapshot *snapshot = NULL, map; ++ struct apple_sftl_checkpoint *image = NULL, *readback = NULL; ++ struct apple_sftl *sftl = NULL; ++ struct apple_sftl_extent *runs, *tail; ++ const u32 split = 1533, block = 7; ++ u32 last, i; ++ int ret; ++ ++ f->checkpoint_slot = true; ++ f->show_staged = true; ++ ret = apple_sftl_load(&f->provider, &sftl); ++ if (ret) ++ return ret; ++ ret = apple_sftl_snapshot(sftl, 65536, &snapshot); ++ apple_sftl_free(sftl); ++ if (ret) ++ return ret; ++ map = *snapshot; ++ last = map.nr_runs - 1; ++ tail = &map.runs[last]; ++ if (tail->provider != APPLE_SFTL_NO_ADDRESS || tail->length <= split) { ++ ret = -EINVAL; ++ goto out; ++ } ++ runs = kunit_kcalloc(f->test, map.nr_runs + split, sizeof(*runs), GFP_KERNEL); ++ if (!runs) { ++ ret = -ENOMEM; ++ goto out; ++ } ++ memcpy(runs, map.runs, last * sizeof(*runs)); ++ for (i = 0; i <= split; i++) ++ runs[last + i] = (struct apple_sftl_extent) { ++ .logical = tail->logical + i, .provider = APPLE_SFTL_NO_ADDRESS, ++ .length = i == split ? tail->length - split : 1, ++ }; ++ map.runs = runs; ++ map.nr_runs += split; ++ ret = apple_sftl_checkpoint_build(&map, &block, 1, 8, &image); ++ if (ret) ++ goto out; ++ if (image->count != 8 || image->first_sequence != map.sequence + 1) { ++ ret = -EINVAL; ++ goto out; ++ } ++ ret = apple_sftl_checkpoint_stage(image, block, 1, &sftl_test_stage_io, f, &readback); ++ if (ret) { ++ if (readback) ++ ret = -EINVAL; ++ } else if (!readback || readback->count != image->count || ++ memcmp(readback->records, image->records, image->count * sizeof(*image->records))) { ++ ret = -EBADMSG; ++ } ++out: ++ apple_sftl_checkpoint_free(readback); ++ apple_sftl_checkpoint_free(image); ++ apple_sftl_snapshot_free(snapshot); ++ return ret; ++} ++ ++static void sftl_test_context_power_cuts(struct kunit *test) ++{ ++ struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); ++ struct sftl_test_page *saved[2]; ++ u8 *data = kunit_kmalloc(test, APPLE_SFTL_CHUNK, GFP_KERNEL); ++ unsigned int cut; ++ ++ KUNIT_ASSERT_NOT_NULL(test, f); ++ KUNIT_ASSERT_NOT_NULL(test, data); ++ KUNIT_ASSERT_EQ(test, sftl_test_second_generation(f), 0); ++ memcpy(saved, f->written[2], sizeof(saved)); ++ for (cut = 0; cut <= 2; cut++) { ++ struct apple_sftl_info info; ++ struct apple_sftl *sftl = NULL; ++ ++ f->written[2][0] = cut ? saved[0] : NULL; ++ f->written[2][1] = cut == 2 ? saved[1] : NULL; ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ apple_sftl_get_info(sftl, &info); ++ KUNIT_EXPECT_EQ(test, info.checkpoint_sequence, ++ cut == 2 ? TEST_APPEND_FIRST + 7 : TEST_CHECKPOINT); ++ KUNIT_EXPECT_EQ(test, info.sequence, TEST_APPEND_FIRST - 1 + 4 * cut); ++ KUNIT_ASSERT_EQ(test, apple_sftl_read(sftl, 0, data), 0); ++ sftl_test_expect_fill(test, data, 'C'); ++ KUNIT_ASSERT_EQ(test, apple_sftl_read(sftl, 2, data), 0); ++ sftl_test_expect_fill(test, data, 'G'); ++ KUNIT_ASSERT_EQ(test, apple_sftl_read(sftl, 6, data), 0); ++ sftl_test_expect_fill(test, data, 6); ++ KUNIT_ASSERT_EQ(test, apple_sftl_read(sftl, 2000, data), 0); ++ sftl_test_expect_fill(test, data, 0); ++ KUNIT_EXPECT_EQ(test, f->programs, 2U); ++ KUNIT_EXPECT_EQ(test, f->erases, 0U); ++ apple_sftl_free(sftl); ++ } ++} ++ ++static void sftl_test_context_stage_failures(struct kunit *test) ++{ ++ enum { BEFORE_PROGRAM, UNCERTAIN_PROGRAM, CORRUPT_READBACK }; ++ unsigned int page, fault; ++ ++ /* Losing power before either page, or losing its completion status, ++ * leaves different bytes on the medium. Reload only what reached NAND. ++ */ ++ for (page = 1; page <= 2; page++) { ++ for (fault = BEFORE_PROGRAM; fault <= CORRUPT_READBACK; fault++) { ++ struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); ++ struct apple_sftl *sftl = NULL; ++ struct apple_sftl_info info; ++ u8 *data = kunit_kmalloc(test, APPLE_SFTL_CHUNK, GFP_KERNEL); ++ unsigned int persisted = fault == BEFORE_PROGRAM ? page - 1 : page; ++ ++ KUNIT_ASSERT_NOT_NULL(test, f); ++ KUNIT_ASSERT_NOT_NULL(test, data); ++ if (fault == CORRUPT_READBACK) { ++ f->corrupt_stage_readback = page; ++ } else { ++ f->fail_program = page; ++ f->uncertain_program = fault == UNCERTAIN_PROGRAM; ++ } ++ KUNIT_EXPECT_LT(test, sftl_test_second_generation(f), 0); ++ KUNIT_EXPECT_EQ(test, f->programs, page); ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ apple_sftl_get_info(sftl, &info); ++ KUNIT_EXPECT_EQ(test, info.checkpoint_sequence, ++ persisted == 2 ? TEST_APPEND_FIRST + 7 : TEST_CHECKPOINT); ++ KUNIT_EXPECT_EQ(test, info.sequence, TEST_APPEND_FIRST - 1 + persisted * 4); ++ KUNIT_ASSERT_EQ(test, apple_sftl_read(sftl, 2, data), 0); ++ sftl_test_expect_fill(test, data, 'G'); ++ KUNIT_EXPECT_EQ(test, f->programs, page); ++ KUNIT_EXPECT_EQ(test, f->erases, 0U); ++ apple_sftl_free(sftl); ++ } ++ } ++} ++ ++static void sftl_test_checkpoint_stage_protects_tail(struct kunit *test) ++{ ++ struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); ++ struct sftl_test_page *dirty = kunit_kmalloc(test, sizeof(*dirty), GFP_KERNEL); ++ ++ KUNIT_ASSERT_NOT_NULL(test, f); ++ KUNIT_ASSERT_NOT_NULL(test, dirty); ++ memset(dirty, 0xff, sizeof(*dirty)); ++ dirty->data[sizeof(dirty->data) - 1] = 0; ++ KUNIT_ASSERT_EQ(test, sftl_test_program_page(f, 0, 0, 7, 127, dirty->data, dirty->oob), 0); ++ KUNIT_EXPECT_EQ(test, sftl_test_second_generation(f), -EBADMSG); ++ KUNIT_EXPECT_EQ(test, f->programs, 1U); ++ KUNIT_EXPECT_PTR_EQ(test, sftl_test_written(f, 2, 0, 0), NULL); ++ KUNIT_ASSERT_NOT_NULL(test, sftl_test_written(f, 2, 0, 127)); ++ KUNIT_EXPECT_EQ(test, sftl_test_written(f, 2, 0, 127)->data[sizeof(dirty->data) - 1], 0); ++ KUNIT_EXPECT_EQ(test, f->erases, 0U); ++} ++ ++static void sftl_test_context_torn_append_replay(struct kunit *test) ++{ ++ struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); ++ struct apple_sftl *sftl = NULL; ++ struct apple_sftl_info info; ++ struct sftl_test_page *written; ++ u8 *data = kunit_kmalloc(test, APPLE_SFTL_CHUNK, GFP_KERNEL); ++ ++ KUNIT_ASSERT_NOT_NULL(test, f); ++ KUNIT_ASSERT_NOT_NULL(test, data); ++ KUNIT_ASSERT_EQ(test, sftl_test_second_generation(f), 0); ++ f->written[2][1] = NULL; ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ memset(data, 'W', APPLE_SFTL_CHUNK); ++ KUNIT_ASSERT_EQ(test, apple_sftl_write(sftl, 2, data, 1), 0); ++ apple_sftl_free(sftl); ++ written = sftl_test_written(f, 0, 0, 0); ++ KUNIT_ASSERT_NOT_NULL(test, written); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le64(written->oob) >> 16, TEST_APPEND_FIRST + 4); ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ apple_sftl_get_info(sftl, &info); ++ KUNIT_EXPECT_EQ(test, info.checkpoint_sequence, TEST_CHECKPOINT); ++ KUNIT_EXPECT_EQ(test, info.sequence, TEST_APPEND_FIRST + 7); ++ KUNIT_ASSERT_EQ(test, apple_sftl_read(sftl, 2, data), 0); ++ sftl_test_expect_fill(test, data, 'W'); ++ KUNIT_ASSERT_EQ(test, apple_sftl_read(sftl, 6, data), 0); ++ sftl_test_expect_fill(test, data, 6); ++ KUNIT_EXPECT_EQ(test, f->programs, 3U); ++ KUNIT_EXPECT_EQ(test, f->erases, 0U); ++ apple_sftl_free(sftl); ++} ++ ++static void sftl_test_reject_corrupt_generation(struct kunit *test) ++{ ++ enum { BAD_CURSOR, DUPLICATE_STAMP, MISSING_STAMP, ORPHAN_PAYLOAD, NO_COMPLETE, DATA_GAP }; ++ unsigned int fault; ++ ++ for (fault = BAD_CURSOR; fault <= DATA_GAP; fault++) { ++ struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); ++ struct apple_sftl *sftl = NULL; ++ struct sftl_test_page *page; ++ u8 *meta; ++ ++ KUNIT_ASSERT_NOT_NULL(test, f); ++ KUNIT_ASSERT_EQ(test, sftl_test_second_generation(f), 0); ++ f->written[2][1] = NULL; ++ page = f->written[2][0]; ++ meta = page->oob + 3 * APPLE_SFTL_META; ++ switch (fault) { ++ case BAD_CURSOR: ++ put_unaligned_le32(17, page->data + APPLE_SFTL_CHUNK); ++ break; ++ case DUPLICATE_STAMP: ++ memcpy(meta, meta - APPLE_SFTL_META, APPLE_SFTL_META); ++ break; ++ case MISSING_STAMP: ++ put_unaligned_le64((TEST_APPEND_FIRST + 4) << 16 | 0x041f, meta); ++ break; ++ case ORPHAN_PAYLOAD: ++ memset(meta, 0xff, APPLE_SFTL_META); ++ break; ++ case NO_COMPLETE: ++ f->fault = SFTL_INCOMPLETE_CONTEXT; ++ break; ++ case DATA_GAP: ++ f->fault = SFTL_JOURNAL_GAP; ++ break; ++ } ++ KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &sftl), -EBADMSG); ++ KUNIT_EXPECT_PTR_EQ(test, sftl, NULL); ++ KUNIT_EXPECT_EQ(test, f->programs, 2U); ++ KUNIT_EXPECT_EQ(test, f->erases, 0U); ++ apple_sftl_free(sftl); ++ } ++} ++ + static void sftl_test_checkpoint_cold_roundtrip(struct kunit *test) + { + struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); +@@ -1286,6 +1764,45 @@ static void sftl_test_gc_preserves_protected_blocks(struct kunit *test) + } + } + ++static void sftl_test_context_pins_fallback_blocks(struct kunit *test) ++{ ++ struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); ++ struct apple_sftl_snapshot *snapshot = NULL; ++ struct apple_sftl *sftl = NULL; ++ u8 *data = kunit_kmalloc(test, APPLE_PPN_PAGE_SIZE, GFP_KERNEL); ++ struct sftl_test_page *last; ++ unsigned int cut, programs; ++ u64 newest; ++ ++ KUNIT_ASSERT_NOT_NULL(test, f); ++ KUNIT_ASSERT_NOT_NULL(test, data); ++ f->checkpoint_slot = true; ++ KUNIT_ASSERT_EQ(test, sftl_test_gc_medium(f, data, GC_CHECKPOINT_BEFORE_OVERWRITE), 0); ++ KUNIT_ASSERT_EQ(test, sftl_test_second_generation(f), 0); ++ last = f->written[2][1]; ++ newest = get_unaligned_le64(last->oob + 3 * APPLE_SFTL_META) >> 16; ++ programs = f->programs; ++ f->ops.erase_block = sftl_test_erase_block; ++ for (cut = 1; cut <= 2; cut++) { ++ f->written[2][1] = cut == 2 ? last : NULL; ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ KUNIT_ASSERT_EQ(test, apple_sftl_snapshot(sftl, 65536, &snapshot), 0); ++ KUNIT_EXPECT_EQ(test, snapshot->checkpoint_sequence, ++ cut == 2 ? newest : f->checkpoint->sequence); ++ KUNIT_EXPECT_EQ(test, snapshot->sequence, newest - (2 - cut) * 4); ++ apple_sftl_snapshot_free(snapshot); ++ KUNIT_ASSERT_EQ(test, apple_sftl_read(sftl, TEST_APPEND_BASE, data), 0); ++ sftl_test_expect_fill(test, data, 'L'); ++ /* The older saved map still refers to block 5, now fully superseded. */ ++ KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 480, data, 1), ++ -ENOSPC); ++ KUNIT_EXPECT_EQ(test, f->programs, programs); ++ KUNIT_EXPECT_EQ(test, f->erases, 0U); ++ apple_sftl_free(sftl); ++ } ++ apple_sftl_checkpoint_free(f->checkpoint); ++} ++ + static void sftl_test_gc_erase_failure(struct kunit *test) + { + static const struct { +@@ -1482,6 +1999,346 @@ static void sftl_test_gc_relocated_data(struct kunit *test, struct apple_sftl *s + sftl_test_expect_fill(test, data, foreground); + } + ++/* Build a 96-record saved context from actual ordinary writes. The native ++ * shape has four chunks of base and a full padded batch of 64 map records. ++ * The old two-lane context really has records on both lanes. ++ */ ++static int sftl_test_native_context(struct sftl_fixture *f) ++{ ++ struct apple_sftl_snapshot *snapshot = NULL, map; ++ struct apple_sftl_checkpoint *image = NULL; ++ struct apple_sftl *sftl = NULL; ++ const u32 blocks[] = { 1, 7 }; ++ int ret; ++ ++ ret = apple_sftl_load(&f->provider, &sftl); ++ if (ret) ++ return ret; ++ ret = apple_sftl_snapshot(sftl, 65536, &snapshot); ++ apple_sftl_free(sftl); ++ if (ret) ++ return ret; ++ map = *snapshot; ++ map.nr_opaque = 32; ++ map.opaque = kvzalloc(array_size(map.nr_opaque, sizeof(*map.opaque)), GFP_KERNEL); ++ if (!map.opaque) { ++ ret = -ENOMEM; ++ goto out; ++ } ++ apple_sftl_test_native_prefix(map.opaque); ++ map.opaque[8].data[8 * blocks[0]] = 7; ++ map.opaque[8].data[8 * blocks[1]] = 7; ++ map.opaque[8].data[8 * 5] = 3; ++ map.opaque[8].data[8 * 5 + 1] = 64; ++ map.opaque[8].data[8 * 6] = 1; ++ put_unaligned_le32(9, map.opaque[28].data + 5 * sizeof(u32)); ++ ret = apple_sftl_checkpoint_build(&map, blocks, 2, 96, &image); ++ kvfree(map.opaque); ++ if (ret) ++ goto out; ++ if (image->count != 96) { ++ apple_sftl_checkpoint_free(image); ++ ret = -EINVAL; ++ goto out; ++ } ++ apple_sftl_checkpoint_free(f->checkpoint); ++ f->checkpoint = image; ++ f->show_staged = true; ++out: ++ apple_sftl_snapshot_free(snapshot); ++ return ret; ++} ++ ++static int sftl_test_native_medium(struct sftl_fixture *f, u8 *data) ++{ ++ int ret = sftl_test_gc_live_medium(f, data); ++ ++ return ret ? ret : sftl_test_native_context(f); ++} ++ ++/* Only this synthetic medium authorizes the remaining stream/role state. */ ++static int sftl_test_prepare_native(void *priv, struct apple_sftl_context_record *records, ++ u32 count, const struct apple_sftl_snapshot *map, ++ u32 victim, u32 landing, const u32 blocks[2]) ++{ ++ struct sftl_fixture *f = priv; ++ ++ f->native_policy_calls++; ++ if (count != 32 || !map->nr_runs || records[0].meta[1] != 1 || ++ blocks[0] != 7 || blocks[1] != 1 || victim != 5 || landing != 6) ++ return -EINVAL; ++ if (records[8].data[8 * victim] != 3 || records[8].data[8 * landing] != 1) ++ return -EINVAL; ++ if (f->drop_context_role) ++ records[8].data[8 * blocks[1]] = 9; ++ if (!f->keep_donor_role) ++ records[8].data[8 * victim] = 9; ++ put_unaligned_le32(0, records[28].data + victim * sizeof(u32)); ++ records[8].data[8 * landing] = 4; ++ if (f->duplicate_stream) ++ records[8].data[8 * 4] = 4; ++ return f->native_policy_error; ++} ++ ++static const struct apple_sftl_checkpoint_ops sftl_test_native_ops = { ++ .prepare_native = sftl_test_prepare_native, ++}; ++ ++static int sftl_test_native_erase(void *priv, unsigned int bus, unsigned int cau, unsigned int block) ++{ ++ struct sftl_fixture *f = priv; ++ ++ f->erases++; ++ if (f->erases == f->native_erase_cut) ++ return -EIO; ++ if (cau || bus > 1) ++ return -EPERM; ++ if (block == 1 && f->loaded_before_retirement && !(f->erased_context_lanes & (1U << bus))) { ++ f->erased_context_lanes |= 1U << bus; ++ return 0; ++ } ++ if (block != 5 || bus || f->erased_context_lanes != 3 || !f->loaded_after_retirement) ++ return -EPERM; ++ memset(f->written[0], 0, 128 * sizeof(*f->written[0])); ++ return 0; ++} ++ ++static void sftl_test_gc_native_publication(struct kunit *test) ++{ ++ struct apple_sftl_info info; ++ unsigned int cut; ++ ++ for (cut = 0; cut <= 3; cut++) { ++ struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); ++ u8 *data = kunit_kmalloc(test, APPLE_PPN_PAGE_SIZE, GFP_KERNEL); ++ struct apple_sftl *sftl = NULL; ++ u64 newest; ++ int ret; ++ ++ KUNIT_ASSERT_NOT_NULL(test, f); ++ KUNIT_ASSERT_NOT_NULL(test, data); ++ KUNIT_ASSERT_EQ(test, sftl_test_native_medium(f, data), 0); ++ newest = f->checkpoint->sequence + 8 + 96; ++ f->ops.erase_block = sftl_test_native_erase; ++ f->native_erase_cut = cut; ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ apple_sftl_set_checkpoint_ops(sftl, &sftl_test_native_ops, f); ++ memset(data, 'W', APPLE_SFTL_CHUNK); ++ ret = apple_sftl_write(sftl, TEST_APPEND_BASE + 5, data, 1); ++ KUNIT_EXPECT_EQ(test, ret, cut ? -EIO : 0); ++ KUNIT_EXPECT_EQ(test, f->native_policy_calls, 1U); ++ KUNIT_EXPECT_EQ(test, f->erases, cut ? cut : 3U); ++ KUNIT_EXPECT_EQ(test, f->erased_context_lanes, cut == 1 ? 0 : (cut == 2 ? 1 : 3)); ++ KUNIT_EXPECT_EQ(test, f->loaded_before_retirement, true); ++ KUNIT_EXPECT_EQ(test, f->loaded_after_retirement, !cut || cut == 3); ++ if (cut) ++ KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 5, data, 1), -EIO); ++ apple_sftl_free(sftl); ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ apple_sftl_get_info(sftl, &info); ++ KUNIT_EXPECT_EQ(test, info.checkpoint_sequence, newest); ++ KUNIT_EXPECT_EQ(test, info.sequence, cut ? newest : newest + 4); ++ sftl_test_gc_relocated_data(test, sftl, data, cut ? 0 : 'W'); ++ if (cut == 2) { ++ KUNIT_EXPECT_EQ(test, info.read_only, true); ++ KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 5, data, 1), ++ -EROFS); ++ } ++ if (cut) ++ KUNIT_ASSERT_NOT_NULL(test, sftl_test_written(f, 0, 0, 0)); ++ else ++ KUNIT_EXPECT_PTR_EQ(test, sftl_test_written(f, 0, 0, 0), NULL); ++ KUNIT_EXPECT_EQ(test, f->erases, cut ? cut : 3U); ++ apple_sftl_free(sftl); ++ if (cut == 2) { ++ /* Residual data on the old lane must predate the new generation. ++ * A newer tag among its surviving records cannot be discarded. ++ */ ++ u8 *meta = f->checkpoint->records[4].meta; ++ ++ put_unaligned_le64((newest + 1) << 16 | 0x021f, meta); ++ KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &sftl), -EBADMSG); ++ KUNIT_EXPECT_PTR_EQ(test, sftl, NULL); ++ apple_sftl_free(sftl); ++ } ++ apple_sftl_checkpoint_free(f->checkpoint); ++ } ++} ++ ++static void sftl_test_gc_native_prefix_cuts(struct kunit *test) ++{ ++ struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); ++ u8 *data = kunit_kmalloc(test, APPLE_PPN_PAGE_SIZE, GFP_KERNEL); ++ struct sftl_test_page *saved[24]; ++ struct apple_sftl *sftl = NULL; ++ struct apple_sftl_info info; ++ unsigned int cut, programs; ++ u64 first; ++ ++ KUNIT_ASSERT_NOT_NULL(test, f); ++ KUNIT_ASSERT_NOT_NULL(test, data); ++ KUNIT_ASSERT_EQ(test, sftl_test_native_medium(f, data), 0); ++ f->ops.erase_block = sftl_test_native_erase; ++ f->native_erase_cut = 1; ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ apple_sftl_set_checkpoint_ops(sftl, &sftl_test_native_ops, f); ++ KUNIT_ASSERT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 5, data, 1), -EIO); ++ apple_sftl_free(sftl); ++ KUNIT_ASSERT_NOT_NULL(test, sftl_test_written(f, 2, 0, 23)); ++ memcpy(saved, f->written[2], sizeof(saved)); ++ first = f->checkpoint->sequence + 9; ++ programs = f->programs; ++ for (cut = 0; cut <= ARRAY_SIZE(saved); cut++) { ++ memcpy(f->written[2], saved, cut * sizeof(*saved)); ++ memset(f->written[2] + cut, 0, (ARRAY_SIZE(saved) - cut) * sizeof(*saved)); ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ apple_sftl_get_info(sftl, &info); ++ KUNIT_EXPECT_EQ(test, info.checkpoint_sequence, ++ cut == 24 ? first + 95 : f->checkpoint->sequence); ++ KUNIT_EXPECT_EQ(test, info.sequence, first - 1 + cut * 4); ++ if (cut && cut < 24) { ++ KUNIT_EXPECT_EQ(test, info.read_only, true); ++ KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 5, data, 1), -EROFS); ++ } ++ sftl_test_gc_relocated_data(test, sftl, data, 0); ++ KUNIT_EXPECT_EQ(test, f->programs, programs); ++ KUNIT_EXPECT_EQ(test, f->erases, 1U); ++ apple_sftl_free(sftl); ++ } ++ apple_sftl_checkpoint_free(f->checkpoint); ++} ++ ++static void sftl_test_gc_native_policy_refuses(struct kunit *test) ++{ ++ struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); ++ u8 *data = kunit_kmalloc(test, APPLE_PPN_PAGE_SIZE, GFP_KERNEL); ++ struct apple_sftl *sftl = NULL; ++ unsigned int programs; ++ ++ KUNIT_ASSERT_NOT_NULL(test, f); ++ KUNIT_ASSERT_NOT_NULL(test, data); ++ KUNIT_ASSERT_EQ(test, sftl_test_native_medium(f, data), 0); ++ f->ops.erase_block = sftl_test_native_erase; ++ f->native_policy_error = -EAGAIN; ++ programs = f->programs; ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ apple_sftl_set_checkpoint_ops(sftl, &sftl_test_native_ops, f); ++ KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 5, data, 1), -EAGAIN); ++ KUNIT_EXPECT_EQ(test, f->native_policy_calls, 1U); ++ f->native_policy_error = 0; ++ f->drop_context_role = true; ++ KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 5, data, 1), -EBADMSG); ++ KUNIT_EXPECT_EQ(test, f->native_policy_calls, 2U); ++ f->drop_context_role = false; ++ f->keep_donor_role = true; ++ KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 5, data, 1), -EBADMSG); ++ f->keep_donor_role = false; ++ f->duplicate_stream = true; ++ KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 5, data, 1), -EBADMSG); ++ KUNIT_EXPECT_EQ(test, f->native_policy_calls, 4U); ++ KUNIT_EXPECT_EQ(test, f->programs, programs); ++ KUNIT_EXPECT_EQ(test, f->erases, 0U); ++ sftl_test_gc_relocated_data(test, sftl, data, 0); ++ apple_sftl_free(sftl); ++ apple_sftl_checkpoint_free(f->checkpoint); ++} ++ ++/* An unnamed context or reserved spare has the same erased bytes as a free ++ * landing block. Its saved role still owns it, even in append-only mode. ++ */ ++static void sftl_test_native_blank_ownership(struct kunit *test) ++{ ++ static const struct { u8 role; u16 live; } cases[] = { ++ { 7, 0 }, { 8, 0 }, { 0xff, 0 }, { 4, 0 }, { 9, 0 }, { 2, 0 }, { 1, 1 }, ++ }; ++ struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); ++ u8 *data = kunit_kmalloc(test, APPLE_PPN_PAGE_SIZE, GFP_KERNEL); ++ struct apple_sftl *sftl = NULL; ++ unsigned int programs, i; ++ u8 *entry; ++ ++ KUNIT_ASSERT_NOT_NULL(test, f); ++ KUNIT_ASSERT_NOT_NULL(test, data); ++ KUNIT_ASSERT_EQ(test, sftl_test_native_medium(f, data), 0); ++ programs = f->programs; ++ entry = f->checkpoint->records[8].data + 6 * 8; ++ for (i = 0; i < ARRAY_SIZE(cases); i++) { ++ entry[0] = cases[i].role; ++ put_unaligned_le16(cases[i].live, entry + 2); ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 5, data, 1), ++ -ENOSPC); ++ KUNIT_EXPECT_EQ(test, f->programs, programs); ++ KUNIT_EXPECT_EQ(test, f->erases, 0U); ++ apple_sftl_free(sftl); ++ } ++ entry[0] = 1; ++ put_unaligned_le16(0, entry + 2); ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 5, data, 1), 0); ++ KUNIT_EXPECT_EQ(test, f->programs, programs + 1); ++ KUNIT_EXPECT_EQ(test, f->erases, 0U); ++ apple_sftl_free(sftl); ++ apple_sftl_checkpoint_free(f->checkpoint); ++} ++ ++static int sftl_test_erase_native_free(void *priv, unsigned int bus, unsigned int cau, ++ unsigned int block) ++{ ++ struct sftl_fixture *f = priv; ++ ++ f->erases++; ++ if (bus || cau || block != 5 || !f->written[0]) ++ return -EPERM; ++ memset(f->written[0], 0, 128 * sizeof(*f->written[0])); ++ return 0; ++} ++ ++/* Both recovered maps have released the old ordinary data. Direct erase ++ * still requires the native checkpoint to have retired its ownership. ++ */ ++static void sftl_test_native_retired_ownership(struct kunit *test) ++{ ++ static const struct { u8 role, flags; u16 live; } cases[] = { ++ { 3, 64, 0 }, { 3, 64, 1 }, { 7, 64, 0 }, { 8, 64, 0 }, ++ { 9, 8, 0 }, { 9, 64, 1 }, ++ }; ++ struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); ++ u8 *data = kunit_kmalloc(test, APPLE_PPN_PAGE_SIZE, GFP_KERNEL); ++ struct apple_sftl *sftl = NULL; ++ unsigned int programs, i; ++ u8 *entry; ++ ++ KUNIT_ASSERT_NOT_NULL(test, f); ++ KUNIT_ASSERT_NOT_NULL(test, data); ++ f->checkpoint_slot = true; ++ KUNIT_ASSERT_EQ(test, sftl_test_gc_medium(f, data, GC_CHECKPOINT_AFTER_OVERWRITE), 0); ++ KUNIT_ASSERT_EQ(test, sftl_test_native_context(f), 0); ++ f->ops.erase_block = sftl_test_erase_native_free; ++ programs = f->programs; ++ entry = f->checkpoint->records[8].data + 5 * 8; ++ for (i = 0; i < ARRAY_SIZE(cases); i++) { ++ entry[0] = cases[i].role; ++ entry[1] = cases[i].flags; ++ put_unaligned_le16(cases[i].live, entry + 2); ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 480, data, 1), ++ -ENOSPC); ++ KUNIT_EXPECT_EQ(test, f->programs, programs); ++ KUNIT_EXPECT_EQ(test, f->erases, 0U); ++ apple_sftl_free(sftl); ++ } ++ entry[0] = 9; ++ entry[1] = 64; ++ put_unaligned_le16(0, entry + 2); ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 480, data, 1), 0); ++ KUNIT_EXPECT_EQ(test, f->programs, programs + 1); ++ KUNIT_EXPECT_EQ(test, f->erases, 1U); ++ apple_sftl_free(sftl); ++ apple_sftl_checkpoint_free(f->checkpoint); ++} ++ + static void sftl_test_gc_retries_reservation(struct kunit *test) + { + struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); +@@ -1516,6 +2373,7 @@ static void sftl_test_gc_relocates_on_write(struct kunit *test) + { + struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); + struct apple_sftl *sftl = NULL, *cold = NULL; ++ struct apple_sftl_snapshot *before, *after; + u8 *data = kunit_kmalloc(test, APPLE_PPN_PAGE_SIZE, GFP_KERNEL); + struct sftl_test_page *page, *copy; + u8 saved[APPLE_PPN_OOB_SIZE]; +@@ -1523,7 +2381,18 @@ static void sftl_test_gc_relocates_on_write(struct kunit *test) + + KUNIT_ASSERT_NOT_NULL(test, f); + KUNIT_ASSERT_NOT_NULL(test, data); ++ f->sequence_bias = 0x20000ULL - (TEST_APPEND_FIRST + 479 + 4 + 6); + KUNIT_ASSERT_EQ(test, sftl_test_gc_live_medium(f, data), 0); ++ /* The five live donor chunks already came from a previous user epoch. */ ++ page = sftl_test_written(f, 0, 0, 120); ++ KUNIT_ASSERT_NOT_NULL(test, page); ++ KUNIT_ASSERT_EQ(test, get_unaligned_le32(page->data + 475 * 16 + 12), 5U); ++ put_unaligned_le32(0, page->data + 475 * 16 + 4); ++ for (i = 475; i < 480; i++) { ++ page = sftl_test_written(f, 0, 0, i / 4); ++ KUNIT_ASSERT_NOT_NULL(test, page); ++ put_unaligned_le32(0, page->oob + i % 4 * 16 + 12); ++ } + f->ops.erase_block = sftl_test_erase_block; + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); + apple_sftl_set_checkpoint_ops(sftl, &sftl_test_gc_ops, f); +@@ -1539,6 +2408,9 @@ static void sftl_test_gc_relocates_on_write(struct kunit *test) + KUNIT_ASSERT_NOT_NULL(test, page); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(page->oob + (slot % 4) * 16 + 8), + TEST_APPEND_BASE + i); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le32(page->oob + (slot % 4) * 16 + 12), 0U); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le32(page->oob + (slot % 4) * 16 + 4), ++ i < 2 ? 1U : 2U); + sftl_test_expect_fill(test, page->data + slot % 4 * 4096, 119 + i); + } + page = sftl_test_written(f, 1, 0, 2); +@@ -1553,6 +2425,18 @@ static void sftl_test_gc_relocates_on_write(struct kunit *test) + memset(data, 'Z', APPLE_PPN_PAGE_SIZE); + for (i = 3; i < TEST_APPEND_PROGRAMS; i++) + KUNIT_ASSERT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 4 * i, data, 4), 0); ++ /* All five sequential keys share one historical age across the weave carry. */ ++ page = sftl_test_written(f, 1, 0, 120); ++ KUNIT_ASSERT_NOT_NULL(test, page); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le32(page->data + 3 * 16), 3U); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le32(page->data + 3 * 16 + 4), 0U); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le32(page->data + 3 * 16 + 8), TEST_APPEND_BASE); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le32(page->data + 3 * 16 + 12), 5U); ++ KUNIT_ASSERT_EQ(test, get_unaligned_le32(page->data + 4 * 16 + 4), 2U); ++ put_unaligned_le32(3, page->data + 4 * 16 + 4); ++ KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &cold), -EBADMSG); ++ apple_sftl_free(cold); ++ put_unaligned_le32(2, page->data + 4 * 16 + 4); + page = sftl_test_written(f, 1, 0, 127); + KUNIT_ASSERT_NOT_NULL(test, page); + KUNIT_EXPECT_EQ(test, page->oob[0], (u8)0x1c); +@@ -1580,8 +2464,14 @@ static void sftl_test_gc_relocates_on_write(struct kunit *test) + KUNIT_ASSERT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 480, data, 1), 0); + KUNIT_ASSERT_NOT_NULL(test, sftl_test_written(f, 0, 0, 0)); + KUNIT_EXPECT_EQ(test, f->erases, 1U); ++ KUNIT_ASSERT_EQ(test, apple_sftl_snapshot(sftl, 65536, &before), 0); + apple_sftl_free(sftl); + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &cold), 0); ++ KUNIT_ASSERT_EQ(test, apple_sftl_snapshot(cold, 65536, &after), 0); ++ KUNIT_ASSERT_EQ(test, after->nr_runs, before->nr_runs); ++ KUNIT_EXPECT_MEMEQ(test, after->runs, before->runs, before->nr_runs * sizeof(*before->runs)); ++ apple_sftl_snapshot_free(after); ++ apple_sftl_snapshot_free(before); + sftl_test_gc_relocated_data(test, cold, data, 'W'); + KUNIT_ASSERT_EQ(test, apple_sftl_read(cold, TEST_APPEND_BASE + 480, data), 0); + sftl_test_expect_fill(test, data, 'Z'); +@@ -1610,6 +2500,8 @@ static void sftl_test_gc_relocation_stops_before_erase(struct kunit *test) + + KUNIT_ASSERT_NOT_NULL(test, f); + KUNIT_ASSERT_NOT_NULL(test, data); ++ /* Cold recovery must replay historical ages under the old checkpoint. */ ++ f->sequence_bias = 0x20000ULL - (TEST_APPEND_FIRST + 479 + 4 + 6); + KUNIT_ASSERT_EQ(test, sftl_test_gc_live_medium(f, data), 0); + f->ops.erase_block = sftl_test_erase_block; + f->commit = modes[mode].commit; +@@ -1633,6 +2525,14 @@ static void sftl_test_gc_relocation_stops_before_erase(struct kunit *test) + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); + sftl_test_gc_relocated_data(test, sftl, data, 0); + apple_sftl_free(sftl); ++ if (modes[mode].copy_error) { ++ struct sftl_test_page *page = sftl_test_written(f, 1, 0, 1); ++ ++ KUNIT_ASSERT_NOT_NULL(test, page); ++ put_unaligned_le32(3, page->oob + 12); ++ KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &sftl), -EBADMSG); ++ apple_sftl_free(sftl); ++ } + apple_sftl_checkpoint_free(f->checkpoint); + } + } +@@ -1673,6 +2573,14 @@ static void sftl_test_gc_preserves_last_sequences(struct kunit *test) + } + + static struct kunit_case apple_sftl_cases[] = { ++ KUNIT_CASE(sftl_test_rotate_mounted), ++ KUNIT_CASE(sftl_test_vfl_rotation_cuts), ++ KUNIT_CASE(sftl_test_vfl_rejects_ambiguous_tables), ++ KUNIT_CASE(sftl_test_native_blank_ownership), ++ KUNIT_CASE(sftl_test_native_retired_ownership), ++ KUNIT_CASE(sftl_test_gc_native_publication), ++ KUNIT_CASE(sftl_test_gc_native_prefix_cuts), ++ KUNIT_CASE(sftl_test_gc_native_policy_refuses), + KUNIT_CASE(sftl_test_gc_retries_reservation), + KUNIT_CASE(sftl_test_gc_preserves_last_sequences), + KUNIT_CASE(sftl_test_gc_relocates_on_write), +@@ -1697,6 +2605,12 @@ static struct kunit_case apple_sftl_cases[] = { + KUNIT_CASE(sftl_test_append_rejects_invalid_window), + KUNIT_CASE(sftl_test_complete_medium), + KUNIT_CASE(sftl_test_checkpoint_cold_roundtrip), ++ KUNIT_CASE(sftl_test_context_power_cuts), ++ KUNIT_CASE(sftl_test_context_stage_failures), ++ KUNIT_CASE(sftl_test_checkpoint_stage_protects_tail), ++ KUNIT_CASE(sftl_test_context_pins_fallback_blocks), ++ KUNIT_CASE(sftl_test_context_torn_append_replay), ++ KUNIT_CASE(sftl_test_reject_corrupt_generation), + KUNIT_CASE(sftl_test_scan_after_open_lane_hole), + KUNIT_CASE(sftl_test_runtime_failure_stops_reads), + KUNIT_CASE(sftl_test_reject_incomplete_or_ambiguous), +diff --git a/drivers/mtd/apple-sftl-vfl.c b/drivers/mtd/apple-sftl-vfl.c +index 9ab992bfe..744ac57e3 100644 +--- a/drivers/mtd/apple-sftl-vfl.c ++++ b/drivers/mtd/apple-sftl-vfl.c +@@ -235,14 +235,18 @@ static int vfl_program(struct vfl_rotation *r, unsigned int page, struct vfl_pag + return 0; + } + +-int apple_sftl_vfl_rotate(struct apple_ppn *ppn, const struct apple_sftl_vfl_source *s, +- const struct apple_sftl_vfl_ops *ops, void *priv) ++int apple_sftl_vfl_rotate_checked(struct apple_ppn *ppn, const struct apple_sftl_vfl_source *s, ++ const struct apple_sftl_vfl_ops *ops, void *priv, ++ bool *started) + { + struct vfl_rotation *r; + u32 age; + unsigned int lane; + int ret; + ++ if (!started) ++ return -EINVAL; ++ *started = false; + if (!ppn || !ppn->ops || !ppn->ops->read_page || !s || + !ops || !ops->erase || !ops->program) + return -EOPNOTSUPP; +@@ -262,8 +266,10 @@ int apple_sftl_vfl_rotate(struct apple_ppn *ppn, const struct apple_sftl_vfl_sou + ret = vfl_source_pages(r); + if (!ret) + ret = vfl_check_target(r); +- if (!ret) ++ if (!ret) { ++ *started = true; + ret = vfl_erase_target(r); ++ } + if (!ret) + ret = vfl_program(r, 0, &r->checkpoint, VFL_GLOBAL, age); + for (lane = 0; !ret && lane < VFL_LANES; lane++) { +@@ -279,4 +285,12 @@ int apple_sftl_vfl_rotate(struct apple_ppn *ppn, const struct apple_sftl_vfl_sou + kvfree(r); + return ret; + } ++ ++int apple_sftl_vfl_rotate(struct apple_ppn *ppn, const struct apple_sftl_vfl_source *s, ++ const struct apple_sftl_vfl_ops *ops, void *priv) ++{ ++ bool started; ++ ++ return apple_sftl_vfl_rotate_checked(ppn, s, ops, priv, &started); ++} + EXPORT_SYMBOL_GPL(apple_sftl_vfl_rotate); +diff --git a/drivers/mtd/apple-sftl-vfl.h b/drivers/mtd/apple-sftl-vfl.h +index 58a62d7f2..2cf5d264a 100644 +--- a/drivers/mtd/apple-sftl-vfl.h ++++ b/drivers/mtd/apple-sftl-vfl.h +@@ -32,6 +32,14 @@ struct apple_sftl_vfl_ops { + u8 page, const u8 *data, const u8 *oob); + }; + ++/* A mounted caller uses this entry point to serialize and revalidate its state. */ ++int apple_sftl_rotate_vfl(struct apple_sftl *sftl, const struct apple_sftl_vfl_ops *ops, ++ void *priv); ++ ++int apple_sftl_vfl_rotate_checked(struct apple_ppn *ppn, ++ const struct apple_sftl_vfl_source *source, ++ const struct apple_sftl_vfl_ops *ops, void *priv, ++ bool *started); + int apple_sftl_vfl_rotate(struct apple_ppn *ppn, + const struct apple_sftl_vfl_source *source, + const struct apple_sftl_vfl_ops *ops, void *priv); +diff --git a/drivers/mtd/apple-sftl.h b/drivers/mtd/apple-sftl.h +index e8c9ffbf9..141a00966 100644 +--- a/drivers/mtd/apple-sftl.h ++++ b/drivers/mtd/apple-sftl.h +@@ -5,15 +5,22 @@ + #include + #include + ++#include "apple-sftl-reclaim.h" ++ + struct apple_sftl; +-struct apple_sftl_checkpoint; + +-/* Internal checkpoint backend. reserve selects owned context blocks without +- * modifying media. install atomically publishes a native checkpoint, retires +- * the previous fallback, and returns independently read, caller-owned bytes. +- * An error may be indeterminate. The caller stops the instance on failure. ++/* ++ * The native policy completes a planned accounting image in memory. The ++ * core owns all I/O, verification and retirement of the old generation. ++ * A compact-format backend can provide its own atomic reserve/install pair. ++ * install returns independently read, caller-owned bytes. On an ambiguous ++ * mutation the writer stops until a fresh recovery. + */ + struct apple_sftl_checkpoint_ops { ++ /* With this policy the core stages and retires real context pages. ++ * It is exclusive with the externally atomic reserve/install pair. ++ */ ++ apple_sftl_prepare_accounting prepare_native; + int (*reserve)(void *priv, u32 *blocks, u32 *nr_blocks); + int (*install)(void *priv, const struct apple_sftl_checkpoint *image, + struct apple_sftl_checkpoint **readback); +@@ -23,6 +30,7 @@ void apple_sftl_set_checkpoint_ops(struct apple_sftl *sftl, + const struct apple_sftl_checkpoint_ops *ops, void *priv); + + struct apple_sftl_info { ++ bool read_only; + u32 logical_pages; + u32 disk_pages; + u32 nr_runs; From 159ece0608cedd2e01fa869dfa6118f78d895b37 Mon Sep 17 00:00:00 2001 From: Shayne <8741557+shaynelegault@users.noreply.github.com> Date: Thu, 19 Nov 2026 08:22:14 -0600 Subject: [PATCH 04/11] P105: verify native reclamation accounting against 13G36 firmware Export the native provider counters, free-block and wear transitions, fresh context allocation and checkpoint publication order. Record the firmware replay, the integrity checks for unlisted context sites and the remaining physical qualification requirements for erase and reuse. Assisted-by: LLM --- chips/s5l8942x.toml | 2 +- devices/p105/device.toml | 5 +- docs/bring-up/p105-sftl-checkpoint-gc.md | 103 +- ...ccount-for-native-flash-provider-eve.patch | 452 ++++ ...ollow-native-allocation-and-checkpoi.patch | 2328 +++++++++++++++++ 5 files changed, 2852 insertions(+), 38 deletions(-) create mode 100644 kernel/patches/armv7/mtd/0014-mtd-apple-sftl-Account-for-native-flash-provider-eve.patch create mode 100644 kernel/patches/armv7/mtd/0015-mtd-apple-sftl-Follow-native-allocation-and-checkpoi.patch diff --git a/chips/s5l8942x.toml b/chips/s5l8942x.toml index 2d28a6ca..5a9159f9 100644 --- a/chips/s5l8942x.toml +++ b/chips/s5l8942x.toml @@ -17,5 +17,5 @@ stages = "Project Cascadia P105 boot chain" reference_version = "Project Cascadia builds its A5 boot chain from 8.4.1 / 12H321" nand_reads = "Measured P105 64 GB PPN geometry has eight CAUs per bus, 2088 blocks per CAU and 128 normal pages per block; exact ARMv7 read-topic qualification is pending (docs/bring-up/p105-nand-reads.md)." nand_program = "The H2FMI driver programs complete PPN pages only into erased destinations, verifies full readback and leaves physical erase disabled pending qualification (docs/bring-up/p105-nand-program.md)." -nand_checkpoint_gc = "The 64 GB SwissPPNFTL reader recovers supported torn checkpoints read-only. Native-format saves and healthy VFL rotation are verified against synthetic and captured media. P105 accounting and physical erase qualification remain open (docs/bring-up/p105-sftl-checkpoint-gc.md)." +nand_checkpoint_gc = "The 64 GB SwissPPNFTL writer and native firmware agree on healthy allocation, wear and both provider statistics records in offline replay. Missing context evidence permits read-only recovery. Physical P105 accounting inputs and erase/reuse await qualification (docs/bring-up/p105-sftl-checkpoint-gc.md)." usb_phy = "P105 uses a 24 MHz reference; the proposed S5L8942X cold-handoff callbacks await an exact-revision boot (docs/bring-up/p105-usb-handoff.md)." diff --git a/devices/p105/device.toml b/devices/p105/device.toml index 753a23ba..3e9c79eb 100644 --- a/devices/p105/device.toml +++ b/devices/p105/device.toml @@ -29,8 +29,9 @@ recipe = "firmware.json" # (OrchardLinux/orchard#93), freed space is never reused and writes fail once # the blank flash runs out. Drop this table when it lands; the installer # warns from it and allows the install either way. -# Torn native checkpoints can recover read-only. P105 checkpoint writes and -# physical erase remain disabled (docs/bring-up/p105-sftl-checkpoint-gc.md). +# Native checkpoint accounting was replayed offline. The physical P105 +# provider still needs qualified events and erase outcomes before reuse +# (docs/bring-up/p105-sftl-checkpoint-gc.md). [storage] reclaims = false diff --git a/docs/bring-up/p105-sftl-checkpoint-gc.md b/docs/bring-up/p105-sftl-checkpoint-gc.md index 5b6fce87..3eb62beb 100644 --- a/docs/bring-up/p105-sftl-checkpoint-gc.md +++ b/docs/bring-up/p105-sftl-checkpoint-gc.md @@ -44,35 +44,54 @@ indeterminate erase or failed verification stops the writer instance. ## Checkpoint publication and recovery -The two-block SwissPPNFTL save and VFL bank rotation operate on different -parts of the medium. +SwissPPNFTL saves allocate a fresh normal-view context group. The separate +VFL rotation uses two replicated SLC banks. | Operation | Write order | Recovery evidence | | --- | --- | --- | -| SwissPPNFTL checkpoint | Write the named blank alternate in normal view, verify each page, reload with both generations visible, retire the old context, reload again, then erase the data donor. | The native base and accounting prefix contains 32 records. The map is padded in batches of 64 records. Coverage of the last logical key can occur many pages before the save finishes. | +| SwissPPNFTL checkpoint | Reserve fresh sites, copy live data, write the base page, sample the two providers, then write the statistics and remaining context pages. Reload with the old generation present, retire its occupied site, release unused new capacity, reload and verify the donor erase. | The base names the new generation's capacity. Old occupied sites are discovered independently. The native accounting prefix has 32 records and map batches have 64. | | Healthy VFL rotation | Erase the four target SLC members, save a checkpoint referencing the source tables, copy all 16 lane tables, then save the final checkpoint with the target references. | Each page has four replicas. The source bank can contain a table update after its last complete checkpoint. The executor checks that tail before changing the target. | Recovery from an incomplete native batch preserves confirmed journal writes and exposes the disk read-only when the next safe sequence is uncertain. -If retirement stops between lanes, the new generation must name the old -context site and every surviving record there must predate the selected -generation. Conflicting references, missing live records or unsupported -geometry can still prevent a mount. - -The encoder derives per-superblock valid logical-page counts and the global -valid-page statistic from the whole map. These values matched every one of -the 2,086 superblocks in the archived native checkpoint. The writer also +For a saved context-role site outside the selected group, recovery checks +the physical pages, lane order, record tags, sequence and map ownership. +Incomplete evidence can yield a read-only mount. Conflicting metadata or +references into a reserved site can prevent recovery. + +The writer derives the valid-page count of each of the 2,086 superblocks +from the whole map. A native save preserves the cached global counts until +the observed firmware statistics refresh. The writer also rebuilds the per-block user sequence statistics from closed footers. A live page copy receives a new physical write sequence and keeps the high 32 bits of the original user sequence in its tag. The tests supply the remaining allocation policy for synthetic media. The P105 driver keeps native checkpoint writes disabled. +## Native allocation, wear and counters + +The healthy 13G36 allocator compares wear across clean and dirty free +blocks. When the clean pool is below six, an allocation can also erase +other low-wear dirty blocks to replenish it. After the last valid page +moves, the closed donor changes from class 3 to dirty-free class 9 and its +read count clears. A successful later erase changes it to clean-free +class 1 and increments its 16-bit wear value. Native firmware also +increments wear when it releases an unused new context reservation, +without issuing an erase for that site. The driver declines an ambiguous +or overflowing transition before reuse. + +The native base program completes before the two getters run. ID 255 +contains 88 bytes of VFL statistics. ID 254 contains 464 bytes from +lower-provider requests and completions. Both are sampled before the +statistics page is programmed. The number of physical rows, command +batching and completion flags all matter. A post-save statistics refresh +updates cached global counts separately. + ## Source and verification The patches in `kernel/patches/armv7/mtd/` apply to the merged [ARMv7 append writer](https://github.com/OrchardLinux/linux/commit/0c6503279c702b44a340c37f072cd9170d6bf4a4). -The [kernel source](https://github.com/shaynelegault/linux/commit/64e248acc722bbf6a1171caf07d3858853de6ad3) +The [kernel source](https://github.com/shaynelegault/linux/commit/e3684d2ab4c71fbef0907f3e584e07af09b59744) contains the synthetic KUnit fixtures. - A normal write moved five live records, programmed a 24-page native-format @@ -81,11 +100,14 @@ contains the synthetic KUnit fixtures. save boundaries, readback faults, and power cuts between the old context's lane erases. - The C reader replayed captured device pages through journal sequence - 187831212. All 15,625,016 keys agreed with the archived map. All 95 - incomplete prefixes of a 96-record second generation preserved the map, - including interruptions inside pages and during map padding. - Erasing 1–15 of the 16 old lanes preserved the new map read-only. A fully - retired old context permitted healthy recovery. + 187831212. All 15,625,016 keys agreed with the archived map. Of the 2,048 + pages at unnamed context-role block 601, only two are in the archive. + The reader exposes that unmodified capture read-only. +- A separate RAM overlay supplied erased values for the 2,046 uncaptured + pages on each of blocks 594, 601 and 636. With that assumption, all 95 + incomplete prefixes of a new 96-record save and all 15 partial + retirements preserved the full map. The overlay started with all + captured pages intact. - Replaying the recorded healthy VFL rotation reproduced all 72 target replica pages, including their out-of-band metadata. All 256 source pages remained intact. Synthetic tests also interrupted the operation after @@ -93,9 +115,28 @@ contains the synthetic KUnit fixtures. - The reader rejects a historical 283-FTL-superblock checkpoint for this 2,086-superblock volume. The native loader and setter treat this field as the size of the FTL address space. -- All 59 host sanitizer cases passed, along with 209 range-map round trips, - 66 relocation cases and 24 native metadata vectors. The ARMv7 object - checks covered 17 configurations. +- Independent execution of the storage firmware matched the C allocation + planner on 600 generated block tables. On 341 captured closed donors the + two implementations agreed on donor state, wear and the separate cached + statistics refresh. +- The firmware and C implementation agreed on all 552 provider bytes at + 426 comparison points, covering the archived initial counters, a + synthetic initial state and integer rollover. +- In a separate end-to-end replay, the original 13G36 foreground GC chose + donor 3 from two eligible blocks, read its footer, and moved keys + 101–104 to block 32. The verifier compared all 16 KiB of data and 64 + out-of-band bytes at the lower-provider write boundary. Native firmware + updated the map, finalized the donor, allocated context sites 33 and 34, + and serialized the statistics after writing the base page. It erased old + context 17, released unused site 34, and subsequently erased the donor. + The Linux event accounting matched the native getters byte-for-byte. + The initial state and successful NAND responses were supplied in RAM. +- The replay used the original ARM storage code from build 13G36, SHA-256 + `fcc7fa01fa80c03ca4de026ce01e8fb2b131abd60fffa1f498e5b901d34e78be`. + It entered foreground GC at `0x81090320`; the checkpoint getters at + `0x810a048c` and `0x8108ac18` supplied the independent reference bytes. +- All 72 host sanitizer cases, 209 map round trips and 66 relocation + differentials passed. Twenty ARMv7 object configurations compiled. The checkpoint metadata and footer evidence comes from IOFlashStorage-730 on build 13G36 and the [pinned Cascadia NAND research](https://github.com/rzxvx/Project-Cascadia/blob/c20b85ebb951c704d7e3a664c378dde8276e3928/docs/research/p105-nand.md). @@ -104,21 +145,13 @@ document the starting evidence. ## Remaining before hardware enablement -The remaining software work is to account for allocation of the landing -block, open write streams, wear and the timing of native counter updates. -On all 341 qualifying captured closed donors, native finalization changes -class 3 to class 9 and clears the read count. After a statistics refresh, -the free count increases and the data count decreases. The native writer -can save the previously cached counters before that refresh. Applying the -donor delta directly to the old checkpoint could therefore publish the -wrong counts. Each save also requests two statistics payloads from the -native provider. ID 255 reads 88 bytes of VFL state (13G36 handler -`0x810a048c`), and ID 254 reads 464 bytes of lower-provider state -(`0x8108ac18`). The archive contains one version of this pair. A pair of -checkpoints bracketing a known native relocation, or an independently -validated replay of those provider events, would allow a byte-for-byte -comparison of the resulting state. Scrub and bad-member replacement also -need qualification. +The physical provider needs a trustworthy starting state and a continuous +record of native-equivalent reads, programs, erases, synchronizations and +their completion flags. One saved counter sample plus the logical journal +cannot reconstruct that history. Replaying two plausible clean-pool +histories selected the same observed landing blocks, 654 and 661, and +issued different maintenance erases. Losing that history invalidates the +counter model. Scrub and bad-member replacement also need qualification. Physical qualification must establish the exact erase footprint in both normal and SLC views, exclusive ownership across raw MTD and SFTL operations, diff --git a/kernel/patches/armv7/mtd/0014-mtd-apple-sftl-Account-for-native-flash-provider-eve.patch b/kernel/patches/armv7/mtd/0014-mtd-apple-sftl-Account-for-native-flash-provider-eve.patch new file mode 100644 index 00000000..d8547827 --- /dev/null +++ b/kernel/patches/armv7/mtd/0014-mtd-apple-sftl-Account-for-native-flash-provider-eve.patch @@ -0,0 +1,452 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Shayne <8741557+shaynelegault@users.noreply.github.com> +Date: Thu, 19 Nov 2026 08:15:35 -0600 +Subject: [PATCH] mtd: apple-sftl: Account for native flash provider events + +Reproduce the measured VFL and lower-provider counters from an explicit +initial state and a complete stream of requests and completions. Track +row counts, scatter requests, single-block erase and synchronization. +Refuse a statistics sample when its event history is incomplete. + +Exercise both buffers at their native widths, including counter rollover. +Keep physical provider registration disabled. + +Assisted-by: LLM +--- + drivers/mtd/Makefile | 4 +- + drivers/mtd/apple-sftl-provider-test.c | 143 +++++++++++++++++ + drivers/mtd/apple-sftl-provider.c | 204 +++++++++++++++++++++++++ + drivers/mtd/apple-sftl-provider.h | 49 ++++++ + 4 files changed, 398 insertions(+), 2 deletions(-) + create mode 100644 drivers/mtd/apple-sftl-provider-test.c + create mode 100644 drivers/mtd/apple-sftl-provider.c + create mode 100644 drivers/mtd/apple-sftl-provider.h + +diff --git a/drivers/mtd/Makefile b/drivers/mtd/Makefile +index 3acbdbac5..227eac233 100644 +--- a/drivers/mtd/Makefile ++++ b/drivers/mtd/Makefile +@@ -33,7 +33,7 @@ obj-$(CONFIG_MTD_UBI) += ubi/ + obj-$(CONFIG_MTD_HYPERBUS) += hyperbus/ + + obj-$(CONFIG_MTD_APPLE_SFTL) += apple-sftl.o +-apple-sftl-y := apple-sftl-core.o apple-sftl-block.o apple-sftl-reclaim.o apple-sftl-account.o apple-sftl-vfl.o ++apple-sftl-y := apple-sftl-core.o apple-sftl-block.o apple-sftl-reclaim.o apple-sftl-account.o apple-sftl-provider.o apple-sftl-vfl.o + obj-$(CONFIG_MTD_APPLE_SFTL_KUNIT_TEST) += apple-sftl-test.o + obj-$(CONFIG_MTD_APPLE_SFTL_KUNIT_TEST) += apple-sftl-reclaim-test.o +-obj-$(CONFIG_MTD_APPLE_SFTL_KUNIT_TEST) += apple-sftl-account-test.o apple-sftl-vfl-test.o ++obj-$(CONFIG_MTD_APPLE_SFTL_KUNIT_TEST) += apple-sftl-account-test.o apple-sftl-provider-test.o apple-sftl-vfl-test.o +diff --git a/drivers/mtd/apple-sftl-provider-test.c b/drivers/mtd/apple-sftl-provider-test.c +new file mode 100644 +index 000000000..a033aacdb +--- /dev/null ++++ b/drivers/mtd/apple-sftl-provider-test.c +@@ -0,0 +1,143 @@ ++// SPDX-License-Identifier: GPL-2.0-only ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#include "apple-sftl-provider.h" ++ ++struct provider_fixture { ++ struct apple_sftl_provider_stats stats; ++ u8 vfl[88]; ++ u8 lower[464]; ++ u8 output[464]; ++}; ++ ++static struct provider_fixture *provider_fixture(struct kunit *test) ++{ ++ return kunit_kzalloc(test, sizeof(struct provider_fixture), GFP_KERNEL); ++} ++ ++static void provider_tlv_snapshot(struct kunit *test) ++{ ++ struct provider_fixture *f = provider_fixture(test); ++ ++ KUNIT_ASSERT_NOT_NULL(test, f); ++ KUNIT_EXPECT_EQ(test, apple_sftl_provider_read_stats(&f->stats, 254, f->output, 464), ++ -ENODATA); ++ memset(f->vfl, 0xa5, sizeof(f->vfl)); ++ memset(f->lower, 0x5a, sizeof(f->lower)); ++ KUNIT_ASSERT_EQ(test, apple_sftl_provider_seed(&f->stats, f->vfl, 88, f->lower, 464), 0); ++ KUNIT_EXPECT_EQ(test, apple_sftl_provider_read_stats(&f->stats, 255, f->output, 88), 88); ++ KUNIT_EXPECT_MEMEQ(test, f->output, f->vfl, 88); ++ KUNIT_EXPECT_EQ(test, apple_sftl_provider_read_stats(&f->stats, 254, f->output, 464), 464); ++ KUNIT_EXPECT_MEMEQ(test, f->output, f->lower, 464); ++ KUNIT_EXPECT_EQ(test, apple_sftl_provider_read_stats(&f->stats, 254, f->output, 88), ++ -EINVAL); ++ KUNIT_EXPECT_EQ(test, apple_sftl_provider_read_stats(&f->stats, 252, f->output, 464), ++ -EINVAL); ++ KUNIT_EXPECT_EQ(test, apple_sftl_provider_submit(&f->stats, 99, 1), -EOPNOTSUPP); ++ KUNIT_EXPECT_EQ(test, apple_sftl_provider_read_stats(&f->stats, 254, f->output, 464), ++ -ENODATA); ++ KUNIT_ASSERT_EQ(test, apple_sftl_provider_seed(&f->stats, f->vfl, 88, f->lower, 464), 0); ++ apple_sftl_provider_lost(&f->stats); ++ KUNIT_EXPECT_EQ(test, apple_sftl_provider_vfl(&f->stats, APPLE_SFTL_VFL_PROGRAM, 4), ++ -ENODATA); ++ KUNIT_EXPECT_EQ(test, apple_sftl_provider_seed(&f->stats, f->vfl, 88, f->lower, 463), ++ -EINVAL); ++} ++ ++static void provider_vfl_counts(struct kunit *test) ++{ ++ struct provider_fixture *f = provider_fixture(test); ++ ++ KUNIT_ASSERT_NOT_NULL(test, f); ++ put_unaligned_le64(0xfffffffffffffffcULL, f->vfl); ++ KUNIT_ASSERT_EQ(test, apple_sftl_provider_seed(&f->stats, f->vfl, 88, f->lower, 464), 0); ++ KUNIT_ASSERT_EQ(test, apple_sftl_provider_vfl(&f->stats, APPLE_SFTL_VFL_PROGRAM, 8), 0); ++ KUNIT_ASSERT_EQ(test, apple_sftl_provider_vfl(&f->stats, APPLE_SFTL_VFL_READ, 19), 0); ++ KUNIT_ASSERT_EQ(test, apple_sftl_provider_vfl(&f->stats, APPLE_SFTL_VFL_ERASE, 0), 0); ++ KUNIT_ASSERT_EQ(test, apple_sftl_provider_vfl(&f->stats, APPLE_SFTL_VFL_SCATTER_BEGIN, 0), 0); ++ KUNIT_ASSERT_EQ(test, apple_sftl_provider_vfl(&f->stats, APPLE_SFTL_VFL_SCATTER_ADD, 0), 0); ++ KUNIT_ASSERT_EQ(test, apple_sftl_provider_vfl(&f->stats, APPLE_SFTL_VFL_SCATTER_ADD, 0), 0); ++ KUNIT_ASSERT_EQ(test, apple_sftl_provider_vfl(&f->stats, APPLE_SFTL_VFL_SCATTER_FLUSH, 13), 0); ++ put_unaligned_le64(4, f->vfl); ++ put_unaligned_le64(32, f->vfl + 8); ++ put_unaligned_le64(1, f->vfl + 16); ++ put_unaligned_le64(1, f->vfl + 24); ++ put_unaligned_le64(1, f->vfl + 40); ++ put_unaligned_le64(2, f->vfl + 48); ++ put_unaligned_le64(1, f->vfl + 56); ++ put_unaligned_le64(1, f->vfl + 64); ++ put_unaligned_le64(1, f->vfl + 72); ++ KUNIT_EXPECT_MEMEQ(test, f->stats.vfl, f->vfl, sizeof(f->vfl)); ++ KUNIT_EXPECT_MEMEQ(test, f->stats.lower, f->lower, sizeof(f->lower)); ++} ++ ++static void provider_lower_results(struct kunit *test) ++{ ++ const u8 flags[] = { 0, 0x1f, 1, 0x10 }; ++ struct provider_fixture *f = provider_fixture(test); ++ ++ KUNIT_ASSERT_NOT_NULL(test, f); ++ put_unaligned_le64(0xffffffffULL, f->lower + 8); ++ KUNIT_ASSERT_EQ(test, apple_sftl_provider_seed(&f->stats, f->vfl, 88, f->lower, 464), 0); ++ KUNIT_ASSERT_EQ(test, apple_sftl_provider_submit(&f->stats, APPLE_SFTL_LOWER_READ, 4), 0); ++ put_unaligned_le64(0x100000003ULL, f->lower + 8); ++ KUNIT_EXPECT_MEMEQ(test, f->stats.lower, f->lower, sizeof(f->lower)); ++ KUNIT_ASSERT_EQ(test, apple_sftl_provider_complete(&f->stats, APPLE_SFTL_LOWER_READ, ++ 0x40, NULL, 4), 0); ++ KUNIT_EXPECT_MEMEQ(test, f->stats.lower, f->lower, sizeof(f->lower)); ++ KUNIT_ASSERT_EQ(test, apple_sftl_provider_complete(&f->stats, APPLE_SFTL_LOWER_READ, ++ 0, flags, ARRAY_SIZE(flags)), 0); ++ put_unaligned_le64(1, f->lower + 120); ++ put_unaligned_le64(1, f->lower + 128); ++ put_unaligned_le64(2, f->lower + 136); ++ put_unaligned_le64(1, f->lower + 448); ++ put_unaligned_le64(1, f->lower + 456); ++ KUNIT_EXPECT_MEMEQ(test, f->stats.lower, f->lower, sizeof(f->lower)); ++ ++ KUNIT_ASSERT_EQ(test, apple_sftl_provider_submit(&f->stats, APPLE_SFTL_LOWER_PROGRAM, 4), 0); ++ KUNIT_ASSERT_EQ(test, apple_sftl_provider_complete(&f->stats, APPLE_SFTL_LOWER_PROGRAM, ++ 0, flags, ARRAY_SIZE(flags)), 0); ++ put_unaligned_le64(4, f->lower); ++ put_unaligned_le64(2, f->lower + 144); ++ put_unaligned_le64(2, f->lower + 152); ++ KUNIT_ASSERT_EQ(test, apple_sftl_provider_submit(&f->stats, APPLE_SFTL_LOWER_ERASE, 4), 0); ++ KUNIT_ASSERT_EQ(test, apple_sftl_provider_complete(&f->stats, APPLE_SFTL_LOWER_ERASE, ++ 0, flags, ARRAY_SIZE(flags)), 0); ++ put_unaligned_le64(4, f->lower + 16); ++ put_unaligned_le64(2, f->lower + 160); ++ put_unaligned_le64(2, f->lower + 168); ++ KUNIT_EXPECT_MEMEQ(test, f->stats.lower, f->lower, sizeof(f->lower)); ++ ++ KUNIT_ASSERT_EQ(test, apple_sftl_provider_submit(&f->stats, APPLE_SFTL_LOWER_SINGLE_ERASE, 1), 0); ++ KUNIT_ASSERT_EQ(test, apple_sftl_provider_single_erase_done(&f->stats, true), 0); ++ KUNIT_ASSERT_EQ(test, apple_sftl_provider_submit(&f->stats, APPLE_SFTL_LOWER_SYNC, 1), 0); ++ put_unaligned_le64(5, f->lower + 16); ++ put_unaligned_le64(1, f->lower + 24); ++ put_unaligned_le64(1, f->lower + 104); ++ put_unaligned_le64(3, f->lower + 168); ++ KUNIT_EXPECT_MEMEQ(test, f->stats.lower, f->lower, sizeof(f->lower)); ++ KUNIT_EXPECT_MEMEQ(test, f->stats.vfl, f->vfl, sizeof(f->vfl)); ++ KUNIT_EXPECT_EQ(test, apple_sftl_provider_complete(&f->stats, APPLE_SFTL_LOWER_ERASE, 0, ++ NULL, 4), -EOPNOTSUPP); ++ KUNIT_EXPECT_EQ(test, apple_sftl_provider_read_stats(&f->stats, 254, f->output, 464), ++ -ENODATA); ++} ++ ++static struct kunit_case provider_cases[] = { ++ KUNIT_CASE(provider_tlv_snapshot), ++ KUNIT_CASE(provider_vfl_counts), ++ KUNIT_CASE(provider_lower_results), ++ {} ++}; ++ ++static struct kunit_suite provider_suite = { ++ .name = "apple-sftl-provider", ++ .test_cases = provider_cases, ++}; ++kunit_test_suite(provider_suite); ++MODULE_LICENSE("GPL"); +diff --git a/drivers/mtd/apple-sftl-provider.c b/drivers/mtd/apple-sftl-provider.c +new file mode 100644 +index 000000000..5fcb0eae1 +--- /dev/null ++++ b/drivers/mtd/apple-sftl-provider.c +@@ -0,0 +1,204 @@ ++// SPDX-License-Identifier: GPL-2.0-only ++/* Replay the native 13G36 provider accounting from an observed checkpoint. */ ++#include ++#include ++#include ++#include ++#include ++ ++#include "apple-sftl-provider.h" ++ ++static void add_count(u8 *record, size_t offset, u32 count) ++{ ++ u8 *field = record + offset; ++ ++ put_unaligned_le64(get_unaligned_le64(field) + count, field); ++} ++ ++void apple_sftl_provider_lost(struct apple_sftl_provider_stats *stats) ++{ ++ stats->valid = false; ++} ++EXPORT_SYMBOL_GPL(apple_sftl_provider_lost); ++ ++static int unknown_event(struct apple_sftl_provider_stats *stats) ++{ ++ apple_sftl_provider_lost(stats); ++ return -EOPNOTSUPP; ++} ++ ++int apple_sftl_provider_seed(struct apple_sftl_provider_stats *stats, const u8 *vfl, ++ size_t vfl_bytes, const u8 *lower, size_t lower_bytes) ++{ ++ if (!vfl || !lower || vfl_bytes != sizeof(stats->vfl) || ++ lower_bytes != sizeof(stats->lower)) { ++ apple_sftl_provider_lost(stats); ++ return -EINVAL; ++ } ++ memcpy(stats->vfl, vfl, vfl_bytes); ++ memcpy(stats->lower, lower, lower_bytes); ++ stats->valid = true; ++ return 0; ++} ++EXPORT_SYMBOL_GPL(apple_sftl_provider_seed); ++ ++/* The argument is the number of logical 4 KiB chunks in a native program, ++ * contiguous read or scatter flush. The other events count one call. ++ * Scatter flush receives the group's actual sum of 16-bit element widths. ++ */ ++int apple_sftl_provider_vfl(struct apple_sftl_provider_stats *stats, ++ enum apple_sftl_vfl_event event, u32 chunks) ++{ ++ u32 calls; ++ ++ if (!stats->valid) ++ return -ENODATA; ++ ++ switch (event) { ++ case APPLE_SFTL_VFL_PROGRAM: ++ if (!chunks || chunks % 4) ++ return unknown_event(stats); ++ add_count(stats->vfl, 0, chunks); ++ calls = 64; ++ break; ++ case APPLE_SFTL_VFL_READ: ++ add_count(stats->vfl, 8, chunks); ++ calls = 24; ++ break; ++ case APPLE_SFTL_VFL_SCATTER_FLUSH: ++ add_count(stats->vfl, 8, chunks); ++ calls = 56; ++ break; ++ case APPLE_SFTL_VFL_ERASE: ++ if (chunks) ++ return unknown_event(stats); ++ add_count(stats->vfl, 16, 1); ++ calls = 72; ++ break; ++ case APPLE_SFTL_VFL_SCATTER_BEGIN: ++ case APPLE_SFTL_VFL_SCATTER_ADD: ++ if (chunks) ++ return unknown_event(stats); ++ calls = event == APPLE_SFTL_VFL_SCATTER_BEGIN ? 40 : 48; ++ break; ++ default: ++ return unknown_event(stats); ++ } ++ add_count(stats->vfl, calls, 1); ++ return 0; ++} ++EXPORT_SYMBOL_GPL(apple_sftl_provider_vfl); ++ ++/* Batch dispatch (0x8108b278) charges rows before entering the physical ++ * provider. Single-row erase (0x8108aba4) and sync (0x8108ad6c) take ++ * separate native entry points. ++ */ ++int apple_sftl_provider_submit(struct apple_sftl_provider_stats *stats, ++ enum apple_sftl_lower_op op, u16 rows) ++{ ++ u32 offset; ++ ++ if (!stats->valid) ++ return -ENODATA; ++ switch (op) { ++ case APPLE_SFTL_LOWER_READ: ++ offset = 8; ++ break; ++ case APPLE_SFTL_LOWER_PROGRAM: ++ offset = 0; ++ break; ++ case APPLE_SFTL_LOWER_ERASE: ++ offset = 16; ++ break; ++ case APPLE_SFTL_LOWER_SINGLE_ERASE: ++ if (rows != 1) ++ return unknown_event(stats); ++ add_count(stats->lower, 104, 1); ++ offset = 16; ++ break; ++ case APPLE_SFTL_LOWER_SYNC: ++ if (rows != 1) ++ return unknown_event(stats); ++ offset = 24; ++ break; ++ default: ++ return unknown_event(stats); ++ } ++ add_count(stats->lower, offset, rows); ++ return 0; ++} ++EXPORT_SYMBOL_GPL(apple_sftl_provider_submit); ++ ++/* Native completion (0x81089c68) reads element flags at +38, stride 12. ++ * The caller must retain the original provider result through translation. ++ */ ++int apple_sftl_provider_complete(struct apple_sftl_provider_stats *stats, ++ enum apple_sftl_lower_op op, u8 vector_status, ++ const u8 *row_status, u16 rows) ++{ ++ u32 i; ++ ++ if (!stats->valid) ++ return -ENODATA; ++ if (op != APPLE_SFTL_LOWER_READ && op != APPLE_SFTL_LOWER_PROGRAM && ++ op != APPLE_SFTL_LOWER_ERASE) ++ return unknown_event(stats); ++ if (vector_status == 0x40) ++ return 0; ++ if (rows && !row_status) ++ return unknown_event(stats); ++ for (i = 0; i < rows; i++) { ++ u8 flags = row_status[i]; ++ ++ if (op == APPLE_SFTL_LOWER_READ) { ++ if (flags & BIT(3)) ++ add_count(stats->lower, 120, 1); ++ else if (flags & BIT(0)) ++ add_count(stats->lower, 128, 1); ++ if (flags & BIT(4)) ++ add_count(stats->lower, 136, 1); ++ if (flags & BIT(1)) ++ add_count(stats->lower, 448, 1); ++ if (flags & BIT(2)) ++ add_count(stats->lower, 456, 1); ++ } else { ++ bool program = op == APPLE_SFTL_LOWER_PROGRAM; ++ ++ if (flags & BIT(0)) ++ add_count(stats->lower, program ? 152 : 168, 1); ++ if (flags & BIT(4)) ++ add_count(stats->lower, program ? 144 : 160, 1); ++ } ++ } ++ return 0; ++} ++EXPORT_SYMBOL_GPL(apple_sftl_provider_complete); ++ ++int apple_sftl_provider_single_erase_done(struct apple_sftl_provider_stats *stats, ++ bool post_erase_sync_failed) ++{ ++ if (!stats->valid) ++ return -ENODATA; ++ if (post_erase_sync_failed) ++ add_count(stats->lower, 168, 1); ++ return 0; ++} ++EXPORT_SYMBOL_GPL(apple_sftl_provider_single_erase_done); ++ ++int apple_sftl_provider_read_stats(void *priv, u32 id, u8 *data, size_t bytes) ++{ ++ const struct apple_sftl_provider_stats *stats = priv; ++ ++ if (!stats || !stats->valid) ++ return -ENODATA; ++ if (!data) ++ return -EINVAL; ++ if (id == 255 && bytes == sizeof(stats->vfl)) ++ memcpy(data, stats->vfl, bytes); ++ else if (id == 254 && bytes == sizeof(stats->lower)) ++ memcpy(data, stats->lower, bytes); ++ else ++ return -EINVAL; ++ return (int)bytes; ++} ++EXPORT_SYMBOL_GPL(apple_sftl_provider_read_stats); +diff --git a/drivers/mtd/apple-sftl-provider.h b/drivers/mtd/apple-sftl-provider.h +new file mode 100644 +index 000000000..716910925 +--- /dev/null ++++ b/drivers/mtd/apple-sftl-provider.h +@@ -0,0 +1,49 @@ ++/* SPDX-License-Identifier: GPL-2.0-only */ ++#ifndef _APPLE_SFTL_PROVIDER_H ++#define _APPLE_SFTL_PROVIDER_H ++ ++#include ++ ++/* IOFlashStorage-730 provider records from the 64 GB P105. Start zeroed. ++ * The provider must serialize updates/snapshots, observe every event since ++ * the seed and call apple_sftl_provider_lost() if that continuity is lost. ++ */ ++struct apple_sftl_provider_stats { ++ u8 vfl[88]; /* TLV 255 */ ++ u8 lower[464]; /* TLV 254 */ ++ bool valid; ++}; ++ ++enum apple_sftl_vfl_event { ++ APPLE_SFTL_VFL_PROGRAM, ++ APPLE_SFTL_VFL_READ, ++ APPLE_SFTL_VFL_ERASE, ++ APPLE_SFTL_VFL_SCATTER_BEGIN, ++ APPLE_SFTL_VFL_SCATTER_ADD, ++ APPLE_SFTL_VFL_SCATTER_FLUSH, ++}; ++ ++/* The first three values match the measured lower-provider batch opcodes. */ ++enum apple_sftl_lower_op { ++ APPLE_SFTL_LOWER_READ = 1, ++ APPLE_SFTL_LOWER_PROGRAM, ++ APPLE_SFTL_LOWER_ERASE, ++ APPLE_SFTL_LOWER_SINGLE_ERASE, ++ APPLE_SFTL_LOWER_SYNC, ++}; ++ ++int apple_sftl_provider_seed(struct apple_sftl_provider_stats *stats, const u8 *vfl, ++ size_t vfl_bytes, const u8 *lower, size_t lower_bytes); ++void apple_sftl_provider_lost(struct apple_sftl_provider_stats *stats); ++int apple_sftl_provider_vfl(struct apple_sftl_provider_stats *stats, ++ enum apple_sftl_vfl_event event, u32 chunks); ++int apple_sftl_provider_submit(struct apple_sftl_provider_stats *stats, ++ enum apple_sftl_lower_op op, u16 rows); ++int apple_sftl_provider_complete(struct apple_sftl_provider_stats *stats, ++ enum apple_sftl_lower_op op, u8 vector_status, ++ const u8 *row_status, u16 rows); ++int apple_sftl_provider_single_erase_done(struct apple_sftl_provider_stats *stats, ++ bool post_erase_sync_failed); ++int apple_sftl_provider_read_stats(void *priv, u32 id, u8 *data, size_t bytes); ++ ++#endif diff --git a/kernel/patches/armv7/mtd/0015-mtd-apple-sftl-Follow-native-allocation-and-checkpoi.patch b/kernel/patches/armv7/mtd/0015-mtd-apple-sftl-Follow-native-allocation-and-checkpoi.patch new file mode 100644 index 00000000..9fdde37c --- /dev/null +++ b/kernel/patches/armv7/mtd/0015-mtd-apple-sftl-Follow-native-allocation-and-checkpoi.patch @@ -0,0 +1,2328 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Shayne <8741557+shaynelegault@users.noreply.github.com> +Date: Thu, 19 Nov 2026 08:18:26 -0600 +Subject: [PATCH] mtd: apple-sftl: Follow native allocation and checkpoint + accounting order + +Allocate a fresh context group and verify the previous occupied site +independently of the new base list. Preserve cached scalar statistics +during a save, sample the VFL and PPN after the base page, and reconcile +successful releases and wear before granting reuse to the normal writer. + +Model healthy free-block selection and clean-pool maintenance. Inspect +unlisted context-role blocks during recovery and fail closed on ambiguous +torn records. Cover disjoint groups, interrupted saves, missing counter +history, completion failures and wear boundaries on synthetic NAND. + +Assisted-by: LLM +--- + .../driver-api/mtd/apple-sftl-reclamation.rst | 49 +- + drivers/mtd/apple-sftl-account-test.c | 104 +++- + drivers/mtd/apple-sftl-account.c | 246 +++++++- + drivers/mtd/apple-sftl-account.h | 42 +- + drivers/mtd/apple-sftl-core.c | 332 ++++++++-- + drivers/mtd/apple-sftl-reclaim.c | 64 +- + drivers/mtd/apple-sftl-reclaim.h | 7 +- + drivers/mtd/apple-sftl-test.c | 571 +++++++++++++++++- + drivers/mtd/apple-sftl.h | 27 +- + 9 files changed, 1309 insertions(+), 133 deletions(-) + +diff --git a/Documentation/driver-api/mtd/apple-sftl-reclamation.rst b/Documentation/driver-api/mtd/apple-sftl-reclamation.rst +index 38abdab54..079a7585a 100644 +--- a/Documentation/driver-api/mtd/apple-sftl-reclamation.rst ++++ b/Documentation/driver-api/mtd/apple-sftl-reclamation.rst +@@ -9,8 +9,8 @@ block. Reclamation must preserve the recovered map for the whole device. + + The block writer allocates from verified blank blocks. An optional, verified + ``erase_block`` provider operation lets the allocator also reclaim obsolete +-blocks. With a complete native accounting policy the allocator can also stage +-a checkpoint in the alternate context block and relocate live data. ++blocks. With a complete native accounting policy the allocator can also ++reserve fresh context sites, relocate live data and publish a checkpoint. + P105 H2FMI does not enable physical erase or supply this policy. When verified + blank blocks run out on that profile, a write returns ``-ENOSPC``. + +@@ -60,8 +60,10 @@ multiple of 64 records with subtype-4 padding. Each padding record + consumes a sequence value. Full logical coverage alone cannot identify the + last durable page of a native save. + +-The encoder recomputes the valid logical-page count of every superblock and +-the associated global statistic (ID 9) from the whole planned map. Closing ++The encoder recomputes the valid logical-page count of every superblock ++from the planned map. The standalone map encoder also normalizes the ++global valid count. Native publication preserves the cached global ++statistics until a known firmware refresh. Closing + a block or replaying a closed footer also updates its subtype-5 user + sequence statistics. Each footer run supplies one sample. + +@@ -71,28 +73,36 @@ one block from the data count to the free count. The native checkpoint + writer can serialize the previous cached counts before that refresh. A + policy must establish the point of serialization as well as the allocation, + stream and wear transitions before allowing the first relocation write. +-The native formatter also queries its provider for two statistics payloads +-(IDs 255 and 254) on each save. ++The native formatter queries two providers after the base page succeeds ++and before writing the statistics page. The VFL record (ID 255) has 88 ++bytes and the lower-provider record (ID 254) has 464. A short or failed ++sample stops publication with the old context intact. A provider can ++reproduce these records from a known seed, complete request batches and ++per-row completions. Missing events invalidate its later samples. + + Saving and recovering a native context + -------------------------------------- + +-The measured two-block format names its active and alternate contexts in the +-base record. The writer verifies exclusive ownership of both and checks the +-old generation's complete extent and the alternate's erased contents. It +-programs the new context in 16 KiB units, reads back each page's data and +-metadata, then cold-loads the medium with both generations visible. The +-expected checkpoint sequence, target location, mapping and lane ownership +-must agree. It erases the old context one lane at a time, verifies it blank +-and cold-loads again before erasing the data donor. ++The base record names the capacity allocated to the new generation. The ++writer supports a bounded case with one occupied old site and two fresh, ++verified clean-free sites. It protects the fresh sites during data ++allocation. It checks that the old generation fits its first listed site, ++that the spare capacity is blank and that the whole map excludes the sites. ++After publishing to the first fresh site it cold-loads with both ++generations visible, erases the old occupied site and reloads again. The ++unused new site is released without issuing an erase. After the verified ++donor erase, the policy receives the runtime roles and incremented wear ++before the donor becomes available for reuse. + + Recovery selects a complete generation and replays later writes. A partially + written native map batch can expose sequence values without revealing how + many further values the interrupted operation reserved. Recovery from that +-state is read-only. When an interrupted retirement leaves older context +-fragments on some lanes, the complete newer generation must own the old +-site and the remaining records must be provably older. Ambiguous fragments, +-conflicting metadata and unaccounted journal gaps prevent recovery. ++state is read-only. Recovery checks unlisted native context-role sites ++independently. A partial retirement must be consistent with an older ++generation at that physical site, including its record offsets and lane ++order. An unreadable supplemental site precludes a writable recovery. ++Conflicting metadata, map ownership and unaccounted journal gaps prevent ++recovery. + + Relocating live data + -------------------- +@@ -154,8 +164,7 @@ each source page, incorporates table updates after the last complete + checkpoint, verifies target erasure, and reads back the initial checkpoint, + all 16 tables and the final checkpoint. It uses separately qualified SLC + erase/program callbacks. Physical P105 supplies neither callback. VFL bank +-rotation is independent of alternating the normal-view SwissPPNFTL context +-blocks. ++rotation is independent of allocating normal-view SwissPPNFTL context sites. + + A fallback to older VFL replicas requires a complete agreeing quartet, + validated references and unchanged addressable ownership. Recovery reports +diff --git a/drivers/mtd/apple-sftl-account-test.c b/drivers/mtd/apple-sftl-account-test.c +index ee60d3f9c..bba306251 100644 +--- a/drivers/mtd/apple-sftl-account-test.c ++++ b/drivers/mtd/apple-sftl-account-test.c +@@ -124,6 +124,10 @@ static void account_context_and_retired_donor(struct kunit *test) + struct apple_sftl_checkpoint image = {}; + struct apple_sftl_snapshot snapshot = {}; + u32 blocks[] = { 4, 1100 }; ++ struct apple_sftl_runtime_stats counters = { ++ .valid = 300, .free = 11, .data = 11, .context = 2, .reserved = 4, ++ .stat37 = 44, .stat38_before_shift = 64063, ++ }; + u8 footer[APPLE_SFTL_META] = { 0x1c, 8 }; + u8 *p, *donor, *reads, *free, *data; + +@@ -157,20 +161,108 @@ static void account_context_and_retired_donor(struct kunit *test) + footer[1] = 9; + KUNIT_EXPECT_EQ(test, apple_sftl_account_retire(&image, 1101, footer), -EOPNOTSUPP); + footer[1] = 8; +- put_unaligned_le64(0, data); +- KUNIT_EXPECT_EQ(test, apple_sftl_account_retire(&image, 1101, footer), -EBADMSG); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(reads), 517U); + KUNIT_EXPECT_EQ(test, donor[0], (u8)3); +- put_unaligned_le64(12, data); + KUNIT_ASSERT_EQ(test, apple_sftl_account_retire(&image, 1101, footer), 0); + KUNIT_EXPECT_EQ(test, donor[0], (u8)9); + KUNIT_EXPECT_EQ(test, donor[1], (u8)8); + KUNIT_EXPECT_EQ(test, get_unaligned_le16(donor + 6), 47); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(reads), 0U); +- KUNIT_EXPECT_EQ(test, get_unaligned_le64(free), 11ULL); +- KUNIT_EXPECT_EQ(test, get_unaligned_le64(data), 11ULL); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le64(free), 10ULL); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le64(data), 12ULL); + KUNIT_EXPECT_EQ(test, apple_sftl_account_retire(&image, 1101, footer), -EOPNOTSUPP); ++ KUNIT_ASSERT_EQ(test, apple_sftl_account_refresh(&image, &counters), 0); + KUNIT_EXPECT_EQ(test, get_unaligned_le64(free), 11ULL); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le64(data), 11ULL); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le64(free + 27 * 16), 44ULL); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le64(free + 28 * 16), 1000ULL); ++ KUNIT_ASSERT_EQ(test, apple_sftl_account_release(&image, 1101, APPLE_SFTL_ERASED_DONOR), 0); ++ KUNIT_EXPECT_EQ(test, donor[0], (u8)1); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le16(donor + 6), 48); ++ KUNIT_EXPECT_EQ(test, apple_sftl_account_release(&image, 1101, APPLE_SFTL_ERASED_DONOR), ++ -EOPNOTSUPP); ++ put_unaligned_le16(0, p + 2); ++ put_unaligned_le16(0xffff, p + 6); ++ KUNIT_EXPECT_EQ(test, apple_sftl_account_release(&image, 1100, APPLE_SFTL_RELEASED_CONTEXT), ++ -EOVERFLOW); ++ KUNIT_EXPECT_EQ(test, p[0], 7); ++ put_unaligned_le16(2, p + 6); ++ KUNIT_ASSERT_EQ(test, apple_sftl_account_release(&image, 1100, APPLE_SFTL_RELEASED_CONTEXT), 0); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le16(p + 6), 3); ++} ++ ++static void account_saved_counts_before_refresh(struct kunit *test) ++{ ++ struct apple_sftl_extent runs[] = { ++ { 0, 7, 2 * APPLE_SFTL_STRIDE }, ++ { 7, APPLE_SFTL_LOGICAL_PAGES - 7, APPLE_SFTL_NO_ADDRESS }, ++ }; ++ struct apple_sftl_snapshot map = { .runs = runs, .nr_runs = ARRAY_SIZE(runs) }; ++ struct apple_sftl_checkpoint image = {}; ++ u8 *valid, *entry; ++ ++ account_fixture(test, &image); ++ KUNIT_ASSERT_NOT_NULL(test, image.records); ++ valid = image.records[4].data + TEST_VALID_STAT_OFFSET; ++ entry = image.records[8].data + 16; ++ put_unaligned_le64(12, valid); ++ put_unaligned_le16(6, entry + 2); ++ KUNIT_EXPECT_EQ(test, apple_sftl_account_map(&image, &map), -EBADMSG); ++ KUNIT_ASSERT_EQ(test, apple_sftl_account_saved_map(&image, &map), 0); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le16(entry + 2), 7); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le64(valid), 12ULL); ++ put_unaligned_le64(APPLE_SFTL_LOGICAL_PAGES + 1, valid); ++ KUNIT_EXPECT_EQ(test, apple_sftl_account_saved_map(&image, &map), -EBADMSG); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le16(entry + 2), 7); ++} ++ ++static void account_native_free_allocation(struct kunit *test) ++{ ++ /* The native refill keeps six unsorted slots. Lower wear arriving ++ * later replaces the first maximum, and ties retain their first slot. ++ */ ++ static const u8 dirty_wear[] = { 10, 12, 9, 9, 11, 15, 6, 7 }; ++ struct apple_sftl_checkpoint image = {}; ++ struct apple_sftl_snapshot snapshot = {}; ++ struct apple_sftl_allocation plan = {}; ++ u8 *table; ++ u32 i; ++ ++ account_fixture(test, &image); ++ KUNIT_ASSERT_NOT_NULL(test, image.records); ++ snapshot.nr_opaque = APPLE_SFTL_ACCOUNT_RECORDS; ++ snapshot.opaque = image.records; ++ table = image.records[8].data; ++ KUNIT_EXPECT_EQ(test, apple_sftl_account_plan_allocation(&snapshot, &plan), -ENOSPC); ++ for (i = 0; i < ARRAY_SIZE(dirty_wear); i++) { ++ table[(i + 1) * 8] = i % 2 ? 2 : 9; ++ put_unaligned_le16(dirty_wear[i], table + (i + 1) * 8 + 6); ++ } ++ for (i = 20; i < 23; i++) { ++ table[i * 8] = 1; ++ put_unaligned_le16(7, table + i * 8 + 6); ++ } ++ KUNIT_ASSERT_EQ(test, apple_sftl_account_plan_allocation(&snapshot, &plan), 0); ++ KUNIT_EXPECT_EQ(test, plan.landing, 20); ++ KUNIT_EXPECT_EQ(test, plan.nr_erase, 3); ++ KUNIT_EXPECT_EQ(test, plan.erase[0], 1); ++ KUNIT_EXPECT_EQ(test, plan.erase[1], 8); ++ KUNIT_EXPECT_EQ(test, plan.erase[2], 3); ++ ++ /* Wear savings greater than one select a dirty landing even when ++ * the clean pool is full. A one-count difference prefers clean. ++ */ ++ for (i = 20; i < 26; i++) { ++ table[i * 8] = 1; ++ put_unaligned_le16(8, table + i * 8 + 6); ++ } ++ KUNIT_ASSERT_EQ(test, apple_sftl_account_plan_allocation(&snapshot, &plan), 0); ++ KUNIT_EXPECT_EQ(test, plan.landing, 7); ++ KUNIT_EXPECT_EQ(test, plan.nr_erase, 1); ++ KUNIT_EXPECT_EQ(test, plan.erase[0], 7); ++ put_unaligned_le16(1, table + 7 * 8 + 2); ++ KUNIT_EXPECT_EQ(test, apple_sftl_account_plan_allocation(&snapshot, &plan), -EBADMSG); ++ KUNIT_EXPECT_EQ(test, plan.landing, 7); + } + + static struct kunit_case account_cases[] = { +@@ -178,6 +270,8 @@ static struct kunit_case account_cases[] = { + KUNIT_CASE(account_invalid_layout_or_map), + KUNIT_CASE(account_footer_user_sequence), + KUNIT_CASE(account_context_and_retired_donor), ++ KUNIT_CASE(account_native_free_allocation), ++ KUNIT_CASE(account_saved_counts_before_refresh), + {} + }; + +diff --git a/drivers/mtd/apple-sftl-account.c b/drivers/mtd/apple-sftl-account.c +index b6153bfd4..a8d50fa7b 100644 +--- a/drivers/mtd/apple-sftl-account.c ++++ b/drivers/mtd/apple-sftl-account.c +@@ -20,6 +20,7 @@ + #define SFTL_READ_COUNT_START 28 + #define SFTL_SB_ENTRY_BYTES 8 + #define SFTL_SB_PER_RECORD (APPLE_SFTL_CHUNK / SFTL_SB_ENTRY_BYTES) ++#define SFTL_MAX_WEAR 0xffff + + bool apple_sftl_native_context_tag(u32 index, const u8 *meta) + { +@@ -103,6 +104,88 @@ static u8 *sftl_sb_entry(struct apple_sftl_context_record *records, u32 block) + return records[record].data + offset; + } + ++struct sftl_free_block { ++ u32 block; ++ u32 wear; ++}; ++ ++/* The refill list has six fixed slots, not a sorted wear queue. On equal ++ * wear the native scan keeps the first occurrence; refill uses slot order. ++ */ ++static void sftl_add_dirty(struct sftl_free_block pool[], u32 block, u16 wear) ++{ ++ u32 i, replace = 0; ++ ++ for (i = 0; i < APPLE_SFTL_CLEAN_POOL; i++) { ++ if (pool[i].block == U32_MAX) { ++ replace = i; ++ break; ++ } ++ if (pool[i].wear > pool[replace].wear) ++ replace = i; ++ } ++ if (wear < pool[replace].wear) ++ pool[replace] = (struct sftl_free_block) { block, wear }; ++} ++ ++/* Healthy 13G36 free-block allocation at 0x810932f8. Firmware first ++ * replenishes the clean pool. It prefers a clean block within one wear ++ * count of the least worn free block, using the candidates from the scan ++ * before replenishment. A selected dirty block may already be in the pool. ++ */ ++int apple_sftl_account_plan_allocation(const struct apple_sftl_snapshot *runtime, ++ struct apple_sftl_allocation *plan) ++{ ++ struct sftl_free_block pool[APPLE_SFTL_CLEAN_POOL]; ++ struct sftl_free_block clean = { U32_MAX, U32_MAX }, least = clean; ++ struct apple_sftl_allocation result = {}; ++ u32 i, nr_clean = 0; ++ bool erased = false; ++ ++ if (!plan || !runtime || runtime->nr_opaque != APPLE_SFTL_ACCOUNT_RECORDS || ++ !sftl_native_prefix(runtime->opaque, runtime->nr_opaque)) ++ return -EINVAL; ++ for (i = 0; i < ARRAY_SIZE(pool); i++) ++ pool[i] = (struct sftl_free_block) { U32_MAX, SFTL_MAX_WEAR }; ++ for (i = 0; i < APPLE_SFTL_SUBBLOCKS; i++) { ++ const u8 *entry = sftl_sb_entry(runtime->opaque, i); ++ u32 wear = get_unaligned_le16(entry + 6); ++ ++ if (entry[0] != 1 && entry[0] != 2 && entry[0] != 9) ++ continue; ++ if (!i || get_unaligned_le16(entry + 2)) ++ return -EBADMSG; ++ if (wear < least.wear) ++ least = (struct sftl_free_block) { i, wear }; ++ if (entry[0] == 1) { ++ nr_clean++; ++ if (wear < clean.wear) ++ clean = (struct sftl_free_block) { i, wear }; ++ } else { ++ sftl_add_dirty(pool, i, wear); ++ } ++ } ++ if (least.block == U32_MAX) ++ return -ENOSPC; ++ result.landing = least.wear + 1 < clean.wear ? least.block : clean.block; ++ for (i = 0; nr_clean < APPLE_SFTL_CLEAN_POOL && i < ARRAY_SIZE(pool); i++) { ++ if (pool[i].block == U32_MAX) ++ break; ++ result.erase[result.nr_erase++] = pool[i].block; ++ erased |= pool[i].block == result.landing; ++ nr_clean++; ++ } ++ if (!erased && sftl_sb_entry(runtime->opaque, result.landing)[0] != 1) { ++ /* The native halfword wraps. Leave that policy unqualified. */ ++ if (least.wear == SFTL_MAX_WEAR) ++ return -EOVERFLOW; ++ result.erase[result.nr_erase++] = result.landing; ++ } ++ *plan = result; ++ return 0; ++} ++EXPORT_SYMBOL_GPL(apple_sftl_account_plan_allocation); ++ + static bool sftl_account_context_pair(struct apple_sftl_context_record *records, u32 count, + const u32 blocks[2]) + { +@@ -141,20 +224,41 @@ bool apple_sftl_account_gc_image(const struct apple_sftl_snapshot *before, + const struct apple_sftl_checkpoint *image, const u32 blocks[2], + u32 donor, u32 landing, u32 live, u16 footer_chunks) + { +- u32 block, seen = 0, reads_per_record = APPLE_SFTL_CHUNK / sizeof(u32); ++ u32 block, old, i, seen = 0, reads_per_record = APPLE_SFTL_CHUNK / sizeof(u32); + const u8 *old_source, *source, *destination, *reads; + +- if (image->count <= APPLE_SFTL_ACCOUNT_RECORDS || ++ if (!sftl_native_prefix(before->opaque, before->nr_opaque) || ++ image->count <= APPLE_SFTL_ACCOUNT_RECORDS || + !sftl_account_context_pair(image->records, image->count, blocks) || + !donor || donor >= APPLE_SFTL_SUBBLOCKS || !landing || landing >= APPLE_SFTL_SUBBLOCKS || + !live || footer_chunks < 4 || footer_chunks > 32 || footer_chunks % 4) + return false; ++ /* This bounded path retires one previously occupied context block. ++ * The save names freshly allocated sites and still serializes the old ++ * role/wear before any retirement, including when the old site is full. ++ */ ++ old = get_unaligned_le32(before->opaque[0].data + 4); ++ if (!old || old >= APPLE_SFTL_SUBBLOCKS || old == blocks[0] || old == blocks[1] || ++ sftl_sb_entry(before->opaque, old)[0] != 7 || ++ get_unaligned_le16(sftl_sb_entry(before->opaque, old) + 6) == SFTL_MAX_WEAR || ++ memcmp(sftl_sb_entry(before->opaque, old), sftl_sb_entry(image->records, old), 8)) ++ return false; ++ for (i = 0; i < 2; i++) { ++ const u8 *prior = sftl_sb_entry(before->opaque, blocks[i]); ++ const u8 *next = sftl_sb_entry(image->records, blocks[i]); ++ ++ if (prior[0] != 1 || get_unaligned_le16(prior + 2) || ++ (i == 1 && get_unaligned_le16(prior + 6) == SFTL_MAX_WEAR) || ++ get_unaligned_le16(prior + 6) != get_unaligned_le16(next + 6)) ++ return false; ++ } + old_source = sftl_sb_entry(before->opaque, donor); + source = sftl_sb_entry(image->records, donor); + destination = sftl_sb_entry(image->records, landing); + reads = image->records[SFTL_READ_COUNT_START + donor / reads_per_record].data + + (donor % reads_per_record) * sizeof(u32); + if (old_source[0] != 3 || source[0] != 9 || source[1] != old_source[1] || ++ get_unaligned_le16(source + 6) == SFTL_MAX_WEAR || + source[1] >> 1 != footer_chunks || + get_unaligned_le16(source + 6) != get_unaligned_le16(old_source + 6) || + get_unaligned_le16(source + 2) || get_unaligned_le32(reads) || +@@ -205,7 +309,7 @@ static int sftl_account_layout(const struct apple_sftl_checkpoint *image) + return 0; + } + +-static u8 *sftl_stats(struct apple_sftl_checkpoint *image) ++static u8 *sftl_stats(struct apple_sftl_checkpoint *image, u8 **provider_stats) + { + u8 *payload = image->records[SFTL_STATS_START].data; + u32 i, offset = sizeof(u32); +@@ -228,11 +332,31 @@ static u8 *sftl_stats(struct apple_sftl_checkpoint *image) + (i == 43 && (id != 255 || words != 22)) || + (i == 44 && (id != 254 || words != 116))) + return NULL; ++ if (provider_stats && i >= 43) ++ provider_stats[i - 43] = payload + offset; + offset += words * 4; + } + return payload; + } + ++/* Native cxtSave writes its base page before fetching these two records. ++ * The caller owns the pending image and discards it upon a short/error read. ++ */ ++int apple_sftl_account_read_stats(struct apple_sftl_checkpoint *image, ++ const struct apple_sftl_gc_io *io, void *priv) ++{ ++ u8 *data[2]; ++ int ret; ++ ++ if (!sftl_stats(image, data)) ++ return -EBADMSG; ++ ret = io->read_native_stats(priv, 255, data[0], 88); ++ if (ret != 88) ++ return ret < 0 ? ret : -EIO; ++ ret = io->read_native_stats(priv, 254, data[1], 464); ++ return ret == 464 ? 0 : (ret < 0 ? ret : -EIO); ++} ++ + /* sftl_stats has checked all of the fixed-size leading counters. */ + static u8 *sftl_stat(u8 *stats, u32 id) + { +@@ -245,8 +369,7 @@ static u8 *sftl_stat(u8 *stats, u32 id) + int apple_sftl_account_retire(struct apple_sftl_checkpoint *image, u32 block, const u8 *footer_meta) + { + u32 reads_per_record = APPLE_SFTL_CHUNK / sizeof(u32); +- u8 *entry, *reads, *stats; +- u64 data, free; ++ u8 *entry, *reads; + u16 chunks; + int ret; + +@@ -262,26 +385,78 @@ int apple_sftl_account_retire(struct apple_sftl_checkpoint *image, u32 block, co + entry = sftl_sb_entry(image->records, block); + if (entry[0] != 3 || entry[1] != footer_meta[1] || get_unaligned_le16(entry + 2)) + return -EOPNOTSUPP; +- stats = sftl_stats(image); +- if (!stats) +- return -EOPNOTSUPP; +- free = get_unaligned_le64(sftl_stat(stats, 10)); +- data = get_unaligned_le64(sftl_stat(stats, 11)); +- if (free >= APPLE_SFTL_SUBBLOCKS || !data || data > APPLE_SFTL_SUBBLOCKS - free) +- return -EBADMSG; + reads = image->records[SFTL_READ_COUNT_START + block / reads_per_record].data + + (block % reads_per_record) * sizeof(u32); +- /* cxt's stats IDs 10/11 copy native free/data counts (0x81094be2). +- * Final invalidation clears reads and moves one block at 0x81090ade. ++ /* At 0x81090ade final invalidation only updates the entry and runtime ++ * totals. A save can see the previous cached free and data statistics. + */ + entry[0] = 9; + put_unaligned_le32(0, reads); +- put_unaligned_le64(free + 1, sftl_stat(stats, 10)); +- put_unaligned_le64(data - 1, sftl_stat(stats, 11)); + return 0; + } + EXPORT_SYMBOL_GPL(apple_sftl_account_retire); + ++int apple_sftl_account_release(struct apple_sftl_checkpoint *image, u32 block, ++ enum apple_sftl_release event) ++{ ++ u8 *entry; ++ u16 wear; ++ int ret; ++ ++ if (!image || !image->records || !block || block >= APPLE_SFTL_SUBBLOCKS || ++ (event != APPLE_SFTL_ERASED_DONOR && event != APPLE_SFTL_RELEASED_CONTEXT)) ++ return -EINVAL; ++ ret = sftl_account_layout(image); ++ if (ret) ++ return ret; ++ entry = sftl_sb_entry(image->records, block); ++ if (entry[0] != (event == APPLE_SFTL_ERASED_DONOR ? 9 : 7) || ++ get_unaligned_le16(entry + 2)) ++ return -EOPNOTSUPP; ++ wear = get_unaligned_le16(entry + 6); ++ if (wear == SFTL_MAX_WEAR) ++ return -EOVERFLOW; ++ /* 0x81093958 follows completed ordinary erase; 0x81093848 also ++ * counts release of an unused context, which requires no erase. ++ */ ++ entry[0] = 1; ++ put_unaligned_le16(wear + 1, entry + 6); ++ return 0; ++} ++EXPORT_SYMBOL_GPL(apple_sftl_account_release); ++ ++/* These seven fields, and only these fields, are sampled by 0x81094bcc. ++ * Call only at the observed refresh boundary, with current runtime inputs. ++ */ ++int apple_sftl_account_refresh(struct apple_sftl_checkpoint *image, ++ const struct apple_sftl_runtime_stats *runtime) ++{ ++ u8 *stats; ++ int ret; ++ ++ if (!image || !image->records || !runtime) ++ return -EINVAL; ++ ret = sftl_account_layout(image); ++ if (ret) ++ return ret; ++ stats = sftl_stats(image, NULL); ++ if (!stats) ++ return -EOPNOTSUPP; ++ if (runtime->valid > APPLE_SFTL_LOGICAL_PAGES || ++ (u64)runtime->free + runtime->data + runtime->context + runtime->reserved > ++ APPLE_SFTL_SUBBLOCKS) ++ return -EBADMSG; ++ put_unaligned_le64(runtime->valid, sftl_stat(stats, 9)); ++ put_unaligned_le64(runtime->free, sftl_stat(stats, 10)); ++ put_unaligned_le64(runtime->data, sftl_stat(stats, 11)); ++ put_unaligned_le64(runtime->context, sftl_stat(stats, 12)); ++ put_unaligned_le64(runtime->reserved, sftl_stat(stats, 13)); ++ put_unaligned_le64(runtime->stat37, sftl_stat(stats, 37)); ++ put_unaligned_le64(runtime->stat38_before_shift >> 6, sftl_stat(stats, 38)); ++ return 0; ++} ++EXPORT_SYMBOL_GPL(apple_sftl_account_refresh); ++ + static int sftl_account_runs(const struct apple_sftl_snapshot *map, u16 *counts, u32 *total) + { + u32 cursor = 0, i; +@@ -313,11 +488,11 @@ static int sftl_account_runs(const struct apple_sftl_snapshot *map, u16 *counts, + return cursor == APPLE_SFTL_LOGICAL_PAGES ? 0 : -EBADMSG; + } + +-int apple_sftl_account_map(struct apple_sftl_checkpoint *image, +- const struct apple_sftl_snapshot *map) ++static int sftl_update_map_counts(struct apple_sftl_checkpoint *image, ++ const struct apple_sftl_snapshot *map, bool preserve_cache) + { + u32 old_total = 0, total = 0, block; +- u8 *valid; ++ u8 *valid, *stats; + u16 *counts; + int ret; + +@@ -327,10 +502,10 @@ int apple_sftl_account_map(struct apple_sftl_checkpoint *image, + ret = sftl_account_layout(image); + if (ret) + return ret; +- valid = sftl_stats(image); +- if (!valid) ++ stats = sftl_stats(image, NULL); ++ if (!stats) + return -EOPNOTSUPP; +- valid = sftl_stat(valid, 9); ++ valid = sftl_stat(stats, 9); + for (block = 0; block < APPLE_SFTL_SUBBLOCKS; block++) { + u16 n = get_unaligned_le16(sftl_sb_entry(image->records, block) + 2); + +@@ -338,8 +513,17 @@ int apple_sftl_account_map(struct apple_sftl_checkpoint *image, + return -EBADMSG; + old_total += n; + } +- if (get_unaligned_le64(valid) != old_total) ++ if (preserve_cache) { ++ u32 id; ++ ++ if (get_unaligned_le64(valid) > APPLE_SFTL_LOGICAL_PAGES) ++ return -EBADMSG; ++ for (id = 10; id <= 13; id++) ++ if (get_unaligned_le64(sftl_stat(stats, id)) > APPLE_SFTL_SUBBLOCKS) ++ return -EBADMSG; ++ } else if (get_unaligned_le64(valid) != old_total) { + return -EBADMSG; ++ } + + counts = kcalloc(APPLE_SFTL_SUBBLOCKS, sizeof(*counts), GFP_NOIO); + if (!counts) +@@ -348,9 +532,23 @@ int apple_sftl_account_map(struct apple_sftl_checkpoint *image, + if (!ret) { + for (block = 0; block < APPLE_SFTL_SUBBLOCKS; block++) + put_unaligned_le16(counts[block], sftl_sb_entry(image->records, block) + 2); +- put_unaligned_le64(total, valid); ++ if (!preserve_cache) ++ put_unaligned_le64(total, valid); + } + kfree(counts); + return ret; + } ++ ++int apple_sftl_account_map(struct apple_sftl_checkpoint *image, ++ const struct apple_sftl_snapshot *map) ++{ ++ return sftl_update_map_counts(image, map, false); ++} + EXPORT_SYMBOL_GPL(apple_sftl_account_map); ++ ++int apple_sftl_account_saved_map(struct apple_sftl_checkpoint *image, ++ const struct apple_sftl_snapshot *map) ++{ ++ return sftl_update_map_counts(image, map, true); ++} ++EXPORT_SYMBOL_GPL(apple_sftl_account_saved_map); +diff --git a/drivers/mtd/apple-sftl-account.h b/drivers/mtd/apple-sftl-account.h +index a5045c63f..ad079aafc 100644 +--- a/drivers/mtd/apple-sftl-account.h ++++ b/drivers/mtd/apple-sftl-account.h +@@ -12,18 +12,50 @@ struct apple_sftl_user_seq { + }; + + struct apple_sftl_checkpoint; ++struct apple_sftl_gc_io; + struct apple_sftl_snapshot; + ++#define APPLE_SFTL_CLEAN_POOL 6 ++ ++struct apple_sftl_allocation { ++ u16 landing; ++ u16 erase[APPLE_SFTL_CLEAN_POOL + 1]; ++ u8 nr_erase; ++}; ++ ++/* Plan one healthy native allocation from current runtime roles and wear. ++ * The caller owns capacity validation and must qualify every listed erase. ++ */ ++int apple_sftl_account_plan_allocation(const struct apple_sftl_snapshot *runtime, ++ struct apple_sftl_allocation *plan); ++ ++int apple_sftl_account_read_stats(struct apple_sftl_checkpoint *image, ++ const struct apple_sftl_gc_io *io, void *priv); + bool apple_sftl_account_context_pair(const struct apple_sftl_snapshot *snapshot, const u32 blocks[2]); + bool apple_sftl_account_gc_image(const struct apple_sftl_snapshot *before, + const struct apple_sftl_checkpoint *image, const u32 blocks[2], + u32 donor, u32 landing, u32 live, u16 footer_chunks); +-/* Donor component of a post-GC state. The caller has validated its footer, +- * removed its final mapping and knows the counters represent the baseline +- * immediately before this retirement. A native save can precede stats refresh. +- */ ++/* The ordinary donor is unmapped and its footer has been verified. */ + int apple_sftl_account_retire(struct apple_sftl_checkpoint *image, u32 block, + const u8 *footer_meta); ++ ++enum apple_sftl_release { ++ APPLE_SFTL_ERASED_DONOR, ++ APPLE_SFTL_RELEASED_CONTEXT, ++}; ++ ++/* The caller has completed the provider erase or positively identified an ++ * unused context reservation released by the native publication protocol. ++ */ ++int apple_sftl_account_release(struct apple_sftl_checkpoint *image, u32 block, ++ enum apple_sftl_release event); ++struct apple_sftl_runtime_stats { ++ u32 valid, free, data, context, reserved; ++ u32 stat37; ++ u32 stat38_before_shift; ++}; ++int apple_sftl_account_refresh(struct apple_sftl_checkpoint *image, ++ const struct apple_sftl_runtime_stats *runtime); + bool apple_sftl_native_context_tag(u32 index, const u8 *meta); + + void apple_sftl_summarize_user_seq(struct apple_sftl_user_seq *out, const u8 *runs, u32 count); +@@ -35,5 +67,7 @@ int apple_sftl_account_user_seq(struct apple_sftl_snapshot *snapshot, + */ + int apple_sftl_account_map(struct apple_sftl_checkpoint *image, + const struct apple_sftl_snapshot *map); ++int apple_sftl_account_saved_map(struct apple_sftl_checkpoint *image, ++ const struct apple_sftl_snapshot *map); + + #endif +diff --git a/drivers/mtd/apple-sftl-core.c b/drivers/mtd/apple-sftl-core.c +index 723837295..857de8b84 100644 +--- a/drivers/mtd/apple-sftl-core.c ++++ b/drivers/mtd/apple-sftl-core.c +@@ -43,6 +43,7 @@ enum sftl_block_role { + SFTL_CLEAN_FREE = 1, + SFTL_CLOSED_DATA = 3, + SFTL_OPEN_DATA = 4, ++ SFTL_CONTEXT = 7, + SFTL_DIRTY_FREE = 9, + }; + +@@ -72,6 +73,7 @@ struct apple_sftl { + struct apple_sftl_vfl_source vfl_source; + bool vfl_degraded, context_degraded, native_context; + u8 roles[SFTL_SUBBLOCKS], block_flags[SFTL_SUBBLOCKS]; ++ u16 wear[SFTL_SUBBLOCKS]; + struct rb_root runs; + struct sftl_records context; + struct apple_sftl_user_seq *closed_user_seq; +@@ -849,8 +851,8 @@ static int sftl_load_ranges(struct apple_sftl *sftl, const u8 *payload, u32 *cur + * can also describe an unnamed context spare, a reserved block or a stream. + * An older saved role can veto a new allocation while that fallback exists. + */ +-static void sftl_load_block_roles(struct apple_sftl *sftl, const u8 *meta, const u8 *payload, +- bool selected) ++static int sftl_load_block_roles(struct apple_sftl *sftl, const u8 *meta, const u8 *payload, ++ bool selected) + { + u32 block = get_unaligned_le16(meta + 8), count = get_unaligned_le16(meta + 10); + +@@ -859,16 +861,20 @@ static void sftl_load_block_roles(struct apple_sftl *sftl, const u8 *meta, const + u16 live = get_unaligned_le16(payload + 2); + + if (selected) { ++ if (role == SFTL_CONTEXT && live) ++ return -EBADMSG; + if ((role == SFTL_CLEAN_FREE || role == SFTL_DIRTY_FREE) && live) + role = 0; + sftl->roles[block] = role; + sftl->block_flags[block] = payload[1]; ++ sftl->wear[block] = get_unaligned_le16(payload + 6); + } else if (role != SFTL_CLEAN_FREE || live) { + __set_bit(block, sftl->fallback_occupied); + } + block++; + payload += 8; + } ++ return 0; + } + + /* -ENODATA distinguishes a valid prefix from malformed records or a read error. */ +@@ -877,7 +883,7 @@ static int sftl_decode_generation(struct apple_sftl *sftl, const struct sftl_rec + { + u32 cursor = 0, map_records = 0, i, previous_order = 0; + u64 first_sequence = records->items[first].stamp & SFTL_SEQUENCE; +- bool base_tags = true, native = false, short_range = false; ++ bool base_tags = true, at_base = false, native = false, short_range = false; + + for (i = first; i < end; i++) { + const struct sftl_record *record = &records->items[i]; +@@ -902,6 +908,8 @@ static int sftl_decode_generation(struct apple_sftl *sftl, const struct sftl_rec + + if (subtype != 1 || !names || names > SFTL_MAX_CONTEXTS) + return -EBADMSG; ++ at_base = !(record->address % SFTL_STRIDE) && ++ get_unaligned_le32(payload + 4) == record->address / SFTL_STRIDE; + for (k = 0; k < names; k++) { + u32 block = get_unaligned_le32(payload + 4 + k * 4); + +@@ -916,7 +924,7 @@ static int sftl_decode_generation(struct apple_sftl *sftl, const struct sftl_rec + if (index < 4 && subtype == 1) { + base_tags &= apple_sftl_native_context_tag(index, meta); + if (index == 3) { +- if (!base_tags || ++ if (!at_base || !base_tags || + get_unaligned_le32(payload + SFTL_CHUNK - 4) != SFTL_SUBBLOCKS) + return -EBADMSG; + native = true; +@@ -924,8 +932,11 @@ static int sftl_decode_generation(struct apple_sftl *sftl, const struct sftl_rec + } + if (native && !apple_sftl_native_context_tag(index, meta)) + return -EBADMSG; +- if (native && subtype == 3) +- sftl_load_block_roles(sftl, meta, payload, true); ++ if (native && subtype == 3) { ++ ret = sftl_load_block_roles(sftl, meta, payload, true); ++ if (ret) ++ return ret; ++ } + if (subtype == 4) { + ret = sftl_load_ranges(sftl, payload, &cursor, &short_range); + if (ret) +@@ -952,6 +963,64 @@ static int sftl_decode_generation(struct apple_sftl *sftl, const struct sftl_rec + return 0; + } + ++/* Losing the first lane while retiring an old context also loses its base ++ * header. The selected checkpoint can still identify the physical block as ++ * reserved. Accept only older native records at their original page offsets, ++ * with whole-lane erasure or an erased tail on each surviving lane. ++ */ ++static int sftl_check_context_remnant(struct apple_sftl *sftl, u32 block, u64 selected) ++{ ++ u32 streams = hweight16(sftl->lanes[block]), pos, j; ++ u16 ended = 0; ++ u64 base = 0; ++ ++ if (!streams) ++ return -EBADMSG; ++ for (pos = 0; pos < streams * 128; pos++) { ++ u16 lane = BIT(pos % streams); ++ unsigned int chunk; ++ int ret = sftl_read_address(sftl, block * SFTL_STRIDE + pos * 4, &chunk); ++ ++ if (ret) { ++ if (ret == -EBADMSG) ++ return ret; ++ /* This site's unread contents cannot establish a writable ++ * medium. The complete checkpoint still serves reads. ++ */ ++ sftl->context_degraded = true; ++ return 0; ++ } ++ if (sftl_erased(sftl->oob, sizeof(sftl->oob))) { ++ if (!sftl_erased(sftl->page, APPLE_PPN_PAGE_SIZE)) ++ return -EBADMSG; ++ ended |= lane; ++ continue; ++ } ++ if (ended & lane) ++ return -EBADMSG; ++ if (!pos && (!get_unaligned_le32(sftl->page) || ++ get_unaligned_le32(sftl->page) > SFTL_MAX_CONTEXTS || ++ get_unaligned_le32(sftl->page + 4) != block || ++ get_unaligned_le32(sftl->page + APPLE_PPN_PAGE_SIZE - 4) != SFTL_SUBBLOCKS)) ++ return -EBADMSG; ++ for (j = 0; j < 4; j++) { ++ const u8 *meta = sftl->oob + SFTL_META * j; ++ u32 offset = 4 * pos + j; ++ u64 sequence = sftl_sequence(meta); ++ ++ if (!apple_sftl_native_context_tag(offset, meta) || sequence <= offset || ++ sequence >= selected || (base && sequence - offset != base)) ++ return -EBADMSG; ++ base = sequence - offset; ++ } ++ } ++ if (base) ++ sftl->context_degraded = true; ++ return 0; ++} ++ ++static void sftl_mark_mapped(struct apple_sftl *sftl, unsigned long *blocks); ++ + static int sftl_context(struct apple_sftl *sftl, const u8 *heads, struct sftl_records *journal) + { + struct sftl_records records = {}; +@@ -1072,14 +1141,41 @@ static int sftl_context(struct apple_sftl *sftl, const u8 *heads, struct sftl_re + ret = -EBADMSG; + goto out; + } +- /* Retired lanes may be wholly erased while old lanes still hold +- * verified context records. Require a newer intact generation which +- * names that site, and keep the writer off until reconciliation. ++ sftl_mark_mapped(sftl, sftl->checkpoint_used); ++ /* A native base names this save's capacity, not the previous ++ * generation. The old base may have disappeared with its first lane. + */ ++ if (sftl->native_context) { ++ u32 checked = 0; ++ u64 selected = records.items[first].stamp & SFTL_SEQUENCE; ++ ++ for (block = 0; block < SFTL_SUBBLOCKS; block++) { ++ if (sftl->roles[block] != SFTL_CONTEXT) ++ continue; ++ if (test_bit(block, sftl->checkpoint_used) || test_bit(block, sftl->vfl_reserved)) { ++ ret = -EBADMSG; ++ goto out; ++ } ++ if (test_bit(block, sftl->unknown_lane_flags)) ++ sftl->context_degraded = true; ++ if (test_bit(block, sftl->reserved) && !test_bit(block, gapped)) ++ continue; ++ __set_bit(block, sftl->reserved); ++ /* Bound additional full-block checks on unfamiliar media. */ ++ if (++checked > SFTL_MAX_CONTEXTS) { ++ sftl->context_degraded = true; ++ continue; ++ } ++ ret = sftl_check_context_remnant(sftl, block, selected); ++ if (ret) ++ goto out; ++ } ++ } + for (i = 0; i < records.count; i++) { + block = records.items[i].address / SFTL_STRIDE; + if (test_bit(block, gapped)) { +- if (i >= first || !test_bit(block, named)) { ++ if (i >= first || (sftl->native_context ? ++ sftl->roles[block] != SFTL_CONTEXT : !test_bit(block, named))) { + ret = -EBADMSG; + goto out; + } +@@ -1534,7 +1630,6 @@ int apple_sftl_load(struct apple_ppn *provider, struct apple_sftl **out) + if (ret) + goto failed; + /* A map entry superseded by journal replay still belongs to the saved map. */ +- sftl_mark_mapped(sftl, sftl->checkpoint_used); + ret = sftl_journal(sftl, heads, &journal); + if (ret) + goto failed; +@@ -1804,7 +1899,9 @@ static int sftl_reclaim_retired(struct apple_sftl *sftl, u32 *claimed, + u32 block; + int ret = -ENOSPC; + +- if (!sftl->ppn->ops->erase_block) ++ if (!sftl->ppn->ops->erase_block || ++ (sftl->native_context && ++ (!sftl->checkpoint_ops || !sftl->checkpoint_ops->account_retired))) + return -ENOSPC; + /* The top 32 physical blocks can contain native bootstrap members. */ + for (block = 1; block < SFTL_BLOCKS - 32; block++) { +@@ -1814,6 +1911,8 @@ static int sftl_reclaim_retired(struct apple_sftl *sftl, u32 *claimed, + + if (test_bit(block, sftl->used) || !sftl_retired_candidate(sftl, block)) + continue; ++ if (sftl->native_context && sftl->wear[block] == 0xffff) ++ continue; + if (!cold) { + ret = apple_sftl_load(sftl->ppn, &cold); + if (ret) +@@ -1835,6 +1934,9 @@ static int sftl_reclaim_retired(struct apple_sftl *sftl, u32 *claimed, + } + if (!sftl_retired_candidate(cold, block) || + cold->checkpoint_start != sftl->checkpoint_start || ++ cold->native_context != sftl->native_context || ++ (sftl->native_context && (cold->wear[block] != sftl->wear[block] || ++ cold->block_flags[block] != sftl->block_flags[block])) || + cold->lanes[block] != victim.lanes) { + ret = -EBADMSG; + break; +@@ -1843,6 +1945,9 @@ static int sftl_reclaim_retired(struct apple_sftl *sftl, u32 *claimed, + if (!ret) + ret = apple_sftl_gc_plan_retired(&active, &saved, &victim, + cold->checkpoint_start); ++ if (!ret && sftl->native_context) ++ ret = sftl->checkpoint_ops->account_retired(sftl->checkpoint_priv, block, ++ sftl->wear[block], sftl->wear[block] + 1, APPLE_SFTL_PREPARE_RETIRED); + if (ret == -EOPNOTSUPP) { + ret = -ENOSPC; + continue; +@@ -1851,11 +1956,18 @@ static int sftl_reclaim_retired(struct apple_sftl *sftl, u32 *claimed, + break; + __set_bit(block, sftl->used); + ret = sftl_erase_verified(sftl, block, deadline); ++ if (!ret && sftl->native_context) ++ ret = sftl->checkpoint_ops->account_retired(sftl->checkpoint_priv, block, ++ sftl->wear[block], sftl->wear[block] + 1, APPLE_SFTL_FINISH_RETIRED); + if (ret) { +- /* Never reissue an ambiguous, possibly partial erase. */ ++ /* A partial erase or failed accounting leaves reuse unproved. */ + sftl->failed = true; + break; + } ++ if (sftl->native_context) { ++ sftl->roles[block] = SFTL_CLEAN_FREE; ++ sftl->wear[block]++; ++ } + *claimed = block; + break; + } +@@ -1865,6 +1977,12 @@ static int sftl_reclaim_retired(struct apple_sftl *sftl, u32 *claimed, + return ret; + } + ++static bool sftl_native_data_site(struct apple_sftl *sftl, u32 block) ++{ ++ return !sftl->native_context || (block < SFTL_BLOCKS - 32 && ++ !test_bit(block, sftl->unknown_lane_flags)); ++} ++ + static int sftl_check_landing(struct apple_sftl *sftl, u32 block, unsigned long deadline) + { + bool blank; +@@ -1874,6 +1992,7 @@ static int sftl_check_landing(struct apple_sftl *sftl, u32 block, unsigned long + * in this transaction after verifying an ordinary closed donor. + */ + if (!block || block >= SFTL_SUBBLOCKS || !sftl->lanes[block] || ++ !sftl_native_data_site(sftl, block) || + (sftl->native_context && sftl->roles[block] != SFTL_CLEAN_FREE && + sftl->roles[block] != SFTL_CLOSED_DATA && sftl->roles[block] != SFTL_DIRTY_FREE) || + test_bit(block, sftl->reserved) || test_bit(block, sftl->vfl_reserved) || +@@ -1892,6 +2011,7 @@ static int sftl_take_blank(struct apple_sftl *sftl, u32 *out, unsigned long dead + while (sftl->next_candidate < SFTL_SUBBLOCKS) { + block = sftl->next_candidate++; + if (!test_bit(block, sftl->candidates) || test_bit(block, sftl->used) || ++ !sftl_native_data_site(sftl, block) || + (sftl->native_context && (sftl->roles[block] != SFTL_CLEAN_FREE || + test_bit(block, sftl->fallback_occupied))) || + (sftl->checkpoint_ops && test_bit(block, sftl->checkpoint_used)) || +@@ -2047,6 +2167,7 @@ static void sftl_adopt_checkpoint(struct apple_sftl *sftl, struct apple_sftl *co + sftl->native_context = cold->native_context; + memcpy(sftl->roles, cold->roles, sizeof(sftl->roles)); + memcpy(sftl->block_flags, cold->block_flags, sizeof(sftl->block_flags)); ++ memcpy(sftl->wear, cold->wear, sizeof(sftl->wear)); + for (block = 0; block < ARRAY_SIZE(sftl->candidates); block++) + sftl->candidates[block] &= cold->candidates[block]; + memcpy(sftl->fallback_occupied, cold->fallback_occupied, sizeof(sftl->fallback_occupied)); +@@ -2061,10 +2182,8 @@ static void sftl_adopt_checkpoint(struct apple_sftl *sftl, struct apple_sftl *co + __set_bit(block, sftl->used); + } + +-/* A native save on this profile uses two named normal-view context blocks. +- * Only accept a complete generation starting at page zero of the active block, +- * with a blank second block. The old generation remains readable while the +- * alternate is programmed. Both blocks must be exclusively context-owned. ++/* The native base lists capacity allocated to this generation. Retirement ++ * operates on the previous generation's occupied sites independently. + */ + static bool sftl_owns_context_block(struct apple_sftl *sftl, u32 block) + { +@@ -2074,25 +2193,23 @@ static bool sftl_owns_context_block(struct apple_sftl *sftl, u32 block) + !test_bit(block, sftl->unknown_lane_flags) && !test_bit(block, sftl->checkpoint_used); + } + +-static int sftl_native_pair(struct apple_sftl *sftl, const struct apple_sftl_snapshot *saved, +- u32 blocks[2], unsigned long deadline) ++static int sftl_native_sites(struct apple_sftl *sftl, const struct apple_sftl_snapshot *saved, ++ const u32 fresh[2], u32 *retiring, unsigned long deadline) + { + const u8 *base = saved->opaque[0].data; + u64 length; +- u32 slots, i, j; ++ u32 prior[2], slots, i, j; + unsigned int chunk; + bool blank; + int ret; + + if (get_unaligned_le32(base) != 2 || !sftl->checkpoint_start) + return -EOPNOTSUPP; +- blocks[0] = get_unaligned_le32(base + 8); +- blocks[1] = get_unaligned_le32(base + 4); +- if (!apple_sftl_account_context_pair(saved, blocks)) +- return -EOPNOTSUPP; +- if (blocks[0] == blocks[1] || !sftl_owns_context_block(sftl, blocks[0]) || +- !sftl_owns_context_block(sftl, blocks[1]) || +- sftl->context.items[0].address != blocks[1] * SFTL_STRIDE) ++ prior[0] = get_unaligned_le32(base + 4); ++ prior[1] = get_unaligned_le32(base + 8); ++ if (!apple_sftl_account_context_pair(saved, prior) || ++ !sftl_owns_context_block(sftl, prior[0]) || !sftl_owns_context_block(sftl, prior[1]) || ++ sftl->context.items[0].address != prior[0] * SFTL_STRIDE) + return -EOPNOTSUPP; + for (i = 0; i < saved->nr_runs; i++) { + const struct apple_sftl_extent *r = &saved->runs[i]; +@@ -2102,17 +2219,18 @@ static int sftl_native_pair(struct apple_sftl *sftl, const struct apple_sftl_sna + continue; + end = (r->provider + r->length - 1) / SFTL_STRIDE; + for (j = 0; j < 2; j++) +- if (r->provider / SFTL_STRIDE <= blocks[j] && blocks[j] <= end) ++ if ((r->provider / SFTL_STRIDE <= fresh[j] && fresh[j] <= end) || ++ (r->provider / SFTL_STRIDE <= prior[j] && prior[j] <= end)) + return -EBADMSG; + } + length = sftl->checkpoint_sequence - sftl->checkpoint_start + 1; +- slots = hweight16(sftl->lanes[blocks[1]]) * 512; ++ slots = hweight16(sftl->lanes[prior[0]]) * 512; + if (!length || length % 4 || length > slots) + return -EOPNOTSUPP; + for (i = 0; i < slots; i += 4) { + if (time_after_eq(jiffies, deadline)) + return -ETIMEDOUT; +- ret = sftl_read_address(sftl, blocks[1] * SFTL_STRIDE + i, &chunk); ++ ret = sftl_read_address(sftl, prior[0] * SFTL_STRIDE + i, &chunk); + if (ret) + return ret; + if (i >= length) { +@@ -2129,8 +2247,19 @@ static int sftl_native_pair(struct apple_sftl *sftl, const struct apple_sftl_sna + return -EOPNOTSUPP; + } + } +- ret = sftl_block_blank(sftl, blocks[0], deadline, &blank); +- return ret ? ret : (blank ? 0 : -EOPNOTSUPP); ++ /* Require a single occupied old site and check all the fresh capacity ++ * before the first data copy. The unused old reservation is left alone. ++ */ ++ ret = sftl_block_blank(sftl, prior[1], deadline, &blank); ++ if (ret || !blank) ++ return ret ? ret : -EOPNOTSUPP; ++ for (i = 0; i < 2; i++) { ++ ret = sftl_block_blank(sftl, fresh[i], deadline, &blank); ++ if (ret || !blank) ++ return ret ? ret : -EOPNOTSUPP; ++ } ++ *retiring = prior[0]; ++ return 0; + } + + static int sftl_context_program(void *priv, u32 address, const struct apple_sftl_gc_page *page) +@@ -2142,9 +2271,20 @@ static int sftl_context_program(void *priv, u32 address, const struct apple_sftl + return sftl_program_address(move->sftl, address, page->data, page->oob); + } + ++static int sftl_context_stats(void *priv, u32 id, u8 *data, size_t bytes) ++{ ++ struct sftl_move *move = priv; ++ struct apple_sftl *sftl = move->sftl; ++ ++ if (time_after_eq(jiffies, move->deadline)) ++ return -ETIMEDOUT; ++ return sftl->checkpoint_ops->read_native_stats(sftl->checkpoint_priv, id, data, bytes); ++} ++ + static const struct apple_sftl_gc_io sftl_context_io = { + .read = sftl_move_read, + .program = sftl_context_program, ++ .read_native_stats = sftl_context_stats, + }; + + static int sftl_reload_plan(struct apple_sftl *sftl, struct apple_sftl_gc *gc, +@@ -2167,7 +2307,8 @@ static int sftl_reload_plan(struct apple_sftl *sftl, struct apple_sftl_gc *gc, + return ret; + } + +-static int sftl_compact(struct apple_sftl *sftl, u32 landing, unsigned long deadline) ++static int sftl_compact(struct apple_sftl *sftl, u32 landing, unsigned long deadline, ++ const u32 *fresh) + { + struct apple_sftl_gc_block destination = { + .block = landing, .lanes = sftl->lanes[landing], .blank = true, +@@ -2177,7 +2318,7 @@ static int sftl_compact(struct apple_sftl *sftl, u32 landing, unsigned long dead + struct apple_sftl_snapshot *before = NULL; + struct apple_sftl_gc *gc = NULL; + struct apple_sftl *cold = NULL; +- u32 blocks[SFTL_MAX_CONTEXTS], nr = ARRAY_SIZE(blocks), block, slots, i; ++ u32 blocks[SFTL_MAX_CONTEXTS], nr = ARRAY_SIZE(blocks), block, slots, i, retiring = 0; + int ret = -ENOSPC; + + for (block = 1; block < SFTL_BLOCKS - 32; block++) { +@@ -2220,7 +2361,8 @@ static int sftl_compact(struct apple_sftl *sftl, u32 landing, unsigned long dead + } + if (sftl->checkpoint_ops->prepare_native) { + nr = 2; +- ret = sftl_native_pair(sftl, before, blocks, deadline); ++ memcpy(blocks, fresh, nr * sizeof(*fresh)); ++ ret = sftl_native_sites(sftl, before, blocks, &retiring, deadline); + if (!ret) + ret = apple_sftl_gc_prepare_native(gc, sftl->checkpoint_ops->prepare_native, + sftl->checkpoint_priv, blocks, hweight16(sftl->lanes[blocks[0]]) * 512); +@@ -2237,7 +2379,7 @@ static int sftl_compact(struct apple_sftl *sftl, u32 landing, unsigned long dead + u32 k; + + if (!blocks[i] || blocks[i] >= SFTL_SUBBLOCKS || blocks[i] == block || +- blocks[i] == landing || !test_bit(blocks[i], sftl->reserved)) { ++ blocks[i] == landing || (!fresh && !test_bit(blocks[i], sftl->reserved))) { + ret = -EINVAL; + goto out; + } +@@ -2260,20 +2402,21 @@ static int sftl_compact(struct apple_sftl *sftl, u32 landing, unsigned long dead + } + goto failed; + } +- ret = apple_sftl_gc_checkpoint(gc, blocks, nr, &image); +- if (ret) +- goto failed; +- if (sftl->checkpoint_ops->prepare_native) +- ret = apple_sftl_checkpoint_stage(image, blocks[0], sftl->lanes[blocks[0]], +- &sftl_context_io, &move, &readback); +- else ++ if (sftl->checkpoint_ops->prepare_native) { ++ ret = apple_sftl_gc_stage_native(gc, blocks[0], sftl->lanes[blocks[0]], ++ &sftl_context_io, &move, &readback); ++ } else { ++ ret = apple_sftl_gc_checkpoint(gc, blocks, nr, &image); ++ if (ret) ++ goto failed; + ret = sftl->checkpoint_ops->install(sftl->checkpoint_priv, image, &readback); +- if (readback == image) { +- readback = NULL; +- ret = -EBADMSG; +- } else if (readback && readback->records == image->records) { +- readback->records = NULL; +- ret = -EBADMSG; ++ if (readback == image) { ++ readback = NULL; ++ ret = -EBADMSG; ++ } else if (readback && readback->records == image->records) { ++ readback->records = NULL; ++ ret = -EBADMSG; ++ } + } + if (ret) + goto failed; +@@ -2291,12 +2434,24 @@ static int sftl_compact(struct apple_sftl *sftl, u32 landing, unsigned long dead + if (!cold->native_context || cold->roles[landing] != SFTL_OPEN_DATA || + cold->roles[block] != SFTL_DIRTY_FREE || + cold->context.items[0].address != blocks[0] * SFTL_STRIDE || ++ cold->roles[retiring] != SFTL_CONTEXT || !sftl_owns_context_block(cold, retiring) || + !sftl_owns_context_block(cold, blocks[0]) || + !sftl_owns_context_block(cold, blocks[1])) { + ret = -EBADMSG; + goto failed; + } +- ret = sftl_erase_verified(sftl, blocks[1], deadline); ++ ret = sftl_erase_verified(sftl, retiring, deadline); ++ if (ret) ++ goto failed; ++ /* The persisted table precedes retirement. Reconcile only ++ * the private runtime copy after the old erase succeeds. ++ * The unused tail of the new group needs no physical erase. ++ */ ++ ret = apple_sftl_account_release(readback, retiring, ++ APPLE_SFTL_RELEASED_CONTEXT); ++ if (!ret) ++ ret = apple_sftl_account_release(readback, blocks[1], ++ APPLE_SFTL_RELEASED_CONTEXT); + if (ret) + goto failed; + ret = sftl_reload_plan(sftl, gc, &cold); +@@ -2313,6 +2468,14 @@ static int sftl_compact(struct apple_sftl *sftl, u32 landing, unsigned long dead + ret = sftl_erase_verified(sftl, block, deadline); + if (ret) + goto failed; ++ if (sftl->native_context) { ++ ret = apple_sftl_account_release(readback, block, APPLE_SFTL_ERASED_DONOR); ++ if (!ret) ++ ret = sftl->checkpoint_ops->finish_native(sftl->checkpoint_priv, ++ readback, retiring, blocks[1], block); ++ if (ret) ++ goto failed; ++ } + sftl_adopt_checkpoint(sftl, cold); + sftl->spare = block; + goto out; +@@ -2333,7 +2496,8 @@ static bool sftl_can_compact(struct apple_sftl *sftl) + { + u32 block; + +- if (!sftl->checkpoint_ops || !sftl->ppn->ops->erase_block) ++ if (!sftl->checkpoint_ops || !sftl->ppn->ops->erase_block || ++ (!sftl->checkpoint_ops->prepare_native && !sftl->checkpoint_ops->install)) + return false; + for (block = 1; block < SFTL_BLOCKS - 32; block++) + if (sftl_closed_candidate(sftl, block) && test_bit(block, sftl->checkpoint_used)) +@@ -2341,9 +2505,9 @@ static bool sftl_can_compact(struct apple_sftl *sftl) + return false; + } + +-static int sftl_claim_block(struct apple_sftl *sftl) ++static int sftl_claim_data_block(struct apple_sftl *sftl, bool compact, const u32 *fresh, ++ unsigned long deadline) + { +- unsigned long deadline = jiffies + SFTL_CLAIM_TIMEOUT; + u32 block, spare; + int ret; + +@@ -2363,7 +2527,7 @@ static int sftl_claim_block(struct apple_sftl *sftl) + return ret; + } + /* Only reserve landing space when a complete commit backend exists. */ +- if (!test_bit(block, sftl->unknown_lane_flags) && sftl_can_compact(sftl)) { ++ if (!test_bit(block, sftl->unknown_lane_flags) && compact) { + ret = sftl_take_blank(sftl, &spare, deadline); + if (!ret) + sftl->spare = spare; +@@ -2382,7 +2546,7 @@ static int sftl_claim_block(struct apple_sftl *sftl) + sftl->spare = block; + return ret; + } +- ret = sftl_compact(sftl, block, deadline); ++ ret = sftl_compact(sftl, block, deadline, fresh); + if (!ret || sftl->failed) + return ret; + if (ret != -ENOSPC && ret != -EOPNOTSUPP) { +@@ -2399,14 +2563,66 @@ static int sftl_claim_block(struct apple_sftl *sftl) + return 0; + } + ++static int sftl_claim_block(struct apple_sftl *sftl) ++{ ++ unsigned long deadline = jiffies + SFTL_CLAIM_TIMEOUT; ++ bool compact = sftl_can_compact(sftl); ++ u32 fresh[2], count = ARRAY_SIZE(fresh), i; ++ int ret; ++ ++ if (!compact || !sftl->native_context) ++ return sftl_claim_data_block(sftl, compact, NULL, deadline); ++ /* A healthy compacting writer needs space for both user data and a ++ * distinct new context group. Reserve in memory before choosing data ++ * space so a blank context site cannot also become the landing block. ++ */ ++ ret = sftl->checkpoint_ops->reserve(sftl->checkpoint_priv, fresh, &count); ++ if (ret == -ENOSPC || ret == -EOPNOTSUPP) ++ return sftl_claim_data_block(sftl, false, NULL, deadline); ++ if (ret) ++ return ret; ++ if (count != ARRAY_SIZE(fresh) || fresh[0] == fresh[1]) ++ return -EBADMSG; ++ for (i = 0; i < count; i++) { ++ u32 b = fresh[i]; ++ ++ if (!b || b >= SFTL_BLOCKS - 32 || !sftl->lanes[b] || ++ sftl->roles[b] != SFTL_CLEAN_FREE || !test_bit(b, sftl->candidates) || ++ test_bit(b, sftl->used) || test_bit(b, sftl->checkpoint_used) || ++ test_bit(b, sftl->reserved) || test_bit(b, sftl->vfl_reserved) || ++ test_bit(b, sftl->unknown_lane_flags) || test_bit(b, sftl->fallback_occupied)) ++ return -EBADMSG; ++ } ++ for (i = 0; i < count; i++) ++ __set_bit(fresh[i], sftl->used); ++ ret = sftl_claim_data_block(sftl, true, fresh, deadline); ++ for (i = 0; i < count; i++) { ++ if (test_bit(fresh[i], sftl->reserved)) ++ continue; ++ __clear_bit(fresh[i], sftl->used); ++ sftl->next_candidate = min(sftl->next_candidate, fresh[i]); ++ } ++ if (ret == -ENOSPC && !sftl->failed) ++ return sftl_claim_data_block(sftl, false, NULL, deadline); ++ return ret; ++} ++ + void apple_sftl_set_checkpoint_ops(struct apple_sftl *sftl, + const struct apple_sftl_checkpoint_ops *ops, void *priv) + { + mutex_lock(&sftl->lock); + sftl->checkpoint_ops = NULL; +- if (ops && ((sftl->native_context && ops->prepare_native && !ops->reserve && !ops->install) || +- (!sftl->native_context && !ops->prepare_native && ops->reserve && ops->install))) +- sftl->checkpoint_ops = ops; ++ if (ops) { ++ bool mixed = ops->prepare_native || ops->read_native_stats || ops->finish_native; ++ ++ if (sftl->native_context && !ops->install) { ++ if ((ops->prepare_native && ops->read_native_stats && ops->finish_native && ops->reserve) || ++ (ops->account_retired && !mixed && !ops->reserve)) ++ sftl->checkpoint_ops = ops; ++ } else if (!sftl->native_context && !mixed && !ops->account_retired && ops->reserve && ops->install) { ++ sftl->checkpoint_ops = ops; ++ } ++ } + sftl->checkpoint_priv = priv; + mutex_unlock(&sftl->lock); + } +diff --git a/drivers/mtd/apple-sftl-reclaim.c b/drivers/mtd/apple-sftl-reclaim.c +index e642acd59..70ebba4e4 100644 +--- a/drivers/mtd/apple-sftl-reclaim.c ++++ b/drivers/mtd/apple-sftl-reclaim.c +@@ -160,9 +160,9 @@ void apple_sftl_checkpoint_free(struct apple_sftl_checkpoint *checkpoint) + } + EXPORT_SYMBOL_GPL(apple_sftl_checkpoint_free); + +-int apple_sftl_checkpoint_build(const struct apple_sftl_snapshot *snapshot, +- const u32 *blocks, u32 nr_blocks, +- u32 max_records, struct apple_sftl_checkpoint **out) ++static int sftl_checkpoint_build(const struct apple_sftl_snapshot *snapshot, ++ const u32 *blocks, u32 nr_blocks, u32 max_records, ++ bool preserve_cached_stats, struct apple_sftl_checkpoint **out) + { + struct apple_sftl_checkpoint *checkpoint; + u32 count, i, j, cursor = 0, run = 0, old_names; +@@ -231,7 +231,8 @@ int apple_sftl_checkpoint_build(const struct apple_sftl_snapshot *snapshot, + } + } + if (sftl_native_context(snapshot)) { +- ret = apple_sftl_account_map(checkpoint, snapshot); ++ ret = preserve_cached_stats ? apple_sftl_account_saved_map(checkpoint, snapshot) : ++ apple_sftl_account_map(checkpoint, snapshot); + if (ret) { + apple_sftl_checkpoint_free(checkpoint); + return ret; +@@ -240,6 +241,13 @@ int apple_sftl_checkpoint_build(const struct apple_sftl_snapshot *snapshot, + *out = checkpoint; + return 0; + } ++ ++int apple_sftl_checkpoint_build(const struct apple_sftl_snapshot *snapshot, ++ const u32 *blocks, u32 nr_blocks, ++ u32 max_records, struct apple_sftl_checkpoint **out) ++{ ++ return sftl_checkpoint_build(snapshot, blocks, nr_blocks, max_records, false, out); ++} + EXPORT_SYMBOL_GPL(apple_sftl_checkpoint_build); + + static bool sftl_overlap(u32 first, u32 length, u32 start, u32 end) +@@ -480,9 +488,10 @@ static int sftl_gc_program(struct apple_sftl_gc_page *buffer, + return memcmp(page, buffer, sizeof(*page)) ? -EBADMSG : 0; + } + +-int apple_sftl_checkpoint_stage(const struct apple_sftl_checkpoint *image, +- u32 block, u16 lanes, const struct apple_sftl_gc_io *io, +- void *priv, struct apple_sftl_checkpoint **readback) ++static int sftl_checkpoint_stage(const struct apple_sftl_checkpoint *image, ++ u32 block, u16 lanes, const struct apple_sftl_gc_io *io, ++ void *priv, struct apple_sftl_checkpoint **readback, ++ struct apple_sftl_gc *gc) + { + struct apple_sftl_checkpoint *copy; + struct apple_sftl_gc_page *pages; +@@ -547,6 +556,15 @@ int apple_sftl_checkpoint_stage(const struct apple_sftl_checkpoint *image, + } + } + for (i = 0; i < image->count; i += 4) { ++ /* 13G36 samples both providers after the one-page base, before ++ * formatting the statistics page. Keep these samples in the GC's ++ * expected image for the independent readback comparison. ++ */ ++ if (gc && i == 4) { ++ ret = apple_sftl_account_read_stats(gc->checkpoint, io, priv); ++ if (ret) ++ goto out; ++ } + for (j = 0; j < 4; j++) { + memcpy(pages[1].data + j * APPLE_SFTL_CHUNK, + image->records[i + j].data, APPLE_SFTL_CHUNK); +@@ -573,8 +591,34 @@ out: + apple_sftl_checkpoint_free(copy); + return ret; + } ++ ++int apple_sftl_checkpoint_stage(const struct apple_sftl_checkpoint *image, ++ u32 block, u16 lanes, const struct apple_sftl_gc_io *io, ++ void *priv, struct apple_sftl_checkpoint **readback) ++{ ++ return sftl_checkpoint_stage(image, block, lanes, io, priv, readback, NULL); ++} + EXPORT_SYMBOL_GPL(apple_sftl_checkpoint_stage); + ++int apple_sftl_gc_stage_native(struct apple_sftl_gc *gc, u32 block, u16 lanes, ++ const struct apple_sftl_gc_io *io, void *priv, ++ struct apple_sftl_checkpoint **readback) ++{ ++ int ret; ++ ++ if (!readback) ++ return -EINVAL; ++ *readback = NULL; ++ if (!gc || gc->stage != APPLE_SFTL_GC_CHECKPOINT || gc->issued || ++ !gc->checkpoint || !io || !io->read_native_stats) ++ return -EINVAL; ++ gc->issued = true; ++ ret = sftl_checkpoint_stage(gc->checkpoint, block, lanes, io, priv, readback, gc); ++ if (ret) ++ gc->stage = APPLE_SFTL_GC_FAILED; ++ return ret; ++} ++ + /* No erase or map publication occurs here. Even an indeterminate program + * consumes the transaction: the caller must recover before writing again. + */ +@@ -669,8 +713,6 @@ int apple_sftl_gc_prepare_native(struct apple_sftl_gc *gc, + if (!gc || !prepare || !blocks || !capacity || gc->stage != APPLE_SFTL_GC_COPY || + gc->copied || gc->checkpoint) + return -EINVAL; +- if (!apple_sftl_account_context_pair(&gc->snapshot, blocks)) +- return -EBADMSG; + /* The policy may change several fields before refusing a transition. + * Keep its edits private so retries always begin with the original state. + */ +@@ -683,8 +725,8 @@ int apple_sftl_gc_prepare_native(struct apple_sftl_gc *gc, + ret = prepare(priv, staged.opaque, staged.nr_opaque, &staged, + gc->victim, gc->destination, blocks); + if (!ret) +- ret = apple_sftl_checkpoint_build(&staged, blocks, 2, +- min(gc->max_records, capacity), &image); ++ ret = sftl_checkpoint_build(&staged, blocks, 2, min(gc->max_records, capacity), ++ true, &image); + if (!ret && !apple_sftl_account_gc_image(&gc->snapshot, image, blocks, + gc->victim, gc->destination, + apple_sftl_gc_copy_chunks(gc) - gc->padding, +diff --git a/drivers/mtd/apple-sftl-reclaim.h b/drivers/mtd/apple-sftl-reclaim.h +index b74d53bcd..a13ff96b9 100644 +--- a/drivers/mtd/apple-sftl-reclaim.h ++++ b/drivers/mtd/apple-sftl-reclaim.h +@@ -85,6 +85,8 @@ struct apple_sftl_gc_page { + struct apple_sftl_gc_io { + int (*read)(void *priv, u32 address, struct apple_sftl_gc_page *page); + int (*program)(void *priv, u32 address, const struct apple_sftl_gc_page *page); ++ /* Native context saves only. Return bytes copied or a negative errno. */ ++ int (*read_native_stats)(void *priv, u32 id, u8 *data, size_t bytes); + }; + + /* Write a prepared context to an exclusively owned, entirely blank block. +@@ -94,10 +96,13 @@ struct apple_sftl_gc_io { + int apple_sftl_checkpoint_stage(const struct apple_sftl_checkpoint *image, + u32 block, u16 lanes, const struct apple_sftl_gc_io *io, + void *priv, struct apple_sftl_checkpoint **readback); ++int apple_sftl_gc_stage_native(struct apple_sftl_gc *gc, u32 block, u16 lanes, ++ const struct apple_sftl_gc_io *io, void *priv, ++ struct apple_sftl_checkpoint **readback); + + /* Platform policy must account for the native roles, streams and counters of + * the transition, or refuse it before any live data is moved. The two +- * context blocks are in new-active, old-active order. ++ * context blocks are fresh reservations for the new generation. + */ + typedef int (*apple_sftl_prepare_accounting)(void *priv, + struct apple_sftl_context_record *opaque, u32 nr_opaque, +diff --git a/drivers/mtd/apple-sftl-test.c b/drivers/mtd/apple-sftl-test.c +index b44cca5c6..a11071b79 100644 +--- a/drivers/mtd/apple-sftl-test.c ++++ b/drivers/mtd/apple-sftl-test.c +@@ -9,6 +9,7 @@ + #include + + #include "apple-sftl.h" ++#include "apple-sftl-provider.h" + #include "apple-sftl-reclaim.h" + #include "apple-sftl-test-fixture.h" + #include "apple-sftl-vfl.h" +@@ -19,6 +20,9 @@ + #define TEST_CLOSED_FIRST 102ULL + #define TEST_CLOSED_CHUNKS 1020U + #define TEST_OPEN_FIRST (TEST_CLOSED_FIRST + TEST_CLOSED_CHUNKS) ++#define TEST_NATIVE_ACTIVE 8U ++#define TEST_NATIVE_SPARE 9U ++#define TEST_NATIVE_SLOT 5U + + enum sftl_fault { + SFTL_NO_FAULT, +@@ -77,18 +81,28 @@ struct sftl_fixture { + bool fail_reserve_once; + unsigned int corrupt_stage_readback; + unsigned int native_erase_cut, native_policy_calls; ++ unsigned int stats_calls, fail_stats_id; ++ bool short_stats, base_read_back, corrupt_stats_readback, native_group; ++ struct apple_sftl_provider_stats *event_stats; ++ u32 native_reservations[2]; ++ int native_reserve_error; ++ unsigned int finish_calls; ++ int finish_error; ++ unsigned int retired_prepares, retired_finishes; ++ int retired_prepare_error, retired_finish_error; + u16 erased_context_lanes; + bool loaded_before_retirement, loaded_after_retirement; + int native_policy_error; +- bool drop_context_role, keep_donor_role, duplicate_stream; ++ bool drop_context_role, keep_donor_role, duplicate_stream, change_old_context; + unsigned int installs, recovery_reads; + enum sftl_test_commit commit; + bool dirty_candidate; + bool vfl_candidate; + bool full_lanes; ++ bool remnant_lanes, unread_remnant, unknown_candidate; + u16 program_lanes; + u64 sequence_bias; +- struct sftl_test_page **written[5]; ++ struct sftl_test_page **written[7]; + struct apple_sftl_checkpoint *checkpoint; + }; + +@@ -172,6 +186,16 @@ static int sftl_test_context(struct sftl_fixture *f, unsigned int lane, + put_unaligned_le16(0, data + i * 2); + if (page == 1 && f->checkpoint_slot) + put_unaligned_le16(0, data + 7 * 2); ++ if (page == 1 && f->native_group) { ++ put_unaligned_le16(0, data + TEST_NATIVE_ACTIVE * 2); ++ put_unaligned_le16(0, data + TEST_NATIVE_SPARE * 2); ++ } ++ if (page == 2 && f->remnant_lanes) ++ put_unaligned_le16(0, data + TEST_NATIVE_SPARE * 2); ++ if (page == 1 && f->unknown_candidate) { ++ put_unaligned_le16(1, data + 6 * 2); ++ put_unaligned_le16(0, data + 2057 * 2); ++ } + if (f->full_lanes) + put_unaligned_le16(0, data + 5 * 2); + if (page == 1 && f->vfl_candidate) { +@@ -283,6 +307,8 @@ static int sftl_test_candidate_slot(unsigned int block) + return block - 5; + if (block == 2056 || block == 2057) + return block - 2053; ++ if (block == TEST_NATIVE_ACTIVE || block == TEST_NATIVE_SPARE) ++ return TEST_NATIVE_SLOT + block - TEST_NATIVE_ACTIVE; + return -1; + } + +@@ -310,7 +336,8 @@ static int sftl_test_read_page(void *priv, unsigned int bus, unsigned int cau, + memset(data, 0xff, 16384); + memset(oob, 0xff, 64); + *status = 0x49; +- if (context && block == 2087 && !lane && !page && sftl_test_written(f, 2, 0, 23)) { ++ if (context && block == 2087 && !lane && !page && ++ sftl_test_written(f, TEST_NATIVE_SLOT, 0, 23)) { + if (!f->erased_context_lanes) + f->loaded_before_retirement = true; + if (f->erased_context_lanes == 3) +@@ -318,6 +345,8 @@ static int sftl_test_read_page(void *priv, unsigned int bus, unsigned int cau, + } + if (!context && block == 1 && (f->erased_context_lanes & (1U << lane))) + return 0; ++ if (!context && f->unread_remnant && block == TEST_NATIVE_SPARE && lane == 1 && !page) ++ return -EOPNOTSUPP; + if (context) { + if (block == 2087 || (f->vfl_candidate && block == 2056 && !lane)) + return sftl_test_context(f, lane, page, data, oob, status); +@@ -339,8 +368,12 @@ static int sftl_test_read_page(void *priv, unsigned int bus, unsigned int cau, + if (slot >= 0 && sftl_test_written(f, slot, lane, page)) { + struct sftl_test_page *written = sftl_test_written(f, slot, lane, page); + ++ if (block == TEST_NATIVE_ACTIVE && !lane && !page) ++ f->base_read_back = true; + memcpy(data, written->data, APPLE_PPN_PAGE_SIZE); + memcpy(oob, written->oob, APPLE_PPN_OOB_SIZE); ++ if (f->corrupt_stats_readback && block == TEST_NATIVE_ACTIVE && !lane && page == 1) ++ data[sizeof(u32) + 43 * 16 + 8] ^= 1; + *status = 0x40; + return 0; + } +@@ -421,6 +454,20 @@ static int sftl_test_program_page(void *priv, unsigned int bus, unsigned int cau + written->oob[63] ^= 1; + return -EIO; + } ++ if (!failure && f->event_stats) { ++ int ret; ++ ++ /* In this synthetic provider each completed program has one native ++ * four-chunk request and one lower row with a successful status. ++ */ ++ ret = apple_sftl_provider_vfl(f->event_stats, APPLE_SFTL_VFL_PROGRAM, 4); ++ if (!ret) ++ ret = apple_sftl_provider_submit(f->event_stats, APPLE_SFTL_LOWER_PROGRAM, 1); ++ if (!ret) ++ ret = apple_sftl_provider_complete(f->event_stats, APPLE_SFTL_LOWER_PROGRAM, ++ 0x40, NULL, 1); ++ return ret; ++ } + return failure ? -EIO : 0; + } + +@@ -2031,6 +2078,10 @@ static int sftl_test_native_context(struct sftl_fixture *f) + map.opaque[8].data[8 * 5] = 3; + map.opaque[8].data[8 * 5 + 1] = 64; + map.opaque[8].data[8 * 6] = 1; ++ if (f->native_group) { ++ map.opaque[8].data[8 * TEST_NATIVE_ACTIVE] = 1; ++ map.opaque[8].data[8 * TEST_NATIVE_SPARE] = 1; ++ } + put_unaligned_le32(9, map.opaque[28].data + 5 * sizeof(u32)); + ret = apple_sftl_checkpoint_build(&map, blocks, 2, 96, &image); + kvfree(map.opaque); +@@ -2053,9 +2104,25 @@ static int sftl_test_native_medium(struct sftl_fixture *f, u8 *data) + { + int ret = sftl_test_gc_live_medium(f, data); + ++ f->native_group = true; ++ f->native_reservations[0] = TEST_NATIVE_ACTIVE; ++ f->native_reservations[1] = TEST_NATIVE_SPARE; + return ret ? ret : sftl_test_native_context(f); + } + ++static int sftl_test_reserve_native(void *priv, u32 *blocks, u32 *count) ++{ ++ struct sftl_fixture *f = priv; ++ ++ if (f->native_reserve_error) ++ return f->native_reserve_error; ++ if (*count < ARRAY_SIZE(f->native_reservations)) ++ return -ENOSPC; ++ memcpy(blocks, f->native_reservations, sizeof(f->native_reservations)); ++ *count = ARRAY_SIZE(f->native_reservations); ++ return 0; ++} ++ + /* Only this synthetic medium authorizes the remaining stream/role state. */ + static int sftl_test_prepare_native(void *priv, struct apple_sftl_context_record *records, + u32 count, const struct apple_sftl_snapshot *map, +@@ -2065,10 +2132,17 @@ static int sftl_test_prepare_native(void *priv, struct apple_sftl_context_record + + f->native_policy_calls++; + if (count != 32 || !map->nr_runs || records[0].meta[1] != 1 || +- blocks[0] != 7 || blocks[1] != 1 || victim != 5 || landing != 6) ++ blocks[0] != TEST_NATIVE_ACTIVE || blocks[1] != TEST_NATIVE_SPARE || ++ victim != 5 || landing != 6) + return -EINVAL; + if (records[8].data[8 * victim] != 3 || records[8].data[8 * landing] != 1) + return -EINVAL; ++ if (records[8].data[8 * blocks[0]] != 1 || records[8].data[8 * blocks[1]] != 1) ++ return -EINVAL; ++ records[8].data[8 * blocks[0]] = 7; ++ records[8].data[8 * blocks[1]] = 7; ++ if (f->change_old_context) ++ records[8].data[8 * 1 + 6]++; + if (f->drop_context_role) + records[8].data[8 * blocks[1]] = 9; + if (!f->keep_donor_role) +@@ -2080,8 +2154,57 @@ static int sftl_test_prepare_native(void *priv, struct apple_sftl_context_record + return f->native_policy_error; + } + ++static int sftl_test_read_native_stats(void *priv, u32 id, u8 *data, size_t bytes) ++{ ++ struct sftl_fixture *f = priv; ++ size_t i; ++ ++ if (++f->stats_calls > 2 || !f->base_read_back || f->erases || ++ !sftl_test_written(f, 1, 0, 1) || sftl_test_written(f, TEST_NATIVE_SLOT, 0, 1) || ++ (f->stats_calls == 1 ? (id != 255 || bytes != 88) : (id != 254 || bytes != 464))) ++ return -EINVAL; ++ if (f->event_stats) ++ return apple_sftl_provider_read_stats(f->event_stats, id, data, bytes); ++ for (i = 0; i < bytes; i++) ++ data[i] = (u8)(id + i); ++ if (id == f->fail_stats_id) ++ return f->short_stats ? (int)bytes - 1 : -ENODATA; ++ return bytes; ++} ++ ++static int sftl_test_finish_native(void *priv, const struct apple_sftl_checkpoint *runtime, ++ u32 old_context, u32 unused_context, u32 donor) ++{ ++ struct sftl_fixture *f = priv; ++ const u32 released[] = { old_context, unused_context, donor }; ++ struct sftl_test_page *saved = sftl_test_written(f, TEST_NATIVE_SLOT, 0, 2); ++ struct sftl_test_page *stat = sftl_test_written(f, TEST_NATIVE_SLOT, 0, 1); ++ unsigned int i; ++ ++ f->finish_calls++; ++ if (old_context != 1 || unused_context != TEST_NATIVE_SPARE || donor != 5 || ++ f->erases != 3 || !f->loaded_after_retirement || !saved || !stat || ++ sftl_test_written(f, 1, 0, 2) || sftl_test_written(f, TEST_NATIVE_SLOT + 1, 0, 0)) ++ return -EINVAL; ++ if (memcmp(runtime->records[4].data + 4 + 8 * 16, stat->data + 4 + 8 * 16, 5 * 16)) ++ return -EINVAL; ++ for (i = 0; i < ARRAY_SIZE(released); i++) { ++ const u8 *entry = runtime->records[8].data + released[i] * 8; ++ const u8 *durable = saved->data + released[i] * 8; ++ ++ if (durable[0] != (released[i] == donor ? 9 : 7) || entry[0] != 1 || ++ get_unaligned_le16(entry + 2) || ++ get_unaligned_le16(entry + 6) != get_unaligned_le16(durable + 6) + 1) ++ return -EINVAL; ++ } ++ return f->finish_error; ++} ++ + static const struct apple_sftl_checkpoint_ops sftl_test_native_ops = { ++ .reserve = sftl_test_reserve_native, + .prepare_native = sftl_test_prepare_native, ++ .read_native_stats = sftl_test_read_native_stats, ++ .finish_native = sftl_test_finish_native, + }; + + static int sftl_test_native_erase(void *priv, unsigned int bus, unsigned int cau, unsigned int block) +@@ -2127,6 +2250,8 @@ static void sftl_test_gc_native_publication(struct kunit *test) + ret = apple_sftl_write(sftl, TEST_APPEND_BASE + 5, data, 1); + KUNIT_EXPECT_EQ(test, ret, cut ? -EIO : 0); + KUNIT_EXPECT_EQ(test, f->native_policy_calls, 1U); ++ KUNIT_EXPECT_EQ(test, f->stats_calls, 2U); ++ KUNIT_EXPECT_EQ(test, f->finish_calls, cut ? 0U : 1U); + KUNIT_EXPECT_EQ(test, f->erases, cut ? cut : 3U); + KUNIT_EXPECT_EQ(test, f->erased_context_lanes, cut == 1 ? 0 : (cut == 2 ? 1 : 3)); + KUNIT_EXPECT_EQ(test, f->loaded_before_retirement, true); +@@ -2150,6 +2275,23 @@ static void sftl_test_gc_native_publication(struct kunit *test) + KUNIT_EXPECT_PTR_EQ(test, sftl_test_written(f, 0, 0, 0), NULL); + KUNIT_EXPECT_EQ(test, f->erases, cut ? cut : 3U); + apple_sftl_free(sftl); ++ if (!cut) { ++ /* Record 4 holds 43 scalar TLVs followed by the two provider blobs. */ ++ struct sftl_test_page *stats = sftl_test_written(f, TEST_NATIVE_SLOT, 0, 1); ++ u32 offset = sizeof(u32) + 43 * 16, id, i; ++ ++ KUNIT_ASSERT_NOT_NULL(test, stats); ++ for (id = 255; id >= 254; id--) { ++ u32 bytes = id == 255 ? 88 : 464; ++ ++ KUNIT_EXPECT_EQ(test, get_unaligned_le32(stats->data + offset), id); ++ KUNIT_EXPECT_EQ(test, get_unaligned_le32(stats->data + offset + 4), bytes / 4); ++ offset += 8; ++ for (i = 0; i < bytes; i++) ++ KUNIT_EXPECT_EQ(test, stats->data[offset + i], (u8)(id + i)); ++ offset += bytes; ++ } ++ } + if (cut == 2) { + /* Residual data on the old lane must predate the new generation. + * A newer tag among its surviving records cannot be discarded. +@@ -2165,6 +2307,167 @@ static void sftl_test_gc_native_publication(struct kunit *test) + } + } + ++static void sftl_test_gc_native_finish_failure(struct kunit *test) ++{ ++ struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); ++ u8 *data = kunit_kmalloc(test, APPLE_PPN_PAGE_SIZE, GFP_KERNEL); ++ struct apple_sftl_info info; ++ struct apple_sftl *sftl = NULL; ++ unsigned int programs; ++ ++ KUNIT_ASSERT_NOT_NULL(test, f); ++ KUNIT_ASSERT_NOT_NULL(test, data); ++ KUNIT_ASSERT_EQ(test, sftl_test_native_medium(f, data), 0); ++ f->ops.erase_block = sftl_test_native_erase; ++ f->finish_error = -ERANGE; ++ programs = f->programs; ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ apple_sftl_set_checkpoint_ops(sftl, &sftl_test_native_ops, f); ++ KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 5, data, 1), -ERANGE); ++ KUNIT_EXPECT_EQ(test, f->finish_calls, 1U); ++ KUNIT_EXPECT_EQ(test, f->erases, 3U); ++ KUNIT_EXPECT_EQ(test, f->programs, programs + 2 + 24); ++ KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 5, data, 1), -EIO); ++ KUNIT_EXPECT_EQ(test, f->programs, programs + 2 + 24); ++ apple_sftl_free(sftl); ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ apple_sftl_get_info(sftl, &info); ++ KUNIT_EXPECT_EQ(test, info.checkpoint_sequence, f->checkpoint->sequence + 8 + 96); ++ sftl_test_gc_relocated_data(test, sftl, data, 0); ++ apple_sftl_free(sftl); ++ apple_sftl_checkpoint_free(f->checkpoint); ++} ++ ++static void sftl_test_gc_native_spare_reuse(struct kunit *test) ++{ ++ struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); ++ u8 *data = kunit_kmalloc(test, APPLE_PPN_PAGE_SIZE, GFP_KERNEL); ++ struct apple_sftl *sftl = NULL; ++ unsigned int i, programs; ++ ++ KUNIT_ASSERT_NOT_NULL(test, f); ++ KUNIT_ASSERT_NOT_NULL(test, data); ++ KUNIT_ASSERT_EQ(test, sftl_test_native_medium(f, data), 0); ++ f->ops.erase_block = sftl_test_native_erase; ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ apple_sftl_set_checkpoint_ops(sftl, &sftl_test_native_ops, f); ++ memset(data, 'U', APPLE_SFTL_CHUNK); ++ KUNIT_ASSERT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 5, data, 1), 0); ++ /* Two copy programs and the first pending write occupy landing pages ++ * 0..2. Fill its tail and the entire one-lane, verified erased donor. ++ */ ++ for (i = 3; i < 2 * TEST_APPEND_PROGRAMS; i++) ++ KUNIT_ASSERT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 5, data, 1), 0); ++ KUNIT_ASSERT_NOT_NULL(test, sftl_test_written(f, 0, 0, TEST_APPEND_PROGRAMS - 1)); ++ programs = f->programs; ++ for (i = 0; i < 2; i++) ++ KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 5, data, 1), -ENOSPC); ++ KUNIT_EXPECT_EQ(test, f->programs, programs); ++ KUNIT_EXPECT_EQ(test, f->program_outside, 0U); ++ KUNIT_EXPECT_EQ(test, f->erases, 3U); ++ KUNIT_EXPECT_EQ(test, f->finish_calls, 1U); ++ KUNIT_EXPECT_PTR_EQ(test, sftl_test_written(f, 2, 0, 0), NULL); ++ KUNIT_EXPECT_PTR_EQ(test, sftl_test_written(f, TEST_NATIVE_SLOT + 1, 0, 0), NULL); ++ apple_sftl_free(sftl); ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ sftl_test_gc_relocated_data(test, sftl, data, 'U'); ++ apple_sftl_free(sftl); ++ apple_sftl_checkpoint_free(f->checkpoint); ++} ++ ++static void sftl_test_gc_native_event_stats(struct kunit *test) ++{ ++ struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); ++ u8 *data = kunit_kmalloc(test, APPLE_PPN_PAGE_SIZE, GFP_KERNEL); ++ struct apple_sftl *sftl = NULL; ++ struct sftl_test_page *saved; ++ u8 expected_vfl[88], expected_lower[464], *vfl, *lower; ++ size_t i; ++ ++ KUNIT_ASSERT_NOT_NULL(test, f); ++ KUNIT_ASSERT_NOT_NULL(test, data); ++ KUNIT_ASSERT_EQ(test, sftl_test_native_medium(f, data), 0); ++ f->event_stats = kunit_kmalloc(test, sizeof(*f->event_stats), GFP_KERNEL); ++ KUNIT_ASSERT_NOT_NULL(test, f->event_stats); ++ /* 43 scalar TLVs precede the provider records in native record 4. */ ++ vfl = f->checkpoint->records[4].data + sizeof(u32) + 43 * 16 + 8; ++ lower = vfl + sizeof(expected_vfl) + 8; ++ for (i = 0; i < sizeof(expected_vfl); i++) ++ expected_vfl[i] = vfl[i] = (u8)(0xf5 + i); ++ for (i = 0; i < sizeof(expected_lower); i++) ++ expected_lower[i] = lower[i] = (u8)(0xfe + i); ++ KUNIT_ASSERT_EQ(test, apple_sftl_provider_seed(f->event_stats, vfl, sizeof(expected_vfl), ++ lower, sizeof(expected_lower)), 0); ++ f->ops.erase_block = sftl_test_native_erase; ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ apple_sftl_set_checkpoint_ops(sftl, &sftl_test_native_ops, f); ++ KUNIT_ASSERT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 5, data, 1), 0); ++ KUNIT_EXPECT_EQ(test, f->stats_calls, 2U); ++ /* The measured getters see two landing programs and the base program. ++ * Later checkpoint pages and the foreground write follow this sample. ++ */ ++ put_unaligned_le64(get_unaligned_le64(expected_vfl) + 12, expected_vfl); ++ put_unaligned_le64(get_unaligned_le64(expected_vfl + 64) + 3, expected_vfl + 64); ++ put_unaligned_le64(get_unaligned_le64(expected_lower) + 3, expected_lower); ++ saved = sftl_test_written(f, TEST_NATIVE_SLOT, 0, 1); ++ KUNIT_ASSERT_NOT_NULL(test, saved); ++ vfl = saved->data + sizeof(u32) + 43 * 16 + 8; ++ lower = vfl + sizeof(expected_vfl) + 8; ++ KUNIT_EXPECT_EQ(test, memcmp(expected_vfl, vfl, sizeof(expected_vfl)), 0); ++ KUNIT_EXPECT_EQ(test, memcmp(expected_lower, lower, sizeof(expected_lower)), 0); ++ apple_sftl_free(sftl); ++ apple_sftl_checkpoint_free(f->checkpoint); ++} ++ ++static void sftl_test_gc_native_stats_failure(struct kunit *test) ++{ ++ static const struct { ++ u32 fail_id; ++ bool short_read; ++ int error; ++ } cases[] = { ++ { 255, false, -ENODATA }, { 254, false, -ENODATA }, ++ { 255, true, -EIO }, { 254, true, -EIO }, ++ { 0, false, -EBADMSG }, /* Both reads succeed. Flash readback differs. */ ++ }; ++ unsigned int i; ++ ++ for (i = 0; i < ARRAY_SIZE(cases); i++) { ++ struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); ++ u8 *data = kunit_kmalloc(test, APPLE_PPN_PAGE_SIZE, GFP_KERNEL); ++ struct apple_sftl_info info; ++ struct apple_sftl *sftl = NULL; ++ unsigned int programs; ++ ++ KUNIT_ASSERT_NOT_NULL(test, f); ++ KUNIT_ASSERT_NOT_NULL(test, data); ++ KUNIT_ASSERT_EQ(test, sftl_test_native_medium(f, data), 0); ++ f->ops.erase_block = sftl_test_native_erase; ++ f->fail_stats_id = cases[i].fail_id; ++ f->short_stats = cases[i].short_read; ++ f->corrupt_stats_readback = !f->fail_stats_id; ++ programs = f->programs; ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ apple_sftl_set_checkpoint_ops(sftl, &sftl_test_native_ops, f); ++ KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 5, data, 1), ++ cases[i].error); ++ KUNIT_EXPECT_EQ(test, f->stats_calls, f->fail_stats_id == 255 ? 1U : 2U); ++ KUNIT_EXPECT_EQ(test, f->programs, programs + (f->corrupt_stats_readback ? 4 : 3)); ++ if (!f->corrupt_stats_readback) ++ KUNIT_EXPECT_PTR_EQ(test, sftl_test_written(f, TEST_NATIVE_SLOT, 0, 1), NULL); ++ KUNIT_EXPECT_EQ(test, f->erases, 0U); ++ KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 5, data, 1), -EIO); ++ apple_sftl_free(sftl); ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ apple_sftl_get_info(sftl, &info); ++ KUNIT_EXPECT_EQ(test, info.checkpoint_sequence, f->checkpoint->sequence); ++ KUNIT_EXPECT_EQ(test, info.read_only, true); ++ sftl_test_gc_relocated_data(test, sftl, data, 0); ++ apple_sftl_free(sftl); ++ apple_sftl_checkpoint_free(f->checkpoint); ++ } ++} ++ + static void sftl_test_gc_native_prefix_cuts(struct kunit *test) + { + struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); +@@ -2184,13 +2487,13 @@ static void sftl_test_gc_native_prefix_cuts(struct kunit *test) + apple_sftl_set_checkpoint_ops(sftl, &sftl_test_native_ops, f); + KUNIT_ASSERT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 5, data, 1), -EIO); + apple_sftl_free(sftl); +- KUNIT_ASSERT_NOT_NULL(test, sftl_test_written(f, 2, 0, 23)); +- memcpy(saved, f->written[2], sizeof(saved)); ++ KUNIT_ASSERT_NOT_NULL(test, sftl_test_written(f, TEST_NATIVE_SLOT, 0, 23)); ++ memcpy(saved, f->written[TEST_NATIVE_SLOT], sizeof(saved)); + first = f->checkpoint->sequence + 9; + programs = f->programs; + for (cut = 0; cut <= ARRAY_SIZE(saved); cut++) { +- memcpy(f->written[2], saved, cut * sizeof(*saved)); +- memset(f->written[2] + cut, 0, (ARRAY_SIZE(saved) - cut) * sizeof(*saved)); ++ memcpy(f->written[TEST_NATIVE_SLOT], saved, cut * sizeof(*saved)); ++ memset(f->written[TEST_NATIVE_SLOT] + cut, 0, (ARRAY_SIZE(saved) - cut) * sizeof(*saved)); + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); + apple_sftl_get_info(sftl, &info); + KUNIT_EXPECT_EQ(test, info.checkpoint_sequence, +@@ -2236,6 +2539,10 @@ static void sftl_test_gc_native_policy_refuses(struct kunit *test) + f->duplicate_stream = true; + KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 5, data, 1), -EBADMSG); + KUNIT_EXPECT_EQ(test, f->native_policy_calls, 4U); ++ f->duplicate_stream = false; ++ f->change_old_context = true; ++ KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 5, data, 1), -EBADMSG); ++ KUNIT_EXPECT_EQ(test, f->native_policy_calls, 5U); + KUNIT_EXPECT_EQ(test, f->programs, programs); + KUNIT_EXPECT_EQ(test, f->erases, 0U); + sftl_test_gc_relocated_data(test, sftl, data, 0); +@@ -2243,6 +2550,82 @@ static void sftl_test_gc_native_policy_refuses(struct kunit *test) + apple_sftl_checkpoint_free(f->checkpoint); + } + ++static void sftl_test_gc_native_reservations(struct kunit *test) ++{ ++ static const struct { ++ u32 second; ++ u8 role; ++ int reserve_error; ++ int result; ++ } cases[] = { ++ { 7, 1, 0, -EBADMSG }, /* Overlaps the old group. */ ++ { TEST_NATIVE_ACTIVE, 1, 0, -EBADMSG }, ++ { TEST_NATIVE_SPARE, 8, 0, -EBADMSG }, ++ { TEST_NATIVE_SPARE, 1, -ENOMEM, -ENOMEM }, ++ { TEST_NATIVE_SPARE, 1, -ENOSPC, 0 }, ++ { TEST_NATIVE_SPARE, 1, -EOPNOTSUPP, 0 }, ++ }; ++ unsigned int i; ++ ++ for (i = 0; i < ARRAY_SIZE(cases); i++) { ++ struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); ++ u8 *data = kunit_kmalloc(test, APPLE_PPN_PAGE_SIZE, GFP_KERNEL); ++ struct apple_sftl *sftl = NULL; ++ unsigned int programs; ++ ++ KUNIT_ASSERT_NOT_NULL(test, f); ++ KUNIT_ASSERT_NOT_NULL(test, data); ++ KUNIT_ASSERT_EQ(test, sftl_test_native_medium(f, data), 0); ++ f->ops.erase_block = sftl_test_native_erase; ++ f->native_reservations[1] = cases[i].second; ++ f->native_reserve_error = cases[i].reserve_error; ++ f->checkpoint->records[8].data[8 * TEST_NATIVE_SPARE] = cases[i].role; ++ programs = f->programs; ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ apple_sftl_set_checkpoint_ops(sftl, &sftl_test_native_ops, f); ++ KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 5, data, 1), ++ cases[i].result); ++ KUNIT_EXPECT_EQ(test, f->programs, programs + !cases[i].result); ++ KUNIT_EXPECT_EQ(test, f->native_policy_calls, 0U); ++ KUNIT_EXPECT_EQ(test, f->stats_calls, 0U); ++ KUNIT_EXPECT_EQ(test, f->erases, 0U); ++ KUNIT_EXPECT_PTR_EQ(test, sftl_test_written(f, TEST_NATIVE_SLOT, 0, 0), NULL); ++ KUNIT_EXPECT_PTR_EQ(test, sftl_test_written(f, TEST_NATIVE_SLOT + 1, 0, 0), NULL); ++ apple_sftl_free(sftl); ++ apple_sftl_checkpoint_free(f->checkpoint); ++ } ++} ++ ++static void sftl_test_gc_native_wear_limit(struct kunit *test) ++{ ++ const u32 sites[] = { 1, TEST_NATIVE_SPARE, 5 }; ++ struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); ++ u8 *data = kunit_kmalloc(test, APPLE_PPN_PAGE_SIZE, GFP_KERNEL); ++ unsigned int i, programs; ++ ++ KUNIT_ASSERT_NOT_NULL(test, f); ++ KUNIT_ASSERT_NOT_NULL(test, data); ++ KUNIT_ASSERT_EQ(test, sftl_test_native_medium(f, data), 0); ++ f->ops.erase_block = sftl_test_native_erase; ++ programs = f->programs; ++ for (i = 0; i < ARRAY_SIZE(sites); i++) { ++ struct apple_sftl *sftl = NULL; ++ u8 *wear = f->checkpoint->records[8].data + 8 * sites[i] + 6; ++ ++ put_unaligned_le16(0xffff, wear); ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ apple_sftl_set_checkpoint_ops(sftl, &sftl_test_native_ops, f); ++ KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 5, data, 1), ++ -EBADMSG); ++ KUNIT_EXPECT_EQ(test, f->programs, programs); ++ KUNIT_EXPECT_EQ(test, f->erases, 0U); ++ KUNIT_EXPECT_EQ(test, f->finish_calls, 0U); ++ apple_sftl_free(sftl); ++ put_unaligned_le16(0, wear); ++ } ++ apple_sftl_checkpoint_free(f->checkpoint); ++} ++ + /* An unnamed context or reserved spare has the same erased bytes as a free + * landing block. Its saved role still owns it, even in append-only mode. + */ +@@ -2261,6 +2644,8 @@ static void sftl_test_native_blank_ownership(struct kunit *test) + KUNIT_ASSERT_NOT_NULL(test, data); + KUNIT_ASSERT_EQ(test, sftl_test_native_medium(f, data), 0); + programs = f->programs; ++ /* This fixture varies the only available append candidate. */ ++ f->native_group = false; + entry = f->checkpoint->records[8].data + 6 * 8; + for (i = 0; i < ARRAY_SIZE(cases); i++) { + entry[0] = cases[i].role; +@@ -2274,6 +2659,17 @@ static void sftl_test_native_blank_ownership(struct kunit *test) + } + entry[0] = 1; + put_unaligned_le16(0, entry + 2); ++ f->unknown_candidate = true; ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 5, data, 1), -ENOSPC); ++ apple_sftl_free(sftl); ++ /* A clean ordinary entry in the physical bootstrap range is also ineligible. */ ++ f->checkpoint->records[8 + 2057 / 512].data[(2057 % 512) * 8] = 1; ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 5, data, 1), -ENOSPC); ++ KUNIT_EXPECT_EQ(test, f->programs, programs); ++ apple_sftl_free(sftl); ++ f->unknown_candidate = false; + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); + KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 5, data, 1), 0); + KUNIT_EXPECT_EQ(test, f->programs, programs + 1); +@@ -2282,6 +2678,75 @@ static void sftl_test_native_blank_ownership(struct kunit *test) + apple_sftl_checkpoint_free(f->checkpoint); + } + ++static void sftl_test_unlisted_context_fragments(struct kunit *test) ++{ ++ struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); ++ u8 *data = kunit_kmalloc(test, APPLE_PPN_PAGE_SIZE, GFP_KERNEL); ++ struct sftl_test_page *fragment = kunit_kzalloc(test, sizeof(*fragment), GFP_KERNEL); ++ struct apple_sftl *sftl = NULL; ++ struct apple_sftl_info info; ++ u8 *entry, *oob, *range; ++ u32 original; ++ unsigned int j, slot = sftl_test_candidate_slot(TEST_NATIVE_SPARE); ++ ++ KUNIT_ASSERT_NOT_NULL(test, f); ++ KUNIT_ASSERT_NOT_NULL(test, data); ++ KUNIT_ASSERT_NOT_NULL(test, fragment); ++ KUNIT_ASSERT_EQ(test, sftl_test_native_medium(f, data), 0); ++ f->remnant_lanes = true; ++ entry = f->checkpoint->records[8].data + TEST_NATIVE_SPARE * 8; ++ entry[0] = 7; ++ oob = fragment->oob; ++ for (j = 0; j < 4; j++) { ++ const struct apple_sftl_context_record *r = &f->checkpoint->records[4 + j]; ++ ++ memcpy(fragment->data + j * APPLE_SFTL_CHUNK, r->data, APPLE_SFTL_CHUNK); ++ memcpy(oob + j * APPLE_SFTL_META, r->meta, APPLE_SFTL_META); ++ put_unaligned_le64(((u64)(100 + j) << 16) | 0x021f, ++ oob + j * APPLE_SFTL_META); ++ } ++ f->written[slot] = kunit_kzalloc(test, 256 * sizeof(*f->written[slot]), GFP_KERNEL); ++ KUNIT_ASSERT_NOT_NULL(test, f->written[slot]); ++ f->written[slot][128] = fragment; /* Physical lane 1 survived lane 0. */ ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ apple_sftl_get_info(sftl, &info); ++ KUNIT_EXPECT_TRUE(test, info.read_only); ++ apple_sftl_free(sftl); ++ ++ oob[1] = 3; ++ KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &sftl), -EBADMSG); ++ KUNIT_EXPECT_PTR_EQ(test, sftl, NULL); ++ oob[1] = 2; ++ oob[2]--; ++ KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &sftl), -EBADMSG); ++ oob[2]++; ++ f->unread_remnant = true; ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ apple_sftl_get_info(sftl, &info); ++ KUNIT_EXPECT_TRUE(test, info.read_only); ++ KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 5, data, 1), -EROFS); ++ apple_sftl_free(sftl); ++ f->unread_remnant = false; ++ f->written[slot][128] = NULL; ++ f->written[slot][129] = fragment; /* A programmed page after a blank lane head. */ ++ KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &sftl), -EBADMSG); ++ f->written[slot][129] = NULL; ++ put_unaligned_le16(1, entry + 2); ++ KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &sftl), -EBADMSG); ++ put_unaligned_le16(0, entry + 2); ++ /* Even when the saved block count is zero, the saved map must agree. */ ++ range = f->checkpoint->records[32].data + 8; ++ original = get_unaligned_le32(range); ++ put_unaligned_le32(TEST_NATIVE_SPARE * APPLE_SFTL_STRIDE, range); ++ KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &sftl), -EBADMSG); ++ put_unaligned_le32(original, range); ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ apple_sftl_get_info(sftl, &info); ++ KUNIT_EXPECT_FALSE(test, info.read_only); ++ apple_sftl_free(sftl); ++ apple_sftl_checkpoint_free(f->checkpoint); ++} ++ + static int sftl_test_erase_native_free(void *priv, unsigned int bus, unsigned int cau, + unsigned int block) + { +@@ -2294,6 +2759,25 @@ static int sftl_test_erase_native_free(void *priv, unsigned int bus, unsigned in + return 0; + } + ++static int sftl_test_account_retired(void *priv, u32 block, u16 old_wear, u16 new_wear, ++ enum apple_sftl_retired_phase phase) ++{ ++ struct sftl_fixture *f = priv; ++ ++ if (block != 5 || old_wear != 47 || new_wear != 48) ++ return -EINVAL; ++ if (phase == APPLE_SFTL_PREPARE_RETIRED) { ++ f->retired_prepares++; ++ return f->erases ? -EINVAL : f->retired_prepare_error; ++ } ++ f->retired_finishes++; ++ return f->erases == 1 && f->retired_prepares ? f->retired_finish_error : -EINVAL; ++} ++ ++static const struct apple_sftl_checkpoint_ops sftl_test_retired_ops = { ++ .account_retired = sftl_test_account_retired, ++}; ++ + /* Both recovered maps have released the old ordinary data. Direct erase + * still requires the native checkpoint to have retired its ownership. + */ +@@ -2317,11 +2801,17 @@ static void sftl_test_native_retired_ownership(struct kunit *test) + f->ops.erase_block = sftl_test_erase_native_free; + programs = f->programs; + entry = f->checkpoint->records[8].data + 5 * 8; ++ put_unaligned_le16(47, entry + 6); + for (i = 0; i < ARRAY_SIZE(cases); i++) { + entry[0] = cases[i].role; + entry[1] = cases[i].flags; + put_unaligned_le16(cases[i].live, entry + 2); ++ if (cases[i].role == 7) { ++ KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &sftl), -EBADMSG); ++ continue; ++ } + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ apple_sftl_set_checkpoint_ops(sftl, &sftl_test_retired_ops, f); + KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 480, data, 1), + -ENOSPC); + KUNIT_EXPECT_EQ(test, f->programs, programs); +@@ -2332,13 +2822,70 @@ static void sftl_test_native_retired_ownership(struct kunit *test) + entry[1] = 64; + put_unaligned_le16(0, entry + 2); + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 480, data, 1), -ENOSPC); ++ apple_sftl_set_checkpoint_ops(sftl, &sftl_test_retired_ops, f); ++ put_unaligned_le16(48, entry + 6); ++ KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 480, data, 1), -EBADMSG); ++ KUNIT_EXPECT_EQ(test, f->retired_prepares, 0U); ++ apple_sftl_free(sftl); ++ put_unaligned_le16(0xffff, entry + 6); ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ apple_sftl_set_checkpoint_ops(sftl, &sftl_test_retired_ops, f); ++ KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 480, data, 1), -ENOSPC); ++ KUNIT_EXPECT_EQ(test, f->retired_prepares, 0U); ++ apple_sftl_free(sftl); ++ put_unaligned_le16(47, entry + 6); ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ apple_sftl_set_checkpoint_ops(sftl, &sftl_test_retired_ops, f); ++ f->retired_prepare_error = -EAGAIN; ++ KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 480, data, 1), -EAGAIN); ++ KUNIT_EXPECT_EQ(test, f->retired_prepares, 1U); ++ KUNIT_EXPECT_EQ(test, f->retired_finishes, 0U); ++ KUNIT_EXPECT_EQ(test, f->erases, 0U); ++ f->retired_prepare_error = 0; + KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 480, data, 1), 0); ++ KUNIT_EXPECT_EQ(test, f->retired_prepares, 2U); ++ KUNIT_EXPECT_EQ(test, f->retired_finishes, 1U); + KUNIT_EXPECT_EQ(test, f->programs, programs + 1); + KUNIT_EXPECT_EQ(test, f->erases, 1U); + apple_sftl_free(sftl); + apple_sftl_checkpoint_free(f->checkpoint); + } + ++static void sftl_test_native_retired_finish_failure(struct kunit *test) ++{ ++ struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); ++ u8 *data = kunit_kmalloc(test, APPLE_PPN_PAGE_SIZE, GFP_KERNEL); ++ struct apple_sftl *sftl = NULL; ++ u8 *entry; ++ u32 programs; ++ ++ KUNIT_ASSERT_NOT_NULL(test, f); ++ KUNIT_ASSERT_NOT_NULL(test, data); ++ f->checkpoint_slot = true; ++ KUNIT_ASSERT_EQ(test, sftl_test_gc_medium(f, data, GC_CHECKPOINT_AFTER_OVERWRITE), 0); ++ KUNIT_ASSERT_EQ(test, sftl_test_native_context(f), 0); ++ f->ops.erase_block = sftl_test_erase_native_free; ++ programs = f->programs; ++ entry = f->checkpoint->records[8].data + 5 * 8; ++ entry[0] = 9; ++ entry[1] = 64; ++ put_unaligned_le16(47, entry + 6); ++ KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); ++ apple_sftl_set_checkpoint_ops(sftl, &sftl_test_retired_ops, f); ++ f->retired_finish_error = -EIO; ++ KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 480, data, 1), -EIO); ++ KUNIT_EXPECT_EQ(test, f->retired_prepares, 1U); ++ KUNIT_EXPECT_EQ(test, f->retired_finishes, 1U); ++ KUNIT_EXPECT_EQ(test, f->erases, 1U); ++ f->retired_finish_error = 0; ++ KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, TEST_APPEND_BASE + 480, data, 1), -EIO); ++ KUNIT_EXPECT_EQ(test, f->erases, 1U); ++ KUNIT_EXPECT_EQ(test, f->programs, programs); ++ apple_sftl_free(sftl); ++ apple_sftl_checkpoint_free(f->checkpoint); ++} ++ + static void sftl_test_gc_retries_reservation(struct kunit *test) + { + struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); +@@ -2578,7 +3125,14 @@ static struct kunit_case apple_sftl_cases[] = { + KUNIT_CASE(sftl_test_vfl_rejects_ambiguous_tables), + KUNIT_CASE(sftl_test_native_blank_ownership), + KUNIT_CASE(sftl_test_native_retired_ownership), ++ KUNIT_CASE(sftl_test_native_retired_finish_failure), + KUNIT_CASE(sftl_test_gc_native_publication), ++ KUNIT_CASE(sftl_test_gc_native_finish_failure), ++ KUNIT_CASE(sftl_test_gc_native_spare_reuse), ++ KUNIT_CASE(sftl_test_gc_native_event_stats), ++ KUNIT_CASE(sftl_test_gc_native_stats_failure), ++ KUNIT_CASE(sftl_test_gc_native_reservations), ++ KUNIT_CASE(sftl_test_gc_native_wear_limit), + KUNIT_CASE(sftl_test_gc_native_prefix_cuts), + KUNIT_CASE(sftl_test_gc_native_policy_refuses), + KUNIT_CASE(sftl_test_gc_retries_reservation), +@@ -2611,6 +3165,7 @@ static struct kunit_case apple_sftl_cases[] = { + KUNIT_CASE(sftl_test_context_pins_fallback_blocks), + KUNIT_CASE(sftl_test_context_torn_append_replay), + KUNIT_CASE(sftl_test_reject_corrupt_generation), ++ KUNIT_CASE(sftl_test_unlisted_context_fragments), + KUNIT_CASE(sftl_test_scan_after_open_lane_hole), + KUNIT_CASE(sftl_test_runtime_failure_stops_reads), + KUNIT_CASE(sftl_test_reject_incomplete_or_ambiguous), +diff --git a/drivers/mtd/apple-sftl.h b/drivers/mtd/apple-sftl.h +index 141a00966..6c16a6ae6 100644 +--- a/drivers/mtd/apple-sftl.h ++++ b/drivers/mtd/apple-sftl.h +@@ -9,6 +9,11 @@ + + struct apple_sftl; + ++enum apple_sftl_retired_phase { ++ APPLE_SFTL_PREPARE_RETIRED, ++ APPLE_SFTL_FINISH_RETIRED, ++}; ++ + /* + * The native policy completes a planned accounting image in memory. The + * core owns all I/O, verification and retirement of the old generation. +@@ -17,10 +22,28 @@ struct apple_sftl; + * mutation the writer stops until a fresh recovery. + */ + struct apple_sftl_checkpoint_ops { +- /* With this policy the core stages and retires real context pages. +- * It is exclusive with the externally atomic reserve/install pair. ++ /* Alongside reserve and read_native_stats this stages a new native ++ * context group. The old active site is discovered from durable pages. + */ + apple_sftl_prepare_accounting prepare_native; ++ /* Read the live native statistics after the base page has committed. ++ * ID 255 is 88 bytes and ID 254 is 464. Return bytes copied or errno. ++ */ ++ int (*read_native_stats)(void *priv, u32 id, u8 *data, size_t bytes); ++ /* Reconcile the observed releases with the policy's runtime state ++ * before the donor can be reused. The image is borrowed for this call. ++ */ ++ int (*finish_native)(void *priv, const struct apple_sftl_checkpoint *runtime, ++ u32 old_context, u32 unused_context, u32 donor); ++ /* Direct reuse of an already dirty-free native donor. PREPARE verifies ++ * current ownership without changing media. FINISH reconciles the ++ * verified erase and wear in the runtime ledger before the first write. ++ */ ++ int (*account_retired)(void *priv, u32 block, u16 old_wear, u16 new_wear, ++ enum apple_sftl_retired_phase phase); ++ /* Pure reservation proposal. On native media return two fresh sites ++ * from the runtime allocation policy, or an error before any mutation. ++ */ + int (*reserve)(void *priv, u32 *blocks, u32 *nr_blocks); + int (*install)(void *priv, const struct apple_sftl_checkpoint *image, + struct apple_sftl_checkpoint **readback); From fed7179c1651000749f5e55ed994559ce7e401c8 Mon Sep 17 00:00:00 2001 From: Shayne <8741557+shaynelegault@users.noreply.github.com> Date: Thu, 19 Nov 2026 08:56:14 -0600 Subject: [PATCH 05/11] P105: stage read-only flash measurement and exclude competing raw writes Serialize raw programs against the lifetime of the recovered SFTL map. Stage a board-specific module to capture both physical views and controller status, and compare paired observations along with adjacent and same-index controls. Publish matching PR kernel and measurement module artifacts for device tests. Keep physical erase disabled pending measured coverage, continuous runtime accounting and cold-boot preservation. Assisted-by: LLM --- .github/workflows/build.yml | 24 +- devices/p105/device.toml | 5 +- docs/bring-up/p105-sftl-checkpoint-gc.md | 48 +++- kernel/modules/p105/capture/Makefile | 1 + kernel/modules/p105/capture/p105_capture.c | 212 ++++++++++++++++ ...Exclude-raw-programs-while-SFTL-owns.patch | 238 ++++++++++++++++++ tests/test_p105_erase_comparison.py | 136 ++++++++++ tools/p105/README.md | 47 ++++ tools/p105/compare_erase.py | 158 ++++++++++++ 9 files changed, 864 insertions(+), 5 deletions(-) create mode 100644 kernel/modules/p105/capture/Makefile create mode 100644 kernel/modules/p105/capture/p105_capture.c create mode 100644 kernel/patches/armv7/mtd/0016-mtd-apple-h2fmi-Exclude-raw-programs-while-SFTL-owns.patch create mode 100644 tests/test_p105_erase_comparison.py create mode 100644 tools/p105/README.md create mode 100644 tools/p105/compare_erase.py diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 71d7f04d..168da41d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -182,7 +182,29 @@ jobs: # Keep only what this build used, so the saved cache stays one build large. ccache --evict-older-than "$(( $(date +%s) - started + 60 ))s" >/dev/null { echo "Compiler cache, $PLATFORM:"; echo '```'; ccache --show-stats; echo '```'; } >> "$GITHUB_STEP_SUMMARY" - # A pull request's kernel only has to compile; nothing assembles it. + # Local P105 builds stage the board's optional capture module separately + # from the platform kernel. Use that same producer for device-test builds. + - name: Build the P105 read-only measurement module + id: p105_capture + if: github.event_name == 'pull_request' && matrix.platform == 'apple-armv7' && steps.reuse.outputs.reused == '' + run: | + bash kernel/build-modules --source "$PWD/out/kernel-work/source" \ + --build "$PWD/out/kernel-work/output" --modules "$PWD/kernel/modules/p105" \ + --arch arm --localversion "-orchard-apple-armv7" --output "$PWD/out/p105-modules" + mkdir out/p105-measurement + cp out/components/kernel/{kernel.release,zImage} out/p105-measurement/ + cp out/components/kernel/dtbs/s5l8942x-p105.dtb out/p105-measurement/ + cp out/p105-modules/capture/p105_capture.ko out/p105-measurement/ + git rev-parse HEAD > out/p105-measurement/orchard.commit + (cd out/p105-measurement && sha256sum kernel.release orchard.commit zImage *.dtb *.ko > SHA256SUMS) + - uses: actions/upload-artifact@v4 + if: steps.p105_capture.outcome == 'success' + with: + name: p105-read-only-measurement + path: out/p105-measurement/ + if-no-files-found: error + retention-days: 7 + # Release builds also archive the full platform components and source. - name: Archive components and corresponding kernel source if: steps.reuse.outputs.reused == '' && github.event_name != 'pull_request' run: | diff --git a/devices/p105/device.toml b/devices/p105/device.toml index 3e9c79eb..0c163a04 100644 --- a/devices/p105/device.toml +++ b/devices/p105/device.toml @@ -29,8 +29,9 @@ recipe = "firmware.json" # (OrchardLinux/orchard#93), freed space is never reused and writes fail once # the blank flash runs out. Drop this table when it lands; the installer # warns from it and allows the install either way. -# Native checkpoint accounting was replayed offline. The physical P105 -# provider still needs qualified events and erase outcomes before reuse +# Native checkpoint accounting was replayed offline. The P105 kernel +# excludes raw programs while SFTL owns the disk. Runtime controller events +# and the physical erase footprint still need hardware qualification # (docs/bring-up/p105-sftl-checkpoint-gc.md). [storage] reclaims = false diff --git a/docs/bring-up/p105-sftl-checkpoint-gc.md b/docs/bring-up/p105-sftl-checkpoint-gc.md index 3eb62beb..076ee503 100644 --- a/docs/bring-up/p105-sftl-checkpoint-gc.md +++ b/docs/bring-up/p105-sftl-checkpoint-gc.md @@ -143,6 +143,50 @@ build 13G36 and the [pinned Cascadia NAND research](https://github.com/rzxvx/Pro The original [planner and rotation notes](https://github.com/shaynelegault/linux/commit/c504eae2a214915dcf5d2c4856c52e7a6ac79d2d) document the starting evidence. +## Testing the PR kernel on P105 + +Use this checkout's `bin/orchard` with the `p105` device profile. The +kernel source and exported patches belong in the `apple-armv7` series. +The board-specific measurement module lives in `kernel/modules/p105/capture/` +and is staged by the device kernel producer. Loading it is an explicit lab +operation. The PR's ARMv7 build also packages the matching kernel, P105 +device tree, module, kernel release, tested Git revision and checksums. + +An earlier installed desktop used a different storage implementation. A +successful launch of that desktop or USB discovery in normal iOS does not +exercise this kernel. Use a RAM-root test environment running the measured +kernel. Confirm the board, kernel build and USB serial or SSH session first. +Keep the internal volumes unmounted during the footprint comparison. The +generic `hardware-check` storage test is for NVMe. + +The H2FMI driver drains raw MTD programs before SFTL reads its first map. +New raw writes return `EBUSY` for the lifetime of that disk, including +requests through an already-open MTD. Probe failure or disk removal +releases the claim. Diagnostic reads stay available and count toward the +physical controller activity a future accounting provider must observe. + +For a read-only baseline, select one physical bus, CAU and block on the +measured 64 GB profile. The optional `p105_capture` module accepts those +three coordinates as parameters (`bus`, `cau`, `block`). A sequential +read of `/dev/p105-capture` returns PPNS records, including the controller +status and all data/OOB bytes. Stream the output to the host. For an +interior block the stream covers 3,456 pages across both views, all 16 +lanes at that block index and the adjacent blocks on the selected lane. +No erase is available through this interface. + +Keep the raw captures private. With two captures around one independently +recorded operation, run `python3 tools/p105/compare_erase.py before.json after.json` +on the host. The tool's `--help` describes the companion files and manifest +fields. It reports observed transitions, unexpected changes and ranges +that were already blank. A blank page cannot show whether an erase also +reached it. Record collateral coverage and limitations with the result. + +For the eventual cold-boot check, compare the CRC-validated primary and +backup LwVM headers, stable filesystem geometry and file contents on iOS +with their pre-test values. The earlier device captures showed that HFSX +mount bookkeeping can change on a normal iOS reboot. Verify the relocated +Linux data and actual donor reuse after a cold start as well. + ## Remaining before hardware enablement The physical provider needs a trustworthy starting state and a continuous @@ -154,8 +198,8 @@ issued different maintenance erases. Losing that history invalidates the counter model. Scrub and bad-member replacement also need qualification. Physical qualification must establish the exact erase footprint in both -normal and SLC views, exclusive ownership across raw MTD and SFTL operations, -and recovery after a partial erase or block retirement. Before a physical +normal and SLC views, verify the raw write exclusion on the tablet, +and establish recovery after a partial erase or block retirement. Before a physical donor can be returned for writing, the driver must also reconcile the reported erase outcome with native wear and free-block state. End-to-end testing on the measured P105 must verify cold-boot recovery and preservation of diff --git a/kernel/modules/p105/capture/Makefile b/kernel/modules/p105/capture/Makefile new file mode 100644 index 00000000..c0f4d063 --- /dev/null +++ b/kernel/modules/p105/capture/Makefile @@ -0,0 +1 @@ +obj-m := p105_capture.o diff --git a/kernel/modules/p105/capture/p105_capture.c b/kernel/modules/p105/capture/p105_capture.c new file mode 100644 index 00000000..f2f42ff4 --- /dev/null +++ b/kernel/modules/p105/capture/p105_capture.c @@ -0,0 +1,212 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Read both P105 flash views through the bound driver's canonical page reader. */ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define BLOCKS 2088 +#define NORMAL_PAGES 128 +#define SLC_PAGES 64 +#define LANES 16 + +static unsigned int bus = UINT_MAX, cau = UINT_MAX, block = UINT_MAX; +module_param(bus, uint, 0444); +module_param(cau, uint, 0444); +module_param(block, uint, 0444); + +static struct device *parent, *child; + +struct ppns_record { + u8 magic[4]; + u8 version, bus, cau, status; + __le32 row, length; + u8 raw[APPLE_PPN_PAGE_SIZE + APPLE_PPN_OOB_SIZE]; +}; + +struct capture { + struct mutex lock; + u32 next; + loff_t offset; + int error; + struct ppns_record record; + u8 data[APPLE_PPN_PAGE_SIZE], oob[APPLE_PPN_OOB_SIZE]; +}; + +static int match_controller(struct device *dev, const void *unused) +{ + return of_device_is_compatible(dev->of_node, "apple,s5l8940x-fmi"); +} + +static int match_sftl(struct device *dev, const void *controller) +{ + return !strcmp(to_platform_device(dev)->name, "apple-sftl") && + dev->parent == controller; +} + +static int read_record(struct capture *s) +{ + struct ppns_record *r = &s->record; + const struct apple_ppn *ppn; + u32 index = s->next / (NORMAL_PAGES + SLC_PAGES); + u32 page = s->next % (NORMAL_PAGES + SLC_PAGES); + u32 physical = block; + bool slc = page >= NORMAL_PAGES; + int ret, i; + + if (index < LANES) { + r->bus = index / 8; + r->cau = index % 8; + } else { + r->bus = bus; + r->cau = cau; + physical = index == LANES && block ? block - 1 : block + 1; + } + if (slc) + page -= NORMAL_PAGES; + + /* The parent owns the provider and destroys the child before freeing + * it. Lock both in driver-core order so unbind cannot free its data. + */ + device_lock(parent); + device_lock(child); + ppn = dev_get_platdata(child); + if (!device_is_registered(child) || !device_is_bound(parent) || + strcmp(dev_driver_string(parent), "apple-h2fmi") || + !device_is_bound(child) || !ppn || ppn->dev != parent || + ppn->buses != 2 || ppn->caus != 8 || ppn->blocks != BLOCKS || + ppn->pages != NORMAL_PAGES || !ppn->ops || !ppn->ops->read_page) + ret = -ENODEV; + else + ret = ppn->ops->read_page(ppn->priv, r->bus, r->cau, physical, page, + slc, s->data, s->oob, &r->status); + device_unlock(child); + device_unlock(parent); + if (ret) + return ret; + /* These statuses carry a data phase. A retired row does not. */ + if (r->status != 0x40 && r->status != 0x42 && r->status != 0x49) + return -EIO; + memcpy(r->magic, "PPNS", 4); + r->version = 1; + r->row = cpu_to_le32((slc ? BIT(22) : 0) | (r->cau << 19) | (physical << 7) | page); + r->length = cpu_to_le32(sizeof(r->raw)); + for (i = 0; i < 4; i++) { + u8 *dst = r->raw + 4112 * i; + const u8 *data = s->data + 4096 * i; + + memcpy(dst, data, 1024); + memcpy(dst + 1024, s->oob + 16 * i, 16); + memcpy(dst + 1040, data + 1024, 3072); + } + return 0; +} + +static int capture_open(struct inode *inode, struct file *file) +{ + struct capture *s; + + if (!capable(CAP_SYS_RAWIO)) + return -EPERM; + if (file->f_mode & FMODE_WRITE) + return -EROFS; + s = kvzalloc(sizeof(*s), GFP_KERNEL); + if (!s) + return -ENOMEM; + mutex_init(&s->lock); + s->offset = sizeof(s->record); + file->private_data = s; + return stream_open(inode, file); +} + +static ssize_t capture_read(struct file *file, char __user *out, size_t count, loff_t *pos) +{ + struct capture *s = file->private_data; + u32 groups = LANES + (block > 0) + (block + 1 < BLOCKS); + ssize_t ret; + + if (!count) + return 0; + if (mutex_lock_interruptible(&s->lock)) + return -ERESTARTSYS; + ret = s->error; + if (ret) + goto out; + if (s->offset == sizeof(s->record)) { + if (s->next == groups * (NORMAL_PAGES + SLC_PAGES)) + goto out; + ret = read_record(s); + if (ret) { + s->error = ret; + goto out; + } + s->next++; + s->offset = 0; + } + ret = simple_read_from_buffer(out, count, &s->offset, &s->record, sizeof(s->record)); +out: + mutex_unlock(&s->lock); + return ret; +} + +static int capture_release(struct inode *inode, struct file *file) +{ + kvfree(file->private_data); + return 0; +} + +static const struct file_operations capture_ops = { + .owner = THIS_MODULE, + .open = capture_open, + .read = capture_read, + .release = capture_release, +}; + +static struct miscdevice capture_device = { + .minor = MISC_DYNAMIC_MINOR, + .name = "p105-capture", + .fops = &capture_ops, + .mode = 0400, +}; + +static int __init capture_init(void) +{ + int ret; + + static_assert(sizeof(struct ppns_record) == 16 + 16448); + if (!of_machine_is_compatible("apple,p105ap")) + return -ENODEV; + if (bus >= 2 || cau >= 8 || block >= BLOCKS) + return -EINVAL; + parent = bus_find_device(&platform_bus_type, NULL, NULL, match_controller); + if (!parent) + return -ENODEV; + child = bus_find_device(&platform_bus_type, NULL, parent, match_sftl); + if (!child) { + put_device(parent); + return -ENODEV; + } + ret = misc_register(&capture_device); + if (ret) { + put_device(child); + put_device(parent); + } + return ret; +} + +static void __exit capture_exit(void) +{ + misc_deregister(&capture_device); + put_device(child); + put_device(parent); +} + +module_init(capture_init); +module_exit(capture_exit); +MODULE_DESCRIPTION("Read-only P105 normal/SLC PPNS capture"); +MODULE_LICENSE("GPL"); diff --git a/kernel/patches/armv7/mtd/0016-mtd-apple-h2fmi-Exclude-raw-programs-while-SFTL-owns.patch b/kernel/patches/armv7/mtd/0016-mtd-apple-h2fmi-Exclude-raw-programs-while-SFTL-owns.patch new file mode 100644 index 00000000..9ac56f6e --- /dev/null +++ b/kernel/patches/armv7/mtd/0016-mtd-apple-h2fmi-Exclude-raw-programs-while-SFTL-owns.patch @@ -0,0 +1,238 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Shayne <8741557+shaynelegault@users.noreply.github.com> +Date: Thu, 19 Nov 2026 08:44:52 -0600 +Subject: [PATCH] mtd: apple-h2fmi: Exclude raw programs while SFTL owns the + map + +Drain raw programs and their readbacks before probing SFTL. Reject new +raw programs through every MTD on the shared controller until the disk +has been removed and its queue drained. Reopen raw access on probe +failure. Raw reads remain available for measurement. + +Exercise concurrent ownership, duplicate claims and release at the raw +program boundary. Keep physical erase qualification separate. + +Assisted-by: LLM +--- + drivers/mtd/apple-sftl-block.c | 11 ++++ + drivers/mtd/devices/mtd-apple-h2fmi-test.c | 31 ++++++++++++ + drivers/mtd/devices/mtd-apple-h2fmi.c | 8 +++ + include/linux/mtd/apple-ppn-access.h | 59 ++++++++++++++++++++++ + include/linux/mtd/apple-ppn.h | 3 ++ + 5 files changed, 112 insertions(+) + create mode 100644 include/linux/mtd/apple-ppn-access.h + +diff --git a/drivers/mtd/apple-sftl-block.c b/drivers/mtd/apple-sftl-block.c +index 31f1dfa14..148a24535 100644 +--- a/drivers/mtd/apple-sftl-block.c ++++ b/drivers/mtd/apple-sftl-block.c +@@ -6,6 +6,7 @@ + #include + #include + #include ++#include + #include + #include + #include +@@ -188,6 +189,16 @@ static int sftl_disk_probe(struct platform_device *pdev) + + if (!provider || !provider->ops || !provider->ops->read_page) + return -EINVAL; ++ /* A read-only disk also needs its recovered map to stay current. */ ++ if (provider->access) { ++ ret = apple_ppn_claim_sftl(provider->access); ++ if (ret) ++ return ret; ++ ret = devm_add_action_or_reset(&pdev->dev, apple_ppn_release_sftl, ++ provider->access); ++ if (ret) ++ return ret; ++ } + sftl = kzalloc(sizeof(*sftl), GFP_KERNEL); + if (!sftl) + return -ENOMEM; +diff --git a/drivers/mtd/devices/mtd-apple-h2fmi-test.c b/drivers/mtd/devices/mtd-apple-h2fmi-test.c +index b22c500e3..cb0d60e87 100644 +--- a/drivers/mtd/devices/mtd-apple-h2fmi-test.c ++++ b/drivers/mtd/devices/mtd-apple-h2fmi-test.c +@@ -1,6 +1,7 @@ + // SPDX-License-Identifier: GPL-2.0-only + #include + #include ++#include + + #include "mtd-apple-h2fmi.h" + +@@ -207,7 +208,37 @@ static void h2fmi_program_status_test(struct kunit *test) + } + } + ++static void h2fmi_raw_write_ownership_test(struct kunit *test) ++{ ++ struct apple_ppn_access access; ++ bool writer; ++ int ret; ++ ++ apple_ppn_access_init(&access); ++ KUNIT_ASSERT_EQ(test, apple_ppn_begin_raw_write(&access), 0); ++ /* An SFTL claim cannot pass the write lock until raw readback ends. */ ++ writer = down_write_trylock(&access.lock); ++ if (writer) ++ up_write(&access.lock); ++ KUNIT_EXPECT_FALSE(test, writer); ++ apple_ppn_end_raw_write(&access); ++ ++ KUNIT_ASSERT_EQ(test, apple_ppn_claim_sftl(&access), 0); ++ KUNIT_EXPECT_EQ(test, apple_ppn_claim_sftl(&access), -EBUSY); ++ ret = apple_ppn_begin_raw_write(&access); ++ if (!ret) ++ apple_ppn_end_raw_write(&access); ++ KUNIT_EXPECT_EQ(test, ret, -EBUSY); ++ apple_ppn_release_sftl(&access); ++ ++ KUNIT_ASSERT_EQ(test, apple_ppn_begin_raw_write(&access), 0); ++ apple_ppn_end_raw_write(&access); ++ KUNIT_ASSERT_EQ(test, apple_ppn_claim_sftl(&access), 0); ++ apple_ppn_release_sftl(&access); ++} ++ + static struct kunit_case h2fmi_test_cases[] = { ++ KUNIT_CASE(h2fmi_raw_write_ownership_test), + KUNIT_CASE(h2fmi_geometry_test), + KUNIT_CASE(h2fmi_invalid_geometry_test), + KUNIT_CASE(h2fmi_row_test), +diff --git a/drivers/mtd/devices/mtd-apple-h2fmi.c b/drivers/mtd/devices/mtd-apple-h2fmi.c +index 661bd0d1f..0f9b0d1b4 100644 +--- a/drivers/mtd/devices/mtd-apple-h2fmi.c ++++ b/drivers/mtd/devices/mtd-apple-h2fmi.c +@@ -34,6 +34,7 @@ + #include + #include + #include ++#include + #include + #include + #include +@@ -116,6 +117,7 @@ struct h2fmi { + unsigned int ndies; + struct platform_device *sftl; + struct apple_ppn_ops ppn_ops; ++ struct apple_ppn_access access; + }; + + /* +@@ -557,8 +559,12 @@ static int h2fmi_mtd_write_oob(struct mtd_info *mtd, loff_t to, + ops->ooblen != H2FMI_PAGE_OOB || ops->ooboffs || + !ops->datbuf || !ops->oobbuf) + return -EINVAL; ++ ret = apple_ppn_begin_raw_write(&fmi->access); ++ if (ret) ++ return ret; + ret = h2fmi_program_page(fmi, b, die->cau, block, pg, + ops->datbuf, ops->oobbuf); ++ apple_ppn_end_raw_write(&fmi->access); + if (!ret) { + ops->retlen = H2FMI_PAGE_DATA; + ops->oobretlen = H2FMI_PAGE_OOB; +@@ -732,6 +738,7 @@ static int h2fmi_register_sftl(struct h2fmi *fmi) + .dev = fmi->dev, + .ops = &fmi->ppn_ops, + .priv = fmi, ++ .access = &fmi->access, + .buses = H2FMI_BUSES, + .caus = g->caus, + .blocks = g->blocks, +@@ -946,6 +953,7 @@ static int h2fmi_probe(struct platform_device *pdev) + if (!fmi) + return -ENOMEM; + fmi->dev = &pdev->dev; ++ apple_ppn_access_init(&fmi->access); + + /* Six regions, in the ADT's order: FMI, FMC, ECC for bus 0, then bus 1. */ + for (i = 0; i < H2FMI_BUSES; i++) { +diff --git a/include/linux/mtd/apple-ppn-access.h b/include/linux/mtd/apple-ppn-access.h +new file mode 100644 +index 000000000..0a6ab812f +--- /dev/null ++++ b/include/linux/mtd/apple-ppn-access.h +@@ -0,0 +1,59 @@ ++/* SPDX-License-Identifier: GPL-2.0-only */ ++#ifndef _LINUX_MTD_APPLE_PPN_ACCESS_H ++#define _LINUX_MTD_APPLE_PPN_ACCESS_H ++ ++#include ++#include ++ ++struct apple_ppn_access { ++ struct rw_semaphore lock; ++ bool sftl_claimed; ++}; ++ ++static inline void apple_ppn_access_init(struct apple_ppn_access *access) ++{ ++ init_rwsem(&access->lock); ++ access->sftl_claimed = false; ++} ++ ++/* Wait for every in-flight raw program and its readback before map discovery. */ ++static inline int apple_ppn_claim_sftl(struct apple_ppn_access *access) ++{ ++ int ret = 0; ++ ++ down_write(&access->lock); ++ if (access->sftl_claimed) ++ ret = -EBUSY; ++ else ++ access->sftl_claimed = true; ++ up_write(&access->lock); ++ return ret; ++} ++ ++/* devres releases the claim after failed probe or after the disk has drained. */ ++static inline void apple_ppn_release_sftl(void *data) ++{ ++ struct apple_ppn_access *access = data; ++ ++ down_write(&access->lock); ++ access->sftl_claimed = false; ++ up_write(&access->lock); ++} ++ ++/* Raw programs may still run on different buses when no map is cached. */ ++static inline int apple_ppn_begin_raw_write(struct apple_ppn_access *access) ++{ ++ down_read(&access->lock); ++ if (access->sftl_claimed) { ++ up_read(&access->lock); ++ return -EBUSY; ++ } ++ return 0; ++} ++ ++static inline void apple_ppn_end_raw_write(struct apple_ppn_access *access) ++{ ++ up_read(&access->lock); ++} ++ ++#endif +diff --git a/include/linux/mtd/apple-ppn.h b/include/linux/mtd/apple-ppn.h +index 04b4b935a..48307b4a1 100644 +--- a/include/linux/mtd/apple-ppn.h ++++ b/include/linux/mtd/apple-ppn.h +@@ -5,6 +5,7 @@ + #include + + struct device; ++struct apple_ppn_access; + + #define APPLE_PPN_PAGE_SIZE 16384 + #define APPLE_PPN_OOB_SIZE 64 +@@ -35,6 +36,8 @@ struct apple_ppn { + u16 caus; + u16 blocks; + u16 pages; ++ /* Shared by all raw writers on media also exposed through SFTL. */ ++ struct apple_ppn_access *access; + }; + + #endif diff --git a/tests/test_p105_erase_comparison.py b/tests/test_p105_erase_comparison.py new file mode 100644 index 00000000..d73a18f7 --- /dev/null +++ b/tests/test_p105_erase_comparison.py @@ -0,0 +1,136 @@ +# SPDX-License-Identifier: GPL-2.0-only +import hashlib +import io +import json +from pathlib import Path +import tempfile +import unittest +from unittest.mock import patch + +from tools.p105 import compare_erase as evidence + + +TARGET = (1, 2, 600) +PROGRAMMED = (0x40, b'witness') +BLANK = (0x49, None) + + +def sample(): + cells = {(b, c, 600) for b in range(2) for c in range(8)} + cells.update({(1, 2, 599), (1, 2, 601)}) + return {(*cell, view, page): PROGRAMMED for cell in cells + for view, count in evidence.PAGES.items() for page in range(count)} + + +def record(*, row=(1 << 22) | (2 << 19) | (600 << 7), status=0x40, raw=bytes(evidence.RAW_SIZE)): + return evidence.HEADER.pack(b'PPNS', 1, 1, 2, status, row, len(raw)) + raw + + +class EraseComparison(unittest.TestCase): + def setUp(self): + self.before = sample() + self.after = self.before.copy() + for page in range(128): + self.after[(*TARGET, 'normal', page)] = BLANK + + def compare(self): + return evidence.compare(self.before, self.after, TARGET, 'normal') + + def test_reports_the_sampled_aliases_and_controls(self): + self.after[(*TARGET, 'slc', 15)] = BLANK + result = self.compare() + self.assertEqual(result['observed_erased_pages'], {'normal': list(range(128)), 'slc': [15]}) + self.assertEqual(result['unchanged_control_pages'], 17 * 192) + self.assertEqual(result['sampled_blocks'], 18) + + def test_slc_issued_erase_compares_the_normal_alias(self): + self.after = self.before.copy() + for page in range(64): + self.after[(*TARGET, 'slc', page)] = BLANK + result = evidence.compare(self.before, self.after, TARGET, 'slc') + self.assertEqual(result['observed_erased_pages'], {'normal': [], 'slc': list(range(64))}) + + def test_reports_blank_ranges_and_separates_measured_transitions(self): + blanks = [(1, 2, 599, 'slc', p) for p in (0, 1, 4)] + blanks += [(*TARGET, 'normal', 6), (*TARGET, 'slc', 63)] + for key in blanks: + self.before[key] = self.after[key] = BLANK + self.before[(*TARGET, 'normal', 6)] = (0x40, None) + result = self.compare() + self.assertEqual(result['comparison'], 'inconclusive') + self.assertEqual(result['previously_blank_pages'], 5) + self.assertEqual(result['previously_blank_ranges'], + {'normal': [[1, 2, 600, 6, 6]], + 'slc': [[1, 2, 599, 0, 1], [1, 2, 599, 4, 4], [1, 2, 600, 63, 63]]}) + self.assertEqual(result['observed_erased_pages']['normal'], [p for p in range(128) if p != 6]) + self.assertEqual(result['unchanged_target_pages']['slc'], list(range(63))) + self.assertEqual(result['unchanged_control_pages'], 17 * 192 - 3) + self.after[(1, 2, 599, 'slc', 4)] = PROGRAMMED + with self.assertRaisesRegex(ValueError, 'unexpected change to blank page'): + self.compare() + + def test_empty_capture_with_valid_coverage_prints_observations_and_exits_one(self): + blank = dict.fromkeys(self.before, BLANK) + manifest = dict(device='unit-A', trial='empty', source='synthetic', target=list(TARGET), view='normal') + with patch('sys.argv', ['compare_erase.py', 'before.json', 'after.json']), \ + patch.object(evidence, 'load_capture', return_value=(manifest, blank)), \ + patch('sys.stdout', new_callable=io.StringIO) as output, self.assertRaises(SystemExit) as err: + evidence.main() + self.assertEqual(err.exception.code, 1) + result = json.loads(output.getvalue()) + self.assertEqual(result['previously_blank_pages'], 18 * 192) + self.assertEqual(result['unchanged_control_pages'], 0) + self.assertEqual(result['observed_erased_pages'], {'normal': [], 'slc': []}) + + def test_mismatched_or_incomplete_comparisons_and_unexpected_changes_fail(self): + for fault, message in [('missing phase page', 'different pages'), ('both missing', 'missing page'), + ('lost neighbor', 'missing target or collateral'), + ('collateral', 'unexpected change'), ('missed erase', 'retained its contents')]: + before, after = self.before.copy(), self.after.copy() + guard = (1, 2, 599, 'slc', 63) + with self.subTest(fault=fault): + if fault == 'missing phase page': + del after[guard] + elif fault == 'both missing': + del before[guard] + del after[guard] + elif fault == 'lost neighbor': + before = {k: v for k, v in before.items() if k[:3] != guard[:3]} + after = {k: v for k, v in after.items() if k[:3] != guard[:3]} + elif fault == 'collateral': + after[guard] = BLANK + else: + after[(*TARGET, 'normal', 127)] = PROGRAMMED + with self.assertRaisesRegex(ValueError, message): + evidence.compare(before, after, TARGET, 'normal') + + def test_checks_raw_header_coordinates_status_data_and_oob(self): + key, original = evidence.decode_record(record()) + self.assertEqual(key, (*TARGET, 'slc', 0)) + raw = bytearray(evidence.RAW_SIZE) + raw[1024] = 1 # First chunk's out-of-band field. + self.assertNotEqual(evidence.decode_record(record(raw=raw))[1], original) + for raw_record in (record()[:-1], record(row=0), record(row=(1 << 22) | (2 << 19) | 64), + record(status=0x42), record(status=0x49)): + with self.assertRaises(ValueError): + evidence.decode_record(raw_record) + self.assertEqual(evidence.decode_record(record(status=0x49, raw=evidence.ERASED))[1], BLANK) + + def test_checks_stream_digest_and_rejects_duplicate_coordinates(self): + with tempfile.TemporaryDirectory() as tmp: + path = Path(tmp) / 'before.json' + data = record() + manifest = dict(phase='before', device='unit-A', trial='trial-1', source='synthetic', + target=list(TARGET), view='normal', sha256=hashlib.sha256(data).hexdigest()) + path.write_text(json.dumps(manifest)) + stream = path.with_suffix('.ppns') + stream.write_bytes(data) + self.assertEqual(len(evidence.load_capture(path, 'before')[1]), 1) + with self.assertRaisesRegex(ValueError, 'expected the after'): + evidence.load_capture(path, 'after') + stream.write_bytes(record(raw=b'\x01' * evidence.RAW_SIZE)) + with self.assertRaisesRegex(ValueError, 'differs from its manifest'): + evidence.load_capture(path, 'before') + stream.write_bytes(data + data) + with self.assertRaisesRegex(ValueError, 'duplicate'): + evidence.load_capture(path, 'before') diff --git a/tools/p105/README.md b/tools/p105/README.md new file mode 100644 index 00000000..8976f2cb --- /dev/null +++ b/tools/p105/README.md @@ -0,0 +1,47 @@ +# Capturing the P105 flash erase footprint + +A P105 device build stages the optional `p105_capture.ko` module in +`lib/modules//extra/`. It has no device alias and must be loaded +explicitly on a running matching kernel. It reads through the bound H2FMI +driver on the measured 64 GB P105. The module accepts the physical bus, CAU +and block as read-only parameters at load. It samples that block index +across all 16 lanes and the adjacent blocks on the selected lane, in both +normal and SLC views. Each open of `/dev/p105-capture` starts a new scan. + +For an authorized observation, use a quiescent system and record its target, +device, trial, driver revision and issued operation separately. On the device, +load the matching module with the actual target values: + +```sh +timeout -k 5 30 modprobe p105_capture bus="$BUS" cau="$CAU" block="$BLOCK" +``` + +Stream directly into a private host file from the already accessible device. +This example uses an existing SSH connection with permission to read the +capture node. The stream contains full flash data and out-of-band metadata. + +```sh +umask 077 +ssh "$DEVICE" 'timeout -k 5 600 cat /dev/p105-capture' > before.ppns.partial && + mv before.ppns.partial before.ppns +``` + +Repeat the read into `after.ppns` after the independently recorded operation. +An interior target produces 3,456 PPNS records (56,899,584 bytes) per phase. +The driver supplies the exact managed status, four 4 KiB chunks and their +out-of-band bytes. An I/O error or a row without a data phase terminates the +stream. The comparator reports an inconclusive result for a refresh-status +row, a missing record or an already blank control. + +Create the sibling `before.json` and `after.json` manifests using the fields +documented in [compare_erase.py](compare_erase.py), the actual target, the +view of the issued operation and the SHA-256 of each complete PPNS file. +Then run: + +```sh +python3 tools/p105/compare_erase.py before.json after.json +``` + +The capture calls the provider's page reader and leaves SFTL ownership in +place. A scan spans many transactions. Other activity may change the medium +or its read counters during the scan. diff --git a/tools/p105/compare_erase.py b/tools/p105/compare_erase.py new file mode 100644 index 00000000..df7c0feb --- /dev/null +++ b/tools/p105/compare_erase.py @@ -0,0 +1,158 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-only +"""Compare saved P105 erase observations. This tool only reads local files. + +Pass before.json and after.json. Each manifest describes its sibling .ppns file, +which concatenates 16-byte PPNS v1 headers and 16,448-byte raw page payloads. +The header contains magic, version, bus, CAU, status, row and payload length +(the last two are little-endian u32). Row bit 22 selects the SLC view. +Example manifest fields (use the corresponding phase and actual SHA-256): + + {"phase":"before", "device":"unit-A", "trial":"run-01", "source":"synthetic", + "target":[0,2,600], "view":"normal", "sha256":"..."} + +Use the same capture-local device and trial identifiers in both phases. +Source is "hardware" or "synthetic". Target is [bus, CAU, physical block]. +Capture all pages of both views at the target, its immediate block neighbors +on that CAU, and the same block index on every CAU of both buses. Additional +complete blocks are also compared. A page already blank before the operation +cannot reveal an erase. The tool reports these pages as inclusive ranges +[bus, CAU, block, first page, last page] and exits 1 after printing the +available observations. Output describes only observed bytes and statuses. +Acquisition, command provenance and physical ownership require independent evidence. +""" +import argparse +import hashlib +import json +from pathlib import Path +import struct + + +HEADER = struct.Struct('<4sBBBBII') +RAW_SIZE = 16448 +ERASED = b'\xff' * RAW_SIZE +PAGES = {'normal': 128, 'slc': 64} +ERASED_STATUS = 0x49 +GOOD_STATUS = 0x40 + + +def decode_record(record): + if len(record) != HEADER.size + RAW_SIZE: + raise ValueError('truncated PPNS record') + magic, version, bus, cau, status, row, length = HEADER.unpack_from(record) + if magic != b'PPNS' or version != 1 or length != RAW_SIZE: + raise ValueError('invalid PPNS page header or payload length') + block = (row >> 7) & 0xfff + page = row & 0x7f + view = 'slc' if row & (1 << 22) else 'normal' + if (bus >= 2 or cau >= 8 or ((row >> 19) & 7) != cau or row >> 23 + or block >= 2088 or page >= PAGES[view]): + raise ValueError('PPNS row outside the measured P105 geometry') + location = (bus, cau, block, view, page) + raw = record[HEADER.size:] + if status not in (GOOD_STATUS, ERASED_STATUS) or (status == ERASED_STATUS and raw != ERASED): + raise ValueError(f'{location}: uncertain status or invalid erased contents') + digest = None if raw == ERASED else hashlib.sha256(raw).digest() + return location, (status, digest) + + +def load_capture(path, phase): + stream_path = path.with_suffix('.ppns') + if not path.is_file() or not stream_path.is_file(): + raise ValueError(f'{phase} requires a regular manifest and PPNS file') + manifest = json.loads(path.read_text()) + if not isinstance(manifest, dict) or manifest.get('phase') != phase: + raise ValueError(f'expected the {phase} manifest') + for key in ('device', 'trial'): + if not isinstance(manifest.get(key), str) or not manifest[key].strip(): + raise ValueError(f'missing {key} identifier') + if manifest.get('source') not in ('hardware', 'synthetic') or manifest.get('view') not in ('normal', 'slc'): + raise ValueError('invalid source or erase view') + target = manifest.get('target') + if (not isinstance(target, list) or len(target) != 3 or + not all(type(v) is int and 0 <= v < limit for v, limit in zip(target, (2, 8, 2088)))): + raise ValueError('invalid erase target') + stream_hash = hashlib.sha256() + pages = {} + with stream_path.open('rb') as stream: + while record := stream.read(HEADER.size + RAW_SIZE): + stream_hash.update(record) + location, value = decode_record(record) + if location in pages: + raise ValueError(f'duplicate PPNS page {location}') + pages[location] = value + if not pages or stream_hash.hexdigest() != manifest.get('sha256'): + raise ValueError(f'{phase} stream is empty or differs from its manifest') + return manifest, pages + + +def compare(before, after, target, issued_view): + if before.keys() != after.keys(): + raise ValueError('before and after cover different pages') + bus, cau, block = target + required = {(b, c, block) for b in range(2) for c in range(8)} + required.update((bus, cau, n) for n in (block - 1, block + 1) if 0 <= n < 2088) + captured = {key[:3] for key in before} + missing = required - captured + if missing: + raise ValueError(f'missing target or collateral control {min(missing)}') + changed = {view: [] for view in PAGES} + stable_target = {view: [] for view in PAGES} + blank_runs = {view: [] for view in PAGES} + blank_pages = 0 + stable_controls = 0 + for cell in sorted(captured): + for view, count in PAGES.items(): + runs = blank_runs[view] + for page in range(count): + key = (*cell, view, page) + if key not in before: + raise ValueError(f'missing page {key}') + old, new = before[key], after[key] + if old[1] is None: + if old != new and (cell != target or new != (ERASED_STATUS, None)): + raise ValueError(f'{key}: unexpected change to blank page bytes or status') + blank_pages += 1 + if runs and tuple(runs[-1][:3]) == cell and runs[-1][4] + 1 == page: + runs[-1][4] = page + else: + runs.append([*cell, page, page]) + elif cell == target and new == (ERASED_STATUS, None): + changed[view].append(page) + elif old != new: + raise ValueError(f'{key}: unexpected change to bytes or status') + elif cell == target and view == issued_view: + raise ValueError(f'{key}: addressed page retained its contents') + elif cell == target: + stable_target[view].append(page) + else: + stable_controls += 1 + return {'target': list(target), 'issued_view': issued_view, + 'observed_erased_pages': changed, 'unchanged_target_pages': stable_target, + 'unchanged_control_pages': stable_controls, + 'previously_blank_pages': blank_pages, 'previously_blank_ranges': blank_runs, + 'comparison': 'inconclusive' if blank_pages else 'all sampled pages accounted for', + 'sampled_blocks': len(captured)} + + +def main(): + parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) + parser.add_argument('before', type=Path) + parser.add_argument('after', type=Path) + args = parser.parse_args() + try: + first, before = load_capture(args.before, 'before') + second, after = load_capture(args.after, 'after') + if any(first[k] != second[k] for k in ('device', 'trial', 'source', 'target', 'view')): + raise ValueError('manifests describe different devices, trials or operations') + result = compare(before, after, tuple(first['target']), first['view']) + result['declared_source'] = first['source'] + except (OSError, ValueError) as error: + parser.exit(1, f'Inconclusive: {error}\n') + print(json.dumps(result, indent=2)) + if result['previously_blank_pages']: + parser.exit(1) + + +if __name__ == '__main__': + main() From 30f30a8fa95675ee039e68d78e03c7dfc8ec6ab0 Mon Sep 17 00:00:00 2001 From: Shayne <8741557+shaynelegault@users.noreply.github.com> Date: Thu, 19 Nov 2026 09:00:24 -0600 Subject: [PATCH 06/11] ci: record the tested merge revision from GitHub Actions Use the checkout SHA provided by the pull request job when packaging the P105 kernel and its capture module. The runner and the build container use different owners for the worktree. Assisted-by: LLM --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 168da41d..d3a3bceb 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -195,7 +195,7 @@ jobs: cp out/components/kernel/{kernel.release,zImage} out/p105-measurement/ cp out/components/kernel/dtbs/s5l8942x-p105.dtb out/p105-measurement/ cp out/p105-modules/capture/p105_capture.ko out/p105-measurement/ - git rev-parse HEAD > out/p105-measurement/orchard.commit + printf '%s\n' "$GITHUB_SHA" > out/p105-measurement/orchard.commit (cd out/p105-measurement && sha256sum kernel.release orchard.commit zImage *.dtb *.ko > SHA256SUMS) - uses: actions/upload-artifact@v4 if: steps.p105_capture.outcome == 'success' From bd8916c876fbd3e0edb93686392804e5404cbb19 Mon Sep 17 00:00:00 2001 From: Shayne <8741557+shaynelegault@users.noreply.github.com> Date: Thu, 19 Nov 2026 09:36:51 -0600 Subject: [PATCH 07/11] p105: verify the RAM session's kernel and internal storage Stage the ARMv7 measurement build in the kernel directory layout consumed by the RAM project. Include the linked kernel notes digest so a running build can be identified independently of the release string. Let hardware-check start on kernels without the optional hotplug sysctl. Fence the P105 SFTL disk and partitions as they register and verify their mount and read-only state around the bounded storage probe. Assisted-by: LLM --- .github/workflows/build.yml | 47 +++++- docs/bring-up/p105-sftl-checkpoint-gc.md | 19 +-- examples/hardware-check/README.md | 13 +- examples/hardware-check/init | 10 +- examples/hardware-check/rootfs/etc/motd | 3 +- .../rootfs/usr/bin/hardware-check | 34 +++- .../rootfs/usr/libexec/sftl-storage | 68 ++++++++ tests/test_hardware_check_storage.py | 147 ++++++++++++++++++ tools/p105/README.md | 34 +++- 9 files changed, 344 insertions(+), 31 deletions(-) create mode 100644 examples/hardware-check/rootfs/usr/libexec/sftl-storage create mode 100644 tests/test_hardware_check_storage.py diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d3a3bceb..b7ce0f7a 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -184,19 +184,52 @@ jobs: { echo "Compiler cache, $PLATFORM:"; echo '```'; ccache --show-stats; echo '```'; } >> "$GITHUB_STEP_SUMMARY" # Local P105 builds stage the board's optional capture module separately # from the platform kernel. Use that same producer for device-test builds. - - name: Build the P105 read-only measurement module + - name: Stage the P105 read-only measurement kernel and modules id: p105_capture if: github.event_name == 'pull_request' && matrix.platform == 'apple-armv7' && steps.reuse.outputs.reused == '' run: | bash kernel/build-modules --source "$PWD/out/kernel-work/source" \ --build "$PWD/out/kernel-work/output" --modules "$PWD/kernel/modules/p105" \ --arch arm --localversion "-orchard-apple-armv7" --output "$PWD/out/p105-modules" - mkdir out/p105-measurement - cp out/components/kernel/{kernel.release,zImage} out/p105-measurement/ - cp out/components/kernel/dtbs/s5l8942x-p105.dtb out/p105-measurement/ - cp out/p105-modules/capture/p105_capture.ko out/p105-measurement/ - printf '%s\n' "$GITHUB_SHA" > out/p105-measurement/orchard.commit - (cd out/p105-measurement && sha256sum kernel.release orchard.commit zImage *.dtb *.ko > SHA256SUMS) + stage="$PWD/out/p105-measurement" + python3 kernel/stage-modules.py --build "$PWD/out/kernel-work/output" \ + --external "$PWD/out/p105-modules" --output "$stage" + read -r platform dtb < <(python3 kernel/series.py --resolve p105) + test "$platform" = "$PLATFORM" + mkdir "$stage/dtbs" + cp out/components/kernel/{kernel.release,zImage} "$stage/" + cp "out/components/kernel/dtbs/$dtb.dtb" "$stage/dtbs/" + cp "$stage/dtbs/$dtb.dtb" "$stage/p105.dtb" + printf '%s\n' "$GITHUB_SHA" > "$stage/orchard.commit" + # ARM exports the linked .notes section byte-for-byte at /sys/kernel/notes. + llvm-readelf-19 --notes out/kernel-work/output/vmlinux | grep 'Build ID:' + llvm-objcopy-19 --only-section=.notes -O binary out/kernel-work/output/vmlinux out/p105-kernel.notes + test -s out/p105-kernel.notes + sha256sum out/p105-kernel.notes | cut -d ' ' -f1 > "$stage/kernel.notes.sha256" + (cd "$stage" && find . -type f ! -name SHA256SUMS -print0 | LC_ALL=C sort -z | xargs -0 sha256sum > SHA256SUMS) + - name: Check the P105 RAM project's kernel inputs + if: steps.p105_capture.outcome == 'success' + run: | + stage="$PWD/out/p105-measurement" + (cd "$stage" && sha256sum --check --status SHA256SUMS) + PYTHONPATH=src python3 - "$stage" <<'PY' + from pathlib import Path + import sys + from orchard.hardware import device + from orchard.image.cascadia import check_compatible + from orchard.kernel import descriptor, layout + + root, stage = Path.cwd(), Path(sys.argv[1]) + platform, dtb = layout(root, 'p105') + if not (stage / descriptor(root, platform)['image']).stat().st_size: + sys.exit('Empty P105 kernel image') + check_compatible((stage / 'dtbs' / (dtb + '.dtb')).read_bytes(), + device('p105', root)['device']['dt_compatible']) + PY + release=$(cat "$stage/kernel.release") + test -s "$stage/lib/modules/$release/modules.dep" + test -s "$stage/lib/modules/$release/extra/p105_capture.ko" + modprobe -d "$stage" -S "$release" --show-depends p105_capture - uses: actions/upload-artifact@v4 if: steps.p105_capture.outcome == 'success' with: diff --git a/docs/bring-up/p105-sftl-checkpoint-gc.md b/docs/bring-up/p105-sftl-checkpoint-gc.md index 076ee503..6a8917ef 100644 --- a/docs/bring-up/p105-sftl-checkpoint-gc.md +++ b/docs/bring-up/p105-sftl-checkpoint-gc.md @@ -149,15 +149,16 @@ Use this checkout's `bin/orchard` with the `p105` device profile. The kernel source and exported patches belong in the `apple-armv7` series. The board-specific measurement module lives in `kernel/modules/p105/capture/` and is staged by the device kernel producer. Loading it is an explicit lab -operation. The PR's ARMv7 build also packages the matching kernel, P105 -device tree, module, kernel release, tested Git revision and checksums. - -An earlier installed desktop used a different storage implementation. A -successful launch of that desktop or USB discovery in normal iOS does not -exercise this kernel. Use a RAM-root test environment running the measured -kernel. Confirm the board, kernel build and USB serial or SSH session first. -Keep the internal volumes unmounted during the footprint comparison. The -generic `hardware-check` storage test is for NVMe. +operation. The PR's ARMv7 build publishes `p105-read-only-measurement` in +Orchard's kernel-directory layout, with the P105 device tree, matching +module tree, kernel release, tested Git revision and file checksums. + +Use the existing [hardware-check RAM project](../../examples/hardware-check/) +for the P105 session. Its `hardware-check storage` command checks the live +read-only flag and mount state of the SFTL disk and its registered partitions +before and after reading one 4 KiB block. Confirm the running kernel's notes +digest against the artifact and keep the internal volumes unmounted. The [capture procedure](../../tools/p105/) +includes host commands for verification and streaming both flash views. The H2FMI driver drains raw MTD programs before SFTL reads its first map. New raw writes return `EBUSY` for the lifetime of that disk, including diff --git a/examples/hardware-check/README.md b/examples/hardware-check/README.md index 9e58b86c..b4da9b2d 100644 --- a/examples/hardware-check/README.md +++ b/examples/hardware-check/README.md @@ -3,8 +3,8 @@ A RAM-only system that checks what a device offers and writes a report you can send back. It works on every Orchard device: each check looks at what is present first, and what the device doesn't have is reported as SKIP, not FAIL. -Nothing is written to the device's storage; NVMe is set read-only as soon as it -appears. +The RAM session keeps internal volumes unmounted and sets NVMe and P105 SFTL +block devices read-only as they appear. ## Run it @@ -16,13 +16,20 @@ orchard shell # or: screen /dev/cu.usbmodem* 115200 At the shell, run `hardware-check`. It takes about three minutes and checks: -- storage (read-only, a 1 GiB read); +- storage (a 1 GiB NVMe read, or a 4 KiB read on P105); - PCIe devices and their drivers; - Wi-Fi: firmware load, scan, association, DHCP, the gateway, a timed download; - the battery gauge and other power supplies; - display and backlight; - the kernel log (IOMMU faults, NVMe timeouts, warnings, oopses). +For storage alone, run `hardware-check storage`. On P105 it waits up to +60 seconds for the SFTL disk. Before and after the read it checks that the disk +and each registered partition are read-only and absent from this session's mount +table, using kernel device numbers to recognize mount aliases. An inaccessible +node, a mount (including a read-only mount), a changed device set or a short +read fails the check. + Then, if you want: `hardware-check touch`, `hardware-check keys` and `hardware-check charge` (plug chargers in and out). `hardware-check wifi` runs only the Wi-Fi checks. diff --git a/examples/hardware-check/init b/examples/hardware-check/init index 8d1501cf..42493e39 100755 --- a/examples/hardware-check/init +++ b/examples/hardware-check/init @@ -18,15 +18,21 @@ exec /dev/console 2>&1 # SmartIO DMA controller and the touchscreen behind it, get their driver from # the uevent helper. Then load drivers for the devices already present. echo /bin/modprobe > /proc/sys/kernel/modprobe -echo /usr/libexec/module-hotplug > /proc/sys/kernel/hotplug +if [ -e /proc/sys/kernel/hotplug ]; then + echo /usr/libexec/module-hotplug > /proc/sys/kernel/hotplug +fi coldplug() { for alias in /sys/bus/*/devices/*/modalias; do [ -r "$alias" ] && modprobe -qb "$(cat "$alias")" 2>/dev/null || true done } -# Storage: fence every NVMe namespace and partition read-only once they appear. +# Fence internal disks in the background so USB can come up during discovery. ( + . /usr/libexec/sftl-storage + if is_p105; then + watch_sftl + fi i=0 while [ ! -e /sys/class/block/nvme0n1 ] && [ "$i" -lt 60 ]; do coldplug diff --git a/examples/hardware-check/rootfs/etc/motd b/examples/hardware-check/rootfs/etc/motd index 1457fb86..95d8dd2f 100644 --- a/examples/hardware-check/rootfs/etc/motd +++ b/examples/hardware-check/rootfs/etc/motd @@ -1,6 +1,7 @@ -RAM-only hardware check. Internal storage is read-only. +RAM-only hardware check. Internal volumes stay unmounted. hardware-check automatic checks (about 3 minutes) + hardware-check storage internal storage only hardware-check touch drag on the screen for 20 s hardware-check keys press the buttons for 20 s hardware-check charge live charger view (Ctrl-C ends) diff --git a/examples/hardware-check/rootfs/usr/bin/hardware-check b/examples/hardware-check/rootfs/usr/bin/hardware-check index 3991a97c..e87b4663 100755 --- a/examples/hardware-check/rootfs/usr/bin/hardware-check +++ b/examples/hardware-check/rootfs/usr/bin/hardware-check @@ -3,6 +3,7 @@ # is present first; what the device doesn't have is SKIP, not FAIL. # # hardware-check the automatic checks (about 3 minutes) +# hardware-check storage internal storage only # hardware-check touch drag on the screen for 20 s # hardware-check keys press the buttons for 20 s # hardware-check wifi only the Wi-Fi checks @@ -35,6 +36,34 @@ check_system() { check_storage() { section "Storage" + . /usr/libexec/sftl-storage + if is_p105; then + i=0 + until sftl_readonly check; do + if [ "$i" -ge 60 ]; then + result FAIL "P105 SFTL disk or partition missing, inaccessible or writable" + return 1 + fi + sleep 1 + i=$((i + 1)) + done + if ! before=$(sftl_probe_state); then + result FAIL "cannot verify P105 SFTL disk and partitions are read-only and unmounted" + return 1 + fi + bytes=$(dd if=/dev/applesftl0 bs=4096 count=1 2>>"$REPORT" | wc -c) + if [ "$bytes" -ne 4096 ]; then + result FAIL "short or failed read from applesftl0 ($bytes bytes)" + return 1 + fi + if ! after=$(sftl_probe_state) || [ "$after" != "$before" ]; then + result FAIL "P105 SFTL disk, partition or mount state changed during the read" + return 1 + fi + result PASS "P105 SFTL disk and registered partitions read-only and unmounted" + result PASS "read 4096 bytes from applesftl0" + return + fi if [ ! -e /sys/class/block/nvme0n1 ]; then result SKIP "no NVMe storage" return @@ -51,7 +80,9 @@ check_storage() { result PASS "read 1 GiB in ${seconds}s" else result FAIL "1 GiB read from nvme0n1" + return 1 fi + [ "$writable" = 0 ] } check_pcie() { @@ -222,8 +253,9 @@ case "${1:-all}" in say "Report: $REPORT, kernel log: /run/dmesg.txt (also at http://172.16.42.1:8080/)" ;; touch) section "Touch"; watch_input 'touch|multitouch|z2' touch 'Drag on the screen' ;; + storage) : > "$REPORT"; check_storage ;; keys) section "Keys"; watch_input 'gpio-keys|button|keys' keys 'Press each button a few times' ;; wifi) : > "$REPORT"; dmesg > /run/dmesg-boot.txt; check_system; check_wifi; dmesg > /run/dmesg.txt; say "DONE" ;; charge) charge ;; - *) echo "usage: hardware-check [wifi|touch|keys|charge]"; exit 2 ;; + *) echo "usage: hardware-check [storage|wifi|touch|keys|charge]"; exit 2 ;; esac diff --git a/examples/hardware-check/rootfs/usr/libexec/sftl-storage b/examples/hardware-check/rootfs/usr/libexec/sftl-storage new file mode 100644 index 00000000..5d7194a9 --- /dev/null +++ b/examples/hardware-check/rootfs/usr/libexec/sftl-storage @@ -0,0 +1,68 @@ +# P105 internal disk in the hardware-check RAM session. +is_p105() { + [ -r /proc/device-tree/compatible ] && + tr '\000' '\n' < /proc/device-tree/compatible | grep -Fxq apple,p105ap +} + +# BLKROGET checks the live block device. In fence mode also set BLKROSET +# and check again. Attempt every registered partition even if one fails. +sftl_readonly() ( + [ -e /sys/class/block/applesftl0 ] || return 1 + status=0 + for disk in /sys/class/block/applesftl0 /sys/class/block/applesftl0p*; do + [ -e "$disk" ] || continue + node=/dev/${disk##*/} + if [ "$(blockdev --getro "$node" 2>/dev/null)" = 1 ]; then continue; fi + if [ "$1" = fence ] && blockdev --setro "$node" 2>/dev/null && + [ "$(blockdev --getro "$node" 2>/dev/null)" = 1 ]; then continue; fi + status=1 + done + [ -e /sys/class/block/applesftl0 ] && return "$status" +) + +# List each registered device number. Only an unmatched partition glob is +# optional. A missing disk or a partition that disappears during the scan fails. +sftl_device_numbers() ( + for disk in /sys/class/block/applesftl0 /sys/class/block/applesftl0p*; do + case "$disk" in *'*') continue ;; esac + IFS= read -r number < "$disk/dev" && [ -n "$number" ] || return 1 + case "$number" in *[!0-9:]*) return 1 ;; esac + printf '%s ' "$number" + done +) + +# A source path in mountinfo can be an alias. Its third field identifies the +# underlying device even for a read-only mount. Check the same device set on +# either side of the mount table read and return it for comparison after I/O. +sftl_probe_state() ( + before=$(sftl_device_numbers) && sftl_readonly check || return 1 + awk -v numbers="$before" ' + BEGIN { + count = split(numbers, devices, " ") + for (i = 1; i <= count; i++) { + if (devices[i] !~ /^[0-9]+:[0-9]+$/) invalid = 1 + internal[devices[i]] = 1 + } + } + NF < 10 || $3 !~ /^[0-9]+:[0-9]+$/ || $3 in internal { invalid = 1 } + END { if (!NR || invalid) exit 1 } + ' /proc/self/mountinfo || return 1 + after=$(sftl_device_numbers) && [ "$after" = "$before" ] || return 1 + printf '%s\n' "$before" +) + +# The ARMv7 kernel has no uevent helper. Partitions can appear after the disk, +# including after coldplug has finished. Keep the serial shell independent. +watch_sftl() { + i=0 + while :; do + if sftl_readonly fence; then + [ -e /run/storage-fence ] || echo "$i" > /run/storage-fence + else + rm -f /run/storage-fence + if [ "$i" -lt 60 ]; then coldplug; fi + fi + sleep 1 + i=$((i + 1)) + done +} diff --git a/tests/test_hardware_check_storage.py b/tests/test_hardware_check_storage.py new file mode 100644 index 00000000..acb1d814 --- /dev/null +++ b/tests/test_hardware_check_storage.py @@ -0,0 +1,147 @@ +"""Exercise the hardware-check RAM project's storage scripts against fixture disks.""" +from pathlib import Path +import subprocess +import tempfile +import unittest + + +EXAMPLE = Path(__file__).resolve().parents[1] / 'examples/hardware-check/rootfs/usr' + + +class HardwareCheckStorage(unittest.TestCase): + def setUp(self): + temp = tempfile.TemporaryDirectory() + self.addCleanup(temp.cleanup) + self.root = Path(temp.name) + for directory in ('sys/class/block', 'dev', 'proc/device-tree', 'proc/self', 'run'): + (self.root / directory).mkdir(parents=True) + self.compatible = self.root / 'proc/device-tree/compatible' + self.compatible.write_bytes(b'apple,p105ap\0apple,s5l8942x\0') + self.mountinfo = self.root / 'proc/self/mountinfo' + self.mountinfo.write_text('24 1 0:18 / /run rw - tmpfs tmpfs rw\n') + + # Map the absolute RAM-root paths to the fixture. Only blockdev's + # kernel ioctls and the one-second clock tick need substitutes. + for name in ('libexec/sftl-storage', 'bin/hardware-check'): + source = (EXAMPLE / name).read_text() + for path in ('/sys/class/block', '/proc/device-tree', '/proc/self/mountinfo', '/run/', '/dev/applesftl', + '/dev/nvme', '/dev/${disk##*/}'): + source = source.replace(path, str(self.root) + path) + source = source.replace('/usr/libexec/sftl-storage', str(self.root / 'sftl-storage')) + (self.root / Path(name).name).write_text(source) + + self.shell = ''' +blockdev() { + case "$1" in + --getro) cat "$2.ro" ;; + --setro) [ -e "$2.ro" ] && { [ -e "$2.ignore" ] || echo 1 > "$2.ro"; } ;; + *) return 1 ;; + esac +} +sleep() { :; } +dd() { touch run/media-read; command dd "$@"; } +''' + + def disk(self, name, ro=0, data=b''): + block = self.root / 'sys/class/block' / name + block.mkdir() + minor = name.removeprefix('applesftl0').removeprefix('p') or '0' + (block / 'dev').write_text(f'253:{minor}\n') + node = self.root / 'dev' / name + node.write_bytes(data) + node.with_suffix('.ro').write_text(f'{ro}\n') + return node + + def run_shell(self, script): + return subprocess.run(['sh', '-c', self.shell + script], cwd=self.root, + capture_output=True, text=True, timeout=15) + + def report(self): + return self.run_shell('set -- storage; . ./hardware-check') + + def test_late_disk_and_partition_are_fenced(self): + result = self.run_shell(''' +set -eu +. ./sftl-storage +coldplug() { :; } +sleep() { + case "$i" in + 0) [ ! -e run/storage-fence ] + mkdir sys/class/block/applesftl0; echo 0 > dev/applesftl0.ro ;; + 1) [ -e run/storage-fence ]; [ "$(cat dev/applesftl0.ro)" = 1 ] + mkdir sys/class/block/applesftl0p3 ;; + 2) [ ! -e run/storage-fence ]; echo 0 > dev/applesftl0p3.ro ;; + 3) [ -e run/storage-fence ] && [ "$(cat dev/applesftl0p3.ro)" = 1 ]; exit $? ;; + esac +} +watch_sftl +''') + self.assertEqual(result.returncode, 0, result.stderr) + + def test_report_waits_for_verified_readonly_and_reads_full_block(self): + self.disk('applesftl0', ro=1, data=bytes(4096)) + partition = self.disk('applesftl0p1') + partition.with_suffix('.ignore').touch() # BLKROSET succeeds without taking effect. + self.assertNotEqual(self.run_shell('. ./sftl-storage; sftl_readonly fence').returncode, 0) + self.assertNotEqual(self.report().returncode, 0) + partition.with_suffix('.ro').write_text('1\n') + result = self.report() + self.assertEqual(result.returncode, 0, result.stderr) + self.assertIn('[PASS] read 4096 bytes from applesftl0', result.stdout) + (self.root / 'dev/applesftl0').write_bytes(bytes(512)) + result = self.report() + self.assertNotEqual(result.returncode, 0) + self.assertIn('[FAIL] short or failed read', result.stdout) + + def test_missing_disk_fails_only_on_p105(self): + result = self.report() + self.assertNotEqual(result.returncode, 0) + self.assertIn('[FAIL] P105 SFTL', result.stdout) + self.compatible.write_bytes(b'apple,j71b\0') + result = self.report() + self.assertEqual(result.returncode, 0, result.stderr) + self.assertIn('[SKIP] no NVMe storage', result.stdout) + + def test_mount_aliases_block_the_read_but_unrelated_mounts_do_not(self): + self.disk('applesftl0', ro=1, data=bytes(4096)) + self.disk('applesftl0p1', ro=1) + read = self.root / 'run/media-read' + for number in ('253:0', '253:1'): + with self.subTest(mounted=number): + self.mountinfo.write_text(f'28 1 {number} / /ios ro - hfsplus /dev/disk/by-label/iOS ro\n') + result = self.report() + self.assertNotEqual(result.returncode, 0) + self.assertIn('unmounted', result.stdout) + self.assertFalse(read.exists()) + self.mountinfo.write_text('28 1 253:10 / /other ro - ext4 /dev/disk/by-label/other ro\n') + self.assertEqual(self.report().returncode, 0) + self.assertTrue(read.exists()) + + def test_missing_or_invalid_mount_evidence_fails_before_io(self): + self.disk('applesftl0', ro=1, data=bytes(4096)) + for contents in ('', 'bad mount data\n', None): + with self.subTest(contents=contents): + if contents is None: + self.mountinfo.unlink() + else: + self.mountinfo.write_text(contents) + self.assertNotEqual(self.report().returncode, 0) + self.assertFalse((self.root / 'run/media-read').exists()) + self.mountinfo.write_text('24 1 0:18 / /run rw - tmpfs tmpfs rw\n') + (self.root / 'sys/class/block/applesftl0/dev').unlink() + self.assertNotEqual(self.report().returncode, 0) + self.assertFalse((self.root / 'run/media-read').exists()) + + def test_partition_registration_during_read_invalidates_the_probe(self): + self.disk('applesftl0', ro=1, data=bytes(4096)) + self.shell += ''' +dd() { + mkdir sys/class/block/applesftl0p2 + echo 253:2 > sys/class/block/applesftl0p2/dev + echo 1 > dev/applesftl0p2.ro + command dd "$@" +} +''' + result = self.report() + self.assertNotEqual(result.returncode, 0) + self.assertIn('state changed during the read', result.stdout) diff --git a/tools/p105/README.md b/tools/p105/README.md index 8976f2cb..abf410d4 100644 --- a/tools/p105/README.md +++ b/tools/p105/README.md @@ -8,21 +8,39 @@ and block as read-only parameters at load. It samples that block index across all 16 lanes and the adjacent blocks on the selected lane, in both normal and SLC views. Each open of `/dev/p105-capture` starts a new scan. -For an authorized observation, use a quiescent system and record its target, -device, trial, driver revision and issued operation separately. On the device, -load the matching module with the actual target values: +Use the PR's `p105-read-only-measurement` artifact as the kernel directory for +the current [hardware-check RAM project](../../examples/hardware-check/). +Set `KERNEL_DIR` to the directory containing `zImage`, `dtbs/`, `lib/` and +`orchard.commit`. From the Orchard checkout, verify the files and the live +kernel before taking a baseline. `bin/orchard exec` uses the running session's +SSH transport. The kernel notes digest distinguishes builds with the same +`uname -r`. ```sh -timeout -k 5 30 modprobe p105_capture bus="$BUS" cau="$CAU" block="$BLOCK" +(cd "$KERNEL_DIR" && shasum -a 256 -c SHA256SUMS) || exit 1 +notes=$(bin/orchard exec -- timeout -k 5 10 sha256sum /sys/kernel/notes) || exit 1 +test "${notes%% *}" = "$(cat "$KERNEL_DIR/kernel.notes.sha256")" || exit 1 +bin/orchard exec -- timeout -k 5 10 cat /proc/self/mountinfo || exit 1 +bin/orchard exec -- timeout -k 5 90 hardware-check storage || exit 1 ``` -Stream directly into a private host file from the already accessible device. -This example uses an existing SSH connection with permission to read the -capture node. The stream contains full flash data and out-of-band metadata. +Confirm that `/` is the RAM root. The storage check uses mountinfo device +numbers to reject a mounted internal disk or partition, including read-only +mounts through aliases. Record the check's 4 KiB read before the baseline as +part of the trial. Record the board, `orchard.commit`, target +coordinates and the issued operation. With the system quiescent, load the +module using the measured bus, CAU and block for this trial. + +```sh +bin/orchard exec -- timeout -k 5 30 modprobe p105_capture bus="$BUS" cau="$CAU" block="$BLOCK" +``` + +Stream the flash data and out-of-band metadata directly into a private host +file. Use a fresh directory for each trial. A failed read leaves a partial file. ```sh umask 077 -ssh "$DEVICE" 'timeout -k 5 600 cat /dev/p105-capture' > before.ppns.partial && +bin/orchard exec -- timeout -k 5 600 cat /dev/p105-capture > before.ppns.partial && mv before.ppns.partial before.ppns ``` From 59bdee56939f90d2e7e285eb69c1cac959f874a5 Mon Sep 17 00:00:00 2001 From: Shayne Legault <8741557+shaynelegault@users.noreply.github.com> Date: Thu, 19 Nov 2026 16:26:39 -0600 Subject: [PATCH 08/11] p105: record read-only flash qualification on the 64 GB iPad Capture the native iOS counters over paired USB and reject mismatched flash geometry. Document the verified raw-write exclusion and the iOS pages read through both operating systems. Keep reclamation pending its physical qualification. Assisted-by: LLM --- chips/s5l8942x.toml | 4 +- devices/p105/device.toml | 14 +- docs/bring-up/p105-sftl-checkpoint-gc.md | 8 +- .../p105-sftl-device-qualification.md | 108 ++++++ docs/compatibility.md | 2 +- kernel/modules/p105/map-audit/Makefile | 2 + .../modules/p105/map-audit/p105_map_audit.c | 366 ++++++++++++++++++ tests/test_p105_ios_counters.py | 89 +++++ tests/test_p105_raw_exclusion.py | 44 +++ tools/p105/README.md | 20 + tools/p105/capture_ios_counters.py | 163 ++++++++ tools/p105/check_raw_exclusion.py | 122 ++++++ 12 files changed, 930 insertions(+), 12 deletions(-) create mode 100644 docs/bring-up/p105-sftl-device-qualification.md create mode 100644 kernel/modules/p105/map-audit/Makefile create mode 100644 kernel/modules/p105/map-audit/p105_map_audit.c create mode 100644 tests/test_p105_ios_counters.py create mode 100644 tests/test_p105_raw_exclusion.py create mode 100644 tools/p105/capture_ios_counters.py create mode 100644 tools/p105/check_raw_exclusion.py diff --git a/chips/s5l8942x.toml b/chips/s5l8942x.toml index 5a9159f9..5f950616 100644 --- a/chips/s5l8942x.toml +++ b/chips/s5l8942x.toml @@ -15,7 +15,7 @@ firmware = "P105 recipe for iOS 8.4.1 encrypted images (docs/bring-up/p105-firmw platform = "P105 on a Cascadia-based build" stages = "Project Cascadia P105 boot chain" reference_version = "Project Cascadia builds its A5 boot chain from 8.4.1 / 12H321" -nand_reads = "Measured P105 64 GB PPN geometry has eight CAUs per bus, 2088 blocks per CAU and 128 normal pages per block; exact ARMv7 read-topic qualification is pending (docs/bring-up/p105-nand-reads.md)." +nand_reads = "The 64 GB P105 has eight CAUs per bus, 2088 blocks per CAU and 128 normal pages per block. PR #93 matched 29 live logical pages to physical data and verified both LwVM headers (docs/bring-up/p105-sftl-device-qualification.md)." nand_program = "The H2FMI driver programs complete PPN pages only into erased destinations, verifies full readback and leaves physical erase disabled pending qualification (docs/bring-up/p105-nand-program.md)." -nand_checkpoint_gc = "The 64 GB SwissPPNFTL writer and native firmware agree on healthy allocation, wear and both provider statistics records in offline replay. Missing context evidence permits read-only recovery. Physical P105 accounting inputs and erase/reuse await qualification (docs/bring-up/p105-sftl-checkpoint-gc.md)." +nand_checkpoint_gc = "The 64 GB P105 verified raw write exclusion and exposed both live statistics records through paired iOS. Counter continuity through shutdown and physical erase and reuse still need qualification (docs/bring-up/p105-sftl-device-qualification.md)." usb_phy = "P105 uses a 24 MHz reference; the proposed S5L8942X cold-handoff callbacks await an exact-revision boot (docs/bring-up/p105-usb-handoff.md)." diff --git a/devices/p105/device.toml b/devices/p105/device.toml index 0c163a04..c58077db 100644 --- a/devices/p105/device.toml +++ b/devices/p105/device.toml @@ -29,10 +29,10 @@ recipe = "firmware.json" # (OrchardLinux/orchard#93), freed space is never reused and writes fail once # the blank flash runs out. Drop this table when it lands; the installer # warns from it and allows the install either way. -# Native checkpoint accounting was replayed offline. The P105 kernel -# excludes raw programs while SFTL owns the disk. Runtime controller events -# and the physical erase footprint still need hardware qualification -# (docs/bring-up/p105-sftl-checkpoint-gc.md). +# On the 64 GB P105 all 16 raw devices refused programs while SFTL owned +# the disk. Paired iOS returned all 69 counters. The handoff to Linux and +# the physical erase footprint still need measurement +# (docs/bring-up/p105-sftl-device-qualification.md). [storage] reclaims = false @@ -50,9 +50,9 @@ accelerometer = "missing" gyroscope = "missing" light_sensor = "missing" touch_id = "missing" -# The H2FMI driver reads the NAND and programs verified pages; this revision is -# untested on hardware (docs/bring-up/p105-nand-reads.md, p105-nand-program.md). -storage_read = "untested" +# The 64 GB P105 read current logical pages matching the physical captures. +# The PR #93 device trial used read-only internal volumes. +storage_read = "working" storage_write = "untested" wifi = "untested" bluetooth_input = "untested" diff --git a/docs/bring-up/p105-sftl-checkpoint-gc.md b/docs/bring-up/p105-sftl-checkpoint-gc.md index 6a8917ef..9395fd0b 100644 --- a/docs/bring-up/p105-sftl-checkpoint-gc.md +++ b/docs/bring-up/p105-sftl-checkpoint-gc.md @@ -190,6 +190,10 @@ Linux data and actual donor reuse after a cold start as well. ## Remaining before hardware enablement +The 64 GB P105 ran the PR kernel and verified raw write exclusion on all +16 devices. The device report records block ownership and the counter +measurements. [P105 qualification results](p105-sftl-device-qualification.md) + The physical provider needs a trustworthy starting state and a continuous record of native-equivalent reads, programs, erases, synchronizations and their completion flags. One saved counter sample plus the logical journal @@ -199,8 +203,8 @@ issued different maintenance erases. Losing that history invalidates the counter model. Scrub and bad-member replacement also need qualification. Physical qualification must establish the exact erase footprint in both -normal and SLC views, verify the raw write exclusion on the tablet, -and establish recovery after a partial erase or block retirement. Before a physical +normal and SLC views and verify recovery after a partial erase or block +retirement. Before a physical donor can be returned for writing, the driver must also reconcile the reported erase outcome with native wear and free-block state. End-to-end testing on the measured P105 must verify cold-boot recovery and preservation of diff --git a/docs/bring-up/p105-sftl-device-qualification.md b/docs/bring-up/p105-sftl-device-qualification.md new file mode 100644 index 00000000..e2983cf2 --- /dev/null +++ b/docs/bring-up/p105-sftl-device-qualification.md @@ -0,0 +1,108 @@ +# PR #93 on the 64 GB iPad mini + +On 19 November 2026 the P105 ran the ARMv7 kernel built for PR head +`bd8916c` from integration commit `8b7f4cbb`. Its kernel notes matched +the build's SHA-256, `bdad8ec70b7f63a04bd1b33002ca89767b3550ef59eb4bcc8a74530efbac1e92`. +Linux ran from the Mac over USB. The internal disk and its three partitions +were read-only and unmounted during the measurements. +[ARMv7 measurement build](https://github.com/OrchardLinux/orchard/actions/runs/44556846039) + +## Raw writes and live ownership + +All 16 raw MTD devices returned `EBUSY` for a complete page request while +SFTL owned the disk. Each request named block 2087, which this driver +rejects before issuing a controller command even when SFTL releases the disk. +The contention test held the semaphore that protects an in-flight raw program +on the real controller. A competing SFTL claim waited for release and then +returned `EBUSY` because the disk was still bound. The test submitted no +physical program. + +An independent load recovered 12,793 map runs and 32 native context records. +Block 896 held 29 current 4 KiB pages across eight lanes in the iOS Data +partition. Every logical read matched the data at its recovered physical +address. The primary and backup LwVM headers passed their CRC and matched the pair from +an earlier native iOS boot. An erase trial on block 896 would put live iOS +data at risk. + +## Spare areas + +Block 1472 had the native dirty-free role and zero saved or current mappings. +Its complete diagnostic capture covered all 16 lanes and one lane on each +neighbor. On 1472, all 2,048 normal rows contained data. Each of the 1,024 +SLC reads returned `0x61` with no data phase. Block 1471 had ten live +mappings across the block. The sampled lane on block 1473 was already blank +in both views. + +The survey sampled page zero on bus 0, CAU 0 in 1,085 further candidate +locations. Each candidate and both neighbors had the dirty-free role, zero +flags and zero saved or current mappings. The survey excluded the recovered +VFL bank members and their neighbors. Of those samples, 1,072 were blank +in both views and 13 returned retired status without data in both views. +These results cover the specified page and lane. The erase footprint and +recovery from a partial erase still need a physical trial with observable +controls. + +## Native counters + +Checkpoint sequence 189791520 contained the 88-byte VFL record (ID 255) +and the 464-byte lower-provider record (ID 254). The live journal reached +189808092. The saved allocator selected block 1380 as its landing block. +By this boot it already held 6,625 live pages. + +Four reads through `/dev/mtd0ro` returned 65,536 bytes matching the +independent physical capture. Reading the checkpoint again yielded identical +counter records and the same journal sequence. A live accounting provider +needs an authoritative sample with a continuous record of operations and +completion flags across the handoff into Linux, including its initial +media scan. Reclamation remains disabled pending that handoff and the +physical erase and cold-boot tests. + +On iOS 9.3.5, the paired USB service returned both live records in +`com.apple.disk_usage` under `NANDInfo`. The collector verified 40 saved +responses and four fresh ones. Each contained matching copies of the 11 VFL +and 58 lower-provider counters. The geometry record +`0x03000f00` contained the measured 64 GB signature at byte offset 48. +Starting there, five little-endian words read `2, 2088, 8, 64, 128`. + +| Read in native iOS | Additional 4 KiB pages in VFL | Change in lower-provider word 1 | +| --- | ---: | ---: | +| 1 MiB on System | 256 | 94 | +| 4 MiB on Data | 1,024 | 433 | + +The first System interval also recorded a program covering 32 +pages. A repeat showed the same read counts with no program delta. In two +comparisons, reversing the read order produced identical changes in all 69 +counters. The paired system log yielded no per-command flash records during +the controlled read. Between the last iOS sample and Linux's first request, +an ordered firmware trace or a verified interval of flash inactivity is still +needed. + +After returning to iOS, both copies of the LwVM partition header passed +their CRC and matched the Linux measurements. All 29 sampled Data pages +from block 896 also matched. These are measurements taken before any +erase trial. + +## Repeating the checks + +Verify the running kernel notes against the measurement artifact and check +the internal volumes before collecting data. Keep the full map and captures +private. [P105 capture procedure](../../tools/p105/) + +On a P105 session with Python 3, stage `tools/p105/check_raw_exclusion.py` +in RAM. The script pins the kernel whose tail-block guard was reviewed. + +```sh +timeout -k 5 45 python3 /run/check_raw_exclusion.py +``` + +The optional `p105_map_audit` module recovers a second map through the +driver's read callback. Its report includes provider offsets and logical page +numbers for the selected block. Run these commands on the matching P105, +with `debugfs` available at `/sys/kernel/debug`. Direct the report to the +host or to tmpfs. Adding `survey=1` at load also samples eligible spare areas. + +```sh +timeout -k 5 180 modprobe p105_map_audit block=1472 +timeout -k 5 30 cat /sys/kernel/debug/p105_map_audit > /run/p105-map-report +timeout -k 5 15 modprobe -r p105_map_audit +``` diff --git a/docs/compatibility.md b/docs/compatibility.md index db40c7d2..2aa5ad8d 100644 --- a/docs/compatibility.md +++ b/docs/compatibility.md @@ -138,7 +138,7 @@ Boards Orchard builds for. States: working, untested, missing, none (no such har | Board | Display | Screen on/off | Backlight | Touchscreen | Buttons | Accelerometer | Gyroscope | Light sensor | Touch ID | Internal storage read | Internal storage write | Wi-Fi | Bluetooth input devices | Bluetooth audio | Cellular | Battery status | Charging | Charging control | Suspend | Resume | USB serial | USB network | USB host | Speakers | Microphone | Cameras | Hardware H.265 video decoding | | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | -| `p105` | untested | untested | untested | untested | untested | missing | missing | missing | missing | untested | untested | untested | untested | untested | none | untested | untested | untested | missing | missing | untested | untested | missing | untested | missing | missing | missing | +| `p105` | untested | untested | untested | untested | untested | missing | missing | missing | missing | working | untested | untested | untested | untested | none | untested | untested | untested | missing | missing | untested | untested | missing | untested | missing | missing | missing | | `j1` | untested | missing | missing | untested | missing | missing | missing | missing | none | missing | missing | untested | missing | missing | none | missing | missing | missing | missing | missing | untested | untested | missing | missing | missing | missing | missing | | `j71` | untested | missing | untested | missing | untested | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | untested | untested | untested | untested | missing | missing | missing | missing | missing | | `j73` | untested | missing | untested | missing | untested | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | untested | untested | untested | untested | missing | missing | missing | missing | missing | diff --git a/kernel/modules/p105/map-audit/Makefile b/kernel/modules/p105/map-audit/Makefile new file mode 100644 index 00000000..ee0db30c --- /dev/null +++ b/kernel/modules/p105/map-audit/Makefile @@ -0,0 +1,2 @@ +obj-m := p105_map_audit.o +ccflags-y += -I$(srctree)/drivers/mtd diff --git a/kernel/modules/p105/map-audit/p105_map_audit.c b/kernel/modules/p105/map-audit/p105_map_audit.c new file mode 100644 index 00000000..56aca532 --- /dev/null +++ b/kernel/modules/p105/map-audit/p105_map_audit.c @@ -0,0 +1,366 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Recover the P105 map through a provider with a read callback only. */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "apple-sftl.h" +#include "apple-sftl-account.h" +#include "apple-sftl-reclaim.h" +#include "apple-sftl-vfl.h" + +static unsigned int block = UINT_MAX; +module_param(block, uint, 0444); +MODULE_PARM_DESC(block, "Physical block index whose live logical mappings to report"); +static bool survey; +module_param(survey, bool, 0444); +MODULE_PARM_DESC(survey, "Sample page zero in both views on lane zero of unmapped dirty-free neighborhoods"); + +static u64 checkpoint, journal; +static u32 run_count, opaque_count, owned_count; +static u32 *owners; +static struct dentry *report; +static struct { + u16 saved, mapped, wear; + u8 role, flags; +} usage[APPLE_SFTL_SUBBLOCKS]; +static struct apple_sftl_allocation saved_plan; +static u8 saved_vfl[88], saved_lower[464]; +static struct { + u8 status[2]; + bool payload[2], nonblank[2], sampled; +} samples[APPLE_SFTL_SUBBLOCKS]; + +static bool dirty_free_neighborhood(u32 b, const struct apple_sftl_vfl_source *vfl) +{ + u32 i; + + for (i = b - 1; i <= b + 1; i++) + if (usage[i].role != 9 || usage[i].flags || usage[i].saved || usage[i].mapped) + return false; + for (i = 0; i < ARRAY_SIZE(vfl->members); i++) + if (vfl->members[i].block >= b - 1 && vfl->members[i].block <= b + 1) + return false; + return true; +} + +static int sample_neighborhoods(struct apple_ppn *reader, struct apple_sftl *cold) +{ + struct apple_sftl_vfl_source vfl; + u8 *data, oob[APPLE_PPN_OOB_SIZE]; + u32 b, view; + int ret = apple_sftl_vfl_source(cold, &vfl); + + if (ret) + return ret; + data = kmalloc(APPLE_PPN_PAGE_SIZE, GFP_KERNEL); + if (!data) + return -ENOMEM; + for (b = 33; b < 2054; b++) { + if (!dirty_free_neighborhood(b, &vfl)) + continue; + for (view = 0; view < 2; view++) { + u8 status = 0; + + ret = reader->ops->read_page(reader->priv, 0, 0, b, 0, view, + data, oob, &status); + samples[b].status[view] = status; + if ((status == 0x45 && !ret) || + (view && status == 0x61 && ret == -EIO)) { + ret = 0; + continue; + } + if (ret) + goto out; + if (status != 0x40 && status != 0x42 && status != 0x49) { + ret = -EBADMSG; + goto out; + } + samples[b].payload[view] = true; + samples[b].nonblank[view] = memchr_inv(data, 0xff, APPLE_PPN_PAGE_SIZE) || + memchr_inv(oob, 0xff, sizeof(oob)); + if (status == 0x49 && samples[b].nonblank[view]) { + ret = -EBADMSG; + goto out; + } + } + samples[b].sampled = true; + cond_resched(); + } +out: + kfree(data); + return ret; +} + +static int read_checkpoint_stats(const u8 *page) +{ + u32 i, pos = 4; + + if (get_unaligned_le32(page) != 45) + return -EBADMSG; + for (i = 0; i < 45; i++) { + u32 words, id; + + if (pos > APPLE_SFTL_CHUNK - 8) + return -EBADMSG; + id = get_unaligned_le32(page + pos); + words = get_unaligned_le32(page + pos + 4); + pos += 8; + if (words > (APPLE_SFTL_CHUNK - pos) / 4 || + (i < 43 && (id != i + 1 || words != 2)) || + (i == 43 && (id != 255 || words * 4 != sizeof(saved_vfl))) || + (i == 44 && (id != 254 || words * 4 != sizeof(saved_lower)))) + return -EBADMSG; + if (i == 43) + memcpy(saved_vfl, page + pos, sizeof(saved_vfl)); + else if (i == 44) + memcpy(saved_lower, page + pos, sizeof(saved_lower)); + pos += words * 4; + } + return 0; +} + +struct claim_check { + struct work_struct work; + struct completion entered, finished; + struct apple_ppn_access *access; + int result; +}; + +static void attempt_claim(struct work_struct *work) +{ + struct claim_check *check = container_of(work, struct claim_check, work); + + complete(&check->entered); + check->result = apple_ppn_claim_sftl(check->access); + complete(&check->finished); +} + +/* Hold the actual raw program semaphore without submitting I/O. The child + * remains bound under its device lock, so the second claim must wait and + * then report EBUSY. Both operations use the shipping ownership code. + */ +static int check_drain(struct apple_ppn_access *access) +{ + struct claim_check check = { .access = access }; + unsigned long until; + bool queued; + + INIT_WORK(&check.work, attempt_claim); + init_completion(&check.entered); + init_completion(&check.finished); + down_read(&access->lock); + if (rwsem_is_contended(&access->lock)) { + up_read(&access->lock); + return -EAGAIN; + } + queue_work(system_unbound_wq, &check.work); + queued = wait_for_completion_timeout(&check.entered, 2 * HZ) != 0; + until = jiffies + 2 * HZ; + while (queued && !rwsem_is_contended(&access->lock) && + !completion_done(&check.finished) && time_before(jiffies, until)) + usleep_range(500, 1000); + queued = queued && rwsem_is_contended(&access->lock) && !completion_done(&check.finished); + up_read(&access->lock); + flush_work(&check.work); + return queued && check.result == -EBUSY ? 0 : -EIO; +} + +static int match_controller(struct device *dev, const void *ignored) +{ + return of_device_is_compatible(dev->of_node, "apple,s5l8940x-fmi"); +} + +static int match_child(struct device *dev, const void *parent) +{ + return dev->parent == parent && + !strcmp(to_platform_device(dev)->name, "apple-sftl"); +} + +static int collect(struct apple_sftl_snapshot *snapshot) +{ + u32 first = block * APPLE_SFTL_STRIDE, i; + int ret; + + /* This production parser checks the complete native accounting prefix. */ + ret = apple_sftl_account_plan_allocation(snapshot, &saved_plan); + if (ret) + return ret; + ret = read_checkpoint_stats(snapshot->opaque[4].data); + if (ret) + return ret; + for (i = 0; i < APPLE_SFTL_SUBBLOCKS; i++) { + const u8 *entry = snapshot->opaque[8 + i / 512].data + 8 * (i % 512); + + usage[i].role = entry[0]; + usage[i].flags = entry[1]; + usage[i].saved = get_unaligned_le16(entry + 2); + usage[i].wear = get_unaligned_le16(entry + 6); + } + + memset(owners, 0xff, APPLE_SFTL_STRIDE * sizeof(*owners)); + for (i = 0; i < snapshot->nr_runs; i++) { + const struct apple_sftl_extent *run = &snapshot->runs[i]; + u64 limit = (u64)run->provider + run->length; + u32 start, end, pos; + + if (run->provider == APPLE_SFTL_NO_ADDRESS) + continue; + if (limit > APPLE_SFTL_SUBBLOCKS * APPLE_SFTL_STRIDE || + (u64)run->logical + run->length > APPLE_SFTL_LOGICAL_PAGES) + return -EBADMSG; + for (pos = run->provider; pos < limit; pos = end) { + u32 b = pos / APPLE_SFTL_STRIDE; + + end = min((b + 1) * APPLE_SFTL_STRIDE, (u32)limit); + if (usage[b].mapped + (end - pos) > APPLE_SFTL_STRIDE) + return -EBADMSG; + usage[b].mapped += end - pos; + } + start = max(first, run->provider); + end = min(first + APPLE_SFTL_STRIDE, (u32)limit); + for (pos = start; pos < end; pos++) { + if (owners[pos - first] != APPLE_SFTL_NO_ADDRESS) + return -EBADMSG; + owners[pos - first] = run->logical + (pos - run->provider); + owned_count++; + } + } + checkpoint = snapshot->checkpoint_sequence; + journal = snapshot->journal_sequence; + run_count = snapshot->nr_runs; + opaque_count = snapshot->nr_opaque; + return 0; +} + +static int recover(struct device *parent, struct device *child) +{ + struct apple_ppn *live = dev_get_platdata(child); + struct apple_ppn_ops ops = {}; + struct apple_ppn reader; + struct apple_sftl_snapshot *snapshot = NULL; + struct apple_sftl *cold = NULL; + int ret; + + if (!device_is_registered(child) || !device_is_bound(parent) || + strcmp(dev_driver_string(parent), "apple-h2fmi") || + !device_is_bound(child) || strcmp(dev_driver_string(child), "apple-sftl") || + !live || live->dev != parent || !live->access || !live->ops || + !live->ops->read_page || live->ops->erase_block || live->buses != 2 || + live->caus != 8 || live->blocks != 2088 || live->pages != 128) + return -ENODEV; + ret = apple_ppn_begin_raw_write(live->access); + if (!ret) { + apple_ppn_end_raw_write(live->access); + return -EPERM; + } + if (ret != -EBUSY) + return ret; + ret = check_drain(live->access); + if (ret) + return ret; + ops.read_page = live->ops->read_page; + reader = *live; + reader.ops = &ops; + reader.access = NULL; + ret = apple_sftl_load(&reader, &cold); + if (!ret) + ret = apple_sftl_snapshot(cold, 8 * SZ_1M, &snapshot); + if (!ret) + ret = collect(snapshot); + if (!ret && survey) + ret = sample_neighborhoods(&reader, cold); + apple_sftl_snapshot_free(snapshot); + apple_sftl_free(cold); + return ret; +} + +static int audit_show(struct seq_file *seq, void *ignored) +{ + u32 i; + + seq_printf(seq, "block %u\ncheckpoint %llu\njournal %llu\nruns %u\ncontext_records %u\nlive_chunks %u\n", + block, checkpoint, journal, run_count, opaque_count, owned_count); + seq_puts(seq, "bound_claim_waited_for_raw_semaphore 1\nphysical_erase_callback 0\n"); + seq_puts(seq, "checkpoint_stats_255 "); + for (i = 0; i < sizeof(saved_vfl); i++) + seq_printf(seq, "%02x", saved_vfl[i]); + seq_puts(seq, "\ncheckpoint_stats_254 "); + for (i = 0; i < sizeof(saved_lower); i++) + seq_printf(seq, "%02x", saved_lower[i]); + seq_putc(seq, '\n'); + seq_printf(seq, "saved_plan_landing %u\n", saved_plan.landing); + for (i = 0; i < saved_plan.nr_erase; i++) + seq_printf(seq, "saved_plan_erase %u\n", saved_plan.erase[i]); + for (i = 0; i < APPLE_SFTL_SUBBLOCKS; i++) + seq_printf(seq, "usage %u %u %u %u %u %u\n", i, usage[i].role, + usage[i].flags, usage[i].saved, usage[i].mapped, usage[i].wear); + for (i = 0; i < APPLE_SFTL_SUBBLOCKS; i++) + if (samples[i].sampled) + seq_printf(seq, "sample %u %02x %u %u %02x %u %u\n", i, + samples[i].status[0], samples[i].payload[0], samples[i].nonblank[0], + samples[i].status[1], samples[i].payload[1], samples[i].nonblank[1]); + for (i = 0; i < APPLE_SFTL_STRIDE; i++) + if (owners[i] != APPLE_SFTL_NO_ADDRESS) + seq_printf(seq, "mapping %u %u\n", i, owners[i]); + return 0; +} +DEFINE_SHOW_ATTRIBUTE(audit); + +static int __init audit_init(void) +{ + struct device *parent, *child; + int ret = -ENODEV; + + if (!of_machine_is_compatible("apple,p105ap") || !block || + block >= APPLE_SFTL_SUBBLOCKS) + return -EINVAL; + parent = bus_find_device(&platform_bus_type, NULL, NULL, match_controller); + if (!parent) + return -ENODEV; + child = bus_find_device(&platform_bus_type, NULL, parent, match_child); + if (!child) + goto put_parent; + owners = kvmalloc_array(APPLE_SFTL_STRIDE, sizeof(*owners), GFP_KERNEL); + if (!owners) { + ret = -ENOMEM; + goto put_child; + } + device_lock(parent); + device_lock(child); + ret = recover(parent, child); + device_unlock(child); + device_unlock(parent); + if (!ret) { + report = debugfs_create_file("p105_map_audit", 0400, NULL, NULL, + &audit_fops); + ret = PTR_ERR_OR_ZERO(report); + } + if (ret) + kvfree(owners); +put_child: + put_device(child); +put_parent: + put_device(parent); + return ret; +} + +static void __exit audit_exit(void) +{ + debugfs_remove(report); + kvfree(owners); +} + +module_init(audit_init); +module_exit(audit_exit); +MODULE_DESCRIPTION("Read-only audit of current logical ownership of a P105 flash block"); +MODULE_LICENSE("GPL"); diff --git a/tests/test_p105_ios_counters.py b/tests/test_p105_ios_counters.py new file mode 100644 index 00000000..ac1793ea --- /dev/null +++ b/tests/test_p105_ios_counters.py @@ -0,0 +1,89 @@ +# SPDX-License-Identifier: GPL-2.0-only +import importlib.util +import json +from pathlib import Path +import plistlib +import struct +import tempfile +import unittest +from types import SimpleNamespace +from unittest.mock import patch + +tools = Path(__file__).resolve().parents[1] / "tools/p105" + + +def load(name): + spec = importlib.util.spec_from_file_location(name, tools / (name + ".py")) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +ios = load("capture_ios_counters") + + +def envelope(selector, raw=b"", flags=0): + return struct.pack("<4I", selector, flags, len(raw), 0) + raw + + +def nandinfo(): + raw = {tag: bytes([tag]) * width for tag, (_, width) in ios.DIRECT.items()} + table = struct.pack(" len(blob) or reserved: + raise ValueError("invalid envelope length or reserved field") + if selector in (OPEN, CLOSE): + if size or flags != 0x20001 or (selector == OPEN and offset != 0): + raise ValueError("invalid envelope boundary") + closed = selector == CLOSE + if selector in wanted: + if flags or selector in chunks: + raise ValueError("flagged or repeated counter record") + chunks[selector] = blob[start:end] + offset = end + if not closed or set(chunks) != wanted: + raise ValueError("incomplete NANDInfo counter envelope") + geometry = chunks[GEOMETRY] + if len(geometry) != 116 or struct.unpack_from("<5I", geometry, 48) != P105_GEOMETRY: + raise ValueError("unexpected flash geometry for the 64 GB P105") + table = chunks[TABLE] + if len(table) < 4 or struct.unpack_from(" len(table) or struct.unpack_from(" MAX_PLIST: + raise ValueError("oversized iOS response") + return request.stdout + + +def timestamp(): + return datetime.now(timezone.utc).isoformat(timespec="microseconds") + + +def save_private(path, data): + with os.fdopen(os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600), "wb") as stream: + stream.write(data) + + +def save_json(path, obj): + save_private(path, (json.dumps(obj, sort_keys=True, indent=2) + "\n").encode()) + + +def capture(args): + if args.samples < 1 or args.samples > 10000 or not 0 <= args.interval <= 60: + raise ValueError("choose 1 to 10000 samples and a 0 to 60 second interval") + for key, expected in (("ProductType", "iPad2,5"), ("HardwareModel", "p105ap"), ("BuildVersion", "13G36")): + value = usb_query(args.ideviceinfo, args.udid, "-k", key).decode().strip() + if value.lower() != expected.lower(): + raise ValueError(f"unexpected {key} for this profile") + args.output.mkdir(mode=0o700) + count = 0 + error = None + try: + for n in range(args.samples): + if n: + time.sleep(args.interval) + wall = timestamp() + start = time.monotonic_ns() + data = usb_query(args.ideviceinfo, args.udid, "-q", "com.apple.disk_usage", "-k", "NANDInfo", "-x") + end = time.monotonic_ns() + received = timestamp() + raw = plistlib.loads(data) + if not isinstance(raw, bytes): + raise ValueError("NANDInfo is not a data property") + pair = decode_nandinfo(raw) + stem = f"sample-{n:05d}" + save_private(args.output / (stem + ".bin"), raw) + save_json(args.output / (stem + ".json"), { + "requested_utc": wall, "received_utc": received, "start_monotonic_ns": start, + "end_monotonic_ns": end, "product": "iPad2,5", "build": "13G36", + "nandinfo_sha256": sha256(raw).hexdigest(), + "provider_sha256": {tag: sha256(buf).hexdigest() for tag, buf in pair.items()}, + }) + count += 1 + except (ValueError, OSError, KeyboardInterrupt, subprocess.TimeoutExpired, plistlib.InvalidFileException) as exc: + # A connection loss is an observation gap even if the next OS comes up. + error = str(exc) if isinstance(exc, (ValueError, OSError)) else type(exc).__name__ + raise + finally: + save_json(args.output / "run.json", { + "samples": count, "requested": args.samples, "error": error, "finished_utc": timestamp(), + "coverage": "iOS USB snapshots", "requested_series_complete": count == args.samples, + "continuous_handoff": False, + }) + print(f"Saved {count} iOS snapshots with matching provider records.") + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--ideviceinfo", default="ideviceinfo") + parser.add_argument("--udid", required=True, help="paired P105 identifier from idevice_id -l") + parser.add_argument("--samples", type=int, default=1) + parser.add_argument("--interval", type=float, default=0, help="seconds between completed queries") + parser.add_argument("output", type=Path, help="new private directory on the host") + args = parser.parse_args() + try: + capture(args) + except (ValueError, OSError, subprocess.TimeoutExpired, plistlib.InvalidFileException) as exc: + print(f"Capture stopped: {type(exc).__name__ if isinstance(exc, subprocess.TimeoutExpired) else exc}", file=sys.stderr) + return 1 + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/tools/p105/check_raw_exclusion.py b/tools/p105/check_raw_exclusion.py new file mode 100644 index 00000000..394a0b88 --- /dev/null +++ b/tools/p105/check_raw_exclusion.py @@ -0,0 +1,122 @@ +#!/usr/bin/env python3 +"""Check the bound P105 raw-write gate using the driver's reserved tail block.""" + +from contextlib import ExitStack +import ctypes +import errno +import fcntl +import hashlib +import json +import os +from pathlib import Path +import stat +import struct + + +# Reviewed h2fmi_mtd_write_oob() and h2fmi_program_page() in the kernel built +# for this artifact. The former tests ownership; the latter rejects both tail +# blocks before the first controller transaction. Pin this test to those bytes. +NOTES = "bdad8ec70b7f63a04bd1b33002ca89767b3550ef59eb4bcc8a74530efbac1e92" +BLOCKS = 2088 +ERASE_SIZE = 2097152 +PAGE_SIZE = 16384 +OOB_SIZE = 64 +REQUEST = struct.Struct(" Date: Thu, 19 Nov 2026 16:44:29 -0600 Subject: [PATCH 09/11] p105: compare complete iOS flash diagnostic intervals Replay the native counter increments from one controlled interval against the other starting sample. Byte-check the complete responses to detect any evidence of read order outside the provider counters. Record the pre-erase file checks on both iOS volumes. Assisted-by: LLM --- .../p105-sftl-device-qualification.md | 24 +++++--- tests/test_p105_ios_counters.py | 36 +++++++++++- tools/p105/README.md | 13 ++++- tools/p105/capture_ios_counters.py | 26 ++++++--- tools/p105/compare_ios_intervals.py | 57 +++++++++++++++++++ 5 files changed, 136 insertions(+), 20 deletions(-) create mode 100644 tools/p105/compare_ios_intervals.py diff --git a/docs/bring-up/p105-sftl-device-qualification.md b/docs/bring-up/p105-sftl-device-qualification.md index e2983cf2..926e85e9 100644 --- a/docs/bring-up/p105-sftl-device-qualification.md +++ b/docs/bring-up/p105-sftl-device-qualification.md @@ -58,9 +58,9 @@ media scan. Reclamation remains disabled pending that handoff and the physical erase and cold-boot tests. On iOS 9.3.5, the paired USB service returned both live records in -`com.apple.disk_usage` under `NANDInfo`. The collector verified 40 saved -responses and four fresh ones. Each contained matching copies of the 11 VFL -and 58 lower-provider counters. The geometry record +`com.apple.disk_usage` under `NANDInfo`. The collector verified 48 responses +across the controlled reads and their checks. Each contained matching copies +of the 11 VFL and 58 lower-provider counters. The geometry record `0x03000f00` contained the measured 64 GB signature at byte offset 48. Starting there, five little-endian words read `2, 2088, 8, 64, 128`. @@ -72,15 +72,23 @@ Starting there, five little-endian words read `2, 2088, 8, 64, 128`. The first System interval also recorded a program covering 32 pages. A repeat showed the same read counts with no program delta. In two comparisons, reversing the read order produced identical changes in all 69 -counters. The paired system log yielded no per-command flash records during -the controlled read. Between the last iOS sample and Linux's first request, -an ordered firmware trace or a verified interval of flash inactivity is still +provider counters. Each full USB reply was 8,766 bytes long. Applying the +counter changes from one read order to the starting reply for the opposite +order reproduced its ending reply byte for byte. This held in both directions +for each pair and included the other 43 values in the statistics table. +One interval with background programs produced a different result. + +The paired system log yielded no per-command flash records during the +controlled read. Between the last iOS sample and Linux's first request, an +ordered firmware trace or a verified interval of flash inactivity is still needed. After returning to iOS, both copies of the LwVM partition header passed their CRC and matched the Linux measurements. All 29 sampled Data pages -from block 896 also matched. These are measurements taken before any -erase trial. +from block 896 also matched. Two native reads gave identical SHA-256 +hashes for 241 font files on System and 13 package manifests on Data. +The file hashes stay on the measurement host for comparison after the +erase trial and cold boot. ## Repeating the checks diff --git a/tests/test_p105_ios_counters.py b/tests/test_p105_ios_counters.py index ac1793ea..c0c19258 100644 --- a/tests/test_p105_ios_counters.py +++ b/tests/test_p105_ios_counters.py @@ -4,6 +4,7 @@ from pathlib import Path import plistlib import struct +import sys import tempfile import unittest from types import SimpleNamespace @@ -20,26 +21,55 @@ def load(name): ios = load("capture_ios_counters") +with patch.dict(sys.modules, {"capture_ios_counters": ios}): + compare = load("compare_ios_intervals") def envelope(selector, raw=b"", flags=0): return struct.pack("<4I", selector, flags, len(raw), 0) + raw -def nandinfo(): +def nandinfo(vfl_value=None, table_value=0, extra=b"odd"): raw = {tag: bytes([tag]) * width for tag, (_, width) in ios.DIRECT.items()} + if vfl_value is not None: + raw[255] = struct.pack(" len(table) or struct.unpack_from(" MAX_PLIST: + raise ValueError("oversized NANDInfo sample") + return data + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + for name in ("before_a", "after_a", "before_b", "after_b"): + parser.add_argument(name, type=Path) + args = parser.parse_args() + try: + same = same_changes(*(read_sample(path) for path in + (args.before_a, args.after_a, args.before_b, args.after_b))) + except (OSError, ValueError) as exc: + parser.exit(2, f"Could not compare iOS samples: {exc}\n") + if same: + print("The same counter increments reproduce both complete ending replies.") + return 0 + print("The full replies contain different increments or changes outside the counters.") + return 1 + + +if __name__ == "__main__": + sys.exit(main()) From bdc1ade6f06476285e1b745d3d45ffc226862c74 Mon Sep 17 00:00:00 2001 From: Shayne Legault <8741557+shaynelegault@users.noreply.github.com> Date: Thu, 19 Nov 2026 17:17:03 -0600 Subject: [PATCH 10/11] p105: verify flash command launches across both Linux buses Check the kernel identity and complete request sequence before exporting the ordered controller timeline. Record the buffer requirement measured with the archived startup workload and the remaining firmware handoff gap. Assisted-by: LLM --- .../p105-sftl-device-qualification.md | 8 + tests/test_p105_controller_trace.py | 203 ++++++++++++ tools/p105/README.md | 49 +++ tools/p105/check_flash_trace.py | 296 ++++++++++++++++++ 4 files changed, 556 insertions(+) create mode 100644 tests/test_p105_controller_trace.py create mode 100644 tools/p105/check_flash_trace.py diff --git a/docs/bring-up/p105-sftl-device-qualification.md b/docs/bring-up/p105-sftl-device-qualification.md index 926e85e9..892ab0b5 100644 --- a/docs/bring-up/p105-sftl-device-qualification.md +++ b/docs/bring-up/p105-sftl-device-qualification.md @@ -83,6 +83,14 @@ controlled read. Between the last iOS sample and Linux's first request, an ordered firmware trace or a verified interval of flash inactivity is still needed. +A host test ran the Linux driver against two simulated flash controllers. +Bus 0 prepared a read first and bus 1 started its read first. The earlier +recorder reported preparation order. The revised recorder agreed with an +independent observer for all 20 starts and four status replies. The host +checker also rejected each of 84 single-event deletions from a sample that +included a timeout. Physical testing of the recorder is pending. +[Linux capture procedure](../../tools/p105/#capture-linux-flash-commands-from-startup) + After returning to iOS, both copies of the LwVM partition header passed their CRC and matched the Linux measurements. All 29 sampled Data pages from block 896 also matched. Two native reads gave identical SHA-256 diff --git a/tests/test_p105_controller_trace.py b/tests/test_p105_controller_trace.py new file mode 100644 index 00000000..e2e2b2aa --- /dev/null +++ b/tests/test_p105_controller_trace.py @@ -0,0 +1,203 @@ +# SPDX-License-Identifier: GPL-2.0-only +import importlib.util +from hashlib import sha256 +import io +import json +from pathlib import Path +import tempfile +import unittest +import uuid + +path = Path(__file__).resolve().parents[1] / "tools/p105/check_flash_trace.py" +spec = importlib.util.spec_from_file_location("check_flash_trace", path) +linux = importlib.util.module_from_spec(spec) +spec.loader.exec_module(linux) + + +def append_event(rows, req, bus, address, kind, op, value): + seq = len(rows) + 1 + rows.append(f"task [000] 1.00: apple_h2fmi_transaction: dev=fmi seq={seq} req={req} " + f"bus={bus} kind={kind} op={op:04x} row={address:08x} value={value}") + + +def request(rows, bus, op, actions, address=0, result=0): + req = len(rows) + 1 + for kind, code, val in [(0, op, 0), *actions, (3, op, result)]: + append_event(rows, req, bus, address, kind, code, val) + + +def read_actions(op, status, data=True): + part = [(1, op, 0), (1, linux.OP_STATUS, 0), (2, linux.OP_STATUS, status)] + if data: + part += [(1, linux.TRANSFER, 0)] + return [*part, (1, linux.FINISH, 0)] + + +def observed_launches(actions): + rows = [] + for kind, code, value in actions: + rows.append((kind, code, value)) + if kind == 1: + rows.extend((4, code, control) for control in linux.LAUNCH_CONTROLS[code]) + return rows + + +def trace_fixture(launches=False): + rows = [] + discover = read_actions(linux.GEOMETRY, 0x40) + for bus in (0, 1): + request(rows, bus, linux.GEOMETRY, observed_launches(discover) if launches else discover) + return rows + + +class ControllerTrace(unittest.TestCase): + def test_older_capture_lacks_cross_bus_launch_order(self): + rows = trace_fixture() + report = linux.verify(rows, "fmi", f"1 {len(rows)}") + self.assertFalse(report["linux_launch_order_observed"]) + with self.assertRaisesRegex(ValueError, "no controller launch"): + linux.verify(rows, "fmi", f"1 {len(rows)}", require_launches=True) + + def test_observed_launches_for_discovery_read_program_and_erase(self): + rows = trace_fixture(launches=True) + request(rows, 1, linux.READ, observed_launches(read_actions(linux.READ, 0x61, False)), 117) + program = [(1, linux.PROGRAM, 0), (1, 0x17, 0), (1, linux.MUT_STATUS, 0), + (2, linux.MUT_STATUS, 0x40)] + request(rows, 0, linux.PROGRAM, observed_launches(program), 118) + erase = [(1, linux.ERASE, 0), (1, linux.MUT_STATUS, 0), (2, linux.MUT_STATUS, 0x40), + (1, linux.OP_STATUS, 0), (2, linux.OP_STATUS, 0x40), (1, linux.FINISH, 0)] + request(rows, 1, linux.ERASE, observed_launches(erase), 119) + timeline = io.StringIO() + report = linux.verify(reversed(rows), "fmi", f"1 {len(rows)}", timeline=timeline) + self.assertEqual(report["controller_launches"], 24) + self.assertTrue(report["linux_launch_order_observed"]) + self.assertFalse(report["continuous_ios_to_linux_handoff"]) + events = [json.loads(line) for line in timeline.getvalue().splitlines()] + self.assertEqual([event["sequence"] for event in events], sorted(event["sequence"] for event in events)) + self.assertEqual([e["value"] for e in events if e["event"] == "reply"], + [0x40, 0x40, 0x61, 0x40, 0x40, 0x40]) + + def test_rejects_missing_repeated_and_wrong_launch_controls_even_with_contiguous_numbers(self): + rows = [] + good = observed_launches(read_actions(linux.GEOMETRY, 0x40)) + request(rows, 0, linux.GEOMETRY, good) + poll = (4, linux.OP_STATUS, 0x50) + issue = good.index((4, linux.GEOMETRY, 0xb)) + poll_index = good.index(poll) + cases = { + "missing issue": good[:issue] + good[issue + 1:], + "missing poll": good[:poll_index] + good[poll_index + 1:], + "duplicate poll": good[:poll_index] + [poll] + good[poll_index:], + "wrong control": good[:issue] + [(4, linux.GEOMETRY, 9)] + good[issue + 1:], + } + for name, bad in cases.items(): + invalid = rows.copy() + request(invalid, 1, linux.GEOMETRY, bad) + timeline = io.StringIO() + with self.subTest(name=name), self.assertRaises(ValueError): + linux.verify(invalid, "fmi", f"1 {len(invalid)}", timeline=timeline) + self.assertEqual(timeline.getvalue(), "") + + def test_overlapping_buses_can_launch_in_reverse_preparation_order(self): + rows = trace_fixture(launches=True) + req0 = len(rows) + 1 + read = observed_launches(read_actions(linux.READ, 0x40)) + append_event(rows, req0, 0, 32, linux.BEGIN, linux.READ, 0) + append_event(rows, req0, 0, 32, *read[0]) + request(rows, 1, linux.READ, read, address=33) + for event in read[1:]: + append_event(rows, req0, 0, 32, *event) + append_event(rows, req0, 0, 32, linux.END_REQUEST, linux.READ, 0) + output = io.StringIO() + linux.verify(reversed(rows), "fmi", f"1 {len(rows)}", timeline=output) + events = [json.loads(line) for line in output.getvalue().splitlines()] + + def read_buses(event): + return [item["bus"] for item in events if item["event"] == event and item["opcode"] == linux.READ] + + self.assertEqual(read_buses("request_start"), [0, 1]) + self.assertEqual(read_buses("launch"), [1, 0]) + self.assertEqual(read_buses("request_end"), [1, 0]) + + def test_geometry_status_only_program_and_erase_with_two_replies(self): + rows = trace_fixture() + request(rows, 1, linux.READ, read_actions(linux.READ, 0x61, False), 110) + request(rows, 0, linux.PROGRAM, [(1, linux.PROGRAM, 0), (1, 0x17, 0), + (1, linux.MUT_STATUS, 0), (2, linux.MUT_STATUS, 0x40)], 111) + request(rows, 0, linux.ERASE, [(1, linux.ERASE, 0), (1, linux.MUT_STATUS, 0), (2, linux.MUT_STATUS, 0x40), + (1, linux.OP_STATUS, 0), (2, linux.OP_STATUS, 0x40), (1, linux.FINISH, 0)], 112) + receipt = linux.verify(reversed(rows), "fmi", f"1 {len(rows)}") + self.assertEqual(receipt["completed_requests"], {"geometry": 2, "read": 1, "program": 1, "erase": 1}) + self.assertFalse(receipt["continuous_ios_to_linux_handoff"]) + + def test_timeout_is_distinct_from_a_status_byte(self): + rows = trace_fixture(launches=True) + actions = [(1, linux.READ, 0), (1, linux.OP_STATUS, 0), (2, linux.OP_STATUS, -110), + (1, linux.FINISH, 0)] + request(rows, 1, linux.READ, observed_launches(actions), result=-110) + timeline = io.StringIO() + report = linux.verify(rows, "fmi", f"1 {len(rows)}", timeline=timeline) + self.assertEqual(report["error_returns"], {"-110": 1}) + self.assertEqual(report["poll_errors"], {"-110": 1}) + self.assertNotIn("-110", report["status_replies"]) + events = [json.loads(line) for line in timeline.getvalue().splitlines()] + self.assertEqual([e["event"] for e in events if e["value"] == -110], ["poll_error", "request_end"]) + + def test_failure_does_not_explain_wrong_commands_or_a_missing_poll_result(self): + rows = trace_fixture(launches=True) + for actions in ([(1, linux.ERASE, 0), (1, linux.FINISH, 0)], + [(1, linux.READ, 0), (1, linux.OP_STATUS, 0), (1, linux.FINISH, 0)]): + bad = rows.copy() + request(bad, 1, linux.READ, observed_launches(actions), result=-110) + with self.subTest(actions=actions), self.assertRaises(ValueError): + linux.verify(bad, "fmi", f"1 {len(bad)}", require_launches=True) + + def test_rejects_drops_at_the_beginning_middle_and_end(self): + rows = trace_fixture() + for i in (0, 7, len(rows) - 1): + with self.subTest(i=i), self.assertRaises(ValueError): + linux.verify(rows[:i] + rows[i + 1:], "fmi", f"1 {len(rows)}") + + def test_rejects_rebind_open_request_wrong_status_or_duplicate(self): + rows = trace_fixture() + wrong_status = [line.replace("kind=2 op=7d77", "kind=2 op=0079") for line in rows] + for events, cut in ((rows, f"2 {len(rows)}"), (rows, f"1 {len(rows) - 1}"), + (rows + [rows[0]], f"1 {len(rows)}"), (wrong_status, f"1 {len(rows)}")): + with self.subTest(cut=cut), self.assertRaises(ValueError): + linux.verify(events, "fmi", cut) + + def test_an_operation_after_a_complete_cut_is_excluded(self): + rows = trace_fixture() + end = len(rows) + request(rows, 1, linux.READ, read_actions(linux.READ, 0x49), 17) + self.assertEqual(linux.verify(rows, "fmi", f"1 {end}")["completed_requests"], {"geometry": 2}) + + def test_one_boot_transfer_checks_identity_and_detects_an_incomplete_stream(self): + values = trace_fixture(launches=True) + boot = str(uuid.uuid4()) + notes = b"test kernel" + expected = sha256(notes).hexdigest() + with tempfile.TemporaryDirectory() as tmp: + root = Path(tmp) + paths = {"sys/kernel/notes": notes, "sys/firmware/devicetree/base/compatible": b"apple,p105ap\0", + "proc/sys/kernel/random/boot_id": boot.encode(), + "sys/kernel/tracing/events/apple_h2fmi/apple_h2fmi_transaction/enable": b"1\n", + "sys/bus/platform/drivers/apple-h2fmi/fmi/flash_trace_sequence": f"1 {len(values)}".encode(), + "sys/kernel/tracing/per_cpu/cpu0/stats": b"overrun: 0\n", + "sys/kernel/tracing/trace": ("\n".join(values) + "\n").encode()} + for name, data in paths.items(): + path = root / name + path.parent.mkdir(parents=True, exist_ok=True) + path.write_bytes(data) + output = io.StringIO() + linux.stream_capture(expected, output, root / "sys", root / "proc") + saved = output.getvalue() + report = linux.verify_capture(io.StringIO(saved), expected) + self.assertEqual(report["boot_id"], boot) + for invalid in (saved.split(linux.END)[0], saved + "extra", saved.replace(boot, str(uuid.uuid4()), 1)): + timeline = io.StringIO() + with self.assertRaises(ValueError): + linux.verify_capture(io.StringIO(invalid), expected, timeline=timeline) + self.assertEqual(timeline.getvalue(), "") + with self.assertRaises(ValueError): + linux.stream_capture("a" * 64, io.StringIO(), root / "sys", root / "proc") diff --git a/tools/p105/README.md b/tools/p105/README.md index ee9fba6a..4093932e 100644 --- a/tools/p105/README.md +++ b/tools/p105/README.md @@ -92,3 +92,52 @@ The comparator checks every byte of each ending response. A match means the same 64-bit counter increments explain both intervals, including any wraparound. On the measured iPad two pairs of opposite read orders matched. [64 GB P105 measurements](../../docs/bring-up/p105-sftl-device-qualification.md) + +## Capture Linux flash commands from startup + +Use a P105 diagnostic kernel built with the +[H2FMI launch recorder](https://github.com/shaynelegault/linux/commit/a82a79427c0de5ec4dde0a9cdf8200fbc03a97ab). +The recorder tracks the register write that starts each operation on both +flash buses and the status each poll returns. A timeout has its own event +in the exported timeline. This kernel still needs a physical P105 run. + +Enable the event before the controller probes. One example Linux command +line reserves 16 MiB per CPU for the trace. + +```text +trace_event=apple_h2fmi:apple_h2fmi_transaction trace_buf_size=16M +``` + +On a verified RAM session with Python 3 and tracefs at `/sys/kernel/tracing`, +put `check_flash_trace.py` in `/run`. On the host, set `KERNEL_DIR` to the +verified diagnostic package. With the internal volumes idle, use a fresh +private directory to save the capture. The device streams the trace over +the session's SSH connection. + +```sh +umask 077 +expected=$(cat "$KERNEL_DIR/kernel.notes.sha256") || exit 1 +bin/orchard exec -- timeout -k 5 120 python3 /run/check_flash_trace.py capture \ + --kernel-notes "$expected" > linux.trace.partial && + python3 tools/p105/check_flash_trace.py verify --require-launches \ + --kernel-notes "$expected" linux.trace.partial && + mv linux.trace.partial linux.trace +``` + +The host checks the boot identity and requires every event from the first +geometry request through a point where both buses had finished their work. +A gap, a controller rebind or a partial transfer invalidates the capture. +For a timeline of requests and results in sequence order, run the following +on the host. The final JSON line holds the counts for the verified interval. + +```sh +python3 tools/p105/check_flash_trace.py verify --timeline \ + --kernel-notes "$expected" linux.trace > linux-timeline.jsonl +``` + +A host replay of 11,193 reads from an archived P105 scan produced 134,340 +events including geometry discovery. Their numeric fields alone occupied +3,761,520 bytes, exceeding the two CPUs' combined default trace capacity +of 2,883,584 bytes. One more read lacked an archived response and was +excluded. Check the sequence on each real boot even with the larger buffer. +Firmware activity before the first Linux request needs a separate witness. diff --git a/tools/p105/check_flash_trace.py b/tools/p105/check_flash_trace.py new file mode 100644 index 00000000..0a0d5943 --- /dev/null +++ b/tools/p105/check_flash_trace.py @@ -0,0 +1,296 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-only +"""Verify a P105 H2FMI trace from the first geometry request to a measured cut.""" +import argparse +from collections import Counter +from dataclasses import dataclass, field +from hashlib import sha256 +import json +from pathlib import Path +import re +import shutil +import sys +import uuid + + +EVENT = "apple_h2fmi_transaction:" +FIELDS = re.compile(r"dev=(\S+) seq=(\d+) req=(\d+) bus=(\d+) kind=(\d+) " + r"op=([0-9a-f]{4}) row=([0-9a-f]{8}) value=(-?\d+)") +GEOMETRY, READ, PROGRAM, ERASE = 0x9792, 0x370a, 0x008a, 0xd76a +OP_STATUS, MUT_STATUS, TRANSFER, FINISH = 0x7d77, 0x79, 0x7a, 0x77 +OPERATIONS = {GEOMETRY: "geometry", READ: "read", PROGRAM: "program", ERASE: "erase"} +BEGIN, STAGE, REPLY, END_REQUEST, LAUNCH = range(5) +TIMELINE_EVENTS = {BEGIN: "request_start", END_REQUEST: "request_end", LAUNCH: "launch", REPLY: "reply"} +# Nonzero FMC_RW_CTRL writes for each staged opcode. A status command +# first runs the command, then starts the FMC's automatic status poll. +LAUNCH_CONTROLS = { + GEOMETRY: (0xb,), READ: (0xb,), ERASE: (0xb,), PROGRAM: (9,), + OP_STATUS: (3, 0x50), MUT_STATUS: (1, 0x50), + TRANSFER: (1,), FINISH: (1,), 0x17: (1,), 0x70: (1,), +} +START = "# orchard-p105-start " +END = "# orchard-p105-end " + + +@dataclass +class Request: + start: int + operation: int + row: int + commands: list = field(default_factory=list) + replies: dict = field(default_factory=dict) + launches: list = field(default_factory=list) + + +def kernel_identity(sysfs, procfs, expected): + digest = sha256((sysfs / "kernel/notes").read_bytes()).hexdigest() + if digest != expected: + raise ValueError("running kernel notes differ from the selected diagnostic build") + compatible = (sysfs / "firmware/devicetree/base/compatible").read_bytes().split(b"\0") + if b"apple,p105ap" not in compatible: + raise ValueError("unexpected board") + boot = uuid.UUID((procfs / "sys/kernel/random/boot_id").read_text().strip()) + return {"kernel_notes_sha256": digest, "boot_id": str(boot)} + + +def stream_capture(expected, output, sysfs=Path("/sys"), procfs=Path("/proc")): + identity = kernel_identity(sysfs, procfs, expected) + tracing = sysfs / "kernel/tracing" + enabled = tracing / "events/apple_h2fmi/apple_h2fmi_transaction/enable" + if enabled.read_text().strip() != "1": + raise ValueError("controller tracepoint is disabled") + boundaries = list((sysfs / "bus/platform/drivers/apple-h2fmi").glob("*/flash_trace_sequence")) + if len(boundaries) != 1: + raise ValueError("expected one bound H2FMI device") + boundary = boundaries[0] + device = boundary.parent.name + cut = boundary.read_text().strip() + if not re.fullmatch(r"1 [1-9][0-9]*", cut): + raise ValueError("controller capture did not start at its first probe") + output.write(START + json.dumps({**identity, "device": device, "boundary": cut}) + "\n") + with (tracing / "trace").open() as stream: + shutil.copyfileobj(stream, output) + later = boundary.read_text().split() + if (len(later) != 2 or later[0] != "1" or int(later[1]) < int(cut.split()[1]) + or kernel_identity(sysfs, procfs, expected) != identity): + raise ValueError("boot or controller changed during transfer") + stats = {p.parent.name: p.read_text() for p in sorted((tracing / "per_cpu").glob("cpu*/stats"))} + output.write(END + json.dumps({**identity, "device": device, "later_sequence": int(later[1]), + "tracefs_cpu_stats": stats}) + "\n") + + +def verify_capture(stream, expected, *, require_launches=False, timeline=None): + first = stream.readline() + if not first.startswith(START): + raise ValueError("missing session header") + meta = json.loads(first[len(START):]) + if meta["kernel_notes_sha256"] != expected or str(uuid.UUID(meta["boot_id"])) != meta["boot_id"]: + raise ValueError("unexpected kernel or boot identity") + + def body(): + for line in stream: + if line.startswith(END): + tail = json.loads(line[len(END):]) + same = all(meta[k] == tail[k] for k in ("kernel_notes_sha256", "boot_id", "device")) + if not same or tail["later_sequence"] < int(meta["boundary"].split()[1]) or stream.read(1): + raise ValueError("session changed or extra data follows the receipt") + return + if line.startswith(START): + raise ValueError("second session header") + yield line + raise ValueError("missing session footer") + + result = verify(body(), meta["device"], meta["boundary"], + require_launches=require_launches, timeline=timeline) + result.update({k: meta[k] for k in ("kernel_notes_sha256", "boot_id", "device")}) + return result + + +def expected_sequence(op, replies): + if op in (GEOMETRY, READ): + if len(replies) != 1 or replies[0] < 0: + raise ValueError("successful read missing its status byte") + transfers = (0x40,) if op == GEOMETRY else (0x40, 0x42, 0x49) + data = [TRANSFER] if replies[0] in transfers else [] + return [op, OP_STATUS, *data, FINISH] + if op == PROGRAM: + if len(replies) != 1 or replies[0] < 0 or not replies[0] & 0x40 or replies[0] & 0x11: + raise ValueError("successful program missing its ready status") + return [PROGRAM, 0x17, MUT_STATUS] + if replies != [0x40, 0x40]: + raise ValueError("successful erase missing its queue or completion reply") + return [ERASE, MUT_STATUS, OP_STATUS, FINISH] + + +def check_completion(request, result, has_launches): + commands = request.commands + values = list(request.replies.values()) + op = request.operation + if has_launches: + for index, (code, count) in enumerate(zip(commands, request.launches)): + if count < 1: + raise ValueError("staged opcode missing its primary launch") + if code in (MUT_STATUS, OP_STATUS) and (count == 2) != (index in request.replies): + raise ValueError("status poll and result disagree") + if result == 0: + if commands != expected_sequence(op, values): + raise ValueError("successful request command sequence differs from the driver") + if op == GEOMETRY and values != [0x40]: + raise ValueError("invalid discovery response") + return + if op == GEOMETRY: + raise ValueError("failed package discovery") + if not commands: + if result != -5: + raise ValueError("unexpected result before the first command") + return + if op == READ: + # Reads always issue FINISH after asserting chip enable, including on timeout. + if commands[-1] != FINISH: + raise ValueError("failed read missing its cleanup command") + prefix = commands[:-1] + path = [READ, OP_STATUS, TRANSFER] + if TRANSFER in prefix and values not in ([0x40], [0x42], [0x49]): + raise ValueError("data transfer without a matching read status") + else: + prefix = commands + path = [PROGRAM, 0x17, MUT_STATUS] if op == PROGRAM else [ERASE, MUT_STATUS, OP_STATUS, FINISH] + if not prefix or prefix != path[:len(prefix)]: + raise ValueError("failed request command sequence differs from the driver") + for index, code in enumerate(prefix[:-1]): + if code in (OP_STATUS, MUT_STATUS) and request.replies.get(index, -1) < 0: + raise ValueError("operation continued past an unfinished status poll") + + +def verify(lines, device, boundary, *, require_launches=False, timeline=None): + fields = boundary.split() + if len(fields) != 2 or fields[0] != "1" or not fields[1].isascii() or not fields[1].isdigit(): + raise ValueError("need the sequence from the first driver probe") + limit = int(fields[1]) + if limit <= 0: + raise ValueError("empty controller trace") + rows = {} + for line in lines: + if EVENT not in line: + continue + match = FIELDS.fullmatch(line.split(EVENT, 1)[1].strip()) + if not match: + raise ValueError("malformed controller event") + dev, seq, req, bus, kind, op, row, val = match.groups() + if dev != device: + continue + seq = int(seq) + if seq == 0 or seq in rows: + raise ValueError("invalid or repeated controller sequence") + if seq <= limit: + rows[seq] = (int(req), int(bus), int(kind), int(op, 16), int(row, 16), int(val)) + if len(rows) != limit: + raise ValueError(f"trace contains {len(rows)} of {limit} required events") + has_launches = any(kind == LAUNCH for _, _, kind, _, _, _ in rows.values()) + if (require_launches or timeline is not None) and not has_launches: + raise ValueError("this capture has no controller launch events") + active = {} + counts, errors, status, poll_errors = Counter(), Counter(), Counter(), Counter() + opened = launch_count = 0 + for seq in range(1, limit + 1): + req, bus, kind, op, row, val = rows[seq] + if bus not in (0, 1): + raise ValueError("unexpected bus") + if kind == BEGIN: + if bus in active or req != seq or op not in OPERATIONS or val: + raise ValueError("invalid request start") + if opened < 2 and (bus != opened or op != GEOMETRY or row or active): + raise ValueError("trace must start with both package geometry requests") + opened += 1 + active[bus] = Request(req, op, row) + continue + request = active.get(bus) + if request is None or request.start != req or request.row != row: + raise ValueError("orphaned event or address change within a request") + commands, launches = request.commands, request.launches + if kind == STAGE: + if op not in LAUNCH_CONTROLS or val: + raise ValueError("unknown command-register event") + commands.append(op) + launches.append(0) + elif kind == LAUNCH: + if not commands or commands[-1] != op: + raise ValueError("launch without its staged opcode") + controls = LAUNCH_CONTROLS[op] + index = launches[-1] + if index >= len(controls) or controls[index] != val: + raise ValueError("unexpected or repeated controller launch") + launches[-1] += 1 + launch_count += 1 + elif kind == REPLY: + index = len(commands) - 1 + if (op not in (MUT_STATUS, OP_STATUS) or not commands or commands[-1] != op or + index in request.replies or not -4095 <= val <= 255): + raise ValueError("status without a matching command") + if has_launches and launches[-1] != len(LAUNCH_CONTROLS[op]): + raise ValueError("status arrived before the poll launch") + request.replies[index] = val + if val < 0: + poll_errors[str(val)] += 1 + else: + status[str(val)] += 1 + elif kind == END_REQUEST: + if op != request.operation or not -4095 <= val <= 0: + raise ValueError("invalid request completion") + check_completion(request, val, has_launches) + if val: + errors[str(val)] += 1 + counts[OPERATIONS[op]] += 1 + del active[bus] + else: + raise ValueError("unknown event kind") + if active or counts["geometry"] != 2: + raise ValueError("in-flight requests at the cut or incomplete discovery") + if timeline is not None: + for seq in range(1, limit + 1): + req, bus, kind, op, row, val = rows[seq] + if kind == STAGE: + continue + name = "poll_error" if kind == REPLY and val < 0 else TIMELINE_EVENTS[kind] + event = {"sequence": seq, "request": req, "bus": bus, "opcode": op, "row": row, + "event": name, "value": val} + timeline.write(json.dumps(event) + "\n") + return {"controller_events": limit, "completed_requests": dict(counts), "error_returns": dict(errors), + "status_replies": dict(status), "poll_errors": dict(poll_errors), + "linux_controller_prefix_complete": True, + "linux_launch_order_observed": has_launches, "controller_launches": launch_count, + "native_provider_grouping_verified": False, "continuous_ios_to_linux_handoff": False} + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + subs = parser.add_subparsers(dest="command", required=True) + take = subs.add_parser("capture", help="stream live P105 evidence to standard output") + check = subs.add_parser("verify", help="verify the saved session on the host") + check.add_argument("trace", type=Path) + check.add_argument("--require-launches", action="store_true", + help="require the recorder that observes both controllers' I/O launches") + check.add_argument("--timeline", action="store_true", + help="export ordered requests, launches and poll results as JSON Lines with a final summary") + for sub in (take, check): + sub.add_argument("--kernel-notes", required=True, help="expected SHA-256 of the diagnostic kernel notes") + args = parser.parse_args() + try: + if not re.fullmatch("[0-9a-f]{64}", args.kernel_notes): + raise ValueError("expected a lowercase SHA-256 for --kernel-notes") + if args.command == "capture": + stream_capture(args.kernel_notes, sys.stdout) + return + with args.trace.open() as stream: + report = verify_capture(stream, args.kernel_notes, require_launches=args.require_launches, + timeline=sys.stdout if args.timeline else None) + except (ValueError, OSError, TypeError, KeyError, IndexError) as exc: + parser.exit(1, f"Capture incomplete: {exc}\n") + if args.timeline: + print(json.dumps({"summary": report})) + else: + print(json.dumps(report, sort_keys=True, indent=2)) + + +if __name__ == "__main__": + sys.exit(main()) From 7549fbbf6260908d42b2519d52431cc895136769 Mon Sep 17 00:00:00 2001 From: Shayne Legault <8741557+shaynelegault@users.noreply.github.com> Date: Thu, 19 Nov 2026 17:27:12 -0600 Subject: [PATCH 11/11] p105: measure native disk request grouping over paired USB Bracket each flash sample with the twelve iOS block counters and verify the boot identity when this option is selected. Record the observed grouping for individual and batched reads on the 64 GB P105. Assisted-by: LLM --- .../p105-sftl-device-qualification.md | 20 +++++++++ tests/test_p105_ios_counters.py | 40 +++++++++++++++++ tools/p105/README.md | 16 +++++++ tools/p105/capture_ios_counters.py | 45 +++++++++++++++++++ 4 files changed, 121 insertions(+) diff --git a/docs/bring-up/p105-sftl-device-qualification.md b/docs/bring-up/p105-sftl-device-qualification.md index 892ab0b5..8dd4a9d7 100644 --- a/docs/bring-up/p105-sftl-device-qualification.md +++ b/docs/bring-up/p105-sftl-device-qualification.md @@ -78,6 +78,26 @@ order reproduced its ending reply byte for byte. This held in both directions for each pair and included the other 43 values in the statistics table. One interval with background programs produced a different result. +The paired diagnostics service also returned 12 totals from +`IOBlockStorageDriver`. A direct registry lookup found the native flash +driver `AppleSwissPPNFTL` and controller `AppleIOPFMI`. Their published +properties described configuration and power state. The following reads +started at the same 64 MiB offset on the native System volume. A sample +from each flash provider and independent disk counters bracketed every read +or batch of reads. + +| iOS System read | Disk read requests | Additional VFL pages | Change in lower-provider word 1 | +| --- | ---: | ---: | ---: | +| One 4 KiB read | 1 | 1 | 1 | +| One 64 KiB read | 1 | 16 | 4 | +| 1 MiB in 4 KiB reads | 256 | 256 | 256 | +| 1 MiB in 64 KiB reads | 16 | 256 | 64 | + +Both repetitions of each row gave the same counts. All four control +intervals read from `/dev/zero` and added zero disk I/O. The disk driver +recorded zero bytes written during all 12 intervals. These counts identify +how iOS grouped the controlled reads while its paired service was running. + The paired system log yielded no per-command flash records during the controlled read. Between the last iOS sample and Linux's first request, an ordered firmware trace or a verified interval of flash inactivity is still diff --git a/tests/test_p105_ios_counters.py b/tests/test_p105_ios_counters.py index c0c19258..16356f9f 100644 --- a/tests/test_p105_ios_counters.py +++ b/tests/test_p105_ios_counters.py @@ -7,6 +7,7 @@ import sys import tempfile import unittest +import uuid from types import SimpleNamespace from unittest.mock import patch @@ -46,6 +47,45 @@ def nandinfo(vfl_value=None, table_value=0, extra=b"odd"): class IOSCounters(unittest.TestCase): + def test_block_counts_bracket_the_flash_sample_and_pin_one_ios_boot(self): + session = str(uuid.uuid4()) + root = plistlib.dumps({"IORegistry": {"BootSessionUUID": session.upper()}}) + before = {key: 0 for key in ios.BLOCK_COUNTERS} + after = {**before, "Bytes (Read)": 65536, "Operations (Read)": 1} + + def block(values): + return plistlib.dumps({"IORegistry": {"IOClass": "IOBlockStorageDriver", "Statistics": values}}) + + for label, last_root in (("same", root), ("restarted", plistlib.dumps({ + "IORegistry": {"BootSessionUUID": str(uuid.uuid4())}}))): + with self.subTest(boot=label), tempfile.TemporaryDirectory() as tmp: + dest = Path(tmp) / "counts" + args = SimpleNamespace(samples=1, interval=0, output=dest, udid="test", + ideviceinfo="info", block_stats="diagnostics") + responses = [b"iPad2,5", b"P105AP", b"13G36", root, block(before), + plistlib.dumps(nandinfo()), block(after), last_root] + with patch.object(ios, "usb_query", side_effect=responses): + if label == "restarted": + with self.assertRaisesRegex(ValueError, "boot changed"): + ios.capture(args) + self.assertEqual(json.loads((dest / "run.json").read_text())["samples"], 0) + self.assertFalse((dest / "sample-00000.bin").exists()) + continue + ios.capture(args) + row = json.loads((dest / "sample-00000.json").read_text()) + self.assertEqual(row["block_before"]["counters"], before) + self.assertEqual(row["block_after"]["counters"], after) + self.assertEqual(row["ios_boot_id"], session) + self.assertLessEqual(row["block_before"]["end_monotonic_ns"], row["start_monotonic_ns"]) + self.assertLessEqual(row["end_monotonic_ns"], row["block_after"]["start_monotonic_ns"]) + + def test_rejects_unexpected_native_block_counters(self): + good = {key: 0 for key in ios.BLOCK_COUNTERS} + for counters in ({}, {**good, "Errors (Write)": -1}, {**good, "Operations (Read)": True}): + reply = {"IORegistry": {"IOClass": "IOBlockStorageDriver", "Statistics": counters}} + with patch.object(ios, "usb_query", return_value=plistlib.dumps(reply)), self.assertRaises(ValueError): + ios.block_stats("diagnostics", "test") + def test_same_increments_across_wrap_and_different_starting_values(self): self.assertTrue(compare.same_changes( nandinfo(2**64 - 2, 10), nandinfo(1, 11), diff --git a/tools/p105/README.md b/tools/p105/README.md index 4093932e..ff75fbd5 100644 --- a/tools/p105/README.md +++ b/tools/p105/README.md @@ -80,6 +80,22 @@ statistics table. It creates a private directory for the responses and records the time window for each query. A disconnect ends the run. Keep the raw responses on the measurement host. +To count requests at the iOS block layer during the same experiment, install +`idevicediagnostics` alongside `ideviceinfo`. With the option below, each +flash sample has a disk counter reading on either side and a checked iOS boot +ID. Each of the two disk readings has its own time window in the sample JSON. + +```sh +python3 tools/p105/capture_ios_counters.py --udid "$P105_UDID" \ + --block-stats idevicediagnostics --samples 4 ./p105-ios-block-counts +``` + +Bracket a controlled read with two samples. Compare the first sample's +`block_after` with the second sample's `block_before` to get the disk +request count for that interval. Compare native samples from the same boot. +The block layer reports totals for reads and writes. The paired service +closes when iOS shuts down. + To compare two controlled intervals, bracket each with a pair of saved samples from the same iPad. Record which reads completed in each interval and their order. Pass the four binary responses to the host comparator. diff --git a/tools/p105/capture_ios_counters.py b/tools/p105/capture_ios_counters.py index 47b1eea3..427f5d60 100644 --- a/tools/p105/capture_ios_counters.py +++ b/tools/p105/capture_ios_counters.py @@ -12,6 +12,7 @@ import subprocess import sys import time +import uuid # Record numbers, flags and sizes as returned by 13G36, little endian. @@ -22,6 +23,8 @@ DIRECT = {255: (0x02000200, 88), 254: (0x03000200, 464)} TABLE_LAYOUT = [(i, 2) for i in range(1, 44)] + [(255, 22), (254, 116)] MAX_PLIST = 1024 * 1024 +BLOCK_COUNTERS = {f"{field} ({direction})" for direction in ("Read", "Write") + for field in ("Bytes", "Errors", "Latency Time", "Operations", "Retries", "Total Time")} def inspect_nandinfo(blob): @@ -97,6 +100,36 @@ def usb_query(tool, udid, *args): return request.stdout +def registry_entry(tool, udid, name): + reply = plistlib.loads(usb_query(tool, udid, "ioregentry", name)) + entry = reply.get("IORegistry") if isinstance(reply, dict) else None + if not isinstance(entry, dict): + raise ValueError(f"missing iOS registry entry for {name}") + return entry + + +def boot_session(tool, udid): + value = registry_entry(tool, udid, "IOPMrootDomain").get("BootSessionUUID") + if not isinstance(value, str): + raise ValueError("missing iOS boot identity") + session = uuid.UUID(value) + if not session.int: + raise ValueError("empty iOS boot identity") + return str(session) + + +def block_stats(tool, udid): + start = time.monotonic_ns() + entry = registry_entry(tool, udid, "IOBlockStorageDriver") + end = time.monotonic_ns() + values = entry.get("Statistics") + if (entry.get("IOClass") != "IOBlockStorageDriver" or not isinstance(values, dict) + or values.keys() != BLOCK_COUNTERS + or any(type(value) is not int or not 0 <= value < 2**64 for value in values.values())): + raise ValueError("unexpected iOS block statistics") + return {"start_monotonic_ns": start, "end_monotonic_ns": end, "counters": values} + + def timestamp(): return datetime.now(timezone.utc).isoformat(timespec="microseconds") @@ -120,10 +153,13 @@ def capture(args): args.output.mkdir(mode=0o700) count = 0 error = None + diagnostics = getattr(args, "block_stats", None) try: + boot = boot_session(diagnostics, args.udid) if diagnostics else None for n in range(args.samples): if n: time.sleep(args.interval) + before = block_stats(diagnostics, args.udid) if diagnostics else None wall = timestamp() start = time.monotonic_ns() data = usb_query(args.ideviceinfo, args.udid, "-q", "com.apple.disk_usage", "-k", "NANDInfo", "-x") @@ -133,6 +169,12 @@ def capture(args): if not isinstance(raw, bytes): raise ValueError("NANDInfo is not a data property") pair = decode_nandinfo(raw) + block_fields = {} + if diagnostics: + after = block_stats(diagnostics, args.udid) + if boot_session(diagnostics, args.udid) != boot: + raise ValueError("iOS boot changed during the capture") + block_fields = {"ios_boot_id": boot, "block_before": before, "block_after": after} stem = f"sample-{n:05d}" save_private(args.output / (stem + ".bin"), raw) save_json(args.output / (stem + ".json"), { @@ -140,6 +182,7 @@ def capture(args): "end_monotonic_ns": end, "product": "iPad2,5", "build": "13G36", "nandinfo_sha256": sha256(raw).hexdigest(), "provider_sha256": {tag: sha256(buf).hexdigest() for tag, buf in pair.items()}, + **block_fields, }) count += 1 except (ValueError, OSError, KeyboardInterrupt, subprocess.TimeoutExpired, plistlib.InvalidFileException) as exc: @@ -158,6 +201,8 @@ def capture(args): def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--ideviceinfo", default="ideviceinfo") + parser.add_argument("--block-stats", metavar="IDEVICEDIAGNOSTICS", + help="bracket each flash sample with iOS disk counters using this executable") parser.add_argument("--udid", required=True, help="paired P105 identifier from idevice_id -l") parser.add_argument("--samples", type=int, default=1) parser.add_argument("--interval", type=float, default=0, help="seconds between completed queries")