diff mbox series

[06/13] of: unittest: Merge of_unittest_apply{,_revert}_overlay_check()

Message ID b733ee0b398ace191a787cce965829f2777fc49e.1689776064.git.geert+renesas@glider.be (mailing list archive)
State Superseded
Delegated to: Geert Uytterhoeven
Headers show
Series of: overlay/unittest: Miscellaneous fixes and improvements | expand

Commit Message

Geert Uytterhoeven July 19, 2023, 3 p.m. UTC
of_unittest_apply_overlay_check() and the first part of
of_unittest_apply_revert_overlay_check() are identical.
Reduce code duplication by replacing them by two wrappers around a
common helper.

Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
---
 drivers/of/unittest.c | 61 ++++++++++++++++---------------------------
 1 file changed, 22 insertions(+), 39 deletions(-)

Comments

Rob Herring (Arm) July 20, 2023, 6:31 p.m. UTC | #1
On Wed, Jul 19, 2023 at 05:00:06PM +0200, Geert Uytterhoeven wrote:
> of_unittest_apply_overlay_check() and the first part of
> of_unittest_apply_revert_overlay_check() are identical.
> Reduce code duplication by replacing them by two wrappers around a
> common helper.
> 
> Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
> ---
>  drivers/of/unittest.c | 61 ++++++++++++++++---------------------------
>  1 file changed, 22 insertions(+), 39 deletions(-)

I would do something like this instead:

8<-------------------------------------------------------------------
diff --git a/drivers/of/unittest.c b/drivers/of/unittest.c
index a406a12eb208..a9635935aa26 100644
--- a/drivers/of/unittest.c
+++ b/drivers/of/unittest.c
@@ -2102,7 +2102,7 @@ static int __init of_unittest_apply_overlay(int overlay_nr, int *ovcs_id)
 }
 
 /* apply an overlay while checking before and after states */
-static int __init of_unittest_apply_overlay_check(int overlay_nr,
+static int __init _of_unittest_apply_overlay_check(int overlay_nr,
 		int unittest_nr, int before, int after,
 		enum overlay_type ovtype)
 {
@@ -2133,6 +2133,16 @@ static int __init of_unittest_apply_overlay_check(int overlay_nr,
 		return -EINVAL;
 	}
 
+	return ovcs_id;
+}
+
+static int __init of_unittest_apply_overlay_check(int overlay_nr,
+		int unittest_nr, int before, int after,
+		enum overlay_type ovtype)
+{
+	int ovcs_id = _of_unittest_apply_overlay_check(overlay_nr, unittest_nr, before, after, ovtype);
+	if (ovcs_id < 0)
+		return ovcs_id;
 	return 0;
 }
 
@@ -2143,31 +2153,9 @@ static int __init of_unittest_apply_revert_overlay_check(int overlay_nr,
 {
 	int ret, ovcs_id, save_ovcs_id;
 
-	/* unittest device must be in before state */
-	if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
-		unittest(0, "%s with device @\"%s\" %s\n",
-				overlay_name_from_nr(overlay_nr),
-				unittest_path(unittest_nr, ovtype),
-				!before ? "enabled" : "disabled");
-		return -EINVAL;
-	}
-
-	/* apply the overlay */
-	ovcs_id = 0;
-	ret = of_unittest_apply_overlay(overlay_nr, &ovcs_id);
-	if (ret != 0) {
-		/* of_unittest_apply_overlay already called unittest() */
-		return ret;
-	}
-
-	/* unittest device must be in after state */
-	if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
-		unittest(0, "%s failed to create @\"%s\" %s\n",
-				overlay_name_from_nr(overlay_nr),
-				unittest_path(unittest_nr, ovtype),
-				!after ? "enabled" : "disabled");
-		return -EINVAL;
-	}
+	ovcs_id = _of_unittest_apply_overlay_check(overlay_nr, unittest_nr, before, after, ovtype);
+	if (ovcs_id < 0)
+		return ovcs_id;
 
 	save_ovcs_id = ovcs_id;
 	ret = of_overlay_remove(&ovcs_id);
Geert Uytterhoeven July 27, 2023, 2:04 p.m. UTC | #2
Hi Rob,

On Thu, Jul 20, 2023 at 8:31 PM Rob Herring <robh@kernel.org> wrote:
> On Wed, Jul 19, 2023 at 05:00:06PM +0200, Geert Uytterhoeven wrote:
> > of_unittest_apply_overlay_check() and the first part of
> > of_unittest_apply_revert_overlay_check() are identical.
> > Reduce code duplication by replacing them by two wrappers around a
> > common helper.
> >
> > Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
> > ---
> >  drivers/of/unittest.c | 61 ++++++++++++++++---------------------------
> >  1 file changed, 22 insertions(+), 39 deletions(-)
>
> I would do something like this instead:
>
> 8<-------------------------------------------------------------------
> diff --git a/drivers/of/unittest.c b/drivers/of/unittest.c
> index a406a12eb208..a9635935aa26 100644
> --- a/drivers/of/unittest.c
> +++ b/drivers/of/unittest.c
> @@ -2102,7 +2102,7 @@ static int __init of_unittest_apply_overlay(int overlay_nr, int *ovcs_id)
>  }
>
>  /* apply an overlay while checking before and after states */
> -static int __init of_unittest_apply_overlay_check(int overlay_nr,
> +static int __init _of_unittest_apply_overlay_check(int overlay_nr,
>                 int unittest_nr, int before, int after,
>                 enum overlay_type ovtype)
>  {
> @@ -2133,6 +2133,16 @@ static int __init of_unittest_apply_overlay_check(int overlay_nr,
>                 return -EINVAL;
>         }
>
> +       return ovcs_id;
> +}
> +
> +static int __init of_unittest_apply_overlay_check(int overlay_nr,
> +               int unittest_nr, int before, int after,
> +               enum overlay_type ovtype)
> +{
> +       int ovcs_id = _of_unittest_apply_overlay_check(overlay_nr, unittest_nr, before, after, ovtype);
> +       if (ovcs_id < 0)
> +               return ovcs_id;
>         return 0;
>  }
>
> @@ -2143,31 +2153,9 @@ static int __init of_unittest_apply_revert_overlay_check(int overlay_nr,
>  {
>         int ret, ovcs_id, save_ovcs_id;
>
> -       /* unittest device must be in before state */
> -       if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
> -               unittest(0, "%s with device @\"%s\" %s\n",
> -                               overlay_name_from_nr(overlay_nr),
> -                               unittest_path(unittest_nr, ovtype),
> -                               !before ? "enabled" : "disabled");
> -               return -EINVAL;
> -       }
> -
> -       /* apply the overlay */
> -       ovcs_id = 0;
> -       ret = of_unittest_apply_overlay(overlay_nr, &ovcs_id);
> -       if (ret != 0) {
> -               /* of_unittest_apply_overlay already called unittest() */
> -               return ret;
> -       }
> -
> -       /* unittest device must be in after state */
> -       if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
> -               unittest(0, "%s failed to create @\"%s\" %s\n",
> -                               overlay_name_from_nr(overlay_nr),
> -                               unittest_path(unittest_nr, ovtype),
> -                               !after ? "enabled" : "disabled");
> -               return -EINVAL;
> -       }
> +       ovcs_id = _of_unittest_apply_overlay_check(overlay_nr, unittest_nr, before, after, ovtype);
> +       if (ovcs_id < 0)
> +               return ovcs_id;
>
>         save_ovcs_id = ovcs_id;
>         ret = of_overlay_remove(&ovcs_id);

That's what I had done first, before I realized I could reduce it by
five more lines of code ;-)

mine:  1 file changed, 22 insertions(+), 39 deletions(-)
yours: 1 file changed, 14 insertions(+), 26 deletions(-)

Anyway, you're the maintainer, so I can update my patch if you insist...

