arrayRCU.txt 5.1 KB

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