xref: /dflybsd-src/sys/dev/drm/amd/lib/chash.c (revision b843c749addef9340ee7d4e250b09fdd492602a1)
1*b843c749SSergey Zigachev /*
2*b843c749SSergey Zigachev  * Copyright 2017 Advanced Micro Devices, Inc.
3*b843c749SSergey Zigachev  *
4*b843c749SSergey Zigachev  * Permission is hereby granted, free of charge, to any person obtaining a
5*b843c749SSergey Zigachev  * copy of this software and associated documentation files (the "Software"),
6*b843c749SSergey Zigachev  * to deal in the Software without restriction, including without limitation
7*b843c749SSergey Zigachev  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*b843c749SSergey Zigachev  * and/or sell copies of the Software, and to permit persons to whom the
9*b843c749SSergey Zigachev  * Software is furnished to do so, subject to the following conditions:
10*b843c749SSergey Zigachev  *
11*b843c749SSergey Zigachev  * The above copyright notice and this permission notice shall be included in
12*b843c749SSergey Zigachev  * all copies or substantial portions of the Software.
13*b843c749SSergey Zigachev  *
14*b843c749SSergey Zigachev  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15*b843c749SSergey Zigachev  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16*b843c749SSergey Zigachev  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17*b843c749SSergey Zigachev  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18*b843c749SSergey Zigachev  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19*b843c749SSergey Zigachev  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20*b843c749SSergey Zigachev  * OTHER DEALINGS IN THE SOFTWARE.
21*b843c749SSergey Zigachev  *
22*b843c749SSergey Zigachev  */
23*b843c749SSergey Zigachev 
24*b843c749SSergey Zigachev #include <linux/types.h>
25*b843c749SSergey Zigachev #include <linux/hash.h>
26*b843c749SSergey Zigachev #include <linux/bug.h>
27*b843c749SSergey Zigachev #include <linux/slab.h>
28*b843c749SSergey Zigachev #include <linux/module.h>
29*b843c749SSergey Zigachev #include <linux/sched/clock.h>
30*b843c749SSergey Zigachev #include <asm/div64.h>
31*b843c749SSergey Zigachev #include <linux/chash.h>
32*b843c749SSergey Zigachev 
33*b843c749SSergey Zigachev /**
34*b843c749SSergey Zigachev  * chash_table_alloc - Allocate closed hash table
35*b843c749SSergey Zigachev  * @table: Pointer to the table structure
36*b843c749SSergey Zigachev  * @bits: Table size will be 2^bits entries
37*b843c749SSergey Zigachev  * @key_size: Size of hash keys in bytes, 4 or 8
38*b843c749SSergey Zigachev  * @value_size: Size of data values in bytes, can be 0
39*b843c749SSergey Zigachev  */
chash_table_alloc(struct chash_table * table,u8 bits,u8 key_size,unsigned int value_size,gfp_t gfp_mask)40*b843c749SSergey Zigachev int chash_table_alloc(struct chash_table *table, u8 bits, u8 key_size,
41*b843c749SSergey Zigachev 		      unsigned int value_size, gfp_t gfp_mask)
42*b843c749SSergey Zigachev {
43*b843c749SSergey Zigachev 	if (bits > 31)
44*b843c749SSergey Zigachev 		return -EINVAL;
45*b843c749SSergey Zigachev 
46*b843c749SSergey Zigachev 	if (key_size != 4 && key_size != 8)
47*b843c749SSergey Zigachev 		return -EINVAL;
48*b843c749SSergey Zigachev 
49*b843c749SSergey Zigachev 	table->data = kcalloc(__CHASH_DATA_SIZE(bits, key_size, value_size),
50*b843c749SSergey Zigachev 		       sizeof(long), gfp_mask);
51*b843c749SSergey Zigachev 	if (!table->data)
52*b843c749SSergey Zigachev 		return -ENOMEM;
53*b843c749SSergey Zigachev 
54*b843c749SSergey Zigachev 	__CHASH_TABLE_INIT(table->table, table->data,
55*b843c749SSergey Zigachev 			   bits, key_size, value_size);
56*b843c749SSergey Zigachev 
57*b843c749SSergey Zigachev 	return 0;
58*b843c749SSergey Zigachev }
59*b843c749SSergey Zigachev EXPORT_SYMBOL(chash_table_alloc);
60*b843c749SSergey Zigachev 
61*b843c749SSergey Zigachev /**
62*b843c749SSergey Zigachev  * chash_table_free - Free closed hash table
63*b843c749SSergey Zigachev  * @table: Pointer to the table structure
64*b843c749SSergey Zigachev  */
chash_table_free(struct chash_table * table)65*b843c749SSergey Zigachev void chash_table_free(struct chash_table *table)
66*b843c749SSergey Zigachev {
67*b843c749SSergey Zigachev 	kfree(table->data);
68*b843c749SSergey Zigachev }
69*b843c749SSergey Zigachev EXPORT_SYMBOL(chash_table_free);
70*b843c749SSergey Zigachev 
71*b843c749SSergey Zigachev #ifdef CONFIG_CHASH_STATS
72*b843c749SSergey Zigachev 
73*b843c749SSergey Zigachev #define DIV_FRAC(nom, denom, quot, frac, frac_digits) do {		\
74*b843c749SSergey Zigachev 		u64 __nom = (nom);					\
75*b843c749SSergey Zigachev 		u64 __denom = (denom);					\
76*b843c749SSergey Zigachev 		u64 __quot, __frac;					\
77*b843c749SSergey Zigachev 		u32 __rem;						\
78*b843c749SSergey Zigachev 									\
79*b843c749SSergey Zigachev 		while (__denom >> 32) {					\
80*b843c749SSergey Zigachev 			__nom   >>= 1;					\
81*b843c749SSergey Zigachev 			__denom >>= 1;					\
82*b843c749SSergey Zigachev 		}							\
83*b843c749SSergey Zigachev 		__quot = __nom;						\
84*b843c749SSergey Zigachev 		__rem  = do_div(__quot, __denom);			\
85*b843c749SSergey Zigachev 		__frac = __rem * (frac_digits) + (__denom >> 1);	\
86*b843c749SSergey Zigachev 		do_div(__frac, __denom);				\
87*b843c749SSergey Zigachev 		(quot) = __quot;					\
88*b843c749SSergey Zigachev 		(frac) = __frac;					\
89*b843c749SSergey Zigachev 	} while (0)
90*b843c749SSergey Zigachev 
__chash_table_dump_stats(struct __chash_table * table)91*b843c749SSergey Zigachev void __chash_table_dump_stats(struct __chash_table *table)
92*b843c749SSergey Zigachev {
93*b843c749SSergey Zigachev 	struct chash_iter iter = CHASH_ITER_INIT(table, 0);
94*b843c749SSergey Zigachev 	u32 filled = 0, empty = 0, tombstones = 0;
95*b843c749SSergey Zigachev 	u64 quot1, quot2;
96*b843c749SSergey Zigachev 	u32 frac1, frac2;
97*b843c749SSergey Zigachev 
98*b843c749SSergey Zigachev 	do {
99*b843c749SSergey Zigachev 		if (chash_iter_is_valid(iter))
100*b843c749SSergey Zigachev 			filled++;
101*b843c749SSergey Zigachev 		else if (chash_iter_is_empty(iter))
102*b843c749SSergey Zigachev 			empty++;
103*b843c749SSergey Zigachev 		else
104*b843c749SSergey Zigachev 			tombstones++;
105*b843c749SSergey Zigachev 		CHASH_ITER_INC(iter);
106*b843c749SSergey Zigachev 	} while (iter.slot);
107*b843c749SSergey Zigachev 
108*b843c749SSergey Zigachev 	pr_debug("chash: key size %u, value size %u\n",
109*b843c749SSergey Zigachev 		 table->key_size, table->value_size);
110*b843c749SSergey Zigachev 	pr_debug("  Slots total/filled/empty/tombstones: %u / %u / %u / %u\n",
111*b843c749SSergey Zigachev 		 1 << table->bits, filled, empty, tombstones);
112*b843c749SSergey Zigachev 	if (table->hits > 0) {
113*b843c749SSergey Zigachev 		DIV_FRAC(table->hits_steps, table->hits, quot1, frac1, 1000);
114*b843c749SSergey Zigachev 		DIV_FRAC(table->hits * 1000, table->hits_time_ns,
115*b843c749SSergey Zigachev 			 quot2, frac2, 1000);
116*b843c749SSergey Zigachev 	} else {
117*b843c749SSergey Zigachev 		quot1 = quot2 = 0;
118*b843c749SSergey Zigachev 		frac1 = frac2 = 0;
119*b843c749SSergey Zigachev 	}
120*b843c749SSergey Zigachev 	pr_debug("  Hits   (avg.cost, rate): %llu (%llu.%03u, %llu.%03u M/s)\n",
121*b843c749SSergey Zigachev 		 table->hits, quot1, frac1, quot2, frac2);
122*b843c749SSergey Zigachev 	if (table->miss > 0) {
123*b843c749SSergey Zigachev 		DIV_FRAC(table->miss_steps, table->miss, quot1, frac1, 1000);
124*b843c749SSergey Zigachev 		DIV_FRAC(table->miss * 1000, table->miss_time_ns,
125*b843c749SSergey Zigachev 			 quot2, frac2, 1000);
126*b843c749SSergey Zigachev 	} else {
127*b843c749SSergey Zigachev 		quot1 = quot2 = 0;
128*b843c749SSergey Zigachev 		frac1 = frac2 = 0;
129*b843c749SSergey Zigachev 	}
130*b843c749SSergey Zigachev 	pr_debug("  Misses (avg.cost, rate): %llu (%llu.%03u, %llu.%03u M/s)\n",
131*b843c749SSergey Zigachev 		 table->miss, quot1, frac1, quot2, frac2);
132*b843c749SSergey Zigachev 	if (table->hits + table->miss > 0) {
133*b843c749SSergey Zigachev 		DIV_FRAC(table->hits_steps + table->miss_steps,
134*b843c749SSergey Zigachev 			 table->hits + table->miss, quot1, frac1, 1000);
135*b843c749SSergey Zigachev 		DIV_FRAC((table->hits + table->miss) * 1000,
136*b843c749SSergey Zigachev 			 (table->hits_time_ns + table->miss_time_ns),
137*b843c749SSergey Zigachev 			 quot2, frac2, 1000);
138*b843c749SSergey Zigachev 	} else {
139*b843c749SSergey Zigachev 		quot1 = quot2 = 0;
140*b843c749SSergey Zigachev 		frac1 = frac2 = 0;
141*b843c749SSergey Zigachev 	}
142*b843c749SSergey Zigachev 	pr_debug("  Total  (avg.cost, rate): %llu (%llu.%03u, %llu.%03u M/s)\n",
143*b843c749SSergey Zigachev 		 table->hits + table->miss, quot1, frac1, quot2, frac2);
144*b843c749SSergey Zigachev 	if (table->relocs > 0) {
145*b843c749SSergey Zigachev 		DIV_FRAC(table->hits + table->miss, table->relocs,
146*b843c749SSergey Zigachev 			 quot1, frac1, 1000);
147*b843c749SSergey Zigachev 		DIV_FRAC(table->reloc_dist, table->relocs, quot2, frac2, 1000);
148*b843c749SSergey Zigachev 		pr_debug("  Relocations (freq, avg.dist): %llu (1:%llu.%03u, %llu.%03u)\n",
149*b843c749SSergey Zigachev 			 table->relocs, quot1, frac1, quot2, frac2);
150*b843c749SSergey Zigachev 	} else {
151*b843c749SSergey Zigachev 		pr_debug("  No relocations\n");
152*b843c749SSergey Zigachev 	}
153*b843c749SSergey Zigachev }
154*b843c749SSergey Zigachev EXPORT_SYMBOL(__chash_table_dump_stats);
155*b843c749SSergey Zigachev 
156*b843c749SSergey Zigachev #undef DIV_FRAC
157*b843c749SSergey Zigachev #endif
158*b843c749SSergey Zigachev 
159*b843c749SSergey Zigachev #define CHASH_INC(table, a) ((a) = ((a) + 1) & (table)->size_mask)
160*b843c749SSergey Zigachev #define CHASH_ADD(table, a, b) (((a) + (b)) & (table)->size_mask)
161*b843c749SSergey Zigachev #define CHASH_SUB(table, a, b) (((a) - (b)) & (table)->size_mask)
162*b843c749SSergey Zigachev #define CHASH_IN_RANGE(table, slot, first, last) \
163*b843c749SSergey Zigachev 	(CHASH_SUB(table, slot, first) <= CHASH_SUB(table, last, first))
164*b843c749SSergey Zigachev 
165*b843c749SSergey Zigachev /*#define CHASH_DEBUG Uncomment this to enable verbose debug output*/
166*b843c749SSergey Zigachev #ifdef CHASH_DEBUG
chash_table_dump(struct __chash_table * table)167*b843c749SSergey Zigachev static void chash_table_dump(struct __chash_table *table)
168*b843c749SSergey Zigachev {
169*b843c749SSergey Zigachev 	struct chash_iter iter = CHASH_ITER_INIT(table, 0);
170*b843c749SSergey Zigachev 
171*b843c749SSergey Zigachev 	do {
172*b843c749SSergey Zigachev 		if ((iter.slot & 3) == 0)
173*b843c749SSergey Zigachev 			pr_debug("%04x: ", iter.slot);
174*b843c749SSergey Zigachev 
175*b843c749SSergey Zigachev 		if (chash_iter_is_valid(iter))
176*b843c749SSergey Zigachev 			pr_debug("[%016llx] ", chash_iter_key(iter));
177*b843c749SSergey Zigachev 		else if (chash_iter_is_empty(iter))
178*b843c749SSergey Zigachev 			pr_debug("[    <empty>     ] ");
179*b843c749SSergey Zigachev 		else
180*b843c749SSergey Zigachev 			pr_debug("[  <tombstone>   ] ");
181*b843c749SSergey Zigachev 
182*b843c749SSergey Zigachev 		if ((iter.slot & 3) == 3)
183*b843c749SSergey Zigachev 			pr_debug("\n");
184*b843c749SSergey Zigachev 
185*b843c749SSergey Zigachev 		CHASH_ITER_INC(iter);
186*b843c749SSergey Zigachev 	} while (iter.slot);
187*b843c749SSergey Zigachev 
188*b843c749SSergey Zigachev 	if ((iter.slot & 3) != 0)
189*b843c749SSergey Zigachev 		pr_debug("\n");
190*b843c749SSergey Zigachev }
191*b843c749SSergey Zigachev 
chash_table_check(struct __chash_table * table)192*b843c749SSergey Zigachev static int chash_table_check(struct __chash_table *table)
193*b843c749SSergey Zigachev {
194*b843c749SSergey Zigachev 	u32 hash;
195*b843c749SSergey Zigachev 	struct chash_iter iter = CHASH_ITER_INIT(table, 0);
196*b843c749SSergey Zigachev 	struct chash_iter cur = CHASH_ITER_INIT(table, 0);
197*b843c749SSergey Zigachev 
198*b843c749SSergey Zigachev 	do {
199*b843c749SSergey Zigachev 		if (!chash_iter_is_valid(iter)) {
200*b843c749SSergey Zigachev 			CHASH_ITER_INC(iter);
201*b843c749SSergey Zigachev 			continue;
202*b843c749SSergey Zigachev 		}
203*b843c749SSergey Zigachev 
204*b843c749SSergey Zigachev 		hash = chash_iter_hash(iter);
205*b843c749SSergey Zigachev 		CHASH_ITER_SET(cur, hash);
206*b843c749SSergey Zigachev 		while (cur.slot != iter.slot) {
207*b843c749SSergey Zigachev 			if (chash_iter_is_empty(cur)) {
208*b843c749SSergey Zigachev 				pr_err("Path to element at %x with hash %x broken at slot %x\n",
209*b843c749SSergey Zigachev 				       iter.slot, hash, cur.slot);
210*b843c749SSergey Zigachev 				chash_table_dump(table);
211*b843c749SSergey Zigachev 				return -EINVAL;
212*b843c749SSergey Zigachev 			}
213*b843c749SSergey Zigachev 			CHASH_ITER_INC(cur);
214*b843c749SSergey Zigachev 		}
215*b843c749SSergey Zigachev 
216*b843c749SSergey Zigachev 		CHASH_ITER_INC(iter);
217*b843c749SSergey Zigachev 	} while (iter.slot);
218*b843c749SSergey Zigachev 
219*b843c749SSergey Zigachev 	return 0;
220*b843c749SSergey Zigachev }
221*b843c749SSergey Zigachev #endif
222*b843c749SSergey Zigachev 
chash_iter_relocate(struct chash_iter dst,struct chash_iter src)223*b843c749SSergey Zigachev static void chash_iter_relocate(struct chash_iter dst, struct chash_iter src)
224*b843c749SSergey Zigachev {
225*b843c749SSergey Zigachev 	BUG_ON(src.table == dst.table && src.slot == dst.slot);
226*b843c749SSergey Zigachev 	BUG_ON(src.table->key_size != dst.table->key_size);
227*b843c749SSergey Zigachev 	BUG_ON(src.table->value_size != dst.table->value_size);
228*b843c749SSergey Zigachev 
229*b843c749SSergey Zigachev 	if (dst.table->key_size == 4)
230*b843c749SSergey Zigachev 		dst.table->keys32[dst.slot] = src.table->keys32[src.slot];
231*b843c749SSergey Zigachev 	else
232*b843c749SSergey Zigachev 		dst.table->keys64[dst.slot] = src.table->keys64[src.slot];
233*b843c749SSergey Zigachev 
234*b843c749SSergey Zigachev 	if (dst.table->value_size)
235*b843c749SSergey Zigachev 		memcpy(chash_iter_value(dst), chash_iter_value(src),
236*b843c749SSergey Zigachev 		       dst.table->value_size);
237*b843c749SSergey Zigachev 
238*b843c749SSergey Zigachev 	chash_iter_set_valid(dst);
239*b843c749SSergey Zigachev 	chash_iter_set_invalid(src);
240*b843c749SSergey Zigachev 
241*b843c749SSergey Zigachev #ifdef CONFIG_CHASH_STATS
242*b843c749SSergey Zigachev 	if (src.table == dst.table) {
243*b843c749SSergey Zigachev 		dst.table->relocs++;
244*b843c749SSergey Zigachev 		dst.table->reloc_dist +=
245*b843c749SSergey Zigachev 			CHASH_SUB(dst.table, src.slot, dst.slot);
246*b843c749SSergey Zigachev 	}
247*b843c749SSergey Zigachev #endif
248*b843c749SSergey Zigachev }
249*b843c749SSergey Zigachev 
250*b843c749SSergey Zigachev /**
251*b843c749SSergey Zigachev  * __chash_table_find - Helper for looking up a hash table entry
252*b843c749SSergey Zigachev  * @iter: Pointer to hash table iterator
253*b843c749SSergey Zigachev  * @key: Key of the entry to find
254*b843c749SSergey Zigachev  * @for_removal: set to true if the element will be removed soon
255*b843c749SSergey Zigachev  *
256*b843c749SSergey Zigachev  * Searches for an entry in the hash table with a given key. iter must
257*b843c749SSergey Zigachev  * be initialized by the caller to point to the home position of the
258*b843c749SSergey Zigachev  * hypothetical entry, i.e. it must be initialized with the hash table
259*b843c749SSergey Zigachev  * and the key's hash as the initial slot for the search.
260*b843c749SSergey Zigachev  *
261*b843c749SSergey Zigachev  * This function also does some local clean-up to speed up future
262*b843c749SSergey Zigachev  * look-ups by relocating entries to better slots and removing
263*b843c749SSergey Zigachev  * tombstones that are no longer needed.
264*b843c749SSergey Zigachev  *
265*b843c749SSergey Zigachev  * If @for_removal is true, the function avoids relocating the entry
266*b843c749SSergey Zigachev  * that is being returned.
267*b843c749SSergey Zigachev  *
268*b843c749SSergey Zigachev  * Returns 0 if the search is successful. In this case iter is updated
269*b843c749SSergey Zigachev  * to point to the found entry. Otherwise %-EINVAL is returned and the
270*b843c749SSergey Zigachev  * iter is updated to point to the first available slot for the given
271*b843c749SSergey Zigachev  * key. If the table is full, the slot is set to -1.
272*b843c749SSergey Zigachev  */
chash_table_find(struct chash_iter * iter,u64 key,bool for_removal)273*b843c749SSergey Zigachev static int chash_table_find(struct chash_iter *iter, u64 key,
274*b843c749SSergey Zigachev 			    bool for_removal)
275*b843c749SSergey Zigachev {
276*b843c749SSergey Zigachev #ifdef CONFIG_CHASH_STATS
277*b843c749SSergey Zigachev 	u64 ts1 = local_clock();
278*b843c749SSergey Zigachev #endif
279*b843c749SSergey Zigachev 	u32 hash = iter->slot;
280*b843c749SSergey Zigachev 	struct chash_iter first_redundant = CHASH_ITER_INIT(iter->table, -1);
281*b843c749SSergey Zigachev 	int first_avail = (for_removal ? -2 : -1);
282*b843c749SSergey Zigachev 
283*b843c749SSergey Zigachev 	while (!chash_iter_is_valid(*iter) || chash_iter_key(*iter) != key) {
284*b843c749SSergey Zigachev 		if (chash_iter_is_empty(*iter)) {
285*b843c749SSergey Zigachev 			/* Found an empty slot, which ends the
286*b843c749SSergey Zigachev 			 * search. Clean up any preceding tombstones
287*b843c749SSergey Zigachev 			 * that are no longer needed because they lead
288*b843c749SSergey Zigachev 			 * to no-where
289*b843c749SSergey Zigachev 			 */
290*b843c749SSergey Zigachev 			if ((int)first_redundant.slot < 0)
291*b843c749SSergey Zigachev 				goto not_found;
292*b843c749SSergey Zigachev 			while (first_redundant.slot != iter->slot) {
293*b843c749SSergey Zigachev 				if (!chash_iter_is_valid(first_redundant))
294*b843c749SSergey Zigachev 					chash_iter_set_empty(first_redundant);
295*b843c749SSergey Zigachev 				CHASH_ITER_INC(first_redundant);
296*b843c749SSergey Zigachev 			}
297*b843c749SSergey Zigachev #ifdef CHASH_DEBUG
298*b843c749SSergey Zigachev 			chash_table_check(iter->table);
299*b843c749SSergey Zigachev #endif
300*b843c749SSergey Zigachev 			goto not_found;
301*b843c749SSergey Zigachev 		} else if (!chash_iter_is_valid(*iter)) {
302*b843c749SSergey Zigachev 			/* Found a tombstone. Remember it as candidate
303*b843c749SSergey Zigachev 			 * for relocating the entry we're looking for
304*b843c749SSergey Zigachev 			 * or for adding a new entry with the given key
305*b843c749SSergey Zigachev 			 */
306*b843c749SSergey Zigachev 			if (first_avail == -1)
307*b843c749SSergey Zigachev 				first_avail = iter->slot;
308*b843c749SSergey Zigachev 			/* Or mark it as the start of a series of
309*b843c749SSergey Zigachev 			 * potentially redundant tombstones
310*b843c749SSergey Zigachev 			 */
311*b843c749SSergey Zigachev 			else if (first_redundant.slot == -1)
312*b843c749SSergey Zigachev 				CHASH_ITER_SET(first_redundant, iter->slot);
313*b843c749SSergey Zigachev 		} else if (first_redundant.slot >= 0) {
314*b843c749SSergey Zigachev 			/* Found a valid, occupied slot with a
315*b843c749SSergey Zigachev 			 * preceding series of tombstones. Relocate it
316*b843c749SSergey Zigachev 			 * to a better position that no longer depends
317*b843c749SSergey Zigachev 			 * on those tombstones
318*b843c749SSergey Zigachev 			 */
319*b843c749SSergey Zigachev 			u32 cur_hash = chash_iter_hash(*iter);
320*b843c749SSergey Zigachev 
321*b843c749SSergey Zigachev 			if (!CHASH_IN_RANGE(iter->table, cur_hash,
322*b843c749SSergey Zigachev 					    first_redundant.slot + 1,
323*b843c749SSergey Zigachev 					    iter->slot)) {
324*b843c749SSergey Zigachev 				/* This entry has a hash at or before
325*b843c749SSergey Zigachev 				 * the first tombstone we found. We
326*b843c749SSergey Zigachev 				 * can relocate it to that tombstone
327*b843c749SSergey Zigachev 				 * and advance to the next tombstone
328*b843c749SSergey Zigachev 				 */
329*b843c749SSergey Zigachev 				chash_iter_relocate(first_redundant, *iter);
330*b843c749SSergey Zigachev 				do {
331*b843c749SSergey Zigachev 					CHASH_ITER_INC(first_redundant);
332*b843c749SSergey Zigachev 				} while (chash_iter_is_valid(first_redundant));
333*b843c749SSergey Zigachev 			} else if (cur_hash != iter->slot) {
334*b843c749SSergey Zigachev 				/* Relocate entry to its home position
335*b843c749SSergey Zigachev 				 * or as close as possible so it no
336*b843c749SSergey Zigachev 				 * longer depends on any preceding
337*b843c749SSergey Zigachev 				 * tombstones
338*b843c749SSergey Zigachev 				 */
339*b843c749SSergey Zigachev 				struct chash_iter new_iter =
340*b843c749SSergey Zigachev 					CHASH_ITER_INIT(iter->table, cur_hash);
341*b843c749SSergey Zigachev 
342*b843c749SSergey Zigachev 				while (new_iter.slot != iter->slot &&
343*b843c749SSergey Zigachev 				       chash_iter_is_valid(new_iter))
344*b843c749SSergey Zigachev 					CHASH_ITER_INC(new_iter);
345*b843c749SSergey Zigachev 
346*b843c749SSergey Zigachev 				if (new_iter.slot != iter->slot)
347*b843c749SSergey Zigachev 					chash_iter_relocate(new_iter, *iter);
348*b843c749SSergey Zigachev 			}
349*b843c749SSergey Zigachev 		}
350*b843c749SSergey Zigachev 
351*b843c749SSergey Zigachev 		CHASH_ITER_INC(*iter);
352*b843c749SSergey Zigachev 		if (iter->slot == hash) {
353*b843c749SSergey Zigachev 			iter->slot = -1;
354*b843c749SSergey Zigachev 			goto not_found;
355*b843c749SSergey Zigachev 		}
356*b843c749SSergey Zigachev 	}
357*b843c749SSergey Zigachev 
358*b843c749SSergey Zigachev #ifdef CONFIG_CHASH_STATS
359*b843c749SSergey Zigachev 	iter->table->hits++;
360*b843c749SSergey Zigachev 	iter->table->hits_steps += CHASH_SUB(iter->table, iter->slot, hash) + 1;
361*b843c749SSergey Zigachev #endif
362*b843c749SSergey Zigachev 
363*b843c749SSergey Zigachev 	if (first_avail >= 0) {
364*b843c749SSergey Zigachev 		CHASH_ITER_SET(first_redundant, first_avail);
365*b843c749SSergey Zigachev 		chash_iter_relocate(first_redundant, *iter);
366*b843c749SSergey Zigachev 		iter->slot = first_redundant.slot;
367*b843c749SSergey Zigachev 		iter->mask = first_redundant.mask;
368*b843c749SSergey Zigachev 	}
369*b843c749SSergey Zigachev 
370*b843c749SSergey Zigachev #ifdef CONFIG_CHASH_STATS
371*b843c749SSergey Zigachev 	iter->table->hits_time_ns += local_clock() - ts1;
372*b843c749SSergey Zigachev #endif
373*b843c749SSergey Zigachev 
374*b843c749SSergey Zigachev 	return 0;
375*b843c749SSergey Zigachev 
376*b843c749SSergey Zigachev not_found:
377*b843c749SSergey Zigachev #ifdef CONFIG_CHASH_STATS
378*b843c749SSergey Zigachev 	iter->table->miss++;
379*b843c749SSergey Zigachev 	iter->table->miss_steps += (iter->slot < 0) ?
380*b843c749SSergey Zigachev 		(1 << iter->table->bits) :
381*b843c749SSergey Zigachev 		CHASH_SUB(iter->table, iter->slot, hash) + 1;
382*b843c749SSergey Zigachev #endif
383*b843c749SSergey Zigachev 
384*b843c749SSergey Zigachev 	if (first_avail >= 0)
385*b843c749SSergey Zigachev 		CHASH_ITER_SET(*iter, first_avail);
386*b843c749SSergey Zigachev 
387*b843c749SSergey Zigachev #ifdef CONFIG_CHASH_STATS
388*b843c749SSergey Zigachev 	iter->table->miss_time_ns += local_clock() - ts1;
389*b843c749SSergey Zigachev #endif
390*b843c749SSergey Zigachev 
391*b843c749SSergey Zigachev 	return -EINVAL;
392*b843c749SSergey Zigachev }
393*b843c749SSergey Zigachev 
__chash_table_copy_in(struct __chash_table * table,u64 key,const void * value)394*b843c749SSergey Zigachev int __chash_table_copy_in(struct __chash_table *table, u64 key,
395*b843c749SSergey Zigachev 			  const void *value)
396*b843c749SSergey Zigachev {
397*b843c749SSergey Zigachev 	u32 hash = (table->key_size == 4) ?
398*b843c749SSergey Zigachev 		hash_32(key, table->bits) : hash_64(key, table->bits);
399*b843c749SSergey Zigachev 	struct chash_iter iter = CHASH_ITER_INIT(table, hash);
400*b843c749SSergey Zigachev 	int r = chash_table_find(&iter, key, false);
401*b843c749SSergey Zigachev 
402*b843c749SSergey Zigachev 	/* Found an existing entry */
403*b843c749SSergey Zigachev 	if (!r) {
404*b843c749SSergey Zigachev 		if (value && table->value_size)
405*b843c749SSergey Zigachev 			memcpy(chash_iter_value(iter), value,
406*b843c749SSergey Zigachev 			       table->value_size);
407*b843c749SSergey Zigachev 		return 1;
408*b843c749SSergey Zigachev 	}
409*b843c749SSergey Zigachev 
410*b843c749SSergey Zigachev 	/* Is there a place to add a new entry? */
411*b843c749SSergey Zigachev 	if (iter.slot < 0) {
412*b843c749SSergey Zigachev 		pr_err("Hash table overflow\n");
413*b843c749SSergey Zigachev 		return -ENOMEM;
414*b843c749SSergey Zigachev 	}
415*b843c749SSergey Zigachev 
416*b843c749SSergey Zigachev 	chash_iter_set_valid(iter);
417*b843c749SSergey Zigachev 
418*b843c749SSergey Zigachev 	if (table->key_size == 4)
419*b843c749SSergey Zigachev 		table->keys32[iter.slot] = key;
420*b843c749SSergey Zigachev 	else
421*b843c749SSergey Zigachev 		table->keys64[iter.slot] = key;
422*b843c749SSergey Zigachev 	if (value && table->value_size)
423*b843c749SSergey Zigachev 		memcpy(chash_iter_value(iter), value, table->value_size);
424*b843c749SSergey Zigachev 
425*b843c749SSergey Zigachev 	return 0;
426*b843c749SSergey Zigachev }
427*b843c749SSergey Zigachev EXPORT_SYMBOL(__chash_table_copy_in);
428*b843c749SSergey Zigachev 
__chash_table_copy_out(struct __chash_table * table,u64 key,void * value,bool remove)429*b843c749SSergey Zigachev int __chash_table_copy_out(struct __chash_table *table, u64 key,
430*b843c749SSergey Zigachev 			   void *value, bool remove)
431*b843c749SSergey Zigachev {
432*b843c749SSergey Zigachev 	u32 hash = (table->key_size == 4) ?
433*b843c749SSergey Zigachev 		hash_32(key, table->bits) : hash_64(key, table->bits);
434*b843c749SSergey Zigachev 	struct chash_iter iter = CHASH_ITER_INIT(table, hash);
435*b843c749SSergey Zigachev 	int r = chash_table_find(&iter, key, remove);
436*b843c749SSergey Zigachev 
437*b843c749SSergey Zigachev 	if (r < 0)
438*b843c749SSergey Zigachev 		return r;
439*b843c749SSergey Zigachev 
440*b843c749SSergey Zigachev 	if (value && table->value_size)
441*b843c749SSergey Zigachev 		memcpy(value, chash_iter_value(iter), table->value_size);
442*b843c749SSergey Zigachev 
443*b843c749SSergey Zigachev 	if (remove)
444*b843c749SSergey Zigachev 		chash_iter_set_invalid(iter);
445*b843c749SSergey Zigachev 
446*b843c749SSergey Zigachev 	return iter.slot;
447*b843c749SSergey Zigachev }
448*b843c749SSergey Zigachev EXPORT_SYMBOL(__chash_table_copy_out);
449*b843c749SSergey Zigachev 
450*b843c749SSergey Zigachev #ifdef CONFIG_CHASH_SELFTEST
451*b843c749SSergey Zigachev /**
452*b843c749SSergey Zigachev  * chash_self_test - Run a self-test of the hash table implementation
453*b843c749SSergey Zigachev  * @bits: Table size will be 2^bits entries
454*b843c749SSergey Zigachev  * @key_size: Size of hash keys in bytes, 4 or 8
455*b843c749SSergey Zigachev  * @min_fill: Minimum fill level during the test
456*b843c749SSergey Zigachev  * @max_fill: Maximum fill level during the test
457*b843c749SSergey Zigachev  * @iterations: Number of test iterations
458*b843c749SSergey Zigachev  *
459*b843c749SSergey Zigachev  * The test adds and removes entries from a hash table, cycling the
460*b843c749SSergey Zigachev  * fill level between min_fill and max_fill entries. Also tests lookup
461*b843c749SSergey Zigachev  * and value retrieval.
462*b843c749SSergey Zigachev  */
chash_self_test(u8 bits,u8 key_size,int min_fill,int max_fill,u64 iterations)463*b843c749SSergey Zigachev static int __init chash_self_test(u8 bits, u8 key_size,
464*b843c749SSergey Zigachev 				  int min_fill, int max_fill,
465*b843c749SSergey Zigachev 				  u64 iterations)
466*b843c749SSergey Zigachev {
467*b843c749SSergey Zigachev 	struct chash_table table;
468*b843c749SSergey Zigachev 	int ret;
469*b843c749SSergey Zigachev 	u64 add_count, rmv_count;
470*b843c749SSergey Zigachev 	u64 value;
471*b843c749SSergey Zigachev 
472*b843c749SSergey Zigachev 	if (key_size == 4 && iterations > 0xffffffff)
473*b843c749SSergey Zigachev 		return -EINVAL;
474*b843c749SSergey Zigachev 	if (min_fill >= max_fill)
475*b843c749SSergey Zigachev 		return -EINVAL;
476*b843c749SSergey Zigachev 
477*b843c749SSergey Zigachev 	ret = chash_table_alloc(&table, bits, key_size, sizeof(u64),
478*b843c749SSergey Zigachev 				GFP_KERNEL);
479*b843c749SSergey Zigachev 	if (ret) {
480*b843c749SSergey Zigachev 		pr_err("chash_table_alloc failed: %d\n", ret);
481*b843c749SSergey Zigachev 		return ret;
482*b843c749SSergey Zigachev 	}
483*b843c749SSergey Zigachev 
484*b843c749SSergey Zigachev 	for (add_count = 0, rmv_count = 0; add_count < iterations;
485*b843c749SSergey Zigachev 	     add_count++) {
486*b843c749SSergey Zigachev 		/* When we hit the max_fill level, remove entries down
487*b843c749SSergey Zigachev 		 * to min_fill
488*b843c749SSergey Zigachev 		 */
489*b843c749SSergey Zigachev 		if (add_count - rmv_count == max_fill) {
490*b843c749SSergey Zigachev 			u64 find_count = rmv_count;
491*b843c749SSergey Zigachev 
492*b843c749SSergey Zigachev 			/* First try to find all entries that we're
493*b843c749SSergey Zigachev 			 * about to remove, confirm their value, test
494*b843c749SSergey Zigachev 			 * writing them back a second time.
495*b843c749SSergey Zigachev 			 */
496*b843c749SSergey Zigachev 			for (; add_count - find_count > min_fill;
497*b843c749SSergey Zigachev 			     find_count++) {
498*b843c749SSergey Zigachev 				ret = chash_table_copy_out(&table, find_count,
499*b843c749SSergey Zigachev 							   &value);
500*b843c749SSergey Zigachev 				if (ret < 0) {
501*b843c749SSergey Zigachev 					pr_err("chash_table_copy_out failed: %d\n",
502*b843c749SSergey Zigachev 					       ret);
503*b843c749SSergey Zigachev 					goto out;
504*b843c749SSergey Zigachev 				}
505*b843c749SSergey Zigachev 				if (value != ~find_count) {
506*b843c749SSergey Zigachev 					pr_err("Wrong value retrieved for key 0x%llx, expected 0x%llx got 0x%llx\n",
507*b843c749SSergey Zigachev 					       find_count, ~find_count, value);
508*b843c749SSergey Zigachev #ifdef CHASH_DEBUG
509*b843c749SSergey Zigachev 					chash_table_dump(&table.table);
510*b843c749SSergey Zigachev #endif
511*b843c749SSergey Zigachev 					ret = -EFAULT;
512*b843c749SSergey Zigachev 					goto out;
513*b843c749SSergey Zigachev 				}
514*b843c749SSergey Zigachev 				ret = chash_table_copy_in(&table, find_count,
515*b843c749SSergey Zigachev 							  &value);
516*b843c749SSergey Zigachev 				if (ret != 1) {
517*b843c749SSergey Zigachev 					pr_err("copy_in second time returned %d, expected 1\n",
518*b843c749SSergey Zigachev 					       ret);
519*b843c749SSergey Zigachev 					ret = -EFAULT;
520*b843c749SSergey Zigachev 					goto out;
521*b843c749SSergey Zigachev 				}
522*b843c749SSergey Zigachev 			}
523*b843c749SSergey Zigachev 			/* Remove them until we hit min_fill level */
524*b843c749SSergey Zigachev 			for (; add_count - rmv_count > min_fill; rmv_count++) {
525*b843c749SSergey Zigachev 				ret = chash_table_remove(&table, rmv_count,
526*b843c749SSergey Zigachev 							 NULL);
527*b843c749SSergey Zigachev 				if (ret < 0) {
528*b843c749SSergey Zigachev 					pr_err("chash_table_remove failed: %d\n",
529*b843c749SSergey Zigachev 					       ret);
530*b843c749SSergey Zigachev 					goto out;
531*b843c749SSergey Zigachev 				}
532*b843c749SSergey Zigachev 			}
533*b843c749SSergey Zigachev 		}
534*b843c749SSergey Zigachev 
535*b843c749SSergey Zigachev 		/* Add a new value */
536*b843c749SSergey Zigachev 		value = ~add_count;
537*b843c749SSergey Zigachev 		ret = chash_table_copy_in(&table, add_count, &value);
538*b843c749SSergey Zigachev 		if (ret != 0) {
539*b843c749SSergey Zigachev 			pr_err("copy_in first time returned %d, expected 0\n",
540*b843c749SSergey Zigachev 			       ret);
541*b843c749SSergey Zigachev 			ret = -EFAULT;
542*b843c749SSergey Zigachev 			goto out;
543*b843c749SSergey Zigachev 		}
544*b843c749SSergey Zigachev 	}
545*b843c749SSergey Zigachev 
546*b843c749SSergey Zigachev 	chash_table_dump_stats(&table);
547*b843c749SSergey Zigachev 	chash_table_reset_stats(&table);
548*b843c749SSergey Zigachev 
549*b843c749SSergey Zigachev out:
550*b843c749SSergey Zigachev 	chash_table_free(&table);
551*b843c749SSergey Zigachev 	return ret;
552*b843c749SSergey Zigachev }
553*b843c749SSergey Zigachev 
554*b843c749SSergey Zigachev static unsigned int chash_test_bits = 10;
555*b843c749SSergey Zigachev MODULE_PARM_DESC(test_bits,
556*b843c749SSergey Zigachev 		 "Selftest number of hash bits ([4..20], default=10)");
557*b843c749SSergey Zigachev module_param_named(test_bits, chash_test_bits, uint, 0444);
558*b843c749SSergey Zigachev 
559*b843c749SSergey Zigachev static unsigned int chash_test_keysize = 8;
560*b843c749SSergey Zigachev MODULE_PARM_DESC(test_keysize, "Selftest keysize (4 or 8, default=8)");
561*b843c749SSergey Zigachev module_param_named(test_keysize, chash_test_keysize, uint, 0444);
562*b843c749SSergey Zigachev 
563*b843c749SSergey Zigachev static unsigned int chash_test_minfill;
564*b843c749SSergey Zigachev MODULE_PARM_DESC(test_minfill, "Selftest minimum #entries (default=50%)");
565*b843c749SSergey Zigachev module_param_named(test_minfill, chash_test_minfill, uint, 0444);
566*b843c749SSergey Zigachev 
567*b843c749SSergey Zigachev static unsigned int chash_test_maxfill;
568*b843c749SSergey Zigachev MODULE_PARM_DESC(test_maxfill, "Selftest maximum #entries (default=80%)");
569*b843c749SSergey Zigachev module_param_named(test_maxfill, chash_test_maxfill, uint, 0444);
570*b843c749SSergey Zigachev 
571*b843c749SSergey Zigachev static unsigned long chash_test_iters;
572*b843c749SSergey Zigachev MODULE_PARM_DESC(test_iters, "Selftest iterations (default=1000 x #entries)");
573*b843c749SSergey Zigachev module_param_named(test_iters, chash_test_iters, ulong, 0444);
574*b843c749SSergey Zigachev 
chash_init(void)575*b843c749SSergey Zigachev static int __init chash_init(void)
576*b843c749SSergey Zigachev {
577*b843c749SSergey Zigachev 	int ret;
578*b843c749SSergey Zigachev 	u64 ts1_ns;
579*b843c749SSergey Zigachev 
580*b843c749SSergey Zigachev 	/* Skip self test on user errors */
581*b843c749SSergey Zigachev 	if (chash_test_bits < 4 || chash_test_bits > 20) {
582*b843c749SSergey Zigachev 		pr_err("chash: test_bits out of range [4..20].\n");
583*b843c749SSergey Zigachev 		return 0;
584*b843c749SSergey Zigachev 	}
585*b843c749SSergey Zigachev 	if (chash_test_keysize != 4 && chash_test_keysize != 8) {
586*b843c749SSergey Zigachev 		pr_err("chash: test_keysize invalid. Must be 4 or 8.\n");
587*b843c749SSergey Zigachev 		return 0;
588*b843c749SSergey Zigachev 	}
589*b843c749SSergey Zigachev 
590*b843c749SSergey Zigachev 	if (!chash_test_minfill)
591*b843c749SSergey Zigachev 		chash_test_minfill = (1 << chash_test_bits) / 2;
592*b843c749SSergey Zigachev 	if (!chash_test_maxfill)
593*b843c749SSergey Zigachev 		chash_test_maxfill = (1 << chash_test_bits) * 4 / 5;
594*b843c749SSergey Zigachev 	if (!chash_test_iters)
595*b843c749SSergey Zigachev 		chash_test_iters = (1 << chash_test_bits) * 1000;
596*b843c749SSergey Zigachev 
597*b843c749SSergey Zigachev 	if (chash_test_minfill >= (1 << chash_test_bits)) {
598*b843c749SSergey Zigachev 		pr_err("chash: test_minfill too big. Must be < table size.\n");
599*b843c749SSergey Zigachev 		return 0;
600*b843c749SSergey Zigachev 	}
601*b843c749SSergey Zigachev 	if (chash_test_maxfill >= (1 << chash_test_bits)) {
602*b843c749SSergey Zigachev 		pr_err("chash: test_maxfill too big. Must be < table size.\n");
603*b843c749SSergey Zigachev 		return 0;
604*b843c749SSergey Zigachev 	}
605*b843c749SSergey Zigachev 	if (chash_test_minfill >= chash_test_maxfill) {
606*b843c749SSergey Zigachev 		pr_err("chash: test_minfill must be < test_maxfill.\n");
607*b843c749SSergey Zigachev 		return 0;
608*b843c749SSergey Zigachev 	}
609*b843c749SSergey Zigachev 	if (chash_test_keysize == 4 && chash_test_iters > 0xffffffff) {
610*b843c749SSergey Zigachev 		pr_err("chash: test_iters must be < 4G for 4 byte keys.\n");
611*b843c749SSergey Zigachev 		return 0;
612*b843c749SSergey Zigachev 	}
613*b843c749SSergey Zigachev 
614*b843c749SSergey Zigachev 	ts1_ns = local_clock();
615*b843c749SSergey Zigachev 	ret = chash_self_test(chash_test_bits, chash_test_keysize,
616*b843c749SSergey Zigachev 			      chash_test_minfill, chash_test_maxfill,
617*b843c749SSergey Zigachev 			      chash_test_iters);
618*b843c749SSergey Zigachev 	if (!ret) {
619*b843c749SSergey Zigachev 		u64 ts_delta_us = local_clock() - ts1_ns;
620*b843c749SSergey Zigachev 		u64 iters_per_second = (u64)chash_test_iters * 1000000;
621*b843c749SSergey Zigachev 
622*b843c749SSergey Zigachev 		do_div(ts_delta_us, 1000);
623*b843c749SSergey Zigachev 		do_div(iters_per_second, ts_delta_us);
624*b843c749SSergey Zigachev 		pr_info("chash: self test took %llu us, %llu iterations/s\n",
625*b843c749SSergey Zigachev 			ts_delta_us, iters_per_second);
626*b843c749SSergey Zigachev 	} else {
627*b843c749SSergey Zigachev 		pr_err("chash: self test failed: %d\n", ret);
628*b843c749SSergey Zigachev 	}
629*b843c749SSergey Zigachev 
630*b843c749SSergey Zigachev 	return ret;
631*b843c749SSergey Zigachev }
632*b843c749SSergey Zigachev 
633*b843c749SSergey Zigachev module_init(chash_init);
634*b843c749SSergey Zigachev 
635*b843c749SSergey Zigachev #endif /* CONFIG_CHASH_SELFTEST */
636*b843c749SSergey Zigachev 
637*b843c749SSergey Zigachev MODULE_DESCRIPTION("Closed hash table");
638*b843c749SSergey Zigachev MODULE_LICENSE("GPL and additional rights");
639