Skip to content

Add RCU section and example - #387

Merged
jserv merged 1 commit into
sysprog21:masterfrom
visitorckw:add-rcu
Aug 23, 2026
Merged

jserv merged 1 commit into
sysprog21:masterfrom
visitorckw:add-rcu

Conversation

@visitorckw

@visitorckw visitorckw commented Aug 22, 2026 •

Copy link
Copy Markdown
Collaborator

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_rcu kernel 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.

  • Docs: lkmpg.tex builds; the RCU subsection renders; \samplec{examples/example_rcu.c} resolves.
  • Build/run: examples/Makefile builds example_rcu.o; loading the module starts a reader kthread, updates values, and frees old memory only after synchronize_rcu(); writers serialize with a spinlock.

Written for commit 5418701. Summary will update on new commits.

Review in cubic

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 3 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread lkmpg.tex
Comment thread lkmpg.tex Outdated

@jserv jserv left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is unnecessary to have filenames starting with example_ in examples/ directory.

@linD026 linD026 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be careful about the line break.

Comment thread examples/example_rcu.c Outdated
rcu_read_lock();
p = rcu_dereference(shared_data);
if (p)
pr_info("RCU Reader: value = %d\n", p->value);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a best practice, it is better not to use pr_info() in the RCU read-side critical section.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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()?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

Comment thread lkmpg.tex

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding some official materials would be better:

Comment thread lkmpg.tex Outdated

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a line break between sentences.

... cannot free it immediately.
Instead, ...

Comment thread lkmpg.tex Outdated
\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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto, add a line break.

Comment thread lkmpg.tex Outdated

\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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

@linD026

linD026 commented Aug 23, 2026

Copy link
Copy Markdown
Collaborator

It is unnecessary to have filenames starting with example_ in examples/ directory.

Other sync examples have an example_ prefix.

obj-m += example_spinlock.o
obj-m += example_rwlock.o
obj-m += example_atomic.o
obj-m += example_mutex.o

I think the author just wants to follow the style?

@jserv

jserv commented Aug 23, 2026

Copy link
Copy Markdown
Contributor

Other sync examples have an example_ prefix.
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.

@visitorckw

Copy link
Copy Markdown
Collaborator Author

Other sync examples have an example_ prefix.
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.
@jserv
jserv merged commit 0e996e2 into sysprog21:master Aug 23, 2026
9 checks passed
@jserv

jserv commented Aug 23, 2026

Copy link
Copy Markdown
Contributor

Thank @visitorckw for contributing!

@visitorckw
visitorckw deleted the add-rcu branch August 24, 2026 05:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants