Merge tag 'perf-tools-fixes-for-v6.4-1-2023-05-20' of git://git.kernel.org/pub/scm...
[linux-block.git] / net / batman-adv / hash.c
CommitLineData
7db7d9f3 1// SPDX-License-Identifier: GPL-2.0
cfa55c6d 2/* Copyright (C) B.A.T.M.A.N. contributors:
c6c8fea2
SE
3 *
4 * Simon Wunderlich, Marek Lindner
c6c8fea2
SE
5 */
6
c6c8fea2 7#include "hash.h"
1e2c2a4f
SE
8#include "main.h"
9
b92b94ac 10#include <linux/gfp.h>
1e2c2a4f
SE
11#include <linux/lockdep.h>
12#include <linux/slab.h>
c6c8fea2
SE
13
14/* clears the hash */
5bf74e9c 15static void batadv_hash_init(struct batadv_hashtable *hash)
c6c8fea2 16{
6b5e971a 17 u32 i;
c6c8fea2 18
cb4cca71 19 for (i = 0; i < hash->size; i++) {
c6c8fea2 20 INIT_HLIST_HEAD(&hash->table[i]);
fb778ea1
ML
21 spin_lock_init(&hash->list_locks[i]);
22 }
05abd7bc
SE
23
24 atomic_set(&hash->generation, 0);
c6c8fea2
SE
25}
26
ff15c27c
SE
27/**
28 * batadv_hash_destroy() - Free only the hashtable and the hash itself
29 * @hash: hash object to destroy
30 */
5bf74e9c 31void batadv_hash_destroy(struct batadv_hashtable *hash)
c6c8fea2 32{
fb778ea1 33 kfree(hash->list_locks);
c6c8fea2
SE
34 kfree(hash->table);
35 kfree(hash);
36}
37
ff15c27c
SE
38/**
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 */
6b5e971a 44struct batadv_hashtable *batadv_hash_new(u32 size)
c6c8fea2 45{
5bf74e9c 46 struct batadv_hashtable *hash;
c6c8fea2 47
704509b8 48 hash = kmalloc(sizeof(*hash), GFP_ATOMIC);
c6c8fea2
SE
49 if (!hash)
50 return NULL;
51
0185dda6 52 hash->table = kmalloc_array(size, sizeof(*hash->table), GFP_ATOMIC);
fb778ea1
ML
53 if (!hash->table)
54 goto free_hash;
c6c8fea2 55
0185dda6
AQ
56 hash->list_locks = kmalloc_array(size, sizeof(*hash->list_locks),
57 GFP_ATOMIC);
fb778ea1
ML
58 if (!hash->list_locks)
59 goto free_table;
c6c8fea2 60
fb778ea1 61 hash->size = size;
7f9f02cb 62 batadv_hash_init(hash);
c6c8fea2 63 return hash;
fb778ea1
ML
64
65free_table:
66 kfree(hash->table);
67free_hash:
68 kfree(hash);
69 return NULL;
70}
5d52dad2 71
ff15c27c
SE
72/**
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 */
5bf74e9c 77void batadv_hash_set_lock_class(struct batadv_hashtable *hash,
5d52dad2
SE
78 struct lock_class_key *key)
79{
6b5e971a 80 u32 i;
5d52dad2
SE
81
82 for (i = 0; i < hash->size; i++)
83 lockdep_set_class(&hash->list_locks[i], key);
84}