blob: 2ce0d5673f4063b66219f66d53f9b82f97b0bd75 [file] [log] [blame]
Sven Eckelmann7db7d9f2017-11-19 15:05:11 +01001// SPDX-License-Identifier: GPL-2.0
Sven Eckelmannac79cbb2017-01-01 00:00:00 +01002/* Copyright (C) 2006-2017 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
37/* free only the hashtable and the hash itself. */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020038void batadv_hash_destroy(struct batadv_hashtable *hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000039{
Marek Lindnerfb778ea2011-01-19 20:01:40 +000040 kfree(hash->list_locks);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000041 kfree(hash->table);
42 kfree(hash);
43}
44
45/* allocates and clears the hash */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020046struct batadv_hashtable *batadv_hash_new(u32 size)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000047{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020048 struct batadv_hashtable *hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000049
Sven Eckelmann704509b2011-05-14 23:14:54 +020050 hash = kmalloc(sizeof(*hash), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000051 if (!hash)
52 return NULL;
53
Antonio Quartulli0185dda2014-05-24 06:43:47 +020054 hash->table = kmalloc_array(size, sizeof(*hash->table), GFP_ATOMIC);
Marek Lindnerfb778ea2011-01-19 20:01:40 +000055 if (!hash->table)
56 goto free_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000057
Antonio Quartulli0185dda2014-05-24 06:43:47 +020058 hash->list_locks = kmalloc_array(size, sizeof(*hash->list_locks),
59 GFP_ATOMIC);
Marek Lindnerfb778ea2011-01-19 20:01:40 +000060 if (!hash->list_locks)
61 goto free_table;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000062
Marek Lindnerfb778ea2011-01-19 20:01:40 +000063 hash->size = size;
Sven Eckelmann7f9f02c2012-05-12 18:33:58 +020064 batadv_hash_init(hash);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000065 return hash;
Marek Lindnerfb778ea2011-01-19 20:01:40 +000066
67free_table:
68 kfree(hash->table);
69free_hash:
70 kfree(hash);
71 return NULL;
72}
Sven Eckelmann5d52dad2012-03-29 12:38:20 +020073
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020074void batadv_hash_set_lock_class(struct batadv_hashtable *hash,
Sven Eckelmann5d52dad2012-03-29 12:38:20 +020075 struct lock_class_key *key)
76{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020077 u32 i;
Sven Eckelmann5d52dad2012-03-29 12:38:20 +020078
79 for (i = 0; i < hash->size; i++)
80 lockdep_set_class(&hash->list_locks[i], key);
81}