How to destroy kmalloc allocated objects Hashtable in Linux kernel
04:26 30 Sep 2026

I use the linux kernel hashtable to store process names and I use kmalooc to allocate individual nodes and char pointers. But how do I destroy all the individual objects in the module exit?

    DECLARE_HASHTABLE(process_exceptions_hashtable, HASHTABLE_SIZE);

    struct h_node *cur;

    char childname[TASK_COMM_LEN];

    get_task_comm(childname, child);

    uint32_t keychild = hash(childname);

    hash_for_each_possible(process_exceptions_hashtable, cur, node, keychild) 
    {
        if (strcmp(cur->process_name, childname) == 0) {    
            return;
        }
    }   

    struct h_node *newnode = kmalloc(sizeof *newnode, GFP_KERNEL);
        newnode->process_name = kmalloc(strlen(childname) + 1, GFP_KERNEL);
    hash_add(process_exceptions_hashtable, &newnode->node, keychild);

is it enough to kfree on the node and the data?

hash_for_each(my_table, bkt, cur, node) {
   kfree(node->process_name);
   kfree(node);
}
c linux linux-kernel