1 .. SPDX-License-Identifier: GPL-2.0 OR GFDL-1. !! 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 >> 4 .. whole. >> 5 .. >> 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 >> 9 .. the License. >> 10 .. >> 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. >> 15 .. >> 16 .. Or, alternatively, >> 17 .. >> 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. >> 24 .. >> 25 .. TODO: replace it to GPL-2.0 OR GFDL-1.2 WITH no-invariant-sections 2 26 3 =========================== 27 =========================== 4 Lockless Ring Buffer Design 28 Lockless Ring Buffer Design 5 =========================== 29 =========================== 6 30 7 Copyright 2009 Red Hat Inc. 31 Copyright 2009 Red Hat Inc. 8 32 9 :Author: Steven Rostedt <srostedt@redhat.com> 33 :Author: Steven Rostedt <srostedt@redhat.com> 10 :License: The GNU Free Documentation License, 34 :License: The GNU Free Documentation License, Version 1.2 11 (dual licensed under the GPL v2) 35 (dual licensed under the GPL v2) 12 :Reviewers: Mathieu Desnoyers, Huang Ying, Hi 36 :Reviewers: Mathieu Desnoyers, Huang Ying, Hidetoshi Seto, 13 and Frederic Weisbecker. 37 and Frederic Weisbecker. 14 38 15 39 16 Written for: 2.6.31 40 Written for: 2.6.31 17 41 18 Terminology used in this Document 42 Terminology used in this Document 19 --------------------------------- 43 --------------------------------- 20 44 21 tail 45 tail 22 - where new writes happen in the ring 46 - where new writes happen in the ring buffer. 23 47 24 head 48 head 25 - where new reads happen in the ring b 49 - where new reads happen in the ring buffer. 26 50 27 producer 51 producer 28 - the task that writes into the ring b 52 - the task that writes into the ring buffer (same as writer) 29 53 30 writer 54 writer 31 - same as producer 55 - same as producer 32 56 33 consumer 57 consumer 34 - the task that reads from the buffer 58 - the task that reads from the buffer (same as reader) 35 59 36 reader 60 reader 37 - same as consumer. 61 - same as consumer. 38 62 39 reader_page 63 reader_page 40 - A page outside the ring buffer used 64 - A page outside the ring buffer used solely (for the most part) 41 by the reader. 65 by the reader. 42 66 43 head_page 67 head_page 44 - a pointer to the page that the reade 68 - a pointer to the page that the reader will use next 45 69 46 tail_page 70 tail_page 47 - a pointer to the page that will be w 71 - a pointer to the page that will be written to next 48 72 49 commit_page 73 commit_page 50 - a pointer to the page with the last 74 - a pointer to the page with the last finished non-nested write. 51 75 52 cmpxchg 76 cmpxchg 53 - hardware-assisted atomic transaction 77 - hardware-assisted atomic transaction that performs the following:: 54 78 55 A = B if previous A == C 79 A = B if previous A == C 56 80 57 R = cmpxchg(A, C, B) is saying tha 81 R = cmpxchg(A, C, B) is saying that we replace A with B if and only 58 if current A is equal to C, an 82 if current A is equal to C, and we put the old (current) 59 A into R 83 A into R 60 84 61 R gets the previous A regardless i 85 R gets the previous A regardless if A is updated with B or not. 62 86 63 To see if the update was successful 87 To see if the update was successful a compare of ``R == C`` 64 may be used. 88 may be used. 65 89 66 The Generic Ring Buffer 90 The Generic Ring Buffer 67 ----------------------- 91 ----------------------- 68 92 69 The ring buffer can be used in either an overw 93 The ring buffer can be used in either an overwrite mode or in 70 producer/consumer mode. 94 producer/consumer mode. 71 95 72 Producer/consumer mode is where if the produce 96 Producer/consumer mode is where if the producer were to fill up the 73 buffer before the consumer could free up anyth 97 buffer before the consumer could free up anything, the producer 74 will stop writing to the buffer. This will los 98 will stop writing to the buffer. This will lose most recent events. 75 99 76 Overwrite mode is where if the producer were t 100 Overwrite mode is where if the producer were to fill up the buffer 77 before the consumer could free up anything, th 101 before the consumer could free up anything, the producer will 78 overwrite the older data. This will lose the o 102 overwrite the older data. This will lose the oldest events. 79 103 80 No two writers can write at the same time (on 104 No two writers can write at the same time (on the same per-cpu buffer), 81 but a writer may interrupt another writer, but 105 but a writer may interrupt another writer, but it must finish writing 82 before the previous writer may continue. This 106 before the previous writer may continue. This is very important to the 83 algorithm. The writers act like a "stack". The 107 algorithm. The writers act like a "stack". The way interrupts works 84 enforces this behavior:: 108 enforces this behavior:: 85 109 86 110 87 writer1 start 111 writer1 start 88 <preempted> writer2 start 112 <preempted> writer2 start 89 <preempted> writer3 start 113 <preempted> writer3 start 90 writer3 finishes 114 writer3 finishes 91 writer2 finishes 115 writer2 finishes 92 writer1 finishes 116 writer1 finishes 93 117 94 This is very much like a writer being preempte 118 This is very much like a writer being preempted by an interrupt and 95 the interrupt doing a write as well. 119 the interrupt doing a write as well. 96 120 97 Readers can happen at any time. But no two rea 121 Readers can happen at any time. But no two readers may run at the 98 same time, nor can a reader preempt/interrupt 122 same time, nor can a reader preempt/interrupt another reader. A reader 99 cannot preempt/interrupt a writer, but it may 123 cannot preempt/interrupt a writer, but it may read/consume from the 100 buffer at the same time as a writer is writing 124 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 re 125 on another processor to do so. A reader may read on its own processor 102 and can be preempted by a writer. 126 and can be preempted by a writer. 103 127 104 A writer can preempt a reader, but a reader ca 128 A writer can preempt a reader, but a reader cannot preempt a writer. 105 But a reader can read the buffer at the same t 129 But a reader can read the buffer at the same time (on another processor) 106 as a writer. 130 as a writer. 107 131 108 The ring buffer is made up of a list of pages 132 The ring buffer is made up of a list of pages held together by a linked list. 109 133 110 At initialization a reader page is allocated f 134 At initialization a reader page is allocated for the reader that is not 111 part of the ring buffer. 135 part of the ring buffer. 112 136 113 The head_page, tail_page and commit_page are a 137 The head_page, tail_page and commit_page are all initialized to point 114 to the same page. 138 to the same page. 115 139 116 The reader page is initialized to have its nex 140 The reader page is initialized to have its next pointer pointing to 117 the head page, and its previous pointer pointi 141 the head page, and its previous pointer pointing to a page before 118 the head page. 142 the head page. 119 143 120 The reader has its own page to use. At start u 144 The reader has its own page to use. At start up time, this page is 121 allocated but is not attached to the list. Whe 145 allocated but is not attached to the list. When the reader wants 122 to read from the buffer, if its page is empty 146 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 147 it will swap its page with the head_page. The old reader page will 124 become part of the ring buffer and the head_pa 148 become part of the ring buffer and the head_page will be removed. 125 The page after the inserted page (old reader_p 149 The page after the inserted page (old reader_page) will become the 126 new head page. 150 new head page. 127 151 128 Once the new page is given to the reader, the 152 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 153 it wants with it, as long as a writer has left that page. 130 154 131 A sample of how the reader page is swapped: No 155 A sample of how the reader page is swapped: Note this does not 132 show the head page in the buffer, it is for de 156 show the head page in the buffer, it is for demonstrating a swap 133 only. 157 only. 134 158 135 :: 159 :: 136 160 137 +------+ 161 +------+ 138 |reader| RING BUFFER 162 |reader| RING BUFFER 139 |page | 163 |page | 140 +------+ 164 +------+ 141 +---+ +---+ +---+ 165 +---+ +---+ +---+ 142 | |-->| |-->| | 166 | |-->| |-->| | 143 | |<--| |<--| | 167 | |<--| |<--| | 144 +---+ +---+ +---+ 168 +---+ +---+ +---+ 145 ^ | ^ | 169 ^ | ^ | 146 | +-------------+ | 170 | +-------------+ | 147 +-----------------+ 171 +-----------------+ 148 172 149 173 150 +------+ 174 +------+ 151 |reader| RING BUFFER 175 |reader| RING BUFFER 152 |page |-------------------+ 176 |page |-------------------+ 153 +------+ v 177 +------+ v 154 | +---+ +---+ +---+ 178 | +---+ +---+ +---+ 155 | | |-->| |-->| | 179 | | |-->| |-->| | 156 | | |<--| |<--| |<-+ 180 | | |<--| |<--| |<-+ 157 | +---+ +---+ +---+ | 181 | +---+ +---+ +---+ | 158 | ^ | ^ | | 182 | ^ | ^ | | 159 | | +-------------+ | | 183 | | +-------------+ | | 160 | +-----------------+ | 184 | +-----------------+ | 161 +------------------------------------+ 185 +------------------------------------+ 162 186 163 +------+ 187 +------+ 164 |reader| RING BUFFER 188 |reader| RING BUFFER 165 |page |-------------------+ 189 |page |-------------------+ 166 +------+ <---------------+ v 190 +------+ <---------------+ v 167 | ^ +---+ +---+ +---+ 191 | ^ +---+ +---+ +---+ 168 | | | |-->| |-->| | 192 | | | |-->| |-->| | 169 | | | | | |<--| |<-+ 193 | | | | | |<--| |<-+ 170 | | +---+ +---+ +---+ | 194 | | +---+ +---+ +---+ | 171 | | | ^ | | 195 | | | ^ | | 172 | | +-------------+ | | 196 | | +-------------+ | | 173 | +-----------------------------+ | 197 | +-----------------------------+ | 174 +------------------------------------+ 198 +------------------------------------+ 175 199 176 +------+ 200 +------+ 177 |buffer| RING BUFFER 201 |buffer| RING BUFFER 178 |page |-------------------+ 202 |page |-------------------+ 179 +------+ <---------------+ v 203 +------+ <---------------+ v 180 | ^ +---+ +---+ +---+ 204 | ^ +---+ +---+ +---+ 181 | | | | | |-->| | 205 | | | | | |-->| | 182 | | New | | | |<--| |<-+ 206 | | New | | | |<--| |<-+ 183 | | Reader +---+ +---+ +---+ | 207 | | Reader +---+ +---+ +---+ | 184 | | page ----^ | | 208 | | page ----^ | | 185 | | | | 209 | | | | 186 | +-----------------------------+ | 210 | +-----------------------------+ | 187 +------------------------------------+ 211 +------------------------------------+ 188 212 189 213 190 214 191 It is possible that the page swapped is the co 215 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 wha 216 if what is in the ring buffer is less than what is held in a buffer page. 193 217 194 :: 218 :: 195 219 196 reader page commit page tail 220 reader page commit page tail page 197 | | | 221 | | | 198 v | | 222 v | | 199 +---+ | | 223 +---+ | | 200 | |<----------+ | 224 | |<----------+ | 201 | |<------------------------+ 225 | |<------------------------+ 202 | |------+ 226 | |------+ 203 +---+ | 227 +---+ | 204 | 228 | 205 v 229 v 206 +---+ +---+ +---+ +---+ 230 +---+ +---+ +---+ +---+ 207 <---| |--->| |--->| |--->| |---> 231 <---| |--->| |--->| |--->| |---> 208 --->| |<---| |<---| |<---| |<--- 232 --->| |<---| |<---| |<---| |<--- 209 +---+ +---+ +---+ +---+ 233 +---+ +---+ +---+ +---+ 210 234 211 This case is still valid for this algorithm. 235 This case is still valid for this algorithm. 212 When the writer leaves the page, it simply goe 236 When the writer leaves the page, it simply goes into the ring buffer 213 since the reader page still points to the next 237 since the reader page still points to the next location in the ring 214 buffer. 238 buffer. 215 239 216 240 217 The main pointers: 241 The main pointers: 218 242 219 reader page 243 reader page 220 - The page used solely by the read 244 - The page used solely by the reader and is not part 221 of the ring buffer (may be swapp 245 of the ring buffer (may be swapped in) 222 246 223 head page 247 head page 224 - the next page in the ring buffer 248 - the next page in the ring buffer that will be swapped 225 with the reader page. 249 with the reader page. 226 250 227 tail page 251 tail page 228 - the page where the next write wi 252 - the page where the next write will take place. 229 253 230 commit page 254 commit page 231 - the page that last finished a wr 255 - the page that last finished a write. 232 256 233 The commit page only is updated by the outermo 257 The commit page only is updated by the outermost writer in the 234 writer stack. A writer that preempts another w 258 writer stack. A writer that preempts another writer will not move the 235 commit page. 259 commit page. 236 260 237 When data is written into the ring buffer, a p 261 When data is written into the ring buffer, a position is reserved 238 in the ring buffer and passed back to the writ 262 in the ring buffer and passed back to the writer. When the writer 239 is finished writing data into that position, i 263 is finished writing data into that position, it commits the write. 240 264 241 Another write (or a read) may take place at an 265 Another write (or a read) may take place at anytime during this 242 transaction. If another write happens it must 266 transaction. If another write happens it must finish before continuing 243 with the previous write. 267 with the previous write. 244 268 245 269 246 Write reserve:: 270 Write reserve:: 247 271 248 Buffer page 272 Buffer page 249 +---------+ 273 +---------+ 250 |written | 274 |written | 251 +---------+ <--- given back to writer ( 275 +---------+ <--- given back to writer (current commit) 252 |reserved | 276 |reserved | 253 +---------+ <--- tail pointer 277 +---------+ <--- tail pointer 254 | empty | 278 | empty | 255 +---------+ 279 +---------+ 256 280 257 Write commit:: 281 Write commit:: 258 282 259 Buffer page 283 Buffer page 260 +---------+ 284 +---------+ 261 |written | 285 |written | 262 +---------+ 286 +---------+ 263 |written | 287 |written | 264 +---------+ <--- next position for writ 288 +---------+ <--- next position for write (current commit) 265 | empty | 289 | empty | 266 +---------+ 290 +---------+ 267 291 268 292 269 If a write happens after the first reserve:: 293 If a write happens after the first reserve:: 270 294 271 Buffer page 295 Buffer page 272 +---------+ 296 +---------+ 273 |written | 297 |written | 274 +---------+ <-- current commit 298 +---------+ <-- current commit 275 |reserved | 299 |reserved | 276 +---------+ <--- given back to second w 300 +---------+ <--- given back to second writer 277 |reserved | 301 |reserved | 278 +---------+ <--- tail pointer 302 +---------+ <--- tail pointer 279 303 280 After second writer commits:: 304 After second writer commits:: 281 305 282 306 283 Buffer page 307 Buffer page 284 +---------+ 308 +---------+ 285 |written | 309 |written | 286 +---------+ <--(last full commit) 310 +---------+ <--(last full commit) 287 |reserved | 311 |reserved | 288 +---------+ 312 +---------+ 289 |pending | 313 |pending | 290 |commit | 314 |commit | 291 +---------+ <--- tail pointer 315 +---------+ <--- tail pointer 292 316 293 When the first writer commits:: 317 When the first writer commits:: 294 318 295 Buffer page 319 Buffer page 296 +---------+ 320 +---------+ 297 |written | 321 |written | 298 +---------+ 322 +---------+ 299 |written | 323 |written | 300 +---------+ 324 +---------+ 301 |written | 325 |written | 302 +---------+ <--(last full commit and ta 326 +---------+ <--(last full commit and tail pointer) 303 327 304 328 305 The commit pointer points to the last write lo 329 The commit pointer points to the last write location that was 306 committed without preempting another write. Wh 330 committed without preempting another write. When a write that 307 preempted another write is committed, it only 331 preempted another write is committed, it only becomes a pending commit 308 and will not be a full commit until all writes 332 and will not be a full commit until all writes have been committed. 309 333 310 The commit page points to the page that has th 334 The commit page points to the page that has the last full commit. 311 The tail page points to the page with the last 335 The tail page points to the page with the last write (before 312 committing). 336 committing). 313 337 314 The tail page is always equal to or after the 338 The tail page is always equal to or after the commit page. It may 315 be several pages ahead. If the tail page catch 339 be several pages ahead. If the tail page catches up to the commit 316 page then no more writes may take place (regar 340 page then no more writes may take place (regardless of the mode 317 of the ring buffer: overwrite and produce/cons 341 of the ring buffer: overwrite and produce/consumer). 318 342 319 The order of pages is:: 343 The order of pages is:: 320 344 321 head page 345 head page 322 commit page 346 commit page 323 tail page 347 tail page 324 348 325 Possible scenario:: 349 Possible scenario:: 326 350 327 tail page 351 tail page 328 head page commit page | 352 head page commit page | 329 | | | 353 | | | 330 v v v 354 v v v 331 +---+ +---+ +---+ +---+ 355 +---+ +---+ +---+ +---+ 332 <---| |--->| |--->| |--->| |---> 356 <---| |--->| |--->| |--->| |---> 333 --->| |<---| |<---| |<---| |<--- 357 --->| |<---| |<---| |<---| |<--- 334 +---+ +---+ +---+ +---+ 358 +---+ +---+ +---+ +---+ 335 359 336 There is a special case that the head page is 360 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 c 361 and possibly the tail page. That is when the commit (and tail) page has been 338 swapped with the reader page. This is because 362 swapped with the reader page. This is because the head page is always 339 part of the ring buffer, but the reader page i 363 part of the ring buffer, but the reader page is not. Whenever there 340 has been less than a full page that has been c 364 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 swap 365 and a reader swaps out a page, it will be swapping out the commit page. 342 366 343 :: 367 :: 344 368 345 reader page commit page tail 369 reader page commit page tail page 346 | | | 370 | | | 347 v | | 371 v | | 348 +---+ | | 372 +---+ | | 349 | |<----------+ | 373 | |<----------+ | 350 | |<------------------------+ 374 | |<------------------------+ 351 | |------+ 375 | |------+ 352 +---+ | 376 +---+ | 353 | 377 | 354 v 378 v 355 +---+ +---+ +---+ +---+ 379 +---+ +---+ +---+ +---+ 356 <---| |--->| |--->| |--->| |---> 380 <---| |--->| |--->| |--->| |---> 357 --->| |<---| |<---| |<---| |<--- 381 --->| |<---| |<---| |<---| |<--- 358 +---+ +---+ +---+ +---+ 382 +---+ +---+ +---+ +---+ 359 ^ 383 ^ 360 | 384 | 361 head page 385 head page 362 386 363 387 364 In this case, the head page will not move when 388 In this case, the head page will not move when the tail and commit 365 move back into the ring buffer. 389 move back into the ring buffer. 366 390 367 The reader cannot swap a page into the ring bu 391 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 l 392 is still on that page. If the read meets the last commit (real commit 369 not pending or reserved), then there is nothin 393 not pending or reserved), then there is nothing more to read. 370 The buffer is considered empty until another f 394 The buffer is considered empty until another full commit finishes. 371 395 372 When the tail meets the head page, if the buff 396 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 397 the head page will be pushed ahead one. If the buffer is in producer/consumer 374 mode, the write will fail. 398 mode, the write will fail. 375 399 376 Overwrite mode:: 400 Overwrite mode:: 377 401 378 tail page 402 tail page 379 | 403 | 380 v 404 v 381 +---+ +---+ +---+ +---+ 405 +---+ +---+ +---+ +---+ 382 <---| |--->| |--->| |--->| |---> 406 <---| |--->| |--->| |--->| |---> 383 --->| |<---| |<---| |<---| |<--- 407 --->| |<---| |<---| |<---| |<--- 384 +---+ +---+ +---+ +---+ 408 +---+ +---+ +---+ +---+ 385 ^ 409 ^ 386 | 410 | 387 head page 411 head page 388 412 389 413 390 tail page 414 tail page 391 | 415 | 392 v 416 v 393 +---+ +---+ +---+ +---+ 417 +---+ +---+ +---+ +---+ 394 <---| |--->| |--->| |--->| |---> 418 <---| |--->| |--->| |--->| |---> 395 --->| |<---| |<---| |<---| |<--- 419 --->| |<---| |<---| |<---| |<--- 396 +---+ +---+ +---+ +---+ 420 +---+ +---+ +---+ +---+ 397 ^ 421 ^ 398 | 422 | 399 head page 423 head page 400 424 401 425 402 tail page 426 tail page 403 | 427 | 404 v 428 v 405 +---+ +---+ +---+ +---+ 429 +---+ +---+ +---+ +---+ 406 <---| |--->| |--->| |--->| |---> 430 <---| |--->| |--->| |--->| |---> 407 --->| |<---| |<---| |<---| |<--- 431 --->| |<---| |<---| |<---| |<--- 408 +---+ +---+ +---+ +---+ 432 +---+ +---+ +---+ +---+ 409 ^ 433 ^ 410 | 434 | 411 head page 435 head page 412 436 413 Note, the reader page will still point to the 437 Note, the reader page will still point to the previous head page. 414 But when a swap takes place, it will use the m 438 But when a swap takes place, it will use the most recent head page. 415 439 416 440 417 Making the Ring Buffer Lockless: 441 Making the Ring Buffer Lockless: 418 -------------------------------- 442 -------------------------------- 419 443 420 The main idea behind the lockless algorithm is 444 The main idea behind the lockless algorithm is to combine the moving 421 of the head_page pointer with the swapping of 445 of the head_page pointer with the swapping of pages with the reader. 422 State flags are placed inside the pointer to t 446 State flags are placed inside the pointer to the page. To do this, 423 each page must be aligned in memory by 4 bytes 447 each page must be aligned in memory by 4 bytes. This will allow the 2 424 least significant bits of the address to be us 448 least significant bits of the address to be used as flags, since 425 they will always be zero for the address. To g 449 they will always be zero for the address. To get the address, 426 simply mask out the flags:: 450 simply mask out the flags:: 427 451 428 MASK = ~3 452 MASK = ~3 429 453 430 address & MASK 454 address & MASK 431 455 432 Two flags will be kept by these two bits: 456 Two flags will be kept by these two bits: 433 457 434 HEADER 458 HEADER 435 - the page being pointed to is a head 459 - the page being pointed to is a head page 436 460 437 UPDATE 461 UPDATE 438 - the page being pointed to is being u 462 - the page being pointed to is being updated by a writer 439 and was or is about to be a head pag 463 and was or is about to be a head page. 440 464 441 :: 465 :: 442 466 443 reader page 467 reader page 444 | 468 | 445 v 469 v 446 +---+ 470 +---+ 447 | |------+ 471 | |------+ 448 +---+ | 472 +---+ | 449 | 473 | 450 v 474 v 451 +---+ +---+ +---+ +---+ 475 +---+ +---+ +---+ +---+ 452 <---| |--->| |-H->| |--->| |---> 476 <---| |--->| |-H->| |--->| |---> 453 --->| |<---| |<---| |<---| |<--- 477 --->| |<---| |<---| |<---| |<--- 454 +---+ +---+ +---+ +---+ 478 +---+ +---+ +---+ +---+ 455 479 456 480 457 The above pointer "-H->" would have the HEADER 481 The above pointer "-H->" would have the HEADER flag set. That is 458 the next page is the next page to be swapped o 482 the next page is the next page to be swapped out by the reader. 459 This pointer means the next page is the head p 483 This pointer means the next page is the head page. 460 484 461 When the tail page meets the head pointer, it 485 When the tail page meets the head pointer, it will use cmpxchg to 462 change the pointer to the UPDATE state:: 486 change the pointer to the UPDATE state:: 463 487 464 488 465 tail page 489 tail page 466 | 490 | 467 v 491 v 468 +---+ +---+ +---+ +---+ 492 +---+ +---+ +---+ +---+ 469 <---| |--->| |-H->| |--->| |---> 493 <---| |--->| |-H->| |--->| |---> 470 --->| |<---| |<---| |<---| |<--- 494 --->| |<---| |<---| |<---| |<--- 471 +---+ +---+ +---+ +---+ 495 +---+ +---+ +---+ +---+ 472 496 473 tail page 497 tail page 474 | 498 | 475 v 499 v 476 +---+ +---+ +---+ +---+ 500 +---+ +---+ +---+ +---+ 477 <---| |--->| |-U->| |--->| |---> 501 <---| |--->| |-U->| |--->| |---> 478 --->| |<---| |<---| |<---| |<--- 502 --->| |<---| |<---| |<---| |<--- 479 +---+ +---+ +---+ +---+ 503 +---+ +---+ +---+ +---+ 480 504 481 "-U->" represents a pointer in the UPDATE stat 505 "-U->" represents a pointer in the UPDATE state. 482 506 483 Any access to the reader will need to take som 507 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 508 the readers. But the writers will never take a lock to write to the 485 ring buffer. This means we only need to worry 509 ring buffer. This means we only need to worry about a single reader, 486 and writes only preempt in "stack" formation. 510 and writes only preempt in "stack" formation. 487 511 488 When the reader tries to swap the page with th 512 When the reader tries to swap the page with the ring buffer, it 489 will also use cmpxchg. If the flag bit in the 513 will also use cmpxchg. If the flag bit in the pointer to the 490 head page does not have the HEADER flag set, t 514 head page does not have the HEADER flag set, the compare will fail 491 and the reader will need to look for the new h 515 and the reader will need to look for the new head page and try again. 492 Note, the flags UPDATE and HEADER are never se 516 Note, the flags UPDATE and HEADER are never set at the same time. 493 517 494 The reader swaps the reader page as follows:: 518 The reader swaps the reader page as follows:: 495 519 496 +------+ 520 +------+ 497 |reader| RING BUFFER 521 |reader| RING BUFFER 498 |page | 522 |page | 499 +------+ 523 +------+ 500 +---+ +---+ +---+ 524 +---+ +---+ +---+ 501 | |--->| |--->| | 525 | |--->| |--->| | 502 | |<---| |<---| | 526 | |<---| |<---| | 503 +---+ +---+ +---+ 527 +---+ +---+ +---+ 504 ^ | ^ | 528 ^ | ^ | 505 | +---------------+ | 529 | +---------------+ | 506 +-----H-------------+ 530 +-----H-------------+ 507 531 508 The reader sets the reader page next pointer a 532 The reader sets the reader page next pointer as HEADER to the page after 509 the head page:: 533 the head page:: 510 534 511 535 512 +------+ 536 +------+ 513 |reader| RING BUFFER 537 |reader| RING BUFFER 514 |page |-------H-----------+ 538 |page |-------H-----------+ 515 +------+ v 539 +------+ v 516 | +---+ +---+ +---+ 540 | +---+ +---+ +---+ 517 | | |--->| |--->| | 541 | | |--->| |--->| | 518 | | |<---| |<---| |<-+ 542 | | |<---| |<---| |<-+ 519 | +---+ +---+ +---+ | 543 | +---+ +---+ +---+ | 520 | ^ | ^ | | 544 | ^ | ^ | | 521 | | +---------------+ | | 545 | | +---------------+ | | 522 | +-----H-------------+ | 546 | +-----H-------------+ | 523 +--------------------------------------+ 547 +--------------------------------------+ 524 548 525 It does a cmpxchg with the pointer to the prev 549 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 po 550 point to the reader page. Note that the new pointer does not have the HEADER 527 flag set. This action atomically moves the he 551 flag set. This action atomically moves the head page forward:: 528 552 529 +------+ 553 +------+ 530 |reader| RING BUFFER 554 |reader| RING BUFFER 531 |page |-------H-----------+ 555 |page |-------H-----------+ 532 +------+ v 556 +------+ v 533 | ^ +---+ +---+ +---+ 557 | ^ +---+ +---+ +---+ 534 | | | |-->| |-->| | 558 | | | |-->| |-->| | 535 | | | |<--| |<--| |<-+ 559 | | | |<--| |<--| |<-+ 536 | | +---+ +---+ +---+ | 560 | | +---+ +---+ +---+ | 537 | | | ^ | | 561 | | | ^ | | 538 | | +-------------+ | | 562 | | +-------------+ | | 539 | +-----------------------------+ | 563 | +-----------------------------+ | 540 +------------------------------------+ 564 +------------------------------------+ 541 565 542 After the new head page is set, the previous p 566 After the new head page is set, the previous pointer of the head page is 543 updated to the reader page:: 567 updated to the reader page:: 544 568 545 +------+ 569 +------+ 546 |reader| RING BUFFER 570 |reader| RING BUFFER 547 |page |-------H-----------+ 571 |page |-------H-----------+ 548 +------+ <---------------+ v 572 +------+ <---------------+ v 549 | ^ +---+ +---+ +---+ 573 | ^ +---+ +---+ +---+ 550 | | | |-->| |-->| | 574 | | | |-->| |-->| | 551 | | | | | |<--| |<-+ 575 | | | | | |<--| |<-+ 552 | | +---+ +---+ +---+ | 576 | | +---+ +---+ +---+ | 553 | | | ^ | | 577 | | | ^ | | 554 | | +-------------+ | | 578 | | +-------------+ | | 555 | +-----------------------------+ | 579 | +-----------------------------+ | 556 +------------------------------------+ 580 +------------------------------------+ 557 581 558 +------+ 582 +------+ 559 |buffer| RING BUFFER 583 |buffer| RING BUFFER 560 |page |-------H-----------+ <--- New head 584 |page |-------H-----------+ <--- New head page 561 +------+ <---------------+ v 585 +------+ <---------------+ v 562 | ^ +---+ +---+ +---+ 586 | ^ +---+ +---+ +---+ 563 | | | | | |-->| | 587 | | | | | |-->| | 564 | | New | | | |<--| |<-+ 588 | | New | | | |<--| |<-+ 565 | | Reader +---+ +---+ +---+ | 589 | | Reader +---+ +---+ +---+ | 566 | | page ----^ | | 590 | | page ----^ | | 567 | | | | 591 | | | | 568 | +-----------------------------+ | 592 | +-----------------------------+ | 569 +------------------------------------+ 593 +------------------------------------+ 570 594 571 Another important point: The page that the rea 595 Another important point: The page that the reader page points back to 572 by its previous pointer (the one that now poin 596 by its previous pointer (the one that now points to the new head page) 573 never points back to the reader page. That is 597 never points back to the reader page. That is because the reader page is 574 not part of the ring buffer. Traversing the ri 598 not part of the ring buffer. Traversing the ring buffer via the next pointers 575 will always stay in the ring buffer. Traversin 599 will always stay in the ring buffer. Traversing the ring buffer via the 576 prev pointers may not. 600 prev pointers may not. 577 601 578 Note, the way to determine a reader page is si 602 Note, the way to determine a reader page is simply by examining the previous 579 pointer of the page. If the next pointer of th 603 pointer of the page. If the next pointer of the previous page does not 580 point back to the original page, then the orig 604 point back to the original page, then the original page is a reader page:: 581 605 582 606 583 +--------+ 607 +--------+ 584 | reader | next +----+ 608 | reader | next +----+ 585 | page |-------->| |<====== 609 | page |-------->| |<====== (buffer page) 586 +--------+ +----+ 610 +--------+ +----+ 587 | | ^ 611 | | ^ 588 | v | next 612 | v | next 589 prev | +----+ 613 prev | +----+ 590 +------------->| | 614 +------------->| | 591 +----+ 615 +----+ 592 616 593 The way the head page moves forward: 617 The way the head page moves forward: 594 618 595 When the tail page meets the head page and the 619 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 620 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 621 writer may move the tail page. The way this is done is that the writer 598 performs a cmpxchg to convert the pointer to t 622 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 623 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 buf 624 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 finish 625 move the head page, until the writer is finished with the move. 602 626 603 This eliminates any races that the reader can 627 This eliminates any races that the reader can have on the writer. The reader 604 must spin, and this is why the reader cannot p 628 must spin, and this is why the reader cannot preempt the writer:: 605 629 606 tail page 630 tail page 607 | 631 | 608 v 632 v 609 +---+ +---+ +---+ +---+ 633 +---+ +---+ +---+ +---+ 610 <---| |--->| |-H->| |--->| |---> 634 <---| |--->| |-H->| |--->| |---> 611 --->| |<---| |<---| |<---| |<--- 635 --->| |<---| |<---| |<---| |<--- 612 +---+ +---+ +---+ +---+ 636 +---+ +---+ +---+ +---+ 613 637 614 tail page 638 tail page 615 | 639 | 616 v 640 v 617 +---+ +---+ +---+ +---+ 641 +---+ +---+ +---+ +---+ 618 <---| |--->| |-U->| |--->| |---> 642 <---| |--->| |-U->| |--->| |---> 619 --->| |<---| |<---| |<---| |<--- 643 --->| |<---| |<---| |<---| |<--- 620 +---+ +---+ +---+ +---+ 644 +---+ +---+ +---+ +---+ 621 645 622 The following page will be made into the new h 646 The following page will be made into the new head page:: 623 647 624 tail page 648 tail page 625 | 649 | 626 v 650 v 627 +---+ +---+ +---+ +---+ 651 +---+ +---+ +---+ +---+ 628 <---| |--->| |-U->| |-H->| |---> 652 <---| |--->| |-U->| |-H->| |---> 629 --->| |<---| |<---| |<---| |<--- 653 --->| |<---| |<---| |<---| |<--- 630 +---+ +---+ +---+ +---+ 654 +---+ +---+ +---+ +---+ 631 655 632 After the new head page has been set, we can s 656 After the new head page has been set, we can set the old head page 633 pointer back to NORMAL:: 657 pointer back to NORMAL:: 634 658 635 tail page 659 tail page 636 | 660 | 637 v 661 v 638 +---+ +---+ +---+ +---+ 662 +---+ +---+ +---+ +---+ 639 <---| |--->| |--->| |-H->| |---> 663 <---| |--->| |--->| |-H->| |---> 640 --->| |<---| |<---| |<---| |<--- 664 --->| |<---| |<---| |<---| |<--- 641 +---+ +---+ +---+ +---+ 665 +---+ +---+ +---+ +---+ 642 666 643 After the head page has been moved, the tail p 667 After the head page has been moved, the tail page may now move forward:: 644 668 645 tail page 669 tail page 646 | 670 | 647 v 671 v 648 +---+ +---+ +---+ +---+ 672 +---+ +---+ +---+ +---+ 649 <---| |--->| |--->| |-H->| |---> 673 <---| |--->| |--->| |-H->| |---> 650 --->| |<---| |<---| |<---| |<--- 674 --->| |<---| |<---| |<---| |<--- 651 +---+ +---+ +---+ +---+ 675 +---+ +---+ +---+ +---+ 652 676 653 677 654 The above are the trivial updates. Now for the 678 The above are the trivial updates. Now for the more complex scenarios. 655 679 656 680 657 As stated before, if enough writes preempt the 681 As stated before, if enough writes preempt the first write, the 658 tail page may make it all the way around the b 682 tail page may make it all the way around the buffer and meet the commit 659 page. At this time, we must start dropping wri 683 page. At this time, we must start dropping writes (usually with some kind 660 of warning to the user). But what happens if t 684 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 th 685 reader page? The commit page is not part of the ring buffer. The tail page 662 must account for this:: 686 must account for this:: 663 687 664 688 665 reader page commit page 689 reader page commit page 666 | | 690 | | 667 v | 691 v | 668 +---+ | 692 +---+ | 669 | |<----------+ 693 | |<----------+ 670 | | 694 | | 671 | |------+ 695 | |------+ 672 +---+ | 696 +---+ | 673 | 697 | 674 v 698 v 675 +---+ +---+ +---+ +---+ 699 +---+ +---+ +---+ +---+ 676 <---| |--->| |-H->| |--->| |---> 700 <---| |--->| |-H->| |--->| |---> 677 --->| |<---| |<---| |<---| |<--- 701 --->| |<---| |<---| |<---| |<--- 678 +---+ +---+ +---+ +---+ 702 +---+ +---+ +---+ +---+ 679 ^ 703 ^ 680 | 704 | 681 tail page 705 tail page 682 706 683 If the tail page were to simply push the head 707 If the tail page were to simply push the head page forward, the commit when 684 leaving the reader page would not be pointing 708 leaving the reader page would not be pointing to the correct page. 685 709 686 The solution to this is to test if the commit 710 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 i 711 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 712 tail page wrapped the buffer, and we must drop new writes. 689 713 690 This is not a race condition, because the comm 714 This is not a race condition, because the commit page can only be moved 691 by the outermost writer (the writer that was p 715 by the outermost writer (the writer that was preempted). 692 This means that the commit will not move while 716 This means that the commit will not move while a writer is moving the 693 tail page. The reader cannot swap the reader p 717 tail page. The reader cannot swap the reader page if it is also being 694 used as the commit page. The reader can simply 718 used as the commit page. The reader can simply check that the commit 695 is off the reader page. Once the commit page l 719 is off the reader page. Once the commit page leaves the reader page 696 it will never go back on it unless a reader do 720 it will never go back on it unless a reader does another swap with the 697 buffer page that is also the commit page. 721 buffer page that is also the commit page. 698 722 699 723 700 Nested writes 724 Nested writes 701 ------------- 725 ------------- 702 726 703 In the pushing forward of the tail page we mus 727 In the pushing forward of the tail page we must first push forward 704 the head page if the head page is the next pag 728 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 729 is not the next page, the tail page is simply updated with a cmpxchg. 706 730 707 Only writers move the tail page. This must be 731 Only writers move the tail page. This must be done atomically to protect 708 against nested writers:: 732 against nested writers:: 709 733 710 temp_page = tail_page 734 temp_page = tail_page 711 next_page = temp_page->next 735 next_page = temp_page->next 712 cmpxchg(tail_page, temp_page, next_page) 736 cmpxchg(tail_page, temp_page, next_page) 713 737 714 The above will update the tail page if it is s 738 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 739 page. If this fails, a nested write pushed it forward, the current write 716 does not need to push it:: 740 does not need to push it:: 717 741 718 742 719 temp page 743 temp page 720 | 744 | 721 v 745 v 722 tail page 746 tail page 723 | 747 | 724 v 748 v 725 +---+ +---+ +---+ +---+ 749 +---+ +---+ +---+ +---+ 726 <---| |--->| |--->| |--->| |---> 750 <---| |--->| |--->| |--->| |---> 727 --->| |<---| |<---| |<---| |<--- 751 --->| |<---| |<---| |<---| |<--- 728 +---+ +---+ +---+ +---+ 752 +---+ +---+ +---+ +---+ 729 753 730 Nested write comes in and moves the tail page 754 Nested write comes in and moves the tail page forward:: 731 755 732 tail page (moved by nest 756 tail page (moved by nested writer) 733 temp page | 757 temp page | 734 | | 758 | | 735 v v 759 v v 736 +---+ +---+ +---+ +---+ 760 +---+ +---+ +---+ +---+ 737 <---| |--->| |--->| |--->| |---> 761 <---| |--->| |--->| |--->| |---> 738 --->| |<---| |<---| |<---| |<--- 762 --->| |<---| |<---| |<---| |<--- 739 +---+ +---+ +---+ +---+ 763 +---+ +---+ +---+ +---+ 740 764 741 The above would fail the cmpxchg, but since th 765 The above would fail the cmpxchg, but since the tail page has already 742 been moved forward, the writer will just try a 766 been moved forward, the writer will just try again to reserve storage 743 on the new tail page. 767 on the new tail page. 744 768 745 But the moving of the head page is a bit more 769 But the moving of the head page is a bit more complex:: 746 770 747 tail page 771 tail page 748 | 772 | 749 v 773 v 750 +---+ +---+ +---+ +---+ 774 +---+ +---+ +---+ +---+ 751 <---| |--->| |-H->| |--->| |---> 775 <---| |--->| |-H->| |--->| |---> 752 --->| |<---| |<---| |<---| |<--- 776 --->| |<---| |<---| |<---| |<--- 753 +---+ +---+ +---+ +---+ 777 +---+ +---+ +---+ +---+ 754 778 755 The write converts the head page pointer to UP 779 The write converts the head page pointer to UPDATE:: 756 780 757 tail page 781 tail page 758 | 782 | 759 v 783 v 760 +---+ +---+ +---+ +---+ 784 +---+ +---+ +---+ +---+ 761 <---| |--->| |-U->| |--->| |---> 785 <---| |--->| |-U->| |--->| |---> 762 --->| |<---| |<---| |<---| |<--- 786 --->| |<---| |<---| |<---| |<--- 763 +---+ +---+ +---+ +---+ 787 +---+ +---+ +---+ +---+ 764 788 765 But if a nested writer preempts here, it will 789 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 790 page is a head page, but it is also nested. It will detect that 767 it is nested and will save that information. T 791 it is nested and will save that information. The detection is the 768 fact that it sees the UPDATE flag instead of a 792 fact that it sees the UPDATE flag instead of a HEADER or NORMAL 769 pointer. 793 pointer. 770 794 771 The nested writer will set the new head page p 795 The nested writer will set the new head page pointer:: 772 796 773 tail page 797 tail page 774 | 798 | 775 v 799 v 776 +---+ +---+ +---+ +---+ 800 +---+ +---+ +---+ +---+ 777 <---| |--->| |-U->| |-H->| |---> 801 <---| |--->| |-U->| |-H->| |---> 778 --->| |<---| |<---| |<---| |<--- 802 --->| |<---| |<---| |<---| |<--- 779 +---+ +---+ +---+ +---+ 803 +---+ +---+ +---+ +---+ 780 804 781 But it will not reset the update back to norma 805 But it will not reset the update back to normal. Only the writer 782 that converted a pointer from HEAD to UPDATE w 806 that converted a pointer from HEAD to UPDATE will convert it back 783 to NORMAL:: 807 to NORMAL:: 784 808 785 tail page 809 tail page 786 | 810 | 787 v 811 v 788 +---+ +---+ +---+ +---+ 812 +---+ +---+ +---+ +---+ 789 <---| |--->| |-U->| |-H->| |---> 813 <---| |--->| |-U->| |-H->| |---> 790 --->| |<---| |<---| |<---| |<--- 814 --->| |<---| |<---| |<---| |<--- 791 +---+ +---+ +---+ +---+ 815 +---+ +---+ +---+ +---+ 792 816 793 After the nested writer finishes, the outermos 817 After the nested writer finishes, the outermost writer will convert 794 the UPDATE pointer to NORMAL:: 818 the UPDATE pointer to NORMAL:: 795 819 796 820 797 tail page 821 tail page 798 | 822 | 799 v 823 v 800 +---+ +---+ +---+ +---+ 824 +---+ +---+ +---+ +---+ 801 <---| |--->| |--->| |-H->| |---> 825 <---| |--->| |--->| |-H->| |---> 802 --->| |<---| |<---| |<---| |<--- 826 --->| |<---| |<---| |<---| |<--- 803 +---+ +---+ +---+ +---+ 827 +---+ +---+ +---+ +---+ 804 828 805 829 806 It can be even more complex if several nested 830 It can be even more complex if several nested writes came in and moved 807 the tail page ahead several pages:: 831 the tail page ahead several pages:: 808 832 809 833 810 (first writer) 834 (first writer) 811 835 812 tail page 836 tail page 813 | 837 | 814 v 838 v 815 +---+ +---+ +---+ +---+ 839 +---+ +---+ +---+ +---+ 816 <---| |--->| |-H->| |--->| |---> 840 <---| |--->| |-H->| |--->| |---> 817 --->| |<---| |<---| |<---| |<--- 841 --->| |<---| |<---| |<---| |<--- 818 +---+ +---+ +---+ +---+ 842 +---+ +---+ +---+ +---+ 819 843 820 The write converts the head page pointer to UP 844 The write converts the head page pointer to UPDATE:: 821 845 822 tail page 846 tail page 823 | 847 | 824 v 848 v 825 +---+ +---+ +---+ +---+ 849 +---+ +---+ +---+ +---+ 826 <---| |--->| |-U->| |--->| |---> 850 <---| |--->| |-U->| |--->| |---> 827 --->| |<---| |<---| |<---| |<--- 851 --->| |<---| |<---| |<---| |<--- 828 +---+ +---+ +---+ +---+ 852 +---+ +---+ +---+ +---+ 829 853 830 Next writer comes in, and sees the update and 854 Next writer comes in, and sees the update and sets up the new 831 head page:: 855 head page:: 832 856 833 (second writer) 857 (second writer) 834 858 835 tail page 859 tail page 836 | 860 | 837 v 861 v 838 +---+ +---+ +---+ +---+ 862 +---+ +---+ +---+ +---+ 839 <---| |--->| |-U->| |-H->| |---> 863 <---| |--->| |-U->| |-H->| |---> 840 --->| |<---| |<---| |<---| |<--- 864 --->| |<---| |<---| |<---| |<--- 841 +---+ +---+ +---+ +---+ 865 +---+ +---+ +---+ +---+ 842 866 843 The nested writer moves the tail page forward. 867 The nested writer moves the tail page forward. But does not set the old 844 update page to NORMAL because it is not the ou 868 update page to NORMAL because it is not the outermost writer:: 845 869 846 tail page 870 tail page 847 | 871 | 848 v 872 v 849 +---+ +---+ +---+ +---+ 873 +---+ +---+ +---+ +---+ 850 <---| |--->| |-U->| |-H->| |---> 874 <---| |--->| |-U->| |-H->| |---> 851 --->| |<---| |<---| |<---| |<--- 875 --->| |<---| |<---| |<---| |<--- 852 +---+ +---+ +---+ +---+ 876 +---+ +---+ +---+ +---+ 853 877 854 Another writer preempts and sees the page afte 878 Another writer preempts and sees the page after the tail page is a head page. 855 It changes it from HEAD to UPDATE:: 879 It changes it from HEAD to UPDATE:: 856 880 857 (third writer) 881 (third writer) 858 882 859 tail page 883 tail page 860 | 884 | 861 v 885 v 862 +---+ +---+ +---+ +---+ 886 +---+ +---+ +---+ +---+ 863 <---| |--->| |-U->| |-U->| |---> 887 <---| |--->| |-U->| |-U->| |---> 864 --->| |<---| |<---| |<---| |<--- 888 --->| |<---| |<---| |<---| |<--- 865 +---+ +---+ +---+ +---+ 889 +---+ +---+ +---+ +---+ 866 890 867 The writer will move the head page forward:: 891 The writer will move the head page forward:: 868 892 869 893 870 (third writer) 894 (third writer) 871 895 872 tail page 896 tail page 873 | 897 | 874 v 898 v 875 +---+ +---+ +---+ +---+ 899 +---+ +---+ +---+ +---+ 876 <---| |--->| |-U->| |-U->| |-H-> 900 <---| |--->| |-U->| |-U->| |-H-> 877 --->| |<---| |<---| |<---| |<--- 901 --->| |<---| |<---| |<---| |<--- 878 +---+ +---+ +---+ +---+ 902 +---+ +---+ +---+ +---+ 879 903 880 But now that the third writer did change the H 904 But now that the third writer did change the HEAD flag to UPDATE it 881 will convert it to normal:: 905 will convert it to normal:: 882 906 883 907 884 (third writer) 908 (third writer) 885 909 886 tail page 910 tail page 887 | 911 | 888 v 912 v 889 +---+ +---+ +---+ +---+ 913 +---+ +---+ +---+ +---+ 890 <---| |--->| |-U->| |--->| |-H-> 914 <---| |--->| |-U->| |--->| |-H-> 891 --->| |<---| |<---| |<---| |<--- 915 --->| |<---| |<---| |<---| |<--- 892 +---+ +---+ +---+ +---+ 916 +---+ +---+ +---+ +---+ 893 917 894 918 895 Then it will move the tail page, and return ba 919 Then it will move the tail page, and return back to the second writer:: 896 920 897 921 898 (second writer) 922 (second writer) 899 923 900 tail page 924 tail page 901 | 925 | 902 v 926 v 903 +---+ +---+ +---+ +---+ 927 +---+ +---+ +---+ +---+ 904 <---| |--->| |-U->| |--->| |-H-> 928 <---| |--->| |-U->| |--->| |-H-> 905 --->| |<---| |<---| |<---| |<--- 929 --->| |<---| |<---| |<---| |<--- 906 +---+ +---+ +---+ +---+ 930 +---+ +---+ +---+ +---+ 907 931 908 932 909 The second writer will fail to move the tail p 933 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 t 934 moved, so it will try again and add its data to the new tail page. 911 It will return to the first writer:: 935 It will return to the first writer:: 912 936 913 937 914 (first writer) 938 (first writer) 915 939 916 tail page 940 tail page 917 | 941 | 918 v 942 v 919 +---+ +---+ +---+ +---+ 943 +---+ +---+ +---+ +---+ 920 <---| |--->| |-U->| |--->| |-H-> 944 <---| |--->| |-U->| |--->| |-H-> 921 --->| |<---| |<---| |<---| |<--- 945 --->| |<---| |<---| |<---| |<--- 922 +---+ +---+ +---+ +---+ 946 +---+ +---+ +---+ +---+ 923 947 924 The first writer cannot know atomically if the 948 The first writer cannot know atomically if the tail page moved 925 while it updates the HEAD page. It will then u 949 while it updates the HEAD page. It will then update the head page to 926 what it thinks is the new head page:: 950 what it thinks is the new head page:: 927 951 928 952 929 (first writer) 953 (first writer) 930 954 931 tail page 955 tail page 932 | 956 | 933 v 957 v 934 +---+ +---+ +---+ +---+ 958 +---+ +---+ +---+ +---+ 935 <---| |--->| |-U->| |-H->| |-H-> 959 <---| |--->| |-U->| |-H->| |-H-> 936 --->| |<---| |<---| |<---| |<--- 960 --->| |<---| |<---| |<---| |<--- 937 +---+ +---+ +---+ +---+ 961 +---+ +---+ +---+ +---+ 938 962 939 Since the cmpxchg returns the old value of the 963 Since the cmpxchg returns the old value of the pointer the first writer 940 will see it succeeded in updating the pointer 964 will see it succeeded in updating the pointer from NORMAL to HEAD. 941 But as we can see, this is not good enough. It 965 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 966 if the tail page is either where it use to be or on the next page:: 943 967 944 968 945 (first writer) 969 (first writer) 946 970 947 A B tail page 971 A B tail page 948 | | | 972 | | | 949 v v v 973 v v v 950 +---+ +---+ +---+ +---+ 974 +---+ +---+ +---+ +---+ 951 <---| |--->| |-U->| |-H->| |-H-> 975 <---| |--->| |-U->| |-H->| |-H-> 952 --->| |<---| |<---| |<---| |<--- 976 --->| |<---| |<---| |<---| |<--- 953 +---+ +---+ +---+ +---+ 977 +---+ +---+ +---+ +---+ 954 978 955 If tail page != A and tail page != B, then it 979 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 980 back to NORMAL. The fact that it only needs to worry about nested 957 writers means that it only needs to check this 981 writers means that it only needs to check this after setting the HEAD page:: 958 982 959 983 960 (first writer) 984 (first writer) 961 985 962 A B tail page 986 A B tail page 963 | | | 987 | | | 964 v v v 988 v v v 965 +---+ +---+ +---+ +---+ 989 +---+ +---+ +---+ +---+ 966 <---| |--->| |-U->| |--->| |-H-> 990 <---| |--->| |-U->| |--->| |-H-> 967 --->| |<---| |<---| |<---| |<--- 991 --->| |<---| |<---| |<---| |<--- 968 +---+ +---+ +---+ +---+ 992 +---+ +---+ +---+ +---+ 969 993 970 Now the writer can update the head page. This 994 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 outermo 995 remain in UPDATE and only reset by the outermost writer. This prevents 972 the reader from seeing the incorrect head page 996 the reader from seeing the incorrect head page:: 973 997 974 998 975 (first writer) 999 (first writer) 976 1000 977 A B tail page 1001 A B tail page 978 | | | 1002 | | | 979 v v v 1003 v v v 980 +---+ +---+ +---+ +---+ 1004 +---+ +---+ +---+ +---+ 981 <---| |--->| |--->| |--->| |-H-> 1005 <---| |--->| |--->| |--->| |-H-> 982 --->| |<---| |<---| |<---| |<--- 1006 --->| |<---| |<---| |<---| |<--- 983 +---+ +---+ +---+ +---+ 1007 +---+ +---+ +---+ +---+
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.