Commit de1dbcee authored by Joel Fernandes (Google)'s avatar Joel Fernandes (Google) Committed by Paul E. McKenney

doc/rcuref: Document real world examples in kernel

Document similar real world examples in the kernel corresponding to the
second and third code snippets. Also correct an issue in
release_referenced() in the code snippet example.

Cc: oleg@redhat.com
Cc: jannh@google.com
Signed-off-by: default avatarJoel Fernandes (Google) <joel@joelfernandes.org>
[ paulmck: Do a bit of wordsmithing. ]
Signed-off-by: default avatarPaul E. McKenney <paulmck@linux.ibm.com>
parent a188339c
...@@ -12,6 +12,7 @@ please read on. ...@@ -12,6 +12,7 @@ please read on.
Reference counting on elements of lists which are protected by traditional Reference counting on elements of lists which are protected by traditional
reader/writer spinlocks or semaphores are straightforward: reader/writer spinlocks or semaphores are straightforward:
CODE LISTING A:
1. 2. 1. 2.
add() search_and_reference() add() search_and_reference()
{ { { {
...@@ -28,7 +29,8 @@ add() search_and_reference() ...@@ -28,7 +29,8 @@ add() search_and_reference()
release_referenced() delete() release_referenced() delete()
{ { { {
... write_lock(&list_lock); ... write_lock(&list_lock);
atomic_dec(&el->rc, relfunc) ... if(atomic_dec_and_test(&el->rc)) ...
kfree(el);
... remove_element ... remove_element
} write_unlock(&list_lock); } write_unlock(&list_lock);
... ...
...@@ -44,6 +46,7 @@ search_and_reference() could potentially hold reference to an element which ...@@ -44,6 +46,7 @@ search_and_reference() could potentially hold reference to an element which
has already been deleted from the list/array. Use atomic_inc_not_zero() has already been deleted from the list/array. Use atomic_inc_not_zero()
in this scenario as follows: in this scenario as follows:
CODE LISTING B:
1. 2. 1. 2.
add() search_and_reference() add() search_and_reference()
{ { { {
...@@ -79,6 +82,7 @@ search_and_reference() code path. In such cases, the ...@@ -79,6 +82,7 @@ search_and_reference() code path. In such cases, the
atomic_dec_and_test() may be moved from delete() to el_free() atomic_dec_and_test() may be moved from delete() to el_free()
as follows: as follows:
CODE LISTING C:
1. 2. 1. 2.
add() search_and_reference() add() search_and_reference()
{ { { {
...@@ -114,6 +118,17 @@ element can therefore safely be freed. This in turn guarantees that if ...@@ -114,6 +118,17 @@ element can therefore safely be freed. This in turn guarantees that if
any reader finds the element, that reader may safely acquire a reference any reader finds the element, that reader may safely acquire a reference
without checking the value of the reference counter. without checking the value of the reference counter.
A clear advantage of the RCU-based pattern in listing C over the one
in listing B is that any call to search_and_reference() that locates
a given object will succeed in obtaining a reference to that object,
even given a concurrent invocation of delete() for that same object.
Similarly, a clear advantage of both listings B and C over listing A is
that a call to delete() is not delayed even if there are an arbitrarily
large number of calls to search_and_reference() searching for the same
object that delete() was invoked on. Instead, all that is delayed is
the eventual invocation of kfree(), which is usually not a problem on
modern computer systems, even the small ones.
In cases where delete() can sleep, synchronize_rcu() can be called from In cases where delete() can sleep, synchronize_rcu() can be called from
delete(), so that el_free() can be subsumed into delete as follows: delete(), so that el_free() can be subsumed into delete as follows:
...@@ -130,3 +145,7 @@ delete() ...@@ -130,3 +145,7 @@ delete()
kfree(el); kfree(el);
... ...
} }
As additional examples in the kernel, the pattern in listing C is used by
reference counting of struct pid, while the pattern in listing B is used by
struct posix_acl.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment