From: Marcin Bernatowicz <marcin.bernatowicz@linux.intel.com>
To: igt-dev@lists.freedesktop.org
Cc: adam.miszczak@linux.intel.com, jakub1.kolakowski@intel.com,
lukasz.laguna@intel.com,
Marcin Bernatowicz <marcin.bernatowicz@linux.intel.com>
Subject: [PATCH v2 i-g-t 1/6] lib/igt_device_sriov: Add generic SR-IOV exit cleanup helper
Date: Mon, 15 Jun 2026 12:32:15 +0200 [thread overview]
Message-ID: <20260615103220.281656-2-marcin.bernatowicz@linux.intel.com> (raw)
In-Reply-To: <20260615103220.281656-1-marcin.bernatowicz@linux.intel.com>
Add SR-IOV exit-handler helpers.
The handler provides best-effort cleanup on exit: optional callback,
VF disable, and optional autoprobe restore.
Add a helper to clear handler state after normal teardown.
Signed-off-by: Marcin Bernatowicz <marcin.bernatowicz@linux.intel.com>
Cc: Adam Miszczak <adam.miszczak@linux.intel.com>
Cc: Jakub Kolakowski <jakub1.kolakowski@intel.com>
Cc: Lukasz Laguna <lukasz.laguna@intel.com>
---
v2: Always try to restore sriov_drivers_autoprobe. (Lukasz)
---
lib/igt_sriov_device.c | 91 ++++++++++++++++++++++++++++++++++++++++++
lib/igt_sriov_device.h | 5 +++
2 files changed, 96 insertions(+)
diff --git a/lib/igt_sriov_device.c b/lib/igt_sriov_device.c
index 8f2cfa50f..da1557010 100644
--- a/lib/igt_sriov_device.c
+++ b/lib/igt_sriov_device.c
@@ -546,3 +546,94 @@ bool intel_is_vf_device(int fd)
return (value & 1) != 0;
}
+
+struct sriov_exit_handler_ctx {
+ int pf;
+ bool restore_autoprobe;
+ bool autoprobe;
+ igt_sriov_exit_cleanup_fn cleanup_fn;
+ void *cleanup_data;
+};
+
+static struct sriov_exit_handler_ctx sriov_exit_ctx = {
+ .pf = -1,
+};
+
+static void sriov_exit_handler_release_fd(void)
+{
+ if (sriov_exit_ctx.pf < 0)
+ return;
+
+ __drm_close_driver(sriov_exit_ctx.pf);
+ sriov_exit_ctx.pf = -1;
+}
+
+static void sriov_exit_handler(int sig)
+{
+ if (sriov_exit_ctx.pf < 0)
+ return;
+
+ igt_sriov_disable_vfs(sriov_exit_ctx.pf);
+
+ if (sriov_exit_ctx.cleanup_fn)
+ sriov_exit_ctx.cleanup_fn(sriov_exit_ctx.pf, sig,
+ sriov_exit_ctx.cleanup_data);
+
+ if (sriov_exit_ctx.autoprobe)
+ igt_sriov_enable_driver_autoprobe(sriov_exit_ctx.pf);
+ else
+ igt_sriov_disable_driver_autoprobe(sriov_exit_ctx.pf);
+}
+
+/**
+ * igt_sriov_install_exit_handler - Install best-effort SR-IOV cleanup handler
+ * @pf: PF device file descriptor
+ * @cleanup_fn: Optional callback invoked after VF disable
+ * @cleanup_data: Opaque callback data
+ *
+ * Registers a process-exit cleanup routine for SR-IOV tests. The handler runs
+ * from normal and signal-triggered exits (when possible), disables VFs,
+ * invokes @cleanup_fn if provided (passing signal number, or 0 on normal
+ * exit), and restores the original autoprobe setting captured at
+ * installation time. Tests that perform full explicit teardown should call
+ * igt_sriov_clear_exit_handler() once that teardown succeeds. An internal
+ * duplicate of @pf is kept so cleanup remains usable even if the caller closes
+ * the original DRM fd before process exit.
+ */
+void igt_sriov_install_exit_handler(int pf,
+ igt_sriov_exit_cleanup_fn cleanup_fn,
+ void *cleanup_data)
+{
+ int pf_dup;
+
+ if (!igt_sriov_is_pf(pf))
+ return;
+
+ pf_dup = dup(pf);
+ igt_assert_f(pf_dup >= 0, "Failed to duplicate PF fd (%s)\n",
+ strerror(errno));
+
+ sriov_exit_handler_release_fd();
+
+ sriov_exit_ctx.pf = pf_dup;
+ sriov_exit_ctx.autoprobe = igt_sriov_is_driver_autoprobe_enabled(pf_dup);
+ sriov_exit_ctx.cleanup_fn = cleanup_fn;
+ sriov_exit_ctx.cleanup_data = cleanup_data;
+
+ igt_install_exit_handler(sriov_exit_handler);
+}
+
+/**
+ * igt_sriov_clear_exit_handler - Disable SR-IOV exit cleanup context
+ *
+ * Clears the active SR-IOV cleanup context, used when a test has completed
+ * explicit teardown and no longer needs best-effort cleanup on process exit.
+ */
+void igt_sriov_clear_exit_handler(void)
+{
+ sriov_exit_handler_release_fd();
+ sriov_exit_ctx.restore_autoprobe = false;
+ sriov_exit_ctx.autoprobe = false;
+ sriov_exit_ctx.cleanup_fn = NULL;
+ sriov_exit_ctx.cleanup_data = NULL;
+}
diff --git a/lib/igt_sriov_device.h b/lib/igt_sriov_device.h
index a417b30d8..4e4ba230a 100644
--- a/lib/igt_sriov_device.h
+++ b/lib/igt_sriov_device.h
@@ -36,6 +36,11 @@ bool igt_sriov_device_reset_exists(int pf, unsigned int vf_num);
bool igt_sriov_device_reset(int pf, unsigned int vf_num);
bool intel_is_vf_device(int device);
const char *igt_sriov_func_str(unsigned int vf_num);
+typedef void (*igt_sriov_exit_cleanup_fn)(int pf, int sig, void *user_data);
+void igt_sriov_install_exit_handler(int pf,
+ igt_sriov_exit_cleanup_fn cleanup_fn,
+ void *cleanup_data);
+void igt_sriov_clear_exit_handler(void);
/**
* __is_valid_range - Helper to check VF range is valid
--
2.43.0
next prev parent reply other threads:[~2026-06-15 10:33 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-15 10:32 [PATCH v2 i-g-t 0/6] Add SR-IOV exit-handler-based cleanup Marcin Bernatowicz
2026-06-15 10:32 ` Marcin Bernatowicz [this message]
2026-06-15 10:32 ` [PATCH v2 i-g-t 2/6] lib/xe/xe_sriov_admin: Add SR-IOV admin exit cleanup restore helper Marcin Bernatowicz
2026-06-15 10:32 ` [PATCH v2 i-g-t 3/6] tests/sriov_basic: Arm SR-IOV exit cleanup Marcin Bernatowicz
2026-06-15 10:32 ` [PATCH v2 i-g-t 4/6] tests/xe: " Marcin Bernatowicz
2026-06-15 10:32 ` [PATCH v2 i-g-t 5/6] tests/xe: Arm SR-IOV exit cleanup with callback Marcin Bernatowicz
2026-06-15 10:32 ` [PATCH v2 i-g-t 6/6] tests/intel/xe_sriov_flr: Arm SR-IOV exit cleanup Marcin Bernatowicz
2026-06-15 17:36 ` ✓ i915.CI.BAT: success for Add SR-IOV exit-handler-based cleanup (rev2) Patchwork
2026-06-15 17:36 ` ✓ Xe.CI.BAT: " Patchwork
2026-06-15 18:44 ` ✗ Xe.CI.FULL: failure " Patchwork
2026-06-16 1:02 ` ✗ i915.CI.Full: " Patchwork
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260615103220.281656-2-marcin.bernatowicz@linux.intel.com \
--to=marcin.bernatowicz@linux.intel.com \
--cc=adam.miszczak@linux.intel.com \
--cc=igt-dev@lists.freedesktop.org \
--cc=jakub1.kolakowski@intel.com \
--cc=lukasz.laguna@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox