Read-Copy Update (RCU)
Total Page:16
File Type:pdf, Size:1020Kb
COMP 790: OS Implementation Read-Copy Update (RCU) Don Porter COMP 790: OS Implementation Logical Diagram Binary Memory Threads Formats Allocators User Today’s Lecture System Calls Kernel RCU File System Networking Sync Memory Device CPU Management Drivers Scheduler Hardware Interrupts Disk Net Consistency COMP 790: OS Implementation RCU in a nutshell • Think about data structures that are mostly read, occasionally written – Like the Linux dcache • RW locks allow concurrent reads – Still require an atomic decrement of a lock counter – Atomic ops are expensive • Idea: Only require locks for writers; carefully update data structure so readers see consistent views of data COMP 790: OS Implementation Motivation (from Paul McKenney’s Thesis) 35 "ideal" "global" 30 "globalrw" 25 20 Performance of RW 15 lock only marginally 10 better than mutex 5 lock Hash Table Searches per Microsecond 0 1 2 3 4 # CPUs COMP 790: OS Implementation Principle (1/2) • Locks have an acquire and release cost – Substantial, since atomic ops are expensive • For short critical regions, this cost dominates performance COMP 790: OS Implementation Principle (2/2) • Reader/writer locks may allow critical regions to execute in parallel • But they still serialize the increment and decrement of the read count with atomic instructions – Atomic instructions performance decreases as more CPUs try to do them at the same time • The read lock itself becomes a scalability bottleneck, even if the data it protects is read 99% of the time COMP 790: OS Implementation Lock-free data structures • Some concurrent data structures have been proposed that don’t require locks • They are difficult to create if one doesn’t already suit your needs; highly error prone • Can eliminate these problems COMP 790: OS Implementation RCU: Split the difference • One of the hardest parts of lock-free algorithms is concurrent changes to pointers – So just use locks and make writers go one-at-a-time • But, make writers be a bit careful so readers see a consistent view of the data structures • If 99% of accesses are readers, avoid performance- killing read lock in the common case COMP 790: OS Implementation Example: Linked lists This implementation needsInsert(B) a lock A C E B B’s next pointer is uninitialized; Reader gets a Reader goes to B page fault COMP 790: OS Implementation Example: Linked lists Insert(B) A C E B Reader goes to C or B- --either is ok COMP 790: OS Implementation Example recap • Notice that we first created node B, and set up all outgoing pointers • Then we overwrite the pointer from A – No atomic instruction or reader lock needed – Either traversal is safe – In some cases, we may need a memory barrier • Key idea: Carefully update the data structure so that a reader can never follow a bad pointer – Writers still serialize using a lock COMP 790: OS Implementation Example 2: Linked lists Delete (C) A C E Reader may still be looking at C. When can we delete? COMP 790: OS Implementation Problem • We logically remove a node by making it unreachable to future readers – No pointers to this node in the list • We eventually need to free the node’s memory – Leaks in a kernel are bad! • When is this safe? – Note that we have to wait for readers to “move on” down the list COMP 790: OS Implementation Worst-case scenario • Reader follows pointer to node X (about to be freed) • Another thread frees X • X is reallocated and overwritten with other data • Reader interprets bytes in X->next as pointer, segmentation fault COMP 790: OS Implementation Quiescence • Trick: Linux doesn’t allow a process to sleep while traversing an RCU-protected data structure – Includes kernel preemption, I/O waiting, etc. • Idea: If every CPU has called schedule() (quiesced), then it is safe to free the node – Each CPU counts the number of times it has called schedule() – Put a to-be-freed item on a list of pending frees – Record timestamp on each CPU – Once each CPU has called schedule, do the free COMP 790: OS Implementation Quiescence, cont • There are some optimizations that keep the per-CPU counter to just a bit – Intuition: All you really need to know is if each CPU has called schedule() once since this list became non-empty – Details left to the reader COMP 790: OS Implementation Limitations • No doubly-linked lists • Can’t immediately reuse embedded list nodes – Must wait for quiescence first – So only useful for lists where an item’s position doesn’t change frequently • Only a few RCU data structures in existence COMP 790: OS Implementation Nonetheless • Linked lists are the workhorse of the Linux kernel • RCU lists are increasingly used where appropriate • Improved performance! COMP 790: OS Implementation Big Picture • Carefully designed data structures Hash Pending – Readers always see List Signals consistent view • Low-level “helper” functions encapsulate RCU “library” complex issues – Memory barriers – Quiescence COMP 790: OS Implementation API • Drop in replacement for read_lock: – rcu_read_lock() • Wrappers such as rcu_assign_pointer() and rcu_dereference_pointer() include memory barriers • Rather than immediately free an object, use call_rcu(object, delete_fn) to do a deferred deletion COMP 790: OS Implementation Code Example From fs/binfmt_elf.c rcu_read_lock(); prstatus->pr_ppid = task_pid_vnr(rcu_dereference(p->real_parent)); rcu_read_unlock(); COMP 790: OS Implementation Simplified Code Example From arch/x86/include/asm/rcupdate.h #define rcu_dereference(p) ({ \ typeof(p) ______p1 = (*(volatile typeof(p)*) &p);\ read_barrier_depends(); // defined by arch \ ______p1; // “returns” this value \ }) COMP 790: OS Implementation Code Example From fs/dcache.c static void d_free(struct dentry *dentry) { /* ... Ommitted code for simplicity */ call_rcu(&dentry->d_rcu, d_callback); } // After quiescence, call_rcu functions are called static void d_callback(struct rcu_head *rcu) { struct dentry *dentry = container_of(head, struct dentry, d_rcu); __d_free(dentry); // Real free } COMP 790: OS Implementation {1,2,3 5,6,7 11,4,8}, while others will see {1,2,3 11,4,8From}. McKenney and Walpole, Introducing Technology The synchronize_rcu() primitive blocks until all pre- existing RCU readers complete, after which point there can into the Linux Kernel: A Case Study be no readers referencing the second element, as indicated 2000 by the green shading on the third row. At this point, all 1800 RCU readers see a single version of the list, namely, {1,2,3 11,4,8}. It is then safe to free that element, as shown on the 1600 last row. 1400 This of course leads to the question of how one could possibly implement synchronize_rcu(), especially in cases 1200 where the read-side primitives generate no code. This ques- 1000 tion is taken up in the next section. 800 2.4 Toy RCU Implementation # RCU API Uses 600 Consider a non-preemptive kernel environment, where all 400 threads run to block. In this case, it is illegal to block while holding a spinlock, as doing so can result in a deadlock sit- 200 uation where all the CPUs are spinning on a lock held by 0 a blocked thread. The CPUs cannot acquire the lock until 2002 2003 2004 2005 2006 2007 2008 2009 it is released, but the blocked thread cannot release until Year after at least one of the CPUs acquires the lock. This same restriction applies to RCU read-side critical sections, so that it is illegal to block while traversing an RCU-protected data Figure 2: RCU API Usage in the Linux Kernel structure. This restriction is sufficient to admit the following trivial implementation of synchronize_rcu(): itive resembles Hsieh’s and Weihl’s scalable reader-writer 1 void synchronize_rcu() locks [17].) 2{ 3 foreach_cpu(cpu) 3. RCU USAGE WITHIN LINUX 4 run_on(cpu); RCU’s usage within the Linux kernel has increased rapidly 5} over the past five years, as shown in Figure 2 [27]. In some cases, RCU has displaced other synchronization mechanisms This code fragment simply runs on each CPU in turn. To in existing code (for example, brlock in the networking pro- see how this works, consider the situation once synchronize_ tocol stacks [50, 65, 66]), while in other cases it has been in- rcu() has started running on CPU 0. Whatever was run- troduced with code implementing new functionality (for ex- ning on CPU 0 beforehand must have blocked, otherwise ample, the audit system within SELinux [51]). Despite its synchronize_rcu() could not have begun running on CPU 0. rapid growth, RCU remains a niche technology, as shown Because it is illegal to block within an RCU read-side critical by the comparison with locking in Figure 3. Nonetheless, section, all prior RCU read-side critical sections running on RCU can be characterized as a reasonably successful niche CPU 0 must have completed. This same line of reasoning technology within the Linux kernel. As such, it is useful to applies to each of the other CPUs that synchronize_rcu() review the path RCU took in achieving this modest level of runs on, so that once synchronize_rcu() has completed, all success, which was due more to RCU’s being dramatically prior RCU read-side critcial sections throughout the system changed by Linux than by Linux being changed by RCU. must have completed. Production-quality synchronize_rcu() implementations are more complex due to the need for performance and scal- 4. RCU BEFORE LINUX ability, the need to preempt RCU read-side critical sections Before Linux, production use of RCU-like mechanisms ap- in real-time systems, and the need to tolerate CPUs being pears to have been confined to large data-processing systems added to and removed from the system, for example, in or- such as the IBM mainframe’s VM/XA [15] and Sequent’s der to conserve energy when the system is mostly idle. (now IBM’s) Symmetry and NUMA-Q systems running the DYNIX/ptx operating system [44].