1 .. This file is dual-licensed: you can use it either under the terms
2 .. of the GPL 2.0 or the GFDL 1.2 license, at your option. Note that this
3 .. dual licensing only applies to this file, and not this project as a
6 .. a) This file is free software; you can redistribute it and/or
7 .. modify it under the terms of the GNU General Public License as
8 .. published by the Free Software Foundation version 2 of
11 .. This file is distributed in the hope that it will be useful,
12 .. but WITHOUT ANY WARRANTY; without even the implied warranty of
13 .. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 .. GNU General Public License for more details.
18 .. b) Permission is granted to copy, distribute and/or modify this
19 .. document under the terms of the GNU Free Documentation License,
20 .. Version 1.2 version published by the Free Software
21 .. Foundation, with no Invariant Sections, no Front-Cover Texts
22 .. and no Back-Cover Texts. A copy of the license is included at
23 .. Documentation/userspace-api/media/fdl-appendix.rst.
25 .. TODO: replace it to GPL-2.0 OR GFDL-1.2 WITH no-invariant-sections
27 ===========================
28 Lockless Ring Buffer Design
29 ===========================
31 Copyright 2009 Red Hat Inc.
33 :Author: Steven Rostedt <srostedt@redhat.com>
34 :License: The GNU Free Documentation License, Version 1.2
35 (dual licensed under the GPL v2)
36 :Reviewers: Mathieu Desnoyers, Huang Ying, Hidetoshi Seto,
37 and Frederic Weisbecker.
42 Terminology used in this Document
43 ---------------------------------
46 - where new writes happen in the ring buffer.
49 - where new reads happen in the ring buffer.
52 - the task that writes into the ring buffer (same as writer)
58 - the task that reads from the buffer (same as reader)
64 - A page outside the ring buffer used solely (for the most part)
68 - a pointer to the page that the reader will use next
71 - a pointer to the page that will be written to next
74 - a pointer to the page with the last finished non-nested write.
77 - hardware-assisted atomic transaction that performs the following::
79 A = B if previous A == C
81 R = cmpxchg(A, C, B) is saying that we replace A with B if and only
82 if current A is equal to C, and we put the old (current)
85 R gets the previous A regardless if A is updated with B or not.
87 To see if the update was successful a compare of ``R == C``
90 The Generic Ring Buffer
91 -----------------------
93 The ring buffer can be used in either an overwrite mode or in
94 producer/consumer mode.
96 Producer/consumer mode is where if the producer were to fill up the
97 buffer before the consumer could free up anything, the producer
98 will stop writing to the buffer. This will lose most recent events.
100 Overwrite mode is where if the producer were to fill up the buffer
101 before the consumer could free up anything, the producer will
102 overwrite the older data. This will lose the oldest events.
104 No two writers can write at the same time (on the same per-cpu buffer),
105 but a writer may interrupt another writer, but it must finish writing
106 before the previous writer may continue. This is very important to the
107 algorithm. The writers act like a "stack". The way interrupts works
108 enforces this behavior::
112 <preempted> writer2 start
113 <preempted> writer3 start
118 This is very much like a writer being preempted by an interrupt and
119 the interrupt doing a write as well.
121 Readers can happen at any time. But no two readers may run at the
122 same time, nor can a reader preempt/interrupt another reader. A reader
123 cannot preempt/interrupt a writer, but it may read/consume from the
124 buffer at the same time as a writer is writing, but the reader must be
125 on another processor to do so. A reader may read on its own processor
126 and can be preempted by a writer.
128 A writer can preempt a reader, but a reader cannot preempt a writer.
129 But a reader can read the buffer at the same time (on another processor)
132 The ring buffer is made up of a list of pages held together by a linked list.
134 At initialization a reader page is allocated for the reader that is not
135 part of the ring buffer.
137 The head_page, tail_page and commit_page are all initialized to point
140 The reader page is initialized to have its next pointer pointing to
141 the head page, and its previous pointer pointing to a page before
144 The reader has its own page to use. At start up time, this page is
145 allocated but is not attached to the list. When the reader wants
146 to read from the buffer, if its page is empty (like it is on start-up),
147 it will swap its page with the head_page. The old reader page will
148 become part of the ring buffer and the head_page will be removed.
149 The page after the inserted page (old reader_page) will become the
152 Once the new page is given to the reader, the reader could do what
153 it wants with it, as long as a writer has left that page.
155 A sample of how the reader page is swapped: Note this does not
156 show the head page in the buffer, it is for demonstrating a swap
176 |page |-------------------+
181 | +---+ +---+ +---+ |
183 | | +-------------+ | |
184 | +-----------------+ |
185 +------------------------------------+
189 |page |-------------------+
190 +------+ <---------------+ v
191 | ^ +---+ +---+ +---+
194 | | +---+ +---+ +---+ |
196 | | +-------------+ | |
197 | +-----------------------------+ |
198 +------------------------------------+
202 |page |-------------------+
203 +------+ <---------------+ v
204 | ^ +---+ +---+ +---+
206 | | New | | | |<--| |<-+
207 | | Reader +---+ +---+ +---+ |
210 | +-----------------------------+ |
211 +------------------------------------+
215 It is possible that the page swapped is the commit page and the tail page,
216 if what is in the ring buffer is less than what is held in a buffer page.
220 reader page commit page tail page
225 | |<------------------------+
230 +---+ +---+ +---+ +---+
231 <---| |--->| |--->| |--->| |--->
232 --->| |<---| |<---| |<---| |<---
233 +---+ +---+ +---+ +---+
235 This case is still valid for this algorithm.
236 When the writer leaves the page, it simply goes into the ring buffer
237 since the reader page still points to the next location in the ring
244 - The page used solely by the reader and is not part
245 of the ring buffer (may be swapped in)
248 - the next page in the ring buffer that will be swapped
249 with the reader page.
252 - the page where the next write will take place.
255 - the page that last finished a write.
257 The commit page only is updated by the outermost writer in the
258 writer stack. A writer that preempts another writer will not move the
261 When data is written into the ring buffer, a position is reserved
262 in the ring buffer and passed back to the writer. When the writer
263 is finished writing data into that position, it commits the write.
265 Another write (or a read) may take place at anytime during this
266 transaction. If another write happens it must finish before continuing
267 with the previous write.
275 +---------+ <--- given back to writer (current commit)
277 +---------+ <--- tail pointer
288 +---------+ <--- next position for write (current commit)
293 If a write happens after the first reserve::
298 +---------+ <-- current commit
300 +---------+ <--- given back to second writer
302 +---------+ <--- tail pointer
304 After second writer commits::
310 +---------+ <--(last full commit)
315 +---------+ <--- tail pointer
317 When the first writer commits::
326 +---------+ <--(last full commit and tail pointer)
329 The commit pointer points to the last write location that was
330 committed without preempting another write. When a write that
331 preempted another write is committed, it only becomes a pending commit
332 and will not be a full commit until all writes have been committed.
334 The commit page points to the page that has the last full commit.
335 The tail page points to the page with the last write (before
338 The tail page is always equal to or after the commit page. It may
339 be several pages ahead. If the tail page catches up to the commit
340 page then no more writes may take place (regardless of the mode
341 of the ring buffer: overwrite and produce/consumer).
343 The order of pages is::
352 head page commit page |
355 +---+ +---+ +---+ +---+
356 <---| |--->| |--->| |--->| |--->
357 --->| |<---| |<---| |<---| |<---
358 +---+ +---+ +---+ +---+
360 There is a special case that the head page is after either the commit page
361 and possibly the tail page. That is when the commit (and tail) page has been
362 swapped with the reader page. This is because the head page is always
363 part of the ring buffer, but the reader page is not. Whenever there
364 has been less than a full page that has been committed inside the ring buffer,
365 and a reader swaps out a page, it will be swapping out the commit page.
369 reader page commit page tail page
374 | |<------------------------+
379 +---+ +---+ +---+ +---+
380 <---| |--->| |--->| |--->| |--->
381 --->| |<---| |<---| |<---| |<---
382 +---+ +---+ +---+ +---+
388 In this case, the head page will not move when the tail and commit
389 move back into the ring buffer.
391 The reader cannot swap a page into the ring buffer if the commit page
392 is still on that page. If the read meets the last commit (real commit
393 not pending or reserved), then there is nothing more to read.
394 The buffer is considered empty until another full commit finishes.
396 When the tail meets the head page, if the buffer is in overwrite mode,
397 the head page will be pushed ahead one. If the buffer is in producer/consumer
398 mode, the write will fail.
405 +---+ +---+ +---+ +---+
406 <---| |--->| |--->| |--->| |--->
407 --->| |<---| |<---| |<---| |<---
408 +---+ +---+ +---+ +---+
417 +---+ +---+ +---+ +---+
418 <---| |--->| |--->| |--->| |--->
419 --->| |<---| |<---| |<---| |<---
420 +---+ +---+ +---+ +---+
429 +---+ +---+ +---+ +---+
430 <---| |--->| |--->| |--->| |--->
431 --->| |<---| |<---| |<---| |<---
432 +---+ +---+ +---+ +---+
437 Note, the reader page will still point to the previous head page.
438 But when a swap takes place, it will use the most recent head page.
441 Making the Ring Buffer Lockless:
442 --------------------------------
444 The main idea behind the lockless algorithm is to combine the moving
445 of the head_page pointer with the swapping of pages with the reader.
446 State flags are placed inside the pointer to the page. To do this,
447 each page must be aligned in memory by 4 bytes. This will allow the 2
448 least significant bits of the address to be used as flags, since
449 they will always be zero for the address. To get the address,
450 simply mask out the flags::
456 Two flags will be kept by these two bits:
459 - the page being pointed to is a head page
462 - the page being pointed to is being updated by a writer
463 and was or is about to be a head page.
475 +---+ +---+ +---+ +---+
476 <---| |--->| |-H->| |--->| |--->
477 --->| |<---| |<---| |<---| |<---
478 +---+ +---+ +---+ +---+
481 The above pointer "-H->" would have the HEADER flag set. That is
482 the next page is the next page to be swapped out by the reader.
483 This pointer means the next page is the head page.
485 When the tail page meets the head pointer, it will use cmpxchg to
486 change the pointer to the UPDATE state::
492 +---+ +---+ +---+ +---+
493 <---| |--->| |-H->| |--->| |--->
494 --->| |<---| |<---| |<---| |<---
495 +---+ +---+ +---+ +---+
500 +---+ +---+ +---+ +---+
501 <---| |--->| |-U->| |--->| |--->
502 --->| |<---| |<---| |<---| |<---
503 +---+ +---+ +---+ +---+
505 "-U->" represents a pointer in the UPDATE state.
507 Any access to the reader will need to take some sort of lock to serialize
508 the readers. But the writers will never take a lock to write to the
509 ring buffer. This means we only need to worry about a single reader,
510 and writes only preempt in "stack" formation.
512 When the reader tries to swap the page with the ring buffer, it
513 will also use cmpxchg. If the flag bit in the pointer to the
514 head page does not have the HEADER flag set, the compare will fail
515 and the reader will need to look for the new head page and try again.
516 Note, the flags UPDATE and HEADER are never set at the same time.
518 The reader swaps the reader page as follows::
529 | +---------------+ |
530 +-----H-------------+
532 The reader sets the reader page next pointer as HEADER to the page after
538 |page |-------H-----------+
542 | | |<---| |<---| |<-+
543 | +---+ +---+ +---+ |
545 | | +---------------+ | |
546 | +-----H-------------+ |
547 +--------------------------------------+
549 It does a cmpxchg with the pointer to the previous head page to make it
550 point to the reader page. Note that the new pointer does not have the HEADER
551 flag set. This action atomically moves the head page forward::
555 |page |-------H-----------+
557 | ^ +---+ +---+ +---+
559 | | | |<--| |<--| |<-+
560 | | +---+ +---+ +---+ |
562 | | +-------------+ | |
563 | +-----------------------------+ |
564 +------------------------------------+
566 After the new head page is set, the previous pointer of the head page is
567 updated to the reader page::
571 |page |-------H-----------+
572 +------+ <---------------+ v
573 | ^ +---+ +---+ +---+
576 | | +---+ +---+ +---+ |
578 | | +-------------+ | |
579 | +-----------------------------+ |
580 +------------------------------------+
584 |page |-------H-----------+ <--- New head page
585 +------+ <---------------+ v
586 | ^ +---+ +---+ +---+
588 | | New | | | |<--| |<-+
589 | | Reader +---+ +---+ +---+ |
592 | +-----------------------------+ |
593 +------------------------------------+
595 Another important point: The page that the reader page points back to
596 by its previous pointer (the one that now points to the new head page)
597 never points back to the reader page. That is because the reader page is
598 not part of the ring buffer. Traversing the ring buffer via the next pointers
599 will always stay in the ring buffer. Traversing the ring buffer via the
600 prev pointers may not.
602 Note, the way to determine a reader page is simply by examining the previous
603 pointer of the page. If the next pointer of the previous page does not
604 point back to the original page, then the original page is a reader page::
608 | reader | next +----+
609 | page |-------->| |<====== (buffer page)
617 The way the head page moves forward:
619 When the tail page meets the head page and the buffer is in overwrite mode
620 and more writes take place, the head page must be moved forward before the
621 writer may move the tail page. The way this is done is that the writer
622 performs a cmpxchg to convert the pointer to the head page from the HEADER
623 flag to have the UPDATE flag set. Once this is done, the reader will
624 not be able to swap the head page from the buffer, nor will it be able to
625 move the head page, until the writer is finished with the move.
627 This eliminates any races that the reader can have on the writer. The reader
628 must spin, and this is why the reader cannot preempt the writer::
633 +---+ +---+ +---+ +---+
634 <---| |--->| |-H->| |--->| |--->
635 --->| |<---| |<---| |<---| |<---
636 +---+ +---+ +---+ +---+
641 +---+ +---+ +---+ +---+
642 <---| |--->| |-U->| |--->| |--->
643 --->| |<---| |<---| |<---| |<---
644 +---+ +---+ +---+ +---+
646 The following page will be made into the new head page::
651 +---+ +---+ +---+ +---+
652 <---| |--->| |-U->| |-H->| |--->
653 --->| |<---| |<---| |<---| |<---
654 +---+ +---+ +---+ +---+
656 After the new head page has been set, we can set the old head page
657 pointer back to NORMAL::
662 +---+ +---+ +---+ +---+
663 <---| |--->| |--->| |-H->| |--->
664 --->| |<---| |<---| |<---| |<---
665 +---+ +---+ +---+ +---+
667 After the head page has been moved, the tail page may now move forward::
672 +---+ +---+ +---+ +---+
673 <---| |--->| |--->| |-H->| |--->
674 --->| |<---| |<---| |<---| |<---
675 +---+ +---+ +---+ +---+
678 The above are the trivial updates. Now for the more complex scenarios.
681 As stated before, if enough writes preempt the first write, the
682 tail page may make it all the way around the buffer and meet the commit
683 page. At this time, we must start dropping writes (usually with some kind
684 of warning to the user). But what happens if the commit was still on the
685 reader page? The commit page is not part of the ring buffer. The tail page
686 must account for this::
689 reader page commit page
699 +---+ +---+ +---+ +---+
700 <---| |--->| |-H->| |--->| |--->
701 --->| |<---| |<---| |<---| |<---
702 +---+ +---+ +---+ +---+
707 If the tail page were to simply push the head page forward, the commit when
708 leaving the reader page would not be pointing to the correct page.
710 The solution to this is to test if the commit page is on the reader page
711 before pushing the head page. If it is, then it can be assumed that the
712 tail page wrapped the buffer, and we must drop new writes.
714 This is not a race condition, because the commit page can only be moved
715 by the outermost writer (the writer that was preempted).
716 This means that the commit will not move while a writer is moving the
717 tail page. The reader cannot swap the reader page if it is also being
718 used as the commit page. The reader can simply check that the commit
719 is off the reader page. Once the commit page leaves the reader page
720 it will never go back on it unless a reader does another swap with the
721 buffer page that is also the commit page.
727 In the pushing forward of the tail page we must first push forward
728 the head page if the head page is the next page. If the head page
729 is not the next page, the tail page is simply updated with a cmpxchg.
731 Only writers move the tail page. This must be done atomically to protect
732 against nested writers::
734 temp_page = tail_page
735 next_page = temp_page->next
736 cmpxchg(tail_page, temp_page, next_page)
738 The above will update the tail page if it is still pointing to the expected
739 page. If this fails, a nested write pushed it forward, the current write
740 does not need to push it::
749 +---+ +---+ +---+ +---+
750 <---| |--->| |--->| |--->| |--->
751 --->| |<---| |<---| |<---| |<---
752 +---+ +---+ +---+ +---+
754 Nested write comes in and moves the tail page forward::
756 tail page (moved by nested writer)
760 +---+ +---+ +---+ +---+
761 <---| |--->| |--->| |--->| |--->
762 --->| |<---| |<---| |<---| |<---
763 +---+ +---+ +---+ +---+
765 The above would fail the cmpxchg, but since the tail page has already
766 been moved forward, the writer will just try again to reserve storage
767 on the new tail page.
769 But the moving of the head page is a bit more complex::
774 +---+ +---+ +---+ +---+
775 <---| |--->| |-H->| |--->| |--->
776 --->| |<---| |<---| |<---| |<---
777 +---+ +---+ +---+ +---+
779 The write converts the head page pointer to UPDATE::
784 +---+ +---+ +---+ +---+
785 <---| |--->| |-U->| |--->| |--->
786 --->| |<---| |<---| |<---| |<---
787 +---+ +---+ +---+ +---+
789 But if a nested writer preempts here, it will see that the next
790 page is a head page, but it is also nested. It will detect that
791 it is nested and will save that information. The detection is the
792 fact that it sees the UPDATE flag instead of a HEADER or NORMAL
795 The nested writer will set the new head page pointer::
800 +---+ +---+ +---+ +---+
801 <---| |--->| |-U->| |-H->| |--->
802 --->| |<---| |<---| |<---| |<---
803 +---+ +---+ +---+ +---+
805 But it will not reset the update back to normal. Only the writer
806 that converted a pointer from HEAD to UPDATE will convert it back
812 +---+ +---+ +---+ +---+
813 <---| |--->| |-U->| |-H->| |--->
814 --->| |<---| |<---| |<---| |<---
815 +---+ +---+ +---+ +---+
817 After the nested writer finishes, the outermost writer will convert
818 the UPDATE pointer to NORMAL::
824 +---+ +---+ +---+ +---+
825 <---| |--->| |--->| |-H->| |--->
826 --->| |<---| |<---| |<---| |<---
827 +---+ +---+ +---+ +---+
830 It can be even more complex if several nested writes came in and moved
831 the tail page ahead several pages::
839 +---+ +---+ +---+ +---+
840 <---| |--->| |-H->| |--->| |--->
841 --->| |<---| |<---| |<---| |<---
842 +---+ +---+ +---+ +---+
844 The write converts the head page pointer to UPDATE::
849 +---+ +---+ +---+ +---+
850 <---| |--->| |-U->| |--->| |--->
851 --->| |<---| |<---| |<---| |<---
852 +---+ +---+ +---+ +---+
854 Next writer comes in, and sees the update and sets up the new
862 +---+ +---+ +---+ +---+
863 <---| |--->| |-U->| |-H->| |--->
864 --->| |<---| |<---| |<---| |<---
865 +---+ +---+ +---+ +---+
867 The nested writer moves the tail page forward. But does not set the old
868 update page to NORMAL because it is not the outermost writer::
873 +---+ +---+ +---+ +---+
874 <---| |--->| |-U->| |-H->| |--->
875 --->| |<---| |<---| |<---| |<---
876 +---+ +---+ +---+ +---+
878 Another writer preempts and sees the page after the tail page is a head page.
879 It changes it from HEAD to UPDATE::
886 +---+ +---+ +---+ +---+
887 <---| |--->| |-U->| |-U->| |--->
888 --->| |<---| |<---| |<---| |<---
889 +---+ +---+ +---+ +---+
891 The writer will move the head page forward::
899 +---+ +---+ +---+ +---+
900 <---| |--->| |-U->| |-U->| |-H->
901 --->| |<---| |<---| |<---| |<---
902 +---+ +---+ +---+ +---+
904 But now that the third writer did change the HEAD flag to UPDATE it
905 will convert it to normal::
913 +---+ +---+ +---+ +---+
914 <---| |--->| |-U->| |--->| |-H->
915 --->| |<---| |<---| |<---| |<---
916 +---+ +---+ +---+ +---+
919 Then it will move the tail page, and return back to the second writer::
927 +---+ +---+ +---+ +---+
928 <---| |--->| |-U->| |--->| |-H->
929 --->| |<---| |<---| |<---| |<---
930 +---+ +---+ +---+ +---+
933 The second writer will fail to move the tail page because it was already
934 moved, so it will try again and add its data to the new tail page.
935 It will return to the first writer::
943 +---+ +---+ +---+ +---+
944 <---| |--->| |-U->| |--->| |-H->
945 --->| |<---| |<---| |<---| |<---
946 +---+ +---+ +---+ +---+
948 The first writer cannot know atomically if the tail page moved
949 while it updates the HEAD page. It will then update the head page to
950 what it thinks is the new head page::
958 +---+ +---+ +---+ +---+
959 <---| |--->| |-U->| |-H->| |-H->
960 --->| |<---| |<---| |<---| |<---
961 +---+ +---+ +---+ +---+
963 Since the cmpxchg returns the old value of the pointer the first writer
964 will see it succeeded in updating the pointer from NORMAL to HEAD.
965 But as we can see, this is not good enough. It must also check to see
966 if the tail page is either where it use to be or on the next page::
974 +---+ +---+ +---+ +---+
975 <---| |--->| |-U->| |-H->| |-H->
976 --->| |<---| |<---| |<---| |<---
977 +---+ +---+ +---+ +---+
979 If tail page != A and tail page != B, then it must reset the pointer
980 back to NORMAL. The fact that it only needs to worry about nested
981 writers means that it only needs to check this after setting the HEAD page::
989 +---+ +---+ +---+ +---+
990 <---| |--->| |-U->| |--->| |-H->
991 --->| |<---| |<---| |<---| |<---
992 +---+ +---+ +---+ +---+
994 Now the writer can update the head page. This is also why the head page must
995 remain in UPDATE and only reset by the outermost writer. This prevents
996 the reader from seeing the incorrect head page::
1004 +---+ +---+ +---+ +---+
1005 <---| |--->| |--->| |--->| |-H->
1006 --->| |<---| |<---| |<---| |<---
1007 +---+ +---+ +---+ +---+