From patchwork Tue Feb 22 11:41:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Janusz Krzysztofik X-Patchwork-Id: 12754931 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 3B38DC433F5 for ; Tue, 22 Feb 2022 11:41:29 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 6EA8010E737; Tue, 22 Feb 2022 11:41:28 +0000 (UTC) Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) by gabe.freedesktop.org (Postfix) with ESMTPS id 7D8D710E737; Tue, 22 Feb 2022 11:41:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1645530087; x=1677066087; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=FN+wIWYXEXV/7UFJu9EhHGZanN+Dq1lhK1ysv801BR8=; b=RA4hpkvwm+jKd+RDu3NMuH3dv9vR5r9bLgobZo2rrWpf9Q34jMiukdkj ZN0UhR+vCBj4zdGrChLNwgmbG5CEVth94i/o0x3icKJOpiugK9pZQ6iCp KgSHzC5LkyfrSQvr41AWR69Jk07heCPh7I6t4tLY4wosAscpowAT4unwv TA4Zmmb78cv4rvGpnYUrPu0Euhh4bRqsq6rOBBqfkgtsl7N4BBA4oj4kd z838uRAH5Veh746iuvH9+TfK6dbHo5lg0qZbhXnq9is22lhASR/R2ZhbM 9dMNlUAoau0LJG+r7RaAYADzoT2KustwqlWX6DxyOWwpVORc9/tUXYL5B g==; X-IronPort-AV: E=McAfee;i="6200,9189,10265"; a="312412377" X-IronPort-AV: E=Sophos;i="5.88,387,1635231600"; d="scan'208";a="312412377" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Feb 2022 03:41:26 -0800 X-IronPort-AV: E=Sophos;i="5.88,387,1635231600"; d="scan'208";a="547708630" Received: from jkrzyszt-mobl1.ger.corp.intel.com ([10.213.20.194]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Feb 2022 03:41:25 -0800 From: Janusz Krzysztofik To: igt-dev@lists.freedesktop.org Date: Tue, 22 Feb 2022 12:41:04 +0100 Message-Id: <20220222114106.75641-1-janusz.krzysztofik@linux.intel.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Subject: [Intel-gfx] [PATCH i-g-t 1/2] lib: Use safe wrappers around libpciaccess initialization functions 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: intel-gfx@lists.freedesktop.org, Chris Wilson Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" Multiple calls to igt functions using pci_system_init() provided by libpciaccess result in memory leaking if not followed by its counterpart pci_system_cleanup() before next use. On the other hand, calling pci_system_cleanup() can affect other users which still depend on global data initialized by pci_system_init(). Introduce safe IGT wrappers around those libpciaccess functions and use those wrappers in IGT library and tests. Signed-off-by: Janusz Krzysztofik Cc: Chris Wilson --- lib/igt_core.c | 20 ++++++++++++++++++++ lib/igt_core.h | 28 ++++++++++++++++++++++++++++ lib/igt_device.c | 2 +- lib/intel_chipset.c | 2 +- 4 files changed, 50 insertions(+), 2 deletions(-) diff --git a/lib/igt_core.c b/lib/igt_core.c index ab27a24d5a..88e2f7d01e 100644 --- a/lib/igt_core.c +++ b/lib/igt_core.c @@ -3070,3 +3070,23 @@ err: return -1; } + +/* IGT wrappers around libpciaccess init/cleanup functions */ + +static void pci_system_exit_handler(int sig) +{ + pci_system_cleanup(); +} + +static void __pci_system_init(void) +{ + if (!igt_warn_on_f(pci_system_init(), "Could not initialize libpciaccess global data\n")) + igt_install_exit_handler(pci_system_exit_handler); +} + +int igt_pci_system_init(void) +{ + static pthread_once_t pci_system_init_once_control = PTHREAD_ONCE_INIT; + + return pthread_once(&pci_system_init_once_control, __pci_system_init); +} diff --git a/lib/igt_core.h b/lib/igt_core.h index 0aad161da5..78dc6202ce 100644 --- a/lib/igt_core.h +++ b/lib/igt_core.h @@ -1452,4 +1452,32 @@ void igt_kmsg(const char *format, ...); #define for_if(expr__) if (!(expr__)) {} else +/** + * igt_pci_system_init: + * IGT wrapper around pci_system_init() + * + * Runs pci_system_init() and installs pci_system_cleanup() as IGT exit handler when + * called first per thread, subsequent calls are noop. Tests should use this wrapper + * instead of pci_system_init() to avoid memory leaking which happens each time a call + * to pci_system_init() is repeated not preceded by pci_system_cleanup() (may easily + * happen in consequence of long jumps performed by IGT flow control functions). + * + * Return value: equal return value of pthread_once() (return value of pci_system_init() + * can't be passed through pthread_once()) + */ +int igt_pci_system_init(void); + +/** + * igt_pci_system_cleanup(): + * IGT replacement for pci_system_cleanup() + * + * For use in IGT library and tests to avoid destroying libpciaccess global data. + * Direct calls to pci_system_cleanup() should be either dropped or replaced with this + * wrapper (for code clarity), otherwise subsequent access to libpciaccess global data + * may be lost unless preceded by direct call to pci_system_init() (not recommended). + */ +static inline void igt_pci_system_cleanup(void) +{ +} + #endif /* IGT_CORE_H */ diff --git a/lib/igt_device.c b/lib/igt_device.c index 07bb0a0d41..c50bf4a1f7 100644 --- a/lib/igt_device.c +++ b/lib/igt_device.c @@ -193,7 +193,7 @@ static struct pci_device *__igt_device_get_pci_device(int fd) return NULL; } - if (pci_system_init()) { + if (igt_pci_system_init()) { igt_warn("Couldn't initialize PCI system\n"); return NULL; } diff --git a/lib/intel_chipset.c b/lib/intel_chipset.c index 4748a3fb85..efb6f17714 100644 --- a/lib/intel_chipset.c +++ b/lib/intel_chipset.c @@ -75,7 +75,7 @@ intel_get_pci_device(void) struct pci_device *pci_dev; int error; - error = pci_system_init(); + error = igt_pci_system_init(); igt_fail_on_f(error != 0, "Couldn't initialize PCI system\n"); From patchwork Tue Feb 22 11:41:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Janusz Krzysztofik X-Patchwork-Id: 12754932 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 6B019C433EF for ; Tue, 22 Feb 2022 11:41:33 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id BF24B10E8AA; Tue, 22 Feb 2022 11:41:32 +0000 (UTC) Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) by gabe.freedesktop.org (Postfix) with ESMTPS id D253010E89A; Tue, 22 Feb 2022 11:41:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1645530088; x=1677066088; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=X3R+PBtl0Yegbpkmh/4yY7FvCBy0MB2PUjrrtFoD+so=; b=cxjvqUvA8TvjMKirlldERawph1f7bGayRylYYVu/8jNPHjxkTslakUIZ rOkP4gOl1YYmLh/HrjoxDunMNNBB8ZmsGLiYElXmObuiWcQRfTCX2cAQ5 +lURPNFc5yk7y/zoQkL2gUBCw812kouEyeR8au3Aw2EbD5STk+pISrxDT 1Gyl0lG8/x/+wANxi5evTvyeBTSbuC0QisqWFiSVt4X93iZ8jP7hwzsjg +h/mydaSnVHsUaziUlEoeB+rP6AsWAhIDGfLmH1IUkJkofkSE8odaE0O4 R25am+fE/QG2Rw/JyawVftcTU14AH81yxyG51EP1SrBGj+Tk3+5CBUPf+ g==; X-IronPort-AV: E=McAfee;i="6200,9189,10265"; a="312412387" X-IronPort-AV: E=Sophos;i="5.88,387,1635231600"; d="scan'208";a="312412387" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Feb 2022 03:41:28 -0800 X-IronPort-AV: E=Sophos;i="5.88,387,1635231600"; d="scan'208";a="547708643" Received: from jkrzyszt-mobl1.ger.corp.intel.com ([10.213.20.194]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Feb 2022 03:41:27 -0800 From: Janusz Krzysztofik To: igt-dev@lists.freedesktop.org Date: Tue, 22 Feb 2022 12:41:05 +0100 Message-Id: <20220222114106.75641-2-janusz.krzysztofik@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220222114106.75641-1-janusz.krzysztofik@linux.intel.com> References: <20220222114106.75641-1-janusz.krzysztofik@linux.intel.com> MIME-Version: 1.0 Subject: [Intel-gfx] [PATCH i-g-t 2/2] lib/igt_device: Add support for accessing unbound VF PCI devices 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: intel-gfx@lists.freedesktop.org, Chris Wilson Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" The library provides igt_device_get_pci_device() function that allows to get access to a PCI device from an open DRM device file descriptor. It can be used on VF devices as long as a DRM driver is bound to them. However, SR-IOV tests may want to exercise VF PCI devices created by a PF without binding any DRM driver to them. While keeping the API of igt_device_get_pci_device() untouched, extend API of its underlying helper __igt_device_get_pci_device() with an extra argument for specifying VF ID of the requested PCI device and expose this function as public. v2: refresh on top of IGT libpciaccess wrappers and drop previously added but no longer needed error unwind path and recommendations for users on calling pci_system_cleanup() after use (Chris), - fix incorrect validation of snprintf() result and misaligned formatting of igt_warn_on_f() arguments. Signed-off-by: Janusz Krzysztofik Cc: Chris Wilson --- lib/igt_device.c | 34 ++++++++++++++++++++++++++++------ lib/igt_device.h | 1 + 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/lib/igt_device.c b/lib/igt_device.c index c50bf4a1f7..1603bf351c 100644 --- a/lib/igt_device.c +++ b/lib/igt_device.c @@ -149,9 +149,9 @@ struct igt_pci_addr { unsigned int function; }; -static int igt_device_get_pci_addr(int fd, struct igt_pci_addr *pci) +static int igt_device_get_pci_addr(int fd, unsigned int vf_id, struct igt_pci_addr *pci) { - char path[IGT_DEV_PATH_LEN]; + char link[20], path[IGT_DEV_PATH_LEN]; char *buf; int sysfs; int len; @@ -159,11 +159,21 @@ static int igt_device_get_pci_addr(int fd, struct igt_pci_addr *pci) if (!igt_device_is_pci(fd)) return -ENODEV; + if (vf_id) + len = snprintf(link, sizeof(link), "device/virtfn%u", vf_id - 1); + else + len = snprintf(link, sizeof(link), "device"); + if (igt_warn_on_f(len >= sizeof(link), + "IGT bug: insufficient buffer space for rendering PCI device link name\n")) + return -ENOSPC; + else if (igt_debug_on_f(len < 0, "unexpected failure from snprintf()\n")) + return len; + sysfs = igt_sysfs_open(fd); if (sysfs == -1) return -ENOENT; - len = readlinkat(sysfs, "device", path, sizeof(path) - 1); + len = readlinkat(sysfs, link, path, sizeof(path) - 1); close(sysfs); if (len == -1) return -ENOENT; @@ -183,12 +193,24 @@ static int igt_device_get_pci_addr(int fd, struct igt_pci_addr *pci) return 0; } -static struct pci_device *__igt_device_get_pci_device(int fd) +/** + * __igt_device_get_pci_device: + * + * @fd: DRM device file descriptor + * @vf_id: virtual function number (0 if native or PF) + * + * Looks up the graphics pci device using libpciaccess. + * unless an error occurs and NULL is returned. + * + * Returns: + * The pci_device, NULL on any failures. + */ +struct pci_device *__igt_device_get_pci_device(int fd, unsigned int vf_id) { struct igt_pci_addr pci_addr; struct pci_device *pci_dev; - if (igt_device_get_pci_addr(fd, &pci_addr)) { + if (igt_device_get_pci_addr(fd, vf_id, &pci_addr)) { igt_warn("Unable to find device PCI address\n"); return NULL; } @@ -231,7 +253,7 @@ struct pci_device *igt_device_get_pci_device(int fd) { struct pci_device *pci_dev; - pci_dev = __igt_device_get_pci_device(fd); + pci_dev = __igt_device_get_pci_device(fd, 0); igt_require(pci_dev); return pci_dev; diff --git a/lib/igt_device.h b/lib/igt_device.h index 278ba7a9b3..1aaa840e25 100644 --- a/lib/igt_device.h +++ b/lib/igt_device.h @@ -33,5 +33,6 @@ void igt_device_drop_master(int fd); int igt_device_get_card_index(int fd); struct pci_device *igt_device_get_pci_device(int fd); +struct pci_device *__igt_device_get_pci_device(int fd, unsigned int vf_id); #endif /* __IGT_DEVICE_H__ */