LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
* [PATCH][next] pinctrl: pinctrl-single: add allocation failure checking of saved_vals
@ 2018-06-06 13:43 Colin King
2018-06-06 16:02 ` Andy Shevchenko
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: Colin King @ 2018-06-06 13:43 UTC (permalink / raw)
To: Tony Lindgren, Haojian Zhuang, Linus Walleij, linux-arm-kernel,
linux-omap, linux-gpio
Cc: kernel-janitors, linux-kernel
From: Colin Ian King <colin.king@canonical.com>
Currently saved_vals is being allocated and there is no check for
failed allocation (which is more likely than normal when using
GFP_ATOMIC). Fix this by checking for a failed allocation and
propagating this error return down the the caller chain.
Detected by CoverityScan, CID#1469841 ("Dereference null return value")
Fixes: 88a1dbdec682 ("pinctrl: pinctrl-single: Add functions to save and restore pinctrl context")
Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
drivers/pinctrl/pinctrl-single.c | 14 +++++++++++---
1 file changed, 11 insertions(+), 3 deletions(-)
diff --git a/drivers/pinctrl/pinctrl-single.c b/drivers/pinctrl/pinctrl-single.c
index 9c3c00515aa0..0905ee002041 100644
--- a/drivers/pinctrl/pinctrl-single.c
+++ b/drivers/pinctrl/pinctrl-single.c
@@ -1588,8 +1588,11 @@ static int pcs_save_context(struct pcs_device *pcs)
mux_bytes = pcs->width / BITS_PER_BYTE;
- if (!pcs->saved_vals)
+ if (!pcs->saved_vals) {
pcs->saved_vals = devm_kzalloc(pcs->dev, pcs->size, GFP_ATOMIC);
+ if (!pcs->saved_vals)
+ return -ENOMEM;
+ }
switch (pcs->width) {
case 64:
@@ -1649,8 +1652,13 @@ static int pinctrl_single_suspend(struct platform_device *pdev,
if (!pcs)
return -EINVAL;
- if (pcs->flags & PCS_CONTEXT_LOSS_OFF)
- pcs_save_context(pcs);
+ if (pcs->flags & PCS_CONTEXT_LOSS_OFF) {
+ int ret;
+
+ ret = pcs_save_context(pcs);
+ if (ret < 0)
+ return ret;
+ }
return pinctrl_force_sleep(pcs->pctl);
}
--
2.17.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH][next] pinctrl: pinctrl-single: add allocation failure checking of saved_vals
2018-06-06 13:43 [PATCH][next] pinctrl: pinctrl-single: add allocation failure checking of saved_vals Colin King
@ 2018-06-06 16:02 ` Andy Shevchenko
2018-06-07 7:35 ` Johan Hovold
2018-06-07 7:29 ` Johan Hovold
2018-06-14 8:31 ` Linus Walleij
2 siblings, 1 reply; 7+ messages in thread
From: Andy Shevchenko @ 2018-06-06 16:02 UTC (permalink / raw)
To: Colin King
Cc: Tony Lindgren, Haojian Zhuang, Linus Walleij,
linux-arm Mailing List, Linux OMAP Mailing List,
open list:GPIO SUBSYSTEM, kernel-janitors,
Linux Kernel Mailing List
On Wed, Jun 6, 2018 at 4:43 PM, Colin King <colin.king@canonical.com> wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> Currently saved_vals is being allocated and there is no check for
> failed allocation (which is more likely than normal when using
> GFP_ATOMIC). Fix this by checking for a failed allocation and
> propagating this error return down the the caller chain.
>
> Detected by CoverityScan, CID#1469841 ("Dereference null return value")
>
> Fixes: 88a1dbdec682 ("pinctrl: pinctrl-single: Add functions to save and restore pinctrl context")
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
> drivers/pinctrl/pinctrl-single.c | 14 +++++++++++---
> 1 file changed, 11 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/pinctrl/pinctrl-single.c b/drivers/pinctrl/pinctrl-single.c
> index 9c3c00515aa0..0905ee002041 100644
> --- a/drivers/pinctrl/pinctrl-single.c
> +++ b/drivers/pinctrl/pinctrl-single.c
> @@ -1588,8 +1588,11 @@ static int pcs_save_context(struct pcs_device *pcs)
>
> mux_bytes = pcs->width / BITS_PER_BYTE;
>
> - if (!pcs->saved_vals)
> + if (!pcs->saved_vals) {
> pcs->saved_vals = devm_kzalloc(pcs->dev, pcs->size, GFP_ATOMIC);
> + if (!pcs->saved_vals)
> + return -ENOMEM;
Wouldn't make sense to move it out of the first condition?
Something like
if (!foo)
foo = ...malloc(...);
if (!foo)
return ...
> + }
>
> switch (pcs->width) {
> case 64:
> @@ -1649,8 +1652,13 @@ static int pinctrl_single_suspend(struct platform_device *pdev,
> if (!pcs)
> return -EINVAL;
>
> - if (pcs->flags & PCS_CONTEXT_LOSS_OFF)
> - pcs_save_context(pcs);
> + if (pcs->flags & PCS_CONTEXT_LOSS_OFF) {
> + int ret;
> +
> + ret = pcs_save_context(pcs);
> + if (ret < 0)
> + return ret;
> + }
>
> return pinctrl_force_sleep(pcs->pctl);
> }
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH][next] pinctrl: pinctrl-single: add allocation failure checking of saved_vals
2018-06-06 13:43 [PATCH][next] pinctrl: pinctrl-single: add allocation failure checking of saved_vals Colin King
2018-06-06 16:02 ` Andy Shevchenko
@ 2018-06-07 7:29 ` Johan Hovold
2018-06-08 6:23 ` Tony Lindgren
2018-06-14 8:31 ` Linus Walleij
2 siblings, 1 reply; 7+ messages in thread
From: Johan Hovold @ 2018-06-07 7:29 UTC (permalink / raw)
To: Colin King
Cc: Tony Lindgren, Haojian Zhuang, Linus Walleij, linux-arm-kernel,
linux-omap, linux-gpio, kernel-janitors, linux-kernel
On Wed, Jun 06, 2018 at 02:43:38PM +0100, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> Currently saved_vals is being allocated and there is no check for
> failed allocation (which is more likely than normal when using
> GFP_ATOMIC). Fix this by checking for a failed allocation and
> propagating this error return down the the caller chain.
>
> Detected by CoverityScan, CID#1469841 ("Dereference null return value")
> Fixes: 88a1dbdec682 ("pinctrl: pinctrl-single: Add functions to save and restore pinctrl context")
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
> drivers/pinctrl/pinctrl-single.c | 14 +++++++++++---
> 1 file changed, 11 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/pinctrl/pinctrl-single.c b/drivers/pinctrl/pinctrl-single.c
> index 9c3c00515aa0..0905ee002041 100644
> --- a/drivers/pinctrl/pinctrl-single.c
> +++ b/drivers/pinctrl/pinctrl-single.c
> @@ -1588,8 +1588,11 @@ static int pcs_save_context(struct pcs_device *pcs)
>
> mux_bytes = pcs->width / BITS_PER_BYTE;
>
> - if (!pcs->saved_vals)
> + if (!pcs->saved_vals) {
> pcs->saved_vals = devm_kzalloc(pcs->dev, pcs->size, GFP_ATOMIC);
> + if (!pcs->saved_vals)
> + return -ENOMEM;
> + }
>
> switch (pcs->width) {
> case 64:
> @@ -1649,8 +1652,13 @@ static int pinctrl_single_suspend(struct platform_device *pdev,
> if (!pcs)
> return -EINVAL;
>
> - if (pcs->flags & PCS_CONTEXT_LOSS_OFF)
> - pcs_save_context(pcs);
> + if (pcs->flags & PCS_CONTEXT_LOSS_OFF) {
> + int ret;
> +
> + ret = pcs_save_context(pcs);
> + if (ret < 0)
> + return ret;
> + }
This appears to be the right fix (along the lines of what the author may
have intended by having the helper return an int), but as a follow-up
patch, why not move the allocation to probe() instead?
Also this doesn't look like something that requires atomic allocation in
the first place, GFP_KERNEL should do for the legacy suspend callback.
> return pinctrl_force_sleep(pcs->pctl);
> }
But for this fix, feel free to add:
Reviewed-by: Johan Hovold <johan@kernel.org>
Thanks,
Johan
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH][next] pinctrl: pinctrl-single: add allocation failure checking of saved_vals
2018-06-06 16:02 ` Andy Shevchenko
@ 2018-06-07 7:35 ` Johan Hovold
2018-06-07 8:26 ` Colin Ian King
0 siblings, 1 reply; 7+ messages in thread
From: Johan Hovold @ 2018-06-07 7:35 UTC (permalink / raw)
To: Andy Shevchenko
Cc: Colin King, Tony Lindgren, Linus Walleij, kernel-janitors,
Linux Kernel Mailing List, open list:GPIO SUBSYSTEM,
Haojian Zhuang, Linux OMAP Mailing List, linux-arm Mailing List
On Wed, Jun 06, 2018 at 07:02:03PM +0300, Andy Shevchenko wrote:
> On Wed, Jun 6, 2018 at 4:43 PM, Colin King <colin.king@canonical.com> wrote:
> > From: Colin Ian King <colin.king@canonical.com>
> >
> > Currently saved_vals is being allocated and there is no check for
> > failed allocation (which is more likely than normal when using
> > GFP_ATOMIC). Fix this by checking for a failed allocation and
> > propagating this error return down the the caller chain.
> >
> > Detected by CoverityScan, CID#1469841 ("Dereference null return value")
> >
> > Fixes: 88a1dbdec682 ("pinctrl: pinctrl-single: Add functions to save and restore pinctrl context")
> > Signed-off-by: Colin Ian King <colin.king@canonical.com>
> > ---
> > drivers/pinctrl/pinctrl-single.c | 14 +++++++++++---
> > 1 file changed, 11 insertions(+), 3 deletions(-)
> >
> > diff --git a/drivers/pinctrl/pinctrl-single.c b/drivers/pinctrl/pinctrl-single.c
> > index 9c3c00515aa0..0905ee002041 100644
> > --- a/drivers/pinctrl/pinctrl-single.c
> > +++ b/drivers/pinctrl/pinctrl-single.c
> > @@ -1588,8 +1588,11 @@ static int pcs_save_context(struct pcs_device *pcs)
> >
> > mux_bytes = pcs->width / BITS_PER_BYTE;
> >
> > - if (!pcs->saved_vals)
> > + if (!pcs->saved_vals) {
> > pcs->saved_vals = devm_kzalloc(pcs->dev, pcs->size, GFP_ATOMIC);
>
> > + if (!pcs->saved_vals)
> > + return -ENOMEM;
>
> Wouldn't make sense to move it out of the first condition?
>
> Something like
>
> if (!foo)
> foo = ...malloc(...);
> if (!foo)
> return ...
No, checking for NULL immediately after the allocation is more obvious
and easier to parse.
Johan
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH][next] pinctrl: pinctrl-single: add allocation failure checking of saved_vals
2018-06-07 7:35 ` Johan Hovold
@ 2018-06-07 8:26 ` Colin Ian King
0 siblings, 0 replies; 7+ messages in thread
From: Colin Ian King @ 2018-06-07 8:26 UTC (permalink / raw)
To: Johan Hovold, Andy Shevchenko
Cc: Tony Lindgren, Linus Walleij, kernel-janitors,
Linux Kernel Mailing List, open list:GPIO SUBSYSTEM,
Haojian Zhuang, Linux OMAP Mailing List, linux-arm Mailing List
On 07/06/18 08:35, Johan Hovold wrote:
> On Wed, Jun 06, 2018 at 07:02:03PM +0300, Andy Shevchenko wrote:
>> On Wed, Jun 6, 2018 at 4:43 PM, Colin King <colin.king@canonical.com> wrote:
>>> From: Colin Ian King <colin.king@canonical.com>
>>>
>>> Currently saved_vals is being allocated and there is no check for
>>> failed allocation (which is more likely than normal when using
>>> GFP_ATOMIC). Fix this by checking for a failed allocation and
>>> propagating this error return down the the caller chain.
>>>
>>> Detected by CoverityScan, CID#1469841 ("Dereference null return value")
>>>
>>> Fixes: 88a1dbdec682 ("pinctrl: pinctrl-single: Add functions to save and restore pinctrl context")
>>> Signed-off-by: Colin Ian King <colin.king@canonical.com>
>>> ---
>>> drivers/pinctrl/pinctrl-single.c | 14 +++++++++++---
>>> 1 file changed, 11 insertions(+), 3 deletions(-)
>>>
>>> diff --git a/drivers/pinctrl/pinctrl-single.c b/drivers/pinctrl/pinctrl-single.c
>>> index 9c3c00515aa0..0905ee002041 100644
>>> --- a/drivers/pinctrl/pinctrl-single.c
>>> +++ b/drivers/pinctrl/pinctrl-single.c
>>> @@ -1588,8 +1588,11 @@ static int pcs_save_context(struct pcs_device *pcs)
>>>
>>> mux_bytes = pcs->width / BITS_PER_BYTE;
>>>
>>> - if (!pcs->saved_vals)
>>> + if (!pcs->saved_vals) {
>>> pcs->saved_vals = devm_kzalloc(pcs->dev, pcs->size, GFP_ATOMIC);
>>
>>> + if (!pcs->saved_vals)
>>> + return -ENOMEM;
>>
>> Wouldn't make sense to move it out of the first condition?
>>
>> Something like
>>
>> if (!foo)
>> foo = ...malloc(...);
>> if (!foo)
>> return ...
>
> No, checking for NULL immediately after the allocation is more obvious
> and easier to parse.
+1 on that
>
> Johan
>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH][next] pinctrl: pinctrl-single: add allocation failure checking of saved_vals
2018-06-07 7:29 ` Johan Hovold
@ 2018-06-08 6:23 ` Tony Lindgren
0 siblings, 0 replies; 7+ messages in thread
From: Tony Lindgren @ 2018-06-08 6:23 UTC (permalink / raw)
To: Johan Hovold
Cc: Colin King, Haojian Zhuang, Linus Walleij, linux-arm-kernel,
linux-omap, linux-gpio, kernel-janitors, linux-kernel
* Johan Hovold <johan@kernel.org> [180607 07:32]:
> On Wed, Jun 06, 2018 at 02:43:38PM +0100, Colin King wrote:
>
> But for this fix, feel free to add:
>
> Reviewed-by: Johan Hovold <johan@kernel.org>
Acked-by: Tony Lindgren <tony@atomide.com>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH][next] pinctrl: pinctrl-single: add allocation failure checking of saved_vals
2018-06-06 13:43 [PATCH][next] pinctrl: pinctrl-single: add allocation failure checking of saved_vals Colin King
2018-06-06 16:02 ` Andy Shevchenko
2018-06-07 7:29 ` Johan Hovold
@ 2018-06-14 8:31 ` Linus Walleij
2 siblings, 0 replies; 7+ messages in thread
From: Linus Walleij @ 2018-06-14 8:31 UTC (permalink / raw)
To: Colin King
Cc: Tony Lindgren, Haojian Zhuang, Linux ARM, Linux-OMAP,
open list:GPIO SUBSYSTEM, kernel-janitors, linux-kernel
On Wed, Jun 6, 2018 at 3:43 PM, Colin King <colin.king@canonical.com> wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> Currently saved_vals is being allocated and there is no check for
> failed allocation (which is more likely than normal when using
> GFP_ATOMIC). Fix this by checking for a failed allocation and
> propagating this error return down the the caller chain.
>
> Detected by CoverityScan, CID#1469841 ("Dereference null return value")
>
> Fixes: 88a1dbdec682 ("pinctrl: pinctrl-single: Add functions to save and restore pinctrl context")
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
Patch applied with Johan's and Tony's ACKs.
Yours,
Linus Walleij
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2018-06-14 8:31 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-06-06 13:43 [PATCH][next] pinctrl: pinctrl-single: add allocation failure checking of saved_vals Colin King
2018-06-06 16:02 ` Andy Shevchenko
2018-06-07 7:35 ` Johan Hovold
2018-06-07 8:26 ` Colin Ian King
2018-06-07 7:29 ` Johan Hovold
2018-06-08 6:23 ` Tony Lindgren
2018-06-14 8:31 ` Linus Walleij
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).