[v2,5/5] ACPI: property: Refactor acpi_data_prop_read_single()

Message ID 20210212141121.62115-5-andriy.shevchenko@linux.intel.com
State New
Headers show
Series
  • [v2,1/5] ACPI: property: Remove dead code
Related show

Commit Message

Andy Shevchenko Feb. 12, 2021, 2:11 p.m.
Refactor acpi_data_prop_read_single() for less LOCs and better maintenance.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/acpi/property.c | 80 ++++++++++++++++++-----------------------
 1 file changed, 34 insertions(+), 46 deletions(-)

Comments

Rafael J. Wysocki Feb. 12, 2021, 2:31 p.m. | #1
On Fri, Feb 12, 2021 at 3:14 PM Andy Shevchenko
<andriy.shevchenko@linux.intel.com> wrote:
 >
> Refactor acpi_data_prop_read_single() for less LOCs and better maintenance.
>
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> ---
>  drivers/acpi/property.c | 80 ++++++++++++++++++-----------------------
>  1 file changed, 34 insertions(+), 46 deletions(-)
>
> diff --git a/drivers/acpi/property.c b/drivers/acpi/property.c
> index e312ebaed8db..494cf283a573 100644
> --- a/drivers/acpi/property.c
> +++ b/drivers/acpi/property.c
> @@ -785,60 +785,48 @@ static int acpi_data_prop_read_single(const struct acpi_device_data *data,
>                                       enum dev_prop_type proptype, void *val)
>  {
>         const union acpi_object *obj;
> -       int ret;
> +       int ret = 0;
>
> -       if (proptype >= DEV_PROP_U8 && proptype <= DEV_PROP_U64) {
> +       if (proptype >= DEV_PROP_U8 && proptype <= DEV_PROP_U64)
>                 ret = acpi_data_get_property(data, propname, ACPI_TYPE_INTEGER, &obj);
> -               if (ret)
> -                       return ret;
> -
> -               switch (proptype) {
> -               case DEV_PROP_U8:
> -                       if (obj->integer.value > U8_MAX)
> -                               return -EOVERFLOW;
> -
> -                       if (val)
> -                               *(u8 *)val = obj->integer.value;
> -
> -                       break;

The empty lines of code above are intentional, so please retain them.

> -               case DEV_PROP_U16:
> -                       if (obj->integer.value > U16_MAX)
> -                               return -EOVERFLOW;
> -
> -                       if (val)
> -                               *(u16 *)val = obj->integer.value;
> -
> -                       break;
> -               case DEV_PROP_U32:
> -                       if (obj->integer.value > U32_MAX)
> -                               return -EOVERFLOW;
> -
> -                       if (val)
> -                               *(u32 *)val = obj->integer.value;
> -
> -                       break;
> -               default:
> -                       if (val)
> -                               *(u64 *)val = obj->integer.value;
> -
> -                       break;
> -               }
> -
> -               if (!val)
> -                       return 1;
> -       } else if (proptype == DEV_PROP_STRING) {
> +       else if (proptype == DEV_PROP_STRING)
>                 ret = acpi_data_get_property(data, propname, ACPI_TYPE_STRING, &obj);
> -               if (ret)
> -                       return ret;
> +       if (ret)
> +               return ret;

else if (!val)
        ret = 1;

>
> +       switch (proptype) {
> +       case DEV_PROP_U8:
> +               if (obj->integer.value > U8_MAX)
> +                       return -EOVERFLOW;
> +               if (val)
> +                       *(u8 *)val = obj->integer.value;
> +               break;
> +       case DEV_PROP_U16:
> +               if (obj->integer.value > U16_MAX)
> +                       return -EOVERFLOW;
> +               if (val)
> +                       *(u16 *)val = obj->integer.value;
> +               break;
> +       case DEV_PROP_U32:
> +               if (obj->integer.value > U32_MAX)
> +                       return -EOVERFLOW;
> +               if (val)
> +                       *(u32 *)val = obj->integer.value;
> +               break;
> +       case DEV_PROP_U64:
> +               if (val)
> +                       *(u64 *)val = obj->integer.value;
> +               break;
> +       case DEV_PROP_STRING:
>                 if (val)
>                         *(char **)val = obj->string.pointer;
> -
>                 return 1;
> -       } else {
> -               ret = -EINVAL;
> +       default:
> +               return -EINVAL;
>         }
> -       return ret;

Retain this.

> +
> +       /* When no storage provided return number of available values */
> +       return val ? 0 : 1;

And this is just not looking good to me, sorry.

>  }
>
>  static int acpi_copy_property_array_u8(const union acpi_object *items, u8 *val,
> --
Andy Shevchenko Feb. 12, 2021, 4 p.m. | #2
On Fri, Feb 12, 2021 at 03:31:24PM +0100, Rafael J. Wysocki wrote:
> On Fri, Feb 12, 2021 at 3:14 PM Andy Shevchenko
> <andriy.shevchenko@linux.intel.com> wrote:

> > Refactor acpi_data_prop_read_single() for less LOCs and better maintenance.

Thanks for review, my answers below.

...

> > +       if (ret)
> > +               return ret;
> 
> else if (!val)
>         ret = 1;

But then it become a bug again :-)

...

> And this is just not looking good to me, sorry.

Yeah, I think this patch is not needed right now. At least it seems no gain
from it.
Andy Shevchenko Feb. 12, 2021, 4:24 p.m. | #3
On Fri, Feb 12, 2021 at 6:11 PM Rafael J. Wysocki <rafael@kernel.org> wrote:
> On Fri, Feb 12, 2021 at 5:01 PM Andy Shevchenko
> <andriy.shevchenko@linux.intel.com> wrote:
> > On Fri, Feb 12, 2021 at 03:31:24PM +0100, Rafael J. Wysocki wrote:
> > > On Fri, Feb 12, 2021 at 3:14 PM Andy Shevchenko
> > > <andriy.shevchenko@linux.intel.com> wrote:

...

> > > > +       if (ret)
> > > > +               return ret;
> > >
> > > else if (!val)
> > >         ret = 1;
> >
> > But then it become a bug again :-)
>
> I'm not sure why?  The checks below will still happen and they may
> cause an error to be returned.

