Blame SOURCES/kvm-address_space_read-address_space_to_flatview-needs-R.patch

37e7a1
From e85fa0f42359527628b5b2243c514acc9670871a Mon Sep 17 00:00:00 2001
37e7a1
From: Paolo Bonzini <pbonzini@redhat.com>
37e7a1
Date: Thu, 8 Mar 2018 15:58:16 +0100
37e7a1
Subject: [PATCH 03/17] address_space_read: address_space_to_flatview needs RCU
37e7a1
 lock
37e7a1
37e7a1
RH-Author: Paolo Bonzini <pbonzini@redhat.com>
37e7a1
Message-id: <20180308155819.20598-4-pbonzini@redhat.com>
37e7a1
Patchwork-id: 79190
37e7a1
O-Subject: [RHEL7.5 qemu-kvm-rhev PATCH 3/6] address_space_read: address_space_to_flatview needs RCU lock
37e7a1
Bugzilla: 1554930
37e7a1
RH-Acked-by: Cornelia Huck <cohuck@redhat.com>
37e7a1
RH-Acked-by: Thomas Huth <thuth@redhat.com>
37e7a1
RH-Acked-by: Laurent Vivier <lvivier@redhat.com>
37e7a1
37e7a1
address_space_read is calling address_space_to_flatview but it can
37e7a1
be called outside the RCU lock.  To fix it, push the rcu_read_lock/unlock
37e7a1
pair up from flatview_read_full to address_space_read's constant size
37e7a1
fast path and address_space_read_full.
37e7a1
37e7a1
Reviewed-by: Alexey Kardashevskiy <aik@ozlabs.ru>
37e7a1
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
37e7a1
(cherry pick from commit b2a44fcad74f1cc7a6786d38eba7db12ab2352ba)
37e7a1
37e7a1
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
37e7a1
---
37e7a1
 exec.c                | 38 +++++++++++++++++++++++++-------------
37e7a1
 include/exec/memory.h | 23 +++++++++--------------
37e7a1
 2 files changed, 34 insertions(+), 27 deletions(-)
37e7a1
37e7a1
diff --git a/exec.c b/exec.c
37e7a1
index 8b50133..43c66da 100644
37e7a1
--- a/exec.c
37e7a1
+++ b/exec.c
37e7a1
@@ -2512,6 +2512,8 @@ static const MemoryRegionOps watch_mem_ops = {
37e7a1
     .endianness = DEVICE_NATIVE_ENDIAN,
37e7a1
 };
37e7a1
 
37e7a1
+static MemTxResult flatview_read(FlatView *fv, hwaddr addr,
37e7a1
+                                      MemTxAttrs attrs, uint8_t *buf, int len);
37e7a1
 static MemTxResult flatview_write(FlatView *fv, hwaddr addr, MemTxAttrs attrs,
37e7a1
                                   const uint8_t *buf, int len);
