1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * Resizable, Scalable, Concurrent Hash Table 4 * 5 * Copyright (c) 2015-2016 Herbert Xu <herbert@gondor.apana.org.au> 6 * Copyright (c) 2014-2015 Thomas Graf <tgraf@suug.ch> 7 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net> 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/irq_work.h> 24 #include <linux/jhash.h> 25 #include <linux/list_nulls.h> 26 #include <linux/workqueue.h> 27 #include <linux/rculist.h> 28 #include <linux/bit_spinlock.h> 29 30 #include <linux/rhashtable-types.h> 31 /* 32 * Objects in an rhashtable have an embedded struct rhash_head 33 * which is linked into as hash chain from the hash table - or one 34 * of two or more hash tables when the rhashtable is being resized. 35 * The end of the chain is marked with a special nulls marks which has 36 * the least significant bit set but otherwise stores the address of 37 * the hash bucket. This allows us to be sure we've found the end 38 * of the right list. 39 * The value stored in the hash bucket has BIT(0) used as a lock bit. 40 * This bit must be atomically set before any changes are made to 41 * the chain. To avoid dereferencing this pointer without clearing 42 * the bit first, we use an opaque 'struct rhash_lock_head *' for the 43 * pointer stored in the bucket. This struct needs to be defined so 44 * that rcu_dereference() works on it, but it has no content so a 45 * cast is needed for it to be useful. This ensures it isn't 46 * used by mistake with clearing the lock bit first. 47 */ 48 struct rhash_lock_head {}; 49 50 /* Maximum chain length before rehash 51 * 52 * The maximum (not average) chain length grows with the size of the hash 53 * table, at a rate of (log N)/(log log N). 54 * 55 * The value of 16 is selected so that even if the hash table grew to 56 * 2^32 you would not expect the maximum chain length to exceed it 57 * unless we are under attack (or extremely unlucky). 58 * 59 * As this limit is only to detect attacks, we don't need to set it to a 60 * lower value as you'd need the chain length to vastly exceed 16 to have 61 * any real effect on the system. 62 */ 63 #define RHT_ELASTICITY 16u 64 65 /** 66 * struct bucket_table - Table of hash buckets 67 * @size: Number of hash buckets 68 * @nest: Number of bits of first-level nested table. 69 * @rehash: Current bucket being rehashed 70 * @hash_rnd: Random seed to fold into hash 71 * @walkers: List of active walkers 72 * @rcu: RCU structure for freeing the table 73 * @future_tbl: Table under construction during rehashing 74 * @ntbl: Nested table used when out of memory. 75 * @buckets: size * hash buckets 76 */ 77 struct bucket_table { 78 unsigned int size; 79 unsigned int nest; 80 u32 hash_rnd; 81 struct list_head walkers; 82 struct rcu_head rcu; 83 84 struct bucket_table __rcu *future_tbl; 85 86 struct lockdep_map dep_map; 87 88 struct rhash_lock_head __rcu *buckets[] ____cacheline_aligned_in_smp; 89 }; 90 91 /* 92 * NULLS_MARKER() expects a hash value with the low 93 * bits mostly likely to be significant, and it discards 94 * the msb. 95 * We give it an address, in which the bottom bit is 96 * always 0, and the msb might be significant. 97 * So we shift the address down one bit to align with 98 * expectations and avoid losing a significant bit. 99 * 100 * We never store the NULLS_MARKER in the hash table 101 * itself as we need the lsb for locking. 102 * Instead we store a NULL 103 */ 104 #define RHT_NULLS_MARKER(ptr) \ 105 ((void *)NULLS_MARKER(((unsigned long) (ptr)) >> 1)) 106 #define INIT_RHT_NULLS_HEAD(ptr) \ 107 ((ptr) = NULL) 108 109 static inline bool rht_is_a_nulls(const struct rhash_head *ptr) 110 { 111 return ((unsigned long) ptr & 1); 112 } 113 114 static inline void *rht_obj(const struct rhashtable *ht, 115 const struct rhash_head *he) 116 { 117 return (char *)he - ht->p.head_offset; 118 } 119 120 static inline unsigned int rht_bucket_index(const struct bucket_table *tbl, 121 unsigned int hash) 122 { 123 return hash & (tbl->size - 1); 124 } 125 126 static __always_inline unsigned int rht_key_get_hash(struct rhashtable *ht, 127 const void *key, const struct rhashtable_params params, 128 unsigned int hash_rnd) 129 { 130 unsigned int hash; 131 132 /* params must be equal to ht->p if it isn't constant. */ 133 if (!__builtin_constant_p(params.key_len)) { 134 hash = ht->p.hashfn(key, ht->key_len, hash_rnd); 135 } else { 136 unsigned int key_len = params.key_len ? : ht->p.key_len; 137 138 if (params.hashfn) 139 hash = params.hashfn(key, key_len, hash_rnd); 140 else if (key_len & (sizeof(u32) - 1)) 141 hash = jhash(key, key_len, hash_rnd); 142 else 143 hash = jhash2(key, key_len / sizeof(u32), hash_rnd); 144 } 145 146 return hash; 147 } 148 149 static __always_inline unsigned int rht_key_hashfn( 150 struct rhashtable *ht, const struct bucket_table *tbl, 151 const void *key, const struct rhashtable_params params) 152 { 153 unsigned int hash = rht_key_get_hash(ht, key, params, tbl->hash_rnd); 154 155 return rht_bucket_index(tbl, hash); 156 } 157 158 static __always_inline unsigned int rht_head_hashfn( 159 struct rhashtable *ht, const struct bucket_table *tbl, 160 const struct rhash_head *he, const struct rhashtable_params params) 161 { 162 const char *ptr = rht_obj(ht, he); 163 164 return likely(params.obj_hashfn) ? 165 rht_bucket_index(tbl, params.obj_hashfn(ptr, params.key_len ?: 166 ht->p.key_len, 167 tbl->hash_rnd)) : 168 rht_key_hashfn(ht, tbl, ptr + params.key_offset, params); 169 } 170 171 /** 172 * rht_grow_above_75 - returns true if nelems > 0.75 * table-size 173 * @ht: hash table 174 * @tbl: current table 175 */ 176 static inline bool rht_grow_above_75(const struct rhashtable *ht, 177 const struct bucket_table *tbl) 178 { 179 /* Expand table when exceeding 75% load */ 180 return atomic_read(&ht->nelems) > (tbl->size / 4 * 3) && 181 (!ht->p.max_size || tbl->size < ht->p.max_size); 182 } 183 184 /** 185 * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size 186 * @ht: hash table 187 * @tbl: current table 188 */ 189 static inline bool rht_shrink_below_30(const struct rhashtable *ht, 190 const struct bucket_table *tbl) 191 { 192 /* Shrink table beneath 30% load */ 193 return atomic_read(&ht->nelems) < (tbl->size * 3 / 10) && 194 tbl->size > ht->p.min_size; 195 } 196 197 /** 198 * rht_grow_above_100 - returns true if nelems > table-size 199 * @ht: hash table 200 * @tbl: current table 201 */ 202 static inline bool rht_grow_above_100(const struct rhashtable *ht, 203 const struct bucket_table *tbl) 204 { 205 return atomic_read(&ht->nelems) > tbl->size && 206 (!ht->p.max_size || tbl->size < ht->p.max_size); 207 } 208 209 /** 210 * rht_grow_above_max - returns true if table is above maximum 211 * @ht: hash table 212 * @tbl: current table 213 */ 214 static inline bool rht_grow_above_max(const struct rhashtable *ht, 215 const struct bucket_table *tbl) 216 { 217 return atomic_read(&ht->nelems) >= ht->max_elems; 218 } 219 220 #ifdef CONFIG_PROVE_LOCKING 221 int lockdep_rht_mutex_is_held(struct rhashtable *ht); 222 int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash); 223 #else 224 static inline int lockdep_rht_mutex_is_held(struct rhashtable *ht) 225 { 226 return 1; 227 } 228 229 static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, 230 u32 hash) 231 { 232 return 1; 233 } 234 #endif /* CONFIG_PROVE_LOCKING */ 235 236 void *rhashtable_insert_slow(struct rhashtable *ht, const void *key, 237 struct rhash_head *obj); 238 239 void rhashtable_walk_enter(struct rhashtable *ht, 240 struct rhashtable_iter *iter); 241 void rhashtable_walk_exit(struct rhashtable_iter *iter); 242 int rhashtable_walk_start_check(struct rhashtable_iter *iter) __acquires_shared(RCU); 243 244 static inline void rhashtable_walk_start(struct rhashtable_iter *iter) 245 __acquires_shared(RCU) 246 { 247 (void)rhashtable_walk_start_check(iter); 248 } 249 250 void *rhashtable_walk_next(struct rhashtable_iter *iter); 251 void *rhashtable_walk_peek(struct rhashtable_iter *iter); 252 void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases_shared(RCU); 253 254 void rhashtable_free_and_destroy(struct rhashtable *ht, 255 void (*free_fn)(void *ptr, void *arg), 256 void *arg); 257 void rhashtable_destroy(struct rhashtable *ht); 258 259 struct rhash_lock_head __rcu **rht_bucket_nested( 260 const struct bucket_table *tbl, unsigned int hash); 261 struct rhash_lock_head __rcu **__rht_bucket_nested( 262 const struct bucket_table *tbl, unsigned int hash); 263 struct rhash_lock_head __rcu **rht_bucket_nested_insert( 264 struct rhashtable *ht, struct bucket_table *tbl, unsigned int hash); 265 266 #define rht_dereference(p, ht) \ 267 rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht)) 268 269 #define rht_dereference_rcu(p, ht) \ 270 rcu_dereference_all_check(p, lockdep_rht_mutex_is_held(ht)) 271 272 #define rht_dereference_bucket(p, tbl, hash) \ 273 rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash)) 274 275 #define rht_dereference_bucket_rcu(p, tbl, hash) \ 276 rcu_dereference_all_check(p, lockdep_rht_bucket_is_held(tbl, hash)) 277 278 #define rht_entry(tpos, pos, member) \ 279 ({ tpos = container_of(pos, typeof(*tpos), member); 1; }) 280 281 static inline struct rhash_lock_head __rcu *const *rht_bucket( 282 const struct bucket_table *tbl, unsigned int hash) 283 { 284 return unlikely(tbl->nest) ? rht_bucket_nested(tbl, hash) : 285 &tbl->buckets[hash]; 286 } 287 288 static inline struct rhash_lock_head __rcu **rht_bucket_var( 289 struct bucket_table *tbl, unsigned int hash) 290 { 291 return unlikely(tbl->nest) ? __rht_bucket_nested(tbl, hash) : 292 &tbl->buckets[hash]; 293 } 294 295 static inline struct rhash_lock_head __rcu **rht_bucket_insert( 296 struct rhashtable *ht, struct bucket_table *tbl, unsigned int hash) 297 { 298 return unlikely(tbl->nest) ? rht_bucket_nested_insert(ht, tbl, hash) : 299 &tbl->buckets[hash]; 300 } 301 302 /* 303 * We lock a bucket by setting BIT(0) in the pointer - this is always 304 * zero in real pointers. The NULLS mark is never stored in the bucket, 305 * rather we store NULL if the bucket is empty. 306 * bit_spin_locks do not handle contention well, but the whole point 307 * of the hashtable design is to achieve minimum per-bucket contention. 308 * A nested hash table might not have a bucket pointer. In that case 309 * we cannot get a lock. For remove and replace the bucket cannot be 310 * interesting and doesn't need locking. 311 * For insert we allocate the bucket if this is the last bucket_table, 312 * and then take the lock. 313 * Sometimes we unlock a bucket by writing a new pointer there. In that 314 * case we don't need to unlock, but we do need to reset state such as 315 * local_bh. For that we have rht_assign_unlock(). As rcu_assign_pointer() 316 * provides the same release semantics that bit_spin_unlock() provides, 317 * this is safe. 318 * When we write to a bucket without unlocking, we use rht_assign_locked(). 319 */ 320 321 static inline unsigned long rht_lock(struct bucket_table *tbl, 322 struct rhash_lock_head __rcu **bkt) 323 __acquires(__bitlock(0, bkt)) 324 { 325 unsigned long flags; 326 327 local_irq_save(flags); 328 bit_spin_lock(0, (unsigned long *)bkt); 329 lock_map_acquire(&tbl->dep_map); 330 return flags; 331 } 332 333 static inline unsigned long rht_lock_nested(struct bucket_table *tbl, 334 struct rhash_lock_head __rcu **bucket, 335 unsigned int subclass) 336 __acquires(__bitlock(0, bucket)) 337 { 338 unsigned long flags; 339 340 local_irq_save(flags); 341 bit_spin_lock(0, (unsigned long *)bucket); 342 lock_acquire_exclusive(&tbl->dep_map, subclass, 0, NULL, _THIS_IP_); 343 return flags; 344 } 345 346 static inline void rht_unlock(struct bucket_table *tbl, 347 struct rhash_lock_head __rcu **bkt, 348 unsigned long flags) 349 __releases(__bitlock(0, bkt)) 350 { 351 lock_map_release(&tbl->dep_map); 352 bit_spin_unlock(0, (unsigned long *)bkt); 353 local_irq_restore(flags); 354 } 355 356 enum rht_lookup_freq { 357 RHT_LOOKUP_NORMAL, 358 RHT_LOOKUP_LIKELY, 359 }; 360 361 static __always_inline struct rhash_head *__rht_ptr( 362 struct rhash_lock_head *p, struct rhash_lock_head __rcu *const *bkt, 363 const enum rht_lookup_freq freq) 364 { 365 unsigned long p_val = (unsigned long)p & ~BIT(0); 366 367 BUILD_BUG_ON(!__builtin_constant_p(freq)); 368 369 if (freq == RHT_LOOKUP_LIKELY) 370 return (struct rhash_head *) 371 (likely(p_val) ? p_val : (unsigned long)RHT_NULLS_MARKER(bkt)); 372 else 373 return (struct rhash_head *) 374 (p_val ?: (unsigned long)RHT_NULLS_MARKER(bkt)); 375 } 376 377 /* 378 * Where 'bkt' is a bucket and might be locked: 379 * rht_ptr_rcu() dereferences that pointer and clears the lock bit. 380 * rht_ptr() dereferences in a context where the bucket is locked. 381 * rht_ptr_exclusive() dereferences in a context where exclusive 382 * access is guaranteed, such as when destroying the table. 383 */ 384 static __always_inline struct rhash_head *__rht_ptr_rcu( 385 struct rhash_lock_head __rcu *const *bkt, 386 const enum rht_lookup_freq freq) 387 { 388 return __rht_ptr(rcu_dereference_all(*bkt), bkt, freq); 389 } 390 391 static inline struct rhash_head *rht_ptr_rcu( 392 struct rhash_lock_head __rcu *const *bkt) 393 { 394 return __rht_ptr_rcu(bkt, RHT_LOOKUP_NORMAL); 395 } 396 397 static inline struct rhash_head *rht_ptr( 398 struct rhash_lock_head __rcu *const *bkt, 399 struct bucket_table *tbl, 400 unsigned int hash) 401 { 402 return __rht_ptr(rht_dereference_bucket(*bkt, tbl, hash), bkt, 403 RHT_LOOKUP_NORMAL); 404 } 405 406 static inline struct rhash_head *rht_ptr_exclusive( 407 struct rhash_lock_head __rcu *const *bkt) 408 { 409 return __rht_ptr(rcu_dereference_protected(*bkt, 1), bkt, 410 RHT_LOOKUP_NORMAL); 411 } 412 413 static inline void rht_assign_locked(struct rhash_lock_head __rcu **bkt, 414 struct rhash_head *obj) 415 { 416 if (rht_is_a_nulls(obj)) 417 obj = NULL; 418 rcu_assign_pointer(*bkt, (void *)((unsigned long)obj | BIT(0))); 419 } 420 421 static inline void rht_assign_unlock(struct bucket_table *tbl, 422 struct rhash_lock_head __rcu **bkt, 423 struct rhash_head *obj, 424 unsigned long flags) 425 __releases(__bitlock(0, bkt)) 426 { 427 if (rht_is_a_nulls(obj)) 428 obj = NULL; 429 lock_map_release(&tbl->dep_map); 430 rcu_assign_pointer(*bkt, (void *)obj); 431 preempt_enable(); 432 __release(__bitlock(0, bkt)); 433 local_irq_restore(flags); 434 } 435 436 /** 437 * rht_for_each_from - iterate over hash chain from given head 438 * @pos: the &struct rhash_head to use as a loop cursor. 439 * @head: the &struct rhash_head to start from 440 * @tbl: the &struct bucket_table 441 * @hash: the hash value / bucket index 442 */ 443 #define rht_for_each_from(pos, head, tbl, hash) \ 444 for (pos = head; \ 445 !rht_is_a_nulls(pos); \ 446 pos = rht_dereference_bucket((pos)->next, tbl, hash)) 447 448 /** 449 * rht_for_each - iterate over hash chain 450 * @pos: the &struct rhash_head to use as a loop cursor. 451 * @tbl: the &struct bucket_table 452 * @hash: the hash value / bucket index 453 */ 454 #define rht_for_each(pos, tbl, hash) \ 455 rht_for_each_from(pos, rht_ptr(rht_bucket(tbl, hash), tbl, hash), \ 456 tbl, hash) 457 458 /** 459 * rht_for_each_entry_from - iterate over hash chain from given head 460 * @tpos: the type * to use as a loop cursor. 461 * @pos: the &struct rhash_head to use as a loop cursor. 462 * @head: the &struct rhash_head to start from 463 * @tbl: the &struct bucket_table 464 * @hash: the hash value / bucket index 465 * @member: name of the &struct rhash_head within the hashable struct. 466 */ 467 #define rht_for_each_entry_from(tpos, pos, head, tbl, hash, member) \ 468 for (pos = head; \ 469 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \ 470 pos = rht_dereference_bucket((pos)->next, tbl, hash)) 471 472 /** 473 * rht_for_each_entry - iterate over hash chain of given type 474 * @tpos: the type * to use as a loop cursor. 475 * @pos: the &struct rhash_head to use as a loop cursor. 476 * @tbl: the &struct bucket_table 477 * @hash: the hash value / bucket index 478 * @member: name of the &struct rhash_head within the hashable struct. 479 */ 480 #define rht_for_each_entry(tpos, pos, tbl, hash, member) \ 481 rht_for_each_entry_from(tpos, pos, \ 482 rht_ptr(rht_bucket(tbl, hash), tbl, hash), \ 483 tbl, hash, member) 484 485 /** 486 * rht_for_each_entry_safe - safely iterate over hash chain of given type 487 * @tpos: the type * to use as a loop cursor. 488 * @pos: the &struct rhash_head to use as a loop cursor. 489 * @next: the &struct rhash_head to use as next in loop cursor. 490 * @tbl: the &struct bucket_table 491 * @hash: the hash value / bucket index 492 * @member: name of the &struct rhash_head within the hashable struct. 493 * 494 * This hash chain list-traversal primitive allows for the looped code to 495 * remove the loop cursor from the list. 496 */ 497 #define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \ 498 for (pos = rht_ptr(rht_bucket(tbl, hash), tbl, hash), \ 499 next = !rht_is_a_nulls(pos) ? \ 500 rht_dereference_bucket(pos->next, tbl, hash) : NULL; \ 501 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \ 502 pos = next, \ 503 next = !rht_is_a_nulls(pos) ? \ 504 rht_dereference_bucket(pos->next, tbl, hash) : NULL) 505 506 /** 507 * rht_for_each_rcu_from - iterate over rcu hash chain from given head 508 * @pos: the &struct rhash_head to use as a loop cursor. 509 * @head: the &struct rhash_head to start from 510 * @tbl: the &struct bucket_table 511 * @hash: the hash value / bucket index 512 * 513 * This hash chain list-traversal primitive may safely run concurrently with 514 * the _rcu mutation primitives such as rhashtable_insert() as long as the 515 * traversal is guarded by rcu_read_lock(). 516 */ 517 #define rht_for_each_rcu_from(pos, head, tbl, hash) \ 518 for (({barrier(); }), \ 519 pos = head; \ 520 !rht_is_a_nulls(pos); \ 521 pos = rcu_dereference_all(pos->next)) 522 523 /** 524 * rht_for_each_rcu - iterate over rcu hash chain 525 * @pos: the &struct rhash_head to use as a loop cursor. 526 * @tbl: the &struct bucket_table 527 * @hash: the hash value / bucket index 528 * 529 * This hash chain list-traversal primitive may safely run concurrently with 530 * the _rcu mutation primitives such as rhashtable_insert() as long as the 531 * traversal is guarded by rcu_read_lock(). 532 */ 533 #define rht_for_each_rcu(pos, tbl, hash) \ 534 for (({barrier(); }), \ 535 pos = rht_ptr_rcu(rht_bucket(tbl, hash)); \ 536 !rht_is_a_nulls(pos); \ 537 pos = rcu_dereference_all(pos->next)) 538 539 /** 540 * rht_for_each_entry_rcu_from - iterated over rcu hash chain from given head 541 * @tpos: the type * to use as a loop cursor. 542 * @pos: the &struct rhash_head to use as a loop cursor. 543 * @head: the &struct rhash_head to start from 544 * @tbl: the &struct bucket_table 545 * @hash: the hash value / bucket index 546 * @member: name of the &struct rhash_head within the hashable struct. 547 * 548 * This hash chain list-traversal primitive may safely run concurrently with 549 * the _rcu mutation primitives such as rhashtable_insert() as long as the 550 * traversal is guarded by rcu_read_lock(). 551 */ 552 #define rht_for_each_entry_rcu_from(tpos, pos, head, tbl, hash, member) \ 553 for (({barrier(); }), \ 554 pos = head; \ 555 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \ 556 pos = rht_dereference_bucket_rcu(pos->next, tbl, hash)) 557 558 /** 559 * rht_for_each_entry_rcu - iterate over rcu hash chain of given type 560 * @tpos: the type * to use as a loop cursor. 561 * @pos: the &struct rhash_head to use as a loop cursor. 562 * @tbl: the &struct bucket_table 563 * @hash: the hash value / bucket index 564 * @member: name of the &struct rhash_head within the hashable struct. 565 * 566 * This hash chain list-traversal primitive may safely run concurrently with 567 * the _rcu mutation primitives such as rhashtable_insert() as long as the 568 * traversal is guarded by rcu_read_lock(). 569 */ 570 #define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \ 571 rht_for_each_entry_rcu_from(tpos, pos, \ 572 rht_ptr_rcu(rht_bucket(tbl, hash)), \ 573 tbl, hash, member) 574 575 /** 576 * rhl_for_each_rcu - iterate over rcu hash table list 577 * @pos: the &struct rlist_head to use as a loop cursor. 578 * @list: the head of the list 579 * 580 * This hash chain list-traversal primitive should be used on the 581 * list returned by rhltable_lookup. 582 */ 583 #define rhl_for_each_rcu(pos, list) \ 584 for (pos = list; pos; pos = rcu_dereference_all(pos->next)) 585 586 /** 587 * rhl_for_each_entry_rcu - iterate over rcu hash table list of given type 588 * @tpos: the type * to use as a loop cursor. 589 * @pos: the &struct rlist_head to use as a loop cursor. 590 * @list: the head of the list 591 * @member: name of the &struct rlist_head within the hashable struct. 592 * 593 * This hash chain list-traversal primitive should be used on the 594 * list returned by rhltable_lookup. 595 */ 596 #define rhl_for_each_entry_rcu(tpos, pos, list, member) \ 597 for (pos = list; pos && rht_entry(tpos, pos, member); \ 598 pos = rcu_dereference_all(pos->next)) 599 600 static inline int rhashtable_compare(struct rhashtable_compare_arg *arg, 601 const void *obj) 602 { 603 struct rhashtable *ht = arg->ht; 604 const char *ptr = obj; 605 606 return memcmp(ptr + ht->p.key_offset, arg->key, ht->p.key_len); 607 } 608 609 /* Internal function, do not use. */ 610 static __always_inline struct rhash_head *__rhashtable_lookup( 611 struct rhashtable *ht, const void *key, 612 const struct rhashtable_params params, 613 const enum rht_lookup_freq freq) 614 __must_hold_shared(RCU) 615 { 616 struct rhashtable_compare_arg arg = { 617 .ht = ht, 618 .key = key, 619 }; 620 struct rhash_lock_head __rcu *const *bkt; 621 struct bucket_table *tbl; 622 struct rhash_head *he; 623 unsigned int hash; 624 625 BUILD_BUG_ON(!__builtin_constant_p(freq)); 626 tbl = rht_dereference_rcu(ht->tbl, ht); 627 restart: 628 hash = rht_key_hashfn(ht, tbl, key, params); 629 bkt = rht_bucket(tbl, hash); 630 do { 631 rht_for_each_rcu_from(he, __rht_ptr_rcu(bkt, freq), tbl, hash) { 632 if (params.obj_cmpfn ? 633 params.obj_cmpfn(&arg, rht_obj(ht, he)) : 634 rhashtable_compare(&arg, rht_obj(ht, he))) 635 continue; 636 return he; 637 } 638 /* An object might have been moved to a different hash chain, 639 * while we walk along it - better check and retry. 640 */ 641 } while (he != RHT_NULLS_MARKER(bkt)); 642 643 /* Ensure we see any new tables. */ 644 smp_rmb(); 645 646 tbl = rht_dereference_rcu(tbl->future_tbl, ht); 647 if (unlikely(tbl)) 648 goto restart; 649 650 return NULL; 651 } 652 653 /** 654 * rhashtable_next_key - return next element after a given key 655 * @ht: hash table 656 * @prev_key: pointer to previous key, or NULL for the first element 657 * 658 * WARNING: this walk is highly unstable. Unlike rhashtable_walk_*(), 659 * it cannot detect a concurrent resize or rehash, so a full iteration 660 * is NOT guaranteed to terminate under adversarial or sustained 661 * rehashing. Callers MUST tolerate skipped and duplicated elements and 662 * SHOULD bound their loop externally. 663 * 664 * Returns the next element in best-effort iteration order, walking the 665 * @tbl chain (including any future_tbl in flight). Caller must hold RCU. 666 * 667 * Pass @prev_key == NULL to obtain the first element. To iterate, set 668 * @prev_key to the key of the previously returned element on each call, 669 * and stop when NULL is returned. 670 * 671 * Best-effort semantics: 672 * - Across the tbl->future_tbl chain, an element being migrated may 673 * transiently appear in both tables and be observed twice. 674 * - Concurrent inserts may or may not be observed. 675 * - Termination of a full iteration loop is NOT guaranteed under 676 * adversarial continuous rehash; callers MUST tolerate skips and 677 * repeats and SHOULD bound their loop externally. 678 * - Behavior on tables that contain duplicate keys is undefined: 679 * duplicates may be skipped, repeated, or trap the walk in a 680 * cycle. Callers requiring duplicate-key iteration must use 681 * rhashtable_walk_*() instead. 682 * - rhltable instances are not supported and return 683 * ERR_PTR(-EOPNOTSUPP). 684 * - If prev_key was concurrently deleted and is not present in any 685 * in-flight table, returns ERR_PTR(-ENOENT). 686 * 687 * Returns entry of the next element, or NULL when iteration is exhausted, 688 * or ERR_PTR(-ENOENT) if prev_key is not found, or 689 * ERR_PTR(-EOPNOTSUPP) if @ht is an rhltable. 690 */ 691 void *rhashtable_next_key(struct rhashtable *ht, const void *prev_key); 692 693 /** 694 * rhashtable_lookup - search hash table 695 * @ht: hash table 696 * @key: the pointer to the key 697 * @params: hash table parameters 698 * 699 * Computes the hash value for the key and traverses the bucket chain looking 700 * for an entry with an identical key. The first matching entry is returned. 701 * 702 * This must only be called under the RCU read lock. 703 * 704 * Returns the first entry on which the compare function returned true. 705 */ 706 static __always_inline void *rhashtable_lookup( 707 struct rhashtable *ht, const void *key, 708 const struct rhashtable_params params) 709 __must_hold_shared(RCU) 710 { 711 struct rhash_head *he = __rhashtable_lookup(ht, key, params, 712 RHT_LOOKUP_NORMAL); 713 714 return he ? rht_obj(ht, he) : NULL; 715 } 716 717 static __always_inline void *rhashtable_lookup_likely( 718 struct rhashtable *ht, const void *key, 719 const struct rhashtable_params params) 720 __must_hold_shared(RCU) 721 { 722 struct rhash_head *he = __rhashtable_lookup(ht, key, params, 723 RHT_LOOKUP_LIKELY); 724 725 return likely(he) ? rht_obj(ht, he) : NULL; 726 } 727 728 /** 729 * rhashtable_lookup_fast - search hash table, without RCU read lock 730 * @ht: hash table 731 * @key: the pointer to the key 732 * @params: hash table parameters 733 * 734 * Computes the hash value for the key and traverses the bucket chain looking 735 * for an entry with an identical key. The first matching entry is returned. 736 * 737 * Only use this function when you have other mechanisms guaranteeing 738 * that the object won't go away after the RCU read lock is released. 739 * 740 * Returns the first entry on which the compare function returned true. 741 */ 742 static __always_inline void *rhashtable_lookup_fast( 743 struct rhashtable *ht, const void *key, 744 const struct rhashtable_params params) 745 { 746 void *obj; 747 748 rcu_read_lock(); 749 obj = rhashtable_lookup(ht, key, params); 750 rcu_read_unlock(); 751 752 return obj; 753 } 754 755 /** 756 * rhltable_lookup - search hash list table 757 * @hlt: hash table 758 * @key: the pointer to the key 759 * @params: hash table parameters 760 * 761 * Computes the hash value for the key and traverses the bucket chain looking 762 * for an entry with an identical key. All matching entries are returned 763 * in a list. 764 * 765 * This must only be called under the RCU read lock. 766 * 767 * Returns the list of entries that match the given key. 768 */ 769 static __always_inline struct rhlist_head *rhltable_lookup( 770 struct rhltable *hlt, const void *key, 771 const struct rhashtable_params params) 772 __must_hold_shared(RCU) 773 { 774 struct rhash_head *he = __rhashtable_lookup(&hlt->ht, key, params, 775 RHT_LOOKUP_NORMAL); 776 777 return he ? container_of(he, struct rhlist_head, rhead) : NULL; 778 } 779 780 static __always_inline struct rhlist_head *rhltable_lookup_likely( 781 struct rhltable *hlt, const void *key, 782 const struct rhashtable_params params) 783 __must_hold_shared(RCU) 784 { 785 struct rhash_head *he = __rhashtable_lookup(&hlt->ht, key, params, 786 RHT_LOOKUP_LIKELY); 787 788 return likely(he) ? container_of(he, struct rhlist_head, rhead) : NULL; 789 } 790 791 /* Internal function, please use rhashtable_insert_fast() instead. This 792 * function returns the existing element already in hashes if there is a clash, 793 * otherwise it returns an error via ERR_PTR(). 794 */ 795 static __always_inline void *__rhashtable_insert_fast( 796 struct rhashtable *ht, const void *key, struct rhash_head *obj, 797 const struct rhashtable_params params, bool rhlist) 798 { 799 struct rhashtable_compare_arg arg = { 800 .ht = ht, 801 .key = key, 802 }; 803 struct rhash_lock_head __rcu **bkt; 804 struct rhash_head __rcu **pprev; 805 struct bucket_table *tbl; 806 struct rhash_head *head; 807 unsigned long flags; 808 unsigned int hash; 809 int elasticity; 810 void *data; 811 812 rcu_read_lock(); 813 814 tbl = rht_dereference_rcu(ht->tbl, ht); 815 hash = rht_head_hashfn(ht, tbl, obj, params); 816 elasticity = RHT_ELASTICITY; 817 bkt = rht_bucket_insert(ht, tbl, hash); 818 data = ERR_PTR(-ENOMEM); 819 if (!bkt) 820 goto out; 821 pprev = NULL; 822 flags = rht_lock(tbl, bkt); 823 824 if (unlikely(rcu_access_pointer(tbl->future_tbl))) { 825 slow_path: 826 rht_unlock(tbl, bkt, flags); 827 rcu_read_unlock(); 828 return rhashtable_insert_slow(ht, key, obj); 829 } 830 831 rht_for_each_from(head, rht_ptr(bkt, tbl, hash), tbl, hash) { 832 struct rhlist_head *plist; 833 struct rhlist_head *list; 834 835 elasticity--; 836 if (!key || 837 (params.obj_cmpfn ? 838 params.obj_cmpfn(&arg, rht_obj(ht, head)) : 839 rhashtable_compare(&arg, rht_obj(ht, head)))) { 840 pprev = &head->next; 841 continue; 842 } 843 844 data = rht_obj(ht, head); 845 846 if (!rhlist) 847 goto out_unlock; 848 849 850 list = container_of(obj, struct rhlist_head, rhead); 851 plist = container_of(head, struct rhlist_head, rhead); 852 853 RCU_INIT_POINTER(list->next, plist); 854 head = rht_dereference_bucket(head->next, tbl, hash); 855 RCU_INIT_POINTER(list->rhead.next, head); 856 if (pprev) { 857 rcu_assign_pointer(*pprev, obj); 858 rht_unlock(tbl, bkt, flags); 859 } else 860 rht_assign_unlock(tbl, bkt, obj, flags); 861 data = NULL; 862 goto out; 863 } 864 865 if (elasticity <= 0 && !params.insecure_elasticity) 866 goto slow_path; 867 868 data = ERR_PTR(-E2BIG); 869 if (unlikely(rht_grow_above_max(ht, tbl))) 870 goto out_unlock; 871 872 if (unlikely(rht_grow_above_100(ht, tbl)) && 873 !params.insecure_elasticity) 874 goto slow_path; 875 876 /* Inserting at head of list makes unlocking free. */ 877 head = rht_ptr(bkt, tbl, hash); 878 879 RCU_INIT_POINTER(obj->next, head); 880 if (rhlist) { 881 struct rhlist_head *list; 882 883 list = container_of(obj, struct rhlist_head, rhead); 884 RCU_INIT_POINTER(list->next, NULL); 885 } 886 887 atomic_inc(&ht->nelems); 888 rht_assign_unlock(tbl, bkt, obj, flags); 889 890 if (rht_grow_above_75(ht, tbl)) 891 irq_work_queue(&ht->run_irq_work); 892 893 data = NULL; 894 out: 895 rcu_read_unlock(); 896 897 return data; 898 899 out_unlock: 900 rht_unlock(tbl, bkt, flags); 901 goto out; 902 } 903 904 /** 905 * rhashtable_insert_fast - insert object into hash table 906 * @ht: hash table 907 * @obj: pointer to hash head inside object 908 * @params: hash table parameters 909 * 910 * Will take the per bucket bitlock to protect against mutual mutations 911 * on the same bucket. Multiple insertions may occur in parallel unless 912 * they map to the same bucket. 913 * 914 * It is safe to call this function from atomic context. 915 * 916 * Will trigger an automatic deferred table resizing if residency in the 917 * table grows beyond 70%. 918 */ 919 static __always_inline int rhashtable_insert_fast( 920 struct rhashtable *ht, struct rhash_head *obj, 921 const struct rhashtable_params params) 922 { 923 void *ret; 924 925 ret = __rhashtable_insert_fast(ht, NULL, obj, params, false); 926 if (IS_ERR(ret)) 927 return PTR_ERR(ret); 928 929 return ret == NULL ? 0 : -EEXIST; 930 } 931 932 /** 933 * rhltable_insert_key - insert object into hash list table 934 * @hlt: hash list table 935 * @key: the pointer to the key 936 * @list: pointer to hash list head inside object 937 * @params: hash table parameters 938 * 939 * Will take the per bucket bitlock to protect against mutual mutations 940 * on the same bucket. Multiple insertions may occur in parallel unless 941 * they map to the same bucket. 942 * 943 * It is safe to call this function from atomic context. 944 * 945 * Will trigger an automatic deferred table resizing if residency in the 946 * table grows beyond 70%. 947 */ 948 static __always_inline int rhltable_insert_key( 949 struct rhltable *hlt, const void *key, struct rhlist_head *list, 950 const struct rhashtable_params params) 951 { 952 return PTR_ERR(__rhashtable_insert_fast(&hlt->ht, key, &list->rhead, 953 params, true)); 954 } 955 956 /** 957 * rhltable_insert - insert object into hash list table 958 * @hlt: hash list table 959 * @list: pointer to hash list head inside object 960 * @params: hash table parameters 961 * 962 * Will take the per bucket bitlock to protect against mutual mutations 963 * on the same bucket. Multiple insertions may occur in parallel unless 964 * they map to the same bucket. 965 * 966 * It is safe to call this function from atomic context. 967 * 968 * Will trigger an automatic deferred table resizing if residency in the 969 * table grows beyond 70%. 970 */ 971 static __always_inline int rhltable_insert( 972 struct rhltable *hlt, struct rhlist_head *list, 973 const struct rhashtable_params params) 974 { 975 const char *key = rht_obj(&hlt->ht, &list->rhead); 976 977 key += params.key_offset; 978 979 return rhltable_insert_key(hlt, key, list, params); 980 } 981 982 /** 983 * rhashtable_lookup_insert_fast - lookup and insert object into hash table 984 * @ht: hash table 985 * @obj: pointer to hash head inside object 986 * @params: hash table parameters 987 * 988 * This lookup function may only be used for fixed key hash table (key_len 989 * parameter set). It will BUG() if used inappropriately. 990 * 991 * It is safe to call this function from atomic context. 992 * 993 * Will trigger an automatic deferred table resizing if residency in the 994 * table grows beyond 70%. 995 */ 996 static __always_inline int rhashtable_lookup_insert_fast( 997 struct rhashtable *ht, struct rhash_head *obj, 998 const struct rhashtable_params params) 999 { 1000 const char *key = rht_obj(ht, obj); 1001 void *ret; 1002 1003 BUG_ON(ht->p.obj_hashfn); 1004 1005 ret = __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params, 1006 false); 1007 if (IS_ERR(ret)) 1008 return PTR_ERR(ret); 1009 1010 return ret == NULL ? 0 : -EEXIST; 1011 } 1012 1013 /** 1014 * rhashtable_lookup_get_insert_fast - lookup and insert object into hash table 1015 * @ht: hash table 1016 * @obj: pointer to hash head inside object 1017 * @params: hash table parameters 1018 * 1019 * Just like rhashtable_lookup_insert_fast(), but this function returns the 1020 * object if it exists, NULL if it did not and the insertion was successful, 1021 * and an ERR_PTR otherwise. 1022 */ 1023 static __always_inline void *rhashtable_lookup_get_insert_fast( 1024 struct rhashtable *ht, struct rhash_head *obj, 1025 const struct rhashtable_params params) 1026 { 1027 const char *key = rht_obj(ht, obj); 1028 1029 BUG_ON(ht->p.obj_hashfn); 1030 1031 return __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params, 1032 false); 1033 } 1034 1035 /** 1036 * rhashtable_lookup_insert_key - search and insert object to hash table 1037 * with explicit key 1038 * @ht: hash table 1039 * @key: key 1040 * @obj: pointer to hash head inside object 1041 * @params: hash table parameters 1042 * 1043 * Lookups may occur in parallel with hashtable mutations and resizing. 1044 * 1045 * Will trigger an automatic deferred table resizing if residency in the 1046 * table grows beyond 70%. 1047 * 1048 * Returns zero on success. 1049 */ 1050 static __always_inline int rhashtable_lookup_insert_key( 1051 struct rhashtable *ht, const void *key, struct rhash_head *obj, 1052 const struct rhashtable_params params) 1053 { 1054 void *ret; 1055 1056 BUG_ON(!ht->p.obj_hashfn || !key); 1057 1058 ret = __rhashtable_insert_fast(ht, key, obj, params, false); 1059 if (IS_ERR(ret)) 1060 return PTR_ERR(ret); 1061 1062 return ret == NULL ? 0 : -EEXIST; 1063 } 1064 1065 /** 1066 * rhashtable_lookup_get_insert_key - lookup and insert object into hash table 1067 * @ht: hash table 1068 * @key: key 1069 * @obj: pointer to hash head inside object 1070 * @params: hash table parameters 1071 * 1072 * Just like rhashtable_lookup_insert_key(), but this function returns the 1073 * object if it exists, NULL if it does not and the insertion was successful, 1074 * and an ERR_PTR otherwise. 1075 */ 1076 static __always_inline void *rhashtable_lookup_get_insert_key( 1077 struct rhashtable *ht, const void *key, struct rhash_head *obj, 1078 const struct rhashtable_params params) 1079 { 1080 BUG_ON(!ht->p.obj_hashfn || !key); 1081 1082 return __rhashtable_insert_fast(ht, key, obj, params, false); 1083 } 1084 1085 /* Internal function, please use rhashtable_remove_fast() instead */ 1086 static __always_inline int __rhashtable_remove_fast_one( 1087 struct rhashtable *ht, struct bucket_table *tbl, 1088 struct rhash_head *obj, const struct rhashtable_params params, 1089 bool rhlist) 1090 { 1091 struct rhash_lock_head __rcu **bkt; 1092 struct rhash_head __rcu **pprev; 1093 struct rhash_head *he; 1094 unsigned long flags; 1095 unsigned int hash; 1096 int err = -ENOENT; 1097 1098 hash = rht_head_hashfn(ht, tbl, obj, params); 1099 bkt = rht_bucket_var(tbl, hash); 1100 if (!bkt) 1101 return -ENOENT; 1102 pprev = NULL; 1103 flags = rht_lock(tbl, bkt); 1104 1105 rht_for_each_from(he, rht_ptr(bkt, tbl, hash), tbl, hash) { 1106 struct rhlist_head *list; 1107 1108 list = container_of(he, struct rhlist_head, rhead); 1109 1110 if (he != obj) { 1111 struct rhlist_head __rcu **lpprev; 1112 1113 pprev = &he->next; 1114 1115 if (!rhlist) 1116 continue; 1117 1118 do { 1119 lpprev = &list->next; 1120 list = rht_dereference_bucket(list->next, 1121 tbl, hash); 1122 } while (list && obj != &list->rhead); 1123 1124 if (!list) 1125 continue; 1126 1127 list = rht_dereference_bucket(list->next, tbl, hash); 1128 RCU_INIT_POINTER(*lpprev, list); 1129 err = 0; 1130 break; 1131 } 1132 1133 obj = rht_dereference_bucket(obj->next, tbl, hash); 1134 err = 1; 1135 1136 if (rhlist) { 1137 list = rht_dereference_bucket(list->next, tbl, hash); 1138 if (list) { 1139 RCU_INIT_POINTER(list->rhead.next, obj); 1140 obj = &list->rhead; 1141 err = 0; 1142 } 1143 } 1144 1145 if (pprev) { 1146 rcu_assign_pointer(*pprev, obj); 1147 rht_unlock(tbl, bkt, flags); 1148 } else { 1149 rht_assign_unlock(tbl, bkt, obj, flags); 1150 } 1151 goto unlocked; 1152 } 1153 1154 rht_unlock(tbl, bkt, flags); 1155 unlocked: 1156 if (err > 0) { 1157 atomic_dec(&ht->nelems); 1158 if (unlikely(ht->p.automatic_shrinking && 1159 rht_shrink_below_30(ht, tbl))) 1160 irq_work_queue(&ht->run_irq_work); 1161 err = 0; 1162 } 1163 1164 return err; 1165 } 1166 1167 /* Internal function, please use rhashtable_remove_fast() instead */ 1168 static __always_inline int __rhashtable_remove_fast( 1169 struct rhashtable *ht, struct rhash_head *obj, 1170 const struct rhashtable_params params, bool rhlist) 1171 { 1172 struct bucket_table *tbl; 1173 int err; 1174 1175 rcu_read_lock(); 1176 1177 tbl = rht_dereference_rcu(ht->tbl, ht); 1178 1179 /* Because we have already taken (and released) the bucket 1180 * lock in old_tbl, if we find that future_tbl is not yet 1181 * visible then that guarantees the entry to still be in 1182 * the old tbl if it exists. 1183 */ 1184 while ((err = __rhashtable_remove_fast_one(ht, tbl, obj, params, 1185 rhlist)) && 1186 (tbl = rht_dereference_rcu(tbl->future_tbl, ht))) 1187 ; 1188 1189 rcu_read_unlock(); 1190 1191 return err; 1192 } 1193 1194 /** 1195 * rhashtable_remove_fast - remove object from hash table 1196 * @ht: hash table 1197 * @obj: pointer to hash head inside object 1198 * @params: hash table parameters 1199 * 1200 * Since the hash chain is single linked, the removal operation needs to 1201 * walk the bucket chain upon removal. The removal operation is thus 1202 * considerable slow if the hash table is not correctly sized. 1203 * 1204 * Will automatically shrink the table if permitted when residency drops 1205 * below 30%. 1206 * 1207 * Returns zero on success, -ENOENT if the entry could not be found. 1208 */ 1209 static __always_inline int rhashtable_remove_fast( 1210 struct rhashtable *ht, struct rhash_head *obj, 1211 const struct rhashtable_params params) 1212 { 1213 return __rhashtable_remove_fast(ht, obj, params, false); 1214 } 1215 1216 /** 1217 * rhltable_remove - remove object from hash list table 1218 * @hlt: hash list table 1219 * @list: pointer to hash list head inside object 1220 * @params: hash table parameters 1221 * 1222 * Since the hash chain is single linked, the removal operation needs to 1223 * walk the bucket chain upon removal. The removal operation is thus 1224 * considerably slower if the hash table is not correctly sized. 1225 * 1226 * Will automatically shrink the table if permitted when residency drops 1227 * below 30% 1228 * 1229 * Returns zero on success, -ENOENT if the entry could not be found. 1230 */ 1231 static __always_inline int rhltable_remove( 1232 struct rhltable *hlt, struct rhlist_head *list, 1233 const struct rhashtable_params params) 1234 { 1235 return __rhashtable_remove_fast(&hlt->ht, &list->rhead, params, true); 1236 } 1237 1238 /* Internal function, please use rhashtable_replace_fast() instead */ 1239 static __always_inline int __rhashtable_replace_fast( 1240 struct rhashtable *ht, struct bucket_table *tbl, 1241 struct rhash_head *obj_old, struct rhash_head *obj_new, 1242 const struct rhashtable_params params) 1243 { 1244 struct rhash_lock_head __rcu **bkt; 1245 struct rhash_head __rcu **pprev; 1246 struct rhash_head *he; 1247 unsigned long flags; 1248 unsigned int hash; 1249 int err = -ENOENT; 1250 1251 /* Minimally, the old and new objects must have same hash 1252 * (which should mean identifiers are the same). 1253 */ 1254 hash = rht_head_hashfn(ht, tbl, obj_old, params); 1255 if (hash != rht_head_hashfn(ht, tbl, obj_new, params)) 1256 return -EINVAL; 1257 1258 bkt = rht_bucket_var(tbl, hash); 1259 if (!bkt) 1260 return -ENOENT; 1261 1262 pprev = NULL; 1263 flags = rht_lock(tbl, bkt); 1264 1265 rht_for_each_from(he, rht_ptr(bkt, tbl, hash), tbl, hash) { 1266 if (he != obj_old) { 1267 pprev = &he->next; 1268 continue; 1269 } 1270 1271 rcu_assign_pointer(obj_new->next, obj_old->next); 1272 if (pprev) { 1273 rcu_assign_pointer(*pprev, obj_new); 1274 rht_unlock(tbl, bkt, flags); 1275 } else { 1276 rht_assign_unlock(tbl, bkt, obj_new, flags); 1277 } 1278 err = 0; 1279 goto unlocked; 1280 } 1281 1282 rht_unlock(tbl, bkt, flags); 1283 1284 unlocked: 1285 return err; 1286 } 1287 1288 /** 1289 * rhashtable_replace_fast - replace an object in hash table 1290 * @ht: hash table 1291 * @obj_old: pointer to hash head inside object being replaced 1292 * @obj_new: pointer to hash head inside object which is new 1293 * @params: hash table parameters 1294 * 1295 * Replacing an object doesn't affect the number of elements in the hash table 1296 * or bucket, so we don't need to worry about shrinking or expanding the 1297 * table here. 1298 * 1299 * Returns zero on success, -ENOENT if the entry could not be found, 1300 * -EINVAL if hash is not the same for the old and new objects. 1301 */ 1302 static __always_inline int rhashtable_replace_fast( 1303 struct rhashtable *ht, struct rhash_head *obj_old, 1304 struct rhash_head *obj_new, 1305 const struct rhashtable_params params) 1306 { 1307 struct bucket_table *tbl; 1308 int err; 1309 1310 rcu_read_lock(); 1311 1312 tbl = rht_dereference_rcu(ht->tbl, ht); 1313 1314 /* Because we have already taken (and released) the bucket 1315 * lock in old_tbl, if we find that future_tbl is not yet 1316 * visible then that guarantees the entry to still be in 1317 * the old tbl if it exists. 1318 */ 1319 while ((err = __rhashtable_replace_fast(ht, tbl, obj_old, 1320 obj_new, params)) && 1321 (tbl = rht_dereference_rcu(tbl->future_tbl, ht))) 1322 ; 1323 1324 rcu_read_unlock(); 1325 1326 return err; 1327 } 1328 1329 /** 1330 * rhltable_walk_enter - Initialise an iterator 1331 * @hlt: Table to walk over 1332 * @iter: Hash table Iterator 1333 * 1334 * This function prepares a hash table walk. 1335 * 1336 * Note that if you restart a walk after rhashtable_walk_stop you 1337 * may see the same object twice. Also, you may miss objects if 1338 * there are removals in between rhashtable_walk_stop and the next 1339 * call to rhashtable_walk_start. 1340 * 1341 * For a completely stable walk you should construct your own data 1342 * structure outside the hash table. 1343 * 1344 * This function may be called from any process context, including 1345 * non-preemptable context, but cannot be called from softirq or 1346 * hardirq context. 1347 * 1348 * You must call rhashtable_walk_exit after this function returns. 1349 */ 1350 static inline void rhltable_walk_enter(struct rhltable *hlt, 1351 struct rhashtable_iter *iter) 1352 { 1353 rhashtable_walk_enter(&hlt->ht, iter); 1354 } 1355 1356 /** 1357 * rhltable_free_and_destroy - free elements and destroy hash list table 1358 * @hlt: the hash list table to destroy 1359 * @free_fn: callback to release resources of element 1360 * @arg: pointer passed to free_fn 1361 * 1362 * See documentation for rhashtable_free_and_destroy. 1363 */ 1364 static inline void rhltable_free_and_destroy(struct rhltable *hlt, 1365 void (*free_fn)(void *ptr, 1366 void *arg), 1367 void *arg) 1368 { 1369 rhashtable_free_and_destroy(&hlt->ht, free_fn, arg); 1370 } 1371 1372 static inline void rhltable_destroy(struct rhltable *hlt) 1373 { 1374 rhltable_free_and_destroy(hlt, NULL, NULL); 1375 } 1376 1377 #endif /* _LINUX_RHASHTABLE_H */ 1378