rculist_nulls.txt 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. Using hlist_nulls to protect read-mostly linked lists and
  2. objects using SLAB_DESTROY_BY_RCU allocations.
  3. Please read the basics in Documentation/RCU/listRCU.txt
  4. Using special makers (called 'nulls') is a convenient way
  5. to solve following problem :
  6. A typical RCU linked list managing objects which are
  7. allocated with SLAB_DESTROY_BY_RCU kmem_cache can
  8. use following algos :
  9. 1) Lookup algo
  10. --------------
  11. rcu_read_lock()
  12. begin:
  13. obj = lockless_lookup(key);
  14. if (obj) {
  15. if (!try_get_ref(obj)) // might fail for free objects
  16. goto begin;
  17. /*
  18. * Because a writer could delete object, and a writer could
  19. * reuse these object before the RCU grace period, we
  20. * must check key after getting the reference on object
  21. */
  22. if (obj->key != key) { // not the object we expected
  23. put_ref(obj);
  24. goto begin;
  25. }
  26. }
  27. rcu_read_unlock();
  28. Beware that lockless_lookup(key) cannot use traditional hlist_for_each_entry_rcu()
  29. but a version with an additional memory barrier (smp_rmb())
  30. lockless_lookup(key)
  31. {
  32. struct hlist_node *node, *next;
  33. for (pos = rcu_dereference((head)->first);
  34. pos && ({ next = pos->next; smp_rmb(); prefetch(next); 1; }) &&
  35. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1; });
  36. pos = rcu_dereference(next))
  37. if (obj->key == key)
  38. return obj;
  39. return NULL;
  40. And note the traditional hlist_for_each_entry_rcu() misses this smp_rmb() :
  41. struct hlist_node *node;
  42. for (pos = rcu_dereference((head)->first);
  43. pos && ({ prefetch(pos->next); 1; }) &&
  44. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1; });
  45. pos = rcu_dereference(pos->next))
  46. if (obj->key == key)
  47. return obj;
  48. return NULL;
  49. }
  50. Quoting Corey Minyard :
  51. "If the object is moved from one list to another list in-between the
  52. time the hash is calculated and the next field is accessed, and the
  53. object has moved to the end of a new list, the traversal will not
  54. complete properly on the list it should have, since the object will
  55. be on the end of the new list and there's not a way to tell it's on a
  56. new list and restart the list traversal. I think that this can be
  57. solved by pre-fetching the "next" field (with proper barriers) before
  58. checking the key."
  59. 2) Insert algo :
  60. ----------------
  61. We need to make sure a reader cannot read the new 'obj->obj_next' value
  62. and previous value of 'obj->key'. Or else, an item could be deleted
  63. from a chain, and inserted into another chain. If new chain was empty
  64. before the move, 'next' pointer is NULL, and lockless reader can
  65. not detect it missed following items in original chain.
  66. /*
  67. * Please note that new inserts are done at the head of list,
  68. * not in the middle or end.
  69. */
  70. obj = kmem_cache_alloc(...);
  71. lock_chain(); // typically a spin_lock()
  72. obj->key = key;
  73. /*
  74. * we need to make sure obj->key is updated before obj->next
  75. * or obj->refcnt
  76. */
  77. smp_wmb();
  78. atomic_set(&obj->refcnt, 1);
  79. hlist_add_head_rcu(&obj->obj_node, list);
  80. unlock_chain(); // typically a spin_unlock()
  81. 3) Remove algo
  82. --------------
  83. Nothing special here, we can use a standard RCU hlist deletion.
  84. But thanks to SLAB_DESTROY_BY_RCU, beware a deleted object can be reused
  85. very very fast (before the end of RCU grace period)
  86. if (put_last_reference_on(obj) {
  87. lock_chain(); // typically a spin_lock()
  88. hlist_del_init_rcu(&obj->obj_node);
  89. unlock_chain(); // typically a spin_unlock()
  90. kmem_cache_free(cachep, obj);
  91. }
  92. --------------------------------------------------------------------------
  93. With hlist_nulls we can avoid extra smp_rmb() in lockless_lookup()
  94. and extra smp_wmb() in insert function.
  95. For example, if we choose to store the slot number as the 'nulls'
  96. end-of-list marker for each slot of the hash table, we can detect
  97. a race (some writer did a delete and/or a move of an object
  98. to another chain) checking the final 'nulls' value if
  99. the lookup met the end of chain. If final 'nulls' value
  100. is not the slot number, then we must restart the lookup at
  101. the beginning. If the object was moved to the same chain,
  102. then the reader doesn't care : It might eventually
  103. scan the list again without harm.
  104. 1) lookup algo
  105. head = &table[slot];
  106. rcu_read_lock();
  107. begin:
  108. hlist_nulls_for_each_entry_rcu(obj, node, head, member) {
  109. if (obj->key == key) {
  110. if (!try_get_ref(obj)) // might fail for free objects
  111. goto begin;
  112. if (obj->key != key) { // not the object we expected
  113. put_ref(obj);
  114. goto begin;
  115. }
  116. goto out;
  117. }
  118. /*
  119. * if the nulls value we got at the end of this lookup is
  120. * not the expected one, we must restart lookup.
  121. * We probably met an item that was moved to another chain.
  122. */
  123. if (get_nulls_value(node) != slot)
  124. goto begin;
  125. obj = NULL;
  126. out:
  127. rcu_read_unlock();
  128. 2) Insert function :
  129. --------------------
  130. /*
  131. * Please note that new inserts are done at the head of list,
  132. * not in the middle or end.
  133. */
  134. obj = kmem_cache_alloc(cachep);
  135. lock_chain(); // typically a spin_lock()
  136. obj->key = key;
  137. /*
  138. * changes to obj->key must be visible before refcnt one
  139. */
  140. smp_wmb();
  141. atomic_set(&obj->refcnt, 1);
  142. /*
  143. * insert obj in RCU way (readers might be traversing chain)
  144. */
  145. hlist_nulls_add_head_rcu(&obj->obj_node, list);
  146. unlock_chain(); // typically a spin_unlock()