2017-03-15 11:46:38

by J. Neuschäfer

[permalink] [raw]
Subject: [PATCH 1/2] soc: qcom: smsm: Handle probe deferral

If qcom_smem_get or qcom_smem_alloc return -EPROBE_DEFER, let the caller
the caller handle it, instead of treating it as an error.

Signed-off-by: Jonathan Neuschäfer <[email protected]>

---
v1:
- TODO: Reading qcom_smsm_probe, I noticed memory leaks in error paths:
smsm, smsm->entries, etc. are allocated (with devm_kzalloc), but not
freed when the function returns early. This should be addressed at
some point (in a separate patch).
---
drivers/soc/qcom/smsm.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/drivers/soc/qcom/smsm.c b/drivers/soc/qcom/smsm.c
index d0337b2a71c8..3918645e5708 100644
--- a/drivers/soc/qcom/smsm.c
+++ b/drivers/soc/qcom/smsm.c
@@ -439,7 +439,9 @@ static int smsm_get_size_info(struct qcom_smsm *smsm)
} *info;

info = qcom_smem_get(QCOM_SMEM_HOST_ANY, SMEM_SMSM_SIZE_INFO, &size);
- if (PTR_ERR(info) == -ENOENT || size != sizeof(*info)) {
+ if (PTR_ERR(info) == -EPROBE_DEFER) {
+ return PTR_ERR(info);
+ } else if (PTR_ERR(info) == -ENOENT || size != sizeof(*info)) {
dev_warn(smsm->dev, "no smsm size info, using defaults\n");
smsm->num_entries = SMSM_DEFAULT_NUM_ENTRIES;
smsm->num_hosts = SMSM_DEFAULT_NUM_HOSTS;
@@ -515,7 +517,9 @@ static int qcom_smsm_probe(struct platform_device *pdev)
/* Acquire the main SMSM state vector */
ret = qcom_smem_alloc(QCOM_SMEM_HOST_ANY, SMEM_SMSM_SHARED_STATE,
smsm->num_entries * sizeof(u32));
- if (ret < 0 && ret != -EEXIST) {
+ if (ret == -EPROBE_DEFER) {
+ return ret;
+ } else if (ret < 0 && ret != -EEXIST) {
dev_err(&pdev->dev, "unable to allocate shared state entry\n");
return ret;
}
--
2.11.0


2017-03-15 11:46:37

by J. Neuschäfer

[permalink] [raw]
Subject: [PATCH 2/2] soc: qcom: smsm: Avoid the use of an uninitialized value

If qcom_smem_get returns an error besides -EPROBE_DEFER or -ENOENT,
don't assume that size has been set.

Signed-off-by: Jonathan Neuschäfer <[email protected]>
---
drivers/soc/qcom/smsm.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/soc/qcom/smsm.c b/drivers/soc/qcom/smsm.c
index 3918645e5708..632c060c9cc0 100644
--- a/drivers/soc/qcom/smsm.c
+++ b/drivers/soc/qcom/smsm.c
@@ -441,7 +441,8 @@ static int smsm_get_size_info(struct qcom_smsm *smsm)
info = qcom_smem_get(QCOM_SMEM_HOST_ANY, SMEM_SMSM_SIZE_INFO, &size);
if (PTR_ERR(info) == -EPROBE_DEFER) {
return PTR_ERR(info);
- } else if (PTR_ERR(info) == -ENOENT || size != sizeof(*info)) {
+ } else if (PTR_ERR(info) == -ENOENT ||
+ (!IS_ERR(info) && size != sizeof(*info))) {
dev_warn(smsm->dev, "no smsm size info, using defaults\n");
smsm->num_entries = SMSM_DEFAULT_NUM_ENTRIES;
smsm->num_hosts = SMSM_DEFAULT_NUM_HOSTS;
--
2.11.0

2017-03-17 14:02:02

by J. Neuschäfer

[permalink] [raw]
Subject: Re: [PATCH 1/2] soc: qcom: smsm: Handle probe deferral

On Wed, Mar 15, 2017 at 12:43:56PM +0100, Jonathan Neuschäfer wrote:
[...]
> - TODO: Reading qcom_smsm_probe, I noticed memory leaks in error paths:
> smsm, smsm->entries, etc. are allocated (with devm_kzalloc), but not
> freed when the function returns early. This should be addressed at
> some point (in a separate patch).

This is not actually true (see Documentation/driver-model/devres.txt).
Sorry for the noise.


Jonathan Neuschäfer


Attachments:
(No filename) (448.00 B)
signature.asc (819.00 B)
Download all attachments

2017-03-28 06:18:36

by Bjorn Andersson

[permalink] [raw]
Subject: Re: [PATCH 1/2] soc: qcom: smsm: Handle probe deferral

On Wed 15 Mar 04:43 PDT 2017, Jonathan Neusch?fer wrote:

> If qcom_smem_get or qcom_smem_alloc return -EPROBE_DEFER, let the caller
> the caller handle it, instead of treating it as an error.
>
> Signed-off-by: Jonathan Neusch?fer <[email protected]>
>
> ---
> v1:
> - TODO: Reading qcom_smsm_probe, I noticed memory leaks in error paths:
> smsm, smsm->entries, etc. are allocated (with devm_kzalloc), but not
> freed when the function returns early. This should be addressed at
> some point (in a separate patch).
> ---
> drivers/soc/qcom/smsm.c | 8 ++++++--
> 1 file changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/soc/qcom/smsm.c b/drivers/soc/qcom/smsm.c
> index d0337b2a71c8..3918645e5708 100644
> --- a/drivers/soc/qcom/smsm.c
> +++ b/drivers/soc/qcom/smsm.c
> @@ -439,7 +439,9 @@ static int smsm_get_size_info(struct qcom_smsm *smsm)
> } *info;
>
> info = qcom_smem_get(QCOM_SMEM_HOST_ANY, SMEM_SMSM_SIZE_INFO, &size);
> - if (PTR_ERR(info) == -ENOENT || size != sizeof(*info)) {
> + if (PTR_ERR(info) == -EPROBE_DEFER) {
> + return PTR_ERR(info);
> + } else if (PTR_ERR(info) == -ENOENT || size != sizeof(*info)) {

The following elseif was supposed to take care of this case, but I
clearly screwed this up.

Rather than adding a special case for EPROBE_DEFER before the two checks
and then fix up the original expression to make errors fall back to the
original else, I think you should rearrange the conditionals.

Probably better to write it like this instead:

if (IS_ERR(info) && PTR_ERR(info) != -ENOENT) {
if (PTR_ERR(info) != -EPROBE_DEFER)
dev_err(smsm->dev, "unable to retrieve smsm size info\n");
return PTR_ERR(info);
} else if (IS_ERR(info) || size != sizeof(*info)) {
dev_warn(smsm->dev, "no smsm size info, using defaults\n");
smsm->num_entries = SMSM_DEFAULT_NUM_ENTRIES;
smsm->num_hosts = SMSM_DEFAULT_NUM_HOSTS;
return 0;
}

> dev_warn(smsm->dev, "no smsm size info, using defaults\n");
> smsm->num_entries = SMSM_DEFAULT_NUM_ENTRIES;
> smsm->num_hosts = SMSM_DEFAULT_NUM_HOSTS;
> @@ -515,7 +517,9 @@ static int qcom_smsm_probe(struct platform_device *pdev)
> /* Acquire the main SMSM state vector */
> ret = qcom_smem_alloc(QCOM_SMEM_HOST_ANY, SMEM_SMSM_SHARED_STATE,
> smsm->num_entries * sizeof(u32));
> - if (ret < 0 && ret != -EEXIST) {
> + if (ret == -EPROBE_DEFER) {
> + return ret;
> + } else if (ret < 0 && ret != -EEXIST) {

The idiomatic way to write this is:

if (ret < 0 && ret != -EEXIST) {
if (ret != -EPROBE_DEFER)
dev_err();
return ret;
}

However, for us to reach this point in smsm_probe() the above
qcom_smem_get() must have returned successfully, i.e. we have SMEM in
place so there's no need to handle this case specifically.

> dev_err(&pdev->dev, "unable to allocate shared state entry\n");
> return ret;
> }

Regards,
Bjorn

2017-04-04 02:38:13

by J. Neuschäfer

[permalink] [raw]
Subject: Re: [PATCH 1/2] soc: qcom: smsm: Handle probe deferral

On Mon, Mar 27, 2017 at 11:18:29PM -0700, Bjorn Andersson wrote:
> On Wed 15 Mar 04:43 PDT 2017, Jonathan Neusch?fer wrote:
[...]
> > info = qcom_smem_get(QCOM_SMEM_HOST_ANY, SMEM_SMSM_SIZE_INFO, &size);
> > - if (PTR_ERR(info) == -ENOENT || size != sizeof(*info)) {
> > + if (PTR_ERR(info) == -EPROBE_DEFER) {
> > + return PTR_ERR(info);
> > + } else if (PTR_ERR(info) == -ENOENT || size != sizeof(*info)) {
>
> The following elseif was supposed to take care of this case, but I
> clearly screwed this up.
>
> Rather than adding a special case for EPROBE_DEFER before the two checks
> and then fix up the original expression to make errors fall back to the
> original else, I think you should rearrange the conditionals.
>
> Probably better to write it like this instead:
>
> if (IS_ERR(info) && PTR_ERR(info) != -ENOENT) {
> if (PTR_ERR(info) != -EPROBE_DEFER)
> dev_err(smsm->dev, "unable to retrieve smsm size info\n");
> return PTR_ERR(info);
> } else if (IS_ERR(info) || size != sizeof(*info)) {
> dev_warn(smsm->dev, "no smsm size info, using defaults\n");
> smsm->num_entries = SMSM_DEFAULT_NUM_ENTRIES;
> smsm->num_hosts = SMSM_DEFAULT_NUM_HOSTS;
> return 0;
> }

Indeed, this looks better.

And it also obsoletes my patch 2/2, which is nice.

> > dev_warn(smsm->dev, "no smsm size info, using defaults\n");
> > smsm->num_entries = SMSM_DEFAULT_NUM_ENTRIES;
> > smsm->num_hosts = SMSM_DEFAULT_NUM_HOSTS;
> > @@ -515,7 +517,9 @@ static int qcom_smsm_probe(struct platform_device *pdev)
> > /* Acquire the main SMSM state vector */
> > ret = qcom_smem_alloc(QCOM_SMEM_HOST_ANY, SMEM_SMSM_SHARED_STATE,
> > smsm->num_entries * sizeof(u32));
> > - if (ret < 0 && ret != -EEXIST) {
> > + if (ret == -EPROBE_DEFER) {
> > + return ret;
> > + } else if (ret < 0 && ret != -EEXIST) {
>
> The idiomatic way to write this is:
>
> if (ret < 0 && ret != -EEXIST) {
> if (ret != -EPROBE_DEFER)
> dev_err();
> return ret;
> }
>
> However, for us to reach this point in smsm_probe() the above
> qcom_smem_get() must have returned successfully, i.e. we have SMEM in
> place so there's no need to handle this case specifically.

I came to the same conclusion but wasn't sure. I'll drop this part from
my patch.

I'll send a v2 of this series, although after applying your suggestions,
I can't claim much originality anymore.


Thanks for the review,
Jonathan Neuschäfer


Attachments:
(No filename) (2.48 kB)
signature.asc (819.00 B)
Download all attachments

2017-04-05 03:47:25

by Bjorn Andersson

[permalink] [raw]
Subject: Re: [PATCH 1/2] soc: qcom: smsm: Handle probe deferral

On Mon 03 Apr 19:38 PDT 2017, Jonathan Neusch?fer wrote:

> On Mon, Mar 27, 2017 at 11:18:29PM -0700, Bjorn Andersson wrote:
[..]
> > However, for us to reach this point in smsm_probe() the above
> > qcom_smem_get() must have returned successfully, i.e. we have SMEM in
> > place so there's no need to handle this case specifically.
>
> I came to the same conclusion but wasn't sure. I'll drop this part from
> my patch.
>
> I'll send a v2 of this series, although after applying your suggestions,
> I can't claim much originality anymore.
>

Don't worry about it. Looking forward to version 2.

Regards,
Bjorn

2017-04-05 12:15:09

by J. Neuschäfer

[permalink] [raw]
Subject: [PATCH v2] soc: qcom: smsm: Improve error handling, quiesce probe deferral

Don't use size if info indicates an error condition. Previously a
non-ENOENT error (such as -EPROBE_DEFER) would lead to size being used
even though it hadn't necessarily been initialized in qcom_smem_get.

Don't print an error message in the -EPROBE_DEFER case.

Signed-off-by: Jonathan Neuschäfer <[email protected]>

---
v2:
- Rewrite based on Bjorn's suggestion.
- Don't check for -EPROBE_DEFER again after qcom_smem_alloc was called
in qcom_smsm_probe. This point is only reached if smsm_get_size_info
returned success, which can only happen if probe deferral is over.
v1:
- was "[PATCH 1/2] soc: qcom: smsm: Handle probe deferral"
---
drivers/soc/qcom/smsm.c | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/drivers/soc/qcom/smsm.c b/drivers/soc/qcom/smsm.c
index d0337b2a71c8..dc540ea92e9d 100644
--- a/drivers/soc/qcom/smsm.c
+++ b/drivers/soc/qcom/smsm.c
@@ -439,14 +439,15 @@ static int smsm_get_size_info(struct qcom_smsm *smsm)
} *info;

info = qcom_smem_get(QCOM_SMEM_HOST_ANY, SMEM_SMSM_SIZE_INFO, &size);
- if (PTR_ERR(info) == -ENOENT || size != sizeof(*info)) {
+ if (IS_ERR(info) && PTR_ERR(info) != -ENOENT) {
+ if (PTR_ERR(info) != -EPROBE_DEFER)
+ dev_err(smsm->dev, "unable to retrieve smsm size info\n");
+ return PTR_ERR(info);
+ } else if (IS_ERR(info) || size != sizeof(*info)) {
dev_warn(smsm->dev, "no smsm size info, using defaults\n");
smsm->num_entries = SMSM_DEFAULT_NUM_ENTRIES;
smsm->num_hosts = SMSM_DEFAULT_NUM_HOSTS;
return 0;
- } else if (IS_ERR(info)) {
- dev_err(smsm->dev, "unable to retrieve smsm size info\n");
- return PTR_ERR(info);
}

smsm->num_entries = info->num_entries;
--
2.11.0

2017-04-05 17:04:20

by Bjorn Andersson

[permalink] [raw]
Subject: Re: [PATCH v2] soc: qcom: smsm: Improve error handling, quiesce probe deferral

On Wed 05 Apr 05:10 PDT 2017, Jonathan Neusch?fer wrote:

> Don't use size if info indicates an error condition. Previously a
> non-ENOENT error (such as -EPROBE_DEFER) would lead to size being used
> even though it hadn't necessarily been initialized in qcom_smem_get.
>
> Don't print an error message in the -EPROBE_DEFER case.
>
> Signed-off-by: Jonathan Neusch?fer <[email protected]>

Reviewed-by: Bjorn Andersson <[email protected]>

Regards,
Bjorn

>
> ---
> v2:
> - Rewrite based on Bjorn's suggestion.
> - Don't check for -EPROBE_DEFER again after qcom_smem_alloc was called
> in qcom_smsm_probe. This point is only reached if smsm_get_size_info
> returned success, which can only happen if probe deferral is over.
> v1:
> - was "[PATCH 1/2] soc: qcom: smsm: Handle probe deferral"
> ---
> drivers/soc/qcom/smsm.c | 9 +++++----
> 1 file changed, 5 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/soc/qcom/smsm.c b/drivers/soc/qcom/smsm.c
> index d0337b2a71c8..dc540ea92e9d 100644
> --- a/drivers/soc/qcom/smsm.c
> +++ b/drivers/soc/qcom/smsm.c
> @@ -439,14 +439,15 @@ static int smsm_get_size_info(struct qcom_smsm *smsm)
> } *info;
>
> info = qcom_smem_get(QCOM_SMEM_HOST_ANY, SMEM_SMSM_SIZE_INFO, &size);
> - if (PTR_ERR(info) == -ENOENT || size != sizeof(*info)) {
> + if (IS_ERR(info) && PTR_ERR(info) != -ENOENT) {
> + if (PTR_ERR(info) != -EPROBE_DEFER)
> + dev_err(smsm->dev, "unable to retrieve smsm size info\n");
> + return PTR_ERR(info);
> + } else if (IS_ERR(info) || size != sizeof(*info)) {
> dev_warn(smsm->dev, "no smsm size info, using defaults\n");
> smsm->num_entries = SMSM_DEFAULT_NUM_ENTRIES;
> smsm->num_hosts = SMSM_DEFAULT_NUM_HOSTS;
> return 0;
> - } else if (IS_ERR(info)) {
> - dev_err(smsm->dev, "unable to retrieve smsm size info\n");
> - return PTR_ERR(info);
> }
>
> smsm->num_entries = info->num_entries;
> --
> 2.11.0
>

2017-06-01 06:34:12

by Andy Gross

[permalink] [raw]
Subject: Re: [PATCH v2] soc: qcom: smsm: Improve error handling, quiesce probe deferral

On Wed, May 31, 2017 at 04:00:03PM +0200, Jonathan Neusch?fer wrote:
> On Wed, Apr 05, 2017 at 10:01:23AM -0700, Bjorn Andersson wrote:
> > On Wed 05 Apr 05:10 PDT 2017, Jonathan Neusch?fer wrote:
> >
> > > Don't use size if info indicates an error condition. Previously a
> > > non-ENOENT error (such as -EPROBE_DEFER) would lead to size being used
> > > even though it hadn't necessarily been initialized in qcom_smem_get.
> > >
> > > Don't print an error message in the -EPROBE_DEFER case.
> > >
> > > Signed-off-by: Jonathan Neusch?fer <[email protected]>
> >
> > Reviewed-by: Bjorn Andersson <[email protected]>
>
> Ping. Andy, are you going to merge this patch?

Yes. I queued this one up on my list of patches.

Regards,

Andy