Skip to contentSkip to conversation

vfio/ap/ccw/samples: Fix device_register() unwind path

PATCH In review Alex Williamson 4 participants 5 messages Dec 2, 2022 – Dec 6, 2022

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vfio/ap/ccw/samples: Fix device_register() unwind path

We always need to call put_device() if device_register() fails. All vfio drivers calling device_register() include a similar unwind stack via gotos, therefore split device_unregister() into its device_del() and put_device() components in the unwind path, and add a goto target to handle only the put_device() requirement.

Fixes d61fc96f47fd ("sample: vfio mdev display - host device")
Fixes 9d1a546c53b4 ("docs: Sample driver to demonstrate how to use Mediated device framework.")
Fixes a5e6e6505f38 ("sample: vfio bochs vbe display (host device for bochs-drm)")
Fixes 9e6f07cd1eaa ("vfio/ccw: create a parent struct")
Fixes 36360658eb5a ("s390: vfio_ap: link the vfio_ap devices to the vfio_ap bus subsystem")
Cc Tony Krowiak <[email protected]>
Cc Halil Pasic <[email protected]>
Cc Jason Herne <[email protected]>
Cc Kirti Wankhede <[email protected]>

---

I didn't intend to usurp Ruan's patch, but since the inline version is collecting reviews, formally post it and include additional fixes tags for vfio-ccw and vfio-ap.

 drivers/s390/cio/vfio_ccw_drv.c   |    3 ++-
 drivers/s390/crypto/vfio_ap_drv.c |    2 +-
 samples/vfio-mdev/mbochs.c        |    7 ++++---
 samples/vfio-mdev/mdpy.c          |    7 ++++---
 samples/vfio-mdev/mtty.c          |    7 ++++---
 5 files changed, 15 insertions(+), 11 deletions(-)
drivers/s390/cio/vfio_ccw_drv.c +2 -1
index c2a65808605a..54aba7cceb33 100644
@@ -199,8 +199,9 @@ static int vfio_ccw_sch_probe(struct subchannel *sch)
 	return 0;
 
 out_unreg:
-	device_unregister(&parent->dev);
+	device_del(&parent->dev);
 out_free:
+	put_device(&parent->dev);
 	dev_set_drvdata(&sch->dev, NULL);
 	return ret;
 }
drivers/s390/crypto/vfio_ap_drv.c +1 -1
index f43cfeabd2cc..997b524bdd2b 100644
@@ -122,7 +122,7 @@ static int vfio_ap_matrix_dev_create(void)
 	return 0;
 
 matrix_drv_err:
-	device_unregister(&matrix_dev->device);
+	device_del(&matrix_dev->device);
 matrix_reg_err:
 	put_device(&matrix_dev->device);
 matrix_alloc_err:
samples/vfio-mdev/mbochs.c +4 -3
index 8b5a3a778a25..e54eb752e1ba 100644
@@ -1430,7 +1430,7 @@ static int __init mbochs_dev_init(void)
 
 	ret = device_register(&mbochs_dev);
 	if (ret)
