ref.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * net/tipc/ref.c: TIPC socket registry code
  3. *
  4. * Copyright (c) 1991-2006, 2014, 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 socket reference entry
  40. * @tsk: pointer to socket associated with reference entry
  41. * @ref: reference value for socket (combines instance & array index info)
  42. */
  43. struct reference {
  44. struct tipc_sock *tsk;
  45. u32 ref;
  46. };
  47. /**
  48. * struct tipc_ref_table - table of TIPC socket reference entries
  49. * @entries: pointer to array of reference entries
  50. * @capacity: array index of first unusable entry
  51. * @init_point: array index of first uninitialized entry
  52. * @first_free: array index of first unused socket reference entry
  53. * @last_free: array index of last unused socket reference entry
  54. * @index_mask: bitmask for array index portion of reference values
  55. * @start_mask: initial value for instance value portion of reference values
  56. */
  57. struct ref_table {
  58. struct reference *entries;
  59. u32 capacity;
  60. u32 init_point;
  61. u32 first_free;
  62. u32 last_free;
  63. u32 index_mask;
  64. u32 start_mask;
  65. };
  66. /*
  67. * Socket reference table consists of 2**N entries.
  68. *
  69. * State Socket ptr Reference
  70. * ----- ---------- ---------
  71. * In use non-NULL XXXX|own index
  72. * (XXXX changes each time entry is acquired)
  73. * Free NULL YYYY|next free index
  74. * (YYYY is one more than last used XXXX)
  75. * Uninitialized NULL 0
  76. *
  77. * Entry 0 is not used; this allows index 0 to denote the end of the free list.
  78. *
  79. * Note that a reference value of 0 does not necessarily indicate that an
  80. * entry is uninitialized, since the last entry in the free list could also
  81. * have a reference value of 0 (although this is unlikely).
  82. */
  83. static struct ref_table tipc_ref_table;
  84. static DEFINE_RWLOCK(ref_table_lock);
  85. /**
  86. * tipc_ref_table_init - create reference table for sockets
  87. */
  88. int tipc_ref_table_init(u32 requested_size, u32 start)
  89. {
  90. struct reference *table;
  91. u32 actual_size;
  92. /* account for unused entry, then round up size to a power of 2 */
  93. requested_size++;
  94. for (actual_size = 16; actual_size < requested_size; actual_size <<= 1)
  95. /* do nothing */ ;
  96. /* allocate table & mark all entries as uninitialized */
  97. table = vzalloc(actual_size * sizeof(struct reference));
  98. if (table == NULL)
  99. return -ENOMEM;
  100. tipc_ref_table.entries = table;
  101. tipc_ref_table.capacity = requested_size;
  102. tipc_ref_table.init_point = 1;
  103. tipc_ref_table.first_free = 0;
  104. tipc_ref_table.last_free = 0;
  105. tipc_ref_table.index_mask = actual_size - 1;
  106. tipc_ref_table.start_mask = start & ~tipc_ref_table.index_mask;
  107. return 0;
  108. }
  109. /**
  110. * tipc_ref_table_stop - destroy reference table for sockets
  111. */
  112. void tipc_ref_table_stop(void)
  113. {
  114. if (!tipc_ref_table.entries)
  115. return;
  116. vfree(tipc_ref_table.entries);
  117. tipc_ref_table.entries = NULL;
  118. }
  119. /* tipc_ref_acquire - create reference to a socket
  120. *
  121. * Register an socket pointer in the reference table.
  122. * Returns a unique reference value that is used from then on to retrieve the
  123. * socket pointer, or to determine if the socket has been deregistered.
  124. */
  125. u32 tipc_ref_acquire(struct tipc_sock *tsk)
  126. {
  127. u32 index;
  128. u32 index_mask;
  129. u32 next_plus_upper;
  130. u32 ref = 0;
  131. struct reference *entry;
  132. if (unlikely(!tsk)) {
  133. pr_err("Attempt to acquire ref. to non-existent obj\n");
  134. return 0;
  135. }
  136. if (unlikely(!tipc_ref_table.entries)) {
  137. pr_err("Ref. table not found in acquisition attempt\n");
  138. return 0;
  139. }
  140. /* Take a free entry, if available; otherwise initialize a new one */
  141. write_lock_bh(&ref_table_lock);
  142. index = tipc_ref_table.first_free;
  143. entry = &tipc_ref_table.entries[index];
  144. if (likely(index)) {
  145. index = tipc_ref_table.first_free;
  146. entry = &(tipc_ref_table.entries[index]);
  147. index_mask = tipc_ref_table.index_mask;
  148. next_plus_upper = entry->ref;
  149. tipc_ref_table.first_free = next_plus_upper & index_mask;
  150. ref = (next_plus_upper & ~index_mask) + index;
  151. entry->tsk = tsk;
  152. } else if (tipc_ref_table.init_point < tipc_ref_table.capacity) {
  153. index = tipc_ref_table.init_point++;
  154. entry = &(tipc_ref_table.entries[index]);
  155. ref = tipc_ref_table.start_mask + index;
  156. }
  157. if (ref) {
  158. entry->ref = ref;
  159. entry->tsk = tsk;
  160. }
  161. write_unlock_bh(&ref_table_lock);
  162. return ref;
  163. }
  164. /* tipc_ref_discard - invalidate reference to an socket
  165. *
  166. * Disallow future references to an socket and free up the entry for re-use.
  167. */
  168. void tipc_ref_discard(u32 ref)
  169. {
  170. struct reference *entry;
  171. u32 index;
  172. u32 index_mask;
  173. if (unlikely(!tipc_ref_table.entries)) {
  174. pr_err("Ref. table not found during discard attempt\n");
  175. return;
  176. }
  177. index_mask = tipc_ref_table.index_mask;
  178. index = ref & index_mask;
  179. entry = &(tipc_ref_table.entries[index]);
  180. write_lock_bh(&ref_table_lock);
  181. if (unlikely(!entry->tsk)) {
  182. pr_err("Attempt to discard ref. to non-existent socket\n");
  183. goto exit;
  184. }
  185. if (unlikely(entry->ref != ref)) {
  186. pr_err("Attempt to discard non-existent reference\n");
  187. goto exit;
  188. }
  189. /*
  190. * Mark entry as unused; increment instance part of entry's reference
  191. * to invalidate any subsequent references
  192. */
  193. entry->tsk = NULL;
  194. entry->ref = (ref & ~index_mask) + (index_mask + 1);
  195. /* Append entry to free entry list */
  196. if (unlikely(tipc_ref_table.first_free == 0))
  197. tipc_ref_table.first_free = index;
  198. else
  199. tipc_ref_table.entries[tipc_ref_table.last_free].ref |= index;
  200. tipc_ref_table.last_free = index;
  201. exit:
  202. write_unlock_bh(&ref_table_lock);
  203. }
  204. /* tipc_sk_get - find referenced socket and return pointer to it
  205. */
  206. struct tipc_sock *tipc_sk_get(u32 ref)
  207. {
  208. struct reference *entry;
  209. struct tipc_sock *tsk;
  210. if (unlikely(!tipc_ref_table.entries))
  211. return NULL;
  212. read_lock_bh(&ref_table_lock);
  213. entry = &tipc_ref_table.entries[ref & tipc_ref_table.index_mask];
  214. tsk = entry->tsk;
  215. if (likely(tsk && (entry->ref == ref)))
  216. sock_hold(&tsk->sk);
  217. else
  218. tsk = NULL;
  219. read_unlock_bh(&ref_table_lock);
  220. return tsk;
  221. }
  222. /* tipc_sk_get_next - lock & return next socket after referenced one
  223. */
  224. struct tipc_sock *tipc_sk_get_next(u32 *ref)
  225. {
  226. struct reference *entry;
  227. struct tipc_sock *tsk = NULL;
  228. uint index = *ref & tipc_ref_table.index_mask;
  229. read_lock_bh(&ref_table_lock);
  230. while (++index < tipc_ref_table.capacity) {
  231. entry = &tipc_ref_table.entries[index];
  232. if (!entry->tsk)
  233. continue;
  234. tsk = entry->tsk;
  235. sock_hold(&tsk->sk);
  236. *ref = entry->ref;
  237. break;
  238. }
  239. read_unlock_bh(&ref_table_lock);
  240. return tsk;
  241. }