Sven Eckelmann | 7db7d9f | 2017-11-19 15:05:11 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Sven Eckelmann | cfa55c6 | 2021-01-01 00:00:01 +0100 | [diff] [blame] | 2 | /* Copyright (C) B.A.T.M.A.N. contributors: |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 3 | * |
| 4 | * Simon Wunderlich, Marek Lindner |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 7 | #include "hash.h" |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 8 | #include "main.h" |
| 9 | |
Sven Eckelmann | b92b94a | 2017-11-19 17:12:02 +0100 | [diff] [blame] | 10 | #include <linux/gfp.h> |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 11 | #include <linux/lockdep.h> |
| 12 | #include <linux/slab.h> |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 13 | |
| 14 | /* clears the hash */ |
Sven Eckelmann | 5bf74e9 | 2012-06-05 22:31:28 +0200 | [diff] [blame] | 15 | static void batadv_hash_init(struct batadv_hashtable *hash) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 16 | { |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 17 | u32 i; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 18 | |
Sven Eckelmann | cb4cca7 | 2012-06-17 16:27:22 +0200 | [diff] [blame] | 19 | for (i = 0; i < hash->size; i++) { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 20 | INIT_HLIST_HEAD(&hash->table[i]); |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 21 | spin_lock_init(&hash->list_locks[i]); |
| 22 | } |
Sven Eckelmann | 05abd7b | 2018-10-30 22:01:25 +0100 | [diff] [blame] | 23 | |
| 24 | atomic_set(&hash->generation, 0); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 25 | } |
| 26 | |
Sven Eckelmann | ff15c27 | 2017-12-02 19:51:53 +0100 | [diff] [blame] | 27 | /** |
| 28 | * batadv_hash_destroy() - Free only the hashtable and the hash itself |
| 29 | * @hash: hash object to destroy |
| 30 | */ |
Sven Eckelmann | 5bf74e9 | 2012-06-05 22:31:28 +0200 | [diff] [blame] | 31 | void batadv_hash_destroy(struct batadv_hashtable *hash) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 32 | { |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 33 | kfree(hash->list_locks); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 34 | kfree(hash->table); |
| 35 | kfree(hash); |
| 36 | } |
| 37 | |
Sven Eckelmann | ff15c27 | 2017-12-02 19:51:53 +0100 | [diff] [blame] | 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 | */ |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 44 | struct batadv_hashtable *batadv_hash_new(u32 size) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 45 | { |
Sven Eckelmann | 5bf74e9 | 2012-06-05 22:31:28 +0200 | [diff] [blame] | 46 | struct batadv_hashtable *hash; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 47 | |
Sven Eckelmann | 704509b | 2011-05-14 23:14:54 +0200 | [diff] [blame] | 48 | hash = kmalloc(sizeof(*hash), GFP_ATOMIC); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 49 | if (!hash) |
| 50 | return NULL; |
| 51 | |
Antonio Quartulli | 0185dda | 2014-05-24 06:43:47 +0200 | [diff] [blame] | 52 | hash->table = kmalloc_array(size, sizeof(*hash->table), GFP_ATOMIC); |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 53 | if (!hash->table) |
| 54 | goto free_hash; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 55 | |
Antonio Quartulli | 0185dda | 2014-05-24 06:43:47 +0200 | [diff] [blame] | 56 | hash->list_locks = kmalloc_array(size, sizeof(*hash->list_locks), |
| 57 | GFP_ATOMIC); |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 58 | if (!hash->list_locks) |
| 59 | goto free_table; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 60 | |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 61 | hash->size = size; |
Sven Eckelmann | 7f9f02c | 2012-05-12 18:33:58 +0200 | [diff] [blame] | 62 | batadv_hash_init(hash); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 63 | return hash; |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 64 | |
| 65 | free_table: |
| 66 | kfree(hash->table); |
| 67 | free_hash: |
| 68 | kfree(hash); |
| 69 | return NULL; |
| 70 | } |
Sven Eckelmann | 5d52dad | 2012-03-29 12:38:20 +0200 | [diff] [blame] | 71 | |
Sven Eckelmann | ff15c27 | 2017-12-02 19:51:53 +0100 | [diff] [blame] | 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 | */ |
Sven Eckelmann | 5bf74e9 | 2012-06-05 22:31:28 +0200 | [diff] [blame] | 77 | void batadv_hash_set_lock_class(struct batadv_hashtable *hash, |
Sven Eckelmann | 5d52dad | 2012-03-29 12:38:20 +0200 | [diff] [blame] | 78 | struct lock_class_key *key) |
| 79 | { |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 80 | u32 i; |
Sven Eckelmann | 5d52dad | 2012-03-29 12:38:20 +0200 | [diff] [blame] | 81 | |
| 82 | for (i = 0; i < hash->size; i++) |
| 83 | lockdep_set_class(&hash->list_locks[i], key); |
| 84 | } |