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.

A Cycle of WaitsTwo managers, two resources. Each holds one and waits for the other. Nothing in the cycle can move, so nothing ever finishes.
  • Manager
  • Memory
  • Control
  • Data
A Cycle of WaitsTwo managers, two resources. Each holds one and waits for the other. Nothing in the cycle can move, so nothing ever finishes. manager Manager A holds X manager Manager B holds Y memory Resource X a target, a buffer, abridge memory Resource Y another one held bywaits forheld bywaits for
  1. manager Manager Aholds X
    • waits for → Resource Y
  2. memory Resource Xa target, a buffer, a bridge
    • held by → Manager A
  3. memory Resource Yanother one
    • held by → Manager B
  4. manager Manager Bholds Y
    • waits for → Resource X
  1. A holds X and wants Y

    Manager A cannot let go of X until its transfer to Y is done.

  2. B holds Y and wants X

    Manager B is in the same position the other way round.

  3. 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.

Five questions this page answers
Question
1How do a deadlock, a livelock and starvation differ?
2What should a hang detector count?
3Why does a poll that never succeeds fool a progress detector?
4How long should a time-out be, and what must it do when it fires?
5Which 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.

Three Things a Detector Can Count
It countsIt seesIt missesOn ap_soc's bench
Activity at a targeta dead busa livelock: the target is busy, nobody finishesnot used
Finished transfers of any managera deadlock, and a fabric livelocka poll that finishes reads for ever; one starved manager while others go onthe hang limit (args.tableHang): table_hang = 1
Progress of the job itselfeverything above, and a poll that never endsnothing, if every wait has a limita 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

New words on this page
WordMeaning
DeadlockA cycle of waits: each party holds what the next needs, so none can move.
LivelockActivity with no progress: requests repeat, nothing ever finishes.
HangA run that stops making progress, whatever the cause.
ProgressWork that finishes: a transfer that ends, a byte copied, a job done.
Time-outA limit on a wait, after which the wait is treated as a failure.
No-progress limitThe bench's limit on cycles with no finished transfer of any manager.
Give upEnd 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.

What each detector counts
CyclesActivity at the targetFinished transfersDetector ADetector B
0 to 9every cycleevery other cyclequiet count stays 0quiet count at most 1
10 onevery cyclenonequiet count stays 0counts up from 10
25every cyclenonenever firesfires: 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

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

The two tables (record-v7-labs.mjs, section hang). A poll's data field is its most reads: 0x7530 is 30,000.
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
What ended each run
TableHang limitEnded byCyclestable_hangFinished transfers
Wait for an eventnoneno_progress20019—2
Wait for an event500no_progress52112
Poll, 30,000 reads500checker (the poll's limit)90,017030,002
Poll, 50 reads500checker (the poll's limit)167052

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).

The poll's message.
BENCH 90009: row 0: poll 10006014 never read 00000002 (mask 00000007), last 00000000

The 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

ap_soc~12 blocks
Phone-class SoChundreds of blocks

This section looks ahead. ap_soc's fabric holds one transfer per target and per bridge.

  1. 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.

  2. Formal liveness

    Some teams prove "every request is eventually answered" with formal tools, instead of hoping a test makes the bad timing.

  3. 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?

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: detector A (activity): never fired detector B (finished transfers): fired at cycle 25

Check Yourself

Predict the outputThe same livelock with a limit of 6 cycles. What is the last line printed?
// From cycle 10 two managers take turns at one target and neither ever finishes: the target is busy in every cycle.
// Detector A ends the run after 6 cycles with no activity at the target. Detector B ends it after 6 cycles
// with no finished transfer of any manager.
module tb;
  int quiet_a = 0, quiet_b = 0, fired_a = -1, fired_b = -1;
  initial begin
    bit activity, finished;
    for (int c = 0; c < 60; c++) begin
      activity = 1;                       // the target sees an address phase in every cycle
      finished = (c < 10) && (c % 2 == 1); // before cycle 10, a transfer finishes every other cycle
      quiet_a = activity ? 0 : quiet_a + 1;
      quiet_b = finished ? 0 : quiet_b + 1;
      if (quiet_a == 6 && fired_a < 0) fired_a = c;
      if (quiet_b == 6 && fired_b < 0) fired_b = c;
    end
    if (fired_a < 0) $display("detector A (activity): never fired");
    else $display("detector A (activity): fired at cycle %0d", fired_a);
    if (fired_b < 0) $display("detector B (finished transfers): never fired");
    else $display("detector B (finished transfers): fired at cycle %0d", fired_b);
  end
endmodule
ConceptIn the lab, why did the hang limit not end the poll?
ConceptThe bus is quiet and no transfer finishes. What is it most likely?
ConceptHow should a poll's limit be chosen?
ConceptWhen every manager of a test is stuck, what can still fail a check?

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.

Practice

Report a problem with this page