blob: e507290cd2c7736ff3e84527a87748dfdc504d9c [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>
Ingo Molnarb2d09102017-02-04 01:27:20 +010028#include <linux/rculist.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 Xuda204202017-02-11 19:26:47 +080064 * @nest: Number of bits of first-level nested table.
Herbert Xu63d512d2015-03-14 13:57:24 +110065 * @rehash: Current bucket being rehashed
Herbert Xu988dfbd2015-03-10 09:27:55 +110066 * @hash_rnd: Random seed to fold into hash
Thomas Graf97defe12015-01-02 23:00:20 +010067 * @locks_mask: Mask to apply before accessing locks[]
68 * @locks: Array of spinlocks protecting individual buckets
Herbert Xueddee5ba2015-03-14 13:57:20 +110069 * @walkers: List of active walkers
Herbert Xu9d901bc2015-03-14 13:57:23 +110070 * @rcu: RCU structure for freeing the table
Herbert Xuc4db8842015-03-14 13:57:25 +110071 * @future_tbl: Table under construction during rehashing
Herbert Xuda204202017-02-11 19:26:47 +080072 * @ntbl: Nested table used when out of memory.
Thomas Graf97defe12015-01-02 23:00:20 +010073 * @buckets: size * hash buckets
74 */
Thomas Graf7e1e7762014-08-02 11:47:44 +020075struct bucket_table {
Herbert Xu63d512d2015-03-14 13:57:24 +110076 unsigned int size;
Herbert Xuda204202017-02-11 19:26:47 +080077 unsigned int nest;
Herbert Xu63d512d2015-03-14 13:57:24 +110078 unsigned int rehash;
Herbert Xu988dfbd2015-03-10 09:27:55 +110079 u32 hash_rnd;
Eric Dumazetb9ebafb2015-02-20 06:48:57 -080080 unsigned int locks_mask;
81 spinlock_t *locks;
Herbert Xueddee5ba2015-03-14 13:57:20 +110082 struct list_head walkers;
Herbert Xu9d901bc2015-03-14 13:57:23 +110083 struct rcu_head rcu;
Eric Dumazetb9ebafb2015-02-20 06:48:57 -080084
Herbert Xuc4db8842015-03-14 13:57:25 +110085 struct bucket_table __rcu *future_tbl;
86
Herbert Xuda204202017-02-11 19:26:47 +080087 struct rhash_head __rcu *buckets[] ____cacheline_aligned_in_smp;
Thomas Graf7e1e7762014-08-02 11:47:44 +020088};
89
Herbert Xu02fd97c2015-03-20 21:57:00 +110090/**
91 * struct rhashtable_compare_arg - Key for the function rhashtable_compare
92 * @ht: Hash table
93 * @key: Key to compare against
94 */
95struct rhashtable_compare_arg {
96 struct rhashtable *ht;
97 const void *key;
98};
99
Thomas Graf7e1e7762014-08-02 11:47:44 +0200100typedef u32 (*rht_hashfn_t)(const void *data, u32 len, u32 seed);
Patrick McHardy49f7b332015-03-25 13:07:45 +0000101typedef u32 (*rht_obj_hashfn_t)(const void *data, u32 len, u32 seed);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100102typedef int (*rht_obj_cmpfn_t)(struct rhashtable_compare_arg *arg,
103 const void *obj);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200104
105struct rhashtable;
106
107/**
108 * struct rhashtable_params - Hash table construction parameters
109 * @nelem_hint: Hint on number of elements, should be 75% of desired size
110 * @key_len: Length of key
111 * @key_offset: Offset of key in struct to be hashed
112 * @head_offset: Offset of rhash_head in struct to be hashed
Herbert Xu07ee0722015-05-15 11:30:47 +0800113 * @insecure_max_entries: Maximum number of entries (may be exceeded)
Herbert Xuc2e213c2015-03-18 20:01:16 +1100114 * @max_size: Maximum size while expanding
115 * @min_size: Minimum size while shrinking
Thomas Graff89bd6f2015-01-02 23:00:21 +0100116 * @nulls_base: Base value to generate nulls marker
Herbert Xuccd57b12015-03-24 00:50:28 +1100117 * @insecure_elasticity: Set to true to disable chain length checks
Thomas Grafb5e2c152015-03-24 20:42:19 +0000118 * @automatic_shrinking: Enable automatic shrinking of tables
Thomas Graf97defe12015-01-02 23:00:20 +0100119 * @locks_mul: Number of bucket locks to allocate per cpu (default: 128)
Herbert Xu31ccde22015-03-24 00:50:21 +1100120 * @hashfn: Hash function (default: jhash2 if !(key_len % 4), or jhash)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200121 * @obj_hashfn: Function to hash object
Herbert Xu02fd97c2015-03-20 21:57:00 +1100122 * @obj_cmpfn: Function to compare key with object
Thomas Graf7e1e7762014-08-02 11:47:44 +0200123 */
124struct rhashtable_params {
125 size_t nelem_hint;
126 size_t key_len;
127 size_t key_offset;
128 size_t head_offset;
Herbert Xu07ee0722015-05-15 11:30:47 +0800129 unsigned int insecure_max_entries;
Herbert Xuc2e213c2015-03-18 20:01:16 +1100130 unsigned int max_size;
131 unsigned int min_size;
Thomas Graff89bd6f2015-01-02 23:00:21 +0100132 u32 nulls_base;
Herbert Xuccd57b12015-03-24 00:50:28 +1100133 bool insecure_elasticity;
Thomas Grafb5e2c152015-03-24 20:42:19 +0000134 bool automatic_shrinking;
Thomas Graf97defe12015-01-02 23:00:20 +0100135 size_t locks_mul;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200136 rht_hashfn_t hashfn;
137 rht_obj_hashfn_t obj_hashfn;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100138 rht_obj_cmpfn_t obj_cmpfn;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200139};
140
141/**
142 * struct rhashtable - Hash table handle
143 * @tbl: Bucket table
144 * @nelems: Number of elements in table
Herbert Xu31ccde22015-03-24 00:50:21 +1100145 * @key_len: Key length for hashfn
Herbert Xuccd57b12015-03-24 00:50:28 +1100146 * @elasticity: Maximum chain length before rehash
Thomas Graf7e1e7762014-08-02 11:47:44 +0200147 * @p: Configuration parameters
Herbert Xuca268932016-09-19 19:00:09 +0800148 * @rhlist: True if this is an rhltable
Thomas Graf97defe12015-01-02 23:00:20 +0100149 * @run_work: Deferred worker to expand/shrink asynchronously
150 * @mutex: Mutex to protect current/future table swapping
Herbert Xuba7c95e2015-03-24 09:53:17 +1100151 * @lock: Spin lock to protect walker list
Thomas Graf7e1e7762014-08-02 11:47:44 +0200152 */
153struct rhashtable {
154 struct bucket_table __rcu *tbl;
Thomas Graf97defe12015-01-02 23:00:20 +0100155 atomic_t nelems;
Herbert Xu31ccde22015-03-24 00:50:21 +1100156 unsigned int key_len;
Herbert Xuccd57b12015-03-24 00:50:28 +1100157 unsigned int elasticity;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200158 struct rhashtable_params p;
Herbert Xuca268932016-09-19 19:00:09 +0800159 bool rhlist;
Ying Xue57699a42015-01-16 11:13:09 +0800160 struct work_struct run_work;
Thomas Graf97defe12015-01-02 23:00:20 +0100161 struct mutex mutex;
Herbert Xuba7c95e2015-03-24 09:53:17 +1100162 spinlock_t lock;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200163};
164
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100165/**
Herbert Xuca268932016-09-19 19:00:09 +0800166 * struct rhltable - Hash table with duplicate objects in a list
167 * @ht: Underlying rhtable
168 */
169struct rhltable {
170 struct rhashtable ht;
171};
172
173/**
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100174 * struct rhashtable_walker - Hash table walker
175 * @list: List entry on list of walkers
Herbert Xueddee5ba2015-03-14 13:57:20 +1100176 * @tbl: The table that we were walking over
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100177 */
178struct rhashtable_walker {
179 struct list_head list;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100180 struct bucket_table *tbl;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100181};
182
183/**
Herbert Xuca268932016-09-19 19:00:09 +0800184 * struct rhashtable_iter - Hash table iterator
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100185 * @ht: Table to iterate through
186 * @p: Current pointer
Herbert Xuca268932016-09-19 19:00:09 +0800187 * @list: Current hash list pointer
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100188 * @walker: Associated rhashtable walker
189 * @slot: Current slot
190 * @skip: Number of entries to skip in slot
191 */
192struct rhashtable_iter {
193 struct rhashtable *ht;
194 struct rhash_head *p;
Herbert Xuca268932016-09-19 19:00:09 +0800195 struct rhlist_head *list;
Herbert Xu246779d2016-08-18 16:50:56 +0800196 struct rhashtable_walker walker;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100197 unsigned int slot;
198 unsigned int skip;
199};
200
Thomas Graff89bd6f2015-01-02 23:00:21 +0100201static inline unsigned long rht_marker(const struct rhashtable *ht, u32 hash)
202{
203 return NULLS_MARKER(ht->p.nulls_base + hash);
204}
205
206#define INIT_RHT_NULLS_HEAD(ptr, ht, hash) \
207 ((ptr) = (typeof(ptr)) rht_marker(ht, hash))
208
209static inline bool rht_is_a_nulls(const struct rhash_head *ptr)
210{
211 return ((unsigned long) ptr & 1);
212}
213
214static inline unsigned long rht_get_nulls_value(const struct rhash_head *ptr)
215{
216 return ((unsigned long) ptr) >> 1;
217}
218
Herbert Xu02fd97c2015-03-20 21:57:00 +1100219static inline void *rht_obj(const struct rhashtable *ht,
220 const struct rhash_head *he)
221{
222 return (char *)he - ht->p.head_offset;
223}
224
225static inline unsigned int rht_bucket_index(const struct bucket_table *tbl,
226 unsigned int hash)
227{
228 return (hash >> RHT_HASH_RESERVED_SPACE) & (tbl->size - 1);
229}
230
231static inline unsigned int rht_key_hashfn(
232 struct rhashtable *ht, const struct bucket_table *tbl,
233 const void *key, const struct rhashtable_params params)
234{
Thomas Graf299e5c32015-03-24 14:18:17 +0100235 unsigned int hash;
Herbert Xude91b252015-03-24 00:50:20 +1100236
Herbert Xu31ccde22015-03-24 00:50:21 +1100237 /* params must be equal to ht->p if it isn't constant. */
238 if (!__builtin_constant_p(params.key_len))
239 hash = ht->p.hashfn(key, ht->key_len, tbl->hash_rnd);
240 else if (params.key_len) {
Thomas Graf299e5c32015-03-24 14:18:17 +0100241 unsigned int key_len = params.key_len;
Herbert Xu31ccde22015-03-24 00:50:21 +1100242
243 if (params.hashfn)
244 hash = params.hashfn(key, key_len, tbl->hash_rnd);
245 else if (key_len & (sizeof(u32) - 1))
246 hash = jhash(key, key_len, tbl->hash_rnd);
247 else
248 hash = jhash2(key, key_len / sizeof(u32),
249 tbl->hash_rnd);
250 } else {
Thomas Graf299e5c32015-03-24 14:18:17 +0100251 unsigned int key_len = ht->p.key_len;
Herbert Xu31ccde22015-03-24 00:50:21 +1100252
253 if (params.hashfn)
254 hash = params.hashfn(key, key_len, tbl->hash_rnd);
255 else
256 hash = jhash(key, key_len, tbl->hash_rnd);
257 }
258
259 return rht_bucket_index(tbl, hash);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100260}
261
262static inline unsigned int rht_head_hashfn(
263 struct rhashtable *ht, const struct bucket_table *tbl,
264 const struct rhash_head *he, const struct rhashtable_params params)
265{
266 const char *ptr = rht_obj(ht, he);
267
268 return likely(params.obj_hashfn) ?
Patrick McHardy49f7b332015-03-25 13:07:45 +0000269 rht_bucket_index(tbl, params.obj_hashfn(ptr, params.key_len ?:
270 ht->p.key_len,
271 tbl->hash_rnd)) :
Herbert Xu02fd97c2015-03-20 21:57:00 +1100272 rht_key_hashfn(ht, tbl, ptr + params.key_offset, params);
273}
274
275/**
276 * rht_grow_above_75 - returns true if nelems > 0.75 * table-size
277 * @ht: hash table
278 * @tbl: current table
279 */
280static inline bool rht_grow_above_75(const struct rhashtable *ht,
281 const struct bucket_table *tbl)
282{
283 /* Expand table when exceeding 75% load */
284 return atomic_read(&ht->nelems) > (tbl->size / 4 * 3) &&
285 (!ht->p.max_size || tbl->size < ht->p.max_size);
286}
287
288/**
289 * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size
290 * @ht: hash table
291 * @tbl: current table
292 */
293static inline bool rht_shrink_below_30(const struct rhashtable *ht,
294 const struct bucket_table *tbl)
295{
296 /* Shrink table beneath 30% load */
297 return atomic_read(&ht->nelems) < (tbl->size * 3 / 10) &&
298 tbl->size > ht->p.min_size;
299}
300
Herbert Xuccd57b12015-03-24 00:50:28 +1100301/**
302 * rht_grow_above_100 - returns true if nelems > table-size
303 * @ht: hash table
304 * @tbl: current table
305 */
306static inline bool rht_grow_above_100(const struct rhashtable *ht,
307 const struct bucket_table *tbl)
308{
Johannes Berg1d8dc3d2015-04-23 16:38:43 +0200309 return atomic_read(&ht->nelems) > tbl->size &&
310 (!ht->p.max_size || tbl->size < ht->p.max_size);
Herbert Xuccd57b12015-03-24 00:50:28 +1100311}
312
Herbert Xu07ee0722015-05-15 11:30:47 +0800313/**
314 * rht_grow_above_max - returns true if table is above maximum
315 * @ht: hash table
316 * @tbl: current table
317 */
318static inline bool rht_grow_above_max(const struct rhashtable *ht,
319 const struct bucket_table *tbl)
320{
321 return ht->p.insecure_max_entries &&
322 atomic_read(&ht->nelems) >= ht->p.insecure_max_entries;
323}
324
Herbert Xu02fd97c2015-03-20 21:57:00 +1100325/* The bucket lock is selected based on the hash and protects mutations
326 * on a group of hash buckets.
327 *
328 * A maximum of tbl->size/2 bucket locks is allocated. This ensures that
329 * a single lock always covers both buckets which may both contains
330 * entries which link to the same bucket of the old table during resizing.
331 * This allows to simplify the locking as locking the bucket in both
332 * tables during resize always guarantee protection.
333 *
334 * IMPORTANT: When holding the bucket lock of both the old and new table
335 * during expansions and shrinking, the old bucket lock must always be
336 * acquired first.
337 */
338static inline spinlock_t *rht_bucket_lock(const struct bucket_table *tbl,
339 unsigned int hash)
340{
341 return &tbl->locks[hash & tbl->locks_mask];
342}
343
Thomas Graf7e1e7762014-08-02 11:47:44 +0200344#ifdef CONFIG_PROVE_LOCKING
Thomas Graf97defe12015-01-02 23:00:20 +0100345int lockdep_rht_mutex_is_held(struct rhashtable *ht);
Thomas Graf88d6ed12015-01-02 23:00:16 +0100346int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200347#else
Thomas Graf97defe12015-01-02 23:00:20 +0100348static inline int lockdep_rht_mutex_is_held(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200349{
350 return 1;
351}
Thomas Graf88d6ed12015-01-02 23:00:16 +0100352
353static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl,
354 u32 hash)
355{
356 return 1;
357}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200358#endif /* CONFIG_PROVE_LOCKING */
359
Herbert Xu488fb86e2015-03-20 21:56:59 +1100360int rhashtable_init(struct rhashtable *ht,
361 const struct rhashtable_params *params);
Herbert Xuca268932016-09-19 19:00:09 +0800362int rhltable_init(struct rhltable *hlt,
363 const struct rhashtable_params *params);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200364
Herbert Xuca268932016-09-19 19:00:09 +0800365void *rhashtable_insert_slow(struct rhashtable *ht, const void *key,
366 struct rhash_head *obj);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200367
Herbert Xu246779d2016-08-18 16:50:56 +0800368void rhashtable_walk_enter(struct rhashtable *ht,
369 struct rhashtable_iter *iter);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100370void rhashtable_walk_exit(struct rhashtable_iter *iter);
371int rhashtable_walk_start(struct rhashtable_iter *iter) __acquires(RCU);
372void *rhashtable_walk_next(struct rhashtable_iter *iter);
373void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU);
374
Thomas Graf6b6f3022015-03-24 14:18:20 +0100375void rhashtable_free_and_destroy(struct rhashtable *ht,
376 void (*free_fn)(void *ptr, void *arg),
377 void *arg);
Thomas Graf97defe12015-01-02 23:00:20 +0100378void rhashtable_destroy(struct rhashtable *ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200379
Herbert Xuda204202017-02-11 19:26:47 +0800380struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl,
381 unsigned int hash);
382struct rhash_head __rcu **rht_bucket_nested_insert(struct rhashtable *ht,
383 struct bucket_table *tbl,
384 unsigned int hash);
385
Thomas Graf7e1e7762014-08-02 11:47:44 +0200386#define rht_dereference(p, ht) \
387 rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht))
388
389#define rht_dereference_rcu(p, ht) \
390 rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht))
391
Thomas Graf88d6ed12015-01-02 23:00:16 +0100392#define rht_dereference_bucket(p, tbl, hash) \
393 rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200394
Thomas Graf88d6ed12015-01-02 23:00:16 +0100395#define rht_dereference_bucket_rcu(p, tbl, hash) \
396 rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash))
397
398#define rht_entry(tpos, pos, member) \
399 ({ tpos = container_of(pos, typeof(*tpos), member); 1; })
400
Herbert Xuda204202017-02-11 19:26:47 +0800401static inline struct rhash_head __rcu *const *rht_bucket(
402 const struct bucket_table *tbl, unsigned int hash)
403{
404 return unlikely(tbl->nest) ? rht_bucket_nested(tbl, hash) :
405 &tbl->buckets[hash];
406}
407
408static inline struct rhash_head __rcu **rht_bucket_var(
409 struct bucket_table *tbl, unsigned int hash)
410{
411 return unlikely(tbl->nest) ? rht_bucket_nested(tbl, hash) :
412 &tbl->buckets[hash];
413}
414
415static inline struct rhash_head __rcu **rht_bucket_insert(
416 struct rhashtable *ht, struct bucket_table *tbl, unsigned int hash)
417{
418 return unlikely(tbl->nest) ? rht_bucket_nested_insert(ht, tbl, hash) :
419 &tbl->buckets[hash];
420}
421
Thomas Graf88d6ed12015-01-02 23:00:16 +0100422/**
423 * rht_for_each_continue - continue iterating over hash chain
424 * @pos: the &struct rhash_head to use as a loop cursor.
425 * @head: the previous &struct rhash_head to continue from
426 * @tbl: the &struct bucket_table
427 * @hash: the hash value / bucket index
428 */
429#define rht_for_each_continue(pos, head, tbl, hash) \
430 for (pos = rht_dereference_bucket(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100431 !rht_is_a_nulls(pos); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100432 pos = rht_dereference_bucket((pos)->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200433
434/**
435 * rht_for_each - iterate over hash chain
Thomas Graf88d6ed12015-01-02 23:00:16 +0100436 * @pos: the &struct rhash_head to use as a loop cursor.
437 * @tbl: the &struct bucket_table
438 * @hash: the hash value / bucket index
Thomas Graf7e1e7762014-08-02 11:47:44 +0200439 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100440#define rht_for_each(pos, tbl, hash) \
Herbert Xuda204202017-02-11 19:26:47 +0800441 rht_for_each_continue(pos, *rht_bucket(tbl, hash), tbl, hash)
Thomas Graf88d6ed12015-01-02 23:00:16 +0100442
443/**
444 * rht_for_each_entry_continue - continue iterating over hash chain
445 * @tpos: the type * to use as a loop cursor.
446 * @pos: the &struct rhash_head to use as a loop cursor.
447 * @head: the previous &struct rhash_head to continue from
448 * @tbl: the &struct bucket_table
449 * @hash: the hash value / bucket index
450 * @member: name of the &struct rhash_head within the hashable struct.
451 */
452#define rht_for_each_entry_continue(tpos, pos, head, tbl, hash, member) \
453 for (pos = rht_dereference_bucket(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100454 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100455 pos = rht_dereference_bucket((pos)->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200456
457/**
458 * rht_for_each_entry - iterate over hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100459 * @tpos: the type * to use as a loop cursor.
460 * @pos: the &struct rhash_head to use as a loop cursor.
461 * @tbl: the &struct bucket_table
462 * @hash: the hash value / bucket index
463 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200464 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100465#define rht_for_each_entry(tpos, pos, tbl, hash, member) \
Herbert Xuda204202017-02-11 19:26:47 +0800466 rht_for_each_entry_continue(tpos, pos, *rht_bucket(tbl, hash), \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100467 tbl, hash, member)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200468
469/**
470 * rht_for_each_entry_safe - safely iterate over hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100471 * @tpos: the type * to use as a loop cursor.
472 * @pos: the &struct rhash_head to use as a loop cursor.
473 * @next: the &struct rhash_head to use as next in loop cursor.
474 * @tbl: the &struct bucket_table
475 * @hash: the hash value / bucket index
476 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200477 *
478 * This hash chain list-traversal primitive allows for the looped code to
479 * remove the loop cursor from the list.
480 */
Herbert Xuda204202017-02-11 19:26:47 +0800481#define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \
482 for (pos = rht_dereference_bucket(*rht_bucket(tbl, hash), tbl, hash), \
483 next = !rht_is_a_nulls(pos) ? \
484 rht_dereference_bucket(pos->next, tbl, hash) : NULL; \
485 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
486 pos = next, \
487 next = !rht_is_a_nulls(pos) ? \
Patrick McHardy607954b2015-01-21 11:12:13 +0000488 rht_dereference_bucket(pos->next, tbl, hash) : NULL)
Thomas Graf88d6ed12015-01-02 23:00:16 +0100489
490/**
491 * rht_for_each_rcu_continue - continue iterating over rcu hash chain
492 * @pos: the &struct rhash_head to use as a loop cursor.
493 * @head: the previous &struct rhash_head to continue from
494 * @tbl: the &struct bucket_table
495 * @hash: the hash value / bucket index
496 *
497 * This hash chain list-traversal primitive may safely run concurrently with
498 * the _rcu mutation primitives such as rhashtable_insert() as long as the
499 * traversal is guarded by rcu_read_lock().
500 */
501#define rht_for_each_rcu_continue(pos, head, tbl, hash) \
502 for (({barrier(); }), \
503 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100504 !rht_is_a_nulls(pos); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100505 pos = rcu_dereference_raw(pos->next))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200506
507/**
508 * rht_for_each_rcu - iterate over rcu hash chain
Thomas Graf88d6ed12015-01-02 23:00:16 +0100509 * @pos: the &struct rhash_head to use as a loop cursor.
510 * @tbl: the &struct bucket_table
511 * @hash: the hash value / bucket index
Thomas Graf7e1e7762014-08-02 11:47:44 +0200512 *
513 * This hash chain list-traversal primitive may safely run concurrently with
Thomas Graf88d6ed12015-01-02 23:00:16 +0100514 * the _rcu mutation primitives such as rhashtable_insert() as long as the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200515 * traversal is guarded by rcu_read_lock().
516 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100517#define rht_for_each_rcu(pos, tbl, hash) \
Herbert Xuda204202017-02-11 19:26:47 +0800518 rht_for_each_rcu_continue(pos, *rht_bucket(tbl, hash), tbl, hash)
Thomas Graf88d6ed12015-01-02 23:00:16 +0100519
520/**
521 * rht_for_each_entry_rcu_continue - continue iterating over rcu hash chain
522 * @tpos: the type * to use as a loop cursor.
523 * @pos: the &struct rhash_head to use as a loop cursor.
524 * @head: the previous &struct rhash_head to continue from
525 * @tbl: the &struct bucket_table
526 * @hash: the hash value / bucket index
527 * @member: name of the &struct rhash_head within the hashable struct.
528 *
529 * This hash chain list-traversal primitive may safely run concurrently with
530 * the _rcu mutation primitives such as rhashtable_insert() as long as the
531 * traversal is guarded by rcu_read_lock().
532 */
533#define rht_for_each_entry_rcu_continue(tpos, pos, head, tbl, hash, member) \
534 for (({barrier(); }), \
535 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100536 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100537 pos = rht_dereference_bucket_rcu(pos->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200538
539/**
540 * rht_for_each_entry_rcu - iterate over rcu hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100541 * @tpos: the type * to use as a loop cursor.
542 * @pos: the &struct rhash_head to use as a loop cursor.
543 * @tbl: the &struct bucket_table
544 * @hash: the hash value / bucket index
545 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200546 *
547 * This hash chain list-traversal primitive may safely run concurrently with
Thomas Graf88d6ed12015-01-02 23:00:16 +0100548 * the _rcu mutation primitives such as rhashtable_insert() as long as the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200549 * traversal is guarded by rcu_read_lock().
550 */
Herbert Xuda204202017-02-11 19:26:47 +0800551#define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \
552 rht_for_each_entry_rcu_continue(tpos, pos, *rht_bucket(tbl, hash), \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100553 tbl, hash, member)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200554
Herbert Xuca268932016-09-19 19:00:09 +0800555/**
556 * rhl_for_each_rcu - iterate over rcu hash table list
557 * @pos: the &struct rlist_head to use as a loop cursor.
558 * @list: the head of the list
559 *
560 * This hash chain list-traversal primitive should be used on the
561 * list returned by rhltable_lookup.
562 */
563#define rhl_for_each_rcu(pos, list) \
564 for (pos = list; pos; pos = rcu_dereference_raw(pos->next))
565
566/**
567 * rhl_for_each_entry_rcu - iterate over rcu hash table list of given type
568 * @tpos: the type * to use as a loop cursor.
569 * @pos: the &struct rlist_head to use as a loop cursor.
570 * @list: the head of the list
571 * @member: name of the &struct rlist_head within the hashable struct.
572 *
573 * This hash chain list-traversal primitive should be used on the
574 * list returned by rhltable_lookup.
575 */
576#define rhl_for_each_entry_rcu(tpos, pos, list, member) \
577 for (pos = list; pos && rht_entry(tpos, pos, member); \
578 pos = rcu_dereference_raw(pos->next))
579
Herbert Xu02fd97c2015-03-20 21:57:00 +1100580static inline int rhashtable_compare(struct rhashtable_compare_arg *arg,
581 const void *obj)
582{
583 struct rhashtable *ht = arg->ht;
584 const char *ptr = obj;
585
586 return memcmp(ptr + ht->p.key_offset, arg->key, ht->p.key_len);
587}
588
Herbert Xuca268932016-09-19 19:00:09 +0800589/* Internal function, do not use. */
590static inline struct rhash_head *__rhashtable_lookup(
Herbert Xu02fd97c2015-03-20 21:57:00 +1100591 struct rhashtable *ht, const void *key,
592 const struct rhashtable_params params)
593{
594 struct rhashtable_compare_arg arg = {
595 .ht = ht,
596 .key = key,
597 };
Herbert Xuda204202017-02-11 19:26:47 +0800598 struct bucket_table *tbl;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100599 struct rhash_head *he;
Thomas Graf299e5c32015-03-24 14:18:17 +0100600 unsigned int hash;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100601
Herbert Xu02fd97c2015-03-20 21:57:00 +1100602 tbl = rht_dereference_rcu(ht->tbl, ht);
603restart:
604 hash = rht_key_hashfn(ht, tbl, key, params);
605 rht_for_each_rcu(he, tbl, hash) {
606 if (params.obj_cmpfn ?
607 params.obj_cmpfn(&arg, rht_obj(ht, he)) :
608 rhashtable_compare(&arg, rht_obj(ht, he)))
609 continue;
Herbert Xuca268932016-09-19 19:00:09 +0800610 return he;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100611 }
612
613 /* Ensure we see any new tables. */
614 smp_rmb();
615
616 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
617 if (unlikely(tbl))
618 goto restart;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100619
620 return NULL;
621}
622
Herbert Xuca268932016-09-19 19:00:09 +0800623/**
624 * rhashtable_lookup - search hash table
625 * @ht: hash table
626 * @key: the pointer to the key
627 * @params: hash table parameters
628 *
629 * Computes the hash value for the key and traverses the bucket chain looking
630 * for a entry with an identical key. The first matching entry is returned.
631 *
632 * This must only be called under the RCU read lock.
633 *
634 * Returns the first entry on which the compare function returned true.
635 */
636static inline void *rhashtable_lookup(
637 struct rhashtable *ht, const void *key,
638 const struct rhashtable_params params)
639{
640 struct rhash_head *he = __rhashtable_lookup(ht, key, params);
641
642 return he ? rht_obj(ht, he) : NULL;
643}
644
645/**
646 * rhashtable_lookup_fast - search hash table, without RCU read lock
647 * @ht: hash table
648 * @key: the pointer to the key
649 * @params: hash table parameters
650 *
651 * Computes the hash value for the key and traverses the bucket chain looking
652 * for a entry with an identical key. The first matching entry is returned.
653 *
654 * Only use this function when you have other mechanisms guaranteeing
655 * that the object won't go away after the RCU read lock is released.
656 *
657 * Returns the first entry on which the compare function returned true.
658 */
659static inline void *rhashtable_lookup_fast(
660 struct rhashtable *ht, const void *key,
661 const struct rhashtable_params params)
662{
663 void *obj;
664
665 rcu_read_lock();
666 obj = rhashtable_lookup(ht, key, params);
667 rcu_read_unlock();
668
669 return obj;
670}
671
672/**
673 * rhltable_lookup - search hash list table
674 * @hlt: hash table
675 * @key: the pointer to the key
676 * @params: hash table parameters
677 *
678 * Computes the hash value for the key and traverses the bucket chain looking
679 * for a entry with an identical key. All matching entries are returned
680 * in a list.
681 *
682 * This must only be called under the RCU read lock.
683 *
684 * Returns the list of entries that match the given key.
685 */
686static inline struct rhlist_head *rhltable_lookup(
687 struct rhltable *hlt, const void *key,
688 const struct rhashtable_params params)
689{
690 struct rhash_head *he = __rhashtable_lookup(&hlt->ht, key, params);
691
692 return he ? container_of(he, struct rhlist_head, rhead) : NULL;
693}
694
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200695/* Internal function, please use rhashtable_insert_fast() instead. This
696 * function returns the existing element already in hashes in there is a clash,
697 * otherwise it returns an error via ERR_PTR().
698 */
699static inline void *__rhashtable_insert_fast(
Herbert Xu02fd97c2015-03-20 21:57:00 +1100700 struct rhashtable *ht, const void *key, struct rhash_head *obj,
Herbert Xuca268932016-09-19 19:00:09 +0800701 const struct rhashtable_params params, bool rhlist)
Herbert Xu02fd97c2015-03-20 21:57:00 +1100702{
703 struct rhashtable_compare_arg arg = {
704 .ht = ht,
705 .key = key,
706 };
Herbert Xuca268932016-09-19 19:00:09 +0800707 struct rhash_head __rcu **pprev;
708 struct bucket_table *tbl;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100709 struct rhash_head *head;
710 spinlock_t *lock;
Thomas Graf299e5c32015-03-24 14:18:17 +0100711 unsigned int hash;
Herbert Xuca268932016-09-19 19:00:09 +0800712 int elasticity;
713 void *data;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100714
715 rcu_read_lock();
716
717 tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xuca268932016-09-19 19:00:09 +0800718 hash = rht_head_hashfn(ht, tbl, obj, params);
719 lock = rht_bucket_lock(tbl, hash);
720 spin_lock_bh(lock);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100721
Herbert Xuca268932016-09-19 19:00:09 +0800722 if (unlikely(rht_dereference_bucket(tbl->future_tbl, tbl, hash))) {
723slow_path:
Herbert Xub8244782015-03-24 00:50:26 +1100724 spin_unlock_bh(lock);
Herbert Xuca268932016-09-19 19:00:09 +0800725 rcu_read_unlock();
726 return rhashtable_insert_slow(ht, key, obj);
Herbert Xub8244782015-03-24 00:50:26 +1100727 }
728
Herbert Xuca268932016-09-19 19:00:09 +0800729 elasticity = ht->elasticity;
Herbert Xuda204202017-02-11 19:26:47 +0800730 pprev = rht_bucket_insert(ht, tbl, hash);
731 data = ERR_PTR(-ENOMEM);
732 if (!pprev)
733 goto out;
734
735 rht_for_each_continue(head, *pprev, tbl, hash) {
Herbert Xuca268932016-09-19 19:00:09 +0800736 struct rhlist_head *plist;
737 struct rhlist_head *list;
Herbert Xu3cf92222015-12-03 20:41:29 +0800738
Herbert Xuca268932016-09-19 19:00:09 +0800739 elasticity--;
740 if (!key ||
741 (params.obj_cmpfn ?
742 params.obj_cmpfn(&arg, rht_obj(ht, head)) :
743 rhashtable_compare(&arg, rht_obj(ht, head))))
744 continue;
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200745
Herbert Xuca268932016-09-19 19:00:09 +0800746 data = rht_obj(ht, head);
747
748 if (!rhlist)
749 goto out;
750
751
752 list = container_of(obj, struct rhlist_head, rhead);
753 plist = container_of(head, struct rhlist_head, rhead);
754
755 RCU_INIT_POINTER(list->next, plist);
756 head = rht_dereference_bucket(head->next, tbl, hash);
757 RCU_INIT_POINTER(list->rhead.next, head);
758 rcu_assign_pointer(*pprev, obj);
759
760 goto good;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100761 }
762
Herbert Xuca268932016-09-19 19:00:09 +0800763 if (elasticity <= 0)
764 goto slow_path;
765
766 data = ERR_PTR(-E2BIG);
Herbert Xu07ee0722015-05-15 11:30:47 +0800767 if (unlikely(rht_grow_above_max(ht, tbl)))
768 goto out;
769
Herbert Xuca268932016-09-19 19:00:09 +0800770 if (unlikely(rht_grow_above_100(ht, tbl)))
771 goto slow_path;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100772
Herbert Xuda204202017-02-11 19:26:47 +0800773 head = rht_dereference_bucket(*pprev, tbl, hash);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100774
775 RCU_INIT_POINTER(obj->next, head);
Herbert Xuca268932016-09-19 19:00:09 +0800776 if (rhlist) {
777 struct rhlist_head *list;
778
779 list = container_of(obj, struct rhlist_head, rhead);
780 RCU_INIT_POINTER(list->next, NULL);
781 }
Herbert Xu02fd97c2015-03-20 21:57:00 +1100782
Herbert Xuda204202017-02-11 19:26:47 +0800783 rcu_assign_pointer(*pprev, obj);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100784
785 atomic_inc(&ht->nelems);
786 if (rht_grow_above_75(ht, tbl))
787 schedule_work(&ht->run_work);
788
Herbert Xuca268932016-09-19 19:00:09 +0800789good:
790 data = NULL;
791
Herbert Xu02fd97c2015-03-20 21:57:00 +1100792out:
793 spin_unlock_bh(lock);
794 rcu_read_unlock();
795
Herbert Xuca268932016-09-19 19:00:09 +0800796 return data;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100797}
798
799/**
800 * rhashtable_insert_fast - insert object into hash table
801 * @ht: hash table
802 * @obj: pointer to hash head inside object
803 * @params: hash table parameters
804 *
805 * Will take a per bucket spinlock to protect against mutual mutations
806 * on the same bucket. Multiple insertions may occur in parallel unless
807 * they map to the same bucket lock.
808 *
809 * It is safe to call this function from atomic context.
810 *
811 * Will trigger an automatic deferred table resizing if the size grows
812 * beyond the watermark indicated by grow_decision() which can be passed
813 * to rhashtable_init().
814 */
815static inline int rhashtable_insert_fast(
816 struct rhashtable *ht, struct rhash_head *obj,
817 const struct rhashtable_params params)
818{
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200819 void *ret;
820
Herbert Xuca268932016-09-19 19:00:09 +0800821 ret = __rhashtable_insert_fast(ht, NULL, obj, params, false);
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200822 if (IS_ERR(ret))
823 return PTR_ERR(ret);
824
825 return ret == NULL ? 0 : -EEXIST;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100826}
827
828/**
Herbert Xuca268932016-09-19 19:00:09 +0800829 * rhltable_insert_key - insert object into hash list table
830 * @hlt: hash list table
831 * @key: the pointer to the key
832 * @list: pointer to hash list head inside object
833 * @params: hash table parameters
834 *
835 * Will take a per bucket spinlock to protect against mutual mutations
836 * on the same bucket. Multiple insertions may occur in parallel unless
837 * they map to the same bucket lock.
838 *
839 * It is safe to call this function from atomic context.
840 *
841 * Will trigger an automatic deferred table resizing if the size grows
842 * beyond the watermark indicated by grow_decision() which can be passed
843 * to rhashtable_init().
844 */
845static inline int rhltable_insert_key(
846 struct rhltable *hlt, const void *key, struct rhlist_head *list,
847 const struct rhashtable_params params)
848{
849 return PTR_ERR(__rhashtable_insert_fast(&hlt->ht, key, &list->rhead,
850 params, true));
851}
852
853/**
854 * rhltable_insert - insert object into hash list table
855 * @hlt: hash list table
856 * @list: pointer to hash list head inside object
857 * @params: hash table parameters
858 *
859 * Will take a per bucket spinlock to protect against mutual mutations
860 * on the same bucket. Multiple insertions may occur in parallel unless
861 * they map to the same bucket lock.
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 rhltable_insert(
870 struct rhltable *hlt, struct rhlist_head *list,
871 const struct rhashtable_params params)
872{
873 const char *key = rht_obj(&hlt->ht, &list->rhead);
874
875 key += params.key_offset;
876
877 return rhltable_insert_key(hlt, key, list, params);
878}
879
880/**
Herbert Xu02fd97c2015-03-20 21:57:00 +1100881 * rhashtable_lookup_insert_fast - lookup and insert object into hash table
882 * @ht: hash table
883 * @obj: pointer to hash head inside object
884 * @params: hash table parameters
885 *
886 * Locks down the bucket chain in both the old and new table if a resize
887 * is in progress to ensure that writers can't remove from the old table
888 * and can't insert to the new table during the atomic operation of search
889 * and insertion. Searches for duplicates in both the old and new table if
890 * a resize is in progress.
891 *
892 * This lookup function may only be used for fixed key hash table (key_len
893 * parameter set). It will BUG() if used inappropriately.
894 *
895 * It is safe to call this function from atomic context.
896 *
897 * Will trigger an automatic deferred table resizing if the size grows
898 * beyond the watermark indicated by grow_decision() which can be passed
899 * to rhashtable_init().
900 */
901static inline int rhashtable_lookup_insert_fast(
902 struct rhashtable *ht, struct rhash_head *obj,
903 const struct rhashtable_params params)
904{
905 const char *key = rht_obj(ht, obj);
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200906 void *ret;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100907
908 BUG_ON(ht->p.obj_hashfn);
909
Herbert Xuca268932016-09-19 19:00:09 +0800910 ret = __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params,
911 false);
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200912 if (IS_ERR(ret))
913 return PTR_ERR(ret);
914
915 return ret == NULL ? 0 : -EEXIST;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100916}
917
918/**
Andreas Gruenbacherf9fe1c12017-03-18 00:36:15 +0100919 * rhashtable_lookup_get_insert_fast - lookup and insert object into hash table
920 * @ht: hash table
921 * @obj: pointer to hash head inside object
922 * @params: hash table parameters
923 *
924 * Just like rhashtable_lookup_insert_fast(), but this function returns the
925 * object if it exists, NULL if it did not and the insertion was successful,
926 * and an ERR_PTR otherwise.
927 */
928static inline void *rhashtable_lookup_get_insert_fast(
929 struct rhashtable *ht, struct rhash_head *obj,
930 const struct rhashtable_params params)
931{
932 const char *key = rht_obj(ht, obj);
933
934 BUG_ON(ht->p.obj_hashfn);
935
936 return __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params,
937 false);
938}
939
940/**
Herbert Xu02fd97c2015-03-20 21:57:00 +1100941 * rhashtable_lookup_insert_key - search and insert object to hash table
942 * with explicit key
943 * @ht: hash table
944 * @key: key
945 * @obj: pointer to hash head inside object
946 * @params: hash table parameters
947 *
948 * Locks down the bucket chain in both the old and new table if a resize
949 * is in progress to ensure that writers can't remove from the old table
950 * and can't insert to the new table during the atomic operation of search
951 * and insertion. Searches for duplicates in both the old and new table if
952 * a resize is in progress.
953 *
954 * Lookups may occur in parallel with hashtable mutations and resizing.
955 *
956 * Will trigger an automatic deferred table resizing if the size grows
957 * beyond the watermark indicated by grow_decision() which can be passed
958 * to rhashtable_init().
959 *
960 * Returns zero on success.
961 */
962static inline int rhashtable_lookup_insert_key(
963 struct rhashtable *ht, const void *key, struct rhash_head *obj,
964 const struct rhashtable_params params)
965{
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200966 void *ret;
967
968 BUG_ON(!ht->p.obj_hashfn || !key);
969
Herbert Xuca268932016-09-19 19:00:09 +0800970 ret = __rhashtable_insert_fast(ht, key, obj, params, false);
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200971 if (IS_ERR(ret))
972 return PTR_ERR(ret);
973
974 return ret == NULL ? 0 : -EEXIST;
975}
976
977/**
978 * rhashtable_lookup_get_insert_key - lookup and insert object into hash table
979 * @ht: hash table
980 * @obj: pointer to hash head inside object
981 * @params: hash table parameters
982 * @data: pointer to element data already in hashes
983 *
984 * Just like rhashtable_lookup_insert_key(), but this function returns the
985 * object if it exists, NULL if it does not and the insertion was successful,
986 * and an ERR_PTR otherwise.
987 */
988static inline void *rhashtable_lookup_get_insert_key(
989 struct rhashtable *ht, const void *key, struct rhash_head *obj,
990 const struct rhashtable_params params)
991{
Herbert Xu02fd97c2015-03-20 21:57:00 +1100992 BUG_ON(!ht->p.obj_hashfn || !key);
993
Herbert Xuca268932016-09-19 19:00:09 +0800994 return __rhashtable_insert_fast(ht, key, obj, params, false);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100995}
996
Thomas Grafac833bd2015-03-24 14:18:18 +0100997/* Internal function, please use rhashtable_remove_fast() instead */
Herbert Xuca268932016-09-19 19:00:09 +0800998static inline int __rhashtable_remove_fast_one(
Herbert Xu02fd97c2015-03-20 21:57:00 +1100999 struct rhashtable *ht, struct bucket_table *tbl,
Herbert Xuca268932016-09-19 19:00:09 +08001000 struct rhash_head *obj, const struct rhashtable_params params,
1001 bool rhlist)
Herbert Xu02fd97c2015-03-20 21:57:00 +11001002{
1003 struct rhash_head __rcu **pprev;
1004 struct rhash_head *he;
1005 spinlock_t * lock;
Thomas Graf299e5c32015-03-24 14:18:17 +01001006 unsigned int hash;
Herbert Xu02fd97c2015-03-20 21:57:00 +11001007 int err = -ENOENT;
1008
1009 hash = rht_head_hashfn(ht, tbl, obj, params);
1010 lock = rht_bucket_lock(tbl, hash);
1011
1012 spin_lock_bh(lock);
1013
Herbert Xuda204202017-02-11 19:26:47 +08001014 pprev = rht_bucket_var(tbl, hash);
1015 rht_for_each_continue(he, *pprev, tbl, hash) {
Herbert Xuca268932016-09-19 19:00:09 +08001016 struct rhlist_head *list;
1017
1018 list = container_of(he, struct rhlist_head, rhead);
1019
Herbert Xu02fd97c2015-03-20 21:57:00 +11001020 if (he != obj) {
Herbert Xuca268932016-09-19 19:00:09 +08001021 struct rhlist_head __rcu **lpprev;
1022
Herbert Xu02fd97c2015-03-20 21:57:00 +11001023 pprev = &he->next;
Herbert Xuca268932016-09-19 19:00:09 +08001024
1025 if (!rhlist)
1026 continue;
1027
1028 do {
1029 lpprev = &list->next;
1030 list = rht_dereference_bucket(list->next,
1031 tbl, hash);
1032 } while (list && obj != &list->rhead);
1033
1034 if (!list)
1035 continue;
1036
1037 list = rht_dereference_bucket(list->next, tbl, hash);
1038 RCU_INIT_POINTER(*lpprev, list);
1039 err = 0;
1040 break;
Herbert Xu02fd97c2015-03-20 21:57:00 +11001041 }
1042
Herbert Xuca268932016-09-19 19:00:09 +08001043 obj = rht_dereference_bucket(obj->next, tbl, hash);
1044 err = 1;
1045
1046 if (rhlist) {
1047 list = rht_dereference_bucket(list->next, tbl, hash);
1048 if (list) {
1049 RCU_INIT_POINTER(list->rhead.next, obj);
1050 obj = &list->rhead;
1051 err = 0;
1052 }
1053 }
1054
1055 rcu_assign_pointer(*pprev, obj);
Herbert Xu02fd97c2015-03-20 21:57:00 +11001056 break;
1057 }
1058
1059 spin_unlock_bh(lock);
1060
Herbert Xuca268932016-09-19 19:00:09 +08001061 if (err > 0) {
1062 atomic_dec(&ht->nelems);
1063 if (unlikely(ht->p.automatic_shrinking &&
1064 rht_shrink_below_30(ht, tbl)))
1065 schedule_work(&ht->run_work);
1066 err = 0;
1067 }
1068
1069 return err;
1070}
1071
1072/* Internal function, please use rhashtable_remove_fast() instead */
1073static inline int __rhashtable_remove_fast(
1074 struct rhashtable *ht, struct rhash_head *obj,
1075 const struct rhashtable_params params, bool rhlist)
1076{
1077 struct bucket_table *tbl;
1078 int err;
1079
1080 rcu_read_lock();
1081
1082 tbl = rht_dereference_rcu(ht->tbl, ht);
1083
1084 /* Because we have already taken (and released) the bucket
1085 * lock in old_tbl, if we find that future_tbl is not yet
1086 * visible then that guarantees the entry to still be in
1087 * the old tbl if it exists.
1088 */
1089 while ((err = __rhashtable_remove_fast_one(ht, tbl, obj, params,
1090 rhlist)) &&
1091 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
1092 ;
1093
1094 rcu_read_unlock();
1095
Herbert Xu02fd97c2015-03-20 21:57:00 +11001096 return err;
1097}
1098
1099/**
1100 * rhashtable_remove_fast - remove object from hash table
1101 * @ht: hash table
1102 * @obj: pointer to hash head inside object
1103 * @params: hash table parameters
1104 *
1105 * Since the hash chain is single linked, the removal operation needs to
1106 * walk the bucket chain upon removal. The removal operation is thus
1107 * considerable slow if the hash table is not correctly sized.
1108 *
1109 * Will automatically shrink the table via rhashtable_expand() if the
1110 * shrink_decision function specified at rhashtable_init() returns true.
1111 *
1112 * Returns zero on success, -ENOENT if the entry could not be found.
1113 */
1114static inline int rhashtable_remove_fast(
1115 struct rhashtable *ht, struct rhash_head *obj,
1116 const struct rhashtable_params params)
1117{
Herbert Xuca268932016-09-19 19:00:09 +08001118 return __rhashtable_remove_fast(ht, obj, params, false);
1119}
Herbert Xu02fd97c2015-03-20 21:57:00 +11001120
Herbert Xuca268932016-09-19 19:00:09 +08001121/**
1122 * rhltable_remove - remove object from hash list table
1123 * @hlt: hash list table
1124 * @list: pointer to hash list head inside object
1125 * @params: hash table parameters
1126 *
1127 * Since the hash chain is single linked, the removal operation needs to
1128 * walk the bucket chain upon removal. The removal operation is thus
1129 * considerable slow if the hash table is not correctly sized.
1130 *
1131 * Will automatically shrink the table via rhashtable_expand() if the
1132 * shrink_decision function specified at rhashtable_init() returns true.
1133 *
1134 * Returns zero on success, -ENOENT if the entry could not be found.
1135 */
1136static inline int rhltable_remove(
1137 struct rhltable *hlt, struct rhlist_head *list,
1138 const struct rhashtable_params params)
1139{
1140 return __rhashtable_remove_fast(&hlt->ht, &list->rhead, params, true);
Herbert Xu02fd97c2015-03-20 21:57:00 +11001141}
1142
Tom Herbert3502cad2015-12-15 15:41:36 -08001143/* Internal function, please use rhashtable_replace_fast() instead */
1144static inline int __rhashtable_replace_fast(
1145 struct rhashtable *ht, struct bucket_table *tbl,
1146 struct rhash_head *obj_old, struct rhash_head *obj_new,
1147 const struct rhashtable_params params)
1148{
1149 struct rhash_head __rcu **pprev;
1150 struct rhash_head *he;
1151 spinlock_t *lock;
1152 unsigned int hash;
1153 int err = -ENOENT;
1154
1155 /* Minimally, the old and new objects must have same hash
1156 * (which should mean identifiers are the same).
1157 */
1158 hash = rht_head_hashfn(ht, tbl, obj_old, params);
1159 if (hash != rht_head_hashfn(ht, tbl, obj_new, params))
1160 return -EINVAL;
1161
1162 lock = rht_bucket_lock(tbl, hash);
1163
1164 spin_lock_bh(lock);
1165
Herbert Xuda204202017-02-11 19:26:47 +08001166 pprev = rht_bucket_var(tbl, hash);
1167 rht_for_each_continue(he, *pprev, tbl, hash) {
Tom Herbert3502cad2015-12-15 15:41:36 -08001168 if (he != obj_old) {
1169 pprev = &he->next;
1170 continue;
1171 }
1172
1173 rcu_assign_pointer(obj_new->next, obj_old->next);
1174 rcu_assign_pointer(*pprev, obj_new);
1175 err = 0;
1176 break;
1177 }
1178
1179 spin_unlock_bh(lock);
1180
1181 return err;
1182}
1183
1184/**
1185 * rhashtable_replace_fast - replace an object in hash table
1186 * @ht: hash table
1187 * @obj_old: pointer to hash head inside object being replaced
1188 * @obj_new: pointer to hash head inside object which is new
1189 * @params: hash table parameters
1190 *
1191 * Replacing an object doesn't affect the number of elements in the hash table
1192 * or bucket, so we don't need to worry about shrinking or expanding the
1193 * table here.
1194 *
1195 * Returns zero on success, -ENOENT if the entry could not be found,
1196 * -EINVAL if hash is not the same for the old and new objects.
1197 */
1198static inline int rhashtable_replace_fast(
1199 struct rhashtable *ht, struct rhash_head *obj_old,
1200 struct rhash_head *obj_new,
1201 const struct rhashtable_params params)
1202{
1203 struct bucket_table *tbl;
1204 int err;
1205
1206 rcu_read_lock();
1207
1208 tbl = rht_dereference_rcu(ht->tbl, ht);
1209
1210 /* Because we have already taken (and released) the bucket
1211 * lock in old_tbl, if we find that future_tbl is not yet
1212 * visible then that guarantees the entry to still be in
1213 * the old tbl if it exists.
1214 */
1215 while ((err = __rhashtable_replace_fast(ht, tbl, obj_old,
1216 obj_new, params)) &&
1217 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
1218 ;
1219
1220 rcu_read_unlock();
1221
1222 return err;
1223}
1224
Herbert Xu246779d2016-08-18 16:50:56 +08001225/* Obsolete function, do not use in new code. */
1226static inline int rhashtable_walk_init(struct rhashtable *ht,
1227 struct rhashtable_iter *iter, gfp_t gfp)
1228{
1229 rhashtable_walk_enter(ht, iter);
1230 return 0;
1231}
1232
Herbert Xuca268932016-09-19 19:00:09 +08001233/**
1234 * rhltable_walk_enter - Initialise an iterator
1235 * @hlt: Table to walk over
1236 * @iter: Hash table Iterator
1237 *
1238 * This function prepares a hash table walk.
1239 *
1240 * Note that if you restart a walk after rhashtable_walk_stop you
1241 * may see the same object twice. Also, you may miss objects if
1242 * there are removals in between rhashtable_walk_stop and the next
1243 * call to rhashtable_walk_start.
1244 *
1245 * For a completely stable walk you should construct your own data
1246 * structure outside the hash table.
1247 *
1248 * This function may sleep so you must not call it from interrupt
1249 * context or with spin locks held.
1250 *
1251 * You must call rhashtable_walk_exit after this function returns.
1252 */
1253static inline void rhltable_walk_enter(struct rhltable *hlt,
1254 struct rhashtable_iter *iter)
1255{
1256 return rhashtable_walk_enter(&hlt->ht, iter);
1257}
1258
1259/**
1260 * rhltable_free_and_destroy - free elements and destroy hash list table
1261 * @hlt: the hash list table to destroy
1262 * @free_fn: callback to release resources of element
1263 * @arg: pointer passed to free_fn
1264 *
1265 * See documentation for rhashtable_free_and_destroy.
1266 */
1267static inline void rhltable_free_and_destroy(struct rhltable *hlt,
1268 void (*free_fn)(void *ptr,
1269 void *arg),
1270 void *arg)
1271{
1272 return rhashtable_free_and_destroy(&hlt->ht, free_fn, arg);
1273}
1274
1275static inline void rhltable_destroy(struct rhltable *hlt)
1276{
1277 return rhltable_free_and_destroy(hlt, NULL, NULL);
1278}
1279
Thomas Graf7e1e7762014-08-02 11:47:44 +02001280#endif /* _LINUX_RHASHTABLE_H */