/src/openssl36/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 | | #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 | 27.2M | #define PREFETCH_NEIGHBORHOOD(x) __builtin_prefetch(x.entries) |
84 | 72.4M | #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 | 36.3M | #define CACHE_LINE_BYTES 64 |
103 | | #define CACHE_LINE_ALIGNMENT CACHE_LINE_BYTES |
104 | | |
105 | 36.3M | #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 | 34.2k | { |
157 | 34.2k | struct ht_neighborhood_st *ret; |
158 | | |
159 | 34.2k | ret = OPENSSL_aligned_alloc_array(len, sizeof(struct ht_neighborhood_st), |
160 | 34.2k | CACHE_LINE_BYTES, freeptr); |
161 | | |
162 | | /* fall back to regular malloc */ |
163 | 34.2k | if (ret == NULL) { |
164 | 0 | ret = *freeptr = OPENSSL_malloc_array(len, sizeof(struct ht_neighborhood_st)); |
165 | 0 | if (ret == NULL) |
166 | 0 | return NULL; |
167 | 0 | } |
168 | 34.2k | memset(ret, 0, sizeof(struct ht_neighborhood_st) * len); |
169 | 34.2k | return ret; |
170 | 34.2k | } |
171 | | |
172 | | static void internal_free_nop(HT_VALUE *v) |
173 | 24.8k | { |
174 | 24.8k | return; |
175 | 24.8k | } |
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 | | int ossl_ht_read_lock(HT *htable) |
237 | 17 | { |
238 | 17 | return ossl_rcu_read_lock(htable->lock); |
239 | 17 | } |
240 | | |
241 | | void ossl_ht_read_unlock(HT *htable) |
242 | 49 | { |
243 | 49 | ossl_rcu_read_unlock(htable->lock); |
244 | 49 | } |
245 | | |
246 | | void ossl_ht_write_lock(HT *htable) |
247 | 245 | { |
248 | 245 | ossl_rcu_write_lock(htable->lock); |
249 | 245 | htable->wpd.need_sync = 0; |
250 | 245 | } |
251 | | |
252 | | void ossl_ht_write_unlock(HT *htable) |
253 | 245 | { |
254 | 245 | int need_sync = htable->wpd.need_sync; |
255 | | |
256 | 245 | htable->wpd.need_sync = 0; |
257 | 245 | ossl_rcu_write_unlock(htable->lock); |
258 | 245 | if (need_sync) |
259 | 172 | ossl_synchronize_rcu(htable->lock); |
260 | 245 | } |
261 | | |
262 | | static void free_oldmd(void *arg) |
263 | 32.5k | { |
264 | 32.5k | struct ht_mutable_data_st *oldmd = arg; |
265 | 32.5k | size_t i, j; |
266 | 32.5k | size_t neighborhood_len = (size_t)oldmd->neighborhood_mask + 1; |
267 | 32.5k | struct ht_internal_value_st *v; |
268 | | |
269 | 368k | for (i = 0; i < neighborhood_len; i++) { |
270 | 335k | PREFETCH_NEIGHBORHOOD(oldmd->neighborhoods[i + 1]); |
271 | 1.67M | for (j = 0; j < NEIGHBORHOOD_LEN; j++) { |
272 | 1.34M | 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.34M | } |
278 | 335k | } |
279 | | |
280 | 32.5k | OPENSSL_free(oldmd->neighborhood_ptr_to_free); |
281 | 32.5k | OPENSSL_free(oldmd); |
282 | 32.5k | } |
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 | 157 | CRYPTO_RCU_CB_ITEM *cbi = NULL; |
289 | | |
290 | 157 | newmd = OPENSSL_zalloc(sizeof(*newmd)); |
291 | 157 | if (newmd == NULL) |
292 | 0 | return 0; |
293 | | |
294 | 157 | newmd->neighborhoods = alloc_new_neighborhood_list(DEFAULT_NEIGH_LEN, |
295 | 157 | &newmd->neighborhood_ptr_to_free); |
296 | 157 | if (newmd->neighborhoods == NULL) { |
297 | 0 | OPENSSL_free(newmd); |
298 | 0 | return 0; |
299 | 0 | } |
300 | | |
301 | 157 | newmd->neighborhood_mask = DEFAULT_NEIGH_LEN - 1; |
302 | | |
303 | 157 | cbi = ossl_rcu_cb_item_new(); |
304 | 157 | if (cbi == NULL) { |
305 | 0 | OPENSSL_free(newmd->neighborhood_ptr_to_free); |
306 | 0 | OPENSSL_free(newmd); |
307 | 0 | return 0; |
308 | 0 | } |
309 | | |
310 | | /* Swap the old and new mutable data sets */ |
311 | 157 | oldmd = ossl_rcu_deref(&h->md); |
312 | 157 | ossl_rcu_assign_ptr(&h->md, &newmd); |
313 | | |
314 | | /* Set the number of entries to 0 */ |
315 | 157 | h->wpd.value_count = 0; |
316 | 157 | h->wpd.neighborhood_len = DEFAULT_NEIGH_LEN; |
317 | | |
318 | 157 | ossl_rcu_call(h->lock, cbi, free_oldmd, oldmd); |
319 | 157 | h->wpd.need_sync = 1; |
320 | | |
321 | 157 | return 1; |
322 | 157 | } |
323 | | |
324 | | int ossl_ht_flush(HT *h) |
325 | 420 | { |
326 | 420 | return ossl_ht_flush_internal(h); |
327 | 420 | } |
328 | | |
329 | | void ossl_ht_free(HT *h) |
330 | 154 | { |
331 | 154 | int flush_ok; |
332 | | |
333 | 154 | if (h == NULL) |
334 | 0 | return; |
335 | | |
336 | 154 | ossl_ht_write_lock(h); |
337 | 154 | flush_ok = ossl_ht_flush_internal(h); |
338 | 154 | ossl_ht_write_unlock(h); |
339 | | /* Freeing the lock does a final sync for us */ |
340 | 154 | CRYPTO_THREAD_lock_free(h->atomic_lock); |
341 | 154 | ossl_rcu_lock_free(h->lock); |
342 | 154 | if (flush_ok) { |
343 | 154 | OPENSSL_free(h->md->neighborhood_ptr_to_free); |
344 | 154 | OPENSSL_free(h->md); |
345 | 154 | } else { |
346 | 0 | free_oldmd(h->md); |
347 | 0 | } |
348 | 154 | OPENSSL_free(h); |
349 | 154 | return; |
350 | 154 | } |
351 | | |
352 | | size_t ossl_ht_count(HT *h) |
353 | 1.97k | { |
354 | 1.97k | size_t count; |
355 | | |
356 | 1.97k | count = h->wpd.value_count; |
357 | 1.97k | return count; |
358 | 1.97k | } |
359 | | |
360 | | void ossl_ht_foreach_until(HT *h, int (*cb)(HT_VALUE *obj, void *arg), |
361 | | void *arg) |
362 | 100 | { |
363 | 100 | size_t i, j; |
364 | 100 | struct ht_mutable_data_st *md; |
365 | | |
366 | 100 | md = ossl_rcu_deref(&h->md); |
367 | 37.9k | for (i = 0; i < md->neighborhood_mask + 1; i++) { |
368 | 37.8k | PREFETCH_NEIGHBORHOOD(md->neighborhoods[i + 1]); |
369 | 189k | for (j = 0; j < NEIGHBORHOOD_LEN; j++) { |
370 | 151k | if (md->neighborhoods[i].entries[j].value != NULL) { |
371 | 650 | if (!cb((HT_VALUE *)md->neighborhoods[i].entries[j].value, arg)) |
372 | 9 | goto out; |
373 | 650 | } |
374 | 151k | } |
375 | 37.8k | } |
376 | 100 | out: |
377 | 100 | return; |
378 | 100 | } |
379 | | |
380 | | HT_VALUE_LIST *ossl_ht_filter(HT *h, size_t max_len, |
381 | | int (*filter)(HT_VALUE *obj, void *arg), |
382 | | void *arg) |
383 | 78 | { |
384 | 78 | struct ht_mutable_data_st *md; |
385 | 78 | HT_VALUE_LIST *list = OPENSSL_zalloc(sizeof(HT_VALUE_LIST) |
386 | 78 | + (sizeof(HT_VALUE *) * max_len)); |
387 | 78 | size_t i, j; |
388 | 78 | struct ht_internal_value_st *v; |
389 | | |
390 | 78 | if (list == NULL) |
391 | 0 | return NULL; |
392 | | |
393 | | /* |
394 | | * The list array lives just beyond the end of |
395 | | * the struct |
396 | | */ |
397 | 78 | list->list = (HT_VALUE **)(list + 1); |
398 | | |
399 | 78 | md = ossl_rcu_deref(&h->md); |
400 | 21.8k | for (i = 0; i < md->neighborhood_mask + 1; i++) { |
401 | 21.7k | PREFETCH_NEIGHBORHOOD(md->neighborhoods[i + 1]); |
402 | 108k | for (j = 0; j < NEIGHBORHOOD_LEN; j++) { |
403 | 86.9k | v = md->neighborhoods[i].entries[j].value; |
404 | 86.9k | if (v != NULL && filter((HT_VALUE *)v, arg)) { |
405 | 5 | list->list[list->list_len++] = (HT_VALUE *)v; |
406 | 5 | if (list->list_len == max_len) |
407 | 5 | goto out; |
408 | 5 | } |
409 | 86.9k | } |
410 | 21.7k | } |
411 | 78 | out: |
412 | 78 | return list; |
413 | 78 | } |
414 | | |
415 | | void ossl_ht_value_list_free(HT_VALUE_LIST *list) |
416 | 78 | { |
417 | 78 | OPENSSL_free(list); |
418 | 78 | } |
419 | | |
420 | | static int compare_hash(uint64_t hash1, uint64_t hash2) |
421 | 49.9M | { |
422 | 49.9M | return (hash1 == hash2); |
423 | 49.9M | } |
424 | | |
425 | | static void free_old_neigh_table(void *arg) |
426 | 100 | { |
427 | 100 | struct ht_mutable_data_st *oldmd = arg; |
428 | | |
429 | 100 | OPENSSL_free(oldmd->neighborhood_ptr_to_free); |
430 | 100 | OPENSSL_free(oldmd); |
431 | 100 | } |
432 | | |
433 | | /* |
434 | | * Increase hash table bucket list |
435 | | * must be called with write_lock held |
436 | | */ |
437 | | static int grow_hashtable(HT *h, size_t oldsize) |
438 | 18 | { |
439 | 18 | struct ht_mutable_data_st *newmd; |
440 | 18 | struct ht_mutable_data_st *oldmd = ossl_rcu_deref(&h->md); |
441 | 18 | CRYPTO_RCU_CB_ITEM *cbi = NULL; |
442 | 18 | int rc = 0; |
443 | 18 | uint64_t oldi, oldj, newi, newj; |
444 | 18 | uint64_t oldhash; |
445 | 18 | struct ht_internal_value_st *oldv; |
446 | 18 | int rehashed; |
447 | 18 | size_t newsize = oldsize * 2; |
448 | | |
449 | 18 | if (h->config.lockless_reads) |
450 | 0 | goto out; |
451 | | |
452 | 18 | if ((newmd = OPENSSL_zalloc(sizeof(*newmd))) == NULL) |
453 | 0 | goto out; |
454 | | |
455 | | /* bucket list is always a power of 2 */ |
456 | 18 | newmd->neighborhoods = alloc_new_neighborhood_list(oldsize * 2, |
457 | 18 | &newmd->neighborhood_ptr_to_free); |
458 | 18 | if (newmd->neighborhoods == NULL) |
459 | 0 | goto out_free; |
460 | | |
461 | | /* being a power of 2 makes for easy mask computation */ |
462 | 18 | newmd->neighborhood_mask = (newsize - 1); |
463 | | |
464 | | /* |
465 | | * Now we need to start rehashing entries |
466 | | * Note we don't need to use atomics here as the new |
467 | | * mutable data hasn't been published |
468 | | */ |
469 | 1.87k | for (oldi = 0; oldi < h->wpd.neighborhood_len; oldi++) { |
470 | 1.85k | PREFETCH_NEIGHBORHOOD(oldmd->neighborhoods[oldi + 1]); |
471 | 9.28k | for (oldj = 0; oldj < NEIGHBORHOOD_LEN; oldj++) { |
472 | 7.42k | oldv = oldmd->neighborhoods[oldi].entries[oldj].value; |
473 | 7.42k | if (oldv == NULL) |
474 | 7.33k | continue; |
475 | 89 | oldhash = oldmd->neighborhoods[oldi].entries[oldj].hash; |
476 | 89 | newi = oldhash & newmd->neighborhood_mask; |
477 | 89 | rehashed = 0; |
478 | 178 | for (newj = 0; newj < NEIGHBORHOOD_LEN; newj++) { |
479 | 178 | if (newmd->neighborhoods[newi].entries[newj].value == NULL) { |
480 | 89 | newmd->neighborhoods[newi].entries[newj].value = oldv; |
481 | 89 | newmd->neighborhoods[newi].entries[newj].hash = oldhash; |
482 | 89 | rehashed = 1; |
483 | 89 | break; |
484 | 89 | } |
485 | 178 | } |
486 | 89 | if (rehashed == 0) { |
487 | | /* we ran out of space in a neighborhood, grow again */ |
488 | 0 | OPENSSL_free(newmd->neighborhood_ptr_to_free); |
489 | 0 | OPENSSL_free(newmd); |
490 | 0 | return grow_hashtable(h, newsize); |
491 | 0 | } |
492 | 89 | } |
493 | 1.85k | } |
494 | | |
495 | | /* |
496 | | * Pre allocate the rcu callback item before assigning the newmd. |
497 | | */ |
498 | 18 | cbi = ossl_rcu_cb_item_new(); |
499 | 18 | if (cbi == NULL) |
500 | 0 | goto out_free; |
501 | | |
502 | | /* |
503 | | * Now that our entries are all hashed into the new bucket list |
504 | | * update our bucket_len and target_max_load |
505 | | */ |
506 | 18 | h->wpd.neighborhood_len = newsize; |
507 | | |
508 | | /* |
509 | | * Now we replace the old mutable data with the new |
510 | | */ |
511 | 18 | ossl_rcu_assign_ptr(&h->md, &newmd); |
512 | 18 | ossl_rcu_call(h->lock, cbi, free_old_neigh_table, oldmd); |
513 | 18 | h->wpd.need_sync = 1; |
514 | | /* |
515 | | * And we're done |
516 | | */ |
517 | 18 | rc = 1; |
518 | | |
519 | 18 | out: |
520 | 18 | return rc; |
521 | 0 | out_free: |
522 | 0 | OPENSSL_free(newmd->neighborhood_ptr_to_free); |
523 | 0 | OPENSSL_free(newmd); |
524 | 0 | goto out; |
525 | 18 | } |
526 | | |
527 | | static void free_old_ht_value(void *arg) |
528 | 3 | { |
529 | 3 | HT_VALUE *h = (HT_VALUE *)arg; |
530 | | |
531 | | /* |
532 | | * Note, this is only called on replacement, |
533 | | * the caller is responsible for freeing the |
534 | | * held data, we just need to free the wrapping |
535 | | * struct here |
536 | | */ |
537 | 3 | OPENSSL_free(h); |
538 | 3 | } |
539 | | |
540 | | static ossl_inline int match_key(HT_KEY *a, HT_KEY *b) |
541 | 36.2M | { |
542 | | /* |
543 | | * keys match if they are both present, the same size |
544 | | * and compare equal in memory |
545 | | */ |
546 | 36.2M | PREFETCH(a->keybuf); |
547 | 36.2M | PREFETCH(b->keybuf); |
548 | 36.2M | if (a->keybuf != NULL && b->keybuf != NULL && a->keysize == b->keysize) |
549 | 36.2M | return !memcmp(a->keybuf, b->keybuf, a->keysize); |
550 | | |
551 | 4.27k | return 1; |
552 | 36.2M | } |
553 | | |
554 | | static int ossl_ht_insert_locked(HT *h, uint64_t hash, |
555 | | struct ht_internal_value_st *newval, |
556 | | HT_VALUE **olddata) |
557 | 28.4k | { |
558 | 28.4k | struct ht_mutable_data_st *md = h->md; |
559 | 28.4k | uint64_t neigh_idx_start = hash & md->neighborhood_mask; |
560 | 28.4k | uint64_t neigh_idx = neigh_idx_start; |
561 | 28.4k | size_t j; |
562 | 28.4k | uint64_t ihash; |
563 | 28.4k | HT_VALUE *ival; |
564 | 28.4k | size_t empty_idx = SIZE_MAX; |
565 | 28.4k | int lockless_reads = h->config.lockless_reads; |
566 | 28.4k | CRYPTO_RCU_CB_ITEM *cbi; |
567 | | |
568 | 28.7k | do { |
569 | 28.7k | PREFETCH_NEIGHBORHOOD(md->neighborhoods[neigh_idx]); |
570 | | |
571 | 43.4k | for (j = 0; j < NEIGHBORHOOD_LEN; j++) { |
572 | 43.1k | ival = ossl_rcu_deref(&md->neighborhoods[neigh_idx].entries[j].value); |
573 | 43.1k | if (ival == NULL) { |
574 | 28.5k | empty_idx = j; |
575 | | /* lockless_reads implies no deletion, we can break out */ |
576 | 28.5k | if (lockless_reads) |
577 | 28.3k | goto not_found; |
578 | 141 | continue; |
579 | 28.5k | } |
580 | 14.5k | if (!CRYPTO_atomic_load(&md->neighborhoods[neigh_idx].entries[j].hash, |
581 | 14.5k | &ihash, h->atomic_lock)) |
582 | 0 | return 0; |
583 | 14.5k | if (compare_hash(hash, ihash) && match_key(&newval->value.key, &ival->key)) { |
584 | 7 | if (olddata == NULL) { |
585 | | /* This would insert a duplicate -> fail */ |
586 | 5 | return 0; |
587 | 5 | } |
588 | | /* Do a replacement */ |
589 | 2 | cbi = ossl_rcu_cb_item_new(); |
590 | 2 | if (cbi == NULL) |
591 | 0 | return 0; |
592 | 2 | if (!CRYPTO_atomic_store(&md->neighborhoods[neigh_idx].entries[j].hash, |
593 | 2 | hash, h->atomic_lock)) |
594 | 0 | return 0; |
595 | 2 | *olddata = (HT_VALUE *)md->neighborhoods[neigh_idx].entries[j].value; |
596 | 2 | ossl_rcu_assign_ptr(&md->neighborhoods[neigh_idx].entries[j].value, |
597 | 2 | &newval); |
598 | 2 | ossl_rcu_call(h->lock, cbi, free_old_ht_value, *olddata); |
599 | 2 | h->wpd.need_sync = 1; |
600 | 2 | return 1; |
601 | 2 | } |
602 | 14.5k | } |
603 | 331 | if (!lockless_reads) |
604 | 64 | break; |
605 | | /* Continue search in subsequent neighborhoods */ |
606 | 267 | neigh_idx = (neigh_idx + 1) & md->neighborhood_mask; |
607 | 267 | } while (neigh_idx != neigh_idx_start); |
608 | | |
609 | 28.4k | not_found: |
610 | | /* If we get to here, its just an insert */ |
611 | 28.4k | if (empty_idx == SIZE_MAX) |
612 | 18 | return -1; /* out of space */ |
613 | 28.4k | if (!CRYPTO_atomic_store(&md->neighborhoods[neigh_idx].entries[empty_idx].hash, |
614 | 28.4k | hash, h->atomic_lock)) |
615 | 0 | return 0; |
616 | 28.4k | h->wpd.value_count++; |
617 | 28.4k | ossl_rcu_assign_ptr(&md->neighborhoods[neigh_idx].entries[empty_idx].value, |
618 | 28.4k | &newval); |
619 | 28.4k | return 1; |
620 | 28.4k | } |
621 | | |
622 | | static struct ht_internal_value_st *alloc_new_value(HT *h, HT_KEY *key, |
623 | | void *data, |
624 | | uintptr_t *type) |
625 | 44.5k | { |
626 | 44.5k | struct ht_internal_value_st *tmp; |
627 | 44.5k | size_t nvsize = sizeof(*tmp); |
628 | | |
629 | 44.5k | if (h->config.collision_check == 1) |
630 | 43.1k | nvsize += key->keysize; |
631 | | |
632 | 44.5k | tmp = OPENSSL_malloc(nvsize); |
633 | | |
634 | 44.5k | if (tmp == NULL) |
635 | 0 | return NULL; |
636 | | |
637 | 44.5k | tmp->ht = h; |
638 | 44.5k | tmp->value.value = data; |
639 | 44.5k | tmp->value.type_id = type; |
640 | 44.5k | tmp->value.key.keybuf = NULL; |
641 | 44.5k | if (h->config.collision_check) { |
642 | 43.1k | tmp->value.key.keybuf = (uint8_t *)(tmp + 1); |
643 | 43.1k | tmp->value.key.keysize = key->keysize; |
644 | 43.1k | memcpy(tmp->value.key.keybuf, key->keybuf, key->keysize); |
645 | 43.1k | } |
646 | | |
647 | 44.5k | return tmp; |
648 | 44.5k | } |
649 | | |
650 | | static void free_value(struct ht_internal_value_st *v) |
651 | 28.3k | { |
652 | 28.3k | OPENSSL_free(v); |
653 | 28.3k | } |
654 | | |
655 | | int ossl_ht_insert(HT *h, HT_KEY *key, HT_VALUE *data, HT_VALUE **olddata) |
656 | 28.4k | { |
657 | 28.4k | struct ht_internal_value_st *newval = NULL; |
658 | 28.4k | uint64_t hash; |
659 | 28.4k | int rc = 0; |
660 | 28.4k | int i; |
661 | | |
662 | 28.4k | if (data->value == NULL) |
663 | 0 | goto out; |
664 | | |
665 | 28.4k | rc = -1; |
666 | 28.4k | newval = alloc_new_value(h, key, data->value, data->type_id); |
667 | 28.4k | if (newval == NULL) |
668 | 0 | goto out; |
669 | | |
670 | | /* |
671 | | * we have to take our lock here to prevent other changes |
672 | | * to the bucket list |
673 | | */ |
674 | 28.4k | hash = h->config.ht_hash_fn(key->keybuf, key->keysize); |
675 | | |
676 | 28.4k | for (i = 0; |
677 | 28.4k | (rc = ossl_ht_insert_locked(h, hash, newval, olddata)) == -1 |
678 | 18 | && i <= (int)NEIGHBORHOOD_LEN; |
679 | 28.4k | ++i) |
680 | 18 | if (!grow_hashtable(h, h->wpd.neighborhood_len)) { |
681 | 0 | rc = -1; |
682 | 0 | break; |
683 | 0 | } |
684 | | |
685 | 28.4k | if (rc <= 0) |
686 | 5 | free_value(newval); |
687 | | |
688 | 28.4k | out: |
689 | 28.4k | return rc; |
690 | 28.4k | } |
691 | | |
692 | | HT_VALUE *ossl_ht_get(HT *h, HT_KEY *key) |
693 | 26.8M | { |
694 | 26.8M | struct ht_mutable_data_st *md; |
695 | 26.8M | uint64_t hash; |
696 | 26.8M | uint64_t neigh_idx_start; |
697 | 26.8M | uint64_t neigh_idx; |
698 | 26.8M | struct ht_internal_value_st *ival = NULL; |
699 | 26.8M | size_t j; |
700 | 26.8M | uint64_t ehash; |
701 | 26.8M | int lockless_reads = h->config.lockless_reads; |
702 | | |
703 | 26.8M | hash = h->config.ht_hash_fn(key->keybuf, key->keysize); |
704 | | |
705 | 26.8M | md = ossl_rcu_deref(&h->md); |
706 | 26.8M | neigh_idx = neigh_idx_start = hash & md->neighborhood_mask; |
707 | 26.8M | do { |
708 | 26.8M | PREFETCH_NEIGHBORHOOD(md->neighborhoods[neigh_idx]); |
709 | 34.2M | for (j = 0; j < NEIGHBORHOOD_LEN; j++) { |
710 | 34.2M | ival = ossl_rcu_deref(&md->neighborhoods[neigh_idx].entries[j].value); |
711 | 34.2M | if (ival == NULL) { |
712 | 2.43M | if (lockless_reads) |
713 | | /* lockless_reads implies no deletion, we can break out */ |
714 | 2.43M | return NULL; |
715 | 114 | continue; |
716 | 2.43M | } |
717 | 31.8M | if (!CRYPTO_atomic_load(&md->neighborhoods[neigh_idx].entries[j].hash, |
718 | 31.8M | &ehash, h->atomic_lock)) |
719 | 0 | return NULL; |
720 | 31.8M | if (compare_hash(hash, ehash) && match_key(&ival->value.key, key)) |
721 | 24.3M | return (HT_VALUE *)ival; |
722 | 31.8M | } |
723 | 4.84k | if (!lockless_reads) |
724 | 43 | break; |
725 | | /* Continue search in subsequent neighborhoods */ |
726 | 4.79k | neigh_idx = (neigh_idx + 1) & md->neighborhood_mask; |
727 | 4.79k | } while (neigh_idx != neigh_idx_start); |
728 | | |
729 | 43 | return NULL; |
730 | 26.8M | } |
731 | | |
732 | | static void free_old_entry(void *arg) |
733 | 2.13k | { |
734 | 2.13k | struct ht_internal_value_st *v = arg; |
735 | | |
736 | 2.13k | v->ht->config.ht_free_fn((HT_VALUE *)v); |
737 | 2.13k | free_value(v); |
738 | 2.13k | } |
739 | | |
740 | | int ossl_ht_delete(HT *h, HT_KEY *key) |
741 | 35 | { |
742 | 35 | uint64_t hash; |
743 | 35 | uint64_t neigh_idx; |
744 | 35 | size_t j; |
745 | 35 | struct ht_internal_value_st *v = NULL; |
746 | 35 | HT_VALUE *nv = NULL; |
747 | 35 | int rc = 0; |
748 | | |
749 | 35 | if (h->config.lockless_reads) |
750 | 0 | return 0; |
751 | | |
752 | 35 | hash = h->config.ht_hash_fn(key->keybuf, key->keysize); |
753 | | |
754 | 35 | neigh_idx = hash & h->md->neighborhood_mask; |
755 | 35 | PREFETCH_NEIGHBORHOOD(h->md->neighborhoods[neigh_idx]); |
756 | 161 | for (j = 0; j < NEIGHBORHOOD_LEN; j++) { |
757 | 131 | v = (struct ht_internal_value_st *)h->md->neighborhoods[neigh_idx].entries[j].value; |
758 | 131 | if (v == NULL) |
759 | 68 | continue; |
760 | 63 | if (compare_hash(hash, h->md->neighborhoods[neigh_idx].entries[j].hash) |
761 | 5 | && match_key(key, &v->value.key)) { |
762 | 5 | CRYPTO_RCU_CB_ITEM *cbi = ossl_rcu_cb_item_new(); |
763 | 5 | if (cbi == NULL) |
764 | 0 | break; |
765 | 5 | if (!CRYPTO_atomic_store(&h->md->neighborhoods[neigh_idx].entries[j].hash, |
766 | 5 | 0, h->atomic_lock)) |
767 | 0 | break; |
768 | 5 | h->wpd.value_count--; |
769 | 5 | ossl_rcu_assign_ptr(&h->md->neighborhoods[neigh_idx].entries[j].value, |
770 | 5 | &nv); |
771 | 5 | ossl_rcu_call(h->lock, cbi, free_old_entry, v); |
772 | 5 | h->wpd.need_sync = 1; |
773 | 5 | rc = 1; |
774 | 5 | break; |
775 | 5 | } |
776 | 63 | } |
777 | 35 | return rc; |
778 | 35 | } |