Read-Copy-Update Synchronization in the Linux Kernel

Read-Copy-Update Synchronization in the Linux Kernel
Slide Note
Embed
Share

The synchronization problem in the Linux kernel involves managing shared data structures among multiple threads. Various synchronization methods are discussed to balance the needs of readers and writers. The Read-Copy-Update (RCU) philosophy aims to achieve scalability by allowing concurrent reads while ensuring consistent memory views for writers. This philosophy is implemented through RCU APIs for both writers and readers, encapsulating memory fences for safe data access.

  • Linux Kernel
  • Synchronization
  • Read-Copy-Update
  • RCU Philosophy
  • Concurrent Reads

Uploaded on Mar 15, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Read-Copy-Update Synchronization in the Linux Kernel David Ferry, Chris Gill CSE 522S - Advanced Operating Systems Washington University in St. Louis St. Louis, MO 63143 1

  2. The Synchronization Problem A common kernel problem: Multiple threads share a data structure. Some are reading Some are writing Shared data Reads and writes should not interfere! CSE 522S Advanced Operating Systems 2

  3. Synchronization Design Tradeoffs All synchronization methods need to balance the needs of readers and writers: Lots of reads Few writes Balanced Reads and writes Lots of writes Few reads Reads tend to be more common Synchronization can prevent concurrency Reader/writer locks Mutual exclusion Or it can allow concurrency at the expense of overheads: Lock free / wait free algorithms Transactional memory CSE 522S Advanced Operating Systems 3

  4. RCU Philosophy Under RCU: Concurrent reads are synchronization-free (which means scalability!) Writers must guarantee that all readers only ever see a consistent view of memory Similar to a publish-subscribe model Before the details, let s look at the API CSE 522S Advanced Operating Systems 4

  5. RCU Writer API Even if pointer write is atomic: 1 2 3 4 5 6 struct foo *ptr = NULL; p = kmalloc(...); p->A = 1; p->B = 2; p->C = 3; ptr = p; Overall code is not safe! Compiler may re-order lines 3-6 CSE 522S Advanced Operating Systems 5

  6. RCU Writer API RCU encapsulates memory fences 1 2 3 4 5 6 struct foo *ptr = NULL; p = kmalloc(...); p->A = 1; p->B = 2; p->C = 3; rcu_assign_ptr(ptr,p); rcu_assign_ptr method publishes P CSE 522S Advanced Operating Systems 6

  7. RCU Reader API Consider reading a data structure: p = ptr; if (p != NULL) do_something(p->A, p->B, p->C); This is also not safe! The values of A, B, and C could change between reads! CSE 522S Advanced Operating Systems 7

  8. RCU Reader API Consider reading a data structure: rcu_read_lock(); p = rcu_dereference(ptr); if (p != NULL) do_something(p->A, p->B, p->C); rcu_read_unlock(); rcu_dereference() can be thought of as subscribing to a specific, valid version of ptr lock/unlock defines RCU critical section CSE 522S Advanced Operating Systems 8

  9. RCU Encapsulation Note, RCU semantics can be encapsulated for specific data structures: rcu_list_add() rcu_for_each_read() But not: rcu_for_each_write() RCU does not allow for concurrent writes! CSE 522S Advanced Operating Systems 9

  10. What does RCU actually do? RCU Read Critical Section RCU Read Critical Section RCU Read Critical Section RCU Read Critical Section RCU Read Critical Section RCU Read Critical Section RCU Read Critical Section RCU Read Critical Section Creation Removal Grace Period Reclamation Time CSE 522S Advanced Operating Systems 10

  11. wait_for_readers() A critical implementation detail is how to wait for outstanding reads. Explicit: reference counting reader locks RCU Read Critical Section RCU Read Critical Section RCU Read Critical Section Implicit: Linux context switch RCU Read Critical Section RCU Read Critical Section Creation Removal Grace Period Reclamation Time CSE 522S Advanced Operating Systems 11

  12. RCU Linked List - Delete D H 1. Want to delete node D Ptr D H 2. Atomically assign pointer over D Ptr 3. D is safe to delete when all reader locks are done D H CSE 522S Advanced Operating Systems 12

  13. RCU Linked List - Update 1 4 M Q H H P P 2 5 Q M Q H H P 3 6 M M Q H H P CSE 522S Advanced Operating Systems 13

  14. RCU usage in sched/core.c RCU-iterates over list of processes Writes back to RCU-protected structure CSE 522S Advanced Operating Systems 14

More Related Content