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 319the grace period. The other CPU will then acquirethe lock and invoke rcu_start_gp(), which, seeingthat a grace period is already in progress, willimmediately release the lock and return.Quick Quiz D.33:How does the code traverse a given path throughthe rcu_node hierarchy from root to leaves?Answer:<strong>It</strong> turns out that the code never needs to do sucha traversal, so there is nothing special in place tohandle this.Quick Quiz D.34:C-preprocessor macros are so 1990s! Why not getwith the times and convert RCU_DATA_PTR_INIT()in Figure D.29 to be a function?Answer:Because, although it is possible to pass a referenceto a particular CPU’s instance of a per-CPUvariable to a function, there does not appear tobe a good way pass a reference to the full set ofinstances of a given per-CPU variable to a function.One could of course build an array of pointers, thenpass a reference to the array in, but that is part ofwhat the RCU_DATA_PTR_INIT() macro is doing inthe first place.Quick Quiz D.35:<strong>What</strong> happens if a CPU comes online betweenthe time that the last online CPU is notifiedon lines 25-26 of Figure D.29 and the time thatregister_cpu_notifier() is invoked on line 27?Answer:Only one CPU is online at this point, so the onlyway another CPU can come online is if this CPUputs it online, which it is not doing.Quick Quiz D.36:Why call cpu_quiet() on line 41 of Figure D.30,given that we are excluding grace periods withvarious locks, and given that any earlier graceperiods would not have been waiting on thispreviously-offlined CPU?Answer:A new grace period might have started just afterthe ->onofflock was released on line 40. Thecpu_quiet() will help expedite such a grace period.Quick Quiz D.37:But what if thercu_node hierarchy has only a singlestructure, as it would on a small system? <strong>What</strong>prevents concurrent grace-period initialization inthat case, given the code in Figure D.32?Answer:The later acquisition of the sole rcu_node structure’s->lock on line 16 excludes grace-periodinitialization, which must acquire this same lock inorder to initialize this sole rcu_node structure forthe new grace period.The ->onofflock is needed only for multi-nodehierarchies, and is used in that case as an alternativeto acquiring and holding all of the rcu_nodestructures’ ->lock fields, which would be incrediblypainful on large systems.Quick Quiz D.38:But does line 25 of Figure D.32 ever really exit theloop? Why or why not?Answer:The only way that line 25 could exit the loop isif all CPUs were to be put offline. This cannothappen in the Linux kernel as of 2.6.28, thoughother environments have been designed to offline allCPUs during the normal shutdown procedure.Quick Quiz D.39:Suppose that line 26 got executed seriously out oforder in Figure D.32, so that lastcomp is set tosome prior grace period, but so that the currentgrace period is still waiting on the now-offline CPU?In this case, won’t the call to cpu_quiet() fail toreport the quiescent state, thus causing the graceperiod to wait forever for this now-offline CPU?Answer:First, the lock acquisitions on lines 16 and 12 wouldprevent the execution of line 26 from being pushedthat far out of order. Nevertheless, even if line 26managed to be misordered that dramatically, whatwould happen is that force_quiescent_state()would eventually be invoked, and would noticethat the current grace period was waiting fora quiescent state from an offline CPU. Then

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

Saved successfully!

Ooh no, something went wrong!