git: 90a6ea5ca0b1 - main - hwpmc: tidy pcd_finalize methods

From: Mitchell Horne <mhorne_at_FreeBSD.org>
Date: Wed, 18 Oct 2023 18:05:58 UTC
The branch main has been updated by mhorne:

URL: https://cgit.FreeBSD.org/src/commit/?id=90a6ea5ca0b1b578e265d2479511bd04347f9510

commit 90a6ea5ca0b1b578e265d2479511bd04347f9510
Author:     Mitchell Horne <mhorne@FreeBSD.org>
AuthorDate: 2023-10-18 17:50:49 +0000
Commit:     Mitchell Horne <mhorne@FreeBSD.org>
CommitDate: 2023-10-18 18:05:44 +0000

    hwpmc: tidy pcd_finalize methods
    
    Make them a little bit more consistent with one another in terms of what
    is done:
     - Add/reduce assertions to what is most useful: a loop to ensure
       pcpu_fini freed everything
     - Add PMCDBG trace entries
    
    The exception is the dmc620/cmn600 classes, which behave a little
    differently, so leave them untouched.
    
    Reviewed by:    jkoshy
    MFC after:      1 week
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D41270
---
 sys/dev/hwpmc/hwpmc_amd.c     | 32 ++++----------------------------
 sys/dev/hwpmc/hwpmc_arm64.c   |  4 ++++
 sys/dev/hwpmc/hwpmc_armv7.c   |  4 ++++
 sys/dev/hwpmc/hwpmc_core.c    |  4 ++++
 sys/dev/hwpmc/hwpmc_powerpc.c |  5 +++++
 sys/dev/hwpmc/hwpmc_soft.c    |  9 ++-------
 sys/dev/hwpmc/hwpmc_tsc.c     | 10 ++--------
 sys/dev/hwpmc/hwpmc_uncore.c  |  4 ++++
 8 files changed, 29 insertions(+), 43 deletions(-)

