amdgpu_register_atpx_handler() registers the ATPX handler with
vga_switcheroo only when ATPX is detected, and ignores the result:
vga_switcheroo has a single handler slot and refuses a second handler
with -EINVAL. amdgpu_unregister_atpx_handler() calls
vga_switcheroo_unregister_handler() in either case, and that clears the
slot no matter who set it.

So unloading amdgpu removes a handler registered by another driver, such
as apple-gmux. No AMD GPU is needed for this; loading and unloading
amdgpu is enough. Afterwards vga_switcheroo can no longer switch outputs,
and a client that uses the vga_switcheroo runtime PM domain, such as
nouveau on a non-Optimus machine with a v1 DSM, can dereference the now
NULL handler during runtime suspend.

Record whether the registration succeeded and unregister only in that
case.

Fixes: d38ceaf99ed0 ("drm/amdgpu: add core driver (v4)")
Cc: [email protected]
Signed-off-by: Jiaming Fan <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_atpx_handler.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_atpx_handler.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_atpx_handler.c
index e2a4644..f946778 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_atpx_handler.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_atpx_handler.c
@@ -45,6 +45,7 @@ struct amdgpu_atpx {
 static struct amdgpu_atpx_priv {
        bool atpx_detected;
        bool bridge_pm_usable;
+       bool handler_registered;
        unsigned int quirks;
        /* handle for device - and atpx */
        acpi_handle dhandle;
@@ -663,7 +664,8 @@ void amdgpu_register_atpx_handler(void)
        if (!r)
                return;
 
-       vga_switcheroo_register_handler(&amdgpu_atpx_handler, handler_flags);
+       if (!vga_switcheroo_register_handler(&amdgpu_atpx_handler, 
handler_flags))
+               amdgpu_atpx_priv.handler_registered = true;
 }
 
 /**
@@ -673,5 +675,14 @@ void amdgpu_register_atpx_handler(void)
  */
 void amdgpu_unregister_atpx_handler(void)
 {
+       /*
+        * vga_switcheroo has a single handler slot and unregistering clears it
+        * no matter who set it, so leave a handler registered by another
+        * driver, such as apple-gmux, in place.
+        */
+       if (!amdgpu_atpx_priv.handler_registered)
+               return;
+
        vga_switcheroo_unregister_handler();
+       amdgpu_atpx_priv.handler_registered = false;
 }
-- 
2.56.0


Reply via email to