ref.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. #include "port.h"
  39. #include "subscr.h"
  40. #include "name_distr.h"
  41. #include "name_table.h"
  42. #include "config.h"
  43. #include "discover.h"
  44. #include "bearer.h"
  45. #include "node.h"
  46. #include "bcast.h"
  47. /**
  48. * struct reference - TIPC object reference entry
  49. * @object: pointer to object associated with reference entry
  50. * @lock: spinlock controlling access to object
  51. * @data: reference value for object (combines instance & array index info)
  52. */
  53. struct reference {
  54. void *object;
  55. spinlock_t lock;
  56. union {
  57. u32 next_plus_upper;
  58. u32 reference;
  59. } data;
  60. };
  61. /**
  62. * struct tipc_ref_table - table of TIPC object reference entries
  63. * @entries: pointer to array of reference entries
  64. * @capacity: array index of first unusable entry
  65. * @init_point: array index of first uninitialized entry
  66. * @first_free: array index of first unused object reference entry
  67. * @last_free: array index of last unused object reference entry
  68. * @index_mask: bitmask for array index portion of reference values
  69. * @start_mask: initial value for instance value portion of reference values
  70. */
  71. struct ref_table {
  72. struct reference *entries;
  73. u32 capacity;
  74. u32 init_point;
  75. u32 first_free;
  76. u32 last_free;
  77. u32 index_mask;
  78. u32 start_mask;
  79. };
  80. /*
  81. * Object reference table consists of 2**N entries.
  82. *
  83. * State Object ptr Reference
  84. * ----- ---------- ---------
  85. * In use non-NULL XXXX|own index
  86. * (XXXX changes each time entry is acquired)
  87. * Free NULL YYYY|next free index
  88. * (YYYY is one more than last used XXXX)
  89. * Uninitialized NULL 0
  90. *
  91. * Entry 0 is not used; this allows index 0 to denote the end of the free list.
  92. *
  93. * Note that a reference value of 0 does not necessarily indicate that an
  94. * entry is uninitialized, since the last entry in the free list could also
  95. * have a reference value of 0 (although this is unlikely).
  96. */
  97. static struct ref_table tipc_ref_table = { NULL };
  98. static DEFINE_RWLOCK(ref_table_lock);
  99. /**
  100. * tipc_ref_table_init - create reference table for objects
  101. */
  102. int tipc_ref_table_init(u32 requested_size, u32 start)
  103. {
  104. struct reference *table;
  105. u32 actual_size;
  106. /* account for unused entry, then round up size to a power of 2 */
  107. requested_size++;
  108. for (actual_size = 16; actual_size < requested_size; actual_size <<= 1)
  109. /* do nothing */ ;
  110. /* allocate table & mark all entries as uninitialized */
  111. table = __vmalloc(actual_size * sizeof(struct reference),
  112. GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO, PAGE_KERNEL);
  113. if (table == NULL)
  114. return -ENOMEM;
  115. tipc_ref_table.entries = table;
  116. tipc_ref_table.capacity = requested_size;
  117. tipc_ref_table.init_point = 1;
  118. tipc_ref_table.first_free = 0;
  119. tipc_ref_table.last_free = 0;
  120. tipc_ref_table.index_mask = actual_size - 1;
  121. tipc_ref_table.start_mask = start & ~tipc_ref_table.index_mask;
  122. return TIPC_OK;
  123. }
  124. /**
  125. * tipc_ref_table_stop - destroy reference table for objects
  126. */
  127. void tipc_ref_table_stop(void)
  128. {
  129. if (!tipc_ref_table.entries)
  130. return;
  131. vfree(tipc_ref_table.entries);
  132. tipc_ref_table.entries = NULL;
  133. }
  134. /**
  135. * tipc_ref_acquire - create reference to an object
  136. *
  137. * Return a unique reference value which can be translated back to the pointer
  138. * 'object' at a later time. Also, pass back a pointer to the lock protecting
  139. * the object, but without locking it.
  140. */
  141. u32 tipc_ref_acquire(void *object, spinlock_t **lock)
  142. {
  143. struct reference *entry;
  144. u32 index;
  145. u32 index_mask;
  146. u32 next_plus_upper;
  147. u32 reference;
  148. if (!object) {
  149. err("Attempt to acquire reference to non-existent object\n");
  150. return 0;
  151. }
  152. if (!tipc_ref_table.entries) {
  153. err("Reference table not found during acquisition attempt\n");
  154. return 0;
  155. }
  156. /* take a free entry, if available; otherwise initialize a new entry */
  157. write_lock_bh(&ref_table_lock);
  158. if (tipc_ref_table.first_free) {
  159. index = tipc_ref_table.first_free;
  160. entry = &(tipc_ref_table.entries[index]);
  161. index_mask = tipc_ref_table.index_mask;
  162. /* take lock in case a previous user of entry still holds it */
  163. spin_lock_bh(&entry->lock);
  164. next_plus_upper = entry->data.next_plus_upper;
  165. tipc_ref_table.first_free = next_plus_upper & index_mask;
  166. reference = (next_plus_upper & ~index_mask) + index;
  167. entry->data.reference = reference;
  168. entry->object = object;
  169. spin_unlock_bh(&entry->lock);
  170. *lock = &entry->lock;
  171. }
  172. else if (tipc_ref_table.init_point < tipc_ref_table.capacity) {
  173. index = tipc_ref_table.init_point++;
  174. entry = &(tipc_ref_table.entries[index]);
  175. spin_lock_init(&entry->lock);
  176. reference = tipc_ref_table.start_mask + index;
  177. entry->data.reference = reference;
  178. entry->object = object;
  179. *lock = &entry->lock;
  180. }
  181. else {
  182. reference = 0;
  183. }
  184. write_unlock_bh(&ref_table_lock);
  185. return reference;
  186. }
  187. /**
  188. * tipc_ref_discard - invalidate references to an object
  189. *
  190. * Disallow future references to an object and free up the entry for re-use.
  191. * Note: The entry's spin_lock may still be busy after discard
  192. */
  193. void tipc_ref_discard(u32 ref)
  194. {
  195. struct reference *entry;
  196. u32 index;
  197. u32 index_mask;
  198. if (!tipc_ref_table.entries) {
  199. err("Reference table not found during discard attempt\n");
  200. return;
  201. }
  202. index_mask = tipc_ref_table.index_mask;
  203. index = ref & index_mask;
  204. entry = &(tipc_ref_table.entries[index]);
  205. write_lock_bh(&ref_table_lock);
  206. if (!entry->object) {
  207. err("Attempt to discard reference to non-existent object\n");
  208. goto exit;
  209. }
  210. if (entry->data.reference != ref) {
  211. err("Attempt to discard non-existent reference\n");
  212. goto exit;
  213. }
  214. /*
  215. * mark entry as unused; increment upper bits of entry's data field
  216. * to invalidate any subsequent references
  217. */
  218. entry->object = NULL;
  219. entry->data.next_plus_upper = (ref & ~index_mask) + (index_mask + 1);
  220. /* append entry to free entry list */
  221. if (tipc_ref_table.first_free == 0)
  222. tipc_ref_table.first_free = index;
  223. else
  224. tipc_ref_table.entries[tipc_ref_table.last_free].
  225. data.next_plus_upper |= index;
  226. tipc_ref_table.last_free = index;
  227. exit:
  228. write_unlock_bh(&ref_table_lock);
  229. }
  230. /**
  231. * tipc_ref_lock - lock referenced object and return pointer to it
  232. */
  233. void *tipc_ref_lock(u32 ref)
  234. {
  235. if (likely(tipc_ref_table.entries)) {
  236. struct reference *r;
  237. r = &tipc_ref_table.entries[ref & tipc_ref_table.index_mask];
  238. if (likely(r->data.reference != 0)) {
  239. spin_lock_bh(&r->lock);
  240. if (likely((r->data.reference == ref) && (r->object)))
  241. return r->object;
  242. spin_unlock_bh(&r->lock);
  243. }
  244. }
  245. return NULL;
  246. }
  247. /**
  248. * tipc_ref_unlock - unlock referenced object
  249. */
  250. void tipc_ref_unlock(u32 ref)
  251. {
  252. if (likely(tipc_ref_table.entries)) {
  253. struct reference *r;
  254. r = &tipc_ref_table.entries[ref & tipc_ref_table.index_mask];
  255. if (likely((r->data.reference == ref) && (r->object)))
  256. spin_unlock_bh(&r->lock);
  257. else
  258. err("tipc_ref_unlock() invoked using "
  259. "invalid reference\n");
  260. }
  261. }
  262. /**
  263. * tipc_ref_deref - return pointer referenced object (without locking it)
  264. */
  265. void *tipc_ref_deref(u32 ref)
  266. {
  267. if (likely(tipc_ref_table.entries)) {
  268. struct reference *r;
  269. r = &tipc_ref_table.entries[ref & tipc_ref_table.index_mask];
  270. if (likely(r->data.reference == ref))
  271. return r->object;
  272. }
  273. return NULL;
  274. }