amdgpu_init() registers the ATPX handler with vga_switcheroo and
allocates the ACPI XCC/NUMA info before it initializes KFD, but its
error paths do not undo any of that:

- when amdgpu_amdkfd_init() fails, only amdgpu_sync_fini() is called;
- when pci_register_driver() fails, its result is returned as is, so
  KFD, the ATPX handler, the ACPI info and the sync slab cache are all
  left behind.

A failed module init frees the module, so vga_switcheroo keeps a
pointer to a handler in freed memory and /dev/kfd keeps file operations
that point into it. A later amdgpu load cannot register its own ATPX
handler, and vga_switcheroo_enable() calls into the stale one.

The KFD path is easy to reach since KFD init failures are no longer
ignored: kfd_init() returns -EINVAL for an out of range sched_policy or
max_num_of_queues_per_device module parameter.

Unwind in the reverse order of initialization, using the same calls as
amdgpu_exit(). amdgpu_amdkfd_fini() is a no-op when KFD was not
initialized, which covers the ignored -ENOENT case.

Fixes: 4259a25341ab ("amdkfd: Only ignore -ENOENT for KFD init failuires")
Fixes: d38ceaf99ed0 ("drm/amdgpu: add core driver (v4)")
Signed-off-by: Jiaming Fan <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c
index 72b3710..322c296 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c
@@ -3220,7 +3220,7 @@ static int __init amdgpu_init(void)
        /* Ignore KFD init failures when CONFIG_HSA_AMD is not set. */
        r = amdgpu_amdkfd_init();
        if (r && r != -ENOENT)
-               goto error_fini_sync;
+               goto error_release_acpi;
 
        if (amdgpu_pp_feature_mask & PP_OVERDRIVE_MASK) {
                add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK);
@@ -3229,9 +3229,17 @@ static int __init amdgpu_init(void)
        }
 
        /* let modprobe override vga console setting */
-       return pci_register_driver(&amdgpu_kms_pci_driver);
+       r = pci_register_driver(&amdgpu_kms_pci_driver);
+       if (r)
+               goto error_fini_kfd;
+
+       return 0;
 
-error_fini_sync:
+error_fini_kfd:
+       amdgpu_amdkfd_fini();
+error_release_acpi:
+       amdgpu_acpi_release();
+       amdgpu_unregister_atpx_handler();
        amdgpu_sync_fini();
        return r;
 }
-- 
2.56.0


Reply via email to