Skip to contentSkip to conversation

PNP: Fix card device cleanup on registration failure

PATCH Applied Yuho Choi 2 participants 2 messages Jul 3 – Jul 6

Subsystems (2)
lore
Share & tools
lore

Yuho Choi

author
#0 lore raw

PATCH 1/1 PNP: Fix card device cleanup on registration failure

pnp_add_card() ignores __pnp_add_device() failures. If device_register() fails there, the device is removed from the global and protocol lists, but remains on the card list and its device reference is not dropped.

Remove the failed device from the card list and call put_device() before continuing with the remaining card devices.

---
 drivers/pnp/card.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/pnp/card.c +9 -2
index 87f5af454751..df5e3d3cbf6c 100644
@@ -254,9 +254,16 @@ int pnp_add_card(struct pnp_card *card)
 	/* we wait until now to add devices in order to ensure the drivers
 	 * will be able to use all of the related devices on the card
 	 * without waiting an unreasonable length of time */
-	list_for_each(pos, &card->devices) {
+	list_for_each_safe(pos, temp, &card->devices) {
 		struct pnp_dev *dev = card_to_pnp_dev(pos);
-		__pnp_add_device(dev);
+		error = __pnp_add_device(dev);
+		if (error) {
+			mutex_lock(&pnp_lock);
+			list_del(&dev->card_list);
+			dev->card = NULL;
+			mutex_unlock(&pnp_lock);
+			put_device(&dev->dev);
+		}
 	}
 
 	/* match with card drivers */
Signature · 1 line
2.43.0
maintainer Applied
#1 lore raw
Quoted 27 lines · Yuho Choi

pnp_add_card() ignores __pnp_add_device() failures. If device_register() fails there, the device is removed from the global and protocol lists, but remains on the card list and its device reference is not dropped.

Remove the failed device from the card list and call put_device() before continuing with the remaining card devices.

---
 drivers/pnp/card.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/pnp/card.c +3 -2
index 87f5af454751..df5e3d3cbf6c 100644
@@ -254,9 +254,16 @@ int pnp_add_card(struct pnp_card *card)
        /* we wait until now to add devices in order to ensure the drivers
         * will be able to use all of the related devices on the card
         * without waiting an unreasonable length of time */
-       list_for_each(pos, &card->devices) {
+       list_for_each_safe(pos, temp, &card->devices) {
                struct pnp_dev *dev = card_to_pnp_dev(pos);
-               __pnp_add_device(dev);
+               error = __pnp_add_device(dev);
+               if (error) {

drivers/pnp/card.c int pnp_add_card(struct pnp_card *card)

Quoted 10 lines
drivers/pnp/card.c +6 -1
+                       mutex_lock(&pnp_lock);
+                       list_del(&dev->card_list);
+                       dev->card = NULL;
+                       mutex_unlock(&pnp_lock);
+                       put_device(&dev->dev);
+               }
        }
 
        /* match with card drivers */
--

Applied as 7.3 material, thanks!