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