Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/kernel/proofs/TicketLock.lean # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / kernel / proofs / TicketLock.lean` 240 lines · 12318 bytes Raw Blame History 1 /- 2 TicketLock.lean -- the arithmetic behind core/spinlock.h (DRA-556). 3 4 The lock holds two 32-bit counters, ticket and serving, compared only for equality and 5 wrapping mod 2^32. While n callers hold tickets (the holder plus waiters), those tickets are 6 serving + 0, serving + 1, ..., serving + (n-1) (mod 2^32), because each caller took the next 7 value of ticket with a fetch-add and the holder releases by serving := serving + 1. 8 9 Proved here, for any modulus M > 0 (the code uses M = 2^32) and n <= M: 10 * mutual exclusion: the caller with offset i may enter (its ticket equals serving) iff i = 0; 11 * FIFO: after a release, the caller at offset i + 1 is at offset i, so callers enter in the 12 order they took tickets. 13 Also proved below: 14 * the queue invariant, derived from the operations alone: Reach2 (acquire = fetch-add, trylock only 15 when ticket = serving, release by the holder) reaches only states whose outstanding tickets are 16 exactly serving, serving+1, ..., ticket-1 (outstanding_eq_range), hence distinct (reach_nodup), and 17 with at most 2^32 outstanding the reduced comparison admits exactly the oldest (reach_mutual_exclusion); 18 wrap_increment: the 32-bit increment is the unbounded one reduced. (Reach/reach_le/enters_iff_head are 19 the smaller lemmas these use.) 20 * the interrupt ordering of core/spinlock.h in a one-CPU model checked by `decide` over the concrete 21 lock programs (lockUnlock_safe, nested_safe, lockUnlock_restores), and each TEST-ONLY mutant of the 22 header unsafe (ticketBeforeMask_unsafe, restoreBeforeRelease_unsafe, noMask_unsafe). 23 Not modelled: memory ordering (acquire/release) and several CPUs running at once; the operations are 24 taken as atomic steps. The link to the C is by construction, not extraction: each Op and each Reach2 25 rule names its statement in core/spinlock.h; tests/test_spinlock.c runs the same three mutants (make 26 spinlock-mutant) and tools/ck_spinlock_asm_check.sh checks the compiled order (make spinlock-asm-check). 27 No Mathlib; core Lean 4 only. 28 -/ 29 30 /-- Ticket of the caller at queue offset `i`, mod `M`. -/ 31 def tk (M s i : Nat) : Nat := (s + i) % M 32 33 /-- A caller enters when its ticket equals the serving counter. -/ 34 def enters (M s i : Nat) : Prop := tk M s i = s % M 35 36 theorem offset_zero_enters (M s : Nat) : enters M s 0 := by 37 simp [enters, tk] 38 39 /-- Mutual exclusion: with fewer than M callers, only offset 0 can enter. -/ 40 theorem only_head_enters (M s i : Nat) (hi : i < M) (h : enters M s i) : i = 0 := by 41 unfold enters tk at h 42 -- (s + i) % M = s % M -> i % M = 0 (since (s + i) % M = (s % M + i % M) % M) 43 have hM : 0 < M := Nat.lt_of_le_of_lt (Nat.zero_le i) hi 44 have h1 : (s % M + i) % M = s % M := by rw [Nat.add_mod, Nat.mod_mod, ← Nat.add_mod] at *; simpa [Nat.add_mod] using h 45 have hlt : s % M < M := Nat.mod_lt _ hM 46 rcases Nat.eq_zero_or_pos i with h0 | hpos 47 · exact h0 48 -- s % M + i lies in (s % M, s % M + M), so its residue differs from s % M 49 by_cases hc : s % M + i < M 50 · rw [Nat.mod_eq_of_lt hc] at h1 51 omega 52 · have hge : M ≤ s % M + i := Nat.le_of_not_lt hc 53 have hlt2 : s % M + i - M < M := by omega 54 have : (s % M + i) % M = s % M + i - M := by 55 rw [Nat.mod_eq_sub_mod hge, Nat.mod_eq_of_lt hlt2] 56 rw [this] at h1 57 omega 58 59 /-- Two callers with offsets below M never both enter. -/ 60 theorem mutual_exclusion (M s i j : Nat) (hi : i < M) (hj : j < M) 61 (h1 : enters M s i) (h2 : enters M s j) : i = j := by 62 rw [only_head_enters M s i hi h1, only_head_enters M s j hj h2] 63 64 /-- Release moves serving to s + 1: the caller at offset i + 1 now has offset i (FIFO). -/ 65 theorem release_shifts (M s i : Nat) : tk M s (i + 1) = tk M (s + 1) i := by 66 unfold tk 67 rw [Nat.add_assoc, Nat.add_comm 1 i] 68 69 /-- FIFO: the caller at offset k enters after exactly k releases, and not before. -/ 70 theorem enters_after_k_releases (M s k : Nat) (hk : k < M) : 71 enters M (s + k) 0 ∧ ∀ r, r < k → ¬ enters M (s + r) (k - r) := by 72 refine ⟨offset_zero_enters M (s + k), ?_⟩ 73 intro r hr h 74 have := only_head_enters M (s + r) (k - r) (by omega) h 75 omega 76 77 /-- Counters wrap: equality mod M is all the code compares, and the tickets above stay 78 consistent across 2^32 (instance used by core/spinlock.h). -/ 79 theorem wrap_instance : enters (2^32) (2^32 - 1) 0 ∧ ¬ enters (2^32) (2^32 - 1) 1 := by 80 refine ⟨offset_zero_enters _ _, ?_⟩ 81 intro h 82 have := only_head_enters (2^32) (2^32 - 1) 1 (by decide) h 83 omega 84 85 /-! ## The queue invariant, derived from the two operations 86 87 The code's counters are 32-bit and wrap. Model them as unbounded naturals `ticket`, `serving`; the code 88 stores `ticket % 2^32` and `serving % 2^32`. Acquire is `ticket := ticket + 1` (the caller keeps the old 89 value, core/spinlock.h ck_spin_lock_nomask fetch-add); release is `serving := serving + 1` and is only 90 done by the holder, i.e. while some ticket is outstanding (ck_spin_unlock_nomask). -/ 91 92 structure Q where 93 ticket : Nat 94 serving : Nat 95 96 /-- States reachable from the unlocked lock (0, 0) by acquires and holder releases. -/ 97 inductive Reach : Q → Prop 98 | init : Reach ⟨0, 0⟩ 99 | take {q : Q} : Reach q → Reach ⟨q.ticket + 1, q.serving⟩ 100 | release {q : Q} : Reach q → q.serving < q.ticket → Reach ⟨q.ticket, q.serving + 1⟩ 101 102 theorem reach_le {q : Q} (h : Reach q) : q.serving ≤ q.ticket := by 103 induction h with 104 | init => exact Nat.le_refl 0 105 | take _ ih => exact Nat.le_succ_of_le ih 106 | release _ hlt _ => exact hlt 107 108 /-- The 32-bit increment the code does equals the unbounded increment, reduced. -/ 109 theorem wrap_increment (M x : Nat) : (x + 1) % M = (x % M + 1) % M := by 110 rw [Nat.add_mod, Nat.add_mod (x % M) 1 M, Nat.mod_mod] 111 112 /-- Every outstanding caller holds a ticket t with serving ≤ t < ticket (it took the old value of ticket 113 after serving had passed nobody it had not served). Its offset is t - serving < ticket - serving. 114 With at most M outstanding, the reduced comparison the code does (t % M = serving % M) admits exactly 115 the caller whose ticket is serving: the head of the queue. -/ 116 theorem enters_iff_head (M : Nat) (q : Q) (t : Nat) 117 (hlo : q.serving ≤ t) (hhi : t < q.ticket) (hM : q.ticket - q.serving ≤ M) : 118 t % M = q.serving % M ↔ t = q.serving := by 119 constructor 120 · intro h 121 have hi : t - q.serving < M := by omega 122 have e : q.serving + (t - q.serving) = t := by omega 123 have h' : enters M q.serving (t - q.serving) := by unfold enters tk; rw [e]; exact h 124 have := only_head_enters M q.serving (t - q.serving) hi h' 125 omega 126 · intro h; rw [h] 127 128 /-! ## Interrupt ordering (the property DRA-556 is about) 129 130 One CPU runs a lock program. State: whether interrupts are masked, how many tickets this CPU has 131 outstanding (held or queued), and the stack of saved mask states. An interrupt can be taken in any state 132 where interrupts are open; a handler that takes the same lock while this CPU has a ticket outstanding spins 133 on a ticket behind its own CPU's and never returns (self-deadlock). So a program is safe when every state it 134 passes through has: outstanding > 0 → masked. -/ 135 136 inductive Op 137 | mask -- ck_irq_save: push the current state, then mask 138 | restore -- ck_irq_restore: pop the saved state 139 | take -- fetch-add on ticket (then spin until served) 140 | release -- store serving + 1 141 deriving DecidableEq 142 143 structure S where 144 masked : Bool 145 outstanding : Nat 146 saved : List Bool 147 deriving DecidableEq 148 149 def S.step (s : S) : Op → S 150 | .mask => { s with masked := true, saved := s.masked :: s.saved } 151 | .restore => { s with masked := s.saved.headD s.masked, saved := s.saved.tail } 152 | .take => { s with outstanding := s.outstanding + 1 } 153 | .release => { s with outstanding := s.outstanding - 1 } 154 155 def S.ok (s : S) : Bool := s.outstanding == 0 || s.masked 156 157 /-- Safe: the start state and every state after each step are ok. -/ 158 def safe : S → List Op → Bool 159 | s, [] => s.ok 160 | s, o :: os => s.ok && safe (s.step o) os 161 162 def start (b : Bool) : S := ⟨b, 0, []⟩ 163 164 /-- core/spinlock.h order: mask, take (ck_spin_lock_irqsave); release, restore (ck_spin_unlock_irqrestore). -/ 165 def lockUnlock : List Op := [.mask, .take, .release, .restore] 166 /-- Mutant CK_SPIN_TEST_TICKET_BEFORE_MASK. -/ 167 def ticketBeforeMask : List Op := [.take, .mask, .release, .restore] 168 /-- Mutant CK_SPIN_TEST_RESTORE_BEFORE_RELEASE. -/ 169 def restoreBeforeRelease : List Op := [.mask, .take, .restore, .release] 170 /-- Mutant CK_SPIN_TEST_NO_IRQ_MASK. -/ 171 def noMask : List Op := [.take, .release] 172 /-- Two locks nested, released in reverse order. -/ 173 def nested : List Op := [.mask, .take, .mask, .take, .release, .restore, .release, .restore] 174 175 theorem lockUnlock_safe (b : Bool) : safe (start b) lockUnlock = true := by cases b <;> decide 176 theorem nested_safe (b : Bool) : safe (start b) nested = true := by cases b <;> decide 177 /-- The caller's mask state comes back unchanged (a pre-masked caller stays masked). -/ 178 theorem lockUnlock_restores (b : Bool) : 179 (lockUnlock.foldl S.step (start b)).masked = b ∧ (lockUnlock.foldl S.step (start b)).outstanding = 0 := by 180 cases b <;> decide 181 theorem ticketBeforeMask_unsafe : safe (start false) ticketBeforeMask = false := by decide 182 theorem restoreBeforeRelease_unsafe : safe (start false) restoreBeforeRelease = false := by decide 183 theorem noMask_unsafe : safe (start false) noMask = false := by decide 184 185 /-! ## The queue invariant over the outstanding tickets (derived, with trylock) 186 187 `out` is the list of tickets held or waited on, oldest first. Acquire (fetch-add) appends the old 188 ticket; trylock (ck_spin_trylock_nomask) does the same but only when ticket = serving (nobody holds or 189 waits); release (by the holder) drops the oldest and moves serving on. From these alone, every 190 reachable state has out = [serving, serving+1, ..., ticket-1]: distinct, contiguous, oldest = serving. -/ 191 192 structure Q2 where 193 ticket : Nat 194 serving : Nat 195 out : List Nat 196 197 inductive Reach2 : Q2 → Prop 198 | init : Reach2 ⟨0, 0, []⟩ 199 | take {q : Q2} : Reach2 q → Reach2 ⟨q.ticket + 1, q.serving, q.out ++ [q.ticket]⟩ 200 | trylock {q : Q2} : Reach2 q → q.ticket = q.serving → Reach2 ⟨q.ticket + 1, q.serving, q.out ++ [q.ticket]⟩ 201 | release {q : Q2} : Reach2 q → q.serving < q.ticket → Reach2 ⟨q.ticket, q.serving + 1, q.out.tail⟩ 202 203 theorem outstanding_eq_range {q : Q2} (h : Reach2 q) : 204 q.serving ≤ q.ticket ∧ q.out = List.range' q.serving (q.ticket - q.serving) := by 205 induction h with 206 | init => exact ⟨Nat.le_refl 0, rfl⟩ 207 | @take q _ ih => 208 obtain ⟨hle, hout⟩ := ih 209 refine ⟨Nat.le_succ_of_le hle, ?_⟩ 210 show q.out ++ [q.ticket] = List.range' q.serving (q.ticket + 1 - q.serving) 211 have e1 : q.ticket + 1 - q.serving = (q.ticket - q.serving) + 1 := by omega 212 have e2 : q.serving + 1 * (q.ticket - q.serving) = q.ticket := by omega 213 rw [hout, e1, List.range'_concat, e2] 214 | @trylock q _ _ ih => 215 obtain ⟨hle, hout⟩ := ih 216 refine ⟨Nat.le_succ_of_le hle, ?_⟩ 217 show q.out ++ [q.ticket] = List.range' q.serving (q.ticket + 1 - q.serving) 218 have e1 : q.ticket + 1 - q.serving = (q.ticket - q.serving) + 1 := by omega 219 have e2 : q.serving + 1 * (q.ticket - q.serving) = q.ticket := by omega 220 rw [hout, e1, List.range'_concat, e2] 221 | @release q _ hlt ih => 222 obtain ⟨_, hout⟩ := ih 223 refine ⟨hlt, ?_⟩ 224 show q.out.tail = List.range' (q.serving + 1) (q.ticket - (q.serving + 1)) 225 have e1 : q.ticket - q.serving = (q.ticket - (q.serving + 1)) + 1 := by omega 226 rw [hout, e1] 227 rfl 228 229 /-- Mutual exclusion from the operations alone: in any reachable state with at most M outstanding 230 callers, the reduced comparison the code does admits exactly one outstanding caller, the oldest. -/ 231 theorem reach_mutual_exclusion (M : Nat) {q : Q2} (h : Reach2 q) (hM : q.ticket - q.serving ≤ M) 232 (t : Nat) (ht : t ∈ q.out) : t % M = q.serving % M ↔ t = q.serving := by 233 obtain ⟨_, hout⟩ := outstanding_eq_range h 234 rw [hout, List.mem_range'_1] at ht 235 exact enters_iff_head M ⟨q.ticket, q.serving⟩ t ht.1 (by simp only; omega) hM 236 237 /-- The outstanding tickets are distinct. -/ 238 theorem reach_nodup {q : Q2} (h : Reach2 q) : q.out.Nodup := by 239 rw [(outstanding_eq_range h).2] 240 exact List.nodup_range'