ref.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * net/tipc/ref.c: TIPC object registry code
  3. *
  4. * Copyright (c) 1991-2006, Ericsson AB
  5. * Copyright (c) 2004-2007, Wind River Systems
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the names of the copyright holders nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * Alternatively, this software may be distributed under the terms of the
  21. * GNU General Public License ("GPL") version 2 as published by the Free
  22. * Software Foundation.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. * POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include "core.h"
  37. #include "ref.h"
  38. /**
  39. * struct reference - TIPC object reference entry
  40. * @object: pointer to object associated with reference entry
  41. * @lock: spinlock controlling access to object
  42. * @ref: reference value for object (combines instance & array index info)
  43. */
  44. struct reference {
  45. void *object;
  46. spinlock_t lock;
  47. u32 ref;
  48. };
  49. /**
  50. * struct tipc_ref_table - table of TIPC object reference entries
  51. * @entries: pointer to array of reference entries
  52. * @capacity: array index of first unusable entry
  53. * @init_point: array index of first uninitialized entry
  54. * @first_free: array index of first unused object reference entry
  55. * @last_free: array index of last unused object reference entry
  56. * @index_mask: bitmask for array index portion of reference values
  57. * @start_mask: initial value for instance value portion of reference values
  58. */
  59. struct ref_table {
  60. struct reference *entries;
  61. u32 capacity;
  62. u32 init_point;
  63. u32 first_free;
  64. u32 last_free;
  65. u32 index_mask;
  66. u32 start_mask;
  67. };
  68. /*
  69. * Object reference table consists of 2**N entries.
  70. *
  71. * State Object ptr Reference
  72. * ----- ---------- ---------
  73. * In use non-NULL XXXX|own index
  74. * (XXXX changes each time entry is acquired)
  75. * Free NULL YYYY|next free index
  76. * (YYYY is one more than last used XXXX)
  77. * Uninitialized NULL 0
  78. *
  79. * Entry 0 is not used; this allows index 0 to denote the end of the free list.
  80. *
  81. * Note that a reference value of 0 does not necessarily indicate that an
  82. * entry is uninitialized, since the last entry in the free list could also
  83. * have a reference value of 0 (although this is unlikely).
  84. */
  85. static struct ref_table tipc_ref_table = { NULL };
  86. static DEFINE_RWLOCK(ref_table_lock);
  87. /**
  88. * tipc_ref_table_init - create reference table for objects
  89. */
  90. int tipc_ref_table_init(u32 requested_size, u32 start)
  91. {
  92. struct reference *table;
  93. u32 actual_size;
  94. /* account for unused entry, then round up size to a power of 2 */
  95. requested_size++;
  96. for (actual_size = 16; actual_size < requested_size; actual_size <<= 1)
  97. /* do nothing */ ;
  98. /* allocate table & mark all entries as uninitialized */
  99. table = __vmalloc(actual_size * sizeof(struct reference),
  100. GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO, PAGE_KERNEL);
  101. if (table == NULL)
  102. return -ENOMEM;
  103. tipc_ref_table.entries = table;
  104. tipc_ref_table.capacity = requested_size;
  105. tipc_ref_table.init_point = 1;
  106. tipc_ref_table.first_free = 0;
  107. tipc_ref_table.last_free = 0;
  108. tipc_ref_table.index_mask = actual_size - 1;
  109. tipc_ref_table.start_mask = start & ~tipc_ref_table.index_mask;
  110. return 0;
  111. }
  112. /**
  113. * tipc_ref_table_stop - destroy reference table for objects
  114. */
  115. void tipc_ref_table_stop(void)
  116. {
  117. if (!tipc_ref_table.entries)
  118. return;
  119. vfree(tipc_ref_table.entries);
  120. tipc_ref_table.entries = NULL;
  121. }
  122. /**
  123. * tipc_ref_acquire - create reference to an object
  124. *
  125. * Register an object pointer in reference table and lock the object.
  126. * Returns a unique reference value that is used from then on to retrieve the
  127. * object pointer, or to determine that the object has been deregistered.
  128. *
  129. * Note: The object is returned in the locked state so that the caller can
  130. * register a partially initialized object, without running the risk that
  131. * the object will be accessed before initialization is complete.
  132. */
  133. u32 tipc_ref_acquire(void *object, spinlock_t **lock)
  134. {
  135. struct reference *entry;
  136. u32 index;
  137. u32 index_mask;
  138. u32 next_plus_upper;
  139. u32 ref;
  140. if (!object) {
  141. err("Attempt to acquire reference to non-existent object\n");
  142. return 0;
  143. }
  144. if (!tipc_ref_table.entries) {
  145. err("Reference table not found during acquisition attempt\n");
  146. return 0;
  147. }
  148. /* take a free entry, if available; otherwise initialize a new entry */
  149. write_lock_bh(&ref_table_lock);
  150. if (tipc_ref_table.first_free) {
  151. index = tipc_ref_table.first_free;
  152. entry = &(tipc_ref_table.entries[index]);
  153. index_mask = tipc_ref_table.index_mask;
  154. /* take lock in case a previous user of entry still holds it */
  155. spin_lock_bh(&entry->lock);
  156. next_plus_upper = entry->ref;
  157. tipc_ref_table.first_free = next_plus_upper & index_mask;
  158. ref = (next_plus_upper & ~index_mask) + index;
  159. entry->ref = ref;
  160. entry->object = object;
  161. *lock = &entry->lock;
  162. }
  163. else if (tipc_ref_table.init_point < tipc_ref_table.capacity) {
  164. index = tipc_ref_table.init_point++;
  165. entry = &(tipc_ref_table.entries[index]);
  166. spin_lock_init(&entry->lock);
  167. spin_lock_bh(&entry->lock);
  168. ref = tipc_ref_table.start_mask + index;
  169. entry->ref = ref;
  170. entry->object = object;
  171. *lock = &entry->lock;
  172. }
  173. else {
  174. ref = 0;
  175. }
  176. write_unlock_bh(&ref_table_lock);
  177. return ref;
  178. }
  179. /**
  180. * tipc_ref_discard - invalidate references to an object
  181. *
  182. * Disallow future references to an object and free up the entry for re-use.
  183. * Note: The entry's spin_lock may still be busy after discard
  184. */
  185. void tipc_ref_discard(u32 ref)
  186. {
  187. struct reference *entry;
  188. u32 index;
  189. u32 index_mask;
  190. if (!tipc_ref_table.entries) {
  191. err("Reference table not found during discard attempt\n");
  192. return;
  193. }
  194. index_mask = tipc_ref_table.index_mask;
  195. index = ref & index_mask;
  196. entry = &(tipc_ref_table.entries[index]);
  197. write_lock_bh(&ref_table_lock);
  198. if (!entry->object) {
  199. err("Attempt to discard reference to non-existent object\n");
  200. goto exit;
  201. }
  202. if (entry->ref != ref) {
  203. err("Attempt to discard non-existent reference\n");
  204. goto exit;
  205. }
  206. /*
  207. * mark entry as unused; increment instance part of entry's reference
  208. * to invalidate any subsequent references
  209. */
  210. entry->object = NULL;
  211. entry->ref = (ref & ~index_mask) + (index_mask + 1);
  212. /* append entry to free entry list */
  213. if (tipc_ref_table.first_free == 0)
  214. tipc_ref_table.first_free = index;
  215. else
  216. tipc_ref_table.entries[tipc_ref_table.last_free].ref |= index;
  217. tipc_ref_table.last_free = index;
  218. exit:
  219. write_unlock_bh(&ref_table_lock);
  220. }
  221. /**
  222. * tipc_ref_lock - lock referenced object and return pointer to it
  223. */
  224. void *tipc_ref_lock(u32 ref)
  225. {
  226. if (likely(tipc_ref_table.entries)) {
  227. struct reference *entry;
  228. entry = &tipc_ref_table.entries[ref &
  229. tipc_ref_table.index_mask];
  230. if (likely(entry->ref != 0)) {
  231. spin_lock_bh(&entry->lock);
  232. if (likely((entry->ref == ref) && (entry->object)))
  233. return entry->object;
  234. spin_unlock_bh(&entry->lock);
  235. }
  236. }
  237. return NULL;
  238. }
  239. /**
  240. * tipc_ref_unlock - unlock referenced object
  241. */
  242. void tipc_ref_unlock(u32 ref)
  243. {
  244. if (likely(tipc_ref_table.entries)) {
  245. struct reference *entry;
  246. entry = &tipc_ref_table.entries[ref &
  247. tipc_ref_table.index_mask];
  248. if (likely((entry->ref == ref) && (entry->object)))
  249. spin_unlock_bh(&entry->lock);
  250. else
  251. err("Attempt to unlock non-existent reference\n");
  252. }
  253. }
  254. /**
  255. * tipc_ref_deref - return pointer referenced object (without locking it)
  256. */
  257. void *tipc_ref_deref(u32 ref)
  258. {
  259. if (likely(tipc_ref_table.entries)) {
  260. struct reference *entry;
  261. entry = &tipc_ref_table.entries[ref &
  262. tipc_ref_table.index_mask];
  263. if (likely(entry->ref == ref))
  264. return entry->object;
  265. }
  266. return NULL;
  267. }