at v3.19-rc2 213 lines 7.1 kB view raw
1/* 2 * Resizable, Scalable, Concurrent Hash Table 3 * 4 * Copyright (c) 2014 Thomas Graf <tgraf@suug.ch> 5 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net> 6 * 7 * Based on the following paper by Josh Triplett, Paul E. McKenney 8 * and Jonathan Walpole: 9 * https://www.usenix.org/legacy/event/atc11/tech/final_files/Triplett.pdf 10 * 11 * Code partially derived from nft_hash 12 * 13 * This program is free software; you can redistribute it and/or modify 14 * it under the terms of the GNU General Public License version 2 as 15 * published by the Free Software Foundation. 16 */ 17 18#ifndef _LINUX_RHASHTABLE_H 19#define _LINUX_RHASHTABLE_H 20 21#include <linux/rculist.h> 22 23struct rhash_head { 24 struct rhash_head __rcu *next; 25}; 26 27#define INIT_HASH_HEAD(ptr) ((ptr)->next = NULL) 28 29struct bucket_table { 30 size_t size; 31 struct rhash_head __rcu *buckets[]; 32}; 33 34typedef u32 (*rht_hashfn_t)(const void *data, u32 len, u32 seed); 35typedef u32 (*rht_obj_hashfn_t)(const void *data, u32 seed); 36 37struct rhashtable; 38 39/** 40 * struct rhashtable_params - Hash table construction parameters 41 * @nelem_hint: Hint on number of elements, should be 75% of desired size 42 * @key_len: Length of key 43 * @key_offset: Offset of key in struct to be hashed 44 * @head_offset: Offset of rhash_head in struct to be hashed 45 * @hash_rnd: Seed to use while hashing 46 * @max_shift: Maximum number of shifts while expanding 47 * @min_shift: Minimum number of shifts while shrinking 48 * @hashfn: Function to hash key 49 * @obj_hashfn: Function to hash object 50 * @grow_decision: If defined, may return true if table should expand 51 * @shrink_decision: If defined, may return true if table should shrink 52 * @mutex_is_held: Must return true if protecting mutex is held 53 */ 54struct rhashtable_params { 55 size_t nelem_hint; 56 size_t key_len; 57 size_t key_offset; 58 size_t head_offset; 59 u32 hash_rnd; 60 size_t max_shift; 61 size_t min_shift; 62 rht_hashfn_t hashfn; 63 rht_obj_hashfn_t obj_hashfn; 64 bool (*grow_decision)(const struct rhashtable *ht, 65 size_t new_size); 66 bool (*shrink_decision)(const struct rhashtable *ht, 67 size_t new_size); 68#ifdef CONFIG_PROVE_LOCKING 69 int (*mutex_is_held)(void *parent); 70 void *parent; 71#endif 72}; 73 74/** 75 * struct rhashtable - Hash table handle 76 * @tbl: Bucket table 77 * @nelems: Number of elements in table 78 * @shift: Current size (1 << shift) 79 * @p: Configuration parameters 80 */ 81struct rhashtable { 82 struct bucket_table __rcu *tbl; 83 size_t nelems; 84 size_t shift; 85 struct rhashtable_params p; 86}; 87 88#ifdef CONFIG_PROVE_LOCKING 89int lockdep_rht_mutex_is_held(const struct rhashtable *ht); 90#else 91static inline int lockdep_rht_mutex_is_held(const struct rhashtable *ht) 92{ 93 return 1; 94} 95#endif /* CONFIG_PROVE_LOCKING */ 96 97int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params); 98 99u32 rhashtable_hashfn(const struct rhashtable *ht, const void *key, u32 len); 100u32 rhashtable_obj_hashfn(const struct rhashtable *ht, void *ptr); 101 102void rhashtable_insert(struct rhashtable *ht, struct rhash_head *node); 103bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *node); 104void rhashtable_remove_pprev(struct rhashtable *ht, struct rhash_head *obj, 105 struct rhash_head __rcu **pprev); 106 107bool rht_grow_above_75(const struct rhashtable *ht, size_t new_size); 108bool rht_shrink_below_30(const struct rhashtable *ht, size_t new_size); 109 110int rhashtable_expand(struct rhashtable *ht); 111int rhashtable_shrink(struct rhashtable *ht); 112 113void *rhashtable_lookup(const struct rhashtable *ht, const void *key); 114void *rhashtable_lookup_compare(const struct rhashtable *ht, u32 hash, 115 bool (*compare)(void *, void *), void *arg); 116 117void rhashtable_destroy(const struct rhashtable *ht); 118 119#define rht_dereference(p, ht) \ 120 rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht)) 121 122#define rht_dereference_rcu(p, ht) \ 123 rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht)) 124 125#define rht_entry(ptr, type, member) container_of(ptr, type, member) 126#define rht_entry_safe(ptr, type, member) \ 127({ \ 128 typeof(ptr) __ptr = (ptr); \ 129 __ptr ? rht_entry(__ptr, type, member) : NULL; \ 130}) 131 132#define rht_next_entry_safe(pos, ht, member) \ 133({ \ 134 pos ? rht_entry_safe(rht_dereference((pos)->member.next, ht), \ 135 typeof(*(pos)), member) : NULL; \ 136}) 137 138/** 139 * rht_for_each - iterate over hash chain 140 * @pos: &struct rhash_head to use as a loop cursor. 141 * @head: head of the hash chain (struct rhash_head *) 142 * @ht: pointer to your struct rhashtable 143 */ 144#define rht_for_each(pos, head, ht) \ 145 for (pos = rht_dereference(head, ht); \ 146 pos; \ 147 pos = rht_dereference((pos)->next, ht)) 148 149/** 150 * rht_for_each_entry - iterate over hash chain of given type 151 * @pos: type * to use as a loop cursor. 152 * @head: head of the hash chain (struct rhash_head *) 153 * @ht: pointer to your struct rhashtable 154 * @member: name of the rhash_head within the hashable struct. 155 */ 156#define rht_for_each_entry(pos, head, ht, member) \ 157 for (pos = rht_entry_safe(rht_dereference(head, ht), \ 158 typeof(*(pos)), member); \ 159 pos; \ 160 pos = rht_next_entry_safe(pos, ht, member)) 161 162/** 163 * rht_for_each_entry_safe - safely iterate over hash chain of given type 164 * @pos: type * to use as a loop cursor. 165 * @n: type * to use for temporary next object storage 166 * @head: head of the hash chain (struct rhash_head *) 167 * @ht: pointer to your struct rhashtable 168 * @member: name of the rhash_head within the hashable struct. 169 * 170 * This hash chain list-traversal primitive allows for the looped code to 171 * remove the loop cursor from the list. 172 */ 173#define rht_for_each_entry_safe(pos, n, head, ht, member) \ 174 for (pos = rht_entry_safe(rht_dereference(head, ht), \ 175 typeof(*(pos)), member), \ 176 n = rht_next_entry_safe(pos, ht, member); \ 177 pos; \ 178 pos = n, \ 179 n = rht_next_entry_safe(pos, ht, member)) 180 181/** 182 * rht_for_each_rcu - iterate over rcu hash chain 183 * @pos: &struct rhash_head to use as a loop cursor. 184 * @head: head of the hash chain (struct rhash_head *) 185 * @ht: pointer to your struct rhashtable 186 * 187 * This hash chain list-traversal primitive may safely run concurrently with 188 * the _rcu fkht mutation primitives such as rht_insert() as long as the 189 * traversal is guarded by rcu_read_lock(). 190 */ 191#define rht_for_each_rcu(pos, head, ht) \ 192 for (pos = rht_dereference_rcu(head, ht); \ 193 pos; \ 194 pos = rht_dereference_rcu((pos)->next, ht)) 195 196/** 197 * rht_for_each_entry_rcu - iterate over rcu hash chain of given type 198 * @pos: type * to use as a loop cursor. 199 * @head: head of the hash chain (struct rhash_head *) 200 * @member: name of the rhash_head within the hashable struct. 201 * 202 * This hash chain list-traversal primitive may safely run concurrently with 203 * the _rcu fkht mutation primitives such as rht_insert() as long as the 204 * traversal is guarded by rcu_read_lock(). 205 */ 206#define rht_for_each_entry_rcu(pos, head, member) \ 207 for (pos = rht_entry_safe(rcu_dereference_raw(head), \ 208 typeof(*(pos)), member); \ 209 pos; \ 210 pos = rht_entry_safe(rcu_dereference_raw((pos)->member.next), \ 211 typeof(*(pos)), member)) 212 213#endif /* _LINUX_RHASHTABLE_H */