git: a77188ffd34f - stable/13 - acpi_cpu: Fix panic if some CPU devices are disabled.

From: Alexander Motin <mav_at_FreeBSD.org>
Date: Tue, 04 Jan 2022 18:03:29 UTC
The branch stable/13 has been updated by mav:

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

commit a77188ffd34f5f6ba1209caa25f227362339f23f
Author:     Alexander Motin <mav@FreeBSD.org>
AuthorDate: 2021-09-25 20:54:28 +0000
Commit:     Alexander Motin <mav@FreeBSD.org>
CommitDate: 2022-01-04 17:21:46 +0000

    acpi_cpu: Fix panic if some CPU devices are disabled.
    
    While there, remove couple unneeded global variables.
    
    (cherry picked from commit 695323ae88c71776e131940ed7dedd25365e932f)
---
 sys/dev/acpica/acpi_cpu.c | 66 +++++++++++++++++++++--------------------------
 1 file changed, 29 insertions(+), 37 deletions(-)

diff --git a/sys/dev/acpica/acpi_cpu.c b/sys/dev/acpica/acpi_cpu.c
index ab4ffda7a02e..0577b6eac8b7 100644
--- a/sys/dev/acpica/acpi_cpu.c
+++ b/sys/dev/acpica/acpi_cpu.c
@@ -154,8 +154,6 @@ static struct sysctl_oid *cpu_sysctl_tree;
 static int		 cpu_cx_generic;
 static int		 cpu_cx_lowest_lim;
 
-static device_t		*cpu_devices;
-static int		 cpu_ndevices;
 static struct acpi_cpu_softc **cpu_softc;
 ACPI_SERIAL_DECL(cpu, "ACPI CPU");
 
@@ -443,26 +441,21 @@ acpi_cpu_attach(device_t dev)
 static void
 acpi_cpu_postattach(void *unused __unused)
 {
-    device_t *devices;
-    int err;
-    int i, n;
-    int attached;
-
-    err = devclass_get_devices(acpi_cpu_devclass, &devices, &n);
-    if (err != 0) {
-	printf("devclass_get_devices(acpi_cpu_devclass) failed\n");
-	return;
+    struct acpi_cpu_softc *sc;
+    int attached = 0, i;
+
+    mtx_lock(&Giant);
+    CPU_FOREACH(i) {
+	if ((sc = cpu_softc[i]) != NULL)
+		bus_generic_probe(sc->cpu_dev);
+    }
+    CPU_FOREACH(i) {
+	if ((sc = cpu_softc[i]) != NULL) {
+		bus_generic_attach(sc->cpu_dev);
+		attached = 1;
+	}
     }
-    attached = 0;
-    for (i = 0; i < n; i++)
-	if (device_is_attached(devices[i]) &&
-	    device_get_driver(devices[i]) == &acpi_cpu_driver)
-	    attached = 1;
-    for (i = 0; i < n; i++)
-	bus_generic_probe(devices[i]);
-    for (i = 0; i < n; i++)
-	bus_generic_attach(devices[i]);
-    free(devices, M_TEMP);
+    mtx_unlock(&Giant);
 
     if (attached) {
 #ifdef EARLY_AP_STARTUP
@@ -937,9 +930,6 @@ acpi_cpu_startup(void *arg)
     struct acpi_cpu_softc *sc;
     int i;
 
-    /* Get set of CPU devices */
-    devclass_get_devices(acpi_cpu_devclass, &cpu_devices, &cpu_ndevices);
-
     /*
      * Setup any quirks that might necessary now that we have probed
      * all the CPUs
@@ -951,9 +941,9 @@ acpi_cpu_startup(void *arg)
 	 * We are using generic Cx mode, probe for available Cx states
 	 * for all processors.
 	 */
-	for (i = 0; i < cpu_ndevices; i++) {
-	    sc = device_get_softc(cpu_devices[i]);
-	    acpi_cpu_generic_cx_probe(sc);
+	CPU_FOREACH(i) {
+	    if ((sc = cpu_softc[i]) != NULL)
+		acpi_cpu_generic_cx_probe(sc);
 	}
     } else {
 	/*
@@ -961,8 +951,9 @@ acpi_cpu_startup(void *arg)
 	 * As we now know for sure that we will be using _CST mode
 	 * install our notify handler.
 	 */
-	for (i = 0; i < cpu_ndevices; i++) {
-	    sc = device_get_softc(cpu_devices[i]);
+	CPU_FOREACH(i) {
+	    if ((sc = cpu_softc[i]) == NULL)
+		continue;
 	    if (cpu_quirks & CPU_QUIRK_NO_C3) {
 		sc->cpu_cx_count = min(sc->cpu_cx_count, sc->cpu_non_c3 + 1);
 	    }
@@ -972,9 +963,9 @@ acpi_cpu_startup(void *arg)
     }
 
     /* Perform Cx final initialization. */
-    for (i = 0; i < cpu_ndevices; i++) {
-	sc = device_get_softc(cpu_devices[i]);
-	acpi_cpu_startup_cx(sc);
+    CPU_FOREACH(i) {
+	if ((sc = cpu_softc[i]) != NULL)
+	    acpi_cpu_startup_cx(sc);
     }
 
     /* Add a sysctl handler to handle global Cx lowest setting */
@@ -985,9 +976,9 @@ acpi_cpu_startup(void *arg)
 
     /* Take over idling from cpu_idle_default(). */
     cpu_cx_lowest_lim = 0;
-    for (i = 0; i < cpu_ndevices; i++) {
-	sc = device_get_softc(cpu_devices[i]);
-	enable_idle(sc);
+    CPU_FOREACH(i) {
+	if ((sc = cpu_softc[i]) != NULL)
+	    enable_idle(sc);
     }
 #if defined(__i386__) || defined(__amd64__)
     cpu_idle_hook = acpi_cpu_idle;
@@ -1517,8 +1508,9 @@ acpi_cpu_global_cx_lowest_sysctl(SYSCTL_HANDLER_ARGS)
     /* Update the new lowest useable Cx state for all CPUs. */
     ACPI_SERIAL_BEGIN(cpu);
     cpu_cx_lowest_lim = val - 1;
-    for (i = 0; i < cpu_ndevices; i++) {
-	sc = device_get_softc(cpu_devices[i]);
+    CPU_FOREACH(i) {
+	if ((sc = cpu_softc[i]) == NULL)
+	    continue;
 	sc->cpu_cx_lowest_lim = cpu_cx_lowest_lim;
 	acpi_cpu_set_cx_lowest(sc);
     }