Linux Audio

Check our new training course

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