Section 17.12

Crash cuts for a two-slot update

Question

Sketch a crash-consistency §8.4 argument for separately written candidate data and a selector.

Short answer

A crash-consistency argument fixes the order of the writes and the recovery rule, then checks every point at which power can fail. The candidate is written, checked and made durable first; only then is a new selector record, the small record naming the current version, written to a second location. The proof assumes recovery can recognize a complete record, reject torn records, and order the two generations correctly. It then uses the newest complete record. A crash before that record is complete finds the old record and the untouched old version; a crash after it finds a complete candidate. Either way, one whole version starts.

Terms in the question

Full answer

Step 1: fix the sequence of writes. At the start, slot A holds the old version, and selector location 0 holds the record S0S_0 = (slot A, sequence number nn, check value). Selector location 1 holds no valid record. The check value is computed from the record’s other bytes, for example as a 32-bit cyclic redundancy check §4.32 (CRC-32), to help detect incomplete or damaged records. The proof below states explicitly which interrupted writes the format must detect. The update then performs these steps in this order.

  1. Erase slot B.

  2. Write into slot B, page by page, the candidate bytes and the signed description of the update, which contains the digest of the candidate, its fixed-length fingerprint computed with a cryptographic hash function §5.1.

  3. Read slot B back, compute the digest of the candidate bytes, and compare it with the digest in the signed description; stop if the two differ.

  4. Wait until every write of steps 1 and 2 is durable, meaning it has reached the storage medium and will survive a power cut.

  5. Erase selector location 1, then write the record S1S_1 = (slot B, sequence number n+1n + 1, check value) into it.

Step 2: fix the recovery rule. At every start, the bootloader §6.30, the small program that runs first after a reset, reads both selector locations. It ignores any record whose check value does not match, takes the valid record with the larger sequence number, and checks the content of the slot that this record names against the digest in that slot’s signed description. If the digests match, it starts that slot; if not, it uses the other valid record.

Step 3: state the invariant. An invariant is a statement that must be true at every instant. Here it has three parts.

(I1)

At least one selector location holds a valid record.

(I2)

The newest valid record, the one with the larger sequence number, names a slot whose content is complete and matches the digest in its signed description.

(I3)

That newest valid record names slot A before the commit and slot B after it.

If (I1) to (I3) hold when a crash happens, the recovery rule starts slot A or slot B, which is an allowed state. The argument therefore reduces to two claims: the invariant holds at the start, and no step, interrupted at any point, breaks it.

Step 4: examine every crash cut.

The commit therefore happens at one instant: when the last byte of S1S_1 becomes durable. Before that instant every recovery starts slot A; after it, every recovery starts slot B.

Step 5: name the assumptions. The argument holds only if all five of the following are true.

  1. Write ordering §8.20. No part of step 5 reaches the storage medium before steps 1 and 2 are complete. On the internal flash memory of a microcontroller §6.1 (a small computer on one chip), the driver usually waits until each write has finished before it starts the next. A storage device with a write cache needs an explicit flush command.

  2. A torn record is detected. The fault model must ensure recovery ignores any interrupted write or erase that leaves a different, spurious record. A 32-bit CRC detects many error patterns, but distinct byte strings can have the same CRC. A CRC alone cannot establish this assumption for arbitrary corruption. Some flash memory leaves bits whose programming was interrupted in a weak state that can read differently on later reads, so this assumption must be checked against the chip’s documentation.

  3. Separate erase units. Flash memory erases whole pages at a time. Slot A, slot B and each selector location occupy pages of their own, so erasing one never erases another.

  4. No overwrite in place. The new record goes to the location that does not hold the newest valid record. Overwriting S0S_0 in place would need an erase first, and a crash between that erase and the write would leave no valid record at all.

  5. Generation ordering. This example compares 32-bit unsigned sequence numbers as ordinary integers and stops updating when the newest number is 4,294,967,295. Otherwise the next number would wrap to 0, and recovery would incorrectly prefer 4,294,967,295. A design that continues across wraparound needs a different ordering rule with its own assumptions.

