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

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