diff --git a/sys/dev/hwpmc/hwpmc_amd.c b/sys/dev/hwpmc/hwpmc_amd.c
index fbbaf92a1547..000958eb9945 100644
--- a/sys/dev/hwpmc/hwpmc_amd.c
+++ b/sys/dev/hwpmc/hwpmc_amd.c
@@ -1173,37 +1173,13 @@ pmc_amd_initialize(void)
 void
 pmc_amd_finalize(struct pmc_mdep *md)
 {
-#if	defined(INVARIANTS)
-	int classindex, i, ncpus, pmcclass;
-#endif
+	PMCDBG0(MDP, INI, 1, "amd-finalize");
 
 	pmc_tsc_finalize(md);
 
-	KASSERT(amd_pcpu != NULL, ("[amd,%d] NULL per-cpu array pointer",
-	    __LINE__));
-
-#if	defined(INVARIANTS)
-	switch (md->pmd_cputype) {
-#if	defined(__i386__)
-	case PMC_CPU_AMD_K7:
-		classindex = PMC_MDEP_CLASS_INDEX_K7;
-		pmcclass = PMC_CLASS_K7;
-		break;
-#endif
-	default:
-		classindex = PMC_MDEP_CLASS_INDEX_K8;
-		pmcclass = PMC_CLASS_K8;
-	}
-
-	KASSERT(md->pmd_classdep[classindex].pcd_class == pmcclass,
-	    ("[amd,%d] pmc class mismatch", __LINE__));
-
-	ncpus = pmc_cpu_max();
-
-	for (i = 0; i < ncpus; i++)
-		KASSERT(amd_pcpu[i] == NULL, ("[amd,%d] non-null pcpu",
-		    __LINE__));
-#endif
+	for (int i = 0; i < pmc_cpu_max(); i++)
+		KASSERT(amd_pcpu[i] == NULL,
+		    ("[amd,%d] non-null pcpu cpu %d", __LINE__, i));
 
 	free(amd_pcpu, M_PMC);
 	amd_pcpu = NULL;
diff --git a/sys/dev/hwpmc/hwpmc_arm64.c b/sys/dev/hwpmc/hwpmc_arm64.c
index 9a5debb8016b..4c38a4ae6183 100644
--- a/sys/dev/hwpmc/hwpmc_arm64.c
+++ b/sys/dev/hwpmc/hwpmc_arm64.c
@@ -611,5 +611,9 @@ pmc_arm64_finalize(struct pmc_mdep *md)
 {
 	PMCDBG0(MDP, INI, 1, "arm64-finalize");
 
+	for (int i = 0; i < pmc_cpu_max(); i++)
+		KASSERT(arm64_pcpu[i] == NULL,
+		    ("[arm64,%d] non-null pcpu cpu %d", __LINE__, i));
+
 	free(arm64_pcpu, M_PMC);
 }
diff --git a/sys/dev/hwpmc/hwpmc_armv7.c b/sys/dev/hwpmc/hwpmc_armv7.c
index 5bb6c6fce291..1488af2ae54e 100644
--- a/sys/dev/hwpmc/hwpmc_armv7.c
+++ b/sys/dev/hwpmc/hwpmc_armv7.c
@@ -535,5 +535,9 @@ pmc_armv7_finalize(struct pmc_mdep *md)
 {
 	PMCDBG0(MDP, INI, 1, "armv7-finalize");
 
+	for (int i = 0; i < pmc_cpu_max(); i++)
+		KASSERT(armv7_pcpu[i] == NULL,
+		    ("[armv7,%d] non-null pcpu cpu %d", __LINE__, i));
+
 	free(armv7_pcpu, M_PMC);
 }
diff --git a/sys/dev/hwpmc/hwpmc_core.c b/sys/dev/hwpmc/hwpmc_core.c
index 3829a03eb729..ddaff0b5955e 100644
--- a/sys/dev/hwpmc/hwpmc_core.c
+++ b/sys/dev/hwpmc/hwpmc_core.c
@@ -1260,6 +1260,10 @@ pmc_core_finalize(struct pmc_mdep *md)
 {
 	PMCDBG0(MDP,INI,1, "core-finalize");
 
+	for (int i = 0; i < pmc_cpu_max(); i++)
+		KASSERT(core_pcpu[i] == NULL,
+		    ("[core,%d] non-null pcpu cpu %d", __LINE__, i));
+
 	free(core_pcpu, M_PMC);
 	core_pcpu = NULL;
 }
diff --git a/sys/dev/hwpmc/hwpmc_powerpc.c b/sys/dev/hwpmc/hwpmc_powerpc.c
index a4bd951fab40..0380988f909f 100644
--- a/sys/dev/hwpmc/hwpmc_powerpc.c
+++ b/sys/dev/hwpmc/hwpmc_powerpc.c
@@ -581,6 +581,11 @@ pmc_md_initialize(void)
 void
 pmc_md_finalize(struct pmc_mdep *md)
 {
+	PMCDBG0(MDP, INI, 1, "powerpc-finalize");
+
+	for (int i = 0; i < pmc_cpu_max(); i++)
+		KASSERT(powerpc_pcpu[i] == NULL,
+		    ("[powerpc,%d] non-null pcpu cpu %d", __LINE__, i));
 
 	free(powerpc_pcpu, M_PMC);
 	powerpc_pcpu = NULL;
diff --git a/sys/dev/hwpmc/hwpmc_soft.c b/sys/dev/hwpmc/hwpmc_soft.c
index 5606164b3e0f..4679969fb45c 100644
--- a/sys/dev/hwpmc/hwpmc_soft.c
+++ b/sys/dev/hwpmc/hwpmc_soft.c
@@ -456,17 +456,12 @@ pmc_soft_initialize(struct pmc_mdep *md)
 void
 pmc_soft_finalize(struct pmc_mdep *md)
 {
-#ifdef	INVARIANTS
-	int i, ncpus;
+	PMCDBG0(MDP, INI, 1, "soft-finalize");
 
-	ncpus = pmc_cpu_max();
-	for (i = 0; i < ncpus; i++)
+	for (int i = 0; i < pmc_cpu_max(); i++)
 		KASSERT(soft_pcpu[i] == NULL, ("[soft,%d] non-null pcpu cpu %d",
 		    __LINE__, i));
 
-	KASSERT(md->pmd_classdep[PMC_CLASS_INDEX_SOFT].pcd_class ==
-	    PMC_CLASS_SOFT, ("[soft,%d] class mismatch", __LINE__));
-#endif
 	ast_deregister(TDA_HWPMC);
 	free(soft_pcpu, M_PMC);
 	soft_pcpu = NULL;
diff --git a/sys/dev/hwpmc/hwpmc_tsc.c b/sys/dev/hwpmc/hwpmc_tsc.c
index f86b93d58bfb..5d31f8a9c61c 100644
--- a/sys/dev/hwpmc/hwpmc_tsc.c
+++ b/sys/dev/hwpmc/hwpmc_tsc.c
@@ -339,18 +339,12 @@ pmc_tsc_initialize(struct pmc_mdep *md, int maxcpu)
 void
 pmc_tsc_finalize(struct pmc_mdep *md __diagused)
 {
-#ifdef	INVARIANTS
-	int i, ncpus;
+	PMCDBG0(MDP, INI, 1, "tsc-finalize");
 
-	ncpus = pmc_cpu_max();
-	for (i = 0; i < ncpus; i++)
+	for (int i = 0; i < pmc_cpu_max(); i++)
 		KASSERT(tsc_pcpu[i] == NULL, ("[tsc,%d] non-null pcpu cpu %d",
 		    __LINE__, i));
 
-	KASSERT(md->pmd_classdep[PMC_MDEP_CLASS_INDEX_TSC].pcd_class ==
-	    PMC_CLASS_TSC, ("[tsc,%d] class mismatch", __LINE__));
-#endif
-
 	free(tsc_pcpu, M_PMC);
 	tsc_pcpu = NULL;
 }
diff --git a/sys/dev/hwpmc/hwpmc_uncore.c b/sys/dev/hwpmc/hwpmc_uncore.c
index fd4266b605ef..0ee41db0658d 100644
--- a/sys/dev/hwpmc/hwpmc_uncore.c
+++ b/sys/dev/hwpmc/hwpmc_uncore.c
@@ -758,6 +758,10 @@ pmc_uncore_finalize(struct pmc_mdep *md)
 {
 	PMCDBG0(MDP,INI,1, "uncore-finalize");
 
+	for (int i = 0; i < pmc_cpu_max(); i++)
+		KASSERT(uncore_pcpu[i] == NULL,
+		    ("[uncore,%d] non-null pcpu cpu %d", __LINE__, i));
+
 	free(uncore_pcpu, M_PMC);
 	uncore_pcpu = NULL;
 }