The Write Buffer and Its Read Hazard

A write buffer takes slow memory writes out of the way of the processor. It also creates a short time in which memory is wrong, and a read can fall into it.

You Will Be Able To

  • Say what a write buffer saves, and where it sits.
  • Explain how a read can return old data while a write waits in the buffer.
  • Compare the two cures: wait for the buffer, or look in it.
Lesson
S3.18
Time
24 min
Runs on
Your browser
Check
6 questions

A program stores 2222 in a word and reads the word back in the next line of code. It gets 1111, the old value. It reads again and gets 1111 again. Memory holds 2222 by then, and the program will never see it.

Before You Start

You need S3.17: a write must reach memory, and a memory write is slow. You need S3.5: a read that misses fetches from memory.

Nothing else is needed.

Five questions this page answers
Question
1What does a write buffer save?
2Why can memory hold old data while the buffer is not empty?
3When does a read return a stale word?
4Which two cures are there, and what does each cost?
5What must a buffer answer when it is asked for an address?

The Idea: A Waiting Room for Writes, and a Read That Jumps the Queue

Some writes must go to memory: every write under write-through, and every dirty block under write-back. Memory is slow. If the processor waited for each of these, the gain of the cache would be lost on writes.

So a cache puts a write buffer between itself and memory. It is a small store for an address and its data. A write goes into it within one cycle, and the processor goes on. The buffer hands the write to memory when memory has time.

The saving has a price that is easy to miss. While a write waits, memory holds the old data. The newest value of that word is in the buffer and nowhere else. This lasts only a few cycles. It is still long enough for a read.

A read that hits in the cache is safe: the line was updated by the write. A read that misses goes to memory. If it asks for an address whose write is still waiting, memory gives the old word. The read has passed the write. A word that is old in this way is called stale.

There are two cures. The read can wait until the buffer is empty, which is simple and slow. Or the read can ask the buffer first: is a write to my address waiting here? If so, the buffer hands over the data, and memory is not asked at all.

New Words

New words on this page
WordMeaning
Write bufferA small store between a cache and memory. It holds writes, each an address and its data, until memory has taken them.
Entry of a bufferOne place in the buffer: one address and one word of data.
Waiting writeA write that is in the buffer and has not reached memory yet.
Stale dataData that was correct once and has been replaced since. A read that returns it is a stale read.
Read hazardThe danger that a read goes to memory while a write to the same address still waits.
Empty the bufferTo wait until memory has taken every waiting write.
Look in the bufferTo compare the address of a read with the addresses of the waiting writes. On a match, the data is taken from the buffer.

One Picture: The Write Buffer and the Read That Passes

A Write Waits, and a Read Passes ItStep through the picture. It adds a write buffer, shows what it saves, and then shows a read that overtakes a waiting write.
  • Processor
  • Memory
  • Bus
  • Control / FSM
A Write Waits, and a Read Passes ItStep through the picture. It adds a write buffer, shows what it saves, and then shows a read that overtakes a waiting write. component Processor writes, then reads component Cache does not hold thisword component Write buffer an address and a wordthat wait component Memory slow: still holds theold word component Look in thebuffer compare the addressof the read a write: one cyclelater, in thebackgrounda reada miss goes to memorya miss asks here firstsame address?
  1. component Processorwrites, then reads
    • a write: one cycle → Write buffer
    • a read → Cache
  2. component Memoryslow: still holds the old word
  3. component Write bufferan address and a word that wait
    • later, in the background → Memory
  4. component Cachedoes not hold this word
    • a miss goes to memory → Memory
    • a miss asks here first → Look in the buffer
  5. component Look in the buffercompare the address of the read
    • same address? → Write buffer
  1. A write without a buffer

    A write that goes to memory makes the processor wait until memory has taken it. Memory is slow, so that is many cycles.

  2. The write buffer

    Put a small store in between. The write goes in within one cycle, and the processor goes on. The buffer passes the write to memory in the background.

  3. Memory is behind

    While the write waits in the buffer, memory still holds the old word. For those cycles the newest data is in the buffer, and nowhere else.

  4. A read passes the write

    Now the processor reads the same address. The cache does not have it, so the read goes to memory. Memory answers with the old word.

  5. The cure

    A read that misses must respect the buffer. Either it waits until the buffer is empty, or it looks in the buffer first and takes the word from there.

