blob: 9194f4d891b1263c48f7442824c55095b56576c0 [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 Eckelmann05abd7b2018-10-30 22:01:25 +010035
36 atomic_set(&hash->generation, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000037}
38
Sven Eckelmannff15c272017-12-02 19:51:53 +010039/**
40 * batadv_hash_destroy() - Free only the hashtable and the hash itself
41 * @hash: hash object to destroy
42 */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020043void batadv_hash_destroy(struct batadv_hashtable *hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000044{
Marek Lindnerfb778ea2011-01-19 20:01:40 +000045 kfree(hash->list_locks);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000046 kfree(hash->table);
47 kfree(hash);
48}
49
Sven Eckelmannff15c272017-12-02 19:51:53 +010050/**
51 * batadv_hash_new() - Allocates and clears the hashtable
52 * @size: number of hash buckets to allocate
53 *
54 * Return: newly allocated hashtable, NULL on errors
55 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020056struct batadv_hashtable *batadv_hash_new(u32 size)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000057{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020058 struct batadv_hashtable *hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000059
Sven Eckelmann704509b2011-05-14 23:14:54 +020060 hash = kmalloc(sizeof(*hash), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000061 if (!hash)
62 return NULL;
63
Antonio Quartulli0185dda2014-05-24 06:43:47 +020064 hash->table = kmalloc_array(size, sizeof(*hash->table), GFP_ATOMIC);
Marek Lindnerfb778ea2011-01-19 20:01:40 +000065 if (!hash->table)
66 goto free_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000067
Antonio Quartulli0185dda2014-05-24 06:43:47 +020068 hash->list_locks = kmalloc_array(size, sizeof(*hash->list_locks),
69 GFP_ATOMIC);
Marek Lindnerfb778ea2011-01-19 20:01:40 +000070 if (!hash->list_locks)
71 goto free_table;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000072
Marek Lindnerfb778ea2011-01-19 20:01:40 +000073 hash->size = size;
Sven Eckelmann7f9f02c2012-05-12 18:33:58 +020074 batadv_hash_init(hash);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000075 return hash;
Marek Lindnerfb778ea2011-01-19 20:01:40 +000076
77free_table:
78 kfree(hash->table);
79free_hash:
80 kfree(hash);
81 return NULL;
82}
Sven Eckelmann5d52dad2012-03-29 12:38:20 +020083
Sven Eckelmannff15c272017-12-02 19:51:53 +010084/**
85 * batadv_hash_set_lock_class() - Set specific lockdep class for hash spinlocks
86 * @hash: hash object to modify
87 * @key: lockdep class key address
88 */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020089void batadv_hash_set_lock_class(struct batadv_hashtable *hash,
Sven Eckelmann5d52dad2012-03-29 12:38:20 +020090 struct lock_class_key *key)
91{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020092 u32 i;
Sven Eckelmann5d52dad2012-03-29 12:38:20 +020093
94 for (i = 0; i < hash->size; i++)
95 lockdep_set_class(&hash->list_locks[i], key);
96}