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

[win-pv-devel] [PATCH 2/2] Use new SystemProcessorCount() function for XENBUS_EVTCHN initialization



Since it's necessary in a few places in the EVTCHN code to map processor
number to vcpu_id, the available processors should be limited to that for
which such a mapping exists.

Signed-off-by: Paul Durrant <paul.durrant@xxxxxxxxxx>
---
 include/xen.h            |  9 ++++++++-
 src/xen/system.c         | 37 ++++++++++++++++++++++++++++++++++---
 src/xenbus/evtchn.c      | 17 ++++++++++++-----
 src/xenbus/evtchn_2l.c   | 16 +++++++++++++---
 src/xenbus/evtchn_fifo.c | 27 ++++++++++++++++++++-------
 src/xenbus/shared_info.c | 23 ++++++++++++++++++-----
 6 files changed, 105 insertions(+), 24 deletions(-)

diff --git a/include/xen.h b/include/xen.h
index 23c7ac0..f03635f 100644
--- a/include/xen.h
+++ b/include/xen.h
@@ -401,8 +401,15 @@ LogPrintf(
 
 XEN_API
 ULONG
+SystemProcessorCount(
+    VOID
+    );
+
+XEN_API
+NTSTATUS
 SystemVirtualCpuIndex(
-    IN  ULONG   Index
+    IN  ULONG           Index,
+    OUT unsigned int    *vcpu_id
     );
 
 #endif  // _XEN_H
diff --git a/src/xen/system.c b/src/xen/system.c
index 3905273..65c9d0e 100644
--- a/src/xen/system.c
+++ b/src/xen/system.c
@@ -666,6 +666,8 @@ SystemProcessorChangeCallback(
         }
 
         Context->Processor = Processor;
+        KeMemoryBarrier();
+
         Context->ProcessorCount = ProcessorCount;
         break;
     }
@@ -972,18 +974,47 @@ fail1:
     return status;
 }
 
+static FORCEINLINE ULONG
+__SystemProcessorCount(
+    VOID
+    )
+{
+    PSYSTEM_CONTEXT     Context = &SystemContext;
+
+    KeMemoryBarrier();
+
+    return Context->ProcessorCount;
+}
+
 XEN_API
 ULONG
+SystemProcessorCount(
+    VOID
+    )
+{
+    return __SystemProcessorCount();
+}
+
+XEN_API
+NTSTATUS
 SystemVirtualCpuIndex(
-    IN  ULONG           Index
+    IN  ULONG           Index,
+    OUT unsigned int    *vcpu_id
     )
 {
     PSYSTEM_CONTEXT     Context = &SystemContext;
     PSYSTEM_PROCESSOR   Processor = &Context->Processor[Index];
+    NTSTATUS            status;
 
-    ASSERT3U(Index, <, Context->ProcessorCount);
+    status = STATUS_UNSUCCESSFUL;
+    if (Index >= __SystemProcessorCount())
+        goto fail1;
+
+    *vcpu_id = Processor->ProcessorID;
+    return STATUS_SUCCESS;
 
-    return Processor->ProcessorID;
+fail1:
+    return status;
 }
 
 VOID
