arrayRCU.rst 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. .. _array_rcu_doc:
  2. Using RCU to Protect Read-Mostly Arrays
  3. =======================================
  4. Although RCU is more commonly used to protect linked lists, it can
  5. also be used to protect arrays. Three situations are as follows:
  6. 1. :ref:`Hash Tables <hash_tables>`
  7. 2. :ref:`Static Arrays <static_arrays>`
  8. 3. :ref:`Resizable Arrays <resizable_arrays>`
  9. Each of these three situations involves an RCU-protected pointer to an
  10. array that is separately indexed. It might be tempting to consider use
  11. of RCU to instead protect the index into an array, however, this use
  12. case is **not** supported. The problem with RCU-protected indexes into
  13. arrays is that compilers can play way too many optimization games with
  14. integers, which means that the rules governing handling of these indexes
  15. are far more trouble than they are worth. If RCU-protected indexes into
  16. arrays prove to be particularly valuable (which they have not thus far),
  17. explicit cooperation from the compiler will be required to permit them
  18. to be safely used.
  19. That aside, each of the three RCU-protected pointer situations are
  20. described in the following sections.
  21. .. _hash_tables:
  22. Situation 1: Hash Tables
  23. ------------------------
  24. Hash tables are often implemented as an array, where each array entry
  25. has a linked-list hash chain. Each hash chain can be protected by RCU
  26. as described in the listRCU.txt document. This approach also applies
  27. to other array-of-list situations, such as radix trees.
  28. .. _static_arrays:
  29. Situation 2: Static Arrays
  30. --------------------------
  31. Static arrays, where the data (rather than a pointer to the data) is
  32. located in each array element, and where the array is never resized,
  33. have not been used with RCU. Rik van Riel recommends using seqlock in
  34. this situation, which would also have minimal read-side overhead as long
  35. as updates are rare.
  36. Quick Quiz:
  37. Why is it so important that updates be rare when using seqlock?
  38. :ref:`Answer to Quick Quiz <answer_quick_quiz_seqlock>`
  39. .. _resizable_arrays:
  40. Situation 3: Resizable Arrays
  41. ------------------------------
  42. Use of RCU for resizable arrays is demonstrated by the grow_ary()
  43. function formerly used by the System V IPC code. The array is used
  44. to map from semaphore, message-queue, and shared-memory IDs to the data
  45. structure that represents the corresponding IPC construct. The grow_ary()
  46. function does not acquire any locks; instead its caller must hold the
  47. ids->sem semaphore.
  48. The grow_ary() function, shown below, does some limit checks, allocates a
  49. new ipc_id_ary, copies the old to the new portion of the new, initializes
  50. the remainder of the new, updates the ids->entries pointer to point to
  51. the new array, and invokes ipc_rcu_putref() to free up the old array.
  52. Note that rcu_assign_pointer() is used to update the ids->entries pointer,
  53. which includes any memory barriers required on whatever architecture
  54. you are running on::
  55. static int grow_ary(struct ipc_ids* ids, int newsize)
  56. {
  57. struct ipc_id_ary* new;
  58. struct ipc_id_ary* old;
  59. int i;
  60. int size = ids->entries->size;
  61. if(newsize > IPCMNI)
  62. newsize = IPCMNI;
  63. if(newsize <= size)
  64. return newsize;
  65. new = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*newsize +
  66. sizeof(struct ipc_id_ary));
  67. if(new == NULL)
  68. return size;
  69. new->size = newsize;
  70. memcpy(new->p, ids->entries->p,
  71. sizeof(struct kern_ipc_perm *)*size +
  72. sizeof(struct ipc_id_ary));
  73. for(i=size;i<newsize;i++) {
  74. new->p[i] = NULL;
  75. }
  76. old = ids->entries;
  77. /*
  78. * Use rcu_assign_pointer() to make sure the memcpyed
  79. * contents of the new array are visible before the new
  80. * array becomes visible.
  81. */
  82. rcu_assign_pointer(ids->entries, new);
  83. ipc_rcu_putref(old);
  84. return newsize;
  85. }
  86. The ipc_rcu_putref() function decrements the array's reference count
  87. and then, if the reference count has dropped to zero, uses call_rcu()
  88. to free the array after a grace period has elapsed.
  89. The array is traversed by the ipc_lock() function. This function
  90. indexes into the array under the protection of rcu_read_lock(),
  91. using rcu_dereference() to pick up the pointer to the array so
  92. that it may later safely be dereferenced -- memory barriers are
  93. required on the Alpha CPU. Since the size of the array is stored
  94. with the array itself, there can be no array-size mismatches, so
  95. a simple check suffices. The pointer to the structure corresponding
  96. to the desired IPC object is placed in "out", with NULL indicating
  97. a non-existent entry. After acquiring "out->lock", the "out->deleted"
  98. flag indicates whether the IPC object is in the process of being
  99. deleted, and, if not, the pointer is returned::
  100. struct kern_ipc_perm* ipc_lock(struct ipc_ids* ids, int id)
  101. {
  102. struct kern_ipc_perm* out;
  103. int lid = id % SEQ_MULTIPLIER;
  104. struct ipc_id_ary* entries;
  105. rcu_read_lock();
  106. entries = rcu_dereference(ids->entries);
  107. if(lid >= entries->size) {
  108. rcu_read_unlock();
  109. return NULL;
  110. }
  111. out = entries->p[lid];
  112. if(out == NULL) {
  113. rcu_read_unlock();
  114. return NULL;
  115. }
  116. spin_lock(&out->lock);
  117. /* ipc_rmid() may have already freed the ID while ipc_lock
  118. * was spinning: here verify that the structure is still valid
  119. */
  120. if (out->deleted) {
  121. spin_unlock(&out->lock);
  122. rcu_read_unlock();
  123. return NULL;
  124. }
  125. return out;
  126. }
  127. .. _answer_quick_quiz_seqlock:
  128. Answer to Quick Quiz:
  129. Why is it so important that updates be rare when using seqlock?
  130. The reason that it is important that updates be rare when
  131. using seqlock is that frequent updates can livelock readers.
  132. One way to avoid this problem is to assign a seqlock for
  133. each array entry rather than to the entire array.