diff mbox

[V2,1/6] PM / OPP: Free resources and properly return error on failure

Message ID 334a9052264630b9157fa9bfc3d4efe945054c34.1439288881.git.viresh.kumar@linaro.org (mailing list archive)
State Superseded, archived
Headers show

Commit Message

Viresh Kumar Aug. 11, 2015, 10:34 a.m. UTC
_of_init_opp_table_v2() isn't freeing up resources on some errors and
the error values returned are also not correct always.

This fixes following problems:
- Return -ENOENT, if no entries are found in the table.
- Use IS_ERR() to properly check return value of _find_device_opp().
- Return error value with PTR_ERR() in above case.
- Free table if _find_device_opp() fails.

Fixes: 274659029c9d ("PM / OPP: Add support to parse operating-points-v2" bindings")
Reported-by: Dan Carpenter <dan.carpenter@oracle.com>
Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
---
 drivers/base/power/opp.c | 29 +++++++++++++++--------------
 1 file changed, 15 insertions(+), 14 deletions(-)

Comments

Dan Carpenter Aug. 11, 2015, 2:43 p.m. UTC | #1
On Tue, Aug 11, 2015 at 04:04:34PM +0530, Viresh Kumar wrote:
> _of_init_opp_table_v2() isn't freeing up resources on some errors and
> the error values returned are also not correct always.
> 
> This fixes following problems:
> - Return -ENOENT, if no entries are found in the table.
> - Use IS_ERR() to properly check return value of _find_device_opp().
> - Return error value with PTR_ERR() in above case.
> - Free table if _find_device_opp() fails.
> 
> Fixes: 274659029c9d ("PM / OPP: Add support to parse operating-points-v2" bindings")
> Reported-by: Dan Carpenter <dan.carpenter@oracle.com>
> Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
> ---
>  drivers/base/power/opp.c | 29 +++++++++++++++--------------
>  1 file changed, 15 insertions(+), 14 deletions(-)
> 
> diff --git a/drivers/base/power/opp.c b/drivers/base/power/opp.c
> index 204c6c945168..bcbd92c3b717 100644
> --- a/drivers/base/power/opp.c
> +++ b/drivers/base/power/opp.c
> @@ -1323,28 +1323,29 @@ static int _of_init_opp_table_v2(struct device *dev,
>  		if (ret) {
>  			dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,
>  				ret);
> -			break;
> +			goto free_table;
>  		}
>  	}
>  
>  	/* There should be one of more OPP defined */
> -	if (WARN_ON(!count))
> +	if (WARN_ON(!count)) {
> +		ret = -ENOENT;
>  		goto put_opp_np;
> +	}

This is weird to me, because we are going backwards.  What happens if
we goto free_table without adding anything?  I suspect it's fine, but if
it's a bug then this code still has problems.

What about if we only increment count when _opp_add_static_v2()
succeeds, and change it back to the original where we break, check
count, then check ret.  That way we don't need to know the details of
free_table to see that the code is correct.

regards,
dan carpenter

--
To unsubscribe from this list: send the line "unsubscribe linux-pm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Viresh Kumar Aug. 11, 2015, 2:59 p.m. UTC | #2
On 11-08-15, 17:43, Dan Carpenter wrote:
> > @@ -1323,28 +1323,29 @@ static int _of_init_opp_table_v2(struct device *dev,
> >  		if (ret) {
> >  			dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,
> >  				ret);
> > -			break;
> > +			goto free_table;
> >  		}
> >  	}
> >  
> >  	/* There should be one of more OPP defined */
> > -	if (WARN_ON(!count))
> > +	if (WARN_ON(!count)) {
> > +		ret = -ENOENT;
> >  		goto put_opp_np;
> > +	}

The purpose of 'count' here is to see if the dtb contained any OPP
nodes or not. i.e. if we ever entered the body of
for_each_available_child_of_node() or not..

Its different than, "if we were able to add any OPPs";

> This is weird to me, because we are going backwards.  What happens if
> we goto free_table without adding anything?

It will WARN() today.

> I suspect it's fine, but if
> it's a bug then this code still has problems.

I don't think we have a bug here, we never added anything and so don't
need to free it.

> What about if we only increment count when _opp_add_static_v2()
> succeeds

That's not what we want.
Dan Carpenter Aug. 11, 2015, 5:11 p.m. UTC | #3
On Tue, Aug 11, 2015 at 08:29:38PM +0530, Viresh Kumar wrote:
> > This is weird to me, because we are going backwards.  What happens if
> > we goto free_table without adding anything?
> 
> It will WARN() today.

