Blame view

net/tipc/ref.c 8.3 KB
b97bf3fd8   Per Liden   [TIPC] Initial merge
1
2
  /*
   * net/tipc/ref.c: TIPC object registry code
c43072852   YOSHIFUJI Hideaki   [NET] TIPC: Fix w...
3
   *
593a5f22d   Per Liden   [TIPC] More updat...
4
   * Copyright (c) 1991-2006, Ericsson AB
4784b7c34   Allan Stephens   [TIPC]: Remove in...
5
   * Copyright (c) 2004-2007, Wind River Systems
b97bf3fd8   Per Liden   [TIPC] Initial merge
6
7
8
9
   * All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions are met:
9ea1fd3c1   Per Liden   [TIPC] License he...
10
11
12
13
14
15
16
17
18
19
20
21
22
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in the
   *    documentation and/or other materials provided with the distribution.
   * 3. Neither the names of the copyright holders nor the names of its
   *    contributors may be used to endorse or promote products derived from
   *    this software without specific prior written permission.
   *
   * Alternatively, this software may be distributed under the terms of the
   * GNU General Public License ("GPL") version 2 as published by the Free
   * Software Foundation.
b97bf3fd8   Per Liden   [TIPC] Initial merge
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
   *
   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   * POSSIBILITY OF SUCH DAMAGE.
   */
  
  #include "core.h"
  #include "ref.h"
b97bf3fd8   Per Liden   [TIPC] Initial merge
39

4784b7c34   Allan Stephens   [TIPC]: Remove in...
40
41
42
43
  /**
   * struct reference - TIPC object reference entry
   * @object: pointer to object associated with reference entry
   * @lock: spinlock controlling access to object
bcff122d4   Allan Stephens   [TIPC]: Cleanup o...
44
   * @ref: reference value for object (combines instance & array index info)
4784b7c34   Allan Stephens   [TIPC]: Remove in...
45
   */
4784b7c34   Allan Stephens   [TIPC]: Remove in...
46
47
48
  struct reference {
  	void *object;
  	spinlock_t lock;
bcff122d4   Allan Stephens   [TIPC]: Cleanup o...
49
  	u32 ref;
4784b7c34   Allan Stephens   [TIPC]: Remove in...
50
51
52
53
54
  };
  
  /**
   * struct tipc_ref_table - table of TIPC object reference entries
   * @entries: pointer to array of reference entries
008950982   Allan Stephens   [TIPC]: Optimized...
55
56
   * @capacity: array index of first unusable entry
   * @init_point: array index of first uninitialized entry
4784b7c34   Allan Stephens   [TIPC]: Remove in...
57
58
   * @first_free: array index of first unused object reference entry
   * @last_free: array index of last unused object reference entry
008950982   Allan Stephens   [TIPC]: Optimized...
59
60
   * @index_mask: bitmask for array index portion of reference values
   * @start_mask: initial value for instance value portion of reference values
4784b7c34   Allan Stephens   [TIPC]: Remove in...
61
   */
4784b7c34   Allan Stephens   [TIPC]: Remove in...
62
63
  struct ref_table {
  	struct reference *entries;
008950982   Allan Stephens   [TIPC]: Optimized...
64
65
  	u32 capacity;
  	u32 init_point;
4784b7c34   Allan Stephens   [TIPC]: Remove in...
66
67
  	u32 first_free;
  	u32 last_free;
008950982   Allan Stephens   [TIPC]: Optimized...
68
69
  	u32 index_mask;
  	u32 start_mask;
4784b7c34   Allan Stephens   [TIPC]: Remove in...
70
  };
b97bf3fd8   Per Liden   [TIPC] Initial merge
71
72
73
  /*
   * Object reference table consists of 2**N entries.
   *
008950982   Allan Stephens   [TIPC]: Optimized...
74
75
76
77
78
79
80
   * State	Object ptr	Reference
   * -----        ----------      ---------
   * In use        non-NULL       XXXX|own index
   *				(XXXX changes each time entry is acquired)
   * Free            NULL         YYYY|next free index
   *				(YYYY is one more than last used XXXX)
   * Uninitialized   NULL         0
b97bf3fd8   Per Liden   [TIPC] Initial merge
81
   *
008950982   Allan Stephens   [TIPC]: Optimized...
82
   * Entry 0 is not used; this allows index 0 to denote the end of the free list.
b97bf3fd8   Per Liden   [TIPC] Initial merge
83
   *
008950982   Allan Stephens   [TIPC]: Optimized...
84
85
86
   * Note that a reference value of 0 does not necessarily indicate that an
   * entry is uninitialized, since the last entry in the free list could also
   * have a reference value of 0 (although this is unlikely).
b97bf3fd8   Per Liden   [TIPC] Initial merge
87
   */