diff --git a/src/xenbus/evtchn.c b/src/xenbus/evtchn.c
index 5769cf3..c5c97e4 100644
--- a/src/xenbus/evtchn.c
+++ b/src/xenbus/evtchn.c
@@ -699,7 +699,9 @@ EvtchnBind(
         goto done;
 
     LocalPort = Channel->LocalPort;
-    vcpu_id = SystemVirtualCpuIndex(Index);
+
+    status = SystemVirtualCpuIndex(Index, &vcpu_id);
+    ASSERT(NT_SUCCESS(status));
 
     status = EventChannelBindVirtualCpu(LocalPort, vcpu_id);
     if (!NT_SUCCESS(status))
@@ -1155,7 +1157,9 @@ EvtchnInterruptEnable(
         if (Processor->Interrupt == NULL)
             continue;
 
-        vcpu_id = SystemVirtualCpuIndex(Index);
+        status = SystemVirtualCpuIndex(Index, &vcpu_id);
+        ASSERT(NT_SUCCESS(status));
+
         Vector = FdoGetInterruptVector(Context->Fdo, Processor->Interrupt);
 
         status = HvmSetEvtchnUpcallVector(vcpu_id, Vector);
@@ -1211,7 +1215,8 @@ EvtchnInterruptDisable(
         if (!Processor->UpcallEnabled)
             continue;
 
-        vcpu_id = SystemVirtualCpuIndex(Index);
+        status = SystemVirtualCpuIndex(Index, &vcpu_id);
+        ASSERT(NT_SUCCESS(status));
 
         (VOID) HvmSetEvtchnUpcallVector(vcpu_id, 0);
         Processor->UpcallEnabled = FALSE;
@@ -1434,7 +1439,7 @@ EvtchnAcquire(
     if (Context->Interrupt == NULL)
         goto fail8;
 
-    Context->ProcessorCount = 
KeQueryActiveProcessorCountEx(ALL_PROCESSOR_GROUPS);
+    Context->ProcessorCount = 
KeQueryMaximumProcessorCountEx(ALL_PROCESSOR_GROUPS);
     Context->Processor = __EvtchnAllocate(sizeof (XENBUS_EVTCHN_PROCESSOR) * 
Context->ProcessorCount);
 
     status = STATUS_NO_MEMORY;
@@ -1460,7 +1465,9 @@ EvtchnAcquire(
                                                     ProcNumber.Number,
                                                     EvtchnInterruptCallback,
                                                     Context);
-        ASSERT(Processor->Interrupt != NULL);
+
+        if (Processor->Interrupt == NULL)
+            continue;
 
         InitializeListHead(&Processor->PendingList);
 
diff --git a/src/xenbus/evtchn_2l.c b/src/xenbus/evtchn_2l.c
index 249543d..913160f 100644
--- a/src/xenbus/evtchn_2l.c
+++ b/src/xenbus/evtchn_2l.c
@@ -67,13 +67,23 @@ __EvtchnTwoLevelFree(
 
 static BOOLEAN
 EvtchnTwoLevelIsProcessorEnabled(
-    IN  PXENBUS_EVTCHN_ABI_CONTEXT      _Context,
-    IN  ULONG                           Index
+    IN  PXENBUS_EVTCHN_ABI_CONTEXT  _Context,
+    IN  ULONG                       Index
     )
 {
+    unsigned int                    vcpu_id;
+    NTSTATUS                        status;
+
     UNREFERENCED_PARAMETER(_Context);
 
-    return (SystemVirtualCpuIndex(Index) == 0) ? TRUE : FALSE;
+    status = SystemVirtualCpuIndex(Index, &vcpu_id);
+    if (!NT_SUCCESS(status))
+        return FALSE;
+
+    if (vcpu_id != 0)
+        return FALSE;
+
+    return TRUE;
 }
 
 static BOOLEAN
diff --git a/src/xenbus/evtchn_fifo.c b/src/xenbus/evtchn_fifo.c
index 98ea0fa..91cbd76 100644
--- a/src/xenbus/evtchn_fifo.c
+++ b/src/xenbus/evtchn_fifo.c
@@ -285,7 +285,12 @@ EvtchnFifoIsProcessorEnabled(
     )
 {
     PXENBUS_EVTCHN_FIFO_CONTEXT     Context = (PVOID)_Context;
-    unsigned int                    vcpu_id = SystemVirtualCpuIndex(Index);
+    unsigned int                    vcpu_id;
+    NTSTATUS                        status;
+    
+    status = SystemVirtualCpuIndex(Index, &vcpu_id);
+    if (!NT_SUCCESS(status))
+        return FALSE;
 
     return (Context->ControlBlockMdl[vcpu_id] != NULL) ? TRUE : FALSE;
 }
@@ -348,16 +353,21 @@ EvtchnFifoPoll(
     )
 {
     PXENBUS_EVTCHN_FIFO_CONTEXT     Context = (PVOID)_Context;
-    unsigned int                    vcpu_id = SystemVirtualCpuIndex(Index);
+    unsigned int                    vcpu_id;
     PMDL                            Mdl;
     evtchn_fifo_control_block_t     *ControlBlock;
     ULONG                           Ready;
     ULONG                           Priority;
     BOOLEAN                         DoneSomething;
-
-    Mdl = Context->ControlBlockMdl[vcpu_id];
+    NTSTATUS                        status;
 
     DoneSomething = FALSE;
+
+    status = SystemVirtualCpuIndex(Index, &vcpu_id);
+    if (!NT_SUCCESS(status))
+        goto done;
+
+    Mdl = Context->ControlBlockMdl[vcpu_id];
     if (Mdl == NULL)
         goto done;
 
@@ -488,7 +498,7 @@ EvtchnFifoAcquire(
     Trace("====>\n");
 
     Index = 0;
-    while (Index < (LONG)KeQueryActiveProcessorCountEx(ALL_PROCESSOR_GROUPS)) {
+    while (Index < (LONG)SystemProcessorCount()) {
         unsigned int        vcpu_id;
         PFN_NUMBER          Pfn;
         PHYSICAL_ADDRESS    Address;
@@ -499,7 +509,9 @@ EvtchnFifoAcquire(
         if (Mdl == NULL)
             goto fail1;
 
-        vcpu_id = SystemVirtualCpuIndex(Index);
+        status = SystemVirtualCpuIndex(Index, &vcpu_id);
+        ASSERT(NT_SUCCESS(status));
+
         Pfn = MmGetMdlPfnArray(Mdl)[0];
 
         status = EventChannelInitControl(Pfn, vcpu_id);
@@ -537,7 +549,8 @@ fail1:
     while (--Index >= 0) {
         unsigned int    vcpu_id;
 
-        vcpu_id = SystemVirtualCpuIndex(Index);
+        status = SystemVirtualCpuIndex(Index, &vcpu_id);
+        ASSERT(NT_SUCCESS(status));
 
         Mdl = Context->ControlBlockMdl[vcpu_id];
         Context->ControlBlockMdl[vcpu_id] = NULL;
diff --git a/src/xenbus/shared_info.c b/src/xenbus/shared_info.c
index 01621ac..9d8bf11 100644
--- a/src/xenbus/shared_info.c
+++ b/src/xenbus/shared_info.c
@@ -161,8 +161,13 @@ SharedInfoUpcallPending(
 {
     PXENBUS_SHARED_INFO_CONTEXT Context = Interface->Context;
     shared_info_t               *Shared = Context->Shared;
-    int                         vcpu_id = SystemVirtualCpuIndex(Index);
+    unsigned int                vcpu_id;
     UCHAR                       Pending;
+    NTSTATUS                    status;
+
+    status = SystemVirtualCpuIndex(Index, &vcpu_id);
+    if (!NT_SUCCESS(status))
+        return FALSE;
 
     KeMemoryBarrier();
 
@@ -181,13 +186,18 @@ SharedInfoEvtchnPoll(
 {
     PXENBUS_SHARED_INFO_CONTEXT     Context = Interface->Context;
     shared_info_t                   *Shared = Context->Shared;
-    int                             vcpu_id = SystemVirtualCpuIndex(Index);
+    unsigned int                    vcpu_id;
     ULONG                           Port;
     ULONG_PTR                       SelectorMask;
     BOOLEAN                         DoneSomething;
+    NTSTATUS                        status;
 
     DoneSomething = FALSE;
 
+    status = SystemVirtualCpuIndex(Index, &vcpu_id);
+    if (!NT_SUCCESS(status))
+        goto done;
+
     KeMemoryBarrier();
 
     SelectorMask = (ULONG_PTR)InterlockedExchangePointer((PVOID 
*)&Shared->vcpu_info[vcpu_id].evtchn_pending_sel, (PVOID)0);
@@ -233,6 +243,7 @@ SharedInfoEvtchnPoll(
 
     Context->Port = Port;
 
+done:
     return DoneSomething;
 }
 
@@ -469,14 +480,16 @@ SharedInfoDebugCallback(
              Index < KeQueryActiveProcessorCountEx(ALL_PROCESSOR_GROUPS);
              Index++) {
             PROCESSOR_NUMBER    ProcNumber;
-            int                 vcpu_id;
+            unsigned int        vcpu_id;
             NTSTATUS            status;
 
+            status = SystemVirtualCpuIndex(Index, &vcpu_id);
+            if (!NT_SUCCESS(status))
+                continue;
+
             status = KeGetProcessorNumberFromIndex(Index, &ProcNumber);
             ASSERT(NT_SUCCESS(status));
 
-            vcpu_id = SystemVirtualCpuIndex(Index);
-
             XENBUS_DEBUG(Printf,
                          &Context->DebugInterface,
                          "CPU %u:%u: PENDING: %s\n",
-- 
2.1.1


_______________________________________________
win-pv-devel mailing list
win-pv-devel@xxxxxxxxxxxxxxxxxxxx
http://lists.xenproject.org/cgi-bin/mailman/listinfo/win-pv-devel


 


Rackspace

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