public inbox for linux-acpi@vger.kernel.org
 help / color / mirror / Atom feed
From: "Nuno Sá" <noname.nuno@gmail.com>
To: Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	"Rafael J. Wysocki" <rafael.j.wysocki@intel.com>,
	linux-acpi@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: "Daniel Scally" <djrscally@gmail.com>,
	"Heikki Krogerus" <heikki.krogerus@linux.intel.com>,
	"Sakari Ailus" <sakari.ailus@linux.intel.com>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Rafael J. Wysocki" <rafael@kernel.org>,
	"Len Brown" <lenb@kernel.org>, "Nuno Sá" <nuno.sa@analog.com>
Subject: Re: [PATCH v2 1/1] device property: Allow error pointer to be passed to fwnode APIs
Date: Sat, 05 Mar 2022 13:46:56 +0100	[thread overview]
Message-ID: <8afcf0b81f78ffdda8bcac5f0fd1d4c40dc4f8d6.camel@gmail.com> (raw)
In-Reply-To: <20220304173256.39059-1-andriy.shevchenko@linux.intel.com>

On Fri, 2022-03-04 at 19:32 +0200, Andy Shevchenko wrote:
> Some of the fwnode APIs might return an error pointer instead of NULL
> or valid fwnode handle. The result of such API call may be considered
> optional and hence the test for it is usually done in a form of
> 
>         fwnode = fwnode_find_reference(...);
>         if (IS_ERR_OR_NULL(fwnode))
>                 ...error handling...
> 
> Nevertheless the resulting fwnode may have bumped reference count and
> hence caller of the above API is obliged to call fwnode_handle_put().
> Since fwnode may be not valid either as NULL or error pointer the
> check
> has to be performed there. This approach uglifies the code and adds
> a point of making a mistake, i.e. forgetting about error point case.
> 
> To prevent this allow error pointer to be passed to the fwnode APIs.
> 
> Fixes: 83b34afb6b79 ("device property: Introduce
> fwnode_find_reference()")
> Reported-by: Nuno Sá <nuno.sa@analog.com>
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> ---
> 
> v2: adjusted the entire fwnode API (Sakari)
> 
> Nuno, can you test this with the ltc2983 series, including the
> IS_ERR_OR_NULL()
> fix to it?
> 

Sure... I will test that series together with this patch.
>  drivers/base/property.c | 45 +++++++++++++++++++++++++--------------
> --
>  include/linux/fwnode.h  | 10 ++++-----
>  2 files changed, 33 insertions(+), 22 deletions(-)
> 
> diff --git a/drivers/base/property.c b/drivers/base/property.c
> index c0e94cce9c29..1922818470b0 100644
> --- a/drivers/base/property.c
> +++ b/drivers/base/property.c
> @@ -9,6 +9,7 @@
>  
>  #include <linux/acpi.h>
>  #include <linux/export.h>
> +#include <linux/fwnode.h>
>  #include <linux/kernel.h>
>  #include <linux/of.h>
>  #include <linux/of_address.h>
> @@ -45,14 +46,16 @@ EXPORT_SYMBOL_GPL(device_property_present);
>  bool fwnode_property_present(const struct fwnode_handle *fwnode,
>                              const char *propname)
>  {
> -       bool ret;
> +       if (IS_ERR_OR_NULL(fwnode))
> +               return false;
>  
> -       ret = fwnode_call_bool_op(fwnode, property_present,
> propname);
> -       if (ret == false && !IS_ERR_OR_NULL(fwnode) &&
> -           !IS_ERR_OR_NULL(fwnode->secondary))
> -               ret = fwnode_call_bool_op(fwnode->secondary,
> property_present,
> -                                        propname);
> -       return ret;
> +       if (fwnode_call_bool_op(fwnode, property_present, propname))
> +               return true;
> +
> +       if (IS_ERR_OR_NULL(fwnode->secondary))
> +               return false;
> +
> +       return fwnode_call_bool_op(fwnode->secondary,
> property_present, propname);
>  }
>  EXPORT_SYMBOL_GPL(fwnode_property_present);
>  
> @@ -232,10 +235,12 @@ static int fwnode_property_read_int_array(const
> struct fwnode_handle *fwnode,
>  {
>         int ret;
>  
> +       if (IS_ERR_OR_NULL(fwnode))
> +               return -EINVAL;
> +
>         ret = fwnode_call_int_op(fwnode, property_read_int_array,
> propname,
>                                  elem_size, val, nval);
> -       if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
> -           !IS_ERR_OR_NULL(fwnode->secondary))
> +       if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode->secondary))
>                 ret = fwnode_call_int_op(
>                         fwnode->secondary, property_read_int_array,
> propname,
>                         elem_size, val, nval);
> @@ -371,10 +376,12 @@ int fwnode_property_read_string_array(const
> struct fwnode_handle *fwnode,
>  {
>         int ret;
>  
> +       if (IS_ERR_OR_NULL(fwnode))
> +               return -EINVAL;
> +
>         ret = fwnode_call_int_op(fwnode, property_read_string_array,
> propname,
>                                  val, nval);
> -       if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode) &&
> -           !IS_ERR_OR_NULL(fwnode->secondary))
> +       if (ret == -EINVAL && !IS_ERR_OR_NULL(fwnode->secondary))
>                 ret = fwnode_call_int_op(fwnode->secondary,
>                                          property_read_string_array,

Isn't !IS_ERR_OR_NULL(fwnode->secondary)) redundant? AFAIU,
fwnode_call_int_op() will already check the fwnode and only call the op
if the pointer is valid... 

- Nuno Sá


  reply	other threads:[~2022-03-05 12:47 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-04 17:32 [PATCH v2 1/1] device property: Allow error pointer to be passed to fwnode APIs Andy Shevchenko
2022-03-05 12:46 ` Nuno Sá [this message]
2022-03-05 21:46   ` Andy Shevchenko
2022-03-07 16:15 ` Sa, Nuno
2022-03-07 16:17   ` Andy Shevchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8afcf0b81f78ffdda8bcac5f0fd1d4c40dc4f8d6.camel@gmail.com \
    --to=noname.nuno@gmail.com \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=djrscally@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=heikki.krogerus@linux.intel.com \
    --cc=lenb@kernel.org \
    --cc=linux-acpi@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nuno.sa@analog.com \
    --cc=rafael.j.wysocki@intel.com \
    --cc=rafael@kernel.org \
    --cc=sakari.ailus@linux.intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox