rculist_nulls.txt 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 geting 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. atomic_inc(&obj->refcnt);
  74. /*
  75. * we need to make sure obj->key is updated before obj->next
  76. */
  77. smp_wmb();
  78. hlist_add_head_rcu(&obj->obj_node, list);
  79. unlock_chain(); // typically a spin_unlock()
  80. 3) Remove algo
  81. --------------
  82. Nothing special here, we can use a standard RCU hlist deletion.
  83. But thanks to SLAB_DESTROY_BY_RCU, beware a deleted object can be reused
  84. very very fast (before the end of RCU grace period)
  85. if (put_last_reference_on(obj) {
  86. lock_chain(); // typically a spin_lock()
  87. hlist_del_init_rcu(&obj->obj_node);
  88. unlock_chain(); // typically a spin_unlock()
  89. kmem_cache_free(cachep, obj);
  90. }
  91. --------------------------------------------------------------------------
  92. With hlist_nulls we can avoid extra smp_rmb() in lockless_lookup()
  93. and extra smp_wmb() in insert function.
  94. For example, if we choose to store the slot number as the 'nulls'
  95. end-of-list marker for each slot of the hash table, we can detect
  96. a race (some writer did a delete and/or a move of an object
  97. to another chain) checking the final 'nulls' value if
  98. the lookup met the end of chain. If final 'nulls' value
  99. is not the slot number, then we must restart the lookup at
  100. the begining. If the object was moved to same chain,
  101. then the reader doesnt care : It might eventually
  102. scan the list again without harm.
  103. 1) lookup algo
  104. head = &table[slot];
  105. rcu_read_lock();
  106. begin:
  107. hlist_nulls_for_each_entry_rcu(obj, node, head, member) {
  108. if (obj->key == key) {
  109. if (!try_get_ref(obj)) // might fail for free objects
  110. goto begin;
  111. if (obj->key != key) { // not the object we expected
  112. put_ref(obj);
  113. goto begin;
  114. }
  115. goto out;
  116. }
  117. /*
  118. * if the nulls value we got at the end of this lookup is
  119. * not the expected one, we must restart lookup.
  120. * We probably met an item that was moved to another chain.
  121. */
  122. if (get_nulls_value(node) != slot)
  123. goto begin;
  124. obj = NULL;
  125. out:
  126. rcu_read_unlock();
  127. 2) Insert function :
  128. --------------------
  129. /*
  130. * Please note that new inserts are done at the head of list,
  131. * not in the middle or end.
  132. */
  133. obj = kmem_cache_alloc(cachep);
  134. lock_chain(); // typically a spin_lock()
  135. obj->key = key;
  136. atomic_set(&obj->refcnt, 1);
  137. /*
  138. * insert obj in RCU way (readers might be traversing chain)
  139. */
  140. hlist_nulls_add_head_rcu(&obj->obj_node, list);
  141. unlock_chain(); // typically a spin_unlock()