4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * libcfs/libcfs/hash.c
34 * Implement a hash class for hash process in lustre system.
36 * Author: YuZhangyong <yzy@clusterfs.com>
38 * 2008-08-15: Brian Behlendorf <behlendorf1@llnl.gov>
39 * - Simplified API and improved documentation
40 * - Added per-hash feature flags:
41 * * CFS_HASH_DEBUG additional validation
42 * * CFS_HASH_REHASH dynamic rehashing
43 * - Added per-hash statistics
44 * - General performance enhancements
46 * 2009-07-31: Liang Zhen <zhen.liang@sun.com>
47 * - move all stuff to libcfs
48 * - don't allow cur_bits != max_bits without setting of CFS_HASH_REHASH
49 * - ignore hs_rwlock if without CFS_HASH_REHASH setting
50 * - buckets are allocated one by one(instead of contiguous memory),
51 * to avoid unnecessary cacheline conflict
53 * 2010-03-01: Liang Zhen <zhen.liang@sun.com>
54 * - "bucket" is a group of hlist_head now, user can specify bucket size
55 * by bkt_bits of cfs_hash_create(), all hlist_heads in a bucket share
56 * one lock for reducing memory overhead.
58 * - support lockless hash, caller will take care of locks:
59 * avoid lock overhead for hash tables that are already protected
60 * by locking in the caller for another reason
62 * - support both spin_lock/rwlock for bucket:
63 * overhead of spinlock contention is lower than read/write
64 * contention of rwlock, so using spinlock to serialize operations on
65 * bucket is more reasonable for those frequently changed hash tables
67 * - support one-single lock mode:
68 * one lock to protect all hash operations to avoid overhead of
69 * multiple locks if hash table is always small
71 * - removed a lot of unnecessary addref & decref on hash element:
72 * addref & decref are atomic operations in many use-cases which
75 * - support non-blocking cfs_hash_add() and cfs_hash_findadd():
76 * some lustre use-cases require these functions to be strictly
77 * non-blocking, we need to schedule required rehash on a different
78 * thread on those cases.
80 * - safer rehash on large hash table
81 * In old implementation, rehash function will exclusively lock the
82 * hash table and finish rehash in one batch, it's dangerous on SMP
83 * system because rehash millions of elements could take long time.
84 * New implemented rehash can release lock and relax CPU in middle
85 * of rehash, it's safe for another thread to search/change on the
86 * hash table even it's in rehasing.
88 * - support two different refcount modes
89 * . hash table has refcount on element
90 * . hash table doesn't change refcount on adding/removing element
92 * - support long name hash table (for param-tree)
94 * - fix a bug for cfs_hash_rehash_key:
95 * in old implementation, cfs_hash_rehash_key could screw up the
96 * hash-table because @key is overwritten without any protection.
97 * Now we need user to define hs_keycpy for those rehash enabled
98 * hash tables, cfs_hash_rehash_key will overwrite hash-key
99 * inside lock by calling hs_keycpy.
101 * - better hash iteration:
102 * Now we support both locked iteration & lockless iteration of hash
103 * table. Also, user can break the iteration by return 1 in callback.
105 #include <linux/seq_file.h>
106 #include <linux/log2.h>
108 #include <libcfs/linux/linux-list.h>
109 #include <libcfs/libcfs.h>
111 #if CFS_HASH_DEBUG_LEVEL >= CFS_HASH_DEBUG_1
112 static unsigned int warn_on_depth = 8;
113 module_param(warn_on_depth, uint, 0644);
114 MODULE_PARM_DESC(warn_on_depth, "warning when hash depth is high.");
117 struct workqueue_struct *cfs_rehash_wq;
120 cfs_hash_nl_lock(union cfs_hash_lock *lock, int exclusive) {}
123 cfs_hash_nl_unlock(union cfs_hash_lock *lock, int exclusive) {}
126 cfs_hash_spin_lock(union cfs_hash_lock *lock, int exclusive)
127 __acquires(&lock->spin)
129 spin_lock(&lock->spin);
133 cfs_hash_spin_unlock(union cfs_hash_lock *lock, int exclusive)
134 __releases(&lock->spin)
136 spin_unlock(&lock->spin);
140 cfs_hash_rw_lock(union cfs_hash_lock *lock, int exclusive)
141 __acquires(&lock->rw)
144 read_lock(&lock->rw);
146 write_lock(&lock->rw);
150 cfs_hash_rw_unlock(union cfs_hash_lock *lock, int exclusive)
151 __releases(&lock->rw)
154 read_unlock(&lock->rw);
156 write_unlock(&lock->rw);
160 static struct cfs_hash_lock_ops cfs_hash_nl_lops = {
161 .hs_lock = cfs_hash_nl_lock,
162 .hs_unlock = cfs_hash_nl_unlock,
163 .hs_bkt_lock = cfs_hash_nl_lock,
164 .hs_bkt_unlock = cfs_hash_nl_unlock,
167 /** no bucket lock, one spinlock to protect everything */
168 static struct cfs_hash_lock_ops cfs_hash_nbl_lops = {
169 .hs_lock = cfs_hash_spin_lock,
170 .hs_unlock = cfs_hash_spin_unlock,
171 .hs_bkt_lock = cfs_hash_nl_lock,
172 .hs_bkt_unlock = cfs_hash_nl_unlock,
175 /** spin bucket lock, rehash is enabled */
176 static struct cfs_hash_lock_ops cfs_hash_bkt_spin_lops = {
177 .hs_lock = cfs_hash_rw_lock,
178 .hs_unlock = cfs_hash_rw_unlock,
179 .hs_bkt_lock = cfs_hash_spin_lock,
180 .hs_bkt_unlock = cfs_hash_spin_unlock,
183 /** rw bucket lock, rehash is enabled */
184 static struct cfs_hash_lock_ops cfs_hash_bkt_rw_lops = {
185 .hs_lock = cfs_hash_rw_lock,
186 .hs_unlock = cfs_hash_rw_unlock,
187 .hs_bkt_lock = cfs_hash_rw_lock,
188 .hs_bkt_unlock = cfs_hash_rw_unlock,
191 /** spin bucket lock, rehash is disabled */
192 static struct cfs_hash_lock_ops cfs_hash_nr_bkt_spin_lops = {
193 .hs_lock = cfs_hash_nl_lock,
194 .hs_unlock = cfs_hash_nl_unlock,
195 .hs_bkt_lock = cfs_hash_spin_lock,
196 .hs_bkt_unlock = cfs_hash_spin_unlock,
199 /** rw bucket lock, rehash is disabled */
200 static struct cfs_hash_lock_ops cfs_hash_nr_bkt_rw_lops = {
201 .hs_lock = cfs_hash_nl_lock,
202 .hs_unlock = cfs_hash_nl_unlock,
203 .hs_bkt_lock = cfs_hash_rw_lock,
204 .hs_bkt_unlock = cfs_hash_rw_unlock,
208 cfs_hash_lock_setup(struct cfs_hash *hs)
210 if (cfs_hash_with_no_lock(hs)) {
211 hs->hs_lops = &cfs_hash_nl_lops;
213 } else if (cfs_hash_with_no_bktlock(hs)) {
214 hs->hs_lops = &cfs_hash_nbl_lops;
215 spin_lock_init(&hs->hs_lock.spin);
217 } else if (cfs_hash_with_rehash(hs)) {
218 rwlock_init(&hs->hs_lock.rw);
220 if (cfs_hash_with_rw_bktlock(hs))
221 hs->hs_lops = &cfs_hash_bkt_rw_lops;
222 else if (cfs_hash_with_spin_bktlock(hs))
223 hs->hs_lops = &cfs_hash_bkt_spin_lops;
227 if (cfs_hash_with_rw_bktlock(hs))
228 hs->hs_lops = &cfs_hash_nr_bkt_rw_lops;
229 else if (cfs_hash_with_spin_bktlock(hs))
230 hs->hs_lops = &cfs_hash_nr_bkt_spin_lops;
237 * Simple hash head without depth tracking
238 * new element is always added to head of hlist
240 struct cfs_hash_head {
241 struct hlist_head hh_head; /**< entries list */
245 cfs_hash_hh_hhead_size(struct cfs_hash *hs)
247 return sizeof(struct cfs_hash_head);
250 static struct hlist_head *
251 cfs_hash_hh_hhead(struct cfs_hash *hs, struct cfs_hash_bd *bd)
253 struct cfs_hash_head *head;
255 head = (struct cfs_hash_head *)&bd->bd_bucket->hsb_head[0];
256 return &head[bd->bd_offset].hh_head;
260 cfs_hash_hh_hnode_add(struct cfs_hash *hs, struct cfs_hash_bd *bd,
261 struct hlist_node *hnode)
263 hlist_add_head(hnode, cfs_hash_hh_hhead(hs, bd));
264 return -1; /* unknown depth */
268 cfs_hash_hh_hnode_del(struct cfs_hash *hs, struct cfs_hash_bd *bd,
269 struct hlist_node *hnode)
271 hlist_del_init(hnode);
272 return -1; /* unknown depth */
276 * Simple hash head with depth tracking
277 * new element is always added to head of hlist
279 struct cfs_hash_head_dep {
280 struct hlist_head hd_head; /**< entries list */
281 unsigned int hd_depth; /**< list length */
285 cfs_hash_hd_hhead_size(struct cfs_hash *hs)
287 return sizeof(struct cfs_hash_head_dep);
290 static struct hlist_head *
291 cfs_hash_hd_hhead(struct cfs_hash *hs, struct cfs_hash_bd *bd)
293 struct cfs_hash_head_dep *head;
295 head = (struct cfs_hash_head_dep *)&bd->bd_bucket->hsb_head[0];
296 return &head[bd->bd_offset].hd_head;
300 cfs_hash_hd_hnode_add(struct cfs_hash *hs, struct cfs_hash_bd *bd,
301 struct hlist_node *hnode)
303 struct cfs_hash_head_dep *hh;
305 hh = container_of(cfs_hash_hd_hhead(hs, bd),
306 struct cfs_hash_head_dep, hd_head);
307 hlist_add_head(hnode, &hh->hd_head);
308 return ++hh->hd_depth;
312 cfs_hash_hd_hnode_del(struct cfs_hash *hs, struct cfs_hash_bd *bd,
313 struct hlist_node *hnode)
315 struct cfs_hash_head_dep *hh;
317 hh = container_of(cfs_hash_hd_hhead(hs, bd),
318 struct cfs_hash_head_dep, hd_head);
319 hlist_del_init(hnode);
320 return --hh->hd_depth;
324 * double links hash head without depth tracking
325 * new element is always added to tail of hlist
327 struct cfs_hash_dhead {
328 struct hlist_head dh_head; /**< entries list */
329 struct hlist_node *dh_tail; /**< the last entry */
333 cfs_hash_dh_hhead_size(struct cfs_hash *hs)
335 return sizeof(struct cfs_hash_dhead);
338 static struct hlist_head *
339 cfs_hash_dh_hhead(struct cfs_hash *hs, struct cfs_hash_bd *bd)
341 struct cfs_hash_dhead *head;
343 head = (struct cfs_hash_dhead *)&bd->bd_bucket->hsb_head[0];
344 return &head[bd->bd_offset].dh_head;
348 cfs_hash_dh_hnode_add(struct cfs_hash *hs, struct cfs_hash_bd *bd,
349 struct hlist_node *hnode)
351 struct cfs_hash_dhead *dh;
353 dh = container_of(cfs_hash_dh_hhead(hs, bd),
354 struct cfs_hash_dhead, dh_head);
355 if (dh->dh_tail != NULL) /* not empty */
356 hlist_add_behind(hnode, dh->dh_tail);
357 else /* empty list */
358 hlist_add_head(hnode, &dh->dh_head);
360 return -1; /* unknown depth */
364 cfs_hash_dh_hnode_del(struct cfs_hash *hs, struct cfs_hash_bd *bd,
365 struct hlist_node *hnd)
367 struct cfs_hash_dhead *dh;
369 dh = container_of(cfs_hash_dh_hhead(hs, bd),
370 struct cfs_hash_dhead, dh_head);
371 if (hnd->next == NULL) { /* it's the tail */
372 dh->dh_tail = (hnd->pprev == &dh->dh_head.first) ? NULL :
373 container_of(hnd->pprev, struct hlist_node, next);
376 return -1; /* unknown depth */
380 * double links hash head with depth tracking
381 * new element is always added to tail of hlist
383 struct cfs_hash_dhead_dep {
384 struct hlist_head dd_head; /**< entries list */
385 struct hlist_node *dd_tail; /**< the last entry */
386 unsigned int dd_depth; /**< list length */
390 cfs_hash_dd_hhead_size(struct cfs_hash *hs)
392 return sizeof(struct cfs_hash_dhead_dep);
395 static struct hlist_head *
396 cfs_hash_dd_hhead(struct cfs_hash *hs, struct cfs_hash_bd *bd)
398 struct cfs_hash_dhead_dep *head;
400 head = (struct cfs_hash_dhead_dep *)&bd->bd_bucket->hsb_head[0];
401 return &head[bd->bd_offset].dd_head;
405 cfs_hash_dd_hnode_add(struct cfs_hash *hs, struct cfs_hash_bd *bd,
406 struct hlist_node *hnode)
408 struct cfs_hash_dhead_dep *dh;
410 dh = container_of(cfs_hash_dd_hhead(hs, bd),
411 struct cfs_hash_dhead_dep, dd_head);
412 if (dh->dd_tail != NULL) /* not empty */
413 hlist_add_behind(hnode, dh->dd_tail);
414 else /* empty list */
415 hlist_add_head(hnode, &dh->dd_head);
417 return ++dh->dd_depth;
421 cfs_hash_dd_hnode_del(struct cfs_hash *hs, struct cfs_hash_bd *bd,
422 struct hlist_node *hnd)
424 struct cfs_hash_dhead_dep *dh;
426 dh = container_of(cfs_hash_dd_hhead(hs, bd),
427 struct cfs_hash_dhead_dep, dd_head);
428 if (hnd->next == NULL) { /* it's the tail */
429 dh->dd_tail = (hnd->pprev == &dh->dd_head.first) ? NULL :
430 container_of(hnd->pprev, struct hlist_node, next);
433 return --dh->dd_depth;
436 static struct cfs_hash_hlist_ops cfs_hash_hh_hops = {
437 .hop_hhead = cfs_hash_hh_hhead,
438 .hop_hhead_size = cfs_hash_hh_hhead_size,
439 .hop_hnode_add = cfs_hash_hh_hnode_add,
440 .hop_hnode_del = cfs_hash_hh_hnode_del,
443 static struct cfs_hash_hlist_ops cfs_hash_hd_hops = {
444 .hop_hhead = cfs_hash_hd_hhead,
445 .hop_hhead_size = cfs_hash_hd_hhead_size,
446 .hop_hnode_add = cfs_hash_hd_hnode_add,
447 .hop_hnode_del = cfs_hash_hd_hnode_del,
450 static struct cfs_hash_hlist_ops cfs_hash_dh_hops = {
451 .hop_hhead = cfs_hash_dh_hhead,
452 .hop_hhead_size = cfs_hash_dh_hhead_size,
453 .hop_hnode_add = cfs_hash_dh_hnode_add,
454 .hop_hnode_del = cfs_hash_dh_hnode_del,
457 static struct cfs_hash_hlist_ops cfs_hash_dd_hops = {
458 .hop_hhead = cfs_hash_dd_hhead,
459 .hop_hhead_size = cfs_hash_dd_hhead_size,
460 .hop_hnode_add = cfs_hash_dd_hnode_add,
461 .hop_hnode_del = cfs_hash_dd_hnode_del,
465 cfs_hash_hlist_setup(struct cfs_hash *hs)
467 if (cfs_hash_with_add_tail(hs)) {
468 hs->hs_hops = cfs_hash_with_depth(hs) ?
469 &cfs_hash_dd_hops : &cfs_hash_dh_hops;
471 hs->hs_hops = cfs_hash_with_depth(hs) ?
472 &cfs_hash_hd_hops : &cfs_hash_hh_hops;
477 cfs_hash_bd_from_key(struct cfs_hash *hs, struct cfs_hash_bucket **bkts,
478 unsigned int bits, const void *key, struct cfs_hash_bd *bd)
480 unsigned int index = cfs_hash_id(hs, key, (1U << bits) - 1);
482 LASSERT(bits == hs->hs_cur_bits || bits == hs->hs_rehash_bits);
484 bd->bd_bucket = bkts[index & ((1U << (bits - hs->hs_bkt_bits)) - 1)];
485 bd->bd_offset = index >> (bits - hs->hs_bkt_bits);
489 cfs_hash_bd_get(struct cfs_hash *hs, const void *key, struct cfs_hash_bd *bd)
491 /* NB: caller should hold hs->hs_rwlock if REHASH is set */
492 if (likely(hs->hs_rehash_buckets == NULL)) {
493 cfs_hash_bd_from_key(hs, hs->hs_buckets,
494 hs->hs_cur_bits, key, bd);
496 LASSERT(hs->hs_rehash_bits != 0);
497 cfs_hash_bd_from_key(hs, hs->hs_rehash_buckets,
498 hs->hs_rehash_bits, key, bd);
501 EXPORT_SYMBOL(cfs_hash_bd_get);
504 cfs_hash_bd_dep_record(struct cfs_hash *hs, struct cfs_hash_bd *bd, int dep_cur)
506 if (likely(dep_cur <= bd->bd_bucket->hsb_depmax))
509 bd->bd_bucket->hsb_depmax = dep_cur;
510 # if CFS_HASH_DEBUG_LEVEL >= CFS_HASH_DEBUG_1
511 if (likely(warn_on_depth == 0 ||
512 max(warn_on_depth, hs->hs_dep_max) >= dep_cur))
515 spin_lock(&hs->hs_dep_lock);
516 hs->hs_dep_max = dep_cur;
517 hs->hs_dep_bkt = bd->bd_bucket->hsb_index;
518 hs->hs_dep_off = bd->bd_offset;
519 hs->hs_dep_bits = hs->hs_cur_bits;
520 spin_unlock(&hs->hs_dep_lock);
522 queue_work(cfs_rehash_wq, &hs->hs_dep_work);
527 cfs_hash_bd_add_locked(struct cfs_hash *hs, struct cfs_hash_bd *bd,
528 struct hlist_node *hnode)
532 rc = hs->hs_hops->hop_hnode_add(hs, bd, hnode);
533 cfs_hash_bd_dep_record(hs, bd, rc);
534 bd->bd_bucket->hsb_version++;
535 if (unlikely(bd->bd_bucket->hsb_version == 0))
536 bd->bd_bucket->hsb_version++;
537 bd->bd_bucket->hsb_count++;
539 if (cfs_hash_with_counter(hs))
540 atomic_inc(&hs->hs_count);
541 if (!cfs_hash_with_no_itemref(hs))
542 cfs_hash_get(hs, hnode);
544 EXPORT_SYMBOL(cfs_hash_bd_add_locked);
547 cfs_hash_bd_del_locked(struct cfs_hash *hs, struct cfs_hash_bd *bd,
548 struct hlist_node *hnode)
550 hs->hs_hops->hop_hnode_del(hs, bd, hnode);
552 LASSERT(bd->bd_bucket->hsb_count > 0);
553 bd->bd_bucket->hsb_count--;
554 bd->bd_bucket->hsb_version++;
555 if (unlikely(bd->bd_bucket->hsb_version == 0))
556 bd->bd_bucket->hsb_version++;
558 if (cfs_hash_with_counter(hs)) {
559 LASSERT(atomic_read(&hs->hs_count) > 0);
560 atomic_dec(&hs->hs_count);
562 if (!cfs_hash_with_no_itemref(hs))
563 cfs_hash_put_locked(hs, hnode);
565 EXPORT_SYMBOL(cfs_hash_bd_del_locked);
568 cfs_hash_bd_move_locked(struct cfs_hash *hs, struct cfs_hash_bd *bd_old,
569 struct cfs_hash_bd *bd_new, struct hlist_node *hnode)
571 struct cfs_hash_bucket *obkt = bd_old->bd_bucket;
572 struct cfs_hash_bucket *nbkt = bd_new->bd_bucket;
575 if (cfs_hash_bd_compare(bd_old, bd_new) == 0)
578 /* use cfs_hash_bd_hnode_add/del, to avoid atomic & refcount ops
579 * in cfs_hash_bd_del/add_locked */
580 hs->hs_hops->hop_hnode_del(hs, bd_old, hnode);
581 rc = hs->hs_hops->hop_hnode_add(hs, bd_new, hnode);
582 cfs_hash_bd_dep_record(hs, bd_new, rc);
584 LASSERT(obkt->hsb_count > 0);
587 if (unlikely(obkt->hsb_version == 0))
591 if (unlikely(nbkt->hsb_version == 0))
596 /** always set, for sanity (avoid ZERO intent) */
597 CFS_HS_LOOKUP_MASK_FIND = 1 << 0,
598 /** return entry with a ref */
599 CFS_HS_LOOKUP_MASK_REF = 1 << 1,
600 /** add entry if not existing */
601 CFS_HS_LOOKUP_MASK_ADD = 1 << 2,
602 /** delete entry, ignore other masks */
603 CFS_HS_LOOKUP_MASK_DEL = 1 << 3,
606 enum cfs_hash_lookup_intent {
607 /** return item w/o refcount */
608 CFS_HS_LOOKUP_IT_PEEK = CFS_HS_LOOKUP_MASK_FIND,
609 /** return item with refcount */
610 CFS_HS_LOOKUP_IT_FIND = (CFS_HS_LOOKUP_MASK_FIND |
611 CFS_HS_LOOKUP_MASK_REF),
612 /** return item w/o refcount if existed, otherwise add */
613 CFS_HS_LOOKUP_IT_ADD = (CFS_HS_LOOKUP_MASK_FIND |
614 CFS_HS_LOOKUP_MASK_ADD),
615 /** return item with refcount if existed, otherwise add */
616 CFS_HS_LOOKUP_IT_FINDADD = (CFS_HS_LOOKUP_IT_FIND |
617 CFS_HS_LOOKUP_MASK_ADD),
618 /** delete if existed */
619 CFS_HS_LOOKUP_IT_FINDDEL = (CFS_HS_LOOKUP_MASK_FIND |
620 CFS_HS_LOOKUP_MASK_DEL)
623 static struct hlist_node *
624 cfs_hash_bd_lookup_intent(struct cfs_hash *hs, struct cfs_hash_bd *bd,
625 const void *key, struct hlist_node *hnode,
626 enum cfs_hash_lookup_intent intent)
629 struct hlist_head *hhead = cfs_hash_bd_hhead(hs, bd);
630 struct hlist_node *ehnode;
631 struct hlist_node *match;
632 int intent_add = (intent & CFS_HS_LOOKUP_MASK_ADD) != 0;
634 /* with this function, we can avoid a lot of useless refcount ops,
635 * which are expensive atomic operations most time. */
636 match = intent_add ? NULL : hnode;
637 hlist_for_each(ehnode, hhead) {
638 if (!cfs_hash_keycmp(hs, key, ehnode))
641 if (match != NULL && match != ehnode) /* can't match */
645 if ((intent & CFS_HS_LOOKUP_MASK_DEL) != 0) {
646 cfs_hash_bd_del_locked(hs, bd, ehnode);
650 /* caller wants refcount? */
651 if ((intent & CFS_HS_LOOKUP_MASK_REF) != 0)
652 cfs_hash_get(hs, ehnode);
659 LASSERT(hnode != NULL);
660 cfs_hash_bd_add_locked(hs, bd, hnode);
665 cfs_hash_bd_lookup_locked(struct cfs_hash *hs, struct cfs_hash_bd *bd,
668 return cfs_hash_bd_lookup_intent(hs, bd, key, NULL,
669 CFS_HS_LOOKUP_IT_FIND);
671 EXPORT_SYMBOL(cfs_hash_bd_lookup_locked);
674 cfs_hash_bd_peek_locked(struct cfs_hash *hs, struct cfs_hash_bd *bd,
677 return cfs_hash_bd_lookup_intent(hs, bd, key, NULL,
678 CFS_HS_LOOKUP_IT_PEEK);
680 EXPORT_SYMBOL(cfs_hash_bd_peek_locked);
683 cfs_hash_multi_bd_lock(struct cfs_hash *hs, struct cfs_hash_bd *bds,
684 unsigned n, int excl)
686 struct cfs_hash_bucket *prev = NULL;
690 * bds must be ascendantly ordered by bd->bd_bucket->hsb_index.
691 * NB: it's possible that several bds point to the same bucket but
692 * have different bd::bd_offset, so need take care of deadlock.
694 cfs_hash_for_each_bd(bds, n, i) {
695 if (prev == bds[i].bd_bucket)
698 LASSERT(prev == NULL ||
699 prev->hsb_index < bds[i].bd_bucket->hsb_index);
700 cfs_hash_bd_lock(hs, &bds[i], excl);
701 prev = bds[i].bd_bucket;
706 cfs_hash_multi_bd_unlock(struct cfs_hash *hs, struct cfs_hash_bd *bds,
707 unsigned n, int excl)
709 struct cfs_hash_bucket *prev = NULL;
712 cfs_hash_for_each_bd(bds, n, i) {
713 if (prev != bds[i].bd_bucket) {
714 cfs_hash_bd_unlock(hs, &bds[i], excl);
715 prev = bds[i].bd_bucket;
720 static struct hlist_node *
721 cfs_hash_multi_bd_lookup_locked(struct cfs_hash *hs, struct cfs_hash_bd *bds,
722 unsigned n, const void *key)
724 struct hlist_node *ehnode;
727 cfs_hash_for_each_bd(bds, n, i) {
728 ehnode = cfs_hash_bd_lookup_intent(hs, &bds[i], key, NULL,
729 CFS_HS_LOOKUP_IT_FIND);
736 static struct hlist_node *
737 cfs_hash_multi_bd_findadd_locked(struct cfs_hash *hs, struct cfs_hash_bd *bds,
738 unsigned n, const void *key,
739 struct hlist_node *hnode, int noref)
741 struct hlist_node *ehnode;
745 LASSERT(hnode != NULL);
746 intent = CFS_HS_LOOKUP_IT_PEEK | (!noref * CFS_HS_LOOKUP_MASK_REF);
748 cfs_hash_for_each_bd(bds, n, i) {
749 ehnode = cfs_hash_bd_lookup_intent(hs, &bds[i], key,
755 if (i == 1) { /* only one bucket */
756 cfs_hash_bd_add_locked(hs, &bds[0], hnode);
758 struct cfs_hash_bd mybd;
760 cfs_hash_bd_get(hs, key, &mybd);
761 cfs_hash_bd_add_locked(hs, &mybd, hnode);
767 static struct hlist_node *
768 cfs_hash_multi_bd_finddel_locked(struct cfs_hash *hs, struct cfs_hash_bd *bds,
769 unsigned n, const void *key,
770 struct hlist_node *hnode)
772 struct hlist_node *ehnode;
775 cfs_hash_for_each_bd(bds, n, i) {
776 ehnode = cfs_hash_bd_lookup_intent(hs, &bds[i], key, hnode,
777 CFS_HS_LOOKUP_IT_FINDDEL);
785 cfs_hash_bd_order(struct cfs_hash_bd *bd1, struct cfs_hash_bd *bd2)
789 if (bd2->bd_bucket == NULL)
792 if (bd1->bd_bucket == NULL) {
794 bd2->bd_bucket = NULL;
798 rc = cfs_hash_bd_compare(bd1, bd2);
800 bd2->bd_bucket = NULL;
803 swap(*bd1, *bd2); /* swab bd1 and bd2 */
808 cfs_hash_dual_bd_get(struct cfs_hash *hs, const void *key,
809 struct cfs_hash_bd *bds)
811 /* NB: caller should hold hs_lock.rw if REHASH is set */
812 cfs_hash_bd_from_key(hs, hs->hs_buckets,
813 hs->hs_cur_bits, key, &bds[0]);
814 if (likely(hs->hs_rehash_buckets == NULL)) {
815 /* no rehash or not rehashing */
816 bds[1].bd_bucket = NULL;
820 LASSERT(hs->hs_rehash_bits != 0);
821 cfs_hash_bd_from_key(hs, hs->hs_rehash_buckets,
822 hs->hs_rehash_bits, key, &bds[1]);
824 cfs_hash_bd_order(&bds[0], &bds[1]);
828 cfs_hash_dual_bd_lock(struct cfs_hash *hs, struct cfs_hash_bd *bds, int excl)
830 cfs_hash_multi_bd_lock(hs, bds, 2, excl);
834 cfs_hash_dual_bd_unlock(struct cfs_hash *hs, struct cfs_hash_bd *bds, int excl)
836 cfs_hash_multi_bd_unlock(hs, bds, 2, excl);
840 cfs_hash_dual_bd_lookup_locked(struct cfs_hash *hs, struct cfs_hash_bd *bds,
843 return cfs_hash_multi_bd_lookup_locked(hs, bds, 2, key);
847 cfs_hash_dual_bd_findadd_locked(struct cfs_hash *hs, struct cfs_hash_bd *bds,
848 const void *key, struct hlist_node *hnode,
851 return cfs_hash_multi_bd_findadd_locked(hs, bds, 2, key,
856 cfs_hash_dual_bd_finddel_locked(struct cfs_hash *hs, struct cfs_hash_bd *bds,
857 const void *key, struct hlist_node *hnode)
859 return cfs_hash_multi_bd_finddel_locked(hs, bds, 2, key, hnode);
863 cfs_hash_buckets_free(struct cfs_hash_bucket **buckets,
864 int bkt_size, int prev_size, int size)
868 for (i = prev_size; i < size; i++) {
869 if (buckets[i] != NULL)
870 LIBCFS_FREE(buckets[i], bkt_size);
873 LIBCFS_FREE(buckets, sizeof(buckets[0]) * size);
877 * Create or grow bucket memory. Return old_buckets if no allocation was
878 * needed, the newly allocated buckets if allocation was needed and
879 * successful, and NULL on error.
881 static struct cfs_hash_bucket **
882 cfs_hash_buckets_realloc(struct cfs_hash *hs, struct cfs_hash_bucket **old_bkts,
883 unsigned int old_size, unsigned int new_size)
885 struct cfs_hash_bucket **new_bkts;
888 LASSERT(old_size == 0 || old_bkts != NULL);
890 if (old_bkts != NULL && old_size == new_size)
893 LIBCFS_ALLOC(new_bkts, sizeof(new_bkts[0]) * new_size);
894 if (new_bkts == NULL)
897 if (old_bkts != NULL) {
898 memcpy(new_bkts, old_bkts,
899 min(old_size, new_size) * sizeof(*old_bkts));
902 for (i = old_size; i < new_size; i++) {
903 struct hlist_head *hhead;
904 struct cfs_hash_bd bd;
906 LIBCFS_ALLOC(new_bkts[i], cfs_hash_bkt_size(hs));
907 if (new_bkts[i] == NULL) {
908 cfs_hash_buckets_free(new_bkts, cfs_hash_bkt_size(hs),
913 new_bkts[i]->hsb_index = i;
914 new_bkts[i]->hsb_version = 1; /* shouldn't be zero */
915 new_bkts[i]->hsb_depmax = -1; /* unknown */
916 bd.bd_bucket = new_bkts[i];
917 cfs_hash_bd_for_each_hlist(hs, &bd, hhead)
918 INIT_HLIST_HEAD(hhead);
920 if (cfs_hash_with_no_lock(hs) ||
921 cfs_hash_with_no_bktlock(hs))
924 if (cfs_hash_with_rw_bktlock(hs))
925 rwlock_init(&new_bkts[i]->hsb_lock.rw);
926 else if (cfs_hash_with_spin_bktlock(hs))
927 spin_lock_init(&new_bkts[i]->hsb_lock.spin);
929 LBUG(); /* invalid use-case */
935 * Initialize new libcfs hash, where:
936 * @name - Descriptive hash name
937 * @cur_bits - Initial hash table size, in bits
938 * @max_bits - Maximum allowed hash table resize, in bits
939 * @ops - Registered hash table operations
940 * @flags - CFS_HASH_REHASH enable synamic hash resizing
941 * - CFS_HASH_SORT enable chained hash sort
943 static void cfs_hash_rehash_worker(struct work_struct *work);
945 #if CFS_HASH_DEBUG_LEVEL >= CFS_HASH_DEBUG_1
946 static void cfs_hash_dep_print(struct work_struct *work)
948 struct cfs_hash *hs = container_of(work, struct cfs_hash, hs_dep_work);
954 spin_lock(&hs->hs_dep_lock);
955 dep = hs->hs_dep_max;
956 bkt = hs->hs_dep_bkt;
957 off = hs->hs_dep_off;
958 bits = hs->hs_dep_bits;
959 spin_unlock(&hs->hs_dep_lock);
961 LCONSOLE_WARN("#### HASH %s (bits: %d): max depth %d at bucket %d/%d\n",
962 hs->hs_name, bits, dep, bkt, off);
963 spin_lock(&hs->hs_dep_lock);
964 hs->hs_dep_bits = 0; /* mark as workitem done */
965 spin_unlock(&hs->hs_dep_lock);
969 static void cfs_hash_depth_wi_init(struct cfs_hash *hs)
971 spin_lock_init(&hs->hs_dep_lock);
972 INIT_WORK(&hs->hs_dep_work, cfs_hash_dep_print);
975 static void cfs_hash_depth_wi_cancel(struct cfs_hash *hs)
977 cancel_work_sync(&hs->hs_dep_work);
980 #else /* CFS_HASH_DEBUG_LEVEL < CFS_HASH_DEBUG_1 */
982 static inline void cfs_hash_depth_wi_init(struct cfs_hash *hs) {}
983 static inline void cfs_hash_depth_wi_cancel(struct cfs_hash *hs) {}
985 #endif /* CFS_HASH_DEBUG_LEVEL >= CFS_HASH_DEBUG_1 */
988 cfs_hash_create(char *name, unsigned cur_bits, unsigned max_bits,
989 unsigned bkt_bits, unsigned extra_bytes,
990 unsigned min_theta, unsigned max_theta,
991 struct cfs_hash_ops *ops, unsigned flags)
998 CLASSERT(CFS_HASH_THETA_BITS < 15);
1000 LASSERT(name != NULL);
1001 LASSERT(ops != NULL);
1002 LASSERT(ops->hs_key);
1003 LASSERT(ops->hs_hash);
1004 LASSERT(ops->hs_object);
1005 LASSERT(ops->hs_keycmp);
1006 LASSERT(ops->hs_get != NULL);
1007 LASSERT(ops->hs_put != NULL || ops->hs_put_locked != NULL);
1009 if ((flags & CFS_HASH_REHASH) != 0)
1010 flags |= CFS_HASH_COUNTER; /* must have counter */
1012 LASSERT(cur_bits > 0);
1013 LASSERT(cur_bits >= bkt_bits);
1014 LASSERT(max_bits >= cur_bits && max_bits < 31);
1015 LASSERT(ergo((flags & CFS_HASH_REHASH) == 0, cur_bits == max_bits));
1016 LASSERT(ergo((flags & CFS_HASH_REHASH) != 0,
1017 (flags & CFS_HASH_NO_LOCK) == 0));
1018 LASSERT(ergo((flags & CFS_HASH_REHASH_KEY) != 0,
1019 ops->hs_keycpy != NULL));
1021 len = (flags & CFS_HASH_BIGNAME) == 0 ?
1022 CFS_HASH_NAME_LEN : CFS_HASH_BIGNAME_LEN;
1023 LIBCFS_ALLOC(hs, offsetof(struct cfs_hash, hs_name[len]));
1027 strlcpy(hs->hs_name, name, len);
1028 hs->hs_flags = flags;
1030 atomic_set(&hs->hs_refcount, 1);
1031 atomic_set(&hs->hs_count, 0);
1033 cfs_hash_lock_setup(hs);
1034 cfs_hash_hlist_setup(hs);
1036 hs->hs_cur_bits = (__u8)cur_bits;
1037 hs->hs_min_bits = (__u8)cur_bits;
1038 hs->hs_max_bits = (__u8)max_bits;
1039 hs->hs_bkt_bits = (__u8)bkt_bits;
1042 hs->hs_extra_bytes = extra_bytes;
1043 hs->hs_rehash_bits = 0;
1044 INIT_WORK(&hs->hs_rehash_work, cfs_hash_rehash_worker);
1045 cfs_hash_depth_wi_init(hs);
1047 if (cfs_hash_with_rehash(hs))
1048 __cfs_hash_set_theta(hs, min_theta, max_theta);
1050 hs->hs_buckets = cfs_hash_buckets_realloc(hs, NULL, 0,
1052 if (hs->hs_buckets != NULL)
1055 LIBCFS_FREE(hs, offsetof(struct cfs_hash, hs_name[len]));
1058 EXPORT_SYMBOL(cfs_hash_create);
1061 * Cleanup libcfs hash @hs.
1064 cfs_hash_destroy(struct cfs_hash *hs)
1066 struct hlist_node *hnode;
1067 struct hlist_node *pos;
1068 struct cfs_hash_bd bd;
1072 LASSERT(hs != NULL);
1073 LASSERT(!cfs_hash_is_exiting(hs) &&
1074 !cfs_hash_is_iterating(hs));
1077 * prohibit further rehashes, don't need any lock because
1078 * I'm the only (last) one can change it.
1081 if (cfs_hash_with_rehash(hs))
1082 cfs_hash_rehash_cancel(hs);
1084 cfs_hash_depth_wi_cancel(hs);
1085 /* rehash should be done/canceled */
1086 LASSERT(hs->hs_buckets != NULL &&
1087 hs->hs_rehash_buckets == NULL);
1089 cfs_hash_for_each_bucket(hs, &bd, i) {
1090 struct hlist_head *hhead;
1092 LASSERT(bd.bd_bucket != NULL);
1093 /* no need to take this lock, just for consistent code */
1094 cfs_hash_bd_lock(hs, &bd, 1);
1096 cfs_hash_bd_for_each_hlist(hs, &bd, hhead) {
1097 hlist_for_each_safe(hnode, pos, hhead) {
1098 LASSERTF(!cfs_hash_with_assert_empty(hs),
1099 "hash %s bucket %u(%u) is not "
1100 " empty: %u items left\n",
1101 hs->hs_name, bd.bd_bucket->hsb_index,
1102 bd.bd_offset, bd.bd_bucket->hsb_count);
1103 /* can't assert key valicate, because we
1104 * can interrupt rehash */
1105 cfs_hash_bd_del_locked(hs, &bd, hnode);
1106 cfs_hash_exit(hs, hnode);
1109 LASSERT(bd.bd_bucket->hsb_count == 0);
1110 cfs_hash_bd_unlock(hs, &bd, 1);
1114 LASSERT(atomic_read(&hs->hs_count) == 0);
1116 cfs_hash_buckets_free(hs->hs_buckets, cfs_hash_bkt_size(hs),
1117 0, CFS_HASH_NBKT(hs));
1118 i = cfs_hash_with_bigname(hs) ?
1119 CFS_HASH_BIGNAME_LEN : CFS_HASH_NAME_LEN;
1120 LIBCFS_FREE(hs, offsetof(struct cfs_hash, hs_name[i]));
1125 struct cfs_hash *cfs_hash_getref(struct cfs_hash *hs)
1127 if (atomic_inc_not_zero(&hs->hs_refcount))
1131 EXPORT_SYMBOL(cfs_hash_getref);
1133 void cfs_hash_putref(struct cfs_hash *hs)
1135 if (atomic_dec_and_test(&hs->hs_refcount))
1136 cfs_hash_destroy(hs);
1138 EXPORT_SYMBOL(cfs_hash_putref);
1141 cfs_hash_rehash_bits(struct cfs_hash *hs)
1143 if (cfs_hash_with_no_lock(hs) ||
1144 !cfs_hash_with_rehash(hs))
1147 if (unlikely(cfs_hash_is_exiting(hs)))
1150 if (unlikely(cfs_hash_is_rehashing(hs)))
1153 if (unlikely(cfs_hash_is_iterating(hs)))
1156 /* XXX: need to handle case with max_theta != 2.0
1157 * and the case with min_theta != 0.5 */
1158 if ((hs->hs_cur_bits < hs->hs_max_bits) &&
1159 (__cfs_hash_theta(hs) > hs->hs_max_theta))
1160 return hs->hs_cur_bits + 1;
1162 if (!cfs_hash_with_shrink(hs))
1165 if ((hs->hs_cur_bits > hs->hs_min_bits) &&
1166 (__cfs_hash_theta(hs) < hs->hs_min_theta))
1167 return hs->hs_cur_bits - 1;
1173 * don't allow inline rehash if:
1174 * - user wants non-blocking change (add/del) on hash table
1175 * - too many elements
1178 cfs_hash_rehash_inline(struct cfs_hash *hs)
1180 return !cfs_hash_with_nblk_change(hs) &&
1181 atomic_read(&hs->hs_count) < CFS_HASH_LOOP_HOG;
1185 * Add item @hnode to libcfs hash @hs using @key. The registered
1186 * ops->hs_get function will be called when the item is added.
1189 cfs_hash_add(struct cfs_hash *hs, const void *key, struct hlist_node *hnode)
1191 struct cfs_hash_bd bd;
1194 LASSERT(hlist_unhashed(hnode));
1196 cfs_hash_lock(hs, 0);
1197 cfs_hash_bd_get_and_lock(hs, key, &bd, 1);
1199 cfs_hash_key_validate(hs, key, hnode);
1200 cfs_hash_bd_add_locked(hs, &bd, hnode);
1202 cfs_hash_bd_unlock(hs, &bd, 1);
1204 bits = cfs_hash_rehash_bits(hs);
1205 cfs_hash_unlock(hs, 0);
1207 cfs_hash_rehash(hs, cfs_hash_rehash_inline(hs));
1209 EXPORT_SYMBOL(cfs_hash_add);
1211 static struct hlist_node *
1212 cfs_hash_find_or_add(struct cfs_hash *hs, const void *key,
1213 struct hlist_node *hnode, int noref)
1215 struct hlist_node *ehnode;
1216 struct cfs_hash_bd bds[2];
1219 LASSERTF(hlist_unhashed(hnode), "hnode = %p\n", hnode);
1221 cfs_hash_lock(hs, 0);
1222 cfs_hash_dual_bd_get_and_lock(hs, key, bds, 1);
1224 cfs_hash_key_validate(hs, key, hnode);
1225 ehnode = cfs_hash_dual_bd_findadd_locked(hs, bds, key,
1227 cfs_hash_dual_bd_unlock(hs, bds, 1);
1229 if (ehnode == hnode) /* new item added */
1230 bits = cfs_hash_rehash_bits(hs);
1231 cfs_hash_unlock(hs, 0);
1233 cfs_hash_rehash(hs, cfs_hash_rehash_inline(hs));
1239 * Add item @hnode to libcfs hash @hs using @key. The registered
1240 * ops->hs_get function will be called if the item was added.
1241 * Returns 0 on success or -EALREADY on key collisions.
1244 cfs_hash_add_unique(struct cfs_hash *hs, const void *key,
1245 struct hlist_node *hnode)
1247 return cfs_hash_find_or_add(hs, key, hnode, 1) != hnode ?
1250 EXPORT_SYMBOL(cfs_hash_add_unique);
1253 * Add item @hnode to libcfs hash @hs using @key. If this @key
1254 * already exists in the hash then ops->hs_get will be called on the
1255 * conflicting entry and that entry will be returned to the caller.
1256 * Otherwise ops->hs_get is called on the item which was added.
1259 cfs_hash_findadd_unique(struct cfs_hash *hs, const void *key,
1260 struct hlist_node *hnode)
1262 hnode = cfs_hash_find_or_add(hs, key, hnode, 0);
1264 return cfs_hash_object(hs, hnode);
1266 EXPORT_SYMBOL(cfs_hash_findadd_unique);
1269 * Delete item @hnode from the libcfs hash @hs using @key. The @key
1270 * is required to ensure the correct hash bucket is locked since there
1271 * is no direct linkage from the item to the bucket. The object
1272 * removed from the hash will be returned and obs->hs_put is called
1273 * on the removed object.
1276 cfs_hash_del(struct cfs_hash *hs, const void *key, struct hlist_node *hnode)
1280 struct cfs_hash_bd bds[2];
1282 cfs_hash_lock(hs, 0);
1283 cfs_hash_dual_bd_get_and_lock(hs, key, bds, 1);
1285 /* NB: do nothing if @hnode is not in hash table */
1286 if (hnode == NULL || !hlist_unhashed(hnode)) {
1287 if (bds[1].bd_bucket == NULL && hnode != NULL) {
1288 cfs_hash_bd_del_locked(hs, &bds[0], hnode);
1290 hnode = cfs_hash_dual_bd_finddel_locked(hs, bds,
1295 if (hnode != NULL) {
1296 obj = cfs_hash_object(hs, hnode);
1297 bits = cfs_hash_rehash_bits(hs);
1300 cfs_hash_dual_bd_unlock(hs, bds, 1);
1301 cfs_hash_unlock(hs, 0);
1303 cfs_hash_rehash(hs, cfs_hash_rehash_inline(hs));
1307 EXPORT_SYMBOL(cfs_hash_del);
1310 * Delete item given @key in libcfs hash @hs. The first @key found in
1311 * the hash will be removed, if the key exists multiple times in the hash
1312 * @hs this function must be called once per key. The removed object
1313 * will be returned and ops->hs_put is called on the removed object.
1316 cfs_hash_del_key(struct cfs_hash *hs, const void *key)
1318 return cfs_hash_del(hs, key, NULL);
1320 EXPORT_SYMBOL(cfs_hash_del_key);
1323 * Lookup an item using @key in the libcfs hash @hs and return it.
1324 * If the @key is found in the hash hs->hs_get() is called and the
1325 * matching objects is returned. It is the callers responsibility
1326 * to call the counterpart ops->hs_put using the cfs_hash_put() macro
1327 * when when finished with the object. If the @key was not found
1328 * in the hash @hs NULL is returned.
1331 cfs_hash_lookup(struct cfs_hash *hs, const void *key)
1334 struct hlist_node *hnode;
1335 struct cfs_hash_bd bds[2];
1337 cfs_hash_lock(hs, 0);
1338 cfs_hash_dual_bd_get_and_lock(hs, key, bds, 0);
1340 hnode = cfs_hash_dual_bd_lookup_locked(hs, bds, key);
1342 obj = cfs_hash_object(hs, hnode);
1344 cfs_hash_dual_bd_unlock(hs, bds, 0);
1345 cfs_hash_unlock(hs, 0);
1349 EXPORT_SYMBOL(cfs_hash_lookup);
1352 cfs_hash_for_each_enter(struct cfs_hash *hs)
1354 LASSERT(!cfs_hash_is_exiting(hs));
1356 if (!cfs_hash_with_rehash(hs))
1359 * NB: it's race on cfs_has_t::hs_iterating, but doesn't matter
1360 * because it's just an unreliable signal to rehash-thread,
1361 * rehash-thread will try to finish rehash ASAP when seeing this.
1363 hs->hs_iterating = 1;
1365 cfs_hash_lock(hs, 1);
1367 cfs_hash_unlock(hs, 1);
1369 /* NB: iteration is mostly called by service thread,
1370 * we tend to cancel pending rehash-request, instead of
1371 * blocking service thread, we will relaunch rehash request
1374 if (cfs_hash_is_rehashing(hs))
1375 cfs_hash_rehash_cancel(hs);
1379 cfs_hash_for_each_exit(struct cfs_hash *hs)
1384 if (!cfs_hash_with_rehash(hs))
1386 cfs_hash_lock(hs, 1);
1387 remained = --hs->hs_iterators;
1388 bits = cfs_hash_rehash_bits(hs);
1389 cfs_hash_unlock(hs, 1);
1390 /* NB: it's race on cfs_has_t::hs_iterating, see above */
1392 hs->hs_iterating = 0;
1394 cfs_hash_rehash(hs, atomic_read(&hs->hs_count) <
1400 * For each item in the libcfs hash @hs call the passed callback @func
1401 * and pass to it as an argument each hash item and the private @data.
1403 * a) the function may sleep!
1404 * b) during the callback:
1405 * . the bucket lock is held so the callback must never sleep.
1406 * . if @removal_safe is true, use can remove current item by
1407 * cfs_hash_bd_del_locked
1410 cfs_hash_for_each_tight(struct cfs_hash *hs, cfs_hash_for_each_cb_t func,
1411 void *data, int remove_safe)
1413 struct hlist_node *hnode;
1414 struct hlist_node *pos;
1415 struct cfs_hash_bd bd;
1417 int excl = !!remove_safe;
1422 cfs_hash_for_each_enter(hs);
1424 cfs_hash_lock(hs, 0);
1425 LASSERT(!cfs_hash_is_rehashing(hs));
1427 cfs_hash_for_each_bucket(hs, &bd, i) {
1428 struct hlist_head *hhead;
1430 cfs_hash_bd_lock(hs, &bd, excl);
1431 if (func == NULL) { /* only glimpse size */
1432 count += bd.bd_bucket->hsb_count;
1433 cfs_hash_bd_unlock(hs, &bd, excl);
1437 cfs_hash_bd_for_each_hlist(hs, &bd, hhead) {
1438 hlist_for_each_safe(hnode, pos, hhead) {
1439 cfs_hash_bucket_validate(hs, &bd, hnode);
1442 if (func(hs, &bd, hnode, data)) {
1443 cfs_hash_bd_unlock(hs, &bd, excl);
1448 cfs_hash_bd_unlock(hs, &bd, excl);
1449 if (loop < CFS_HASH_LOOP_HOG)
1452 cfs_hash_unlock(hs, 0);
1454 cfs_hash_lock(hs, 0);
1457 cfs_hash_unlock(hs, 0);
1459 cfs_hash_for_each_exit(hs);
1463 struct cfs_hash_cond_arg {
1464 cfs_hash_cond_opt_cb_t func;
1469 cfs_hash_cond_del_locked(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1470 struct hlist_node *hnode, void *data)
1472 struct cfs_hash_cond_arg *cond = data;
1474 if (cond->func(cfs_hash_object(hs, hnode), cond->arg))
1475 cfs_hash_bd_del_locked(hs, bd, hnode);
1480 * Delete item from the libcfs hash @hs when @func return true.
1481 * The write lock being hold during loop for each bucket to avoid
1482 * any object be reference.
1485 cfs_hash_cond_del(struct cfs_hash *hs, cfs_hash_cond_opt_cb_t func, void *data)
1487 struct cfs_hash_cond_arg arg = {
1492 cfs_hash_for_each_tight(hs, cfs_hash_cond_del_locked, &arg, 1);
1494 EXPORT_SYMBOL(cfs_hash_cond_del);
1497 cfs_hash_for_each(struct cfs_hash *hs,
1498 cfs_hash_for_each_cb_t func, void *data)
1500 cfs_hash_for_each_tight(hs, func, data, 0);
1502 EXPORT_SYMBOL(cfs_hash_for_each);
1505 cfs_hash_for_each_safe(struct cfs_hash *hs,
1506 cfs_hash_for_each_cb_t func, void *data)
1508 cfs_hash_for_each_tight(hs, func, data, 1);
1510 EXPORT_SYMBOL(cfs_hash_for_each_safe);
1513 cfs_hash_peek(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1514 struct hlist_node *hnode, void *data)
1517 return 1; /* return 1 to break the loop */
1521 cfs_hash_is_empty(struct cfs_hash *hs)
1525 cfs_hash_for_each_tight(hs, cfs_hash_peek, &empty, 0);
1528 EXPORT_SYMBOL(cfs_hash_is_empty);
1531 cfs_hash_size_get(struct cfs_hash *hs)
1533 return cfs_hash_with_counter(hs) ?
1534 atomic_read(&hs->hs_count) :
1535 cfs_hash_for_each_tight(hs, NULL, NULL, 0);
1537 EXPORT_SYMBOL(cfs_hash_size_get);
1540 * cfs_hash_for_each_relax:
1541 * Iterate the hash table and call @func on each item without
1542 * any lock. This function can't guarantee to finish iteration
1543 * if these features are enabled:
1545 * a. if rehash_key is enabled, an item can be moved from
1546 * one bucket to another bucket
1547 * b. user can remove non-zero-ref item from hash-table,
1548 * so the item can be removed from hash-table, even worse,
1549 * it's possible that user changed key and insert to another
1551 * there's no way for us to finish iteration correctly on previous
1552 * two cases, so iteration has to be stopped on change.
1555 cfs_hash_for_each_relax(struct cfs_hash *hs, cfs_hash_for_each_cb_t func,
1556 void *data, int start)
1558 struct hlist_node *hnode;
1559 struct hlist_node *next = NULL;
1560 struct cfs_hash_bd bd;
1569 stop_on_change = cfs_hash_with_rehash_key(hs) ||
1570 !cfs_hash_with_no_itemref(hs);
1571 has_put_locked = hs->hs_ops->hs_put_locked != NULL;
1572 cfs_hash_lock(hs, 0);
1574 LASSERT(!cfs_hash_is_rehashing(hs));
1576 cfs_hash_for_each_bucket(hs, &bd, i) {
1577 struct hlist_head *hhead;
1581 else if (end > 0 && i >= end)
1584 cfs_hash_bd_lock(hs, &bd, 0);
1585 version = cfs_hash_bd_version_get(&bd);
1587 cfs_hash_bd_for_each_hlist(hs, &bd, hhead) {
1588 hnode = hhead->first;
1591 cfs_hash_get(hs, hnode);
1592 for (; hnode != NULL; hnode = next) {
1593 cfs_hash_bucket_validate(hs, &bd, hnode);
1596 cfs_hash_get(hs, next);
1597 cfs_hash_bd_unlock(hs, &bd, 0);
1598 cfs_hash_unlock(hs, 0);
1600 rc = func(hs, &bd, hnode, data);
1601 if (stop_on_change || !has_put_locked)
1602 cfs_hash_put(hs, hnode);
1607 cfs_hash_lock(hs, 0);
1608 cfs_hash_bd_lock(hs, &bd, 0);
1609 if (stop_on_change) {
1611 cfs_hash_bd_version_get(&bd))
1613 } else if (has_put_locked) {
1614 cfs_hash_put_locked(hs, hnode);
1616 if (rc) /* callback wants to break iteration */
1620 if (has_put_locked) {
1621 cfs_hash_put_locked(hs, next);
1625 } else if (rc != 0) {
1629 cfs_hash_bd_unlock(hs, &bd, 0);
1630 if (next != NULL && !has_put_locked) {
1631 cfs_hash_put(hs, next);
1634 if (rc) /* callback wants to break iteration */
1638 if (start > 0 && rc == 0) {
1644 cfs_hash_unlock(hs, 0);
1649 cfs_hash_for_each_nolock(struct cfs_hash *hs,
1650 cfs_hash_for_each_cb_t func, void *data, int start)
1654 if (cfs_hash_with_no_lock(hs) ||
1655 cfs_hash_with_rehash_key(hs) ||
1656 !cfs_hash_with_no_itemref(hs))
1657 RETURN(-EOPNOTSUPP);
1659 if (hs->hs_ops->hs_get == NULL ||
1660 (hs->hs_ops->hs_put == NULL &&
1661 hs->hs_ops->hs_put_locked == NULL))
1662 RETURN(-EOPNOTSUPP);
1664 cfs_hash_for_each_enter(hs);
1665 cfs_hash_for_each_relax(hs, func, data, start);
1666 cfs_hash_for_each_exit(hs);
1670 EXPORT_SYMBOL(cfs_hash_for_each_nolock);
1673 * For each hash bucket in the libcfs hash @hs call the passed callback
1674 * @func until all the hash buckets are empty. The passed callback @func
1675 * or the previously registered callback hs->hs_put must remove the item
1676 * from the hash. You may either use the cfs_hash_del() or hlist_del()
1677 * functions. No rwlocks will be held during the callback @func it is
1678 * safe to sleep if needed. This function will not terminate until the
1679 * hash is empty. Note it is still possible to concurrently add new
1680 * items in to the hash. It is the callers responsibility to ensure
1681 * the required locking is in place to prevent concurrent insertions.
1684 cfs_hash_for_each_empty(struct cfs_hash *hs,
1685 cfs_hash_for_each_cb_t func, void *data)
1690 if (cfs_hash_with_no_lock(hs))
1693 if (hs->hs_ops->hs_get == NULL ||
1694 (hs->hs_ops->hs_put == NULL &&
1695 hs->hs_ops->hs_put_locked == NULL))
1698 cfs_hash_for_each_enter(hs);
1699 while (cfs_hash_for_each_relax(hs, func, data, 0)) {
1700 CDEBUG(D_INFO, "Try to empty hash: %s, loop: %u\n",
1703 cfs_hash_for_each_exit(hs);
1706 EXPORT_SYMBOL(cfs_hash_for_each_empty);
1709 cfs_hash_hlist_for_each(struct cfs_hash *hs, unsigned hindex,
1710 cfs_hash_for_each_cb_t func, void *data)
1712 struct hlist_head *hhead;
1713 struct hlist_node *hnode;
1714 struct cfs_hash_bd bd;
1716 cfs_hash_for_each_enter(hs);
1717 cfs_hash_lock(hs, 0);
1718 if (hindex >= CFS_HASH_NHLIST(hs))
1721 cfs_hash_bd_index_set(hs, hindex, &bd);
1723 cfs_hash_bd_lock(hs, &bd, 0);
1724 hhead = cfs_hash_bd_hhead(hs, &bd);
1725 hlist_for_each(hnode, hhead) {
1726 if (func(hs, &bd, hnode, data))
1729 cfs_hash_bd_unlock(hs, &bd, 0);
1731 cfs_hash_unlock(hs, 0);
1732 cfs_hash_for_each_exit(hs);
1735 EXPORT_SYMBOL(cfs_hash_hlist_for_each);
1738 * For each item in the libcfs hash @hs which matches the @key call
1739 * the passed callback @func and pass to it as an argument each hash
1740 * item and the private @data. During the callback the bucket lock
1741 * is held so the callback must never sleep.
1744 cfs_hash_for_each_key(struct cfs_hash *hs, const void *key,
1745 cfs_hash_for_each_cb_t func, void *data)
1747 struct hlist_node *hnode;
1748 struct cfs_hash_bd bds[2];
1751 cfs_hash_lock(hs, 0);
1753 cfs_hash_dual_bd_get_and_lock(hs, key, bds, 0);
1755 cfs_hash_for_each_bd(bds, 2, i) {
1756 struct hlist_head *hlist = cfs_hash_bd_hhead(hs, &bds[i]);
1758 hlist_for_each(hnode, hlist) {
1759 cfs_hash_bucket_validate(hs, &bds[i], hnode);
1761 if (cfs_hash_keycmp(hs, key, hnode)) {
1762 if (func(hs, &bds[i], hnode, data))
1768 cfs_hash_dual_bd_unlock(hs, bds, 0);
1769 cfs_hash_unlock(hs, 0);
1771 EXPORT_SYMBOL(cfs_hash_for_each_key);
1774 * Rehash the libcfs hash @hs to the given @bits. This can be used
1775 * to grow the hash size when excessive chaining is detected, or to
1776 * shrink the hash when it is larger than needed. When the CFS_HASH_REHASH
1777 * flag is set in @hs the libcfs hash may be dynamically rehashed
1778 * during addition or removal if the hash's theta value exceeds
1779 * either the hs->hs_min_theta or hs->max_theta values. By default
1780 * these values are tuned to keep the chained hash depth small, and
1781 * this approach assumes a reasonably uniform hashing function. The
1782 * theta thresholds for @hs are tunable via cfs_hash_set_theta().
1785 cfs_hash_rehash_cancel(struct cfs_hash *hs)
1787 LASSERT(cfs_hash_with_rehash(hs));
1788 cancel_work_sync(&hs->hs_rehash_work);
1792 cfs_hash_rehash(struct cfs_hash *hs, int do_rehash)
1796 LASSERT(cfs_hash_with_rehash(hs) && !cfs_hash_with_no_lock(hs));
1798 cfs_hash_lock(hs, 1);
1800 rc = cfs_hash_rehash_bits(hs);
1802 cfs_hash_unlock(hs, 1);
1806 hs->hs_rehash_bits = rc;
1808 /* launch and return */
1809 queue_work(cfs_rehash_wq, &hs->hs_rehash_work);
1810 cfs_hash_unlock(hs, 1);
1814 /* rehash right now */
1815 cfs_hash_unlock(hs, 1);
1817 cfs_hash_rehash_worker(&hs->hs_rehash_work);
1821 cfs_hash_rehash_bd(struct cfs_hash *hs, struct cfs_hash_bd *old)
1823 struct cfs_hash_bd new;
1824 struct hlist_head *hhead;
1825 struct hlist_node *hnode;
1826 struct hlist_node *pos;
1830 /* hold cfs_hash_lock(hs, 1), so don't need any bucket lock */
1831 cfs_hash_bd_for_each_hlist(hs, old, hhead) {
1832 hlist_for_each_safe(hnode, pos, hhead) {
1833 key = cfs_hash_key(hs, hnode);
1834 LASSERT(key != NULL);
1835 /* Validate hnode is in the correct bucket. */
1836 cfs_hash_bucket_validate(hs, old, hnode);
1838 * Delete from old hash bucket; move to new bucket.
1839 * ops->hs_key must be defined.
1841 cfs_hash_bd_from_key(hs, hs->hs_rehash_buckets,
1842 hs->hs_rehash_bits, key, &new);
1843 cfs_hash_bd_move_locked(hs, old, &new, hnode);
1851 cfs_hash_rehash_worker(struct work_struct *work)
1853 struct cfs_hash *hs = container_of(work, struct cfs_hash,
1855 struct cfs_hash_bucket **bkts;
1856 struct cfs_hash_bd bd;
1857 unsigned int old_size;
1858 unsigned int new_size;
1864 LASSERT(hs != NULL && cfs_hash_with_rehash(hs));
1866 cfs_hash_lock(hs, 0);
1867 LASSERT(cfs_hash_is_rehashing(hs));
1869 old_size = CFS_HASH_NBKT(hs);
1870 new_size = CFS_HASH_RH_NBKT(hs);
1872 cfs_hash_unlock(hs, 0);
1875 * don't need hs::hs_rwlock for hs::hs_buckets,
1876 * because nobody can change bkt-table except me.
1878 bkts = cfs_hash_buckets_realloc(hs, hs->hs_buckets,
1879 old_size, new_size);
1880 cfs_hash_lock(hs, 1);
1886 if (bkts == hs->hs_buckets) {
1887 bkts = NULL; /* do nothing */
1891 rc = __cfs_hash_theta(hs);
1892 if ((rc >= hs->hs_min_theta) && (rc <= hs->hs_max_theta)) {
1893 /* free the new allocated bkt-table */
1894 old_size = new_size;
1895 new_size = CFS_HASH_NBKT(hs);
1900 LASSERT(hs->hs_rehash_buckets == NULL);
1901 hs->hs_rehash_buckets = bkts;
1904 cfs_hash_for_each_bucket(hs, &bd, i) {
1905 if (cfs_hash_is_exiting(hs)) {
1907 /* someone wants to destroy the hash, abort now */
1908 if (old_size < new_size) /* OK to free old bkt-table */
1910 /* it's shrinking, need free new bkt-table */
1911 hs->hs_rehash_buckets = NULL;
1912 old_size = new_size;
1913 new_size = CFS_HASH_NBKT(hs);
1917 count += cfs_hash_rehash_bd(hs, &bd);
1918 if (count < CFS_HASH_LOOP_HOG ||
1919 cfs_hash_is_iterating(hs)) { /* need to finish ASAP */
1924 cfs_hash_unlock(hs, 1);
1926 cfs_hash_lock(hs, 1);
1929 hs->hs_rehash_count++;
1931 bkts = hs->hs_buckets;
1932 hs->hs_buckets = hs->hs_rehash_buckets;
1933 hs->hs_rehash_buckets = NULL;
1935 hs->hs_cur_bits = hs->hs_rehash_bits;
1937 hs->hs_rehash_bits = 0;
1938 bsize = cfs_hash_bkt_size(hs);
1939 cfs_hash_unlock(hs, 1);
1940 /* can't refer to @hs anymore because it could be destroyed */
1942 cfs_hash_buckets_free(bkts, bsize, new_size, old_size);
1944 CDEBUG(D_INFO, "early quit of rehashing: %d\n", rc);
1948 * Rehash the object referenced by @hnode in the libcfs hash @hs. The
1949 * @old_key must be provided to locate the objects previous location
1950 * in the hash, and the @new_key will be used to reinsert the object.
1951 * Use this function instead of a cfs_hash_add() + cfs_hash_del()
1952 * combo when it is critical that there is no window in time where the
1953 * object is missing from the hash. When an object is being rehashed
1954 * the registered cfs_hash_get() and cfs_hash_put() functions will
1957 void cfs_hash_rehash_key(struct cfs_hash *hs, const void *old_key,
1958 void *new_key, struct hlist_node *hnode)
1960 struct cfs_hash_bd bds[3];
1961 struct cfs_hash_bd old_bds[2];
1962 struct cfs_hash_bd new_bd;
1964 LASSERT(!hlist_unhashed(hnode));
1966 cfs_hash_lock(hs, 0);
1968 cfs_hash_dual_bd_get(hs, old_key, old_bds);
1969 cfs_hash_bd_get(hs, new_key, &new_bd);
1971 bds[0] = old_bds[0];
1972 bds[1] = old_bds[1];
1975 /* NB: bds[0] and bds[1] are ordered already */
1976 cfs_hash_bd_order(&bds[1], &bds[2]);
1977 cfs_hash_bd_order(&bds[0], &bds[1]);
1979 cfs_hash_multi_bd_lock(hs, bds, 3, 1);
1980 if (likely(old_bds[1].bd_bucket == NULL)) {
1981 cfs_hash_bd_move_locked(hs, &old_bds[0], &new_bd, hnode);
1983 cfs_hash_dual_bd_finddel_locked(hs, old_bds, old_key, hnode);
1984 cfs_hash_bd_add_locked(hs, &new_bd, hnode);
1986 /* overwrite key inside locks, otherwise may screw up with
1987 * other operations, i.e: rehash */
1988 cfs_hash_keycpy(hs, hnode, new_key);
1990 cfs_hash_multi_bd_unlock(hs, bds, 3, 1);
1991 cfs_hash_unlock(hs, 0);
1993 EXPORT_SYMBOL(cfs_hash_rehash_key);
1995 void cfs_hash_debug_header(struct seq_file *m)
1997 seq_printf(m, "%-*s cur min max theta t-min t-max flags rehash count maxdep maxdepb distribution\n",
1998 CFS_HASH_BIGNAME_LEN, "name");
2000 EXPORT_SYMBOL(cfs_hash_debug_header);
2002 static struct cfs_hash_bucket **
2003 cfs_hash_full_bkts(struct cfs_hash *hs)
2005 /* NB: caller should hold hs->hs_rwlock if REHASH is set */
2006 if (hs->hs_rehash_buckets == NULL)
2007 return hs->hs_buckets;
2009 LASSERT(hs->hs_rehash_bits != 0);
2010 return hs->hs_rehash_bits > hs->hs_cur_bits ?
2011 hs->hs_rehash_buckets : hs->hs_buckets;
2015 cfs_hash_full_nbkt(struct cfs_hash *hs)
2017 /* NB: caller should hold hs->hs_rwlock if REHASH is set */
2018 if (hs->hs_rehash_buckets == NULL)
2019 return CFS_HASH_NBKT(hs);
2021 LASSERT(hs->hs_rehash_bits != 0);
2022 return hs->hs_rehash_bits > hs->hs_cur_bits ?
2023 CFS_HASH_RH_NBKT(hs) : CFS_HASH_NBKT(hs);
2026 void cfs_hash_debug_str(struct cfs_hash *hs, struct seq_file *m)
2028 int dist[8] = { 0, };
2035 cfs_hash_lock(hs, 0);
2036 theta = __cfs_hash_theta(hs);
2038 seq_printf(m, "%-*s %5d %5d %5d %d.%03d %d.%03d %d.%03d 0x%02x %6d ",
2039 CFS_HASH_BIGNAME_LEN, hs->hs_name,
2040 1 << hs->hs_cur_bits, 1 << hs->hs_min_bits,
2041 1 << hs->hs_max_bits,
2042 __cfs_hash_theta_int(theta), __cfs_hash_theta_frac(theta),
2043 __cfs_hash_theta_int(hs->hs_min_theta),
2044 __cfs_hash_theta_frac(hs->hs_min_theta),
2045 __cfs_hash_theta_int(hs->hs_max_theta),
2046 __cfs_hash_theta_frac(hs->hs_max_theta),
2047 hs->hs_flags, hs->hs_rehash_count);
2050 * The distribution is a summary of the chained hash depth in
2051 * each of the libcfs hash buckets. Each buckets hsb_count is
2052 * divided by the hash theta value and used to generate a
2053 * histogram of the hash distribution. A uniform hash will
2054 * result in all hash buckets being close to the average thus
2055 * only the first few entries in the histogram will be non-zero.
2056 * If you hash function results in a non-uniform hash the will
2057 * be observable by outlier bucks in the distribution histogram.
2059 * Uniform hash distribution: 128/128/0/0/0/0/0/0
2060 * Non-Uniform hash distribution: 128/125/0/0/0/0/2/1
2062 for (i = 0; i < cfs_hash_full_nbkt(hs); i++) {
2063 struct cfs_hash_bd bd;
2065 bd.bd_bucket = cfs_hash_full_bkts(hs)[i];
2066 cfs_hash_bd_lock(hs, &bd, 0);
2067 if (maxdep < bd.bd_bucket->hsb_depmax) {
2068 maxdep = bd.bd_bucket->hsb_depmax;
2069 maxdepb = ffz(~maxdep);
2071 total += bd.bd_bucket->hsb_count;
2072 dist[min(fls(bd.bd_bucket->hsb_count / max(theta, 1)), 7)]++;
2073 cfs_hash_bd_unlock(hs, &bd, 0);
2076 seq_printf(m, "%7d %7d %7d ", total, maxdep, maxdepb);
2077 for (i = 0; i < 8; i++)
2078 seq_printf(m, "%d%c", dist[i], (i == 7) ? '\n' : '/');
2080 cfs_hash_unlock(hs, 0);
2082 EXPORT_SYMBOL(cfs_hash_debug_str);