/src/openssl/crypto/hashtable/hashtable.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2024-2026 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 | | /* |
75 | | * When we do a lookup/insert/delete, there is a high likelihood |
76 | | * that we will iterate over at least part of the neighborhood list |
77 | | * As such, because we design a neighborhood entry to fit into a single |
78 | | * cache line it is advantageous, when supported to fetch the entire |
79 | | * structure for faster lookups |
80 | | */ |
81 | | #if defined(__GNUC__) || defined(__CLANG__) |
82 | 225k | #define PREFETCH_NEIGHBORHOOD(x) __builtin_prefetch(x.entries) |
83 | 420k | #define PREFETCH(x) __builtin_prefetch(x) |
84 | | #define ALIGN __attribute__((aligned(8))) |
85 | | #else |
86 | | #define PREFETCH_NEIGHBORHOOD(x) |
87 | | #define PREFETCH(x) |
88 | | #define ALIGN |
89 | | #endif |
90 | | |
91 | | /* |
92 | | * Define our neighborhood list length |
93 | | * Note: It should always be a power of 2 |
94 | | */ |
95 | 0 | #define DEFAULT_NEIGH_LEN_LOG 4 |
96 | 0 | #define DEFAULT_NEIGH_LEN (1 << DEFAULT_NEIGH_LEN_LOG) |
97 | | |
98 | | /* |
99 | | * For now assume cache line size is 64 bytes |
100 | | */ |
101 | 654k | #define CACHE_LINE_BYTES 64 |
102 | | #define CACHE_LINE_ALIGNMENT CACHE_LINE_BYTES |
103 | | |
104 | 654k | #define NEIGHBORHOOD_LEN (CACHE_LINE_BYTES / sizeof(struct ht_neighborhood_entry_st)) |
105 | | /* |
106 | | * Defines our chains of values |
107 | | */ |
108 | | struct ht_internal_value_st { |
109 | | HT_VALUE value; |
110 | | HT *ht; |
111 | | }; |
112 | | |
113 | | struct ht_neighborhood_entry_st { |
114 | | uint64_t hash; |
115 | | struct ht_internal_value_st *value; |
116 | | } ALIGN; |
117 | | |
118 | | struct ht_neighborhood_st { |
119 | | struct ht_neighborhood_entry_st entries[NEIGHBORHOOD_LEN]; |
120 | | }; |
121 | | |
122 | | /* |
123 | | * Updates to data in this struct |
124 | | * require an rcu sync after modification |
125 | | * prior to free |
126 | | */ |
127 | | struct ht_mutable_data_st { |
128 | | struct ht_neighborhood_st *neighborhoods; |
129 | | void *neighborhood_ptr_to_free; |
130 | | uint64_t neighborhood_mask; |
131 | | }; |
132 | | |
133 | | /* |
134 | | * Private data may be updated on the write |
135 | | * side only, and so do not require rcu sync |
136 | | */ |
137 | | struct ht_write_private_data_st { |
138 | | size_t neighborhood_len; |
139 | | size_t value_count; |
140 | | int need_sync; |
141 | | }; |
142 | | |
143 | | struct ht_internal_st { |
144 | | HT_CONFIG config; |
145 | | CRYPTO_RCU_LOCK *lock; |
146 | | CRYPTO_RWLOCK *atomic_lock; |
147 | | struct ht_mutable_data_st *md; |
148 | | struct ht_write_private_data_st wpd; |
149 | | }; |
150 | | |
151 | | static void free_value(struct ht_internal_value_st *v); |
152 | | |
153 | | static struct ht_neighborhood_st *alloc_new_neighborhood_list(size_t len, |
154 | | void **freeptr) |
155 | 292 | { |
156 | 292 | struct ht_neighborhood_st *ret; |
157 | | |
158 | 292 | #if !defined(OPENSSL_SMALL_FOOTPRINT) |
159 | 292 | ret = OPENSSL_aligned_alloc_array(len, sizeof(struct ht_neighborhood_st), |
160 | 292 | CACHE_LINE_BYTES, freeptr); |
161 | | |
162 | | /* fall back to regular malloc */ |
163 | 292 | if (ret == NULL) |
164 | 0 | #endif |
165 | 0 | { |
166 | 0 | ret = *freeptr = OPENSSL_malloc_array(len, sizeof(struct ht_neighborhood_st)); |
167 | 0 | if (ret == NULL) |
168 | 0 | return NULL; |
169 | 0 | } |
170 | 292 | memset(ret, 0, sizeof(struct ht_neighborhood_st) * len); |
171 | 292 | return ret; |
172 | 292 | } |
173 | | |
174 | | static void internal_free_nop(HT_VALUE *v) |
175 | 0 | { |
176 | 0 | return; |
177 | 0 | } |
178 | | |
179 | | static uint64_t internal_ht_hash_fn(HT_KEY *key) |
180 | 225k | { |
181 | 225k | return ossl_fnv1a_hash(key->keybuf, key->keysize); |
182 | 225k | } |
183 | | |
184 | | HT *ossl_ht_new(const HT_CONFIG *conf) |
185 | 272 | { |
186 | 272 | HT *new = OPENSSL_zalloc(sizeof(*new)); |
187 | | |
188 | 272 | if (new == NULL) |
189 | 0 | return NULL; |
190 | | |
191 | 272 | if (conf->lockless_reads && conf->no_rcu) |
192 | 0 | goto err; |
193 | | |
194 | 272 | if (!conf->no_rcu) { |
195 | 16 | new->atomic_lock = CRYPTO_THREAD_lock_new(); |
196 | 16 | if (new->atomic_lock == NULL) |
197 | 0 | goto err; |
198 | 16 | } |
199 | 272 | memcpy(&new->config, conf, sizeof(*conf)); |
200 | | |
201 | 272 | if (new->config.init_neighborhoods != 0) { |
202 | 272 | new->wpd.neighborhood_len = new->config.init_neighborhoods; |
203 | | /* round up to the next power of 2 */ |
204 | 272 | new->wpd.neighborhood_len--; |
205 | 272 | new->wpd.neighborhood_len |= new->wpd.neighborhood_len >> 1; |
206 | 272 | new->wpd.neighborhood_len |= new->wpd.neighborhood_len >> 2; |
207 | 272 | new->wpd.neighborhood_len |= new->wpd.neighborhood_len >> 4; |
208 | 272 | new->wpd.neighborhood_len |= new->wpd.neighborhood_len >> 8; |
209 | 272 | new->wpd.neighborhood_len |= new->wpd.neighborhood_len >> 16; |
210 | 272 | new->wpd.neighborhood_len++; |
211 | 272 | } else { |
212 | 0 | new->wpd.neighborhood_len = DEFAULT_NEIGH_LEN; |
213 | 0 | } |
214 | | |
215 | 272 | if (new->config.ht_free_fn == NULL) |
216 | 16 | new->config.ht_free_fn = internal_free_nop; |
217 | | |
218 | 272 | new->md = OPENSSL_zalloc(sizeof(*new->md)); |
219 | 272 | if (new->md == NULL) |
220 | 0 | goto err; |
221 | | |
222 | 272 | new->md->neighborhoods = alloc_new_neighborhood_list(new->wpd.neighborhood_len, |
223 | 272 | &new->md->neighborhood_ptr_to_free); |
224 | 272 | if (new->md->neighborhoods == NULL) |
225 | 0 | goto err; |
226 | 272 | new->md->neighborhood_mask = new->wpd.neighborhood_len - 1; |
227 | | |
228 | 272 | if (!conf->no_rcu) { |
229 | 16 | new->lock = ossl_rcu_lock_new(1, conf->ctx); |
230 | 16 | if (new->lock == NULL) |
231 | 0 | goto err; |
232 | 16 | } |
233 | 272 | if (new->config.ht_hash_fn == NULL) |
234 | 272 | new->config.ht_hash_fn = internal_ht_hash_fn; |
235 | | |
236 | 272 | return new; |
237 | | |
238 | 0 | err: |
239 | 0 | if (!conf->no_rcu) { |
240 | 0 | CRYPTO_THREAD_lock_free(new->atomic_lock); |
241 | 0 | ossl_rcu_lock_free(new->lock); |
242 | 0 | } |
243 | 0 | if (new->md != NULL) |
244 | 0 | OPENSSL_free(new->md->neighborhood_ptr_to_free); |
245 | 0 | OPENSSL_free(new->md); |
246 | 0 | OPENSSL_free(new); |
247 | 0 | return NULL; |
248 | 272 | } |
249 | | |
250 | | int ossl_ht_read_lock(HT *htable) |
251 | 0 | { |
252 | 0 | if (htable->config.no_rcu) |
253 | 0 | return 1; |
254 | | |
255 | 0 | return ossl_rcu_read_lock(htable->lock); |
256 | 0 | } |
257 | | |
258 | | void ossl_ht_read_unlock(HT *htable) |
259 | 0 | { |
260 | 0 | if (htable->config.no_rcu) |
261 | 0 | return; |
262 | | |
263 | 0 | ossl_rcu_read_unlock(htable->lock); |
264 | 0 | } |
265 | | |
266 | | void ossl_ht_write_lock(HT *htable) |
267 | 0 | { |
268 | 0 | if (htable->config.no_rcu) |
269 | 0 | return; |
270 | | |
271 | 0 | ossl_rcu_write_lock(htable->lock); |
272 | 0 | htable->wpd.need_sync = 0; |
273 | 0 | } |
274 | | |
275 | | void ossl_ht_write_unlock(HT *htable) |
276 | 0 | { |
277 | 0 | int need_sync = htable->wpd.need_sync; |
278 | |
|
279 | 0 | if (htable->config.no_rcu) |
280 | 0 | return; |
281 | | |
282 | 0 | htable->wpd.need_sync = 0; |
283 | 0 | ossl_rcu_write_unlock(htable->lock); |
284 | 0 | if (need_sync) |
285 | 0 | ossl_synchronize_rcu(htable->lock); |
286 | 0 | } |
287 | | |
288 | | static void free_oldmd(void *arg) |
289 | 0 | { |
290 | 0 | struct ht_mutable_data_st *oldmd = arg; |
291 | 0 | size_t i, j; |
292 | 0 | size_t neighborhood_len = (size_t)oldmd->neighborhood_mask + 1; |
293 | 0 | struct ht_internal_value_st *v; |
294 | |
|
295 | 0 | for (i = 0; i < neighborhood_len; i++) { |
296 | 0 | PREFETCH_NEIGHBORHOOD(oldmd->neighborhoods[i + 1]); |
297 | 0 | for (j = 0; j < NEIGHBORHOOD_LEN; j++) { |
298 | 0 | if (oldmd->neighborhoods[i].entries[j].value != NULL) { |
299 | 0 | v = oldmd->neighborhoods[i].entries[j].value; |
300 | 0 | v->ht->config.ht_free_fn((HT_VALUE *)v); |
301 | 0 | free_value(v); |
302 | 0 | } |
303 | 0 | } |
304 | 0 | } |
305 | |
|
306 | 0 | OPENSSL_free(oldmd->neighborhood_ptr_to_free); |
307 | 0 | OPENSSL_free(oldmd); |
308 | 0 | } |
309 | | |
310 | | static int ossl_ht_flush_internal(HT *h) |
311 | 0 | { |
312 | 0 | struct ht_mutable_data_st *newmd = NULL; |
313 | 0 | struct ht_mutable_data_st *oldmd = NULL; |
314 | 0 | CRYPTO_RCU_CB_ITEM *cbi = NULL; |
315 | |
|
316 | 0 | newmd = OPENSSL_zalloc(sizeof(*newmd)); |
317 | 0 | if (newmd == NULL) |
318 | 0 | return 0; |
319 | | |
320 | 0 | newmd->neighborhoods = alloc_new_neighborhood_list(DEFAULT_NEIGH_LEN, |
321 | 0 | &newmd->neighborhood_ptr_to_free); |
322 | 0 | if (newmd->neighborhoods == NULL) { |
323 | 0 | OPENSSL_free(newmd); |
324 | 0 | return 0; |
325 | 0 | } |
326 | | |
327 | 0 | newmd->neighborhood_mask = DEFAULT_NEIGH_LEN - 1; |
328 | |
|
329 | 0 | if (!h->config.no_rcu) { |
330 | 0 | cbi = ossl_rcu_cb_item_new(); |
331 | 0 | if (cbi == NULL) { |
332 | 0 | OPENSSL_free(newmd->neighborhood_ptr_to_free); |
333 | 0 | OPENSSL_free(newmd); |
334 | 0 | return 0; |
335 | 0 | } |
336 | | |
337 | | /* Swap the old and new mutable data sets */ |
338 | 0 | oldmd = ossl_rcu_deref(&h->md); |
339 | 0 | ossl_rcu_assign_ptr(&h->md, &newmd); |
340 | 0 | } else { |
341 | 0 | oldmd = h->md; |
342 | 0 | h->md = newmd; |
343 | 0 | } |
344 | | |
345 | | /* Set the number of entries to 0 */ |
346 | 0 | h->wpd.value_count = 0; |
347 | 0 | h->wpd.neighborhood_len = DEFAULT_NEIGH_LEN; |
348 | |
|
349 | 0 | if (!h->config.no_rcu) { |
350 | 0 | ossl_rcu_call(h->lock, cbi, free_oldmd, oldmd); |
351 | 0 | h->wpd.need_sync = 1; |
352 | 0 | } else { |
353 | 0 | free_oldmd(oldmd); |
354 | 0 | } |
355 | |
|
356 | 0 | return 1; |
357 | 0 | } |
358 | | |
359 | | int ossl_ht_flush(HT *h) |
360 | 0 | { |
361 | 0 | return ossl_ht_flush_internal(h); |
362 | 0 | } |
363 | | |
364 | | void ossl_ht_free(HT *h) |
365 | 0 | { |
366 | 0 | int flush_ok; |
367 | |
|
368 | 0 | if (h == NULL) |
369 | 0 | return; |
370 | | |
371 | 0 | ossl_ht_write_lock(h); |
372 | 0 | flush_ok = ossl_ht_flush_internal(h); |
373 | 0 | ossl_ht_write_unlock(h); |
374 | | /* Freeing the lock does a final sync for us */ |
375 | 0 | if (!h->config.no_rcu) { |
376 | 0 | CRYPTO_THREAD_lock_free(h->atomic_lock); |
377 | 0 | ossl_rcu_lock_free(h->lock); |
378 | 0 | } |
379 | 0 | if (flush_ok) { |
380 | 0 | OPENSSL_free(h->md->neighborhood_ptr_to_free); |
381 | 0 | OPENSSL_free(h->md); |
382 | 0 | } else { |
383 | 0 | free_oldmd(h->md); |
384 | 0 | } |
385 | 0 | OPENSSL_free(h); |
386 | 0 | return; |
387 | 0 | } |
388 | | |
389 | | size_t ossl_ht_count(HT *h) |
390 | 85 | { |
391 | 85 | size_t count; |
392 | | |
393 | 85 | count = h->wpd.value_count; |
394 | 85 | return count; |
395 | 85 | } |
396 | | |
397 | | void ossl_ht_foreach_until(HT *h, int (*cb)(HT_VALUE *obj, void *arg), |
398 | | void *arg) |
399 | 0 | { |
400 | 0 | size_t i, j; |
401 | 0 | struct ht_mutable_data_st *md; |
402 | |
|
403 | 0 | md = ossl_rcu_deref(&h->md); |
404 | 0 | for (i = 0; i < md->neighborhood_mask + 1; i++) { |
405 | 0 | PREFETCH_NEIGHBORHOOD(md->neighborhoods[i + 1]); |
406 | 0 | for (j = 0; j < NEIGHBORHOOD_LEN; j++) { |
407 | 0 | if (md->neighborhoods[i].entries[j].value != NULL) { |
408 | 0 | if (!cb((HT_VALUE *)md->neighborhoods[i].entries[j].value, arg)) |
409 | 0 | goto out; |
410 | 0 | } |
411 | 0 | } |
412 | 0 | } |
413 | 0 | out: |
414 | 0 | return; |
415 | 0 | } |
416 | | |
417 | | HT_VALUE_LIST *ossl_ht_filter(HT *h, size_t max_len, |
418 | | int (*filter)(HT_VALUE *obj, void *arg), |
419 | | void *arg) |
420 | 0 | { |
421 | 0 | struct ht_mutable_data_st *md; |
422 | 0 | HT_VALUE_LIST *list = OPENSSL_zalloc(sizeof(HT_VALUE_LIST) |
423 | 0 | + (sizeof(HT_VALUE *) * max_len)); |
424 | 0 | size_t i, j; |
425 | 0 | struct ht_internal_value_st *v; |
426 | |
|
427 | 0 | if (list == NULL) |
428 | 0 | return NULL; |
429 | | |
430 | | /* |
431 | | * The list array lives just beyond the end of |
432 | | * the struct |
433 | | */ |
434 | 0 | list->list = (HT_VALUE **)(list + 1); |
435 | |
|
436 | 0 | md = ossl_rcu_deref(&h->md); |
437 | 0 | for (i = 0; i < md->neighborhood_mask + 1; i++) { |
438 | 0 | PREFETCH_NEIGHBORHOOD(md->neighborhoods[i + 1]); |
439 | 0 | for (j = 0; j < NEIGHBORHOOD_LEN; j++) { |
440 | 0 | v = md->neighborhoods[i].entries[j].value; |
441 | 0 | if (v != NULL && filter((HT_VALUE *)v, arg)) { |
442 | 0 | list->list[list->list_len++] = (HT_VALUE *)v; |
443 | 0 | if (list->list_len == max_len) |
444 | 0 | goto out; |
445 | 0 | } |
446 | 0 | } |
447 | 0 | } |
448 | 0 | out: |
449 | 0 | return list; |
450 | 0 | } |
451 | | |
452 | | void ossl_ht_value_list_free(HT_VALUE_LIST *list) |
453 | 0 | { |
454 | 0 | OPENSSL_free(list); |
455 | 0 | } |
456 | | |
457 | | static int compare_hash(uint64_t hash1, uint64_t hash2) |
458 | 554k | { |
459 | 554k | return (hash1 == hash2); |
460 | 554k | } |
461 | | |
462 | | static void free_old_neigh_table(void *arg) |
463 | 20 | { |
464 | 20 | struct ht_mutable_data_st *oldmd = arg; |
465 | | |
466 | 20 | OPENSSL_free(oldmd->neighborhood_ptr_to_free); |
467 | 20 | OPENSSL_free(oldmd); |
468 | 20 | } |
469 | | |
470 | | /* |
471 | | * Increase hash table bucket list |
472 | | * must be called with write_lock held |
473 | | */ |
474 | | static int grow_hashtable(HT *h, size_t oldsize) |
475 | 20 | { |
476 | 20 | struct ht_mutable_data_st *newmd; |
477 | 20 | struct ht_mutable_data_st *oldmd = ossl_rcu_deref(&h->md); |
478 | 20 | CRYPTO_RCU_CB_ITEM *cbi = NULL; |
479 | 20 | int rc = 0; |
480 | 20 | uint64_t oldi, oldj, newi, newj; |
481 | 20 | uint64_t oldhash; |
482 | 20 | struct ht_internal_value_st *oldv; |
483 | 20 | int rehashed; |
484 | 20 | size_t newsize = oldsize * 2; |
485 | | |
486 | 20 | if (h->config.lockless_reads) |
487 | 0 | goto out; |
488 | | |
489 | 20 | if ((newmd = OPENSSL_zalloc(sizeof(*newmd))) == NULL) |
490 | 0 | goto out; |
491 | | |
492 | | /* bucket list is always a power of 2 */ |
493 | 20 | newmd->neighborhoods = alloc_new_neighborhood_list(oldsize * 2, |
494 | 20 | &newmd->neighborhood_ptr_to_free); |
495 | 20 | if (newmd->neighborhoods == NULL) |
496 | 0 | goto out_free; |
497 | | |
498 | | /* being a power of 2 makes for easy mask computation */ |
499 | 20 | newmd->neighborhood_mask = (newsize - 1); |
500 | | |
501 | | /* |
502 | | * Now we need to start rehashing entries |
503 | | * Note we don't need to use atomics here as the new |
504 | | * mutable data hasn't been published |
505 | | */ |
506 | 44 | for (oldi = 0; oldi < h->wpd.neighborhood_len; oldi++) { |
507 | 24 | PREFETCH_NEIGHBORHOOD(oldmd->neighborhoods[oldi + 1]); |
508 | 120 | for (oldj = 0; oldj < NEIGHBORHOOD_LEN; oldj++) { |
509 | 96 | oldv = oldmd->neighborhoods[oldi].entries[oldj].value; |
510 | 96 | if (oldv == NULL) |
511 | 6 | continue; |
512 | 90 | oldhash = oldmd->neighborhoods[oldi].entries[oldj].hash; |
513 | 90 | newi = oldhash & newmd->neighborhood_mask; |
514 | 90 | rehashed = 0; |
515 | 133 | for (newj = 0; newj < NEIGHBORHOOD_LEN; newj++) { |
516 | 133 | if (newmd->neighborhoods[newi].entries[newj].value == NULL) { |
517 | 90 | newmd->neighborhoods[newi].entries[newj].value = oldv; |
518 | 90 | newmd->neighborhoods[newi].entries[newj].hash = oldhash; |
519 | 90 | rehashed = 1; |
520 | 90 | break; |
521 | 90 | } |
522 | 133 | } |
523 | 90 | if (rehashed == 0) { |
524 | | /* we ran out of space in a neighborhood, grow again */ |
525 | 0 | OPENSSL_free(newmd->neighborhood_ptr_to_free); |
526 | 0 | OPENSSL_free(newmd); |
527 | 0 | return grow_hashtable(h, newsize); |
528 | 0 | } |
529 | 90 | } |
530 | 24 | } |
531 | | |
532 | | /* |
533 | | * Pre allocate the rcu callback item before assigning the newmd. |
534 | | */ |
535 | 20 | if (!h->config.no_rcu) { |
536 | 0 | cbi = ossl_rcu_cb_item_new(); |
537 | 0 | if (cbi == NULL) |
538 | 0 | goto out_free; |
539 | 0 | } |
540 | | |
541 | | /* |
542 | | * Now that our entries are all hashed into the new bucket list |
543 | | * update our bucket_len and target_max_load |
544 | | */ |
545 | 20 | h->wpd.neighborhood_len = newsize; |
546 | | |
547 | | /* |
548 | | * Now we replace the old mutable data with the new |
549 | | */ |
550 | 20 | if (!h->config.no_rcu) { |
551 | 0 | ossl_rcu_assign_ptr(&h->md, &newmd); |
552 | 0 | ossl_rcu_call(h->lock, cbi, free_old_neigh_table, oldmd); |
553 | 0 | h->wpd.need_sync = 1; |
554 | 20 | } else { |
555 | 20 | h->md = newmd; |
556 | 20 | free_old_neigh_table(oldmd); |
557 | 20 | } |
558 | | /* |
559 | | * And we're done |
560 | | */ |
561 | 20 | rc = 1; |
562 | | |
563 | 20 | out: |
564 | 20 | return rc; |
565 | 0 | out_free: |
566 | 0 | OPENSSL_free(newmd->neighborhood_ptr_to_free); |
567 | 0 | OPENSSL_free(newmd); |
568 | 0 | goto out; |
569 | 20 | } |
570 | | |
571 | | static void free_old_ht_value(void *arg) |
572 | 0 | { |
573 | 0 | HT_VALUE *h = (HT_VALUE *)arg; |
574 | | |
575 | | /* |
576 | | * Note, this is only called on replacement, |
577 | | * the caller is responsible for freeing the |
578 | | * held data, we just need to free the wrapping |
579 | | * struct here |
580 | | */ |
581 | 0 | OPENSSL_free(h); |
582 | 0 | } |
583 | | |
584 | | static ossl_inline int match_key(HT_KEY *a, HT_KEY *b) |
585 | 210k | { |
586 | | /* |
587 | | * keys match if they are both present, the same size |
588 | | * and compare equal in memory |
589 | | */ |
590 | 210k | PREFETCH(a->keybuf); |
591 | 210k | PREFETCH(b->keybuf); |
592 | 210k | if (a->keybuf != NULL && b->keybuf != NULL && a->keysize == b->keysize) |
593 | 210k | return !memcmp(a->keybuf, b->keybuf, a->keysize); |
594 | | |
595 | 0 | return 1; |
596 | 210k | } |
597 | | |
598 | | static int ossl_ht_insert_locked(HT *h, uint64_t hash, |
599 | | struct ht_internal_value_st *newval, |
600 | | HT_VALUE **olddata) |
601 | 6.60k | { |
602 | 6.60k | struct ht_mutable_data_st *md = h->md; |
603 | 6.60k | uint64_t neigh_idx_start = hash & md->neighborhood_mask; |
604 | 6.60k | uint64_t neigh_idx = neigh_idx_start; |
605 | 6.60k | size_t j; |
606 | 6.60k | uint64_t ihash; |
607 | 6.60k | HT_VALUE *ival; |
608 | 6.60k | size_t empty_idx = SIZE_MAX; |
609 | 6.60k | int lockless_reads = h->config.lockless_reads; |
610 | | |
611 | 6.60k | do { |
612 | 6.60k | PREFETCH_NEIGHBORHOOD(md->neighborhoods[neigh_idx]); |
613 | | |
614 | 9.63k | for (j = 0; j < NEIGHBORHOOD_LEN; j++) { |
615 | 9.44k | if (!h->config.no_rcu) |
616 | 8.68k | ival = ossl_rcu_deref(&md->neighborhoods[neigh_idx].entries[j].value); |
617 | 760 | else |
618 | 760 | ival = (HT_VALUE *)md->neighborhoods[neigh_idx].entries[j].value; |
619 | 9.44k | if (ival == NULL) { |
620 | 6.80k | empty_idx = j; |
621 | | /* lockless_reads implies no deletion, we can break out */ |
622 | 6.80k | if (lockless_reads) |
623 | 6.41k | goto not_found; |
624 | 390 | continue; |
625 | 6.80k | } |
626 | 2.64k | if (!h->config.no_rcu) { |
627 | 2.27k | if (!CRYPTO_atomic_load(&md->neighborhoods[neigh_idx].entries[j].hash, |
628 | 2.27k | &ihash, h->atomic_lock)) |
629 | 0 | return 0; |
630 | 2.27k | } else { |
631 | 370 | ihash = md->neighborhoods[neigh_idx].entries[j].hash; |
632 | 370 | } |
633 | 2.64k | if (compare_hash(hash, ihash) && match_key(&newval->value.key, &ival->key)) { |
634 | 0 | if (olddata == NULL) { |
635 | | /* This would insert a duplicate -> fail */ |
636 | 0 | return 0; |
637 | 0 | } |
638 | | /* Do a replacement */ |
639 | 0 | if (!h->config.no_rcu) { |
640 | 0 | CRYPTO_RCU_CB_ITEM *cbi = ossl_rcu_cb_item_new(); |
641 | 0 | if (cbi == NULL) |
642 | 0 | return 0; |
643 | 0 | if (!CRYPTO_atomic_store(&md->neighborhoods[neigh_idx].entries[j].hash, |
644 | 0 | hash, h->atomic_lock)) { |
645 | 0 | ossl_rcu_cb_item_free(cbi); |
646 | 0 | return 0; |
647 | 0 | } |
648 | 0 | *olddata = (HT_VALUE *)md->neighborhoods[neigh_idx].entries[j].value; |
649 | 0 | ossl_rcu_assign_ptr(&md->neighborhoods[neigh_idx].entries[j].value, |
650 | 0 | &newval); |
651 | 0 | ossl_rcu_call(h->lock, cbi, free_old_ht_value, *olddata); |
652 | 0 | } else { |
653 | 0 | md->neighborhoods[neigh_idx].entries[j].hash = hash; |
654 | 0 | *olddata = (HT_VALUE *)md->neighborhoods[neigh_idx].entries[j].value; |
655 | 0 | md->neighborhoods[neigh_idx].entries[j].value = newval; |
656 | 0 | } |
657 | 0 | h->wpd.need_sync = 1; |
658 | 0 | return 1; |
659 | 0 | } |
660 | 2.64k | } |
661 | 190 | if (!lockless_reads) |
662 | 190 | break; |
663 | | /* Continue search in subsequent neighborhoods */ |
664 | 0 | neigh_idx = (neigh_idx + 1) & md->neighborhood_mask; |
665 | 0 | } while (neigh_idx != neigh_idx_start); |
666 | | |
667 | 6.60k | not_found: |
668 | | /* If we get to here, its just an insert */ |
669 | 6.60k | if (empty_idx == SIZE_MAX) |
670 | 20 | return -1; /* out of space */ |
671 | 6.58k | if (!h->config.no_rcu) { |
672 | 6.41k | if (!CRYPTO_atomic_store(&md->neighborhoods[neigh_idx].entries[empty_idx].hash, |
673 | 6.41k | hash, h->atomic_lock)) |
674 | 0 | return 0; |
675 | 6.41k | ossl_rcu_assign_ptr(&md->neighborhoods[neigh_idx].entries[empty_idx].value, |
676 | 6.41k | &newval); |
677 | 6.41k | } else { |
678 | 170 | md->neighborhoods[neigh_idx].entries[empty_idx].hash = hash; |
679 | 170 | md->neighborhoods[neigh_idx].entries[empty_idx].value = newval; |
680 | 170 | } |
681 | 6.58k | h->wpd.value_count++; |
682 | 6.58k | return 1; |
683 | 6.58k | } |
684 | | |
685 | | static struct ht_internal_value_st *alloc_new_value(HT *h, HT_KEY *key, |
686 | | void *data, |
687 | | uintptr_t *type) |
688 | 6.58k | { |
689 | 6.58k | struct ht_internal_value_st *tmp; |
690 | 6.58k | size_t nvsize = sizeof(*tmp); |
691 | | |
692 | 6.58k | if (h->config.collision_check == 1) |
693 | 6.58k | nvsize += key->keysize; |
694 | | |
695 | 6.58k | tmp = OPENSSL_malloc(nvsize); |
696 | | |
697 | 6.58k | if (tmp == NULL) |
698 | 0 | return NULL; |
699 | | |
700 | 6.58k | tmp->ht = h; |
701 | 6.58k | tmp->value.value = data; |
702 | 6.58k | tmp->value.type_id = type; |
703 | 6.58k | tmp->value.key.keybuf = NULL; |
704 | 6.58k | if (h->config.collision_check) { |
705 | 6.58k | tmp->value.key.keybuf = (uint8_t *)(tmp + 1); |
706 | 6.58k | tmp->value.key.keysize = key->keysize; |
707 | 6.58k | memcpy(tmp->value.key.keybuf, key->keybuf, key->keysize); |
708 | 6.58k | } |
709 | | |
710 | 6.58k | return tmp; |
711 | 6.58k | } |
712 | | |
713 | | static void free_value(struct ht_internal_value_st *v) |
714 | 0 | { |
715 | 0 | OPENSSL_free(v); |
716 | 0 | } |
717 | | |
718 | | int ossl_ht_insert(HT *h, HT_KEY *key, HT_VALUE *data, HT_VALUE **olddata) |
719 | 6.58k | { |
720 | 6.58k | struct ht_internal_value_st *newval = NULL; |
721 | 6.58k | uint64_t hash; |
722 | 6.58k | int rc = 0; |
723 | 6.58k | int i; |
724 | | |
725 | 6.58k | if (data->value == NULL) |
726 | 0 | goto out; |
727 | | |
728 | 6.58k | rc = -1; |
729 | 6.58k | newval = alloc_new_value(h, key, data->value, data->type_id); |
730 | 6.58k | if (newval == NULL) |
731 | 0 | goto out; |
732 | | |
733 | 6.58k | if (key->cached_hash) |
734 | 0 | hash = key->cached_hash; |
735 | 6.58k | else |
736 | 6.58k | hash = key->cached_hash = newval->value.key.cached_hash = h->config.ht_hash_fn(key); |
737 | | |
738 | | /* |
739 | | * we have to take our lock here to prevent other changes |
740 | | * to the bucket list |
741 | | */ |
742 | 6.58k | for (i = 0; |
743 | 6.60k | (rc = ossl_ht_insert_locked(h, hash, newval, olddata)) == -1 |
744 | 20 | && i <= (int)NEIGHBORHOOD_LEN; |
745 | 6.58k | ++i) |
746 | 20 | if (!grow_hashtable(h, h->wpd.neighborhood_len)) { |
747 | 0 | rc = -1; |
748 | 0 | break; |
749 | 0 | } |
750 | | |
751 | 6.58k | if (rc <= 0) |
752 | 0 | free_value(newval); |
753 | | |
754 | 6.58k | out: |
755 | 6.58k | return rc; |
756 | 6.58k | } |
757 | | |
758 | | HT_VALUE *ossl_ht_get(HT *h, HT_KEY *key) |
759 | 218k | { |
760 | 218k | struct ht_mutable_data_st *md; |
761 | 218k | uint64_t hash; |
762 | 218k | uint64_t neigh_idx_start; |
763 | 218k | uint64_t neigh_idx; |
764 | 218k | struct ht_internal_value_st *ival = NULL; |
765 | 218k | size_t j; |
766 | 218k | uint64_t ehash; |
767 | 218k | int lockless_reads = h->config.lockless_reads; |
768 | | |
769 | 218k | if (key->cached_hash) |
770 | 0 | hash = key->cached_hash; |
771 | 218k | else |
772 | 218k | hash = key->cached_hash = h->config.ht_hash_fn(key); |
773 | | |
774 | 218k | if (!h->config.no_rcu) |
775 | 121k | md = ossl_rcu_deref(&h->md); |
776 | 97.6k | else |
777 | 97.6k | md = h->md; |
778 | 218k | neigh_idx = neigh_idx_start = hash & md->neighborhood_mask; |
779 | 218k | do { |
780 | 218k | PREFETCH_NEIGHBORHOOD(md->neighborhoods[neigh_idx]); |
781 | 645k | for (j = 0; j < NEIGHBORHOOD_LEN; j++) { |
782 | 645k | if (!h->config.no_rcu) |
783 | 292k | ival = ossl_rcu_deref(&md->neighborhoods[neigh_idx].entries[j].value); |
784 | 352k | else |
785 | 352k | ival = md->neighborhoods[neigh_idx].entries[j].value; |
786 | 645k | if (ival == NULL) { |
787 | 92.9k | if (lockless_reads) |
788 | | /* lockless_reads implies no deletion, we can break out */ |
789 | 8.70k | return NULL; |
790 | 84.2k | continue; |
791 | 92.9k | } |
792 | 552k | if (!h->config.no_rcu) { |
793 | 283k | if (!CRYPTO_atomic_load(&md->neighborhoods[neigh_idx].entries[j].hash, |
794 | 283k | &ehash, h->atomic_lock)) |
795 | 0 | return NULL; |
796 | 283k | } else { |
797 | 268k | ehash = md->neighborhoods[neigh_idx].entries[j].hash; |
798 | 268k | } |
799 | 552k | if (compare_hash(hash, ehash) && match_key(&ival->value.key, key)) |
800 | 210k | return (HT_VALUE *)ival; |
801 | 552k | } |
802 | 37 | if (!lockless_reads) |
803 | 37 | break; |
804 | | /* Continue search in subsequent neighborhoods */ |
805 | 0 | neigh_idx = (neigh_idx + 1) & md->neighborhood_mask; |
806 | 0 | } while (neigh_idx != neigh_idx_start); |
807 | | |
808 | 37 | return NULL; |
809 | 218k | } |
810 | | |
811 | | static void free_old_entry(void *arg) |
812 | 0 | { |
813 | 0 | struct ht_internal_value_st *v = arg; |
814 | |
|
815 | 0 | v->ht->config.ht_free_fn((HT_VALUE *)v); |
816 | 0 | free_value(v); |
817 | 0 | } |
818 | | |
819 | | int ossl_ht_delete(HT *h, HT_KEY *key) |
820 | 0 | { |
821 | 0 | uint64_t hash; |
822 | 0 | uint64_t neigh_idx; |
823 | 0 | size_t j; |
824 | 0 | struct ht_internal_value_st *v = NULL; |
825 | 0 | HT_VALUE *nv = NULL; |
826 | 0 | int rc = 0; |
827 | |
|
828 | 0 | if (h->config.lockless_reads) |
829 | 0 | return 0; |
830 | | |
831 | 0 | if (key->cached_hash) |
832 | 0 | hash = key->cached_hash; |
833 | 0 | else |
834 | 0 | hash = key->cached_hash = h->config.ht_hash_fn(key); |
835 | |
|
836 | 0 | neigh_idx = hash & h->md->neighborhood_mask; |
837 | 0 | PREFETCH_NEIGHBORHOOD(h->md->neighborhoods[neigh_idx]); |
838 | 0 | for (j = 0; j < NEIGHBORHOOD_LEN; j++) { |
839 | 0 | v = (struct ht_internal_value_st *)h->md->neighborhoods[neigh_idx].entries[j].value; |
840 | 0 | if (v == NULL) |
841 | 0 | continue; |
842 | 0 | if (compare_hash(hash, h->md->neighborhoods[neigh_idx].entries[j].hash) |
843 | 0 | && match_key(key, &v->value.key)) { |
844 | 0 | if (!h->config.no_rcu) { |
845 | 0 | CRYPTO_RCU_CB_ITEM *cbi = ossl_rcu_cb_item_new(); |
846 | 0 | if (cbi == NULL) |
847 | 0 | break; |
848 | 0 | if (!CRYPTO_atomic_store(&h->md->neighborhoods[neigh_idx].entries[j].hash, |
849 | 0 | 0, h->atomic_lock)) { |
850 | 0 | ossl_rcu_cb_item_free(cbi); |
851 | 0 | break; |
852 | 0 | } |
853 | 0 | ossl_rcu_assign_ptr(&h->md->neighborhoods[neigh_idx].entries[j].value, &nv); |
854 | 0 | ossl_rcu_call(h->lock, cbi, free_old_entry, v); |
855 | 0 | } else { |
856 | 0 | h->md->neighborhoods[neigh_idx].entries[j].hash = 0; |
857 | 0 | h->md->neighborhoods[neigh_idx].entries[j].value = NULL; |
858 | 0 | free_old_entry(v); |
859 | 0 | } |
860 | 0 | h->wpd.value_count--; |
861 | 0 | h->wpd.need_sync = 1; |
862 | 0 | rc = 1; |
863 | 0 | break; |
864 | 0 | } |
865 | 0 | } |
866 | |
|
867 | 0 | return rc; |
868 | 0 | } |
869 | | |
870 | | HT_VALUE *ossl_ht_deref_value(HT *h, HT_VALUE **val) |
871 | 97.5k | { |
872 | 97.5k | HT_VALUE *v; |
873 | | |
874 | 97.5k | if (!h->config.no_rcu) |
875 | 0 | v = ossl_rcu_deref(val); |
876 | 97.5k | else |
877 | 97.5k | v = *val; |
878 | | |
879 | 97.5k | return v; |
880 | 97.5k | } |
881 | | |
882 | | void *ossl_ht_inner_value(HT *h, HT_VALUE *v) |
883 | 0 | { |
884 | 0 | void *inner; |
885 | |
|
886 | 0 | if (!h->config.no_rcu) { |
887 | 0 | inner = v->value; |
888 | 0 | } else { |
889 | 0 | inner = v->value; |
890 | 0 | OPENSSL_free(v); |
891 | 0 | } |
892 | |
|
893 | 0 | return inner; |
894 | 0 | } |