Then the current code is buggy.

> 
> > I suspect it's fine, but if
> > it's a bug then this code still has problems.
> 
> I don't think we have a bug here, we never added anything and so don't
> need to free it.
> 
> > What about if we only increment count when _opp_add_static_v2()
> > succeeds
> 
> That's not what we want.

If the first call to _opp_add_static_v2() fails we call
of_free_opp_table() and you say that triggers a WARN().

regards,
dan carpenter

--
To unsubscribe from this list: send the line "unsubscribe linux-pm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Viresh Kumar Aug. 12, 2015, 6:43 a.m. UTC | #4
On 11-08-15, 20:11, Dan Carpenter wrote:
> On Tue, Aug 11, 2015 at 08:29:38PM +0530, Viresh Kumar wrote:
> > > This is weird to me, because we are going backwards.  What happens if
> > > we goto free_table without adding anything?
> > 
> > It will WARN() today.
> 
> Then the current code is buggy.

Urg, it wouldn't WARN in this case. Sorry, didn't read it correctly
earlier.

> > > I suspect it's fine, but if
> > > it's a bug then this code still has problems.
> > 
> > I don't think we have a bug here, we never added anything and so don't
> > need to free it.
> > 
> > > What about if we only increment count when _opp_add_static_v2()
> > > succeeds
> > 
> > That's not what we want.
> 
> If the first call to _opp_add_static_v2() fails we call
> of_free_opp_table() and you say that triggers a WARN().

No it doesn't.

So, coming back to the point you made about freeing table on !count,
because there were no nodes present in the DT opp table, we have never
tried to add any OPPs. And so there is no need to call
of_free_opp_table() in that case.

Do you still think the current code is wrong ?
Dan Carpenter Aug. 12, 2015, 8:11 a.m. UTC | #5
On Wed, Aug 12, 2015 at 12:13:09PM +0530, Viresh Kumar wrote:
> > If the first call to _opp_add_static_v2() fails we call
> > of_free_opp_table() and you say that triggers a WARN().
> 
> No it doesn't.
> 
> So, coming back to the point you made about freeing table on !count,
> because there were no nodes present in the DT opp table, we have never
> tried to add any OPPs. And so there is no need to call
> of_free_opp_table() in that case.
> 
> Do you still think the current code is wrong ?

If it doesn't WARN() then it's not buggy, but it's still ugly.  We
should not call of_free_opp_table() because we *tried* to add an OPP, we
should only call it if we *succeeded*.

The way the code is written and from your emails I was afraid that if
you tried to call _opp_add_static_v2() and it fails then it leaves
artifacts lying around that need to be cleaned up by the caller.  This
would be the ugliest scenario.  But I looked at _opp_add_static_v2()
and looks fine.  It cleans up properly on failure.  We only need to
clean up if it succeeds.

regards,
dan carpenter
--
To unsubscribe from this list: send the line "unsubscribe linux-pm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/drivers/base/power/opp.c b/drivers/base/power/opp.c
index 204c6c945168..bcbd92c3b717 100644
--- a/drivers/base/power/opp.c
+++ b/drivers/base/power/opp.c
@@ -1323,28 +1323,29 @@  static int _of_init_opp_table_v2(struct device *dev,
 		if (ret) {
 			dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,
 				ret);
-			break;
+			goto free_table;
 		}
 	}
 
 	/* There should be one of more OPP defined */
-	if (WARN_ON(!count))
+	if (WARN_ON(!count)) {
+		ret = -ENOENT;
 		goto put_opp_np;
+	}
 
-	if (!ret) {
-		if (!dev_opp) {
-			dev_opp = _find_device_opp(dev);
-			if (WARN_ON(!dev_opp))
-				goto put_opp_np;
-		}
-
-		dev_opp->np = opp_np;
-		dev_opp->shared_opp = of_property_read_bool(opp_np,
-							    "opp-shared");
-	} else {
-		of_free_opp_table(dev);
+	dev_opp = _find_device_opp(dev);
+	if (WARN_ON(IS_ERR(dev_opp))) {
+		ret = PTR_ERR(dev_opp);
+		goto free_table;
 	}
 
+	dev_opp->np = opp_np;
+	dev_opp->shared_opp = of_property_read_bool(opp_np, "opp-shared");
+
+	goto put_opp_np;
+
+free_table:
+	of_free_opp_table(dev);
 put_opp_np:
 	of_node_put(opp_np);