blob: 8016e619787f9e7a890c3acfb772243b4b19165b [file] [log] [blame]
Sven Eckelmann7db7d9f2017-11-19 15:05:11 +01001// SPDX-License-Identifier: GPL-2.0
Sven Eckelmanncfa55c62021-01-01 00:00:01 +01002/* Copyright (C) B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00003 *
4 * Simon Wunderlich, Marek Lindner
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00005 */
6
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00007#include "hash.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +02008#include "main.h"
9
Sven Eckelmannb92b94a2017-11-19 17:12:02 +010010#include <linux/gfp.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020011#include <linux/lockdep.h>
12#include <linux/slab.h>
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000013
14/* clears the hash */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020015static void batadv_hash_init(struct batadv_hashtable *hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000016{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020017 u32 i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000018
Sven Eckelmanncb4cca72012-06-17 16:27:22 +020019 for (i = 0; i < hash->size; i++) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000020 INIT_HLIST_HEAD(&hash->table[i]);
Marek Lindnerfb778ea2011-01-19 20:01:40 +000021 spin_lock_init(&hash->list_locks[i]);
22 }
Sven Eckelmann05abd7b2018-10-30 22:01:25 +010023
24 atomic_set(&hash->generation, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000025}
26
Sven Eckelmannff15c272017-12-02 19:51:53 +010027/**
28 * batadv_hash_destroy() - Free only the hashtable and the hash itself
29 * @hash: hash object to destroy
30 */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020031void batadv_hash_destroy(struct batadv_hashtable *hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000032{
Marek Lindnerfb778ea2011-01-19 20:01:40 +000033 kfree(hash->list_locks);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000034 kfree(hash->table);
35 kfree(hash);
36}
37
Sven Eckelmannff15c272017-12-02 19:51:53 +010038/**
39 * batadv_hash_new() - Allocates and clears the hashtable
40 * @size: number of hash buckets to allocate
41 *
42 * Return: newly allocated hashtable, NULL on errors
43 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020044struct batadv_hashtable *batadv_hash_new(u32 size)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000045{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020046 struct batadv_hashtable *hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000047
Sven Eckelmann704509b2011-05-14 23:14:54 +020048 hash = kmalloc(sizeof(*hash), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000049 if (!hash)
50 return NULL;
51
Antonio Quartulli0185dda2014-05-24 06:43:47 +020052 hash->table = kmalloc_array(size, sizeof(*hash->table), GFP_ATOMIC);
Marek Lindnerfb778ea2011-01-19 20:01:40 +000053 if (!hash->table)
54 goto free_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000055
Antonio Quartulli0185dda2014-05-24 06:43:47 +020056 hash->list_locks = kmalloc_array(size, sizeof(*hash->list_locks),
57 GFP_ATOMIC);
Marek Lindnerfb778ea2011-01-19 20:01:40 +000058 if (!hash->list_locks)
59 goto free_table;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000060
Marek Lindnerfb778ea2011-01-19 20:01:40 +000061 hash->size = size;
Sven Eckelmann7f9f02c2012-05-12 18:33:58 +020062 batadv_hash_init(hash);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000063 return hash;
Marek Lindnerfb778ea2011-01-19 20:01:40 +000064
65free_table:
66 kfree(hash->table);
67free_hash:
68 kfree(hash);
69 return NULL;
70}
Sven Eckelmann5d52dad2012-03-29 12:38:20 +020071
Sven Eckelmannff15c272017-12-02 19:51:53 +010072/**
73 * batadv_hash_set_lock_class() - Set specific lockdep class for hash spinlocks
74 * @hash: hash object to modify
75 * @key: lockdep class key address
76 */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020077void batadv_hash_set_lock_class(struct batadv_hashtable *hash,
Sven Eckelmann5d52dad2012-03-29 12:38:20 +020078 struct lock_class_key *key)
79{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020080 u32 i;
Sven Eckelmann5d52dad2012-03-29 12:38:20 +020081
82 for (i = 0; i < hash->size; i++)
83 lockdep_set_class(&hash->list_locks[i], key);
84}