Deadlock, Livelock and Hang Detection
A test that never finishes tells you nothing, unless something ends it with a reason. Tell a deadlock from a livelock from starvation, and choose what a hang detector should count. Then give every wait a limit that ends in a failed check.
You Will Be Able To
- Tell a deadlock, a livelock and starvation apart by what moves and what does not.
- Choose what a hang detector counts: activity, finished transfers or the job's own progress.
- Give every wait of a test a limit, sized from measurements, that ends in a failed check.
- Keep one manager able to report when the others are stuck.
- Lesson
- V7.5
- Time
- 26 min
- Runs on
- Your browser
- Check
- 5 questions
A stuck test is the quietest failure there is. The bus stops, the log stops, and the run sits until a limit ends it, with no line saying why. On ap_soc's bench, one kind of wait took 90,017 cycles to fail.
- Manager
- Memory
- Control
- Data
- A holds X and wants Y
Manager A cannot let go of X until its transfer to Y is done.
- B holds Y and wants X
Manager B is in the same position the other way round.
- The cycle
Follow the arrows: A waits for B, which waits for A. That loop is a deadlock. A wait that does not close a loop always ends.
Read the diagram as text
- Manager A: holds X
- Manager B: holds Y
- Resource X: a target, a buffer, a bridge
- Resource Y: another one
- Resource X → Manager A: held by
- Manager A → Resource Y: waits for
- Resource Y → Manager B: held by
- Manager B → Resource X: waits for
The general shape. In a fabric the resources are targets, bridges and buffers that a transfer holds while it waits for the next one.
Before You Start
Read Fairness and Starvation, Ending a Test Honestly and Ordering and Deadlock in an Interconnect first.
| Question | |
|---|---|
| 1 | How do a deadlock, a livelock and starvation differ? |
| 2 | What should a hang detector count? |
| 3 | Why does a poll that never succeeds fool a progress detector? |
| 4 | How long should a time-out be, and what must it do when it fires? |
| 5 | Which manager can still report when the fabric is stuck? |
The Idea: Three Ways to Stop
A deadlock is a cycle of waits. Each party holds something the next one needs, so nothing moves and nothing finishes. A bus in deadlock is quiet: no transfer ends, often no transfer even starts.
A livelock looks busy. Requests are made, granted, and made again, but no transfer ever finishes. A target can see an address phase in every cycle while every manager waits. Starvation is narrower: the fabric works for most managers, and one never gets through.
A hang is found by absence, and that has a cost. You can never see that an answer will never come, only that it has not come yet. So every detector needs a limit: a number of quiet cycles after which "not yet" is treated as "never". Set it too low and a slow, correct chip fails; set it too high and every hang wastes run time.
A test meets all three as the same symptom: it never ends. So the bench needs detectors, and each must count the right thing. A detector that counts activity calls a livelock healthy. One that counts finished transfers is fooled by a poll that finishes reads for ever. Only a check of the job itself sees everything.
| It counts | It sees | It misses | On ap_soc's bench |
|---|---|---|---|
| Activity at a target | a dead bus | a livelock: the target is busy, nobody finishes | not used |
| Finished transfers of any manager | a deadlock, and a fabric livelock | a poll that finishes reads for ever; one starved manager while others go on | the hang limit (args.tableHang): table_hang = 1 |
| Progress of the job itself | everything above, and a poll that never ends | nothing, if every wait has a limit | a poll's own limit, a check of the DMA's bytes, the debug host's give-up |
The no-progress limit of the bench (+noretire) counts like the hang limit: no transfer of any manager. tools/soc/ap_soc/bench/soc_tb/README.md.
New Words
| Word | Meaning |
|---|---|
| Deadlock | A cycle of waits: each party holds what the next needs, so none can move. |
| Livelock | Activity with no progress: requests repeat, nothing ever finishes. |
| Hang | A run that stops making progress, whatever the cause. |
| Progress | Work that finishes: a transfer that ends, a byte copied, a job done. |
| Time-out | A limit on a wait, after which the wait is treated as a failure. |
| No-progress limit | The bench's limit on cycles with no finished transfer of any manager. |
| Give up | End a wait at its limit with a known answer, here a read that returns 0. |
The Smallest Example: Two Detectors, One Livelock
From cycle 10, two managers take turns at one target, and neither ever finishes. Before cycle 10 a transfer finished every other cycle. Detector A counts cycles with no activity at the target. Detector B counts cycles with no finished transfer. Both fire after 16 quiet cycles.
| Cycles | Activity at the target | Finished transfers | Detector A | Detector B |
|---|---|---|---|---|
| 0 to 9 | every cycle | every other cycle | quiet count stays 0 | quiet count at most 1 |
| 10 on | every cycle | none | quiet count stays 0 | counts up from 10 |
| 25 | every cycle | none | never fires | fires: 16 quiet cycles |
The example below prints both detectors' results.
Detector A never fires: the target was busy in every cycle. Detector B fires at cycle 25, the last transfer having finished at cycle 9. A livelock is invisible to anyone who counts activity.
The Rules
Rule 1: Name what is stuck before you fix it.
Nothing moves: a deadlock, or a target that stopped answering and so closes no cycle. Busy, but nothing finishes: a livelock. One manager never finishes: starvation. Each has a different cause.
Rule 2: Count finished work, not activity.
A livelock is all activity. Detect a hang by transfers that end, and better, by the job's own progress.
Rule 3: Give every wait a limit that ends in a failed check.
A poll gets a most number of reads; a debug read gives up and returns 0. The failed check names what never came.
Rule 4: Size a limit from the measured worst case.
Too short, and a slow but correct chip fails. Too long, and every hang costs run time. Measure on the good chip, then add a margin.
Rule 5: Keep a manager that can still report.
A stuck manager reports nothing. A second manager with a limit on its own wait can fail a check while the first is stuck.
Rule 6: Never let a time-out pass quietly.
A retry that hides the time-out turns a hang into a slow test. Every expiry is a failure with a message.
Sources for These Rules
Full random concurrency surfacing internal deadlocks, livelocks and stalls: MEHTA-DV 14.4.
The bench's end-of-test rules: a checker error, the fail word, the cycle limit, the no-progress limit, the pass word (facts 27.12). The hang limit for seat tables gives reason no_progress and table_hang = 1 (the bench README). A run that only meets a limit is no caught fault in a graded problem (tools/soc/ap_soc/bench/faults/fault-runs.mjs).
The debug host's give-up: tools/soc/ap_soc/bench/soc_tb/soc_pads.sv. The bench's APB checker also has a liveness watchdog on wait cycles (96). The AHB checker has no such rule (content/modules/v3/protocols/).
Deadlock as a cycle of dependencies in an interconnect: lesson S4.9 of the SoC Architecture track.
The Lab: Two Waits That Never End
Two small tables wait for something that never comes, on the good chip. The first waits for an event that nobody sends. The second polls the DMA's STATUS for DONE, but no job was started.
core r 10004000 00000000 ffffffff 2a5310c5 0 1
tb wait_event 00000077 00000000 ffffffff 00000000 0 1
core poll 10006014 00007530 00000007 00000002 0 1| Table | Hang limit | Ended by | Cycles | table_hang | Finished transfers |
|---|---|---|---|---|---|
| Wait for an event | none | no_progress | 20019 | — | 2 |
| Wait for an event | 500 | no_progress | 521 | 1 | 2 |
| Poll, 30,000 reads | 500 | checker (the poll's limit) | 90,017 | 0 | 30,002 |
| Poll, 50 reads | 500 | checker (the poll's limit) | 167 | 0 | 52 |
Recorded on ap_soc v1.5, Verilator 5.052 2026-09-05, +core=bfm.
The event wait is a true hang: after the seat's two transfers, nothing finished. With no hang limit, the bench's no-progress limit ended it at 20019 cycles. A hang limit of 500 ended it at 521, with table_hang = 1 (rule 2). Neither run says what it waited for.
The poll is different. Its reads finish, 30,002 of them, so the hang limit never fired: from the bus's view this is progress. Only the poll's own limit ended it, after 90,017 cycles. Its message names the register and the value it never saw (rule 3).
BENCH 90009: row 0: poll 10006014 never read 00000002 (mask 00000007), last 00000000The same poll with a limit of 50 reads failed at cycle 167, with the same message. Which limit is right? The one a good chip never reaches with margin, and no more (rule 4). A DMA job of a few bytes is done in far fewer than 50 reads on the good chip.
The bench's JTAG host has such a limit built in. It gives up on a debug read after 64 scans that answer BUSY, and the read returns 0. So a debug read during a fabric hang still ends. A check of its value fails while the seat sits stuck (rule 5). Problem V7.5.2 makes use of exactly this.
Why It Is Built This Way
Why does a graded problem refuse a run that only meets a limit as proof of a faulty chip? Because a limit says only that something stopped, not what. A test is better when it fails a check that names the thing that never came.
Why count finished transfers for the hang limit, and not activity? A livelock is pure activity. A detector of finished transfers is cheap and sees both deadlock and fabric livelock. What it cannot see, a test's own limited waits must cover.
Traps
The patient-poll trap: a poll with no limit, or with a huge one, waits as long as the cycle limit allows. On this bench a poll of 30,000 reads took 90,017 cycles to fail. A thousand such tests make a slow regression.
The busy-bus trap: a livelock detector that counts activity at a target. Every livelock passes it, because a livelock is nothing but activity.
The hidden-retry trap: a driver times out, quietly retries, and the second try works. The test passes and the hang is never reported. The chip ships with it.
The everyone-stuck trap: the only checks in the test are made by the manager that hangs. The run ends at a limit with no failed check, and a graded problem cannot count it as a found bug. Give another manager something to check.
At Scale
This section looks ahead. ap_soc's fabric holds one transfer per target and per bridge.
Deadlock by design rules
Fabrics with buffers are built with rules, such as separate request and response paths, so no cycle of waits can form. Verification checks the rules hold.
Formal liveness
Some teams prove "every request is eventually answered" with formal tools, instead of hoping a test makes the bad timing.
Watchdogs in silicon
Chips add bus time-outs that answer an error after a limit, so software can recover. Their limit is tested like any other.
Try It
Run it: the activity detector never fires; the finished-transfer detector fires at cycle 25.
Shorten the limit to 6 (the predict question below). When does detector B fire?
What would detector B say about a poll whose reads all finish?
detector A (activity): never fired detector B (finished transfers): fired at cycle 25Check Yourself
Common Mistakes
Wrong Detect a hang by activity on the bus.
A livelock is all activity.
Fix Count finished transfers, and check the job's own progress.
Wrong Poll for a flag with no limit.
A flag that never comes costs the whole cycle limit and names nothing.
Fix Give each poll a limit from the measured worst case, and let it fail.
Wrong Put every check of a test on one manager.
When that manager hangs, nothing can fail.
Fix Give a second manager with a limited wait something to check.
Remember
- Deadlock: nothing moves. Livelock: busy, nothing finishes. Starvation: one manager never finishes.
- A hang detector must count finished work, not activity; the job's own progress is best.
- Every wait needs a limit, sized from the measured worst case, that ends in a failed check.
- When managers hang, a second manager with a limited wait is what can still report.