[PATCH 1/2] ata: pata_parport: pin the protocol module before device_register()
From: Pei Xiao
Date: Wed Sep 02 2026 - 08:13:00 EST
pi_init_one() calls device_register() before try_module_get(). Between
these two calls the device is already visible but the module is not
pinned yet, so an unload in this window leaves pi->proto dangling:
pi_init_one() rmmod -f <proto>
--------------------------------------------------------
device_register(&pi->dev)
device visible on the bus
module memory freed
pi->proto = pr <- writes into freed memory / dangles
try_module_get(...) <- too late, module already gone
Take the module reference before registering the device, and drop it
on the device_register() failure path.
Fixes: 246a1c4c6b7f ("ata: pata_parport: add driver (PARIDE replacement)")
Signed-off-by: Pei Xiao <xiaopei01@xxxxxxxxxx>
---
drivers/ata/pata_parport/pata_parport.c | 13 +++++++++----
1 file changed, 9 insertions(+), 4 deletions(-)
diff --git a/drivers/ata/pata_parport/pata_parport.c b/drivers/ata/pata_parport/pata_parport.c
index cf81a6128f55..7462f9b1acc5 100644
--- a/drivers/ata/pata_parport/pata_parport.c
+++ b/drivers/ata/pata_parport/pata_parport.c
@@ -509,6 +509,14 @@ static struct pi_adapter *pi_init_one(struct parport *parport,
return NULL;
}
+ pi->proto = pr;
+
+ if (!try_module_get(pi->proto->owner)) {
+ kfree(pi);
+ ida_free(&pata_parport_bus_dev_ids, id);
+ return NULL;
+ }
+
/* set up pi->dev before pi_probe_unit() so it can use dev_printk() */
pi->dev.parent = pata_parport_bus;
pi->dev.bus = &pata_parport_bus_type;
@@ -517,15 +525,12 @@ static struct pi_adapter *pi_init_one(struct parport *parport,
pi->dev.id = id;
dev_set_name(&pi->dev, "pata_parport.%u", pi->dev.id);
if (device_register(&pi->dev)) {
+ module_put(pi->proto->owner);
put_device(&pi->dev);
/* pata_parport_dev_release will do ida_free(dev->id) and kfree(pi) */
return NULL;
}
- pi->proto = pr;
-
- if (!try_module_get(pi->proto->owner))
- goto out_unreg_dev;
if (pi->proto->init_proto && pi->proto->init_proto(pi) < 0)
goto out_module_put;
--
2.25.1