Skip to contentSkip to conversation

usb: roles: fix of node refcount leak in usb_role_switch_is_parent()

PATCH Superseded Yang Yingliang 3 participants 6 messages Nov 19, 2022 – Nov 21, 2022 USB

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 usb: roles: fix of node refcount leak in usb_role_switch_is_parent()

I got the following report while doing device(mt6370-tcpc) load test with CONFIG_OF_UNITTEST and CONFIG_OF_DYNAMIC enabled:

OF: ERROR: memory leak, expected refcount 1 instead of 2, of_node_get()/of_node_put() unbalanced - destroy cset entry:
attach overlay node /i2c/pmic@34

The 'parent' returned by fwnode_get_parent() with refcount incremented. it needs be put after using.

Fixes 6fadd72943b8 ("usb: roles: get usb-role-switch from parent")

---
v1 -> v2:
Add description to how is the report generated.

---
 drivers/usb/roles/class.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/usb/roles/class.c +5 -1
index dfaed7eee94f..289950e5fcfb 100644
@@ -106,10 +106,14 @@ usb_role_switch_is_parent(struct fwnode_handle *fwnode)
 	struct fwnode_handle *parent = fwnode_get_parent(fwnode);
 	struct device *dev;
 
-	if (!parent || !fwnode_property_present(parent, "usb-role-switch"))
+	if (!parent || !fwnode_property_present(parent, "usb-role-switch")) {
+		if (parent)
+			fwnode_handle_put(parent);
 		return NULL;
+	}
 
 	dev = class_find_device_by_fwnode(role_class, parent);
+	fwnode_handle_put(parent);
 	return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
 }
 
Signature · 1 line
2.25.1
#1 lore raw
Quoted 40 lines · Yang Yingliang

I got the following report while doing device(mt6370-tcpc) load test with CONFIG_OF_UNITTEST and CONFIG_OF_DYNAMIC enabled:

OF: ERROR: memory leak, expected refcount 1 instead of 2, of_node_get()/of_node_put() unbalanced - destroy cset entry:
attach overlay node /i2c/pmic@34

The 'parent' returned by fwnode_get_parent() with refcount incremented.
it needs be put after using.

Fixes 6fadd72943b8 ("usb: roles: get usb-role-switch from parent")

---
v1 -> v2:
Add description to how is the report generated.

---
 drivers/usb/roles/class.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/usb/roles/class.c +5 -1
index dfaed7eee94f..289950e5fcfb 100644
@@ -106,10 +106,14 @@ usb_role_switch_is_parent(struct fwnode_handle
 *fwnode)
 	struct fwnode_handle *parent = fwnode_get_parent(fwnode);
 	struct device *dev;
 
