[PATCH] ata: pata_parport: Fix missing module refcount for pata_parport itself

Pei Xiao <[email protected]> Thu, 30 Jul 2026 16:13:22 +0800
Newsgroups gmane.linux.kernel,gmane.linux.ide
Message-ID <a8d9807618ab786313654099c904d613075e1b63.1785398816.git.xiaopei01@kylinos.cn>
The pata_parport core module can be unloaded while devices are still
attached, because there is no module reference taken for the core
module itself -- only for the protocol driver.

Add a __module_get(THIS_MODULE) in the success path of pi_init_one()
and the corresponding module_put(THIS_MODULE) in
pata_parport_dev_release(), so that rmmod pata_parport is prevented
when devices are present.

In the dev_release callback, the pi->proto guard ensures that
module_put() is skipped for the early error case where
device_register() fails before __module_get() was called.

Fixes: 246a1c4c6b7f ("ata: pata_parport: add driver (PARIDE replacement)")
Signed-off-by: Pei Xiao <[email protected]>
---
 drivers/ata/pata_parport/pata_parport.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/drivers/ata/pata_parport/pata_parport.c b/drivers/ata/pata_parport/pata_parport.c
index cf81a6128f55..b73b023b0443 100644
--- a/drivers/ata/pata_parport/pata_parport.c
+++ b/drivers/ata/pata_parport/pata_parport.c
@@ -455,6 +455,12 @@ static void pata_parport_dev_release(struct device *dev)
 {
 	struct pi_adapter *pi = container_of(dev, struct pi_adapter, dev);
 
+	/*
+	 * pi->proto is NULL when device_register() fails early,
+	 * before __module_get() was called -- skip module_put().
+	 */
+	if (pi->proto)
+		module_put(THIS_MODULE);
 	ida_free(&pata_parport_bus_dev_ids, dev->id);
 	kfree(pi);
 }
@@ -524,6 +530,8 @@ static struct pi_adapter *pi_init_one(struct parport *parport,
 
 	pi->proto = pr;
 
+	__module_get(THIS_MODULE);
+
 	if (!try_module_get(pi->proto->owner))
 		goto out_unreg_dev;
 	if (pi->proto->init_proto && pi->proto->init_proto(pi) < 0)
-- 
2.25.1