hc
2024-01-03 2f7c68cb55ecb7331f2381deb497c27155f32faf
kernel/drivers/net/ethernet/mellanox/mlxsw/spectrum_kvdl.c
....@@ -2,13 +2,15 @@
22 /* Copyright (c) 2016-2018 Mellanox Technologies. All rights reserved */
33
44 #include <linux/kernel.h>
5
+#include <linux/mutex.h>
56 #include <linux/slab.h>
67
78 #include "spectrum.h"
89
910 struct mlxsw_sp_kvdl {
1011 const struct mlxsw_sp_kvdl_ops *kvdl_ops;
11
- unsigned long priv[0];
12
+ struct mutex kvdl_lock; /* Protects kvdl allocations */
13
+ unsigned long priv[];
1214 /* priv has to be always the last item */
1315 };
1416
....@@ -22,6 +24,7 @@
2224 GFP_KERNEL);
2325 if (!kvdl)
2426 return -ENOMEM;
27
+ mutex_init(&kvdl->kvdl_lock);
2528 kvdl->kvdl_ops = kvdl_ops;
2629 mlxsw_sp->kvdl = kvdl;
2730
....@@ -31,6 +34,7 @@
3134 return 0;
3235
3336 err_init:
37
+ mutex_destroy(&kvdl->kvdl_lock);
3438 kfree(kvdl);
3539 return err;
3640 }
....@@ -40,6 +44,7 @@
4044 struct mlxsw_sp_kvdl *kvdl = mlxsw_sp->kvdl;
4145
4246 kvdl->kvdl_ops->fini(mlxsw_sp, kvdl->priv);
47
+ mutex_destroy(&kvdl->kvdl_lock);
4348 kfree(kvdl);
4449 }
4550
....@@ -48,9 +53,14 @@
4853 unsigned int entry_count, u32 *p_entry_index)
4954 {
5055 struct mlxsw_sp_kvdl *kvdl = mlxsw_sp->kvdl;
56
+ int err;
5157
52
- return kvdl->kvdl_ops->alloc(mlxsw_sp, kvdl->priv, type,
53
- entry_count, p_entry_index);
58
+ mutex_lock(&kvdl->kvdl_lock);
59
+ err = kvdl->kvdl_ops->alloc(mlxsw_sp, kvdl->priv, type,
60
+ entry_count, p_entry_index);
61
+ mutex_unlock(&kvdl->kvdl_lock);
62
+
63
+ return err;
5464 }
5565
5666 void mlxsw_sp_kvdl_free(struct mlxsw_sp *mlxsw_sp,
....@@ -59,8 +69,10 @@
5969 {
6070 struct mlxsw_sp_kvdl *kvdl = mlxsw_sp->kvdl;
6171
72
+ mutex_lock(&kvdl->kvdl_lock);
6273 kvdl->kvdl_ops->free(mlxsw_sp, kvdl->priv, type,
6374 entry_count, entry_index);
75
+ mutex_unlock(&kvdl->kvdl_lock);
6476 }
6577
6678 int mlxsw_sp_kvdl_alloc_count_query(struct mlxsw_sp *mlxsw_sp,