1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * Implementation of the hash table type. 41da177e4SLinus Torvalds * 57efbb60bSStephen Smalley * Author : Stephen Smalley, <sds@tycho.nsa.gov> 61da177e4SLinus Torvalds */ 71da177e4SLinus Torvalds #include <linux/kernel.h> 81da177e4SLinus Torvalds #include <linux/slab.h> 91da177e4SLinus Torvalds #include <linux/errno.h> 101da177e4SLinus Torvalds #include "hashtab.h" 111da177e4SLinus Torvalds 127c620eceSKyeongdon Kim static struct kmem_cache *hashtab_node_cachep; 137c620eceSKyeongdon Kim 14e0ac568dSOndrej Mosnacek /* 15e0ac568dSOndrej Mosnacek * Here we simply round the number of elements up to the nearest power of two. 16e0ac568dSOndrej Mosnacek * I tried also other options like rouding down or rounding to the closest 17e0ac568dSOndrej Mosnacek * power of two (up or down based on which is closer), but I was unable to 18e0ac568dSOndrej Mosnacek * find any significant difference in lookup/insert performance that would 19e0ac568dSOndrej Mosnacek * justify switching to a different (less intuitive) formula. It could be that 20e0ac568dSOndrej Mosnacek * a different formula is actually more optimal, but any future changes here 21e0ac568dSOndrej Mosnacek * should be supported with performance/memory usage data. 22e0ac568dSOndrej Mosnacek * 23e0ac568dSOndrej Mosnacek * The total memory used by the htable arrays (only) with Fedora policy loaded 24e0ac568dSOndrej Mosnacek * is approximately 163 KB at the time of writing. 25e0ac568dSOndrej Mosnacek */ 26e0ac568dSOndrej Mosnacek static u32 hashtab_compute_size(u32 nel) 27e0ac568dSOndrej Mosnacek { 28e0ac568dSOndrej Mosnacek return nel == 0 ? 0 : roundup_pow_of_two(nel); 29e0ac568dSOndrej Mosnacek } 30e0ac568dSOndrej Mosnacek 3124def7bbSOndrej Mosnacek int hashtab_init(struct hashtab *h, u32 nel_hint) 321da177e4SLinus Torvalds { 3303414a49SOndrej Mosnacek h->size = hashtab_compute_size(nel_hint); 3403414a49SOndrej Mosnacek h->nel = 0; 3503414a49SOndrej Mosnacek if (!h->size) 3603414a49SOndrej Mosnacek return 0; 371da177e4SLinus Torvalds 3803414a49SOndrej Mosnacek h->htable = kcalloc(h->size, sizeof(*h->htable), GFP_KERNEL); 3903414a49SOndrej Mosnacek return h->htable ? 0 : -ENOMEM; 401da177e4SLinus Torvalds } 411da177e4SLinus Torvalds 4254b27f92SOndrej Mosnacek int __hashtab_insert(struct hashtab *h, struct hashtab_node **dst, 4354b27f92SOndrej Mosnacek void *key, void *datum) 441da177e4SLinus Torvalds { 4554b27f92SOndrej Mosnacek struct hashtab_node *newnode; 461da177e4SLinus Torvalds 477c620eceSKyeongdon Kim newnode = kmem_cache_zalloc(hashtab_node_cachep, GFP_KERNEL); 48cb8d21e3SMarkus Elfring if (!newnode) 491da177e4SLinus Torvalds return -ENOMEM; 501da177e4SLinus Torvalds newnode->key = key; 511da177e4SLinus Torvalds newnode->datum = datum; 5254b27f92SOndrej Mosnacek newnode->next = *dst; 5354b27f92SOndrej Mosnacek *dst = newnode; 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds h->nel++; 561da177e4SLinus Torvalds return 0; 571da177e4SLinus Torvalds } 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds void hashtab_destroy(struct hashtab *h) 601da177e4SLinus Torvalds { 611da177e4SLinus Torvalds u32 i; 621da177e4SLinus Torvalds struct hashtab_node *cur, *temp; 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds for (i = 0; i < h->size; i++) { 651da177e4SLinus Torvalds cur = h->htable[i]; 66dbc74c65SVesa-Matti Kari while (cur) { 671da177e4SLinus Torvalds temp = cur; 681da177e4SLinus Torvalds cur = cur->next; 697c620eceSKyeongdon Kim kmem_cache_free(hashtab_node_cachep, temp); 701da177e4SLinus Torvalds } 711da177e4SLinus Torvalds h->htable[i] = NULL; 721da177e4SLinus Torvalds } 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds kfree(h->htable); 751da177e4SLinus Torvalds h->htable = NULL; 761da177e4SLinus Torvalds } 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds int hashtab_map(struct hashtab *h, 791da177e4SLinus Torvalds int (*apply)(void *k, void *d, void *args), 801da177e4SLinus Torvalds void *args) 811da177e4SLinus Torvalds { 821da177e4SLinus Torvalds u32 i; 831da177e4SLinus Torvalds int ret; 841da177e4SLinus Torvalds struct hashtab_node *cur; 851da177e4SLinus Torvalds 861da177e4SLinus Torvalds for (i = 0; i < h->size; i++) { 871da177e4SLinus Torvalds cur = h->htable[i]; 88dbc74c65SVesa-Matti Kari while (cur) { 891da177e4SLinus Torvalds ret = apply(cur->key, cur->datum, args); 901da177e4SLinus Torvalds if (ret) 911da177e4SLinus Torvalds return ret; 921da177e4SLinus Torvalds cur = cur->next; 931da177e4SLinus Torvalds } 941da177e4SLinus Torvalds } 951da177e4SLinus Torvalds return 0; 961da177e4SLinus Torvalds } 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds void hashtab_stat(struct hashtab *h, struct hashtab_info *info) 1001da177e4SLinus Torvalds { 1011da177e4SLinus Torvalds u32 i, chain_len, slots_used, max_chain_len; 1021da177e4SLinus Torvalds struct hashtab_node *cur; 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds slots_used = 0; 1051da177e4SLinus Torvalds max_chain_len = 0; 1065794ed76SColin Ian King for (i = 0; i < h->size; i++) { 1071da177e4SLinus Torvalds cur = h->htable[i]; 1081da177e4SLinus Torvalds if (cur) { 1091da177e4SLinus Torvalds slots_used++; 1101da177e4SLinus Torvalds chain_len = 0; 1111da177e4SLinus Torvalds while (cur) { 1121da177e4SLinus Torvalds chain_len++; 1131da177e4SLinus Torvalds cur = cur->next; 1141da177e4SLinus Torvalds } 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds if (chain_len > max_chain_len) 1171da177e4SLinus Torvalds max_chain_len = chain_len; 1181da177e4SLinus Torvalds } 1191da177e4SLinus Torvalds } 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds info->slots_used = slots_used; 1221da177e4SLinus Torvalds info->max_chain_len = max_chain_len; 1231da177e4SLinus Torvalds } 124aa8e712cSStephen Smalley 125aa8e712cSStephen Smalley void __init hashtab_cache_init(void) 1267c620eceSKyeongdon Kim { 1277c620eceSKyeongdon Kim hashtab_node_cachep = kmem_cache_create("hashtab_node", 1287c620eceSKyeongdon Kim sizeof(struct hashtab_node), 1297c620eceSKyeongdon Kim 0, SLAB_PANIC, NULL); 1307c620eceSKyeongdon Kim } 131