cregit-Linux how code gets into the kernel

Release 4.15 security/selinux/ss/hashtab.c

// SPDX-License-Identifier: GPL-2.0
/*
 * Implementation of the hash table type.
 *
 * Author : Stephen Smalley, <sds@tycho.nsa.gov>
 */
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/sched.h>
#include "hashtab.h"


static struct kmem_cache *hashtab_node_cachep;


struct hashtab *hashtab_create(u32 (*hash_value)(struct hashtab *h, const void *key), int (*keycmp)(struct hashtab *h, const void *key1, const void *key2), u32 size) { struct hashtab *p; u32 i; p = kzalloc(sizeof(*p), GFP_KERNEL); if (!p) return p; p->size = size; p->nel = 0; p->hash_value = hash_value; p->keycmp = keycmp; p->htable = kmalloc_array(size, sizeof(*p->htable), GFP_KERNEL); if (!p->htable) { kfree(p); return NULL; } for (i = 0; i < size; i++) p->htable[i] = NULL; return p; }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Morton15494.48%120.00%
SF Markus Elfring53.07%240.00%
Chad Sellers31.84%120.00%
James Morris10.61%120.00%
Total163100.00%5100.00%


int hashtab_insert(struct hashtab *h, void *key, void *datum) { u32 hvalue; struct hashtab_node *prev, *cur, *newnode; cond_resched(); if (!h || h->nel == HASHTAB_MAX_NODES) return -EINVAL; hvalue = h->hash_value(h, key); prev = NULL; cur = h->htable[hvalue]; while (cur && h->keycmp(h, key, cur->key) > 0) { prev = cur; cur = cur->next; } if (cur && (h->keycmp(h, key, cur->key) == 0)) return -EEXIST; newnode = kmem_cache_zalloc(hashtab_node_cachep, GFP_KERNEL); if (!newnode) return -ENOMEM; newnode->key = key; newnode->datum = datum; if (prev) { newnode->next = prev->next; prev->next = newnode; } else { newnode->next = h->htable[hvalue]; h->htable[hvalue] = newnode; } h->nel++; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Morton20597.16%125.00%
Dave Jones31.42%125.00%
Kyeongdon Kim20.95%125.00%
SF Markus Elfring10.47%125.00%
Total211100.00%4100.00%


void *hashtab_search(struct hashtab *h, const void *key) { u32 hvalue; struct hashtab_node *cur; if (!h) return NULL; hvalue = h->hash_value(h, key); cur = h->htable[hvalue]; while (cur && h->keycmp(h, key, cur->key) > 0) cur = cur->next; if (!cur || (h->keycmp(h, key, cur->key) != 0)) return NULL; return cur->datum; }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Morton10598.13%133.33%
SF Markus Elfring10.93%133.33%
Chad Sellers10.93%133.33%
Total107100.00%3100.00%


void hashtab_destroy(struct hashtab *h) { u32 i; struct hashtab_node *cur, *temp; if (!h) return; for (i = 0; i < h->size; i++) { cur = h->htable[i]; while (cur) { temp = cur; cur = cur->next; kmem_cache_free(hashtab_node_cachep, temp); } h->htable[i] = NULL; } kfree(h->htable); h->htable = NULL; kfree(h); }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Morton10097.09%150.00%
Kyeongdon Kim32.91%150.00%
Total103100.00%2100.00%


int hashtab_map(struct hashtab *h, int (*apply)(void *k, void *d, void *args), void *args) { u32 i; int ret; struct hashtab_node *cur; if (!h) return 0; for (i = 0; i < h->size; i++) { cur = h->htable[i]; while (cur) { ret = apply(cur->key, cur->datum, args); if (ret) return ret; cur = cur->next; } } return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Morton115100.00%1100.00%
Total115100.00%1100.00%


void hashtab_stat(struct hashtab *h, struct hashtab_info *info) { u32 i, chain_len, slots_used, max_chain_len; struct hashtab_node *cur; slots_used = 0; max_chain_len = 0; for (i = 0; i < h->size; i++) { cur = h->htable[i]; if (cur) { slots_used++; chain_len = 0; while (cur) { chain_len++; cur = cur->next; } if (chain_len > max_chain_len) max_chain_len = chain_len; } } info->slots_used = slots_used; info->max_chain_len = max_chain_len; }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Morton113100.00%1100.00%
Total113100.00%1100.00%


void hashtab_cache_init(void) { hashtab_node_cachep = kmem_cache_create("hashtab_node", sizeof(struct hashtab_node), 0, SLAB_PANIC, NULL); }

Contributors

PersonTokensPropCommitsCommitProp
Kyeongdon Kim26100.00%1100.00%
Total26100.00%1100.00%


void hashtab_cache_destroy(void) { kmem_cache_destroy(hashtab_node_cachep); }

Contributors

PersonTokensPropCommitsCommitProp
Kyeongdon Kim12100.00%1100.00%
Total12100.00%1100.00%


Overall Contributors

PersonTokensPropCommitsCommitProp
Andrew Morton80492.10%220.00%
Kyeongdon Kim495.61%110.00%
SF Markus Elfring70.80%220.00%
Dave Jones60.69%110.00%
Chad Sellers40.46%110.00%
Greg Kroah-Hartman10.11%110.00%
James Morris10.11%110.00%
Stephen D. Smalley10.11%110.00%
Total873100.00%10100.00%
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.
Created with cregit.