ring-buffer-design.rst 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  1. .. SPDX-License-Identifier: GPL-2.0 OR GFDL-1.2-no-invariants-only
  2. ===========================
  3. Lockless Ring Buffer Design
  4. ===========================
  5. Copyright 2009 Red Hat Inc.
  6. :Author: Steven Rostedt <srostedt@redhat.com>
  7. :License: The GNU Free Documentation License, Version 1.2
  8. (dual licensed under the GPL v2)
  9. :Reviewers: Mathieu Desnoyers, Huang Ying, Hidetoshi Seto,
  10. and Frederic Weisbecker.
  11. Written for: 2.6.31
  12. Terminology used in this Document
  13. ---------------------------------
  14. tail
  15. - where new writes happen in the ring buffer.
  16. head
  17. - where new reads happen in the ring buffer.
  18. producer
  19. - the task that writes into the ring buffer (same as writer)
  20. writer
  21. - same as producer
  22. consumer
  23. - the task that reads from the buffer (same as reader)
  24. reader
  25. - same as consumer.
  26. reader_page
  27. - A page outside the ring buffer used solely (for the most part)
  28. by the reader.
  29. head_page
  30. - a pointer to the page that the reader will use next
  31. tail_page
  32. - a pointer to the page that will be written to next
  33. commit_page
  34. - a pointer to the page with the last finished non-nested write.
  35. cmpxchg
  36. - hardware-assisted atomic transaction that performs the following::
  37. A = B if previous A == C
  38. R = cmpxchg(A, C, B) is saying that we replace A with B if and only
  39. if current A is equal to C, and we put the old (current)
  40. A into R
  41. R gets the previous A regardless if A is updated with B or not.
  42. To see if the update was successful a compare of ``R == C``
  43. may be used.
  44. The Generic Ring Buffer
  45. -----------------------
  46. The ring buffer can be used in either an overwrite mode or in
  47. producer/consumer mode.
  48. Producer/consumer mode is where if the producer were to fill up the
  49. buffer before the consumer could free up anything, the producer
  50. will stop writing to the buffer. This will lose most recent events.
  51. Overwrite mode is where if the producer were to fill up the buffer
  52. before the consumer could free up anything, the producer will
  53. overwrite the older data. This will lose the oldest events.
  54. No two writers can write at the same time (on the same per-cpu buffer),
  55. but a writer may interrupt another writer, but it must finish writing
  56. before the previous writer may continue. This is very important to the
  57. algorithm. The writers act like a "stack". The way interrupts works
  58. enforces this behavior::
  59. writer1 start
  60. <preempted> writer2 start
  61. <preempted> writer3 start
  62. writer3 finishes
  63. writer2 finishes
  64. writer1 finishes
  65. This is very much like a writer being preempted by an interrupt and
  66. the interrupt doing a write as well.
  67. Readers can happen at any time. But no two readers may run at the
  68. same time, nor can a reader preempt/interrupt another reader. A reader
  69. cannot preempt/interrupt a writer, but it may read/consume from the
  70. buffer at the same time as a writer is writing, but the reader must be
  71. on another processor to do so. A reader may read on its own processor
  72. and can be preempted by a writer.
  73. A writer can preempt a reader, but a reader cannot preempt a writer.
  74. But a reader can read the buffer at the same time (on another processor)
  75. as a writer.
  76. The ring buffer is made up of a list of pages held together by a linked list.
  77. At initialization a reader page is allocated for the reader that is not
  78. part of the ring buffer.
  79. The head_page, tail_page and commit_page are all initialized to point
  80. to the same page.
  81. The reader page is initialized to have its next pointer pointing to
  82. the head page, and its previous pointer pointing to a page before
  83. the head page.
  84. The reader has its own page to use. At start up time, this page is
  85. allocated but is not attached to the list. When the reader wants
  86. to read from the buffer, if its page is empty (like it is on start-up),
  87. it will swap its page with the head_page. The old reader page will
  88. become part of the ring buffer and the head_page will be removed.
  89. The page after the inserted page (old reader_page) will become the
  90. new head page.
  91. Once the new page is given to the reader, the reader could do what
  92. it wants with it, as long as a writer has left that page.
  93. A sample of how the reader page is swapped: Note this does not
  94. show the head page in the buffer, it is for demonstrating a swap
  95. only.
  96. ::
  97. +------+
  98. |reader| RING BUFFER
  99. |page |
  100. +------+
  101. +---+ +---+ +---+
  102. | |-->| |-->| |
  103. | |<--| |<--| |
  104. +---+ +---+ +---+
  105. ^ | ^ |
  106. | +-------------+ |
  107. +-----------------+
  108. +------+
  109. |reader| RING BUFFER
  110. |page |-------------------+
  111. +------+ v
  112. | +---+ +---+ +---+
  113. | | |-->| |-->| |
  114. | | |<--| |<--| |<-+
  115. | +---+ +---+ +---+ |
  116. | ^ | ^ | |
  117. | | +-------------+ | |
  118. | +-----------------+ |
  119. +------------------------------------+
  120. +------+
  121. |reader| RING BUFFER
  122. |page |-------------------+
  123. +------+ <---------------+ v
  124. | ^ +---+ +---+ +---+
  125. | | | |-->| |-->| |
  126. | | | | | |<--| |<-+
  127. | | +---+ +---+ +---+ |
  128. | | | ^ | |
  129. | | +-------------+ | |
  130. | +-----------------------------+ |
  131. +------------------------------------+
  132. +------+
  133. |buffer| RING BUFFER
  134. |page |-------------------+
  135. +------+ <---------------+ v
  136. | ^ +---+ +---+ +---+
  137. | | | | | |-->| |
  138. | | New | | | |<--| |<-+
  139. | | Reader +---+ +---+ +---+ |
  140. | | page ----^ | |
  141. | | | |
  142. | +-----------------------------+ |
  143. +------------------------------------+
  144. It is possible that the page swapped is the commit page and the tail page,
  145. if what is in the ring buffer is less than what is held in a buffer page.
  146. ::
  147. reader page commit page tail page
  148. | | |
  149. v | |
  150. +---+ | |
  151. | |<----------+ |
  152. | |<------------------------+
  153. | |------+
  154. +---+ |
  155. |
  156. v
  157. +---+ +---+ +---+ +---+
  158. <---| |--->| |--->| |--->| |--->
  159. --->| |<---| |<---| |<---| |<---
  160. +---+ +---+ +---+ +---+
  161. This case is still valid for this algorithm.
  162. When the writer leaves the page, it simply goes into the ring buffer
  163. since the reader page still points to the next location in the ring
  164. buffer.
  165. The main pointers:
  166. reader page
  167. - The page used solely by the reader and is not part
  168. of the ring buffer (may be swapped in)
  169. head page
  170. - the next page in the ring buffer that will be swapped
  171. with the reader page.
  172. tail page
  173. - the page where the next write will take place.
  174. commit page
  175. - the page that last finished a write.
  176. The commit page only is updated by the outermost writer in the
  177. writer stack. A writer that preempts another writer will not move the
  178. commit page.
  179. When data is written into the ring buffer, a position is reserved
  180. in the ring buffer and passed back to the writer. When the writer
  181. is finished writing data into that position, it commits the write.
  182. Another write (or a read) may take place at anytime during this
  183. transaction. If another write happens it must finish before continuing
  184. with the previous write.
  185. Write reserve::
  186. Buffer page
  187. +---------+
  188. |written |
  189. +---------+ <--- given back to writer (current commit)
  190. |reserved |
  191. +---------+ <--- tail pointer
  192. | empty |
  193. +---------+
  194. Write commit::
  195. Buffer page
  196. +---------+
  197. |written |
  198. +---------+
  199. |written |
  200. +---------+ <--- next position for write (current commit)
  201. | empty |
  202. +---------+
  203. If a write happens after the first reserve::
  204. Buffer page
  205. +---------+
  206. |written |
  207. +---------+ <-- current commit
  208. |reserved |
  209. +---------+ <--- given back to second writer
  210. |reserved |
  211. +---------+ <--- tail pointer
  212. After second writer commits::
  213. Buffer page
  214. +---------+
  215. |written |
  216. +---------+ <--(last full commit)
  217. |reserved |
  218. +---------+
  219. |pending |
  220. |commit |
  221. +---------+ <--- tail pointer
  222. When the first writer commits::
  223. Buffer page
  224. +---------+
  225. |written |
  226. +---------+
  227. |written |
  228. +---------+
  229. |written |
  230. +---------+ <--(last full commit and tail pointer)
  231. The commit pointer points to the last write location that was
  232. committed without preempting another write. When a write that
  233. preempted another write is committed, it only becomes a pending commit
  234. and will not be a full commit until all writes have been committed.
  235. The commit page points to the page that has the last full commit.
  236. The tail page points to the page with the last write (before
  237. committing).
  238. The tail page is always equal to or after the commit page. It may
  239. be several pages ahead. If the tail page catches up to the commit
  240. page then no more writes may take place (regardless of the mode
  241. of the ring buffer: overwrite and produce/consumer).
  242. The order of pages is::
  243. head page
  244. commit page
  245. tail page
  246. Possible scenario::
  247. tail page
  248. head page commit page |
  249. | | |
  250. v v v
  251. +---+ +---+ +---+ +---+
  252. <---| |--->| |--->| |--->| |--->
  253. --->| |<---| |<---| |<---| |<---
  254. +---+ +---+ +---+ +---+
  255. There is a special case that the head page is after either the commit page
  256. and possibly the tail page. That is when the commit (and tail) page has been
  257. swapped with the reader page. This is because the head page is always
  258. part of the ring buffer, but the reader page is not. Whenever there
  259. has been less than a full page that has been committed inside the ring buffer,
  260. and a reader swaps out a page, it will be swapping out the commit page.
  261. ::
  262. reader page commit page tail page
  263. | | |
  264. v | |
  265. +---+ | |
  266. | |<----------+ |
  267. | |<------------------------+
  268. | |------+
  269. +---+ |
  270. |
  271. v
  272. +---+ +---+ +---+ +---+
  273. <---| |--->| |--->| |--->| |--->
  274. --->| |<---| |<---| |<---| |<---
  275. +---+ +---+ +---+ +---+
  276. ^
  277. |
  278. head page
  279. In this case, the head page will not move when the tail and commit
  280. move back into the ring buffer.
  281. The reader cannot swap a page into the ring buffer if the commit page
  282. is still on that page. If the read meets the last commit (real commit
  283. not pending or reserved), then there is nothing more to read.
  284. The buffer is considered empty until another full commit finishes.
  285. When the tail meets the head page, if the buffer is in overwrite mode,
  286. the head page will be pushed ahead one. If the buffer is in producer/consumer
  287. mode, the write will fail.
  288. Overwrite mode::
  289. tail page
  290. |
  291. v
  292. +---+ +---+ +---+ +---+
  293. <---| |--->| |--->| |--->| |--->
  294. --->| |<---| |<---| |<---| |<---
  295. +---+ +---+ +---+ +---+
  296. ^
  297. |
  298. head page
  299. tail page
  300. |
  301. v
  302. +---+ +---+ +---+ +---+
  303. <---| |--->| |--->| |--->| |--->
  304. --->| |<---| |<---| |<---| |<---
  305. +---+ +---+ +---+ +---+
  306. ^
  307. |
  308. head page
  309. tail page
  310. |
  311. v
  312. +---+ +---+ +---+ +---+
  313. <---| |--->| |--->| |--->| |--->
  314. --->| |<---| |<---| |<---| |<---
  315. +---+ +---+ +---+ +---+
  316. ^
  317. |
  318. head page
  319. Note, the reader page will still point to the previous head page.
  320. But when a swap takes place, it will use the most recent head page.
  321. Making the Ring Buffer Lockless:
  322. --------------------------------
  323. The main idea behind the lockless algorithm is to combine the moving
  324. of the head_page pointer with the swapping of pages with the reader.
  325. State flags are placed inside the pointer to the page. To do this,
  326. each page must be aligned in memory by 4 bytes. This will allow the 2
  327. least significant bits of the address to be used as flags, since
  328. they will always be zero for the address. To get the address,
  329. simply mask out the flags::
  330. MASK = ~3
  331. address & MASK
  332. Two flags will be kept by these two bits:
  333. HEADER
  334. - the page being pointed to is a head page
  335. UPDATE
  336. - the page being pointed to is being updated by a writer
  337. and was or is about to be a head page.
  338. ::
  339. reader page
  340. |
  341. v
  342. +---+
  343. | |------+
  344. +---+ |
  345. |
  346. v
  347. +---+ +---+ +---+ +---+
  348. <---| |--->| |-H->| |--->| |--->
  349. --->| |<---| |<---| |<---| |<---
  350. +---+ +---+ +---+ +---+
  351. The above pointer "-H->" would have the HEADER flag set. That is
  352. the next page is the next page to be swapped out by the reader.
  353. This pointer means the next page is the head page.
  354. When the tail page meets the head pointer, it will use cmpxchg to
  355. change the pointer to the UPDATE state::
  356. tail page
  357. |
  358. v
  359. +---+ +---+ +---+ +---+
  360. <---| |--->| |-H->| |--->| |--->
  361. --->| |<---| |<---| |<---| |<---
  362. +---+ +---+ +---+ +---+
  363. tail page
  364. |
  365. v
  366. +---+ +---+ +---+ +---+
  367. <---| |--->| |-U->| |--->| |--->
  368. --->| |<---| |<---| |<---| |<---
  369. +---+ +---+ +---+ +---+
  370. "-U->" represents a pointer in the UPDATE state.
  371. Any access to the reader will need to take some sort of lock to serialize
  372. the readers. But the writers will never take a lock to write to the
  373. ring buffer. This means we only need to worry about a single reader,
  374. and writes only preempt in "stack" formation.
  375. When the reader tries to swap the page with the ring buffer, it
  376. will also use cmpxchg. If the flag bit in the pointer to the
  377. head page does not have the HEADER flag set, the compare will fail
  378. and the reader will need to look for the new head page and try again.
  379. Note, the flags UPDATE and HEADER are never set at the same time.
  380. The reader swaps the reader page as follows::
  381. +------+
  382. |reader| RING BUFFER
  383. |page |
  384. +------+
  385. +---+ +---+ +---+
  386. | |--->| |--->| |
  387. | |<---| |<---| |
  388. +---+ +---+ +---+
  389. ^ | ^ |
  390. | +---------------+ |
  391. +-----H-------------+
  392. The reader sets the reader page next pointer as HEADER to the page after
  393. the head page::
  394. +------+
  395. |reader| RING BUFFER
  396. |page |-------H-----------+
  397. +------+ v
  398. | +---+ +---+ +---+
  399. | | |--->| |--->| |
  400. | | |<---| |<---| |<-+
  401. | +---+ +---+ +---+ |
  402. | ^ | ^ | |
  403. | | +---------------+ | |
  404. | +-----H-------------+ |
  405. +--------------------------------------+
  406. It does a cmpxchg with the pointer to the previous head page to make it
  407. point to the reader page. Note that the new pointer does not have the HEADER
  408. flag set. This action atomically moves the head page forward::
  409. +------+
  410. |reader| RING BUFFER
  411. |page |-------H-----------+
  412. +------+ v
  413. | ^ +---+ +---+ +---+
  414. | | | |-->| |-->| |
  415. | | | |<--| |<--| |<-+
  416. | | +---+ +---+ +---+ |
  417. | | | ^ | |
  418. | | +-------------+ | |
  419. | +-----------------------------+ |
  420. +------------------------------------+
  421. After the new head page is set, the previous pointer of the head page is
  422. updated to the reader page::
  423. +------+
  424. |reader| RING BUFFER
  425. |page |-------H-----------+
  426. +------+ <---------------+ v
  427. | ^ +---+ +---+ +---+
  428. | | | |-->| |-->| |
  429. | | | | | |<--| |<-+
  430. | | +---+ +---+ +---+ |
  431. | | | ^ | |
  432. | | +-------------+ | |
  433. | +-----------------------------+ |
  434. +------------------------------------+
  435. +------+
  436. |buffer| RING BUFFER
  437. |page |-------H-----------+ <--- New head page
  438. +------+ <---------------+ v
  439. | ^ +---+ +---+ +---+
  440. | | | | | |-->| |
  441. | | New | | | |<--| |<-+
  442. | | Reader +---+ +---+ +---+ |
  443. | | page ----^ | |
  444. | | | |
  445. | +-----------------------------+ |
  446. +------------------------------------+
  447. Another important point: The page that the reader page points back to
  448. by its previous pointer (the one that now points to the new head page)
  449. never points back to the reader page. That is because the reader page is
  450. not part of the ring buffer. Traversing the ring buffer via the next pointers
  451. will always stay in the ring buffer. Traversing the ring buffer via the
  452. prev pointers may not.
  453. Note, the way to determine a reader page is simply by examining the previous
  454. pointer of the page. If the next pointer of the previous page does not
  455. point back to the original page, then the original page is a reader page::
  456. +--------+
  457. | reader | next +----+
  458. | page |-------->| |<====== (buffer page)
  459. +--------+ +----+
  460. | | ^
  461. | v | next
  462. prev | +----+
  463. +------------->| |
  464. +----+
  465. The way the head page moves forward:
  466. When the tail page meets the head page and the buffer is in overwrite mode
  467. and more writes take place, the head page must be moved forward before the
  468. writer may move the tail page. The way this is done is that the writer
  469. performs a cmpxchg to convert the pointer to the head page from the HEADER
  470. flag to have the UPDATE flag set. Once this is done, the reader will
  471. not be able to swap the head page from the buffer, nor will it be able to
  472. move the head page, until the writer is finished with the move.
  473. This eliminates any races that the reader can have on the writer. The reader
  474. must spin, and this is why the reader cannot preempt the writer::
  475. tail page
  476. |
  477. v
  478. +---+ +---+ +---+ +---+
  479. <---| |--->| |-H->| |--->| |--->
  480. --->| |<---| |<---| |<---| |<---
  481. +---+ +---+ +---+ +---+
  482. tail page
  483. |
  484. v
  485. +---+ +---+ +---+ +---+
  486. <---| |--->| |-U->| |--->| |--->
  487. --->| |<---| |<---| |<---| |<---
  488. +---+ +---+ +---+ +---+
  489. The following page will be made into the new head page::
  490. tail page
  491. |
  492. v
  493. +---+ +---+ +---+ +---+
  494. <---| |--->| |-U->| |-H->| |--->
  495. --->| |<---| |<---| |<---| |<---
  496. +---+ +---+ +---+ +---+
  497. After the new head page has been set, we can set the old head page
  498. pointer back to NORMAL::
  499. tail page
  500. |
  501. v
  502. +---+ +---+ +---+ +---+
  503. <---| |--->| |--->| |-H->| |--->
  504. --->| |<---| |<---| |<---| |<---
  505. +---+ +---+ +---+ +---+
  506. After the head page has been moved, the tail page may now move forward::
  507. tail page
  508. |
  509. v
  510. +---+ +---+ +---+ +---+
  511. <---| |--->| |--->| |-H->| |--->
  512. --->| |<---| |<---| |<---| |<---
  513. +---+ +---+ +---+ +---+
  514. The above are the trivial updates. Now for the more complex scenarios.
  515. As stated before, if enough writes preempt the first write, the
  516. tail page may make it all the way around the buffer and meet the commit
  517. page. At this time, we must start dropping writes (usually with some kind
  518. of warning to the user). But what happens if the commit was still on the
  519. reader page? The commit page is not part of the ring buffer. The tail page
  520. must account for this::
  521. reader page commit page
  522. | |
  523. v |
  524. +---+ |
  525. | |<----------+
  526. | |
  527. | |------+
  528. +---+ |
  529. |
  530. v
  531. +---+ +---+ +---+ +---+
  532. <---| |--->| |-H->| |--->| |--->
  533. --->| |<---| |<---| |<---| |<---
  534. +---+ +---+ +---+ +---+
  535. ^
  536. |
  537. tail page
  538. If the tail page were to simply push the head page forward, the commit when
  539. leaving the reader page would not be pointing to the correct page.
  540. The solution to this is to test if the commit page is on the reader page
  541. before pushing the head page. If it is, then it can be assumed that the
  542. tail page wrapped the buffer, and we must drop new writes.
  543. This is not a race condition, because the commit page can only be moved
  544. by the outermost writer (the writer that was preempted).
  545. This means that the commit will not move while a writer is moving the
  546. tail page. The reader cannot swap the reader page if it is also being
  547. used as the commit page. The reader can simply check that the commit
  548. is off the reader page. Once the commit page leaves the reader page
  549. it will never go back on it unless a reader does another swap with the
  550. buffer page that is also the commit page.
  551. Nested writes
  552. -------------
  553. In the pushing forward of the tail page we must first push forward
  554. the head page if the head page is the next page. If the head page
  555. is not the next page, the tail page is simply updated with a cmpxchg.
  556. Only writers move the tail page. This must be done atomically to protect
  557. against nested writers::
  558. temp_page = tail_page
  559. next_page = temp_page->next
  560. cmpxchg(tail_page, temp_page, next_page)
  561. The above will update the tail page if it is still pointing to the expected
  562. page. If this fails, a nested write pushed it forward, the current write
  563. does not need to push it::
  564. temp page
  565. |
  566. v
  567. tail page
  568. |
  569. v
  570. +---+ +---+ +---+ +---+
  571. <---| |--->| |--->| |--->| |--->
  572. --->| |<---| |<---| |<---| |<---
  573. +---+ +---+ +---+ +---+
  574. Nested write comes in and moves the tail page forward::
  575. tail page (moved by nested writer)
  576. temp page |
  577. | |
  578. v v
  579. +---+ +---+ +---+ +---+
  580. <---| |--->| |--->| |--->| |--->
  581. --->| |<---| |<---| |<---| |<---
  582. +---+ +---+ +---+ +---+
  583. The above would fail the cmpxchg, but since the tail page has already
  584. been moved forward, the writer will just try again to reserve storage
  585. on the new tail page.
  586. But the moving of the head page is a bit more complex::
  587. tail page
  588. |
  589. v
  590. +---+ +---+ +---+ +---+
  591. <---| |--->| |-H->| |--->| |--->
  592. --->| |<---| |<---| |<---| |<---
  593. +---+ +---+ +---+ +---+
  594. The write converts the head page pointer to UPDATE::
  595. tail page
  596. |
  597. v
  598. +---+ +---+ +---+ +---+
  599. <---| |--->| |-U->| |--->| |--->
  600. --->| |<---| |<---| |<---| |<---
  601. +---+ +---+ +---+ +---+
  602. But if a nested writer preempts here, it will see that the next
  603. page is a head page, but it is also nested. It will detect that
  604. it is nested and will save that information. The detection is the
  605. fact that it sees the UPDATE flag instead of a HEADER or NORMAL
  606. pointer.
  607. The nested writer will set the new head page pointer::
  608. tail page
  609. |
  610. v
  611. +---+ +---+ +---+ +---+
  612. <---| |--->| |-U->| |-H->| |--->
  613. --->| |<---| |<---| |<---| |<---
  614. +---+ +---+ +---+ +---+
  615. But it will not reset the update back to normal. Only the writer
  616. that converted a pointer from HEAD to UPDATE will convert it back
  617. to NORMAL::
  618. tail page
  619. |
  620. v
  621. +---+ +---+ +---+ +---+
  622. <---| |--->| |-U->| |-H->| |--->
  623. --->| |<---| |<---| |<---| |<---
  624. +---+ +---+ +---+ +---+
  625. After the nested writer finishes, the outermost writer will convert
  626. the UPDATE pointer to NORMAL::
  627. tail page
  628. |
  629. v
  630. +---+ +---+ +---+ +---+
  631. <---| |--->| |--->| |-H->| |--->
  632. --->| |<---| |<---| |<---| |<---
  633. +---+ +---+ +---+ +---+
  634. It can be even more complex if several nested writes came in and moved
  635. the tail page ahead several pages::
  636. (first writer)
  637. tail page
  638. |
  639. v
  640. +---+ +---+ +---+ +---+
  641. <---| |--->| |-H->| |--->| |--->
  642. --->| |<---| |<---| |<---| |<---
  643. +---+ +---+ +---+ +---+
  644. The write converts the head page pointer to UPDATE::
  645. tail page
  646. |
  647. v
  648. +---+ +---+ +---+ +---+
  649. <---| |--->| |-U->| |--->| |--->
  650. --->| |<---| |<---| |<---| |<---
  651. +---+ +---+ +---+ +---+
  652. Next writer comes in, and sees the update and sets up the new
  653. head page::
  654. (second writer)
  655. tail page
  656. |
  657. v
  658. +---+ +---+ +---+ +---+
  659. <---| |--->| |-U->| |-H->| |--->
  660. --->| |<---| |<---| |<---| |<---
  661. +---+ +---+ +---+ +---+
  662. The nested writer moves the tail page forward. But does not set the old
  663. update page to NORMAL because it is not the outermost writer::
  664. tail page
  665. |
  666. v
  667. +---+ +---+ +---+ +---+
  668. <---| |--->| |-U->| |-H->| |--->
  669. --->| |<---| |<---| |<---| |<---
  670. +---+ +---+ +---+ +---+
  671. Another writer preempts and sees the page after the tail page is a head page.
  672. It changes it from HEAD to UPDATE::
  673. (third writer)
  674. tail page
  675. |
  676. v
  677. +---+ +---+ +---+ +---+
  678. <---| |--->| |-U->| |-U->| |--->
  679. --->| |<---| |<---| |<---| |<---
  680. +---+ +---+ +---+ +---+
  681. The writer will move the head page forward::
  682. (third writer)
  683. tail page
  684. |
  685. v
  686. +---+ +---+ +---+ +---+
  687. <---| |--->| |-U->| |-U->| |-H->
  688. --->| |<---| |<---| |<---| |<---
  689. +---+ +---+ +---+ +---+
  690. But now that the third writer did change the HEAD flag to UPDATE it
  691. will convert it to normal::
  692. (third writer)
  693. tail page
  694. |
  695. v
  696. +---+ +---+ +---+ +---+
  697. <---| |--->| |-U->| |--->| |-H->
  698. --->| |<---| |<---| |<---| |<---
  699. +---+ +---+ +---+ +---+
  700. Then it will move the tail page, and return back to the second writer::
  701. (second writer)
  702. tail page
  703. |
  704. v
  705. +---+ +---+ +---+ +---+
  706. <---| |--->| |-U->| |--->| |-H->
  707. --->| |<---| |<---| |<---| |<---
  708. +---+ +---+ +---+ +---+
  709. The second writer will fail to move the tail page because it was already
  710. moved, so it will try again and add its data to the new tail page.
  711. It will return to the first writer::
  712. (first writer)
  713. tail page
  714. |
  715. v
  716. +---+ +---+ +---+ +---+
  717. <---| |--->| |-U->| |--->| |-H->
  718. --->| |<---| |<---| |<---| |<---
  719. +---+ +---+ +---+ +---+
  720. The first writer cannot know atomically if the tail page moved
  721. while it updates the HEAD page. It will then update the head page to
  722. what it thinks is the new head page::
  723. (first writer)
  724. tail page
  725. |
  726. v
  727. +---+ +---+ +---+ +---+
  728. <---| |--->| |-U->| |-H->| |-H->
  729. --->| |<---| |<---| |<---| |<---
  730. +---+ +---+ +---+ +---+
  731. Since the cmpxchg returns the old value of the pointer the first writer
  732. will see it succeeded in updating the pointer from NORMAL to HEAD.
  733. But as we can see, this is not good enough. It must also check to see
  734. if the tail page is either where it use to be or on the next page::
  735. (first writer)
  736. A B tail page
  737. | | |
  738. v v v
  739. +---+ +---+ +---+ +---+
  740. <---| |--->| |-U->| |-H->| |-H->
  741. --->| |<---| |<---| |<---| |<---
  742. +---+ +---+ +---+ +---+
  743. If tail page != A and tail page != B, then it must reset the pointer
  744. back to NORMAL. The fact that it only needs to worry about nested
  745. writers means that it only needs to check this after setting the HEAD page::
  746. (first writer)
  747. A B tail page
  748. | | |
  749. v v v
  750. +---+ +---+ +---+ +---+
  751. <---| |--->| |-U->| |--->| |-H->
  752. --->| |<---| |<---| |<---| |<---
  753. +---+ +---+ +---+ +---+
  754. Now the writer can update the head page. This is also why the head page must
  755. remain in UPDATE and only reset by the outermost writer. This prevents
  756. the reader from seeing the incorrect head page::
  757. (first writer)
  758. A B tail page
  759. | | |
  760. v v v
  761. +---+ +---+ +---+ +---+
  762. <---| |--->| |--->| |--->| |-H->
  763. --->| |<---| |<---| |<---| |<---
  764. +---+ +---+ +---+ +---+