[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH xenbus 4/8] Allocate SYSTEM_PROCESSOR array up-front



From: Paul Durrant <pdurrant@xxxxxxxxxx>

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@xxxxxxxxxx>
---
 include/xen.h            |   6 ---
 src/xen/system.c         | 110 ++++++++++++++-------------------------
 src/xenbus/evtchn_fifo.c |   5 +-
 3 files changed, 42 insertions(+), 79 deletions(-)

diff --git a/include/xen.h b/include/xen.h
index fd3bb3ebb4b8..e1ed1178f0d2 100644
--- a/include/xen.h
+++ b/include/xen.h
@@ -448,12 +448,6 @@ LogRemoveDisposition(
 
 // SYSTEM
 
-XEN_API
-ULONG
-SystemProcessorCount(
-    VOID
-    );
-
 XEN_API
 NTSTATUS
 SystemProcessorVcpuId(
diff --git a/src/xen/system.c b/src/xen/system.c
index 14de3a2de015..5323b3e11b35 100644
--- a/src/xen/system.c
+++ b/src/xen/system.c
@@ -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)));
diff --git a/src/xenbus/evtchn_fifo.c b/src/xenbus/evtchn_fifo.c
index 475f99de10d7..3b3f4938ed73 100644
--- a/src/xenbus/evtchn_fifo.c
+++ b/src/xenbus/evtchn_fifo.c
@@ -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;
-- 
2.17.1




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.