Read the diagram as text
  • Processor: writes, then reads
  • Cache: does not hold this word
  • Write buffer: an address and a word that wait
  • Memory: slow: still holds the old word
  • Look in the buffer: compare the address of the read
  • Processor → Write buffer: a write: one cycle
  • Write buffer → Memory: later, in the background
  • Processor → Cache: a read
  • Cache → Memory: a miss goes to memory
  • Cache → Look in the buffer: a miss asks here first
  • Look in the buffer → Write buffer: same address?

A write buffer between a cache and memory. The lab below has a buffer with one entry.

The Smallest Example, Worked by Hand

Time is counted in ticks. Memory needs 6 ticks to take a write and 4 ticks to answer a read. Word 20 of memory holds 1111, and it is not in the cache. The program writes 2222 to word 20, and reads word 20 straight after.

A write, and a read that starts one tick later
TickThe bufferMemory word 20The read
12222 for word 20 goes in1111starts: a miss, so it asks memory
2 to 4waits1111waits for memory
5waits1111memory answers: 1111
6memory takes the write: empty2222too late: 1111 is in the cache now

The read ends at tick 5. The write reaches memory at tick 6. The read was faster by one tick.

The read returned 1111, and the cache kept it. From tick 6 on memory holds 2222, but every later read of word 20 hits in the cache and gets 1111. One tick of bad luck has become a permanent fault.

Now the two cures, on the same two steps.

The two cures
CureWhat the read doesThe read ends at tickData
Nonegoes to memory at once51111, stale
Wait for the bufferwaits until tick 6, then asks memory102222
Look in the bufferfinds word 20 in the buffer22222

Waiting costs 5 more ticks. Looking costs a comparator for each entry, and it is even faster than memory.

The Rules

  1. Rule 1: A write buffer lets the processor go on after one cycle.

    The buffer passes the write to memory in the background.

  2. Rule 2: While a write waits in the buffer, memory holds old data for that address.

    The newest value is in the buffer, and nowhere else.

  3. Rule 3: A read that hits in the cache is safe. A read that goes to memory is not.

    It returns stale data if a write to its address still waits.

  4. Rule 4: A stale read is kept: the old word goes into the cache.

    Later reads hit on it, long after memory has become right.

  5. Rule 5: Cure one: empty the buffer before any read goes to memory.

    Simple. Every such read waits for the slow writes in front of it.

  6. Rule 6: Cure two: look in the buffer first, and answer from it on a match.

    The buffer must give the word that memory would hold once the buffer is empty.

Sources for These Rules

The textbook "Computer Organization and Design" by Patterson and Hennessy, section 5.3, "Handling Writes". The write buffer holds data that waits to be written to memory, so that the processor can go on. Section 5.4 counts the stalls of a full write buffer.

The textbook "Digital Design and Computer Architecture" by Harris and Harris, section 8.3.4, on write policies.

The books describe the write buffer and what it saves. The read hazard and its two cures are common practice in cache design. They are given here as such, not checked against a book. The plan of this course lists the hazard for this lesson.

The times of 6 and 4 ticks, the buffer of one entry and the cache of one line are our own teaching example.

Now Run It

The lab at the end of this page is test code. It has a memory, a cache of one line, and a write buffer with one entry. A write that misses goes to the buffer only. Each row gives the tick at which a step ends.

Printed by the lab on Icarus, with FIX = 0: a read that misses goes straight to memory.
t=1 write 20 <= 00002222 | into the buffer, the processor goes on
t=5 read  20: miss | data=00001111, fetched from memory
t=6 read  20: hit  | data=00001111
t=16 the end   | memory word 20 holds 00002222, and the cache holds 00001111

