]>
Commit | Line | Data |
---|---|---|
9fe45dc1 BB |
1 | /*****************************************************************************\ |
2 | * Copyright (C) 2010 Lawrence Livermore National Security, LLC. | |
3 | * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER). | |
4 | * Written by Brian Behlendorf <behlendorf1@llnl.gov>. | |
5 | * UCRL-CODE-235197 | |
6 | * | |
7 | * This file is part of the SPL, Solaris Porting Layer. | |
3d6af2dd | 8 | * For details, see <http://zfsonlinux.org/>. |
9fe45dc1 BB |
9 | * |
10 | * The SPL is free software; you can redistribute it and/or modify it | |
11 | * under the terms of the GNU General Public License as published by the | |
12 | * Free Software Foundation; either version 2 of the License, or (at your | |
13 | * option) any later version. | |
14 | * | |
15 | * The SPL is distributed in the hope that it will be useful, but WITHOUT | |
16 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
17 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
18 | * for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License along | |
21 | * with the SPL. If not, see <http://www.gnu.org/licenses/>. | |
22 | ***************************************************************************** | |
23 | * Solaris Porting Layer (SPL) Thread Specific Data Implementation. | |
24 | * | |
25 | * Thread specific data has implemented using a hash table, this avoids | |
26 | * the need to add a member to the task structure and allows maximum | |
27 | * portability between kernels. This implementation has been optimized | |
28 | * to keep the tsd_set() and tsd_get() times as small as possible. | |
29 | * | |
30 | * The majority of the entries in the hash table are for specific tsd | |
31 | * entries. These entries are hashed by the product of their key and | |
32 | * pid because by design the key and pid are guaranteed to be unique. | |
33 | * Their product also has the desirable properly that it will be uniformly | |
34 | * distributed over the hash bins providing neither the pid nor key is zero. | |
35 | * Under linux the zero pid is always the init process and thus won't be | |
36 | * used, and this implementation is careful to never to assign a zero key. | |
37 | * By default the hash table is sized to 512 bins which is expected to | |
38 | * be sufficient for light to moderate usage of thread specific data. | |
39 | * | |
40 | * The hash table contains two additional type of entries. They first | |
41 | * type is entry is called a 'key' entry and it is added to the hash during | |
42 | * tsd_create(). It is used to store the address of the destructor function | |
43 | * and it is used as an anchor point. All tsd entries which use the same | |
44 | * key will be linked to this entry. This is used during tsd_destory() to | |
45 | * quickly call the destructor function for all tsd associated with the key. | |
46 | * The 'key' entry may be looked up with tsd_hash_search() by passing the | |
47 | * key you wish to lookup and DTOR_PID constant as the pid. | |
48 | * | |
49 | * The second type of entry is called a 'pid' entry and it is added to the | |
50 | * hash the first time a process set a key. The 'pid' entry is also used | |
51 | * as an anchor and all tsd for the process will be linked to it. This | |
52 | * list is using during tsd_exit() to ensure all registered destructors | |
53 | * are run for the process. The 'pid' entry may be looked up with | |
54 | * tsd_hash_search() by passing the PID_KEY constant as the key, and | |
55 | * the process pid. Note that tsd_exit() is called by thread_exit() | |
56 | * so if your using the Solaris thread API you should not need to call | |
57 | * tsd_exit() directly. | |
58 | * | |
59 | \*****************************************************************************/ | |
60 | ||
61 | #include <sys/kmem.h> | |
62 | #include <sys/thread.h> | |
63 | #include <sys/tsd.h> | |
e5b9b344 | 64 | #include <linux/hash.h> |
9fe45dc1 BB |
65 | |
66 | typedef struct tsd_hash_bin { | |
67 | spinlock_t hb_lock; | |
68 | struct hlist_head hb_head; | |
69 | } tsd_hash_bin_t; | |
70 | ||
71 | typedef struct tsd_hash_table { | |
72 | spinlock_t ht_lock; | |
73 | uint_t ht_bits; | |
74 | uint_t ht_key; | |
75 | tsd_hash_bin_t *ht_bins; | |
76 | } tsd_hash_table_t; | |
77 | ||
78 | typedef struct tsd_hash_entry { | |
79 | uint_t he_key; | |
80 | pid_t he_pid; | |
81 | dtor_func_t he_dtor; | |
82 | void *he_value; | |
83 | struct hlist_node he_list; | |
84 | struct list_head he_key_list; | |
85 | struct list_head he_pid_list; | |
86 | } tsd_hash_entry_t; | |
87 | ||
88 | static tsd_hash_table_t *tsd_hash_table = NULL; | |
89 | ||
90 | ||
91 | /* | |
92 | * tsd_hash_search - searches hash table for tsd_hash_entry | |
93 | * @table: hash table | |
94 | * @key: search key | |
95 | * @pid: search pid | |
96 | */ | |
97 | static tsd_hash_entry_t * | |
98 | tsd_hash_search(tsd_hash_table_t *table, uint_t key, pid_t pid) | |
99 | { | |
100 | struct hlist_node *node; | |
101 | tsd_hash_entry_t *entry; | |
102 | tsd_hash_bin_t *bin; | |
103 | ulong_t hash; | |
9fe45dc1 BB |
104 | |
105 | hash = hash_long((ulong_t)key * (ulong_t)pid, table->ht_bits); | |
106 | bin = &table->ht_bins[hash]; | |
107 | spin_lock(&bin->hb_lock); | |
4a31e5aa RY |
108 | hlist_for_each(node, &bin->hb_head) { |
109 | entry = list_entry(node, tsd_hash_entry_t, he_list); | |
9fe45dc1 BB |
110 | if ((entry->he_key == key) && (entry->he_pid == pid)) { |
111 | spin_unlock(&bin->hb_lock); | |
8d9a23e8 | 112 | return (entry); |
9fe45dc1 BB |
113 | } |
114 | } | |
115 | ||
116 | spin_unlock(&bin->hb_lock); | |
8d9a23e8 | 117 | return (NULL); |
9fe45dc1 BB |
118 | } |
119 | ||
120 | /* | |
121 | * tsd_hash_dtor - call the destructor and free all entries on the list | |
122 | * @work: list of hash entries | |
123 | * | |
124 | * For a list of entries which have all already been removed from the | |
125 | * hash call their registered destructor then free the associated memory. | |
126 | */ | |
127 | static void | |
128 | tsd_hash_dtor(struct hlist_head *work) | |
129 | { | |
130 | tsd_hash_entry_t *entry; | |
9fe45dc1 BB |
131 | |
132 | while (!hlist_empty(work)) { | |
133 | entry = hlist_entry(work->first, tsd_hash_entry_t, he_list); | |
134 | hlist_del(&entry->he_list); | |
135 | ||
136 | if (entry->he_dtor && entry->he_pid != DTOR_PID) | |
137 | entry->he_dtor(entry->he_value); | |
138 | ||
139 | kmem_free(entry, sizeof(tsd_hash_entry_t)); | |
140 | } | |
9fe45dc1 BB |
141 | } |
142 | ||
143 | /* | |
144 | * tsd_hash_add - adds an entry to hash table | |
145 | * @table: hash table | |
146 | * @key: search key | |
147 | * @pid: search pid | |
148 | * | |
149 | * The caller is responsible for ensuring the unique key/pid do not | |
150 | * already exist in the hash table. This possible because all entries | |
151 | * are thread specific thus a concurrent thread will never attempt to | |
152 | * add this key/pid. Because multiple bins must be checked to add | |
153 | * links to the dtor and pid entries the entire table is locked. | |
154 | */ | |
155 | static int | |
156 | tsd_hash_add(tsd_hash_table_t *table, uint_t key, pid_t pid, void *value) | |
157 | { | |
158 | tsd_hash_entry_t *entry, *dtor_entry, *pid_entry; | |
159 | tsd_hash_bin_t *bin; | |
160 | ulong_t hash; | |
161 | int rc = 0; | |
9fe45dc1 BB |
162 | |
163 | ASSERT3P(tsd_hash_search(table, key, pid), ==, NULL); | |
164 | ||
165 | /* New entry allocate structure, set value, and add to hash */ | |
dea3505d | 166 | entry = kmem_alloc(sizeof(tsd_hash_entry_t), KM_PUSHPAGE); |
9fe45dc1 | 167 | if (entry == NULL) |
8d9a23e8 | 168 | return (ENOMEM); |
9fe45dc1 BB |
169 | |
170 | entry->he_key = key; | |
171 | entry->he_pid = pid; | |
172 | entry->he_value = value; | |
173 | INIT_HLIST_NODE(&entry->he_list); | |
174 | INIT_LIST_HEAD(&entry->he_key_list); | |
175 | INIT_LIST_HEAD(&entry->he_pid_list); | |
176 | ||
177 | spin_lock(&table->ht_lock); | |
178 | ||
179 | /* Destructor entry must exist for all valid keys */ | |
180 | dtor_entry = tsd_hash_search(table, entry->he_key, DTOR_PID); | |
181 | ASSERT3P(dtor_entry, !=, NULL); | |
182 | entry->he_dtor = dtor_entry->he_dtor; | |
183 | ||
184 | /* Process entry must exist for all valid processes */ | |
185 | pid_entry = tsd_hash_search(table, PID_KEY, entry->he_pid); | |
186 | ASSERT3P(pid_entry, !=, NULL); | |
187 | ||
188 | hash = hash_long((ulong_t)key * (ulong_t)pid, table->ht_bits); | |
189 | bin = &table->ht_bins[hash]; | |
190 | spin_lock(&bin->hb_lock); | |
191 | ||
192 | /* Add to the hash, key, and pid lists */ | |
193 | hlist_add_head(&entry->he_list, &bin->hb_head); | |
194 | list_add(&entry->he_key_list, &dtor_entry->he_key_list); | |
195 | list_add(&entry->he_pid_list, &pid_entry->he_pid_list); | |
196 | ||
197 | spin_unlock(&bin->hb_lock); | |
198 | spin_unlock(&table->ht_lock); | |
199 | ||
8d9a23e8 | 200 | return (rc); |
9fe45dc1 BB |
201 | } |
202 | ||
203 | /* | |
204 | * tsd_hash_add_key - adds a destructor entry to the hash table | |
205 | * @table: hash table | |
206 | * @keyp: search key | |
207 | * @dtor: key destructor | |
208 | * | |
209 | * For every unique key there is a single entry in the hash which is used | |
210 | * as anchor. All other thread specific entries for this key are linked | |
211 | * to this anchor via the 'he_key_list' list head. On return they keyp | |
212 | * will be set to the next available key for the hash table. | |
213 | */ | |
214 | static int | |
215 | tsd_hash_add_key(tsd_hash_table_t *table, uint_t *keyp, dtor_func_t dtor) | |
216 | { | |
217 | tsd_hash_entry_t *tmp_entry, *entry; | |
218 | tsd_hash_bin_t *bin; | |
219 | ulong_t hash; | |
220 | int keys_checked = 0; | |
9fe45dc1 BB |
221 | |
222 | ASSERT3P(table, !=, NULL); | |
223 | ||
224 | /* Allocate entry to be used as a destructor for this key */ | |
dea3505d | 225 | entry = kmem_alloc(sizeof(tsd_hash_entry_t), KM_PUSHPAGE); |
9fe45dc1 | 226 | if (entry == NULL) |
8d9a23e8 | 227 | return (ENOMEM); |
9fe45dc1 BB |
228 | |
229 | /* Determine next available key value */ | |
230 | spin_lock(&table->ht_lock); | |
231 | do { | |
232 | /* Limited to TSD_KEYS_MAX concurrent unique keys */ | |
233 | if (table->ht_key++ > TSD_KEYS_MAX) | |
234 | table->ht_key = 1; | |
235 | ||
236 | /* Ensure failure when all TSD_KEYS_MAX keys are in use */ | |
237 | if (keys_checked++ >= TSD_KEYS_MAX) { | |
238 | spin_unlock(&table->ht_lock); | |
8d9a23e8 | 239 | return (ENOENT); |
9fe45dc1 BB |
240 | } |
241 | ||
242 | tmp_entry = tsd_hash_search(table, table->ht_key, DTOR_PID); | |
243 | } while (tmp_entry); | |
244 | ||
245 | /* Add destructor entry in to hash table */ | |
246 | entry->he_key = *keyp = table->ht_key; | |
247 | entry->he_pid = DTOR_PID; | |
248 | entry->he_dtor = dtor; | |
249 | entry->he_value = NULL; | |
250 | INIT_HLIST_NODE(&entry->he_list); | |
251 | INIT_LIST_HEAD(&entry->he_key_list); | |
252 | INIT_LIST_HEAD(&entry->he_pid_list); | |
253 | ||
254 | hash = hash_long((ulong_t)*keyp * (ulong_t)DTOR_PID, table->ht_bits); | |
255 | bin = &table->ht_bins[hash]; | |
256 | spin_lock(&bin->hb_lock); | |
257 | ||
258 | hlist_add_head(&entry->he_list, &bin->hb_head); | |
259 | ||
260 | spin_unlock(&bin->hb_lock); | |
261 | spin_unlock(&table->ht_lock); | |
262 | ||
8d9a23e8 | 263 | return (0); |
9fe45dc1 BB |
264 | } |
265 | ||
266 | /* | |
267 | * tsd_hash_add_pid - adds a process entry to the hash table | |
268 | * @table: hash table | |
269 | * @pid: search pid | |
270 | * | |
271 | * For every process these is a single entry in the hash which is used | |
272 | * as anchor. All other thread specific entries for this process are | |
273 | * linked to this anchor via the 'he_pid_list' list head. | |
274 | */ | |
275 | static int | |
276 | tsd_hash_add_pid(tsd_hash_table_t *table, pid_t pid) | |
277 | { | |
278 | tsd_hash_entry_t *entry; | |
279 | tsd_hash_bin_t *bin; | |
280 | ulong_t hash; | |
9fe45dc1 BB |
281 | |
282 | /* Allocate entry to be used as the process reference */ | |
dea3505d | 283 | entry = kmem_alloc(sizeof(tsd_hash_entry_t), KM_PUSHPAGE); |
9fe45dc1 | 284 | if (entry == NULL) |
8d9a23e8 | 285 | return (ENOMEM); |
9fe45dc1 BB |
286 | |
287 | spin_lock(&table->ht_lock); | |
288 | entry->he_key = PID_KEY; | |
289 | entry->he_pid = pid; | |
290 | entry->he_dtor = NULL; | |
291 | entry->he_value = NULL; | |
292 | INIT_HLIST_NODE(&entry->he_list); | |
293 | INIT_LIST_HEAD(&entry->he_key_list); | |
294 | INIT_LIST_HEAD(&entry->he_pid_list); | |
295 | ||
296 | hash = hash_long((ulong_t)PID_KEY * (ulong_t)pid, table->ht_bits); | |
297 | bin = &table->ht_bins[hash]; | |
298 | spin_lock(&bin->hb_lock); | |
299 | ||
300 | hlist_add_head(&entry->he_list, &bin->hb_head); | |
301 | ||
302 | spin_unlock(&bin->hb_lock); | |
303 | spin_unlock(&table->ht_lock); | |
304 | ||
8d9a23e8 | 305 | return (0); |
9fe45dc1 BB |
306 | } |
307 | ||
308 | /* | |
309 | * tsd_hash_del - delete an entry from hash table, key, and pid lists | |
310 | * @table: hash table | |
311 | * @key: search key | |
312 | * @pid: search pid | |
313 | */ | |
314 | static void | |
315 | tsd_hash_del(tsd_hash_table_t *table, tsd_hash_entry_t *entry) | |
316 | { | |
9fe45dc1 BB |
317 | ASSERT(spin_is_locked(&table->ht_lock)); |
318 | hlist_del(&entry->he_list); | |
319 | list_del_init(&entry->he_key_list); | |
320 | list_del_init(&entry->he_pid_list); | |
9fe45dc1 BB |
321 | } |
322 | ||
323 | /* | |
324 | * tsd_hash_table_init - allocate a hash table | |
325 | * @bits: hash table size | |
326 | * | |
327 | * A hash table with 2^bits bins will be created, it may not be resized | |
328 | * after the fact and must be free'd with tsd_hash_table_fini(). | |
329 | */ | |
330 | static tsd_hash_table_t * | |
331 | tsd_hash_table_init(uint_t bits) | |
332 | { | |
333 | tsd_hash_table_t *table; | |
334 | int hash, size = (1 << bits); | |
9fe45dc1 BB |
335 | |
336 | table = kmem_zalloc(sizeof(tsd_hash_table_t), KM_SLEEP); | |
337 | if (table == NULL) | |
8d9a23e8 | 338 | return (NULL); |
9fe45dc1 | 339 | |
46b3945d BB |
340 | table->ht_bins = kmem_zalloc(sizeof(tsd_hash_bin_t) * size, |
341 | KM_SLEEP | KM_NODEBUG); | |
9fe45dc1 BB |
342 | if (table->ht_bins == NULL) { |
343 | kmem_free(table, sizeof(tsd_hash_table_t)); | |
8d9a23e8 | 344 | return (NULL); |
9fe45dc1 BB |
345 | } |
346 | ||
347 | for (hash = 0; hash < size; hash++) { | |
348 | spin_lock_init(&table->ht_bins[hash].hb_lock); | |
349 | INIT_HLIST_HEAD(&table->ht_bins[hash].hb_head); | |
350 | } | |
351 | ||
352 | spin_lock_init(&table->ht_lock); | |
353 | table->ht_bits = bits; | |
354 | table->ht_key = 1; | |
355 | ||
8d9a23e8 | 356 | return (table); |
9fe45dc1 BB |
357 | } |
358 | ||
359 | /* | |
360 | * tsd_hash_table_fini - free a hash table | |
361 | * @table: hash table | |
362 | * | |
363 | * Free a hash table allocated by tsd_hash_table_init(). If the hash | |
364 | * table is not empty this function will call the proper destructor for | |
365 | * all remaining entries before freeing the memory used by those entries. | |
366 | */ | |
367 | static void | |
368 | tsd_hash_table_fini(tsd_hash_table_t *table) | |
369 | { | |
370 | HLIST_HEAD(work); | |
371 | tsd_hash_bin_t *bin; | |
372 | tsd_hash_entry_t *entry; | |
373 | int size, i; | |
9fe45dc1 BB |
374 | |
375 | ASSERT3P(table, !=, NULL); | |
376 | spin_lock(&table->ht_lock); | |
377 | for (i = 0, size = (1 << table->ht_bits); i < size; i++) { | |
378 | bin = &table->ht_bins[i]; | |
379 | spin_lock(&bin->hb_lock); | |
380 | while (!hlist_empty(&bin->hb_head)) { | |
381 | entry = hlist_entry(bin->hb_head.first, | |
382 | tsd_hash_entry_t, he_list); | |
383 | tsd_hash_del(table, entry); | |
384 | hlist_add_head(&entry->he_list, &work); | |
385 | } | |
386 | spin_unlock(&bin->hb_lock); | |
387 | } | |
388 | spin_unlock(&table->ht_lock); | |
389 | ||
390 | tsd_hash_dtor(&work); | |
391 | kmem_free(table->ht_bins, sizeof(tsd_hash_bin_t)*(1<<table->ht_bits)); | |
392 | kmem_free(table, sizeof(tsd_hash_table_t)); | |
9fe45dc1 BB |
393 | } |
394 | ||
395 | /* | |
396 | * tsd_set - set thread specific data | |
397 | * @key: lookup key | |
398 | * @value: value to set | |
399 | * | |
400 | * Caller must prevent racing tsd_create() or tsd_destroy(), protected | |
401 | * from racing tsd_get() or tsd_set() because it is thread specific. | |
402 | * This function has been optimized to be fast for the update case. | |
403 | * When setting the tsd initially it will be slower due to additional | |
404 | * required locking and potential memory allocations. | |
405 | */ | |
406 | int | |
407 | tsd_set(uint_t key, void *value) | |
408 | { | |
409 | tsd_hash_table_t *table; | |
410 | tsd_hash_entry_t *entry; | |
411 | pid_t pid; | |
412 | int rc; | |
9fe45dc1 BB |
413 | |
414 | table = tsd_hash_table; | |
415 | pid = curthread->pid; | |
416 | ASSERT3P(table, !=, NULL); | |
417 | ||
418 | if ((key == 0) || (key > TSD_KEYS_MAX)) | |
8d9a23e8 | 419 | return (EINVAL); |
9fe45dc1 BB |
420 | |
421 | /* Entry already exists in hash table update value */ | |
422 | entry = tsd_hash_search(table, key, pid); | |
423 | if (entry) { | |
424 | entry->he_value = value; | |
8d9a23e8 | 425 | return (0); |
9fe45dc1 BB |
426 | } |
427 | ||
428 | /* Add a process entry to the hash if not yet exists */ | |
429 | entry = tsd_hash_search(table, PID_KEY, pid); | |
430 | if (entry == NULL) { | |
431 | rc = tsd_hash_add_pid(table, pid); | |
432 | if (rc) | |
8d9a23e8 | 433 | return (rc); |
9fe45dc1 BB |
434 | } |
435 | ||
436 | rc = tsd_hash_add(table, key, pid, value); | |
8d9a23e8 | 437 | return (rc); |
9fe45dc1 BB |
438 | } |
439 | EXPORT_SYMBOL(tsd_set); | |
440 | ||
441 | /* | |
442 | * tsd_get - get thread specific data | |
443 | * @key: lookup key | |
444 | * | |
445 | * Caller must prevent racing tsd_create() or tsd_destroy(). This | |
446 | * implementation is designed to be fast and scalable, it does not | |
447 | * lock the entire table only a single hash bin. | |
448 | */ | |
449 | void * | |
450 | tsd_get(uint_t key) | |
451 | { | |
452 | tsd_hash_entry_t *entry; | |
9fe45dc1 BB |
453 | |
454 | ASSERT3P(tsd_hash_table, !=, NULL); | |
455 | ||
456 | if ((key == 0) || (key > TSD_KEYS_MAX)) | |
8d9a23e8 | 457 | return (NULL); |
9fe45dc1 BB |
458 | |
459 | entry = tsd_hash_search(tsd_hash_table, key, curthread->pid); | |
460 | if (entry == NULL) | |
8d9a23e8 | 461 | return (NULL); |
9fe45dc1 | 462 | |
8d9a23e8 | 463 | return (entry->he_value); |
9fe45dc1 BB |
464 | } |
465 | EXPORT_SYMBOL(tsd_get); | |
466 | ||
467 | /* | |
468 | * tsd_create - create thread specific data key | |
469 | * @keyp: lookup key address | |
470 | * @dtor: destructor called during tsd_destroy() or tsd_exit() | |
471 | * | |
472 | * Provided key must be set to 0 or it assumed to be already in use. | |
473 | * The dtor is allowed to be NULL in which case no additional cleanup | |
474 | * for the data is performed during tsd_destroy() or tsd_exit(). | |
475 | * | |
476 | * Caller must prevent racing tsd_set() or tsd_get(), this function is | |
477 | * safe from racing tsd_create(), tsd_destroy(), and tsd_exit(). | |
478 | */ | |
479 | void | |
480 | tsd_create(uint_t *keyp, dtor_func_t dtor) | |
481 | { | |
9fe45dc1 | 482 | ASSERT3P(keyp, !=, NULL); |
8d9a23e8 | 483 | if (*keyp) |
9fe45dc1 | 484 | return; |
9fe45dc1 BB |
485 | |
486 | (void)tsd_hash_add_key(tsd_hash_table, keyp, dtor); | |
9fe45dc1 BB |
487 | } |
488 | EXPORT_SYMBOL(tsd_create); | |
489 | ||
490 | /* | |
491 | * tsd_destroy - destroy thread specific data | |
492 | * @keyp: lookup key address | |
493 | * | |
494 | * Destroys the thread specific data on all threads which use this key. | |
495 | * | |
496 | * Caller must prevent racing tsd_set() or tsd_get(), this function is | |
497 | * safe from racing tsd_create(), tsd_destroy(), and tsd_exit(). | |
498 | */ | |
499 | void | |
500 | tsd_destroy(uint_t *keyp) | |
501 | { | |
502 | HLIST_HEAD(work); | |
503 | tsd_hash_table_t *table; | |
504 | tsd_hash_entry_t *dtor_entry, *entry; | |
6ef94aa6 BB |
505 | tsd_hash_bin_t *dtor_entry_bin, *entry_bin; |
506 | ulong_t hash; | |
9fe45dc1 BB |
507 | |
508 | table = tsd_hash_table; | |
509 | ASSERT3P(table, !=, NULL); | |
510 | ||
511 | spin_lock(&table->ht_lock); | |
512 | dtor_entry = tsd_hash_search(table, *keyp, DTOR_PID); | |
513 | if (dtor_entry == NULL) { | |
514 | spin_unlock(&table->ht_lock); | |
9fe45dc1 BB |
515 | return; |
516 | } | |
517 | ||
518 | /* | |
519 | * All threads which use this key must be linked off of the | |
520 | * DTOR_PID entry. They are removed from the hash table and | |
521 | * linked in to a private working list to be destroyed. | |
522 | */ | |
523 | while (!list_empty(&dtor_entry->he_key_list)) { | |
524 | entry = list_entry(dtor_entry->he_key_list.next, | |
525 | tsd_hash_entry_t, he_key_list); | |
526 | ASSERT3U(dtor_entry->he_key, ==, entry->he_key); | |
527 | ASSERT3P(dtor_entry->he_dtor, ==, entry->he_dtor); | |
6ef94aa6 BB |
528 | |
529 | hash = hash_long((ulong_t)entry->he_key * | |
530 | (ulong_t)entry->he_pid, table->ht_bits); | |
531 | entry_bin = &table->ht_bins[hash]; | |
532 | ||
533 | spin_lock(&entry_bin->hb_lock); | |
9fe45dc1 BB |
534 | tsd_hash_del(table, entry); |
535 | hlist_add_head(&entry->he_list, &work); | |
6ef94aa6 | 536 | spin_unlock(&entry_bin->hb_lock); |
9fe45dc1 BB |
537 | } |
538 | ||
6ef94aa6 BB |
539 | hash = hash_long((ulong_t)dtor_entry->he_key * |
540 | (ulong_t)dtor_entry->he_pid, table->ht_bits); | |
541 | dtor_entry_bin = &table->ht_bins[hash]; | |
542 | ||
543 | spin_lock(&dtor_entry_bin->hb_lock); | |
9fe45dc1 BB |
544 | tsd_hash_del(table, dtor_entry); |
545 | hlist_add_head(&dtor_entry->he_list, &work); | |
6ef94aa6 | 546 | spin_unlock(&dtor_entry_bin->hb_lock); |
9fe45dc1 BB |
547 | spin_unlock(&table->ht_lock); |
548 | ||
549 | tsd_hash_dtor(&work); | |
550 | *keyp = 0; | |
9fe45dc1 BB |
551 | } |
552 | EXPORT_SYMBOL(tsd_destroy); | |
553 | ||
554 | /* | |
555 | * tsd_exit - destroys all thread specific data for this thread | |
556 | * | |
557 | * Destroys all the thread specific data for this thread. | |
558 | * | |
559 | * Caller must prevent racing tsd_set() or tsd_get(), this function is | |
560 | * safe from racing tsd_create(), tsd_destroy(), and tsd_exit(). | |
561 | */ | |
562 | void | |
563 | tsd_exit(void) | |
564 | { | |
565 | HLIST_HEAD(work); | |
566 | tsd_hash_table_t *table; | |
567 | tsd_hash_entry_t *pid_entry, *entry; | |
6ef94aa6 BB |
568 | tsd_hash_bin_t *pid_entry_bin, *entry_bin; |
569 | ulong_t hash; | |
9fe45dc1 BB |
570 | |
571 | table = tsd_hash_table; | |
572 | ASSERT3P(table, !=, NULL); | |
573 | ||
574 | spin_lock(&table->ht_lock); | |
575 | pid_entry = tsd_hash_search(table, PID_KEY, curthread->pid); | |
576 | if (pid_entry == NULL) { | |
577 | spin_unlock(&table->ht_lock); | |
9fe45dc1 BB |
578 | return; |
579 | } | |
580 | ||
581 | /* | |
582 | * All keys associated with this pid must be linked off of the | |
583 | * PID_KEY entry. They are removed from the hash table and | |
6ef94aa6 | 584 | * linked in to a private working list to be destroyed. |
9fe45dc1 | 585 | */ |
6ef94aa6 | 586 | |
9fe45dc1 BB |
587 | while (!list_empty(&pid_entry->he_pid_list)) { |
588 | entry = list_entry(pid_entry->he_pid_list.next, | |
589 | tsd_hash_entry_t, he_pid_list); | |
590 | ASSERT3U(pid_entry->he_pid, ==, entry->he_pid); | |
6ef94aa6 BB |
591 | |
592 | hash = hash_long((ulong_t)entry->he_key * | |
593 | (ulong_t)entry->he_pid, table->ht_bits); | |
594 | entry_bin = &table->ht_bins[hash]; | |
595 | ||
596 | spin_lock(&entry_bin->hb_lock); | |
9fe45dc1 BB |
597 | tsd_hash_del(table, entry); |
598 | hlist_add_head(&entry->he_list, &work); | |
6ef94aa6 | 599 | spin_unlock(&entry_bin->hb_lock); |
9fe45dc1 BB |
600 | } |
601 | ||
6ef94aa6 BB |
602 | hash = hash_long((ulong_t)pid_entry->he_key * |
603 | (ulong_t)pid_entry->he_pid, table->ht_bits); | |
604 | pid_entry_bin = &table->ht_bins[hash]; | |
605 | ||
606 | spin_lock(&pid_entry_bin->hb_lock); | |
9fe45dc1 BB |
607 | tsd_hash_del(table, pid_entry); |
608 | hlist_add_head(&pid_entry->he_list, &work); | |
6ef94aa6 | 609 | spin_unlock(&pid_entry_bin->hb_lock); |
9fe45dc1 BB |
610 | spin_unlock(&table->ht_lock); |
611 | ||
612 | tsd_hash_dtor(&work); | |
9fe45dc1 BB |
613 | } |
614 | EXPORT_SYMBOL(tsd_exit); | |
615 | ||
1114ae6a BB |
616 | int |
617 | spl_tsd_init(void) | |
9fe45dc1 | 618 | { |
9fe45dc1 BB |
619 | tsd_hash_table = tsd_hash_table_init(TSD_HASH_TABLE_BITS_DEFAULT); |
620 | if (tsd_hash_table == NULL) | |
8d9a23e8 | 621 | return (1); |
9fe45dc1 | 622 | |
8d9a23e8 | 623 | return (0); |
9fe45dc1 BB |
624 | } |
625 | ||
1114ae6a BB |
626 | void |
627 | spl_tsd_fini(void) | |
9fe45dc1 | 628 | { |
9fe45dc1 BB |
629 | tsd_hash_table_fini(tsd_hash_table); |
630 | tsd_hash_table = NULL; | |
9fe45dc1 | 631 | } |