blob: 48754ab07cdfcfd3886fdb7fef11cecfe08ab63b [file] [log] [blame]
NeilBrown0eb71a92018-06-18 12:52:50 +10001/* SPDX-License-Identifier: GPL-2.0 */
Thomas Graf7e1e7762014-08-02 11:47:44 +02002/*
3 * Resizable, Scalable, Concurrent Hash Table
4 *
Herbert Xuca268932016-09-19 19:00:09 +08005 * Copyright (c) 2015-2016 Herbert Xu <herbert@gondor.apana.org.au>
Thomas Grafb5e2c152015-03-24 20:42:19 +00006 * Copyright (c) 2014-2015 Thomas Graf <tgraf@suug.ch>
Thomas Graf7e1e7762014-08-02 11:47:44 +02007 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
8 *
Thomas Graf7e1e7762014-08-02 11:47:44 +02009 * Code partially derived from nft_hash
Herbert Xudc0ee262015-03-20 21:57:06 +110010 * Rewritten with rehash code from br_multicast plus single list
11 * pointer as suggested by Josh Triplett
Thomas Graf7e1e7762014-08-02 11:47:44 +020012 *
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
Herbert Xu3cf92222015-12-03 20:41:29 +080021#include <linux/err.h>
Herbert Xu6626af62015-03-20 18:18:45 -040022#include <linux/errno.h>
Herbert Xu31ccde22015-03-24 00:50:21 +110023#include <linux/jhash.h>
Thomas Graff89bd6f2015-01-02 23:00:21 +010024#include <linux/list_nulls.h>
Thomas Graf97defe12015-01-02 23:00:20 +010025#include <linux/workqueue.h>
Ingo Molnarb2d09102017-02-04 01:27:20 +010026#include <linux/rculist.h>
Thomas Graf7e1e7762014-08-02 11:47:44 +020027
NeilBrown0eb71a92018-06-18 12:52:50 +100028#include <linux/rhashtable-types.h>
Thomas Graff89bd6f2015-01-02 23:00:21 +010029/*
30 * The end of the chain is marked with a special nulls marks which has
31 * the following format:
32 *
33 * +-------+-----------------------------------------------------+-+
34 * | Base | Hash |1|
35 * +-------+-----------------------------------------------------+-+
36 *
37 * Base (4 bits) : Reserved to distinguish between multiple tables.
38 * Specified via &struct rhashtable_params.nulls_base.
39 * Hash (27 bits): Full hash (unmasked) of first element added to bucket
40 * 1 (1 bit) : Nulls marker (always set)
41 *
42 * The remaining bits of the next pointer remain unused for now.
43 */
44#define RHT_BASE_BITS 4
45#define RHT_HASH_BITS 27
46#define RHT_BASE_SHIFT RHT_HASH_BITS
47
Herbert Xu02fd97c2015-03-20 21:57:00 +110048/* Base bits plus 1 bit for nulls marker */
49#define RHT_HASH_RESERVED_SPACE (RHT_BASE_BITS + 1)
50
Florian Westphal5f8ddea2017-04-16 02:55:09 +020051/* Maximum chain length before rehash
52 *
53 * The maximum (not average) chain length grows with the size of the hash
54 * table, at a rate of (log N)/(log log N).
55 *
56 * The value of 16 is selected so that even if the hash table grew to
57 * 2^32 you would not expect the maximum chain length to exceed it
58 * unless we are under attack (or extremely unlucky).
59 *
60 * As this limit is only to detect attacks, we don't need to set it to a
61 * lower value as you'd need the chain length to vastly exceed 16 to have
62 * any real effect on the system.
63 */
64#define RHT_ELASTICITY 16u
65
Thomas Graf97defe12015-01-02 23:00:20 +010066/**
67 * struct bucket_table - Table of hash buckets
68 * @size: Number of hash buckets
Herbert Xuda204202017-02-11 19:26:47 +080069 * @nest: Number of bits of first-level nested table.
Herbert Xu63d512d2015-03-14 13:57:24 +110070 * @rehash: Current bucket being rehashed
Herbert Xu988dfbd2015-03-10 09:27:55 +110071 * @hash_rnd: Random seed to fold into hash
Thomas Graf97defe12015-01-02 23:00:20 +010072 * @locks_mask: Mask to apply before accessing locks[]
73 * @locks: Array of spinlocks protecting individual buckets
Herbert Xueddee5ba2015-03-14 13:57:20 +110074 * @walkers: List of active walkers
Herbert Xu9d901bc2015-03-14 13:57:23 +110075 * @rcu: RCU structure for freeing the table
Herbert Xuc4db8842015-03-14 13:57:25 +110076 * @future_tbl: Table under construction during rehashing
Herbert Xuda204202017-02-11 19:26:47 +080077 * @ntbl: Nested table used when out of memory.
Thomas Graf97defe12015-01-02 23:00:20 +010078 * @buckets: size * hash buckets
79 */
Thomas Graf7e1e7762014-08-02 11:47:44 +020080struct bucket_table {
Herbert Xu63d512d2015-03-14 13:57:24 +110081 unsigned int size;
Herbert Xuda204202017-02-11 19:26:47 +080082 unsigned int nest;
Herbert Xu63d512d2015-03-14 13:57:24 +110083 unsigned int rehash;
Herbert Xu988dfbd2015-03-10 09:27:55 +110084 u32 hash_rnd;
Eric Dumazetb9ebafb2015-02-20 06:48:57 -080085 unsigned int locks_mask;
86 spinlock_t *locks;
Herbert Xueddee5ba2015-03-14 13:57:20 +110087 struct list_head walkers;
Herbert Xu9d901bc2015-03-14 13:57:23 +110088 struct rcu_head rcu;
Eric Dumazetb9ebafb2015-02-20 06:48:57 -080089
Herbert Xuc4db8842015-03-14 13:57:25 +110090 struct bucket_table __rcu *future_tbl;
91
Herbert Xuda204202017-02-11 19:26:47 +080092 struct rhash_head __rcu *buckets[] ____cacheline_aligned_in_smp;
Thomas Graf7e1e7762014-08-02 11:47:44 +020093};
94
Thomas Graff89bd6f2015-01-02 23:00:21 +010095static inline unsigned long rht_marker(const struct rhashtable *ht, u32 hash)
96{
97 return NULLS_MARKER(ht->p.nulls_base + hash);
98}
99
100#define INIT_RHT_NULLS_HEAD(ptr, ht, hash) \
101 ((ptr) = (typeof(ptr)) rht_marker(ht, hash))
102
103static inline bool rht_is_a_nulls(const struct rhash_head *ptr)
104{
105 return ((unsigned long) ptr & 1);
106}
107
108static inline unsigned long rht_get_nulls_value(const struct rhash_head *ptr)
109{
110 return ((unsigned long) ptr) >> 1;
111}
112
Herbert Xu02fd97c2015-03-20 21:57:00 +1100113static inline void *rht_obj(const struct rhashtable *ht,
114 const struct rhash_head *he)
115{
116 return (char *)he - ht->p.head_offset;
117}
118
119static inline unsigned int rht_bucket_index(const struct bucket_table *tbl,
120 unsigned int hash)
121{
122 return (hash >> RHT_HASH_RESERVED_SPACE) & (tbl->size - 1);
123}
124
Tom Herbert2b860932017-12-04 10:31:43 -0800125static inline unsigned int rht_key_get_hash(struct rhashtable *ht,
126 const void *key, const struct rhashtable_params params,
127 unsigned int hash_rnd)
Herbert Xu02fd97c2015-03-20 21:57:00 +1100128{
Thomas Graf299e5c32015-03-24 14:18:17 +0100129 unsigned int hash;
Herbert Xude91b252015-03-24 00:50:20 +1100130
Herbert Xu31ccde22015-03-24 00:50:21 +1100131 /* params must be equal to ht->p if it isn't constant. */
132 if (!__builtin_constant_p(params.key_len))
Tom Herbert2b860932017-12-04 10:31:43 -0800133 hash = ht->p.hashfn(key, ht->key_len, hash_rnd);
Herbert Xu31ccde22015-03-24 00:50:21 +1100134 else if (params.key_len) {
Thomas Graf299e5c32015-03-24 14:18:17 +0100135 unsigned int key_len = params.key_len;
Herbert Xu31ccde22015-03-24 00:50:21 +1100136
137 if (params.hashfn)
Tom Herbert2b860932017-12-04 10:31:43 -0800138 hash = params.hashfn(key, key_len, hash_rnd);
Herbert Xu31ccde22015-03-24 00:50:21 +1100139 else if (key_len & (sizeof(u32) - 1))
Tom Herbert2b860932017-12-04 10:31:43 -0800140 hash = jhash(key, key_len, hash_rnd);
Herbert Xu31ccde22015-03-24 00:50:21 +1100141 else
Tom Herbert2b860932017-12-04 10:31:43 -0800142 hash = jhash2(key, key_len / sizeof(u32), hash_rnd);
Herbert Xu31ccde22015-03-24 00:50:21 +1100143 } else {
Thomas Graf299e5c32015-03-24 14:18:17 +0100144 unsigned int key_len = ht->p.key_len;
Herbert Xu31ccde22015-03-24 00:50:21 +1100145
146 if (params.hashfn)
Tom Herbert2b860932017-12-04 10:31:43 -0800147 hash = params.hashfn(key, key_len, hash_rnd);
Herbert Xu31ccde22015-03-24 00:50:21 +1100148 else
Tom Herbert2b860932017-12-04 10:31:43 -0800149 hash = jhash(key, key_len, hash_rnd);
Herbert Xu31ccde22015-03-24 00:50:21 +1100150 }
151
Tom Herbert2b860932017-12-04 10:31:43 -0800152 return hash;
153}
154
155static inline unsigned int rht_key_hashfn(
156 struct rhashtable *ht, const struct bucket_table *tbl,
157 const void *key, const struct rhashtable_params params)
158{
159 unsigned int hash = rht_key_get_hash(ht, key, params, tbl->hash_rnd);
160
Herbert Xu31ccde22015-03-24 00:50:21 +1100161 return rht_bucket_index(tbl, hash);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100162}
163
164static inline unsigned int rht_head_hashfn(
165 struct rhashtable *ht, const struct bucket_table *tbl,
166 const struct rhash_head *he, const struct rhashtable_params params)
167{
168 const char *ptr = rht_obj(ht, he);
169
170 return likely(params.obj_hashfn) ?
Patrick McHardy49f7b332015-03-25 13:07:45 +0000171 rht_bucket_index(tbl, params.obj_hashfn(ptr, params.key_len ?:
172 ht->p.key_len,
173 tbl->hash_rnd)) :
Herbert Xu02fd97c2015-03-20 21:57:00 +1100174 rht_key_hashfn(ht, tbl, ptr + params.key_offset, params);
175}
176
177/**
178 * rht_grow_above_75 - returns true if nelems > 0.75 * table-size
179 * @ht: hash table
180 * @tbl: current table
181 */
182static inline bool rht_grow_above_75(const struct rhashtable *ht,
183 const struct bucket_table *tbl)
184{
185 /* Expand table when exceeding 75% load */
186 return atomic_read(&ht->nelems) > (tbl->size / 4 * 3) &&
187 (!ht->p.max_size || tbl->size < ht->p.max_size);
188}
189
190/**
191 * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size
192 * @ht: hash table
193 * @tbl: current table
194 */
195static inline bool rht_shrink_below_30(const struct rhashtable *ht,
196 const struct bucket_table *tbl)
197{
198 /* Shrink table beneath 30% load */
199 return atomic_read(&ht->nelems) < (tbl->size * 3 / 10) &&
200 tbl->size > ht->p.min_size;
201}
202
Herbert Xuccd57b12015-03-24 00:50:28 +1100203/**
204 * rht_grow_above_100 - returns true if nelems > table-size
205 * @ht: hash table
206 * @tbl: current table
207 */
208static inline bool rht_grow_above_100(const struct rhashtable *ht,
209 const struct bucket_table *tbl)
210{
Johannes Berg1d8dc3d2015-04-23 16:38:43 +0200211 return atomic_read(&ht->nelems) > tbl->size &&
212 (!ht->p.max_size || tbl->size < ht->p.max_size);
Herbert Xuccd57b12015-03-24 00:50:28 +1100213}
214
Herbert Xu07ee0722015-05-15 11:30:47 +0800215/**
216 * rht_grow_above_max - returns true if table is above maximum
217 * @ht: hash table
218 * @tbl: current table
219 */
220static inline bool rht_grow_above_max(const struct rhashtable *ht,
221 const struct bucket_table *tbl)
222{
Herbert Xu6d684e52017-04-27 13:44:51 +0800223 return atomic_read(&ht->nelems) >= ht->max_elems;
Herbert Xu07ee0722015-05-15 11:30:47 +0800224}
225
Herbert Xu02fd97c2015-03-20 21:57:00 +1100226/* The bucket lock is selected based on the hash and protects mutations
227 * on a group of hash buckets.
228 *
229 * A maximum of tbl->size/2 bucket locks is allocated. This ensures that
230 * a single lock always covers both buckets which may both contains
231 * entries which link to the same bucket of the old table during resizing.
232 * This allows to simplify the locking as locking the bucket in both
233 * tables during resize always guarantee protection.
234 *
235 * IMPORTANT: When holding the bucket lock of both the old and new table
236 * during expansions and shrinking, the old bucket lock must always be
237 * acquired first.
238 */
239static inline spinlock_t *rht_bucket_lock(const struct bucket_table *tbl,
240 unsigned int hash)
241{
242 return &tbl->locks[hash & tbl->locks_mask];
243}
244
Thomas Graf7e1e7762014-08-02 11:47:44 +0200245#ifdef CONFIG_PROVE_LOCKING
Thomas Graf97defe12015-01-02 23:00:20 +0100246int lockdep_rht_mutex_is_held(struct rhashtable *ht);
Thomas Graf88d6ed12015-01-02 23:00:16 +0100247int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200248#else
Thomas Graf97defe12015-01-02 23:00:20 +0100249static inline int lockdep_rht_mutex_is_held(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200250{
251 return 1;
252}
Thomas Graf88d6ed12015-01-02 23:00:16 +0100253
254static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl,
255 u32 hash)
256{
257 return 1;
258}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200259#endif /* CONFIG_PROVE_LOCKING */
260
Herbert Xuca268932016-09-19 19:00:09 +0800261void *rhashtable_insert_slow(struct rhashtable *ht, const void *key,
262 struct rhash_head *obj);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200263
Herbert Xu246779d2016-08-18 16:50:56 +0800264void rhashtable_walk_enter(struct rhashtable *ht,
265 struct rhashtable_iter *iter);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100266void rhashtable_walk_exit(struct rhashtable_iter *iter);
Tom Herbert97a6ec42017-12-04 10:31:41 -0800267int rhashtable_walk_start_check(struct rhashtable_iter *iter) __acquires(RCU);
268
269static inline void rhashtable_walk_start(struct rhashtable_iter *iter)
270{
271 (void)rhashtable_walk_start_check(iter);
272}
273
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100274void *rhashtable_walk_next(struct rhashtable_iter *iter);
Tom Herbert2db54b42017-12-04 10:31:42 -0800275void *rhashtable_walk_peek(struct rhashtable_iter *iter);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100276void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU);
277
Thomas Graf6b6f3022015-03-24 14:18:20 +0100278void rhashtable_free_and_destroy(struct rhashtable *ht,
279 void (*free_fn)(void *ptr, void *arg),
280 void *arg);
Thomas Graf97defe12015-01-02 23:00:20 +0100281void rhashtable_destroy(struct rhashtable *ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200282
Herbert Xuda204202017-02-11 19:26:47 +0800283struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl,
284 unsigned int hash);
285struct rhash_head __rcu **rht_bucket_nested_insert(struct rhashtable *ht,
286 struct bucket_table *tbl,
287 unsigned int hash);
288
Thomas Graf7e1e7762014-08-02 11:47:44 +0200289#define rht_dereference(p, ht) \
290 rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht))
291
292#define rht_dereference_rcu(p, ht) \
293 rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht))
294
Thomas Graf88d6ed12015-01-02 23:00:16 +0100295#define rht_dereference_bucket(p, tbl, hash) \
296 rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200297
Thomas Graf88d6ed12015-01-02 23:00:16 +0100298#define rht_dereference_bucket_rcu(p, tbl, hash) \
299 rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash))
300
301#define rht_entry(tpos, pos, member) \
302 ({ tpos = container_of(pos, typeof(*tpos), member); 1; })
303
Herbert Xuda204202017-02-11 19:26:47 +0800304static inline struct rhash_head __rcu *const *rht_bucket(
305 const struct bucket_table *tbl, unsigned int hash)
306{
307 return unlikely(tbl->nest) ? rht_bucket_nested(tbl, hash) :
308 &tbl->buckets[hash];
309}
310
311static inline struct rhash_head __rcu **rht_bucket_var(
312 struct bucket_table *tbl, unsigned int hash)
313{
314 return unlikely(tbl->nest) ? rht_bucket_nested(tbl, hash) :
315 &tbl->buckets[hash];
316}
317
318static inline struct rhash_head __rcu **rht_bucket_insert(
319 struct rhashtable *ht, struct bucket_table *tbl, unsigned int hash)
320{
321 return unlikely(tbl->nest) ? rht_bucket_nested_insert(ht, tbl, hash) :
322 &tbl->buckets[hash];
323}
324
Thomas Graf88d6ed12015-01-02 23:00:16 +0100325/**
326 * rht_for_each_continue - continue iterating over hash chain
327 * @pos: the &struct rhash_head to use as a loop cursor.
328 * @head: the previous &struct rhash_head to continue from
329 * @tbl: the &struct bucket_table
330 * @hash: the hash value / bucket index
331 */
332#define rht_for_each_continue(pos, head, tbl, hash) \
333 for (pos = rht_dereference_bucket(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100334 !rht_is_a_nulls(pos); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100335 pos = rht_dereference_bucket((pos)->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200336
337/**
338 * rht_for_each - iterate over hash chain
Thomas Graf88d6ed12015-01-02 23:00:16 +0100339 * @pos: the &struct rhash_head to use as a loop cursor.
340 * @tbl: the &struct bucket_table
341 * @hash: the hash value / bucket index
Thomas Graf7e1e7762014-08-02 11:47:44 +0200342 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100343#define rht_for_each(pos, tbl, hash) \
Herbert Xuda204202017-02-11 19:26:47 +0800344 rht_for_each_continue(pos, *rht_bucket(tbl, hash), tbl, hash)
Thomas Graf88d6ed12015-01-02 23:00:16 +0100345
346/**
347 * rht_for_each_entry_continue - continue iterating over hash chain
348 * @tpos: the type * to use as a loop cursor.
349 * @pos: the &struct rhash_head to use as a loop cursor.
350 * @head: the previous &struct rhash_head to continue from
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.
354 */
355#define rht_for_each_entry_continue(tpos, pos, head, tbl, hash, member) \
356 for (pos = rht_dereference_bucket(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100357 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100358 pos = rht_dereference_bucket((pos)->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200359
360/**
361 * rht_for_each_entry - iterate over hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100362 * @tpos: the type * to use as a loop cursor.
363 * @pos: the &struct rhash_head to use as a 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.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200367 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100368#define rht_for_each_entry(tpos, pos, tbl, hash, member) \
Herbert Xuda204202017-02-11 19:26:47 +0800369 rht_for_each_entry_continue(tpos, pos, *rht_bucket(tbl, hash), \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100370 tbl, hash, member)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200371
372/**
373 * rht_for_each_entry_safe - safely iterate over hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100374 * @tpos: the type * to use as a loop cursor.
375 * @pos: the &struct rhash_head to use as a loop cursor.
376 * @next: the &struct rhash_head to use as next in loop cursor.
377 * @tbl: the &struct bucket_table
378 * @hash: the hash value / bucket index
379 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200380 *
381 * This hash chain list-traversal primitive allows for the looped code to
382 * remove the loop cursor from the list.
383 */
Herbert Xuda204202017-02-11 19:26:47 +0800384#define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \
385 for (pos = rht_dereference_bucket(*rht_bucket(tbl, hash), tbl, hash), \
386 next = !rht_is_a_nulls(pos) ? \
387 rht_dereference_bucket(pos->next, tbl, hash) : NULL; \
388 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
389 pos = next, \
390 next = !rht_is_a_nulls(pos) ? \
Patrick McHardy607954b2015-01-21 11:12:13 +0000391 rht_dereference_bucket(pos->next, tbl, hash) : NULL)
Thomas Graf88d6ed12015-01-02 23:00:16 +0100392
393/**
394 * rht_for_each_rcu_continue - continue iterating over rcu hash chain
395 * @pos: the &struct rhash_head to use as a loop cursor.
396 * @head: the previous &struct rhash_head to continue from
397 * @tbl: the &struct bucket_table
398 * @hash: the hash value / bucket index
399 *
400 * This hash chain list-traversal primitive may safely run concurrently with
401 * the _rcu mutation primitives such as rhashtable_insert() as long as the
402 * traversal is guarded by rcu_read_lock().
403 */
404#define rht_for_each_rcu_continue(pos, head, tbl, hash) \
405 for (({barrier(); }), \
406 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100407 !rht_is_a_nulls(pos); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100408 pos = rcu_dereference_raw(pos->next))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200409
410/**
411 * rht_for_each_rcu - iterate over rcu hash chain
Thomas Graf88d6ed12015-01-02 23:00:16 +0100412 * @pos: the &struct rhash_head to use as a loop cursor.
413 * @tbl: the &struct bucket_table
414 * @hash: the hash value / bucket index
Thomas Graf7e1e7762014-08-02 11:47:44 +0200415 *
416 * This hash chain list-traversal primitive may safely run concurrently with
Thomas Graf88d6ed12015-01-02 23:00:16 +0100417 * the _rcu mutation primitives such as rhashtable_insert() as long as the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200418 * traversal is guarded by rcu_read_lock().
419 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100420#define rht_for_each_rcu(pos, tbl, hash) \
Herbert Xuda204202017-02-11 19:26:47 +0800421 rht_for_each_rcu_continue(pos, *rht_bucket(tbl, hash), tbl, hash)
Thomas Graf88d6ed12015-01-02 23:00:16 +0100422
423/**
424 * rht_for_each_entry_rcu_continue - continue iterating over rcu hash chain
425 * @tpos: the type * to use as a loop cursor.
426 * @pos: the &struct rhash_head to use as a loop cursor.
427 * @head: the previous &struct rhash_head to continue from
428 * @tbl: the &struct bucket_table
429 * @hash: the hash value / bucket index
430 * @member: name of the &struct rhash_head within the hashable struct.
431 *
432 * This hash chain list-traversal primitive may safely run concurrently with
433 * the _rcu mutation primitives such as rhashtable_insert() as long as the
434 * traversal is guarded by rcu_read_lock().
435 */
436#define rht_for_each_entry_rcu_continue(tpos, pos, head, tbl, hash, member) \
437 for (({barrier(); }), \
438 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100439 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100440 pos = rht_dereference_bucket_rcu(pos->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200441
442/**
443 * rht_for_each_entry_rcu - iterate over rcu hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100444 * @tpos: the type * to use as a loop cursor.
445 * @pos: the &struct rhash_head to use as a loop cursor.
446 * @tbl: the &struct bucket_table
447 * @hash: the hash value / bucket index
448 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200449 *
450 * This hash chain list-traversal primitive may safely run concurrently with
Thomas Graf88d6ed12015-01-02 23:00:16 +0100451 * the _rcu mutation primitives such as rhashtable_insert() as long as the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200452 * traversal is guarded by rcu_read_lock().
453 */
Herbert Xuda204202017-02-11 19:26:47 +0800454#define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \
455 rht_for_each_entry_rcu_continue(tpos, pos, *rht_bucket(tbl, hash), \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100456 tbl, hash, member)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200457
Herbert Xuca268932016-09-19 19:00:09 +0800458/**
459 * rhl_for_each_rcu - iterate over rcu hash table list
460 * @pos: the &struct rlist_head to use as a loop cursor.
461 * @list: the head of the list
462 *
463 * This hash chain list-traversal primitive should be used on the
464 * list returned by rhltable_lookup.
465 */
466#define rhl_for_each_rcu(pos, list) \
467 for (pos = list; pos; pos = rcu_dereference_raw(pos->next))
468
469/**
470 * rhl_for_each_entry_rcu - iterate over rcu hash table list of given type
471 * @tpos: the type * to use as a loop cursor.
472 * @pos: the &struct rlist_head to use as a loop cursor.
473 * @list: the head of the list
474 * @member: name of the &struct rlist_head within the hashable struct.
475 *
476 * This hash chain list-traversal primitive should be used on the
477 * list returned by rhltable_lookup.
478 */
479#define rhl_for_each_entry_rcu(tpos, pos, list, member) \
480 for (pos = list; pos && rht_entry(tpos, pos, member); \
481 pos = rcu_dereference_raw(pos->next))
482
Herbert Xu02fd97c2015-03-20 21:57:00 +1100483static inline int rhashtable_compare(struct rhashtable_compare_arg *arg,
484 const void *obj)
485{
486 struct rhashtable *ht = arg->ht;
487 const char *ptr = obj;
488
489 return memcmp(ptr + ht->p.key_offset, arg->key, ht->p.key_len);
490}
491
Herbert Xuca268932016-09-19 19:00:09 +0800492/* Internal function, do not use. */
493static inline struct rhash_head *__rhashtable_lookup(
Herbert Xu02fd97c2015-03-20 21:57:00 +1100494 struct rhashtable *ht, const void *key,
495 const struct rhashtable_params params)
496{
497 struct rhashtable_compare_arg arg = {
498 .ht = ht,
499 .key = key,
500 };
Herbert Xuda204202017-02-11 19:26:47 +0800501 struct bucket_table *tbl;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100502 struct rhash_head *he;
Thomas Graf299e5c32015-03-24 14:18:17 +0100503 unsigned int hash;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100504
Herbert Xu02fd97c2015-03-20 21:57:00 +1100505 tbl = rht_dereference_rcu(ht->tbl, ht);
506restart:
507 hash = rht_key_hashfn(ht, tbl, key, params);
508 rht_for_each_rcu(he, tbl, hash) {
509 if (params.obj_cmpfn ?
510 params.obj_cmpfn(&arg, rht_obj(ht, he)) :
511 rhashtable_compare(&arg, rht_obj(ht, he)))
512 continue;
Herbert Xuca268932016-09-19 19:00:09 +0800513 return he;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100514 }
515
516 /* Ensure we see any new tables. */
517 smp_rmb();
518
519 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
520 if (unlikely(tbl))
521 goto restart;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100522
523 return NULL;
524}
525
Herbert Xuca268932016-09-19 19:00:09 +0800526/**
527 * rhashtable_lookup - search hash table
528 * @ht: hash table
529 * @key: the pointer to the key
530 * @params: hash table parameters
531 *
532 * Computes the hash value for the key and traverses the bucket chain looking
533 * for a entry with an identical key. The first matching entry is returned.
534 *
535 * This must only be called under the RCU read lock.
536 *
537 * Returns the first entry on which the compare function returned true.
538 */
539static inline void *rhashtable_lookup(
540 struct rhashtable *ht, const void *key,
541 const struct rhashtable_params params)
542{
543 struct rhash_head *he = __rhashtable_lookup(ht, key, params);
544
545 return he ? rht_obj(ht, he) : NULL;
546}
547
548/**
549 * rhashtable_lookup_fast - search hash table, without RCU read lock
550 * @ht: 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. The first matching entry is returned.
556 *
557 * Only use this function when you have other mechanisms guaranteeing
558 * that the object won't go away after the RCU read lock is released.
559 *
560 * Returns the first entry on which the compare function returned true.
561 */
562static inline void *rhashtable_lookup_fast(
563 struct rhashtable *ht, const void *key,
564 const struct rhashtable_params params)
565{
566 void *obj;
567
568 rcu_read_lock();
569 obj = rhashtable_lookup(ht, key, params);
570 rcu_read_unlock();
571
572 return obj;
573}
574
575/**
576 * rhltable_lookup - search hash list table
577 * @hlt: hash table
578 * @key: the pointer to the key
579 * @params: hash table parameters
580 *
581 * Computes the hash value for the key and traverses the bucket chain looking
582 * for a entry with an identical key. All matching entries are returned
583 * in a list.
584 *
585 * This must only be called under the RCU read lock.
586 *
587 * Returns the list of entries that match the given key.
588 */
589static inline struct rhlist_head *rhltable_lookup(
590 struct rhltable *hlt, const void *key,
591 const struct rhashtable_params params)
592{
593 struct rhash_head *he = __rhashtable_lookup(&hlt->ht, key, params);
594
595 return he ? container_of(he, struct rhlist_head, rhead) : NULL;
596}
597
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200598/* Internal function, please use rhashtable_insert_fast() instead. This
599 * function returns the existing element already in hashes in there is a clash,
600 * otherwise it returns an error via ERR_PTR().
601 */
602static inline void *__rhashtable_insert_fast(
Herbert Xu02fd97c2015-03-20 21:57:00 +1100603 struct rhashtable *ht, const void *key, struct rhash_head *obj,
Herbert Xuca268932016-09-19 19:00:09 +0800604 const struct rhashtable_params params, bool rhlist)
Herbert Xu02fd97c2015-03-20 21:57:00 +1100605{
606 struct rhashtable_compare_arg arg = {
607 .ht = ht,
608 .key = key,
609 };
Herbert Xuca268932016-09-19 19:00:09 +0800610 struct rhash_head __rcu **pprev;
611 struct bucket_table *tbl;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100612 struct rhash_head *head;
613 spinlock_t *lock;
Thomas Graf299e5c32015-03-24 14:18:17 +0100614 unsigned int hash;
Herbert Xuca268932016-09-19 19:00:09 +0800615 int elasticity;
616 void *data;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100617
618 rcu_read_lock();
619
620 tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xuca268932016-09-19 19:00:09 +0800621 hash = rht_head_hashfn(ht, tbl, obj, params);
622 lock = rht_bucket_lock(tbl, hash);
623 spin_lock_bh(lock);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100624
Herbert Xuca268932016-09-19 19:00:09 +0800625 if (unlikely(rht_dereference_bucket(tbl->future_tbl, tbl, hash))) {
626slow_path:
Herbert Xub8244782015-03-24 00:50:26 +1100627 spin_unlock_bh(lock);
Herbert Xuca268932016-09-19 19:00:09 +0800628 rcu_read_unlock();
629 return rhashtable_insert_slow(ht, key, obj);
Herbert Xub8244782015-03-24 00:50:26 +1100630 }
631
Florian Westphal5f8ddea2017-04-16 02:55:09 +0200632 elasticity = RHT_ELASTICITY;
Herbert Xuda204202017-02-11 19:26:47 +0800633 pprev = rht_bucket_insert(ht, tbl, hash);
634 data = ERR_PTR(-ENOMEM);
635 if (!pprev)
636 goto out;
637
638 rht_for_each_continue(head, *pprev, tbl, hash) {
Herbert Xuca268932016-09-19 19:00:09 +0800639 struct rhlist_head *plist;
640 struct rhlist_head *list;
Herbert Xu3cf92222015-12-03 20:41:29 +0800641
Herbert Xuca268932016-09-19 19:00:09 +0800642 elasticity--;
643 if (!key ||
644 (params.obj_cmpfn ?
645 params.obj_cmpfn(&arg, rht_obj(ht, head)) :
Paul Blakeyd3dcf8e2018-03-04 17:29:48 +0200646 rhashtable_compare(&arg, rht_obj(ht, head)))) {
647 pprev = &head->next;
Herbert Xuca268932016-09-19 19:00:09 +0800648 continue;
Paul Blakeyd3dcf8e2018-03-04 17:29:48 +0200649 }
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200650
Herbert Xuca268932016-09-19 19:00:09 +0800651 data = rht_obj(ht, head);
652
653 if (!rhlist)
654 goto out;
655
656
657 list = container_of(obj, struct rhlist_head, rhead);
658 plist = container_of(head, struct rhlist_head, rhead);
659
660 RCU_INIT_POINTER(list->next, plist);
661 head = rht_dereference_bucket(head->next, tbl, hash);
662 RCU_INIT_POINTER(list->rhead.next, head);
663 rcu_assign_pointer(*pprev, obj);
664
665 goto good;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100666 }
667
Herbert Xuca268932016-09-19 19:00:09 +0800668 if (elasticity <= 0)
669 goto slow_path;
670
671 data = ERR_PTR(-E2BIG);
Herbert Xu07ee0722015-05-15 11:30:47 +0800672 if (unlikely(rht_grow_above_max(ht, tbl)))
673 goto out;
674
Herbert Xuca268932016-09-19 19:00:09 +0800675 if (unlikely(rht_grow_above_100(ht, tbl)))
676 goto slow_path;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100677
Herbert Xuda204202017-02-11 19:26:47 +0800678 head = rht_dereference_bucket(*pprev, tbl, hash);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100679
680 RCU_INIT_POINTER(obj->next, head);
Herbert Xuca268932016-09-19 19:00:09 +0800681 if (rhlist) {
682 struct rhlist_head *list;
683
684 list = container_of(obj, struct rhlist_head, rhead);
685 RCU_INIT_POINTER(list->next, NULL);
686 }
Herbert Xu02fd97c2015-03-20 21:57:00 +1100687
Herbert Xuda204202017-02-11 19:26:47 +0800688 rcu_assign_pointer(*pprev, obj);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100689
690 atomic_inc(&ht->nelems);
691 if (rht_grow_above_75(ht, tbl))
692 schedule_work(&ht->run_work);
693
Herbert Xuca268932016-09-19 19:00:09 +0800694good:
695 data = NULL;
696
Herbert Xu02fd97c2015-03-20 21:57:00 +1100697out:
698 spin_unlock_bh(lock);
699 rcu_read_unlock();
700
Herbert Xuca268932016-09-19 19:00:09 +0800701 return data;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100702}
703
704/**
705 * rhashtable_insert_fast - insert object into hash table
706 * @ht: hash table
707 * @obj: pointer to hash head inside object
708 * @params: hash table parameters
709 *
710 * Will take a per bucket spinlock to protect against mutual mutations
711 * on the same bucket. Multiple insertions may occur in parallel unless
712 * they map to the same bucket lock.
713 *
714 * It is safe to call this function from atomic context.
715 *
NeilBrown0c6f69a2018-04-24 08:29:13 +1000716 * Will trigger an automatic deferred table resizing if residency in the
717 * table grows beyond 70%.
Herbert Xu02fd97c2015-03-20 21:57:00 +1100718 */
719static inline int rhashtable_insert_fast(
720 struct rhashtable *ht, struct rhash_head *obj,
721 const struct rhashtable_params params)
722{
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200723 void *ret;
724
Herbert Xuca268932016-09-19 19:00:09 +0800725 ret = __rhashtable_insert_fast(ht, NULL, obj, params, false);
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200726 if (IS_ERR(ret))
727 return PTR_ERR(ret);
728
729 return ret == NULL ? 0 : -EEXIST;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100730}
731
732/**
Herbert Xuca268932016-09-19 19:00:09 +0800733 * rhltable_insert_key - insert object into hash list table
734 * @hlt: hash list table
735 * @key: the pointer to the key
736 * @list: pointer to hash list head inside object
737 * @params: hash table parameters
738 *
739 * Will take a per bucket spinlock to protect against mutual mutations
740 * on the same bucket. Multiple insertions may occur in parallel unless
741 * they map to the same bucket lock.
742 *
743 * It is safe to call this function from atomic context.
744 *
NeilBrown0c6f69a2018-04-24 08:29:13 +1000745 * Will trigger an automatic deferred table resizing if residency in the
746 * table grows beyond 70%.
Herbert Xuca268932016-09-19 19:00:09 +0800747 */
748static inline int rhltable_insert_key(
749 struct rhltable *hlt, const void *key, struct rhlist_head *list,
750 const struct rhashtable_params params)
751{
752 return PTR_ERR(__rhashtable_insert_fast(&hlt->ht, key, &list->rhead,
753 params, true));
754}
755
756/**
757 * rhltable_insert - insert object into hash list table
758 * @hlt: hash list table
759 * @list: pointer to hash list head inside object
760 * @params: hash table parameters
761 *
762 * Will take a per bucket spinlock to protect against mutual mutations
763 * on the same bucket. Multiple insertions may occur in parallel unless
764 * they map to the same bucket lock.
765 *
766 * It is safe to call this function from atomic context.
767 *
NeilBrown0c6f69a2018-04-24 08:29:13 +1000768 * Will trigger an automatic deferred table resizing if residency in the
769 * table grows beyond 70%.
Herbert Xuca268932016-09-19 19:00:09 +0800770 */
771static inline int rhltable_insert(
772 struct rhltable *hlt, struct rhlist_head *list,
773 const struct rhashtable_params params)
774{
775 const char *key = rht_obj(&hlt->ht, &list->rhead);
776
777 key += params.key_offset;
778
779 return rhltable_insert_key(hlt, key, list, params);
780}
781
782/**
Herbert Xu02fd97c2015-03-20 21:57:00 +1100783 * rhashtable_lookup_insert_fast - lookup and insert object into hash table
784 * @ht: hash table
785 * @obj: pointer to hash head inside object
786 * @params: hash table parameters
787 *
788 * Locks down the bucket chain in both the old and new table if a resize
789 * is in progress to ensure that writers can't remove from the old table
790 * and can't insert to the new table during the atomic operation of search
791 * and insertion. Searches for duplicates in both the old and new table if
792 * a resize is in progress.
793 *
794 * This lookup function may only be used for fixed key hash table (key_len
795 * parameter set). It will BUG() if used inappropriately.
796 *
797 * It is safe to call this function from atomic context.
798 *
NeilBrown0c6f69a2018-04-24 08:29:13 +1000799 * Will trigger an automatic deferred table resizing if residency in the
800 * table grows beyond 70%.
Herbert Xu02fd97c2015-03-20 21:57:00 +1100801 */
802static inline int rhashtable_lookup_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);
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200807 void *ret;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100808
809 BUG_ON(ht->p.obj_hashfn);
810
Herbert Xuca268932016-09-19 19:00:09 +0800811 ret = __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params,
812 false);
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200813 if (IS_ERR(ret))
814 return PTR_ERR(ret);
815
816 return ret == NULL ? 0 : -EEXIST;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100817}
818
819/**
Andreas Gruenbacherf9fe1c12017-03-18 00:36:15 +0100820 * rhashtable_lookup_get_insert_fast - lookup and insert object into hash table
821 * @ht: hash table
822 * @obj: pointer to hash head inside object
823 * @params: hash table parameters
824 *
825 * Just like rhashtable_lookup_insert_fast(), but this function returns the
826 * object if it exists, NULL if it did not and the insertion was successful,
827 * and an ERR_PTR otherwise.
828 */
829static inline void *rhashtable_lookup_get_insert_fast(
830 struct rhashtable *ht, struct rhash_head *obj,
831 const struct rhashtable_params params)
832{
833 const char *key = rht_obj(ht, obj);
834
835 BUG_ON(ht->p.obj_hashfn);
836
837 return __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params,
838 false);
839}
840
841/**
Herbert Xu02fd97c2015-03-20 21:57:00 +1100842 * rhashtable_lookup_insert_key - search and insert object to hash table
843 * with explicit key
844 * @ht: hash table
845 * @key: key
846 * @obj: pointer to hash head inside object
847 * @params: hash table parameters
848 *
849 * Locks down the bucket chain in both the old and new table if a resize
850 * is in progress to ensure that writers can't remove from the old table
851 * and can't insert to the new table during the atomic operation of search
852 * and insertion. Searches for duplicates in both the old and new table if
853 * a resize is in progress.
854 *
855 * Lookups may occur in parallel with hashtable mutations and resizing.
856 *
NeilBrown0c6f69a2018-04-24 08:29:13 +1000857 * Will trigger an automatic deferred table resizing if residency in the
858 * table grows beyond 70%.
Herbert Xu02fd97c2015-03-20 21:57:00 +1100859 *
860 * Returns zero on success.
861 */
862static inline int rhashtable_lookup_insert_key(
863 struct rhashtable *ht, const void *key, struct rhash_head *obj,
864 const struct rhashtable_params params)
865{
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200866 void *ret;
867
868 BUG_ON(!ht->p.obj_hashfn || !key);
869
Herbert Xuca268932016-09-19 19:00:09 +0800870 ret = __rhashtable_insert_fast(ht, key, obj, params, false);
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200871 if (IS_ERR(ret))
872 return PTR_ERR(ret);
873
874 return ret == NULL ? 0 : -EEXIST;
875}
876
877/**
878 * rhashtable_lookup_get_insert_key - lookup and insert object into hash table
879 * @ht: hash table
880 * @obj: pointer to hash head inside object
881 * @params: hash table parameters
882 * @data: pointer to element data already in hashes
883 *
884 * Just like rhashtable_lookup_insert_key(), but this function returns the
885 * object if it exists, NULL if it does not and the insertion was successful,
886 * and an ERR_PTR otherwise.
887 */
888static inline void *rhashtable_lookup_get_insert_key(
889 struct rhashtable *ht, const void *key, struct rhash_head *obj,
890 const struct rhashtable_params params)
891{
Herbert Xu02fd97c2015-03-20 21:57:00 +1100892 BUG_ON(!ht->p.obj_hashfn || !key);
893
Herbert Xuca268932016-09-19 19:00:09 +0800894 return __rhashtable_insert_fast(ht, key, obj, params, false);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100895}
896
Thomas Grafac833bd2015-03-24 14:18:18 +0100897/* Internal function, please use rhashtable_remove_fast() instead */
Herbert Xuca268932016-09-19 19:00:09 +0800898static inline int __rhashtable_remove_fast_one(
Herbert Xu02fd97c2015-03-20 21:57:00 +1100899 struct rhashtable *ht, struct bucket_table *tbl,
Herbert Xuca268932016-09-19 19:00:09 +0800900 struct rhash_head *obj, const struct rhashtable_params params,
901 bool rhlist)
Herbert Xu02fd97c2015-03-20 21:57:00 +1100902{
903 struct rhash_head __rcu **pprev;
904 struct rhash_head *he;
905 spinlock_t * lock;
Thomas Graf299e5c32015-03-24 14:18:17 +0100906 unsigned int hash;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100907 int err = -ENOENT;
908
909 hash = rht_head_hashfn(ht, tbl, obj, params);
910 lock = rht_bucket_lock(tbl, hash);
911
912 spin_lock_bh(lock);
913
Herbert Xuda204202017-02-11 19:26:47 +0800914 pprev = rht_bucket_var(tbl, hash);
915 rht_for_each_continue(he, *pprev, tbl, hash) {
Herbert Xuca268932016-09-19 19:00:09 +0800916 struct rhlist_head *list;
917
918 list = container_of(he, struct rhlist_head, rhead);
919
Herbert Xu02fd97c2015-03-20 21:57:00 +1100920 if (he != obj) {
Herbert Xuca268932016-09-19 19:00:09 +0800921 struct rhlist_head __rcu **lpprev;
922
Herbert Xu02fd97c2015-03-20 21:57:00 +1100923 pprev = &he->next;
Herbert Xuca268932016-09-19 19:00:09 +0800924
925 if (!rhlist)
926 continue;
927
928 do {
929 lpprev = &list->next;
930 list = rht_dereference_bucket(list->next,
931 tbl, hash);
932 } while (list && obj != &list->rhead);
933
934 if (!list)
935 continue;
936
937 list = rht_dereference_bucket(list->next, tbl, hash);
938 RCU_INIT_POINTER(*lpprev, list);
939 err = 0;
940 break;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100941 }
942
Herbert Xuca268932016-09-19 19:00:09 +0800943 obj = rht_dereference_bucket(obj->next, tbl, hash);
944 err = 1;
945
946 if (rhlist) {
947 list = rht_dereference_bucket(list->next, tbl, hash);
948 if (list) {
949 RCU_INIT_POINTER(list->rhead.next, obj);
950 obj = &list->rhead;
951 err = 0;
952 }
953 }
954
955 rcu_assign_pointer(*pprev, obj);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100956 break;
957 }
958
959 spin_unlock_bh(lock);
960
Herbert Xuca268932016-09-19 19:00:09 +0800961 if (err > 0) {
962 atomic_dec(&ht->nelems);
963 if (unlikely(ht->p.automatic_shrinking &&
964 rht_shrink_below_30(ht, tbl)))
965 schedule_work(&ht->run_work);
966 err = 0;
967 }
968
969 return err;
970}
971
972/* Internal function, please use rhashtable_remove_fast() instead */
973static inline int __rhashtable_remove_fast(
974 struct rhashtable *ht, struct rhash_head *obj,
975 const struct rhashtable_params params, bool rhlist)
976{
977 struct bucket_table *tbl;
978 int err;
979
980 rcu_read_lock();
981
982 tbl = rht_dereference_rcu(ht->tbl, ht);
983
984 /* Because we have already taken (and released) the bucket
985 * lock in old_tbl, if we find that future_tbl is not yet
986 * visible then that guarantees the entry to still be in
987 * the old tbl if it exists.
988 */
989 while ((err = __rhashtable_remove_fast_one(ht, tbl, obj, params,
990 rhlist)) &&
991 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
992 ;
993
994 rcu_read_unlock();
995
Herbert Xu02fd97c2015-03-20 21:57:00 +1100996 return err;
997}
998
999/**
1000 * rhashtable_remove_fast - remove object from hash table
1001 * @ht: hash table
1002 * @obj: pointer to hash head inside object
1003 * @params: hash table parameters
1004 *
1005 * Since the hash chain is single linked, the removal operation needs to
1006 * walk the bucket chain upon removal. The removal operation is thus
1007 * considerable slow if the hash table is not correctly sized.
1008 *
NeilBrown0c6f69a2018-04-24 08:29:13 +10001009 * Will automatically shrink the table if permitted when residency drops
1010 * below 30%.
Herbert Xu02fd97c2015-03-20 21:57:00 +11001011 *
1012 * Returns zero on success, -ENOENT if the entry could not be found.
1013 */
1014static inline int rhashtable_remove_fast(
1015 struct rhashtable *ht, struct rhash_head *obj,
1016 const struct rhashtable_params params)
1017{
Herbert Xuca268932016-09-19 19:00:09 +08001018 return __rhashtable_remove_fast(ht, obj, params, false);
1019}
Herbert Xu02fd97c2015-03-20 21:57:00 +11001020
Herbert Xuca268932016-09-19 19:00:09 +08001021/**
1022 * rhltable_remove - remove object from hash list table
1023 * @hlt: hash list table
1024 * @list: pointer to hash list head inside object
1025 * @params: hash table parameters
1026 *
1027 * Since the hash chain is single linked, the removal operation needs to
1028 * walk the bucket chain upon removal. The removal operation is thus
1029 * considerable slow if the hash table is not correctly sized.
1030 *
NeilBrown0c6f69a2018-04-24 08:29:13 +10001031 * Will automatically shrink the table if permitted when residency drops
1032 * below 30%
Herbert Xuca268932016-09-19 19:00:09 +08001033 *
1034 * Returns zero on success, -ENOENT if the entry could not be found.
1035 */
1036static inline int rhltable_remove(
1037 struct rhltable *hlt, struct rhlist_head *list,
1038 const struct rhashtable_params params)
1039{
1040 return __rhashtable_remove_fast(&hlt->ht, &list->rhead, params, true);
Herbert Xu02fd97c2015-03-20 21:57:00 +11001041}
1042
Tom Herbert3502cad2015-12-15 15:41:36 -08001043/* Internal function, please use rhashtable_replace_fast() instead */
1044static inline int __rhashtable_replace_fast(
1045 struct rhashtable *ht, struct bucket_table *tbl,
1046 struct rhash_head *obj_old, struct rhash_head *obj_new,
1047 const struct rhashtable_params params)
1048{
1049 struct rhash_head __rcu **pprev;
1050 struct rhash_head *he;
1051 spinlock_t *lock;
1052 unsigned int hash;
1053 int err = -ENOENT;
1054
1055 /* Minimally, the old and new objects must have same hash
1056 * (which should mean identifiers are the same).
1057 */
1058 hash = rht_head_hashfn(ht, tbl, obj_old, params);
1059 if (hash != rht_head_hashfn(ht, tbl, obj_new, params))
1060 return -EINVAL;
1061
1062 lock = rht_bucket_lock(tbl, hash);
1063
1064 spin_lock_bh(lock);
1065
Herbert Xuda204202017-02-11 19:26:47 +08001066 pprev = rht_bucket_var(tbl, hash);
1067 rht_for_each_continue(he, *pprev, tbl, hash) {
Tom Herbert3502cad2015-12-15 15:41:36 -08001068 if (he != obj_old) {
1069 pprev = &he->next;
1070 continue;
1071 }
1072
1073 rcu_assign_pointer(obj_new->next, obj_old->next);
1074 rcu_assign_pointer(*pprev, obj_new);
1075 err = 0;
1076 break;
1077 }
1078
1079 spin_unlock_bh(lock);
1080
1081 return err;
1082}
1083
1084/**
1085 * rhashtable_replace_fast - replace an object in hash table
1086 * @ht: hash table
1087 * @obj_old: pointer to hash head inside object being replaced
1088 * @obj_new: pointer to hash head inside object which is new
1089 * @params: hash table parameters
1090 *
1091 * Replacing an object doesn't affect the number of elements in the hash table
1092 * or bucket, so we don't need to worry about shrinking or expanding the
1093 * table here.
1094 *
1095 * Returns zero on success, -ENOENT if the entry could not be found,
1096 * -EINVAL if hash is not the same for the old and new objects.
1097 */
1098static inline int rhashtable_replace_fast(
1099 struct rhashtable *ht, struct rhash_head *obj_old,
1100 struct rhash_head *obj_new,
1101 const struct rhashtable_params params)
1102{
1103 struct bucket_table *tbl;
1104 int err;
1105
1106 rcu_read_lock();
1107
1108 tbl = rht_dereference_rcu(ht->tbl, ht);
1109
1110 /* Because we have already taken (and released) the bucket
1111 * lock in old_tbl, if we find that future_tbl is not yet
1112 * visible then that guarantees the entry to still be in
1113 * the old tbl if it exists.
1114 */
1115 while ((err = __rhashtable_replace_fast(ht, tbl, obj_old,
1116 obj_new, params)) &&
1117 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
1118 ;
1119
1120 rcu_read_unlock();
1121
1122 return err;
1123}
1124
Herbert Xu246779d2016-08-18 16:50:56 +08001125/* Obsolete function, do not use in new code. */
1126static inline int rhashtable_walk_init(struct rhashtable *ht,
1127 struct rhashtable_iter *iter, gfp_t gfp)
1128{
1129 rhashtable_walk_enter(ht, iter);
1130 return 0;
1131}
1132
Herbert Xuca268932016-09-19 19:00:09 +08001133/**
1134 * rhltable_walk_enter - Initialise an iterator
1135 * @hlt: Table to walk over
1136 * @iter: Hash table Iterator
1137 *
1138 * This function prepares a hash table walk.
1139 *
1140 * Note that if you restart a walk after rhashtable_walk_stop you
1141 * may see the same object twice. Also, you may miss objects if
1142 * there are removals in between rhashtable_walk_stop and the next
1143 * call to rhashtable_walk_start.
1144 *
1145 * For a completely stable walk you should construct your own data
1146 * structure outside the hash table.
1147 *
NeilBrown82266e92018-04-24 08:29:13 +10001148 * This function may be called from any process context, including
1149 * non-preemptable context, but cannot be called from softirq or
1150 * hardirq context.
Herbert Xuca268932016-09-19 19:00:09 +08001151 *
1152 * You must call rhashtable_walk_exit after this function returns.
1153 */
1154static inline void rhltable_walk_enter(struct rhltable *hlt,
1155 struct rhashtable_iter *iter)
1156{
1157 return rhashtable_walk_enter(&hlt->ht, iter);
1158}
1159
1160/**
1161 * rhltable_free_and_destroy - free elements and destroy hash list table
1162 * @hlt: the hash list table to destroy
1163 * @free_fn: callback to release resources of element
1164 * @arg: pointer passed to free_fn
1165 *
1166 * See documentation for rhashtable_free_and_destroy.
1167 */
1168static inline void rhltable_free_and_destroy(struct rhltable *hlt,
1169 void (*free_fn)(void *ptr,
1170 void *arg),
1171 void *arg)
1172{
1173 return rhashtable_free_and_destroy(&hlt->ht, free_fn, arg);
1174}
1175
1176static inline void rhltable_destroy(struct rhltable *hlt)
1177{
1178 return rhltable_free_and_destroy(hlt, NULL, NULL);
1179}
1180
Thomas Graf7e1e7762014-08-02 11:47:44 +02001181#endif /* _LINUX_RHASHTABLE_H */