]> git.proxmox.com Git - mirror_spl.git/blob - module/spl/spl-tsd.c
c9d532f4e7a4bd76ab1e0c67c20b5e08876a0228
[mirror_spl.git] / module / spl / spl-tsd.c
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.
8 * For details, see <http://zfsonlinux.org/>.
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>
64
65 typedef struct tsd_hash_bin {
66 spinlock_t hb_lock;
67 struct hlist_head hb_head;
68 } tsd_hash_bin_t;
69
70 typedef struct tsd_hash_table {
71 spinlock_t ht_lock;
72 uint_t ht_bits;
73 uint_t ht_key;
74 tsd_hash_bin_t *ht_bins;
75 } tsd_hash_table_t;
76
77 typedef struct tsd_hash_entry {
78 uint_t he_key;
79 pid_t he_pid;
80 dtor_func_t he_dtor;
81 void *he_value;
82 struct hlist_node he_list;
83 struct list_head he_key_list;
84 struct list_head he_pid_list;
85 } tsd_hash_entry_t;
86
87 static tsd_hash_table_t *tsd_hash_table = NULL;
88
89
90 /*
91 * tsd_hash_search - searches hash table for tsd_hash_entry
92 * @table: hash table
93 * @key: search key
94 * @pid: search pid
95 */
96 static tsd_hash_entry_t *
97 tsd_hash_search(tsd_hash_table_t *table, uint_t key, pid_t pid)
98 {
99 struct hlist_node *node;
100 tsd_hash_entry_t *entry;
101 tsd_hash_bin_t *bin;
102 ulong_t hash;
103
104 hash = hash_long((ulong_t)key * (ulong_t)pid, table->ht_bits);
105 bin = &table->ht_bins[hash];
106 spin_lock(&bin->hb_lock);
107 hlist_for_each(node, &bin->hb_head) {
108 entry = list_entry(node, tsd_hash_entry_t, he_list);
109 if ((entry->he_key == key) && (entry->he_pid == pid)) {
110 spin_unlock(&bin->hb_lock);
111 return (entry);
112 }
113 }
114
115 spin_unlock(&bin->hb_lock);
116 return (NULL);
117 }
118
119 /*
120 * tsd_hash_dtor - call the destructor and free all entries on the list
121 * @work: list of hash entries
122 *
123 * For a list of entries which have all already been removed from the
124 * hash call their registered destructor then free the associated memory.
125 */
126 static void
127 tsd_hash_dtor(struct hlist_head *work)
128 {
129 tsd_hash_entry_t *entry;
130
131 while (!hlist_empty(work)) {
132 entry = hlist_entry(work->first, tsd_hash_entry_t, he_list);
133 hlist_del(&entry->he_list);
134
135 if (entry->he_dtor && entry->he_pid != DTOR_PID)
136 entry->he_dtor(entry->he_value);
137
138 kmem_free(entry, sizeof(tsd_hash_entry_t));
139 }
140 }
141
142 /*
143 * tsd_hash_add - adds an entry to hash table
144 * @table: hash table
145 * @key: search key
146 * @pid: search pid
147 *
148 * The caller is responsible for ensuring the unique key/pid do not
149 * already exist in the hash table. This possible because all entries
150 * are thread specific thus a concurrent thread will never attempt to
151 * add this key/pid. Because multiple bins must be checked to add
152 * links to the dtor and pid entries the entire table is locked.
153 */
154 static int
155 tsd_hash_add(tsd_hash_table_t *table, uint_t key, pid_t pid, void *value)
156 {
157 tsd_hash_entry_t *entry, *dtor_entry, *pid_entry;
158 tsd_hash_bin_t *bin;
159 ulong_t hash;
160 int rc = 0;
161
162 ASSERT3P(tsd_hash_search(table, key, pid), ==, NULL);
163
164 /* New entry allocate structure, set value, and add to hash */
165 entry = kmem_alloc(sizeof(tsd_hash_entry_t), KM_PUSHPAGE);
166 if (entry == NULL)
167 return (ENOMEM);
168
169 entry->he_key = key;
170 entry->he_pid = pid;
171 entry->he_value = value;
172 INIT_HLIST_NODE(&entry->he_list);
173 INIT_LIST_HEAD(&entry->he_key_list);
174 INIT_LIST_HEAD(&entry->he_pid_list);
175
176 spin_lock(&table->ht_lock);
177
178 /* Destructor entry must exist for all valid keys */
179 dtor_entry = tsd_hash_search(table, entry->he_key, DTOR_PID);
180 ASSERT3P(dtor_entry, !=, NULL);
181 entry->he_dtor = dtor_entry->he_dtor;
182
183 /* Process entry must exist for all valid processes */
184 pid_entry = tsd_hash_search(table, PID_KEY, entry->he_pid);
185 ASSERT3P(pid_entry, !=, NULL);
186
187 hash = hash_long((ulong_t)key * (ulong_t)pid, table->ht_bits);
188 bin = &table->ht_bins[hash];
189 spin_lock(&bin->hb_lock);
190
191 /* Add to the hash, key, and pid lists */
192 hlist_add_head(&entry->he_list, &bin->hb_head);
193 list_add(&entry->he_key_list, &dtor_entry->he_key_list);
194 list_add(&entry->he_pid_list, &pid_entry->he_pid_list);
195
196 spin_unlock(&bin->hb_lock);
197 spin_unlock(&table->ht_lock);
198
199 return (rc);
200 }
201
202 /*
203 * tsd_hash_add_key - adds a destructor entry to the hash table
204 * @table: hash table
205 * @keyp: search key
206 * @dtor: key destructor
207 *
208 * For every unique key there is a single entry in the hash which is used
209 * as anchor. All other thread specific entries for this key are linked
210 * to this anchor via the 'he_key_list' list head. On return they keyp
211 * will be set to the next available key for the hash table.
212 */
213 static int
214 tsd_hash_add_key(tsd_hash_table_t *table, uint_t *keyp, dtor_func_t dtor)
215 {
216 tsd_hash_entry_t *tmp_entry, *entry;
217 tsd_hash_bin_t *bin;
218 ulong_t hash;
219 int keys_checked = 0;
220
221 ASSERT3P(table, !=, NULL);
222
223 /* Allocate entry to be used as a destructor for this key */
224 entry = kmem_alloc(sizeof(tsd_hash_entry_t), KM_PUSHPAGE);
225 if (entry == NULL)
226 return (ENOMEM);
227
228 /* Determine next available key value */
229 spin_lock(&table->ht_lock);
230 do {
231 /* Limited to TSD_KEYS_MAX concurrent unique keys */
232 if (table->ht_key++ > TSD_KEYS_MAX)
233 table->ht_key = 1;
234
235 /* Ensure failure when all TSD_KEYS_MAX keys are in use */
236 if (keys_checked++ >= TSD_KEYS_MAX) {
237 spin_unlock(&table->ht_lock);
238 return (ENOENT);
239 }
240
241 tmp_entry = tsd_hash_search(table, table->ht_key, DTOR_PID);
242 } while (tmp_entry);
243
244 /* Add destructor entry in to hash table */
245 entry->he_key = *keyp = table->ht_key;
246 entry->he_pid = DTOR_PID;
247 entry->he_dtor = dtor;
248 entry->he_value = NULL;
249 INIT_HLIST_NODE(&entry->he_list);
250 INIT_LIST_HEAD(&entry->he_key_list);
251 INIT_LIST_HEAD(&entry->he_pid_list);
252
253 hash = hash_long((ulong_t)*keyp * (ulong_t)DTOR_PID, table->ht_bits);
254 bin = &table->ht_bins[hash];
255 spin_lock(&bin->hb_lock);
256
257 hlist_add_head(&entry->he_list, &bin->hb_head);
258
259 spin_unlock(&bin->hb_lock);
260 spin_unlock(&table->ht_lock);
261
262 return (0);
263 }
264
265 /*
266 * tsd_hash_add_pid - adds a process entry to the hash table
267 * @table: hash table
268 * @pid: search pid
269 *
270 * For every process these is a single entry in the hash which is used
271 * as anchor. All other thread specific entries for this process are
272 * linked to this anchor via the 'he_pid_list' list head.
273 */
274 static int
275 tsd_hash_add_pid(tsd_hash_table_t *table, pid_t pid)
276 {
277 tsd_hash_entry_t *entry;
278 tsd_hash_bin_t *bin;
279 ulong_t hash;
280
281 /* Allocate entry to be used as the process reference */
282 entry = kmem_alloc(sizeof(tsd_hash_entry_t), KM_PUSHPAGE);
283 if (entry == NULL)
284 return (ENOMEM);
285
286 spin_lock(&table->ht_lock);
287 entry->he_key = PID_KEY;
288 entry->he_pid = pid;
289 entry->he_dtor = NULL;
290 entry->he_value = NULL;
291 INIT_HLIST_NODE(&entry->he_list);
292 INIT_LIST_HEAD(&entry->he_key_list);
293 INIT_LIST_HEAD(&entry->he_pid_list);
294
295 hash = hash_long((ulong_t)PID_KEY * (ulong_t)pid, table->ht_bits);
296 bin = &table->ht_bins[hash];
297 spin_lock(&bin->hb_lock);
298
299 hlist_add_head(&entry->he_list, &bin->hb_head);
300
301 spin_unlock(&bin->hb_lock);
302 spin_unlock(&table->ht_lock);
303
304 return (0);
305 }
306
307 /*
308 * tsd_hash_del - delete an entry from hash table, key, and pid lists
309 * @table: hash table
310 * @key: search key
311 * @pid: search pid
312 */
313 static void
314 tsd_hash_del(tsd_hash_table_t *table, tsd_hash_entry_t *entry)
315 {
316 ASSERT(spin_is_locked(&table->ht_lock));
317 hlist_del(&entry->he_list);
318 list_del_init(&entry->he_key_list);
319 list_del_init(&entry->he_pid_list);
320 }
321
322 /*
323 * tsd_hash_table_init - allocate a hash table
324 * @bits: hash table size
325 *
326 * A hash table with 2^bits bins will be created, it may not be resized
327 * after the fact and must be free'd with tsd_hash_table_fini().
328 */
329 static tsd_hash_table_t *
330 tsd_hash_table_init(uint_t bits)
331 {
332 tsd_hash_table_t *table;
333 int hash, size = (1 << bits);
334
335 table = kmem_zalloc(sizeof(tsd_hash_table_t), KM_SLEEP);
336 if (table == NULL)
337 return (NULL);
338
339 table->ht_bins = kmem_zalloc(sizeof(tsd_hash_bin_t) * size,
340 KM_SLEEP | KM_NODEBUG);
341 if (table->ht_bins == NULL) {
342 kmem_free(table, sizeof(tsd_hash_table_t));
343 return (NULL);
344 }
345
346 for (hash = 0; hash < size; hash++) {
347 spin_lock_init(&table->ht_bins[hash].hb_lock);
348 INIT_HLIST_HEAD(&table->ht_bins[hash].hb_head);
349 }
350
351 spin_lock_init(&table->ht_lock);
352 table->ht_bits = bits;
353 table->ht_key = 1;
354
355 return (table);
356 }
357
358 /*
359 * tsd_hash_table_fini - free a hash table
360 * @table: hash table
361 *
362 * Free a hash table allocated by tsd_hash_table_init(). If the hash
363 * table is not empty this function will call the proper destructor for
364 * all remaining entries before freeing the memory used by those entries.
365 */
366 static void
367 tsd_hash_table_fini(tsd_hash_table_t *table)
368 {
369 HLIST_HEAD(work);
370 tsd_hash_bin_t *bin;
371 tsd_hash_entry_t *entry;
372 int size, i;
373
374 ASSERT3P(table, !=, NULL);
375 spin_lock(&table->ht_lock);
376 for (i = 0, size = (1 << table->ht_bits); i < size; i++) {
377 bin = &table->ht_bins[i];
378 spin_lock(&bin->hb_lock);
379 while (!hlist_empty(&bin->hb_head)) {
380 entry = hlist_entry(bin->hb_head.first,
381 tsd_hash_entry_t, he_list);
382 tsd_hash_del(table, entry);
383 hlist_add_head(&entry->he_list, &work);
384 }
385 spin_unlock(&bin->hb_lock);
386 }
387 spin_unlock(&table->ht_lock);
388
389 tsd_hash_dtor(&work);
390 kmem_free(table->ht_bins, sizeof(tsd_hash_bin_t)*(1<<table->ht_bits));
391 kmem_free(table, sizeof(tsd_hash_table_t));
392 }
393
394 /*
395 * tsd_set - set thread specific data
396 * @key: lookup key
397 * @value: value to set
398 *
399 * Caller must prevent racing tsd_create() or tsd_destroy(), protected
400 * from racing tsd_get() or tsd_set() because it is thread specific.
401 * This function has been optimized to be fast for the update case.
402 * When setting the tsd initially it will be slower due to additional
403 * required locking and potential memory allocations.
404 */
405 int
406 tsd_set(uint_t key, void *value)
407 {
408 tsd_hash_table_t *table;
409 tsd_hash_entry_t *entry;
410 pid_t pid;
411 int rc;
412
413 table = tsd_hash_table;
414 pid = curthread->pid;
415 ASSERT3P(table, !=, NULL);
416
417 if ((key == 0) || (key > TSD_KEYS_MAX))
418 return (EINVAL);
419
420 /* Entry already exists in hash table update value */
421 entry = tsd_hash_search(table, key, pid);
422 if (entry) {
423 entry->he_value = value;
424 return (0);
425 }
426
427 /* Add a process entry to the hash if not yet exists */
428 entry = tsd_hash_search(table, PID_KEY, pid);
429 if (entry == NULL) {
430 rc = tsd_hash_add_pid(table, pid);
431 if (rc)
432 return (rc);
433 }
434
435 rc = tsd_hash_add(table, key, pid, value);
436 return (rc);
437 }
438 EXPORT_SYMBOL(tsd_set);
439
440 /*
441 * tsd_get - get thread specific data
442 * @key: lookup key
443 *
444 * Caller must prevent racing tsd_create() or tsd_destroy(). This
445 * implementation is designed to be fast and scalable, it does not
446 * lock the entire table only a single hash bin.
447 */
448 void *
449 tsd_get(uint_t key)
450 {
451 tsd_hash_entry_t *entry;
452
453 ASSERT3P(tsd_hash_table, !=, NULL);
454
455 if ((key == 0) || (key > TSD_KEYS_MAX))
456 return (NULL);
457
458 entry = tsd_hash_search(tsd_hash_table, key, curthread->pid);
459 if (entry == NULL)
460 return (NULL);
461
462 return (entry->he_value);
463 }
464 EXPORT_SYMBOL(tsd_get);
465
466 /*
467 * tsd_create - create thread specific data key
468 * @keyp: lookup key address
469 * @dtor: destructor called during tsd_destroy() or tsd_exit()
470 *
471 * Provided key must be set to 0 or it assumed to be already in use.
472 * The dtor is allowed to be NULL in which case no additional cleanup
473 * for the data is performed during tsd_destroy() or tsd_exit().
474 *
475 * Caller must prevent racing tsd_set() or tsd_get(), this function is
476 * safe from racing tsd_create(), tsd_destroy(), and tsd_exit().
477 */
478 void
479 tsd_create(uint_t *keyp, dtor_func_t dtor)
480 {
481 ASSERT3P(keyp, !=, NULL);
482 if (*keyp)
483 return;
484
485 (void)tsd_hash_add_key(tsd_hash_table, keyp, dtor);
486 }
487 EXPORT_SYMBOL(tsd_create);
488
489 /*
490 * tsd_destroy - destroy thread specific data
491 * @keyp: lookup key address
492 *
493 * Destroys the thread specific data on all threads which use this key.
494 *
495 * Caller must prevent racing tsd_set() or tsd_get(), this function is
496 * safe from racing tsd_create(), tsd_destroy(), and tsd_exit().
497 */
498 void
499 tsd_destroy(uint_t *keyp)
500 {
501 HLIST_HEAD(work);
502 tsd_hash_table_t *table;
503 tsd_hash_entry_t *dtor_entry, *entry;
504 tsd_hash_bin_t *dtor_entry_bin, *entry_bin;
505 ulong_t hash;
506
507 table = tsd_hash_table;
508 ASSERT3P(table, !=, NULL);
509
510 spin_lock(&table->ht_lock);
511 dtor_entry = tsd_hash_search(table, *keyp, DTOR_PID);
512 if (dtor_entry == NULL) {
513 spin_unlock(&table->ht_lock);
514 return;
515 }
516
517 /*
518 * All threads which use this key must be linked off of the
519 * DTOR_PID entry. They are removed from the hash table and
520 * linked in to a private working list to be destroyed.
521 */
522 while (!list_empty(&dtor_entry->he_key_list)) {
523 entry = list_entry(dtor_entry->he_key_list.next,
524 tsd_hash_entry_t, he_key_list);
525 ASSERT3U(dtor_entry->he_key, ==, entry->he_key);
526 ASSERT3P(dtor_entry->he_dtor, ==, entry->he_dtor);
527
528 hash = hash_long((ulong_t)entry->he_key *
529 (ulong_t)entry->he_pid, table->ht_bits);
530 entry_bin = &table->ht_bins[hash];
531
532 spin_lock(&entry_bin->hb_lock);
533 tsd_hash_del(table, entry);
534 hlist_add_head(&entry->he_list, &work);
535 spin_unlock(&entry_bin->hb_lock);
536 }
537
538 hash = hash_long((ulong_t)dtor_entry->he_key *
539 (ulong_t)dtor_entry->he_pid, table->ht_bits);
540 dtor_entry_bin = &table->ht_bins[hash];
541
542 spin_lock(&dtor_entry_bin->hb_lock);
543 tsd_hash_del(table, dtor_entry);
544 hlist_add_head(&dtor_entry->he_list, &work);
545 spin_unlock(&dtor_entry_bin->hb_lock);
546 spin_unlock(&table->ht_lock);
547
548 tsd_hash_dtor(&work);
549 *keyp = 0;
550 }
551 EXPORT_SYMBOL(tsd_destroy);
552
553 /*
554 * tsd_exit - destroys all thread specific data for this thread
555 *
556 * Destroys all the thread specific data for this thread.
557 *
558 * Caller must prevent racing tsd_set() or tsd_get(), this function is
559 * safe from racing tsd_create(), tsd_destroy(), and tsd_exit().
560 */
561 void
562 tsd_exit(void)
563 {
564 HLIST_HEAD(work);
565 tsd_hash_table_t *table;
566 tsd_hash_entry_t *pid_entry, *entry;
567 tsd_hash_bin_t *pid_entry_bin, *entry_bin;
568 ulong_t hash;
569
570 table = tsd_hash_table;
571 ASSERT3P(table, !=, NULL);
572
573 spin_lock(&table->ht_lock);
574 pid_entry = tsd_hash_search(table, PID_KEY, curthread->pid);
575 if (pid_entry == NULL) {
576 spin_unlock(&table->ht_lock);
577 return;
578 }
579
580 /*
581 * All keys associated with this pid must be linked off of the
582 * PID_KEY entry. They are removed from the hash table and
583 * linked in to a private working list to be destroyed.
584 */
585
586 while (!list_empty(&pid_entry->he_pid_list)) {
587 entry = list_entry(pid_entry->he_pid_list.next,
588 tsd_hash_entry_t, he_pid_list);
589 ASSERT3U(pid_entry->he_pid, ==, entry->he_pid);
590
591 hash = hash_long((ulong_t)entry->he_key *
592 (ulong_t)entry->he_pid, table->ht_bits);
593 entry_bin = &table->ht_bins[hash];
594
595 spin_lock(&entry_bin->hb_lock);
596 tsd_hash_del(table, entry);
597 hlist_add_head(&entry->he_list, &work);
598 spin_unlock(&entry_bin->hb_lock);
599 }
600
601 hash = hash_long((ulong_t)pid_entry->he_key *
602 (ulong_t)pid_entry->he_pid, table->ht_bits);
603 pid_entry_bin = &table->ht_bins[hash];
604
605 spin_lock(&pid_entry_bin->hb_lock);
606 tsd_hash_del(table, pid_entry);
607 hlist_add_head(&pid_entry->he_list, &work);
608 spin_unlock(&pid_entry_bin->hb_lock);
609 spin_unlock(&table->ht_lock);
610
611 tsd_hash_dtor(&work);
612 }
613 EXPORT_SYMBOL(tsd_exit);
614
615 int
616 spl_tsd_init(void)
617 {
618 tsd_hash_table = tsd_hash_table_init(TSD_HASH_TABLE_BITS_DEFAULT);
619 if (tsd_hash_table == NULL)
620 return (1);
621
622 return (0);
623 }
624
625 void
626 spl_tsd_fini(void)
627 {
628 tsd_hash_table_fini(tsd_hash_table);
629 tsd_hash_table = NULL;
630 }