]>
Commit | Line | Data |
---|---|---|
62e2eb23 | 1 | /* |
9fe45dc1 BB |
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/>. | |
62e2eb23 BB |
22 | * |
23 | * | |
9fe45dc1 BB |
24 | * Solaris Porting Layer (SPL) Thread Specific Data Implementation. |
25 | * | |
26 | * Thread specific data has implemented using a hash table, this avoids | |
27 | * the need to add a member to the task structure and allows maximum | |
28 | * portability between kernels. This implementation has been optimized | |
29 | * to keep the tsd_set() and tsd_get() times as small as possible. | |
30 | * | |
31 | * The majority of the entries in the hash table are for specific tsd | |
32 | * entries. These entries are hashed by the product of their key and | |
33 | * pid because by design the key and pid are guaranteed to be unique. | |
34 | * Their product also has the desirable properly that it will be uniformly | |
35 | * distributed over the hash bins providing neither the pid nor key is zero. | |
36 | * Under linux the zero pid is always the init process and thus won't be | |
37 | * used, and this implementation is careful to never to assign a zero key. | |
38 | * By default the hash table is sized to 512 bins which is expected to | |
39 | * be sufficient for light to moderate usage of thread specific data. | |
40 | * | |
41 | * The hash table contains two additional type of entries. They first | |
42 | * type is entry is called a 'key' entry and it is added to the hash during | |
43 | * tsd_create(). It is used to store the address of the destructor function | |
44 | * and it is used as an anchor point. All tsd entries which use the same | |
45 | * key will be linked to this entry. This is used during tsd_destory() to | |
46 | * quickly call the destructor function for all tsd associated with the key. | |
47 | * The 'key' entry may be looked up with tsd_hash_search() by passing the | |
48 | * key you wish to lookup and DTOR_PID constant as the pid. | |
49 | * | |
50 | * The second type of entry is called a 'pid' entry and it is added to the | |
51 | * hash the first time a process set a key. The 'pid' entry is also used | |
52 | * as an anchor and all tsd for the process will be linked to it. This | |
53 | * list is using during tsd_exit() to ensure all registered destructors | |
54 | * are run for the process. The 'pid' entry may be looked up with | |
55 | * tsd_hash_search() by passing the PID_KEY constant as the key, and | |
56 | * the process pid. Note that tsd_exit() is called by thread_exit() | |
57 | * so if your using the Solaris thread API you should not need to call | |
58 | * tsd_exit() directly. | |
59 | * | |
62e2eb23 | 60 | */ |
9fe45dc1 BB |
61 | |
62 | #include <sys/kmem.h> | |
63 | #include <sys/thread.h> | |
64 | #include <sys/tsd.h> | |
e5b9b344 | 65 | #include <linux/hash.h> |
9fe45dc1 BB |
66 | |
67 | typedef struct tsd_hash_bin { | |
68 | spinlock_t hb_lock; | |
69 | struct hlist_head hb_head; | |
70 | } tsd_hash_bin_t; | |
71 | ||
72 | typedef struct tsd_hash_table { | |
73 | spinlock_t ht_lock; | |
74 | uint_t ht_bits; | |
75 | uint_t ht_key; | |
76 | tsd_hash_bin_t *ht_bins; | |
77 | } tsd_hash_table_t; | |
78 | ||
79 | typedef struct tsd_hash_entry { | |
80 | uint_t he_key; | |
81 | pid_t he_pid; | |
82 | dtor_func_t he_dtor; | |
83 | void *he_value; | |
84 | struct hlist_node he_list; | |
85 | struct list_head he_key_list; | |
86 | struct list_head he_pid_list; | |
87 | } tsd_hash_entry_t; | |
88 | ||
89 | static tsd_hash_table_t *tsd_hash_table = NULL; | |
90 | ||
91 | ||
92 | /* | |
93 | * tsd_hash_search - searches hash table for tsd_hash_entry | |
94 | * @table: hash table | |
95 | * @key: search key | |
96 | * @pid: search pid | |
97 | */ | |
98 | static tsd_hash_entry_t * | |
99 | tsd_hash_search(tsd_hash_table_t *table, uint_t key, pid_t pid) | |
100 | { | |
101 | struct hlist_node *node; | |
102 | tsd_hash_entry_t *entry; | |
103 | tsd_hash_bin_t *bin; | |
104 | ulong_t hash; | |
9fe45dc1 BB |
105 | |
106 | hash = hash_long((ulong_t)key * (ulong_t)pid, table->ht_bits); | |
107 | bin = &table->ht_bins[hash]; | |
108 | spin_lock(&bin->hb_lock); | |
4a31e5aa RY |
109 | hlist_for_each(node, &bin->hb_head) { |
110 | entry = list_entry(node, tsd_hash_entry_t, he_list); | |
9fe45dc1 BB |
111 | if ((entry->he_key == key) && (entry->he_pid == pid)) { |
112 | spin_unlock(&bin->hb_lock); | |
8d9a23e8 | 113 | return (entry); |
9fe45dc1 BB |
114 | } |
115 | } | |
116 | ||
117 | spin_unlock(&bin->hb_lock); | |
8d9a23e8 | 118 | return (NULL); |
9fe45dc1 BB |
119 | } |
120 | ||
121 | /* | |
122 | * tsd_hash_dtor - call the destructor and free all entries on the list | |
123 | * @work: list of hash entries | |
124 | * | |
125 | * For a list of entries which have all already been removed from the | |
126 | * hash call their registered destructor then free the associated memory. | |
127 | */ | |
128 | static void | |
129 | tsd_hash_dtor(struct hlist_head *work) | |
130 | { | |
131 | tsd_hash_entry_t *entry; | |
9fe45dc1 BB |
132 | |
133 | while (!hlist_empty(work)) { | |
134 | entry = hlist_entry(work->first, tsd_hash_entry_t, he_list); | |
135 | hlist_del(&entry->he_list); | |
136 | ||
137 | if (entry->he_dtor && entry->he_pid != DTOR_PID) | |
138 | entry->he_dtor(entry->he_value); | |
139 | ||
62e2eb23 | 140 | kmem_free(entry, sizeof (tsd_hash_entry_t)); |
9fe45dc1 | 141 | } |
9fe45dc1 BB |
142 | } |
143 | ||
144 | /* | |
145 | * tsd_hash_add - adds an entry to hash table | |
146 | * @table: hash table | |
147 | * @key: search key | |
148 | * @pid: search pid | |
149 | * | |
150 | * The caller is responsible for ensuring the unique key/pid do not | |
151 | * already exist in the hash table. This possible because all entries | |
152 | * are thread specific thus a concurrent thread will never attempt to | |
153 | * add this key/pid. Because multiple bins must be checked to add | |
154 | * links to the dtor and pid entries the entire table is locked. | |
155 | */ | |
156 | static int | |
157 | tsd_hash_add(tsd_hash_table_t *table, uint_t key, pid_t pid, void *value) | |
158 | { | |
159 | tsd_hash_entry_t *entry, *dtor_entry, *pid_entry; | |
160 | tsd_hash_bin_t *bin; | |
161 | ulong_t hash; | |
162 | int rc = 0; | |
9fe45dc1 BB |
163 | |
164 | ASSERT3P(tsd_hash_search(table, key, pid), ==, NULL); | |
165 | ||
166 | /* New entry allocate structure, set value, and add to hash */ | |
62e2eb23 | 167 | entry = kmem_alloc(sizeof (tsd_hash_entry_t), KM_PUSHPAGE); |
9fe45dc1 | 168 | if (entry == NULL) |
8d9a23e8 | 169 | return (ENOMEM); |
9fe45dc1 BB |
170 | |
171 | entry->he_key = key; | |
172 | entry->he_pid = pid; | |
173 | entry->he_value = value; | |
174 | INIT_HLIST_NODE(&entry->he_list); | |
175 | INIT_LIST_HEAD(&entry->he_key_list); | |
176 | INIT_LIST_HEAD(&entry->he_pid_list); | |
177 | ||
178 | spin_lock(&table->ht_lock); | |
179 | ||
180 | /* Destructor entry must exist for all valid keys */ | |
181 | dtor_entry = tsd_hash_search(table, entry->he_key, DTOR_PID); | |
182 | ASSERT3P(dtor_entry, !=, NULL); | |
183 | entry->he_dtor = dtor_entry->he_dtor; | |
184 | ||
185 | /* Process entry must exist for all valid processes */ | |
186 | pid_entry = tsd_hash_search(table, PID_KEY, entry->he_pid); | |
187 | ASSERT3P(pid_entry, !=, NULL); | |
188 | ||
189 | hash = hash_long((ulong_t)key * (ulong_t)pid, table->ht_bits); | |
190 | bin = &table->ht_bins[hash]; | |
191 | spin_lock(&bin->hb_lock); | |
192 | ||
193 | /* Add to the hash, key, and pid lists */ | |
194 | hlist_add_head(&entry->he_list, &bin->hb_head); | |
195 | list_add(&entry->he_key_list, &dtor_entry->he_key_list); | |
196 | list_add(&entry->he_pid_list, &pid_entry->he_pid_list); | |
197 | ||
198 | spin_unlock(&bin->hb_lock); | |
199 | spin_unlock(&table->ht_lock); | |
200 | ||
8d9a23e8 | 201 | return (rc); |
9fe45dc1 BB |
202 | } |
203 | ||
204 | /* | |
205 | * tsd_hash_add_key - adds a destructor entry to the hash table | |
206 | * @table: hash table | |
207 | * @keyp: search key | |
208 | * @dtor: key destructor | |
209 | * | |
210 | * For every unique key there is a single entry in the hash which is used | |
211 | * as anchor. All other thread specific entries for this key are linked | |
212 | * to this anchor via the 'he_key_list' list head. On return they keyp | |
213 | * will be set to the next available key for the hash table. | |
214 | */ | |
215 | static int | |
216 | tsd_hash_add_key(tsd_hash_table_t *table, uint_t *keyp, dtor_func_t dtor) | |
217 | { | |
218 | tsd_hash_entry_t *tmp_entry, *entry; | |
219 | tsd_hash_bin_t *bin; | |
220 | ulong_t hash; | |
221 | int keys_checked = 0; | |
9fe45dc1 BB |
222 | |
223 | ASSERT3P(table, !=, NULL); | |
224 | ||
225 | /* Allocate entry to be used as a destructor for this key */ | |
62e2eb23 | 226 | entry = kmem_alloc(sizeof (tsd_hash_entry_t), KM_PUSHPAGE); |
9fe45dc1 | 227 | if (entry == NULL) |
8d9a23e8 | 228 | return (ENOMEM); |
9fe45dc1 BB |
229 | |
230 | /* Determine next available key value */ | |
231 | spin_lock(&table->ht_lock); | |
232 | do { | |
233 | /* Limited to TSD_KEYS_MAX concurrent unique keys */ | |
234 | if (table->ht_key++ > TSD_KEYS_MAX) | |
235 | table->ht_key = 1; | |
236 | ||
237 | /* Ensure failure when all TSD_KEYS_MAX keys are in use */ | |
238 | if (keys_checked++ >= TSD_KEYS_MAX) { | |
239 | spin_unlock(&table->ht_lock); | |
8d9a23e8 | 240 | return (ENOENT); |
9fe45dc1 BB |
241 | } |
242 | ||
243 | tmp_entry = tsd_hash_search(table, table->ht_key, DTOR_PID); | |
244 | } while (tmp_entry); | |
245 | ||
246 | /* Add destructor entry in to hash table */ | |
247 | entry->he_key = *keyp = table->ht_key; | |
248 | entry->he_pid = DTOR_PID; | |
249 | entry->he_dtor = dtor; | |
250 | entry->he_value = NULL; | |
251 | INIT_HLIST_NODE(&entry->he_list); | |
252 | INIT_LIST_HEAD(&entry->he_key_list); | |
253 | INIT_LIST_HEAD(&entry->he_pid_list); | |
254 | ||
255 | hash = hash_long((ulong_t)*keyp * (ulong_t)DTOR_PID, table->ht_bits); | |
256 | bin = &table->ht_bins[hash]; | |
257 | spin_lock(&bin->hb_lock); | |
258 | ||
259 | hlist_add_head(&entry->he_list, &bin->hb_head); | |
260 | ||
261 | spin_unlock(&bin->hb_lock); | |
262 | spin_unlock(&table->ht_lock); | |
263 | ||
8d9a23e8 | 264 | return (0); |
9fe45dc1 BB |
265 | } |
266 | ||
267 | /* | |
268 | * tsd_hash_add_pid - adds a process entry to the hash table | |
269 | * @table: hash table | |
270 | * @pid: search pid | |
271 | * | |
272 | * For every process these is a single entry in the hash which is used | |
273 | * as anchor. All other thread specific entries for this process are | |
274 | * linked to this anchor via the 'he_pid_list' list head. | |
275 | */ | |
276 | static int | |
277 | tsd_hash_add_pid(tsd_hash_table_t *table, pid_t pid) | |
278 | { | |
279 | tsd_hash_entry_t *entry; | |
280 | tsd_hash_bin_t *bin; | |
281 | ulong_t hash; | |
9fe45dc1 BB |
282 | |
283 | /* Allocate entry to be used as the process reference */ | |
62e2eb23 | 284 | entry = kmem_alloc(sizeof (tsd_hash_entry_t), KM_PUSHPAGE); |
9fe45dc1 | 285 | if (entry == NULL) |
8d9a23e8 | 286 | return (ENOMEM); |
9fe45dc1 BB |
287 | |
288 | spin_lock(&table->ht_lock); | |
289 | entry->he_key = PID_KEY; | |
290 | entry->he_pid = pid; | |
291 | entry->he_dtor = NULL; | |
292 | entry->he_value = NULL; | |
293 | INIT_HLIST_NODE(&entry->he_list); | |
294 | INIT_LIST_HEAD(&entry->he_key_list); | |
295 | INIT_LIST_HEAD(&entry->he_pid_list); | |
296 | ||
297 | hash = hash_long((ulong_t)PID_KEY * (ulong_t)pid, table->ht_bits); | |
298 | bin = &table->ht_bins[hash]; | |
299 | spin_lock(&bin->hb_lock); | |
300 | ||
301 | hlist_add_head(&entry->he_list, &bin->hb_head); | |
302 | ||
303 | spin_unlock(&bin->hb_lock); | |
304 | spin_unlock(&table->ht_lock); | |
305 | ||
8d9a23e8 | 306 | return (0); |
9fe45dc1 BB |
307 | } |
308 | ||
309 | /* | |
310 | * tsd_hash_del - delete an entry from hash table, key, and pid lists | |
311 | * @table: hash table | |
312 | * @key: search key | |
313 | * @pid: search pid | |
314 | */ | |
315 | static void | |
316 | tsd_hash_del(tsd_hash_table_t *table, tsd_hash_entry_t *entry) | |
317 | { | |
9fe45dc1 BB |
318 | ASSERT(spin_is_locked(&table->ht_lock)); |
319 | hlist_del(&entry->he_list); | |
320 | list_del_init(&entry->he_key_list); | |
321 | list_del_init(&entry->he_pid_list); | |
9fe45dc1 BB |
322 | } |
323 | ||
324 | /* | |
325 | * tsd_hash_table_init - allocate a hash table | |
326 | * @bits: hash table size | |
327 | * | |
328 | * A hash table with 2^bits bins will be created, it may not be resized | |
329 | * after the fact and must be free'd with tsd_hash_table_fini(). | |
330 | */ | |
331 | static tsd_hash_table_t * | |
332 | tsd_hash_table_init(uint_t bits) | |
333 | { | |
334 | tsd_hash_table_t *table; | |
335 | int hash, size = (1 << bits); | |
9fe45dc1 | 336 | |
62e2eb23 | 337 | table = kmem_zalloc(sizeof (tsd_hash_table_t), KM_SLEEP); |
9fe45dc1 | 338 | if (table == NULL) |
8d9a23e8 | 339 | return (NULL); |
9fe45dc1 | 340 | |
62e2eb23 | 341 | table->ht_bins = kmem_zalloc(sizeof (tsd_hash_bin_t) * size, KM_SLEEP); |
9fe45dc1 | 342 | if (table->ht_bins == NULL) { |
62e2eb23 | 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); | |
62e2eb23 | 380 | while (!hlist_empty(&bin->hb_head)) { |
9fe45dc1 | 381 | entry = hlist_entry(bin->hb_head.first, |
62e2eb23 | 382 | tsd_hash_entry_t, he_list); |
9fe45dc1 BB |
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); | |
62e2eb23 BB |
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 | ||
3d39d0af CC |
395 | /* |
396 | * tsd_remove_entry - remove a tsd entry for this thread | |
397 | * @entry: entry to remove | |
398 | * | |
399 | * Remove the thread specific data @entry for this thread. | |
400 | * If this is the last entry for this thread, also remove the PID entry. | |
401 | */ | |
402 | static void | |
403 | tsd_remove_entry(tsd_hash_entry_t *entry) | |
404 | { | |
405 | HLIST_HEAD(work); | |
406 | tsd_hash_table_t *table; | |
407 | tsd_hash_entry_t *pid_entry; | |
408 | tsd_hash_bin_t *pid_entry_bin, *entry_bin; | |
409 | ulong_t hash; | |
410 | ||
411 | table = tsd_hash_table; | |
412 | ASSERT3P(table, !=, NULL); | |
413 | ASSERT3P(entry, !=, NULL); | |
414 | ||
415 | spin_lock(&table->ht_lock); | |
416 | ||
417 | hash = hash_long((ulong_t)entry->he_key * | |
418 | (ulong_t)entry->he_pid, table->ht_bits); | |
419 | entry_bin = &table->ht_bins[hash]; | |
420 | ||
421 | /* save the possible pid_entry */ | |
422 | pid_entry = list_entry(entry->he_pid_list.next, tsd_hash_entry_t, | |
423 | he_pid_list); | |
424 | ||
425 | /* remove entry */ | |
426 | spin_lock(&entry_bin->hb_lock); | |
427 | tsd_hash_del(table, entry); | |
428 | hlist_add_head(&entry->he_list, &work); | |
429 | spin_unlock(&entry_bin->hb_lock); | |
430 | ||
431 | /* if pid_entry is indeed pid_entry, then remove it if it's empty */ | |
432 | if (pid_entry->he_key == PID_KEY && | |
433 | list_empty(&pid_entry->he_pid_list)) { | |
434 | hash = hash_long((ulong_t)pid_entry->he_key * | |
435 | (ulong_t)pid_entry->he_pid, table->ht_bits); | |
436 | pid_entry_bin = &table->ht_bins[hash]; | |
437 | ||
438 | spin_lock(&pid_entry_bin->hb_lock); | |
439 | tsd_hash_del(table, pid_entry); | |
440 | hlist_add_head(&pid_entry->he_list, &work); | |
441 | spin_unlock(&pid_entry_bin->hb_lock); | |
442 | } | |
443 | ||
444 | spin_unlock(&table->ht_lock); | |
445 | ||
446 | tsd_hash_dtor(&work); | |
447 | } | |
448 | ||
9fe45dc1 BB |
449 | /* |
450 | * tsd_set - set thread specific data | |
451 | * @key: lookup key | |
452 | * @value: value to set | |
453 | * | |
454 | * Caller must prevent racing tsd_create() or tsd_destroy(), protected | |
455 | * from racing tsd_get() or tsd_set() because it is thread specific. | |
456 | * This function has been optimized to be fast for the update case. | |
457 | * When setting the tsd initially it will be slower due to additional | |
458 | * required locking and potential memory allocations. | |
459 | */ | |
460 | int | |
461 | tsd_set(uint_t key, void *value) | |
462 | { | |
463 | tsd_hash_table_t *table; | |
464 | tsd_hash_entry_t *entry; | |
465 | pid_t pid; | |
466 | int rc; | |
3d39d0af CC |
467 | /* mark remove if value is NULL */ |
468 | boolean_t remove = (value == NULL); | |
9fe45dc1 BB |
469 | |
470 | table = tsd_hash_table; | |
471 | pid = curthread->pid; | |
472 | ASSERT3P(table, !=, NULL); | |
473 | ||
474 | if ((key == 0) || (key > TSD_KEYS_MAX)) | |
8d9a23e8 | 475 | return (EINVAL); |
9fe45dc1 BB |
476 | |
477 | /* Entry already exists in hash table update value */ | |
478 | entry = tsd_hash_search(table, key, pid); | |
479 | if (entry) { | |
480 | entry->he_value = value; | |
3d39d0af CC |
481 | /* remove the entry */ |
482 | if (remove) | |
483 | tsd_remove_entry(entry); | |
8d9a23e8 | 484 | return (0); |
9fe45dc1 BB |
485 | } |
486 | ||
3d39d0af CC |
487 | /* don't create entry if value is NULL */ |
488 | if (remove) | |
489 | return (0); | |
490 | ||
9fe45dc1 BB |
491 | /* Add a process entry to the hash if not yet exists */ |
492 | entry = tsd_hash_search(table, PID_KEY, pid); | |
493 | if (entry == NULL) { | |
494 | rc = tsd_hash_add_pid(table, pid); | |
495 | if (rc) | |
8d9a23e8 | 496 | return (rc); |
9fe45dc1 BB |
497 | } |
498 | ||
499 | rc = tsd_hash_add(table, key, pid, value); | |
8d9a23e8 | 500 | return (rc); |
9fe45dc1 BB |
501 | } |
502 | EXPORT_SYMBOL(tsd_set); | |
503 | ||
504 | /* | |
505 | * tsd_get - get thread specific data | |
506 | * @key: lookup key | |
507 | * | |
508 | * Caller must prevent racing tsd_create() or tsd_destroy(). This | |
509 | * implementation is designed to be fast and scalable, it does not | |
510 | * lock the entire table only a single hash bin. | |
511 | */ | |
512 | void * | |
513 | tsd_get(uint_t key) | |
514 | { | |
515 | tsd_hash_entry_t *entry; | |
9fe45dc1 BB |
516 | |
517 | ASSERT3P(tsd_hash_table, !=, NULL); | |
518 | ||
519 | if ((key == 0) || (key > TSD_KEYS_MAX)) | |
8d9a23e8 | 520 | return (NULL); |
9fe45dc1 BB |
521 | |
522 | entry = tsd_hash_search(tsd_hash_table, key, curthread->pid); | |
523 | if (entry == NULL) | |
8d9a23e8 | 524 | return (NULL); |
9fe45dc1 | 525 | |
8d9a23e8 | 526 | return (entry->he_value); |
9fe45dc1 BB |
527 | } |
528 | EXPORT_SYMBOL(tsd_get); | |
529 | ||
16522ac2 CC |
530 | /* |
531 | * tsd_get_by_thread - get thread specific data for specified thread | |
532 | * @key: lookup key | |
533 | * @thread: thread to lookup | |
534 | * | |
535 | * Caller must prevent racing tsd_create() or tsd_destroy(). This | |
536 | * implementation is designed to be fast and scalable, it does not | |
537 | * lock the entire table only a single hash bin. | |
538 | */ | |
539 | void * | |
540 | tsd_get_by_thread(uint_t key, kthread_t *thread) | |
541 | { | |
542 | tsd_hash_entry_t *entry; | |
543 | ||
544 | ASSERT3P(tsd_hash_table, !=, NULL); | |
545 | ||
546 | if ((key == 0) || (key > TSD_KEYS_MAX)) | |
547 | return (NULL); | |
548 | ||
549 | entry = tsd_hash_search(tsd_hash_table, key, thread->pid); | |
550 | if (entry == NULL) | |
551 | return (NULL); | |
552 | ||
553 | return (entry->he_value); | |
554 | } | |
555 | EXPORT_SYMBOL(tsd_get_by_thread); | |
556 | ||
9fe45dc1 BB |
557 | /* |
558 | * tsd_create - create thread specific data key | |
559 | * @keyp: lookup key address | |
560 | * @dtor: destructor called during tsd_destroy() or tsd_exit() | |
561 | * | |
562 | * Provided key must be set to 0 or it assumed to be already in use. | |
563 | * The dtor is allowed to be NULL in which case no additional cleanup | |
564 | * for the data is performed during tsd_destroy() or tsd_exit(). | |
565 | * | |
566 | * Caller must prevent racing tsd_set() or tsd_get(), this function is | |
567 | * safe from racing tsd_create(), tsd_destroy(), and tsd_exit(). | |
568 | */ | |
569 | void | |
570 | tsd_create(uint_t *keyp, dtor_func_t dtor) | |
571 | { | |
9fe45dc1 | 572 | ASSERT3P(keyp, !=, NULL); |
8d9a23e8 | 573 | if (*keyp) |
9fe45dc1 | 574 | return; |
9fe45dc1 | 575 | |
62e2eb23 | 576 | (void) tsd_hash_add_key(tsd_hash_table, keyp, dtor); |
9fe45dc1 BB |
577 | } |
578 | EXPORT_SYMBOL(tsd_create); | |
579 | ||
580 | /* | |
581 | * tsd_destroy - destroy thread specific data | |
582 | * @keyp: lookup key address | |
583 | * | |
584 | * Destroys the thread specific data on all threads which use this key. | |
585 | * | |
586 | * Caller must prevent racing tsd_set() or tsd_get(), this function is | |
587 | * safe from racing tsd_create(), tsd_destroy(), and tsd_exit(). | |
588 | */ | |
589 | void | |
590 | tsd_destroy(uint_t *keyp) | |
591 | { | |
592 | HLIST_HEAD(work); | |
593 | tsd_hash_table_t *table; | |
594 | tsd_hash_entry_t *dtor_entry, *entry; | |
6ef94aa6 BB |
595 | tsd_hash_bin_t *dtor_entry_bin, *entry_bin; |
596 | ulong_t hash; | |
9fe45dc1 BB |
597 | |
598 | table = tsd_hash_table; | |
599 | ASSERT3P(table, !=, NULL); | |
600 | ||
601 | spin_lock(&table->ht_lock); | |
602 | dtor_entry = tsd_hash_search(table, *keyp, DTOR_PID); | |
603 | if (dtor_entry == NULL) { | |
604 | spin_unlock(&table->ht_lock); | |
9fe45dc1 BB |
605 | return; |
606 | } | |
607 | ||
608 | /* | |
609 | * All threads which use this key must be linked off of the | |
610 | * DTOR_PID entry. They are removed from the hash table and | |
611 | * linked in to a private working list to be destroyed. | |
612 | */ | |
62e2eb23 | 613 | while (!list_empty(&dtor_entry->he_key_list)) { |
9fe45dc1 | 614 | entry = list_entry(dtor_entry->he_key_list.next, |
62e2eb23 | 615 | tsd_hash_entry_t, he_key_list); |
9fe45dc1 BB |
616 | ASSERT3U(dtor_entry->he_key, ==, entry->he_key); |
617 | ASSERT3P(dtor_entry->he_dtor, ==, entry->he_dtor); | |
6ef94aa6 BB |
618 | |
619 | hash = hash_long((ulong_t)entry->he_key * | |
62e2eb23 | 620 | (ulong_t)entry->he_pid, table->ht_bits); |
6ef94aa6 BB |
621 | entry_bin = &table->ht_bins[hash]; |
622 | ||
623 | spin_lock(&entry_bin->hb_lock); | |
9fe45dc1 BB |
624 | tsd_hash_del(table, entry); |
625 | hlist_add_head(&entry->he_list, &work); | |
6ef94aa6 | 626 | spin_unlock(&entry_bin->hb_lock); |
9fe45dc1 BB |
627 | } |
628 | ||
6ef94aa6 BB |
629 | hash = hash_long((ulong_t)dtor_entry->he_key * |
630 | (ulong_t)dtor_entry->he_pid, table->ht_bits); | |
631 | dtor_entry_bin = &table->ht_bins[hash]; | |
632 | ||
633 | spin_lock(&dtor_entry_bin->hb_lock); | |
9fe45dc1 BB |
634 | tsd_hash_del(table, dtor_entry); |
635 | hlist_add_head(&dtor_entry->he_list, &work); | |
6ef94aa6 | 636 | spin_unlock(&dtor_entry_bin->hb_lock); |
9fe45dc1 BB |
637 | spin_unlock(&table->ht_lock); |
638 | ||
639 | tsd_hash_dtor(&work); | |
640 | *keyp = 0; | |
9fe45dc1 BB |
641 | } |
642 | EXPORT_SYMBOL(tsd_destroy); | |
643 | ||
644 | /* | |
645 | * tsd_exit - destroys all thread specific data for this thread | |
646 | * | |
647 | * Destroys all the thread specific data for this thread. | |
648 | * | |
649 | * Caller must prevent racing tsd_set() or tsd_get(), this function is | |
650 | * safe from racing tsd_create(), tsd_destroy(), and tsd_exit(). | |
651 | */ | |
652 | void | |
653 | tsd_exit(void) | |
654 | { | |
655 | HLIST_HEAD(work); | |
656 | tsd_hash_table_t *table; | |
657 | tsd_hash_entry_t *pid_entry, *entry; | |
6ef94aa6 BB |
658 | tsd_hash_bin_t *pid_entry_bin, *entry_bin; |
659 | ulong_t hash; | |
9fe45dc1 BB |
660 | |
661 | table = tsd_hash_table; | |
662 | ASSERT3P(table, !=, NULL); | |
663 | ||
664 | spin_lock(&table->ht_lock); | |
665 | pid_entry = tsd_hash_search(table, PID_KEY, curthread->pid); | |
666 | if (pid_entry == NULL) { | |
667 | spin_unlock(&table->ht_lock); | |
9fe45dc1 BB |
668 | return; |
669 | } | |
670 | ||
671 | /* | |
672 | * All keys associated with this pid must be linked off of the | |
673 | * PID_KEY entry. They are removed from the hash table and | |
6ef94aa6 | 674 | * linked in to a private working list to be destroyed. |
9fe45dc1 | 675 | */ |
6ef94aa6 | 676 | |
62e2eb23 | 677 | while (!list_empty(&pid_entry->he_pid_list)) { |
9fe45dc1 | 678 | entry = list_entry(pid_entry->he_pid_list.next, |
62e2eb23 | 679 | tsd_hash_entry_t, he_pid_list); |
9fe45dc1 | 680 | ASSERT3U(pid_entry->he_pid, ==, entry->he_pid); |
6ef94aa6 BB |
681 | |
682 | hash = hash_long((ulong_t)entry->he_key * | |
683 | (ulong_t)entry->he_pid, table->ht_bits); | |
684 | entry_bin = &table->ht_bins[hash]; | |
685 | ||
686 | spin_lock(&entry_bin->hb_lock); | |
9fe45dc1 BB |
687 | tsd_hash_del(table, entry); |
688 | hlist_add_head(&entry->he_list, &work); | |
6ef94aa6 | 689 | spin_unlock(&entry_bin->hb_lock); |
9fe45dc1 BB |
690 | } |
691 | ||
6ef94aa6 BB |
692 | hash = hash_long((ulong_t)pid_entry->he_key * |
693 | (ulong_t)pid_entry->he_pid, table->ht_bits); | |
694 | pid_entry_bin = &table->ht_bins[hash]; | |
695 | ||
696 | spin_lock(&pid_entry_bin->hb_lock); | |
9fe45dc1 BB |
697 | tsd_hash_del(table, pid_entry); |
698 | hlist_add_head(&pid_entry->he_list, &work); | |
6ef94aa6 | 699 | spin_unlock(&pid_entry_bin->hb_lock); |
9fe45dc1 BB |
700 | spin_unlock(&table->ht_lock); |
701 | ||
702 | tsd_hash_dtor(&work); | |
9fe45dc1 BB |
703 | } |
704 | EXPORT_SYMBOL(tsd_exit); | |
705 | ||
1114ae6a BB |
706 | int |
707 | spl_tsd_init(void) | |
9fe45dc1 | 708 | { |
9fe45dc1 BB |
709 | tsd_hash_table = tsd_hash_table_init(TSD_HASH_TABLE_BITS_DEFAULT); |
710 | if (tsd_hash_table == NULL) | |
8d9a23e8 | 711 | return (1); |
9fe45dc1 | 712 | |
8d9a23e8 | 713 | return (0); |
9fe45dc1 BB |
714 | } |
715 | ||
1114ae6a BB |
716 | void |
717 | spl_tsd_fini(void) | |
9fe45dc1 | 718 | { |
9fe45dc1 BB |
719 | tsd_hash_table_fini(tsd_hash_table); |
720 | tsd_hash_table = NULL; | |
9fe45dc1 | 721 | } |