1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2022 NVIDIA corporation & affiliates.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice unmodified, this list of conditions, and the following
11 * disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #ifndef _LINUXKPI_LINUX_HASHTABLE_H
29 #define _LINUXKPI_LINUX_HASHTABLE_H
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33
34 #include <linux/hash.h>
35 #include <linux/kernel.h>
36 #include <linux/list.h>
37 #include <linux/log2.h>
38 #include <linux/rcupdate.h>
39 #include <linux/rculist.h>
40
41 #include <ck_queue.h>
42
43 struct lkpi_hash_entry {
44 CK_LIST_ENTRY(lkpi_hash_entry) entry;
45 };
46
47 struct lkpi_hash_head {
48 CK_LIST_HEAD(, lkpi_hash_entry) head;
49 };
50
51 #define DEFINE_HASHTABLE(name, bits) \
52 struct lkpi_hash_head name[1UL << (bits)]
53
54 #define DEFINE_READ_MOSTLY_HASHTABLE(name, bits) \
55 struct lkpi_hash_head name[1UL << (bits)] __read_mostly
56
57 #define DECLARE_HASHTABLE(name, bits) \
58 struct lkpi_hash_head name[1UL << (bits)]
59
60 #define HASH_SIZE(name) ARRAY_SIZE(name)
61 #define HASH_BITS(name) ilog2(HASH_SIZE(name))
62
63 #define hash_min(...) \
64 hash_long(__VA_ARGS__)
65
66 static inline void
__hash_init(struct lkpi_hash_head * ht,unsigned long size)67 __hash_init(struct lkpi_hash_head *ht, unsigned long size)
68 {
69 unsigned long x;
70
71 for (x = 0; x != size; x++)
72 CK_LIST_INIT(&ht[x].head);
73 }
74
75 #define hash_init(ht) \
76 __hash_init(ht, HASH_SIZE(ht))
77
78 #define hash_add(...) \
79 hash_add_rcu(__VA_ARGS__)
80
81 static inline void
__hash_node_type_assert(struct hlist_node * node)82 __hash_node_type_assert(struct hlist_node *node)
83 {
84 /*
85 * Unfortunately Linux doesn't have an own type for the hash
86 * table node entries. The purpose of this function is simply
87 * to check the type of the passed argument.
88 */
89 CTASSERT(sizeof(struct lkpi_hash_entry) == sizeof(*node));
90 }
91
92 #define hash_add_rcu(ht, node, key) do { \
93 struct lkpi_hash_head *__head = &(ht)[hash_min(key, HASH_BITS(ht))]; \
94 __hash_node_type_assert(node); \
95 CK_LIST_INSERT_HEAD(&__head->head, \
96 (struct lkpi_hash_entry *)(node), entry); \
97 } while (0)
98
99 static inline bool
hash_hashed(struct hlist_node * node)100 hash_hashed(struct hlist_node *node)
101 {
102 return (((struct lkpi_hash_entry *)node)->entry.cle_prev != NULL);
103 }
104
105 static inline bool
__hash_empty(struct lkpi_hash_head * ht,unsigned long size)106 __hash_empty(struct lkpi_hash_head *ht, unsigned long size)
107 {
108 unsigned long x;
109
110 for (x = 0; x != size; x++) {
111 if (!CK_LIST_EMPTY(&ht[x].head))
112 return (false);
113 }
114 return (true);
115 }
116
117 #define hash_empty(ht) \
118 __hash_empty(ht, HASH_SIZE(ht))
119
120 #define hash_del(...) \
121 hash_del_rcu(__VA_ARGS__)
122
123 static inline void
hash_del_rcu(struct hlist_node * node)124 hash_del_rcu(struct hlist_node *node)
125 {
126 CK_LIST_REMOVE((struct lkpi_hash_entry *)node, entry);
127 memset(node, 0, sizeof(*node));
128 }
129
130 #define __hash_first(ht, type, member) ({ \
131 const struct lkpi_hash_entry *__first = CK_LIST_FIRST(&(ht)->head); \
132 __hash_node_type_assert(&((type *)0)->member); \
133 (__first != NULL ? container_of((const void *)__first, type, member) : NULL); \
134 })
135
136 #define __hash_next(obj, type, member) ({ \
137 const struct lkpi_hash_entry *__next = \
138 CK_LIST_NEXT((struct lkpi_hash_entry *)&(obj)->member, entry); \
139 __hash_node_type_assert(&(obj)->member); \
140 (__next != NULL ? container_of((const void *)__next, type, member) : NULL); \
141 })
142
143 #define hash_for_each(...) \
144 hash_for_each_rcu(__VA_ARGS__)
145
146 #define hash_for_each_rcu(name, bkt, obj, member) \
147 for ((bkt) = 0, (obj) = NULL; (obj) == NULL && \
148 (bkt) != HASH_SIZE(name); (bkt)++) \
149 for ((obj) = __hash_first(&(name)[bkt], \
150 __typeof(*(obj)), member); \
151 (obj) != NULL; \
152 (obj) = __hash_next(obj, \
153 __typeof(*(obj)), member))
154
155 #define hash_for_each_safe(name, bkt, tmp, obj, member) \
156 for ((bkt) = 0, (obj) = NULL; (obj) == NULL && \
157 (bkt) != HASH_SIZE(name); (bkt)++) \
158 for ((obj) = __hash_first(&(name)[bkt], \
159 __typeof(*(obj)), member); \
160 (obj) != NULL && ((tmp) = &__hash_next(obj, \
161 __typeof(*(obj)), member)->member, 1); \
162 (obj) = container_of(tmp, __typeof(*(obj)), member))
163
164 #define hash_for_each_possible(...) \
165 hash_for_each_possible_rcu(__VA_ARGS__)
166
167 #define hash_for_each_possible_rcu_notrace(...) \
168 hash_for_each_possible_rcu(__VA_ARGS__)
169
170 #define hash_for_each_possible_rcu(name, obj, member, key) \
171 for ((obj) = __hash_first(&(name)[hash_min(key, HASH_BITS(name))], \
172 __typeof(*(obj)), member); \
173 (obj) != NULL; \
174 (obj) = __hash_next(obj, __typeof(*(obj)), member))
175
176 #define hash_for_each_possible_safe(name, obj, tmp, member, key) \
177 for ((obj) = __hash_first(&(name)[hash_min(key, HASH_BITS(name))], \
178 __typeof(*(obj)), member); \
179 (obj) != NULL && ((tmp) = &__hash_next(obj, \
180 __typeof(*(obj)), member)->member, 1); \
181 (obj) = container_of(tmp, __typeof(*(obj)), member))
182
183 #endif /* _LINUXKPI_LINUX_HASHTABLE_H */
184