Skip to contentSkip to conversation

powermac: Call of_node_put(bk_node) only once in pmac_has_backlight_type()

PATCH In review Markus Elfring 6 participants 10 messages Oct 2, 2024 – Nov 17, 2024

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 powermac: Call of_node_put(bk_node) only once in pmac_has_backlight_type()

From: Markus Elfring <[email protected]>
Date: Wed, 2 Oct 2024 21:50:27 +0200

An of_node_put(bk_node) call was immediately used after a pointer check for an of_get_property() call in this function implementation. Thus call such a function only once instead directly before the check.

This issue was transformed by using the Coccinelle software.

---
 arch/powerpc/platforms/powermac/backlight.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
arch/powerpc/platforms/powermac/backlight.c +2 -4
index 12bc01353bd3..d3666595a62e 100644
@@ -61,11 +61,9 @@ int pmac_has_backlight_type(const char *type)
 	if (bk_node) {
 		const char *prop = of_get_property(bk_node,
 				"backlight-control", NULL);
-		if (prop && strncmp(prop, type, strlen(type)) == 0) {
-			of_node_put(bk_node);
-			return 1;
-		}
 		of_node_put(bk_node);
+		if (prop && strncmp(prop, type, strlen(type)) == 0)
+			return 1;
 	}
 
 	return 0;

--
2.46.1

#1 lore raw

Le 02/10/2024 à 22:02, Markus Elfring a écrit :

Quoted 6 lines

From: Markus Elfring <[email protected]>
Date: Wed, 2 Oct 2024 21:50:27 +0200

An of_node_put(bk_node) call was immediately used after a pointer check for an of_get_property() call in this function implementation. Thus call such a function only once instead directly before the check.

It seems pointless to perform a put immediately after a get. Shouldn't of_find_property() be used instead ? And then of_property_read_string() would probably be better.

Maybe you can even use of_property_match_string().

Quoted 29 lines

This issue was transformed by using the Coccinelle software.

---
  arch/powerpc/platforms/powermac/backlight.c | 6 ++----
  1 file changed, 2 insertions(+), 4 deletions(-)
arch/powerpc/platforms/powermac/backlight.c +2 -5
index 12bc01353bd3..d3666595a62e 100644
@@ -61,11 +61,9 @@ int pmac_has_backlight_type(const char *type)
  	if (bk_node) {
  		const char *prop = of_get_property(bk_node,
  				"backlight-control", NULL);
-		if (prop && strncmp(prop, type, strlen(type)) == 0) {
-			of_node_put(bk_node);
-			return 1;
-		}
  		of_node_put(bk_node);
+		if (prop && strncmp(prop, type, strlen(type)) == 0)
+			return 1;
  	}
 
  	return 0;
--

2.46.1

#2 lore raw

First of all, the change is wrong. We can't dereference "prop" after calling of_node_put(). You have to be a bit extra careful reviewing Markus's patches because a lot of the rest of us have blocked these messages so you're on your own in that way.

Quoted 10 lines · Christophe Leroy

Le 02/10/2024 à 22:02, Markus Elfring a écrit :

Quoted 6 lines

From: Markus Elfring <[email protected]>
Date: Wed, 2 Oct 2024 21:50:27 +0200

An of_node_put(bk_node) call was immediately used after a pointer check for an of_get_property() call in this function implementation. Thus call such a function only once instead directly before the check.

Quoted 5 lines

It seems pointless to perform a put immediately after a get. Shouldn't of_find_property() be used instead ? And then of_property_read_string() would probably be better.

Maybe you can even use of_property_match_string().

The of_get_property() function doesn't do a get as in get/put, it just finds the property and returns it. It doesn't bump the reference count. It's a confusing name in that way. The The of_node_put() pairs with of_find_node_by_name().

regards,
dan carpenter

#3 lore raw
Quoted 4 lines · Dan Carpenter

First of all, the change is wrong. We can't dereference "prop" after calling of_node_put(). You have to be a bit extra careful reviewing Markus's patches because a lot of the rest of us have blocked these messages so you're on your own in that way.

Yep, I plonked him some time ago and everything is in spam.

The code looks just incorrect and I think Markus did not understand it before transforming.

Best regards,
Krzysztof

#4 lore raw
In reply to Dan Carpenter
Quoted 2 lines

First of all, the change is wrong. We can't dereference "prop" after calling of_node_put().

…

Quoted 4 lines

The of_get_property() function doesn't do a get as in get/put, it just finds the property and returns it. It doesn't bump the reference count. It's a confusing name in that way. The The of_node_put() pairs with of_find_node_by_name().

Thanks for your information.

* Do you see opportunities for improving the software documentation accordingly?

* How much can source code analysis tools influence development efforts?

Regards,
Markus

#5 lore raw
In reply to Christophe Leroy
Quoted 8 lines · Christophe Leroy

Le 02/10/2024 à 22:02, Markus Elfring a écrit :

Quoted 6 lines

From: Markus Elfring <[email protected]>
Date: Wed, 2 Oct 2024 21:50:27 +0200

An of_node_put(bk_node) call was immediately used after a pointer check for an of_get_property() call in this function implementation. Thus call such a function only once instead directly before the check.

Quoted 5 lines

It seems pointless to perform a put immediately after a get. Shouldn't of_find_property() be used instead ? And then of_property_read_string() would probably be better.

Maybe you can even use of_property_match_string().

Yes that would clean it up nicely I think, eg:

int pmac_has_backlight_type(const char *type)

{
        struct device_node* bk_node = of_find_node_by_name(NULL, "backlight");
        int i;

        i = of_property_match_string(bk_node, "backlight-control", type);
        of_node_put(bk_node);

        return i >= 0;
}

cheers

#6 lore raw

PATCH 1/1 powermac: Use of_property_match_string() in pmac_has_backlight_type()

In reply to Christophe Leroy

From: Markus Elfring <[email protected]>
Date: Fri, 11 Oct 2024 18:10:06 +0200

Replace an of_get_property() call by of_property_match_string() so that this function implementation can be simplified.

---
 arch/powerpc/platforms/powermac/backlight.c | 14 +++-----------
 1 file changed, 3 insertions(+), 11 deletions(-)
arch/powerpc/platforms/powermac/backlight.c +3 -11
index 12bc01353bd3..79741370c40c 100644
@@ -57,18 +57,10 @@ struct backlight_device *pmac_backlight;
 int pmac_has_backlight_type(const char *type)
 {
 	struct device_node* bk_node = of_find_node_by_name(NULL, "backlight");
+	int i = of_property_match_string(bk_node, "backlight-control", type);
 
-	if (bk_node) {
-		const char *prop = of_get_property(bk_node,
-				"backlight-control", NULL);
-		if (prop && strncmp(prop, type, strlen(type)) == 0) {
-			of_node_put(bk_node);
-			return 1;
-		}
-		of_node_put(bk_node);
-	}
-
-	return 0;
+	of_node_put(bk_node);
+	return i >= 0;
 }
 
 static void pmac_backlight_key_worker(struct work_struct *work)

--
2.46.1

Reviewed-by on v1 1/1
#7 lore raw

Re: [PATCH] powermac: Use of_property_match_string() in pmac_has_backlight_type()

Le 11/10/2024 à 18:18, Markus Elfring a écrit :

Quoted 28 lines

From: Markus Elfring <[email protected]>
Date: Fri, 11 Oct 2024 18:10:06 +0200

Replace an of_get_property() call by of_property_match_string() so that this function implementation can be simplified.

---
  arch/powerpc/platforms/powermac/backlight.c | 14 +++-----------
  1 file changed, 3 insertions(+), 11 deletions(-)
arch/powerpc/platforms/powermac/backlight.c +1 -3
index 12bc01353bd3..79741370c40c 100644
@@ -57,18 +57,10 @@ struct backlight_device *pmac_backlight;
  int pmac_has_backlight_type(const char *type)
  {
  	struct device_node* bk_node = of_find_node_by_name(NULL, "backlight");
+	int i = of_property_match_string(bk_node, "backlight-control", type);
 
-	if (bk_node) {
-		const char *prop = of_get_property(bk_node,
-				"backlight-control", NULL);

arch/powerpc/platforms/powermac/backlight.c struct backlight_device *pmac_backlight; · line 63

Quoted 10 lines
arch/powerpc/platforms/powermac/backlight.c +2 -8
-		if (prop && strncmp(prop, type, strlen(type)) == 0) {
-			of_node_put(bk_node);
-			return 1;
-		}
-		of_node_put(bk_node);
-	}
-
-	return 0;
+	of_node_put(bk_node);
+	return i >= 0;

Could have been:

	return !IS_ERR_VALUE(i);

Never mind,

Quoted 6 lines
arch/powerpc/platforms/powermac/backlight.c +0 -1
  }
 
  static void pmac_backlight_key_worker(struct work_struct *work)
--

2.46.1

#8 lore raw

Re: [PATCH] powermac: Use of_property_match_string() in pmac_has_backlight_type()

Quoted 26 lines · Christophe Leroy
Quoted 13 lines
arch/powerpc/platforms/powermac/backlight.c +1 -3
index 12bc01353bd3..79741370c40c 100644
@@ -57,18 +57,10 @@ struct backlight_device *pmac_backlight;
  int pmac_has_backlight_type(const char *type)
  {
  	struct device_node* bk_node = of_find_node_by_name(NULL, "backlight");
+	int i = of_property_match_string(bk_node, "backlight-control", type);
 
-	if (bk_node) {
-		const char *prop = of_get_property(bk_node,
-				"backlight-control", NULL);

arch/powerpc/platforms/powermac/backlight.c struct backlight_device *pmac_backlight; · line 63

Quoted 10 lines
arch/powerpc/platforms/powermac/backlight.c +2 -8
-		if (prop && strncmp(prop, type, strlen(type)) == 0) {
-			of_node_put(bk_node);
-			return 1;
-		}
-		of_node_put(bk_node);
-	}
-
-	return 0;
+	of_node_put(bk_node);
+	return i >= 0;

Could have been:

Quoted 2 lines
	return !IS_ERR_VALUE(i);

IS_ERR_VALUE() macro should only be used when you're dealing with memory addresses. What I mean is there places in mm/ where we pass addresses as unsigned long values instead of pointers. For example, get_unmapped_area() returns unsigned long. The IS_ERR_VALUE() macro is necessary for that.

For regular error codes, we can just check for negatives. we don't have do anything fancy.

Of course, you can find counter examples, like msm_iommu_attach_dev() or st_fdma_of_xlate(). <small joke>But in those cases, it's done to deliberately to ensure that the code will never accidentally get built on 64bit systems. </small joke>

regards,
dan carpenter