Putting all the steps inside one function of the source code does not make them one indivisible step. The guarantee comes only from the order of the writes, the behavior of the storage and the recovery rule.

Worked example

A device has flash memory with pages of 4,096 bytes (4 KiB). Slot A occupies pages 0 to 63 and slot B pages 64 to 127, so each slot holds 64×4=25664 \times 4 = 256 KiB. Selector location 0 is page 128, and location 1 is page 129. A selector record is 12 bytes: the slot number (4 bytes, 0 for A and 1 for B), the sequence number (4 bytes) and the CRC-32 of those first 8 bytes (4 bytes), in the variant of CRC-32 used by Ethernet and by the zlib compression library. Erased flash reads as bytes of value 0xFF, and the recovery rule treats a location in that state as empty. At the start, location 0 holds S0S_0 = (A, 41) with a correct check value, and location 1 is erased. The candidate with its signed description is 200,000 bytes long. Since 200,000/4,096≈48.8200{,}000 / 4{,}096 \approx 48.8, it fills 49 pages, pages 64 to 112.

  1. Power fails while page 94, the 31st page of the candidate, is being written. Location 0 holds the only valid record, S0S_0 = (A, 41). Recovery starts slot A, and the update starts again later.

  2. Power fails after all 49 pages are written and the digest has matched, but before location 1 is written. The outcome is the same: slot A starts.

  3. Power fails while S1S_1 = (B, 42) is being written. The slot and sequence fields were programmed, but the check-value field still reads 0xFFFFFFFF. The 8 programmed bytes are 01 00 00 00 2A 00 00 00: the numbers 1 and 42, each stored lowest byte first, where 0x2A is 42 in hexadecimal. Their CRC-32 is 0x660790AD, which differs from 0xFFFFFFFF, so recovery ignores the record and starts slot A.

  4. Power fails after S1S_1 is complete. Both records are valid, 42>4142 > 41, and recovery starts slot B.

  5. The next update writes a candidate into slot A, then erases location 0 in order to write S0′S_0' = (A, 43) there. Power fails during that erase. Location 0 may still hold the old record (A, 41), may be empty, or may be rejected as damaged. In all three cases S1S_1 = (B, 42) is still valid, and recovery starts slot B. Because the two locations alternate, the record being written is never the only valid one.

Two broken variants show what the assumptions protect.

Follow-up questions and limits

Does this argument cover the whole update? No, only the two slots and the selector. A real update often changes more persistent state §8.1: a trial flag and a start counter for a confirmation window §8.24, an anti-rollback §8.26 floor (the lowest version the device will accept), keys and replay counters. Each extra record adds crash cuts that must be listed. For example, if the updater raises the floor from version 7 to version 8 before writing S1S_1, a crash between the two writes leaves the selector naming version 7 in slot A while the floor is 8, and a bootloader that enforces the floor refuses to start anything. Writing S1S_1 first, and raising the floor only after version 8 is confirmed, removes that cut.

Why not make the whole update one atomic write? The example flash device supplies no atomic write for a 200,000-byte image. The selector reduces the large update to one small write. Even that write can be interrupted if the record format and storage guarantees together make every harmful partial result detectable under the stated fault model.

How is the argument tested? By power-loss injection §8.38: cutting the power, or forcing a reset, at many points of the write sequence and checking the recovered state each time. Testing samples crash cuts and checks the assumptions on real hardware; the argument covers every cut, provided the assumptions hold.

This answer does not cover whether the new version works once started, which needs a success test and a fallback, nor the wear on the two selector pages, one of which is erased in every update.

Fullest treatment: Crash consistency (§8.4).

Related entries: Commit record and selector (§8.21), Torn write (§8.19), Write ordering (§8.20), Failure atomicity (§8.5), A/B update (§8.10), Inductive invariant and its three obligations (§19.2.4), Power-loss injection (§8.38), Checksum versus CRC (§4.32). Further reading: [154].