Skip to contentSkip to conversation

tty: sunsu: Simplify device_node cleanup by using __free

PATCH Superseded Shresth Prasad 4 participants 9 messages May 1, 2024 – May 14, 2024

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 tty: sunsu: Simplify device_node cleanup by using __free

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.

Tested using a qemu x86_64 virtual machine.

---
Changes in v2:
- Specify how the patch was tested

 drivers/tty/serial/sunsu.c | 37 +++++++++++--------------------------
 1 file changed, 11 insertions(+), 26 deletions(-)
drivers/tty/serial/sunsu.c +11 -26
index 67a5fc70bb4b..0f463da5e7ce 100644
@@ -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)
Signature · 1 line
2.44.0
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 10 lines · Shresth Prasad

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.

Tested using a qemu x86_64 virtual machine.

Quoted 8 lines

---
Changes in v2:
- Specify how the patch was tested

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

Nice cleanup.

drivers/tty/serial/sunsu.c static inline struct console *SUNSU_CONSOLE(void) · line 1386

Quoted 10 lines
drivers/tty/serial/sunsu.c +2 -2
@@ -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 we used c++, that would be even nicer; like:
Destroyer ap(of_find_node_by_path("/aliases"));

But we don't :P. OTOH. can't we achieve that with macro-fu and typeof() magic? Perhaps not really exactly the above, but something like
Destroyer(ap, of_find_node_by_path("/aliases"));
maybe?

thanks,

Signature · 2 lines
js
suse labs
#2 lore raw
Quoted 18 lines · Jiri Slaby
Quoted 16 lines · Shresth Prasad

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.

Tested using a qemu x86_64 virtual machine.

---
Changes in v2:
- Specify how the patch was tested

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

Nice cleanup.

Thanks :)

Quoted 15 lines

drivers/tty/serial/sunsu.c static inline struct console *SUNSU_CONSOLE(void) · line 1386

Quoted 11 lines
drivers/tty/serial/sunsu.c +2 -2
@@ -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 we used c++, that would be even nicer; like:
Destroyer ap(of_find_node_by_path("/aliases"));

Quoted 4 lines

But we don't :P. OTOH. can't we achieve that with macro-fu and typeof() magic? Perhaps not really exactly the above, but something like
Destroyer(ap, of_find_node_by_path("/aliases"));
maybe?

Hmm, a macro like that could probably be written but it's more convenient to use the GCC attribute like in the current implementation, IMO.

Jonathan Corbert, who introduced this, wrote about it here:
https://lwn.net/Articles/934679/

Regards,
Shresth

#3 lore raw
In reply to Shresth Prasad
Quoted 9 lines · Shresth Prasad

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.

Tested using a qemu x86_64 virtual machine.

Eh, how can you test this with an x86_64 VM ???

config SERIAL_SUNSU
tristate "Sun SU serial support"
depends on SPARC && PCI

Signature · 1 line
 i.
Quoted 70 lines

---
Changes in v2:
- Specify how the patch was tested

 drivers/tty/serial/sunsu.c | 37 +++++++++++--------------------------
 1 file changed, 11 insertions(+), 26 deletions(-)
drivers/tty/serial/sunsu.c +11 -26
index 67a5fc70bb4b..0f463da5e7ce 100644
@@ -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)
#4 lore raw
Quoted 11 lines · Ilpo Järvinen
Quoted 8 lines · Shresth Prasad

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.

Quoted 8 lines
Quoted 1 line

Tested using a qemu x86_64 virtual machine.

Eh, how can you test this with an x86_64 VM ???

config SERIAL_SUNSU
tristate "Sun SU serial support"
depends on SPARC && PCI

By that, I mean that I compiled the kernel and ran the produced bzImage on a x86_64 qemu machine.
I unfortunately don't have the hardware to test it on, but I don't think the change is complex enough to require testing on real hardware (unless I'm assuming incorrectly).

Regards,
Shresth

Quoted 70 lines
Quoted 70 lines

---
Changes in v2:
- Specify how the patch was tested

 drivers/tty/serial/sunsu.c | 37 +++++++++++--------------------------
 1 file changed, 11 insertions(+), 26 deletions(-)
drivers/tty/serial/sunsu.c +11 -26
index 67a5fc70bb4b..0f463da5e7ce 100644
@@ -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)
maintainer
#5 lore raw
Quoted 17 lines · Shresth Prasad
Quoted 15 lines · Ilpo Järvinen
Quoted 9 lines · Shresth Prasad

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.

