Skip to contentSkip to conversation

char: pcmcia: scr24x_cs: Fix failure handling of device_create()

PATCH In review [email protected] 2 participants 2 messages May 27, 2021 – May 28, 2021 Char and misc drivers

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 char: pcmcia: scr24x_cs: Fix failure handling of device_create()

From: Nijam Haider <[email protected]>

Ignored error in device_create() and pcmcia_enable_device() this patch implements proper error handling.

---
V3 -> V4: Added label and moved the cleanup code
V2 -> V3: Added description, Changelog and removed whitespace error V1 -> V2: Split the patch into two parts and addressed review comments

---
 drivers/char/pcmcia/scr24x_cs.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
drivers/char/pcmcia/scr24x_cs.c +13 -6
index 47feb39af34c..ba84b4dd13d3 100644
@@ -233,6 +233,7 @@ static int scr24x_probe(struct pcmcia_device *link)
 {
 	struct scr24x_dev *dev;
 	int ret;
+	struct device *dev_ret;
 
 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
 	if (!dev)
@@ -271,22 +272,28 @@ static int scr24x_probe(struct pcmcia_device *link)
 		goto err;
 
 	ret = pcmcia_enable_device(link);
-	if (ret < 0) {
-		pcmcia_disable_device(link);
-		goto err;
-	}
+	if (ret < 0)
+		goto err_device;
 
-	device_create(scr24x_class, NULL, MKDEV(MAJOR(scr24x_devt), dev->devno),
+	dev_ret = device_create(scr24x_class, NULL, MKDEV(MAJOR(scr24x_devt), dev->devno),
 		      NULL, "scr24x%d", dev->devno);
+	if (IS_ERR(dev_ret)) {
+		dev_err(&link->dev, "device_create failed for %d\n",
+			dev->devno);
+		goto err_device;
+	}
 
 	dev_info(&link->dev, "SCR24x Chip Card Interface\n");
 	return 0;
 
+err_device:
+	pcmcia_disable_device(link);
+	cdev_del(&dev->c_dev);
 err:
 	if (dev->devno < SCR24X_DEVS)
 		clear_bit(dev->devno, scr24x_minors);
 	kfree (dev);
-	return ret;
+	return -ENODEV;
 }
 
 static void scr24x_remove(struct pcmcia_device *link)
Signature · 1 line
2.17.1
Acked-by on v4 1/1
#1 lore raw

Hi,

much better now, thank you.

There's one small remark below.

Quoted 48 lines · On Fri, May 28, 2021 at 01:04:29AM +0530, [email protected] wrote:

From: Nijam Haider <[email protected]>

Ignored error in device_create() and pcmcia_enable_device() this patch implements proper error handling.

---
V3 -> V4: Added label and moved the cleanup code
V2 -> V3: Added description, Changelog and removed whitespace error V1 -> V2: Split the patch into two parts and addressed review comments

---
 drivers/char/pcmcia/scr24x_cs.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
drivers/char/pcmcia/scr24x_cs.c +9 -5
index 47feb39af34c..ba84b4dd13d3 100644
@@ -233,6 +233,7 @@ static int scr24x_probe(struct pcmcia_device *link)
 {
 	struct scr24x_dev *dev;
 	int ret;
+	struct device *dev_ret;
 
 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
 	if (!dev)
@@ -271,22 +272,28 @@ static int scr24x_probe(struct pcmcia_device *link)
 		goto err;
 
 	ret = pcmcia_enable_device(link);
-	if (ret < 0) {
-		pcmcia_disable_device(link);
-		goto err;
-	}
+	if (ret < 0)
+		goto err_device;
 
-	device_create(scr24x_class, NULL, MKDEV(MAJOR(scr24x_devt), dev->devno),
+	dev_ret = device_create(scr24x_class, NULL, MKDEV(MAJOR(scr24x_devt), dev->devno),
 		      NULL, "scr24x%d", dev->devno);
+	if (IS_ERR(dev_ret)) {
+		dev_err(&link->dev, "device_create failed for %d\n",
+			dev->devno);
+		goto err_device;
+	}
 
 	dev_info(&link->dev, "SCR24x Chip Card Interface\n");
 	return 0;

drivers/char/pcmcia/scr24x_cs.c static int scr24x_probe(struct pcmcia_device *link) · line 296

Quoted 10 lines
drivers/char/pcmcia/scr24x_cs.c +4 -1
 
+err_device:
+	pcmcia_disable_device(link);
+	cdev_del(&dev->c_dev);
 err:
 	if (dev->devno < SCR24X_DEVS)
 		clear_bit(dev->devno, scr24x_minors);
 	kfree (dev);
-	return ret;
+	return -ENODEV;

Why -ENODEV? Couldn't we propagate the original error with something like "ret = PTR_ERR(dev_ret);" just before the "goto err_device"?

Thank you,
Lubo

Quoted 6 lines
drivers/char/pcmcia/scr24x_cs.c +0 -0
 }
 
 static void scr24x_remove(struct pcmcia_device *link)
Signature · 1 line
2.17.1