blob: 9e37b7812c3c361fa1a904b184ec98f74d4ce9e8 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/ref.c: TIPC object registry code
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Per Liden593a5f22006-01-11 19:14:19 +01004 * Copyright (c) 1991-2006, Ericsson AB
Allan Stephens4784b7c2008-04-16 18:21:16 -07005 * Copyright (c) 2004-2007, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * 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:
Per Liden9ea1fd32006-01-11 13:30:43 +010010 *
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.
Per Lidenb97bf3f2006-01-02 19:04:38 +010023 *
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
37#include "core.h"
38#include "ref.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010039
Allan Stephens4784b7c2008-04-16 18:21:16 -070040/**
41 * struct reference - TIPC object reference entry
42 * @object: pointer to object associated with reference entry
43 * @lock: spinlock controlling access to object
Allan Stephensbcff1222008-04-16 18:22:20 -070044 * @ref: reference value for object (combines instance & array index info)
Allan Stephens4784b7c2008-04-16 18:21:16 -070045 */
46
47struct reference {
48 void *object;
49 spinlock_t lock;
Allan Stephensbcff1222008-04-16 18:22:20 -070050 u32 ref;
Allan Stephens4784b7c2008-04-16 18:21:16 -070051};
52
53/**
54 * struct tipc_ref_table - table of TIPC object reference entries
55 * @entries: pointer to array of reference entries
Allan Stephens00895092008-04-16 18:21:47 -070056 * @capacity: array index of first unusable entry
57 * @init_point: array index of first uninitialized entry
Allan Stephens4784b7c2008-04-16 18:21:16 -070058 * @first_free: array index of first unused object reference entry
59 * @last_free: array index of last unused object reference entry
Allan Stephens00895092008-04-16 18:21:47 -070060 * @index_mask: bitmask for array index portion of reference values
61 * @start_mask: initial value for instance value portion of reference values
Allan Stephens4784b7c2008-04-16 18:21:16 -070062 */
63
64struct ref_table {
65 struct reference *entries;
Allan Stephens00895092008-04-16 18:21:47 -070066 u32 capacity;
67 u32 init_point;
Allan Stephens4784b7c2008-04-16 18:21:16 -070068 u32 first_free;
69 u32 last_free;
Allan Stephens00895092008-04-16 18:21:47 -070070 u32 index_mask;
71 u32 start_mask;
Allan Stephens4784b7c2008-04-16 18:21:16 -070072};
73
Per Lidenb97bf3f2006-01-02 19:04:38 +010074/*
75 * Object reference table consists of 2**N entries.
76 *
Allan Stephens00895092008-04-16 18:21:47 -070077 * State Object ptr Reference
78 * ----- ---------- ---------
79 * In use non-NULL XXXX|own index
80 * (XXXX changes each time entry is acquired)
81 * Free NULL YYYY|next free index
82 * (YYYY is one more than last used XXXX)
83 * Uninitialized NULL 0
Per Lidenb97bf3f2006-01-02 19:04:38 +010084 *
Allan Stephens00895092008-04-16 18:21:47 -070085 * Entry 0 is not used; this allows index 0 to denote the end of the free list.
Per Lidenb97bf3f2006-01-02 19:04:38 +010086 *
Allan Stephens00895092008-04-16 18:21:47 -070087 * Note that a reference value of 0 does not necessarily indicate that an
88 * entry is uninitialized, since the last entry in the free list could also
89 * have a reference value of 0 (although this is unlikely).
Per Lidenb97bf3f2006-01-02 19:04:38 +010090 */
91
Allan Stephense3ec9c72010-12-31 18:59:34 +000092static struct ref_table tipc_ref_table;
Per Lidenb97bf3f2006-01-02 19:04:38 +010093
Ingo Molnar34af9462006-06-27 02:53:55 -070094static DEFINE_RWLOCK(ref_table_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +010095
96/**
Per Liden4323add2006-01-18 00:38:21 +010097 * tipc_ref_table_init - create reference table for objects
Per Lidenb97bf3f2006-01-02 19:04:38 +010098 */
99
Per Liden4323add2006-01-18 00:38:21 +0100100int tipc_ref_table_init(u32 requested_size, u32 start)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100101{
102 struct reference *table;
Allan Stephens00895092008-04-16 18:21:47 -0700103 u32 actual_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100104
Allan Stephens00895092008-04-16 18:21:47 -0700105 /* account for unused entry, then round up size to a power of 2 */
106
107 requested_size++;
108 for (actual_size = 16; actual_size < requested_size; actual_size <<= 1)
109 /* do nothing */ ;
110
111 /* allocate table & mark all entries as uninitialized */
112
Eric Dumazet966567b2011-12-19 16:01:38 -0500113 table = vzalloc(actual_size * sizeof(struct reference));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100114 if (table == NULL)
115 return -ENOMEM;
116
Per Liden4323add2006-01-18 00:38:21 +0100117 tipc_ref_table.entries = table;
Allan Stephens00895092008-04-16 18:21:47 -0700118 tipc_ref_table.capacity = requested_size;
119 tipc_ref_table.init_point = 1;
120 tipc_ref_table.first_free = 0;
121 tipc_ref_table.last_free = 0;
122 tipc_ref_table.index_mask = actual_size - 1;
123 tipc_ref_table.start_mask = start & ~tipc_ref_table.index_mask;
124
Allan Stephens0e35fd52008-07-14 22:44:01 -0700125 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100126}
127
128/**
Per Liden4323add2006-01-18 00:38:21 +0100129 * tipc_ref_table_stop - destroy reference table for objects
Per Lidenb97bf3f2006-01-02 19:04:38 +0100130 */
131
Per Liden4323add2006-01-18 00:38:21 +0100132void tipc_ref_table_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100133{
Per Liden4323add2006-01-18 00:38:21 +0100134 if (!tipc_ref_table.entries)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100135 return;
136
Per Liden4323add2006-01-18 00:38:21 +0100137 vfree(tipc_ref_table.entries);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800138 tipc_ref_table.entries = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100139}
140
141/**
Per Liden4323add2006-01-18 00:38:21 +0100142 * tipc_ref_acquire - create reference to an object
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900143 *
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700144 * Register an object pointer in reference table and lock the object.
145 * Returns a unique reference value that is used from then on to retrieve the
146 * object pointer, or to determine that the object has been deregistered.
147 *
148 * Note: The object is returned in the locked state so that the caller can
149 * register a partially initialized object, without running the risk that
150 * the object will be accessed before initialization is complete.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100151 */
152
Per Liden4323add2006-01-18 00:38:21 +0100153u32 tipc_ref_acquire(void *object, spinlock_t **lock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100154{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100155 u32 index;
156 u32 index_mask;
157 u32 next_plus_upper;
Allan Stephensbcff1222008-04-16 18:22:20 -0700158 u32 ref;
Neil Hormana2f46ee2010-03-16 08:14:33 +0000159 struct reference *entry = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100160
Allan Stephensf1310722006-06-25 23:51:37 -0700161 if (!object) {
162 err("Attempt to acquire reference to non-existent object\n");
163 return 0;
164 }
165 if (!tipc_ref_table.entries) {
166 err("Reference table not found during acquisition attempt\n");
167 return 0;
168 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100169
Allan Stephens00895092008-04-16 18:21:47 -0700170 /* take a free entry, if available; otherwise initialize a new entry */
171
Per Liden4323add2006-01-18 00:38:21 +0100172 write_lock_bh(&ref_table_lock);
173 if (tipc_ref_table.first_free) {
174 index = tipc_ref_table.first_free;
175 entry = &(tipc_ref_table.entries[index]);
176 index_mask = tipc_ref_table.index_mask;
Allan Stephensbcff1222008-04-16 18:22:20 -0700177 next_plus_upper = entry->ref;
Per Liden4323add2006-01-18 00:38:21 +0100178 tipc_ref_table.first_free = next_plus_upper & index_mask;
Allan Stephensbcff1222008-04-16 18:22:20 -0700179 ref = (next_plus_upper & ~index_mask) + index;
Allan Stephens0e659672010-12-31 18:59:32 +0000180 } else if (tipc_ref_table.init_point < tipc_ref_table.capacity) {
Allan Stephens00895092008-04-16 18:21:47 -0700181 index = tipc_ref_table.init_point++;
182 entry = &(tipc_ref_table.entries[index]);
183 spin_lock_init(&entry->lock);
Allan Stephensbcff1222008-04-16 18:22:20 -0700184 ref = tipc_ref_table.start_mask + index;
Allan Stephens0e659672010-12-31 18:59:32 +0000185 } else {
Allan Stephensbcff1222008-04-16 18:22:20 -0700186 ref = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100187 }
Per Liden4323add2006-01-18 00:38:21 +0100188 write_unlock_bh(&ref_table_lock);
Allan Stephens00895092008-04-16 18:21:47 -0700189
Neil Hormana2f46ee2010-03-16 08:14:33 +0000190 /*
191 * Grab the lock so no one else can modify this entry
192 * While we assign its ref value & object pointer
193 */
194 if (entry) {
195 spin_lock_bh(&entry->lock);
196 entry->ref = ref;
197 entry->object = object;
198 *lock = &entry->lock;
199 /*
200 * keep it locked, the caller is responsible
201 * for unlocking this when they're done with it
202 */
203 }
204
Allan Stephensbcff1222008-04-16 18:22:20 -0700205 return ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100206}
207
208/**
Per Liden4323add2006-01-18 00:38:21 +0100209 * tipc_ref_discard - invalidate references to an object
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900210 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100211 * Disallow future references to an object and free up the entry for re-use.
212 * Note: The entry's spin_lock may still be busy after discard
213 */
214
Per Liden4323add2006-01-18 00:38:21 +0100215void tipc_ref_discard(u32 ref)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100216{
217 struct reference *entry;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900218 u32 index;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100219 u32 index_mask;
220
Allan Stephensf1310722006-06-25 23:51:37 -0700221 if (!tipc_ref_table.entries) {
222 err("Reference table not found during discard attempt\n");
223 return;
224 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100225
Per Liden4323add2006-01-18 00:38:21 +0100226 index_mask = tipc_ref_table.index_mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100227 index = ref & index_mask;
Per Liden4323add2006-01-18 00:38:21 +0100228 entry = &(tipc_ref_table.entries[index]);
Allan Stephensf1310722006-06-25 23:51:37 -0700229
Allan Stephens00895092008-04-16 18:21:47 -0700230 write_lock_bh(&ref_table_lock);
231
Allan Stephensf1310722006-06-25 23:51:37 -0700232 if (!entry->object) {
233 err("Attempt to discard reference to non-existent object\n");
234 goto exit;
235 }
Allan Stephensbcff1222008-04-16 18:22:20 -0700236 if (entry->ref != ref) {
Allan Stephensf1310722006-06-25 23:51:37 -0700237 err("Attempt to discard non-existent reference\n");
238 goto exit;
239 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100240
Allan Stephens00895092008-04-16 18:21:47 -0700241 /*
Allan Stephensbcff1222008-04-16 18:22:20 -0700242 * mark entry as unused; increment instance part of entry's reference
Allan Stephens00895092008-04-16 18:21:47 -0700243 * to invalidate any subsequent references
244 */
245
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800246 entry->object = NULL;
Allan Stephensbcff1222008-04-16 18:22:20 -0700247 entry->ref = (ref & ~index_mask) + (index_mask + 1);
Allan Stephens00895092008-04-16 18:21:47 -0700248
249 /* append entry to free entry list */
250
Per Liden4323add2006-01-18 00:38:21 +0100251 if (tipc_ref_table.first_free == 0)
252 tipc_ref_table.first_free = index;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100253 else
Allan Stephensbcff1222008-04-16 18:22:20 -0700254 tipc_ref_table.entries[tipc_ref_table.last_free].ref |= index;
Per Liden4323add2006-01-18 00:38:21 +0100255 tipc_ref_table.last_free = index;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100256
Allan Stephensf1310722006-06-25 23:51:37 -0700257exit:
Per Liden4323add2006-01-18 00:38:21 +0100258 write_unlock_bh(&ref_table_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100259}
260
Allan Stephens4784b7c2008-04-16 18:21:16 -0700261/**
262 * tipc_ref_lock - lock referenced object and return pointer to it
263 */
264
265void *tipc_ref_lock(u32 ref)
266{
267 if (likely(tipc_ref_table.entries)) {
Allan Stephensbcff1222008-04-16 18:22:20 -0700268 struct reference *entry;
Allan Stephens4784b7c2008-04-16 18:21:16 -0700269
Allan Stephensbcff1222008-04-16 18:22:20 -0700270 entry = &tipc_ref_table.entries[ref &
271 tipc_ref_table.index_mask];
272 if (likely(entry->ref != 0)) {
273 spin_lock_bh(&entry->lock);
274 if (likely((entry->ref == ref) && (entry->object)))
275 return entry->object;
276 spin_unlock_bh(&entry->lock);
Allan Stephens00895092008-04-16 18:21:47 -0700277 }
Allan Stephens4784b7c2008-04-16 18:21:16 -0700278 }
279 return NULL;
280}
281
Allan Stephens4784b7c2008-04-16 18:21:16 -0700282
283/**
284 * tipc_ref_deref - return pointer referenced object (without locking it)
285 */
286
287void *tipc_ref_deref(u32 ref)
288{
289 if (likely(tipc_ref_table.entries)) {
Allan Stephensbcff1222008-04-16 18:22:20 -0700290 struct reference *entry;
Allan Stephens4784b7c2008-04-16 18:21:16 -0700291
Allan Stephensbcff1222008-04-16 18:22:20 -0700292 entry = &tipc_ref_table.entries[ref &
293 tipc_ref_table.index_mask];
294 if (likely(entry->ref == ref))
295 return entry->object;
Allan Stephens4784b7c2008-04-16 18:21:16 -0700296 }
297 return NULL;
298}
299