#ifdef _KERNEL
#include <sys/cdefs.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/atomic.h>
#include <sys/cprng.h>
#include <sys/hash.h>
#include <sys/kmem.h>
#include <sys/lock.h>
#include <sys/sdt.h>
#include <sys/thmap.h>
#define THMAP_RCSID(a) __KERNEL_RCSID(0, a)
#else
#include <inttypes.h>
#include <limits.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define THMAP_RCSID(a) __RCSID(a)
#define SET_ERROR(e) (e)
#include "thmap.h"
#include "utils.h"
#endif
THMAP_RCSID("$NetBSD: subr_thmap.c,v 1.17 2026/01/04 03:21:04 riastradh Exp $");
#include <crypto/blake2/blake2s.h>
#ifdef _KERNEL
#define ASSERT KASSERT
#define atomic_thread_fence(x) membar_release()
#define atomic_compare_exchange_weak_explicit_32(p, e, n, m1, m2) \
(atomic_cas_32((p), *(e), (n)) == *(e))
#define atomic_compare_exchange_weak_explicit_ptr(p, e, n, m1, m2) \
(atomic_cas_ptr((p), *(void **)(e), (void *)(n)) == *(void **)(e))
#define atomic_exchange_explicit(o, n, m1) atomic_swap_ptr((o), (n))
#define murmurhash3 murmurhash2
#endif
#define HASHVAL_SEEDLEN (16)
#define HASHVAL_BITS (32)
#define HASHVAL_MOD (HASHVAL_BITS - 1)
#define HASHVAL_SHIFT (5)
#define ROOT_BITS (6)
#define ROOT_SIZE (1 << ROOT_BITS)
#define ROOT_MASK (ROOT_SIZE - 1)
#define ROOT_MSBITS (HASHVAL_BITS - ROOT_BITS)
#define LEVEL_BITS (4)
#define LEVEL_SIZE (1 << LEVEL_BITS)
#define LEVEL_MASK (LEVEL_SIZE - 1)
typedef uintptr_t thmap_ptr_t;
typedef uintptr_t atomic_thmap_ptr_t;
#define THMAP_NULL ((thmap_ptr_t)0)
#define THMAP_LEAF_BIT (0x1)
#define THMAP_ALIGNED_P(p) (((uintptr_t)(p) & 3) == 0)
#define THMAP_ALIGN(p) ((uintptr_t)(p) & ~(uintptr_t)3)
#define THMAP_INODE_P(p) (((uintptr_t)(p) & THMAP_LEAF_BIT) == 0)
#define THMAP_GETPTR(th, p) ((void *)((th)->baseptr + (uintptr_t)(p)))
#define THMAP_GETOFF(th, p) ((thmap_ptr_t)((uintptr_t)(p) - (th)->baseptr))
#define THMAP_NODE(th, p) THMAP_GETPTR(th, THMAP_ALIGN(p))
#define NODE_LOCKED (1U << 31)
#define NODE_DELETED (1U << 30)
#define NODE_COUNT(s) ((s) & 0x3fffffff)
typedef struct {
uint32_t state;
thmap_ptr_t parent;
atomic_thmap_ptr_t slots[LEVEL_SIZE];
} thmap_inode_t;
#define THMAP_INODE_LEN sizeof(thmap_inode_t)
typedef struct {
thmap_ptr_t key;
size_t len;
void * val;
} thmap_leaf_t;
typedef struct {
const uint8_t * seed;
unsigned rslot;
unsigned level;
unsigned hashidx;
uint32_t hashval;
} thmap_query_t;
union thmap_align {
void * p;
uint64_t v;
};
typedef struct thmap_gc thmap_gc_t;
struct thmap_gc {
size_t len;
thmap_gc_t * next;
char data[] __aligned(sizeof(union thmap_align));
};
#define THMAP_ROOT_LEN (sizeof(thmap_ptr_t) * ROOT_SIZE)
struct thmap {
uintptr_t baseptr;
atomic_thmap_ptr_t * root;
unsigned flags;
const thmap_ops_t * ops;
thmap_gc_t * gc_list;
uint8_t seed[HASHVAL_SEEDLEN];
};
static void stage_mem_gc(thmap_t *, uintptr_t, size_t);
static uintptr_t
alloc_wrapper(size_t len)
{
return (uintptr_t)kmem_intr_alloc(len, KM_NOSLEEP);
}
static void
free_wrapper(uintptr_t addr, size_t len)
{
kmem_intr_free((void *)addr, len);
}
static const thmap_ops_t thmap_default_ops = {
.alloc = alloc_wrapper,
.free = free_wrapper
};
static uintptr_t
gc_alloc(const thmap_t *thmap, size_t len)
{
const size_t alloclen = offsetof(struct thmap_gc, data[len]);
const uintptr_t gcaddr = thmap->ops->alloc(alloclen);
if (!gcaddr)
return 0;
thmap_gc_t *const gc = THMAP_GETPTR(thmap, gcaddr);
gc->len = len;
return THMAP_GETOFF(thmap, &gc->data[0]);
}
static void
gc_free(const thmap_t *thmap, uintptr_t addr, size_t len)
{
const size_t alloclen = offsetof(struct thmap_gc, data[len]);
char *const ptr = THMAP_GETPTR(thmap, addr);
thmap_gc_t *const gc = container_of(ptr, struct thmap_gc, data[0]);
const uintptr_t gcaddr = THMAP_GETOFF(thmap, gc);
KASSERTMSG(gc->len == len, "thmap=%p ops=%p addr=%p len=%zu"
" gc=%p gc->len=%zu",
thmap, thmap->ops, (void *)addr, len, gc, gc->len);
thmap->ops->free(gcaddr, alloclen);
}
static inline bool __diagused
node_locked_p(thmap_inode_t *node)
{
return (atomic_load_relaxed(&node->state) & NODE_LOCKED) != 0;
}
static void
lock_node(thmap_inode_t *node)
{
unsigned bcount = SPINLOCK_BACKOFF_MIN;
uint32_t s;
again:
s = atomic_load_relaxed(&node->state);
if (s & NODE_LOCKED) {
SPINLOCK_BACKOFF(bcount);
goto again;
}
if (!atomic_compare_exchange_weak_explicit_32(&node->state,
&s, s | NODE_LOCKED, memory_order_acquire, memory_order_relaxed)) {
bcount = SPINLOCK_BACKOFF_MIN;
goto again;
}
}
static void
unlock_node(thmap_inode_t *node)
{
uint32_t s = atomic_load_relaxed(&node->state) & ~NODE_LOCKED;
ASSERT(node_locked_p(node));
atomic_store_release(&node->state, s);
}
static inline uint32_t
hash(const uint8_t seed[static HASHVAL_SEEDLEN], const void *key, size_t len,
uint32_t level)
{
struct blake2s B;
uint32_t h;
if (level == 0)
return murmurhash3(key, len, 0);
blake2s_init(&B, sizeof h, seed, HASHVAL_SEEDLEN);
blake2s_update(&B, &level, sizeof level);
blake2s_update(&B, key, len);
blake2s_final(&B, &h);
return h;
}
static inline void
hashval_init(thmap_query_t *query, const uint8_t seed[static HASHVAL_SEEDLEN],
const void * restrict key, size_t len)
{
const uint32_t hashval = hash(seed, key, len, 0);
query->seed = seed;
query->rslot = ((hashval >> ROOT_MSBITS) ^ len) & ROOT_MASK;
query->level = 0;
query->hashval = hashval;
query->hashidx = 0;
}
static unsigned
hashval_getslot(thmap_query_t *query, const void * restrict key, size_t len)
{
const unsigned offset = query->level * LEVEL_BITS;
const unsigned shift = offset & HASHVAL_MOD;
const unsigned i = offset >> HASHVAL_SHIFT;
if (query->hashidx != i) {
query->hashval = hash(query->seed, key, len, i);
query->hashidx = i;
}
return (query->hashval >> shift) & LEVEL_MASK;
}
static unsigned
hashval_getleafslot(const thmap_t *thmap,
const thmap_leaf_t *leaf, unsigned level)
{
const void *key = THMAP_GETPTR(thmap, leaf->key);
const unsigned offset = level * LEVEL_BITS;
const unsigned shift = offset & HASHVAL_MOD;
const unsigned i = offset >> HASHVAL_SHIFT;
return (hash(thmap->seed, key, leaf->len, i) >> shift) & LEVEL_MASK;
}
static inline unsigned
hashval_getl0slot(const thmap_t *thmap, const thmap_query_t *query,
const thmap_leaf_t *leaf)
{
if (__predict_true(query->hashidx == 0)) {
return query->hashval & LEVEL_MASK;
}
return hashval_getleafslot(thmap, leaf, 0);
}
static bool
key_cmp_p(const thmap_t *thmap, const thmap_leaf_t *leaf,
const void * restrict key, size_t len)
{
const void *leafkey = THMAP_GETPTR(thmap, leaf->key);
return len == leaf->len && memcmp(key, leafkey, len) == 0;
}
static thmap_inode_t *
node_create(thmap_t *thmap, thmap_inode_t *parent)
{
thmap_inode_t *node;
uintptr_t p;
p = gc_alloc(thmap, THMAP_INODE_LEN);
if (!p) {
return NULL;
}
node = THMAP_GETPTR(thmap, p);
ASSERT(THMAP_ALIGNED_P(node));
memset(node, 0, THMAP_INODE_LEN);
if (parent) {
atomic_store_relaxed(&node->state, NODE_LOCKED);
node->parent = THMAP_GETOFF(thmap, parent);
}
return node;
}
static void
node_insert(thmap_inode_t *node, unsigned slot, thmap_ptr_t child)
{
ASSERT(node_locked_p(node) || node->parent == THMAP_NULL);
ASSERT((atomic_load_relaxed(&node->state) & NODE_DELETED) == 0);
ASSERT(atomic_load_relaxed(&node->slots[slot]) == THMAP_NULL);
ASSERT(NODE_COUNT(atomic_load_relaxed(&node->state)) < LEVEL_SIZE);
atomic_store_relaxed(&node->slots[slot], child);
atomic_store_relaxed(&node->state,
atomic_load_relaxed(&node->state) + 1);
}
static void
node_remove(thmap_inode_t *node, unsigned slot)
{
ASSERT(node_locked_p(node));
ASSERT((atomic_load_relaxed(&node->state) & NODE_DELETED) == 0);
ASSERT(atomic_load_relaxed(&node->slots[slot]) != THMAP_NULL);
ASSERT(NODE_COUNT(atomic_load_relaxed(&node->state)) > 0);
ASSERT(NODE_COUNT(atomic_load_relaxed(&node->state)) <= LEVEL_SIZE);
atomic_store_relaxed(&node->slots[slot], THMAP_NULL);
atomic_store_relaxed(&node->state,
atomic_load_relaxed(&node->state) - 1);
}
static thmap_leaf_t *
leaf_create(const thmap_t *thmap, const void *key, size_t len, void *val)
{
thmap_leaf_t *leaf;
uintptr_t leaf_off, key_off;
leaf_off = gc_alloc(thmap, sizeof(thmap_leaf_t));
if (!leaf_off) {
return NULL;
}
leaf = THMAP_GETPTR(thmap, leaf_off);
ASSERT(THMAP_ALIGNED_P(leaf));
if ((thmap->flags & THMAP_NOCOPY) == 0) {
key_off = gc_alloc(thmap, len);
if (!key_off) {
gc_free(thmap, leaf_off, sizeof(thmap_leaf_t));
return NULL;
}
memcpy(THMAP_GETPTR(thmap, key_off), key, len);
leaf->key = key_off;
} else {
leaf->key = (uintptr_t)key;
}
leaf->len = len;
leaf->val = val;
return leaf;
}
static void
leaf_free(const thmap_t *thmap, thmap_leaf_t *leaf)
{
if ((thmap->flags & THMAP_NOCOPY) == 0) {
gc_free(thmap, leaf->key, leaf->len);
}
gc_free(thmap, THMAP_GETOFF(thmap, leaf), sizeof(thmap_leaf_t));
}
static thmap_leaf_t *
get_leaf(const thmap_t *thmap, thmap_inode_t *parent, unsigned slot)
{
thmap_ptr_t node;
node = atomic_load_consume(&parent->slots[slot]);
if (THMAP_INODE_P(node)) {
return NULL;
}
return THMAP_NODE(thmap, node);
}
static inline int
root_try_put(thmap_t *thmap, const thmap_query_t *query, thmap_leaf_t *leaf)
{
thmap_ptr_t expected;
const unsigned i = query->rslot;
thmap_inode_t *node;
thmap_ptr_t nptr;
unsigned slot;
if (atomic_load_relaxed(&thmap->root[i])) {
return SET_ERROR(EEXIST);
}
node = node_create(thmap, NULL);
if (__predict_false(node == NULL)) {
return SET_ERROR(ENOMEM);
}
slot = hashval_getl0slot(thmap, query, leaf);
node_insert(node, slot, THMAP_GETOFF(thmap, leaf) | THMAP_LEAF_BIT);
nptr = THMAP_GETOFF(thmap, node);
again:
if (atomic_load_relaxed(&thmap->root[i])) {
gc_free(thmap, nptr, THMAP_INODE_LEN);
return SET_ERROR(EEXIST);
}
expected = THMAP_NULL;
if (!atomic_compare_exchange_weak_explicit_ptr(&thmap->root[i], &expected,
nptr, memory_order_release, memory_order_relaxed)) {
goto again;
}
return 0;
}
static thmap_inode_t *
find_edge_node(const thmap_t *thmap, thmap_query_t *query,
const void * restrict key, size_t len, unsigned *slot)
{
thmap_ptr_t root_slot;
thmap_inode_t *parent;
thmap_ptr_t node;
unsigned off;
ASSERT(query->level == 0);
root_slot = atomic_load_consume(&thmap->root[query->rslot]);
parent = THMAP_NODE(thmap, root_slot);
if (!parent) {
return NULL;
}
descend:
off = hashval_getslot(query, key, len);
node = atomic_load_consume(&parent->slots[off]);
if (node && THMAP_INODE_P(node)) {
parent = THMAP_NODE(thmap, node);
query->level++;
goto descend;
}
if (atomic_load_relaxed(&parent->state) & NODE_DELETED) {
return NULL;
}
*slot = off;
return parent;
}
static thmap_inode_t *
find_edge_node_locked(const thmap_t *thmap, thmap_query_t *query,
const void * restrict key, size_t len, unsigned *slot)
{
thmap_inode_t *node;
thmap_ptr_t target;
retry:
node = find_edge_node(thmap, query, key, len, slot);
if (!node) {
query->level = 0;
return NULL;
}
lock_node(node);
if (__predict_false(atomic_load_relaxed(&node->state) & NODE_DELETED)) {
unlock_node(node);
query->level = 0;
return NULL;
}
target = atomic_load_relaxed(&node->slots[*slot]);
if (__predict_false(target && THMAP_INODE_P(target))) {
unlock_node(node);
query->level = 0;
goto retry;
}
return node;
}
void *
thmap_get(thmap_t *thmap, const void *key, size_t len)
{
thmap_query_t query;
thmap_inode_t *parent;
thmap_leaf_t *leaf;
unsigned slot;
hashval_init(&query, thmap->seed, key, len);
parent = find_edge_node(thmap, &query, key, len, &slot);
if (!parent) {
return NULL;
}
leaf = get_leaf(thmap, parent, slot);
if (!leaf) {
return NULL;
}
if (!key_cmp_p(thmap, leaf, key, len)) {
return NULL;
}
return leaf->val;
}
void *
thmap_put(thmap_t *thmap, const void *key, size_t len, void *val)
{
thmap_query_t query;
thmap_leaf_t *leaf, *other;
thmap_inode_t *parent, *child;
unsigned slot, other_slot;
thmap_ptr_t target;
leaf = leaf_create(thmap, key, len, val);
if (__predict_false(!leaf)) {
return NULL;
}
hashval_init(&query, thmap->seed, key, len);
retry:
switch (root_try_put(thmap, &query, leaf)) {
case 0:
return val;
case EEXIST:
break;
case ENOMEM:
return NULL;
default:
__unreachable();
}
atomic_thread_fence(memory_order_release);
parent = find_edge_node_locked(thmap, &query, key, len, &slot);
if (!parent) {
goto retry;
}
target = atomic_load_relaxed(&parent->slots[slot]);
if (THMAP_INODE_P(target)) {
target = THMAP_GETOFF(thmap, leaf) | THMAP_LEAF_BIT;
node_insert(parent, slot, target);
goto out;
}
other = THMAP_NODE(thmap, target);
if (key_cmp_p(thmap, other, key, len)) {
leaf_free(thmap, leaf);
val = other->val;
goto out;
}
descend:
child = node_create(thmap, parent);
if (__predict_false(!child)) {
leaf_free(thmap, leaf);
val = NULL;
goto out;
}
query.level++;
other_slot = hashval_getleafslot(thmap, other, query.level);
target = THMAP_GETOFF(thmap, other) | THMAP_LEAF_BIT;
node_insert(child, other_slot, target);
atomic_store_release(&parent->slots[slot], THMAP_GETOFF(thmap, child));
unlock_node(parent);
ASSERT(node_locked_p(child));
parent = child;
slot = hashval_getslot(&query, key, len);
if (slot == other_slot) {
goto descend;
}
target = THMAP_GETOFF(thmap, leaf) | THMAP_LEAF_BIT;
node_insert(parent, slot, target);
out:
unlock_node(parent);
return val;
}
void *
thmap_del(thmap_t *thmap, const void *key, size_t len)
{
thmap_query_t query;
thmap_leaf_t *leaf;
thmap_inode_t *parent;
unsigned slot;
void *val;
hashval_init(&query, thmap->seed, key, len);
parent = find_edge_node_locked(thmap, &query, key, len, &slot);
if (!parent) {
return NULL;
}
leaf = get_leaf(thmap, parent, slot);
if (!leaf || !key_cmp_p(thmap, leaf, key, len)) {
unlock_node(parent);
return NULL;
}
ASSERT(THMAP_NODE(thmap, atomic_load_relaxed(&parent->slots[slot]))
== leaf);
node_remove(parent, slot);
while (query.level &&
NODE_COUNT(atomic_load_relaxed(&parent->state)) == 0) {
thmap_inode_t *node = parent;
ASSERT(atomic_load_relaxed(&node->state) == NODE_LOCKED);
query.level--;
slot = hashval_getslot(&query, key, len);
parent = THMAP_NODE(thmap, node->parent);
ASSERT(parent != NULL);
lock_node(parent);
ASSERT((atomic_load_relaxed(&parent->state) & NODE_DELETED)
== 0);
atomic_store_relaxed(&node->state,
atomic_load_relaxed(&node->state) | NODE_DELETED);
unlock_node(node);
ASSERT(THMAP_NODE(thmap,
atomic_load_relaxed(&parent->slots[slot])) == node);
node_remove(parent, slot);
stage_mem_gc(thmap, THMAP_GETOFF(thmap, node), THMAP_INODE_LEN);
}
if (NODE_COUNT(atomic_load_relaxed(&parent->state)) == 0) {
const unsigned rslot = query.rslot;
const thmap_ptr_t nptr =
atomic_load_relaxed(&thmap->root[rslot]);
ASSERT(query.level == 0);
ASSERT(parent->parent == THMAP_NULL);
ASSERT(THMAP_GETOFF(thmap, parent) == nptr);
atomic_store_relaxed(&parent->state,
atomic_load_relaxed(&parent->state) | NODE_DELETED);
atomic_store_relaxed(&thmap->root[rslot], THMAP_NULL);
stage_mem_gc(thmap, nptr, THMAP_INODE_LEN);
}
unlock_node(parent);
val = leaf->val;
if ((thmap->flags & THMAP_NOCOPY) == 0) {
stage_mem_gc(thmap, leaf->key, leaf->len);
}
stage_mem_gc(thmap, THMAP_GETOFF(thmap, leaf), sizeof(thmap_leaf_t));
return val;
}
static void
stage_mem_gc(thmap_t *thmap, uintptr_t addr, size_t len)
{
char *const ptr = THMAP_GETPTR(thmap, addr);
thmap_gc_t *head, *gc;
gc = container_of(ptr, struct thmap_gc, data[0]);
KASSERTMSG(gc->len == len,
"thmap=%p ops=%p ptr=%p len=%zu gc=%p gc->len=%zu",
thmap, thmap->ops, (char *)addr, len, gc, gc->len);
retry:
head = atomic_load_relaxed(&thmap->gc_list);
gc->next = head;
if (!atomic_compare_exchange_weak_explicit_ptr(&thmap->gc_list, &head, gc,
memory_order_release, memory_order_relaxed)) {
goto retry;
}
}
void *
thmap_stage_gc(thmap_t *thmap)
{
return atomic_exchange_explicit(&thmap->gc_list, NULL,
memory_order_acquire);
}
void
thmap_gc(thmap_t *thmap, void *ref)
{
thmap_gc_t *gc = ref;
while (gc) {
thmap_gc_t *next = gc->next;
gc_free(thmap, THMAP_GETOFF(thmap, &gc->data[0]), gc->len);
gc = next;
}
}
thmap_t *
thmap_create(uintptr_t baseptr, const thmap_ops_t *ops, unsigned flags)
{
thmap_t *thmap;
uintptr_t root;
if (!THMAP_ALIGNED_P(baseptr)) {
return NULL;
}
thmap = kmem_zalloc(sizeof(thmap_t), KM_SLEEP);
thmap->baseptr = baseptr;
thmap->ops = ops ? ops : &thmap_default_ops;
thmap->flags = flags;
if ((thmap->flags & THMAP_SETROOT) == 0) {
root = gc_alloc(thmap, THMAP_ROOT_LEN);
if (!root) {
kmem_free(thmap, sizeof(thmap_t));
return NULL;
}
thmap->root = THMAP_GETPTR(thmap, root);
memset(thmap->root, 0, THMAP_ROOT_LEN);
}
cprng_strong(kern_cprng, thmap->seed, sizeof thmap->seed, 0);
return thmap;
}
int
thmap_setroot(thmap_t *thmap, uintptr_t root_off)
{
if (thmap->root) {
return -1;
}
thmap->root = THMAP_GETPTR(thmap, root_off);
return 0;
}
uintptr_t
thmap_getroot(const thmap_t *thmap)
{
return THMAP_GETOFF(thmap, thmap->root);
}
void
thmap_destroy(thmap_t *thmap)
{
uintptr_t root = THMAP_GETOFF(thmap, thmap->root);
void *ref;
ref = thmap_stage_gc(thmap);
thmap_gc(thmap, ref);
if ((thmap->flags & THMAP_SETROOT) == 0) {
gc_free(thmap, root, THMAP_ROOT_LEN);
}
kmem_free(thmap, sizeof(thmap_t));
}