blob: 4202ad0931300fe776697a081494a433b54e3b61 [file] [log] [blame]
Paul E. McKenneyd19720a2006-02-01 03:06:42 -08001Reference-count design for elements of lists/arrays protected by RCU.
Dipankar Sarmac0dfb292005-09-09 13:04:09 -07002
Paul E. McKenneyd19720a2006-02-01 03:06:42 -08003Reference counting on elements of lists which are protected by traditional
4reader/writer spinlocks or semaphores are straightforward:
Dipankar Sarmac0dfb292005-09-09 13:04:09 -07005
Nick Piggin095975d2006-01-08 01:02:19 -080061. 2.
7add() search_and_reference()
8{ {
9 alloc_object read_lock(&list_lock);
10 ... search_for_element
11 atomic_set(&el->rc, 1); atomic_inc(&el->rc);
12 write_lock(&list_lock); ...
13 add_element read_unlock(&list_lock);
14 ... ...
15 write_unlock(&list_lock); }
Dipankar Sarmac0dfb292005-09-09 13:04:09 -070016}
17
183. 4.
19release_referenced() delete()
20{ {
Nick Piggin095975d2006-01-08 01:02:19 -080021 ... write_lock(&list_lock);
22 atomic_dec(&el->rc, relfunc) ...
23 ... delete_element
24} write_unlock(&list_lock);
25 ...
26 if (atomic_dec_and_test(&el->rc))
27 kfree(el);
28 ...
Dipankar Sarmac0dfb292005-09-09 13:04:09 -070029 }
30
Paul E. McKenneyd19720a2006-02-01 03:06:42 -080031If this list/array is made lock free using RCU as in changing the
Lai Jiangshane8aed682008-09-10 11:01:07 +080032write_lock() in add() and delete() to spin_lock() and changing read_lock()
33in search_and_reference() to rcu_read_lock(), the atomic_inc() in
34search_and_reference() could potentially hold reference to an element which
Paul E. McKenneyd19720a2006-02-01 03:06:42 -080035has already been deleted from the list/array. Use atomic_inc_not_zero()
36in this scenario as follows:
Dipankar Sarmac0dfb292005-09-09 13:04:09 -070037
381. 2.
39add() search_and_reference()
40{ {
Nick Piggin095975d2006-01-08 01:02:19 -080041 alloc_object rcu_read_lock();
42 ... search_for_element
Lai Jiangshane8aed682008-09-10 11:01:07 +080043 atomic_set(&el->rc, 1); if (!atomic_inc_not_zero(&el->rc)) {
44 spin_lock(&list_lock); rcu_read_unlock();
Nick Piggin095975d2006-01-08 01:02:19 -080045 return FAIL;
46 add_element }
47 ... ...
Lai Jiangshane8aed682008-09-10 11:01:07 +080048 spin_unlock(&list_lock); rcu_read_unlock();
Dipankar Sarmac0dfb292005-09-09 13:04:09 -070049} }
503. 4.
51release_referenced() delete()
52{ {
Lai Jiangshane8aed682008-09-10 11:01:07 +080053 ... spin_lock(&list_lock);
Paul E. McKenneyd19720a2006-02-01 03:06:42 -080054 if (atomic_dec_and_test(&el->rc)) ...
55 call_rcu(&el->head, el_free); delete_element
Lai Jiangshane8aed682008-09-10 11:01:07 +080056 ... spin_unlock(&list_lock);
Paul E. McKenneyd19720a2006-02-01 03:06:42 -080057} ...
Nick Piggin095975d2006-01-08 01:02:19 -080058 if (atomic_dec_and_test(&el->rc))
59 call_rcu(&el->head, el_free);
60 ...
Dipankar Sarmac0dfb292005-09-09 13:04:09 -070061 }
62
Paul E. McKenneyd19720a2006-02-01 03:06:42 -080063Sometimes, a reference to the element needs to be obtained in the
64update (write) stream. In such cases, atomic_inc_not_zero() might be
65overkill, since we hold the update-side spinlock. One might instead
66use atomic_inc() in such cases.