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