diff mbox series

[v3,next] tty: sunsu: Simplify device_node cleanup by using __free

Message ID 20240518114051.18125-3-shresthprasad7@gmail.com
State New
Headers show
Series [v3,next] tty: sunsu: Simplify device_node cleanup by using __free | expand

Commit Message

Shresth Prasad May 18, 2024, 11:40 a.m. UTC
Add `__free` function attribute to `ap` and `match` pointer
initialisations which ensure that the pointers are freed as soon as they
go out of scope, thus removing the need to manually free them using
`of_node_put`.

This also removes the need for the `goto` statement and the `rc`
variable.

Suggested-by: Julia Lawall <julia.lawall@inria.fr>
Signed-off-by: Shresth Prasad <shresthprasad7@gmail.com>
---
Changes in v3:
    - Remove incorrect testing statement

I've tested the patch by cross compiling it for sparc systems and
booting the produced `image` file in `qemu-system-sparc`.

---
 drivers/tty/serial/sunsu.c | 37 +++++++++++--------------------------
 1 file changed, 11 insertions(+), 26 deletions(-)

Comments

Shresth Prasad June 1, 2024, 11:23 p.m. UTC | #1
On Sat, May 18, 2024 at 5:11 PM Shresth Prasad <shresthprasad7@gmail.com> wrote:
>
> Add `__free` function attribute to `ap` and `match` pointer
> initialisations which ensure that the pointers are freed as soon as they
> go out of scope, thus removing the need to manually free them using
> `of_node_put`.
>
> This also removes the need for the `goto` statement and the `rc`
> variable.
>
> Suggested-by: Julia Lawall <julia.lawall@inria.fr>
> Signed-off-by: Shresth Prasad <shresthprasad7@gmail.com>
> ---
> Changes in v3:
>     - Remove incorrect testing statement
>
> I've tested the patch by cross compiling it for sparc systems and
> booting the produced `image` file in `qemu-system-sparc`.
>
> ---
>  drivers/tty/serial/sunsu.c | 37 +++++++++++--------------------------
>  1 file changed, 11 insertions(+), 26 deletions(-)
>
> diff --git a/drivers/tty/serial/sunsu.c b/drivers/tty/serial/sunsu.c
> index 67a5fc70bb4b..0f463da5e7ce 100644
> --- a/drivers/tty/serial/sunsu.c
> +++ b/drivers/tty/serial/sunsu.c
> @@ -1382,44 +1382,29 @@ static inline struct console *SUNSU_CONSOLE(void)
>
>  static enum su_type su_get_type(struct device_node *dp)
>  {
> -       struct device_node *ap = of_find_node_by_path("/aliases");
> -       enum su_type rc = SU_PORT_PORT;
> +       struct device_node *ap __free(device_node) =
> +                           of_find_node_by_path("/aliases");
>
>         if (ap) {
>                 const char *keyb = of_get_property(ap, "keyboard", NULL);
>                 const char *ms = of_get_property(ap, "mouse", NULL);
> -               struct device_node *match;
>
>                 if (keyb) {
> -                       match = of_find_node_by_path(keyb);
> +                       struct device_node *match __free(device_node) =
> +                                           of_find_node_by_path(keyb);
>
> -                       /*
> -                        * The pointer is used as an identifier not
> -                        * as a pointer, we can drop the refcount on
> -                        * the of__node immediately after getting it.
> -                        */
> -                       of_node_put(match);
> -
> -                       if (dp == match) {
> -                               rc = SU_PORT_KBD;
> -                               goto out;
> -                       }
> +                       if (dp == match)
> +                               return SU_PORT_KBD;
>                 }
>                 if (ms) {
> -                       match = of_find_node_by_path(ms);
> +                       struct device_node *match __free(device_node) =
> +                                           of_find_node_by_path(ms);
>
> -                       of_node_put(match);
> -
> -                       if (dp == match) {
> -                               rc = SU_PORT_MS;
> -                               goto out;
> -                       }
> +                       if (dp == match)
> +                               return SU_PORT_MS;
>                 }
>         }
> -
> -out:
> -       of_node_put(ap);
> -       return rc;
> +       return SU_PORT_PORT;
>  }
>
>  static int su_probe(struct platform_device *op)
> --
> 2.44.0
>

Hi,

Any updates on this patch?

Regards,
Shresth
diff mbox series

Patch

diff --git a/drivers/tty/serial/sunsu.c b/drivers/tty/serial/sunsu.c
index 67a5fc70bb4b..0f463da5e7ce 100644
--- a/drivers/tty/serial/sunsu.c
+++ b/drivers/tty/serial/sunsu.c
@@ -1382,44 +1382,29 @@  static inline struct console *SUNSU_CONSOLE(void)
 
 static enum su_type su_get_type(struct device_node *dp)
 {
-	struct device_node *ap = of_find_node_by_path("/aliases");
-	enum su_type rc = SU_PORT_PORT;
+	struct device_node *ap __free(device_node) =
+			    of_find_node_by_path("/aliases");
 
 	if (ap) {
 		const char *keyb = of_get_property(ap, "keyboard", NULL);
 		const char *ms = of_get_property(ap, "mouse", NULL);
-		struct device_node *match;
 
 		if (keyb) {
-			match = of_find_node_by_path(keyb);
+			struct device_node *match __free(device_node) =
+					    of_find_node_by_path(keyb);
 
-			/*
-			 * The pointer is used as an identifier not
-			 * as a pointer, we can drop the refcount on
-			 * the of__node immediately after getting it.
-			 */
-			of_node_put(match);
-
-			if (dp == match) {
-				rc = SU_PORT_KBD;
-				goto out;
-			}
+			if (dp == match)
+				return SU_PORT_KBD;
 		}
 		if (ms) {
-			match = of_find_node_by_path(ms);
+			struct device_node *match __free(device_node) =
+					    of_find_node_by_path(ms);
 
-			of_node_put(match);
-
-			if (dp == match) {
-				rc = SU_PORT_MS;
-				goto out;
-			}
+			if (dp == match)
+				return SU_PORT_MS;
 		}
 	}
-
-out:
-	of_node_put(ap);
-	return rc;
+	return SU_PORT_PORT;
 }
 
 static int su_probe(struct platform_device *op)