Commit 2c087dfc authored by Christophe JAILLET's avatar Christophe JAILLET Committed by David S. Miller

mlxsw: spectrum: Use 'bitmap_zalloc()' when applicable

Use 'bitmap_zalloc()' to simplify code, improve the semantic and avoid
some open-coded arithmetic in allocator arguments.

Also change the corresponding 'kfree()' into 'bitmap_free()' to keep
consistency.
Signed-off-by: default avatarChristophe JAILLET <christophe.jaillet@wanadoo.fr>
Reviewed-by: default avatarIdo Schimmel <idosch@nvidia.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 7ce9a701
...@@ -119,7 +119,6 @@ mlxsw_sp_acl_atcam_region_12kb_init(struct mlxsw_sp_acl_atcam_region *aregion) ...@@ -119,7 +119,6 @@ mlxsw_sp_acl_atcam_region_12kb_init(struct mlxsw_sp_acl_atcam_region *aregion)
{ {
struct mlxsw_sp *mlxsw_sp = aregion->region->mlxsw_sp; struct mlxsw_sp *mlxsw_sp = aregion->region->mlxsw_sp;
struct mlxsw_sp_acl_atcam_region_12kb *region_12kb; struct mlxsw_sp_acl_atcam_region_12kb *region_12kb;
size_t alloc_size;
u64 max_lkey_id; u64 max_lkey_id;
int err; int err;
...@@ -131,8 +130,7 @@ mlxsw_sp_acl_atcam_region_12kb_init(struct mlxsw_sp_acl_atcam_region *aregion) ...@@ -131,8 +130,7 @@ mlxsw_sp_acl_atcam_region_12kb_init(struct mlxsw_sp_acl_atcam_region *aregion)
if (!region_12kb) if (!region_12kb)
return -ENOMEM; return -ENOMEM;
alloc_size = BITS_TO_LONGS(max_lkey_id) * sizeof(unsigned long); region_12kb->used_lkey_id = bitmap_zalloc(max_lkey_id, GFP_KERNEL);
region_12kb->used_lkey_id = kzalloc(alloc_size, GFP_KERNEL);
if (!region_12kb->used_lkey_id) { if (!region_12kb->used_lkey_id) {
err = -ENOMEM; err = -ENOMEM;
goto err_used_lkey_id_alloc; goto err_used_lkey_id_alloc;
...@@ -149,7 +147,7 @@ mlxsw_sp_acl_atcam_region_12kb_init(struct mlxsw_sp_acl_atcam_region *aregion) ...@@ -149,7 +147,7 @@ mlxsw_sp_acl_atcam_region_12kb_init(struct mlxsw_sp_acl_atcam_region *aregion)
return 0; return 0;
err_rhashtable_init: err_rhashtable_init:
kfree(region_12kb->used_lkey_id); bitmap_free(region_12kb->used_lkey_id);
err_used_lkey_id_alloc: err_used_lkey_id_alloc:
kfree(region_12kb); kfree(region_12kb);
return err; return err;
...@@ -161,7 +159,7 @@ mlxsw_sp_acl_atcam_region_12kb_fini(struct mlxsw_sp_acl_atcam_region *aregion) ...@@ -161,7 +159,7 @@ mlxsw_sp_acl_atcam_region_12kb_fini(struct mlxsw_sp_acl_atcam_region *aregion)
struct mlxsw_sp_acl_atcam_region_12kb *region_12kb = aregion->priv; struct mlxsw_sp_acl_atcam_region_12kb *region_12kb = aregion->priv;
rhashtable_destroy(&region_12kb->lkey_ht); rhashtable_destroy(&region_12kb->lkey_ht);
kfree(region_12kb->used_lkey_id); bitmap_free(region_12kb->used_lkey_id);
kfree(region_12kb); kfree(region_12kb);
} }
......
...@@ -36,7 +36,6 @@ int mlxsw_sp_acl_tcam_init(struct mlxsw_sp *mlxsw_sp, ...@@ -36,7 +36,6 @@ int mlxsw_sp_acl_tcam_init(struct mlxsw_sp *mlxsw_sp,
u64 max_tcam_regions; u64 max_tcam_regions;
u64 max_regions; u64 max_regions;
u64 max_groups; u64 max_groups;
size_t alloc_size;
int err; int err;
mutex_init(&tcam->lock); mutex_init(&tcam->lock);
...@@ -52,15 +51,13 @@ int mlxsw_sp_acl_tcam_init(struct mlxsw_sp *mlxsw_sp, ...@@ -52,15 +51,13 @@ int mlxsw_sp_acl_tcam_init(struct mlxsw_sp *mlxsw_sp,
if (max_tcam_regions < max_regions) if (max_tcam_regions < max_regions)
max_regions = max_tcam_regions; max_regions = max_tcam_regions;
alloc_size = sizeof(tcam->used_regions[0]) * BITS_TO_LONGS(max_regions); tcam->used_regions = bitmap_zalloc(max_regions, GFP_KERNEL);
tcam->used_regions = kzalloc(alloc_size, GFP_KERNEL);
if (!tcam->used_regions) if (!tcam->used_regions)
return -ENOMEM; return -ENOMEM;
tcam->max_regions = max_regions; tcam->max_regions = max_regions;
max_groups = MLXSW_CORE_RES_GET(mlxsw_sp->core, ACL_MAX_GROUPS); max_groups = MLXSW_CORE_RES_GET(mlxsw_sp->core, ACL_MAX_GROUPS);
alloc_size = sizeof(tcam->used_groups[0]) * BITS_TO_LONGS(max_groups); tcam->used_groups = bitmap_zalloc(max_groups, GFP_KERNEL);
tcam->used_groups = kzalloc(alloc_size, GFP_KERNEL);
if (!tcam->used_groups) { if (!tcam->used_groups) {
err = -ENOMEM; err = -ENOMEM;
goto err_alloc_used_groups; goto err_alloc_used_groups;
...@@ -76,9 +73,9 @@ int mlxsw_sp_acl_tcam_init(struct mlxsw_sp *mlxsw_sp, ...@@ -76,9 +73,9 @@ int mlxsw_sp_acl_tcam_init(struct mlxsw_sp *mlxsw_sp,
return 0; return 0;
err_tcam_init: err_tcam_init:
kfree(tcam->used_groups); bitmap_free(tcam->used_groups);
err_alloc_used_groups: err_alloc_used_groups:
kfree(tcam->used_regions); bitmap_free(tcam->used_regions);
return err; return err;
} }
...@@ -89,8 +86,8 @@ void mlxsw_sp_acl_tcam_fini(struct mlxsw_sp *mlxsw_sp, ...@@ -89,8 +86,8 @@ void mlxsw_sp_acl_tcam_fini(struct mlxsw_sp *mlxsw_sp,
mutex_destroy(&tcam->lock); mutex_destroy(&tcam->lock);
ops->fini(mlxsw_sp, tcam->priv); ops->fini(mlxsw_sp, tcam->priv);
kfree(tcam->used_groups); bitmap_free(tcam->used_groups);
kfree(tcam->used_regions); bitmap_free(tcam->used_regions);
} }
int mlxsw_sp_acl_tcam_priority_get(struct mlxsw_sp *mlxsw_sp, int mlxsw_sp_acl_tcam_priority_get(struct mlxsw_sp *mlxsw_sp,
......
...@@ -122,7 +122,6 @@ int mlxsw_sp_counter_pool_init(struct mlxsw_sp *mlxsw_sp) ...@@ -122,7 +122,6 @@ int mlxsw_sp_counter_pool_init(struct mlxsw_sp *mlxsw_sp)
unsigned int sub_pools_count = ARRAY_SIZE(mlxsw_sp_counter_sub_pools); unsigned int sub_pools_count = ARRAY_SIZE(mlxsw_sp_counter_sub_pools);
struct devlink *devlink = priv_to_devlink(mlxsw_sp->core); struct devlink *devlink = priv_to_devlink(mlxsw_sp->core);
struct mlxsw_sp_counter_pool *pool; struct mlxsw_sp_counter_pool *pool;
unsigned int map_size;
int err; int err;
pool = kzalloc(struct_size(pool, sub_pools, sub_pools_count), pool = kzalloc(struct_size(pool, sub_pools, sub_pools_count),
...@@ -143,9 +142,7 @@ int mlxsw_sp_counter_pool_init(struct mlxsw_sp *mlxsw_sp) ...@@ -143,9 +142,7 @@ int mlxsw_sp_counter_pool_init(struct mlxsw_sp *mlxsw_sp)
devlink_resource_occ_get_register(devlink, MLXSW_SP_RESOURCE_COUNTERS, devlink_resource_occ_get_register(devlink, MLXSW_SP_RESOURCE_COUNTERS,
mlxsw_sp_counter_pool_occ_get, pool); mlxsw_sp_counter_pool_occ_get, pool);
map_size = BITS_TO_LONGS(pool->pool_size) * sizeof(unsigned long); pool->usage = bitmap_zalloc(pool->pool_size, GFP_KERNEL);
pool->usage = kzalloc(map_size, GFP_KERNEL);
if (!pool->usage) { if (!pool->usage) {
err = -ENOMEM; err = -ENOMEM;
goto err_usage_alloc; goto err_usage_alloc;
...@@ -158,7 +155,7 @@ int mlxsw_sp_counter_pool_init(struct mlxsw_sp *mlxsw_sp) ...@@ -158,7 +155,7 @@ int mlxsw_sp_counter_pool_init(struct mlxsw_sp *mlxsw_sp)
return 0; return 0;
err_sub_pools_init: err_sub_pools_init:
kfree(pool->usage); bitmap_free(pool->usage);
err_usage_alloc: err_usage_alloc:
devlink_resource_occ_get_unregister(devlink, devlink_resource_occ_get_unregister(devlink,
MLXSW_SP_RESOURCE_COUNTERS); MLXSW_SP_RESOURCE_COUNTERS);
...@@ -176,7 +173,7 @@ void mlxsw_sp_counter_pool_fini(struct mlxsw_sp *mlxsw_sp) ...@@ -176,7 +173,7 @@ void mlxsw_sp_counter_pool_fini(struct mlxsw_sp *mlxsw_sp)
WARN_ON(find_first_bit(pool->usage, pool->pool_size) != WARN_ON(find_first_bit(pool->usage, pool->pool_size) !=
pool->pool_size); pool->pool_size);
WARN_ON(atomic_read(&pool->active_entries_count)); WARN_ON(atomic_read(&pool->active_entries_count));
kfree(pool->usage); bitmap_free(pool->usage);
devlink_resource_occ_get_unregister(devlink, devlink_resource_occ_get_unregister(devlink,
MLXSW_SP_RESOURCE_COUNTERS); MLXSW_SP_RESOURCE_COUNTERS);
kfree(pool); kfree(pool);
......
...@@ -1635,16 +1635,13 @@ mlxsw_sp_mid *__mlxsw_sp_mc_alloc(struct mlxsw_sp *mlxsw_sp, ...@@ -1635,16 +1635,13 @@ mlxsw_sp_mid *__mlxsw_sp_mc_alloc(struct mlxsw_sp *mlxsw_sp,
u16 fid) u16 fid)
{ {
struct mlxsw_sp_mid *mid; struct mlxsw_sp_mid *mid;
size_t alloc_size;
mid = kzalloc(sizeof(*mid), GFP_KERNEL); mid = kzalloc(sizeof(*mid), GFP_KERNEL);
if (!mid) if (!mid)
return NULL; return NULL;
alloc_size = sizeof(unsigned long) * mid->ports_in_mid = bitmap_zalloc(mlxsw_core_max_ports(mlxsw_sp->core),
BITS_TO_LONGS(mlxsw_core_max_ports(mlxsw_sp->core)); GFP_KERNEL);
mid->ports_in_mid = kzalloc(alloc_size, GFP_KERNEL);
if (!mid->ports_in_mid) if (!mid->ports_in_mid)
goto err_ports_in_mid_alloc; goto err_ports_in_mid_alloc;
...@@ -1663,7 +1660,7 @@ mlxsw_sp_mid *__mlxsw_sp_mc_alloc(struct mlxsw_sp *mlxsw_sp, ...@@ -1663,7 +1660,7 @@ mlxsw_sp_mid *__mlxsw_sp_mc_alloc(struct mlxsw_sp *mlxsw_sp,
return mid; return mid;
err_write_mdb_entry: err_write_mdb_entry:
kfree(mid->ports_in_mid); bitmap_free(mid->ports_in_mid);
err_ports_in_mid_alloc: err_ports_in_mid_alloc:
kfree(mid); kfree(mid);
return NULL; return NULL;
...@@ -1680,7 +1677,7 @@ static int mlxsw_sp_port_remove_from_mid(struct mlxsw_sp_port *mlxsw_sp_port, ...@@ -1680,7 +1677,7 @@ static int mlxsw_sp_port_remove_from_mid(struct mlxsw_sp_port *mlxsw_sp_port,
mlxsw_core_max_ports(mlxsw_sp->core))) { mlxsw_core_max_ports(mlxsw_sp->core))) {
err = mlxsw_sp_mc_remove_mdb_entry(mlxsw_sp, mid); err = mlxsw_sp_mc_remove_mdb_entry(mlxsw_sp, mid);
list_del(&mid->list); list_del(&mid->list);
kfree(mid->ports_in_mid); bitmap_free(mid->ports_in_mid);
kfree(mid); kfree(mid);
} }
return err; return err;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment