Skip to contentSkip to conversation

net: dsa: Use devm_ prefixed allocations

PATCH Superseded Neil Armstrong 3 participants 7 messages Oct 2, 2015 – Oct 3, 2015

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 net: dsa: Use devm_ prefixed allocations

To simplify and prevent memory leakage when unbinding, use the devm_ memory allocation calls.

---
 net/dsa/dsa.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
net/dsa/dsa.c +3 -3
index c59fa5d..98f94c2 100644
@@ -305,7 +305,7 @@ static int dsa_switch_setup_one(struct dsa_switch *ds, struct device *parent)
 	if (ret < 0)
 		goto out;
 
-	ds->slave_mii_bus = mdiobus_alloc();
+	ds->slave_mii_bus = devm_mdiobus_alloc(parent);
 	if (ds->slave_mii_bus == NULL) {
 		ret = -ENOMEM;
 		goto out;
@@ -400,7 +400,7 @@ dsa_switch_setup(struct dsa_switch_tree *dst, int index,
 	/*
 	 * Allocate and initialise switch state.
 	 */
-	ds = kzalloc(sizeof(*ds) + drv->priv_size, GFP_KERNEL);
+	ds = devm_kzalloc(parent, sizeof(*ds) + drv->priv_size, GFP_KERNEL);
 	if (ds == NULL)
 		return ERR_PTR(-ENOMEM);
 
@@ -883,7 +883,7 @@ static int dsa_probe(struct platform_device *pdev)
 		goto out;
 	}
 
-	dst = kzalloc(sizeof(*dst), GFP_KERNEL);
+	dst = devm_kzalloc(&pdev->dev, sizeof(*dst), GFP_KERNEL);
 	if (dst == NULL) {
 		dev_put(dev);
 		ret = -ENOMEM;
Signature · 1 line
1.9.1
#2 lore raw
In reply to Neil Armstrong
Quoted 34 lines · Neil Armstrong

To simplify and prevent memory leakage when unbinding, use the devm_ memory allocation calls.

---
  net/dsa/dsa.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
net/dsa/dsa.c +2 -2
index c59fa5d..98f94c2 100644
@@ -305,7 +305,7 @@ static int dsa_switch_setup_one(struct dsa_switch *ds, struct device *parent)
  	if (ret < 0)
  		goto out;
 
-	ds->slave_mii_bus = mdiobus_alloc();
+	ds->slave_mii_bus = devm_mdiobus_alloc(parent);
  	if (ds->slave_mii_bus == NULL) {
  		ret = -ENOMEM;
  		goto out;
@@ -400,7 +400,7 @@ dsa_switch_setup(struct dsa_switch_tree *dst, int index,
  	/*
  	 * Allocate and initialise switch state.
  	 */
-	ds = kzalloc(sizeof(*ds) + drv->priv_size, GFP_KERNEL);
+	ds = devm_kzalloc(parent, sizeof(*ds) + drv->priv_size, GFP_KERNEL);
  	if (ds == NULL)
  		return ERR_PTR(-ENOMEM);
 

net/dsa/dsa.c static int dsa_probe(struct platform_device *pdev) · line 889

Quoted 8 lines
net/dsa/dsa.c +1 -1
@@ -883,7 +883,7 @@ static int dsa_probe(struct platform_device *pdev)
  		goto out;
  	}
 
-	dst = kzalloc(sizeof(*dst), GFP_KERNEL);
+	dst = devm_kzalloc(&pdev->dev, sizeof(*dst), GFP_KERNEL);
  	if (dst == NULL) {
  		dev_put(dev);
  		ret = -ENOMEM;

Shouldn't you remove the correspoding kfree(), etc. calls?

MBR, Sergei

#3 lore raw
Quoted 47 lines · Sergei Shtylyov
Quoted 34 lines · Neil Armstrong

To simplify and prevent memory leakage when unbinding, use the devm_ memory allocation calls.

---
  net/dsa/dsa.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
net/dsa/dsa.c +2 -2
index c59fa5d..98f94c2 100644
@@ -305,7 +305,7 @@ static int dsa_switch_setup_one(struct dsa_switch *ds, struct device *parent)
      if (ret < 0)
          goto out;
 
-    ds->slave_mii_bus = mdiobus_alloc();
+    ds->slave_mii_bus = devm_mdiobus_alloc(parent);
      if (ds->slave_mii_bus == NULL) {
          ret = -ENOMEM;
          goto out;
@@ -400,7 +400,7 @@ dsa_switch_setup(struct dsa_switch_tree *dst, int index,
      /*
       * Allocate and initialise switch state.
       */
-    ds = kzalloc(sizeof(*ds) + drv->priv_size, GFP_KERNEL);
+    ds = devm_kzalloc(parent, sizeof(*ds) + drv->priv_size, GFP_KERNEL);
      if (ds == NULL)
          return ERR_PTR(-ENOMEM);
 

net/dsa/dsa.c static int dsa_probe(struct platform_device *pdev) · line 889

Quoted 8 lines
net/dsa/dsa.c +1 -1
@@ -883,7 +883,7 @@ static int dsa_probe(struct platform_device *pdev)
          goto out;
      }
 
-    dst = kzalloc(sizeof(*dst), GFP_KERNEL);
+    dst = devm_kzalloc(&pdev->dev, sizeof(*dst), GFP_KERNEL);
      if (dst == NULL) {
          dev_put(dev);
          ret = -ENOMEM;

Shouldn't you remove the correspoding kfree(), etc. calls?

Quoted 2 lines

MBR, Sergei

The corresponding kfree() calls were all missing.

Neil

#4 lore raw
Quoted 45 lines · Neil Armstrong
Quoted 45 lines
Quoted 34 lines

To simplify and prevent memory leakage when unbinding, use the devm_ memory allocation calls.

---
   net/dsa/dsa.c | 6 +++---
   1 file changed, 3 insertions(+), 3 deletions(-)
net/dsa/dsa.c +2 -2
index c59fa5d..98f94c2 100644
@@ -305,7 +305,7 @@ static int dsa_switch_setup_one(struct dsa_switch *ds, struct device *parent)
       if (ret < 0)
           goto out;
 
-    ds->slave_mii_bus = mdiobus_alloc();
+    ds->slave_mii_bus = devm_mdiobus_alloc(parent);
       if (ds->slave_mii_bus == NULL) {
           ret = -ENOMEM;
           goto out;
@@ -400,7 +400,7 @@ dsa_switch_setup(struct dsa_switch_tree *dst, int index,
       /*
        * Allocate and initialise switch state.
        */
-    ds = kzalloc(sizeof(*ds) + drv->priv_size, GFP_KERNEL);
+    ds = devm_kzalloc(parent, sizeof(*ds) + drv->priv_size, GFP_KERNEL);
       if (ds == NULL)
           return ERR_PTR(-ENOMEM);
 

net/dsa/dsa.c static int dsa_probe(struct platform_device *pdev) · line 889

Quoted 8 lines
net/dsa/dsa.c +1 -1
@@ -883,7 +883,7 @@ static int dsa_probe(struct platform_device *pdev)
           goto out;
       }
 
-    dst = kzalloc(sizeof(*dst), GFP_KERNEL);
+    dst = devm_kzalloc(&pdev->dev, sizeof(*dst), GFP_KERNEL);
       if (dst == NULL) {
           dev_put(dev);
           ret = -ENOMEM;

Shouldn't you remove the correspoding kfree(), etc. calls?

Quoted 3 lines
Quoted 2 lines

MBR, Sergei

The corresponding kfree() calls were all missing.

Then this patch should be for net, not net-next. Either that, or add the
kfree() calls first, then remove them in this net-next patch.

Quoted 1 line

Neil

MBR, Sergei

#5 lore raw
In reply to Neil Armstrong
Quoted 48 lines · Neil Armstrong
Quoted 47 lines · Sergei Shtylyov
Quoted 34 lines · Neil Armstrong

To simplify and prevent memory leakage when unbinding, use the devm_ memory allocation calls.

---
  net/dsa/dsa.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
net/dsa/dsa.c +2 -2
index c59fa5d..98f94c2 100644
@@ -305,7 +305,7 @@ static int dsa_switch_setup_one(struct dsa_switch *ds, struct device *parent)
      if (ret < 0)
          goto out;
 
-    ds->slave_mii_bus = mdiobus_alloc();
+    ds->slave_mii_bus = devm_mdiobus_alloc(parent);
      if (ds->slave_mii_bus == NULL) {
          ret = -ENOMEM;
          goto out;
@@ -400,7 +400,7 @@ dsa_switch_setup(struct dsa_switch_tree *dst, int index,
      /*
       * Allocate and initialise switch state.
       */
-    ds = kzalloc(sizeof(*ds) + drv->priv_size, GFP_KERNEL);
+    ds = devm_kzalloc(parent, sizeof(*ds) + drv->priv_size, GFP_KERNEL);
      if (ds == NULL)
          return ERR_PTR(-ENOMEM);
 

net/dsa/dsa.c static int dsa_probe(struct platform_device *pdev) · line 889

Quoted 8 lines
net/dsa/dsa.c +1 -1
@@ -883,7 +883,7 @@ static int dsa_probe(struct platform_device *pdev)
          goto out;
      }
 
-    dst = kzalloc(sizeof(*dst), GFP_KERNEL);
+    dst = devm_kzalloc(&pdev->dev, sizeof(*dst), GFP_KERNEL);
      if (dst == NULL) {
          dev_put(dev);
          ret = -ENOMEM;

Shouldn't you remove the correspoding kfree(), etc. calls?

Quoted 3 lines
Quoted 2 lines

MBR, Sergei

The corresponding kfree() calls were all missing.

Not in the error handling path. mdiobus_alloc has a corresponding mdiobus_free in the same function.
The ds kzalloc in dsa_switch_setup has a kfree in dsa_switch_setup_one.

- Felix

#6 lore raw
Quoted 49 lines · Felix Fietkau
Quoted 48 lines · Neil Armstrong
Quoted 47 lines · Sergei Shtylyov
Quoted 34 lines · Neil Armstrong

To simplify and prevent memory leakage when unbinding, use the devm_ memory allocation calls.

---
  net/dsa/dsa.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
net/dsa/dsa.c +2 -2
index c59fa5d..98f94c2 100644
@@ -305,7 +305,7 @@ static int dsa_switch_setup_one(struct dsa_switch *ds, struct device *parent)
      if (ret < 0)
          goto out;
 
-    ds->slave_mii_bus = mdiobus_alloc();
+    ds->slave_mii_bus = devm_mdiobus_alloc(parent);
      if (ds->slave_mii_bus == NULL) {
          ret = -ENOMEM;
          goto out;
@@ -400,7 +400,7 @@ dsa_switch_setup(struct dsa_switch_tree *dst, int index,
      /*
       * Allocate and initialise switch state.
       */
-    ds = kzalloc(sizeof(*ds) + drv->priv_size, GFP_KERNEL);
+    ds = devm_kzalloc(parent, sizeof(*ds) + drv->priv_size, GFP_KERNEL);
      if (ds == NULL)
          return ERR_PTR(-ENOMEM);
 

net/dsa/dsa.c static int dsa_probe(struct platform_device *pdev) · line 889

Quoted 8 lines
net/dsa/dsa.c +1 -1
@@ -883,7 +883,7 @@ static int dsa_probe(struct platform_device *pdev)
          goto out;
      }
 
-    dst = kzalloc(sizeof(*dst), GFP_KERNEL);
+    dst = devm_kzalloc(&pdev->dev, sizeof(*dst), GFP_KERNEL);
      if (dst == NULL) {
          dev_put(dev);
          ret = -ENOMEM;

Shouldn't you remove the correspoding kfree(), etc. calls?

Quoted 9 lines
Quoted 3 lines
Quoted 2 lines

MBR, Sergei

The corresponding kfree() calls were all missing.

Not in the error handling path. mdiobus_alloc has a corresponding mdiobus_free in the same function.
The ds kzalloc in dsa_switch_setup has a kfree in dsa_switch_setup_one.

- Felix

Thanks Felix & Sergei,

I will repost based on net with an intermediate commit adding the missing kfree calls.

Neil