@@ -14,19 +14,19 @@ Using 'nulls'
14
14
=============
15
15
16
16
Using special makers (called 'nulls') is a convenient way
17
- to solve following problem :
17
+ to solve following problem.
18
18
19
- A typical RCU linked list managing objects which are
20
- allocated with SLAB_TYPESAFE_BY_RCU kmem_cache can
21
- use following algos :
19
+ Without 'nulls', a typical RCU linked list managing objects which are
20
+ allocated with SLAB_TYPESAFE_BY_RCU kmem_cache can use the following
21
+ algorithms :
22
22
23
- 1) Lookup algo
24
- --------------
23
+ 1) Lookup algorithm
24
+ -------------------
25
25
26
26
::
27
27
28
- rcu_read_lock()
29
28
begin:
29
+ rcu_read_lock()
30
30
obj = lockless_lookup(key);
31
31
if (obj) {
32
32
if (!try_get_ref(obj)) // might fail for free objects
@@ -38,6 +38,7 @@ use following algos :
38
38
*/
39
39
if (obj->key != key) { // not the object we expected
40
40
put_ref(obj);
41
+ rcu_read_unlock();
41
42
goto begin;
42
43
}
43
44
}
@@ -52,9 +53,9 @@ but a version with an additional memory barrier (smp_rmb())
52
53
{
53
54
struct hlist_node *node, *next;
54
55
for (pos = rcu_dereference((head)->first);
55
- pos && ({ next = pos->next; smp_rmb(); prefetch(next); 1; }) &&
56
- ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1; });
57
- pos = rcu_dereference(next))
56
+ pos && ({ next = pos->next; smp_rmb(); prefetch(next); 1; }) &&
57
+ ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1; });
58
+ pos = rcu_dereference(next))
58
59
if (obj->key == key)
59
60
return obj;
60
61
return NULL;
@@ -64,9 +65,9 @@ And note the traditional hlist_for_each_entry_rcu() misses this smp_rmb()::
64
65
65
66
struct hlist_node *node;
66
67
for (pos = rcu_dereference((head)->first);
67
- pos && ({ prefetch(pos->next); 1; }) &&
68
- ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1; });
69
- pos = rcu_dereference(pos->next))
68
+ pos && ({ prefetch(pos->next); 1; }) &&
69
+ ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1; });
70
+ pos = rcu_dereference(pos->next))
70
71
if (obj->key == key)
71
72
return obj;
72
73
return NULL;
@@ -82,36 +83,32 @@ Quoting Corey Minyard::
82
83
solved by pre-fetching the "next" field (with proper barriers) before
83
84
checking the key."
84
85
85
- 2) Insert algo
86
- --------------
86
+ 2) Insertion algorithm
87
+ ----------------------
87
88
88
89
We need to make sure a reader cannot read the new 'obj->obj_next' value
89
- and previous value of 'obj->key'. Or else , an item could be deleted
90
+ and previous value of 'obj->key'. Otherwise , an item could be deleted
90
91
from a chain, and inserted into another chain. If new chain was empty
91
- before the move, 'next' pointer is NULL, and lockless reader can
92
- not detect it missed following items in original chain.
92
+ before the move, 'next' pointer is NULL, and lockless reader can not
93
+ detect the fact that it missed following items in original chain.
93
94
94
95
::
95
96
96
97
/*
97
- * Please note that new inserts are done at the head of list,
98
- * not in the middle or end.
99
- */
98
+ * Please note that new inserts are done at the head of list,
99
+ * not in the middle or end.
100
+ */
100
101
obj = kmem_cache_alloc(...);
101
102
lock_chain(); // typically a spin_lock()
102
103
obj->key = key;
103
- /*
104
- * we need to make sure obj->key is updated before obj->next
105
- * or obj->refcnt
106
- */
107
- smp_wmb();
108
- atomic_set(&obj->refcnt, 1);
104
+ atomic_set_release(&obj->refcnt, 1); // key before refcnt
109
105
hlist_add_head_rcu(&obj->obj_node, list);
110
106
unlock_chain(); // typically a spin_unlock()
111
107
112
108
113
- 3) Remove algo
114
- --------------
109
+ 3) Removal algorithm
110
+ --------------------
111
+
115
112
Nothing special here, we can use a standard RCU hlist deletion.
116
113
But thanks to SLAB_TYPESAFE_BY_RCU, beware a deleted object can be reused
117
114
very very fast (before the end of RCU grace period)
@@ -133,7 +130,7 @@ Avoiding extra smp_rmb()
133
130
========================
134
131
135
132
With hlist_nulls we can avoid extra smp_rmb() in lockless_lookup()
136
- and extra smp_wmb () in insert function.
133
+ and extra _release () in insert function.
137
134
138
135
For example, if we choose to store the slot number as the 'nulls'
139
136
end-of-list marker for each slot of the hash table, we can detect
@@ -142,59 +139,61 @@ to another chain) checking the final 'nulls' value if
142
139
the lookup met the end of chain. If final 'nulls' value
143
140
is not the slot number, then we must restart the lookup at
144
141
the beginning. If the object was moved to the same chain,
145
- then the reader doesn't care : It might eventually
142
+ then the reader doesn't care: It might occasionally
146
143
scan the list again without harm.
147
144
148
145
149
- 1) lookup algo
150
- --------------
146
+ 1) lookup algorithm
147
+ -------------------
151
148
152
149
::
153
150
154
151
head = &table[slot];
155
- rcu_read_lock();
156
152
begin:
153
+ rcu_read_lock();
157
154
hlist_nulls_for_each_entry_rcu(obj, node, head, member) {
158
155
if (obj->key == key) {
159
- if (!try_get_ref(obj)) // might fail for free objects
156
+ if (!try_get_ref(obj)) { // might fail for free objects
157
+ rcu_read_unlock();
160
158
goto begin;
159
+ }
161
160
if (obj->key != key) { // not the object we expected
162
161
put_ref(obj);
162
+ rcu_read_unlock();
163
163
goto begin;
164
164
}
165
- goto out;
165
+ goto out;
166
+ }
167
+ }
168
+
169
+ // If the nulls value we got at the end of this lookup is
170
+ // not the expected one, we must restart lookup.
171
+ // We probably met an item that was moved to another chain.
172
+ if (get_nulls_value(node) != slot) {
173
+ put_ref(obj);
174
+ rcu_read_unlock();
175
+ goto begin;
166
176
}
167
- /*
168
- * if the nulls value we got at the end of this lookup is
169
- * not the expected one, we must restart lookup.
170
- * We probably met an item that was moved to another chain.
171
- */
172
- if (get_nulls_value(node) != slot)
173
- goto begin;
174
177
obj = NULL;
175
178
176
179
out:
177
180
rcu_read_unlock();
178
181
179
- 2) Insert function
180
- ------------------
182
+ 2) Insert algorithm
183
+ -------------------
181
184
182
185
::
183
186
184
187
/*
185
- * Please note that new inserts are done at the head of list,
186
- * not in the middle or end.
187
- */
188
+ * Please note that new inserts are done at the head of list,
189
+ * not in the middle or end.
190
+ */
188
191
obj = kmem_cache_alloc(cachep);
189
192
lock_chain(); // typically a spin_lock()
190
193
obj->key = key;
194
+ atomic_set_release(&obj->refcnt, 1); // key before refcnt
191
195
/*
192
- * changes to obj->key must be visible before refcnt one
193
- */
194
- smp_wmb();
195
- atomic_set(&obj->refcnt, 1);
196
- /*
197
- * insert obj in RCU way (readers might be traversing chain)
198
- */
196
+ * insert obj in RCU way (readers might be traversing chain)
197
+ */
199
198
hlist_nulls_add_head_rcu(&obj->obj_node, list);
200
199
unlock_chain(); // typically a spin_unlock()
0 commit comments