From patchwork Fri May 27 10:50:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Tvrtko Ursulin X-Patchwork-Id: 12863267 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 A6E3EC433EF for ; Fri, 27 May 2022 10:50:54 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id CA9E310E2D7; Fri, 27 May 2022 10:50:53 +0000 (UTC) Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by gabe.freedesktop.org (Postfix) with ESMTPS id BBF7C10E066; Fri, 27 May 2022 10:50:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1653648651; x=1685184651; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=CJhT6tZ4Cs0BuvfUU3fiJpIcTj6HeGMX5ZF3A8n8wpk=; b=eF8jyN+bYV6DTB9rpGyELtFMqvrm6UO+gQ6Np2ZkseVPwKUTa5FI9LDA lr8KBKRe54Spbh8njfEeZIY0JlkLYrzBZvzfOHd9F9w4N8H/SDYju2oh2 0VcS/lbQOCL7lumwXd4USEwAyWyjzhDZpIU3bneb40DlKVffyYkdg4i8W WUl9AupNO1PQTVaOQOnmd2t4h7x5+fz3O/zJZXsYc1L9yzlY8V4Zl6eq8 6qXD9OA8eLtk/R4ba2zZ3mRCRvkYONqw/ISeezNz/hQC8M5gAl04q9m8r Dqh1FqCk/+B01GN6I4JpQPRxAXHCkE4zDZSb9nJ56k6TGMSe+Ny3P01XW w==; X-IronPort-AV: E=McAfee;i="6400,9594,10359"; a="262066131" X-IronPort-AV: E=Sophos;i="5.91,255,1647327600"; d="scan'208";a="262066131" Received: from orsmga006.jf.intel.com ([10.7.209.51]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 May 2022 03:50:51 -0700 X-IronPort-AV: E=Sophos;i="5.91,255,1647327600"; d="scan'208";a="550155687" Received: from dnanduri-mobl.ger.corp.intel.com (HELO tursulin-mobl2.home) ([10.213.215.174]) by orsmga006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 May 2022 03:50:50 -0700 From: Tvrtko Ursulin To: igt-dev@lists.freedesktop.org Date: Fri, 27 May 2022 11:50:40 +0100 Message-Id: <20220527105042.2233128-1-tvrtko.ursulin@linux.intel.com> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 Subject: [Intel-gfx] [PATCH i-g-t 1/3] lib/igt_device_scan: Free filtered devices in igt_devices_free 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 Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" From: Tvrtko Ursulin Fix a possible oversight. Signed-off-by: Tvrtko Ursulin Reviewed-by: Zbigniew Kempczyński --- lib/igt_device_scan.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/igt_device_scan.c b/lib/igt_device_scan.c index 3c23fe0eb520..a30433ae2cff 100644 --- a/lib/igt_device_scan.c +++ b/lib/igt_device_scan.c @@ -814,6 +814,11 @@ void igt_devices_free(void) igt_device_free(dev); free(dev); } + + igt_list_for_each_entry_safe(dev, tmp, &igt_devs.filtered, link) { + igt_list_del(&dev->link); + free(dev); + } } /** From patchwork Fri May 27 10:50:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tvrtko Ursulin X-Patchwork-Id: 12863268 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 93BBCC433F5 for ; Fri, 27 May 2022 10:50:57 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 039D010EF35; Fri, 27 May 2022 10:50:54 +0000 (UTC) Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by gabe.freedesktop.org (Postfix) with ESMTPS id 95CA710E066; Fri, 27 May 2022 10:50:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1653648652; x=1685184652; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Wsuymuwxlq2BN9bs/rjeDMAwrcGerv+qNnnnby9Xed8=; b=K3Pheesb70drROIE1LKoE4szu0AtigbpVdua6XrsYrPK/dm/3nqVPdy/ rmCspYjnV3MPmeNmQi6UOJx/IXNL1JlpSSADAhS1Pttz74etZSwmXXpdo PKzh7VJisZnS7fLUf4azxSD5474bBSvZZB+GbliuhBBms2SP18+KIFuKf kIKd0aTFkEHAVWQKSeD5wXEvKPzuJIPUbsZy2IuKXgILz1OGGcSyTLuaF BfqKsmqMwzFTBAXaVkjLz5yczspd1XUiHgpfUdI54MvZibNIN3OF6o1zC GGMLyVEZQzC8aCLU59KxEoeiKVRIKnnwIxOxbJKxj+zgmg25yMU2eDUKc Q==; X-IronPort-AV: E=McAfee;i="6400,9594,10359"; a="262066135" X-IronPort-AV: E=Sophos;i="5.91,255,1647327600"; d="scan'208";a="262066135" Received: from orsmga006.jf.intel.com ([10.7.209.51]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 May 2022 03:50:52 -0700 X-IronPort-AV: E=Sophos;i="5.91,255,1647327600"; d="scan'208";a="550155692" Received: from dnanduri-mobl.ger.corp.intel.com (HELO tursulin-mobl2.home) ([10.213.215.174]) by orsmga006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 May 2022 03:50:51 -0700 From: Tvrtko Ursulin To: igt-dev@lists.freedesktop.org Date: Fri, 27 May 2022 11:50:41 +0100 Message-Id: <20220527105042.2233128-2-tvrtko.ursulin@linux.intel.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220527105042.2233128-1-tvrtko.ursulin@linux.intel.com> References: <20220527105042.2233128-1-tvrtko.ursulin@linux.intel.com> MIME-Version: 1.0 Subject: [Intel-gfx] [PATCH i-g-t 2/3] lib/drm_fdinfo: Ensure buffer is null terminated 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 Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" From: Tvrtko Ursulin Ensure buffer is null terminated at the point where the read ended and not at the end of the whole buffer. Otherwise string parsing can stray into un-initialised memory. Signed-off-by: Tvrtko Ursulin Reviewed-by: Petri Latvala --- lib/igt_drm_fdinfo.c | 8 ++++---- lib/igt_drm_fdinfo.h | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/igt_drm_fdinfo.c b/lib/igt_drm_fdinfo.c index b422f67a4ace..250d9e8917f2 100644 --- a/lib/igt_drm_fdinfo.c +++ b/lib/igt_drm_fdinfo.c @@ -44,12 +44,12 @@ static size_t read_fdinfo(char *buf, const size_t sz, int at, const char *name) if (fd < 0) return 0; - buf[sz - 1] = 0; - count = read(fd, buf, sz); - buf[sz - 1] = 0; + count = read(fd, buf, sz - 1); + if (count > 0) + buf[count - 1] = 0; close(fd); - return count; + return count > 0 ? count : 0; } static int parse_engine(char *line, struct drm_client_fdinfo *info, diff --git a/lib/igt_drm_fdinfo.h b/lib/igt_drm_fdinfo.h index 5db63e28b07e..8759471615bd 100644 --- a/lib/igt_drm_fdinfo.h +++ b/lib/igt_drm_fdinfo.h @@ -46,7 +46,7 @@ struct drm_client_fdinfo { * igt_parse_drm_fdinfo: Parses the drm fdinfo file * * @drm_fd: DRM file descriptor - * @info: Structure to populate with read data + * @info: Structure to populate with read data. Must be zeroed. * * Returns the number of valid drm fdinfo keys found or zero if not all * mandatory keys were present or no engines found. @@ -58,7 +58,7 @@ unsigned int igt_parse_drm_fdinfo(int drm_fd, struct drm_client_fdinfo *info); * * @dir: File descriptor pointing to /proc/pid/fdinfo directory * @fd: String representation of the file descriptor number to parse. - * @info: Structure to populate with read data + * @info: Structure to populate with read data. Must be zeroed. * * Returns the number of valid drm fdinfo keys found or zero if not all * mandatory keys were present or no engines found. From patchwork Fri May 27 10:50:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Tvrtko Ursulin X-Patchwork-Id: 12863269 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 4FA86C433F5 for ; Fri, 27 May 2022 10:51:00 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id B46E610EF4E; Fri, 27 May 2022 10:50:58 +0000 (UTC) Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by gabe.freedesktop.org (Postfix) with ESMTPS id 1B76310EF4E; Fri, 27 May 2022 10:50:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1653648654; x=1685184654; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=FSiKACiRGQtkHjcw1RluOzxMa/YKxVziVMudDTg5gec=; b=QgP5Dtc1dyshoAie5BhjDDAGZ8c159AfMGHZ3yEs8AbqWY7VGP1uKCdL hcjidS/e57ayjMBmROCT9wAe2Dowh29RpnUjY6gOb3gqwLjh55fP9U8Te 9UwN3ed9ixEfk3PU42yZYGjnyEwis/9c8WbUo3xYRjFOiAHKNdMzb2vU3 MRVom+gRnM19g7efumPKYSvrC/KTcpHUY6hOS6Tbm2zQEhBXd0k6mdpHI B0L+ZkU0ILNnx7TT5+xXkbxHaNc475mE3jXhHtbrBjq0MNlrCJiC9VKYz HUR5t66BT6S/O9WvvFzA/R05HiGtZZYdL2qmoz+o+ltkLtJTCPtQOn/A3 w==; X-IronPort-AV: E=McAfee;i="6400,9594,10359"; a="262066139" X-IronPort-AV: E=Sophos;i="5.91,255,1647327600"; d="scan'208";a="262066139" Received: from orsmga006.jf.intel.com ([10.7.209.51]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 May 2022 03:50:53 -0700 X-IronPort-AV: E=Sophos;i="5.91,255,1647327600"; d="scan'208";a="550155699" Received: from dnanduri-mobl.ger.corp.intel.com (HELO tursulin-mobl2.home) ([10.213.215.174]) by orsmga006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 May 2022 03:50:52 -0700 From: Tvrtko Ursulin To: igt-dev@lists.freedesktop.org Date: Fri, 27 May 2022 11:50:42 +0100 Message-Id: <20220527105042.2233128-3-tvrtko.ursulin@linux.intel.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220527105042.2233128-1-tvrtko.ursulin@linux.intel.com> References: <20220527105042.2233128-1-tvrtko.ursulin@linux.intel.com> MIME-Version: 1.0 Subject: [Intel-gfx] [PATCH i-g-t 3/3] intel_gpu_top: Free all memory on exit 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 Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" From: Tvrtko Ursulin Be nice and explicitly free all memory on exit. Also fix a Valgrind reported unitilised conditional jump. Signed-off-by: Tvrtko Ursulin Cc: Petri Latvala Reviewed-by: Petri Latvala --- tools/intel_gpu_top.c | 51 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 9 deletions(-) diff --git a/tools/intel_gpu_top.c b/tools/intel_gpu_top.c index 26986a822bb7..997aff582ff7 100644 --- a/tools/intel_gpu_top.c +++ b/tools/intel_gpu_top.c @@ -437,6 +437,36 @@ static struct engines *discover_engines(char *device) return engines; } +static void free_engines(struct engines *engines) +{ + struct pmu_counter **pmu, *free_list[] = { + &engines->r_gpu, + &engines->r_pkg, + &engines->imc_reads, + &engines->imc_writes, + NULL + }; + unsigned int i; + + for (pmu = &free_list[0]; *pmu; pmu++) { + if ((*pmu)->present) + free((char *)(*pmu)->units); + } + + for (i = 0; i < engines->num_engines; i++) { + struct engine *engine = engine_ptr(engines, i); + + free((char *)engine->name); + free((char *)engine->short_name); + free((char *)engine->display_name); + } + + closedir(engines->root); + + free(engines->class); + free(engines); +} + #define _open_pmu(type, cnt, pmu, fd) \ ({ \ int fd__; \ @@ -1073,7 +1103,7 @@ static size_t freadat2buf(char *buf, const size_t sz, DIR *at, const char *name) return count; } -static struct clients *scan_clients(struct clients *clients) +static struct clients *scan_clients(struct clients *clients, bool display) { struct dirent *proc_dent; struct client *c; @@ -1181,7 +1211,7 @@ next: break; } - return display_clients(clients); + return display ? display_clients(clients) : clients; } static const char *bars[] = { " ", "▏", "▎", "▍", "▌", "▋", "▊", "▉", "█" }; @@ -2391,7 +2421,7 @@ static void process_stdin(unsigned int timeout_us) static bool has_drm_fdinfo(const struct igt_device_card *card) { - struct drm_client_fdinfo info; + struct drm_client_fdinfo info = { }; unsigned int cnt; int fd; @@ -2572,7 +2602,7 @@ int main(int argc, char **argv) } pmu_sample(engines); - scan_clients(clients); + scan_clients(clients, false); codename = igt_device_get_pretty_name(&card, false); while (!stop_top) { @@ -2599,7 +2629,7 @@ int main(int argc, char **argv) pmu_sample(engines); t = (double)(engines->ts.cur - engines->ts.prev) / 1e9; - disp_clients = scan_clients(clients); + disp_clients = scan_clients(clients, true); if (stop_top) break; @@ -2649,21 +2679,24 @@ int main(int argc, char **argv) pops->close_struct(); } - if (stop_top) - break; - if (disp_clients != clients) free_clients(disp_clients); + if (stop_top) + break; + if (output_mode == INTERACTIVE) process_stdin(period_us); else usleep(period_us); } + if (clients) + free_clients(clients); + free(codename); err: - free(engines); + free_engines(engines); free(pmu_device); exit: igt_devices_free();