Tested using a qemu x86_64 virtual machine.

Eh, how can you test this with an x86_64 VM ???

Quoted 7 lines
Quoted 4 lines

config SERIAL_SUNSU
tristate "Sun SU serial support"
depends on SPARC && PCI

By that, I mean that I compiled the kernel and ran the produced bzImage on a x86_64 qemu machine.

But you didn't include the driver you were testing :(

Quoted 3 lines

I unfortunately don't have the hardware to test it on, but I don't think the change is complex enough to require testing on real hardware (unless I'm assuming incorrectly).

That's why I asked if you had tested this or not...

#6 lore raw
Quoted 27 lines · Greg KH
Quoted 17 lines · Shresth Prasad
Quoted 15 lines · Ilpo Järvinen
Quoted 9 lines · Shresth Prasad

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.

Tested using a qemu x86_64 virtual machine.

Eh, how can you test this with an x86_64 VM ???

Quoted 7 lines
Quoted 4 lines

config SERIAL_SUNSU
tristate "Sun SU serial support"
depends on SPARC && PCI

By that, I mean that I compiled the kernel and ran the produced bzImage on a x86_64 qemu machine.

Quoted 8 lines

But you didn't include the driver you were testing :(

Quoted 3 lines

I unfortunately don't have the hardware to test it on, but I don't think the change is complex enough to require testing on real hardware (unless I'm assuming incorrectly).

That's why I asked if you had tested this or not...

Really sorry about that, I thought compiling and booting would qualify as testing. What should I be doing then?

Regards,
Shresth

maintainer
#7 lore raw
Quoted 34 lines · Shresth Prasad
Quoted 33 lines · Greg KH
Quoted 17 lines · Shresth Prasad
Quoted 15 lines · Ilpo Järvinen
Quoted 9 lines · Shresth Prasad

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.

Tested using a qemu x86_64 virtual machine.

Eh, how can you test this with an x86_64 VM ???

Quoted 7 lines
Quoted 4 lines

config SERIAL_SUNSU
tristate "Sun SU serial support"
depends on SPARC && PCI

By that, I mean that I compiled the kernel and ran the produced bzImage on a x86_64 qemu machine.

But you didn't include the driver you were testing :(

Quoted 3 lines

I unfortunately don't have the hardware to test it on, but I don't think the change is complex enough to require testing on real hardware (unless I'm assuming incorrectly).

Quoted 5 lines
Quoted 2 lines

That's why I asked if you had tested this or not...

Really sorry about that, I thought compiling and booting would qualify as testing. What should I be doing then?

Compiling and booting the code you change would be a good start :)

thanks,

greg k-h

#8 lore raw
Quoted 39 lines · Greg KH
Quoted 37 lines · Shresth Prasad
Quoted 35 lines · Greg KH
Quoted 17 lines · Shresth Prasad
Quoted 15 lines · Ilpo Järvinen
Quoted 9 lines · Shresth Prasad

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.

Tested using a qemu x86_64 virtual machine.

Eh, how can you test this with an x86_64 VM ???

Quoted 7 lines
Quoted 4 lines

config SERIAL_SUNSU
tristate "Sun SU serial support"
depends on SPARC && PCI

By that, I mean that I compiled the kernel and ran the produced bzImage on a x86_64 qemu machine.

But you didn't include the driver you were testing :(

Quoted 3 lines

I unfortunately don't have the hardware to test it on, but I don't think the change is complex enough to require testing on real hardware (unless I'm assuming incorrectly).

That's why I asked if you had tested this or not...

Quoted 8 lines
Quoted 2 lines

Really sorry about that, I thought compiling and booting would qualify as testing. What should I be doing then?

Compiling and booting the code you change would be a good start :)

thanks,

greg k-h

I've managed to successfully cross compile the kernel for sparc, along with this module, but I couldn't figure out how to boot the generated kernel image.

I looked around for quite a while but couldn't find any straightforward guide suggesting how I would go about booting my own kernel on qemu-system-sparc.

I would really appreciate it if you could point me in the right direction.

Regards,
Shresth