Oh, I misinterpreted ret = as plain return. Right. Seems okay.

Patch

diff --git a/drivers/acpi/property.c b/drivers/acpi/property.c
index e312ebaed8db..494cf283a573 100644
--- a/drivers/acpi/property.c
+++ b/drivers/acpi/property.c
@@ -785,60 +785,48 @@  static int acpi_data_prop_read_single(const struct acpi_device_data *data,
 				      enum dev_prop_type proptype, void *val)
 {
 	const union acpi_object *obj;
-	int ret;
+	int ret = 0;
 
-	if (proptype >= DEV_PROP_U8 && proptype <= DEV_PROP_U64) {
+	if (proptype >= DEV_PROP_U8 && proptype <= DEV_PROP_U64)
 		ret = acpi_data_get_property(data, propname, ACPI_TYPE_INTEGER, &obj);
-		if (ret)
-			return ret;
-
-		switch (proptype) {
-		case DEV_PROP_U8:
-			if (obj->integer.value > U8_MAX)
-				return -EOVERFLOW;
-
-			if (val)
-				*(u8 *)val = obj->integer.value;
-
-			break;
-		case DEV_PROP_U16:
-			if (obj->integer.value > U16_MAX)
-				return -EOVERFLOW;
-
-			if (val)
-				*(u16 *)val = obj->integer.value;
-
-			break;
-		case DEV_PROP_U32:
-			if (obj->integer.value > U32_MAX)
-				return -EOVERFLOW;
-
-			if (val)
-				*(u32 *)val = obj->integer.value;
-
-			break;
-		default:
-			if (val)
-				*(u64 *)val = obj->integer.value;
-
-			break;
-		}
-
-		if (!val)
-			return 1;
-	} else if (proptype == DEV_PROP_STRING) {
+	else if (proptype == DEV_PROP_STRING)
 		ret = acpi_data_get_property(data, propname, ACPI_TYPE_STRING, &obj);
-		if (ret)
-			return ret;
+	if (ret)
+		return ret;
 
+	switch (proptype) {
+	case DEV_PROP_U8:
+		if (obj->integer.value > U8_MAX)
+			return -EOVERFLOW;
+		if (val)
+			*(u8 *)val = obj->integer.value;
+		break;
+	case DEV_PROP_U16:
+		if (obj->integer.value > U16_MAX)
+			return -EOVERFLOW;
+		if (val)
+			*(u16 *)val = obj->integer.value;
+		break;
+	case DEV_PROP_U32:
+		if (obj->integer.value > U32_MAX)
+			return -EOVERFLOW;
+		if (val)
+			*(u32 *)val = obj->integer.value;
+		break;
+	case DEV_PROP_U64:
+		if (val)
+			*(u64 *)val = obj->integer.value;
+		break;
+	case DEV_PROP_STRING:
 		if (val)
 			*(char **)val = obj->string.pointer;
-
 		return 1;
-	} else {
-		ret = -EINVAL;
+	default:
+		return -EINVAL;
 	}
-	return ret;
+
+	/* When no storage provided return number of available values */
+	return val ? 0 : 1;
 }
 
 static int acpi_copy_property_array_u8(const union acpi_object *items, u8 *val,