Rules 1 and 2 at work: the first row. The write is done at tick 1, as far as the processor can tell. Memory will not have it before tick 6.

Rule 3 at work: the second row. The read is a miss, goes to memory, and ends at tick 5 with 1111. Rule 4 at work: the third row. The next read is a hit, and it returns 1111 again.

The last row is the proof. Memory word 20 holds 2222, and the cache holds 1111. The two will not agree again until the line is replaced.

Here is the same run with the first cure, and then with the second:

First four rows: FIX = 1, the read waits for the buffer. Last four rows: FIX = 2, the read looks in the buffer.
t=1 write 20 <= 00002222 | into the buffer, the processor goes on
t=10 read  20: miss | data=00002222, fetched from memory
t=11 read  20: hit  | data=00002222
t=21 the end   | memory word 20 holds 00002222, and the cache holds 00002222
t=1 write 20 <= 00002222 | into the buffer, the processor goes on
t=2 read  20: miss | data=00002222, taken from the buffer
t=3 read  20: hit  | data=00002222
t=13 the end   | memory word 20 holds 00002222, and the cache holds 00002222

Rule 5 at work: with FIX = 1 the read ends at tick 10 and returns 2222. Rule 6 at work: with FIX = 2 it ends at tick 2, with the word taken from the buffer.

This lab leaves out a buffer with several entries, and writes of single bytes. Both make the look harder, and the practice problem of this page is about the first.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
A write bufferThe processor waits for every memory writeA write costs one cycle. Memory is behind for a while, and reads must take care.
Look in the bufferEmpty the buffer firstNo waiting. One address comparator for each entry, on the path of every miss.
Empty the buffer firstLook in the bufferAlmost no logic. A read behind a write is as slow as that write.
Several entriesOne entrySeveral writes in a row fit. The look must then choose among several matches.

The Trap: The Test That Was Too Polite

The team that built the buffer with no cure had a test for it. The test wrote a word, did five ticks of other work, and read the word back. It passed every night. Here is that test in the lab:

The lab with FIX = 0. The bench waits 5 ticks between the write and the read.
t=1 write 20 <= 00003333 | into the buffer, the processor goes on
t=10 read  20: miss | data=00003333, fetched from memory
t=20 the end   | memory word 20 holds 00003333, and the cache holds 00003333

The read starts at tick 6. By then memory has taken the write, so the read returns 3333 and all is well. The hazard is only open while the write waits, and the test never looked inside that time.

Real programs are not polite. A compiler stores a value and loads it again in the next instruction all the time. Rule 3 names the exact case to test: a read that misses in the cache, for an address that is still in the buffer.

At Scale

This page1 entry
A real cache4 to 8 entries, each a whole block

This section looks ahead. Real write buffers have several entries, and the same hazard appears in other places.

  1. Several matches

    Two waiting writes can have the same address. The look must then give what memory will hold in the end.

  2. Parts of a word

    A waiting write of one byte and a read of the whole word match only in part. Many designs empty the buffer in that case.

  3. A full buffer

    When writes arrive faster than memory takes them, the buffer fills up, and the processor must wait after all.

  4. The same hazard elsewhere

    The wrapper in front of an SRAM had it in a small form: a read that meets a write waited one cycle.

Try It

Run the lab. Check the ticks against the hand table.

Set FIX to 1 and then to 2. Predict the tick and the data of the first read each time.

Set WRITE_TICKS to 3 with FIX at 0. Predict the data of the first read, and say why the fault has gone into hiding.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: t=1 write 20 <= 00002222 | into the buffer, the processor goes on t=5 read 20: miss | data=00001111, fetched from memory t=6 read 20: hit | data=00001111 t=16 the end | memory word 20 holds 00002222, and the cache holds 00001111

Check Yourself

