]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - include/linux/rhashtable.h
rhashtable: Add barrier to ensure we see new tables in walker
[mirror_ubuntu-bionic-kernel.git] / include / linux / rhashtable.h
CommitLineData
7e1e7763
TG
1/*
2 * Resizable, Scalable, Concurrent Hash Table
3 *
dc0ee268 4 * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
7e1e7763
TG
5 * Copyright (c) 2014 Thomas Graf <tgraf@suug.ch>
6 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
7 *
7e1e7763 8 * Code partially derived from nft_hash
dc0ee268
HX
9 * Rewritten with rehash code from br_multicast plus single list
10 * pointer as suggested by Josh Triplett
7e1e7763
TG
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
f2dba9c6 20#include <linux/compiler.h>
6626af69 21#include <linux/errno.h>
f89bd6f8 22#include <linux/list_nulls.h>
97defe1e 23#include <linux/workqueue.h>
86b35b64 24#include <linux/mutex.h>
02fd97c3 25#include <linux/rcupdate.h>
7e1e7763 26
f89bd6f8
TG
27/*
28 * The end of the chain is marked with a special nulls marks which has
29 * the following format:
30 *
31 * +-------+-----------------------------------------------------+-+
32 * | Base | Hash |1|
33 * +-------+-----------------------------------------------------+-+
34 *
35 * Base (4 bits) : Reserved to distinguish between multiple tables.
36 * Specified via &struct rhashtable_params.nulls_base.
37 * Hash (27 bits): Full hash (unmasked) of first element added to bucket
38 * 1 (1 bit) : Nulls marker (always set)
39 *
40 * The remaining bits of the next pointer remain unused for now.
41 */
42#define RHT_BASE_BITS 4
43#define RHT_HASH_BITS 27
44#define RHT_BASE_SHIFT RHT_HASH_BITS
45
02fd97c3
HX
46/* Base bits plus 1 bit for nulls marker */
47#define RHT_HASH_RESERVED_SPACE (RHT_BASE_BITS + 1)
48
7e1e7763 49struct rhash_head {
5300fdcb 50 struct rhash_head __rcu *next;
7e1e7763
TG
51};
52
97defe1e
TG
53/**
54 * struct bucket_table - Table of hash buckets
55 * @size: Number of hash buckets
63d512d0 56 * @rehash: Current bucket being rehashed
988dfbd7 57 * @hash_rnd: Random seed to fold into hash
97defe1e
TG
58 * @locks_mask: Mask to apply before accessing locks[]
59 * @locks: Array of spinlocks protecting individual buckets
eddee5ba 60 * @walkers: List of active walkers
9d901bc0 61 * @rcu: RCU structure for freeing the table
c4db8848 62 * @future_tbl: Table under construction during rehashing
97defe1e
TG
63 * @buckets: size * hash buckets
64 */
7e1e7763 65struct bucket_table {
63d512d0
HX
66 unsigned int size;
67 unsigned int rehash;
988dfbd7 68 u32 hash_rnd;
b9ebafbe
ED
69 unsigned int locks_mask;
70 spinlock_t *locks;
eddee5ba 71 struct list_head walkers;
9d901bc0 72 struct rcu_head rcu;
b9ebafbe 73
c4db8848
HX
74 struct bucket_table __rcu *future_tbl;
75
b9ebafbe 76 struct rhash_head __rcu *buckets[] ____cacheline_aligned_in_smp;
7e1e7763
TG
77};
78
02fd97c3
HX
79/**
80 * struct rhashtable_compare_arg - Key for the function rhashtable_compare
81 * @ht: Hash table
82 * @key: Key to compare against
83 */
84struct rhashtable_compare_arg {
85 struct rhashtable *ht;
86 const void *key;
87};
88
7e1e7763
TG
89typedef u32 (*rht_hashfn_t)(const void *data, u32 len, u32 seed);
90typedef u32 (*rht_obj_hashfn_t)(const void *data, u32 seed);
02fd97c3
HX
91typedef int (*rht_obj_cmpfn_t)(struct rhashtable_compare_arg *arg,
92 const void *obj);
7e1e7763
TG
93
94struct rhashtable;
95
96/**
97 * struct rhashtable_params - Hash table construction parameters
98 * @nelem_hint: Hint on number of elements, should be 75% of desired size
99 * @key_len: Length of key
100 * @key_offset: Offset of key in struct to be hashed
101 * @head_offset: Offset of rhash_head in struct to be hashed
c2e213cf
HX
102 * @max_size: Maximum size while expanding
103 * @min_size: Minimum size while shrinking
f89bd6f8 104 * @nulls_base: Base value to generate nulls marker
97defe1e 105 * @locks_mul: Number of bucket locks to allocate per cpu (default: 128)
7e1e7763
TG
106 * @hashfn: Function to hash key
107 * @obj_hashfn: Function to hash object
02fd97c3 108 * @obj_cmpfn: Function to compare key with object
7e1e7763
TG
109 */
110struct rhashtable_params {
111 size_t nelem_hint;
112 size_t key_len;
113 size_t key_offset;
114 size_t head_offset;
c2e213cf
HX
115 unsigned int max_size;
116 unsigned int min_size;
f89bd6f8 117 u32 nulls_base;
97defe1e 118 size_t locks_mul;
7e1e7763
TG
119 rht_hashfn_t hashfn;
120 rht_obj_hashfn_t obj_hashfn;
02fd97c3 121 rht_obj_cmpfn_t obj_cmpfn;
7e1e7763
TG
122};
123
124/**
125 * struct rhashtable - Hash table handle
126 * @tbl: Bucket table
127 * @nelems: Number of elements in table
7e1e7763 128 * @p: Configuration parameters
97defe1e
TG
129 * @run_work: Deferred worker to expand/shrink asynchronously
130 * @mutex: Mutex to protect current/future table swapping
131 * @being_destroyed: True if table is set up for destruction
7e1e7763
TG
132 */
133struct rhashtable {
134 struct bucket_table __rcu *tbl;
97defe1e 135 atomic_t nelems;
a5b6846f 136 bool being_destroyed;
7e1e7763 137 struct rhashtable_params p;
57699a40 138 struct work_struct run_work;
97defe1e 139 struct mutex mutex;
7e1e7763
TG
140};
141
f2dba9c6
HX
142/**
143 * struct rhashtable_walker - Hash table walker
144 * @list: List entry on list of walkers
eddee5ba 145 * @tbl: The table that we were walking over
f2dba9c6
HX
146 */
147struct rhashtable_walker {
148 struct list_head list;
eddee5ba 149 struct bucket_table *tbl;
f2dba9c6
HX
150};
151
152/**
153 * struct rhashtable_iter - Hash table iterator, fits into netlink cb
154 * @ht: Table to iterate through
155 * @p: Current pointer
156 * @walker: Associated rhashtable walker
157 * @slot: Current slot
158 * @skip: Number of entries to skip in slot
159 */
160struct rhashtable_iter {
161 struct rhashtable *ht;
162 struct rhash_head *p;
163 struct rhashtable_walker *walker;
164 unsigned int slot;
165 unsigned int skip;
166};
167
f89bd6f8
TG
168static inline unsigned long rht_marker(const struct rhashtable *ht, u32 hash)
169{
170 return NULLS_MARKER(ht->p.nulls_base + hash);
171}
172
173#define INIT_RHT_NULLS_HEAD(ptr, ht, hash) \
174 ((ptr) = (typeof(ptr)) rht_marker(ht, hash))
175
176static inline bool rht_is_a_nulls(const struct rhash_head *ptr)
177{
178 return ((unsigned long) ptr & 1);
179}
180
181static inline unsigned long rht_get_nulls_value(const struct rhash_head *ptr)
182{
183 return ((unsigned long) ptr) >> 1;
184}
185
02fd97c3
HX
186static inline void *rht_obj(const struct rhashtable *ht,
187 const struct rhash_head *he)
188{
189 return (char *)he - ht->p.head_offset;
190}
191
192static inline unsigned int rht_bucket_index(const struct bucket_table *tbl,
193 unsigned int hash)
194{
195 return (hash >> RHT_HASH_RESERVED_SPACE) & (tbl->size - 1);
196}
197
198static inline unsigned int rht_key_hashfn(
199 struct rhashtable *ht, const struct bucket_table *tbl,
200 const void *key, const struct rhashtable_params params)
201{
202 return rht_bucket_index(tbl, params.hashfn(key, params.key_len ?:
203 ht->p.key_len,
204 tbl->hash_rnd));
205}
206
207static inline unsigned int rht_head_hashfn(
208 struct rhashtable *ht, const struct bucket_table *tbl,
209 const struct rhash_head *he, const struct rhashtable_params params)
210{
211 const char *ptr = rht_obj(ht, he);
212
213 return likely(params.obj_hashfn) ?
214 rht_bucket_index(tbl, params.obj_hashfn(ptr, tbl->hash_rnd)) :
215 rht_key_hashfn(ht, tbl, ptr + params.key_offset, params);
216}
217
218/**
219 * rht_grow_above_75 - returns true if nelems > 0.75 * table-size
220 * @ht: hash table
221 * @tbl: current table
222 */
223static inline bool rht_grow_above_75(const struct rhashtable *ht,
224 const struct bucket_table *tbl)
225{
226 /* Expand table when exceeding 75% load */
227 return atomic_read(&ht->nelems) > (tbl->size / 4 * 3) &&
228 (!ht->p.max_size || tbl->size < ht->p.max_size);
229}
230
231/**
232 * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size
233 * @ht: hash table
234 * @tbl: current table
235 */
236static inline bool rht_shrink_below_30(const struct rhashtable *ht,
237 const struct bucket_table *tbl)
238{
239 /* Shrink table beneath 30% load */
240 return atomic_read(&ht->nelems) < (tbl->size * 3 / 10) &&
241 tbl->size > ht->p.min_size;
242}
243
244/* The bucket lock is selected based on the hash and protects mutations
245 * on a group of hash buckets.
246 *
247 * A maximum of tbl->size/2 bucket locks is allocated. This ensures that
248 * a single lock always covers both buckets which may both contains
249 * entries which link to the same bucket of the old table during resizing.
250 * This allows to simplify the locking as locking the bucket in both
251 * tables during resize always guarantee protection.
252 *
253 * IMPORTANT: When holding the bucket lock of both the old and new table
254 * during expansions and shrinking, the old bucket lock must always be
255 * acquired first.
256 */
257static inline spinlock_t *rht_bucket_lock(const struct bucket_table *tbl,
258 unsigned int hash)
259{
260 return &tbl->locks[hash & tbl->locks_mask];
261}
262
7e1e7763 263#ifdef CONFIG_PROVE_LOCKING
97defe1e 264int lockdep_rht_mutex_is_held(struct rhashtable *ht);
88d6ed15 265int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash);
7e1e7763 266#else
97defe1e 267static inline int lockdep_rht_mutex_is_held(struct rhashtable *ht)
7e1e7763
TG
268{
269 return 1;
270}
88d6ed15
TG
271
272static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl,
273 u32 hash)
274{
275 return 1;
276}
7e1e7763
TG
277#endif /* CONFIG_PROVE_LOCKING */
278
488fb86e
HX
279int rhashtable_init(struct rhashtable *ht,
280 const struct rhashtable_params *params);
7e1e7763 281
02fd97c3
HX
282int rhashtable_insert_slow(struct rhashtable *ht, const void *key,
283 struct rhash_head *obj,
284 struct bucket_table *old_tbl);
7e1e7763 285
6eba8224
TG
286int rhashtable_expand(struct rhashtable *ht);
287int rhashtable_shrink(struct rhashtable *ht);
7e1e7763 288
f2dba9c6
HX
289int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter);
290void rhashtable_walk_exit(struct rhashtable_iter *iter);
291int rhashtable_walk_start(struct rhashtable_iter *iter) __acquires(RCU);
292void *rhashtable_walk_next(struct rhashtable_iter *iter);
293void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU);
294
97defe1e 295void rhashtable_destroy(struct rhashtable *ht);
7e1e7763
TG
296
297#define rht_dereference(p, ht) \
298 rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht))
299
300#define rht_dereference_rcu(p, ht) \
301 rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht))
302
88d6ed15
TG
303#define rht_dereference_bucket(p, tbl, hash) \
304 rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash))
7e1e7763 305
88d6ed15
TG
306#define rht_dereference_bucket_rcu(p, tbl, hash) \
307 rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash))
308
309#define rht_entry(tpos, pos, member) \
310 ({ tpos = container_of(pos, typeof(*tpos), member); 1; })
7e1e7763
TG
311
312/**
88d6ed15
TG
313 * rht_for_each_continue - continue iterating over hash chain
314 * @pos: the &struct rhash_head to use as a loop cursor.
315 * @head: the previous &struct rhash_head to continue from
316 * @tbl: the &struct bucket_table
317 * @hash: the hash value / bucket index
7e1e7763 318 */
88d6ed15
TG
319#define rht_for_each_continue(pos, head, tbl, hash) \
320 for (pos = rht_dereference_bucket(head, tbl, hash); \
f89bd6f8 321 !rht_is_a_nulls(pos); \
88d6ed15
TG
322 pos = rht_dereference_bucket((pos)->next, tbl, hash))
323
324/**
325 * rht_for_each - iterate over hash chain
326 * @pos: the &struct rhash_head to use as a loop cursor.
327 * @tbl: the &struct bucket_table
328 * @hash: the hash value / bucket index
329 */
330#define rht_for_each(pos, tbl, hash) \
331 rht_for_each_continue(pos, (tbl)->buckets[hash], tbl, hash)
332
333/**
334 * rht_for_each_entry_continue - continue iterating over hash chain
335 * @tpos: the type * to use as a loop cursor.
336 * @pos: the &struct rhash_head to use as a loop cursor.
337 * @head: the previous &struct rhash_head to continue from
338 * @tbl: the &struct bucket_table
339 * @hash: the hash value / bucket index
340 * @member: name of the &struct rhash_head within the hashable struct.
341 */
342#define rht_for_each_entry_continue(tpos, pos, head, tbl, hash, member) \
343 for (pos = rht_dereference_bucket(head, tbl, hash); \
f89bd6f8 344 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
88d6ed15 345 pos = rht_dereference_bucket((pos)->next, tbl, hash))
7e1e7763
TG
346
347/**
348 * rht_for_each_entry - iterate over hash chain of given type
88d6ed15
TG
349 * @tpos: the type * to use as a loop cursor.
350 * @pos: the &struct rhash_head to use as a loop cursor.
351 * @tbl: the &struct bucket_table
352 * @hash: the hash value / bucket index
353 * @member: name of the &struct rhash_head within the hashable struct.
7e1e7763 354 */
88d6ed15
TG
355#define rht_for_each_entry(tpos, pos, tbl, hash, member) \
356 rht_for_each_entry_continue(tpos, pos, (tbl)->buckets[hash], \
357 tbl, hash, member)
7e1e7763
TG
358
359/**
360 * rht_for_each_entry_safe - safely iterate over hash chain of given type
88d6ed15
TG
361 * @tpos: the type * to use as a loop cursor.
362 * @pos: the &struct rhash_head to use as a loop cursor.
363 * @next: the &struct rhash_head to use as next in loop cursor.
364 * @tbl: the &struct bucket_table
365 * @hash: the hash value / bucket index
366 * @member: name of the &struct rhash_head within the hashable struct.
7e1e7763
TG
367 *
368 * This hash chain list-traversal primitive allows for the looped code to
369 * remove the loop cursor from the list.
370 */
88d6ed15
TG
371#define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \
372 for (pos = rht_dereference_bucket((tbl)->buckets[hash], tbl, hash), \
f89bd6f8
TG
373 next = !rht_is_a_nulls(pos) ? \
374 rht_dereference_bucket(pos->next, tbl, hash) : NULL; \
375 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
607954b0
PM
376 pos = next, \
377 next = !rht_is_a_nulls(pos) ? \
378 rht_dereference_bucket(pos->next, tbl, hash) : NULL)
88d6ed15
TG
379
380/**
381 * rht_for_each_rcu_continue - continue iterating over rcu hash chain
382 * @pos: the &struct rhash_head to use as a loop cursor.
383 * @head: the previous &struct rhash_head to continue from
384 * @tbl: the &struct bucket_table
385 * @hash: the hash value / bucket index
386 *
387 * This hash chain list-traversal primitive may safely run concurrently with
388 * the _rcu mutation primitives such as rhashtable_insert() as long as the
389 * traversal is guarded by rcu_read_lock().
390 */
391#define rht_for_each_rcu_continue(pos, head, tbl, hash) \
392 for (({barrier(); }), \
393 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
f89bd6f8 394 !rht_is_a_nulls(pos); \
88d6ed15 395 pos = rcu_dereference_raw(pos->next))
7e1e7763
TG
396
397/**
398 * rht_for_each_rcu - iterate over rcu hash chain
88d6ed15
TG
399 * @pos: the &struct rhash_head to use as a loop cursor.
400 * @tbl: the &struct bucket_table
401 * @hash: the hash value / bucket index
7e1e7763
TG
402 *
403 * This hash chain list-traversal primitive may safely run concurrently with
88d6ed15 404 * the _rcu mutation primitives such as rhashtable_insert() as long as the
7e1e7763
TG
405 * traversal is guarded by rcu_read_lock().
406 */
88d6ed15
TG
407#define rht_for_each_rcu(pos, tbl, hash) \
408 rht_for_each_rcu_continue(pos, (tbl)->buckets[hash], tbl, hash)
409
410/**
411 * rht_for_each_entry_rcu_continue - continue iterating over rcu hash chain
412 * @tpos: the type * to use as a loop cursor.
413 * @pos: the &struct rhash_head to use as a loop cursor.
414 * @head: the previous &struct rhash_head to continue from
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 * This hash chain list-traversal primitive may safely run concurrently with
420 * the _rcu mutation primitives such as rhashtable_insert() as long as the
421 * traversal is guarded by rcu_read_lock().
422 */
423#define rht_for_each_entry_rcu_continue(tpos, pos, head, tbl, hash, member) \
424 for (({barrier(); }), \
425 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
f89bd6f8 426 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
88d6ed15 427 pos = rht_dereference_bucket_rcu(pos->next, tbl, hash))
7e1e7763
TG
428
429/**
430 * rht_for_each_entry_rcu - iterate over rcu hash chain of given type
88d6ed15
TG
431 * @tpos: the type * to use as a loop cursor.
432 * @pos: the &struct rhash_head to use as a loop cursor.
433 * @tbl: the &struct bucket_table
434 * @hash: the hash value / bucket index
435 * @member: name of the &struct rhash_head within the hashable struct.
7e1e7763
TG
436 *
437 * This hash chain list-traversal primitive may safely run concurrently with
88d6ed15 438 * the _rcu mutation primitives such as rhashtable_insert() as long as the
7e1e7763
TG
439 * traversal is guarded by rcu_read_lock().
440 */
88d6ed15
TG
441#define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \
442 rht_for_each_entry_rcu_continue(tpos, pos, (tbl)->buckets[hash],\
443 tbl, hash, member)
7e1e7763 444
02fd97c3
HX
445static inline int rhashtable_compare(struct rhashtable_compare_arg *arg,
446 const void *obj)
447{
448 struct rhashtable *ht = arg->ht;
449 const char *ptr = obj;
450
451 return memcmp(ptr + ht->p.key_offset, arg->key, ht->p.key_len);
452}
453
454/**
455 * rhashtable_lookup_fast - search hash table, inlined version
456 * @ht: hash table
457 * @key: the pointer to the key
458 * @params: hash table parameters
459 *
460 * Computes the hash value for the key and traverses the bucket chain looking
461 * for a entry with an identical key. The first matching entry is returned.
462 *
463 * Returns the first entry on which the compare function returned true.
464 */
465static inline void *rhashtable_lookup_fast(
466 struct rhashtable *ht, const void *key,
467 const struct rhashtable_params params)
468{
469 struct rhashtable_compare_arg arg = {
470 .ht = ht,
471 .key = key,
472 };
473 const struct bucket_table *tbl;
474 struct rhash_head *he;
475 unsigned hash;
476
477 rcu_read_lock();
478
479 tbl = rht_dereference_rcu(ht->tbl, ht);
480restart:
481 hash = rht_key_hashfn(ht, tbl, key, params);
482 rht_for_each_rcu(he, tbl, hash) {
483 if (params.obj_cmpfn ?
484 params.obj_cmpfn(&arg, rht_obj(ht, he)) :
485 rhashtable_compare(&arg, rht_obj(ht, he)))
486 continue;
487 rcu_read_unlock();
488 return rht_obj(ht, he);
489 }
490
491 /* Ensure we see any new tables. */
492 smp_rmb();
493
494 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
495 if (unlikely(tbl))
496 goto restart;
497 rcu_read_unlock();
498
499 return NULL;
500}
501
502static inline int __rhashtable_insert_fast(
503 struct rhashtable *ht, const void *key, struct rhash_head *obj,
504 const struct rhashtable_params params)
505{
506 struct rhashtable_compare_arg arg = {
507 .ht = ht,
508 .key = key,
509 };
510 int err = -EEXIST;
511 struct bucket_table *tbl, *new_tbl;
512 struct rhash_head *head;
513 spinlock_t *lock;
514 unsigned hash;
515
516 rcu_read_lock();
517
518 tbl = rht_dereference_rcu(ht->tbl, ht);
519 hash = rht_head_hashfn(ht, tbl, obj, params);
520 lock = rht_bucket_lock(tbl, hash);
521
522 spin_lock_bh(lock);
523
524 /* Because we have already taken the bucket lock in tbl,
525 * if we find that future_tbl is not yet visible then
526 * that guarantees all other insertions of the same entry
527 * will also grab the bucket lock in tbl because until
528 * the rehash completes ht->tbl won't be changed.
529 */
530 new_tbl = rht_dereference_rcu(tbl->future_tbl, ht);
531 if (unlikely(new_tbl)) {
532 err = rhashtable_insert_slow(ht, key, obj, new_tbl);
533 goto out;
534 }
535
536 if (!key)
537 goto skip_lookup;
538
539 rht_for_each(head, tbl, hash) {
540 if (unlikely(!(params.obj_cmpfn ?
541 params.obj_cmpfn(&arg, rht_obj(ht, head)) :
542 rhashtable_compare(&arg, rht_obj(ht, head)))))
543 goto out;
544 }
545
546skip_lookup:
547 err = 0;
548
549 head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash);
550
551 RCU_INIT_POINTER(obj->next, head);
552
553 rcu_assign_pointer(tbl->buckets[hash], obj);
554
555 atomic_inc(&ht->nelems);
556 if (rht_grow_above_75(ht, tbl))
557 schedule_work(&ht->run_work);
558
559out:
560 spin_unlock_bh(lock);
561 rcu_read_unlock();
562
563 return err;
564}
565
566/**
567 * rhashtable_insert_fast - insert object into hash table
568 * @ht: hash table
569 * @obj: pointer to hash head inside object
570 * @params: hash table parameters
571 *
572 * Will take a per bucket spinlock to protect against mutual mutations
573 * on the same bucket. Multiple insertions may occur in parallel unless
574 * they map to the same bucket lock.
575 *
576 * It is safe to call this function from atomic context.
577 *
578 * Will trigger an automatic deferred table resizing if the size grows
579 * beyond the watermark indicated by grow_decision() which can be passed
580 * to rhashtable_init().
581 */
582static inline int rhashtable_insert_fast(
583 struct rhashtable *ht, struct rhash_head *obj,
584 const struct rhashtable_params params)
585{
586 return __rhashtable_insert_fast(ht, NULL, obj, params);
587}
588
589/**
590 * rhashtable_lookup_insert_fast - lookup and insert object into hash table
591 * @ht: hash table
592 * @obj: pointer to hash head inside object
593 * @params: hash table parameters
594 *
595 * Locks down the bucket chain in both the old and new table if a resize
596 * is in progress to ensure that writers can't remove from the old table
597 * and can't insert to the new table during the atomic operation of search
598 * and insertion. Searches for duplicates in both the old and new table if
599 * a resize is in progress.
600 *
601 * This lookup function may only be used for fixed key hash table (key_len
602 * parameter set). It will BUG() if used inappropriately.
603 *
604 * It is safe to call this function from atomic context.
605 *
606 * Will trigger an automatic deferred table resizing if the size grows
607 * beyond the watermark indicated by grow_decision() which can be passed
608 * to rhashtable_init().
609 */
610static inline int rhashtable_lookup_insert_fast(
611 struct rhashtable *ht, struct rhash_head *obj,
612 const struct rhashtable_params params)
613{
614 const char *key = rht_obj(ht, obj);
615
616 BUG_ON(ht->p.obj_hashfn);
617
618 return __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj,
619 params);
620}
621
622/**
623 * rhashtable_lookup_insert_key - search and insert object to hash table
624 * with explicit key
625 * @ht: hash table
626 * @key: key
627 * @obj: pointer to hash head inside object
628 * @params: hash table parameters
629 *
630 * Locks down the bucket chain in both the old and new table if a resize
631 * is in progress to ensure that writers can't remove from the old table
632 * and can't insert to the new table during the atomic operation of search
633 * and insertion. Searches for duplicates in both the old and new table if
634 * a resize is in progress.
635 *
636 * Lookups may occur in parallel with hashtable mutations and resizing.
637 *
638 * Will trigger an automatic deferred table resizing if the size grows
639 * beyond the watermark indicated by grow_decision() which can be passed
640 * to rhashtable_init().
641 *
642 * Returns zero on success.
643 */
644static inline int rhashtable_lookup_insert_key(
645 struct rhashtable *ht, const void *key, struct rhash_head *obj,
646 const struct rhashtable_params params)
647{
648 BUG_ON(!ht->p.obj_hashfn || !key);
649
650 return __rhashtable_insert_fast(ht, key, obj, params);
651}
652
653static inline int __rhashtable_remove_fast(
654 struct rhashtable *ht, struct bucket_table *tbl,
655 struct rhash_head *obj, const struct rhashtable_params params)
656{
657 struct rhash_head __rcu **pprev;
658 struct rhash_head *he;
659 spinlock_t * lock;
660 unsigned hash;
661 int err = -ENOENT;
662
663 hash = rht_head_hashfn(ht, tbl, obj, params);
664 lock = rht_bucket_lock(tbl, hash);
665
666 spin_lock_bh(lock);
667
668 pprev = &tbl->buckets[hash];
669 rht_for_each(he, tbl, hash) {
670 if (he != obj) {
671 pprev = &he->next;
672 continue;
673 }
674
675 rcu_assign_pointer(*pprev, obj->next);
676 err = 0;
677 break;
678 }
679
680 spin_unlock_bh(lock);
681
682 return err;
683}
684
685/**
686 * rhashtable_remove_fast - remove object from hash table
687 * @ht: hash table
688 * @obj: pointer to hash head inside object
689 * @params: hash table parameters
690 *
691 * Since the hash chain is single linked, the removal operation needs to
692 * walk the bucket chain upon removal. The removal operation is thus
693 * considerable slow if the hash table is not correctly sized.
694 *
695 * Will automatically shrink the table via rhashtable_expand() if the
696 * shrink_decision function specified at rhashtable_init() returns true.
697 *
698 * Returns zero on success, -ENOENT if the entry could not be found.
699 */
700static inline int rhashtable_remove_fast(
701 struct rhashtable *ht, struct rhash_head *obj,
702 const struct rhashtable_params params)
703{
704 struct bucket_table *tbl;
705 int err;
706
707 rcu_read_lock();
708
709 tbl = rht_dereference_rcu(ht->tbl, ht);
710
711 /* Because we have already taken (and released) the bucket
712 * lock in old_tbl, if we find that future_tbl is not yet
713 * visible then that guarantees the entry to still be in
714 * the old tbl if it exists.
715 */
716 while ((err = __rhashtable_remove_fast(ht, tbl, obj, params)) &&
717 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
718 ;
719
720 if (err)
721 goto out;
722
723 atomic_dec(&ht->nelems);
724 if (rht_shrink_below_30(ht, tbl))
725 schedule_work(&ht->run_work);
726
727out:
728 rcu_read_unlock();
729
730 return err;
731}
732
7e1e7763 733#endif /* _LINUX_RHASHTABLE_H */