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