Predict the outputWith no cure, the bench writes 7777 to word 20, reads word 30, and then reads word 20. Memory takes a write in 6 ticks and answers a read in 4. What does the last row say?
// A write buffer of one entry between a cache and a slow memory. Time is in ticks of this model.
module tb;
  localparam FIX = 0;           // 0: a read that misses goes straight to memory
                               // 1: it first waits until the buffer is empty
                               // 2: it first looks in the buffer, and takes the word from there
  localparam WRITE_TICKS = 6;  // memory needs 6 ticks to take a write
  localparam READ_TICKS  = 4;  // and 4 ticks to answer a read
  logic [31:0] mem [0:255];
  logic        line_valid = 0;         // the cache: one line of one word is enough here
  logic [7:0]  line_addr;
  logic [31:0] line_data;
  logic        buf_valid = 0;          // the write buffer: one address and one word
  logic [7:0]  buf_addr;
  logic [31:0] buf_data;
  int buf_left, now = 0;

  task automatic pass(input int ticks);            // time passes, and the buffer empties into memory
    repeat (ticks) begin
      now++;
      if (buf_valid) begin
        buf_left--;
        if (buf_left == 0) begin mem[buf_addr] = buf_data;  buf_valid = 0; end
      end
    end
  endtask

  // A write: the word goes into the buffer, and the processor goes on after one tick.
  task automatic write(input logic [7:0] address, input logic [31:0] data);
    while (buf_valid) pass(1);                     // one entry only: wait if it is taken
    if (line_valid && line_addr == address) line_data = data;
    buf_valid = 1;  buf_addr = address;  buf_data = data;  buf_left = WRITE_TICKS;
    pass(1);
    $display("t=%0d write %02h <= %08h | into the buffer, the processor goes on", now, address, data);
  endtask

  task automatic read(input logic [7:0] address);
    if (line_valid && line_addr == address) begin
      pass(1);
      $display("t=%0d read  %02h: hit  | data=%08h", now, address, line_data);
    end else begin
      if (FIX == 1) while (buf_valid) pass(1);
      if (FIX == 2 && buf_valid && buf_addr == address) begin
        line_data = buf_data;
        pass(1);
        $display("t=%0d read  %02h: miss | data=%08h, taken from the buffer", now, address, line_data);
      end else begin
        pass(READ_TICKS);
        line_data = mem[address];
        $display("t=%0d read  %02h: miss | data=%08h, fetched from memory", now, address, line_data);
      end
      line_valid = 1;  line_addr = address;
    end
  endtask

  initial begin
    for (int n = 0; n < 256; n++) mem[n] = 32'h0000_1111;
    write(8'h20, 32'h0000_7777);
    read(8'h30);                 // another word first
    read(8'h20);
    pass(10);
    $display("t=%0d the end   | memory word 20 holds %08h, and the cache holds %08h", now, mem[8'h20], line_data);
  end
endmodule
ConceptWhat does a write buffer save?
ConceptA write waits in the buffer. What does memory hold for that address?
ConceptWhich read can return stale data?
ConceptWhat does the cure "empty the buffer first" cost?
ConceptA buffer is asked for an address. Which word must it give?

Common Mistakes

Wrong Let a read that misses go to memory without a look at the buffer.

Memory holds old data while a write waits.

Fix Look in the buffer first, or wait until it is empty.

Wrong Test the buffer with a pause between write and read.

The hazard exists only while the write waits.

Fix Read the same address at once, with the word not in the cache.

Wrong Think of a stale read as a short glitch.

The old word is stored in the cache, and later reads hit on it.

Fix Treat it as lost data: it stays wrong until the line is replaced.

Remember

  • A write buffer lets the processor go on after one cycle. Memory gets the write later.
  • While a write waits, memory is stale for that address.
  • A read that misses must wait for the buffer, or look in it first.
  • A stale read is stored in the cache, so the fault stays.

Practice

Report a problem with this page