1 .. SPDX-License-Identifier: GPL-2.0 OR GFDL-1.2-no-invariants-only
3 ===========================
4 Lockless Ring Buffer Design
5 ===========================
7 Copyright 2009 Red Hat Inc.
9 :Author: Steven Rostedt <srostedt@redhat.com>
10 :License: The GNU Free Documentation License, Version 1.2
11 (dual licensed under the GPL v2)
12 :Reviewers: Mathieu Desnoyers, Huang Ying, Hidetoshi Seto,
13 and Frederic Weisbecker.
18 Terminology used in this Document
19 ---------------------------------
22 - where new writes happen in the ring buffer.
25 - where new reads happen in the ring buffer.
28 - the task that writes into the ring buffer (same as writer)
34 - the task that reads from the buffer (same as reader)
40 - A page outside the ring buffer used solely (for the most part)
44 - a pointer to the page that the reader will use next
47 - a pointer to the page that will be written to next
50 - a pointer to the page with the last finished non-nested write.
53 - hardware-assisted atomic transaction that performs the following::
55 A = B if previous A == C
57 R = cmpxchg(A, C, B) is saying that we replace A with B if and only
58 if current A is equal to C, and we put the old (current)
61 R gets the previous A regardless if A is updated with B or not.
63 To see if the update was successful a compare of ``R == C``
66 The Generic Ring Buffer
67 -----------------------
69 The ring buffer can be used in either an overwrite mode or in
70 producer/consumer mode.
72 Producer/consumer mode is where if the producer were to fill up the
73 buffer before the consumer could free up anything, the producer
74 will stop writing to the buffer. This will lose most recent events.
76 Overwrite mode is where if the producer were to fill up the buffer
77 before the consumer could free up anything, the producer will
78 overwrite the older data. This will lose the oldest events.
80 No two writers can write at the same time (on the same per-cpu buffer),
81 but a writer may interrupt another writer, but it must finish writing
82 before the previous writer may continue. This is very important to the
83 algorithm. The writers act like a "stack". The way interrupts works
84 enforces this behavior::
88 <preempted> writer2 start
89 <preempted> writer3 start
94 This is very much like a writer being preempted by an interrupt and
95 the interrupt doing a write as well.
97 Readers can happen at any time. But no two readers may run at the
98 same time, nor can a reader preempt/interrupt another reader. A reader
99 cannot preempt/interrupt a writer, but it may read/consume from the
100 buffer at the same time as a writer is writing, but the reader must be
101 on another processor to do so. A reader may read on its own processor
102 and can be preempted by a writer.
104 A writer can preempt a reader, but a reader cannot preempt a writer.
105 But a reader can read the buffer at the same time (on another processor)
108 The ring buffer is made up of a list of pages held together by a linked list.
110 At initialization a reader page is allocated for the reader that is not
111 part of the ring buffer.
113 The head_page, tail_page and commit_page are all initialized to point
116 The reader page is initialized to have its next pointer pointing to
117 the head page, and its previous pointer pointing to a page before
120 The reader has its own page to use. At start up time, this page is
121 allocated but is not attached to the list. When the reader wants
122 to read from the buffer, if its page is empty (like it is on start-up),
123 it will swap its page with the head_page. The old reader page will
124 become part of the ring buffer and the head_page will be removed.
125 The page after the inserted page (old reader_page) will become the
128 Once the new page is given to the reader, the reader could do what
129 it wants with it, as long as a writer has left that page.
131 A sample of how the reader page is swapped: Note this does not
132 show the head page in the buffer, it is for demonstrating a swap
152 |page |-------------------+
157 | +---+ +---+ +---+ |
159 | | +-------------+ | |
160 | +-----------------+ |
161 +------------------------------------+
165 |page |-------------------+
166 +------+ <---------------+ v
167 | ^ +---+ +---+ +---+
170 | | +---+ +---+ +---+ |
172 | | +-------------+ | |
173 | +-----------------------------+ |
174 +------------------------------------+
178 |page |-------------------+
179 +------+ <---------------+ v
180 | ^ +---+ +---+ +---+
182 | | New | | | |<--| |<-+
183 | | Reader +---+ +---+ +---+ |
186 | +-----------------------------+ |
187 +------------------------------------+
191 It is possible that the page swapped is the commit page and the tail page,
192 if what is in the ring buffer is less than what is held in a buffer page.
196 reader page commit page tail page
201 | |<------------------------+
206 +---+ +---+ +---+ +---+
207 <---| |--->| |--->| |--->| |--->
208 --->| |<---| |<---| |<---| |<---
209 +---+ +---+ +---+ +---+
211 This case is still valid for this algorithm.
212 When the writer leaves the page, it simply goes into the ring buffer
213 since the reader page still points to the next location in the ring
220 - The page used solely by the reader and is not part
221 of the ring buffer (may be swapped in)
224 - the next page in the ring buffer that will be swapped
225 with the reader page.
228 - the page where the next write will take place.
231 - the page that last finished a write.
233 The commit page only is updated by the outermost writer in the
234 writer stack. A writer that preempts another writer will not move the
237 When data is written into the ring buffer, a position is reserved
238 in the ring buffer and passed back to the writer. When the writer
239 is finished writing data into that position, it commits the write.
241 Another write (or a read) may take place at anytime during this
242 transaction. If another write happens it must finish before continuing
243 with the previous write.
251 +---------+ <--- given back to writer (current commit)
253 +---------+ <--- tail pointer
264 +---------+ <--- next position for write (current commit)
269 If a write happens after the first reserve::
274 +---------+ <-- current commit
276 +---------+ <--- given back to second writer
278 +---------+ <--- tail pointer
280 After second writer commits::
286 +---------+ <--(last full commit)
291 +---------+ <--- tail pointer
293 When the first writer commits::
302 +---------+ <--(last full commit and tail pointer)
305 The commit pointer points to the last write location that was
306 committed without preempting another write. When a write that
307 preempted another write is committed, it only becomes a pending commit
308 and will not be a full commit until all writes have been committed.
310 The commit page points to the page that has the last full commit.
311 The tail page points to the page with the last write (before
314 The tail page is always equal to or after the commit page. It may
315 be several pages ahead. If the tail page catches up to the commit
316 page then no more writes may take place (regardless of the mode
317 of the ring buffer: overwrite and produce/consumer).
319 The order of pages is::
328 head page commit page |
331 +---+ +---+ +---+ +---+
332 <---| |--->| |--->| |--->| |--->
333 --->| |<---| |<---| |<---| |<---
334 +---+ +---+ +---+ +---+
336 There is a special case that the head page is after either the commit page
337 and possibly the tail page. That is when the commit (and tail) page has been
338 swapped with the reader page. This is because the head page is always
339 part of the ring buffer, but the reader page is not. Whenever there
340 has been less than a full page that has been committed inside the ring buffer,
341 and a reader swaps out a page, it will be swapping out the commit page.
345 reader page commit page tail page
350 | |<------------------------+
355 +---+ +---+ +---+ +---+
356 <---| |--->| |--->| |--->| |--->
357 --->| |<---| |<---| |<---| |<---
358 +---+ +---+ +---+ +---+
364 In this case, the head page will not move when the tail and commit
365 move back into the ring buffer.
367 The reader cannot swap a page into the ring buffer if the commit page
368 is still on that page. If the read meets the last commit (real commit
369 not pending or reserved), then there is nothing more to read.
370 The buffer is considered empty until another full commit finishes.
372 When the tail meets the head page, if the buffer is in overwrite mode,
373 the head page will be pushed ahead one. If the buffer is in producer/consumer
374 mode, the write will fail.
381 +---+ +---+ +---+ +---+
382 <---| |--->| |--->| |--->| |--->
383 --->| |<---| |<---| |<---| |<---
384 +---+ +---+ +---+ +---+
393 +---+ +---+ +---+ +---+
394 <---| |--->| |--->| |--->| |--->
395 --->| |<---| |<---| |<---| |<---
396 +---+ +---+ +---+ +---+
405 +---+ +---+ +---+ +---+
406 <---| |--->| |--->| |--->| |--->
407 --->| |<---| |<---| |<---| |<---
408 +---+ +---+ +---+ +---+
413 Note, the reader page will still point to the previous head page.
414 But when a swap takes place, it will use the most recent head page.
417 Making the Ring Buffer Lockless:
418 --------------------------------
420 The main idea behind the lockless algorithm is to combine the moving
421 of the head_page pointer with the swapping of pages with the reader.
422 State flags are placed inside the pointer to the page. To do this,
423 each page must be aligned in memory by 4 bytes. This will allow the 2
424 least significant bits of the address to be used as flags, since
425 they will always be zero for the address. To get the address,
426 simply mask out the flags::
432 Two flags will be kept by these two bits:
435 - the page being pointed to is a head page
438 - the page being pointed to is being updated by a writer
439 and was or is about to be a head page.
451 +---+ +---+ +---+ +---+
452 <---| |--->| |-H->| |--->| |--->
453 --->| |<---| |<---| |<---| |<---
454 +---+ +---+ +---+ +---+
457 The above pointer "-H->" would have the HEADER flag set. That is
458 the next page is the next page to be swapped out by the reader.
459 This pointer means the next page is the head page.
461 When the tail page meets the head pointer, it will use cmpxchg to
462 change the pointer to the UPDATE state::
468 +---+ +---+ +---+ +---+
469 <---| |--->| |-H->| |--->| |--->
470 --->| |<---| |<---| |<---| |<---
471 +---+ +---+ +---+ +---+
476 +---+ +---+ +---+ +---+
477 <---| |--->| |-U->| |--->| |--->
478 --->| |<---| |<---| |<---| |<---
479 +---+ +---+ +---+ +---+
481 "-U->" represents a pointer in the UPDATE state.
483 Any access to the reader will need to take some sort of lock to serialize
484 the readers. But the writers will never take a lock to write to the
485 ring buffer. This means we only need to worry about a single reader,
486 and writes only preempt in "stack" formation.
488 When the reader tries to swap the page with the ring buffer, it
489 will also use cmpxchg. If the flag bit in the pointer to the
490 head page does not have the HEADER flag set, the compare will fail
491 and the reader will need to look for the new head page and try again.
492 Note, the flags UPDATE and HEADER are never set at the same time.
494 The reader swaps the reader page as follows::
505 | +---------------+ |
506 +-----H-------------+
508 The reader sets the reader page next pointer as HEADER to the page after
514 |page |-------H-----------+
518 | | |<---| |<---| |<-+
519 | +---+ +---+ +---+ |
521 | | +---------------+ | |
522 | +-----H-------------+ |
523 +--------------------------------------+
525 It does a cmpxchg with the pointer to the previous head page to make it
526 point to the reader page. Note that the new pointer does not have the HEADER
527 flag set. This action atomically moves the head page forward::
531 |page |-------H-----------+
533 | ^ +---+ +---+ +---+
535 | | | |<--| |<--| |<-+
536 | | +---+ +---+ +---+ |
538 | | +-------------+ | |
539 | +-----------------------------+ |
540 +------------------------------------+
542 After the new head page is set, the previous pointer of the head page is
543 updated to the reader page::
547 |page |-------H-----------+
548 +------+ <---------------+ v
549 | ^ +---+ +---+ +---+
552 | | +---+ +---+ +---+ |
554 | | +-------------+ | |
555 | +-----------------------------+ |
556 +------------------------------------+
560 |page |-------H-----------+ <--- New head page
561 +------+ <---------------+ v
562 | ^ +---+ +---+ +---+
564 | | New | | | |<--| |<-+
565 | | Reader +---+ +---+ +---+ |
568 | +-----------------------------+ |
569 +------------------------------------+
571 Another important point: The page that the reader page points back to
572 by its previous pointer (the one that now points to the new head page)
573 never points back to the reader page. That is because the reader page is
574 not part of the ring buffer. Traversing the ring buffer via the next pointers
575 will always stay in the ring buffer. Traversing the ring buffer via the
576 prev pointers may not.
578 Note, the way to determine a reader page is simply by examining the previous
579 pointer of the page. If the next pointer of the previous page does not
580 point back to the original page, then the original page is a reader page::
584 | reader | next +----+
585 | page |-------->| |<====== (buffer page)
593 The way the head page moves forward:
595 When the tail page meets the head page and the buffer is in overwrite mode
596 and more writes take place, the head page must be moved forward before the
597 writer may move the tail page. The way this is done is that the writer
598 performs a cmpxchg to convert the pointer to the head page from the HEADER
599 flag to have the UPDATE flag set. Once this is done, the reader will
600 not be able to swap the head page from the buffer, nor will it be able to
601 move the head page, until the writer is finished with the move.
603 This eliminates any races that the reader can have on the writer. The reader
604 must spin, and this is why the reader cannot preempt the writer::
609 +---+ +---+ +---+ +---+
610 <---| |--->| |-H->| |--->| |--->
611 --->| |<---| |<---| |<---| |<---
612 +---+ +---+ +---+ +---+
617 +---+ +---+ +---+ +---+
618 <---| |--->| |-U->| |--->| |--->
619 --->| |<---| |<---| |<---| |<---
620 +---+ +---+ +---+ +---+
622 The following page will be made into the new head page::
627 +---+ +---+ +---+ +---+
628 <---| |--->| |-U->| |-H->| |--->
629 --->| |<---| |<---| |<---| |<---
630 +---+ +---+ +---+ +---+
632 After the new head page has been set, we can set the old head page
633 pointer back to NORMAL::
638 +---+ +---+ +---+ +---+
639 <---| |--->| |--->| |-H->| |--->
640 --->| |<---| |<---| |<---| |<---
641 +---+ +---+ +---+ +---+
643 After the head page has been moved, the tail page may now move forward::
648 +---+ +---+ +---+ +---+
649 <---| |--->| |--->| |-H->| |--->
650 --->| |<---| |<---| |<---| |<---
651 +---+ +---+ +---+ +---+
654 The above are the trivial updates. Now for the more complex scenarios.
657 As stated before, if enough writes preempt the first write, the
658 tail page may make it all the way around the buffer and meet the commit
659 page. At this time, we must start dropping writes (usually with some kind
660 of warning to the user). But what happens if the commit was still on the
661 reader page? The commit page is not part of the ring buffer. The tail page
662 must account for this::
665 reader page commit page
675 +---+ +---+ +---+ +---+
676 <---| |--->| |-H->| |--->| |--->
677 --->| |<---| |<---| |<---| |<---
678 +---+ +---+ +---+ +---+
683 If the tail page were to simply push the head page forward, the commit when
684 leaving the reader page would not be pointing to the correct page.
686 The solution to this is to test if the commit page is on the reader page
687 before pushing the head page. If it is, then it can be assumed that the
688 tail page wrapped the buffer, and we must drop new writes.
690 This is not a race condition, because the commit page can only be moved
691 by the outermost writer (the writer that was preempted).
692 This means that the commit will not move while a writer is moving the
693 tail page. The reader cannot swap the reader page if it is also being
694 used as the commit page. The reader can simply check that the commit
695 is off the reader page. Once the commit page leaves the reader page
696 it will never go back on it unless a reader does another swap with the
697 buffer page that is also the commit page.
703 In the pushing forward of the tail page we must first push forward
704 the head page if the head page is the next page. If the head page
705 is not the next page, the tail page is simply updated with a cmpxchg.
707 Only writers move the tail page. This must be done atomically to protect
708 against nested writers::
710 temp_page = tail_page
711 next_page = temp_page->next
712 cmpxchg(tail_page, temp_page, next_page)
714 The above will update the tail page if it is still pointing to the expected
715 page. If this fails, a nested write pushed it forward, the current write
716 does not need to push it::
725 +---+ +---+ +---+ +---+
726 <---| |--->| |--->| |--->| |--->
727 --->| |<---| |<---| |<---| |<---
728 +---+ +---+ +---+ +---+
730 Nested write comes in and moves the tail page forward::
732 tail page (moved by nested writer)
736 +---+ +---+ +---+ +---+
737 <---| |--->| |--->| |--->| |--->
738 --->| |<---| |<---| |<---| |<---
739 +---+ +---+ +---+ +---+
741 The above would fail the cmpxchg, but since the tail page has already
742 been moved forward, the writer will just try again to reserve storage
743 on the new tail page.
745 But the moving of the head page is a bit more complex::
750 +---+ +---+ +---+ +---+
751 <---| |--->| |-H->| |--->| |--->
752 --->| |<---| |<---| |<---| |<---
753 +---+ +---+ +---+ +---+
755 The write converts the head page pointer to UPDATE::
760 +---+ +---+ +---+ +---+
761 <---| |--->| |-U->| |--->| |--->
762 --->| |<---| |<---| |<---| |<---
763 +---+ +---+ +---+ +---+
765 But if a nested writer preempts here, it will see that the next
766 page is a head page, but it is also nested. It will detect that
767 it is nested and will save that information. The detection is the
768 fact that it sees the UPDATE flag instead of a HEADER or NORMAL
771 The nested writer will set the new head page pointer::
776 +---+ +---+ +---+ +---+
777 <---| |--->| |-U->| |-H->| |--->
778 --->| |<---| |<---| |<---| |<---
779 +---+ +---+ +---+ +---+
781 But it will not reset the update back to normal. Only the writer
782 that converted a pointer from HEAD to UPDATE will convert it back
788 +---+ +---+ +---+ +---+
789 <---| |--->| |-U->| |-H->| |--->
790 --->| |<---| |<---| |<---| |<---
791 +---+ +---+ +---+ +---+
793 After the nested writer finishes, the outermost writer will convert
794 the UPDATE pointer to NORMAL::
800 +---+ +---+ +---+ +---+
801 <---| |--->| |--->| |-H->| |--->
802 --->| |<---| |<---| |<---| |<---
803 +---+ +---+ +---+ +---+
806 It can be even more complex if several nested writes came in and moved
807 the tail page ahead several pages::
815 +---+ +---+ +---+ +---+
816 <---| |--->| |-H->| |--->| |--->
817 --->| |<---| |<---| |<---| |<---
818 +---+ +---+ +---+ +---+
820 The write converts the head page pointer to UPDATE::
825 +---+ +---+ +---+ +---+
826 <---| |--->| |-U->| |--->| |--->
827 --->| |<---| |<---| |<---| |<---
828 +---+ +---+ +---+ +---+
830 Next writer comes in, and sees the update and sets up the new
838 +---+ +---+ +---+ +---+
839 <---| |--->| |-U->| |-H->| |--->
840 --->| |<---| |<---| |<---| |<---
841 +---+ +---+ +---+ +---+
843 The nested writer moves the tail page forward. But does not set the old
844 update page to NORMAL because it is not the outermost writer::
849 +---+ +---+ +---+ +---+
850 <---| |--->| |-U->| |-H->| |--->
851 --->| |<---| |<---| |<---| |<---
852 +---+ +---+ +---+ +---+
854 Another writer preempts and sees the page after the tail page is a head page.
855 It changes it from HEAD to UPDATE::
862 +---+ +---+ +---+ +---+
863 <---| |--->| |-U->| |-U->| |--->
864 --->| |<---| |<---| |<---| |<---
865 +---+ +---+ +---+ +---+
867 The writer will move the head page forward::
875 +---+ +---+ +---+ +---+
876 <---| |--->| |-U->| |-U->| |-H->
877 --->| |<---| |<---| |<---| |<---
878 +---+ +---+ +---+ +---+
880 But now that the third writer did change the HEAD flag to UPDATE it
881 will convert it to normal::
889 +---+ +---+ +---+ +---+
890 <---| |--->| |-U->| |--->| |-H->
891 --->| |<---| |<---| |<---| |<---
892 +---+ +---+ +---+ +---+
895 Then it will move the tail page, and return back to the second writer::
903 +---+ +---+ +---+ +---+
904 <---| |--->| |-U->| |--->| |-H->
905 --->| |<---| |<---| |<---| |<---
906 +---+ +---+ +---+ +---+
909 The second writer will fail to move the tail page because it was already
910 moved, so it will try again and add its data to the new tail page.
911 It will return to the first writer::
919 +---+ +---+ +---+ +---+
920 <---| |--->| |-U->| |--->| |-H->
921 --->| |<---| |<---| |<---| |<---
922 +---+ +---+ +---+ +---+
924 The first writer cannot know atomically if the tail page moved
925 while it updates the HEAD page. It will then update the head page to
926 what it thinks is the new head page::
934 +---+ +---+ +---+ +---+
935 <---| |--->| |-U->| |-H->| |-H->
936 --->| |<---| |<---| |<---| |<---
937 +---+ +---+ +---+ +---+
939 Since the cmpxchg returns the old value of the pointer the first writer
940 will see it succeeded in updating the pointer from NORMAL to HEAD.
941 But as we can see, this is not good enough. It must also check to see
942 if the tail page is either where it use to be or on the next page::
950 +---+ +---+ +---+ +---+
951 <---| |--->| |-U->| |-H->| |-H->
952 --->| |<---| |<---| |<---| |<---
953 +---+ +---+ +---+ +---+
955 If tail page != A and tail page != B, then it must reset the pointer
956 back to NORMAL. The fact that it only needs to worry about nested
957 writers means that it only needs to check this after setting the HEAD page::
965 +---+ +---+ +---+ +---+
966 <---| |--->| |-U->| |--->| |-H->
967 --->| |<---| |<---| |<---| |<---
968 +---+ +---+ +---+ +---+
970 Now the writer can update the head page. This is also why the head page must
971 remain in UPDATE and only reset by the outermost writer. This prevents
972 the reader from seeing the incorrect head page::
980 +---+ +---+ +---+ +---+
981 <---| |--->| |--->| |--->| |-H->
982 --->| |<---| |<---| |<---| |<---
983 +---+ +---+ +---+ +---+