Gr{oetje,eeting}s,

                        Geert
Rob Herring (Arm) July 27, 2023, 5:58 p.m. UTC | #3
On Thu, Jul 27, 2023 at 8:04 AM Geert Uytterhoeven <geert@linux-m68k.org> wrote:
>
> Hi Rob,
>
> On Thu, Jul 20, 2023 at 8:31 PM Rob Herring <robh@kernel.org> wrote:
> > On Wed, Jul 19, 2023 at 05:00:06PM +0200, Geert Uytterhoeven wrote:
> > > of_unittest_apply_overlay_check() and the first part of
> > > of_unittest_apply_revert_overlay_check() are identical.
> > > Reduce code duplication by replacing them by two wrappers around a
> > > common helper.
> > >
> > > Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
> > > ---
> > >  drivers/of/unittest.c | 61 ++++++++++++++++---------------------------
> > >  1 file changed, 22 insertions(+), 39 deletions(-)
> >
> > I would do something like this instead:
> >
> > 8<-------------------------------------------------------------------
> > diff --git a/drivers/of/unittest.c b/drivers/of/unittest.c
> > index a406a12eb208..a9635935aa26 100644
> > --- a/drivers/of/unittest.c
> > +++ b/drivers/of/unittest.c
> > @@ -2102,7 +2102,7 @@ static int __init of_unittest_apply_overlay(int overlay_nr, int *ovcs_id)
> >  }
> >
> >  /* apply an overlay while checking before and after states */
> > -static int __init of_unittest_apply_overlay_check(int overlay_nr,
> > +static int __init _of_unittest_apply_overlay_check(int overlay_nr,
> >                 int unittest_nr, int before, int after,
> >                 enum overlay_type ovtype)
> >  {
> > @@ -2133,6 +2133,16 @@ static int __init of_unittest_apply_overlay_check(int overlay_nr,
> >                 return -EINVAL;
> >         }
> >
> > +       return ovcs_id;
> > +}
> > +
> > +static int __init of_unittest_apply_overlay_check(int overlay_nr,
> > +               int unittest_nr, int before, int after,
> > +               enum overlay_type ovtype)
> > +{
> > +       int ovcs_id = _of_unittest_apply_overlay_check(overlay_nr, unittest_nr, before, after, ovtype);
> > +       if (ovcs_id < 0)
> > +               return ovcs_id;
> >         return 0;
> >  }
> >
> > @@ -2143,31 +2153,9 @@ static int __init of_unittest_apply_revert_overlay_check(int overlay_nr,
> >  {
> >         int ret, ovcs_id, save_ovcs_id;
> >
> > -       /* unittest device must be in before state */
> > -       if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
> > -               unittest(0, "%s with device @\"%s\" %s\n",
> > -                               overlay_name_from_nr(overlay_nr),
> > -                               unittest_path(unittest_nr, ovtype),
> > -                               !before ? "enabled" : "disabled");
> > -               return -EINVAL;
> > -       }
> > -
> > -       /* apply the overlay */
> > -       ovcs_id = 0;
> > -       ret = of_unittest_apply_overlay(overlay_nr, &ovcs_id);
> > -       if (ret != 0) {
> > -               /* of_unittest_apply_overlay already called unittest() */
> > -               return ret;
> > -       }
> > -
> > -       /* unittest device must be in after state */
> > -       if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
> > -               unittest(0, "%s failed to create @\"%s\" %s\n",
> > -                               overlay_name_from_nr(overlay_nr),
> > -                               unittest_path(unittest_nr, ovtype),
> > -                               !after ? "enabled" : "disabled");
> > -               return -EINVAL;
> > -       }
> > +       ovcs_id = _of_unittest_apply_overlay_check(overlay_nr, unittest_nr, before, after, ovtype);
> > +       if (ovcs_id < 0)
> > +               return ovcs_id;
> >
> >         save_ovcs_id = ovcs_id;
> >         ret = of_overlay_remove(&ovcs_id);
>
> That's what I had done first, before I realized I could reduce it by
> five more lines of code ;-)
>
> mine:  1 file changed, 22 insertions(+), 39 deletions(-)
> yours: 1 file changed, 14 insertions(+), 26 deletions(-)