e3ec9c7d5   Allan Stephens   tipc: remove zero...
88
  static struct ref_table tipc_ref_table;
b97bf3fd8   Per Liden   [TIPC] Initial merge
89

34af946a2   Ingo Molnar   [PATCH] spin/rwlo...
90
  static DEFINE_RWLOCK(ref_table_lock);
b97bf3fd8   Per Liden   [TIPC] Initial merge
91
92
  
  /**
4323add67   Per Liden   [TIPC] Avoid poll...
93
   * tipc_ref_table_init - create reference table for objects
b97bf3fd8   Per Liden   [TIPC] Initial merge
94
   */
4323add67   Per Liden   [TIPC] Avoid poll...
95
  int tipc_ref_table_init(u32 requested_size, u32 start)
b97bf3fd8   Per Liden   [TIPC] Initial merge
96
97
  {
  	struct reference *table;
008950982   Allan Stephens   [TIPC]: Optimized...
98
  	u32 actual_size;
b97bf3fd8   Per Liden   [TIPC] Initial merge
99

008950982   Allan Stephens   [TIPC]: Optimized...
100
101
102
103
104
105
106
  	/* account for unused entry, then round up size to a power of 2 */
  
  	requested_size++;
  	for (actual_size = 16; actual_size < requested_size; actual_size <<= 1)
  		/* do nothing */ ;
  
  	/* allocate table & mark all entries as uninitialized */
966567b76   Eric Dumazet   net: two vzalloc(...
107
  	table = vzalloc(actual_size * sizeof(struct reference));
b97bf3fd8   Per Liden   [TIPC] Initial merge
108
109
  	if (table == NULL)
  		return -ENOMEM;
4323add67   Per Liden   [TIPC] Avoid poll...
110
  	tipc_ref_table.entries = table;
008950982   Allan Stephens   [TIPC]: Optimized...
111
112
113
114
115
116
  	tipc_ref_table.capacity = requested_size;
  	tipc_ref_table.init_point = 1;
  	tipc_ref_table.first_free = 0;
  	tipc_ref_table.last_free = 0;
  	tipc_ref_table.index_mask = actual_size - 1;
  	tipc_ref_table.start_mask = start & ~tipc_ref_table.index_mask;
0e35fd5e5   Allan Stephens   tipc: Eliminate i...
117
  	return 0;
b97bf3fd8   Per Liden   [TIPC] Initial merge
118
119
120
  }
  
  /**
4323add67   Per Liden   [TIPC] Avoid poll...
121
   * tipc_ref_table_stop - destroy reference table for objects
b97bf3fd8   Per Liden   [TIPC] Initial merge
122
   */
4323add67   Per Liden   [TIPC] Avoid poll...
123
  void tipc_ref_table_stop(void)
b97bf3fd8   Per Liden   [TIPC] Initial merge
124
  {
4323add67   Per Liden   [TIPC] Avoid poll...
125
  	if (!tipc_ref_table.entries)
b97bf3fd8   Per Liden   [TIPC] Initial merge
126
  		return;
4323add67   Per Liden   [TIPC] Avoid poll...
127
  	vfree(tipc_ref_table.entries);
1fc54d8f4   Sam Ravnborg   [TIPC]: Fix simpl...
128
  	tipc_ref_table.entries = NULL;
b97bf3fd8   Per Liden   [TIPC] Initial merge
129
130
131
  }
  
  /**
4323add67   Per Liden   [TIPC] Avoid poll...
132
   * tipc_ref_acquire - create reference to an object
c43072852   YOSHIFUJI Hideaki   [NET] TIPC: Fix w...
133
   *
7ef43ebaa   Allan Stephens   tipc: Fix race co...
134
135
136
137
138
139
140
   * Register an object pointer in reference table and lock the object.
   * Returns a unique reference value that is used from then on to retrieve the
   * object pointer, or to determine that the object has been deregistered.
   *
   * Note: The object is returned in the locked state so that the caller can
   * register a partially initialized object, without running the risk that
   * the object will be accessed before initialization is complete.
b97bf3fd8   Per Liden   [TIPC] Initial merge
141
   */
4323add67   Per Liden   [TIPC] Avoid poll...
142
  u32 tipc_ref_acquire(void *object, spinlock_t **lock)
b97bf3fd8   Per Liden   [TIPC] Initial merge
143
  {
b97bf3fd8   Per Liden   [TIPC] Initial merge
144
145
146
  	u32 index;
  	u32 index_mask;
  	u32 next_plus_upper;
bcff122d4   Allan Stephens   [TIPC]: Cleanup o...
147
  	u32 ref;
a2f46ee1b   Neil Horman   tipc: fix lockdep...
148
  	struct reference *entry = NULL;
b97bf3fd8   Per Liden   [TIPC] Initial merge
149

f131072c3   Allan Stephens   [TIPC]: First pha...
150
  	if (!object) {
2cf8aa19f   Erik Hugne   tipc: use standar...
151
152
  		pr_err("Attempt to acquire ref. to non-existent obj
  ");
f131072c3   Allan Stephens   [TIPC]: First pha...
153
154
155
  		return 0;
  	}
  	if (!tipc_ref_table.entries) {
2cf8aa19f   Erik Hugne   tipc: use standar...
156
157
  		pr_err("Ref. table not found in acquisition attempt
  ");
f131072c3   Allan Stephens   [TIPC]: First pha...
158
159
  		return 0;
  	}
b97bf3fd8   Per Liden   [TIPC] Initial merge
160

008950982   Allan Stephens   [TIPC]: Optimized...
161
  	/* take a free entry, if available; otherwise initialize a new entry */
4323add67   Per Liden   [TIPC] Avoid poll...
162
163
164
165
166
  	write_lock_bh(&ref_table_lock);
  	if (tipc_ref_table.first_free) {
  		index = tipc_ref_table.first_free;
  		entry = &(tipc_ref_table.entries[index]);
  		index_mask = tipc_ref_table.index_mask;
bcff122d4   Allan Stephens   [TIPC]: Cleanup o...
167
  		next_plus_upper = entry->ref;
4323add67   Per Liden   [TIPC] Avoid poll...
168
  		tipc_ref_table.first_free = next_plus_upper & index_mask;
bcff122d4   Allan Stephens   [TIPC]: Cleanup o...
169
  		ref = (next_plus_upper & ~index_mask) + index;
0e65967e3   Allan Stephens   tipc: cleanup var...
170
  	} else if (tipc_ref_table.init_point < tipc_ref_table.capacity) {
008950982   Allan Stephens   [TIPC]: Optimized...
171
172
173
  		index = tipc_ref_table.init_point++;
  		entry = &(tipc_ref_table.entries[index]);
  		spin_lock_init(&entry->lock);
bcff122d4   Allan Stephens   [TIPC]: Cleanup o...
174
  		ref = tipc_ref_table.start_mask + index;
0e65967e3   Allan Stephens   tipc: cleanup var...
175
  	} else {
bcff122d4   Allan Stephens   [TIPC]: Cleanup o...
176
  		ref = 0;
b97bf3fd8   Per Liden   [TIPC] Initial merge
177
  	}
4323add67   Per Liden   [TIPC] Avoid poll...
178
  	write_unlock_bh(&ref_table_lock);
008950982   Allan Stephens   [TIPC]: Optimized...
179

a2f46ee1b   Neil Horman   tipc: fix lockdep...
180
181
182
183
184
185
186
187
188
189
190
191
192
193
  	/*
  	 * Grab the lock so no one else can modify this entry
  	 * While we assign its ref value & object pointer
  	 */
  	if (entry) {
  		spin_lock_bh(&entry->lock);
  		entry->ref = ref;
  		entry->object = object;
  		*lock = &entry->lock;
  		/*
  		 * keep it locked, the caller is responsible
  		 * for unlocking this when they're done with it
  		 */
  	}
bcff122d4   Allan Stephens   [TIPC]: Cleanup o...
194
  	return ref;
b97bf3fd8   Per Liden   [TIPC] Initial merge
195
196
197
  }
  
  /**
4323add67   Per Liden   [TIPC] Avoid poll...
198
   * tipc_ref_discard - invalidate references to an object
c43072852   YOSHIFUJI Hideaki   [NET] TIPC: Fix w...
199
   *
b97bf3fd8   Per Liden   [TIPC] Initial merge
200
201
202
   * Disallow future references to an object and free up the entry for re-use.
   * Note: The entry's spin_lock may still be busy after discard
   */
4323add67   Per Liden   [TIPC] Avoid poll...
203
  void tipc_ref_discard(u32 ref)
b97bf3fd8   Per Liden   [TIPC] Initial merge
204
205
  {
  	struct reference *entry;
c43072852   YOSHIFUJI Hideaki   [NET] TIPC: Fix w...
206
  	u32 index;
b97bf3fd8   Per Liden   [TIPC] Initial merge
207
  	u32 index_mask;
f131072c3   Allan Stephens   [TIPC]: First pha...
208
  	if (!tipc_ref_table.entries) {
2cf8aa19f   Erik Hugne   tipc: use standar...
209
210
  		pr_err("Ref. table not found during discard attempt
  ");
f131072c3   Allan Stephens   [TIPC]: First pha...
211
212
  		return;
  	}
b97bf3fd8   Per Liden   [TIPC] Initial merge
213

4323add67   Per Liden   [TIPC] Avoid poll...
214
  	index_mask = tipc_ref_table.index_mask;
b97bf3fd8   Per Liden   [TIPC] Initial merge
215
  	index = ref & index_mask;
4323add67   Per Liden   [TIPC] Avoid poll...
216
  	entry = &(tipc_ref_table.entries[index]);
f131072c3   Allan Stephens   [TIPC]: First pha...
217

008950982   Allan Stephens   [TIPC]: Optimized...
218
  	write_lock_bh(&ref_table_lock);
f131072c3   Allan Stephens   [TIPC]: First pha...
219
  	if (!entry->object) {
2cf8aa19f   Erik Hugne   tipc: use standar...
220
221
  		pr_err("Attempt to discard ref. to non-existent obj
  ");
f131072c3   Allan Stephens   [TIPC]: First pha...
222
223
  		goto exit;
  	}
bcff122d4   Allan Stephens   [TIPC]: Cleanup o...
224
  	if (entry->ref != ref) {
2cf8aa19f   Erik Hugne   tipc: use standar...
225
226
  		pr_err("Attempt to discard non-existent reference
  ");
f131072c3   Allan Stephens   [TIPC]: First pha...
227
228
  		goto exit;
  	}
b97bf3fd8   Per Liden   [TIPC] Initial merge
229

008950982   Allan Stephens   [TIPC]: Optimized...
230
  	/*
bcff122d4   Allan Stephens   [TIPC]: Cleanup o...
231
  	 * mark entry as unused; increment instance part of entry's reference
008950982   Allan Stephens   [TIPC]: Optimized...
232
233
  	 * to invalidate any subsequent references
  	 */
1fc54d8f4   Sam Ravnborg   [TIPC]: Fix simpl...
234
  	entry->object = NULL;
bcff122d4   Allan Stephens   [TIPC]: Cleanup o...
235
  	entry->ref = (ref & ~index_mask) + (index_mask + 1);
008950982   Allan Stephens   [TIPC]: Optimized...
236
237
  
  	/* append entry to free entry list */
4323add67   Per Liden   [TIPC] Avoid poll...
238
239
  	if (tipc_ref_table.first_free == 0)
  		tipc_ref_table.first_free = index;
b97bf3fd8   Per Liden   [TIPC] Initial merge
240
  	else
bcff122d4   Allan Stephens   [TIPC]: Cleanup o...
241
  		tipc_ref_table.entries[tipc_ref_table.last_free].ref |= index;
4323add67   Per Liden   [TIPC] Avoid poll...
242
  	tipc_ref_table.last_free = index;
b97bf3fd8   Per Liden   [TIPC] Initial merge
243

f131072c3   Allan Stephens   [TIPC]: First pha...
244
  exit:
4323add67   Per Liden   [TIPC] Avoid poll...
245
  	write_unlock_bh(&ref_table_lock);
b97bf3fd8   Per Liden   [TIPC] Initial merge
246
  }
4784b7c34   Allan Stephens   [TIPC]: Remove in...
247
248
249
  /**
   * tipc_ref_lock - lock referenced object and return pointer to it
   */
4784b7c34   Allan Stephens   [TIPC]: Remove in...
250
251
252
  void *tipc_ref_lock(u32 ref)
  {
  	if (likely(tipc_ref_table.entries)) {
bcff122d4   Allan Stephens   [TIPC]: Cleanup o...
253
254
255
256
257
258
259
260
261
  		struct reference *entry;
  
  		entry = &tipc_ref_table.entries[ref &
  						tipc_ref_table.index_mask];
  		if (likely(entry->ref != 0)) {
  			spin_lock_bh(&entry->lock);
  			if (likely((entry->ref == ref) && (entry->object)))
  				return entry->object;
  			spin_unlock_bh(&entry->lock);
008950982   Allan Stephens   [TIPC]: Optimized...
262
  		}
4784b7c34   Allan Stephens   [TIPC]: Remove in...
263
264
265
  	}
  	return NULL;
  }
4784b7c34   Allan Stephens   [TIPC]: Remove in...
266
267
268
269
  
  /**
   * tipc_ref_deref - return pointer referenced object (without locking it)
   */
4784b7c34   Allan Stephens   [TIPC]: Remove in...
270
271
272
  void *tipc_ref_deref(u32 ref)
  {
  	if (likely(tipc_ref_table.entries)) {
bcff122d4   Allan Stephens   [TIPC]: Cleanup o...
273
  		struct reference *entry;
4784b7c34   Allan Stephens   [TIPC]: Remove in...
274

bcff122d4   Allan Stephens   [TIPC]: Cleanup o...
275
276
277
278
  		entry = &tipc_ref_table.entries[ref &
  						tipc_ref_table.index_mask];
  		if (likely(entry->ref == ref))
  			return entry->object;
4784b7c34   Allan Stephens   [TIPC]: Remove in...
279
280
281
  	}
  	return NULL;
  }