From owner-svn-src-stable@freebsd.org Tue Aug 4 12:04:48 2020 Return-Path: Delivered-To: svn-src-stable@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 7FF1937226F; Tue, 4 Aug 2020 12:04:48 +0000 (UTC) (envelope-from freqlabs@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4BLYN02pGCz4H0h; Tue, 4 Aug 2020 12:04:48 +0000 (UTC) (envelope-from freqlabs@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4373524F58; Tue, 4 Aug 2020 12:04:48 +0000 (UTC) (envelope-from freqlabs@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 074C4m6G020736; Tue, 4 Aug 2020 12:04:48 GMT (envelope-from freqlabs@FreeBSD.org) Received: (from freqlabs@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 074C4lCp020734; Tue, 4 Aug 2020 12:04:47 GMT (envelope-from freqlabs@FreeBSD.org) Message-Id: <202008041204.074C4lCp020734@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: freqlabs set sender to freqlabs@FreeBSD.org using -f From: Ryan Moeller Date: Tue, 4 Aug 2020 12:04:47 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org Subject: svn commit: r363831 - in stable/12: lib/libpmc sys/dev/hwpmc X-SVN-Group: stable-12 X-SVN-Commit-Author: freqlabs X-SVN-Commit-Paths: in stable/12: lib/libpmc sys/dev/hwpmc X-SVN-Commit-Revision: 363831 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-stable@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: SVN commit messages for all the -stable branches of the src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 04 Aug 2020 12:04:48 -0000 Author: freqlabs Date: Tue Aug 4 12:04:47 2020 New Revision: 363831 URL: https://svnweb.freebsd.org/changeset/base/363831 Log: MFC r363630: libpmc: Use known pmc_cpuid buffer size Use the existing PMC_CPUID_LEN to size pmc_cpuid in the kernel and various buffers for reading it in libpmc. This avoids some extra syscalls and malloc/frees. While in here, use strlcpy to copy a user-provided cpuid string instead of memcpy, to make sure we terminate the buffer. Reviewed by: mav Sponsored by: iXsystems, Inc. Differential Revision: https://reviews.freebsd.org/D25679 Modified: stable/12/lib/libpmc/libpmc_pmu_util.c stable/12/sys/dev/hwpmc/hwpmc_mod.c Directory Properties: stable/12/ (props changed) Modified: stable/12/lib/libpmc/libpmc_pmu_util.c ============================================================================== --- stable/12/lib/libpmc/libpmc_pmu_util.c Tue Aug 4 11:13:13 2020 (r363830) +++ stable/12/lib/libpmc/libpmc_pmu_util.c Tue Aug 4 12:04:47 2020 (r363831) @@ -30,6 +30,7 @@ #include #include +#include #include #include #include @@ -89,20 +90,13 @@ static struct pmu_alias pmu_amd_alias_table[] = { static pmu_mfr_t pmu_events_mfr(void) { - char *buf; - size_t s; + char buf[PMC_CPUID_LEN]; + size_t s = sizeof(buf); pmu_mfr_t mfr; - if (sysctlbyname("kern.hwpmc.cpuid", (void *)NULL, &s, + if (sysctlbyname("kern.hwpmc.cpuid", buf, &s, (void *)NULL, 0) == -1) return (PMU_INVALID); - if ((buf = malloc(s + 1)) == NULL) - return (PMU_INVALID); - if (sysctlbyname("kern.hwpmc.cpuid", buf, &s, - (void *)NULL, 0) == -1) { - free(buf); - return (PMU_INVALID); - } if (strcasestr(buf, "AuthenticAMD") != NULL || strcasestr(buf, "HygonGenuine") != NULL) mfr = PMU_AMD; @@ -110,7 +104,6 @@ pmu_events_mfr(void) mfr = PMU_INTEL; else mfr = PMU_INVALID; - free(buf); return (mfr); } @@ -169,17 +162,14 @@ pmu_events_map_get(const char *cpuid) { regex_t re; regmatch_t pmatch[1]; - size_t s; - char buf[64]; + char buf[PMC_CPUID_LEN]; + size_t s = sizeof(buf); int match; const struct pmu_events_map *pme; if (cpuid != NULL) { - memcpy(buf, cpuid, 64); + strlcpy(buf, cpuid, s); } else { - if (sysctlbyname("kern.hwpmc.cpuid", (void *)NULL, &s, - (void *)NULL, 0) == -1) - return (NULL); if (sysctlbyname("kern.hwpmc.cpuid", buf, &s, (void *)NULL, 0) == -1) return (NULL); Modified: stable/12/sys/dev/hwpmc/hwpmc_mod.c ============================================================================== --- stable/12/sys/dev/hwpmc/hwpmc_mod.c Tue Aug 4 11:13:13 2020 (r363830) +++ stable/12/sys/dev/hwpmc/hwpmc_mod.c Tue Aug 4 12:04:47 2020 (r363831) @@ -304,7 +304,7 @@ static int pmc_callchaindepth = PMC_CALLCHAIN_DEPTH; SYSCTL_INT(_kern_hwpmc, OID_AUTO, callchaindepth, CTLFLAG_RDTUN, &pmc_callchaindepth, 0, "depth of call chain records"); -char pmc_cpuid[64]; +char pmc_cpuid[PMC_CPUID_LEN]; SYSCTL_STRING(_kern_hwpmc, OID_AUTO, cpuid, CTLFLAG_RD, pmc_cpuid, 0, "cpu version string"); #ifdef HWPMC_DEBUG