Less change to review is also worthwhile.

> Anyway, you're the maintainer, so I can update my patch if you insist...

The other thing about this that I noticed is I recall gregkh not
liking the pattern where function parameters change what the function
does (e.g. do_x_or_y(bool do_y)).

So yes, I prefer mine.

Rob
diff mbox series

Patch

diff --git a/drivers/of/unittest.c b/drivers/of/unittest.c
index 42abfcd0cdffa4af..4c21c8ce99b9d8ff 100644
--- a/drivers/of/unittest.c
+++ b/drivers/of/unittest.c
@@ -2101,46 +2101,10 @@  static int __init of_unittest_apply_overlay(int overlay_nr, int *ovcs_id)
 	return 0;
 }
 
-/* apply an overlay while checking before and after states */
-static int __init of_unittest_apply_overlay_check(int overlay_nr,
-		int unittest_nr, int before, int after,
-		enum overlay_type ovtype)
-{
-	int ret, ovcs_id;
-
-	/* unittest device must be in before state */
-	if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
-		unittest(0, "%s with device @\"%s\" %s\n",
-				overlay_name_from_nr(overlay_nr),
-				unittest_path(unittest_nr, ovtype),
-				!before ? "enabled" : "disabled");
-		return -EINVAL;
-	}
-
-	/* apply the overlay */
-	ovcs_id = 0;
-	ret = of_unittest_apply_overlay(overlay_nr, &ovcs_id);
-	if (ret != 0) {
-		/* of_unittest_apply_overlay already called unittest() */
-		return ret;
-	}
-
-	/* unittest device must be in after state */
-	if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
-		unittest(0, "%s with device @\"%s\" %s\n",
-				overlay_name_from_nr(overlay_nr),
-				unittest_path(unittest_nr, ovtype),
-				!after ? "enabled" : "disabled");
-		return -EINVAL;
-	}
-
-	return 0;
-}
-
-/* apply an overlay and then revert it while checking before, after states */
-static int __init of_unittest_apply_revert_overlay_check(int overlay_nr,
+/* apply an overlay and optionally revert it while checking states */
+static int __init __of_unittest_apply_revert_overlay_check(int overlay_nr,
 		int unittest_nr, int before, int after,
-		enum overlay_type ovtype)
+		enum overlay_type ovtype, bool revert)
 {
 	int ret, ovcs_id, save_ovcs_id;
 
@@ -2170,6 +2134,9 @@  static int __init of_unittest_apply_revert_overlay_check(int overlay_nr,
 		return -EINVAL;
 	}
 
+	if (!revert)
+		return 0;
+
 	/* remove the overlay */
 	save_ovcs_id = ovcs_id;
 	ret = of_overlay_remove(&ovcs_id);
@@ -2193,6 +2160,22 @@  static int __init of_unittest_apply_revert_overlay_check(int overlay_nr,
 	return 0;
 }
 
+/* apply an overlay while checking before and after states */
+static inline int __init of_unittest_apply_overlay_check(int overlay_nr,
+	int unittest_nr, int before, int after, enum overlay_type ovtype)
+{
+	return __of_unittest_apply_revert_overlay_check(overlay_nr,
+				unittest_nr, before, after, ovtype, false);
+}
+
+/* apply an overlay and then revert it while checking before, after states */
+static int __init of_unittest_apply_revert_overlay_check(int overlay_nr,
+	int unittest_nr, int before, int after, enum overlay_type ovtype)
+{
+	return __of_unittest_apply_revert_overlay_check(overlay_nr,
+				unittest_nr, before, after, ovtype, true);
+}
+
 /* test activation of device */
 static void __init of_unittest_overlay_0(void)
 {