/src/openssl/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 | | /* |
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 | 1.99M | #define PREFETCH_NEIGHBORHOOD(x) __builtin_prefetch(x.entries) |
83 | 3.98M | #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 | 612 | #define DEFAULT_NEIGH_LEN_LOG 4 |
96 | 612 | #define DEFAULT_NEIGH_LEN (1 << DEFAULT_NEIGH_LEN_LOG) |
97 | | |
98 | | /* |
99 | | * For now assume cache line size is 64 bytes |
100 | | */ |
101 | 4.38M | #define CACHE_LINE_BYTES 64 |
102 | | #define CACHE_LINE_ALIGNMENT CACHE_LINE_BYTES |
103 | | |
104 | 4.38M | #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 | 358 | { |
156 | 358 | struct ht_neighborhood_st *ret; |
157 | | |
158 | 358 | #if !defined(OPENSSL_SMALL_FOOTPRINT) |
159 | 358 | ret = OPENSSL_aligned_alloc_array(len, sizeof(struct ht_neighborhood_st), |
160 | 358 | CACHE_LINE_BYTES, freeptr); |
161 | | |
162 | | /* fall back to regular malloc */ |
163 | 358 | 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 | 358 | memset(ret, 0, sizeof(struct ht_neighborhood_st) * len); |
171 | 358 | return ret; |
172 | 358 | } |
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 | 1.99M | { |
181 | 1.99M | return ossl_fnv1a_hash(key->keybuf, key->keysize); |
182 | 1.99M | } |
183 | | |
184 | | HT *ossl_ht_new(const HT_CONFIG *conf) |
185 | 153 | { |
186 | 153 | HT *new = OPENSSL_zalloc(sizeof(*new)); |
187 | | |
188 | 153 | if (new == NULL) |
189 | 0 | return NULL; |
190 | | |
191 | 153 | if (conf->lockless_reads && conf->no_rcu) |
192 | 0 | goto err; |
193 | | |
194 | 153 | if (!conf->no_rcu) { |
195 | 9 | new->atomic_lock = CRYPTO_THREAD_lock_new(); |
196 | 9 | if (new->atomic_lock == NULL) |
197 | 0 | goto err; |
198 | 9 | } |
199 | 153 | memcpy(&new->config, conf, sizeof(*conf)); |
200 | | |
201 | 153 | if (new->config.init_neighborhoods != 0) { |
202 | 153 | new->wpd.neighborhood_len = new->config.init_neighborhoods; |
203 | | /* round up to the next power of 2 */ |
204 | 153 | new->wpd.neighborhood_len--; |
205 | 153 | new->wpd.neighborhood_len |= new->wpd.neighborhood_len >> 1; |
206 | 153 | new->wpd.neighborhood_len |= new->wpd.neighborhood_len >> 2; |
207 | 153 | new->wpd.neighborhood_len |= new->wpd.neighborhood_len >> 4; |
208 | 153 | new->wpd.neighborhood_len |= new->wpd.neighborhood_len >> 8; |
209 | 153 | new->wpd.neighborhood_len |= new->wpd.neighborhood_len >> 16; |
210 | 153 | new->wpd.neighborhood_len++; |
211 | 153 | } else { |
212 | 0 | new->wpd.neighborhood_len = DEFAULT_NEIGH_LEN; |
213 | 0 | } |
214 | | |
215 | 153 | if (new->config.ht_free_fn == NULL) |
216 | 9 | new->config.ht_free_fn = internal_free_nop; |
217 | | |
218 | 153 | new->md = OPENSSL_zalloc(sizeof(*new->md)); |
219 | 153 | if (new->md == NULL) |
220 | 0 | goto err; |
221 | | |
222 | 153 | new->md->neighborhoods = alloc_new_neighborhood_list(new->wpd.neighborhood_len, |
223 | 153 | &new->md->neighborhood_ptr_to_free); |
224 | 153 | if (new->md->neighborhoods == NULL) |
225 | 0 | goto err; |
226 | 153 | new->md->neighborhood_mask = new->wpd.neighborhood_len - 1; |
227 | | |
228 | 153 | if (!conf->no_rcu) { |
229 | 9 | new->lock = ossl_rcu_lock_new(1, conf->ctx); |
230 | 9 | if (new->lock == NULL) |
231 | 0 | goto err; |
232 | 9 | } |
233 | 153 | if (new->config.ht_hash_fn == NULL) |
234 | 153 | new->config.ht_hash_fn = internal_ht_hash_fn; |
235 | | |
236 | 153 | 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 | 153 | } |
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 | 204 | { |
290 | 204 | struct ht_mutable_data_st *oldmd = arg; |
291 | 204 | size_t i, j; |
292 | 204 | size_t neighborhood_len = (size_t)oldmd->neighborhood_mask + 1; |
293 | 204 | struct ht_internal_value_st *v; |
294 | | |
295 | 2.02k | for (i = 0; i < neighborhood_len; i++) { |
296 | 1.82k | PREFETCH_NEIGHBORHOOD(oldmd->neighborhoods[i + 1]); |
297 | 9.12k | for (j = 0; j < NEIGHBORHOOD_LEN; j++) { |
298 | 7.29k | 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 | 7.29k | } |
304 | 1.82k | } |
305 | | |
306 | 204 | OPENSSL_free(oldmd->neighborhood_ptr_to_free); |
307 | 204 | OPENSSL_free(oldmd); |
308 | 204 | } |
309 | | |
310 | | static int ossl_ht_flush_internal(HT *h) |
311 | 204 | { |
312 | 204 | struct ht_mutable_data_st *newmd = NULL; |
313 | 204 | struct ht_mutable_data_st *oldmd = NULL; |
314 | | |
315 | 204 | newmd = OPENSSL_zalloc(sizeof(*newmd)); |
316 | 204 | if (newmd == NULL) |
317 | 0 | return 0; |
318 | | |
319 | 204 | newmd->neighborhoods = alloc_new_neighborhood_list(DEFAULT_NEIGH_LEN, |
320 | 204 | &newmd->neighborhood_ptr_to_free); |
321 | 204 | if (newmd->neighborhoods == NULL) { |
322 | 0 | OPENSSL_free(newmd); |
323 | 0 | return 0; |
324 | 0 | } |
325 | | |
326 | 204 | newmd->neighborhood_mask = DEFAULT_NEIGH_LEN - 1; |
327 | | |
328 | | /* Swap the old and new mutable data sets */ |
329 | 204 | if (!h->config.no_rcu) { |
330 | 0 | oldmd = ossl_rcu_deref(&h->md); |
331 | 0 | ossl_rcu_assign_ptr(&h->md, &newmd); |
332 | 204 | } else { |
333 | 204 | oldmd = h->md; |
334 | 204 | h->md = newmd; |
335 | 204 | } |
336 | | |
337 | | /* Set the number of entries to 0 */ |
338 | 204 | h->wpd.value_count = 0; |
339 | 204 | h->wpd.neighborhood_len = DEFAULT_NEIGH_LEN; |
340 | | |
341 | 204 | if (!h->config.no_rcu) { |
342 | 0 | ossl_rcu_call(h->lock, free_oldmd, oldmd); |
343 | 204 | } else { |
344 | 204 | free_oldmd(oldmd); |
345 | 204 | } |
346 | 204 | h->wpd.need_sync = 1; |
347 | | |
348 | 204 | return 1; |
349 | 204 | } |
350 | | |
351 | | int ossl_ht_flush(HT *h) |
352 | 204 | { |
353 | 204 | return ossl_ht_flush_internal(h); |
354 | 204 | } |
355 | | |
356 | | void ossl_ht_free(HT *h) |
357 | 0 | { |
358 | 0 | if (h == NULL) |
359 | 0 | return; |
360 | | |
361 | 0 | ossl_ht_write_lock(h); |
362 | 0 | ossl_ht_flush_internal(h); |
363 | 0 | ossl_ht_write_unlock(h); |
364 | | /* Freeing the lock does a final sync for us */ |
365 | 0 | if (!h->config.no_rcu) { |
366 | 0 | CRYPTO_THREAD_lock_free(h->atomic_lock); |
367 | 0 | ossl_rcu_lock_free(h->lock); |
368 | 0 | } |
369 | 0 | OPENSSL_free(h->md->neighborhood_ptr_to_free); |
370 | 0 | OPENSSL_free(h->md); |
371 | 0 | OPENSSL_free(h); |
372 | 0 | return; |
373 | 0 | } |
374 | | |
375 | | size_t ossl_ht_count(HT *h) |
376 | 43 | { |
377 | 43 | size_t count; |
378 | | |
379 | 43 | count = h->wpd.value_count; |
380 | 43 | return count; |
381 | 43 | } |
382 | | |
383 | | void ossl_ht_foreach_until(HT *h, int (*cb)(HT_VALUE *obj, void *arg), |
384 | | void *arg) |
385 | 0 | { |
386 | 0 | size_t i, j; |
387 | 0 | struct ht_mutable_data_st *md; |
388 | |
|
389 | 0 | md = ossl_rcu_deref(&h->md); |
390 | 0 | for (i = 0; i < md->neighborhood_mask + 1; i++) { |
391 | 0 | PREFETCH_NEIGHBORHOOD(md->neighborhoods[i + 1]); |
392 | 0 | for (j = 0; j < NEIGHBORHOOD_LEN; j++) { |
393 | 0 | if (md->neighborhoods[i].entries[j].value != NULL) { |
394 | 0 | if (!cb((HT_VALUE *)md->neighborhoods[i].entries[j].value, arg)) |
395 | 0 | goto out; |
396 | 0 | } |
397 | 0 | } |
398 | 0 | } |
399 | 0 | out: |
400 | 0 | return; |
401 | 0 | } |
402 | | |
403 | | HT_VALUE_LIST *ossl_ht_filter(HT *h, size_t max_len, |
404 | | int (*filter)(HT_VALUE *obj, void *arg), |
405 | | void *arg) |
406 | 0 | { |
407 | 0 | struct ht_mutable_data_st *md; |
408 | 0 | HT_VALUE_LIST *list = OPENSSL_zalloc(sizeof(HT_VALUE_LIST) |
409 | 0 | + (sizeof(HT_VALUE *) * max_len)); |
410 | 0 | size_t i, j; |
411 | 0 | struct ht_internal_value_st *v; |
412 | |
|
413 | 0 | if (list == NULL) |
414 | 0 | return NULL; |
415 | | |
416 | | /* |
417 | | * The list array lives just beyond the end of |
418 | | * the struct |
419 | | */ |
420 | 0 | list->list = (HT_VALUE **)(list + 1); |
421 | |
|
422 | 0 | md = ossl_rcu_deref(&h->md); |
423 | 0 | for (i = 0; i < md->neighborhood_mask + 1; i++) { |
424 | 0 | PREFETCH_NEIGHBORHOOD(md->neighborhoods[i + 1]); |
425 | 0 | for (j = 0; j < NEIGHBORHOOD_LEN; j++) { |
426 | 0 | v = md->neighborhoods[i].entries[j].value; |
427 | 0 | if (v != NULL && filter((HT_VALUE *)v, arg)) { |
428 | 0 | list->list[list->list_len++] = (HT_VALUE *)v; |
429 | 0 | if (list->list_len == max_len) |
430 | 0 | goto out; |
431 | 0 | } |
432 | 0 | } |
433 | 0 | } |
434 | 0 | out: |
435 | 0 | return list; |
436 | 0 | } |
437 | | |
438 | | void ossl_ht_value_list_free(HT_VALUE_LIST *list) |
439 | 0 | { |
440 | 0 | OPENSSL_free(list); |
441 | 0 | } |
442 | | |
443 | | static int compare_hash(uint64_t hash1, uint64_t hash2) |
444 | 3.56M | { |
445 | 3.56M | return (hash1 == hash2); |
446 | 3.56M | } |
447 | | |
448 | | static void free_old_neigh_table(void *arg) |
449 | 1 | { |
450 | 1 | struct ht_mutable_data_st *oldmd = arg; |
451 | | |
452 | 1 | OPENSSL_free(oldmd->neighborhood_ptr_to_free); |
453 | 1 | OPENSSL_free(oldmd); |
454 | 1 | } |
455 | | |
456 | | /* |
457 | | * Increase hash table bucket list |
458 | | * must be called with write_lock held |
459 | | */ |
460 | | static int grow_hashtable(HT *h, size_t oldsize) |
461 | 1 | { |
462 | 1 | struct ht_mutable_data_st *newmd; |
463 | 1 | struct ht_mutable_data_st *oldmd = ossl_rcu_deref(&h->md); |
464 | 1 | int rc = 0; |
465 | 1 | uint64_t oldi, oldj, newi, newj; |
466 | 1 | uint64_t oldhash; |
467 | 1 | struct ht_internal_value_st *oldv; |
468 | 1 | int rehashed; |
469 | 1 | size_t newsize = oldsize * 2; |
470 | | |
471 | 1 | if (h->config.lockless_reads) |
472 | 0 | goto out; |
473 | | |
474 | 1 | if ((newmd = OPENSSL_zalloc(sizeof(*newmd))) == NULL) |
475 | 0 | goto out; |
476 | | |
477 | | /* bucket list is always a power of 2 */ |
478 | 1 | newmd->neighborhoods = alloc_new_neighborhood_list(oldsize * 2, |
479 | 1 | &newmd->neighborhood_ptr_to_free); |
480 | 1 | if (newmd->neighborhoods == NULL) |
481 | 0 | goto out_free; |
482 | | |
483 | | /* being a power of 2 makes for easy mask computation */ |
484 | 1 | newmd->neighborhood_mask = (newsize - 1); |
485 | | |
486 | | /* |
487 | | * Now we need to start rehashing entries |
488 | | * Note we don't need to use atomics here as the new |
489 | | * mutable data hasn't been published |
490 | | */ |
491 | 17 | for (oldi = 0; oldi < h->wpd.neighborhood_len; oldi++) { |
492 | 16 | PREFETCH_NEIGHBORHOOD(oldmd->neighborhoods[oldi + 1]); |
493 | 80 | for (oldj = 0; oldj < NEIGHBORHOOD_LEN; oldj++) { |
494 | 64 | oldv = oldmd->neighborhoods[oldi].entries[oldj].value; |
495 | 64 | if (oldv == NULL) |
496 | 23 | continue; |
497 | 41 | oldhash = oldmd->neighborhoods[oldi].entries[oldj].hash; |
498 | 41 | newi = oldhash & newmd->neighborhood_mask; |
499 | 41 | rehashed = 0; |
500 | 61 | for (newj = 0; newj < NEIGHBORHOOD_LEN; newj++) { |
501 | 61 | if (newmd->neighborhoods[newi].entries[newj].value == NULL) { |
502 | 41 | newmd->neighborhoods[newi].entries[newj].value = oldv; |
503 | 41 | newmd->neighborhoods[newi].entries[newj].hash = oldhash; |
504 | 41 | rehashed = 1; |
505 | 41 | break; |
506 | 41 | } |
507 | 61 | } |
508 | 41 | if (rehashed == 0) { |
509 | | /* we ran out of space in a neighborhood, grow again */ |
510 | 0 | OPENSSL_free(newmd->neighborhoods); |
511 | 0 | OPENSSL_free(newmd); |
512 | 0 | return grow_hashtable(h, newsize); |
513 | 0 | } |
514 | 41 | } |
515 | 16 | } |
516 | | /* |
517 | | * Now that our entries are all hashed into the new bucket list |
518 | | * update our bucket_len and target_max_load |
519 | | */ |
520 | 1 | h->wpd.neighborhood_len = newsize; |
521 | | |
522 | | /* |
523 | | * Now we replace the old mutable data with the new |
524 | | */ |
525 | 1 | if (!h->config.no_rcu) { |
526 | 0 | ossl_rcu_assign_ptr(&h->md, &newmd); |
527 | 0 | ossl_rcu_call(h->lock, free_old_neigh_table, oldmd); |
528 | 0 | h->wpd.need_sync = 1; |
529 | 1 | } else { |
530 | 1 | h->md = newmd; |
531 | 1 | free_old_neigh_table(oldmd); |
532 | 1 | } |
533 | | /* |
534 | | * And we're done |
535 | | */ |
536 | 1 | rc = 1; |
537 | | |
538 | 1 | out: |
539 | 1 | return rc; |
540 | 0 | out_free: |
541 | 0 | OPENSSL_free(newmd->neighborhoods); |
542 | 0 | OPENSSL_free(newmd); |
543 | 0 | goto out; |
544 | 1 | } |
545 | | |
546 | | static void free_old_ht_value(void *arg) |
547 | 0 | { |
548 | 0 | HT_VALUE *h = (HT_VALUE *)arg; |
549 | | |
550 | | /* |
551 | | * Note, this is only called on replacement, |
552 | | * the caller is responsible for freeing the |
553 | | * held data, we just need to free the wrapping |
554 | | * struct here |
555 | | */ |
556 | 0 | OPENSSL_free(h); |
557 | 0 | } |
558 | | |
559 | | static ossl_inline int match_key(HT_KEY *a, HT_KEY *b) |
560 | 1.99M | { |
561 | | /* |
562 | | * keys match if they are both present, the same size |
563 | | * and compare equal in memory |
564 | | */ |
565 | 1.99M | PREFETCH(a->keybuf); |
566 | 1.99M | PREFETCH(b->keybuf); |
567 | 1.99M | if (a->keybuf != NULL && b->keybuf != NULL && a->keysize == b->keysize) |
568 | 1.99M | return !memcmp(a->keybuf, b->keybuf, a->keysize); |
569 | | |
570 | 0 | return 1; |
571 | 1.99M | } |
572 | | |
573 | | static int ossl_ht_insert_locked(HT *h, uint64_t hash, |
574 | | struct ht_internal_value_st *newval, |
575 | | HT_VALUE **olddata) |
576 | 1.40k | { |
577 | 1.40k | struct ht_mutable_data_st *md = h->md; |
578 | 1.40k | uint64_t neigh_idx_start = hash & md->neighborhood_mask; |
579 | 1.40k | uint64_t neigh_idx = neigh_idx_start; |
580 | 1.40k | size_t j; |
581 | 1.40k | uint64_t ihash; |
582 | 1.40k | HT_VALUE *ival; |
583 | 1.40k | size_t empty_idx = SIZE_MAX; |
584 | 1.40k | int lockless_reads = h->config.lockless_reads; |
585 | | |
586 | 1.40k | do { |
587 | 1.40k | PREFETCH_NEIGHBORHOOD(md->neighborhoods[neigh_idx]); |
588 | | |
589 | 2.15k | for (j = 0; j < NEIGHBORHOOD_LEN; j++) { |
590 | 2.06k | if (!h->config.no_rcu) |
591 | 1.72k | ival = ossl_rcu_deref(&md->neighborhoods[neigh_idx].entries[j].value); |
592 | 348 | else |
593 | 348 | ival = (HT_VALUE *)md->neighborhoods[neigh_idx].entries[j].value; |
594 | 2.06k | if (ival == NULL) { |
595 | 1.58k | empty_idx = j; |
596 | | /* lockless_reads implies no deletion, we can break out */ |
597 | 1.58k | if (lockless_reads) |
598 | 1.31k | goto not_found; |
599 | 267 | continue; |
600 | 1.58k | } |
601 | 483 | if (!h->config.no_rcu) { |
602 | 402 | if (!CRYPTO_atomic_load(&md->neighborhoods[neigh_idx].entries[j].hash, |
603 | 402 | &ihash, h->atomic_lock)) |
604 | 0 | return 0; |
605 | 402 | } else { |
606 | 81 | ihash = md->neighborhoods[neigh_idx].entries[j].hash; |
607 | 81 | } |
608 | 483 | if (compare_hash(hash, ihash) && match_key(&newval->value.key, &ival->key)) { |
609 | 0 | if (olddata == NULL) { |
610 | | /* This would insert a duplicate -> fail */ |
611 | 0 | return 0; |
612 | 0 | } |
613 | | /* Do a replacement */ |
614 | 0 | if (!h->config.no_rcu) { |
615 | 0 | if (!CRYPTO_atomic_store(&md->neighborhoods[neigh_idx].entries[j].hash, |
616 | 0 | hash, h->atomic_lock)) |
617 | 0 | return 0; |
618 | 0 | *olddata = (HT_VALUE *)md->neighborhoods[neigh_idx].entries[j].value; |
619 | 0 | ossl_rcu_assign_ptr(&md->neighborhoods[neigh_idx].entries[j].value, |
620 | 0 | &newval); |
621 | 0 | ossl_rcu_call(h->lock, free_old_ht_value, *olddata); |
622 | 0 | } else { |
623 | 0 | md->neighborhoods[neigh_idx].entries[j].hash = hash; |
624 | 0 | *olddata = (HT_VALUE *)md->neighborhoods[neigh_idx].entries[j].value; |
625 | 0 | md->neighborhoods[neigh_idx].entries[j].value = newval; |
626 | 0 | } |
627 | 0 | h->wpd.need_sync = 1; |
628 | 0 | return 1; |
629 | 0 | } |
630 | 483 | } |
631 | 88 | if (!lockless_reads) |
632 | 87 | break; |
633 | | /* Continue search in subsequent neighborhoods */ |
634 | 1 | neigh_idx = (neigh_idx + 1) & md->neighborhood_mask; |
635 | 1 | } while (neigh_idx != neigh_idx_start); |
636 | | |
637 | 1.40k | not_found: |
638 | | /* If we get to here, its just an insert */ |
639 | 1.40k | if (empty_idx == SIZE_MAX) |
640 | 1 | return -1; /* out of space */ |
641 | 1.40k | if (!h->config.no_rcu) { |
642 | 1.31k | if (!CRYPTO_atomic_store(&md->neighborhoods[neigh_idx].entries[empty_idx].hash, |
643 | 1.31k | hash, h->atomic_lock)) |
644 | 0 | return 0; |
645 | 1.31k | ossl_rcu_assign_ptr(&md->neighborhoods[neigh_idx].entries[empty_idx].value, |
646 | 1.31k | &newval); |
647 | 1.31k | } else { |
648 | 86 | md->neighborhoods[neigh_idx].entries[empty_idx].hash = hash; |
649 | 86 | md->neighborhoods[neigh_idx].entries[empty_idx].value = newval; |
650 | 86 | } |
651 | 1.40k | h->wpd.value_count++; |
652 | 1.40k | return 1; |
653 | 1.40k | } |
654 | | |
655 | | static struct ht_internal_value_st *alloc_new_value(HT *h, HT_KEY *key, |
656 | | void *data, |
657 | | uintptr_t *type) |
658 | 1.40k | { |
659 | 1.40k | struct ht_internal_value_st *tmp; |
660 | 1.40k | size_t nvsize = sizeof(*tmp); |
661 | | |
662 | 1.40k | if (h->config.collision_check == 1) |
663 | 1.40k | nvsize += key->keysize; |
664 | | |
665 | 1.40k | tmp = OPENSSL_malloc(nvsize); |
666 | | |
667 | 1.40k | if (tmp == NULL) |
668 | 0 | return NULL; |
669 | | |
670 | 1.40k | tmp->ht = h; |
671 | 1.40k | tmp->value.value = data; |
672 | 1.40k | tmp->value.type_id = type; |
673 | 1.40k | tmp->value.key.keybuf = NULL; |
674 | 1.40k | if (h->config.collision_check) { |
675 | 1.40k | tmp->value.key.keybuf = (uint8_t *)(tmp + 1); |
676 | 1.40k | tmp->value.key.keysize = key->keysize; |
677 | 1.40k | memcpy(tmp->value.key.keybuf, key->keybuf, key->keysize); |
678 | 1.40k | } |
679 | | |
680 | 1.40k | return tmp; |
681 | 1.40k | } |
682 | | |
683 | | static void free_value(struct ht_internal_value_st *v) |
684 | 0 | { |
685 | 0 | OPENSSL_free(v); |
686 | 0 | } |
687 | | |
688 | | int ossl_ht_insert(HT *h, HT_KEY *key, HT_VALUE *data, HT_VALUE **olddata) |
689 | 1.40k | { |
690 | 1.40k | struct ht_internal_value_st *newval = NULL; |
691 | 1.40k | uint64_t hash; |
692 | 1.40k | int rc = 0; |
693 | 1.40k | int i; |
694 | | |
695 | 1.40k | if (data->value == NULL) |
696 | 0 | goto out; |
697 | | |
698 | 1.40k | rc = -1; |
699 | 1.40k | newval = alloc_new_value(h, key, data->value, data->type_id); |
700 | 1.40k | if (newval == NULL) |
701 | 0 | goto out; |
702 | | |
703 | 1.40k | if (key->cached_hash) |
704 | 0 | hash = key->cached_hash; |
705 | 1.40k | else |
706 | 1.40k | hash = key->cached_hash = newval->value.key.cached_hash = h->config.ht_hash_fn(key); |
707 | | |
708 | | /* |
709 | | * we have to take our lock here to prevent other changes |
710 | | * to the bucket list |
711 | | */ |
712 | 1.40k | for (i = 0; |
713 | 1.40k | (rc = ossl_ht_insert_locked(h, hash, newval, olddata)) == -1 |
714 | 1 | && i < 4; |
715 | 1.40k | ++i) |
716 | 1 | if (!grow_hashtable(h, h->wpd.neighborhood_len)) { |
717 | 0 | rc = -1; |
718 | 0 | break; |
719 | 0 | } |
720 | | |
721 | 1.40k | if (rc <= 0) |
722 | 0 | free_value(newval); |
723 | | |
724 | 1.40k | out: |
725 | 1.40k | return rc; |
726 | 1.40k | } |
727 | | |
728 | | HT_VALUE *ossl_ht_get(HT *h, HT_KEY *key) |
729 | 1.99M | { |
730 | 1.99M | struct ht_mutable_data_st *md; |
731 | 1.99M | uint64_t hash; |
732 | 1.99M | uint64_t neigh_idx_start; |
733 | 1.99M | uint64_t neigh_idx; |
734 | 1.99M | struct ht_internal_value_st *ival = NULL; |
735 | 1.99M | size_t j; |
736 | 1.99M | uint64_t ehash; |
737 | 1.99M | int lockless_reads = h->config.lockless_reads; |
738 | | |
739 | 1.99M | if (key->cached_hash) |
740 | 0 | hash = key->cached_hash; |
741 | 1.99M | else |
742 | 1.99M | hash = key->cached_hash = h->config.ht_hash_fn(key); |
743 | | |
744 | 1.99M | if (!h->config.no_rcu) |
745 | 1.00M | md = ossl_rcu_deref(&h->md); |
746 | 994k | else |
747 | 994k | md = h->md; |
748 | 1.99M | neigh_idx = neigh_idx_start = hash & md->neighborhood_mask; |
749 | 1.99M | do { |
750 | 1.99M | PREFETCH_NEIGHBORHOOD(md->neighborhoods[neigh_idx]); |
751 | 4.37M | for (j = 0; j < NEIGHBORHOOD_LEN; j++) { |
752 | 4.37M | if (!h->config.no_rcu) |
753 | 2.52M | ival = ossl_rcu_deref(&md->neighborhoods[neigh_idx].entries[j].value); |
754 | 1.84M | else |
755 | 1.84M | ival = md->neighborhoods[neigh_idx].entries[j].value; |
756 | 4.37M | if (ival == NULL) { |
757 | 807k | if (lockless_reads) |
758 | | /* lockless_reads implies no deletion, we can break out */ |
759 | 3.42k | return NULL; |
760 | 804k | continue; |
761 | 807k | } |
762 | 3.56M | if (!h->config.no_rcu) { |
763 | 2.52M | if (!CRYPTO_atomic_load(&md->neighborhoods[neigh_idx].entries[j].hash, |
764 | 2.52M | &ehash, h->atomic_lock)) |
765 | 0 | return NULL; |
766 | 2.52M | } else { |
767 | 1.03M | ehash = md->neighborhoods[neigh_idx].entries[j].hash; |
768 | 1.03M | } |
769 | 3.56M | if (compare_hash(hash, ehash) && match_key(&ival->value.key, key)) |
770 | 1.99M | return (HT_VALUE *)ival; |
771 | 3.56M | } |
772 | 44 | if (!lockless_reads) |
773 | 39 | break; |
774 | | /* Continue search in subsequent neighborhoods */ |
775 | 5 | neigh_idx = (neigh_idx + 1) & md->neighborhood_mask; |
776 | 5 | } while (neigh_idx != neigh_idx_start); |
777 | | |
778 | 39 | return NULL; |
779 | 1.99M | } |
780 | | |
781 | | static void free_old_entry(void *arg) |
782 | 0 | { |
783 | 0 | struct ht_internal_value_st *v = arg; |
784 | |
|
785 | 0 | v->ht->config.ht_free_fn((HT_VALUE *)v); |
786 | 0 | free_value(v); |
787 | 0 | } |
788 | | |
789 | | int ossl_ht_delete(HT *h, HT_KEY *key) |
790 | 0 | { |
791 | 0 | uint64_t hash; |
792 | 0 | uint64_t neigh_idx; |
793 | 0 | size_t j; |
794 | 0 | struct ht_internal_value_st *v = NULL; |
795 | 0 | HT_VALUE *nv = NULL; |
796 | 0 | int rc = 0; |
797 | |
|
798 | 0 | if (h->config.lockless_reads) |
799 | 0 | return 0; |
800 | | |
801 | 0 | if (key->cached_hash) |
802 | 0 | hash = key->cached_hash; |
803 | 0 | else |
804 | 0 | hash = key->cached_hash = h->config.ht_hash_fn(key); |
805 | |
|
806 | 0 | neigh_idx = hash & h->md->neighborhood_mask; |
807 | 0 | PREFETCH_NEIGHBORHOOD(h->md->neighborhoods[neigh_idx]); |
808 | 0 | for (j = 0; j < NEIGHBORHOOD_LEN; j++) { |
809 | 0 | v = (struct ht_internal_value_st *)h->md->neighborhoods[neigh_idx].entries[j].value; |
810 | 0 | if (v == NULL) |
811 | 0 | continue; |
812 | 0 | if (compare_hash(hash, h->md->neighborhoods[neigh_idx].entries[j].hash) |
813 | 0 | && match_key(key, &v->value.key)) { |
814 | 0 | if (!h->config.no_rcu) { |
815 | 0 | if (!CRYPTO_atomic_store(&h->md->neighborhoods[neigh_idx].entries[j].hash, |
816 | 0 | 0, h->atomic_lock)) |
817 | 0 | break; |
818 | 0 | ossl_rcu_assign_ptr(&h->md->neighborhoods[neigh_idx].entries[j].value, &nv); |
819 | 0 | } else { |
820 | 0 | h->md->neighborhoods[neigh_idx].entries[j].hash = 0; |
821 | 0 | h->md->neighborhoods[neigh_idx].entries[j].value = NULL; |
822 | 0 | } |
823 | 0 | h->wpd.value_count--; |
824 | 0 | rc = 1; |
825 | 0 | break; |
826 | 0 | } |
827 | 0 | } |
828 | 0 | if (rc == 1) { |
829 | 0 | if (!h->config.no_rcu) |
830 | 0 | ossl_rcu_call(h->lock, free_old_entry, v); |
831 | 0 | else |
832 | 0 | free_old_entry(v); |
833 | 0 | h->wpd.need_sync = 1; |
834 | 0 | } |
835 | |
|
836 | 0 | return rc; |
837 | 0 | } |
838 | | |
839 | | HT_VALUE *ossl_ht_deref_value(HT *h, HT_VALUE **val) |
840 | 993k | { |
841 | 993k | HT_VALUE *v; |
842 | | |
843 | 993k | if (!h->config.no_rcu) |
844 | 0 | v = ossl_rcu_deref(val); |
845 | 993k | else |
846 | 993k | v = *val; |
847 | | |
848 | 993k | return v; |
849 | 993k | } |
850 | | |
851 | | void *ossl_ht_inner_value(HT *h, HT_VALUE *v) |
852 | 0 | { |
853 | 0 | void *inner; |
854 | |
|
855 | 0 | if (!h->config.no_rcu) { |
856 | 0 | inner = v->value; |
857 | 0 | } else { |
858 | 0 | inner = v->value; |
859 | 0 | OPENSSL_free(v); |
860 | 0 | } |
861 | |
|
862 | 0 | return inner; |
863 | 0 | } |