2 * Copyright 2017 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
24 #include <linux/types.h>
25 #include <linux/hash.h>
26 #include <linux/bug.h>
27 #include <linux/slab.h>
28 #include <linux/module.h>
29 #include <linux/sched/clock.h>
30 #include <asm/div64.h>
31 #include <linux/chash.h>
34 * chash_table_alloc - Allocate closed hash table
35 * @table: Pointer to the table structure
36 * @bits: Table size will be 2^bits entries
37 * @key_size: Size of hash keys in bytes, 4 or 8
38 * @value_size: Size of data values in bytes, can be 0
40 int chash_table_alloc(struct chash_table
*table
, u8 bits
, u8 key_size
,
41 unsigned int value_size
, gfp_t gfp_mask
)
46 if (key_size
!= 4 && key_size
!= 8)
49 table
->data
= kcalloc(__CHASH_DATA_SIZE(bits
, key_size
, value_size
),
50 sizeof(long), gfp_mask
);
54 __CHASH_TABLE_INIT(table
->table
, table
->data
,
55 bits
, key_size
, value_size
);
59 EXPORT_SYMBOL(chash_table_alloc
);
62 * chash_table_free - Free closed hash table
63 * @table: Pointer to the table structure
65 void chash_table_free(struct chash_table
*table
)
69 EXPORT_SYMBOL(chash_table_free
);
71 #ifdef CONFIG_CHASH_STATS
73 #define DIV_FRAC(nom, denom, quot, frac, frac_digits) do { \
75 u64 __denom = (denom); \
79 while (__denom >> 32) { \
84 __rem = do_div(__quot, __denom); \
85 __frac = __rem * (frac_digits) + (__denom >> 1); \
86 do_div(__frac, __denom); \
91 void __chash_table_dump_stats(struct __chash_table
*table
)
93 struct chash_iter iter
= CHASH_ITER_INIT(table
, 0);
94 u32 filled
= 0, empty
= 0, tombstones
= 0;
99 if (chash_iter_is_valid(iter
))
101 else if (chash_iter_is_empty(iter
))
105 CHASH_ITER_INC(iter
);
108 pr_debug("chash: key size %u, value size %u\n",
109 table
->key_size
, table
->value_size
);
110 pr_debug(" Slots total/filled/empty/tombstones: %u / %u / %u / %u\n",
111 1 << table
->bits
, filled
, empty
, tombstones
);
112 if (table
->hits
> 0) {
113 DIV_FRAC(table
->hits_steps
, table
->hits
, quot1
, frac1
, 1000);
114 DIV_FRAC(table
->hits
* 1000, table
->hits_time_ns
,
120 pr_debug(" Hits (avg.cost, rate): %llu (%llu.%03u, %llu.%03u M/s)\n",
121 table
->hits
, quot1
, frac1
, quot2
, frac2
);
122 if (table
->miss
> 0) {
123 DIV_FRAC(table
->miss_steps
, table
->miss
, quot1
, frac1
, 1000);
124 DIV_FRAC(table
->miss
* 1000, table
->miss_time_ns
,
130 pr_debug(" Misses (avg.cost, rate): %llu (%llu.%03u, %llu.%03u M/s)\n",
131 table
->miss
, quot1
, frac1
, quot2
, frac2
);
132 if (table
->hits
+ table
->miss
> 0) {
133 DIV_FRAC(table
->hits_steps
+ table
->miss_steps
,
134 table
->hits
+ table
->miss
, quot1
, frac1
, 1000);
135 DIV_FRAC((table
->hits
+ table
->miss
) * 1000,
136 (table
->hits_time_ns
+ table
->miss_time_ns
),
142 pr_debug(" Total (avg.cost, rate): %llu (%llu.%03u, %llu.%03u M/s)\n",
143 table
->hits
+ table
->miss
, quot1
, frac1
, quot2
, frac2
);
144 if (table
->relocs
> 0) {
145 DIV_FRAC(table
->hits
+ table
->miss
, table
->relocs
,
147 DIV_FRAC(table
->reloc_dist
, table
->relocs
, quot2
, frac2
, 1000);
148 pr_debug(" Relocations (freq, avg.dist): %llu (1:%llu.%03u, %llu.%03u)\n",
149 table
->relocs
, quot1
, frac1
, quot2
, frac2
);
151 pr_debug(" No relocations\n");
154 EXPORT_SYMBOL(__chash_table_dump_stats
);
159 #define CHASH_INC(table, a) ((a) = ((a) + 1) & (table)->size_mask)
160 #define CHASH_ADD(table, a, b) (((a) + (b)) & (table)->size_mask)
161 #define CHASH_SUB(table, a, b) (((a) - (b)) & (table)->size_mask)
162 #define CHASH_IN_RANGE(table, slot, first, last) \
163 (CHASH_SUB(table, slot, first) <= CHASH_SUB(table, last, first))
165 /*#define CHASH_DEBUG Uncomment this to enable verbose debug output*/
167 static void chash_table_dump(struct __chash_table
*table
)
169 struct chash_iter iter
= CHASH_ITER_INIT(table
, 0);
172 if ((iter
.slot
& 3) == 0)
173 pr_debug("%04x: ", iter
.slot
);
175 if (chash_iter_is_valid(iter
))
176 pr_debug("[%016llx] ", chash_iter_key(iter
));
177 else if (chash_iter_is_empty(iter
))
178 pr_debug("[ <empty> ] ");
180 pr_debug("[ <tombstone> ] ");
182 if ((iter
.slot
& 3) == 3)
185 CHASH_ITER_INC(iter
);
188 if ((iter
.slot
& 3) != 0)
192 static int chash_table_check(struct __chash_table
*table
)
195 struct chash_iter iter
= CHASH_ITER_INIT(table
, 0);
196 struct chash_iter cur
= CHASH_ITER_INIT(table
, 0);
199 if (!chash_iter_is_valid(iter
)) {
200 CHASH_ITER_INC(iter
);
204 hash
= chash_iter_hash(iter
);
205 CHASH_ITER_SET(cur
, hash
);
206 while (cur
.slot
!= iter
.slot
) {
207 if (chash_iter_is_empty(cur
)) {
208 pr_err("Path to element at %x with hash %x broken at slot %x\n",
209 iter
.slot
, hash
, cur
.slot
);
210 chash_table_dump(table
);
216 CHASH_ITER_INC(iter
);
223 static void chash_iter_relocate(struct chash_iter dst
, struct chash_iter src
)
225 BUG_ON(src
.table
== dst
.table
&& src
.slot
== dst
.slot
);
226 BUG_ON(src
.table
->key_size
!= dst
.table
->key_size
);
227 BUG_ON(src
.table
->value_size
!= dst
.table
->value_size
);
229 if (dst
.table
->key_size
== 4)
230 dst
.table
->keys32
[dst
.slot
] = src
.table
->keys32
[src
.slot
];
232 dst
.table
->keys64
[dst
.slot
] = src
.table
->keys64
[src
.slot
];
234 if (dst
.table
->value_size
)
235 memcpy(chash_iter_value(dst
), chash_iter_value(src
),
236 dst
.table
->value_size
);
238 chash_iter_set_valid(dst
);
239 chash_iter_set_invalid(src
);
241 #ifdef CONFIG_CHASH_STATS
242 if (src
.table
== dst
.table
) {
244 dst
.table
->reloc_dist
+=
245 CHASH_SUB(dst
.table
, src
.slot
, dst
.slot
);
251 * __chash_table_find - Helper for looking up a hash table entry
252 * @iter: Pointer to hash table iterator
253 * @key: Key of the entry to find
254 * @for_removal: set to true if the element will be removed soon
256 * Searches for an entry in the hash table with a given key. iter must
257 * be initialized by the caller to point to the home position of the
258 * hypothetical entry, i.e. it must be initialized with the hash table
259 * and the key's hash as the initial slot for the search.
261 * This function also does some local clean-up to speed up future
262 * look-ups by relocating entries to better slots and removing
263 * tombstones that are no longer needed.
265 * If @for_removal is true, the function avoids relocating the entry
266 * that is being returned.
268 * Returns 0 if the search is successful. In this case iter is updated
269 * to point to the found entry. Otherwise %-EINVAL is returned and the
270 * iter is updated to point to the first available slot for the given
271 * key. If the table is full, the slot is set to -1.
273 static int chash_table_find(struct chash_iter
*iter
, u64 key
,
276 #ifdef CONFIG_CHASH_STATS
277 u64 ts1
= local_clock();
279 u32 hash
= iter
->slot
;
280 struct chash_iter first_redundant
= CHASH_ITER_INIT(iter
->table
, -1);
281 int first_avail
= (for_removal
? -2 : -1);
283 while (!chash_iter_is_valid(*iter
) || chash_iter_key(*iter
) != key
) {
284 if (chash_iter_is_empty(*iter
)) {
285 /* Found an empty slot, which ends the
286 * search. Clean up any preceding tombstones
287 * that are no longer needed because they lead
290 if ((int)first_redundant
.slot
< 0)
292 while (first_redundant
.slot
!= iter
->slot
) {
293 if (!chash_iter_is_valid(first_redundant
))
294 chash_iter_set_empty(first_redundant
);
295 CHASH_ITER_INC(first_redundant
);
298 chash_table_check(iter
->table
);
301 } else if (!chash_iter_is_valid(*iter
)) {
302 /* Found a tombstone. Remember it as candidate
303 * for relocating the entry we're looking for
304 * or for adding a new entry with the given key
306 if (first_avail
== -1)
307 first_avail
= iter
->slot
;
308 /* Or mark it as the start of a series of
309 * potentially redundant tombstones
311 else if (first_redundant
.slot
== -1)
312 CHASH_ITER_SET(first_redundant
, iter
->slot
);
313 } else if (first_redundant
.slot
>= 0) {
314 /* Found a valid, occupied slot with a
315 * preceding series of tombstones. Relocate it
316 * to a better position that no longer depends
317 * on those tombstones
319 u32 cur_hash
= chash_iter_hash(*iter
);
321 if (!CHASH_IN_RANGE(iter
->table
, cur_hash
,
322 first_redundant
.slot
+ 1,
324 /* This entry has a hash at or before
325 * the first tombstone we found. We
326 * can relocate it to that tombstone
327 * and advance to the next tombstone
329 chash_iter_relocate(first_redundant
, *iter
);
331 CHASH_ITER_INC(first_redundant
);
332 } while (chash_iter_is_valid(first_redundant
));
333 } else if (cur_hash
!= iter
->slot
) {
334 /* Relocate entry to its home position
335 * or as close as possible so it no
336 * longer depends on any preceding
339 struct chash_iter new_iter
=
340 CHASH_ITER_INIT(iter
->table
, cur_hash
);
342 while (new_iter
.slot
!= iter
->slot
&&
343 chash_iter_is_valid(new_iter
))
344 CHASH_ITER_INC(new_iter
);
346 if (new_iter
.slot
!= iter
->slot
)
347 chash_iter_relocate(new_iter
, *iter
);
351 CHASH_ITER_INC(*iter
);
352 if (iter
->slot
== hash
) {
358 #ifdef CONFIG_CHASH_STATS
360 iter
->table
->hits_steps
+= CHASH_SUB(iter
->table
, iter
->slot
, hash
) + 1;
363 if (first_avail
>= 0) {
364 CHASH_ITER_SET(first_redundant
, first_avail
);
365 chash_iter_relocate(first_redundant
, *iter
);
366 iter
->slot
= first_redundant
.slot
;
367 iter
->mask
= first_redundant
.mask
;
370 #ifdef CONFIG_CHASH_STATS
371 iter
->table
->hits_time_ns
+= local_clock() - ts1
;
377 #ifdef CONFIG_CHASH_STATS
379 iter
->table
->miss_steps
+= (iter
->slot
< 0) ?
380 (1 << iter
->table
->bits
) :
381 CHASH_SUB(iter
->table
, iter
->slot
, hash
) + 1;
384 if (first_avail
>= 0)
385 CHASH_ITER_SET(*iter
, first_avail
);
387 #ifdef CONFIG_CHASH_STATS
388 iter
->table
->miss_time_ns
+= local_clock() - ts1
;
394 int __chash_table_copy_in(struct __chash_table
*table
, u64 key
,
397 u32 hash
= (table
->key_size
== 4) ?
398 hash_32(key
, table
->bits
) : hash_64(key
, table
->bits
);
399 struct chash_iter iter
= CHASH_ITER_INIT(table
, hash
);
400 int r
= chash_table_find(&iter
, key
, false);
402 /* Found an existing entry */
404 if (value
&& table
->value_size
)
405 memcpy(chash_iter_value(iter
), value
,
410 /* Is there a place to add a new entry? */
412 pr_err("Hash table overflow\n");
416 chash_iter_set_valid(iter
);
418 if (table
->key_size
== 4)
419 table
->keys32
[iter
.slot
] = key
;
421 table
->keys64
[iter
.slot
] = key
;
422 if (value
&& table
->value_size
)
423 memcpy(chash_iter_value(iter
), value
, table
->value_size
);
427 EXPORT_SYMBOL(__chash_table_copy_in
);
429 int __chash_table_copy_out(struct __chash_table
*table
, u64 key
,
430 void *value
, bool remove
)
432 u32 hash
= (table
->key_size
== 4) ?
433 hash_32(key
, table
->bits
) : hash_64(key
, table
->bits
);
434 struct chash_iter iter
= CHASH_ITER_INIT(table
, hash
);
435 int r
= chash_table_find(&iter
, key
, remove
);
440 if (value
&& table
->value_size
)
441 memcpy(value
, chash_iter_value(iter
), table
->value_size
);
444 chash_iter_set_invalid(iter
);
448 EXPORT_SYMBOL(__chash_table_copy_out
);
450 #ifdef CONFIG_CHASH_SELFTEST
452 * chash_self_test - Run a self-test of the hash table implementation
453 * @bits: Table size will be 2^bits entries
454 * @key_size: Size of hash keys in bytes, 4 or 8
455 * @min_fill: Minimum fill level during the test
456 * @max_fill: Maximum fill level during the test
457 * @iterations: Number of test iterations
459 * The test adds and removes entries from a hash table, cycling the
460 * fill level between min_fill and max_fill entries. Also tests lookup
461 * and value retrieval.
463 static int __init
chash_self_test(u8 bits
, u8 key_size
,
464 int min_fill
, int max_fill
,
467 struct chash_table table
;
469 u64 add_count
, rmv_count
;
472 if (key_size
== 4 && iterations
> 0xffffffff)
474 if (min_fill
>= max_fill
)
477 ret
= chash_table_alloc(&table
, bits
, key_size
, sizeof(u64
),
480 pr_err("chash_table_alloc failed: %d\n", ret
);
484 for (add_count
= 0, rmv_count
= 0; add_count
< iterations
;
486 /* When we hit the max_fill level, remove entries down
489 if (add_count
- rmv_count
== max_fill
) {
490 u64 find_count
= rmv_count
;
492 /* First try to find all entries that we're
493 * about to remove, confirm their value, test
494 * writing them back a second time.
496 for (; add_count
- find_count
> min_fill
;
498 ret
= chash_table_copy_out(&table
, find_count
,
501 pr_err("chash_table_copy_out failed: %d\n",
505 if (value
!= ~find_count
) {
506 pr_err("Wrong value retrieved for key 0x%llx, expected 0x%llx got 0x%llx\n",
507 find_count
, ~find_count
, value
);
509 chash_table_dump(&table
.table
);
514 ret
= chash_table_copy_in(&table
, find_count
,
517 pr_err("copy_in second time returned %d, expected 1\n",
523 /* Remove them until we hit min_fill level */
524 for (; add_count
- rmv_count
> min_fill
; rmv_count
++) {
525 ret
= chash_table_remove(&table
, rmv_count
,
528 pr_err("chash_table_remove failed: %d\n",
535 /* Add a new value */
537 ret
= chash_table_copy_in(&table
, add_count
, &value
);
539 pr_err("copy_in first time returned %d, expected 0\n",
546 chash_table_dump_stats(&table
);
547 chash_table_reset_stats(&table
);
550 chash_table_free(&table
);
554 static unsigned int chash_test_bits
= 10;
555 MODULE_PARM_DESC(test_bits
,
556 "Selftest number of hash bits ([4..20], default=10)");
557 module_param_named(test_bits
, chash_test_bits
, uint
, 0444);
559 static unsigned int chash_test_keysize
= 8;
560 MODULE_PARM_DESC(test_keysize
, "Selftest keysize (4 or 8, default=8)");
561 module_param_named(test_keysize
, chash_test_keysize
, uint
, 0444);
563 static unsigned int chash_test_minfill
;
564 MODULE_PARM_DESC(test_minfill
, "Selftest minimum #entries (default=50%)");
565 module_param_named(test_minfill
, chash_test_minfill
, uint
, 0444);
567 static unsigned int chash_test_maxfill
;
568 MODULE_PARM_DESC(test_maxfill
, "Selftest maximum #entries (default=80%)");
569 module_param_named(test_maxfill
, chash_test_maxfill
, uint
, 0444);
571 static unsigned long chash_test_iters
;
572 MODULE_PARM_DESC(test_iters
, "Selftest iterations (default=1000 x #entries)");
573 module_param_named(test_iters
, chash_test_iters
, ulong
, 0444);
575 static int __init
chash_init(void)
580 /* Skip self test on user errors */
581 if (chash_test_bits
< 4 || chash_test_bits
> 20) {
582 pr_err("chash: test_bits out of range [4..20].\n");
585 if (chash_test_keysize
!= 4 && chash_test_keysize
!= 8) {
586 pr_err("chash: test_keysize invalid. Must be 4 or 8.\n");
590 if (!chash_test_minfill
)
591 chash_test_minfill
= (1 << chash_test_bits
) / 2;
592 if (!chash_test_maxfill
)
593 chash_test_maxfill
= (1 << chash_test_bits
) * 4 / 5;
594 if (!chash_test_iters
)
595 chash_test_iters
= (1 << chash_test_bits
) * 1000;
597 if (chash_test_minfill
>= (1 << chash_test_bits
)) {
598 pr_err("chash: test_minfill too big. Must be < table size.\n");
601 if (chash_test_maxfill
>= (1 << chash_test_bits
)) {
602 pr_err("chash: test_maxfill too big. Must be < table size.\n");
605 if (chash_test_minfill
>= chash_test_maxfill
) {
606 pr_err("chash: test_minfill must be < test_maxfill.\n");
609 if (chash_test_keysize
== 4 && chash_test_iters
> 0xffffffff) {
610 pr_err("chash: test_iters must be < 4G for 4 byte keys.\n");
614 ts1_ns
= local_clock();
615 ret
= chash_self_test(chash_test_bits
, chash_test_keysize
,
616 chash_test_minfill
, chash_test_maxfill
,
619 u64 ts_delta_us
= local_clock() - ts1_ns
;
620 u64 iters_per_second
= (u64
)chash_test_iters
* 1000000;
622 do_div(ts_delta_us
, 1000);
623 do_div(iters_per_second
, ts_delta_us
);
624 pr_info("chash: self test took %llu us, %llu iterations/s\n",
625 ts_delta_us
, iters_per_second
);
627 pr_err("chash: self test failed: %d\n", ret
);
633 module_init(chash_init
);
635 #endif /* CONFIG_CHASH_SELFTEST */
637 MODULE_DESCRIPTION("Closed hash table");
638 MODULE_LICENSE("GPL and additional rights");