10.07.2015 Views

Is Parallel Programming Hard, And, If So, What Can You Do About It?

Is Parallel Programming Hard, And, If So, What Can You Do About It?

Is Parallel Programming Hard, And, If So, What Can You Do About It?

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

F.15. CHAPTER D: READ-COPY UPDATE IMPLEMENTATIONS 315Quick Quiz D.8:OK, so what is the story with the colors?Answer:Data structures analogous to rcu_state (includingrcu_ctrlblk) are yellow, those containingthe bitmaps used to determine when CPUs havechecked in are pink, and the per-CPU rcu_datastructures are blue. The data structures used toconserve energy (such as rcu_dynticks) will becolored green.Quick Quiz D.9:Given such an egregious bug, why does Linux runat all?Answer:Because the Linux kernel contains device driversthat are (relatively) well behaved. Few if any ofthem spin in RCU read-side critical sections forthe many milliseconds that would be required toprovoke this bug. The bug nevertheless does needto be fixed, and this variant of RCU does fix it.Quick Quiz D.10:But doesn’t this state diagram indicate thatdyntick-idle CPUs will get hit with reschedule IP<strong>Is</strong>?Won’t that wake them up?Answer:No. Keep in mind that RCU is handling groups ofCPUs. One particular group might contain bothdyntick-idle CPUs and CPUs in normal mode thathave somehow managed to avoid passing through aquiescent state. Only the latter group will be senta reschedule IPI; the dyntick-idle CPUs will merelybe marked as being in an extended quiescent state.Quick Quiz D.11:But what happens if a CPU tries to report goingthrough a quiescent state (by clearing its bit) beforethe bit-setting CPU has finished?Answer:There are three cases to consider here:1. A CPU corresponding to a non-yet-initializedleaf rcu_node structure tries to report a quiescentstate. This CPU will see its bit alreadycleared, so will give up on reporting its quiescentstate. <strong>So</strong>me later quiescent state will servefor the new grace period.2. A CPU corresponding to a leaf rcu_node structurethat is currently being initialized tries toreport a quiescent state. This CPU will see thatthe rcu_node structure’s ->lock is held, so willspin until it is released. But once the lock is released,the rcu_node structure will have beeninitialized, reducing to the following case.3. A CPU corresponding to a leaf rcu_node thathasalreadybeeninitializedtriestoreportaquiescentstate. This CPU will find its bit set, andwill therefore clear it. <strong>If</strong> it is the last CPU forthat leaf node, it will move up to the next levelof the hierarchy. However, this CPU cannotpossiblybethelastCPUinthesystemtoreporta quiescent state, given that the CPU doing theinitialization cannot yet have checked in.<strong>So</strong>, in all three cases, the potential race is resolvedcorrectly.Quick Quiz D.12:<strong>And</strong> what happens if all CPUs try to report goingthrough a quiescent state before the bit-settingCPU has finished, thus ending the new grace periodbefore it starts?Answer:The bit-setting CPU cannot pass through a quiescentstate during initialization, as it has irqsdisabled. <strong>It</strong>s bits therefore remain non-zero, preventingthe grace period from ending until the datastructure has been fully initialized.Quick Quiz D.13:<strong>And</strong> what happens if one CPU comes out ofdyntick-idle mode and then passed through aquiescent state just as another CPU notices thatthe first CPU was in dyntick-idle mode? Couldn’tthey both attempt to report a quiescent state atthe same time, resulting in confusion?Answer:They will both attempt to acquire the lock on thesame leaf rcu_node structure. The first one toacquire the lock will report the quiescent state andclear the appropriate bit, and the second one toacquire the lock will see that this bit has alreadybeen cleared.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!