<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
read-copy-update	B-Operating_System
(	O
RCU	O
)	O
is	O
a	O
synchronization	O
mechanism	O
that	O
avoids	O
the	O
use	O
of	O
lock	B-Operating_System
primitives	O
while	O
multiple	O
threads	B-Operating_System
concurrently	O
read	O
and	O
update	O
elements	O
that	O
are	O
linked	O
through	O
pointers	O
and	O
that	O
belong	O
to	O
shared	B-Operating_System
data	I-Operating_System
structures	O
(	O
e.g.	O
,	O
linked	B-Data_Structure
lists	I-Data_Structure
,	O
trees	B-Application
,	O
hash	B-Algorithm
tables	I-Algorithm
)	O
.	O
</s>
<s>
Whenever	O
a	O
thread	B-Operating_System
is	O
inserting	O
or	O
deleting	O
elements	O
of	O
data	B-General_Concept
structures	I-General_Concept
in	O
shared	B-Operating_System
memory	I-Operating_System
,	O
all	O
readers	O
are	O
guaranteed	O
to	O
see	O
and	O
traverse	O
either	O
the	O
older	O
or	O
the	O
new	O
structure	O
,	O
therefore	O
avoiding	O
inconsistencies	O
(	O
e.g.	O
,	O
dereferencing	O
null	O
pointers	O
)	O
.	O
</s>
<s>
A	O
thread	B-Operating_System
wishing	O
to	O
do	O
this	O
uses	O
the	O
following	O
steps	O
:	O
</s>
<s>
sleep	O
until	O
the	O
operating	O
system	O
kernel	B-Operating_System
determines	O
that	O
there	O
are	O
no	O
readers	O
left	O
using	O
the	O
old	O
structure	O
,	O
for	O
example	O
,	O
in	O
the	O
Linux	B-Operating_System
kernel	I-Operating_System
,	O
by	O
using	O
,	O
</s>
<s>
once	O
awakened	O
by	O
the	O
kernel	B-Operating_System
,	O
deallocate	O
the	O
old	O
structure	O
.	O
</s>
<s>
So	O
the	O
structure	O
is	O
read	O
concurrently	O
with	O
a	O
thread	B-Operating_System
copying	O
in	O
order	O
to	O
do	O
an	O
update	O
,	O
hence	O
the	O
name	O
"	O
read-copy	O
update	O
"	O
.	O
</s>
<s>
The	O
abbreviation	O
"	O
RCU	O
"	O
was	O
one	O
of	O
many	O
contributions	O
by	O
the	O
Linux	B-Operating_System
community	O
.	O
</s>
<s>
Other	O
names	O
for	O
similar	O
techniques	O
include	O
passive	O
serialization	O
and	O
MP	O
defer	O
by	O
VM/XA	B-Application
programmers	O
and	O
generations	O
by	O
K42	B-Operating_System
and	O
programmers	O
.	O
</s>
<s>
A	O
key	O
property	O
of	O
RCU	O
is	O
that	O
readers	O
can	O
access	O
a	O
data	B-General_Concept
structure	I-General_Concept
even	O
when	O
it	O
is	O
in	O
the	O
process	O
of	O
being	O
updated	O
:	O
RCU	O
updaters	O
cannot	O
block	O
readers	O
or	O
force	O
them	O
to	O
retry	O
their	O
accesses	O
.	O
</s>
<s>
This	O
procedure	O
demonstrates	O
how	O
new	O
data	O
may	O
be	O
inserted	O
into	O
a	O
linked	O
data	B-General_Concept
structure	I-General_Concept
even	O
though	O
readers	O
are	O
concurrently	O
traversing	O
the	O
data	B-General_Concept
structure	I-General_Concept
before	O
,	O
during	O
,	O
and	O
after	O
the	O
insertion	O
.	O
</s>
<s>
The	O
first	O
state	O
shows	O
a	O
linked	B-Data_Structure
list	I-Data_Structure
containing	O
elements	O
,	O
,	O
and	O
.	O
</s>
<s>
Readers	O
accessing	O
the	O
link	O
from	O
element	O
will	O
either	O
obtain	O
a	O
reference	O
to	O
element	O
or	O
element	O
,	O
but	O
either	O
way	O
,	O
each	O
reader	O
will	O
see	O
a	O
valid	O
and	O
correctly	O
formatted	O
linked	B-Data_Structure
list	I-Data_Structure
.	O
</s>
<s>
This	O
is	O
in	O
stark	O
contrast	O
with	O
more	O
traditional	O
synchronization	O
primitives	O
such	O
as	O
locking	B-Operating_System
or	O
transactions	B-General_Concept
that	O
coordinate	O
in	O
time	O
,	O
but	O
not	O
in	O
space	O
.	O
</s>
<s>
This	O
procedure	O
demonstrates	O
how	O
old	O
data	O
may	O
be	O
removed	O
from	O
a	O
linked	O
data	B-General_Concept
structure	I-General_Concept
even	O
though	O
readers	O
are	O
concurrently	O
traversing	O
the	O
data	B-General_Concept
structure	I-General_Concept
before	O
,	O
during	O
,	O
and	O
after	O
the	O
deletion	O
.	O
</s>
<s>
Given	O
insertion	O
and	O
deletion	O
,	O
a	O
wide	O
variety	O
of	O
data	B-General_Concept
structures	I-General_Concept
can	O
be	O
implemented	O
using	O
RCU	O
.	O
</s>
<s>
Any	O
statement	O
that	O
is	O
not	O
within	O
an	O
RCU	O
read-side	O
critical	O
section	O
is	O
said	O
to	O
be	O
in	O
a	O
quiescent	O
state	O
,	O
and	O
such	O
statements	O
are	O
not	O
permitted	O
to	O
hold	O
references	O
to	O
RCU-protected	O
data	B-General_Concept
structures	I-General_Concept
,	O
nor	O
is	O
the	O
wait-for-readers	O
operation	O
required	O
to	O
wait	O
for	O
threads	B-Operating_System
in	O
quiescent	O
states	O
.	O
</s>
<s>
Any	O
time	O
period	O
during	O
which	O
each	O
thread	B-Operating_System
resides	O
at	O
least	O
once	O
in	O
a	O
quiescent	O
state	O
is	O
called	O
a	O
grace	O
period	O
.	O
</s>
<s>
It	O
turns	O
out	O
that	O
this	O
guarantee	O
can	O
be	O
provided	O
with	O
extremely	O
small	O
read-side	O
overheads	O
,	O
in	O
fact	O
,	O
in	O
the	O
limiting	O
case	O
that	O
is	O
actually	O
realized	O
by	O
server-class	O
Linux-kernel	O
builds	O
,	O
the	O
read-side	O
overhead	O
is	O
exactly	O
zero	O
.	O
</s>
<s>
The	O
removal	O
phase	O
removes	O
references	O
to	O
data	O
items	O
within	O
a	O
data	B-General_Concept
structure	I-General_Concept
(	O
possibly	O
by	O
replacing	O
them	O
with	O
references	O
to	O
new	O
versions	O
of	O
these	O
data	O
items	O
)	O
,	O
and	O
can	O
run	O
concurrently	O
with	O
RCU	O
read-side	O
critical	O
sections	O
.	O
</s>
<s>
The	O
reason	O
that	O
it	O
is	O
safe	O
to	O
run	O
the	O
removal	O
phase	O
concurrently	O
with	O
RCU	O
readers	O
is	O
the	O
semantics	O
of	O
modern	O
CPUs	O
guarantee	O
that	O
readers	O
will	O
see	O
either	O
the	O
old	O
or	O
the	O
new	O
version	O
of	O
the	O
data	B-General_Concept
structure	I-General_Concept
rather	O
than	O
a	O
partially	O
updated	O
reference	O
.	O
</s>
<s>
Ensure	O
that	O
all	O
readers	O
accessing	O
RCU-protected	O
data	B-General_Concept
structures	I-General_Concept
carry	O
out	O
their	O
references	O
from	O
within	O
an	O
RCU	O
read-side	O
critical	O
section	O
.	O
</s>
<s>
Remove	O
pointers	O
to	O
a	O
data	B-General_Concept
structure	I-General_Concept
,	O
so	O
that	O
subsequent	O
readers	O
cannot	O
gain	O
a	O
reference	O
to	O
it	O
.	O
</s>
<s>
Wait	O
for	O
a	O
grace	O
period	O
to	O
elapse	O
,	O
so	O
that	O
all	O
previous	O
readers	O
(	O
which	O
might	O
still	O
have	O
pointers	O
to	O
the	O
data	B-General_Concept
structure	I-General_Concept
removed	O
in	O
the	O
prior	O
step	O
)	O
will	O
have	O
completed	O
their	O
RCU	O
read-side	O
critical	O
sections	O
.	O
</s>
<s>
At	O
this	O
point	O
,	O
there	O
cannot	O
be	O
any	O
readers	O
still	O
holding	O
references	O
to	O
the	O
data	B-General_Concept
structure	I-General_Concept
,	O
so	O
it	O
now	O
may	O
safely	O
be	O
reclaimed	O
(	O
e.g.	O
,	O
freed	O
)	O
.	O
</s>
<s>
In	O
the	O
above	O
procedure	O
(	O
which	O
matches	O
the	O
earlier	O
diagram	O
)	O
,	O
the	O
updater	O
is	O
performing	O
both	O
the	O
removal	O
and	O
the	O
reclamation	O
step	O
,	O
but	O
it	O
is	O
often	O
helpful	O
for	O
an	O
entirely	O
different	O
thread	B-Operating_System
to	O
do	O
the	O
reclamation	O
.	O
</s>
<s>
Reference	O
counting	O
can	O
be	O
used	O
to	O
let	O
the	O
reader	O
perform	O
removal	O
so	O
,	O
even	O
if	O
the	O
same	O
thread	B-Operating_System
performs	O
both	O
the	O
update	O
step	O
(	O
step	O
(	O
2	O
)	O
above	O
)	O
and	O
the	O
reclamation	O
step	O
(	O
step	O
(	O
4	O
)	O
above	O
)	O
,	O
it	O
is	O
often	O
helpful	O
to	O
think	O
of	O
them	O
separately	O
.	O
</s>
<s>
RCU	O
is	O
perhaps	O
the	O
most	O
common	O
non-blocking	B-Operating_System
algorithm	I-Operating_System
for	O
a	O
shared	B-Operating_System
data	I-Operating_System
structure	O
.	O
</s>
<s>
RCU	O
is	O
completely	O
wait-free	B-Operating_System
for	O
any	O
number	O
of	O
readers	O
.	O
</s>
<s>
Single-writer	O
implementations	O
RCU	O
are	O
also	O
lock-free	B-Operating_System
for	O
the	O
writer	O
.	O
</s>
<s>
Some	O
multi-writer	O
implementations	O
of	O
RCU	O
are	O
lock-free	B-Operating_System
.	O
</s>
<s>
Other	O
multi-writer	O
implementations	O
of	O
RCU	O
serialize	O
writers	O
with	O
a	O
lock	B-Operating_System
.	O
</s>
<s>
By	O
early	O
2008	O
,	O
there	O
were	O
almost	O
2,000	O
uses	O
of	O
the	O
RCU	O
API	B-Application
within	O
the	O
Linux	B-Operating_System
kernel	I-Operating_System
including	O
the	O
networking	O
protocol	O
stacks	O
and	O
the	O
memory-management	O
system	O
.	O
</s>
<s>
Since	O
2006	O
,	O
researchers	O
have	O
applied	O
RCU	O
and	O
similar	O
techniques	O
to	O
a	O
number	O
of	O
problems	O
,	O
including	O
management	O
of	O
metadata	O
used	O
in	O
dynamic	O
analysis	O
,	O
managing	O
the	O
lifetime	O
of	O
clustered	O
objects	O
,	O
managing	O
object	O
lifetime	O
in	O
the	O
K42	B-Operating_System
research	O
operating	O
system	O
,	O
and	O
optimizing	O
software	B-Operating_System
transactional	I-Operating_System
memory	I-Operating_System
implementations	O
.	O
</s>
<s>
Dragonfly	B-Application
BSD	I-Application
uses	O
a	O
technique	O
similar	O
to	O
RCU	O
that	O
most	O
closely	O
resembles	O
Linux	B-Operating_System
's	O
Sleepable	O
RCU	O
(	O
SRCU	O
)	O
implementation	O
.	O
</s>
<s>
In	O
contrast	O
,	O
in	O
more	O
conventional	O
lock-based	O
schemes	O
,	O
readers	O
must	O
use	O
heavy-weight	O
synchronization	O
in	O
order	O
to	O
prevent	O
an	O
updater	O
from	O
deleting	O
the	O
data	B-General_Concept
structure	I-General_Concept
out	O
from	O
under	O
them	O
.	O
</s>
<s>
The	O
reason	O
is	O
that	O
lock-based	O
updaters	O
typically	O
update	O
data	O
in	O
place	O
,	O
and	O
must	O
therefore	O
exclude	O
readers	O
.	O
</s>
<s>
Concurrent	O
RCU	O
readers	O
can	O
then	O
continue	O
accessing	O
the	O
old	O
versions	O
,	O
and	O
can	O
dispense	O
with	O
the	O
atomic	O
read-modify-write	O
instructions	O
,	O
memory	B-General_Concept
barriers	I-General_Concept
,	O
and	O
cache	O
misses	O
that	O
are	O
so	O
expensive	O
on	O
modern	O
SMP	B-Operating_System
computer	O
systems	O
,	O
even	O
in	O
absence	O
of	O
lock	B-Operating_System
contention	I-Operating_System
.	O
</s>
<s>
For	O
example	O
,	O
they	O
provide	O
immunity	O
to	O
most	O
deadlock	B-Operating_System
and	O
livelock	O
conditions	O
.	O
</s>
<s>
RCU	O
is	O
available	O
in	O
a	O
number	O
of	O
operating	O
systems	O
,	O
and	O
was	O
added	O
to	O
the	O
Linux	B-Operating_System
kernel	I-Operating_System
in	O
October	O
2002	O
.	O
</s>
<s>
The	O
implementation	O
of	O
RCU	O
in	O
version	O
2.6	O
of	O
the	O
Linux	B-Operating_System
kernel	I-Operating_System
is	O
among	O
the	O
better-known	O
RCU	O
implementations	O
,	O
and	O
will	O
be	O
used	O
as	O
an	O
inspiration	O
for	O
the	O
RCU	O
API	B-Application
in	O
the	O
remainder	O
of	O
this	O
article	O
.	O
</s>
<s>
The	O
core	O
API	B-Application
(	O
Application	B-Application
Programming	I-Application
Interface	I-Application
)	O
is	O
quite	O
small	O
:	O
</s>
<s>
rcu_read_lock( )	O
:	O
Marks	O
an	O
RCU-protected	O
data	B-General_Concept
structure	I-General_Concept
so	O
that	O
it	O
wo	O
n't	O
be	O
reclaimed	O
for	O
the	O
full	O
duration	O
of	O
that	O
critical	O
section	O
.	O
</s>
<s>
Since	O
synchronize_rcu	O
is	O
the	O
API	B-Application
that	O
must	O
figure	O
out	O
when	O
readers	O
are	O
done	O
,	O
its	O
implementation	O
is	O
key	O
to	O
RCU	O
.	O
</s>
<s>
This	O
callback	O
variant	O
is	O
called	O
call_rcu	O
in	O
the	O
Linux	B-Operating_System
kernel	I-Operating_System
.	O
</s>
<s>
This	O
function	O
returns	O
the	O
new	O
value	O
,	O
and	O
also	O
executes	O
any	O
memory	B-General_Concept
barrier	I-General_Concept
instructions	O
required	O
for	O
a	O
given	O
CPU	O
architecture	O
.	O
</s>
<s>
It	O
also	O
executes	O
any	O
directives	O
required	O
by	O
the	O
compiler	O
or	O
the	O
CPU	O
,	O
for	O
example	O
,	O
a	O
volatile	O
cast	O
for	O
gcc	O
,	O
a	O
memory_order_consume	O
load	O
for	O
C/C	O
++11	O
or	O
the	O
memory-barrier	O
instruction	O
required	O
by	O
the	O
old	O
DEC	B-Device
Alpha	I-Device
CPU	O
.	O
</s>
<s>
The	O
diagram	O
on	O
the	O
right	O
shows	O
how	O
each	O
API	B-Application
communicates	O
among	O
the	O
reader	O
,	O
updater	O
,	O
and	O
reclaimer	O
.	O
</s>
<s>
Efficient	O
implementations	O
of	O
the	O
RCU	O
infrastructure	O
make	O
heavy	O
use	O
of	O
batching	O
in	O
order	O
to	O
amortize	O
their	O
overhead	O
over	O
many	O
uses	O
of	O
the	O
corresponding	O
APIs	B-Application
.	O
</s>
<s>
This	O
section	O
presents	O
one	O
such	O
"	O
toy	O
"	O
implementation	O
that	O
works	O
in	O
a	O
non-preemptive	B-Operating_System
environment	I-Operating_System
.	O
</s>
<s>
This	O
is	O
the	O
great	O
strength	O
of	O
classic	O
RCU	O
in	O
a	O
non-preemptive	O
kernel	B-Operating_System
:	O
read-side	O
overhead	O
is	O
precisely	O
zero	O
,	O
as	O
smp_read_barrier_depends( )	O
is	O
an	O
empty	O
macro	O
on	O
all	O
but	O
DEC	B-Device
Alpha	I-Device
CPUs	O
;	O
such	O
memory	B-General_Concept
barriers	I-General_Concept
are	O
not	O
needed	O
on	O
modern	O
CPUs	O
.	O
</s>
<s>
And	O
there	O
is	O
no	O
way	O
that	O
rcu_read_lock	O
can	O
participate	O
in	O
a	O
deadlock	B-Operating_System
cycle	O
,	O
cause	O
a	O
realtime	O
process	O
to	O
miss	O
its	O
scheduling	O
deadline	O
,	O
precipitate	O
priority	B-Operating_System
inversion	I-Operating_System
,	O
or	O
result	O
in	O
high	O
lock	B-Operating_System
contention	I-Operating_System
.	O
</s>
<s>
Recall	O
that	O
this	O
is	O
a	O
non-preemptive	B-Operating_System
environment	I-Operating_System
and	O
that	O
blocking	O
within	O
an	O
RCU	O
read-side	O
critical	O
section	O
is	O
illegal	O
,	O
which	O
imply	O
that	O
there	O
can	O
be	O
no	O
preemption	O
points	O
within	O
an	O
RCU	O
read-side	O
critical	O
section	O
.	O
</s>
<s>
Although	O
RCU	O
can	O
be	O
used	O
in	O
many	O
different	O
ways	O
,	O
a	O
very	O
common	O
use	O
of	O
RCU	O
is	O
analogous	O
to	O
reader	O
–	O
writer	O
locking	B-Operating_System
.	O
</s>
<s>
The	O
following	O
side-by-side	O
code	O
display	O
shows	O
how	O
closely	O
related	O
reader	O
–	O
writer	O
locking	B-Operating_System
and	O
RCU	O
can	O
be	O
.	O
</s>
<s>
Read-side	O
locking	B-Operating_System
moves	O
to	O
rcu_read_lock	O
and	O
rcu_read_unlock	O
,	O
update-side	O
locking	B-Operating_System
moves	O
from	O
a	O
reader-writer	O
lock	B-Operating_System
to	O
a	O
simple	O
spinlock	O
,	O
and	O
a	O
synchronize_rcu	O
precedes	O
the	O
kfree	O
.	O
</s>
<s>
For	O
example	O
,	O
if	O
multiple	O
independent	O
list	O
updates	O
must	O
be	O
seen	O
as	O
a	O
single	O
atomic	B-General_Concept
update	I-General_Concept
,	O
converting	O
to	O
RCU	O
will	O
require	O
special	O
care	O
.	O
</s>
<s>
H	O
.	O
T	O
.	O
Kung	O
and	O
Q	O
.	O
Lehman	O
described	O
use	O
of	O
garbage	B-General_Concept
collectors	I-General_Concept
to	O
implement	O
RCU-like	O
access	O
to	O
a	O
binary	O
search	O
tree	O
.	O
</s>
<s>
Udi	O
Manber	O
and	O
Richard	O
Ladner	O
extended	O
Kung	O
's	O
and	O
Lehman	O
's	O
work	O
to	O
non-garbage-collected	O
environments	O
by	O
deferring	O
reclamation	O
until	O
all	O
threads	B-Operating_System
running	O
at	O
removal	O
time	O
have	O
terminated	O
,	O
which	O
works	O
in	O
environments	O
that	O
do	O
not	O
have	O
long-lived	O
threads	B-Operating_System
.	O
</s>
<s>
described	O
a	O
lazy	O
translation	B-Architecture
lookaside	I-Architecture
buffer	I-Architecture
(	O
TLB	O
)	O
implementation	O
that	O
deferred	O
reclaiming	O
virtual-address	O
space	O
until	O
all	O
CPUs	O
flushed	O
their	O
TLB	O
,	O
which	O
is	O
similar	O
in	O
spirit	O
to	O
some	O
RCU	O
implementations	O
.	O
</s>
<s>
This	O
patent	O
describes	O
an	O
RCU-like	O
mechanism	O
that	O
was	O
apparently	O
used	O
in	O
VM/XA	B-Application
on	O
IBM	B-Device
mainframes	I-Device
.	O
</s>
<s>
Aju	O
John	O
proposed	O
an	O
RCU-like	O
implementation	O
where	O
updaters	O
simply	O
wait	O
for	O
a	O
fixed	O
period	O
of	O
time	O
,	O
under	O
the	O
assumption	O
that	O
readers	O
would	O
all	O
complete	O
within	O
that	O
fixed	O
time	O
,	O
as	O
might	O
be	O
appropriate	O
in	O
a	O
hard	O
real-time	B-General_Concept
system	I-General_Concept
.	O
</s>
<s>
J	O
.	O
Slingwine	O
and	O
P	O
.	O
E	O
.	O
McKenney	O
received	O
US	O
Patent	O
5,442,758	O
in	O
August	O
1995	O
,	O
which	O
describes	O
RCU	O
as	O
implemented	O
in	O
DYNIX/ptx	B-Operating_System
and	O
later	O
in	O
the	O
Linux	B-Operating_System
kernel	I-Operating_System
.	O
</s>
<s>
B	O
.	O
Gamsa	O
,	O
O	O
.	O
Krieger	O
,	O
J	O
.	O
Appavoo	O
,	O
and	O
M	O
.	O
Stumm	O
described	O
an	O
RCU-like	O
mechanism	O
used	O
in	O
the	O
University	O
of	O
Toronto	O
and	O
the	O
closely	O
related	O
IBM	O
Research	O
K42	B-Operating_System
research	O
operating	O
systems	O
.	O
</s>
<s>
Rusty	O
Russell	O
and	O
Phil	O
Rumpf	O
described	O
RCU-like	O
techniques	O
for	O
handling	O
unloading	O
of	O
Linux	B-Operating_System
kernel	I-Operating_System
modules	O
.	O
</s>
