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

[XENVBD PATCH 3/3] ring: Weaken and delete unnecessary barriers


  • To: win-pv-devel@xxxxxxxxxxxxxxxxxxxx
  • From: Tu Dinh <ngoc-tu.dinh@xxxxxxxxxx>
  • Date: Mon, 18 May 2026 12:25:26 +0200
  • Authentication-results: eu.smtp.expurgate.cloud; dkim=pass header.s=selector1 header.d=vates.tech header.i="@vates.tech" header.h="From:Subject:Date:Message-ID:To:Cc:MIME-Version:Content-Type:In-Reply-To:References:Feedback-ID"
  • Cc: Tu Dinh <ngoc-tu.dinh@xxxxxxxxxx>
  • Delivery-date: Mon, 18 May 2026 10:25:43 +0000
  • Feedback-id: default:8631fc262581453bbf619ec5b2062170:Sweego
  • List-id: Developer list for the Windows PV Drivers subproject <win-pv-devel.lists.xenproject.org>

KeMemoryBarrier() is too strong for many of the barriers used in here.

BlkifRingPoll:
* First barrier replaced with the equivalent xen_mb() for consistency.
* Second barrier only needs to be an acquire, moved down a little bit as
  a micro-optimization.
* Third barrier only needs to be a release (rmb+wmb).

__BlkifRingCompleteResponse
* Interlocked was replaced with non-interlocked since operation is done
  under lock. Release barrier in BlkifRingPoll is enough.

__BlkifRingTryAcquireLock/__BlkifRingTryReleaseLock:
* All barriers are unnecessary since InterlockedCompareExchangePointer
  is already a full barrier.

RingWatchdog:
* Barriers not necessary since __BlkifRingAcquire/ReleaseLock are
  already full barriers, and BlkifRingPoll has its own barriers.

Signed-off-by: Tu Dinh <ngoc-tu.dinh@xxxxxxxxxx>
---
 src/xenvbd/ring.c | 25 ++++++-------------------
 1 file changed, 6 insertions(+), 19 deletions(-)

diff --git a/src/xenvbd/ring.c b/src/xenvbd/ring.c
index 205e93a..10ca6ee 100644
--- a/src/xenvbd/ring.c
+++ b/src/xenvbd/ring.c
@@ -1203,7 +1203,7 @@ __BlkifRingCompleteResponse(
     BlkifRingPutRequest(BlkifRing, Request);
 
     // complete srb
-    if (InterlockedDecrement(&SrbExt->RequestCount) == 0) {
+    if (--SrbExt->RequestCount == 0) {
         __BlkifRingCompleteSrb(BlkifRing, SrbExt);
     }
 }
@@ -1228,16 +1228,16 @@ BlkifRingPoll(
         RING_IDX            rsp_prod;
         RING_IDX            rsp_cons;
 
-        KeMemoryBarrier();
+        xen_mb();
 
         rsp_prod = BlkifRing->Shared->rsp_prod;
         rsp_cons = BlkifRing->Front.rsp_cons;
 
-        KeMemoryBarrier();
-
         if (rsp_cons == rsp_prod || Retry)
             break;
 
+        xen_rmb();
+
         while (rsp_cons != rsp_prod && !Retry) {
             blkif_response_t    *rsp;
             PXENVBD_REQUEST     Request;
@@ -1260,7 +1260,8 @@ BlkifRingPoll(
                 Retry = TRUE;
         }
 
-        KeMemoryBarrier();
+        xen_rmb();
+        xen_wmb();
 
         BlkifRing->Front.rsp_cons = rsp_cons;
         BlkifRing->Shared->rsp_event = rsp_cons + 1;
@@ -1455,8 +1456,6 @@ __BlkifRingTryAcquireLock(
 
     ASSERT3U(KeGetCurrentIrql(), == , DISPATCH_LEVEL);
 
-    KeMemoryBarrier();
-
     Old = (ULONG_PTR)BlkifRing->Lock & ~XENVBD_LOCK_BIT;
     New = Old | XENVBD_LOCK_BIT;
 
@@ -1464,13 +1463,10 @@ __BlkifRingTryAcquireLock(
         (PVOID)New,
                                                              (PVOID)Old) == 
Old) ? TRUE : FALSE;
 
-    KeMemoryBarrier();
-
 #if DBG
     if (Acquired) {
         ASSERT3P(BlkifRing->LockThread, == , NULL);
         BlkifRing->LockThread = KeGetCurrentThread();
-        KeMemoryBarrier();
     }
 #endif
 
@@ -1523,19 +1519,14 @@ __BlkifRingTryReleaseLock(
     BlkifRing->LockThread = NULL;
 #endif
 
-    KeMemoryBarrier();
-
     Released = ((ULONG_PTR)InterlockedCompareExchangePointer(&BlkifRing->Lock,
         (PVOID)New,
                                                              (PVOID)Old) == 
Old) ? TRUE : FALSE;
 
-    KeMemoryBarrier();
-
 #if DBG
     if (!Released) {
         ASSERT3P(BlkifRing->LockThread, == , NULL);
         BlkifRing->LockThread = KeGetCurrentThread();
-        KeMemoryBarrier();
     }
 #endif
 
@@ -1694,8 +1685,6 @@ RingWatchdog(
         __BlkifRingAcquireLock(BlkifRing);
 
         if (BlkifRing->Enabled) {
-            KeMemoryBarrier();
-
             if (BlkifRing->Shared->rsp_prod != rsp_prod &&
                 BlkifRing->Front.rsp_cons == rsp_cons) {
                 XENBUS_DEBUG(Trigger,
@@ -1707,8 +1696,6 @@ RingWatchdog(
                 (VOID) BlkifRingPoll(BlkifRing);
             }
 
-            KeMemoryBarrier();
-
             rsp_prod = BlkifRing->Shared->rsp_prod;
             rsp_cons = BlkifRing->Front.rsp_cons;
         }
-- 
2.54.0.windows.1



--
Ngoc Tu Dinh | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech

 


Rackspace

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