[PATCH 3/5] drm/i915: Handle driver load/unload better for mock selftests

Jason Ekstrand jason at jlekstrand.net
Mon Jul 19 08:37:48 UTC 2021


When we run the mock selftests, we don't actually let the driver load
all the way.  Instead, we run the selftests as part of driver load and
then exit early.  Perviously, i915_init() would return a non-zero error
code on failure and a zero error code on success.  In i915_exit(), we
check i915_pci_driver.driver.owner to detect if i915_init exited early
and don't tear anything down.  However, if selftests ran, the early exit
would happen after i915_globals_init() and we i915_globals_exit() was
never called, leaking all our slabs.

The most annoying part is that you don't actually notice the failure as
part of the self-tests since leaking a bit of memory, while bad, doesn't
result in anything observable from userspace.  Instead, the next time we
load the driver (usually for next IGT test), i915_globals_init() gets
invoked again, we go to allocate a bunch of new memory slabs, those
implicitly create debugfs entries, and debugfs warns that we're trying
to create directories and files that already exist.  Since this all
happens as part of the next driver load, it shows up in the dmesg-warn
of whatever IGT test ran after the mock selftests.

While the obvious thing to do here might be to call i915_globals_exit()
after selftests, that's not actually safe.  There are various objects
which get initialized as part of the mock selftests which get tied to
the DRM driver.  They will be cleaned up before module unload but it's
tricky to get them all in the selftest tear-down path.  The safer thing
to do is to let the module partially load, and then tear down only the
globals as part of i915_exit().

Signed-off-by: Jason Ekstrand <jason at jlekstrand.net>
Fixes: 32eb6bcfdda9 ("drm/i915: Make request allocation caches global")
Cc: Daniel Vetter <daniel at ffwll.ch>
---
 drivers/gpu/drm/i915/i915_pci.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/drivers/gpu/drm/i915/i915_pci.c b/drivers/gpu/drm/i915/i915_pci.c
index 4e627b57d31a2..8e097712b7c17 100644
--- a/drivers/gpu/drm/i915/i915_pci.c
+++ b/drivers/gpu/drm/i915/i915_pci.c
@@ -1199,13 +1199,18 @@ static int __init i915_init(void)
 	bool use_kms = true;
 	int err;
 
+	/* We use this to detect early returns from i915_init() so we don't
+	 * tear anything down in i915_exit()
+	 */
+	i915_pci_driver.driver.owner = NULL;
+
 	err = i915_globals_init();
 	if (err)
 		return err;
 
 	err = i915_mock_selftests();
 	if (err)
-		return err > 0 ? 0 : err;
+		return 0;
 
 	/*
 	 * Enable KMS by default, unless explicitly overriden by
@@ -1240,12 +1245,11 @@ static int __init i915_init(void)
 
 static void __exit i915_exit(void)
 {
-	if (!i915_pci_driver.driver.owner)
-		return;
-
-	i915_perf_sysctl_unregister();
-	pci_unregister_driver(&i915_pci_driver);
-	i915_pmu_exit();
+	if (i915_pci_driver.driver.owner) {
+		i915_perf_sysctl_unregister();
+		pci_unregister_driver(&i915_pci_driver);
+		i915_pmu_exit();
+	}
 	i915_globals_exit();
 }
 
-- 
2.31.1



More information about the Intel-gfx-trybot mailing list