Skip to contentSkip to conversation

drm/etnaviv: Check for platform_device_register_simple() failure

PATCH Superseded Fabio Estevam 2 participants 4 messages Jun 25, 2018 – Jun 27, 2018

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/2 drm/etnaviv: Check for platform_device_register_simple() failure

From: Fabio Estevam <[email protected]>

platform_device_register_simple() may fail, so we should better check its return value and propagate it in the case of error.

Fixes 246774d17fc0 ("drm/etnaviv: remove the need for a gpu-subsystem DT node")

---
Changes since v2:
- Newly introduced in this version

 drivers/gpu/drm/etnaviv/etnaviv_drv.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
drivers/gpu/drm/etnaviv/etnaviv_drv.c +15 -3
index e5013a9..ac23c0f 100644
@@ -631,6 +631,8 @@ static struct platform_driver etnaviv_platform_driver = {
 	},
 };
 
+static struct platform_device *etnaviv_drm;
+
 static int __init etnaviv_init(void)
 {
 	int ret;
@@ -644,7 +646,7 @@ static int __init etnaviv_init(void)
 
 	ret = platform_driver_register(&etnaviv_platform_driver);
 	if (ret != 0)
-		platform_driver_unregister(&etnaviv_gpu_driver);
+		goto unregister_gpu_driver;
 
 	/*
 	 * If the DT contains at least one available GPU device, instantiate
@@ -653,12 +655,22 @@ static int __init etnaviv_init(void)
 	for_each_compatible_node(np, NULL, "vivante,gc") {
 		if (!of_device_is_available(np))
 			continue;
-
-		platform_device_register_simple("etnaviv", -1, NULL, 0);
+		etnaviv_drm = platform_device_register_simple("etnaviv", -1,
+							      NULL, 0);
+		if (IS_ERR(etnaviv_drm)) {
+			ret = PTR_ERR(etnaviv_drm);
+			goto unregister_platform_driver;
+		}
 		of_node_put(np);
 		break;
 	}
 
+	return 0;
+
+unregister_platform_driver:
+	platform_driver_unregister(&etnaviv_platform_driver);
+unregister_gpu_driver:
+	platform_driver_unregister(&etnaviv_gpu_driver);
 	return ret;
 }
 module_init(etnaviv_init);
Signature · 1 line
2.7.4
#1 lore raw

PATCH v3 2/2 drm/etnaviv: Fix driver unregistering

From: Fabio Estevam <[email protected]>

Russell King reported:

"When removing and reloading the etnaviv module, the following splat occurs:

sysfs: cannot create duplicate filename '/devices/platform/etnaviv'

CPU: 0 PID: 1471 Comm: modprobe Not tainted 4.17.0+ #1608
Hardware name: Marvell Dove (Cubox)
Backtrace:
[<c00157d4>] (dump_backtrace) from [<c0015b8c>] (show_stack+0x18/0x1c)
 r6:ef033e38 r5:ee07b340 r4:edb9d000 r3:00000000
[<c0015b74>] (show_stack) from [<c0620784>] (dump_stack+0x20/0x28)
[<c0620764>] (dump_stack) from [<c01bcd24>] (sysfs_warn_dup+0x5c/0x70)
[<c01bccc8>] (sysfs_warn_dup) from [<c01bce14>] (sysfs_create_dir_ns+0x90/0x98)

..."

Commit 246774d17fc0 ("drm/etnaviv: remove the need for a gpu-subsystem DT node") introduced DRM registration via platform_device_register_simple(), but missed to call platform_device_unregister() inside etnaviv_exit().

Fix the problem by calling platform_device_unregister() inside etnaviv_exit(). While at it, also rearrange the function calls in the exit path to make them happen in the opposite order of registration.

Tested on a imx6-sabresd board.

Fixes 246774d17fc0 ("drm/etnaviv: remove the need for a gpu-subsystem DT node")

---
Changes since v2:
- Add a 'available_gpu' bool variable to indicate that an available "vivante,gc" has been found

 drivers/gpu/drm/etnaviv/etnaviv_drv.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/gpu/drm/etnaviv/etnaviv_drv.c +5 -1
index ac23c0f..9044bf9 100644
@@ -632,6 +632,7 @@ static struct platform_driver etnaviv_platform_driver = {
 };
 
 static struct platform_device *etnaviv_drm;
+static bool available_gpu;
 
 static int __init etnaviv_init(void)
 {
@@ -655,6 +656,7 @@ static int __init etnaviv_init(void)
 	for_each_compatible_node(np, NULL, "vivante,gc") {
 		if (!of_device_is_available(np))
 			continue;
+		available_gpu = true;
 		etnaviv_drm = platform_device_register_simple("etnaviv", -1,
 							      NULL, 0);
 		if (IS_ERR(etnaviv_drm)) {
@@ -677,8 +679,10 @@ module_init(etnaviv_init);
 
 static void __exit etnaviv_exit(void)
 {
-	platform_driver_unregister(&etnaviv_gpu_driver);
+	if (available_gpu)
+		platform_device_unregister(etnaviv_drm);
 	platform_driver_unregister(&etnaviv_platform_driver);
+	platform_driver_unregister(&etnaviv_gpu_driver);
 }
 module_exit(etnaviv_exit);
 
Signature · 1 line
2.7.4
Reviewed-by on v3 2/2
#2 lore raw

Re: [PATCH v3 2/2] drm/etnaviv: Fix driver unregistering

Quoted 47 lines · Fabio Estevam

From: Fabio Estevam <[email protected]>

Russell King reported:

"When removing and reloading the etnaviv module, the following splat occurs:

sysfs: cannot create duplicate filename '/devices/platform/etnaviv'

CPU: 0 PID: 1471 Comm: modprobe Not tainted 4.17.0+ #1608
Hardware name: Marvell Dove (Cubox)
Backtrace:
[<c00157d4>] (dump_backtrace) from [<c0015b8c>] (show_stack+0x18/0x1c)
 r6:ef033e38 r5:ee07b340 r4:edb9d000 r3:00000000
[<c0015b74>] (show_stack) from [<c0620784>] (dump_stack+0x20/0x28)
[<c0620764>] (dump_stack) from [<c01bcd24>] (sysfs_warn_dup+0x5c/0x70)
[<c01bccc8>] (sysfs_warn_dup) from [<c01bce14>] (sysfs_create_dir_ns+0x90/0x98)

..."

Commit 246774d17fc0 ("drm/etnaviv: remove the need for a gpu-subsystem DT node") introduced DRM registration via platform_device_register_simple(), but missed to call platform_device_unregister() inside etnaviv_exit().

Fix the problem by calling platform_device_unregister() inside etnaviv_exit(). While at it, also rearrange the function calls in the exit path to make them happen in the opposite order of registration.

Tested on a imx6-sabresd board.

Fixes 246774d17fc0 ("drm/etnaviv: remove the need for a gpu-subsystem DT node")

---
Changes since v2:
- Add a 'available_gpu' bool variable to indicate that an available "vivante,gc" has been found

 drivers/gpu/drm/etnaviv/etnaviv_drv.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/gpu/drm/etnaviv/etnaviv_drv.c +0 -0
index ac23c0f..9044bf9 100644

drivers/gpu/drm/etnaviv/etnaviv_drv.c static struct platform_driver etnaviv_platform_driver = { · line 635

Quoted 4 lines
drivers/gpu/drm/etnaviv/etnaviv_drv.c +1 -0
@@ -632,6 +632,7 @@ static struct platform_driver etnaviv_platform_driver = {
 };
 
 static struct platform_device *etnaviv_drm;
+static bool available_gpu;

This would not be necessary if etnaviv_drm weren't ever set to an error value.

Quoted 11 lines
drivers/gpu/drm/etnaviv/etnaviv_drv.c +1 -0
 static int __init etnaviv_init(void)
 {
@@ -655,6 +656,7 @@ static int __init etnaviv_init(void)
 	for_each_compatible_node(np, NULL, "vivante,gc") {
 		if (!of_device_is_available(np))
 			continue;
+		available_gpu = true;
 		etnaviv_drm = platform_device_register_simple("etnaviv", -1,
 							      NULL, 0);
 		if (IS_ERR(etnaviv_drm)) {

drivers/gpu/drm/etnaviv/etnaviv_drv.c module_init(etnaviv_init); · line 683

Quoted 6 lines
drivers/gpu/drm/etnaviv/etnaviv_drv.c +2 -1
@@ -677,8 +679,10 @@ module_init(etnaviv_init);
 
 static void __exit etnaviv_exit(void)
 {
-	platform_driver_unregister(&etnaviv_gpu_driver);
+	if (available_gpu)
+		platform_device_unregister(etnaviv_drm);

Both platform_device_del and platform_device_put (called by platform_device_unregister) will gracefully degrade to a no-op if etnaviv_drm == NULL, so this could just be:

	platform_device_unregister(etnaviv_drm);

drivers/gpu/drm/etnaviv/etnaviv_drv.c module_init(etnaviv_init); · line 687

Quoted 4 lines
drivers/gpu/drm/etnaviv/etnaviv_drv.c +1 -0
 	platform_driver_unregister(&etnaviv_platform_driver);
+	platform_driver_unregister(&etnaviv_gpu_driver);
 }
 module_exit(etnaviv_exit);

I'd change the first patch and drop the available_gpu static variable, but either way:

regards
Philipp

#3 lore raw
In reply to Fabio Estevam
Quoted 38 lines · Fabio Estevam

From: Fabio Estevam <[email protected]>

platform_device_register_simple() may fail, so we should better check its return value and propagate it in the case of error.

Fixes 246774d17fc0 ("drm/etnaviv: remove the need for a gpu-subsystem DT node")

---
Changes since v2:
- Newly introduced in this version

 drivers/gpu/drm/etnaviv/etnaviv_drv.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
drivers/gpu/drm/etnaviv/etnaviv_drv.c +3 -1
index e5013a9..ac23c0f 100644
@@ -631,6 +631,8 @@ static struct platform_driver etnaviv_platform_driver = {
 	},
 };
 
+static struct platform_device *etnaviv_drm;
+
 static int __init etnaviv_init(void)
 {
 	int ret;
@@ -644,7 +646,7 @@ static int __init etnaviv_init(void)
 
 	ret = platform_driver_register(&etnaviv_platform_driver);
 	if (ret != 0)
-		platform_driver_unregister(&etnaviv_gpu_driver);
+		goto unregister_gpu_driver;
 
 	/*
 	 * If the DT contains at least one available GPU device, instantiate

drivers/gpu/drm/etnaviv/etnaviv_drv.c static int __init etnaviv_init(void) · line 659

Quoted 7 lines
drivers/gpu/drm/etnaviv/etnaviv_drv.c +2 -2
@@ -653,12 +655,22 @@ static int __init etnaviv_init(void)
 	for_each_compatible_node(np, NULL, "vivante,gc") {
 		if (!of_device_is_available(np))
 			continue;
-
-		platform_device_register_simple("etnaviv", -1, NULL, 0);
+		etnaviv_drm = platform_device_register_simple("etnaviv", -1,
+							      NULL, 0);

Rather than store the error value in the global variable, I'd use a local variable here and only set etnaviv_drm if this doesn't fail.

drivers/gpu/drm/etnaviv/etnaviv_drv.c static int __init etnaviv_init(void) · line 665

Quoted 6 lines
drivers/gpu/drm/etnaviv/etnaviv_drv.c +4 -0
+		if (IS_ERR(etnaviv_drm)) {
+			ret = PTR_ERR(etnaviv_drm);
+			goto unregister_platform_driver;
+		}
 		of_node_put(np);
 		break;

Ah, focusing on etnaviv_exit last time I've completely overlooked this break.

drivers/gpu/drm/etnaviv/etnaviv_drv.c static int __init etnaviv_init(void) · line 676

Quoted 11 lines
drivers/gpu/drm/etnaviv/etnaviv_drv.c +6 -0
 	}
 
+	return 0;
+
+unregister_platform_driver:
+	platform_driver_unregister(&etnaviv_platform_driver);
+unregister_gpu_driver:
+	platform_driver_unregister(&etnaviv_gpu_driver);
 	return ret;
 }
 module_init(etnaviv_init);

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel