From 50cafebc0ba285e133a35f4569875e4152eea827 Mon Sep 17 00:00:00 2001 From: Alexandre Figueiredo <17xande@gmail.com> Date: Tue, 1 Dec 2026 03:10:08 +0200 Subject: [PATCH 1/3] mtd: apple-sftl: accept older-epoch flag-1 records after a 16 GB checkpoint On a 16 GB iPad mini running iOS 9.3.5, journal collection failed one or two page reads after a checkpoint-covered record in the flag-1 stream in two of nine failed boots. This timing suggests that iOS GC continued the stream after the checkpoint with type-1/flag-1 records that retained the original user epoch. GC is an inference: the rejected tags have not been captured, and the hypothesis still needs device confirmation. A later successful capture did show older-epoch flag-1 data above the checkpoint, including in a reused context block. Boot f-1 later exposed an open-block torn-tail refusal caused by a type-2 flag-1 TRIM; that case is fixed separately. Suspected closed-summary refusals were never located. Allow that record shape after the checkpoint on the 16 GB geometry. The original epoch cannot exceed the sequence epoch and the key must be within the exported disk. Replay still requires a gapless sequence run, and the highest sequence for a key wins, as it already does for flag-0 relocations and for flag-1 runs in closed blocks, whose summary epochs the reader does not check. The user epoch only feeds per-block age statistics. The shared check also covers a context spare reused after the checkpoint; a first page there that mixes checkpoint-covered and newer records is still refused, as the checkpoint's role table would have named such a block as data. Type-2 rules are unchanged. A full open block ending in such records is now refused, where its last page was taken as torn before. Keep 64 GB unchanged: it is writable, so a checkpoint saved after a wrong acceptance would persist it, and no 64 GB medium has shown the shape. The 16 GB layout is read-only. Exercise moves after a native checkpoint, including a reused context spare and a block that straddles the checkpoint, and check that future epochs, hidden keys and sequence gaps remain rejected. Assisted-by: LLM Signed-off-by: Alexandre Figueiredo <17xande@gmail.com> --- drivers/mtd/apple-sftl-core.c | 12 ++- drivers/mtd/apple-sftl-test.c | 139 ++++++++++++++++++++++++++++++++-- 2 files changed, 144 insertions(+), 7 deletions(-) diff --git a/drivers/mtd/apple-sftl-core.c b/drivers/mtd/apple-sftl-core.c index 4a810908a1c057..03f4ecd5224b28 100644 --- a/drivers/mtd/apple-sftl-core.c +++ b/drivers/mtd/apple-sftl-core.c @@ -78,6 +78,11 @@ struct sftl_geometry { bool summary_sequences; /* a summary may consume one sequence per chunk */ bool degrade_remnants; /* a context block with an unknown base only blocks writes */ bool hidden_tail_type2; /* iOS 9 writes the hidden tail as type 2, flag 1 */ + bool older_epoch_subtype1; /* type 1, flag 1 after the checkpoint may keep an + * older user epoch. 16 GB only until confirmed on + * the device: it is read-only, while a 64 GB + * checkpoint would persist a wrong acceptance. + */ }; /* 64 GB Hynix PPN with 13G36 (iOS 9.3.5) on the media. */ @@ -105,6 +110,7 @@ static const struct sftl_geometry sftl_16gb = { .map_batch = 16, .disk_pages = 3906250, .lpages = 3906266, .summary_sequences = true, .degrade_remnants = true, .hidden_tail_type2 = true, + .older_epoch_subtype1 = true, }; static const struct sftl_geometry *const sftl_geometries[] = { @@ -1395,9 +1401,11 @@ static bool sftl_journal_meta(const struct apple_sftl *sftl, const u8 *meta) u64 sequence = sftl_sequence(meta); /* Native subtype-1 data covered by the checkpoint can retain an - * earlier user epoch. It occupies the page but is never replayed. + * earlier user epoch. On 16 GB media allow the same shape in replay: + * a move has a new sequence but can retain the original user epoch. */ - if (meta[0] == 1 && meta[1] == 1 && sequence <= sftl->checkpoint_sequence && + if (meta[0] == 1 && meta[1] == 1 && + (sequence <= sftl->checkpoint_sequence || sftl->geo->older_epoch_subtype1) && get_unaligned_le32(meta + 8) < sftl->geo->disk_pages) return get_unaligned_le32(meta + 12) <= sequence >> 16; diff --git a/drivers/mtd/apple-sftl-test.c b/drivers/mtd/apple-sftl-test.c index 95637f39e05292..a79685b18a4810 100644 --- a/drivers/mtd/apple-sftl-test.c +++ b/drivers/mtd/apple-sftl-test.c @@ -3464,9 +3464,15 @@ struct t16_fixture { bool remnant_reused; /* erased and rewritten with the journal's next records */ bool remnant_mixed; /* ... but its last record is an old one */ bool remnant_context; /* its second chunk is a context record newer than the checkpoint */ + bool remnant_moved; /* reused for flag-1 moves, whose user epoch is older */ u32 hidden_key; /* two pages of type 2, flag 1 records, then data, after the new block */ u32 hidden_epoch_skew; /* added to the hidden records' user epoch */ bool hidden_last; /* only one such page, every record with hidden_key */ + u32 moved_pre; /* flag-1 slots the checkpoint covers, before the moves */ + u32 moved_slots; /* type 1, flag 1 slots at the head of the next block */ + u32 moved_key; /* first moved key; its source is the old block */ + bool moved_future; /* the user epoch is one ahead of the sequence's */ + bool moved_last; /* omit ordinary records after the moved slots */ }; /* Fixed records before the ranges: the native base, statistics and tables. */ @@ -3674,13 +3680,18 @@ static void t16_slot(struct t16_fixture *f, u32 block, u32 slot, u8 *payload, u8 } return; } - if (block == T16_REMNANT && f->remnant && f->remnant_reused && slot < 4 && - !(f->remnant_mixed && slot == 3)) { - u64 sequence = T16_CONTEXT_BASE + t16_records(f) + t16_new_slots() + slot; + if (block == T16_REMNANT && f->remnant && f->remnant_reused && + slot < 4 && !(f->remnant_mixed && slot == 3)) { + u32 index = slot; + u64 sequence = T16_CONTEXT_BASE + t16_records(f) + t16_new_slots() + index; + /* A move keeps its source's user epoch; its higher sequence wins. + * The 'x' payload marker is test-only: a real move copies its source. + */ memset(payload, 'x', 4096); - put_unaligned_le32(T16_NEXT_LBA + slot, payload); - t16_meta(meta, 1, 0, sequence, T16_NEXT_LBA + slot, sequence >> 16); + put_unaligned_le32(T16_NEXT_LBA + index, payload); + t16_meta(meta, 1, f->remnant_moved, sequence, T16_NEXT_LBA + index, + f->remnant_moved ? 0 : sequence >> 16); return; } if (block == T16_REMNANT && f->remnant && f->remnant_context && slot == 1) { @@ -3715,6 +3726,36 @@ static void t16_slot(struct t16_fixture *f, u32 block, u32 slot, u8 *payload, u8 } return; } + if (block == T16_NEXT && f->moved_slots) { + u64 sequence = T16_CONTEXT_BASE + t16_records(f) + t16_new_slots() + + slot - f->moved_pre; + u32 key; + + if (slot < f->moved_pre) { + /* Flag-1 data the checkpoint already covers. */ + key = T16_NEXT_LBA + 8 + slot; + memset(payload, 's', 4096); + t16_meta(meta, 1, 1, T16_CONTEXT_BASE - f->moved_pre + slot, key, 0); + } else if (slot < f->moved_pre + f->moved_slots) { + slot -= f->moved_pre; + /* A move keeps its source's user epoch; its higher sequence wins. + * The 'g' marker is test-only: a real move copies its source. + */ + key = f->moved_key + slot; + memset(payload, 'g', 4096); + t16_meta(meta, 1, 1, sequence, key, + f->moved_future ? (sequence >> 16) + 1 : 0); + } else if (!f->moved_last && slot < f->moved_pre + f->moved_slots + 4) { + slot -= f->moved_pre + f->moved_slots; + key = T16_NEXT_LBA + slot; + memset(payload, 'x', 4096); + t16_meta(meta, 1, 0, sequence, key, sequence >> 16); + } else { + return; + } + put_unaligned_le32(key, payload); + return; + } if (block == T16_NEXT && f->next_block && slot < T16_SUMMARY) { u64 sequence = T16_CONTEXT_BASE + t16_records(f) + t16_new_slots() + slot; @@ -3916,6 +3957,91 @@ static void sftl_test_16gb_reused_context_block(struct kunit *test) KUNIT_EXPECT_PTR_EQ(test, sftl, NULL); } +static void sftl_test_16gb_moved_context_spare(struct kunit *test) +{ + struct t16_fixture *f = t16_fixture(test); + struct apple_sftl *sftl = NULL; + struct apple_sftl_info info; + u32 i; + + KUNIT_ASSERT_NOT_NULL(test, f); + f->remnant = true; + f->remnant_reused = true; + f->remnant_moved = true; + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); + apple_sftl_get_info(sftl, &info); + KUNIT_EXPECT_EQ(test, info.journal_sequence, t16_last(f)); + for (i = 0; i < 4; i++) + t16_expect_page(test, sftl, T16_NEXT_LBA + i, 'x', T16_NEXT_LBA + i); + apple_sftl_free(sftl); +} + +/* One open block holds flag-1 pages the checkpoint covers, then moves above + * it with older user epochs. The block loads and the moves are replayed. + */ +static void sftl_test_16gb_moved_straddle(struct kunit *test) +{ + struct t16_fixture *f = t16_fixture(test); + struct apple_sftl *sftl = NULL; + struct apple_sftl_info info; + u32 i; + + KUNIT_ASSERT_NOT_NULL(test, f); + f->moved_pre = 2; + f->moved_slots = 4; + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); + apple_sftl_get_info(sftl, &info); + KUNIT_EXPECT_EQ(test, info.journal_sequence, t16_last(f) + 4); + for (i = 0; i < 4; i++) { + t16_expect_page(test, sftl, i, 'g', i); + t16_expect_page(test, sftl, T16_NEXT_LBA + i, 'x', T16_NEXT_LBA + i); + } + apple_sftl_free(sftl); +} + +static void sftl_test_16gb_moved_after_checkpoint(struct kunit *test) +{ + struct t16_fixture *f = t16_fixture(test); + struct apple_sftl *sftl = NULL; + struct apple_sftl_info info; + u32 i; + + KUNIT_ASSERT_NOT_NULL(test, f); + f->moved_slots = 8; + 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, info.journal_sequence, t16_last(f) + 8); + /* The newer move wins over the summary even with an older user epoch. */ + for (i = 0; i < 8; i++) + t16_expect_page(test, sftl, i, 'g', i); + for (i = 0; i < 4; i++) + t16_expect_page(test, sftl, T16_NEXT_LBA + i, 'x', T16_NEXT_LBA + i); + apple_sftl_free(sftl); + + /* Valid moved records still cannot bridge missing sequences. */ + f->new_block = false; + KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &sftl), -EBADMSG); + f->new_block = true; + + /* Future epochs and old-epoch keys outside the disk are not moves. */ + f->moved_future = true; + KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &sftl), -EBADMSG); + f->moved_future = false; + f->moved_key = T16_DISK; + KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &sftl), -EBADMSG); + + /* A single bad page at the end is still torn; its data is not mapped. */ + f->moved_key = 0; + f->moved_future = true; + f->moved_slots = 4; + f->moved_last = true; + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); + t16_expect_page(test, sftl, 0, 'o', 0); + t16_expect_page(test, sftl, T16_NEW_LBA, 'n', T16_NEW_LBA); + apple_sftl_free(sftl); +} + /* iOS 9 writes the hidden tail, past the exported disk, as type 2 with * flag 1. Several such pages before data in an open block are not torn * pages: the hidden pages are mapped and the exported disk is unaffected. @@ -4093,6 +4219,9 @@ static struct kunit_case apple_sftl_cases[] = { KUNIT_CASE(sftl_test_16gb_native_summary), KUNIT_CASE(sftl_test_16gb_context_remnant), KUNIT_CASE(sftl_test_16gb_reused_context_block), + KUNIT_CASE(sftl_test_16gb_moved_context_spare), + KUNIT_CASE(sftl_test_16gb_moved_after_checkpoint), + KUNIT_CASE(sftl_test_16gb_moved_straddle), KUNIT_CASE(sftl_test_16gb_hidden_tail), {} }; From d33617ad1ce7d8dbf9fb060139baffa897ef485b Mon Sep 17 00:00:00 2001 From: Alexandre Figueiredo <17xande@gmail.com> Date: Wed, 2 Dec 2026 09:22:00 +0200 Subject: [PATCH 2/3] mtd: apple-sftl: accept iOS 9's type-2 flag-1 TRIM records on 16 GB media A 16 GB iPad mini (P105) running iOS 9.3.5 refused the flash with -EBADMSG at probe on boot f-1, with the older-epoch change applied: block=900 slot=1592 seq=555473968 checkpoint=555474063 tag=020130dc1b2100000100ffff1b210000 (treated as a torn tail) block=900 slot=1596 seq=555488956 checkpoint=555474063 tag=0100bc161c210000042f0500dd200000 (valid record after it) The first tag is type 2, flag 1, key 0xffff0001 (SFTL_TRIM), with a user epoch matching the sequence epoch. Two earlier boots logged the same tag 79 sequences below their checkpoints. The reader accepted only flag-0 TRIMs, so it mistook the page for a torn tail and refused the next valid page in the open block. Fix that open-block torn-tail cause by accepting the flag-1 tag with a matching epoch on the 16 GB geometry, which already accepts iOS 9's type-2 flag-1 hidden tail. Use the same journal validation for TRIMs referenced by closed-block summaries. A flag-1 TRIM in such a summary could plausibly explain a refusal, but the suspected closed-summary refusals were never located and no capture confirms this. Above the checkpoint, consume each accepted flag-1 TRIM sequence without applying its payload. The payload format is unverified and flag 1 may denote a relocated TRIM, which could discard newer data if applied. A missed TRIM only leaves stale data in LBAs the filesystem already treats as free. Leave flag-0 replay unchanged. Test the observed open-block shape, sequence consumption with mappings preserved in open and closed blocks, invalid tags and torn tails, and refusal of a flag-1 TRIM followed by valid data on 64 GB media. Assisted-by: LLM Signed-off-by: Alexandre Figueiredo <17xande@gmail.com> --- drivers/mtd/apple-sftl-core.c | 20 +++- drivers/mtd/apple-sftl-test.c | 203 +++++++++++++++++++++++++++++++++- 2 files changed, 217 insertions(+), 6 deletions(-) diff --git a/drivers/mtd/apple-sftl-core.c b/drivers/mtd/apple-sftl-core.c index 03f4ecd5224b28..3ef448ef9b66f6 100644 --- a/drivers/mtd/apple-sftl-core.c +++ b/drivers/mtd/apple-sftl-core.c @@ -1418,6 +1418,17 @@ static bool sftl_journal_meta(const struct apple_sftl *sftl, const u8 *meta) get_unaligned_le32(meta + 8) < sftl->geo->lpages) return sftl_user_epoch_valid(sftl, meta, sequence); + /* iOS 9 also writes a TRIM record as type 2 with flag 1 (a 16 GB P105 + * showed the tag three times, each 79 or 95 sequences below its + * checkpoint and followed by ordinary records in the same open block). + * The epoch must equal the sequence's, as for every type-2 record. + * If replay encounters one above the checkpoint, it consumes the + * sequence without applying the unverified payload. + */ + if (sftl->geo->hidden_tail_type2 && meta[0] == 2 && meta[1] == 1 && + get_unaligned_le32(meta + 8) == SFTL_TRIM) + return get_unaligned_le32(meta + 12) == sequence >> 16; + return sftl_erased(meta, SFTL_META) || ((meta[0] == 1 || meta[0] == 2) && sftl_user_epoch_valid(sftl, meta, sequence) && @@ -1565,7 +1576,8 @@ static int sftl_footer(struct apple_sftl *sftl, const u8 *head, u32 block, if (ret) goto out; memcpy(meta, sftl->oob + chunk * SFTL_META, sizeof(meta)); - if (meta[0] != 2 || meta[1] || sftl_sequence(meta) != sequence || + /* sftl_add_journal() checks the flag and epoch. */ + if (meta[0] != 2 || sftl_sequence(meta) != sequence || get_unaligned_le32(meta + 8) != SFTL_TRIM) { ret = -EBADMSG; goto out; @@ -1789,6 +1801,12 @@ static int sftl_journal(struct apple_sftl *sftl, const u8 *heads, struct sftl_re const u8 *payload; u32 count, offset; + /* Flag 1 may mark a relocated TRIM. Applying its + * unverified payload could discard newer data; a + * missed TRIM leaves data in filesystem-free LBAs. + */ + if (sftl_flags(record) == 1) + continue; ret = sftl_read_address(sftl, record->address, &chunk); if (ret) goto out; diff --git a/drivers/mtd/apple-sftl-test.c b/drivers/mtd/apple-sftl-test.c index a79685b18a4810..ed7859049aedf0 100644 --- a/drivers/mtd/apple-sftl-test.c +++ b/drivers/mtd/apple-sftl-test.c @@ -1448,6 +1448,39 @@ static void sftl_test_hidden_tail_is_torn_on_64gb(struct kunit *test) KUNIT_EXPECT_PTR_EQ(test, sftl, NULL); } +static void sftl_test_trim_flag1_is_torn_on_64gb(struct kunit *test) +{ + struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); + struct sftl_test_page *page = kunit_kzalloc(test, sizeof(*page), GFP_KERNEL); + struct apple_sftl *sftl = NULL; + unsigned int i; + + KUNIT_ASSERT_NOT_NULL(test, f); + KUNIT_ASSERT_NOT_NULL(test, page); + put_unaligned_le32(1, page->data); + put_unaligned_le32(2, page->data + 8); + for (i = 0; i < 4; i++) + sftl_test_meta(page->oob + i * APPLE_SFTL_META, 2, 0, + TEST_APPEND_FIRST + i, 0xffff0001); + KUNIT_ASSERT_EQ(test, sftl_test_program_page(f, 0, 0, 5, 0, page->data, page->oob), 0); + memset(page->data, 'n', sizeof(page->data)); + for (i = 0; i < 4; i++) + sftl_test_meta(page->oob + i * APPLE_SFTL_META, 1, 0, + TEST_APPEND_FIRST + 4 + i, TEST_APPEND_BASE + i); + KUNIT_ASSERT_EQ(test, sftl_test_program_page(f, 0, 0, 5, 1, page->data, page->oob), 0); + /* With flag 0 the same medium, including the later data page, loads. */ + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); + apple_sftl_free(sftl); + + page = sftl_test_written(f, 0, 0, 0); + KUNIT_ASSERT_NOT_NULL(test, page); + for (i = 0; i < 4; i++) + page->oob[i * APPLE_SFTL_META + 1] = 1; + KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &sftl), -EBADMSG); + KUNIT_EXPECT_PTR_EQ(test, sftl, NULL); + apple_sftl_free(sftl); +} + static void sftl_test_append_scans_entire_candidate(struct kunit *test) { struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); @@ -3473,6 +3506,14 @@ struct t16_fixture { u32 moved_key; /* first moved key; its source is the old block */ bool moved_future; /* the user epoch is one ahead of the sequence's */ bool moved_last; /* omit ordinary records after the moved slots */ + u32 trim_pre; /* type 2, flag 1 TRIM slots the checkpoint covers, at the next block's head */ + u32 trim_slots; /* the same after the checkpoint, targeting LBAs 0 and 1 */ + int trim_epoch_skew; /* added to the covered or closed TRIM's user epoch */ + u32 trim_key; /* key of the covered records when not SFTL_TRIM */ + u8 trim_flag; /* flags of the covered records when not 1 */ + bool trim_bad_count; /* the first TRIM above the checkpoint claims too many ranges */ + bool trim_last; /* omit ordinary records after the TRIM slots */ + bool closed_trim; /* one TRIM after the data run in the new closed block */ }; /* Fixed records before the ranges: the native base, statistics and tables. */ @@ -3648,6 +3689,7 @@ static void t16_slot(struct t16_fixture *f, u32 block, u32 slot, u8 *payload, u8 } if (block == T16_NEW && f->new_block) { u64 base = T16_CONTEXT_BASE + t16_records(f); + u32 pad_start = T16_NEW_RUN + f->closed_trim; if (slot >= t16_new_slots()) { u32 index = slot - t16_new_slots(); @@ -3658,15 +3700,23 @@ static void t16_slot(struct t16_fixture *f, u32 block, u32 slot, u8 *payload, u8 put_unaligned_le16(0xffff, meta + 14); memset(payload, 0, 4096); if (!index) { - /* One data run, then padding up to the summary. */ + /* One data run, an optional TRIM, then padding. */ put_unaligned_le32(0, payload); put_unaligned_le32(base >> 16, payload + 4); put_unaligned_le32(T16_NEW_LBA, payload + 8); put_unaligned_le32(T16_NEW_RUN, payload + 12); - put_unaligned_le32(T16_NEW_RUN, payload + 16); - put_unaligned_le32(base >> 16, payload + 20); - put_unaligned_le32(0xffff0000, payload + 24); - put_unaligned_le32(t16_new_slots() - T16_NEW_RUN, payload + 28); + payload += 16; + if (f->closed_trim) { + put_unaligned_le32(T16_NEW_RUN, payload); + put_unaligned_le32(base >> 16, payload + 4); + put_unaligned_le32(0xffff0001, payload + 8); + put_unaligned_le32(1, payload + 12); + payload += 16; + } + put_unaligned_le32(pad_start, payload); + put_unaligned_le32(base >> 16, payload + 4); + put_unaligned_le32(0xffff0000, payload + 8); + put_unaligned_le32(t16_new_slots() - pad_start, payload + 12); } return; } @@ -3674,6 +3724,12 @@ static void t16_slot(struct t16_fixture *f, u32 block, u32 slot, u8 *payload, u8 memset(payload, 'n', 4096); put_unaligned_le32(T16_NEW_LBA + slot, payload); t16_meta(meta, 1, 0, base + slot, T16_NEW_LBA + slot, (base + slot) >> 16); + } else if (slot < pad_start) { + memset(payload, 0, 4096); + put_unaligned_le32(1, payload); + put_unaligned_le32(2, payload + 8); + t16_meta(meta, 2, f->trim_flag ?: 1, base + slot, 0xffff0001, + ((base + slot) >> 16) + f->trim_epoch_skew); } else { memset(payload, 0, 4096); t16_meta(meta, 1, 0, base + slot, 0xffff0000, (base + slot) >> 16); @@ -3726,6 +3782,37 @@ static void t16_slot(struct t16_fixture *f, u32 block, u32 slot, u8 *payload, u8 } return; } + if (block == T16_NEXT && (f->trim_pre || f->trim_slots)) { + u64 base = T16_CONTEXT_BASE + t16_records(f) + t16_new_slots(); + u32 above = slot - f->trim_pre; + + /* iOS 9 writes a TRIM as type 2, flag 1: the key is the TRIM tag. */ + if (slot < f->trim_pre) { + u64 sequence = T16_CONTEXT_BASE - f->trim_pre + slot; + + memset(payload, 0, 4096); + t16_meta(meta, 2, f->trim_flag ?: 1, sequence, f->trim_key ?: 0xffff0001, + (sequence >> 16) + f->trim_epoch_skew); + } else if (above < f->trim_slots) { + u64 sequence = base + above; + + memset(payload, 0, 4096); + t16_meta(meta, 2, 1, sequence, 0xffff0001, sequence >> 16); + if (!above) { + put_unaligned_le32(f->trim_bad_count ? 4096 : 1, payload); + put_unaligned_le32(0, payload + 4); + put_unaligned_le32(2, payload + 8); + } + } else if (!f->trim_last && above < f->trim_slots + 4) { + u64 sequence = base + above; + + memset(payload, 'x', 4096); + put_unaligned_le32(T16_NEXT_LBA + above - f->trim_slots, payload); + t16_meta(meta, 1, 0, sequence, T16_NEXT_LBA + above - f->trim_slots, + sequence >> 16); + } + return; + } if (block == T16_NEXT && f->moved_slots) { u64 sequence = T16_CONTEXT_BASE + t16_records(f) + t16_new_slots() + slot - f->moved_pre; @@ -4042,6 +4129,109 @@ static void sftl_test_16gb_moved_after_checkpoint(struct kunit *test) apple_sftl_free(sftl); } +/* iOS 9 writes a TRIM as type 2, flag 1, and keeps writing after it in the + * same open block. Seen on a 16 GB P105: such a record 79 or 95 sequences + * below the checkpoint, then ordinary records above it. It is not a torn page. + */ +static void sftl_test_16gb_trim_flag1(struct kunit *test) +{ + struct t16_fixture *f = t16_fixture(test); + struct apple_sftl *sftl = NULL; + struct apple_sftl_info info; + u64 base; + u32 i; + + KUNIT_ASSERT_NOT_NULL(test, f); + base = T16_CONTEXT_BASE + t16_records(f) + t16_new_slots(); + + /* Covered TRIM page, then ordinary records: loads, nothing is unmapped. */ + f->trim_pre = 4; + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); + apple_sftl_get_info(sftl, &info); + KUNIT_EXPECT_EQ(test, info.journal_sequence, base + 4 - 1); + t16_expect_page(test, sftl, 0, 'o', 0); + for (i = 0; i < 4; i++) + t16_expect_page(test, sftl, T16_NEXT_LBA + i, 'x', T16_NEXT_LBA + i); + apple_sftl_free(sftl); + + /* Above the checkpoint the sequence is consumed, preserving both LBAs. */ + f->trim_pre = 0; + f->trim_slots = 4; + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); + apple_sftl_get_info(sftl, &info); + KUNIT_EXPECT_EQ(test, info.journal_sequence, base + 8 - 1); + t16_expect_page(test, sftl, 0, 'o', 0); + t16_expect_page(test, sftl, 1, 'o' + 1, 1); + t16_expect_page(test, sftl, 2, 'o' + 2, 2); + for (i = 0; i < 4; i++) + t16_expect_page(test, sftl, T16_NEXT_LBA + i, 'x', T16_NEXT_LBA + i); + apple_sftl_free(sftl); + + /* Another tag with a hidden key, or a TRIM with a bad epoch, is a torn + * page; valid pages after it make the medium inconsistent. + */ + f->trim_slots = 0; + f->trim_pre = 4; + f->trim_epoch_skew = 1; + KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &sftl), -EBADMSG); + f->trim_epoch_skew = -1; + KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &sftl), -EBADMSG); + f->trim_epoch_skew = 0; + f->trim_key = 0xffff0002; + KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &sftl), -EBADMSG); + f->trim_key = 0xffff0000; + KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &sftl), -EBADMSG); + f->trim_key = 0; + f->trim_flag = 2; + KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &sftl), -EBADMSG); + f->trim_flag = 0; + + /* The same page as the last one of the block is a torn tail. */ + f->trim_last = true; + f->trim_key = 0xffff0002; + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); + t16_expect_page(test, sftl, 0, 'o', 0); + apple_sftl_free(sftl); + f->trim_key = 0; + f->trim_epoch_skew = 1; + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); + apple_sftl_free(sftl); + + /* The payload is ignored, even if it cannot be a flag-0 payload. */ + f->trim_epoch_skew = 0; + f->trim_last = false; + f->trim_pre = 0; + f->trim_slots = 4; + f->trim_bad_count = true; + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); + t16_expect_page(test, sftl, 0, 'o', 0); + apple_sftl_free(sftl); +} + +static void sftl_test_16gb_closed_trim_flag1(struct kunit *test) +{ + struct t16_fixture *f = t16_fixture(test); + struct apple_sftl *sftl = NULL; + struct apple_sftl_info info; + + KUNIT_ASSERT_NOT_NULL(test, f); + f->closed_trim = true; + f->next_block = true; + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); + apple_sftl_get_info(sftl, &info); + KUNIT_EXPECT_EQ(test, info.journal_sequence, t16_last(f)); + t16_expect_page(test, sftl, 0, 'o', 0); + t16_expect_page(test, sftl, 1, 'o' + 1, 1); + t16_expect_page(test, sftl, T16_NEXT_LBA, 'x', T16_NEXT_LBA); + apple_sftl_free(sftl); + + f->trim_flag = 2; + KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &sftl), -EBADMSG); + f->trim_flag = 0; + f->trim_epoch_skew = 1; + KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &sftl), -EBADMSG); +} + /* iOS 9 writes the hidden tail, past the exported disk, as type 2 with * flag 1. Several such pages before data in an open block are not torn * pages: the hidden pages are mapped and the exported disk is unaffected. @@ -4191,6 +4381,7 @@ static struct kunit_case apple_sftl_cases[] = { KUNIT_CASE(sftl_test_checkpointed_open_copies), KUNIT_CASE(sftl_test_torn_page_not_last), KUNIT_CASE(sftl_test_hidden_tail_is_torn_on_64gb), + KUNIT_CASE(sftl_test_trim_flag1_is_torn_on_64gb), KUNIT_CASE(sftl_test_append_scans_entire_candidate), KUNIT_CASE(sftl_test_append_reserves_vfl_members), KUNIT_CASE(sftl_test_append_closes_at_sequence_limit), @@ -4222,6 +4413,8 @@ static struct kunit_case apple_sftl_cases[] = { KUNIT_CASE(sftl_test_16gb_moved_context_spare), KUNIT_CASE(sftl_test_16gb_moved_after_checkpoint), KUNIT_CASE(sftl_test_16gb_moved_straddle), + KUNIT_CASE(sftl_test_16gb_trim_flag1), + KUNIT_CASE(sftl_test_16gb_closed_trim_flag1), KUNIT_CASE(sftl_test_16gb_hidden_tail), {} }; From e3212f3c0abe8d98e5b316474ace3602979096d7 Mon Sep 17 00:00:00 2001 From: Alexandre Figueiredo <17xande@gmail.com> Date: Wed, 2 Dec 2026 09:32:42 +0200 Subject: [PATCH 3/3] mtd: apple-sftl: test refusal of a flag-1 TRIM in a 64 GB closed summary The 64 GB geometry accepts only flag-0 TRIMs. A closed block's summary names its TRIM records, and each is validated like an open block's. Cover that path with a summary whose last record is a TRIM: flag 0 loads, flag 1 is refused with -EBADMSG. Assisted-by: LLM Signed-off-by: Alexandre Figueiredo <17xande@gmail.com> --- drivers/mtd/apple-sftl-test.c | 38 +++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/drivers/mtd/apple-sftl-test.c b/drivers/mtd/apple-sftl-test.c index ed7859049aedf0..a266329fe5a417 100644 --- a/drivers/mtd/apple-sftl-test.c +++ b/drivers/mtd/apple-sftl-test.c @@ -42,6 +42,8 @@ enum sftl_fault { SFTL_UNKNOWN_STATUS, SFTL_TORN_FOOTER, SFTL_RUNTIME_FAILURE, + SFTL_CLOSED_TRIM_FLAG0, + SFTL_CLOSED_TRIM_FLAG1, }; enum sftl_test_commit { @@ -281,6 +283,19 @@ static void sftl_test_closed(struct sftl_fixture *f, unsigned int position, memset(data + i * 4096, (slot + 1) & 0xff, 4096); sftl_test_meta(oob + i * 16, 1, 0, TEST_CLOSED_FIRST + f->sequence_bias + slot, slot + 1); + if (slot == TEST_CLOSED_CHUNKS - 1 && + (f->fault == SFTL_CLOSED_TRIM_FLAG0 || + f->fault == SFTL_CLOSED_TRIM_FLAG1)) { + /* The last record is a TRIM of LBA 1 (the summary says so). */ + memset(data + i * 4096, 0, 4096); + put_unaligned_le32(1, data + i * 4096); + put_unaligned_le32(TEST_CLOSED_FIRST >> 16, data + i * 4096 + 4); + put_unaligned_le32(1, data + i * 4096 + 8); + sftl_test_meta(oob + i * 16, 2, + f->fault == SFTL_CLOSED_TRIM_FLAG1, + TEST_CLOSED_FIRST + f->sequence_bias + slot, + 0xffff0001); + } } } else { u32 span = TEST_CLOSED_CHUNKS; @@ -290,6 +305,13 @@ static void sftl_test_closed(struct sftl_fixture *f, unsigned int position, put_unaligned_le32((TEST_CLOSED_FIRST + f->sequence_bias) >> 16, data + 4); put_unaligned_le32(1, data + 8); put_unaligned_le32(TEST_CLOSED_CHUNKS, data + 12); + if (f->fault == SFTL_CLOSED_TRIM_FLAG0 || f->fault == SFTL_CLOSED_TRIM_FLAG1) { + put_unaligned_le32(TEST_CLOSED_CHUNKS - 1, data + 12); + put_unaligned_le32(TEST_CLOSED_CHUNKS - 1, data + 16); + put_unaligned_le32((TEST_CLOSED_FIRST + f->sequence_bias) >> 16, data + 20); + put_unaligned_le32(0xffff0001, data + 24); + put_unaligned_le32(1, data + 28); + } if (f->fault == SFTL_ALLOCATION_FLOOR) span += 8; sftl_test_meta(oob, 0x1c, 8, TEST_CLOSED_FIRST + f->sequence_bias, span); @@ -1481,6 +1503,21 @@ static void sftl_test_trim_flag1_is_torn_on_64gb(struct kunit *test) apple_sftl_free(sftl); } +static void sftl_test_closed_trim_flag1_is_torn_on_64gb(struct kunit *test) +{ + struct sftl_fixture *f = sftl_test_fixture(test, SFTL_CLOSED_TRIM_FLAG0); + struct apple_sftl *sftl = NULL; + + KUNIT_ASSERT_NOT_NULL(test, f); + /* A flag-0 TRIM in the closed block's summary loads. */ + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); + apple_sftl_free(sftl); + sftl = NULL; + f->fault = SFTL_CLOSED_TRIM_FLAG1; + KUNIT_EXPECT_EQ(test, apple_sftl_load(&f->provider, &sftl), -EBADMSG); + KUNIT_EXPECT_PTR_EQ(test, sftl, NULL); +} + static void sftl_test_append_scans_entire_candidate(struct kunit *test) { struct sftl_fixture *f = sftl_test_fixture(test, SFTL_NO_FAULT); @@ -4382,6 +4419,7 @@ static struct kunit_case apple_sftl_cases[] = { KUNIT_CASE(sftl_test_torn_page_not_last), KUNIT_CASE(sftl_test_hidden_tail_is_torn_on_64gb), KUNIT_CASE(sftl_test_trim_flag1_is_torn_on_64gb), + KUNIT_CASE(sftl_test_closed_trim_flag1_is_torn_on_64gb), KUNIT_CASE(sftl_test_append_scans_entire_candidate), KUNIT_CASE(sftl_test_append_reserves_vfl_members), KUNIT_CASE(sftl_test_append_closes_at_sequence_limit),