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 12*cd2bb4cbSOndrej Mosnacek static struct kmem_cache *hashtab_node_cachep __ro_after_init; 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 125c7c556f1SStephen Smalley int hashtab_duplicate(struct hashtab *new, struct hashtab *orig, 126c7c556f1SStephen Smalley int (*copy)(struct hashtab_node *new, 127c7c556f1SStephen Smalley struct hashtab_node *orig, void *args), 128c7c556f1SStephen Smalley int (*destroy)(void *k, void *d, void *args), 129c7c556f1SStephen Smalley void *args) 130c7c556f1SStephen Smalley { 131c7c556f1SStephen Smalley struct hashtab_node *cur, *tmp, *tail; 132c7c556f1SStephen Smalley int i, rc; 133c7c556f1SStephen Smalley 134c7c556f1SStephen Smalley memset(new, 0, sizeof(*new)); 135c7c556f1SStephen Smalley 136c7c556f1SStephen Smalley new->htable = kcalloc(orig->size, sizeof(*new->htable), GFP_KERNEL); 137c7c556f1SStephen Smalley if (!new->htable) 138c7c556f1SStephen Smalley return -ENOMEM; 139c7c556f1SStephen Smalley 140c7c556f1SStephen Smalley new->size = orig->size; 141c7c556f1SStephen Smalley 142c7c556f1SStephen Smalley for (i = 0; i < orig->size; i++) { 143c7c556f1SStephen Smalley tail = NULL; 144c7c556f1SStephen Smalley for (cur = orig->htable[i]; cur; cur = cur->next) { 145c7c556f1SStephen Smalley tmp = kmem_cache_zalloc(hashtab_node_cachep, 146c7c556f1SStephen Smalley GFP_KERNEL); 147c7c556f1SStephen Smalley if (!tmp) 148c7c556f1SStephen Smalley goto error; 149c7c556f1SStephen Smalley rc = copy(tmp, cur, args); 150c7c556f1SStephen Smalley if (rc) { 151c7c556f1SStephen Smalley kmem_cache_free(hashtab_node_cachep, tmp); 152c7c556f1SStephen Smalley goto error; 153c7c556f1SStephen Smalley } 154c7c556f1SStephen Smalley tmp->next = NULL; 155c7c556f1SStephen Smalley if (!tail) 156c7c556f1SStephen Smalley new->htable[i] = tmp; 157c7c556f1SStephen Smalley else 158c7c556f1SStephen Smalley tail->next = tmp; 159c7c556f1SStephen Smalley tail = tmp; 160c7c556f1SStephen Smalley new->nel++; 161c7c556f1SStephen Smalley } 162c7c556f1SStephen Smalley } 163c7c556f1SStephen Smalley 164c7c556f1SStephen Smalley return 0; 165c7c556f1SStephen Smalley 166c7c556f1SStephen Smalley error: 167c7c556f1SStephen Smalley for (i = 0; i < new->size; i++) { 168c7c556f1SStephen Smalley for (cur = new->htable[i]; cur; cur = tmp) { 169c7c556f1SStephen Smalley tmp = cur->next; 170c7c556f1SStephen Smalley destroy(cur->key, cur->datum, args); 171c7c556f1SStephen Smalley kmem_cache_free(hashtab_node_cachep, cur); 172c7c556f1SStephen Smalley } 173c7c556f1SStephen Smalley } 174c7c556f1SStephen Smalley kmem_cache_free(hashtab_node_cachep, new); 175c7c556f1SStephen Smalley return -ENOMEM; 176c7c556f1SStephen Smalley } 177c7c556f1SStephen Smalley 178aa8e712cSStephen Smalley void __init hashtab_cache_init(void) 1797c620eceSKyeongdon Kim { 1807c620eceSKyeongdon Kim hashtab_node_cachep = kmem_cache_create("hashtab_node", 1817c620eceSKyeongdon Kim sizeof(struct hashtab_node), 1827c620eceSKyeongdon Kim 0, SLAB_PANIC, NULL); 1837c620eceSKyeongdon Kim } 184