ring-buffer-design.txt 30 KB

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