Path: blob/master/Documentation/RCU/rculist_nulls.txt
10821 views
Using hlist_nulls to protect read-mostly linked lists and1objects using SLAB_DESTROY_BY_RCU allocations.23Please read the basics in Documentation/RCU/listRCU.txt45Using special makers (called 'nulls') is a convenient way6to solve following problem :78A typical RCU linked list managing objects which are9allocated with SLAB_DESTROY_BY_RCU kmem_cache can10use following algos :11121) Lookup algo13--------------14rcu_read_lock()15begin:16obj = lockless_lookup(key);17if (obj) {18if (!try_get_ref(obj)) // might fail for free objects19goto begin;20/*21* Because a writer could delete object, and a writer could22* reuse these object before the RCU grace period, we23* must check key after getting the reference on object24*/25if (obj->key != key) { // not the object we expected26put_ref(obj);27goto begin;28}29}30rcu_read_unlock();3132Beware that lockless_lookup(key) cannot use traditional hlist_for_each_entry_rcu()33but a version with an additional memory barrier (smp_rmb())3435lockless_lookup(key)36{37struct hlist_node *node, *next;38for (pos = rcu_dereference((head)->first);39pos && ({ next = pos->next; smp_rmb(); prefetch(next); 1; }) &&40({ tpos = hlist_entry(pos, typeof(*tpos), member); 1; });41pos = rcu_dereference(next))42if (obj->key == key)43return obj;44return NULL;4546And note the traditional hlist_for_each_entry_rcu() misses this smp_rmb() :4748struct hlist_node *node;49for (pos = rcu_dereference((head)->first);50pos && ({ prefetch(pos->next); 1; }) &&51({ tpos = hlist_entry(pos, typeof(*tpos), member); 1; });52pos = rcu_dereference(pos->next))53if (obj->key == key)54return obj;55return NULL;56}5758Quoting Corey Minyard :5960"If the object is moved from one list to another list in-between the61time the hash is calculated and the next field is accessed, and the62object has moved to the end of a new list, the traversal will not63complete properly on the list it should have, since the object will64be on the end of the new list and there's not a way to tell it's on a65new list and restart the list traversal. I think that this can be66solved by pre-fetching the "next" field (with proper barriers) before67checking the key."68692) Insert algo :70----------------7172We need to make sure a reader cannot read the new 'obj->obj_next' value73and previous value of 'obj->key'. Or else, an item could be deleted74from a chain, and inserted into another chain. If new chain was empty75before the move, 'next' pointer is NULL, and lockless reader can76not detect it missed following items in original chain.7778/*79* Please note that new inserts are done at the head of list,80* not in the middle or end.81*/82obj = kmem_cache_alloc(...);83lock_chain(); // typically a spin_lock()84obj->key = key;85/*86* we need to make sure obj->key is updated before obj->next87* or obj->refcnt88*/89smp_wmb();90atomic_set(&obj->refcnt, 1);91hlist_add_head_rcu(&obj->obj_node, list);92unlock_chain(); // typically a spin_unlock()9394953) Remove algo96--------------97Nothing special here, we can use a standard RCU hlist deletion.98But thanks to SLAB_DESTROY_BY_RCU, beware a deleted object can be reused99very very fast (before the end of RCU grace period)100101if (put_last_reference_on(obj) {102lock_chain(); // typically a spin_lock()103hlist_del_init_rcu(&obj->obj_node);104unlock_chain(); // typically a spin_unlock()105kmem_cache_free(cachep, obj);106}107108109110--------------------------------------------------------------------------111With hlist_nulls we can avoid extra smp_rmb() in lockless_lookup()112and extra smp_wmb() in insert function.113114For example, if we choose to store the slot number as the 'nulls'115end-of-list marker for each slot of the hash table, we can detect116a race (some writer did a delete and/or a move of an object117to another chain) checking the final 'nulls' value if118the lookup met the end of chain. If final 'nulls' value119is not the slot number, then we must restart the lookup at120the beginning. If the object was moved to the same chain,121then the reader doesn't care : It might eventually122scan the list again without harm.1231241251) lookup algo126127head = &table[slot];128rcu_read_lock();129begin:130hlist_nulls_for_each_entry_rcu(obj, node, head, member) {131if (obj->key == key) {132if (!try_get_ref(obj)) // might fail for free objects133goto begin;134if (obj->key != key) { // not the object we expected135put_ref(obj);136goto begin;137}138goto out;139}140/*141* if the nulls value we got at the end of this lookup is142* not the expected one, we must restart lookup.143* We probably met an item that was moved to another chain.144*/145if (get_nulls_value(node) != slot)146goto begin;147obj = NULL;148149out:150rcu_read_unlock();1511522) Insert function :153--------------------154155/*156* Please note that new inserts are done at the head of list,157* not in the middle or end.158*/159obj = kmem_cache_alloc(cachep);160lock_chain(); // typically a spin_lock()161obj->key = key;162/*163* changes to obj->key must be visible before refcnt one164*/165smp_wmb();166atomic_set(&obj->refcnt, 1);167/*168* insert obj in RCU way (readers might be traversing chain)169*/170hlist_nulls_add_head_rcu(&obj->obj_node, list);171unlock_chain(); // typically a spin_unlock()172173174