37e7a1
 static bool flatview_access_valid(FlatView *fv, hwaddr addr, int len,
37e7a1
@@ -3029,24 +3031,18 @@ MemTxResult flatview_read_continue(FlatView *fv, hwaddr addr,
37e7a1
     return result;
37e7a1
 }
37e7a1
 
37e7a1
-MemTxResult flatview_read_full(FlatView *fv, hwaddr addr,
37e7a1
-                               MemTxAttrs attrs, uint8_t *buf, int len)
37e7a1
+/* Called from RCU critical section.  */
37e7a1
+static MemTxResult flatview_read(FlatView *fv, hwaddr addr,
37e7a1
+                                 MemTxAttrs attrs, uint8_t *buf, int len)
37e7a1
 {
37e7a1
     hwaddr l;
37e7a1
     hwaddr addr1;
37e7a1
     MemoryRegion *mr;
37e7a1
-    MemTxResult result = MEMTX_OK;
37e7a1
-
37e7a1
-    if (len > 0) {
37e7a1
-        rcu_read_lock();
37e7a1
-        l = len;
37e7a1
-        mr = flatview_translate(fv, addr, &addr1, &l, false);
37e7a1
-        result = flatview_read_continue(fv, addr, attrs, buf, len,
37e7a1
-                                        addr1, l, mr);
37e7a1
-        rcu_read_unlock();
37e7a1
-    }
37e7a1
 
37e7a1
-    return result;
37e7a1
+    l = len;
37e7a1
+    mr = flatview_translate(fv, addr, &addr1, &l, false);
37e7a1
+    return flatview_read_continue(fv, addr, attrs, buf, len,
37e7a1
+                                  addr1, l, mr);
37e7a1
 }
37e7a1
 
37e7a1
 static MemTxResult flatview_rw(FlatView *fv, hwaddr addr, MemTxAttrs attrs,
37e7a1
@@ -3067,6 +3063,22 @@ MemTxResult address_space_rw(AddressSpace *as, hwaddr addr,
37e7a1
                        addr, attrs, buf, len, is_write);
37e7a1
 }
37e7a1
 
37e7a1
+MemTxResult address_space_read_full(AddressSpace *as, hwaddr addr,
37e7a1
+                                    MemTxAttrs attrs, uint8_t *buf, int len)
37e7a1
+{
37e7a1
+    MemTxResult result = MEMTX_OK;
37e7a1
+    FlatView *fv;
37e7a1
+
37e7a1
+    if (len > 0) {
37e7a1
+        rcu_read_lock();
37e7a1
+        fv = address_space_to_flatview(as);
37e7a1
+        result = flatview_read(fv, addr, attrs, buf, len);
37e7a1
+        rcu_read_unlock();
37e7a1
+    }
37e7a1
+
37e7a1
+    return result;
37e7a1
+}
37e7a1
+
37e7a1
 MemTxResult address_space_write(AddressSpace *as, hwaddr addr,
37e7a1
                                 MemTxAttrs attrs,
37e7a1
                                 const uint8_t *buf, int len)
37e7a1
diff --git a/include/exec/memory.h b/include/exec/memory.h
37e7a1
index 3df0dcc..a66b6ef 100644
37e7a1
--- a/include/exec/memory.h
37e7a1
+++ b/include/exec/memory.h
37e7a1
@@ -1917,13 +1917,12 @@ void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
37e7a1
 
37e7a1
 
37e7a1
 /* Internal functions, part of the implementation of address_space_read.  */
37e7a1
+MemTxResult address_space_read_full(AddressSpace *as, hwaddr addr,
37e7a1
+                                    MemTxAttrs attrs, uint8_t *buf, int len);
37e7a1
 MemTxResult flatview_read_continue(FlatView *fv, hwaddr addr,
37e7a1
                                    MemTxAttrs attrs, uint8_t *buf,
37e7a1
                                    int len, hwaddr addr1, hwaddr l,
37e7a1
                                    MemoryRegion *mr);
37e7a1
-
37e7a1
-MemTxResult flatview_read_full(FlatView *fv, hwaddr addr,
37e7a1
-                               MemTxAttrs attrs, uint8_t *buf, int len);
37e7a1
 void *qemu_map_ram_ptr(RAMBlock *ram_block, ram_addr_t addr);
37e7a1
 
37e7a1
 static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write)
37e7a1
@@ -1942,7 +1941,7 @@ static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write)
37e7a1
  *
37e7a1
  * Return a MemTxResult indicating whether the operation succeeded
37e7a1
  * or failed (eg unassigned memory, device rejected the transaction,
37e7a1
- * IOMMU fault).
37e7a1
+ * IOMMU fault).  Called within RCU critical section.
37e7a1
  *
37e7a1
  * @as: #AddressSpace to be accessed
37e7a1
  * @addr: address within that address space
37e7a1
@@ -1950,17 +1949,20 @@ static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write)
37e7a1
  * @buf: buffer with the data transferred
37e7a1
  */
37e7a1
 static inline __attribute__((__always_inline__))
37e7a1
-MemTxResult flatview_read(FlatView *fv, hwaddr addr, MemTxAttrs attrs,
37e7a1
-                          uint8_t *buf, int len)
37e7a1
+MemTxResult address_space_read(AddressSpace *as, hwaddr addr,
37e7a1
+                               MemTxAttrs attrs, uint8_t *buf,
37e7a1
+                               int len)
37e7a1
 {
37e7a1
     MemTxResult result = MEMTX_OK;
37e7a1
     hwaddr l, addr1;
37e7a1
     void *ptr;
37e7a1
     MemoryRegion *mr;
37e7a1
+    FlatView *fv;
37e7a1
 
37e7a1
     if (__builtin_constant_p(len)) {
37e7a1
         if (len) {
37e7a1
             rcu_read_lock();
37e7a1
+            fv = address_space_to_flatview(as);
37e7a1
             l = len;
37e7a1
             mr = flatview_translate(fv, addr, &addr1, &l, false);
37e7a1
             if (len == l && memory_access_is_direct(mr, false)) {
37e7a1
@@ -1973,18 +1975,11 @@ MemTxResult flatview_read(FlatView *fv, hwaddr addr, MemTxAttrs attrs,
37e7a1
             rcu_read_unlock();
37e7a1
         }
37e7a1
     } else {
37e7a1
-        result = flatview_read_full(fv, addr, attrs, buf, len);
37e7a1
+        result = address_space_read_full(as, addr, attrs, buf, len);
37e7a1
     }
37e7a1
     return result;
37e7a1
 }
37e7a1
 
37e7a1
-static inline MemTxResult address_space_read(AddressSpace *as, hwaddr addr,
37e7a1
-                                             MemTxAttrs attrs, uint8_t *buf,
37e7a1
-                                             int len)
37e7a1
-{
37e7a1
-    return flatview_read(address_space_to_flatview(as), addr, attrs, buf, len);
37e7a1
-}
37e7a1
-
37e7a1
 /**
37e7a1
  * address_space_read_cached: read from a cached RAM region
37e7a1
  *
37e7a1
-- 
37e7a1
1.8.3.1
37e7a1