blob: 7b49e4001778f0a53d14eb76ee96595a9a02e775 [file] [log] [blame]
Sven Eckelmann7db7d9f2017-11-19 15:05:11 +01001// SPDX-License-Identifier: GPL-2.0
Sven Eckelmann6b1aea82018-01-01 00:00:00 +01002/* Copyright (C) 2006-2018 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00003 *
4 * Simon Wunderlich, Marek Lindner
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
Antonio Quartulliebf38fb2013-11-03 20:40:48 +010016 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000017 */
18
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000019#include "hash.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020020#include "main.h"
21
Sven Eckelmannb92b94a2017-11-19 17:12:02 +010022#include <linux/gfp.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020023#include <linux/lockdep.h>
24#include <linux/slab.h>
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000025
26/* clears the hash */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020027static void batadv_hash_init(struct batadv_hashtable *hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000028{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020029 u32 i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000030
Sven Eckelmanncb4cca72012-06-17 16:27:22 +020031 for (i = 0; i < hash->size; i++) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000032 INIT_HLIST_HEAD(&hash->table[i]);
Marek Lindnerfb778ea2011-01-19 20:01:40 +000033 spin_lock_init(&hash->list_locks[i]);
34 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000035}
36
Sven Eckelmannff15c272017-12-02 19:51:53 +010037/**
38 * batadv_hash_destroy() - Free only the hashtable and the hash itself
39 * @hash: hash object to destroy
40 */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020041void batadv_hash_destroy(struct batadv_hashtable *hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000042{
Marek Lindnerfb778ea2011-01-19 20:01:40 +000043 kfree(hash->list_locks);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000044 kfree(hash->table);
45 kfree(hash);
46}
47
Sven Eckelmannff15c272017-12-02 19:51:53 +010048/**
49 * batadv_hash_new() - Allocates and clears the hashtable
50 * @size: number of hash buckets to allocate
51 *
52 * Return: newly allocated hashtable, NULL on errors
53 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020054struct batadv_hashtable *batadv_hash_new(u32 size)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000055{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020056 struct batadv_hashtable *hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000057
Sven Eckelmann704509b2011-05-14 23:14:54 +020058 hash = kmalloc(sizeof(*hash), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000059 if (!hash)
60 return NULL;
61
Antonio Quartulli0185dda2014-05-24 06:43:47 +020062 hash->table = kmalloc_array(size, sizeof(*hash->table), GFP_ATOMIC);
Marek Lindnerfb778ea2011-01-19 20:01:40 +000063 if (!hash->table)
64 goto free_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000065
Antonio Quartulli0185dda2014-05-24 06:43:47 +020066 hash->list_locks = kmalloc_array(size, sizeof(*hash->list_locks),
67 GFP_ATOMIC);
Marek Lindnerfb778ea2011-01-19 20:01:40 +000068 if (!hash->list_locks)
69 goto free_table;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000070
Marek Lindnerfb778ea2011-01-19 20:01:40 +000071 hash->size = size;
Sven Eckelmann7f9f02c2012-05-12 18:33:58 +020072 batadv_hash_init(hash);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000073 return hash;
Marek Lindnerfb778ea2011-01-19 20:01:40 +000074
75free_table:
76 kfree(hash->table);
77free_hash:
78 kfree(hash);
79 return NULL;
80}
Sven Eckelmann5d52dad2012-03-29 12:38:20 +020081
Sven Eckelmannff15c272017-12-02 19:51:53 +010082/**
83 * batadv_hash_set_lock_class() - Set specific lockdep class for hash spinlocks
84 * @hash: hash object to modify
85 * @key: lockdep class key address
86 */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020087void batadv_hash_set_lock_class(struct batadv_hashtable *hash,
Sven Eckelmann5d52dad2012-03-29 12:38:20 +020088 struct lock_class_key *key)
89{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020090 u32 i;
Sven Eckelmann5d52dad2012-03-29 12:38:20 +020091
92 for (i = 0; i < hash->size; i++)
93 lockdep_set_class(&hash->list_locks[i], key);
94}