]> xenbits.xensource.com Git - pvdrivers/win/xenbus.git/commitdiff
Allocate SYSTEM_PROCESSOR array up-front
authorPaul Durrant <pdurrant@amazon.com>
Sat, 30 Jan 2021 14:29:22 +0000 (14:29 +0000)
committerPaul Durrant <pdurrant@amazon.com>
Mon, 8 Feb 2021 15:23:54 +0000 (15:23 +0000)
Most code uses KeQueryMaximumProcessorCountEx() to determine the number of
CPUs in the system, so remove the one and only caller of
SystemProcessorCount(), remove it from the XEN_API and allocate the array
up-front (also using KeQueryMaximumProcessorCountEx()) rather than growing
it in response to each processor callback.

Signed-off-by: Paul Durrant <pdurrant@amazon.com>
include/xen.h
src/xen/system.c
src/xenbus/evtchn_fifo.c

index fd3bb3ebb4b871b52be3459d96fe093569b4cf9a..e1ed1178f0d2de35bcb2fd3d2f33ac9efce8b581 100644 (file)
@@ -448,12 +448,6 @@ LogRemoveDisposition(
 
 // SYSTEM
 
-XEN_API
-ULONG
-SystemProcessorCount(
-    VOID
-    );
-
 XEN_API
 NTSTATUS
 SystemProcessorVcpuId(
index 14de3a2de015b1ae9f1d8ed0e514774b8b77090f..5323b3e11b350778e03e0c619dda1f9ad2c1c459 100644 (file)
@@ -627,18 +627,6 @@ SystemProcessorTeardown(
     RtlZeroMemory(Processor->Manufacturer, sizeof (Processor->Manufacturer));
 }
 
-static FORCEINLINE ULONG
-__SystemProcessorCount(
-    VOID
-    )
-{
-    PSYSTEM_CONTEXT     Context = &SystemContext;
-
-    KeMemoryBarrier();
-
-    return Context->ProcessorCount;
-}
-
 XEN_API
 NTSTATUS
 SystemProcessorVcpuId(
@@ -651,7 +639,7 @@ SystemProcessorVcpuId(
     NTSTATUS            status;
 
     status = STATUS_UNSUCCESSFUL;
-    if (Cpu >= __SystemProcessorCount())
+    if (Cpu >= Context->ProcessorCount)
         goto fail1;
 
     *vcpu_id = Processor->ProcessorID;
@@ -673,7 +661,7 @@ SystemProcessorVcpuInfo(
     NTSTATUS            status;
 
     status = STATUS_UNSUCCESSFUL;
-    if (Cpu >= __SystemProcessorCount())
+    if (Cpu >= Context->ProcessorCount)
         goto fail1;
 
     ASSERT(*Processor->Registered);
@@ -702,7 +690,7 @@ SystemProcessorRegisterVcpuInfo(
     NTSTATUS            status;
 
     status = STATUS_UNSUCCESSFUL;
-    if (Cpu >= __SystemProcessorCount())
+    if (Cpu >= Context->ProcessorCount)
         goto fail1;
 
     ASSERT(Mdl->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA);
@@ -840,6 +828,7 @@ SystemProcessorChangeCallback(
     NTSTATUS                                    status;
 
     UNREFERENCED_PARAMETER(Argument);
+    UNREFERENCED_PARAMETER(Status);
 
     Cpu = Change->NtNumber;
 
@@ -852,36 +841,9 @@ SystemProcessorChangeCallback(
           ProcessorChangeName(Change->State));
 
     switch (Change->State) {
-    case KeProcessorAddStartNotify: {
-        PSYSTEM_PROCESSOR   Processor;
-        ULONG               ProcessorCount;
-
-        if (Cpu < Context->ProcessorCount)
-            break;
-
-        ProcessorCount = Cpu + 1;
-        Processor = __SystemAllocate(sizeof (SYSTEM_PROCESSOR) *
-                                     ProcessorCount);
-
-        if (Processor == NULL) {
-            *Status = STATUS_NO_MEMORY;
-            break;
-        }
-
-        if (Context->ProcessorCount != 0) {
-            RtlCopyMemory(Processor,
-                          Context->Processor,
-                          sizeof (SYSTEM_PROCESSOR) *
-                          Context->ProcessorCount);
-            __SystemFree(Context->Processor);
-        }
-
-        Context->Processor = Processor;
-        KeMemoryBarrier();
-
-        Context->ProcessorCount = ProcessorCount;
+    case KeProcessorAddStartNotify:
         break;
-    }
+
     case KeProcessorAddCompleteNotify: {
         PSYSTEM_PROCESSOR   Processor;
 
@@ -1007,7 +969,7 @@ SystemDeregisterProcessorChangeCallback(
     KeDeregisterProcessorChangeCallback(Context->ProcessorChangeHandle);
     Context->ProcessorChangeHandle = NULL;
 
-    for (Cpu = 0; Cpu < __SystemProcessorCount(); Cpu++) {
+    for (Cpu = 0; Cpu < Context->ProcessorCount; Cpu++) {
         PSYSTEM_PROCESSOR   Processor = &Context->Processor[Cpu];
 
         SystemProcessorDeregisterVcpuInfo(Cpu);
@@ -1020,10 +982,6 @@ SystemDeregisterProcessorChangeCallback(
         ASSERT(IsZeroMemory(Processor, sizeof (SYSTEM_PROCESSOR)));
     }
 
-    __SystemFree(Context->Processor);
-    Context->Processor = NULL;
-    Context->ProcessorCount = 0;
-
     SystemFreeVcpuInfo();
 }
 
@@ -1233,7 +1191,7 @@ SystemCheckProcessors(
     ULONG           Cpu;
     NTSTATUS        status;
 
-    for (Cpu = 0; Cpu < __SystemProcessorCount(); Cpu++)
+    for (Cpu = 0; Cpu < Context->ProcessorCount; Cpu++)
     {
         PSYSTEM_PROCESSOR   Processor = &Context->Processor[Cpu];
 
@@ -1271,59 +1229,69 @@ SystemInitialize(
     if (References != 1)
         goto fail1;
 
+    Context->ProcessorCount = KeQueryMaximumProcessorCountEx(ALL_PROCESSOR_GROUPS);
+    Context->Processor = __SystemAllocate(sizeof (SYSTEM_PROCESSOR) * Context->ProcessorCount);
+
+    status = STATUS_NO_MEMORY;
+    if (Context->Processor == NULL)
+        goto fail2;
+
     status = SystemGetStartOptions();
     if (!NT_SUCCESS(status))
-        goto fail2;
+        goto fail3;
 
     status = SystemGetVersionInformation();
     if (!NT_SUCCESS(status))
-        goto fail3;
+        goto fail4;
 
     status = SystemGetMemoryInformation();
     if (!NT_SUCCESS(status))
-        goto fail4;
+        goto fail5;
 
     status = SystemGetAcpiInformation();
     if (!NT_SUCCESS(status))
-        goto fail5;
+        goto fail6;
 
     status = SystemRegisterProcessorChangeCallback();
     if (!NT_SUCCESS(status))
-        goto fail6;
+        goto fail7;
 
     status = SystemRegisterPowerStateCallback();
     if (!NT_SUCCESS(status))
-        goto fail7;
+        goto fail8;
 
     status = SystemGetTimeInformation();
     if (!NT_SUCCESS(status))
-        goto fail8;
+        goto fail9;
 
     status = SystemCheckProcessors();
     if (!NT_SUCCESS(status))
-        goto fail9;
+        goto fail10;
 
     return STATUS_SUCCESS;
 
+fail10:
+    Error("fail10\n");
+
 fail9:
     Error("fail9\n");
 
+    SystemDeregisterPowerStateCallback();
+
 fail8:
     Error("fail8\n");
 
-    SystemDeregisterPowerStateCallback();
+    SystemDeregisterProcessorChangeCallback();
 
 fail7:
     Error("fail7\n");
 
-    SystemDeregisterProcessorChangeCallback();
+    __SystemFree(Context->Madt);
+    Context->Madt = NULL;
 
 fail6:
     Error("fail6\n");
 
-    __SystemFree(Context->Madt);
-    Context->Madt = NULL;
-
 fail5:
     Error("fail5\n");
 
@@ -1333,9 +1301,13 @@ fail4:
 fail3:
     Error("fail3\n");
 
+    __SystemFree(Context->Processor);
+
 fail2:
     Error("fail2\n");
 
+    Context->ProcessorCount = 0;
+
 fail1:
     Error("fail1 (%08x)\n", status);
 
@@ -1344,15 +1316,6 @@ fail1:
     return status;
 }
 
-XEN_API
-ULONG
-SystemProcessorCount(
-    VOID
-    )
-{
-    return __SystemProcessorCount();
-}
-
 XEN_API
 PHYSICAL_ADDRESS
 SystemMaximumPhysicalAddress(
@@ -1447,6 +1410,9 @@ SystemTeardown(
 
     Context->MaximumPhysicalAddress.QuadPart = 0;
 
+    __SystemFree(Context->Processor);
+    Context->ProcessorCount = 0;
+
     (VOID) InterlockedDecrement(&Context->References);
 
     ASSERT(IsZeroMemory(Context, sizeof (SYSTEM_CONTEXT)));
index 475f99de10d715509e85bd0d3f8ffaf63719ac94..3b3f4938ed73c5a4db239483352d18be99b6434c 100644 (file)
@@ -487,6 +487,7 @@ EvtchnFifoAcquire(
 {
     PXENBUS_EVTCHN_FIFO_CONTEXT     Context = (PVOID)_Context;
     KIRQL                           Irql;
+    LONG                            ProcessorCount;
     LONG                            Index;
     PMDL                            Mdl;
     NTSTATUS                        status;
@@ -498,8 +499,10 @@ EvtchnFifoAcquire(
 
     Trace("====>\n");
 
+    ProcessorCount = KeQueryMaximumProcessorCountEx(ALL_PROCESSOR_GROUPS);
+
     Index = 0;
-    while (Index < (LONG)SystemProcessorCount()) {
+    while (Index < ProcessorCount) {
         unsigned int        vcpu_id;
         PFN_NUMBER          Pfn;
         PHYSICAL_ADDRESS    Address;