1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * Resizable, Scalable, Concurrent Hash Table 4 * 5 * Copyright (c) 2015-2016 Herbert Xu <[email protected]> 6 * Copyright (c) 2014-2015 Thomas Graf <[email protected]> 7 * Copyright (c) 2008-2014 Patrick McHardy <[email protected]> 8 * 9 * Code partially derived from nft_hash 10 * Rewritten with rehash code from br_multicast plus single list 11 * pointer as suggested by Josh Triplett 12 * 13 * This program is free software; you can redistribute it and/or modify 14 * it under the terms of the GNU General Public License version 2 as 15 * published by the Free Software Foundation. 16 */ 17 18 #ifndef _LINUX_RHASHTABLE_H 19 #define _LINUX_RHASHTABLE_H 20 21 #include <linux/err.h> 22 #include <linux/errno.h> 23 #include <linux/jhash.h> 24 #include <linux/list_nulls.h> 25 #include <linux/workqueue.h> 26 #include <linux/rculist.h> 27 28 #include <linux/rhashtable-types.h> 29 /* 30 * The end of the chain is marked with a special nulls marks which has 31 * the following format: 32 * 33 * +-------+-----------------------------------------------------+-+ 34 * | Base | Hash |1| 35 * +-------+-----------------------------------------------------+-+ 36 * 37 * Base (4 bits) : Reserved to distinguish between multiple tables. 38 * Specified via &struct rhashtable_params.nulls_base. 39 * Hash (27 bits): Full hash (unmasked) of first element added to bucket 40 * 1 (1 bit) : Nulls marker (always set) 41 * 42 * The remaining bits of the next pointer remain unused for now. 43 */ 44 #define RHT_BASE_BITS 4 45 #define RHT_HASH_BITS 27 46 #define RHT_BASE_SHIFT RHT_HASH_BITS 47 48 /* Base bits plus 1 bit for nulls marker */ 49 #define RHT_HASH_RESERVED_SPACE (RHT_BASE_BITS + 1) 50 51 /* Maximum chain length before rehash 52 * 53 * The maximum (not average) chain length grows with the size of the hash 54 * table, at a rate of (log N)/(log log N). 55 * 56 * The value of 16 is selected so that even if the hash table grew to 57 * 2^32 you would not expect the maximum chain length to exceed it 58 * unless we are under attack (or extremely unlucky). 59 * 60 * As this limit is only to detect attacks, we don't need to set it to a 61 * lower value as you'd need the chain length to vastly exceed 16 to have 62 * any real effect on the system. 63 */ 64 #define RHT_ELASTICITY 16u 65 66 /** 67 * struct bucket_table - Table of hash buckets 68 * @size: Number of hash buckets 69 * @nest: Number of bits of first-level nested table. 70 * @rehash: Current bucket being rehashed 71 * @hash_rnd: Random seed to fold into hash 72 * @locks_mask: Mask to apply before accessing locks[] 73 * @locks: Array of spinlocks protecting individual buckets 74 * @walkers: List of active walkers 75 * @rcu: RCU structure for freeing the table 76 * @future_tbl: Table under construction during rehashing 77 * @ntbl: Nested table used when out of memory. 78 * @buckets: size * hash buckets 79 */ 80 struct bucket_table { 81 unsigned int size; 82 unsigned int nest; 83 unsigned int rehash; 84 u32 hash_rnd; 85 unsigned int locks_mask; 86 spinlock_t *locks; 87 struct list_head walkers; 88 struct rcu_head rcu; 89 90 struct bucket_table __rcu *future_tbl; 91 92 struct rhash_head __rcu *buckets[] ____cacheline_aligned_in_smp; 93 }; 94 95 static inline unsigned long rht_marker(const struct rhashtable *ht, u32 hash) 96 { 97 return NULLS_MARKER(ht->p.nulls_base + hash); 98 } 99 100 #define INIT_RHT_NULLS_HEAD(ptr, ht, hash) \ 101 ((ptr) = (typeof(ptr)) rht_marker(ht, hash)) 102 103 static inline bool rht_is_a_nulls(const struct rhash_head *ptr) 104 { 105 return ((unsigned long) ptr & 1); 106 } 107 108 static inline unsigned long rht_get_nulls_value(const struct rhash_head *ptr) 109 { 110 return ((unsigned long) ptr) >> 1; 111 } 112 113 static inline void *rht_obj(const struct rhashtable *ht, 114 const struct rhash_head *he) 115 { 116 return (char *)he - ht->p.head_offset; 117 } 118 119 static inline unsigned int rht_bucket_index(const struct bucket_table *tbl, 120 unsigned int hash) 121 { 122 return (hash >> RHT_HASH_RESERVED_SPACE) & (tbl->size - 1); 123 } 124 125 static inline unsigned int rht_key_get_hash(struct rhashtable *ht, 126 const void *key, const struct rhashtable_params params, 127 unsigned int hash_rnd) 128 { 129 unsigned int hash; 130 131 /* params must be equal to ht->p if it isn't constant. */ 132 if (!__builtin_constant_p(params.key_len)) 133 hash = ht->p.hashfn(key, ht->key_len, hash_rnd); 134 else if (params.key_len) { 135 unsigned int key_len = params.key_len; 136 137 if (params.hashfn) 138 hash = params.hashfn(key, key_len, hash_rnd); 139 else if (key_len & (sizeof(u32) - 1)) 140 hash = jhash(key, key_len, hash_rnd); 141 else 142 hash = jhash2(key, key_len / sizeof(u32), hash_rnd); 143 } else { 144 unsigned int key_len = ht->p.key_len; 145 146 if (params.hashfn) 147 hash = params.hashfn(key, key_len, hash_rnd); 148 else 149 hash = jhash(key, key_len, hash_rnd); 150 } 151 152 return hash; 153 } 154 155 static inline unsigned int rht_key_hashfn( 156 struct rhashtable *ht, const struct bucket_table *tbl, 157 const void *key, const struct rhashtable_params params) 158 { 159 unsigned int hash = rht_key_get_hash(ht, key, params, tbl->hash_rnd); 160 161 return rht_bucket_index(tbl, hash); 162 } 163 164 static inline unsigned int rht_head_hashfn( 165 struct rhashtable *ht, const struct bucket_table *tbl, 166 const struct rhash_head *he, const struct rhashtable_params params) 167 { 168 const char *ptr = rht_obj(ht, he); 169 170 return likely(params.obj_hashfn) ? 171 rht_bucket_index(tbl, params.obj_hashfn(ptr, params.key_len ?: 172 ht->p.key_len, 173 tbl->hash_rnd)) : 174 rht_key_hashfn(ht, tbl, ptr + params.key_offset, params); 175 } 176 177 /** 178 * rht_grow_above_75 - returns true if nelems > 0.75 * table-size 179 * @ht: hash table 180 * @tbl: current table 181 */ 182 static inline bool rht_grow_above_75(const struct rhashtable *ht, 183 const struct bucket_table *tbl) 184 { 185 /* Expand table when exceeding 75% load */ 186 return atomic_read(&ht->nelems) > (tbl->size / 4 * 3) && 187 (!ht->p.max_size || tbl->size < ht->p.max_size); 188 } 189 190 /** 191 * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size 192 * @ht: hash table 193 * @tbl: current table 194 */ 195 static inline bool rht_shrink_below_30(const struct rhashtable *ht, 196 const struct bucket_table *tbl) 197 { 198 /* Shrink table beneath 30% load */ 199 return atomic_read(&ht->nelems) < (tbl->size * 3 / 10) && 200 tbl->size > ht->p.min_size; 201 } 202 203 /** 204 * rht_grow_above_100 - returns true if nelems > table-size 205 * @ht: hash table 206 * @tbl: current table 207 */ 208 static inline bool rht_grow_above_100(const struct rhashtable *ht, 209 const struct bucket_table *tbl) 210 { 211 return atomic_read(&ht->nelems) > tbl->size && 212 (!ht->p.max_size || tbl->size < ht->p.max_size); 213 } 214 215 /** 216 * rht_grow_above_max - returns true if table is above maximum 217 * @ht: hash table 218 * @tbl: current table 219 */ 220 static inline bool rht_grow_above_max(const struct rhashtable *ht, 221 const struct bucket_table *tbl) 222 { 223 return atomic_read(&ht->nelems) >= ht->max_elems; 224 } 225 226 /* The bucket lock is selected based on the hash and protects mutations 227 * on a group of hash buckets. 228 * 229 * A maximum of tbl->size/2 bucket locks is allocated. This ensures that 230 * a single lock always covers both buckets which may both contains 231 * entries which link to the same bucket of the old table during resizing. 232 * This allows to simplify the locking as locking the bucket in both 233 * tables during resize always guarantee protection. 234 * 235 * IMPORTANT: When holding the bucket lock of both the old and new table 236 * during expansions and shrinking, the old bucket lock must always be 237 * acquired first. 238 */ 239 static inline spinlock_t *rht_bucket_lock(const struct bucket_table *tbl, 240 unsigned int hash) 241 { 242 return &tbl->locks[hash & tbl->locks_mask]; 243 } 244 245 #ifdef CONFIG_PROVE_LOCKING 246 int lockdep_rht_mutex_is_held(struct rhashtable *ht); 247 int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash); 248 #else 249 static inline int lockdep_rht_mutex_is_held(struct rhashtable *ht) 250 { 251 return 1; 252 } 253 254 static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, 255 u32 hash) 256 { 257 return 1; 258 } 259 #endif /* CONFIG_PROVE_LOCKING */ 260 261 void *rhashtable_insert_slow(struct rhashtable *ht, const void *key, 262 struct rhash_head *obj); 263 264 void rhashtable_walk_enter(struct rhashtable *ht, 265 struct rhashtable_iter *iter); 266 void rhashtable_walk_exit(struct rhashtable_iter *iter); 267 int rhashtable_walk_start_check(struct rhashtable_iter *iter) __acquires(RCU); 268 269 static inline void rhashtable_walk_start(struct rhashtable_iter *iter) 270 { 271 (void)rhashtable_walk_start_check(iter); 272 } 273 274 void *rhashtable_walk_next(struct rhashtable_iter *iter); 275 void *rhashtable_walk_peek(struct rhashtable_iter *iter); 276 void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU); 277 278 void rhashtable_free_and_destroy(struct rhashtable *ht, 279 void (*free_fn)(void *ptr, void *arg), 280 void *arg); 281 void rhashtable_destroy(struct rhashtable *ht); 282 283 struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl, 284 unsigned int hash); 285 struct rhash_head __rcu **rht_bucket_nested_insert(struct rhashtable *ht, 286 struct bucket_table *tbl, 287 unsigned int hash); 288 289 #define rht_dereference(p, ht) \ 290 rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht)) 291 292 #define rht_dereference_rcu(p, ht) \ 293 rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht)) 294 295 #define rht_dereference_bucket(p, tbl, hash) \ 296 rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash)) 297 298 #define rht_dereference_bucket_rcu(p, tbl, hash) \ 299 rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash)) 300 301 #define rht_entry(tpos, pos, member) \ 302 ({ tpos = container_of(pos, typeof(*tpos), member); 1; }) 303 304 static inline struct rhash_head __rcu *const *rht_bucket( 305 const struct bucket_table *tbl, unsigned int hash) 306 { 307 return unlikely(tbl->nest) ? rht_bucket_nested(tbl, hash) : 308 &tbl->buckets[hash]; 309 } 310 311 static inline struct rhash_head __rcu **rht_bucket_var( 312 struct bucket_table *tbl, unsigned int hash) 313 { 314 return unlikely(tbl->nest) ? rht_bucket_nested(tbl, hash) : 315 &tbl->buckets[hash]; 316 } 317 318 static inline struct rhash_head __rcu **rht_bucket_insert( 319 struct rhashtable *ht, struct bucket_table *tbl, unsigned int hash) 320 { 321 return unlikely(tbl->nest) ? rht_bucket_nested_insert(ht, tbl, hash) : 322 &tbl->buckets[hash]; 323 } 324 325 /** 326 * rht_for_each_continue - continue iterating over hash chain 327 * @pos: the &struct rhash_head to use as a loop cursor. 328 * @head: the previous &struct rhash_head to continue from 329 * @tbl: the &struct bucket_table 330 * @hash: the hash value / bucket index 331 */ 332 #define rht_for_each_continue(pos, head, tbl, hash) \ 333 for (pos = rht_dereference_bucket(head, tbl, hash); \ 334 !rht_is_a_nulls(pos); \ 335 pos = rht_dereference_bucket((pos)->next, tbl, hash)) 336 337 /** 338 * rht_for_each - iterate over hash chain 339 * @pos: the &struct rhash_head to use as a loop cursor. 340 * @tbl: the &struct bucket_table 341 * @hash: the hash value / bucket index 342 */ 343 #define rht_for_each(pos, tbl, hash) \ 344 rht_for_each_continue(pos, *rht_bucket(tbl, hash), tbl, hash) 345 346 /** 347 * rht_for_each_entry_continue - continue iterating over hash chain 348 * @tpos: the type * to use as a loop cursor. 349 * @pos: the &struct rhash_head to use as a loop cursor. 350 * @head: the previous &struct rhash_head to continue from 351 * @tbl: the &struct bucket_table 352 * @hash: the hash value / bucket index 353 * @member: name of the &struct rhash_head within the hashable struct. 354 */ 355 #define rht_for_each_entry_continue(tpos, pos, head, tbl, hash, member) \ 356 for (pos = rht_dereference_bucket(head, tbl, hash); \ 357 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \ 358 pos = rht_dereference_bucket((pos)->next, tbl, hash)) 359 360 /** 361 * rht_for_each_entry - iterate over hash chain of given type 362 * @tpos: the type * to use as a loop cursor. 363 * @pos: the &struct rhash_head to use as a loop cursor. 364 * @tbl: the &struct bucket_table 365 * @hash: the hash value / bucket index 366 * @member: name of the &struct rhash_head within the hashable struct. 367 */ 368 #define rht_for_each_entry(tpos, pos, tbl, hash, member) \ 369 rht_for_each_entry_continue(tpos, pos, *rht_bucket(tbl, hash), \ 370 tbl, hash, member) 371 372 /** 373 * rht_for_each_entry_safe - safely iterate over hash chain of given type 374 * @tpos: the type * to use as a loop cursor. 375 * @pos: the &struct rhash_head to use as a loop cursor. 376 * @next: the &struct rhash_head to use as next in loop cursor. 377 * @tbl: the &struct bucket_table 378 * @hash: the hash value / bucket index 379 * @member: name of the &struct rhash_head within the hashable struct. 380 * 381 * This hash chain list-traversal primitive allows for the looped code to 382 * remove the loop cursor from the list. 383 */ 384 #define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \ 385 for (pos = rht_dereference_bucket(*rht_bucket(tbl, hash), tbl, hash), \ 386 next = !rht_is_a_nulls(pos) ? \ 387 rht_dereference_bucket(pos->next, tbl, hash) : NULL; \ 388 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \ 389 pos = next, \ 390 next = !rht_is_a_nulls(pos) ? \ 391 rht_dereference_bucket(pos->next, tbl, hash) : NULL) 392 393 /** 394 * rht_for_each_rcu_continue - continue iterating over rcu hash chain 395 * @pos: the &struct rhash_head to use as a loop cursor. 396 * @head: the previous &struct rhash_head to continue from 397 * @tbl: the &struct bucket_table 398 * @hash: the hash value / bucket index 399 * 400 * This hash chain list-traversal primitive may safely run concurrently with 401 * the _rcu mutation primitives such as rhashtable_insert() as long as the 402 * traversal is guarded by rcu_read_lock(). 403 */ 404 #define rht_for_each_rcu_continue(pos, head, tbl, hash) \ 405 for (({barrier(); }), \ 406 pos = rht_dereference_bucket_rcu(head, tbl, hash); \ 407 !rht_is_a_nulls(pos); \ 408 pos = rcu_dereference_raw(pos->next)) 409 410 /** 411 * rht_for_each_rcu - iterate over rcu hash chain 412 * @pos: the &struct rhash_head to use as a loop cursor. 413 * @tbl: the &struct bucket_table 414 * @hash: the hash value / bucket index 415 * 416 * This hash chain list-traversal primitive may safely run concurrently with 417 * the _rcu mutation primitives such as rhashtable_insert() as long as the 418 * traversal is guarded by rcu_read_lock(). 419 */ 420 #define rht_for_each_rcu(pos, tbl, hash) \ 421 rht_for_each_rcu_continue(pos, *rht_bucket(tbl, hash), tbl, hash) 422 423 /** 424 * rht_for_each_entry_rcu_continue - continue iterating over rcu hash chain 425 * @tpos: the type * to use as a loop cursor. 426 * @pos: the &struct rhash_head to use as a loop cursor. 427 * @head: the previous &struct rhash_head to continue from 428 * @tbl: the &struct bucket_table 429 * @hash: the hash value / bucket index 430 * @member: name of the &struct rhash_head within the hashable struct. 431 * 432 * This hash chain list-traversal primitive may safely run concurrently with 433 * the _rcu mutation primitives such as rhashtable_insert() as long as the 434 * traversal is guarded by rcu_read_lock(). 435 */ 436 #define rht_for_each_entry_rcu_continue(tpos, pos, head, tbl, hash, member) \ 437 for (({barrier(); }), \ 438 pos = rht_dereference_bucket_rcu(head, tbl, hash); \ 439 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \ 440 pos = rht_dereference_bucket_rcu(pos->next, tbl, hash)) 441 442 /** 443 * rht_for_each_entry_rcu - iterate over rcu hash chain of given type 444 * @tpos: the type * to use as a loop cursor. 445 * @pos: the &struct rhash_head to use as a loop cursor. 446 * @tbl: the &struct bucket_table 447 * @hash: the hash value / bucket index 448 * @member: name of the &struct rhash_head within the hashable struct. 449 * 450 * This hash chain list-traversal primitive may safely run concurrently with 451 * the _rcu mutation primitives such as rhashtable_insert() as long as the 452 * traversal is guarded by rcu_read_lock(). 453 */ 454 #define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \ 455 rht_for_each_entry_rcu_continue(tpos, pos, *rht_bucket(tbl, hash), \ 456 tbl, hash, member) 457 458 /** 459 * rhl_for_each_rcu - iterate over rcu hash table list 460 * @pos: the &struct rlist_head to use as a loop cursor. 461 * @list: the head of the list 462 * 463 * This hash chain list-traversal primitive should be used on the 464 * list returned by rhltable_lookup. 465 */ 466 #define rhl_for_each_rcu(pos, list) \ 467 for (pos = list; pos; pos = rcu_dereference_raw(pos->next)) 468 469 /** 470 * rhl_for_each_entry_rcu - iterate over rcu hash table list of given type 471 * @tpos: the type * to use as a loop cursor. 472 * @pos: the &struct rlist_head to use as a loop cursor. 473 * @list: the head of the list 474 * @member: name of the &struct rlist_head within the hashable struct. 475 * 476 * This hash chain list-traversal primitive should be used on the 477 * list returned by rhltable_lookup. 478 */ 479 #define rhl_for_each_entry_rcu(tpos, pos, list, member) \ 480 for (pos = list; pos && rht_entry(tpos, pos, member); \ 481 pos = rcu_dereference_raw(pos->next)) 482 483 static inline int rhashtable_compare(struct rhashtable_compare_arg *arg, 484 const void *obj) 485 { 486 struct rhashtable *ht = arg->ht; 487 const char *ptr = obj; 488 489 return memcmp(ptr + ht->p.key_offset, arg->key, ht->p.key_len); 490 } 491 492 /* Internal function, do not use. */ 493 static inline struct rhash_head *__rhashtable_lookup( 494 struct rhashtable *ht, const void *key, 495 const struct rhashtable_params params) 496 { 497 struct rhashtable_compare_arg arg = { 498 .ht = ht, 499 .key = key, 500 }; 501 struct bucket_table *tbl; 502 struct rhash_head *he; 503 unsigned int hash; 504 505 tbl = rht_dereference_rcu(ht->tbl, ht); 506 restart: 507 hash = rht_key_hashfn(ht, tbl, key, params); 508 rht_for_each_rcu(he, tbl, hash) { 509 if (params.obj_cmpfn ? 510 params.obj_cmpfn(&arg, rht_obj(ht, he)) : 511 rhashtable_compare(&arg, rht_obj(ht, he))) 512 continue; 513 return he; 514 } 515 516 /* Ensure we see any new tables. */ 517 smp_rmb(); 518 519 tbl = rht_dereference_rcu(tbl->future_tbl, ht); 520 if (unlikely(tbl)) 521 goto restart; 522 523 return NULL; 524 } 525 526 /** 527 * rhashtable_lookup - search hash table 528 * @ht: hash table 529 * @key: the pointer to the key 530 * @params: hash table parameters 531 * 532 * Computes the hash value for the key and traverses the bucket chain looking 533 * for a entry with an identical key. The first matching entry is returned. 534 * 535 * This must only be called under the RCU read lock. 536 * 537 * Returns the first entry on which the compare function returned true. 538 */ 539 static inline void *rhashtable_lookup( 540 struct rhashtable *ht, const void *key, 541 const struct rhashtable_params params) 542 { 543 struct rhash_head *he = __rhashtable_lookup(ht, key, params); 544 545 return he ? rht_obj(ht, he) : NULL; 546 } 547 548 /** 549 * rhashtable_lookup_fast - search hash table, without RCU read lock 550 * @ht: hash table 551 * @key: the pointer to the key 552 * @params: hash table parameters 553 * 554 * Computes the hash value for the key and traverses the bucket chain looking 555 * for a entry with an identical key. The first matching entry is returned. 556 * 557 * Only use this function when you have other mechanisms guaranteeing 558 * that the object won't go away after the RCU read lock is released. 559 * 560 * Returns the first entry on which the compare function returned true. 561 */ 562 static inline void *rhashtable_lookup_fast( 563 struct rhashtable *ht, const void *key, 564 const struct rhashtable_params params) 565 { 566 void *obj; 567 568 rcu_read_lock(); 569 obj = rhashtable_lookup(ht, key, params); 570 rcu_read_unlock(); 571 572 return obj; 573 } 574 575 /** 576 * rhltable_lookup - search hash list table 577 * @hlt: hash table 578 * @key: the pointer to the key 579 * @params: hash table parameters 580 * 581 * Computes the hash value for the key and traverses the bucket chain looking 582 * for a entry with an identical key. All matching entries are returned 583 * in a list. 584 * 585 * This must only be called under the RCU read lock. 586 * 587 * Returns the list of entries that match the given key. 588 */ 589 static inline struct rhlist_head *rhltable_lookup( 590 struct rhltable *hlt, const void *key, 591 const struct rhashtable_params params) 592 { 593 struct rhash_head *he = __rhashtable_lookup(&hlt->ht, key, params); 594 595 return he ? container_of(he, struct rhlist_head, rhead) : NULL; 596 } 597 598 /* Internal function, please use rhashtable_insert_fast() instead. This 599 * function returns the existing element already in hashes in there is a clash, 600 * otherwise it returns an error via ERR_PTR(). 601 */ 602 static inline void *__rhashtable_insert_fast( 603 struct rhashtable *ht, const void *key, struct rhash_head *obj, 604 const struct rhashtable_params params, bool rhlist) 605 { 606 struct rhashtable_compare_arg arg = { 607 .ht = ht, 608 .key = key, 609 }; 610 struct rhash_head __rcu **pprev; 611 struct bucket_table *tbl; 612 struct rhash_head *head; 613 spinlock_t *lock; 614 unsigned int hash; 615 int elasticity; 616 void *data; 617 618 rcu_read_lock(); 619 620 tbl = rht_dereference_rcu(ht->tbl, ht); 621 hash = rht_head_hashfn(ht, tbl, obj, params); 622 lock = rht_bucket_lock(tbl, hash); 623 spin_lock_bh(lock); 624 625 if (unlikely(rht_dereference_bucket(tbl->future_tbl, tbl, hash))) { 626 slow_path: 627 spin_unlock_bh(lock); 628 rcu_read_unlock(); 629 return rhashtable_insert_slow(ht, key, obj); 630 } 631 632 elasticity = RHT_ELASTICITY; 633 pprev = rht_bucket_insert(ht, tbl, hash); 634 data = ERR_PTR(-ENOMEM); 635 if (!pprev) 636 goto out; 637 638 rht_for_each_continue(head, *pprev, tbl, hash) { 639 struct rhlist_head *plist; 640 struct rhlist_head *list; 641 642 elasticity--; 643 if (!key || 644 (params.obj_cmpfn ? 645 params.obj_cmpfn(&arg, rht_obj(ht, head)) : 646 rhashtable_compare(&arg, rht_obj(ht, head)))) { 647 pprev = &head->next; 648 continue; 649 } 650 651 data = rht_obj(ht, head); 652 653 if (!rhlist) 654 goto out; 655 656 657 list = container_of(obj, struct rhlist_head, rhead); 658 plist = container_of(head, struct rhlist_head, rhead); 659 660 RCU_INIT_POINTER(list->next, plist); 661 head = rht_dereference_bucket(head->next, tbl, hash); 662 RCU_INIT_POINTER(list->rhead.next, head); 663 rcu_assign_pointer(*pprev, obj); 664 665 goto good; 666 } 667 668 if (elasticity <= 0) 669 goto slow_path; 670 671 data = ERR_PTR(-E2BIG); 672 if (unlikely(rht_grow_above_max(ht, tbl))) 673 goto out; 674 675 if (unlikely(rht_grow_above_100(ht, tbl))) 676 goto slow_path; 677 678 head = rht_dereference_bucket(*pprev, tbl, hash); 679 680 RCU_INIT_POINTER(obj->next, head); 681 if (rhlist) { 682 struct rhlist_head *list; 683 684 list = container_of(obj, struct rhlist_head, rhead); 685 RCU_INIT_POINTER(list->next, NULL); 686 } 687 688 rcu_assign_pointer(*pprev, obj); 689 690 atomic_inc(&ht->nelems); 691 if (rht_grow_above_75(ht, tbl)) 692 schedule_work(&ht->run_work); 693 694 good: 695 data = NULL; 696 697 out: 698 spin_unlock_bh(lock); 699 rcu_read_unlock(); 700 701 return data; 702 } 703 704 /** 705 * rhashtable_insert_fast - insert object into hash table 706 * @ht: hash table 707 * @obj: pointer to hash head inside object 708 * @params: hash table parameters 709 * 710 * Will take a per bucket spinlock to protect against mutual mutations 711 * on the same bucket. Multiple insertions may occur in parallel unless 712 * they map to the same bucket lock. 713 * 714 * It is safe to call this function from atomic context. 715 * 716 * Will trigger an automatic deferred table resizing if residency in the 717 * table grows beyond 70%. 718 */ 719 static inline int rhashtable_insert_fast( 720 struct rhashtable *ht, struct rhash_head *obj, 721 const struct rhashtable_params params) 722 { 723 void *ret; 724 725 ret = __rhashtable_insert_fast(ht, NULL, obj, params, false); 726 if (IS_ERR(ret)) 727 return PTR_ERR(ret); 728 729 return ret == NULL ? 0 : -EEXIST; 730 } 731 732 /** 733 * rhltable_insert_key - insert object into hash list table 734 * @hlt: hash list table 735 * @key: the pointer to the key 736 * @list: pointer to hash list head inside object 737 * @params: hash table parameters 738 * 739 * Will take a per bucket spinlock to protect against mutual mutations 740 * on the same bucket. Multiple insertions may occur in parallel unless 741 * they map to the same bucket lock. 742 * 743 * It is safe to call this function from atomic context. 744 * 745 * Will trigger an automatic deferred table resizing if residency in the 746 * table grows beyond 70%. 747 */ 748 static inline int rhltable_insert_key( 749 struct rhltable *hlt, const void *key, struct rhlist_head *list, 750 const struct rhashtable_params params) 751 { 752 return PTR_ERR(__rhashtable_insert_fast(&hlt->ht, key, &list->rhead, 753 params, true)); 754 } 755 756 /** 757 * rhltable_insert - insert object into hash list table 758 * @hlt: hash list table 759 * @list: pointer to hash list head inside object 760 * @params: hash table parameters 761 * 762 * Will take a per bucket spinlock to protect against mutual mutations 763 * on the same bucket. Multiple insertions may occur in parallel unless 764 * they map to the same bucket lock. 765 * 766 * It is safe to call this function from atomic context. 767 * 768 * Will trigger an automatic deferred table resizing if residency in the 769 * table grows beyond 70%. 770 */ 771 static inline int rhltable_insert( 772 struct rhltable *hlt, struct rhlist_head *list, 773 const struct rhashtable_params params) 774 { 775 const char *key = rht_obj(&hlt->ht, &list->rhead); 776 777 key += params.key_offset; 778 779 return rhltable_insert_key(hlt, key, list, params); 780 } 781 782 /** 783 * rhashtable_lookup_insert_fast - lookup and insert object into hash table 784 * @ht: hash table 785 * @obj: pointer to hash head inside object 786 * @params: hash table parameters 787 * 788 * Locks down the bucket chain in both the old and new table if a resize 789 * is in progress to ensure that writers can't remove from the old table 790 * and can't insert to the new table during the atomic operation of search 791 * and insertion. Searches for duplicates in both the old and new table if 792 * a resize is in progress. 793 * 794 * This lookup function may only be used for fixed key hash table (key_len 795 * parameter set). It will BUG() if used inappropriately. 796 * 797 * It is safe to call this function from atomic context. 798 * 799 * Will trigger an automatic deferred table resizing if residency in the 800 * table grows beyond 70%. 801 */ 802 static inline int rhashtable_lookup_insert_fast( 803 struct rhashtable *ht, struct rhash_head *obj, 804 const struct rhashtable_params params) 805 { 806 const char *key = rht_obj(ht, obj); 807 void *ret; 808 809 BUG_ON(ht->p.obj_hashfn); 810 811 ret = __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params, 812 false); 813 if (IS_ERR(ret)) 814 return PTR_ERR(ret); 815 816 return ret == NULL ? 0 : -EEXIST; 817 } 818 819 /** 820 * rhashtable_lookup_get_insert_fast - lookup and insert object into hash table 821 * @ht: hash table 822 * @obj: pointer to hash head inside object 823 * @params: hash table parameters 824 * 825 * Just like rhashtable_lookup_insert_fast(), but this function returns the 826 * object if it exists, NULL if it did not and the insertion was successful, 827 * and an ERR_PTR otherwise. 828 */ 829 static inline void *rhashtable_lookup_get_insert_fast( 830 struct rhashtable *ht, struct rhash_head *obj, 831 const struct rhashtable_params params) 832 { 833 const char *key = rht_obj(ht, obj); 834 835 BUG_ON(ht->p.obj_hashfn); 836 837 return __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params, 838 false); 839 } 840 841 /** 842 * rhashtable_lookup_insert_key - search and insert object to hash table 843 * with explicit key 844 * @ht: hash table 845 * @key: key 846 * @obj: pointer to hash head inside object 847 * @params: hash table parameters 848 * 849 * Locks down the bucket chain in both the old and new table if a resize 850 * is in progress to ensure that writers can't remove from the old table 851 * and can't insert to the new table during the atomic operation of search 852 * and insertion. Searches for duplicates in both the old and new table if 853 * a resize is in progress. 854 * 855 * Lookups may occur in parallel with hashtable mutations and resizing. 856 * 857 * Will trigger an automatic deferred table resizing if residency in the 858 * table grows beyond 70%. 859 * 860 * Returns zero on success. 861 */ 862 static inline int rhashtable_lookup_insert_key( 863 struct rhashtable *ht, const void *key, struct rhash_head *obj, 864 const struct rhashtable_params params) 865 { 866 void *ret; 867 868 BUG_ON(!ht->p.obj_hashfn || !key); 869 870 ret = __rhashtable_insert_fast(ht, key, obj, params, false); 871 if (IS_ERR(ret)) 872 return PTR_ERR(ret); 873 874 return ret == NULL ? 0 : -EEXIST; 875 } 876 877 /** 878 * rhashtable_lookup_get_insert_key - lookup and insert object into hash table 879 * @ht: hash table 880 * @obj: pointer to hash head inside object 881 * @params: hash table parameters 882 * @data: pointer to element data already in hashes 883 * 884 * Just like rhashtable_lookup_insert_key(), but this function returns the 885 * object if it exists, NULL if it does not and the insertion was successful, 886 * and an ERR_PTR otherwise. 887 */ 888 static inline void *rhashtable_lookup_get_insert_key( 889 struct rhashtable *ht, const void *key, struct rhash_head *obj, 890 const struct rhashtable_params params) 891 { 892 BUG_ON(!ht->p.obj_hashfn || !key); 893 894 return __rhashtable_insert_fast(ht, key, obj, params, false); 895 } 896 897 /* Internal function, please use rhashtable_remove_fast() instead */ 898 static inline int __rhashtable_remove_fast_one( 899 struct rhashtable *ht, struct bucket_table *tbl, 900 struct rhash_head *obj, const struct rhashtable_params params, 901 bool rhlist) 902 { 903 struct rhash_head __rcu **pprev; 904 struct rhash_head *he; 905 spinlock_t * lock; 906 unsigned int hash; 907 int err = -ENOENT; 908 909 hash = rht_head_hashfn(ht, tbl, obj, params); 910 lock = rht_bucket_lock(tbl, hash); 911 912 spin_lock_bh(lock); 913 914 pprev = rht_bucket_var(tbl, hash); 915 rht_for_each_continue(he, *pprev, tbl, hash) { 916 struct rhlist_head *list; 917 918 list = container_of(he, struct rhlist_head, rhead); 919 920 if (he != obj) { 921 struct rhlist_head __rcu **lpprev; 922 923 pprev = &he->next; 924 925 if (!rhlist) 926 continue; 927 928 do { 929 lpprev = &list->next; 930 list = rht_dereference_bucket(list->next, 931 tbl, hash); 932 } while (list && obj != &list->rhead); 933 934 if (!list) 935 continue; 936 937 list = rht_dereference_bucket(list->next, tbl, hash); 938 RCU_INIT_POINTER(*lpprev, list); 939 err = 0; 940 break; 941 } 942 943 obj = rht_dereference_bucket(obj->next, tbl, hash); 944 err = 1; 945 946 if (rhlist) { 947 list = rht_dereference_bucket(list->next, tbl, hash); 948 if (list) { 949 RCU_INIT_POINTER(list->rhead.next, obj); 950 obj = &list->rhead; 951 err = 0; 952 } 953 } 954 955 rcu_assign_pointer(*pprev, obj); 956 break; 957 } 958 959 spin_unlock_bh(lock); 960 961 if (err > 0) { 962 atomic_dec(&ht->nelems); 963 if (unlikely(ht->p.automatic_shrinking && 964 rht_shrink_below_30(ht, tbl))) 965 schedule_work(&ht->run_work); 966 err = 0; 967 } 968 969 return err; 970 } 971 972 /* Internal function, please use rhashtable_remove_fast() instead */ 973 static inline int __rhashtable_remove_fast( 974 struct rhashtable *ht, struct rhash_head *obj, 975 const struct rhashtable_params params, bool rhlist) 976 { 977 struct bucket_table *tbl; 978 int err; 979 980 rcu_read_lock(); 981 982 tbl = rht_dereference_rcu(ht->tbl, ht); 983 984 /* Because we have already taken (and released) the bucket 985 * lock in old_tbl, if we find that future_tbl is not yet 986 * visible then that guarantees the entry to still be in 987 * the old tbl if it exists. 988 */ 989 while ((err = __rhashtable_remove_fast_one(ht, tbl, obj, params, 990 rhlist)) && 991 (tbl = rht_dereference_rcu(tbl->future_tbl, ht))) 992 ; 993 994 rcu_read_unlock(); 995 996 return err; 997 } 998 999 /** 1000 * rhashtable_remove_fast - remove object from hash table 1001 * @ht: hash table 1002 * @obj: pointer to hash head inside object 1003 * @params: hash table parameters 1004 * 1005 * Since the hash chain is single linked, the removal operation needs to 1006 * walk the bucket chain upon removal. The removal operation is thus 1007 * considerable slow if the hash table is not correctly sized. 1008 * 1009 * Will automatically shrink the table if permitted when residency drops 1010 * below 30%. 1011 * 1012 * Returns zero on success, -ENOENT if the entry could not be found. 1013 */ 1014 static inline int rhashtable_remove_fast( 1015 struct rhashtable *ht, struct rhash_head *obj, 1016 const struct rhashtable_params params) 1017 { 1018 return __rhashtable_remove_fast(ht, obj, params, false); 1019 } 1020 1021 /** 1022 * rhltable_remove - remove object from hash list table 1023 * @hlt: hash list table 1024 * @list: pointer to hash list head inside object 1025 * @params: hash table parameters 1026 * 1027 * Since the hash chain is single linked, the removal operation needs to 1028 * walk the bucket chain upon removal. The removal operation is thus 1029 * considerable slow if the hash table is not correctly sized. 1030 * 1031 * Will automatically shrink the table if permitted when residency drops 1032 * below 30% 1033 * 1034 * Returns zero on success, -ENOENT if the entry could not be found. 1035 */ 1036 static inline int rhltable_remove( 1037 struct rhltable *hlt, struct rhlist_head *list, 1038 const struct rhashtable_params params) 1039 { 1040 return __rhashtable_remove_fast(&hlt->ht, &list->rhead, params, true); 1041 } 1042 1043 /* Internal function, please use rhashtable_replace_fast() instead */ 1044 static inline int __rhashtable_replace_fast( 1045 struct rhashtable *ht, struct bucket_table *tbl, 1046 struct rhash_head *obj_old, struct rhash_head *obj_new, 1047 const struct rhashtable_params params) 1048 { 1049 struct rhash_head __rcu **pprev; 1050 struct rhash_head *he; 1051 spinlock_t *lock; 1052 unsigned int hash; 1053 int err = -ENOENT; 1054 1055 /* Minimally, the old and new objects must have same hash 1056 * (which should mean identifiers are the same). 1057 */ 1058 hash = rht_head_hashfn(ht, tbl, obj_old, params); 1059 if (hash != rht_head_hashfn(ht, tbl, obj_new, params)) 1060 return -EINVAL; 1061 1062 lock = rht_bucket_lock(tbl, hash); 1063 1064 spin_lock_bh(lock); 1065 1066 pprev = rht_bucket_var(tbl, hash); 1067 rht_for_each_continue(he, *pprev, tbl, hash) { 1068 if (he != obj_old) { 1069 pprev = &he->next; 1070 continue; 1071 } 1072 1073 rcu_assign_pointer(obj_new->next, obj_old->next); 1074 rcu_assign_pointer(*pprev, obj_new); 1075 err = 0; 1076 break; 1077 } 1078 1079 spin_unlock_bh(lock); 1080 1081 return err; 1082 } 1083 1084 /** 1085 * rhashtable_replace_fast - replace an object in hash table 1086 * @ht: hash table 1087 * @obj_old: pointer to hash head inside object being replaced 1088 * @obj_new: pointer to hash head inside object which is new 1089 * @params: hash table parameters 1090 * 1091 * Replacing an object doesn't affect the number of elements in the hash table 1092 * or bucket, so we don't need to worry about shrinking or expanding the 1093 * table here. 1094 * 1095 * Returns zero on success, -ENOENT if the entry could not be found, 1096 * -EINVAL if hash is not the same for the old and new objects. 1097 */ 1098 static inline int rhashtable_replace_fast( 1099 struct rhashtable *ht, struct rhash_head *obj_old, 1100 struct rhash_head *obj_new, 1101 const struct rhashtable_params params) 1102 { 1103 struct bucket_table *tbl; 1104 int err; 1105 1106 rcu_read_lock(); 1107 1108 tbl = rht_dereference_rcu(ht->tbl, ht); 1109 1110 /* Because we have already taken (and released) the bucket 1111 * lock in old_tbl, if we find that future_tbl is not yet 1112 * visible then that guarantees the entry to still be in 1113 * the old tbl if it exists. 1114 */ 1115 while ((err = __rhashtable_replace_fast(ht, tbl, obj_old, 1116 obj_new, params)) && 1117 (tbl = rht_dereference_rcu(tbl->future_tbl, ht))) 1118 ; 1119 1120 rcu_read_unlock(); 1121 1122 return err; 1123 } 1124 1125 /* Obsolete function, do not use in new code. */ 1126 static inline int rhashtable_walk_init(struct rhashtable *ht, 1127 struct rhashtable_iter *iter, gfp_t gfp) 1128 { 1129 rhashtable_walk_enter(ht, iter); 1130 return 0; 1131 } 1132 1133 /** 1134 * rhltable_walk_enter - Initialise an iterator 1135 * @hlt: Table to walk over 1136 * @iter: Hash table Iterator 1137 * 1138 * This function prepares a hash table walk. 1139 * 1140 * Note that if you restart a walk after rhashtable_walk_stop you 1141 * may see the same object twice. Also, you may miss objects if 1142 * there are removals in between rhashtable_walk_stop and the next 1143 * call to rhashtable_walk_start. 1144 * 1145 * For a completely stable walk you should construct your own data 1146 * structure outside the hash table. 1147 * 1148 * This function may be called from any process context, including 1149 * non-preemptable context, but cannot be called from softirq or 1150 * hardirq context. 1151 * 1152 * You must call rhashtable_walk_exit after this function returns. 1153 */ 1154 static inline void rhltable_walk_enter(struct rhltable *hlt, 1155 struct rhashtable_iter *iter) 1156 { 1157 return rhashtable_walk_enter(&hlt->ht, iter); 1158 } 1159 1160 /** 1161 * rhltable_free_and_destroy - free elements and destroy hash list table 1162 * @hlt: the hash list table to destroy 1163 * @free_fn: callback to release resources of element 1164 * @arg: pointer passed to free_fn 1165 * 1166 * See documentation for rhashtable_free_and_destroy. 1167 */ 1168 static inline void rhltable_free_and_destroy(struct rhltable *hlt, 1169 void (*free_fn)(void *ptr, 1170 void *arg), 1171 void *arg) 1172 { 1173 return rhashtable_free_and_destroy(&hlt->ht, free_fn, arg); 1174 } 1175 1176 static inline void rhltable_destroy(struct rhltable *hlt) 1177 { 1178 return rhltable_free_and_destroy(hlt, NULL, NULL); 1179 } 1180 1181 #endif /* _LINUX_RHASHTABLE_H */ 1182