GNU Linux-libre 4.4.288-gnu1
[releases.git] / include / linux / rhashtable.h
1 /*
2  * Resizable, Scalable, Concurrent Hash Table
3  *
4  * Copyright (c) 2015 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
52 struct rhash_head {
53         struct rhash_head __rcu         *next;
54 };
55
56 /**
57  * struct bucket_table - Table of hash buckets
58  * @size: Number of hash buckets
59  * @rehash: Current bucket being rehashed
60  * @hash_rnd: Random seed to fold into hash
61  * @locks_mask: Mask to apply before accessing locks[]
62  * @locks: Array of spinlocks protecting individual buckets
63  * @walkers: List of active walkers
64  * @rcu: RCU structure for freeing the table
65  * @future_tbl: Table under construction during rehashing
66  * @buckets: size * hash buckets
67  */
68 struct bucket_table {
69         unsigned int            size;
70         unsigned int            rehash;
71         u32                     hash_rnd;
72         unsigned int            locks_mask;
73         spinlock_t              *locks;
74         struct list_head        walkers;
75         struct rcu_head         rcu;
76
77         struct bucket_table __rcu *future_tbl;
78
79         struct rhash_head __rcu *buckets[] ____cacheline_aligned_in_smp;
80 };
81
82 /**
83  * struct rhashtable_compare_arg - Key for the function rhashtable_compare
84  * @ht: Hash table
85  * @key: Key to compare against
86  */
87 struct rhashtable_compare_arg {
88         struct rhashtable *ht;
89         const void *key;
90 };
91
92 typedef u32 (*rht_hashfn_t)(const void *data, u32 len, u32 seed);
93 typedef u32 (*rht_obj_hashfn_t)(const void *data, u32 len, u32 seed);
94 typedef int (*rht_obj_cmpfn_t)(struct rhashtable_compare_arg *arg,
95                                const void *obj);
96
97 struct rhashtable;
98
99 /**
100  * struct rhashtable_params - Hash table construction parameters
101  * @nelem_hint: Hint on number of elements, should be 75% of desired size
102  * @key_len: Length of key
103  * @key_offset: Offset of key in struct to be hashed
104  * @head_offset: Offset of rhash_head in struct to be hashed
105  * @insecure_max_entries: Maximum number of entries (may be exceeded)
106  * @max_size: Maximum size while expanding
107  * @min_size: Minimum size while shrinking
108  * @nulls_base: Base value to generate nulls marker
109  * @insecure_elasticity: Set to true to disable chain length checks
110  * @automatic_shrinking: Enable automatic shrinking of tables
111  * @locks_mul: Number of bucket locks to allocate per cpu (default: 128)
112  * @hashfn: Hash function (default: jhash2 if !(key_len % 4), or jhash)
113  * @obj_hashfn: Function to hash object
114  * @obj_cmpfn: Function to compare key with object
115  */
116 struct rhashtable_params {
117         size_t                  nelem_hint;
118         size_t                  key_len;
119         size_t                  key_offset;
120         size_t                  head_offset;
121         unsigned int            insecure_max_entries;
122         unsigned int            max_size;
123         unsigned int            min_size;
124         u32                     nulls_base;
125         bool                    insecure_elasticity;
126         bool                    automatic_shrinking;
127         size_t                  locks_mul;
128         rht_hashfn_t            hashfn;
129         rht_obj_hashfn_t        obj_hashfn;
130         rht_obj_cmpfn_t         obj_cmpfn;
131 };
132
133 /**
134  * struct rhashtable - Hash table handle
135  * @tbl: Bucket table
136  * @key_len: Key length for hashfn
137  * @elasticity: Maximum chain length before rehash
138  * @p: Configuration parameters
139  * @run_work: Deferred worker to expand/shrink asynchronously
140  * @mutex: Mutex to protect current/future table swapping
141  * @lock: Spin lock to protect walker list
142  * @nelems: Number of elements in table
143  */
144 struct rhashtable {
145         struct bucket_table __rcu       *tbl;
146         unsigned int                    key_len;
147         unsigned int                    elasticity;
148         struct rhashtable_params        p;
149         struct work_struct              run_work;
150         struct mutex                    mutex;
151         spinlock_t                      lock;
152         atomic_t                        nelems;
153 };
154
155 /**
156  * struct rhashtable_walker - Hash table walker
157  * @list: List entry on list of walkers
158  * @tbl: The table that we were walking over
159  */
160 struct rhashtable_walker {
161         struct list_head list;
162         struct bucket_table *tbl;
163 };
164
165 /**
166  * struct rhashtable_iter - Hash table iterator, fits into netlink cb
167  * @ht: Table to iterate through
168  * @p: Current pointer
169  * @walker: Associated rhashtable walker
170  * @slot: Current slot
171  * @skip: Number of entries to skip in slot
172  */
173 struct rhashtable_iter {
174         struct rhashtable *ht;
175         struct rhash_head *p;
176         struct rhashtable_walker *walker;
177         unsigned int slot;
178         unsigned int skip;
179 };
180
181 static inline unsigned long rht_marker(const struct rhashtable *ht, u32 hash)
182 {
183         return NULLS_MARKER(ht->p.nulls_base + hash);
184 }
185
186 #define INIT_RHT_NULLS_HEAD(ptr, ht, hash) \
187         ((ptr) = (typeof(ptr)) rht_marker(ht, hash))
188
189 static inline bool rht_is_a_nulls(const struct rhash_head *ptr)
190 {
191         return ((unsigned long) ptr & 1);
192 }
193
194 static inline unsigned long rht_get_nulls_value(const struct rhash_head *ptr)
195 {
196         return ((unsigned long) ptr) >> 1;
197 }
198
199 static inline void *rht_obj(const struct rhashtable *ht,
200                             const struct rhash_head *he)
201 {
202         return (char *)he - ht->p.head_offset;
203 }
204
205 static inline unsigned int rht_bucket_index(const struct bucket_table *tbl,
206                                             unsigned int hash)
207 {
208         return (hash >> RHT_HASH_RESERVED_SPACE) & (tbl->size - 1);
209 }
210
211 static inline unsigned int rht_key_hashfn(
212         struct rhashtable *ht, const struct bucket_table *tbl,
213         const void *key, const struct rhashtable_params params)
214 {
215         unsigned int hash;
216
217         /* params must be equal to ht->p if it isn't constant. */
218         if (!__builtin_constant_p(params.key_len))
219                 hash = ht->p.hashfn(key, ht->key_len, tbl->hash_rnd);
220         else if (params.key_len) {
221                 unsigned int key_len = params.key_len;
222
223                 if (params.hashfn)
224                         hash = params.hashfn(key, key_len, tbl->hash_rnd);
225                 else if (key_len & (sizeof(u32) - 1))
226                         hash = jhash(key, key_len, tbl->hash_rnd);
227                 else
228                         hash = jhash2(key, key_len / sizeof(u32),
229                                       tbl->hash_rnd);
230         } else {
231                 unsigned int key_len = ht->p.key_len;
232
233                 if (params.hashfn)
234                         hash = params.hashfn(key, key_len, tbl->hash_rnd);
235                 else
236                         hash = jhash(key, key_len, tbl->hash_rnd);
237         }
238
239         return rht_bucket_index(tbl, hash);
240 }
241
242 static inline unsigned int rht_head_hashfn(
243         struct rhashtable *ht, const struct bucket_table *tbl,
244         const struct rhash_head *he, const struct rhashtable_params params)
245 {
246         const char *ptr = rht_obj(ht, he);
247
248         return likely(params.obj_hashfn) ?
249                rht_bucket_index(tbl, params.obj_hashfn(ptr, params.key_len ?:
250                                                             ht->p.key_len,
251                                                        tbl->hash_rnd)) :
252                rht_key_hashfn(ht, tbl, ptr + params.key_offset, params);
253 }
254
255 /**
256  * rht_grow_above_75 - returns true if nelems > 0.75 * table-size
257  * @ht:         hash table
258  * @tbl:        current table
259  */
260 static inline bool rht_grow_above_75(const struct rhashtable *ht,
261                                      const struct bucket_table *tbl)
262 {
263         /* Expand table when exceeding 75% load */
264         return atomic_read(&ht->nelems) > (tbl->size / 4 * 3) &&
265                (!ht->p.max_size || tbl->size < ht->p.max_size);
266 }
267
268 /**
269  * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size
270  * @ht:         hash table
271  * @tbl:        current table
272  */
273 static inline bool rht_shrink_below_30(const struct rhashtable *ht,
274                                        const struct bucket_table *tbl)
275 {
276         /* Shrink table beneath 30% load */
277         return atomic_read(&ht->nelems) < (tbl->size * 3 / 10) &&
278                tbl->size > ht->p.min_size;
279 }
280
281 /**
282  * rht_grow_above_100 - returns true if nelems > table-size
283  * @ht:         hash table
284  * @tbl:        current table
285  */
286 static inline bool rht_grow_above_100(const struct rhashtable *ht,
287                                       const struct bucket_table *tbl)
288 {
289         return atomic_read(&ht->nelems) > tbl->size &&
290                 (!ht->p.max_size || tbl->size < ht->p.max_size);
291 }
292
293 /**
294  * rht_grow_above_max - returns true if table is above maximum
295  * @ht:         hash table
296  * @tbl:        current table
297  */
298 static inline bool rht_grow_above_max(const struct rhashtable *ht,
299                                       const struct bucket_table *tbl)
300 {
301         return ht->p.insecure_max_entries &&
302                atomic_read(&ht->nelems) >= ht->p.insecure_max_entries;
303 }
304
305 /* The bucket lock is selected based on the hash and protects mutations
306  * on a group of hash buckets.
307  *
308  * A maximum of tbl->size/2 bucket locks is allocated. This ensures that
309  * a single lock always covers both buckets which may both contains
310  * entries which link to the same bucket of the old table during resizing.
311  * This allows to simplify the locking as locking the bucket in both
312  * tables during resize always guarantee protection.
313  *
314  * IMPORTANT: When holding the bucket lock of both the old and new table
315  * during expansions and shrinking, the old bucket lock must always be
316  * acquired first.
317  */
318 static inline spinlock_t *rht_bucket_lock(const struct bucket_table *tbl,
319                                           unsigned int hash)
320 {
321         return &tbl->locks[hash & tbl->locks_mask];
322 }
323
324 #ifdef CONFIG_PROVE_LOCKING
325 int lockdep_rht_mutex_is_held(struct rhashtable *ht);
326 int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash);
327 #else
328 static inline int lockdep_rht_mutex_is_held(struct rhashtable *ht)
329 {
330         return 1;
331 }
332
333 static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl,
334                                              u32 hash)
335 {
336         return 1;
337 }
338 #endif /* CONFIG_PROVE_LOCKING */
339
340 int rhashtable_init(struct rhashtable *ht,
341                     const struct rhashtable_params *params);
342
343 struct bucket_table *rhashtable_insert_slow(struct rhashtable *ht,
344                                             const void *key,
345                                             struct rhash_head *obj,
346                                             struct bucket_table *old_tbl,
347                                             void **data);
348 int rhashtable_insert_rehash(struct rhashtable *ht, struct bucket_table *tbl);
349
350 int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter);
351 void rhashtable_walk_exit(struct rhashtable_iter *iter);
352 int rhashtable_walk_start(struct rhashtable_iter *iter) __acquires(RCU);
353 void *rhashtable_walk_next(struct rhashtable_iter *iter);
354 void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU);
355
356 void rhashtable_free_and_destroy(struct rhashtable *ht,
357                                  void (*free_fn)(void *ptr, void *arg),
358                                  void *arg);
359 void rhashtable_destroy(struct rhashtable *ht);
360
361 #define rht_dereference(p, ht) \
362         rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht))
363
364 #define rht_dereference_rcu(p, ht) \
365         rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht))
366
367 #define rht_dereference_bucket(p, tbl, hash) \
368         rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash))
369
370 #define rht_dereference_bucket_rcu(p, tbl, hash) \
371         rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash))
372
373 #define rht_entry(tpos, pos, member) \
374         ({ tpos = container_of(pos, typeof(*tpos), member); 1; })
375
376 /**
377  * rht_for_each_continue - continue iterating over hash chain
378  * @pos:        the &struct rhash_head to use as a loop cursor.
379  * @head:       the previous &struct rhash_head to continue from
380  * @tbl:        the &struct bucket_table
381  * @hash:       the hash value / bucket index
382  */
383 #define rht_for_each_continue(pos, head, tbl, hash) \
384         for (pos = rht_dereference_bucket(head, tbl, hash); \
385              !rht_is_a_nulls(pos); \
386              pos = rht_dereference_bucket((pos)->next, tbl, hash))
387
388 /**
389  * rht_for_each - iterate over hash chain
390  * @pos:        the &struct rhash_head to use as a loop cursor.
391  * @tbl:        the &struct bucket_table
392  * @hash:       the hash value / bucket index
393  */
394 #define rht_for_each(pos, tbl, hash) \
395         rht_for_each_continue(pos, (tbl)->buckets[hash], tbl, hash)
396
397 /**
398  * rht_for_each_entry_continue - continue iterating over hash chain
399  * @tpos:       the type * to use as a loop cursor.
400  * @pos:        the &struct rhash_head to use as a loop cursor.
401  * @head:       the previous &struct rhash_head to continue from
402  * @tbl:        the &struct bucket_table
403  * @hash:       the hash value / bucket index
404  * @member:     name of the &struct rhash_head within the hashable struct.
405  */
406 #define rht_for_each_entry_continue(tpos, pos, head, tbl, hash, member) \
407         for (pos = rht_dereference_bucket(head, tbl, hash);             \
408              (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member);    \
409              pos = rht_dereference_bucket((pos)->next, tbl, hash))
410
411 /**
412  * rht_for_each_entry - iterate over hash chain of given type
413  * @tpos:       the type * to use as a loop cursor.
414  * @pos:        the &struct rhash_head to use as a loop cursor.
415  * @tbl:        the &struct bucket_table
416  * @hash:       the hash value / bucket index
417  * @member:     name of the &struct rhash_head within the hashable struct.
418  */
419 #define rht_for_each_entry(tpos, pos, tbl, hash, member)                \
420         rht_for_each_entry_continue(tpos, pos, (tbl)->buckets[hash],    \
421                                     tbl, hash, member)
422
423 /**
424  * rht_for_each_entry_safe - safely iterate over hash chain of given type
425  * @tpos:       the type * to use as a loop cursor.
426  * @pos:        the &struct rhash_head to use as a loop cursor.
427  * @next:       the &struct rhash_head to use as next in loop cursor.
428  * @tbl:        the &struct bucket_table
429  * @hash:       the hash value / bucket index
430  * @member:     name of the &struct rhash_head within the hashable struct.
431  *
432  * This hash chain list-traversal primitive allows for the looped code to
433  * remove the loop cursor from the list.
434  */
435 #define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member)         \
436         for (pos = rht_dereference_bucket((tbl)->buckets[hash], tbl, hash), \
437              next = !rht_is_a_nulls(pos) ?                                  \
438                        rht_dereference_bucket(pos->next, tbl, hash) : NULL; \
439              (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member);        \
440              pos = next,                                                    \
441              next = !rht_is_a_nulls(pos) ?                                  \
442                        rht_dereference_bucket(pos->next, tbl, hash) : NULL)
443
444 /**
445  * rht_for_each_rcu_continue - continue iterating over rcu hash chain
446  * @pos:        the &struct rhash_head to use as a loop cursor.
447  * @head:       the previous &struct rhash_head to continue from
448  * @tbl:        the &struct bucket_table
449  * @hash:       the hash value / bucket index
450  *
451  * This hash chain list-traversal primitive may safely run concurrently with
452  * the _rcu mutation primitives such as rhashtable_insert() as long as the
453  * traversal is guarded by rcu_read_lock().
454  */
455 #define rht_for_each_rcu_continue(pos, head, tbl, hash)                 \
456         for (({barrier(); }),                                           \
457              pos = rht_dereference_bucket_rcu(head, tbl, hash);         \
458              !rht_is_a_nulls(pos);                                      \
459              pos = rcu_dereference_raw(pos->next))
460
461 /**
462  * rht_for_each_rcu - iterate over rcu hash chain
463  * @pos:        the &struct rhash_head to use as a loop cursor.
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(pos, tbl, hash)                                \
472         rht_for_each_rcu_continue(pos, (tbl)->buckets[hash], tbl, hash)
473
474 /**
475  * rht_for_each_entry_rcu_continue - continue iterating over rcu hash chain
476  * @tpos:       the type * to use as a loop cursor.
477  * @pos:        the &struct rhash_head to use as a loop cursor.
478  * @head:       the previous &struct rhash_head to continue from
479  * @tbl:        the &struct bucket_table
480  * @hash:       the hash value / bucket index
481  * @member:     name of the &struct rhash_head within the hashable struct.
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_entry_rcu_continue(tpos, pos, head, tbl, hash, member) \
488         for (({barrier(); }),                                               \
489              pos = rht_dereference_bucket_rcu(head, tbl, hash);             \
490              (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member);        \
491              pos = rht_dereference_bucket_rcu(pos->next, tbl, hash))
492
493 /**
494  * rht_for_each_entry_rcu - iterate over rcu hash chain of given type
495  * @tpos:       the type * to use as a loop cursor.
496  * @pos:        the &struct rhash_head to use as a loop cursor.
497  * @tbl:        the &struct bucket_table
498  * @hash:       the hash value / bucket index
499  * @member:     name of the &struct rhash_head within the hashable struct.
500  *
501  * This hash chain list-traversal primitive may safely run concurrently with
502  * the _rcu mutation primitives such as rhashtable_insert() as long as the
503  * traversal is guarded by rcu_read_lock().
504  */
505 #define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member)            \
506         rht_for_each_entry_rcu_continue(tpos, pos, (tbl)->buckets[hash],\
507                                         tbl, hash, member)
508
509 static inline int rhashtable_compare(struct rhashtable_compare_arg *arg,
510                                      const void *obj)
511 {
512         struct rhashtable *ht = arg->ht;
513         const char *ptr = obj;
514
515         return memcmp(ptr + ht->p.key_offset, arg->key, ht->p.key_len);
516 }
517
518 /* Internal function, do not use. */
519 static inline struct rhash_head *__rhashtable_lookup(
520         struct rhashtable *ht, const void *key,
521         const struct rhashtable_params params)
522 {
523         struct rhashtable_compare_arg arg = {
524                 .ht = ht,
525                 .key = key,
526         };
527         const struct bucket_table *tbl;
528         struct rhash_head *he;
529         unsigned int hash;
530
531         tbl = rht_dereference_rcu(ht->tbl, ht);
532 restart:
533         hash = rht_key_hashfn(ht, tbl, key, params);
534         rht_for_each_rcu(he, tbl, hash) {
535                 if (params.obj_cmpfn ?
536                     params.obj_cmpfn(&arg, rht_obj(ht, he)) :
537                     rhashtable_compare(&arg, rht_obj(ht, he)))
538                         continue;
539                 return he;
540         }
541
542         /* Ensure we see any new tables. */
543         smp_rmb();
544
545         tbl = rht_dereference_rcu(tbl->future_tbl, ht);
546         if (unlikely(tbl))
547                 goto restart;
548
549         return NULL;
550 }
551
552 /**
553  * rhashtable_lookup - search hash table
554  * @ht:         hash table
555  * @key:        the pointer to the key
556  * @params:     hash table parameters
557  *
558  * Computes the hash value for the key and traverses the bucket chain looking
559  * for a entry with an identical key. The first matching entry is returned.
560  *
561  * This must only be called under the RCU read lock.
562  *
563  * Returns the first entry on which the compare function returned true.
564  */
565 static inline void *rhashtable_lookup(
566         struct rhashtable *ht, const void *key,
567         const struct rhashtable_params params)
568 {
569         struct rhash_head *he = __rhashtable_lookup(ht, key, params);
570
571         return he ? rht_obj(ht, he) : NULL;
572 }
573
574 /**
575  * rhashtable_lookup_fast - search hash table, without RCU read lock
576  * @ht:         hash table
577  * @key:        the pointer to the key
578  * @params:     hash table parameters
579  *
580  * Computes the hash value for the key and traverses the bucket chain looking
581  * for a entry with an identical key. The first matching entry is returned.
582  *
583  * Only use this function when you have other mechanisms guaranteeing
584  * that the object won't go away after the RCU read lock is released.
585  *
586  * Returns the first entry on which the compare function returned true.
587  */
588 static inline void *rhashtable_lookup_fast(
589         struct rhashtable *ht, const void *key,
590         const struct rhashtable_params params)
591 {
592         void *obj;
593
594         rcu_read_lock();
595         obj = rhashtable_lookup(ht, key, params);
596         rcu_read_unlock();
597
598         return obj;
599 }
600
601 /* Internal function, please use rhashtable_insert_fast() instead. This
602  * function returns the existing element already in hashes in there is a clash,
603  * otherwise it returns an error via ERR_PTR().
604  */
605 static inline void *__rhashtable_insert_fast(
606         struct rhashtable *ht, const void *key, struct rhash_head *obj,
607         const struct rhashtable_params params)
608 {
609         struct rhashtable_compare_arg arg = {
610                 .ht = ht,
611                 .key = key,
612         };
613         struct bucket_table *tbl, *new_tbl;
614         struct rhash_head *head;
615         spinlock_t *lock;
616         unsigned int elasticity;
617         unsigned int hash;
618         void *data = NULL;
619         int err;
620
621 restart:
622         rcu_read_lock();
623
624         tbl = rht_dereference_rcu(ht->tbl, ht);
625
626         /* All insertions must grab the oldest table containing
627          * the hashed bucket that is yet to be rehashed.
628          */
629         for (;;) {
630                 hash = rht_head_hashfn(ht, tbl, obj, params);
631                 lock = rht_bucket_lock(tbl, hash);
632                 spin_lock_bh(lock);
633
634                 if (tbl->rehash <= hash)
635                         break;
636
637                 spin_unlock_bh(lock);
638                 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
639         }
640
641         new_tbl = rht_dereference_rcu(tbl->future_tbl, ht);
642         if (unlikely(new_tbl)) {
643                 tbl = rhashtable_insert_slow(ht, key, obj, new_tbl, &data);
644                 if (!IS_ERR_OR_NULL(tbl))
645                         goto slow_path;
646
647                 err = PTR_ERR(tbl);
648                 if (err == -EEXIST)
649                         err = 0;
650
651                 goto out;
652         }
653
654         err = -E2BIG;
655         if (unlikely(rht_grow_above_max(ht, tbl)))
656                 goto out;
657
658         if (unlikely(rht_grow_above_100(ht, tbl))) {
659 slow_path:
660                 spin_unlock_bh(lock);
661                 err = rhashtable_insert_rehash(ht, tbl);
662                 rcu_read_unlock();
663                 if (err)
664                         return ERR_PTR(err);
665
666                 goto restart;
667         }
668
669         err = 0;
670         elasticity = ht->elasticity;
671         rht_for_each(head, tbl, hash) {
672                 if (key &&
673                     unlikely(!(params.obj_cmpfn ?
674                                params.obj_cmpfn(&arg, rht_obj(ht, head)) :
675                                rhashtable_compare(&arg, rht_obj(ht, head))))) {
676                         data = rht_obj(ht, head);
677                         goto out;
678                 }
679                 if (!--elasticity)
680                         goto slow_path;
681         }
682
683         head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash);
684
685         RCU_INIT_POINTER(obj->next, head);
686
687         rcu_assign_pointer(tbl->buckets[hash], obj);
688
689         atomic_inc(&ht->nelems);
690         if (rht_grow_above_75(ht, tbl))
691                 schedule_work(&ht->run_work);
692
693 out:
694         spin_unlock_bh(lock);
695         rcu_read_unlock();
696
697         return err ? ERR_PTR(err) : data;
698 }
699
700 /**
701  * rhashtable_insert_fast - insert object into hash table
702  * @ht:         hash table
703  * @obj:        pointer to hash head inside object
704  * @params:     hash table parameters
705  *
706  * Will take a per bucket spinlock to protect against mutual mutations
707  * on the same bucket. Multiple insertions may occur in parallel unless
708  * they map to the same bucket lock.
709  *
710  * It is safe to call this function from atomic context.
711  *
712  * Will trigger an automatic deferred table resizing if the size grows
713  * beyond the watermark indicated by grow_decision() which can be passed
714  * to rhashtable_init().
715  */
716 static inline int rhashtable_insert_fast(
717         struct rhashtable *ht, struct rhash_head *obj,
718         const struct rhashtable_params params)
719 {
720         void *ret;
721
722         ret = __rhashtable_insert_fast(ht, NULL, obj, params);
723         if (IS_ERR(ret))
724                 return PTR_ERR(ret);
725
726         return ret == NULL ? 0 : -EEXIST;
727 }
728
729 /**
730  * rhashtable_lookup_insert_fast - lookup and insert object into hash table
731  * @ht:         hash table
732  * @obj:        pointer to hash head inside object
733  * @params:     hash table parameters
734  *
735  * Locks down the bucket chain in both the old and new table if a resize
736  * is in progress to ensure that writers can't remove from the old table
737  * and can't insert to the new table during the atomic operation of search
738  * and insertion. Searches for duplicates in both the old and new table if
739  * a resize is in progress.
740  *
741  * This lookup function may only be used for fixed key hash table (key_len
742  * parameter set). It will BUG() if used inappropriately.
743  *
744  * It is safe to call this function from atomic context.
745  *
746  * Will trigger an automatic deferred table resizing if the size grows
747  * beyond the watermark indicated by grow_decision() which can be passed
748  * to rhashtable_init().
749  */
750 static inline int rhashtable_lookup_insert_fast(
751         struct rhashtable *ht, struct rhash_head *obj,
752         const struct rhashtable_params params)
753 {
754         const char *key = rht_obj(ht, obj);
755         void *ret;
756
757         BUG_ON(ht->p.obj_hashfn);
758
759         ret = __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params);
760         if (IS_ERR(ret))
761                 return PTR_ERR(ret);
762
763         return ret == NULL ? 0 : -EEXIST;
764 }
765
766 /**
767  * rhashtable_lookup_insert_key - search and insert object to hash table
768  *                                with explicit key
769  * @ht:         hash table
770  * @key:        key
771  * @obj:        pointer to hash head inside object
772  * @params:     hash table parameters
773  *
774  * Locks down the bucket chain in both the old and new table if a resize
775  * is in progress to ensure that writers can't remove from the old table
776  * and can't insert to the new table during the atomic operation of search
777  * and insertion. Searches for duplicates in both the old and new table if
778  * a resize is in progress.
779  *
780  * Lookups may occur in parallel with hashtable mutations and resizing.
781  *
782  * Will trigger an automatic deferred table resizing if the size grows
783  * beyond the watermark indicated by grow_decision() which can be passed
784  * to rhashtable_init().
785  *
786  * Returns zero on success.
787  */
788 static inline int rhashtable_lookup_insert_key(
789         struct rhashtable *ht, const void *key, struct rhash_head *obj,
790         const struct rhashtable_params params)
791 {
792         void *ret;
793
794         BUG_ON(!ht->p.obj_hashfn || !key);
795
796         ret = __rhashtable_insert_fast(ht, key, obj, params);
797         if (IS_ERR(ret))
798                 return PTR_ERR(ret);
799
800         return ret == NULL ? 0 : -EEXIST;
801 }
802
803 /**
804  * rhashtable_lookup_get_insert_key - lookup and insert object into hash table
805  * @ht:         hash table
806  * @obj:        pointer to hash head inside object
807  * @params:     hash table parameters
808  * @data:       pointer to element data already in hashes
809  *
810  * Just like rhashtable_lookup_insert_key(), but this function returns the
811  * object if it exists, NULL if it does not and the insertion was successful,
812  * and an ERR_PTR otherwise.
813  */
814 static inline void *rhashtable_lookup_get_insert_key(
815         struct rhashtable *ht, const void *key, struct rhash_head *obj,
816         const struct rhashtable_params params)
817 {
818         BUG_ON(!ht->p.obj_hashfn || !key);
819
820         return __rhashtable_insert_fast(ht, key, obj, params);
821 }
822
823 /* Internal function, please use rhashtable_remove_fast() instead */
824 static inline int __rhashtable_remove_fast(
825         struct rhashtable *ht, struct bucket_table *tbl,
826         struct rhash_head *obj, const struct rhashtable_params params)
827 {
828         struct rhash_head __rcu **pprev;
829         struct rhash_head *he;
830         spinlock_t * lock;
831         unsigned int hash;
832         int err = -ENOENT;
833
834         hash = rht_head_hashfn(ht, tbl, obj, params);
835         lock = rht_bucket_lock(tbl, hash);
836
837         spin_lock_bh(lock);
838
839         pprev = &tbl->buckets[hash];
840         rht_for_each(he, tbl, hash) {
841                 if (he != obj) {
842                         pprev = &he->next;
843                         continue;
844                 }
845
846                 rcu_assign_pointer(*pprev, obj->next);
847                 err = 0;
848                 break;
849         }
850
851         spin_unlock_bh(lock);
852
853         return err;
854 }
855
856 /**
857  * rhashtable_remove_fast - remove object from hash table
858  * @ht:         hash table
859  * @obj:        pointer to hash head inside object
860  * @params:     hash table parameters
861  *
862  * Since the hash chain is single linked, the removal operation needs to
863  * walk the bucket chain upon removal. The removal operation is thus
864  * considerable slow if the hash table is not correctly sized.
865  *
866  * Will automatically shrink the table via rhashtable_expand() if the
867  * shrink_decision function specified at rhashtable_init() returns true.
868  *
869  * Returns zero on success, -ENOENT if the entry could not be found.
870  */
871 static inline int rhashtable_remove_fast(
872         struct rhashtable *ht, struct rhash_head *obj,
873         const struct rhashtable_params params)
874 {
875         struct bucket_table *tbl;
876         int err;
877
878         rcu_read_lock();
879
880         tbl = rht_dereference_rcu(ht->tbl, ht);
881
882         /* Because we have already taken (and released) the bucket
883          * lock in old_tbl, if we find that future_tbl is not yet
884          * visible then that guarantees the entry to still be in
885          * the old tbl if it exists.
886          */
887         while ((err = __rhashtable_remove_fast(ht, tbl, obj, params)) &&
888                (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
889                 ;
890
891         if (err)
892                 goto out;
893
894         atomic_dec(&ht->nelems);
895         if (unlikely(ht->p.automatic_shrinking &&
896                      rht_shrink_below_30(ht, tbl)))
897                 schedule_work(&ht->run_work);
898
899 out:
900         rcu_read_unlock();
901
902         return err;
903 }
904
905 #endif /* _LINUX_RHASHTABLE_H */