From patchwork Fri Aug 28 07:59:15 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Janusz Krzysztofik X-Patchwork-Id: 11742319 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id F0736913 for ; Fri, 28 Aug 2020 08:00:26 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id D7A9220C56 for ; Fri, 28 Aug 2020 08:00:26 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D7A9220C56 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linux.intel.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=intel-gfx-bounces@lists.freedesktop.org Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id BC5616EB88; Fri, 28 Aug 2020 08:00:25 +0000 (UTC) X-Original-To: intel-gfx@lists.freedesktop.org Delivered-To: intel-gfx@lists.freedesktop.org Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by gabe.freedesktop.org (Postfix) with ESMTPS id 136BA6EB70; Fri, 28 Aug 2020 08:00:24 +0000 (UTC) IronPort-SDR: 6vN/SStGPfMKcjqvxDL8I9f0rYVzLvR9xohk4yJCvjBT0+l0OKF7odiRLK32iJI8k2ihQA3h9W RhsSsccojBJA== X-IronPort-AV: E=McAfee;i="6000,8403,9726"; a="174676545" X-IronPort-AV: E=Sophos;i="5.76,363,1592895600"; d="scan'208";a="174676545" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Aug 2020 01:00:23 -0700 IronPort-SDR: zhsbVwL7d8OXW3Yp7U7tpHshbzAc6xA+qm+k6tdmEKwN5l2JpxFykQH6g8o6OPoXnlDnQ+1VGx IVHxzixwSNBg== X-IronPort-AV: E=Sophos;i="5.76,363,1592895600"; d="scan'208";a="444755948" Received: from jkrzyszt-desk.igk.intel.com ([172.22.244.18]) by orsmga004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Aug 2020 01:00:03 -0700 From: Janusz Krzysztofik To: igt-dev@lists.freedesktop.org Date: Fri, 28 Aug 2020 09:59:15 +0200 Message-Id: <20200828075927.17061-10-janusz.krzysztofik@linux.intel.com> X-Mailer: git-send-email 2.21.1 In-Reply-To: <20200828075927.17061-1-janusz.krzysztofik@linux.intel.com> References: <20200828075927.17061-1-janusz.krzysztofik@linux.intel.com> MIME-Version: 1.0 Subject: [Intel-gfx] [PATCH i-g-t v5 09/21] tests/core_hotunplug: Prepare invariant data once per test run X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?utf-8?q?Micha=C5=82_Winiarski?= , intel-gfx@lists.freedesktop.org Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" Each subtest now calls a prepare() helper which opens a couple of files required by that subtest. Those files are then closed after use, either directly from the subtest body, or indirectly from inside one of helper functions called during the subtest execution. That approach not only makes life cycle of individual file descriptors difficult to follow but also prevents us from re-running health checks on subtest failures from follow up igt_fixture sections since we may need to retry bus rescan or driver rebind operations. Two of those files - device bus and driver sysfs nodes - are not affected nor interfere with driver unbind / device unplug operations performed by subtests. Then, there is not much sense in closing and reopening those nodes. Open them once at the beginning of a test run, then close them as late as on test completion. The prepare() helper also populates a device bus address string used by driver unbind / rebind operations. Since the bus address of an exercised device never changes, also prepare that string only once at the beginning of a test run. Note that it is the same as the last component of a device filter string which is already resolved and installed from an initial igt_fixture section of the test. Then, initialize the device bus address field of a hotunplug structure instance with a pointer to the respective substring of that filter rather than resolving it again from the device sysfs node pathname. There is one more sysfs node - a DRM device node - now opened by the prepare() helper for subtests which perform device remove operations. That node can't be opened only once at the beginning of a test run because its open file descriptor is no longer usable as soon as a driver unbind operation is performed. On the other hand, it can't be opened easily from inside a device_remove() helper since some subtests just don't open the device so its file descriptor used by igt_sysfs_open() may just not be available. However, note that only a PCI sysfs node of the device, not necessarily the DRM one, is actually required for a successful device remove operation, and that node can be opened easily from a bus file descriptor using a device bus address string, both already available. Then, change the semantics of a .fd.sysfs_dev field of the hotunplug structure from DRM to PCI device sysfs file descriptor, then let the device_remove() helper open the device PCI node by itself and store its file descriptor in that field. Also, for still more easy access to the device PCI node, use a 'subsystem/devices' sub-node of the PCI device as its bus sysfs location instead of just 'subsystem', then adjust a relative path to the bus 'rescan' function accordingly. A side benefit of using the PCI device sysfs node, not the DRM one, while removing the device is that a future subtest may now easily perform both driver unbind and device remove operations in a row. v2: Rebase only. v3: Refresh. v4: Still assert a device dile descriptor closed cleanly on subtest start, a device sysfs file descriptor still before open. Suggested-by: Michał Winiarski Signed-off-by: Janusz Krzysztofik Reviewed-by: Michał Winiarski # v1 --- tests/core_hotunplug.c | 85 ++++++++++++++++-------------------------- 1 file changed, 33 insertions(+), 52 deletions(-) diff --git a/tests/core_hotunplug.c b/tests/core_hotunplug.c index 2884c3f77..1da0e5a9f 100644 --- a/tests/core_hotunplug.c +++ b/tests/core_hotunplug.c @@ -86,45 +86,31 @@ static int close_sysfs(int fd_sysfs_dev) return local_close(fd_sysfs_dev, "Device sysfs node close failed"); } -static void prepare_for_unbind(struct hotunplug *priv, char *buf, int buflen) +static void prepare(struct hotunplug *priv) { - int len; + const char *filter = igt_device_filter_get(0), *sysfs_path; - igt_assert(buflen); + igt_assert(filter); - priv->fd.sysfs_drv = openat(priv->fd.sysfs_dev, "device/driver", - O_DIRECTORY); - igt_assert_fd(priv->fd.sysfs_drv); - - len = readlinkat(priv->fd.sysfs_dev, "device", buf, buflen - 1); - buf[len] = '\0'; - priv->dev_bus_addr = strrchr(buf, '/'); + priv->dev_bus_addr = strrchr(filter, '/'); igt_assert(priv->dev_bus_addr++); - /* sysfs_dev no longer needed */ - priv->fd.sysfs_dev = close_sysfs(priv->fd.sysfs_dev); - igt_assert_eq(priv->fd.sysfs_dev, -1); -} + sysfs_path = strchr(filter, ':'); + igt_assert(sysfs_path++); -static void prepare(struct hotunplug *priv, char *buf, int buflen) -{ - /* assert device file descriptors closed cleanly on subtest start */ - igt_assert_eq(priv->fd.drm, -1); igt_assert_eq(priv->fd.sysfs_dev, -1); + priv->fd.sysfs_dev = open(sysfs_path, O_DIRECTORY); + igt_assert_fd(priv->fd.sysfs_dev); - priv->fd.drm = local_drm_open_driver("", " for subtest"); + priv->fd.sysfs_drv = openat(priv->fd.sysfs_dev, "driver", O_DIRECTORY); + igt_assert_fd(priv->fd.sysfs_drv); - priv->fd.sysfs_dev = igt_sysfs_open(priv->fd.drm); - igt_assert_fd(priv->fd.sysfs_dev); + priv->fd.sysfs_bus = openat(priv->fd.sysfs_dev, "subsystem/devices", + O_DIRECTORY); + igt_assert_fd(priv->fd.sysfs_bus); - if (buf) { - prepare_for_unbind(priv, buf, buflen); - } else { - /* prepare for bus rescan */ - priv->fd.sysfs_bus = openat(priv->fd.sysfs_dev, - "device/subsystem", O_DIRECTORY); - igt_assert_fd(priv->fd.sysfs_bus); - } + priv->fd.sysfs_dev = close_sysfs(priv->fd.sysfs_dev); + igt_assert_eq(priv->fd.sysfs_dev, -1); } /* Unbind the driver from the device */ @@ -137,8 +123,6 @@ static void driver_unbind(struct hotunplug *priv, const char *prefix) igt_sysfs_set(priv->fd.sysfs_drv, "unbind", priv->dev_bus_addr); igt_reset_timeout(); priv->failure = NULL; - - /* don't close fd.sysfs_drv, it will be used for driver rebinding */ } /* Re-bind the driver to the device */ @@ -151,18 +135,21 @@ static void driver_bind(struct hotunplug *priv) igt_sysfs_set(priv->fd.sysfs_drv, "bind", priv->dev_bus_addr); igt_reset_timeout(); priv->failure = NULL; - - close(priv->fd.sysfs_drv); } /* Remove (virtually unplug) the device from its bus */ static void device_unplug(struct hotunplug *priv, const char *prefix) { + igt_assert_eq(priv->fd.sysfs_dev, -1); + priv->fd.sysfs_dev = openat(priv->fd.sysfs_bus, priv->dev_bus_addr, + O_DIRECTORY); + igt_assert_fd(priv->fd.sysfs_dev); + igt_debug("%sunplugging the device\n", prefix); priv->failure = "Device unplug timeout!"; igt_set_timeout(60, priv->failure); - igt_sysfs_set(priv->fd.sysfs_dev, "device/remove", "1"); + igt_sysfs_set(priv->fd.sysfs_dev, "remove", "1"); igt_reset_timeout(); priv->failure = NULL; @@ -176,11 +163,9 @@ static void bus_rescan(struct hotunplug *priv) priv->failure = "Bus rescan timeout!"; igt_set_timeout(60, priv->failure); - igt_sysfs_set(priv->fd.sysfs_bus, "rescan", "1"); + igt_sysfs_set(priv->fd.sysfs_bus, "../rescan", "1"); igt_reset_timeout(); priv->failure = NULL; - - close(priv->fd.sysfs_bus); } static void healthcheck(struct hotunplug *priv) @@ -233,12 +218,6 @@ static void set_filter_from_device(int fd) static void unbind_rebind(struct hotunplug *priv) { - char buf[PATH_MAX]; - - prepare(priv, buf, sizeof(buf)); - - igt_debug("closing the device\n"); - priv->fd.drm = close_device(priv->fd.drm); igt_assert_eq(priv->fd.drm, -1); driver_unbind(priv, ""); @@ -250,10 +229,6 @@ static void unbind_rebind(struct hotunplug *priv) static void unplug_rescan(struct hotunplug *priv) { - prepare(priv, NULL, 0); - - igt_debug("closing the device\n"); - priv->fd.drm = close_device(priv->fd.drm); igt_assert_eq(priv->fd.drm, -1); device_unplug(priv, ""); @@ -265,9 +240,8 @@ static void unplug_rescan(struct hotunplug *priv) static void hotunbind_lateclose(struct hotunplug *priv) { - char buf[PATH_MAX]; - - prepare(priv, buf, sizeof(buf)); + igt_assert_eq(priv->fd.drm, -1); + priv->fd.drm = local_drm_open_driver("", " for hotunbind"); driver_unbind(priv, "hot "); @@ -281,7 +255,8 @@ static void hotunbind_lateclose(struct hotunplug *priv) static void hotunplug_lateclose(struct hotunplug *priv) { - prepare(priv, NULL, 0); + igt_assert_eq(priv->fd.drm, -1); + priv->fd.drm = local_drm_open_driver("", " for hotunplug"); device_unplug(priv, "hot "); @@ -317,6 +292,8 @@ igt_main set_filter_from_device(fd_drm); igt_assert_eq(close_device(fd_drm), -1); + + prepare(&priv); } igt_describe("Check if the driver can be cleanly unbound from a device believed to be closed"); @@ -344,6 +321,10 @@ igt_main igt_subtest("hotunplug-lateclose") hotunplug_lateclose(&priv); - igt_fixture + igt_fixture { post_healthcheck(&priv); + + close(priv.fd.sysfs_bus); + close(priv.fd.sysfs_drv); + } }