Skip to contentSkip to conversation

pstore/ram: Replace of_node_put with __free() for automatic cleanup

PATCH Superseded Abhinav Jain 3 participants 4 messages Apr 15, 2024 – Jun 5, 2024 Pstore filesystem

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pstore/ram: Replace of_node_put with __free() for automatic cleanup

Add __free(device_node) to the parent_node struct declaration. Move declaration to initialization for ensuring scope sanity. Remove of_node_put from parent_node struct.

---
 fs/pstore/ram.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
fs/pstore/ram.c +1 -3
index b1a455f42e93..14f2f4864e48 100644
@@ -644,7 +644,6 @@ static int ramoops_parse_dt(struct platform_device *pdev,
 			    struct ramoops_platform_data *pdata)
 {
 	struct device_node *of_node = pdev->dev.of_node;
-	struct device_node *parent_node;
 	struct resource *res;
 	u32 value;
 	int ret;
@@ -704,14 +703,13 @@ static int ramoops_parse_dt(struct platform_device *pdev,
 	 * we're not a child of "reserved-memory" and mimicking the
 	 * expected behavior.
 	 */
-	parent_node = of_get_parent(of_node);
+	struct device_node *parent_node __free(device_node) = of_node_parent(of_node);
 	if (!of_node_name_eq(parent_node, "reserved-memory") &&
 	    !pdata->console_size && !pdata->ftrace_size &&
 	    !pdata->pmsg_size && !pdata->ecc_info.ecc_size) {
 		pdata->console_size = pdata->record_size;
 		pdata->pmsg_size = pdata->record_size;
 	}
-	of_node_put(parent_node);
 
 	return 0;
 }
Signature · 1 line
2.34.1
#1 lore raw
Quoted 23 lines · Abhinav Jain

Add __free(device_node) to the parent_node struct declaration. Move declaration to initialization for ensuring scope sanity. Remove of_node_put from parent_node struct.

---
 fs/pstore/ram.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
fs/pstore/ram.c +0 -1
index b1a455f42e93..14f2f4864e48 100644
@@ -644,7 +644,6 @@ static int ramoops_parse_dt(struct platform_device *pdev,
 			    struct ramoops_platform_data *pdata)
 {
 	struct device_node *of_node = pdev->dev.of_node;
-	struct device_node *parent_node;
 	struct resource *res;
 	u32 value;
 	int ret;

fs/pstore/ram.c static int ramoops_parse_dt(struct platform_device *pdev, · line 706

Quoted 5 lines
fs/pstore/ram.c +1 -1
@@ -704,14 +703,13 @@ static int ramoops_parse_dt(struct platform_device *pdev,
 	 * we're not a child of "reserved-memory" and mimicking the
 	 * expected behavior.
 	 */
-	parent_node = of_get_parent(of_node);
+	struct device_node *parent_node __free(device_node) = of_node_parent(of_node);

Please don't move variable definitions into the middle of the function body. :)

fs/pstore/ram.c static int ramoops_parse_dt(struct platform_device *pdev, · line 714

Quoted 7 lines
fs/pstore/ram.c +0 -1
 	if (!of_node_name_eq(parent_node, "reserved-memory") &&
 	    !pdata->console_size && !pdata->ftrace_size &&
 	    !pdata->pmsg_size && !pdata->ecc_info.ecc_size) {
 		pdata->console_size = pdata->record_size;
 		pdata->pmsg_size = pdata->record_size;
 	}
-	of_node_put(parent_node);

So this change is functionally fine, but there's really no good reason to do this -- there is no fancy error handling here, so there's no benefit to making this change. It doesn't really help readability.

-Kees

Quoted 6 lines
fs/pstore/ram.c +0 -0
 	return 0;
 }
Signature · 1 line
2.34.1
Signature · 1 line
Kees Cook
#2 lore raw
Quoted 24 lines · Kees Cook
Quoted 23 lines · Abhinav Jain

Add __free(device_node) to the parent_node struct declaration. Move declaration to initialization for ensuring scope sanity. Remove of_node_put from parent_node struct.

---
 fs/pstore/ram.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
fs/pstore/ram.c +0 -1
index b1a455f42e93..14f2f4864e48 100644
@@ -644,7 +644,6 @@ static int ramoops_parse_dt(struct platform_device *pdev,
 			    struct ramoops_platform_data *pdata)
 {
 	struct device_node *of_node = pdev->dev.of_node;
-	struct device_node *parent_node;
 	struct resource *res;
 	u32 value;
 	int ret;
@@ -704,14 +703,13 @@ static int ramoops_parse_dt(struct platform_device *pdev,
Quoted 8 lines

fs/pstore/ram.c static int ramoops_parse_dt(struct platform_device *pdev, · line 706

Quoted 5 lines
fs/pstore/ram.c +1 -1
 	 * we're not a child of "reserved-memory" and mimicking the
 	 * expected behavior.
 	 */
-	parent_node = of_get_parent(of_node);
+	struct device_node *parent_node __free(device_node) = of_node_parent(of_node);

Please don't move variable definitions into the middle of the function body. :)

This is done in other cases where it makes more sense to put the initialization later in the function. The point is that the variable has to be initialized, and puttng the declaration lower, which is now allowed when needed, is better than first uselessly initializing the variable to NULL.

Quoted 12 lines

fs/pstore/ram.c static int ramoops_parse_dt(struct platform_device *pdev, · line 714

Quoted 8 lines
fs/pstore/ram.c +0 -1
 	if (!of_node_name_eq(parent_node, "reserved-memory") &&
 	    !pdata->console_size && !pdata->ftrace_size &&
 	    !pdata->pmsg_size && !pdata->ecc_info.ecc_size) {
 		pdata->console_size = pdata->record_size;
 		pdata->pmsg_size = pdata->record_size;
 	}
-	of_node_put(parent_node);

So this change is functionally fine, but there's really no good reason to do this -- there is no fancy error handling here, so there's no benefit to making this change. It doesn't really help readability.

The benefit is general consistency across the code base. If we could just get rid of all local-scoped of_node_puts, then we could more easily check that device nodes are safely used, without having to study the rest of the code.

Just my opinion. You decide.

julia