blob: 4421e5ccb09231fcee613678f69717028c0b63b4 [file] [log] [blame]
Thomas Graf7e1e7762014-08-02 11:47:44 +02001/*
2 * Resizable, Scalable, Concurrent Hash Table
3 *
Herbert Xuca268932016-09-19 19:00:09 +08004 * Copyright (c) 2015-2016 Herbert Xu <herbert@gondor.apana.org.au>
Thomas Grafb5e2c152015-03-24 20:42:19 +00005 * Copyright (c) 2014-2015 Thomas Graf <tgraf@suug.ch>
Thomas Graf7e1e7762014-08-02 11:47:44 +02006 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
7 *
Thomas Graf7e1e7762014-08-02 11:47:44 +02008 * Code partially derived from nft_hash
Herbert Xudc0ee262015-03-20 21:57:06 +11009 * Rewritten with rehash code from br_multicast plus single list
10 * pointer as suggested by Josh Triplett
Thomas Graf7e1e7762014-08-02 11:47:44 +020011 *
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
Herbert Xu07ee0722015-05-15 11:30:47 +080020#include <linux/atomic.h>
Herbert Xuf2dba9c2015-02-04 07:33:23 +110021#include <linux/compiler.h>
Herbert Xu3cf92222015-12-03 20:41:29 +080022#include <linux/err.h>
Herbert Xu6626af62015-03-20 18:18:45 -040023#include <linux/errno.h>
Herbert Xu31ccde22015-03-24 00:50:21 +110024#include <linux/jhash.h>
Thomas Graff89bd6f2015-01-02 23:00:21 +010025#include <linux/list_nulls.h>
Thomas Graf97defe12015-01-02 23:00:20 +010026#include <linux/workqueue.h>
Ying Xue86b35b62015-01-04 15:25:09 +080027#include <linux/mutex.h>
Herbert Xu02fd97c2015-03-20 21:57:00 +110028#include <linux/rcupdate.h>
Thomas Graf7e1e7762014-08-02 11:47:44 +020029
Thomas Graff89bd6f2015-01-02 23:00:21 +010030/*
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
Herbert Xu02fd97c2015-03-20 21:57:00 +110049/* Base bits plus 1 bit for nulls marker */
50#define RHT_HASH_RESERVED_SPACE (RHT_BASE_BITS + 1)
51
Thomas Graf7e1e7762014-08-02 11:47:44 +020052struct rhash_head {
Thomas Graf5300fdc2014-08-13 16:38:29 +020053 struct rhash_head __rcu *next;
Thomas Graf7e1e7762014-08-02 11:47:44 +020054};
55
Herbert Xuca268932016-09-19 19:00:09 +080056struct rhlist_head {
57 struct rhash_head rhead;
58 struct rhlist_head __rcu *next;
59};
60
Thomas Graf97defe12015-01-02 23:00:20 +010061/**
62 * struct bucket_table - Table of hash buckets
63 * @size: Number of hash buckets
Herbert Xu63d512d2015-03-14 13:57:24 +110064 * @rehash: Current bucket being rehashed
Herbert Xu988dfbd2015-03-10 09:27:55 +110065 * @hash_rnd: Random seed to fold into hash
Thomas Graf97defe12015-01-02 23:00:20 +010066 * @locks_mask: Mask to apply before accessing locks[]
67 * @locks: Array of spinlocks protecting individual buckets
Herbert Xueddee5ba2015-03-14 13:57:20 +110068 * @walkers: List of active walkers
Herbert Xu9d901bc2015-03-14 13:57:23 +110069 * @rcu: RCU structure for freeing the table
Herbert Xuc4db8842015-03-14 13:57:25 +110070 * @future_tbl: Table under construction during rehashing
Thomas Graf97defe12015-01-02 23:00:20 +010071 * @buckets: size * hash buckets
72 */
Thomas Graf7e1e7762014-08-02 11:47:44 +020073struct bucket_table {
Herbert Xu63d512d2015-03-14 13:57:24 +110074 unsigned int size;
75 unsigned int rehash;
Herbert Xu988dfbd2015-03-10 09:27:55 +110076 u32 hash_rnd;
Eric Dumazetb9ebafb2015-02-20 06:48:57 -080077 unsigned int locks_mask;
78 spinlock_t *locks;
Herbert Xueddee5ba2015-03-14 13:57:20 +110079 struct list_head walkers;
Herbert Xu9d901bc2015-03-14 13:57:23 +110080 struct rcu_head rcu;
Eric Dumazetb9ebafb2015-02-20 06:48:57 -080081
Herbert Xuc4db8842015-03-14 13:57:25 +110082 struct bucket_table __rcu *future_tbl;
83
Eric Dumazetb9ebafb2015-02-20 06:48:57 -080084 struct rhash_head __rcu *buckets[] ____cacheline_aligned_in_smp;
Thomas Graf7e1e7762014-08-02 11:47:44 +020085};
86
Herbert Xu02fd97c2015-03-20 21:57:00 +110087/**
88 * struct rhashtable_compare_arg - Key for the function rhashtable_compare
89 * @ht: Hash table
90 * @key: Key to compare against
91 */
92struct rhashtable_compare_arg {
93 struct rhashtable *ht;
94 const void *key;
95};
96
Thomas Graf7e1e7762014-08-02 11:47:44 +020097typedef u32 (*rht_hashfn_t)(const void *data, u32 len, u32 seed);
Patrick McHardy49f7b332015-03-25 13:07:45 +000098typedef u32 (*rht_obj_hashfn_t)(const void *data, u32 len, u32 seed);
Herbert Xu02fd97c2015-03-20 21:57:00 +110099typedef int (*rht_obj_cmpfn_t)(struct rhashtable_compare_arg *arg,
100 const void *obj);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200101
102struct rhashtable;
103
104/**
105 * struct rhashtable_params - Hash table construction parameters
106 * @nelem_hint: Hint on number of elements, should be 75% of desired size
107 * @key_len: Length of key
108 * @key_offset: Offset of key in struct to be hashed
109 * @head_offset: Offset of rhash_head in struct to be hashed
Herbert Xu07ee0722015-05-15 11:30:47 +0800110 * @insecure_max_entries: Maximum number of entries (may be exceeded)
Herbert Xuc2e213c2015-03-18 20:01:16 +1100111 * @max_size: Maximum size while expanding
112 * @min_size: Minimum size while shrinking
Thomas Graff89bd6f2015-01-02 23:00:21 +0100113 * @nulls_base: Base value to generate nulls marker
Herbert Xuccd57b12015-03-24 00:50:28 +1100114 * @insecure_elasticity: Set to true to disable chain length checks
Thomas Grafb5e2c152015-03-24 20:42:19 +0000115 * @automatic_shrinking: Enable automatic shrinking of tables
Thomas Graf97defe12015-01-02 23:00:20 +0100116 * @locks_mul: Number of bucket locks to allocate per cpu (default: 128)
Herbert Xu31ccde22015-03-24 00:50:21 +1100117 * @hashfn: Hash function (default: jhash2 if !(key_len % 4), or jhash)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200118 * @obj_hashfn: Function to hash object
Herbert Xu02fd97c2015-03-20 21:57:00 +1100119 * @obj_cmpfn: Function to compare key with object
Thomas Graf7e1e7762014-08-02 11:47:44 +0200120 */
121struct rhashtable_params {
122 size_t nelem_hint;
123 size_t key_len;
124 size_t key_offset;
125 size_t head_offset;
Herbert Xu07ee0722015-05-15 11:30:47 +0800126 unsigned int insecure_max_entries;
Herbert Xuc2e213c2015-03-18 20:01:16 +1100127 unsigned int max_size;
128 unsigned int min_size;
Thomas Graff89bd6f2015-01-02 23:00:21 +0100129 u32 nulls_base;
Herbert Xuccd57b12015-03-24 00:50:28 +1100130 bool insecure_elasticity;
Thomas Grafb5e2c152015-03-24 20:42:19 +0000131 bool automatic_shrinking;
Thomas Graf97defe12015-01-02 23:00:20 +0100132 size_t locks_mul;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200133 rht_hashfn_t hashfn;
134 rht_obj_hashfn_t obj_hashfn;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100135 rht_obj_cmpfn_t obj_cmpfn;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200136};
137
138/**
139 * struct rhashtable - Hash table handle
140 * @tbl: Bucket table
Herbert Xu31ccde22015-03-24 00:50:21 +1100141 * @key_len: Key length for hashfn
Herbert Xuccd57b12015-03-24 00:50:28 +1100142 * @elasticity: Maximum chain length before rehash
Thomas Graf7e1e7762014-08-02 11:47:44 +0200143 * @p: Configuration parameters
Herbert Xuca268932016-09-19 19:00:09 +0800144 * @rhlist: True if this is an rhltable
Thomas Graf97defe12015-01-02 23:00:20 +0100145 * @run_work: Deferred worker to expand/shrink asynchronously
146 * @mutex: Mutex to protect current/future table swapping
Herbert Xuba7c95e2015-03-24 09:53:17 +1100147 * @lock: Spin lock to protect walker list
Eric Dumazet6060bcd2018-10-10 12:30:03 -0700148 * @nelems: Number of elements in table
Thomas Graf7e1e7762014-08-02 11:47:44 +0200149 */
150struct rhashtable {
151 struct bucket_table __rcu *tbl;
Herbert Xu31ccde22015-03-24 00:50:21 +1100152 unsigned int key_len;
Herbert Xuccd57b12015-03-24 00:50:28 +1100153 unsigned int elasticity;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200154 struct rhashtable_params p;
Herbert Xuca268932016-09-19 19:00:09 +0800155 bool rhlist;
Ying Xue57699a42015-01-16 11:13:09 +0800156 struct work_struct run_work;
Thomas Graf97defe12015-01-02 23:00:20 +0100157 struct mutex mutex;
Herbert Xuba7c95e2015-03-24 09:53:17 +1100158 spinlock_t lock;
Eric Dumazet6060bcd2018-10-10 12:30:03 -0700159 atomic_t nelems;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200160};
161
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100162/**
Herbert Xuca268932016-09-19 19:00:09 +0800163 * struct rhltable - Hash table with duplicate objects in a list
164 * @ht: Underlying rhtable
165 */
166struct rhltable {
167 struct rhashtable ht;
168};
169
170/**
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100171 * struct rhashtable_walker - Hash table walker
172 * @list: List entry on list of walkers
Herbert Xueddee5ba2015-03-14 13:57:20 +1100173 * @tbl: The table that we were walking over
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100174 */
175struct rhashtable_walker {
176 struct list_head list;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100177 struct bucket_table *tbl;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100178};
179
180/**
Herbert Xuca268932016-09-19 19:00:09 +0800181 * struct rhashtable_iter - Hash table iterator
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100182 * @ht: Table to iterate through
183 * @p: Current pointer
Herbert Xuca268932016-09-19 19:00:09 +0800184 * @list: Current hash list pointer
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100185 * @walker: Associated rhashtable walker
186 * @slot: Current slot
187 * @skip: Number of entries to skip in slot
188 */
189struct rhashtable_iter {
190 struct rhashtable *ht;
191 struct rhash_head *p;
Herbert Xuca268932016-09-19 19:00:09 +0800192 struct rhlist_head *list;
Herbert Xu246779d2016-08-18 16:50:56 +0800193 struct rhashtable_walker walker;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100194 unsigned int slot;
195 unsigned int skip;
196};
197
Thomas Graff89bd6f2015-01-02 23:00:21 +0100198static inline unsigned long rht_marker(const struct rhashtable *ht, u32 hash)
199{
200 return NULLS_MARKER(ht->p.nulls_base + hash);
201}
202
203#define INIT_RHT_NULLS_HEAD(ptr, ht, hash) \
204 ((ptr) = (typeof(ptr)) rht_marker(ht, hash))
205
206static inline bool rht_is_a_nulls(const struct rhash_head *ptr)
207{
208 return ((unsigned long) ptr & 1);
209}
210
211static inline unsigned long rht_get_nulls_value(const struct rhash_head *ptr)
212{
213 return ((unsigned long) ptr) >> 1;
214}
215
Herbert Xu02fd97c2015-03-20 21:57:00 +1100216static inline void *rht_obj(const struct rhashtable *ht,
217 const struct rhash_head *he)
218{
219 return (char *)he - ht->p.head_offset;
220}
221
222static inline unsigned int rht_bucket_index(const struct bucket_table *tbl,
223 unsigned int hash)
224{
225 return (hash >> RHT_HASH_RESERVED_SPACE) & (tbl->size - 1);
226}
227
228static inline unsigned int rht_key_hashfn(
229 struct rhashtable *ht, const struct bucket_table *tbl,
230 const void *key, const struct rhashtable_params params)
231{
Thomas Graf299e5c32015-03-24 14:18:17 +0100232 unsigned int hash;
Herbert Xude91b252015-03-24 00:50:20 +1100233
Herbert Xu31ccde22015-03-24 00:50:21 +1100234 /* params must be equal to ht->p if it isn't constant. */
235 if (!__builtin_constant_p(params.key_len))
236 hash = ht->p.hashfn(key, ht->key_len, tbl->hash_rnd);
237 else if (params.key_len) {
Thomas Graf299e5c32015-03-24 14:18:17 +0100238 unsigned int key_len = params.key_len;
Herbert Xu31ccde22015-03-24 00:50:21 +1100239
240 if (params.hashfn)
241 hash = params.hashfn(key, key_len, tbl->hash_rnd);
242 else if (key_len & (sizeof(u32) - 1))
243 hash = jhash(key, key_len, tbl->hash_rnd);
244 else
245 hash = jhash2(key, key_len / sizeof(u32),
246 tbl->hash_rnd);
247 } else {
Thomas Graf299e5c32015-03-24 14:18:17 +0100248 unsigned int key_len = ht->p.key_len;
Herbert Xu31ccde22015-03-24 00:50:21 +1100249
250 if (params.hashfn)
251 hash = params.hashfn(key, key_len, tbl->hash_rnd);
252 else
253 hash = jhash(key, key_len, tbl->hash_rnd);
254 }
255
256 return rht_bucket_index(tbl, hash);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100257}
258
259static inline unsigned int rht_head_hashfn(
260 struct rhashtable *ht, const struct bucket_table *tbl,
261 const struct rhash_head *he, const struct rhashtable_params params)
262{
263 const char *ptr = rht_obj(ht, he);
264
265 return likely(params.obj_hashfn) ?
Patrick McHardy49f7b332015-03-25 13:07:45 +0000266 rht_bucket_index(tbl, params.obj_hashfn(ptr, params.key_len ?:
267 ht->p.key_len,
268 tbl->hash_rnd)) :
Herbert Xu02fd97c2015-03-20 21:57:00 +1100269 rht_key_hashfn(ht, tbl, ptr + params.key_offset, params);
270}
271
272/**
273 * rht_grow_above_75 - returns true if nelems > 0.75 * table-size
274 * @ht: hash table
275 * @tbl: current table
276 */
277static inline bool rht_grow_above_75(const struct rhashtable *ht,
278 const struct bucket_table *tbl)
279{
280 /* Expand table when exceeding 75% load */
281 return atomic_read(&ht->nelems) > (tbl->size / 4 * 3) &&
282 (!ht->p.max_size || tbl->size < ht->p.max_size);
283}
284
285/**
286 * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size
287 * @ht: hash table
288 * @tbl: current table
289 */
290static inline bool rht_shrink_below_30(const struct rhashtable *ht,
291 const struct bucket_table *tbl)
292{
293 /* Shrink table beneath 30% load */
294 return atomic_read(&ht->nelems) < (tbl->size * 3 / 10) &&
295 tbl->size > ht->p.min_size;
296}
297
Herbert Xuccd57b12015-03-24 00:50:28 +1100298/**
299 * rht_grow_above_100 - returns true if nelems > table-size
300 * @ht: hash table
301 * @tbl: current table
302 */
303static inline bool rht_grow_above_100(const struct rhashtable *ht,
304 const struct bucket_table *tbl)
305{
Johannes Berg1d8dc3d2015-04-23 16:38:43 +0200306 return atomic_read(&ht->nelems) > tbl->size &&
307 (!ht->p.max_size || tbl->size < ht->p.max_size);
Herbert Xuccd57b12015-03-24 00:50:28 +1100308}
309
Herbert Xu07ee0722015-05-15 11:30:47 +0800310/**
311 * rht_grow_above_max - returns true if table is above maximum
312 * @ht: hash table
313 * @tbl: current table
314 */
315static inline bool rht_grow_above_max(const struct rhashtable *ht,
316 const struct bucket_table *tbl)
317{
318 return ht->p.insecure_max_entries &&
319 atomic_read(&ht->nelems) >= ht->p.insecure_max_entries;
320}
321
Herbert Xu02fd97c2015-03-20 21:57:00 +1100322/* The bucket lock is selected based on the hash and protects mutations
323 * on a group of hash buckets.
324 *
325 * A maximum of tbl->size/2 bucket locks is allocated. This ensures that
326 * a single lock always covers both buckets which may both contains
327 * entries which link to the same bucket of the old table during resizing.
328 * This allows to simplify the locking as locking the bucket in both
329 * tables during resize always guarantee protection.
330 *
331 * IMPORTANT: When holding the bucket lock of both the old and new table
332 * during expansions and shrinking, the old bucket lock must always be
333 * acquired first.
334 */
335static inline spinlock_t *rht_bucket_lock(const struct bucket_table *tbl,
336 unsigned int hash)
337{
338 return &tbl->locks[hash & tbl->locks_mask];
339}
340
Thomas Graf7e1e7762014-08-02 11:47:44 +0200341#ifdef CONFIG_PROVE_LOCKING
Thomas Graf97defe12015-01-02 23:00:20 +0100342int lockdep_rht_mutex_is_held(struct rhashtable *ht);
Thomas Graf88d6ed12015-01-02 23:00:16 +0100343int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200344#else
Thomas Graf97defe12015-01-02 23:00:20 +0100345static inline int lockdep_rht_mutex_is_held(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200346{
347 return 1;
348}
Thomas Graf88d6ed12015-01-02 23:00:16 +0100349
350static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl,
351 u32 hash)
352{
353 return 1;
354}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200355#endif /* CONFIG_PROVE_LOCKING */
356
Herbert Xu488fb86e2015-03-20 21:56:59 +1100357int rhashtable_init(struct rhashtable *ht,
358 const struct rhashtable_params *params);
Herbert Xuca268932016-09-19 19:00:09 +0800359int rhltable_init(struct rhltable *hlt,
360 const struct rhashtable_params *params);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200361
Herbert Xuca268932016-09-19 19:00:09 +0800362void *rhashtable_insert_slow(struct rhashtable *ht, const void *key,
363 struct rhash_head *obj);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200364
Herbert Xu246779d2016-08-18 16:50:56 +0800365void rhashtable_walk_enter(struct rhashtable *ht,
366 struct rhashtable_iter *iter);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100367void rhashtable_walk_exit(struct rhashtable_iter *iter);
368int rhashtable_walk_start(struct rhashtable_iter *iter) __acquires(RCU);
369void *rhashtable_walk_next(struct rhashtable_iter *iter);
370void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU);
371
Thomas Graf6b6f3022015-03-24 14:18:20 +0100372void rhashtable_free_and_destroy(struct rhashtable *ht,
373 void (*free_fn)(void *ptr, void *arg),
374 void *arg);
Thomas Graf97defe12015-01-02 23:00:20 +0100375void rhashtable_destroy(struct rhashtable *ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200376
377#define rht_dereference(p, ht) \
378 rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht))
379
380#define rht_dereference_rcu(p, ht) \
381 rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht))
382
Thomas Graf88d6ed12015-01-02 23:00:16 +0100383#define rht_dereference_bucket(p, tbl, hash) \
384 rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200385
Thomas Graf88d6ed12015-01-02 23:00:16 +0100386#define rht_dereference_bucket_rcu(p, tbl, hash) \
387 rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash))
388
389#define rht_entry(tpos, pos, member) \
390 ({ tpos = container_of(pos, typeof(*tpos), member); 1; })
391
392/**
393 * rht_for_each_continue - continue iterating over hash chain
394 * @pos: the &struct rhash_head to use as a loop cursor.
395 * @head: the previous &struct rhash_head to continue from
396 * @tbl: the &struct bucket_table
397 * @hash: the hash value / bucket index
398 */
399#define rht_for_each_continue(pos, head, tbl, hash) \
400 for (pos = rht_dereference_bucket(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100401 !rht_is_a_nulls(pos); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100402 pos = rht_dereference_bucket((pos)->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200403
404/**
405 * rht_for_each - iterate over hash chain
Thomas Graf88d6ed12015-01-02 23:00:16 +0100406 * @pos: the &struct rhash_head to use as a loop cursor.
407 * @tbl: the &struct bucket_table
408 * @hash: the hash value / bucket index
Thomas Graf7e1e7762014-08-02 11:47:44 +0200409 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100410#define rht_for_each(pos, tbl, hash) \
411 rht_for_each_continue(pos, (tbl)->buckets[hash], tbl, hash)
412
413/**
414 * rht_for_each_entry_continue - continue iterating over hash chain
415 * @tpos: the type * to use as a loop cursor.
416 * @pos: the &struct rhash_head to use as a loop cursor.
417 * @head: the previous &struct rhash_head to continue from
418 * @tbl: the &struct bucket_table
419 * @hash: the hash value / bucket index
420 * @member: name of the &struct rhash_head within the hashable struct.
421 */
422#define rht_for_each_entry_continue(tpos, pos, head, tbl, hash, member) \
423 for (pos = rht_dereference_bucket(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100424 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100425 pos = rht_dereference_bucket((pos)->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200426
427/**
428 * rht_for_each_entry - iterate over hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100429 * @tpos: the type * to use as a loop cursor.
430 * @pos: the &struct rhash_head to use as a loop cursor.
431 * @tbl: the &struct bucket_table
432 * @hash: the hash value / bucket index
433 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200434 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100435#define rht_for_each_entry(tpos, pos, tbl, hash, member) \
436 rht_for_each_entry_continue(tpos, pos, (tbl)->buckets[hash], \
437 tbl, hash, member)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200438
439/**
440 * rht_for_each_entry_safe - safely iterate over hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100441 * @tpos: the type * to use as a loop cursor.
442 * @pos: the &struct rhash_head to use as a loop cursor.
443 * @next: the &struct rhash_head to use as next in loop cursor.
444 * @tbl: the &struct bucket_table
445 * @hash: the hash value / bucket index
446 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200447 *
448 * This hash chain list-traversal primitive allows for the looped code to
449 * remove the loop cursor from the list.
450 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100451#define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \
452 for (pos = rht_dereference_bucket((tbl)->buckets[hash], tbl, hash), \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100453 next = !rht_is_a_nulls(pos) ? \
454 rht_dereference_bucket(pos->next, tbl, hash) : NULL; \
455 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
Patrick McHardy607954b2015-01-21 11:12:13 +0000456 pos = next, \
457 next = !rht_is_a_nulls(pos) ? \
458 rht_dereference_bucket(pos->next, tbl, hash) : NULL)
Thomas Graf88d6ed12015-01-02 23:00:16 +0100459
460/**
461 * rht_for_each_rcu_continue - continue iterating over rcu hash chain
462 * @pos: the &struct rhash_head to use as a loop cursor.
463 * @head: the previous &struct rhash_head to continue from
464 * @tbl: the &struct bucket_table
465 * @hash: the hash value / bucket index
466 *
467 * This hash chain list-traversal primitive may safely run concurrently with
468 * the _rcu mutation primitives such as rhashtable_insert() as long as the
469 * traversal is guarded by rcu_read_lock().
470 */
471#define rht_for_each_rcu_continue(pos, head, tbl, hash) \
472 for (({barrier(); }), \
473 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100474 !rht_is_a_nulls(pos); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100475 pos = rcu_dereference_raw(pos->next))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200476
477/**
478 * rht_for_each_rcu - iterate over rcu hash chain
Thomas Graf88d6ed12015-01-02 23:00:16 +0100479 * @pos: the &struct rhash_head to use as a loop cursor.
480 * @tbl: the &struct bucket_table
481 * @hash: the hash value / bucket index
Thomas Graf7e1e7762014-08-02 11:47:44 +0200482 *
483 * This hash chain list-traversal primitive may safely run concurrently with
Thomas Graf88d6ed12015-01-02 23:00:16 +0100484 * the _rcu mutation primitives such as rhashtable_insert() as long as the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200485 * traversal is guarded by rcu_read_lock().
486 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100487#define rht_for_each_rcu(pos, tbl, hash) \
488 rht_for_each_rcu_continue(pos, (tbl)->buckets[hash], tbl, hash)
489
490/**
491 * rht_for_each_entry_rcu_continue - continue iterating over rcu hash chain
492 * @tpos: the type * to use as a loop cursor.
493 * @pos: the &struct rhash_head to use as a loop cursor.
494 * @head: the previous &struct rhash_head to continue from
495 * @tbl: the &struct bucket_table
496 * @hash: the hash value / bucket index
497 * @member: name of the &struct rhash_head within the hashable struct.
498 *
499 * This hash chain list-traversal primitive may safely run concurrently with
500 * the _rcu mutation primitives such as rhashtable_insert() as long as the
501 * traversal is guarded by rcu_read_lock().
502 */
503#define rht_for_each_entry_rcu_continue(tpos, pos, head, tbl, hash, member) \
504 for (({barrier(); }), \
505 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100506 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100507 pos = rht_dereference_bucket_rcu(pos->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200508
509/**
510 * rht_for_each_entry_rcu - iterate over rcu hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100511 * @tpos: the type * to use as a loop cursor.
512 * @pos: the &struct rhash_head to use as a loop cursor.
513 * @tbl: the &struct bucket_table
514 * @hash: the hash value / bucket index
515 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200516 *
517 * This hash chain list-traversal primitive may safely run concurrently with
Thomas Graf88d6ed12015-01-02 23:00:16 +0100518 * the _rcu mutation primitives such as rhashtable_insert() as long as the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200519 * traversal is guarded by rcu_read_lock().
520 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100521#define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \
522 rht_for_each_entry_rcu_continue(tpos, pos, (tbl)->buckets[hash],\
523 tbl, hash, member)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200524
Herbert Xuca268932016-09-19 19:00:09 +0800525/**
526 * rhl_for_each_rcu - iterate over rcu hash table list
527 * @pos: the &struct rlist_head to use as a loop cursor.
528 * @list: the head of the list
529 *
530 * This hash chain list-traversal primitive should be used on the
531 * list returned by rhltable_lookup.
532 */
533#define rhl_for_each_rcu(pos, list) \
534 for (pos = list; pos; pos = rcu_dereference_raw(pos->next))
535
536/**
537 * rhl_for_each_entry_rcu - iterate over rcu hash table list of given type
538 * @tpos: the type * to use as a loop cursor.
539 * @pos: the &struct rlist_head to use as a loop cursor.
540 * @list: the head of the list
541 * @member: name of the &struct rlist_head within the hashable struct.
542 *
543 * This hash chain list-traversal primitive should be used on the
544 * list returned by rhltable_lookup.
545 */
546#define rhl_for_each_entry_rcu(tpos, pos, list, member) \
547 for (pos = list; pos && rht_entry(tpos, pos, member); \
548 pos = rcu_dereference_raw(pos->next))
549
Herbert Xu02fd97c2015-03-20 21:57:00 +1100550static inline int rhashtable_compare(struct rhashtable_compare_arg *arg,
551 const void *obj)
552{
553 struct rhashtable *ht = arg->ht;
554 const char *ptr = obj;
555
556 return memcmp(ptr + ht->p.key_offset, arg->key, ht->p.key_len);
557}
558
Herbert Xuca268932016-09-19 19:00:09 +0800559/* Internal function, do not use. */
560static inline struct rhash_head *__rhashtable_lookup(
Herbert Xu02fd97c2015-03-20 21:57:00 +1100561 struct rhashtable *ht, const void *key,
562 const struct rhashtable_params params)
563{
564 struct rhashtable_compare_arg arg = {
565 .ht = ht,
566 .key = key,
567 };
568 const struct bucket_table *tbl;
569 struct rhash_head *he;
Thomas Graf299e5c32015-03-24 14:18:17 +0100570 unsigned int hash;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100571
Herbert Xu02fd97c2015-03-20 21:57:00 +1100572 tbl = rht_dereference_rcu(ht->tbl, ht);
573restart:
574 hash = rht_key_hashfn(ht, tbl, key, params);
575 rht_for_each_rcu(he, tbl, hash) {
576 if (params.obj_cmpfn ?
577 params.obj_cmpfn(&arg, rht_obj(ht, he)) :
578 rhashtable_compare(&arg, rht_obj(ht, he)))
579 continue;
Herbert Xuca268932016-09-19 19:00:09 +0800580 return he;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100581 }
582
583 /* Ensure we see any new tables. */
584 smp_rmb();
585
586 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
587 if (unlikely(tbl))
588 goto restart;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100589
590 return NULL;
591}
592
Herbert Xuca268932016-09-19 19:00:09 +0800593/**
594 * rhashtable_lookup - search hash table
595 * @ht: hash table
596 * @key: the pointer to the key
597 * @params: hash table parameters
598 *
599 * Computes the hash value for the key and traverses the bucket chain looking
600 * for a entry with an identical key. The first matching entry is returned.
601 *
602 * This must only be called under the RCU read lock.
603 *
604 * Returns the first entry on which the compare function returned true.
605 */
606static inline void *rhashtable_lookup(
607 struct rhashtable *ht, const void *key,
608 const struct rhashtable_params params)
609{
610 struct rhash_head *he = __rhashtable_lookup(ht, key, params);
611
612 return he ? rht_obj(ht, he) : NULL;
613}
614
615/**
616 * rhashtable_lookup_fast - search hash table, without RCU read lock
617 * @ht: hash table
618 * @key: the pointer to the key
619 * @params: hash table parameters
620 *
621 * Computes the hash value for the key and traverses the bucket chain looking
622 * for a entry with an identical key. The first matching entry is returned.
623 *
624 * Only use this function when you have other mechanisms guaranteeing
625 * that the object won't go away after the RCU read lock is released.
626 *
627 * Returns the first entry on which the compare function returned true.
628 */
629static inline void *rhashtable_lookup_fast(
630 struct rhashtable *ht, const void *key,
631 const struct rhashtable_params params)
632{
633 void *obj;
634
635 rcu_read_lock();
636 obj = rhashtable_lookup(ht, key, params);
637 rcu_read_unlock();
638
639 return obj;
640}
641
642/**
643 * rhltable_lookup - search hash list table
644 * @hlt: hash table
645 * @key: the pointer to the key
646 * @params: hash table parameters
647 *
648 * Computes the hash value for the key and traverses the bucket chain looking
649 * for a entry with an identical key. All matching entries are returned
650 * in a list.
651 *
652 * This must only be called under the RCU read lock.
653 *
654 * Returns the list of entries that match the given key.
655 */
656static inline struct rhlist_head *rhltable_lookup(
657 struct rhltable *hlt, const void *key,
658 const struct rhashtable_params params)
659{
660 struct rhash_head *he = __rhashtable_lookup(&hlt->ht, key, params);
661
662 return he ? container_of(he, struct rhlist_head, rhead) : NULL;
663}
664
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200665/* Internal function, please use rhashtable_insert_fast() instead. This
666 * function returns the existing element already in hashes in there is a clash,
667 * otherwise it returns an error via ERR_PTR().
668 */
669static inline void *__rhashtable_insert_fast(
Herbert Xu02fd97c2015-03-20 21:57:00 +1100670 struct rhashtable *ht, const void *key, struct rhash_head *obj,
Herbert Xuca268932016-09-19 19:00:09 +0800671 const struct rhashtable_params params, bool rhlist)
Herbert Xu02fd97c2015-03-20 21:57:00 +1100672{
673 struct rhashtable_compare_arg arg = {
674 .ht = ht,
675 .key = key,
676 };
Herbert Xuca268932016-09-19 19:00:09 +0800677 struct rhash_head __rcu **pprev;
678 struct bucket_table *tbl;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100679 struct rhash_head *head;
680 spinlock_t *lock;
Thomas Graf299e5c32015-03-24 14:18:17 +0100681 unsigned int hash;
Herbert Xuca268932016-09-19 19:00:09 +0800682 int elasticity;
683 void *data;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100684
685 rcu_read_lock();
686
687 tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xuca268932016-09-19 19:00:09 +0800688 hash = rht_head_hashfn(ht, tbl, obj, params);
689 lock = rht_bucket_lock(tbl, hash);
690 spin_lock_bh(lock);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100691
Herbert Xuca268932016-09-19 19:00:09 +0800692 if (unlikely(rht_dereference_bucket(tbl->future_tbl, tbl, hash))) {
693slow_path:
Herbert Xub8244782015-03-24 00:50:26 +1100694 spin_unlock_bh(lock);
Herbert Xuca268932016-09-19 19:00:09 +0800695 rcu_read_unlock();
696 return rhashtable_insert_slow(ht, key, obj);
Herbert Xub8244782015-03-24 00:50:26 +1100697 }
698
Herbert Xuca268932016-09-19 19:00:09 +0800699 elasticity = ht->elasticity;
700 pprev = &tbl->buckets[hash];
701 rht_for_each(head, tbl, hash) {
702 struct rhlist_head *plist;
703 struct rhlist_head *list;
Herbert Xu3cf92222015-12-03 20:41:29 +0800704
Herbert Xuca268932016-09-19 19:00:09 +0800705 elasticity--;
706 if (!key ||
707 (params.obj_cmpfn ?
708 params.obj_cmpfn(&arg, rht_obj(ht, head)) :
Paul Blakeyad621702018-03-04 17:29:48 +0200709 rhashtable_compare(&arg, rht_obj(ht, head)))) {
710 pprev = &head->next;
Herbert Xuca268932016-09-19 19:00:09 +0800711 continue;
Paul Blakeyad621702018-03-04 17:29:48 +0200712 }
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200713
Herbert Xuca268932016-09-19 19:00:09 +0800714 data = rht_obj(ht, head);
715
716 if (!rhlist)
717 goto out;
718
719
720 list = container_of(obj, struct rhlist_head, rhead);
721 plist = container_of(head, struct rhlist_head, rhead);
722
723 RCU_INIT_POINTER(list->next, plist);
724 head = rht_dereference_bucket(head->next, tbl, hash);
725 RCU_INIT_POINTER(list->rhead.next, head);
726 rcu_assign_pointer(*pprev, obj);
727
728 goto good;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100729 }
730
Herbert Xuca268932016-09-19 19:00:09 +0800731 if (elasticity <= 0)
732 goto slow_path;
733
734 data = ERR_PTR(-E2BIG);
Herbert Xu07ee0722015-05-15 11:30:47 +0800735 if (unlikely(rht_grow_above_max(ht, tbl)))
736 goto out;
737
Herbert Xuca268932016-09-19 19:00:09 +0800738 if (unlikely(rht_grow_above_100(ht, tbl)))
739 goto slow_path;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100740
Herbert Xu02fd97c2015-03-20 21:57:00 +1100741 head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash);
742
743 RCU_INIT_POINTER(obj->next, head);
Herbert Xuca268932016-09-19 19:00:09 +0800744 if (rhlist) {
745 struct rhlist_head *list;
746
747 list = container_of(obj, struct rhlist_head, rhead);
748 RCU_INIT_POINTER(list->next, NULL);
749 }
Herbert Xu02fd97c2015-03-20 21:57:00 +1100750
751 rcu_assign_pointer(tbl->buckets[hash], obj);
752
753 atomic_inc(&ht->nelems);
754 if (rht_grow_above_75(ht, tbl))
755 schedule_work(&ht->run_work);
756
Herbert Xuca268932016-09-19 19:00:09 +0800757good:
758 data = NULL;
759
Herbert Xu02fd97c2015-03-20 21:57:00 +1100760out:
761 spin_unlock_bh(lock);
762 rcu_read_unlock();
763
Herbert Xuca268932016-09-19 19:00:09 +0800764 return data;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100765}
766
767/**
768 * rhashtable_insert_fast - insert object into hash table
769 * @ht: hash table
770 * @obj: pointer to hash head inside object
771 * @params: hash table parameters
772 *
773 * Will take a per bucket spinlock to protect against mutual mutations
774 * on the same bucket. Multiple insertions may occur in parallel unless
775 * they map to the same bucket lock.
776 *
777 * It is safe to call this function from atomic context.
778 *
779 * Will trigger an automatic deferred table resizing if the size grows
780 * beyond the watermark indicated by grow_decision() which can be passed
781 * to rhashtable_init().
782 */
783static inline int rhashtable_insert_fast(
784 struct rhashtable *ht, struct rhash_head *obj,
785 const struct rhashtable_params params)
786{
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200787 void *ret;
788
Herbert Xuca268932016-09-19 19:00:09 +0800789 ret = __rhashtable_insert_fast(ht, NULL, obj, params, false);
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200790 if (IS_ERR(ret))
791 return PTR_ERR(ret);
792
793 return ret == NULL ? 0 : -EEXIST;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100794}
795
796/**
Herbert Xuca268932016-09-19 19:00:09 +0800797 * rhltable_insert_key - insert object into hash list table
798 * @hlt: hash list table
799 * @key: the pointer to the key
800 * @list: pointer to hash list head inside object
801 * @params: hash table parameters
802 *
803 * Will take a per bucket spinlock to protect against mutual mutations
804 * on the same bucket. Multiple insertions may occur in parallel unless
805 * they map to the same bucket lock.
806 *
807 * It is safe to call this function from atomic context.
808 *
809 * Will trigger an automatic deferred table resizing if the size grows
810 * beyond the watermark indicated by grow_decision() which can be passed
811 * to rhashtable_init().
812 */
813static inline int rhltable_insert_key(
814 struct rhltable *hlt, const void *key, struct rhlist_head *list,
815 const struct rhashtable_params params)
816{
817 return PTR_ERR(__rhashtable_insert_fast(&hlt->ht, key, &list->rhead,
818 params, true));
819}
820
821/**
822 * rhltable_insert - insert object into hash list table
823 * @hlt: hash list table
824 * @list: pointer to hash list head inside object
825 * @params: hash table parameters
826 *
827 * Will take a per bucket spinlock to protect against mutual mutations
828 * on the same bucket. Multiple insertions may occur in parallel unless
829 * they map to the same bucket lock.
830 *
831 * It is safe to call this function from atomic context.
832 *
833 * Will trigger an automatic deferred table resizing if the size grows
834 * beyond the watermark indicated by grow_decision() which can be passed
835 * to rhashtable_init().
836 */
837static inline int rhltable_insert(
838 struct rhltable *hlt, struct rhlist_head *list,
839 const struct rhashtable_params params)
840{
841 const char *key = rht_obj(&hlt->ht, &list->rhead);
842
843 key += params.key_offset;
844
845 return rhltable_insert_key(hlt, key, list, params);
846}
847
848/**
Herbert Xu02fd97c2015-03-20 21:57:00 +1100849 * rhashtable_lookup_insert_fast - lookup and insert object into hash table
850 * @ht: hash table
851 * @obj: pointer to hash head inside object
852 * @params: hash table parameters
853 *
854 * Locks down the bucket chain in both the old and new table if a resize
855 * is in progress to ensure that writers can't remove from the old table
856 * and can't insert to the new table during the atomic operation of search
857 * and insertion. Searches for duplicates in both the old and new table if
858 * a resize is in progress.
859 *
860 * This lookup function may only be used for fixed key hash table (key_len
861 * parameter set). It will BUG() if used inappropriately.
862 *
863 * It is safe to call this function from atomic context.
864 *
865 * Will trigger an automatic deferred table resizing if the size grows
866 * beyond the watermark indicated by grow_decision() which can be passed
867 * to rhashtable_init().
868 */
869static inline int rhashtable_lookup_insert_fast(
870 struct rhashtable *ht, struct rhash_head *obj,
871 const struct rhashtable_params params)
872{
873 const char *key = rht_obj(ht, obj);
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200874 void *ret;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100875
876 BUG_ON(ht->p.obj_hashfn);
877
Herbert Xuca268932016-09-19 19:00:09 +0800878 ret = __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params,
879 false);
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200880 if (IS_ERR(ret))
881 return PTR_ERR(ret);
882
883 return ret == NULL ? 0 : -EEXIST;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100884}
885
886/**
887 * rhashtable_lookup_insert_key - search and insert object to hash table
888 * with explicit key
889 * @ht: hash table
890 * @key: key
891 * @obj: pointer to hash head inside object
892 * @params: hash table parameters
893 *
894 * Locks down the bucket chain in both the old and new table if a resize
895 * is in progress to ensure that writers can't remove from the old table
896 * and can't insert to the new table during the atomic operation of search
897 * and insertion. Searches for duplicates in both the old and new table if
898 * a resize is in progress.
899 *
900 * Lookups may occur in parallel with hashtable mutations and resizing.
901 *
902 * Will trigger an automatic deferred table resizing if the size grows
903 * beyond the watermark indicated by grow_decision() which can be passed
904 * to rhashtable_init().
905 *
906 * Returns zero on success.
907 */
908static inline int rhashtable_lookup_insert_key(
909 struct rhashtable *ht, const void *key, struct rhash_head *obj,
910 const struct rhashtable_params params)
911{
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200912 void *ret;
913
914 BUG_ON(!ht->p.obj_hashfn || !key);
915
Herbert Xuca268932016-09-19 19:00:09 +0800916 ret = __rhashtable_insert_fast(ht, key, obj, params, false);
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200917 if (IS_ERR(ret))
918 return PTR_ERR(ret);
919
920 return ret == NULL ? 0 : -EEXIST;
921}
922
923/**
924 * rhashtable_lookup_get_insert_key - lookup and insert object into hash table
925 * @ht: hash table
926 * @obj: pointer to hash head inside object
927 * @params: hash table parameters
928 * @data: pointer to element data already in hashes
929 *
930 * Just like rhashtable_lookup_insert_key(), but this function returns the
931 * object if it exists, NULL if it does not and the insertion was successful,
932 * and an ERR_PTR otherwise.
933 */
934static inline void *rhashtable_lookup_get_insert_key(
935 struct rhashtable *ht, const void *key, struct rhash_head *obj,
936 const struct rhashtable_params params)
937{
Herbert Xu02fd97c2015-03-20 21:57:00 +1100938 BUG_ON(!ht->p.obj_hashfn || !key);
939
Herbert Xuca268932016-09-19 19:00:09 +0800940 return __rhashtable_insert_fast(ht, key, obj, params, false);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100941}
942
Thomas Grafac833bd2015-03-24 14:18:18 +0100943/* Internal function, please use rhashtable_remove_fast() instead */
Herbert Xuca268932016-09-19 19:00:09 +0800944static inline int __rhashtable_remove_fast_one(
Herbert Xu02fd97c2015-03-20 21:57:00 +1100945 struct rhashtable *ht, struct bucket_table *tbl,
Herbert Xuca268932016-09-19 19:00:09 +0800946 struct rhash_head *obj, const struct rhashtable_params params,
947 bool rhlist)
Herbert Xu02fd97c2015-03-20 21:57:00 +1100948{
949 struct rhash_head __rcu **pprev;
950 struct rhash_head *he;
951 spinlock_t * lock;
Thomas Graf299e5c32015-03-24 14:18:17 +0100952 unsigned int hash;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100953 int err = -ENOENT;
954
955 hash = rht_head_hashfn(ht, tbl, obj, params);
956 lock = rht_bucket_lock(tbl, hash);
957
958 spin_lock_bh(lock);
959
960 pprev = &tbl->buckets[hash];
961 rht_for_each(he, tbl, hash) {
Herbert Xuca268932016-09-19 19:00:09 +0800962 struct rhlist_head *list;
963
964 list = container_of(he, struct rhlist_head, rhead);
965
Herbert Xu02fd97c2015-03-20 21:57:00 +1100966 if (he != obj) {
Herbert Xuca268932016-09-19 19:00:09 +0800967 struct rhlist_head __rcu **lpprev;
968
Herbert Xu02fd97c2015-03-20 21:57:00 +1100969 pprev = &he->next;
Herbert Xuca268932016-09-19 19:00:09 +0800970
971 if (!rhlist)
972 continue;
973
974 do {
975 lpprev = &list->next;
976 list = rht_dereference_bucket(list->next,
977 tbl, hash);
978 } while (list && obj != &list->rhead);
979
980 if (!list)
981 continue;
982
983 list = rht_dereference_bucket(list->next, tbl, hash);
984 RCU_INIT_POINTER(*lpprev, list);
985 err = 0;
986 break;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100987 }
988
Herbert Xuca268932016-09-19 19:00:09 +0800989 obj = rht_dereference_bucket(obj->next, tbl, hash);
990 err = 1;
991
992 if (rhlist) {
993 list = rht_dereference_bucket(list->next, tbl, hash);
994 if (list) {
995 RCU_INIT_POINTER(list->rhead.next, obj);
996 obj = &list->rhead;
997 err = 0;
998 }
999 }
1000
1001 rcu_assign_pointer(*pprev, obj);
Herbert Xu02fd97c2015-03-20 21:57:00 +11001002 break;
1003 }
1004
1005 spin_unlock_bh(lock);
1006
Herbert Xuca268932016-09-19 19:00:09 +08001007 if (err > 0) {
1008 atomic_dec(&ht->nelems);
1009 if (unlikely(ht->p.automatic_shrinking &&
1010 rht_shrink_below_30(ht, tbl)))
1011 schedule_work(&ht->run_work);
1012 err = 0;
1013 }
1014
1015 return err;
1016}
1017
1018/* Internal function, please use rhashtable_remove_fast() instead */
1019static inline int __rhashtable_remove_fast(
1020 struct rhashtable *ht, struct rhash_head *obj,
1021 const struct rhashtable_params params, bool rhlist)
1022{
1023 struct bucket_table *tbl;
1024 int err;
1025
1026 rcu_read_lock();
1027
1028 tbl = rht_dereference_rcu(ht->tbl, ht);
1029
1030 /* Because we have already taken (and released) the bucket
1031 * lock in old_tbl, if we find that future_tbl is not yet
1032 * visible then that guarantees the entry to still be in
1033 * the old tbl if it exists.
1034 */
1035 while ((err = __rhashtable_remove_fast_one(ht, tbl, obj, params,
1036 rhlist)) &&
1037 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
1038 ;
1039
1040 rcu_read_unlock();
1041
Herbert Xu02fd97c2015-03-20 21:57:00 +11001042 return err;
1043}
1044
1045/**
1046 * rhashtable_remove_fast - remove object from hash table
1047 * @ht: hash table
1048 * @obj: pointer to hash head inside object
1049 * @params: hash table parameters
1050 *
1051 * Since the hash chain is single linked, the removal operation needs to
1052 * walk the bucket chain upon removal. The removal operation is thus
1053 * considerable slow if the hash table is not correctly sized.
1054 *
1055 * Will automatically shrink the table via rhashtable_expand() if the
1056 * shrink_decision function specified at rhashtable_init() returns true.
1057 *
1058 * Returns zero on success, -ENOENT if the entry could not be found.
1059 */
1060static inline int rhashtable_remove_fast(
1061 struct rhashtable *ht, struct rhash_head *obj,
1062 const struct rhashtable_params params)
1063{
Herbert Xuca268932016-09-19 19:00:09 +08001064 return __rhashtable_remove_fast(ht, obj, params, false);
1065}
Herbert Xu02fd97c2015-03-20 21:57:00 +11001066
Herbert Xuca268932016-09-19 19:00:09 +08001067/**
1068 * rhltable_remove - remove object from hash list table
1069 * @hlt: hash list table
1070 * @list: pointer to hash list head inside object
1071 * @params: hash table parameters
1072 *
1073 * Since the hash chain is single linked, the removal operation needs to
1074 * walk the bucket chain upon removal. The removal operation is thus
1075 * considerable slow if the hash table is not correctly sized.
1076 *
1077 * Will automatically shrink the table via rhashtable_expand() if the
1078 * shrink_decision function specified at rhashtable_init() returns true.
1079 *
1080 * Returns zero on success, -ENOENT if the entry could not be found.
1081 */
1082static inline int rhltable_remove(
1083 struct rhltable *hlt, struct rhlist_head *list,
1084 const struct rhashtable_params params)
1085{
1086 return __rhashtable_remove_fast(&hlt->ht, &list->rhead, params, true);
Herbert Xu02fd97c2015-03-20 21:57:00 +11001087}
1088
Tom Herbert3502cad2015-12-15 15:41:36 -08001089/* Internal function, please use rhashtable_replace_fast() instead */
1090static inline int __rhashtable_replace_fast(
1091 struct rhashtable *ht, struct bucket_table *tbl,
1092 struct rhash_head *obj_old, struct rhash_head *obj_new,
1093 const struct rhashtable_params params)
1094{
1095 struct rhash_head __rcu **pprev;
1096 struct rhash_head *he;
1097 spinlock_t *lock;
1098 unsigned int hash;
1099 int err = -ENOENT;
1100
1101 /* Minimally, the old and new objects must have same hash
1102 * (which should mean identifiers are the same).
1103 */
1104 hash = rht_head_hashfn(ht, tbl, obj_old, params);
1105 if (hash != rht_head_hashfn(ht, tbl, obj_new, params))
1106 return -EINVAL;
1107
1108 lock = rht_bucket_lock(tbl, hash);
1109
1110 spin_lock_bh(lock);
1111
1112 pprev = &tbl->buckets[hash];
1113 rht_for_each(he, tbl, hash) {
1114 if (he != obj_old) {
1115 pprev = &he->next;
1116 continue;
1117 }
1118
1119 rcu_assign_pointer(obj_new->next, obj_old->next);
1120 rcu_assign_pointer(*pprev, obj_new);
1121 err = 0;
1122 break;
1123 }
1124
1125 spin_unlock_bh(lock);
1126
1127 return err;
1128}
1129
1130/**
1131 * rhashtable_replace_fast - replace an object in hash table
1132 * @ht: hash table
1133 * @obj_old: pointer to hash head inside object being replaced
1134 * @obj_new: pointer to hash head inside object which is new
1135 * @params: hash table parameters
1136 *
1137 * Replacing an object doesn't affect the number of elements in the hash table
1138 * or bucket, so we don't need to worry about shrinking or expanding the
1139 * table here.
1140 *
1141 * Returns zero on success, -ENOENT if the entry could not be found,
1142 * -EINVAL if hash is not the same for the old and new objects.
1143 */
1144static inline int rhashtable_replace_fast(
1145 struct rhashtable *ht, struct rhash_head *obj_old,
1146 struct rhash_head *obj_new,
1147 const struct rhashtable_params params)
1148{
1149 struct bucket_table *tbl;
1150 int err;
1151
1152 rcu_read_lock();
1153
1154 tbl = rht_dereference_rcu(ht->tbl, ht);
1155
1156 /* Because we have already taken (and released) the bucket
1157 * lock in old_tbl, if we find that future_tbl is not yet
1158 * visible then that guarantees the entry to still be in
1159 * the old tbl if it exists.
1160 */
1161 while ((err = __rhashtable_replace_fast(ht, tbl, obj_old,
1162 obj_new, params)) &&
1163 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
1164 ;
1165
1166 rcu_read_unlock();
1167
1168 return err;
1169}
1170
Herbert Xu246779d2016-08-18 16:50:56 +08001171/* Obsolete function, do not use in new code. */
1172static inline int rhashtable_walk_init(struct rhashtable *ht,
1173 struct rhashtable_iter *iter, gfp_t gfp)
1174{
1175 rhashtable_walk_enter(ht, iter);
1176 return 0;
1177}
1178
Herbert Xuca268932016-09-19 19:00:09 +08001179/**
1180 * rhltable_walk_enter - Initialise an iterator
1181 * @hlt: Table to walk over
1182 * @iter: Hash table Iterator
1183 *
1184 * This function prepares a hash table walk.
1185 *
1186 * Note that if you restart a walk after rhashtable_walk_stop you
1187 * may see the same object twice. Also, you may miss objects if
1188 * there are removals in between rhashtable_walk_stop and the next
1189 * call to rhashtable_walk_start.
1190 *
1191 * For a completely stable walk you should construct your own data
1192 * structure outside the hash table.
1193 *
1194 * This function may sleep so you must not call it from interrupt
1195 * context or with spin locks held.
1196 *
1197 * You must call rhashtable_walk_exit after this function returns.
1198 */
1199static inline void rhltable_walk_enter(struct rhltable *hlt,
1200 struct rhashtable_iter *iter)
1201{
1202 return rhashtable_walk_enter(&hlt->ht, iter);
1203}
1204
1205/**
1206 * rhltable_free_and_destroy - free elements and destroy hash list table
1207 * @hlt: the hash list table to destroy
1208 * @free_fn: callback to release resources of element
1209 * @arg: pointer passed to free_fn
1210 *
1211 * See documentation for rhashtable_free_and_destroy.
1212 */
1213static inline void rhltable_free_and_destroy(struct rhltable *hlt,
1214 void (*free_fn)(void *ptr,
1215 void *arg),
1216 void *arg)
1217{
1218 return rhashtable_free_and_destroy(&hlt->ht, free_fn, arg);
1219}
1220
1221static inline void rhltable_destroy(struct rhltable *hlt)
1222{
1223 return rhltable_free_and_destroy(hlt, NULL, NULL);
1224}
1225
Thomas Graf7e1e7762014-08-02 11:47:44 +02001226#endif /* _LINUX_RHASHTABLE_H */