Bug#1130365: [PATCH] parport: Fix race between port and client registration

Ben Hutchings <[email protected]> Tue, 5 May 2026 20:45:12 +0200
Newsgroups gmane.linux.debian.devel.bugs.general,gmane.linux.kernel.stable,gmane.linux.parport
Message-ID <[email protected]>
--oFaP8Rm7b43qwgz2
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

The parport subsystem registers port devices before they are fully
initialised, resulting in a race condition where client drivers such
as lp can attach to ports that are not completely initialised or even
being torn down.

When the port and client drivers are built as modules and loaded
around the same time during boot, this occasionally results in a
crash.  I was able to make this happen reliably in a VM with a
PC-style parallel port by patching parport_pc to fail probing:

> --- a/drivers/parport/parport_pc.c
> +++ b/drivers/parport/parport_pc.c
> @@ -2069,7 +2069,7 @@ static struct parport *__parport_pc_probe_port(unsi=
gned long int base,
>  	if (!p)
>  		goto out3;
>
> -	base_res =3D request_region(base, 3, p->name);
> +	base_res =3D NULL;
>  	if (!base_res)
>  		goto out4;
>

and then running:

    while true; do
        modprobe lp & modprobe parport_pc
	wait
	rmmod lp parport_pc
    done

for a few seconds.

In the long term I think port registration should be changed to put
the call to device_add() inside parport_announce_port(), but since the
latter currently cannot fail this will require changing all port
drivers.

For now, add a flag to indicate whether a port has been "announced"
and only try to attach client drivers to ports when the flag is set.

Fixes: 6fa45a226897 ("parport: add device-model to parport subsystem")
Closes: https://bugs.debian.org/1130365
Closes: https://lore.kernel.org/all/6ba903ad-9897-42bb-8c2d-337385cc3746@mo=
lgen.mpg.de/
Cc: [email protected]
Signed-off-by: Ben Hutchings <[email protected]>
---
 drivers/parport/share.c | 11 +++++++++--
 include/linux/parport.h |  1 +
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/drivers/parport/share.c b/drivers/parport/share.c
index ba5292828703..eb0977ca1605 100644
--- a/drivers/parport/share.c
+++ b/drivers/parport/share.c
@@ -214,10 +214,14 @@ static void get_lowlevel_driver(void)
 static int port_check(struct device *dev, void *dev_drv)
 {
 	struct parport_driver *drv =3D dev_drv;
+	struct parport *port;
=20
 	/* only send ports, do not send other devices connected to bus */
-	if (is_parport(dev))
-		drv->match_port(to_parport_dev(dev));
+	if (is_parport(dev)) {
+		port =3D to_parport_dev(dev);
+		if (test_bit(PARPORT_ANNOUNCED, &port->devflags))
+			drv->match_port(port);
+	}
 	return 0;
 }
=20
@@ -532,6 +536,7 @@ void parport_announce_port(struct parport *port)
 		if (slave)
 			attach_driver_chain(slave);
 	}
+	set_bit(PARPORT_ANNOUNCED, &port->devflags);
 	mutex_unlock(&registration_lock);
 }
 EXPORT_SYMBOL(parport_announce_port);
@@ -561,6 +566,8 @@ void parport_remove_port(struct parport *port)
=20
 	mutex_lock(&registration_lock);
=20
+	clear_bit(PARPORT_ANNOUNCED, &port->devflags);
+
 	/* Spread the word. */
 	detach_driver_chain(port);
=20
diff --git a/include/linux/parport.h b/include/linux/parport.h
index 464c2ad28039..f64cb0676e3b 100644
--- a/include/linux/parport.h
+++ b/include/linux/parport.h
@@ -240,6 +240,7 @@ struct parport {
=20
 	unsigned long devflags;
 #define PARPORT_DEVPROC_REGISTERED	0
+#define PARPORT_ANNOUNCED		1
 	struct pardevice *proc_device;	/* Currently register proc device */
=20
 	struct list_head full_list;

--oFaP8Rm7b43qwgz2
Content-Type: application/pgp-signature; name="signature.asc"

-----BEGIN PGP SIGNATURE-----

iQIzBAABCgAdFiEErCspvTSmr92z9o8157/I7JWGEQkFAmn6OrMACgkQ57/I7JWG
EQniyg/8DI/t3+V54LsZso7KV7VFI2qO5/U9uutYBkJYOMhj5wpB+kR/71ohenmk
EjhgWRpR3GIbJIN0s05byQ/OC4x8h32erkE1KUx0exOYu8KT908R1u0fBgHqCBvv
FV7dEwI+K3UxGO5vXy/9A6XrunutzCDX4q0sFi4KmZQEYKDjnT4JPzxu2JGaBu8r
/kKrBpbf2LeN82p+gD+gPrn2oYNxlCnQQcR00TiUQPI/JxjLWXPLqtWKF9tHjGRh
aPCCqb8Is+lsnDciVrqZPJiZ/EU+bKOmZdyK2G1SNXkrljbmOFbHAisLLu3dKHe2
WuyCJxCosscX5S9+0bsrb93MASHJl6DuvD0aiqNXYe+BnKSHy7FvomqvG/ip+txQ
4rEc0chyTFchuNgtHNVPsury3fQPsKI6t+T36uk5GXscyuG5etwwKn+beFokRUgm
WFwkhzRPyaSHBrAsu/0Uo63ROyMssVYpxd+cMpe+GS9f4jgZ2rbp2X8CHokJy5mV
jMtiKzTUvAbkfKEog5U8qH/1SImtzJaGlIbfZwvKjyLnntlBGk8Keg70isj5wR2G
Dy6xutqLb5GteAXzc1jCawn/DJnxFfexzF1aiU0i0nRWPf2j6LV5j5LL3lZVTL1k
TQn1ZK96QyYb0pz0njlSEr84AyWUZcbg7mCMqi64/5rwcSf1Bts=
=0DIu
-----END PGP SIGNATURE-----

--oFaP8Rm7b43qwgz2--