/src/openssl35/crypto/hashtable/hashtable.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2024-2025 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | * |
9 | | * |
10 | | * |
11 | | * Notes On hash table design and layout |
12 | | * This hashtable uses a hopscotch algorithm to do indexing. The data structure |
13 | | * looks as follows: |
14 | | * |
15 | | * hash +--------------+ |
16 | | * value+------->+ HT_VALUE | |
17 | | * + +--------------+ |
18 | | * +-------+ |
19 | | * | | |
20 | | * +---------------------------------------------------------+ |
21 | | * | | | | | | |
22 | | * | entry | entry | entry | entry | | |
23 | | * | | | | | | |
24 | | * +---------------------------------------------------------+ |
25 | | * | | | |
26 | | * | | | |
27 | | * +---------------------------------------------------------+ |
28 | | * | + + + |
29 | | * | neighborhood[0] neighborhood[1] | |
30 | | * | | |
31 | | * | | |
32 | | * +---------------------------------------------------------+ |
33 | | * | |
34 | | * + |
35 | | * neighborhoods |
36 | | * |
37 | | * On lookup/insert/delete, the items key is hashed to a 64 bit value |
38 | | * and the result is masked to provide an index into the neighborhoods |
39 | | * table. Once a neighborhood is determined, an in-order search is done |
40 | | * of the elements in the neighborhood indexes entries for a matching hash |
41 | | * value, if found, the corresponding HT_VALUE is used for the respective |
42 | | * operation. The number of entries in a neighborhood is determined at build |
43 | | * time based on the cacheline size of the target CPU. The intent is for a |
44 | | * neighborhood to have all entries in the neighborhood fit into a single cache |
45 | | * line to speed up lookups. If all entries in a neighborhood are in use at the |
46 | | * time of an insert, the table is expanded and rehashed. |
47 | | * |
48 | | * Lockless reads hash table is based on the same design but does not |
49 | | * allow growing and deletion. Thus subsequent neighborhoods are always |
50 | | * searched for a match until an empty entry is found. |
51 | | */ |
52 | | |
53 | | #include <string.h> |
54 | | #include <internal/rcu.h> |
55 | | #include <internal/hashtable.h> |
56 | | #include <internal/hashfunc.h> |
57 | | #include <openssl/rand.h> |
58 | | |
59 | | /* |
60 | | * gcc defines __SANITIZE_THREAD__ |
61 | | * but clang uses the feature attributes api |
62 | | * map the latter to the former |
63 | | */ |
64 | | #if defined(__clang__) && defined(__has_feature) |
65 | | #if __has_feature(thread_sanitizer) |
66 | | #define __SANITIZE_THREADS__ |
67 | | #endif |
68 | | #endif |
69 | | |
70 | | #ifdef __SANITIZE_THREADS__ |
71 | | #include <sanitizer/tsan_interface.h> |
72 | | #endif |
73 | | |
74 | | #include "internal/numbers.h" |
75 | | /* |
76 | | * When we do a lookup/insert/delete, there is a high likelihood |
77 | | * that we will iterate over at least part of the neighborhood list |
78 | | * As such, because we design a neighborhood entry to fit into a single |
79 | | * cache line it is advantageous, when supported to fetch the entire |
80 | | * structure for faster lookups |
81 | | */ |
82 | | #if defined(__GNUC__) || defined(__CLANG__) |
83 | 26.5M | #define PREFETCH_NEIGHBORHOOD(x) __builtin_prefetch(x.entries) |
84 | 70.0M | #define PREFETCH(x) __builtin_prefetch(x) |
85 | | #define ALIGN __attribute__((aligned(8))) |
86 | | #else |
87 | | #define PREFETCH_NEIGHBORHOOD(x) |
88 | | #define PREFETCH(x) |
89 | | #define ALIGN |
90 | | #endif |
91 | | |
92 | | /* |
93 | | * Define our neighborhood list length |
94 | | * Note: It should always be a power of 2 |
95 | | */ |
96 | 477 | #define DEFAULT_NEIGH_LEN_LOG 4 |
97 | 477 | #define DEFAULT_NEIGH_LEN (1 << DEFAULT_NEIGH_LEN_LOG) |
98 | | |
99 | | /* |
100 | | * For now assume cache line size is 64 bytes |
101 | | */ |
102 | 35.7M | #define CACHE_LINE_BYTES 64 |
103 | | #define CACHE_LINE_ALIGNMENT CACHE_LINE_BYTES |
104 | | |
105 | 35.7M | #define NEIGHBORHOOD_LEN (CACHE_LINE_BYTES / sizeof(struct ht_neighborhood_entry_st)) |
106 | | /* |
107 | | * Defines our chains of values |
108 | | */ |
109 | | struct ht_internal_value_st { |
110 | | HT_VALUE value; |
111 | | HT *ht; |
112 | | }; |
113 | | |
114 | | struct ht_neighborhood_entry_st { |
115 | | uint64_t hash; |
116 | | struct ht_internal_value_st *value; |
117 | | } ALIGN; |
118 | | |
119 | | struct ht_neighborhood_st { |
120 | | struct ht_neighborhood_entry_st entries[NEIGHBORHOOD_LEN]; |
121 | | }; |
122 | | |
123 | | /* |
124 | | * Updates to data in this struct |
125 | | * require an rcu sync after modification |
126 | | * prior to free |
127 | | */ |
128 | | struct ht_mutable_data_st { |
129 | | struct ht_neighborhood_st *neighborhoods; |
130 | | void *neighborhood_ptr_to_free; |
131 | | uint64_t neighborhood_mask; |
132 | | }; |
133 | | |
134 | | /* |
135 | | * Private data may be updated on the write |
136 | | * side only, and so do not require rcu sync |
137 | | */ |
138 | | struct ht_write_private_data_st { |
139 | | size_t neighborhood_len; |
140 | | size_t value_count; |
141 | | int need_sync; |
142 | | }; |
143 | | |
144 | | struct ht_internal_st { |
145 | | HT_CONFIG config; |
146 | | CRYPTO_RCU_LOCK *lock; |
147 | | CRYPTO_RWLOCK *atomic_lock; |
148 | | struct ht_mutable_data_st *md; |
149 | | struct ht_write_private_data_st wpd; |
150 | | }; |
151 | | |
152 | | static void free_value(struct ht_internal_value_st *v); |
153 | | |
154 | | static struct ht_neighborhood_st *alloc_new_neighborhood_list(size_t len, |
155 | | void **freeptr) |
156 | 63.2k | { |
157 | 63.2k | struct ht_neighborhood_st *ret; |
158 | | |
159 | 63.2k | ret = OPENSSL_aligned_alloc(sizeof(struct ht_neighborhood_st) * len, |
160 | 63.2k | CACHE_LINE_BYTES, freeptr); |
161 | | |
162 | | /* fall back to regular malloc */ |
163 | 63.2k | if (ret == NULL) { |
164 | 0 | ret = *freeptr = OPENSSL_malloc(sizeof(struct ht_neighborhood_st) * len); |
165 | 0 | if (ret == NULL) |
166 | 0 | return NULL; |
167 | 0 | } |
168 | 63.2k | memset(ret, 0, sizeof(struct ht_neighborhood_st) * len); |
169 | 63.2k | return ret; |
170 | 63.2k | } |
171 | | |
172 | | static void internal_free_nop(HT_VALUE *v) |
173 | 24.7k | { |
174 | 24.7k | return; |
175 | 24.7k | } |
176 | | |
177 | | HT *ossl_ht_new(const HT_CONFIG *conf) |
178 | 242 | { |
179 | 242 | HT *new = OPENSSL_zalloc(sizeof(*new)); |
180 | | |
181 | 242 | if (new == NULL) |
182 | 0 | return NULL; |
183 | | |
184 | 242 | new->atomic_lock = CRYPTO_THREAD_lock_new(); |
185 | 242 | if (new->atomic_lock == NULL) |
186 | 0 | goto err; |
187 | | |
188 | 242 | memcpy(&new->config, conf, sizeof(*conf)); |
189 | | |
190 | 242 | if (new->config.init_neighborhoods != 0) { |
191 | 236 | new->wpd.neighborhood_len = new->config.init_neighborhoods; |
192 | | /* round up to the next power of 2 */ |
193 | 236 | new->wpd.neighborhood_len--; |
194 | 236 | new->wpd.neighborhood_len |= new->wpd.neighborhood_len >> 1; |
195 | 236 | new->wpd.neighborhood_len |= new->wpd.neighborhood_len >> 2; |
196 | 236 | new->wpd.neighborhood_len |= new->wpd.neighborhood_len >> 4; |
197 | 236 | new->wpd.neighborhood_len |= new->wpd.neighborhood_len >> 8; |
198 | 236 | new->wpd.neighborhood_len |= new->wpd.neighborhood_len >> 16; |
199 | 236 | new->wpd.neighborhood_len++; |
200 | 236 | } else { |
201 | 6 | new->wpd.neighborhood_len = DEFAULT_NEIGH_LEN; |
202 | 6 | } |
203 | | |
204 | 242 | if (new->config.ht_free_fn == NULL) |
205 | 236 | new->config.ht_free_fn = internal_free_nop; |
206 | | |
207 | 242 | new->md = OPENSSL_zalloc(sizeof(*new->md)); |
208 | 242 | if (new->md == NULL) |
209 | 0 | goto err; |
210 | | |
211 | 242 | new->md->neighborhoods = alloc_new_neighborhood_list(new->wpd.neighborhood_len, |
212 | 242 | &new->md->neighborhood_ptr_to_free); |
213 | 242 | if (new->md->neighborhoods == NULL) |
214 | 0 | goto err; |
215 | 242 | new->md->neighborhood_mask = new->wpd.neighborhood_len - 1; |
216 | | |
217 | 242 | new->lock = ossl_rcu_lock_new(1, conf->ctx); |
218 | 242 | if (new->lock == NULL) |
219 | 0 | goto err; |
220 | | |
221 | 242 | if (new->config.ht_hash_fn == NULL) |
222 | 242 | new->config.ht_hash_fn = ossl_fnv1a_hash; |
223 | | |
224 | 242 | return new; |
225 | | |
226 | 0 | err: |
227 | 0 | CRYPTO_THREAD_lock_free(new->atomic_lock); |
228 | 0 | ossl_rcu_lock_free(new->lock); |
229 | 0 | if (new->md != NULL) |
230 | 0 | OPENSSL_free(new->md->neighborhood_ptr_to_free); |
231 | 0 | OPENSSL_free(new->md); |
232 | 0 | OPENSSL_free(new); |
233 | 0 | return NULL; |
234 | 242 | } |
235 | | |
236 | | void ossl_ht_read_lock(HT *htable) |
237 | 39 | { |
238 | 39 | ossl_rcu_read_lock(htable->lock); |
239 | 39 | } |
240 | | |
241 | | void ossl_ht_read_unlock(HT *htable) |
242 | 58 | { |
243 | 58 | ossl_rcu_read_unlock(htable->lock); |
244 | 58 | } |
245 | | |
246 | | void ossl_ht_write_lock(HT *htable) |
247 | 285 | { |
248 | 285 | ossl_rcu_write_lock(htable->lock); |
249 | 285 | htable->wpd.need_sync = 0; |
250 | 285 | } |
251 | | |
252 | | void ossl_ht_write_unlock(HT *htable) |
253 | 285 | { |
254 | 285 | int need_sync = htable->wpd.need_sync; |
255 | | |
256 | 285 | htable->wpd.need_sync = 0; |
257 | 285 | ossl_rcu_write_unlock(htable->lock); |
258 | 285 | if (need_sync) |
259 | 181 | ossl_synchronize_rcu(htable->lock); |
260 | 285 | } |
261 | | |
262 | | static void free_oldmd(void *arg) |
263 | 31.0k | { |
264 | 31.0k | struct ht_mutable_data_st *oldmd = arg; |
265 | 31.0k | size_t i, j; |
266 | 31.0k | size_t neighborhood_len = (size_t)oldmd->neighborhood_mask + 1; |
267 | 31.0k | struct ht_internal_value_st *v; |
268 | | |
269 | 356k | for (i = 0; i < neighborhood_len; i++) { |
270 | 324k | PREFETCH_NEIGHBORHOOD(oldmd->neighborhoods[i + 1]); |
271 | 1.62M | for (j = 0; j < NEIGHBORHOOD_LEN; j++) { |
272 | 1.29M | if (oldmd->neighborhoods[i].entries[j].value != NULL) { |
273 | 26.1k | v = oldmd->neighborhoods[i].entries[j].value; |
274 | 26.1k | v->ht->config.ht_free_fn((HT_VALUE *)v); |
275 | 26.1k | free_value(v); |
276 | 26.1k | } |
277 | 1.29M | } |
278 | 324k | } |
279 | | |
280 | 31.0k | OPENSSL_free(oldmd->neighborhood_ptr_to_free); |
281 | 31.0k | OPENSSL_free(oldmd); |
282 | 31.0k | } |
283 | | |
284 | | static int ossl_ht_flush_internal(HT *h) |
285 | 157 | { |
286 | 157 | struct ht_mutable_data_st *newmd = NULL; |
287 | 157 | struct ht_mutable_data_st *oldmd = NULL; |
288 | | |
289 | 157 | newmd = OPENSSL_zalloc(sizeof(*newmd)); |
290 | 157 | if (newmd == NULL) |
291 | 0 | return 0; |
292 | | |
293 | 157 | newmd->neighborhoods = alloc_new_neighborhood_list(DEFAULT_NEIGH_LEN, |
294 | 157 | &newmd->neighborhood_ptr_to_free); |
295 | 157 | if (newmd->neighborhoods == NULL) { |
296 | 0 | OPENSSL_free(newmd); |
297 | 0 | return 0; |
298 | 0 | } |
299 | | |
300 | 157 | newmd->neighborhood_mask = DEFAULT_NEIGH_LEN - 1; |
301 | | |
302 | | /* Swap the old and new mutable data sets */ |
303 | 157 | oldmd = ossl_rcu_deref(&h->md); |
304 | 157 | ossl_rcu_assign_ptr(&h->md, &newmd); |
305 | | |
306 | | /* Set the number of entries to 0 */ |
307 | 157 | h->wpd.value_count = 0; |
308 | 157 | h->wpd.neighborhood_len = DEFAULT_NEIGH_LEN; |
309 | | |
310 | 157 | ossl_rcu_call(h->lock, free_oldmd, oldmd); |
311 | 157 | h->wpd.need_sync = 1; |
312 | 157 | return 1; |
313 | 157 | } |
314 | | |
315 | | int ossl_ht_flush(HT *h) |
316 | 420 | { |
317 | 420 | return ossl_ht_flush_internal(h); |
318 | 420 | } |
319 | | |
320 | | void ossl_ht_free(HT *h) |
321 | 154 | { |
322 | 154 | if (h == NULL) |
323 | 0 | return; |
324 | | |
325 | 154 | ossl_ht_write_lock(h); |
326 | 154 | ossl_ht_flush_internal(h); |
327 | 154 | ossl_ht_write_unlock(h); |
328 | | /* Freeing the lock does a final sync for us */ |
329 | 154 | CRYPTO_THREAD_lock_free(h->atomic_lock); |
330 | 154 | ossl_rcu_lock_free(h->lock); |
331 | 154 | OPENSSL_free(h->md->neighborhood_ptr_to_free); |
332 | 154 | OPENSSL_free(h->md); |
333 | 154 | OPENSSL_free(h); |
334 | 154 | return; |
335 | 154 | } |
336 | | |
337 | | size_t ossl_ht_count(HT *h) |
338 | 1.60k | { |
339 | 1.60k | size_t count; |
340 | | |
341 | 1.60k | count = h->wpd.value_count; |
342 | 1.60k | return count; |
343 | 1.60k | } |
344 | | |
345 | | void ossl_ht_foreach_until(HT *h, int (*cb)(HT_VALUE *obj, void *arg), |
346 | | void *arg) |
347 | 95 | { |
348 | 95 | size_t i, j; |
349 | 95 | struct ht_mutable_data_st *md; |
350 | | |
351 | 95 | md = ossl_rcu_deref(&h->md); |
352 | 85.2k | for (i = 0; i < md->neighborhood_mask + 1; i++) { |
353 | 85.1k | PREFETCH_NEIGHBORHOOD(md->neighborhoods[i + 1]); |
354 | 425k | for (j = 0; j < NEIGHBORHOOD_LEN; j++) { |
355 | 340k | if (md->neighborhoods[i].entries[j].value != NULL) { |
356 | 609 | if (!cb((HT_VALUE *)md->neighborhoods[i].entries[j].value, arg)) |
357 | 7 | goto out; |
358 | 609 | } |
359 | 340k | } |
360 | 85.1k | } |
361 | 95 | out: |
362 | 95 | return; |
363 | 95 | } |
364 | | |
365 | | HT_VALUE_LIST *ossl_ht_filter(HT *h, size_t max_len, |
366 | | int (*filter)(HT_VALUE *obj, void *arg), |
367 | | void *arg) |
368 | 72 | { |
369 | 72 | struct ht_mutable_data_st *md; |
370 | 72 | HT_VALUE_LIST *list = OPENSSL_zalloc(sizeof(HT_VALUE_LIST) |
371 | 72 | + (sizeof(HT_VALUE *) * max_len)); |
372 | 72 | size_t i, j; |
373 | 72 | struct ht_internal_value_st *v; |
374 | | |
375 | 72 | if (list == NULL) |
376 | 0 | return NULL; |
377 | | |
378 | | /* |
379 | | * The list array lives just beyond the end of |
380 | | * the struct |
381 | | */ |
382 | 72 | list->list = (HT_VALUE **)(list + 1); |
383 | | |
384 | 72 | md = ossl_rcu_deref(&h->md); |
385 | 61.6k | for (i = 0; i < md->neighborhood_mask + 1; i++) { |
386 | 61.6k | PREFETCH_NEIGHBORHOOD(md->neighborhoods[i + 1]); |
387 | 308k | for (j = 0; j < NEIGHBORHOOD_LEN; j++) { |
388 | 246k | v = md->neighborhoods[i].entries[j].value; |
389 | 246k | if (v != NULL && filter((HT_VALUE *)v, arg)) { |
390 | 5 | list->list[list->list_len++] = (HT_VALUE *)v; |
391 | 5 | if (list->list_len == max_len) |
392 | 5 | goto out; |
393 | 5 | } |
394 | 246k | } |
395 | 61.6k | } |
396 | 72 | out: |
397 | 72 | return list; |
398 | 72 | } |
399 | | |
400 | | void ossl_ht_value_list_free(HT_VALUE_LIST *list) |
401 | 72 | { |
402 | 72 | OPENSSL_free(list); |
403 | 72 | } |
404 | | |
405 | | static int compare_hash(uint64_t hash1, uint64_t hash2) |
406 | 48.2M | { |
407 | 48.2M | return (hash1 == hash2); |
408 | 48.2M | } |
409 | | |
410 | | static void free_old_neigh_table(void *arg) |
411 | 105 | { |
412 | 105 | struct ht_mutable_data_st *oldmd = arg; |
413 | | |
414 | 105 | OPENSSL_free(oldmd->neighborhood_ptr_to_free); |
415 | 105 | OPENSSL_free(oldmd); |
416 | 105 | } |
417 | | |
418 | | /* |
419 | | * Increase hash table bucket list |
420 | | * must be called with write_lock held |
421 | | */ |
422 | | static int grow_hashtable(HT *h, size_t oldsize) |
423 | 24 | { |
424 | 24 | struct ht_mutable_data_st *newmd; |
425 | 24 | struct ht_mutable_data_st *oldmd = ossl_rcu_deref(&h->md); |
426 | 24 | int rc = 0; |
427 | 24 | uint64_t oldi, oldj, newi, newj; |
428 | 24 | uint64_t oldhash; |
429 | 24 | struct ht_internal_value_st *oldv; |
430 | 24 | int rehashed; |
431 | 24 | size_t newsize = oldsize * 2; |
432 | | |
433 | 24 | if (h->config.lockless_reads) |
434 | 0 | goto out; |
435 | | |
436 | 24 | if ((newmd = OPENSSL_zalloc(sizeof(*newmd))) == NULL) |
437 | 0 | goto out; |
438 | | |
439 | | /* bucket list is always a power of 2 */ |
440 | 24 | newmd->neighborhoods = alloc_new_neighborhood_list(oldsize * 2, |
441 | 24 | &newmd->neighborhood_ptr_to_free); |
442 | 24 | if (newmd->neighborhoods == NULL) |
443 | 0 | goto out_free; |
444 | | |
445 | | /* being a power of 2 makes for easy mask computation */ |
446 | 24 | newmd->neighborhood_mask = (newsize - 1); |
447 | | |
448 | | /* |
449 | | * Now we need to start rehashing entries |
450 | | * Note we don't need to use atomics here as the new |
451 | | * mutable data hasn't been published |
452 | | */ |
453 | 2.16k | for (oldi = 0; oldi < h->wpd.neighborhood_len; oldi++) { |
454 | 2.14k | PREFETCH_NEIGHBORHOOD(oldmd->neighborhoods[oldi + 1]); |
455 | 10.7k | for (oldj = 0; oldj < NEIGHBORHOOD_LEN; oldj++) { |
456 | 8.57k | oldv = oldmd->neighborhoods[oldi].entries[oldj].value; |
457 | 8.57k | if (oldv == NULL) |
458 | 8.43k | continue; |
459 | 143 | oldhash = oldmd->neighborhoods[oldi].entries[oldj].hash; |
460 | 143 | newi = oldhash & newmd->neighborhood_mask; |
461 | 143 | rehashed = 0; |
462 | 247 | for (newj = 0; newj < NEIGHBORHOOD_LEN; newj++) { |
463 | 247 | if (newmd->neighborhoods[newi].entries[newj].value == NULL) { |
464 | 143 | newmd->neighborhoods[newi].entries[newj].value = oldv; |
465 | 143 | newmd->neighborhoods[newi].entries[newj].hash = oldhash; |
466 | 143 | rehashed = 1; |
467 | 143 | break; |
468 | 143 | } |
469 | 247 | } |
470 | 143 | if (rehashed == 0) { |
471 | | /* we ran out of space in a neighborhood, grow again */ |
472 | 0 | OPENSSL_free(newmd->neighborhoods); |
473 | 0 | OPENSSL_free(newmd); |
474 | 0 | return grow_hashtable(h, newsize); |
475 | 0 | } |
476 | 143 | } |
477 | 2.14k | } |
478 | | /* |
479 | | * Now that our entries are all hashed into the new bucket list |
480 | | * update our bucket_len and target_max_load |
481 | | */ |
482 | 24 | h->wpd.neighborhood_len = newsize; |
483 | | |
484 | | /* |
485 | | * Now we replace the old mutable data with the new |
486 | | */ |
487 | 24 | ossl_rcu_assign_ptr(&h->md, &newmd); |
488 | 24 | ossl_rcu_call(h->lock, free_old_neigh_table, oldmd); |
489 | 24 | h->wpd.need_sync = 1; |
490 | | /* |
491 | | * And we're done |
492 | | */ |
493 | 24 | rc = 1; |
494 | | |
495 | 24 | out: |
496 | 24 | return rc; |
497 | 0 | out_free: |
498 | 0 | OPENSSL_free(newmd->neighborhoods); |
499 | 0 | OPENSSL_free(newmd); |
500 | 0 | goto out; |
501 | 24 | } |
502 | | |
503 | | static void free_old_ht_value(void *arg) |
504 | 5 | { |
505 | 5 | HT_VALUE *h = (HT_VALUE *)arg; |
506 | | |
507 | | /* |
508 | | * Note, this is only called on replacement, |
509 | | * the caller is responsible for freeing the |
510 | | * held data, we just need to free the wrapping |
511 | | * struct here |
512 | | */ |
513 | 5 | OPENSSL_free(h); |
514 | 5 | } |
515 | | |
516 | | static ossl_inline int match_key(HT_KEY *a, HT_KEY *b) |
517 | 35.0M | { |
518 | | /* |
519 | | * keys match if they are both present, the same size |
520 | | * and compare equal in memory |
521 | | */ |
522 | 35.0M | PREFETCH(a->keybuf); |
523 | 35.0M | PREFETCH(b->keybuf); |
524 | 35.0M | if (a->keybuf != NULL && b->keybuf != NULL && a->keysize == b->keysize) |
525 | 35.0M | return !memcmp(a->keybuf, b->keybuf, a->keysize); |
526 | | |
527 | 3.67k | return 1; |
528 | 35.0M | } |
529 | | |
530 | | static int ossl_ht_insert_locked(HT *h, uint64_t hash, |
531 | | struct ht_internal_value_st *newval, |
532 | | HT_VALUE **olddata) |
533 | 28.4k | { |
534 | 28.4k | struct ht_mutable_data_st *md = h->md; |
535 | 28.4k | uint64_t neigh_idx_start = hash & md->neighborhood_mask; |
536 | 28.4k | uint64_t neigh_idx = neigh_idx_start; |
537 | 28.4k | size_t j; |
538 | 28.4k | uint64_t ihash; |
539 | 28.4k | HT_VALUE *ival; |
540 | 28.4k | size_t empty_idx = SIZE_MAX; |
541 | 28.4k | int lockless_reads = h->config.lockless_reads; |
542 | | |
543 | 28.7k | do { |
544 | 28.7k | PREFETCH_NEIGHBORHOOD(md->neighborhoods[neigh_idx]); |
545 | | |
546 | 43.4k | for (j = 0; j < NEIGHBORHOOD_LEN; j++) { |
547 | 43.1k | ival = ossl_rcu_deref(&md->neighborhoods[neigh_idx].entries[j].value); |
548 | 43.1k | if (ival == NULL) { |
549 | 28.5k | empty_idx = j; |
550 | | /* lockless_reads implies no deletion, we can break out */ |
551 | 28.5k | if (lockless_reads) |
552 | 28.3k | goto not_found; |
553 | 165 | continue; |
554 | 28.5k | } |
555 | 14.6k | if (!CRYPTO_atomic_load(&md->neighborhoods[neigh_idx].entries[j].hash, |
556 | 14.6k | &ihash, h->atomic_lock)) |
557 | 0 | return 0; |
558 | 14.6k | if (compare_hash(hash, ihash) && match_key(&newval->value.key, &ival->key)) { |
559 | 12 | if (olddata == NULL) { |
560 | | /* This would insert a duplicate -> fail */ |
561 | 8 | return 0; |
562 | 8 | } |
563 | | /* Do a replacement */ |
564 | 4 | if (!CRYPTO_atomic_store(&md->neighborhoods[neigh_idx].entries[j].hash, |
565 | 4 | hash, h->atomic_lock)) |
566 | 0 | return 0; |
567 | 4 | *olddata = (HT_VALUE *)md->neighborhoods[neigh_idx].entries[j].value; |
568 | 4 | ossl_rcu_assign_ptr(&md->neighborhoods[neigh_idx].entries[j].value, |
569 | 4 | &newval); |
570 | 4 | ossl_rcu_call(h->lock, free_old_ht_value, *olddata); |
571 | 4 | h->wpd.need_sync = 1; |
572 | 4 | return 1; |
573 | 4 | } |
574 | 14.6k | } |
575 | 353 | if (!lockless_reads) |
576 | 86 | break; |
577 | | /* Continue search in subsequent neighborhoods */ |
578 | 267 | neigh_idx = (neigh_idx + 1) & md->neighborhood_mask; |
579 | 267 | } while (neigh_idx != neigh_idx_start); |
580 | | |
581 | 28.4k | not_found: |
582 | | /* If we get to here, its just an insert */ |
583 | 28.4k | if (empty_idx == SIZE_MAX) |
584 | 24 | return -1; /* out of space */ |
585 | 28.4k | if (!CRYPTO_atomic_store(&md->neighborhoods[neigh_idx].entries[empty_idx].hash, |
586 | 28.4k | hash, h->atomic_lock)) |
587 | 0 | return 0; |
588 | 28.4k | h->wpd.value_count++; |
589 | 28.4k | ossl_rcu_assign_ptr(&md->neighborhoods[neigh_idx].entries[empty_idx].value, |
590 | 28.4k | &newval); |
591 | 28.4k | return 1; |
592 | 28.4k | } |
593 | | |
594 | | static struct ht_internal_value_st *alloc_new_value(HT *h, HT_KEY *key, |
595 | | void *data, |
596 | | uintptr_t *type) |
597 | 43.7k | { |
598 | 43.7k | struct ht_internal_value_st *tmp; |
599 | 43.7k | size_t nvsize = sizeof(*tmp); |
600 | | |
601 | 43.7k | if (h->config.collision_check == 1) |
602 | 42.4k | nvsize += key->keysize; |
603 | | |
604 | 43.7k | tmp = OPENSSL_malloc(nvsize); |
605 | | |
606 | 43.7k | if (tmp == NULL) |
607 | 0 | return NULL; |
608 | | |
609 | 43.7k | tmp->ht = h; |
610 | 43.7k | tmp->value.value = data; |
611 | 43.7k | tmp->value.type_id = type; |
612 | 43.7k | tmp->value.key.keybuf = NULL; |
613 | 43.7k | if (h->config.collision_check) { |
614 | 42.4k | tmp->value.key.keybuf = (uint8_t *)(tmp + 1); |
615 | 42.4k | tmp->value.key.keysize = key->keysize; |
616 | 42.4k | memcpy(tmp->value.key.keybuf, key->keybuf, key->keysize); |
617 | 42.4k | } |
618 | | |
619 | 43.7k | return tmp; |
620 | 43.7k | } |
621 | | |
622 | | static void free_value(struct ht_internal_value_st *v) |
623 | 27.6k | { |
624 | 27.6k | OPENSSL_free(v); |
625 | 27.6k | } |
626 | | |
627 | | int ossl_ht_insert(HT *h, HT_KEY *key, HT_VALUE *data, HT_VALUE **olddata) |
628 | 28.4k | { |
629 | 28.4k | struct ht_internal_value_st *newval = NULL; |
630 | 28.4k | uint64_t hash; |
631 | 28.4k | int rc = 0; |
632 | 28.4k | int i; |
633 | | |
634 | 28.4k | if (data->value == NULL) |
635 | 0 | goto out; |
636 | | |
637 | 28.4k | newval = alloc_new_value(h, key, data->value, data->type_id); |
638 | 28.4k | if (newval == NULL) |
639 | 0 | goto out; |
640 | | |
641 | | /* |
642 | | * we have to take our lock here to prevent other changes |
643 | | * to the bucket list |
644 | | */ |
645 | 28.4k | hash = h->config.ht_hash_fn(key->keybuf, key->keysize); |
646 | | |
647 | 28.4k | for (i = 0; |
648 | 28.4k | (rc = ossl_ht_insert_locked(h, hash, newval, olddata)) == -1 |
649 | 24 | && i < 4; |
650 | 28.4k | ++i) |
651 | 24 | if (!grow_hashtable(h, h->wpd.neighborhood_len)) { |
652 | 0 | rc = -1; |
653 | 0 | break; |
654 | 0 | } |
655 | | |
656 | 28.4k | if (rc <= 0) |
657 | 8 | free_value(newval); |
658 | | |
659 | 28.4k | out: |
660 | 28.4k | return rc; |
661 | 28.4k | } |
662 | | |
663 | | HT_VALUE *ossl_ht_get(HT *h, HT_KEY *key) |
664 | 26.0M | { |
665 | 26.0M | struct ht_mutable_data_st *md; |
666 | 26.0M | uint64_t hash; |
667 | 26.0M | uint64_t neigh_idx_start; |
668 | 26.0M | uint64_t neigh_idx; |
669 | 26.0M | struct ht_internal_value_st *ival = NULL; |
670 | 26.0M | size_t j; |
671 | 26.0M | uint64_t ehash; |
672 | 26.0M | int lockless_reads = h->config.lockless_reads; |
673 | | |
674 | 26.0M | hash = h->config.ht_hash_fn(key->keybuf, key->keysize); |
675 | | |
676 | 26.0M | md = ossl_rcu_deref(&h->md); |
677 | 26.0M | neigh_idx = neigh_idx_start = hash & md->neighborhood_mask; |
678 | 26.0M | do { |
679 | 26.0M | PREFETCH_NEIGHBORHOOD(md->neighborhoods[neigh_idx]); |
680 | 33.3M | for (j = 0; j < NEIGHBORHOOD_LEN; j++) { |
681 | 33.3M | ival = ossl_rcu_deref(&md->neighborhoods[neigh_idx].entries[j].value); |
682 | 33.3M | if (ival == NULL) { |
683 | 2.36M | if (lockless_reads) |
684 | | /* lockless_reads implies no deletion, we can break out */ |
685 | 2.36M | return NULL; |
686 | 131 | continue; |
687 | 2.36M | } |
688 | 30.9M | if (!CRYPTO_atomic_load(&md->neighborhoods[neigh_idx].entries[j].hash, |
689 | 30.9M | &ehash, h->atomic_lock)) |
690 | 0 | return NULL; |
691 | 30.9M | if (compare_hash(hash, ehash) && match_key(&ival->value.key, key)) |
692 | 23.6M | return (HT_VALUE *)ival; |
693 | 30.9M | } |
694 | 5.36k | if (!lockless_reads) |
695 | 51 | break; |
696 | | /* Continue search in subsequent neighborhoods */ |
697 | 5.31k | neigh_idx = (neigh_idx + 1) & md->neighborhood_mask; |
698 | 5.31k | } while (neigh_idx != neigh_idx_start); |
699 | | |
700 | 51 | return NULL; |
701 | 26.0M | } |
702 | | |
703 | | static void free_old_entry(void *arg) |
704 | 1.50k | { |
705 | 1.50k | struct ht_internal_value_st *v = arg; |
706 | | |
707 | 1.50k | v->ht->config.ht_free_fn((HT_VALUE *)v); |
708 | 1.50k | free_value(v); |
709 | 1.50k | } |
710 | | |
711 | | int ossl_ht_delete(HT *h, HT_KEY *key) |
712 | 54 | { |
713 | 54 | uint64_t hash; |
714 | 54 | uint64_t neigh_idx; |
715 | 54 | size_t j; |
716 | 54 | struct ht_internal_value_st *v = NULL; |
717 | 54 | HT_VALUE *nv = NULL; |
718 | 54 | int rc = 0; |
719 | | |
720 | 54 | if (h->config.lockless_reads) |
721 | 0 | return 0; |
722 | | |
723 | 54 | hash = h->config.ht_hash_fn(key->keybuf, key->keysize); |
724 | | |
725 | 54 | neigh_idx = hash & h->md->neighborhood_mask; |
726 | 54 | PREFETCH_NEIGHBORHOOD(h->md->neighborhoods[neigh_idx]); |
727 | 249 | for (j = 0; j < NEIGHBORHOOD_LEN; j++) { |
728 | 201 | v = (struct ht_internal_value_st *)h->md->neighborhoods[neigh_idx].entries[j].value; |
729 | 201 | if (v == NULL) |
730 | 113 | continue; |
731 | 88 | if (compare_hash(hash, h->md->neighborhoods[neigh_idx].entries[j].hash) |
732 | 6 | && match_key(key, &v->value.key)) { |
733 | 6 | if (!CRYPTO_atomic_store(&h->md->neighborhoods[neigh_idx].entries[j].hash, |
734 | 6 | 0, h->atomic_lock)) |
735 | 0 | break; |
736 | 6 | h->wpd.value_count--; |
737 | 6 | ossl_rcu_assign_ptr(&h->md->neighborhoods[neigh_idx].entries[j].value, |
738 | 6 | &nv); |
739 | 6 | rc = 1; |
740 | 6 | break; |
741 | 6 | } |
742 | 88 | } |
743 | 54 | if (rc == 1) { |
744 | 6 | ossl_rcu_call(h->lock, free_old_entry, v); |
745 | 6 | h->wpd.need_sync = 1; |
746 | 6 | } |
747 | 54 | return rc; |
748 | 54 | } |