/src/openssl/crypto/hashtable/hashtable.c
Line | Count | Source (jump to first uncovered line) |
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 | 133k | # define PREFETCH_NEIGHBORHOOD(x) __builtin_prefetch(x.entries) |
84 | 221k | # define PREFETCH(x) __builtin_prefetch(x) |
85 | | #else |
86 | | # define PREFETCH_NEIGHBORHOOD(x) |
87 | | # define PREFETCH(x) |
88 | | #endif |
89 | | |
90 | | /* |
91 | | * Define our neighborhood list length |
92 | | * Note: It should always be a power of 2 |
93 | | */ |
94 | 48 | #define DEFAULT_NEIGH_LEN_LOG 4 |
95 | 48 | #define DEFAULT_NEIGH_LEN (1 << DEFAULT_NEIGH_LEN_LOG) |
96 | | |
97 | | /* |
98 | | * For now assume cache line size is 64 bytes |
99 | | */ |
100 | 176k | #define CACHE_LINE_BYTES 64 |
101 | | #define CACHE_LINE_ALIGNMENT CACHE_LINE_BYTES |
102 | | |
103 | 176k | #define NEIGHBORHOOD_LEN (CACHE_LINE_BYTES / sizeof(struct ht_neighborhood_entry_st)) |
104 | | /* |
105 | | * Defines our chains of values |
106 | | */ |
107 | | struct ht_internal_value_st { |
108 | | HT_VALUE value; |
109 | | HT *ht; |
110 | | }; |
111 | | |
112 | | struct ht_neighborhood_entry_st { |
113 | | uint64_t hash; |
114 | | struct ht_internal_value_st *value; |
115 | | }; |
116 | | |
117 | | struct ht_neighborhood_st { |
118 | | struct ht_neighborhood_entry_st entries[NEIGHBORHOOD_LEN]; |
119 | | }; |
120 | | |
121 | | /* |
122 | | * Updates to data in this struct |
123 | | * require an rcu sync after modification |
124 | | * prior to free |
125 | | */ |
126 | | struct ht_mutable_data_st { |
127 | | struct ht_neighborhood_st *neighborhoods; |
128 | | void *neighborhood_ptr_to_free; |
129 | | uint64_t neighborhood_mask; |
130 | | }; |
131 | | |
132 | | /* |
133 | | * Private data may be updated on the write |
134 | | * side only, and so do not require rcu sync |
135 | | */ |
136 | | struct ht_write_private_data_st { |
137 | | size_t neighborhood_len; |
138 | | size_t value_count; |
139 | | int need_sync; |
140 | | }; |
141 | | |
142 | | struct ht_internal_st { |
143 | | HT_CONFIG config; |
144 | | CRYPTO_RCU_LOCK *lock; |
145 | | CRYPTO_RWLOCK *atomic_lock; |
146 | | struct ht_mutable_data_st *md; |
147 | | struct ht_write_private_data_st wpd; |
148 | | }; |
149 | | |
150 | | static void free_value(struct ht_internal_value_st *v); |
151 | | |
152 | | static struct ht_neighborhood_st *alloc_new_neighborhood_list(size_t len, |
153 | | void **freeptr) |
154 | 32 | { |
155 | 32 | struct ht_neighborhood_st *ret; |
156 | | |
157 | 32 | ret = OPENSSL_aligned_alloc_array(len, sizeof(struct ht_neighborhood_st), |
158 | 32 | CACHE_LINE_BYTES, freeptr); |
159 | | |
160 | | /* fall back to regular malloc */ |
161 | 32 | if (ret == NULL) { |
162 | 0 | ret = *freeptr = |
163 | 0 | OPENSSL_malloc_array(len, sizeof(struct ht_neighborhood_st)); |
164 | 0 | if (ret == NULL) |
165 | 0 | return NULL; |
166 | 0 | } |
167 | 32 | memset(ret, 0, sizeof(struct ht_neighborhood_st) * len); |
168 | 32 | return ret; |
169 | 32 | } |
170 | | |
171 | | static void internal_free_nop(HT_VALUE *v) |
172 | 6.30k | { |
173 | 6.30k | return; |
174 | 6.30k | } |
175 | | |
176 | | HT *ossl_ht_new(const HT_CONFIG *conf) |
177 | 16 | { |
178 | 16 | HT *new = OPENSSL_zalloc(sizeof(*new)); |
179 | | |
180 | 16 | if (new == NULL) |
181 | 0 | return NULL; |
182 | | |
183 | 16 | new->atomic_lock = CRYPTO_THREAD_lock_new(); |
184 | 16 | if (new->atomic_lock == NULL) |
185 | 0 | goto err; |
186 | | |
187 | 16 | memcpy(&new->config, conf, sizeof(*conf)); |
188 | | |
189 | 16 | if (new->config.init_neighborhoods != 0) { |
190 | 16 | new->wpd.neighborhood_len = new->config.init_neighborhoods; |
191 | | /* round up to the next power of 2 */ |
192 | 16 | new->wpd.neighborhood_len--; |
193 | 16 | new->wpd.neighborhood_len |= new->wpd.neighborhood_len >> 1; |
194 | 16 | new->wpd.neighborhood_len |= new->wpd.neighborhood_len >> 2; |
195 | 16 | new->wpd.neighborhood_len |= new->wpd.neighborhood_len >> 4; |
196 | 16 | new->wpd.neighborhood_len |= new->wpd.neighborhood_len >> 8; |
197 | 16 | new->wpd.neighborhood_len |= new->wpd.neighborhood_len >> 16; |
198 | 16 | new->wpd.neighborhood_len++; |
199 | 16 | } else { |
200 | 0 | new->wpd.neighborhood_len = DEFAULT_NEIGH_LEN; |
201 | 0 | } |
202 | | |
203 | 16 | if (new->config.ht_free_fn == NULL) |
204 | 16 | new->config.ht_free_fn = internal_free_nop; |
205 | | |
206 | 16 | new->md = OPENSSL_zalloc(sizeof(*new->md)); |
207 | 16 | if (new->md == NULL) |
208 | 0 | goto err; |
209 | | |
210 | 16 | new->md->neighborhoods = |
211 | 16 | alloc_new_neighborhood_list(new->wpd.neighborhood_len, |
212 | 16 | &new->md->neighborhood_ptr_to_free); |
213 | 16 | if (new->md->neighborhoods == NULL) |
214 | 0 | goto err; |
215 | 16 | new->md->neighborhood_mask = new->wpd.neighborhood_len - 1; |
216 | | |
217 | 16 | new->lock = ossl_rcu_lock_new(1, conf->ctx); |
218 | 16 | if (new->lock == NULL) |
219 | 0 | goto err; |
220 | | |
221 | 16 | if (new->config.ht_hash_fn == NULL) |
222 | 16 | new->config.ht_hash_fn = ossl_fnv1a_hash; |
223 | | |
224 | 16 | 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 | 16 | } |
235 | | |
236 | | int ossl_ht_read_lock(HT *htable) |
237 | 0 | { |
238 | 0 | return ossl_rcu_read_lock(htable->lock); |
239 | 0 | } |
240 | | |
241 | | void ossl_ht_read_unlock(HT *htable) |
242 | 0 | { |
243 | 0 | ossl_rcu_read_unlock(htable->lock); |
244 | 0 | } |
245 | | |
246 | | void ossl_ht_write_lock(HT *htable) |
247 | 16 | { |
248 | 16 | ossl_rcu_write_lock(htable->lock); |
249 | 16 | htable->wpd.need_sync = 0; |
250 | 16 | } |
251 | | |
252 | | void ossl_ht_write_unlock(HT *htable) |
253 | 16 | { |
254 | 16 | int need_sync = htable->wpd.need_sync; |
255 | | |
256 | 16 | htable->wpd.need_sync = 0; |
257 | 16 | ossl_rcu_write_unlock(htable->lock); |
258 | 16 | if (need_sync) |
259 | 16 | ossl_synchronize_rcu(htable->lock); |
260 | 16 | } |
261 | | |
262 | | static void free_oldmd(void *arg) |
263 | 16 | { |
264 | 16 | struct ht_mutable_data_st *oldmd = arg; |
265 | 16 | size_t i, j; |
266 | 16 | size_t neighborhood_len = (size_t)oldmd->neighborhood_mask + 1; |
267 | 16 | struct ht_internal_value_st *v; |
268 | | |
269 | 8.20k | for (i = 0; i < neighborhood_len; i++) { |
270 | 8.19k | PREFETCH_NEIGHBORHOOD(oldmd->neighborhoods[i + 1]); |
271 | 40.9k | for (j = 0; j < NEIGHBORHOOD_LEN; j++) { |
272 | 32.7k | if (oldmd->neighborhoods[i].entries[j].value != NULL) { |
273 | 6.30k | v = oldmd->neighborhoods[i].entries[j].value; |
274 | 6.30k | v->ht->config.ht_free_fn((HT_VALUE *)v); |
275 | 6.30k | free_value(v); |
276 | 6.30k | } |
277 | 32.7k | } |
278 | 8.19k | } |
279 | | |
280 | 16 | OPENSSL_free(oldmd->neighborhood_ptr_to_free); |
281 | 16 | OPENSSL_free(oldmd); |
282 | 16 | } |
283 | | |
284 | | static int ossl_ht_flush_internal(HT *h) |
285 | 16 | { |
286 | 16 | struct ht_mutable_data_st *newmd = NULL; |
287 | 16 | struct ht_mutable_data_st *oldmd = NULL; |
288 | | |
289 | 16 | newmd = OPENSSL_zalloc(sizeof(*newmd)); |
290 | 16 | if (newmd == NULL) |
291 | 0 | return 0; |
292 | | |
293 | 16 | newmd->neighborhoods = alloc_new_neighborhood_list(DEFAULT_NEIGH_LEN, |
294 | 16 | &newmd->neighborhood_ptr_to_free); |
295 | 16 | if (newmd->neighborhoods == NULL) { |
296 | 0 | OPENSSL_free(newmd); |
297 | 0 | return 0; |
298 | 0 | } |
299 | | |
300 | 16 | newmd->neighborhood_mask = DEFAULT_NEIGH_LEN - 1; |
301 | | |
302 | | /* Swap the old and new mutable data sets */ |
303 | 16 | oldmd = ossl_rcu_deref(&h->md); |
304 | 16 | ossl_rcu_assign_ptr(&h->md, &newmd); |
305 | | |
306 | | /* Set the number of entries to 0 */ |
307 | 16 | h->wpd.value_count = 0; |
308 | 16 | h->wpd.neighborhood_len = DEFAULT_NEIGH_LEN; |
309 | | |
310 | 16 | ossl_rcu_call(h->lock, free_oldmd, oldmd); |
311 | 16 | h->wpd.need_sync = 1; |
312 | 16 | return 1; |
313 | 16 | } |
314 | | |
315 | | int ossl_ht_flush(HT *h) |
316 | 0 | { |
317 | 0 | return ossl_ht_flush_internal(h); |
318 | 0 | } |
319 | | |
320 | | void ossl_ht_free(HT *h) |
321 | 16 | { |
322 | 16 | if (h == NULL) |
323 | 0 | return; |
324 | | |
325 | 16 | ossl_ht_write_lock(h); |
326 | 16 | ossl_ht_flush_internal(h); |
327 | 16 | ossl_ht_write_unlock(h); |
328 | | /* Freeing the lock does a final sync for us */ |
329 | 16 | CRYPTO_THREAD_lock_free(h->atomic_lock); |
330 | 16 | ossl_rcu_lock_free(h->lock); |
331 | 16 | OPENSSL_free(h->md->neighborhood_ptr_to_free); |
332 | 16 | OPENSSL_free(h->md); |
333 | 16 | OPENSSL_free(h); |
334 | 16 | return; |
335 | 16 | } |
336 | | |
337 | | size_t ossl_ht_count(HT *h) |
338 | 0 | { |
339 | 0 | size_t count; |
340 | |
|
341 | 0 | count = h->wpd.value_count; |
342 | 0 | return count; |
343 | 0 | } |
344 | | |
345 | | void ossl_ht_foreach_until(HT *h, int (*cb)(HT_VALUE *obj, void *arg), |
346 | | void *arg) |
347 | 0 | { |
348 | 0 | size_t i, j; |
349 | 0 | struct ht_mutable_data_st *md; |
350 | |
|
351 | 0 | md = ossl_rcu_deref(&h->md); |
352 | 0 | for (i = 0; i < md->neighborhood_mask + 1; i++) { |
353 | 0 | PREFETCH_NEIGHBORHOOD(md->neighborhoods[i + 1]); |
354 | 0 | for (j = 0; j < NEIGHBORHOOD_LEN; j++) { |
355 | 0 | if (md->neighborhoods[i].entries[j].value != NULL) { |
356 | 0 | if (!cb((HT_VALUE *)md->neighborhoods[i].entries[j].value, arg)) |
357 | 0 | goto out; |
358 | 0 | } |
359 | 0 | } |
360 | 0 | } |
361 | 0 | out: |
362 | 0 | return; |
363 | 0 | } |
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 | 0 | { |
369 | 0 | struct ht_mutable_data_st *md; |
370 | 0 | HT_VALUE_LIST *list = OPENSSL_zalloc(sizeof(HT_VALUE_LIST) |
371 | 0 | + (sizeof(HT_VALUE *) * max_len)); |
372 | 0 | size_t i, j; |
373 | 0 | struct ht_internal_value_st *v; |
374 | |
|
375 | 0 | if (list == NULL) |
376 | 0 | return NULL; |
377 | | |
378 | | /* |
379 | | * The list array lives just beyond the end of |
380 | | * the struct |
381 | | */ |
382 | 0 | list->list = (HT_VALUE **)(list + 1); |
383 | |
|
384 | 0 | md = ossl_rcu_deref(&h->md); |
385 | 0 | for (i = 0; i < md->neighborhood_mask + 1; i++) { |
386 | 0 | PREFETCH_NEIGHBORHOOD(md->neighborhoods[i+1]); |
387 | 0 | for (j = 0; j < NEIGHBORHOOD_LEN; j++) { |
388 | 0 | v = md->neighborhoods[i].entries[j].value; |
389 | 0 | if (v != NULL && filter((HT_VALUE *)v, arg)) { |
390 | 0 | list->list[list->list_len++] = (HT_VALUE *)v; |
391 | 0 | if (list->list_len == max_len) |
392 | 0 | goto out; |
393 | 0 | } |
394 | 0 | } |
395 | 0 | } |
396 | 0 | out: |
397 | 0 | return list; |
398 | 0 | } |
399 | | |
400 | | void ossl_ht_value_list_free(HT_VALUE_LIST *list) |
401 | 0 | { |
402 | 0 | OPENSSL_free(list); |
403 | 0 | } |
404 | | |
405 | | static int compare_hash(uint64_t hash1, uint64_t hash2) |
406 | 120k | { |
407 | 120k | return (hash1 == hash2); |
408 | 120k | } |
409 | | |
410 | | static void free_old_neigh_table(void *arg) |
411 | 0 | { |
412 | 0 | struct ht_mutable_data_st *oldmd = arg; |
413 | |
|
414 | 0 | OPENSSL_free(oldmd->neighborhood_ptr_to_free); |
415 | 0 | OPENSSL_free(oldmd); |
416 | 0 | } |
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 | 0 | { |
424 | 0 | struct ht_mutable_data_st *newmd; |
425 | 0 | struct ht_mutable_data_st *oldmd = ossl_rcu_deref(&h->md); |
426 | 0 | int rc = 0; |
427 | 0 | uint64_t oldi, oldj, newi, newj; |
428 | 0 | uint64_t oldhash; |
429 | 0 | struct ht_internal_value_st *oldv; |
430 | 0 | int rehashed; |
431 | 0 | size_t newsize = oldsize * 2; |
432 | |
|
433 | 0 | if (h->config.lockless_reads) |
434 | 0 | goto out; |
435 | | |
436 | 0 | if ((newmd = OPENSSL_zalloc(sizeof(*newmd))) == NULL) |
437 | 0 | goto out; |
438 | | |
439 | | /* bucket list is always a power of 2 */ |
440 | 0 | newmd->neighborhoods = alloc_new_neighborhood_list(oldsize * 2, |
441 | 0 | &newmd->neighborhood_ptr_to_free); |
442 | 0 | if (newmd->neighborhoods == NULL) |
443 | 0 | goto out_free; |
444 | | |
445 | | /* being a power of 2 makes for easy mask computation */ |
446 | 0 | 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 | 0 | for (oldi = 0; oldi < h->wpd.neighborhood_len; oldi++) { |
454 | 0 | PREFETCH_NEIGHBORHOOD(oldmd->neighborhoods[oldi + 1]); |
455 | 0 | for (oldj = 0; oldj < NEIGHBORHOOD_LEN; oldj++) { |
456 | 0 | oldv = oldmd->neighborhoods[oldi].entries[oldj].value; |
457 | 0 | if (oldv == NULL) |
458 | 0 | continue; |
459 | 0 | oldhash = oldmd->neighborhoods[oldi].entries[oldj].hash; |
460 | 0 | newi = oldhash & newmd->neighborhood_mask; |
461 | 0 | rehashed = 0; |
462 | 0 | for (newj = 0; newj < NEIGHBORHOOD_LEN; newj++) { |
463 | 0 | if (newmd->neighborhoods[newi].entries[newj].value == NULL) { |
464 | 0 | newmd->neighborhoods[newi].entries[newj].value = oldv; |
465 | 0 | newmd->neighborhoods[newi].entries[newj].hash = oldhash; |
466 | 0 | rehashed = 1; |
467 | 0 | break; |
468 | 0 | } |
469 | 0 | } |
470 | 0 | 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 | 0 | } |
477 | 0 | } |
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 | 0 | h->wpd.neighborhood_len = newsize; |
483 | | |
484 | | /* |
485 | | * Now we replace the old mutable data with the new |
486 | | */ |
487 | 0 | ossl_rcu_assign_ptr(&h->md, &newmd); |
488 | 0 | ossl_rcu_call(h->lock, free_old_neigh_table, oldmd); |
489 | 0 | h->wpd.need_sync = 1; |
490 | | /* |
491 | | * And we're done |
492 | | */ |
493 | 0 | rc = 1; |
494 | |
|
495 | 0 | out: |
496 | 0 | return rc; |
497 | 0 | out_free: |
498 | 0 | OPENSSL_free(newmd->neighborhoods); |
499 | 0 | OPENSSL_free(newmd); |
500 | 0 | goto out; |
501 | 0 | } |
502 | | |
503 | | static void free_old_ht_value(void *arg) |
504 | 0 | { |
505 | 0 | 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 | 0 | OPENSSL_free(h); |
514 | 0 | } |
515 | | |
516 | | static ossl_inline int match_key(HT_KEY *a, HT_KEY *b) |
517 | 110k | { |
518 | | /* |
519 | | * keys match if they are both present, the same size |
520 | | * and compare equal in memory |
521 | | */ |
522 | 110k | PREFETCH(a->keybuf); |
523 | 110k | PREFETCH(b->keybuf); |
524 | 110k | if (a->keybuf != NULL && b->keybuf != NULL && a->keysize == b->keysize) |
525 | 110k | return !memcmp(a->keybuf, b->keybuf, a->keysize); |
526 | | |
527 | 0 | return 1; |
528 | 110k | } |
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 | 6.30k | { |
534 | 6.30k | struct ht_mutable_data_st *md = h->md; |
535 | 6.30k | uint64_t neigh_idx_start = hash & md->neighborhood_mask; |
536 | 6.30k | uint64_t neigh_idx = neigh_idx_start; |
537 | 6.30k | size_t j; |
538 | 6.30k | uint64_t ihash; |
539 | 6.30k | HT_VALUE *ival; |
540 | 6.30k | size_t empty_idx = SIZE_MAX; |
541 | 6.30k | int lockless_reads = h->config.lockless_reads; |
542 | | |
543 | 6.32k | do { |
544 | 6.32k | PREFETCH_NEIGHBORHOOD(md->neighborhoods[neigh_idx]); |
545 | | |
546 | 8.70k | for (j = 0; j < NEIGHBORHOOD_LEN; j++) { |
547 | 8.68k | ival = ossl_rcu_deref(&md->neighborhoods[neigh_idx].entries[j].value); |
548 | 8.68k | if (ival == NULL) { |
549 | 6.30k | empty_idx = j; |
550 | | /* lockless_reads implies no deletion, we can break out */ |
551 | 6.30k | if (lockless_reads) |
552 | 6.30k | goto not_found; |
553 | 0 | continue; |
554 | 6.30k | } |
555 | 2.38k | if (!CRYPTO_atomic_load(&md->neighborhoods[neigh_idx].entries[j].hash, |
556 | 2.38k | &ihash, h->atomic_lock)) |
557 | 0 | return 0; |
558 | 2.38k | if (compare_hash(hash, ihash) && match_key(&newval->value.key, |
559 | 0 | &ival->key)) { |
560 | 0 | if (olddata == NULL) { |
561 | | /* This would insert a duplicate -> fail */ |
562 | 0 | return 0; |
563 | 0 | } |
564 | | /* Do a replacement */ |
565 | 0 | if (!CRYPTO_atomic_store(&md->neighborhoods[neigh_idx].entries[j].hash, |
566 | 0 | hash, h->atomic_lock)) |
567 | 0 | return 0; |
568 | 0 | *olddata = (HT_VALUE *)md->neighborhoods[neigh_idx].entries[j].value; |
569 | 0 | ossl_rcu_assign_ptr(&md->neighborhoods[neigh_idx].entries[j].value, |
570 | 0 | &newval); |
571 | 0 | ossl_rcu_call(h->lock, free_old_ht_value, *olddata); |
572 | 0 | h->wpd.need_sync = 1; |
573 | 0 | return 1; |
574 | 0 | } |
575 | 2.38k | } |
576 | 16 | if (!lockless_reads) |
577 | 0 | break; |
578 | | /* Continue search in subsequent neighborhoods */ |
579 | 16 | neigh_idx = (neigh_idx + 1) & md->neighborhood_mask; |
580 | 16 | } while (neigh_idx != neigh_idx_start); |
581 | | |
582 | 6.30k | not_found: |
583 | | /* If we get to here, its just an insert */ |
584 | 6.30k | if (empty_idx == SIZE_MAX) |
585 | 0 | return -1; /* out of space */ |
586 | 6.30k | if (!CRYPTO_atomic_store(&md->neighborhoods[neigh_idx].entries[empty_idx].hash, |
587 | 6.30k | hash, h->atomic_lock)) |
588 | 0 | return 0; |
589 | 6.30k | h->wpd.value_count++; |
590 | 6.30k | ossl_rcu_assign_ptr(&md->neighborhoods[neigh_idx].entries[empty_idx].value, |
591 | 6.30k | &newval); |
592 | 6.30k | return 1; |
593 | 6.30k | } |
594 | | |
595 | | static struct ht_internal_value_st *alloc_new_value(HT *h, HT_KEY *key, |
596 | | void *data, |
597 | | uintptr_t *type) |
598 | 6.30k | { |
599 | 6.30k | struct ht_internal_value_st *tmp; |
600 | 6.30k | size_t nvsize = sizeof(*tmp); |
601 | | |
602 | 6.30k | if (h->config.collision_check == 1) |
603 | 6.30k | nvsize += key->keysize; |
604 | | |
605 | 6.30k | tmp = OPENSSL_malloc(nvsize); |
606 | | |
607 | 6.30k | if (tmp == NULL) |
608 | 0 | return NULL; |
609 | | |
610 | 6.30k | tmp->ht = h; |
611 | 6.30k | tmp->value.value = data; |
612 | 6.30k | tmp->value.type_id = type; |
613 | 6.30k | tmp->value.key.keybuf = NULL; |
614 | 6.30k | if (h->config.collision_check) { |
615 | 6.30k | tmp->value.key.keybuf = (uint8_t *)(tmp + 1); |
616 | 6.30k | tmp->value.key.keysize = key->keysize; |
617 | 6.30k | memcpy(tmp->value.key.keybuf, key->keybuf, key->keysize); |
618 | 6.30k | } |
619 | | |
620 | | |
621 | 6.30k | return tmp; |
622 | 6.30k | } |
623 | | |
624 | | static void free_value(struct ht_internal_value_st *v) |
625 | 6.30k | { |
626 | 6.30k | OPENSSL_free(v); |
627 | 6.30k | } |
628 | | |
629 | | int ossl_ht_insert(HT *h, HT_KEY *key, HT_VALUE *data, HT_VALUE **olddata) |
630 | 6.30k | { |
631 | 6.30k | struct ht_internal_value_st *newval = NULL; |
632 | 6.30k | uint64_t hash; |
633 | 6.30k | int rc = 0; |
634 | 6.30k | int i; |
635 | | |
636 | 6.30k | if (data->value == NULL) |
637 | 0 | goto out; |
638 | | |
639 | 6.30k | rc = -1; |
640 | 6.30k | newval = alloc_new_value(h, key, data->value, data->type_id); |
641 | 6.30k | if (newval == NULL) |
642 | 0 | goto out; |
643 | | |
644 | | /* |
645 | | * we have to take our lock here to prevent other changes |
646 | | * to the bucket list |
647 | | */ |
648 | 6.30k | hash = h->config.ht_hash_fn(key->keybuf, key->keysize); |
649 | | |
650 | 6.30k | for (i = 0; |
651 | 6.30k | (rc = ossl_ht_insert_locked(h, hash, newval, olddata)) == -1 |
652 | 6.30k | && i < 4; |
653 | 6.30k | ++i) |
654 | 0 | if (!grow_hashtable(h, h->wpd.neighborhood_len)) { |
655 | 0 | rc = -1; |
656 | 0 | break; |
657 | 0 | } |
658 | | |
659 | 6.30k | if (rc <= 0) |
660 | 0 | free_value(newval); |
661 | | |
662 | 6.30k | out: |
663 | 6.30k | return rc; |
664 | 6.30k | } |
665 | | |
666 | | HT_VALUE *ossl_ht_get(HT *h, HT_KEY *key) |
667 | 119k | { |
668 | 119k | struct ht_mutable_data_st *md; |
669 | 119k | uint64_t hash; |
670 | 119k | uint64_t neigh_idx_start; |
671 | 119k | uint64_t neigh_idx; |
672 | 119k | struct ht_internal_value_st *ival = NULL; |
673 | 119k | size_t j; |
674 | 119k | uint64_t ehash; |
675 | 119k | int lockless_reads = h->config.lockless_reads; |
676 | | |
677 | 119k | hash = h->config.ht_hash_fn(key->keybuf, key->keysize); |
678 | | |
679 | 119k | md = ossl_rcu_deref(&h->md); |
680 | 119k | neigh_idx = neigh_idx_start = hash & md->neighborhood_mask; |
681 | 119k | do { |
682 | 119k | PREFETCH_NEIGHBORHOOD(md->neighborhoods[neigh_idx]); |
683 | 126k | for (j = 0; j < NEIGHBORHOOD_LEN; j++) { |
684 | 126k | ival = ossl_rcu_deref(&md->neighborhoods[neigh_idx].entries[j].value); |
685 | 126k | if (ival == NULL) { |
686 | 8.49k | if (lockless_reads) |
687 | | /* lockless_reads implies no deletion, we can break out */ |
688 | 8.49k | return NULL; |
689 | 0 | continue; |
690 | 8.49k | } |
691 | 118k | if (!CRYPTO_atomic_load(&md->neighborhoods[neigh_idx].entries[j].hash, |
692 | 118k | &ehash, h->atomic_lock)) |
693 | 0 | return NULL; |
694 | 118k | if (compare_hash(hash, ehash) && match_key(&ival->value.key, key)) |
695 | 110k | return (HT_VALUE *)ival; |
696 | 118k | } |
697 | 32 | if (!lockless_reads) |
698 | 0 | break; |
699 | | /* Continue search in subsequent neighborhoods */ |
700 | 32 | neigh_idx = (neigh_idx + 1) & md->neighborhood_mask; |
701 | 32 | } while (neigh_idx != neigh_idx_start); |
702 | | |
703 | 0 | return NULL; |
704 | 119k | } |
705 | | |
706 | | static void free_old_entry(void *arg) |
707 | 0 | { |
708 | 0 | struct ht_internal_value_st *v = arg; |
709 | |
|
710 | 0 | v->ht->config.ht_free_fn((HT_VALUE *)v); |
711 | 0 | free_value(v); |
712 | 0 | } |
713 | | |
714 | | int ossl_ht_delete(HT *h, HT_KEY *key) |
715 | 0 | { |
716 | 0 | uint64_t hash; |
717 | 0 | uint64_t neigh_idx; |
718 | 0 | size_t j; |
719 | 0 | struct ht_internal_value_st *v = NULL; |
720 | 0 | HT_VALUE *nv = NULL; |
721 | 0 | int rc = 0; |
722 | |
|
723 | 0 | if (h->config.lockless_reads) |
724 | 0 | return 0; |
725 | | |
726 | 0 | hash = h->config.ht_hash_fn(key->keybuf, key->keysize); |
727 | |
|
728 | 0 | neigh_idx = hash & h->md->neighborhood_mask; |
729 | 0 | PREFETCH_NEIGHBORHOOD(h->md->neighborhoods[neigh_idx]); |
730 | 0 | for (j = 0; j < NEIGHBORHOOD_LEN; j++) { |
731 | 0 | v = (struct ht_internal_value_st *)h->md->neighborhoods[neigh_idx].entries[j].value; |
732 | 0 | if (v == NULL) |
733 | 0 | continue; |
734 | 0 | if (compare_hash(hash, h->md->neighborhoods[neigh_idx].entries[j].hash) |
735 | 0 | && match_key(key, &v->value.key)) { |
736 | 0 | if (!CRYPTO_atomic_store(&h->md->neighborhoods[neigh_idx].entries[j].hash, |
737 | 0 | 0, h->atomic_lock)) |
738 | 0 | break; |
739 | 0 | h->wpd.value_count--; |
740 | 0 | ossl_rcu_assign_ptr(&h->md->neighborhoods[neigh_idx].entries[j].value, |
741 | 0 | &nv); |
742 | 0 | rc = 1; |
743 | 0 | break; |
744 | 0 | } |
745 | 0 | } |
746 | 0 | if (rc == 1) { |
747 | 0 | ossl_rcu_call(h->lock, free_old_entry, v); |
748 | 0 | h->wpd.need_sync = 1; |
749 | 0 | } |
750 | 0 | return rc; |
751 | 0 | } |