-	if (!parent || !fwnode_property_present(parent, "usb-role-
 switch"))
+	if (!parent || !fwnode_property_present(parent, "usb-role-
 switch")) {
+		if (parent)
+			fwnode_handle_put(parent);
 		return NULL;
+	}
 
 	dev = class_find_device_by_fwnode(role_class, parent);
+	fwnode_handle_put(parent);
Quoted 3 lines

return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
}

+ Heikki

#2 lore raw

Hi,

Quoted 30 lines · Chunfeng Yun (云春峰)
Quoted 29 lines · Yang Yingliang

I got the following report while doing device(mt6370-tcpc) load test with CONFIG_OF_UNITTEST and CONFIG_OF_DYNAMIC enabled:

OF: ERROR: memory leak, expected refcount 1 instead of 2, of_node_get()/of_node_put() unbalanced - destroy cset entry:
attach overlay node /i2c/pmic@34

The 'parent' returned by fwnode_get_parent() with refcount incremented.
it needs be put after using.

Fixes 6fadd72943b8 ("usb: roles: get usb-role-switch from parent")

---
v1 -> v2:
Add description to how is the report generated.

---
 drivers/usb/roles/class.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/usb/roles/class.c +0 -0
index dfaed7eee94f..289950e5fcfb 100644
@@ -106,10 +106,14 @@ usb_role_switch_is_parent(struct fwnode_handle
 *fwnode)
 	struct fwnode_handle *parent = fwnode_get_parent(fwnode);
 	struct device *dev;
 
Quoted 8 lines

drivers/usb/roles/class.c usb_role_switch_is_parent(struct fwnode_handle *fwnode) · line 113

Quoted 8 lines
drivers/usb/roles/class.c +4 -1
-	if (!parent || !fwnode_property_present(parent, "usb-role-
 switch"))
+	if (!parent || !fwnode_property_present(parent, "usb-role-
 switch")) {
+		if (parent)
+			fwnode_handle_put(parent);
 		return NULL;
+	}

fwnode API should be NULL safe, so perhaps like this - clean also the old condition while at it:

        if (!fwnode_property_present(parent, "usb-role-switch")) {
                fwnode_handle_put(parent);
                return NULL;
        }
Quoted 6 lines
Quoted 5 lines
drivers/usb/roles/class.c +1 -0
 	dev = class_find_device_by_fwnode(role_class, parent);
+	fwnode_handle_put(parent);
 	return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);

}

+ Heikki

Thanks Chunfeng!

cheers,

Signature · 1 line
heikki
#3 lore raw

Hi,

Quoted 44 lines · Heikki Krogerus

Hi,

Quoted 30 lines · Chunfeng Yun (云春峰)
Quoted 29 lines · Yang Yingliang

I got the following report while doing device(mt6370-tcpc) load test with CONFIG_OF_UNITTEST and CONFIG_OF_DYNAMIC enabled:

OF: ERROR: memory leak, expected refcount 1 instead of 2, of_node_get()/of_node_put() unbalanced - destroy cset entry:
attach overlay node /i2c/pmic@34

The 'parent' returned by fwnode_get_parent() with refcount incremented.
it needs be put after using.

Fixes 6fadd72943b8 ("usb: roles: get usb-role-switch from parent")

---
v1 -> v2:
Add description to how is the report generated.

---
  drivers/usb/roles/class.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
drivers/usb/roles/class.c +0 -0
index dfaed7eee94f..289950e5fcfb 100644
@@ -106,10 +106,14 @@ usb_role_switch_is_parent(struct fwnode_handle
 *fwnode)
  	struct fwnode_handle *parent = fwnode_get_parent(fwnode);
  	struct device *dev;
 
Quoted 8 lines

drivers/usb/roles/class.c usb_role_switch_is_parent(struct fwnode_handle *fwnode) · line 113

Quoted 8 lines
drivers/usb/roles/class.c +4 -1
-	if (!parent || !fwnode_property_present(parent, "usb-role-
 switch"))
+	if (!parent || !fwnode_property_present(parent, "usb-role-
 switch")) {
+		if (parent)
+			fwnode_handle_put(parent);
  		return NULL;
+	}

fwnode API should be NULL safe, so perhaps like this - clean also the old condition while at it:

Quoted 4 lines
         if (!fwnode_property_present(parent, "usb-role-switch")) {
                 fwnode_handle_put(parent);
                 return NULL;
         }

Thanks for your suggestion, I will send a v3 to change this.

Thanks,
Yang

Quoted 11 lines
Quoted 7 lines
Quoted 6 lines
drivers/usb/roles/class.c +1 -0
  	dev = class_find_device_by_fwnode(role_class, parent);
+	fwnode_handle_put(parent);
  	return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
  }

+ Heikki

Thanks Chunfeng!

cheers,

#4 lore raw
In reply to Yang Yingliang

Hi Yingliang,

Quoted 40 lines · Yang Yingliang

I got the following report while doing device(mt6370-tcpc) load test with CONFIG_OF_UNITTEST and CONFIG_OF_DYNAMIC enabled:

OF: ERROR: memory leak, expected refcount 1 instead of 2, of_node_get()/of_node_put() unbalanced - destroy cset entry:
attach overlay node /i2c/pmic@34

The 'parent' returned by fwnode_get_parent() with refcount incremented.
it needs be put after using.

Fixes 6fadd72943b8 ("usb: roles: get usb-role-switch from parent")

---
v1 -> v2:
Add description to how is the report generated.

---
 drivers/usb/roles/class.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/usb/roles/class.c +5 -1
index dfaed7eee94f..289950e5fcfb 100644
@@ -106,10 +106,14 @@ usb_role_switch_is_parent(struct fwnode_handle
 *fwnode)
 	struct fwnode_handle *parent = fwnode_get_parent(fwnode);
 	struct device *dev;
 
-	if (!parent || !fwnode_property_present(parent, "usb-role-
 switch"))
+	if (!parent || !fwnode_property_present(parent, "usb-role-
 switch")) {
+		if (parent)
+			fwnode_handle_put(parent);
 		return NULL;
+	}
 
 	dev = class_find_device_by_fwnode(role_class, parent);
+	fwnode_handle_put(parent);
Quoted 2 lines

return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
}

Please use get_maintainer script to find who you should sent to after generating patches

e.g.:
./scripts/get_maintainer.pl --no-rolestats 00* | gawk '{printf(" --cc=\"%s\" ", $0)} END{printf("\n")}'

Thanks

Quoted 1 line
#5 lore raw
Quoted 48 lines · Chunfeng Yun (云春峰)

Hi Yingliang,

Quoted 40 lines · Yang Yingliang

I got the following report while doing device(mt6370-tcpc) load test with CONFIG_OF_UNITTEST and CONFIG_OF_DYNAMIC enabled:

OF: ERROR: memory leak, expected refcount 1 instead of 2, of_node_get()/of_node_put() unbalanced - destroy cset entry:
attach overlay node /i2c/pmic@34

The 'parent' returned by fwnode_get_parent() with refcount incremented.
it needs be put after using.

Fixes 6fadd72943b8 ("usb: roles: get usb-role-switch from parent")

---
v1 -> v2:
Add description to how is the report generated.

---
  drivers/usb/roles/class.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
drivers/usb/roles/class.c +5 -1
index dfaed7eee94f..289950e5fcfb 100644
@@ -106,10 +106,14 @@ usb_role_switch_is_parent(struct fwnode_handle
 *fwnode)
  	struct fwnode_handle *parent = fwnode_get_parent(fwnode);
  	struct device *dev;
 
-	if (!parent || !fwnode_property_present(parent, "usb-role-
 switch"))
+	if (!parent || !fwnode_property_present(parent, "usb-role-
 switch")) {
+		if (parent)
+			fwnode_handle_put(parent);
  		return NULL;
+	}
 
  	dev = class_find_device_by_fwnode(role_class, parent);
+	fwnode_handle_put(parent);
Quoted 2 lines
  	return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
  }

Please use get_maintainer script to find who you should sent to after generating patches

Quoted 5 lines

e.g.:
./scripts/get_maintainer.pl --no-rolestats 00* | gawk '{printf(" --cc=\"%s\" ", $0)} END{printf("\n")}'

Thanks

I used get_maintainer.pl to get the maintainer in Linux 6.1-rc5 and sent the patch.

./scripts/get_maintainer.pl 0001-usb-roles-fix-of-node-refcount-leak-in-usb_role_swit.patch Greg Kroah-Hartman <[email protected]> (supporter:USB SUBSYSTEM,blamed_fixes:1/1=100%) Chunfeng Yun <[email protected]> (blamed_fixes:1/1=100%) [email protected] (open list:USB SUBSYSTEM)
[email protected] (open list)

Thanks,
Yang

Quoted 2 lines
Quoted 2 lines