arrayRCU.txt 4.4 KB

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