From: Suniel Mahesh <[email protected]>
Hi
This patch series replaces the current API's which request
for device resources in driver probe, with devm_*() functions
of the kernel framework as recommended by the kernel community.
Doing so simplifies driver cleanup paths and code organization.
The current set of API's whch request for device resources are
not device managed.
The devm_*() functions of the kernel framework are kernel managed
resources which the kernel tracks and then automatically releases
them when the device goes away.
Patch 1/3, replaces kzalloc with devm_kzalloc, kfree's and blank
lines are removed accordingly.
Patch 2/3, utilizes devm_ioremap_resource for map and unmap of
device resources.
request_mem_region(), ioremap() and corresponding error handling
is replaced with devm_ioremap_resource().
release_mem_region() and iounmap() are dropped. A struct member in
struct ssi_drvdata is dropped as it seemed redundant. Log messages
adjusted accordingly.
Patch 3/3, replaces platform_get_resource(), request_irq() and
corresponding error handling with platform_get_irq() and
devm_request_irq().
free_irq is not required any more, devm_request_irq() free's it on
driver detach.
A struct member in struct ssi_drvdata and a bool variable in driver probe
are dropped as they seemed redundant.
Changed type of a member in struct ssi_drvdata to use it with platform_get_irq().
Log messages adjusted accordingly.
Note:
Patch was tested and built(ARCH=arm) on next-20170714.
No build issues reported, however it was not tested on
real hardware.
Please drop any patch if they break the flow. As per my
analysis these changes should not create a problem.
Thanks, Suniel
Suniel Mahesh (3):
staging: ccree: Replace kzalloc with devm_kzalloc
staging: ccree: Convert to devm_ioremap_resource for map, unmap
staging: ccree: Use platform_get_irq and devm_request_irq
drivers/staging/ccree/ssi_driver.c | 92 +++++++++++---------------------------
drivers/staging/ccree/ssi_driver.h | 4 +-
2 files changed, 26 insertions(+), 70 deletions(-)
--
1.9.1
From: Suniel Mahesh <[email protected]>
It is recommended to use managed function devm_kzalloc, which
simplifies driver cleanup paths and driver code.
This patch does the following:
(a) replace kzalloc with devm_kzalloc.
(b) drop kfree(), because memory allocated with devm_kzalloc() is
automatically freed on driver detach, otherwise it leads to a double
free.
(c) remove unnecessary blank lines.
Signed-off-by: Suniel Mahesh <[email protected]>
---
Note:
- Patch was tested and built(ARCH=arm) on next-20170714.
No build issues reported, however it was not tested on
real hardware.
---
drivers/staging/ccree/ssi_driver.c | 10 ++++------
1 file changed, 4 insertions(+), 6 deletions(-)
diff --git a/drivers/staging/ccree/ssi_driver.c b/drivers/staging/ccree/ssi_driver.c
index 78709b92..f231ecf 100644
--- a/drivers/staging/ccree/ssi_driver.c
+++ b/drivers/staging/ccree/ssi_driver.c
@@ -224,13 +224,15 @@ static int init_cc_resources(struct platform_device *plat_dev)
struct resource *req_mem_cc_regs = NULL;
void __iomem *cc_base = NULL;
bool irq_registered = false;
- struct ssi_drvdata *new_drvdata = kzalloc(sizeof(struct ssi_drvdata), GFP_KERNEL);
+ struct ssi_drvdata *new_drvdata;
struct device *dev = &plat_dev->dev;
struct device_node *np = dev->of_node;
u32 signature_val;
int rc = 0;
- if (unlikely(!new_drvdata)) {
+ new_drvdata = devm_kzalloc(&plat_dev->dev, sizeof(struct ssi_drvdata),
+ GFP_KERNEL);
+ if (!new_drvdata) {
SSI_LOG_ERR("Failed to allocate drvdata");
rc = -ENOMEM;
goto init_cc_res_err;
@@ -431,10 +433,8 @@ static int init_cc_resources(struct platform_device *plat_dev)
resource_size(new_drvdata->res_mem));
new_drvdata->res_mem = NULL;
}
- kfree(new_drvdata);
dev_set_drvdata(&plat_dev->dev, NULL);
}
-
return rc;
}
@@ -475,8 +475,6 @@ static void cleanup_cc_resources(struct platform_device *plat_dev)
drvdata->cc_base = NULL;
drvdata->res_mem = NULL;
}
-
- kfree(drvdata);
dev_set_drvdata(&plat_dev->dev, NULL);
}
--
1.9.1
From: Suniel Mahesh <[email protected]>
It is recommended to use managed function devm_ioremap_resource(),
which simplifies driver cleanup paths and driver code.
This patch does the following:
(a) replace request_mem_region(), ioremap() and corresponding error
handling with devm_ioremap_resource().
(b) remove struct resource pointer(res_mem) in struct ssi_drvdata as it
seems redundant, use struct resource pointer which is defined locally and
adjust return value of platform_get_resource() accordingly.
(c) release_mem_region() and iounmap() are dropped, since devm_ioremap_
resource() releases and unmaps mem region on driver detach.
(d) adjust log messages accordingly and remove any blank lines.
Signed-off-by: Suniel Mahesh <[email protected]>
---
Note:
- Patch was tested and built(ARCH=arm) on next-20170714.
No build issues reported, however it was not tested on
real hardware.
---
drivers/staging/ccree/ssi_driver.c | 59 ++++++++++----------------------------
drivers/staging/ccree/ssi_driver.h | 1 -
2 files changed, 15 insertions(+), 45 deletions(-)
diff --git a/drivers/staging/ccree/ssi_driver.c b/drivers/staging/ccree/ssi_driver.c
index f231ecf..dca0ce8 100644
--- a/drivers/staging/ccree/ssi_driver.c
+++ b/drivers/staging/ccree/ssi_driver.c
@@ -247,34 +247,21 @@ static int init_cc_resources(struct platform_device *plat_dev)
dev_set_drvdata(&plat_dev->dev, new_drvdata);
/* Get device resources */
/* First CC registers space */
- new_drvdata->res_mem = platform_get_resource(plat_dev, IORESOURCE_MEM, 0);
- if (unlikely(!new_drvdata->res_mem)) {
- SSI_LOG_ERR("Failed getting IO memory resource\n");
- rc = -ENODEV;
- goto init_cc_res_err;
- }
- SSI_LOG_DEBUG("Got MEM resource (%s): start=0x%llX end=0x%llX\n",
- new_drvdata->res_mem->name,
- (unsigned long long)new_drvdata->res_mem->start,
- (unsigned long long)new_drvdata->res_mem->end);
+ req_mem_cc_regs = platform_get_resource(plat_dev, IORESOURCE_MEM, 0);
/* Map registers space */
- req_mem_cc_regs = request_mem_region(new_drvdata->res_mem->start, resource_size(new_drvdata->res_mem), "arm_cc7x_regs");
- if (unlikely(!req_mem_cc_regs)) {
- SSI_LOG_ERR("Couldn't allocate registers memory region at "
- "0x%08X\n", (unsigned int)new_drvdata->res_mem->start);
- rc = -EBUSY;
- goto init_cc_res_err;
- }
- cc_base = ioremap(new_drvdata->res_mem->start, resource_size(new_drvdata->res_mem));
- if (unlikely(!cc_base)) {
- SSI_LOG_ERR("ioremap[CC](0x%08X,0x%08X) failed\n",
- (unsigned int)new_drvdata->res_mem->start, (unsigned int)resource_size(new_drvdata->res_mem));
- rc = -ENOMEM;
+ new_drvdata->cc_base = devm_ioremap_resource(&plat_dev->dev,
+ req_mem_cc_regs);
+ if (IS_ERR(new_drvdata->cc_base)) {
+ rc = PTR_ERR(new_drvdata->cc_base);
goto init_cc_res_err;
}
- SSI_LOG_DEBUG("CC registers mapped from %pa to 0x%p\n", &new_drvdata->res_mem->start, cc_base);
- new_drvdata->cc_base = cc_base;
-
+ SSI_LOG_DEBUG("Got MEM resource (%s): start=0x%llX end=0x%llX\n",
+ req_mem_cc_regs->name,
+ (unsigned long long)req_mem_cc_regs->start,
+ (unsigned long long)req_mem_cc_regs->end);
+ SSI_LOG_DEBUG("CC registers mapped from %pa to 0x%p\n",
+ &req_mem_cc_regs->start, new_drvdata->cc_base);
+ cc_base = new_drvdata->cc_base;
/* Then IRQ */
new_drvdata->res_irq = platform_get_resource(plat_dev, IORESOURCE_IRQ, 0);
if (unlikely(!new_drvdata->res_irq)) {
@@ -421,17 +408,9 @@ static int init_cc_resources(struct platform_device *plat_dev)
#ifdef ENABLE_CC_SYSFS
ssi_sysfs_fini();
#endif
-
- if (req_mem_cc_regs) {
- if (irq_registered) {
- free_irq(new_drvdata->res_irq->start, new_drvdata);
- new_drvdata->res_irq = NULL;
- iounmap(cc_base);
- new_drvdata->cc_base = NULL;
- }
- release_mem_region(new_drvdata->res_mem->start,
- resource_size(new_drvdata->res_mem));
- new_drvdata->res_mem = NULL;
+ if (irq_registered) {
+ free_irq(new_drvdata->res_irq->start, new_drvdata);
+ new_drvdata->res_irq = NULL;
}
dev_set_drvdata(&plat_dev->dev, NULL);
}
@@ -467,14 +446,6 @@ static void cleanup_cc_resources(struct platform_device *plat_dev)
cc_clk_off(drvdata);
free_irq(drvdata->res_irq->start, drvdata);
drvdata->res_irq = NULL;
-
- if (drvdata->cc_base) {
- iounmap(drvdata->cc_base);
- release_mem_region(drvdata->res_mem->start,
- resource_size(drvdata->res_mem));
- drvdata->cc_base = NULL;
- drvdata->res_mem = NULL;
- }
dev_set_drvdata(&plat_dev->dev, NULL);
}
diff --git a/drivers/staging/ccree/ssi_driver.h b/drivers/staging/ccree/ssi_driver.h
index c1ed61f..4b38fe2 100644
--- a/drivers/staging/ccree/ssi_driver.h
+++ b/drivers/staging/ccree/ssi_driver.h
@@ -129,7 +129,6 @@ struct ssi_crypto_req {
* @fw_ver: SeP loaded firmware version
*/
struct ssi_drvdata {
- struct resource *res_mem;
struct resource *res_irq;
void __iomem *cc_base;
unsigned int irq;
--
1.9.1
From: Suniel Mahesh <[email protected]>
It is recommended to use managed function devm_request_irq(),
which simplifies driver cleanup paths and driver code.
This patch does the following:
(a) replace platform_get_resource(), request_irq() and corresponding
error handling with platform_get_irq() and devm_request_irq().
(b) remove struct resource pointer(res_irq) in struct ssi_drvdata as
it seems redundant.
(c) change type of member irq in struct ssi_drvdata from unsigned int
to int, as return type of platform_get_irq is int and can be used in
error handling.
(d) remove irq_registered variable from driver probe as it seems
redundant.
(e) free_irq is not required any more, devm_request_irq() free's it
on driver detach.
(f) adjust log messages accordingly and remove any blank lines.
Signed-off-by: Suniel Mahesh <[email protected]>
---
Note:
- Patch was tested and built(ARCH=arm) on next-20170714.
No build issues reported, however it was not tested on
real hardware.
---
drivers/staging/ccree/ssi_driver.c | 29 +++++++++--------------------
drivers/staging/ccree/ssi_driver.h | 3 +--
2 files changed, 10 insertions(+), 22 deletions(-)
diff --git a/drivers/staging/ccree/ssi_driver.c b/drivers/staging/ccree/ssi_driver.c
index dca0ce8..11b62d0 100644
--- a/drivers/staging/ccree/ssi_driver.c
+++ b/drivers/staging/ccree/ssi_driver.c
@@ -223,7 +223,6 @@ static int init_cc_resources(struct platform_device *plat_dev)
{
struct resource *req_mem_cc_regs = NULL;
void __iomem *cc_base = NULL;
- bool irq_registered = false;
struct ssi_drvdata *new_drvdata;
struct device *dev = &plat_dev->dev;
struct device_node *np = dev->of_node;
@@ -263,25 +262,22 @@ static int init_cc_resources(struct platform_device *plat_dev)
&req_mem_cc_regs->start, new_drvdata->cc_base);
cc_base = new_drvdata->cc_base;
/* Then IRQ */
- new_drvdata->res_irq = platform_get_resource(plat_dev, IORESOURCE_IRQ, 0);
- if (unlikely(!new_drvdata->res_irq)) {
+ new_drvdata->irq = platform_get_irq(plat_dev, 0);
+ if (new_drvdata->irq < 0) {
SSI_LOG_ERR("Failed getting IRQ resource\n");
- rc = -ENODEV;
+ rc = new_drvdata->irq;
goto init_cc_res_err;
}
- rc = request_irq(new_drvdata->res_irq->start, cc_isr,
- IRQF_SHARED, "arm_cc7x", new_drvdata);
- if (unlikely(rc != 0)) {
- SSI_LOG_ERR("Could not register to interrupt %llu\n",
- (unsigned long long)new_drvdata->res_irq->start);
+ rc = devm_request_irq(&plat_dev->dev, new_drvdata->irq, cc_isr,
+ IRQF_SHARED, "arm_cc7x", new_drvdata);
+ if (rc) {
+ SSI_LOG_ERR("Could not register to interrupt: %d\n",
+ new_drvdata->irq);
goto init_cc_res_err;
}
init_completion(&new_drvdata->icache_setup_completion);
- irq_registered = true;
- SSI_LOG_DEBUG("Registered to IRQ (%s) %llu\n",
- new_drvdata->res_irq->name,
- (unsigned long long)new_drvdata->res_irq->start);
+ SSI_LOG_DEBUG("Registered to IRQ: %d\n", new_drvdata->irq);
new_drvdata->plat_dev = plat_dev;
@@ -408,10 +404,6 @@ static int init_cc_resources(struct platform_device *plat_dev)
#ifdef ENABLE_CC_SYSFS
ssi_sysfs_fini();
#endif
- if (irq_registered) {
- free_irq(new_drvdata->res_irq->start, new_drvdata);
- new_drvdata->res_irq = NULL;
- }
dev_set_drvdata(&plat_dev->dev, NULL);
}
return rc;
@@ -441,11 +433,8 @@ static void cleanup_cc_resources(struct platform_device *plat_dev)
#ifdef ENABLE_CC_SYSFS
ssi_sysfs_fini();
#endif
-
fini_cc_regs(drvdata);
cc_clk_off(drvdata);
- free_irq(drvdata->res_irq->start, drvdata);
- drvdata->res_irq = NULL;
dev_set_drvdata(&plat_dev->dev, NULL);
}
diff --git a/drivers/staging/ccree/ssi_driver.h b/drivers/staging/ccree/ssi_driver.h
index 4b38fe2..6fcd151 100644
--- a/drivers/staging/ccree/ssi_driver.h
+++ b/drivers/staging/ccree/ssi_driver.h
@@ -129,9 +129,8 @@ struct ssi_crypto_req {
* @fw_ver: SeP loaded firmware version
*/
struct ssi_drvdata {
- struct resource *res_irq;
void __iomem *cc_base;
- unsigned int irq;
+ int irq;
u32 irq_mask;
u32 fw_ver;
/* Calibration time of start/stop
--
1.9.1
On Sat, Jul 15, 2017 at 01:21:54PM +0530, [email protected] wrote:
> From: Suniel Mahesh <[email protected]>
>
> It is recommended to use managed function devm_kzalloc, which
> simplifies driver cleanup paths and driver code.
> This patch does the following:
> (a) replace kzalloc with devm_kzalloc.
> (b) drop kfree(), because memory allocated with devm_kzalloc() is
> automatically freed on driver detach, otherwise it leads to a double
> free.
> (c) remove unnecessary blank lines.
>
> Signed-off-by: Suniel Mahesh <[email protected]>
> ---
> Note:
> - Patch was tested and built(ARCH=arm) on next-20170714.
> No build issues reported, however it was not tested on
> real hardware.
> ---
> drivers/staging/ccree/ssi_driver.c | 10 ++++------
> 1 file changed, 4 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/staging/ccree/ssi_driver.c b/drivers/staging/ccree/ssi_driver.c
> index 78709b92..f231ecf 100644
> --- a/drivers/staging/ccree/ssi_driver.c
> +++ b/drivers/staging/ccree/ssi_driver.c
> @@ -224,13 +224,15 @@ static int init_cc_resources(struct platform_device *plat_dev)
> struct resource *req_mem_cc_regs = NULL;
> void __iomem *cc_base = NULL;
> bool irq_registered = false;
> - struct ssi_drvdata *new_drvdata = kzalloc(sizeof(struct ssi_drvdata), GFP_KERNEL);
> + struct ssi_drvdata *new_drvdata;
> struct device *dev = &plat_dev->dev;
> struct device_node *np = dev->of_node;
> u32 signature_val;
> int rc = 0;
>
> - if (unlikely(!new_drvdata)) {
> + new_drvdata = devm_kzalloc(&plat_dev->dev, sizeof(struct ssi_drvdata),
sizeof(*new_drvdata), right?
thanks,
greg k-h
On Monday 17 July 2017 06:03 PM, Greg KH wrote:
> On Sat, Jul 15, 2017 at 01:21:54PM +0530, [email protected] wrote:
>> From: Suniel Mahesh <[email protected]>
>>
>> It is recommended to use managed function devm_kzalloc, which
>> simplifies driver cleanup paths and driver code.
>> This patch does the following:
>> (a) replace kzalloc with devm_kzalloc.
>> (b) drop kfree(), because memory allocated with devm_kzalloc() is
>> automatically freed on driver detach, otherwise it leads to a double
>> free.
>> (c) remove unnecessary blank lines.
>>
>> Signed-off-by: Suniel Mahesh <[email protected]>
>> ---
>> Note:
>> - Patch was tested and built(ARCH=arm) on next-20170714.
>> No build issues reported, however it was not tested on
>> real hardware.
>> ---
>> drivers/staging/ccree/ssi_driver.c | 10 ++++------
>> 1 file changed, 4 insertions(+), 6 deletions(-)
>>
>> diff --git a/drivers/staging/ccree/ssi_driver.c b/drivers/staging/ccree/ssi_driver.c
>> index 78709b92..f231ecf 100644
>> --- a/drivers/staging/ccree/ssi_driver.c
>> +++ b/drivers/staging/ccree/ssi_driver.c
>> @@ -224,13 +224,15 @@ static int init_cc_resources(struct platform_device *plat_dev)
>> struct resource *req_mem_cc_regs = NULL;
>> void __iomem *cc_base = NULL;
>> bool irq_registered = false;
>> - struct ssi_drvdata *new_drvdata = kzalloc(sizeof(struct ssi_drvdata), GFP_KERNEL);
>> + struct ssi_drvdata *new_drvdata;
>> struct device *dev = &plat_dev->dev;
>> struct device_node *np = dev->of_node;
>> u32 signature_val;
>> int rc = 0;
>>
>> - if (unlikely(!new_drvdata)) {
>> + new_drvdata = devm_kzalloc(&plat_dev->dev, sizeof(struct ssi_drvdata),
>
> sizeof(*new_drvdata), right?
>
Yes, sending v2 in a while. Thanks for the review
suniel
> thanks,
>
> greg k-h
>
From: Suniel Mahesh <[email protected]>
It is recommended to use managed function devm_kzalloc, which
simplifies driver cleanup paths and driver code.
This patch does the following:
(a) replace kzalloc with devm_kzalloc.
(b) drop kfree(), because memory allocated with devm_kzalloc() is
automatically freed on driver detach, otherwise it leads to a double
free.
(c) remove unnecessary blank lines.
Signed-off-by: Suniel Mahesh <[email protected]>
---
Changes for v2:
- Changes done as suggested by Greg-KH.
- Rebased on top of next-20170718.
---
Note:
- Patch was tested and built(ARCH=arm) on next-20170718.
No build issues reported, however it was not tested on
real hardware.
---
drivers/staging/ccree/ssi_driver.c | 10 ++++------
1 file changed, 4 insertions(+), 6 deletions(-)
diff --git a/drivers/staging/ccree/ssi_driver.c b/drivers/staging/ccree/ssi_driver.c
index d7b9a63..e918cf4 100644
--- a/drivers/staging/ccree/ssi_driver.c
+++ b/drivers/staging/ccree/ssi_driver.c
@@ -223,13 +223,15 @@ static int init_cc_resources(struct platform_device *plat_dev)
struct resource *req_mem_cc_regs = NULL;
void __iomem *cc_base = NULL;
bool irq_registered = false;
- struct ssi_drvdata *new_drvdata = kzalloc(sizeof(struct ssi_drvdata), GFP_KERNEL);
+ struct ssi_drvdata *new_drvdata;
struct device *dev = &plat_dev->dev;
struct device_node *np = dev->of_node;
u32 signature_val;
int rc = 0;
- if (unlikely(!new_drvdata)) {
+ new_drvdata = devm_kzalloc(&plat_dev->dev, sizeof(*new_drvdata),
+ GFP_KERNEL);
+ if (!new_drvdata) {
SSI_LOG_ERR("Failed to allocate drvdata");
rc = -ENOMEM;
goto init_cc_res_err;
@@ -434,10 +436,8 @@ static int init_cc_resources(struct platform_device *plat_dev)
resource_size(new_drvdata->res_mem));
new_drvdata->res_mem = NULL;
}
- kfree(new_drvdata);
dev_set_drvdata(&plat_dev->dev, NULL);
}
-
return rc;
}
@@ -478,8 +478,6 @@ static void cleanup_cc_resources(struct platform_device *plat_dev)
drvdata->cc_base = NULL;
drvdata->res_mem = NULL;
}
-
- kfree(drvdata);
dev_set_drvdata(&plat_dev->dev, NULL);
}
--
1.9.1
From: Suniel Mahesh <[email protected]>
It is recommended to use managed function devm_request_irq(),
which simplifies driver cleanup paths and driver code.
This patch does the following:
(a) replace platform_get_resource(), request_irq() and corresponding
error handling with platform_get_irq() and devm_request_irq().
(b) remove struct resource pointer(res_irq) in struct ssi_drvdata as
it seems redundant.
(c) change type of member irq in struct ssi_drvdata from unsigned int
to int, as return type of platform_get_irq is int and can be used in
error handling.
(d) remove irq_registered variable from driver probe as it seems
redundant.
(e) free_irq is not required any more, devm_request_irq() free's it
on driver detach.
(f) adjust log messages accordingly and remove any blank lines.
Signed-off-by: Suniel Mahesh <[email protected]>
---
Changes for v2:
- Rebased on top of next-20170718.
---
Note:
- Patch was tested and built(ARCH=arm) on next-20170718.
No build issues reported, however it was not tested on
real hardware.
---
drivers/staging/ccree/ssi_driver.c | 30 +++++++++---------------------
drivers/staging/ccree/ssi_driver.h | 3 +--
2 files changed, 10 insertions(+), 23 deletions(-)
diff --git a/drivers/staging/ccree/ssi_driver.c b/drivers/staging/ccree/ssi_driver.c
index 36b7c92..bac27d4 100644
--- a/drivers/staging/ccree/ssi_driver.c
+++ b/drivers/staging/ccree/ssi_driver.c
@@ -222,7 +222,6 @@ static int init_cc_resources(struct platform_device *plat_dev)
{
struct resource *req_mem_cc_regs = NULL;
void __iomem *cc_base = NULL;
- bool irq_registered = false;
struct ssi_drvdata *new_drvdata;
struct device *dev = &plat_dev->dev;
struct device_node *np = dev->of_node;
@@ -262,26 +261,22 @@ static int init_cc_resources(struct platform_device *plat_dev)
&req_mem_cc_regs->start, new_drvdata->cc_base);
cc_base = new_drvdata->cc_base;
/* Then IRQ */
- new_drvdata->res_irq = platform_get_resource(plat_dev, IORESOURCE_IRQ, 0);
- if (unlikely(!new_drvdata->res_irq)) {
+ new_drvdata->irq = platform_get_irq(plat_dev, 0);
+ if (new_drvdata->irq < 0) {
SSI_LOG_ERR("Failed getting IRQ resource\n");
- rc = -ENODEV;
+ rc = new_drvdata->irq;
goto init_cc_res_err;
}
- rc = request_irq(new_drvdata->res_irq->start, cc_isr,
- IRQF_SHARED, "arm_cc7x", new_drvdata);
- if (unlikely(rc != 0)) {
- SSI_LOG_ERR("Could not register to interrupt %llu\n",
- (unsigned long long)new_drvdata->res_irq->start);
+ rc = devm_request_irq(&plat_dev->dev, new_drvdata->irq, cc_isr,
+ IRQF_SHARED, "arm_cc7x", new_drvdata);
+ if (rc) {
+ SSI_LOG_ERR("Could not register to interrupt %d\n",
+ new_drvdata->irq);
goto init_cc_res_err;
}
init_completion(&new_drvdata->icache_setup_completion);
- irq_registered = true;
- SSI_LOG_DEBUG("Registered to IRQ (%s) %llu\n",
- new_drvdata->res_irq->name,
- (unsigned long long)new_drvdata->res_irq->start);
-
+ SSI_LOG_DEBUG("Registered to IRQ: %d\n", new_drvdata->irq);
new_drvdata->plat_dev = plat_dev;
rc = cc_clk_on(new_drvdata);
@@ -410,10 +405,6 @@ static int init_cc_resources(struct platform_device *plat_dev)
#ifdef ENABLE_CC_SYSFS
ssi_sysfs_fini();
#endif
- if (irq_registered) {
- free_irq(new_drvdata->res_irq->start, new_drvdata);
- new_drvdata->res_irq = NULL;
- }
dev_set_drvdata(&plat_dev->dev, NULL);
}
return rc;
@@ -443,11 +434,8 @@ static void cleanup_cc_resources(struct platform_device *plat_dev)
#ifdef ENABLE_CC_SYSFS
ssi_sysfs_fini();
#endif
-
fini_cc_regs(drvdata);
cc_clk_off(drvdata);
- free_irq(drvdata->res_irq->start, drvdata);
- drvdata->res_irq = NULL;
dev_set_drvdata(&plat_dev->dev, NULL);
}
diff --git a/drivers/staging/ccree/ssi_driver.h b/drivers/staging/ccree/ssi_driver.h
index 518c0bf..88ef370 100644
--- a/drivers/staging/ccree/ssi_driver.h
+++ b/drivers/staging/ccree/ssi_driver.h
@@ -128,9 +128,8 @@ struct ssi_crypto_req {
* @fw_ver: SeP loaded firmware version
*/
struct ssi_drvdata {
- struct resource *res_irq;
void __iomem *cc_base;
- unsigned int irq;
+ int irq;
u32 irq_mask;
u32 fw_ver;
/* Calibration time of start/stop
--
1.9.1
From: Suniel Mahesh <[email protected]>
It is recommended to use managed function devm_ioremap_resource(),
which simplifies driver cleanup paths and driver code.
This patch does the following:
(a) replace request_mem_region(), ioremap() and corresponding error
handling with devm_ioremap_resource().
(b) remove struct resource pointer(res_mem) in struct ssi_drvdata as it
seems redundant, use struct resource pointer which is defined locally and
adjust return value of platform_get_resource() accordingly.
(c) release_mem_region() and iounmap() are dropped, since devm_ioremap_
resource() releases and unmaps mem region on driver detach.
(d) adjust log messages accordingly and remove any blank lines.
Signed-off-by: Suniel Mahesh <[email protected]>
---
Changes for v2:
- format specifiers changed in log messages.
- Rebased on top of next-20170718.
---
Note:
- Patch was tested and built(ARCH=arm) on next-20170718.
No build issues reported, however it was not tested on
real hardware.
---
drivers/staging/ccree/ssi_driver.c | 60 ++++++++++----------------------------
drivers/staging/ccree/ssi_driver.h | 1 -
2 files changed, 15 insertions(+), 46 deletions(-)
diff --git a/drivers/staging/ccree/ssi_driver.c b/drivers/staging/ccree/ssi_driver.c
index e918cf4..36b7c92 100644
--- a/drivers/staging/ccree/ssi_driver.c
+++ b/drivers/staging/ccree/ssi_driver.c
@@ -246,35 +246,21 @@ static int init_cc_resources(struct platform_device *plat_dev)
dev_set_drvdata(&plat_dev->dev, new_drvdata);
/* Get device resources */
/* First CC registers space */
- new_drvdata->res_mem = platform_get_resource(plat_dev, IORESOURCE_MEM, 0);
- if (unlikely(!new_drvdata->res_mem)) {
- SSI_LOG_ERR("Failed getting IO memory resource\n");
- rc = -ENODEV;
- goto init_cc_res_err;
- }
- SSI_LOG_DEBUG("Got MEM resource (%s): start=%pad end=%pad\n",
- new_drvdata->res_mem->name,
- new_drvdata->res_mem->start,
- new_drvdata->res_mem->end);
+ req_mem_cc_regs = platform_get_resource(plat_dev, IORESOURCE_MEM, 0);
/* Map registers space */
- req_mem_cc_regs = request_mem_region(new_drvdata->res_mem->start, resource_size(new_drvdata->res_mem), "arm_cc7x_regs");
- if (unlikely(!req_mem_cc_regs)) {
- SSI_LOG_ERR("Couldn't allocate registers memory region at "
- "0x%08X\n", (unsigned int)new_drvdata->res_mem->start);
- rc = -EBUSY;
- goto init_cc_res_err;
- }
- cc_base = ioremap(new_drvdata->res_mem->start, resource_size(new_drvdata->res_mem));
- if (unlikely(!cc_base)) {
- SSI_LOG_ERR("ioremap[CC](0x%08X,0x%08X) failed\n",
- (unsigned int)new_drvdata->res_mem->start,
- (unsigned int)resource_size(new_drvdata->res_mem));
- rc = -ENOMEM;
+ new_drvdata->cc_base = devm_ioremap_resource(&plat_dev->dev,
+ req_mem_cc_regs);
+ if (IS_ERR(new_drvdata->cc_base)) {
+ rc = PTR_ERR(new_drvdata->cc_base);
goto init_cc_res_err;
}
- SSI_LOG_DEBUG("CC registers mapped from %pa to 0x%p\n", &new_drvdata->res_mem->start, cc_base);
- new_drvdata->cc_base = cc_base;
-
+ SSI_LOG_DEBUG("Got MEM resource (%s): start=%pad end=%pad\n",
+ req_mem_cc_regs->name,
+ req_mem_cc_regs->start,
+ req_mem_cc_regs->end);
+ SSI_LOG_DEBUG("CC registers mapped from %pa to 0x%p\n",
+ &req_mem_cc_regs->start, new_drvdata->cc_base);
+ cc_base = new_drvdata->cc_base;
/* Then IRQ */
new_drvdata->res_irq = platform_get_resource(plat_dev, IORESOURCE_IRQ, 0);
if (unlikely(!new_drvdata->res_irq)) {
@@ -424,17 +410,9 @@ static int init_cc_resources(struct platform_device *plat_dev)
#ifdef ENABLE_CC_SYSFS
ssi_sysfs_fini();
#endif
-
- if (req_mem_cc_regs) {
- if (irq_registered) {
- free_irq(new_drvdata->res_irq->start, new_drvdata);
- new_drvdata->res_irq = NULL;
- iounmap(cc_base);
- new_drvdata->cc_base = NULL;
- }
- release_mem_region(new_drvdata->res_mem->start,
- resource_size(new_drvdata->res_mem));
- new_drvdata->res_mem = NULL;
+ if (irq_registered) {
+ free_irq(new_drvdata->res_irq->start, new_drvdata);
+ new_drvdata->res_irq = NULL;
}
dev_set_drvdata(&plat_dev->dev, NULL);
}
@@ -470,14 +448,6 @@ static void cleanup_cc_resources(struct platform_device *plat_dev)
cc_clk_off(drvdata);
free_irq(drvdata->res_irq->start, drvdata);
drvdata->res_irq = NULL;
-
- if (drvdata->cc_base) {
- iounmap(drvdata->cc_base);
- release_mem_region(drvdata->res_mem->start,
- resource_size(drvdata->res_mem));
- drvdata->cc_base = NULL;
- drvdata->res_mem = NULL;
- }
dev_set_drvdata(&plat_dev->dev, NULL);
}
diff --git a/drivers/staging/ccree/ssi_driver.h b/drivers/staging/ccree/ssi_driver.h
index b6ad89a..518c0bf 100644
--- a/drivers/staging/ccree/ssi_driver.h
+++ b/drivers/staging/ccree/ssi_driver.h
@@ -128,7 +128,6 @@ struct ssi_crypto_req {
* @fw_ver: SeP loaded firmware version
*/
struct ssi_drvdata {
- struct resource *res_mem;
struct resource *res_irq;
void __iomem *cc_base;
unsigned int irq;
--
1.9.1
On Tue, Jul 18, 2017 at 1:58 PM, <[email protected]> wrote:
> From: Suniel Mahesh <[email protected]>
>
> It is recommended to use managed function devm_kzalloc, which
> simplifies driver cleanup paths and driver code.
> This patch does the following:
> (a) replace kzalloc with devm_kzalloc.
> (b) drop kfree(), because memory allocated with devm_kzalloc() is
> automatically freed on driver detach, otherwise it leads to a double
> free.
> (c) remove unnecessary blank lines.
>
> Signed-off-by: Suniel Mahesh <[email protected]>
> ---
> Changes for v2:
>
> - Changes done as suggested by Greg-KH.
> - Rebased on top of next-20170718.
> ---
> Note:
>
> - Patch was tested and built(ARCH=arm) on next-20170718.
> No build issues reported, however it was not tested on
> real hardware.
I verified the patch set on CrytoCell HW and it's fine.
Unfortunately, my coding style fixes patch set made two of these to
fail to apply.
Following up is a rebased patch. There are no changes except resolving
conflicts.
--
Gilad Ben-Yossef
Chief Coffee Drinker
"If you take a class in large-scale robotics, can you end up in a
situation where the homework eats your dog?"
-- Jean-Baptiste Queru
From: Suniel Mahesh <[email protected]>
It is recommended to use managed function devm_kzalloc, which
simplifies driver cleanup paths and driver code.
This patch does the following:
(a) replace kzalloc with devm_kzalloc.
(b) drop kfree(), because memory allocated with devm_kzalloc() is
automatically freed on driver detach, otherwise it leads to a double
free.
(c) remove unnecessary blank lines.
Signed-off-by: Suniel Mahesh <[email protected]>
[gby: rebase on top of latest coding style fixes changes]
Acked-by: Gilad Ben-Yossef <[email protected]>
---
drivers/staging/ccree/ssi_driver.c | 10 ++++------
1 file changed, 4 insertions(+), 6 deletions(-)
diff --git a/drivers/staging/ccree/ssi_driver.c b/drivers/staging/ccree/ssi_driver.c
index 1cae2b7..97dfc2c 100644
--- a/drivers/staging/ccree/ssi_driver.c
+++ b/drivers/staging/ccree/ssi_driver.c
@@ -223,13 +223,15 @@ static int init_cc_resources(struct platform_device *plat_dev)
struct resource *req_mem_cc_regs = NULL;
void __iomem *cc_base = NULL;
bool irq_registered = false;
- struct ssi_drvdata *new_drvdata = kzalloc(sizeof(*new_drvdata), GFP_KERNEL);
+ struct ssi_drvdata *new_drvdata;
struct device *dev = &plat_dev->dev;
struct device_node *np = dev->of_node;
u32 signature_val;
int rc = 0;
- if (unlikely(!new_drvdata)) {
+ new_drvdata = devm_kzalloc(&plat_dev->dev, sizeof(*new_drvdata),
+ GFP_KERNEL);
+ if (!new_drvdata) {
SSI_LOG_ERR("Failed to allocate drvdata");
rc = -ENOMEM;
goto init_cc_res_err;
@@ -434,10 +436,8 @@ static int init_cc_resources(struct platform_device *plat_dev)
resource_size(new_drvdata->res_mem));
new_drvdata->res_mem = NULL;
}
- kfree(new_drvdata);
dev_set_drvdata(&plat_dev->dev, NULL);
}
-
return rc;
}
@@ -478,8 +478,6 @@ static void cleanup_cc_resources(struct platform_device *plat_dev)
drvdata->cc_base = NULL;
drvdata->res_mem = NULL;
}
-
- kfree(drvdata);
dev_set_drvdata(&plat_dev->dev, NULL);
}
--
2.1.4
From: Suniel Mahesh <[email protected]>
It is recommended to use managed function devm_ioremap_resource(),
which simplifies driver cleanup paths and driver code.
This patch does the following:
(a) replace request_mem_region(), ioremap() and corresponding error
handling with devm_ioremap_resource().
(b) remove struct resource pointer(res_mem) in struct ssi_drvdata as it
seems redundant, use struct resource pointer which is defined locally and
adjust return value of platform_get_resource() accordingly.
(c) release_mem_region() and iounmap() are dropped, since devm_ioremap_
resource() releases and unmaps mem region on driver detach.
(d) adjust log messages accordingly and remove any blank lines.
Signed-off-by: Suniel Mahesh <[email protected]>
[gby: rebase on top of latest coding style fixes changes]
Acked-by: Gilad Ben-Yossef <[email protected]>
---
drivers/staging/ccree/ssi_driver.c | 60 ++++++++++----------------------------
drivers/staging/ccree/ssi_driver.h | 1 -
2 files changed, 15 insertions(+), 46 deletions(-)
diff --git a/drivers/staging/ccree/ssi_driver.c b/drivers/staging/ccree/ssi_driver.c
index 97dfc2c..603eb03 100644
--- a/drivers/staging/ccree/ssi_driver.c
+++ b/drivers/staging/ccree/ssi_driver.c
@@ -246,35 +246,21 @@ static int init_cc_resources(struct platform_device *plat_dev)
dev_set_drvdata(&plat_dev->dev, new_drvdata);
/* Get device resources */
/* First CC registers space */
- new_drvdata->res_mem = platform_get_resource(plat_dev, IORESOURCE_MEM, 0);
- if (unlikely(!new_drvdata->res_mem)) {
- SSI_LOG_ERR("Failed getting IO memory resource\n");
- rc = -ENODEV;
- goto init_cc_res_err;
- }
- SSI_LOG_DEBUG("Got MEM resource (%s): start=%pad end=%pad\n",
- new_drvdata->res_mem->name,
- new_drvdata->res_mem->start,
- new_drvdata->res_mem->end);
+ req_mem_cc_regs = platform_get_resource(plat_dev, IORESOURCE_MEM, 0);
/* Map registers space */
- req_mem_cc_regs = request_mem_region(new_drvdata->res_mem->start, resource_size(new_drvdata->res_mem), "arm_cc7x_regs");
- if (unlikely(!req_mem_cc_regs)) {
- SSI_LOG_ERR("Couldn't allocate registers memory region at 0x%08X\n",
- (unsigned int)new_drvdata->res_mem->start);
- rc = -EBUSY;
- goto init_cc_res_err;
- }
- cc_base = ioremap(new_drvdata->res_mem->start, resource_size(new_drvdata->res_mem));
- if (unlikely(!cc_base)) {
- SSI_LOG_ERR("ioremap[CC](0x%08X,0x%08X) failed\n",
- (unsigned int)new_drvdata->res_mem->start,
- (unsigned int)resource_size(new_drvdata->res_mem));
- rc = -ENOMEM;
+ new_drvdata->cc_base = devm_ioremap_resource(&plat_dev->dev,
+ req_mem_cc_regs);
+ if (IS_ERR(new_drvdata->cc_base)) {
+ rc = PTR_ERR(new_drvdata->cc_base);
goto init_cc_res_err;
}
- SSI_LOG_DEBUG("CC registers mapped from %pa to 0x%p\n", &new_drvdata->res_mem->start, cc_base);
- new_drvdata->cc_base = cc_base;
-
+ SSI_LOG_DEBUG("Got MEM resource (%s): start=%pad end=%pad\n",
+ req_mem_cc_regs->name,
+ req_mem_cc_regs->start,
+ req_mem_cc_regs->end);
+ SSI_LOG_DEBUG("CC registers mapped from %pa to 0x%p\n",
+ &req_mem_cc_regs->start, new_drvdata->cc_base);
+ cc_base = new_drvdata->cc_base;
/* Then IRQ */
new_drvdata->res_irq = platform_get_resource(plat_dev, IORESOURCE_IRQ, 0);
if (unlikely(!new_drvdata->res_irq)) {
@@ -424,17 +410,9 @@ static int init_cc_resources(struct platform_device *plat_dev)
#ifdef ENABLE_CC_SYSFS
ssi_sysfs_fini();
#endif
-
- if (req_mem_cc_regs) {
- if (irq_registered) {
- free_irq(new_drvdata->res_irq->start, new_drvdata);
- new_drvdata->res_irq = NULL;
- iounmap(cc_base);
- new_drvdata->cc_base = NULL;
- }
- release_mem_region(new_drvdata->res_mem->start,
- resource_size(new_drvdata->res_mem));
- new_drvdata->res_mem = NULL;
+ if (irq_registered) {
+ free_irq(new_drvdata->res_irq->start, new_drvdata);
+ new_drvdata->res_irq = NULL;
}
dev_set_drvdata(&plat_dev->dev, NULL);
}
@@ -470,14 +448,6 @@ static void cleanup_cc_resources(struct platform_device *plat_dev)
cc_clk_off(drvdata);
free_irq(drvdata->res_irq->start, drvdata);
drvdata->res_irq = NULL;
-
- if (drvdata->cc_base) {
- iounmap(drvdata->cc_base);
- release_mem_region(drvdata->res_mem->start,
- resource_size(drvdata->res_mem));
- drvdata->cc_base = NULL;
- drvdata->res_mem = NULL;
- }
dev_set_drvdata(&plat_dev->dev, NULL);
}
diff --git a/drivers/staging/ccree/ssi_driver.h b/drivers/staging/ccree/ssi_driver.h
index b6ad89a..518c0bf 100644
--- a/drivers/staging/ccree/ssi_driver.h
+++ b/drivers/staging/ccree/ssi_driver.h
@@ -128,7 +128,6 @@ struct ssi_crypto_req {
* @fw_ver: SeP loaded firmware version
*/
struct ssi_drvdata {
- struct resource *res_mem;
struct resource *res_irq;
void __iomem *cc_base;
unsigned int irq;
--
2.1.4
From: Suniel Mahesh <[email protected]>
It is recommended to use managed function devm_request_irq(),
which simplifies driver cleanup paths and driver code.
This patch does the following:
(a) replace platform_get_resource(), request_irq() and corresponding
error handling with platform_get_irq() and devm_request_irq().
(b) remove struct resource pointer(res_irq) in struct ssi_drvdata as
it seems redundant.
(c) change type of member irq in struct ssi_drvdata from unsigned int
to int, as return type of platform_get_irq is int and can be used in
error handling.
(d) remove irq_registered variable from driver probe as it seems
redundant.
(e) free_irq is not required any more, devm_request_irq() free's it
on driver detach.
(f) adjust log messages accordingly and remove any blank lines.
Signed-off-by: Suniel Mahesh <[email protected]>
Acked-by: Gilad Ben-Yossef <[email protected]>
---
drivers/staging/ccree/ssi_driver.c | 30 +++++++++---------------------
drivers/staging/ccree/ssi_driver.h | 3 +--
2 files changed, 10 insertions(+), 23 deletions(-)
diff --git a/drivers/staging/ccree/ssi_driver.c b/drivers/staging/ccree/ssi_driver.c
index 603eb03..c18e7e3 100644
--- a/drivers/staging/ccree/ssi_driver.c
+++ b/drivers/staging/ccree/ssi_driver.c
@@ -222,7 +222,6 @@ static int init_cc_resources(struct platform_device *plat_dev)
{
struct resource *req_mem_cc_regs = NULL;
void __iomem *cc_base = NULL;
- bool irq_registered = false;
struct ssi_drvdata *new_drvdata;
struct device *dev = &plat_dev->dev;
struct device_node *np = dev->of_node;
@@ -262,26 +261,22 @@ static int init_cc_resources(struct platform_device *plat_dev)
&req_mem_cc_regs->start, new_drvdata->cc_base);
cc_base = new_drvdata->cc_base;
/* Then IRQ */
- new_drvdata->res_irq = platform_get_resource(plat_dev, IORESOURCE_IRQ, 0);
- if (unlikely(!new_drvdata->res_irq)) {
+ new_drvdata->irq = platform_get_irq(plat_dev, 0);
+ if (new_drvdata->irq < 0) {
SSI_LOG_ERR("Failed getting IRQ resource\n");
- rc = -ENODEV;
+ rc = new_drvdata->irq;
goto init_cc_res_err;
}
- rc = request_irq(new_drvdata->res_irq->start, cc_isr,
- IRQF_SHARED, "arm_cc7x", new_drvdata);
- if (unlikely(rc != 0)) {
- SSI_LOG_ERR("Could not register to interrupt %llu\n",
- (unsigned long long)new_drvdata->res_irq->start);
+ rc = devm_request_irq(&plat_dev->dev, new_drvdata->irq, cc_isr,
+ IRQF_SHARED, "arm_cc7x", new_drvdata);
+ if (rc) {
+ SSI_LOG_ERR("Could not register to interrupt %d\n",
+ new_drvdata->irq);
goto init_cc_res_err;
}
init_completion(&new_drvdata->icache_setup_completion);
- irq_registered = true;
- SSI_LOG_DEBUG("Registered to IRQ (%s) %llu\n",
- new_drvdata->res_irq->name,
- (unsigned long long)new_drvdata->res_irq->start);
-
+ SSI_LOG_DEBUG("Registered to IRQ: %d\n", new_drvdata->irq);
new_drvdata->plat_dev = plat_dev;
rc = cc_clk_on(new_drvdata);
@@ -410,10 +405,6 @@ static int init_cc_resources(struct platform_device *plat_dev)
#ifdef ENABLE_CC_SYSFS
ssi_sysfs_fini();
#endif
- if (irq_registered) {
- free_irq(new_drvdata->res_irq->start, new_drvdata);
- new_drvdata->res_irq = NULL;
- }
dev_set_drvdata(&plat_dev->dev, NULL);
}
return rc;
@@ -443,11 +434,8 @@ static void cleanup_cc_resources(struct platform_device *plat_dev)
#ifdef ENABLE_CC_SYSFS
ssi_sysfs_fini();
#endif
-
fini_cc_regs(drvdata);
cc_clk_off(drvdata);
- free_irq(drvdata->res_irq->start, drvdata);
- drvdata->res_irq = NULL;
dev_set_drvdata(&plat_dev->dev, NULL);
}
diff --git a/drivers/staging/ccree/ssi_driver.h b/drivers/staging/ccree/ssi_driver.h
index 518c0bf..88ef370 100644
--- a/drivers/staging/ccree/ssi_driver.h
+++ b/drivers/staging/ccree/ssi_driver.h
@@ -128,9 +128,8 @@ struct ssi_crypto_req {
* @fw_ver: SeP loaded firmware version
*/
struct ssi_drvdata {
- struct resource *res_irq;
void __iomem *cc_base;
- unsigned int irq;
+ int irq;
u32 irq_mask;
u32 fw_ver;
/* Calibration time of start/stop
--
2.1.4
On Thu, Jul 27, 2017 at 05:27:33PM +0300, Gilad Ben-Yossef wrote:
> + new_drvdata->cc_base = devm_ioremap_resource(&plat_dev->dev,
> + req_mem_cc_regs);
> + if (IS_ERR(new_drvdata->cc_base)) {
> + rc = PTR_ERR(new_drvdata->cc_base);
> goto init_cc_res_err;
^^^^^^^^^^^^^^^^^^^^
(This code was in the original and not introduced by the patch.)
Ideally, the goto name should say what the goto does. In this case it
does everything. Unfortunately trying to do everything is very
complicated so obviously the error handling is going to be full of bugs.
The first thing the error handling does is:
ssi_aead_free(new_drvdata);
But this function assumes that if new_drvdata->aead_handle is non-NULL
then that means we have called:
INIT_LIST_HEAD(&aead_handle->aead_list);
That assumption is false if the aead_handle->sram_workspace_addr
allocation fails. It can't actually fail in the current code... So
that's good, I suppose. Reviewing this code is really hard, because I
have to jump back and forth through several functions in different
files.
Moving on two the second error handling function:
ssi_hash_free(new_drvdata);
This one has basically the same assumption that if ->hash_handle is
allocated that means we called:
INIT_LIST_HEAD(&hash_handle->hash_list);
That assumption is not true if ssi_hash_init_sram_digest_consts(drvdata);
fails. That function can fail in real life. Except the the error
handling in ssi_hash_alloc() sets ->hash_handle to NULL. So the bug is
just a leak and not a crashing bug.
I've reviewed the first two lines of the error handling just to give a
feel for how complicated "do everything" style error handling is to
review.
The better way to do error handling is:
1) Only free things which have been allocated.
2) The unwind code should mirror the wind up code.
3) Every allocation function should have a free function.
4) Label names should tell you what the goto does.
5) Use direct returns and literals where possible.
6) Generally it's better to keep the error path and the success path
separate.
7) Do error handling as opposed to success handling.
one = alloc();
if (!one)
return -ENOMEM;
if (foo) {
two = alloc();
if (!two) {
ret = -ENOMEM;
goto free_one;
}
}
three = alloc();
if (!three) {
ret = -ENOMEM;
goto free_two;
}
...
return 0;
free_two:
if (foo)
free(two);
free_one:
free(one);
return ret;
This style of error handling is easier to review. You only need to
remember the most recent thing that you have allocated. You can tell
from the goto that it frees it so you don't have to scroll to the
bottom of the function or jump to a different file.
regards,
dan carpenter
On Friday 28 July 2017 01:18 AM, Dan Carpenter wrote:
> On Thu, Jul 27, 2017 at 05:27:33PM +0300, Gilad Ben-Yossef wrote:
>> + new_drvdata->cc_base = devm_ioremap_resource(&plat_dev->dev,
>> + req_mem_cc_regs);
>> + if (IS_ERR(new_drvdata->cc_base)) {
>> + rc = PTR_ERR(new_drvdata->cc_base);
>> goto init_cc_res_err;
> ^^^^^^^^^^^^^^^^^^^^
> (This code was in the original and not introduced by the patch.)
Hi Dan, the above lines of code were not in the original except
"goto init_cc_res_err;" which was doing the error handling at different
places.
This patch has added those first three lines. I was constantly checking the latest
linux-next and staging-testing / next git trees.
>
> Ideally, the goto name should say what the goto does. In this case it
> does everything. Unfortunately trying to do everything is very
> complicated so obviously the error handling is going to be full of bugs.
>
> The first thing the error handling does is:
>
> ssi_aead_free(new_drvdata);
>
> But this function assumes that if new_drvdata->aead_handle is non-NULL
> then that means we have called:
>
> INIT_LIST_HEAD(&aead_handle->aead_list);
>
> That assumption is false if the aead_handle->sram_workspace_addr
> allocation fails. It can't actually fail in the current code... So
> that's good, I suppose. Reviewing this code is really hard, because I
> have to jump back and forth through several functions in different
> files.
>
> Moving on two the second error handling function:
>
> ssi_hash_free(new_drvdata);
>
> This one has basically the same assumption that if ->hash_handle is
> allocated that means we called:
>
> INIT_LIST_HEAD(&hash_handle->hash_list);
>
> That assumption is not true if ssi_hash_init_sram_digest_consts(drvdata);
> fails. That function can fail in real life. Except the the error
> handling in ssi_hash_alloc() sets ->hash_handle to NULL. So the bug is
> just a leak and not a crashing bug.
>
> I've reviewed the first two lines of the error handling just to give a
> feel for how complicated "do everything" style error handling is to
> review.
>
> The better way to do error handling is:
> 1) Only free things which have been allocated.
> 2) The unwind code should mirror the wind up code.
> 3) Every allocation function should have a free function.
> 4) Label names should tell you what the goto does.
> 5) Use direct returns and literals where possible.
> 6) Generally it's better to keep the error path and the success path
> separate.
> 7) Do error handling as opposed to success handling.
>
> one = alloc();
> if (!one)
> return -ENOMEM;
> if (foo) {
> two = alloc();
> if (!two) {
> ret = -ENOMEM;
> goto free_one;
> }
> }
> three = alloc();
> if (!three) {
> ret = -ENOMEM;
> goto free_two;
> }
> ...
>
> return 0;
>
> free_two:
> if (foo)
> free(two);
> free_one:
> free(one);
>
> return ret;
>
> This style of error handling is easier to review. You only need to
> remember the most recent thing that you have allocated. You can tell
> from the goto that it frees it so you don't have to scroll to the
> bottom of the function or jump to a different file.
I understand, its make sense, may be we should come up with something
better and simpler.
Thanks
Suniel
>
> regards,
> dan carpenter
>
>
On Thu, Jul 27, 2017 at 05:27:32PM +0300, Gilad Ben-Yossef wrote:
> From: Suniel Mahesh <[email protected]>
>
> It is recommended to use managed function devm_kzalloc, which
> simplifies driver cleanup paths and driver code.
> This patch does the following:
> (a) replace kzalloc with devm_kzalloc.
> (b) drop kfree(), because memory allocated with devm_kzalloc() is
> automatically freed on driver detach, otherwise it leads to a double
> free.
> (c) remove unnecessary blank lines.
>
> Signed-off-by: Suniel Mahesh <[email protected]>
> [gby: rebase on top of latest coding style fixes changes]
> Acked-by: Gilad Ben-Yossef <[email protected]>
None of these 3 applied to my tree :(
On Fri, Jul 28, 2017 at 09:59:41AM +0530, Suniel Mahesh wrote:
> On Friday 28 July 2017 01:18 AM, Dan Carpenter wrote:
> > On Thu, Jul 27, 2017 at 05:27:33PM +0300, Gilad Ben-Yossef wrote:
> >> + new_drvdata->cc_base = devm_ioremap_resource(&plat_dev->dev,
> >> + req_mem_cc_regs);
> >> + if (IS_ERR(new_drvdata->cc_base)) {
> >> + rc = PTR_ERR(new_drvdata->cc_base);
> >> goto init_cc_res_err;
> > ^^^^^^^^^^^^^^^^^^^^
> > (This code was in the original and not introduced by the patch.)
>
> Hi Dan, the above lines of code were not in the original except
> "goto init_cc_res_err;" which was doing the error handling at different
> places.
>
Yes, yes. I wasn't commenting on the patch just the existing code.
The patch is fine.
regards,
dan carpenter
On Thu, Jul 27, 2017 at 10:48 PM, Dan Carpenter
<[email protected]> wrote:
> On Thu, Jul 27, 2017 at 05:27:33PM +0300, Gilad Ben-Yossef wrote:
>> + new_drvdata->cc_base = devm_ioremap_resource(&plat_dev->dev,
>> + req_mem_cc_regs);
>> + if (IS_ERR(new_drvdata->cc_base)) {
>> + rc = PTR_ERR(new_drvdata->cc_base);
>> goto init_cc_res_err;
> ^^^^^^^^^^^^^^^^^^^^
> (This code was in the original and not introduced by the patch.)
>
> Ideally, the goto name should say what the goto does. In this case it
> does everything. Unfortunately trying to do everything is very
> complicated so obviously the error handling is going to be full of bugs.
Thank you. This review is most helpful. I've added a patch to address
the specific
issues you've raised.
I also see some additional places that can get similar treatment to
both what you
suggested as well as what Suneil has done. I'll address those in
follow up patches.
Gilad
--
Gilad Ben-Yossef
Chief Coffee Drinker
"If you take a class in large-scale robotics, can you end up in a
situation where the homework eats your dog?"
-- Jean-Baptiste Queru