Add RCU section and example - #387
Conversation
There was a problem hiding this comment.
All reported issues were addressed across 3 files
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
jserv
left a comment
There was a problem hiding this comment.
It is unnecessary to have filenames starting with example_ in examples/ directory.
linD026
left a comment
There was a problem hiding this comment.
Be careful about the line break.
| rcu_read_lock(); | ||
| p = rcu_dereference(shared_data); | ||
| if (p) | ||
| pr_info("RCU Reader: value = %d\n", p->value); |
There was a problem hiding this comment.
As a best practice, it is better not to use pr_info() in the RCU read-side critical section.
There was a problem hiding this comment.
I just added pr_info() to make the read value visible, otherwise reading it silently feels a bit meaningless for an example. How about copying the value to a local variable first, and then calling pr_info() after rcu_read_unlock()?
|
|
||
| Keep in mind that RCU only protects the readers. Concurrent writers must still serialize among themselves using a standard lock, such as a spinlock or a mutex. | ||
|
|
||
| The following example demonstrates a basic RCU implementation where a reader safely accesses shared data while a writer updates it. |
There was a problem hiding this comment.
Adding some official materials would be better:
|
|
||
| Unlike spinlocks or mutexes, RCU readers do not acquire any locks or write to shared memory. This completely avoids cache-line bouncing and results in near-zero overhead for the read path. The tradeoff is that the updaters bear the cost of synchronization. When modifying a data structure, an updater creates a copy, modifies it, and then publishes the new version by replacing the old pointer in a single atomic operation. | ||
|
|
||
| Because concurrent readers might still be accessing the old data, the updater cannot free it immediately. Instead, it must wait for a "grace period" to elapse---a duration long enough to ensure that all pre-existing readers have left their critical sections. Only then can the old memory be safely reclaimed. |
There was a problem hiding this comment.
Add a line break between sentences.
... cannot free it immediately.
Instead, ...
| \label{sec:rcu} | ||
| Read-Copy-Update (RCU) is a synchronization mechanism that allows extremely fast, lock-free reads while still permitting concurrent updates. It is highly optimized for read-mostly scenarios. | ||
|
|
||
| Unlike spinlocks or mutexes, RCU readers do not acquire any locks or write to shared memory. This completely avoids cache-line bouncing and results in near-zero overhead for the read path. The tradeoff is that the updaters bear the cost of synchronization. When modifying a data structure, an updater creates a copy, modifies it, and then publishes the new version by replacing the old pointer in a single atomic operation. |
|
|
||
| \subsection{Read-Copy-Update (RCU)} | ||
| \label{sec:rcu} | ||
| Read-Copy-Update (RCU) is a synchronization mechanism that allows extremely fast, lock-free reads while still permitting concurrent updates. It is highly optimized for read-mostly scenarios. |
Other sync examples have an I think the author just wants to follow the style? |
Agree. We can use shorter prefixes that do not conflict with built-in Linux kernel module names later. |
Yeah, I was just aligning with the existing style, but I completely agree. Linus has actually complained about this exact kind of redundancy to maintainers before. I can send a follow up cleanup PR to rename them all once this gets merged. |
Introduce a new subsection in the synchronization chapter to explain basic RCU concepts. A new kernel module is also included to demonstrate RCU API usage for safely reading and updating shared data.
|
Thank @visitorckw for contributing! |
Introduce a new subsection in the synchronization chapter to explain basic RCU concepts. A new kernel module is also included to demonstrate RCU API usage for safely reading and updating shared data.
Summary by cubic
Adds an RCU subsection to the synchronization chapter and a runnable
example_rcukernel module to demonstrate lockless reads with serialized writers. Previously there was no RCU coverage or sample; now the docs and examples build include this by default.lkmpg.texbuilds; the RCU subsection renders;\samplec{examples/example_rcu.c}resolves.examples/Makefilebuildsexample_rcu.o; loading the module starts a reader kthread, updates values, and frees old memory only aftersynchronize_rcu(); writers serialize with a spinlock.Written for commit 5418701. Summary will update on new commits.