Commit 6ffdc7b9 authored by Cihangir Akturk's avatar Cihangir Akturk Committed by Greg Kroah-Hartman

staging: fsl-mc: get rid of mutex_locked variables

Remove mutex_locked variables which are used to determine whether mutex is
locked, instead add another label to unlock mutex on premature exits due to
an error.

This patch also addresses the folowing warnings reported by coccinelle:

drivers/staging/fsl-mc/bus/mc-allocator.c:237:1-7: preceding lock on line 204
drivers/staging/fsl-mc/bus/mc-allocator.c:89:1-7: preceding lock on line 57
drivers/staging/fsl-mc/bus/mc-allocator.c:157:1-7: preceding lock on line 124
Signed-off-by: default avatarCihangir Akturk <cakturk@gmail.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent c3f46348
...@@ -39,7 +39,6 @@ static int __must_check fsl_mc_resource_pool_add_device(struct fsl_mc_bus ...@@ -39,7 +39,6 @@ static int __must_check fsl_mc_resource_pool_add_device(struct fsl_mc_bus
struct fsl_mc_resource *resource; struct fsl_mc_resource *resource;
struct fsl_mc_device *mc_bus_dev = &mc_bus->mc_dev; struct fsl_mc_device *mc_bus_dev = &mc_bus->mc_dev;
int error = -EINVAL; int error = -EINVAL;
bool mutex_locked = false;
if (WARN_ON(pool_type < 0 || pool_type >= FSL_MC_NUM_POOL_TYPES)) if (WARN_ON(pool_type < 0 || pool_type >= FSL_MC_NUM_POOL_TYPES))
goto out; goto out;
...@@ -55,13 +54,12 @@ static int __must_check fsl_mc_resource_pool_add_device(struct fsl_mc_bus ...@@ -55,13 +54,12 @@ static int __must_check fsl_mc_resource_pool_add_device(struct fsl_mc_bus
goto out; goto out;
mutex_lock(&res_pool->mutex); mutex_lock(&res_pool->mutex);
mutex_locked = true;
if (WARN_ON(res_pool->max_count < 0)) if (WARN_ON(res_pool->max_count < 0))
goto out; goto out_unlock;
if (WARN_ON(res_pool->free_count < 0 || if (WARN_ON(res_pool->free_count < 0 ||
res_pool->free_count > res_pool->max_count)) res_pool->free_count > res_pool->max_count))
goto out; goto out_unlock;
resource = devm_kzalloc(&mc_bus_dev->dev, sizeof(*resource), resource = devm_kzalloc(&mc_bus_dev->dev, sizeof(*resource),
GFP_KERNEL); GFP_KERNEL);
...@@ -69,7 +67,7 @@ static int __must_check fsl_mc_resource_pool_add_device(struct fsl_mc_bus ...@@ -69,7 +67,7 @@ static int __must_check fsl_mc_resource_pool_add_device(struct fsl_mc_bus
error = -ENOMEM; error = -ENOMEM;
dev_err(&mc_bus_dev->dev, dev_err(&mc_bus_dev->dev,
"Failed to allocate memory for fsl_mc_resource\n"); "Failed to allocate memory for fsl_mc_resource\n");
goto out; goto out_unlock;
} }
resource->type = pool_type; resource->type = pool_type;
...@@ -82,10 +80,9 @@ static int __must_check fsl_mc_resource_pool_add_device(struct fsl_mc_bus ...@@ -82,10 +80,9 @@ static int __must_check fsl_mc_resource_pool_add_device(struct fsl_mc_bus
res_pool->free_count++; res_pool->free_count++;
res_pool->max_count++; res_pool->max_count++;
error = 0; error = 0;
out_unlock:
mutex_unlock(&res_pool->mutex);
out: out:
if (mutex_locked)
mutex_unlock(&res_pool->mutex);
return error; return error;
} }
...@@ -106,7 +103,6 @@ static int __must_check fsl_mc_resource_pool_remove_device(struct fsl_mc_device ...@@ -106,7 +103,6 @@ static int __must_check fsl_mc_resource_pool_remove_device(struct fsl_mc_device
struct fsl_mc_resource_pool *res_pool; struct fsl_mc_resource_pool *res_pool;
struct fsl_mc_resource *resource; struct fsl_mc_resource *resource;
int error = -EINVAL; int error = -EINVAL;
bool mutex_locked = false;
if (WARN_ON(!FSL_MC_IS_ALLOCATABLE(mc_dev->obj_desc.type))) if (WARN_ON(!FSL_MC_IS_ALLOCATABLE(mc_dev->obj_desc.type)))
goto out; goto out;
...@@ -122,13 +118,12 @@ static int __must_check fsl_mc_resource_pool_remove_device(struct fsl_mc_device ...@@ -122,13 +118,12 @@ static int __must_check fsl_mc_resource_pool_remove_device(struct fsl_mc_device
goto out; goto out;
mutex_lock(&res_pool->mutex); mutex_lock(&res_pool->mutex);
mutex_locked = true;
if (WARN_ON(res_pool->max_count <= 0)) if (WARN_ON(res_pool->max_count <= 0))
goto out; goto out_unlock;
if (WARN_ON(res_pool->free_count <= 0 || if (WARN_ON(res_pool->free_count <= 0 ||
res_pool->free_count > res_pool->max_count)) res_pool->free_count > res_pool->max_count))
goto out; goto out_unlock;
/* /*
* If the device is currently allocated, its resource is not * If the device is currently allocated, its resource is not
...@@ -139,7 +134,7 @@ static int __must_check fsl_mc_resource_pool_remove_device(struct fsl_mc_device ...@@ -139,7 +134,7 @@ static int __must_check fsl_mc_resource_pool_remove_device(struct fsl_mc_device
dev_err(&mc_bus_dev->dev, dev_err(&mc_bus_dev->dev,
"Device %s cannot be removed from resource pool\n", "Device %s cannot be removed from resource pool\n",
dev_name(&mc_dev->dev)); dev_name(&mc_dev->dev));
goto out; goto out_unlock;
} }
list_del(&resource->node); list_del(&resource->node);
...@@ -150,10 +145,9 @@ static int __must_check fsl_mc_resource_pool_remove_device(struct fsl_mc_device ...@@ -150,10 +145,9 @@ static int __must_check fsl_mc_resource_pool_remove_device(struct fsl_mc_device
devm_kfree(&mc_bus_dev->dev, resource); devm_kfree(&mc_bus_dev->dev, resource);
mc_dev->resource = NULL; mc_dev->resource = NULL;
error = 0; error = 0;
out_unlock:
mutex_unlock(&res_pool->mutex);
out: out:
if (mutex_locked)
mutex_unlock(&res_pool->mutex);
return error; return error;
} }
...@@ -188,21 +182,19 @@ int __must_check fsl_mc_resource_allocate(struct fsl_mc_bus *mc_bus, ...@@ -188,21 +182,19 @@ int __must_check fsl_mc_resource_allocate(struct fsl_mc_bus *mc_bus,
struct fsl_mc_resource *resource; struct fsl_mc_resource *resource;
struct fsl_mc_device *mc_bus_dev = &mc_bus->mc_dev; struct fsl_mc_device *mc_bus_dev = &mc_bus->mc_dev;
int error = -EINVAL; int error = -EINVAL;
bool mutex_locked = false;
BUILD_BUG_ON(ARRAY_SIZE(fsl_mc_pool_type_strings) != BUILD_BUG_ON(ARRAY_SIZE(fsl_mc_pool_type_strings) !=
FSL_MC_NUM_POOL_TYPES); FSL_MC_NUM_POOL_TYPES);
*new_resource = NULL; *new_resource = NULL;
if (WARN_ON(pool_type < 0 || pool_type >= FSL_MC_NUM_POOL_TYPES)) if (WARN_ON(pool_type < 0 || pool_type >= FSL_MC_NUM_POOL_TYPES))
goto error; goto out;
res_pool = &mc_bus->resource_pools[pool_type]; res_pool = &mc_bus->resource_pools[pool_type];
if (WARN_ON(res_pool->mc_bus != mc_bus)) if (WARN_ON(res_pool->mc_bus != mc_bus))
goto error; goto out;
mutex_lock(&res_pool->mutex); mutex_lock(&res_pool->mutex);
mutex_locked = true;
resource = list_first_entry_or_null(&res_pool->free_list, resource = list_first_entry_or_null(&res_pool->free_list,
struct fsl_mc_resource, node); struct fsl_mc_resource, node);
...@@ -212,28 +204,26 @@ int __must_check fsl_mc_resource_allocate(struct fsl_mc_bus *mc_bus, ...@@ -212,28 +204,26 @@ int __must_check fsl_mc_resource_allocate(struct fsl_mc_bus *mc_bus,
dev_err(&mc_bus_dev->dev, dev_err(&mc_bus_dev->dev,
"No more resources of type %s left\n", "No more resources of type %s left\n",
fsl_mc_pool_type_strings[pool_type]); fsl_mc_pool_type_strings[pool_type]);
goto error; goto out_unlock;
} }
if (WARN_ON(resource->type != pool_type)) if (WARN_ON(resource->type != pool_type))
goto error; goto out_unlock;
if (WARN_ON(resource->parent_pool != res_pool)) if (WARN_ON(resource->parent_pool != res_pool))
goto error; goto out_unlock;
if (WARN_ON(res_pool->free_count <= 0 || if (WARN_ON(res_pool->free_count <= 0 ||
res_pool->free_count > res_pool->max_count)) res_pool->free_count > res_pool->max_count))
goto error; goto out_unlock;
list_del(&resource->node); list_del(&resource->node);
INIT_LIST_HEAD(&resource->node); INIT_LIST_HEAD(&resource->node);
res_pool->free_count--; res_pool->free_count--;
error = 0;
out_unlock:
mutex_unlock(&res_pool->mutex); mutex_unlock(&res_pool->mutex);
*new_resource = resource; *new_resource = resource;
return 0; out:
error:
if (mutex_locked)
mutex_unlock(&res_pool->mutex);
return error; return error;
} }
EXPORT_SYMBOL_GPL(fsl_mc_resource_allocate); EXPORT_SYMBOL_GPL(fsl_mc_resource_allocate);
...@@ -241,26 +231,23 @@ EXPORT_SYMBOL_GPL(fsl_mc_resource_allocate); ...@@ -241,26 +231,23 @@ EXPORT_SYMBOL_GPL(fsl_mc_resource_allocate);
void fsl_mc_resource_free(struct fsl_mc_resource *resource) void fsl_mc_resource_free(struct fsl_mc_resource *resource)
{ {
struct fsl_mc_resource_pool *res_pool; struct fsl_mc_resource_pool *res_pool;
bool mutex_locked = false;
res_pool = resource->parent_pool; res_pool = resource->parent_pool;
if (WARN_ON(resource->type != res_pool->type)) if (WARN_ON(resource->type != res_pool->type))
goto out; return;
mutex_lock(&res_pool->mutex); mutex_lock(&res_pool->mutex);
mutex_locked = true;
if (WARN_ON(res_pool->free_count < 0 || if (WARN_ON(res_pool->free_count < 0 ||
res_pool->free_count >= res_pool->max_count)) res_pool->free_count >= res_pool->max_count))
goto out; goto out_unlock;
if (WARN_ON(!list_empty(&resource->node))) if (WARN_ON(!list_empty(&resource->node)))
goto out; goto out_unlock;
list_add_tail(&resource->node, &res_pool->free_list); list_add_tail(&resource->node, &res_pool->free_list);
res_pool->free_count++; res_pool->free_count++;
out: out_unlock:
if (mutex_locked) mutex_unlock(&res_pool->mutex);
mutex_unlock(&res_pool->mutex);
} }
EXPORT_SYMBOL_GPL(fsl_mc_resource_free); EXPORT_SYMBOL_GPL(fsl_mc_resource_free);
......
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