Skip to contentSkip to conversation

extcon: driver model release call not needed

PATCH Applied anish kumar 2 participants 3 messages Oct 21, 2012 – Oct 22, 2012 External connector subsystem (extcon)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 extcon: driver model release call not needed

From: anish kumar <[email protected]>

There was a case where free and list_del can be called twice on the same pointer.So fixed it by re-arranging the code and removing a function which was not needed.

---
 drivers/extcon/extcon-class.c |   71 ++++++++++++++++++-----------------------
 1 files changed, 31 insertions(+), 40 deletions(-)
drivers/extcon/extcon-class.c +31 -40
index 754f133..347db10 100644
@@ -544,47 +544,10 @@ static int create_extcon_class(void)
 	return 0;
 }
 
-static void extcon_cleanup(struct extcon_dev *edev, bool skip)
-{
-	mutex_lock(&extcon_dev_list_lock);
-	list_del(&edev->entry);
-	mutex_unlock(&extcon_dev_list_lock);
-
-	if (!skip && get_device(edev->dev)) {
-		int index;
-
-		if (edev->mutually_exclusive && edev->max_supported) {
-			for (index = 0; edev->mutually_exclusive[index];
-			     index++)
-				kfree(edev->d_attrs_muex[index].attr.name);
-			kfree(edev->d_attrs_muex);
-			kfree(edev->attrs_muex);
-		}
-
-		for (index = 0; index < edev->max_supported; index++)
-			kfree(edev->cables[index].attr_g.name);
-
-		if (edev->max_supported) {
-			kfree(edev->extcon_dev_type.groups);
-			kfree(edev->cables);
-		}
-
-#if defined(CONFIG_ANDROID)
-		if (switch_class)
-			class_compat_remove_link(switch_class, edev->dev, NULL);
-#endif
-		device_unregister(edev->dev);
-		put_device(edev->dev);
-	}
-
-	kfree(edev->dev);
-}
-
 static void extcon_dev_release(struct device *dev)
 {
-	struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
-
-	extcon_cleanup(edev, true);
+	struct extcon_dev *edev = dev_get_drvdata(dev);
+	kfree(edev->dev);
 }
 
 static const char *muex_name = "mutually_exclusive";
@@ -810,7 +773,35 @@ EXPORT_SYMBOL_GPL(extcon_dev_register);
  */
 void extcon_dev_unregister(struct extcon_dev *edev)
 {
-	extcon_cleanup(edev, false);
+	mutex_lock(&extcon_dev_list_lock);
+	list_del(&edev->entry);
+	mutex_unlock(&extcon_dev_list_lock);
+
+	if (get_device(edev->dev) != NULL) {
+		int index;
+		if (edev->mutually_exclusive && edev->max_supported) {
+			for (index = 0; edev->mutually_exclusive[index];
+					index++)
+				kfree(edev->d_attrs_muex[index].attr.name);
+			kfree(edev->d_attrs_muex);
+			kfree(edev->attrs_muex);
+		}
+
+		for (index = 0; index < edev->max_supported; index++)
+			kfree(edev->cables[index].attr_g.name);
+
+		if (edev->max_supported) {
+			kfree(edev->extcon_dev_type.groups);
+			kfree(edev->cables);
+		}
+
+#if defined(CONFIG_ANDROID)
+		if (switch_class)
+			class_compat_remove_link(switch_class, edev->dev, NULL);
+#endif
+		device_unregister(edev->dev);
+		put_device(edev->dev);
+	}
 }
 EXPORT_SYMBOL_GPL(extcon_dev_unregister);
 
Signature · 1 line
1.7.1
#1 lore raw

Hi Choi,

This is based on http://git.infradead.org/users/kmpark/linux-samsung
this branch.
anish

Quoted 102 lines · anish kumar

From: anish kumar <[email protected]>

There was a case where free and list_del can be called twice on the same pointer.So fixed it by re-arranging the code and removing a function which was not needed.

---
 drivers/extcon/extcon-class.c |   71 ++++++++++++++++++-----------------------
 1 files changed, 31 insertions(+), 40 deletions(-)
drivers/extcon/extcon-class.c +31 -40
index 754f133..347db10 100644
@@ -544,47 +544,10 @@ static int create_extcon_class(void)
 	return 0;
 }
 
-static void extcon_cleanup(struct extcon_dev *edev, bool skip)
-{
-	mutex_lock(&extcon_dev_list_lock);
-	list_del(&edev->entry);
-	mutex_unlock(&extcon_dev_list_lock);
-
-	if (!skip && get_device(edev->dev)) {
-		int index;
-
-		if (edev->mutually_exclusive && edev->max_supported) {
-			for (index = 0; edev->mutually_exclusive[index];
-			     index++)
-				kfree(edev->d_attrs_muex[index].attr.name);
-			kfree(edev->d_attrs_muex);
-			kfree(edev->attrs_muex);
-		}
-
-		for (index = 0; index < edev->max_supported; index++)
-			kfree(edev->cables[index].attr_g.name);
-
-		if (edev->max_supported) {
-			kfree(edev->extcon_dev_type.groups);
-			kfree(edev->cables);
-		}
-
-#if defined(CONFIG_ANDROID)
-		if (switch_class)
-			class_compat_remove_link(switch_class, edev->dev, NULL);
-#endif
-		device_unregister(edev->dev);
-		put_device(edev->dev);
-	}
-
-	kfree(edev->dev);
-}
-
 static void extcon_dev_release(struct device *dev)
 {
-	struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
-
-	extcon_cleanup(edev, true);
+	struct extcon_dev *edev = dev_get_drvdata(dev);
+	kfree(edev->dev);
 }
 
 static const char *muex_name = "mutually_exclusive";
@@ -810,7 +773,35 @@ EXPORT_SYMBOL_GPL(extcon_dev_register);
  */
 void extcon_dev_unregister(struct extcon_dev *edev)
 {
-	extcon_cleanup(edev, false);
+	mutex_lock(&extcon_dev_list_lock);
+	list_del(&edev->entry);
+	mutex_unlock(&extcon_dev_list_lock);
+
+	if (get_device(edev->dev) != NULL) {
+		int index;
+		if (edev->mutually_exclusive && edev->max_supported) {
+			for (index = 0; edev->mutually_exclusive[index];
+					index++)
+				kfree(edev->d_attrs_muex[index].attr.name);
+			kfree(edev->d_attrs_muex);
+			kfree(edev->attrs_muex);
+		}
+
+		for (index = 0; index < edev->max_supported; index++)
+			kfree(edev->cables[index].attr_g.name);
+
+		if (edev->max_supported) {
+			kfree(edev->extcon_dev_type.groups);
+			kfree(edev->cables);
+		}
+
+#if defined(CONFIG_ANDROID)
+		if (switch_class)
+			class_compat_remove_link(switch_class, edev->dev, NULL);
+#endif
+		device_unregister(edev->dev);
+		put_device(edev->dev);
+	}
 }
 EXPORT_SYMBOL_GPL(extcon_dev_unregister);
 
maintainer Applied
#2 lore raw
In reply to anish kumar
Quoted 11 lines · anish kumar

From: anish kumar <[email protected]>

There was a case where free and list_del can be called twice on the same pointer.So fixed it by re-arranging the code and removing a function which was not needed.

---
 drivers/extcon/extcon-class.c |   71 ++++++++++++++++++-----------------------
 1 files changed, 31 insertions(+), 40 deletions(-)

Applied, thanks,

But, There were some minor issue, so I fix and applied it.

[...]

drivers/extcon/extcon-class.c static int create_extcon_class(void) · line 550

Quoted 7 lines

static void extcon_dev_release(struct device *dev) {

drivers/extcon/extcon-class.c +2 -3
-	struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
-
-	extcon_cleanup(edev, true);
+	struct extcon_dev *edev = dev_get_drvdata(dev);
+	kfree(edev->dev);

It is not necessary to get extcon_dev instance through dev_get_drvdata() and only call kfree(dev) by using extcon_dev_release()'s parameter.

	kfree(dev);

drivers/extcon/extcon-class.c EXPORT_SYMBOL_GPL(extcon_dev_register); · line 780

Quoted 13 lines

}

static const char *muex_name = "mutually_exclusive";

drivers/extcon/extcon-class.c +5 -1
@@ -810,7 +773,35 @@ EXPORT_SYMBOL_GPL(extcon_dev_register);
  */
 void extcon_dev_unregister(struct extcon_dev *edev)
 {
-	extcon_cleanup(edev, false);
+	mutex_lock(&extcon_dev_list_lock);
+	list_del(&edev->entry);
+	mutex_unlock(&extcon_dev_list_lock);
+
+	if (get_device(edev->dev) != NULL) {

I prefer minimal indentation, so I will modify as below code.

	int index;

	mutex_lock(&extcon_dev_list_lock);
	list_del(&edev->entry);
	mutex_unlock(&extcon_dev_list_lock);

	if (IS_ERR_OR_NULL(get_device(edev->dev))) {
		dev_err(edev->dev, "Failed to unregister extcon_dev (%s)\n",
				dev_name(edev->dev));
		return;
	}

	if (edev->mutually_exclusive && edev->max_supported) {
		for (index = 0; edev->mutually_exclusive[index];
				index++)
			kfree(edev->d_attrs_muex[index].attr.name);
		kfree(edev->d_attrs_muex);
		kfree(edev->attrs_muex);
	}

[...]

Cheers,
Chanwoo Choi