-		goto err_class;
+		goto err_put;
 
 	ret = mdev_register_parent(&mbochs_parent, &mbochs_dev, &mbochs_driver,
 				   mbochs_mdev_types,
@@ -1441,8 +1441,9 @@ static int __init mbochs_dev_init(void)
 	return 0;
 
 err_device:
-	device_unregister(&mbochs_dev);
-err_class:
+	device_del(&mbochs_dev);
+err_put:
+	put_device(&mbochs_dev);
 	class_destroy(mbochs_class);
 err_driver:
 	mdev_unregister_driver(&mbochs_driver);
samples/vfio-mdev/mdpy.c +4 -3
index 721fb06c6413..e8400fdab71d 100644
@@ -717,7 +717,7 @@ static int __init mdpy_dev_init(void)
 
 	ret = device_register(&mdpy_dev);
 	if (ret)
-		goto err_class;
+		goto err_put;
 
 	ret = mdev_register_parent(&mdpy_parent, &mdpy_dev, &mdpy_driver,
 				   mdpy_mdev_types,
@@ -728,8 +728,9 @@ static int __init mdpy_dev_init(void)
 	return 0;
 
 err_device:
-	device_unregister(&mdpy_dev);
-err_class:
+	device_del(&mdpy_dev);
+err_put:
+	put_device(&mdpy_dev);
 	class_destroy(mdpy_class);
 err_driver:
 	mdev_unregister_driver(&mdpy_driver);
samples/vfio-mdev/mtty.c +4 -3
index 3c2a421b9b69..e887de672c52 100644
@@ -1330,7 +1330,7 @@ static int __init mtty_dev_init(void)
 
 	ret = device_register(&mtty_dev.dev);
 	if (ret)
-		goto err_class;
+		goto err_put;
 
 	ret = mdev_register_parent(&mtty_dev.parent, &mtty_dev.dev,
 				   &mtty_driver, mtty_mdev_types,
@@ -1340,8 +1340,9 @@ static int __init mtty_dev_init(void)
 	return 0;
 
 err_device:
-	device_unregister(&mtty_dev.dev);
-err_class:
+	device_del(&mtty_dev.dev);
+err_put:
+	put_device(&mtty_dev.dev);
 	class_destroy(mtty_dev.vd_class);
 err_driver:
 	mdev_unregister_driver(&mtty_driver);
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 39 lines · Alex Williamson

We always need to call put_device() if device_register() fails. All vfio drivers calling device_register() include a similar unwind stack via gotos, therefore split device_unregister() into its device_del() and put_device() components in the unwind path, and add a goto target to handle only the put_device() requirement.

Fixes d61fc96f47fd ("sample: vfio mdev display - host device")
Fixes 9d1a546c53b4 ("docs: Sample driver to demonstrate how to use Mediated device framework.")
Fixes a5e6e6505f38 ("sample: vfio bochs vbe display (host device for bochs-drm)")
Fixes 9e6f07cd1eaa ("vfio/ccw: create a parent struct")
Fixes 36360658eb5a ("s390: vfio_ap: link the vfio_ap devices to the vfio_ap bus subsystem")
Cc Tony Krowiak <[email protected]>
Cc Halil Pasic <[email protected]>
Cc Jason Herne <[email protected]>
Cc Kirti Wankhede <[email protected]>

---

I didn't intend to usurp Ruan's patch, but since the inline version is collecting reviews, formally post it and include additional fixes tags for vfio-ccw and vfio-ap.

  drivers/s390/cio/vfio_ccw_drv.c   |    3 ++-
  drivers/s390/crypto/vfio_ap_drv.c |    2 +-
  samples/vfio-mdev/mbochs.c        |    7 ++++---
  samples/vfio-mdev/mdpy.c          |    7 ++++---
  samples/vfio-mdev/mtty.c          |    7 ++++---
  5 files changed, 15 insertions(+), 11 deletions(-)

...

drivers/s390/crypto/vfio_ap_drv.c +0 -0
index f43cfeabd2cc..997b524bdd2b 100644

drivers/s390/crypto/vfio_ap_drv.c static int vfio_ap_matrix_dev_create(void) · line 128

Quoted 8 lines
drivers/s390/crypto/vfio_ap_drv.c +1 -1
@@ -122,7 +122,7 @@ static int vfio_ap_matrix_dev_create(void)
  	return 0;
 
  matrix_drv_err:
-	device_unregister(&matrix_dev->device);
+	device_del(&matrix_dev->device);
  matrix_reg_err:
  	put_device(&matrix_dev->device);
  matrix_alloc_err:
maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Alex Williamson
Quoted 138 lines · Alex Williamson

We always need to call put_device() if device_register() fails. All vfio drivers calling device_register() include a similar unwind stack via gotos, therefore split device_unregister() into its device_del() and put_device() components in the unwind path, and add a goto target to handle only the put_device() requirement.

Fixes d61fc96f47fd ("sample: vfio mdev display - host device")
Fixes 9d1a546c53b4 ("docs: Sample driver to demonstrate how to use Mediated device framework.")
Fixes a5e6e6505f38 ("sample: vfio bochs vbe display (host device for bochs-drm)")
Fixes 9e6f07cd1eaa ("vfio/ccw: create a parent struct")
Fixes 36360658eb5a ("s390: vfio_ap: link the vfio_ap devices to the vfio_ap bus subsystem")
Cc Tony Krowiak <[email protected]>
Cc Halil Pasic <[email protected]>
Cc Jason Herne <[email protected]>
Cc Kirti Wankhede <[email protected]>

---

I didn't intend to usurp Ruan's patch, but since the inline version is collecting reviews, formally post it and include additional fixes tags for vfio-ccw and vfio-ap.

  drivers/s390/cio/vfio_ccw_drv.c   |    3 ++-
  drivers/s390/crypto/vfio_ap_drv.c |    2 +-
  samples/vfio-mdev/mbochs.c        |    7 ++++---
  samples/vfio-mdev/mdpy.c          |    7 ++++---
  samples/vfio-mdev/mtty.c          |    7 ++++---
  5 files changed, 15 insertions(+), 11 deletions(-)
drivers/s390/cio/vfio_ccw_drv.c +2 -1
index c2a65808605a..54aba7cceb33 100644
@@ -199,8 +199,9 @@ static int vfio_ccw_sch_probe(struct subchannel *sch)
  	return 0;
 
  out_unreg:
-	device_unregister(&parent->dev);
+	device_del(&parent->dev);
  out_free:
+	put_device(&parent->dev);
  	dev_set_drvdata(&sch->dev, NULL);
  	return ret;
  }
drivers/s390/crypto/vfio_ap_drv.c +1 -1
index f43cfeabd2cc..997b524bdd2b 100644
@@ -122,7 +122,7 @@ static int vfio_ap_matrix_dev_create(void)
  	return 0;
 
  matrix_drv_err:
-	device_unregister(&matrix_dev->device);
+	device_del(&matrix_dev->device);
  matrix_reg_err:
  	put_device(&matrix_dev->device);
  matrix_alloc_err:
samples/vfio-mdev/mbochs.c +4 -3
index 8b5a3a778a25..e54eb752e1ba 100644
@@ -1430,7 +1430,7 @@ static int __init mbochs_dev_init(void)
 
  	ret = device_register(&mbochs_dev);
  	if (ret)
-		goto err_class;
+		goto err_put;
 
  	ret = mdev_register_parent(&mbochs_parent, &mbochs_dev, &mbochs_driver,
  				   mbochs_mdev_types,
@@ -1441,8 +1441,9 @@ static int __init mbochs_dev_init(void)
  	return 0;
 
  err_device:
-	device_unregister(&mbochs_dev);
-err_class:
+	device_del(&mbochs_dev);
+err_put:
+	put_device(&mbochs_dev);
  	class_destroy(mbochs_class);
  err_driver:
  	mdev_unregister_driver(&mbochs_driver);
samples/vfio-mdev/mdpy.c +4 -3
index 721fb06c6413..e8400fdab71d 100644
@@ -717,7 +717,7 @@ static int __init mdpy_dev_init(void)
 
  	ret = device_register(&mdpy_dev);
  	if (ret)
-		goto err_class;
+		goto err_put;
 
  	ret = mdev_register_parent(&mdpy_parent, &mdpy_dev, &mdpy_driver,
  				   mdpy_mdev_types,
@@ -728,8 +728,9 @@ static int __init mdpy_dev_init(void)
  	return 0;
 
  err_device:
-	device_unregister(&mdpy_dev);
-err_class:
+	device_del(&mdpy_dev);
+err_put:
+	put_device(&mdpy_dev);
  	class_destroy(mdpy_class);
  err_driver:
  	mdev_unregister_driver(&mdpy_driver);
samples/vfio-mdev/mtty.c +4 -3
index 3c2a421b9b69..e887de672c52 100644
@@ -1330,7 +1330,7 @@ static int __init mtty_dev_init(void)
 
  	ret = device_register(&mtty_dev.dev);
  	if (ret)
-		goto err_class;
+		goto err_put;
 
  	ret = mdev_register_parent(&mtty_dev.parent, &mtty_dev.dev,
  				   &mtty_driver, mtty_mdev_types,
@@ -1340,8 +1340,9 @@ static int __init mtty_dev_init(void)
  	return 0;
 
  err_device:
-	device_unregister(&mtty_dev.dev);
-err_class:
+	device_del(&mtty_dev.dev);
+err_put:
+	put_device(&mtty_dev.dev);
  	class_destroy(mtty_dev.vd_class);
  err_driver:
  	mdev_unregister_driver(&mtty_driver);
#3 lore raw
Quoted 24 lines · On Mon, 5 Dec 2022 14:20:53 -0500 Anthony Krowiak <[email protected]>…
Quoted 21 lines · Alex Williamson

We always need to call put_device() if device_register() fails. All vfio drivers calling device_register() include a similar unwind stack via gotos, therefore split device_unregister() into its device_del() and put_device() components in the unwind path, and add a goto target to handle only the put_device() requirement.

Fixes d61fc96f47fd ("sample: vfio mdev display - host device")
Fixes 9d1a546c53b4 ("docs: Sample driver to demonstrate how to use Mediated device framework.")
Fixes a5e6e6505f38 ("sample: vfio bochs vbe display (host device for bochs-drm)")
Fixes 9e6f07cd1eaa ("vfio/ccw: create a parent struct")
Fixes 36360658eb5a ("s390: vfio_ap: link the vfio_ap devices to the vfio_ap bus subsystem")
Cc Tony Krowiak <[email protected]>
Cc Halil Pasic <[email protected]>
Cc Jason Herne <[email protected]>
Cc Kirti Wankhede <[email protected]>

---

With all the ccw and ap acks, applied to vfio next branch for v6.2. Thanks,

Alex

Quoted 118 lines
Quoted 117 lines

I didn't intend to usurp Ruan's patch, but since the inline version is collecting reviews, formally post it and include additional fixes tags for vfio-ccw and vfio-ap.

  drivers/s390/cio/vfio_ccw_drv.c   |    3 ++-
  drivers/s390/crypto/vfio_ap_drv.c |    2 +-
  samples/vfio-mdev/mbochs.c        |    7 ++++---
  samples/vfio-mdev/mdpy.c          |    7 ++++---
  samples/vfio-mdev/mtty.c          |    7 ++++---
  5 files changed, 15 insertions(+), 11 deletions(-)
drivers/s390/cio/vfio_ccw_drv.c +2 -1
index c2a65808605a..54aba7cceb33 100644
@@ -199,8 +199,9 @@ static int vfio_ccw_sch_probe(struct subchannel *sch)
  	return 0;
 
  out_unreg:
-	device_unregister(&parent->dev);
+	device_del(&parent->dev);
  out_free:
+	put_device(&parent->dev);
  	dev_set_drvdata(&sch->dev, NULL);
  	return ret;
  }
drivers/s390/crypto/vfio_ap_drv.c +1 -1
index f43cfeabd2cc..997b524bdd2b 100644
@@ -122,7 +122,7 @@ static int vfio_ap_matrix_dev_create(void)
  	return 0;
 
  matrix_drv_err:
-	device_unregister(&matrix_dev->device);
+	device_del(&matrix_dev->device);
  matrix_reg_err:
  	put_device(&matrix_dev->device);
  matrix_alloc_err:
samples/vfio-mdev/mbochs.c +4 -3
index 8b5a3a778a25..e54eb752e1ba 100644
@@ -1430,7 +1430,7 @@ static int __init mbochs_dev_init(void)
 
  	ret = device_register(&mbochs_dev);
  	if (ret)
-		goto err_class;
+		goto err_put;
 
  	ret = mdev_register_parent(&mbochs_parent, &mbochs_dev, &mbochs_driver,
  				   mbochs_mdev_types,
@@ -1441,8 +1441,9 @@ static int __init mbochs_dev_init(void)
  	return 0;
 
  err_device:
-	device_unregister(&mbochs_dev);
-err_class:
+	device_del(&mbochs_dev);
+err_put:
+	put_device(&mbochs_dev);
  	class_destroy(mbochs_class);
  err_driver:
  	mdev_unregister_driver(&mbochs_driver);
samples/vfio-mdev/mdpy.c +4 -3
index 721fb06c6413..e8400fdab71d 100644
@@ -717,7 +717,7 @@ static int __init mdpy_dev_init(void)
 
  	ret = device_register(&mdpy_dev);
  	if (ret)
-		goto err_class;
+		goto err_put;
 
  	ret = mdev_register_parent(&mdpy_parent, &mdpy_dev, &mdpy_driver,
  				   mdpy_mdev_types,
@@ -728,8 +728,9 @@ static int __init mdpy_dev_init(void)
  	return 0;
 
  err_device:
-	device_unregister(&mdpy_dev);
-err_class:
+	device_del(&mdpy_dev);
+err_put:
+	put_device(&mdpy_dev);
  	class_destroy(mdpy_class);
  err_driver:
  	mdev_unregister_driver(&mdpy_driver);
samples/vfio-mdev/mtty.c +4 -3
index 3c2a421b9b69..e887de672c52 100644
@@ -1330,7 +1330,7 @@ static int __init mtty_dev_init(void)
 
  	ret = device_register(&mtty_dev.dev);
  	if (ret)
-		goto err_class;
+		goto err_put;
 
  	ret = mdev_register_parent(&mtty_dev.parent, &mtty_dev.dev,
  				   &mtty_driver, mtty_mdev_types,
@@ -1340,8 +1340,9 @@ static int __init mtty_dev_init(void)
  	return 0;
 
  err_device:
-	device_unregister(&mtty_dev.dev);
-err_class:
+	device_del(&mtty_dev.dev);
+err_put:
+	put_device(&mtty_dev.dev);
  	class_destroy(mtty_dev.vd_class);
  err_driver:
  	mdev_unregister_driver(&mtty_driver);
maintainer Reviewed-by on v1 1/1
#4 lore raw
In reply to Alex Williamson
Quoted 6 lines · On Fri, 02 Dec 2022 09:46:15 -0700 Alex Williamson…

We always need to call put_device() if device_register() fails. All vfio drivers calling device_register() include a similar unwind stack via gotos, therefore split device_unregister() into its device_del() and put_device() components in the unwind path, and add a goto target to handle only the put_device() requirement.

Quoted 14 lines
Fixes d61fc96f47fd ("sample: vfio mdev display - host device")
Fixes 9d1a546c53b4 ("docs: Sample driver to demonstrate how to use Mediated device framework.")
Fixes a5e6e6505f38 ("sample: vfio bochs vbe display (host device for bochs-drm)")
Fixes 9e6f07cd1eaa ("vfio/ccw: create a parent struct")
Fixes 36360658eb5a ("s390: vfio_ap: link the vfio_ap devices to the vfio_ap bus subsystem")
Cc Tony Krowiak <[email protected]>
Cc Halil Pasic <[email protected]>
Cc Jason Herne <[email protected]>
Cc Kirti Wankhede <[email protected]>