All of lore.kernel.org
 help / color / mirror / Atom feed
From: Thomas Huth <thuth@redhat.com>
To: qemu-devel@nongnu.org, Richard Henderson <richard.henderson@linaro.org>
Cc: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Subject: [PULL 05/21] hw/pci-bridge: Fix release ordering by embedding PCIBridgeWindows within PCIBridge
Date: Mon, 15 May 2023 15:02:17 +0200	[thread overview]
Message-ID: <20230515130233.418183-6-thuth@redhat.com> (raw)
In-Reply-To: <20230515130233.418183-1-thuth@redhat.com>

From: Jonathan Cameron <Jonathan.Cameron@huawei.com>

The lifetime of the PCIBridgeWindows instance accessed via the windows pointer
in struct PCIBridge is managed separately from the PCIBridge itself.

Triggered by ./qemu-system-x86_64 -M x-remote -display none -monitor stdio
QEMU monitor: device_add cxl-downstream

In some error handling paths (such as the above due to attaching a cxl-downstream
port anything other than a cxl-upstream port) the g_free() of the PCIBridge
windows in pci_bridge_region_cleanup() is called before the final call of
flatview_uref() in address_space_set_flatview() ultimately from
drain_call_rcu()

At one stage this resulted in a crash, currently can still be observed using
valgrind which records a use after free.

When present, only one instance is allocated. pci_bridge_update_mappings()
can operate directly on an instance rather than creating a new one and
swapping it in.  Thus there appears to be no reason to not directly
couple the lifetimes of the two structures by embedding the PCIBridgeWindows
within the PCIBridge removing the need for the problematic separate free.

Patch is same as was posted deep in the discussion.
https://lore.kernel.org/qemu-devel/20230403171232.000020bb@huawei.com/

Reported-by: Thomas Huth <thuth@redhat.com>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Message-Id: <20230421122550.28234-1-Jonathan.Cameron@huawei.com>
Reviewed-by: Thomas Huth <thuth@redhat.com>
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
 include/hw/pci/pci_bridge.h |  2 +-
 hw/pci/pci_bridge.c         | 19 ++++++++-----------
 2 files changed, 9 insertions(+), 12 deletions(-)

diff --git a/include/hw/pci/pci_bridge.h b/include/hw/pci/pci_bridge.h
index 01670e9e65..ea54a81a15 100644
--- a/include/hw/pci/pci_bridge.h
+++ b/include/hw/pci/pci_bridge.h
@@ -73,7 +73,7 @@ struct PCIBridge {
     MemoryRegion address_space_mem;
     MemoryRegion address_space_io;
 
-    PCIBridgeWindows *windows;
+    PCIBridgeWindows windows;
 
     pci_map_irq_fn map_irq;
     const char *bus_name;
diff --git a/hw/pci/pci_bridge.c b/hw/pci/pci_bridge.c
index dd5af508f9..e7b9345615 100644
--- a/hw/pci/pci_bridge.c
+++ b/hw/pci/pci_bridge.c
@@ -184,11 +184,11 @@ static void pci_bridge_init_vga_aliases(PCIBridge *br, PCIBus *parent,
     }
 }
 
-static PCIBridgeWindows *pci_bridge_region_init(PCIBridge *br)
+static void pci_bridge_region_init(PCIBridge *br)
 {
     PCIDevice *pd = PCI_DEVICE(br);
     PCIBus *parent = pci_get_bus(pd);
-    PCIBridgeWindows *w = g_new(PCIBridgeWindows, 1);
+    PCIBridgeWindows *w = &br->windows;
     uint16_t cmd = pci_get_word(pd->config + PCI_COMMAND);
 
     pci_bridge_init_alias(br, &w->alias_pref_mem,
@@ -211,8 +211,6 @@ static PCIBridgeWindows *pci_bridge_region_init(PCIBridge *br)
                           cmd & PCI_COMMAND_IO);
 
     pci_bridge_init_vga_aliases(br, parent, w->alias_vga);
-
-    return w;
 }
 
 static void pci_bridge_region_del(PCIBridge *br, PCIBridgeWindows *w)
@@ -234,19 +232,18 @@ static void pci_bridge_region_cleanup(PCIBridge *br, PCIBridgeWindows *w)
     object_unparent(OBJECT(&w->alias_vga[QEMU_PCI_VGA_IO_LO]));
     object_unparent(OBJECT(&w->alias_vga[QEMU_PCI_VGA_IO_HI]));
     object_unparent(OBJECT(&w->alias_vga[QEMU_PCI_VGA_MEM]));
-    g_free(w);
 }
 
 void pci_bridge_update_mappings(PCIBridge *br)
 {
-    PCIBridgeWindows *w = br->windows;
+    PCIBridgeWindows *w = &br->windows;
 
     /* Make updates atomic to: handle the case of one VCPU updating the bridge
      * while another accesses an unaffected region. */
     memory_region_transaction_begin();
-    pci_bridge_region_del(br, br->windows);
+    pci_bridge_region_del(br, w);
     pci_bridge_region_cleanup(br, w);
-    br->windows = pci_bridge_region_init(br);
+    pci_bridge_region_init(br);
     memory_region_transaction_commit();
 }
 
@@ -385,7 +382,7 @@ void pci_bridge_initfn(PCIDevice *dev, const char *typename)
     sec_bus->address_space_io = &br->address_space_io;
     memory_region_init(&br->address_space_io, OBJECT(br), "pci_bridge_io",
                        4 * GiB);
-    br->windows = pci_bridge_region_init(br);
+    pci_bridge_region_init(br);
     QLIST_INIT(&sec_bus->child);
     QLIST_INSERT_HEAD(&parent->child, sec_bus, sibling);
 }
@@ -396,8 +393,8 @@ void pci_bridge_exitfn(PCIDevice *pci_dev)
     PCIBridge *s = PCI_BRIDGE(pci_dev);
     assert(QLIST_EMPTY(&s->sec_bus.child));
     QLIST_REMOVE(&s->sec_bus, sibling);
-    pci_bridge_region_del(s, s->windows);
-    pci_bridge_region_cleanup(s, s->windows);
+    pci_bridge_region_del(s, &s->windows);
+    pci_bridge_region_cleanup(s, &s->windows);
     /* object_unparent() is called automatically during device deletion */
 }
 
-- 
2.31.1



  parent reply	other threads:[~2023-05-15 13:06 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-15 13:02 [PULL 00/21] Tests, docs, s390x and misc patches Thomas Huth
2023-05-15 13:02 ` [PULL 01/21] tests/avocado/virtio-gpu: Fix the URLs of the test_virtio_vga_virgl test Thomas Huth
2023-05-15 13:02 ` [PULL 02/21] sysemu/kvm: Remove unused headers Thomas Huth
2023-05-15 13:02 ` [PULL 03/21] net: stream: test reconnect option with an unix socket Thomas Huth
2023-05-15 13:02 ` [PULL 04/21] tests/qtest: replace qmp_discard_response with qtest_qmp_assert_success Thomas Huth
2023-05-15 13:02 ` Thomas Huth [this message]
2023-05-15 13:02 ` [PULL 06/21] Add information how to fix common build error on Windows in symlink-install-tree Thomas Huth
2023-05-15 13:02 ` [PULL 07/21] tests: libvirt-ci: Update to commit 'c8971e90ac' to pull in mformat and xorriso Thomas Huth
2023-05-15 13:02 ` [PULL 08/21] tests/lcitool: Add mtools and xorriso and remove genisoimage as dependencies Thomas Huth
2023-05-15 13:02 ` [PULL 09/21] util/async-teardown: wire up query-command-line-options Thomas Huth
2023-05-15 13:02 ` [PULL 10/21] s390x/pv: Fix spurious warning with asynchronous teardown Thomas Huth
2023-05-15 13:02 ` [PULL 11/21] docs/devel: remind developers to run CI container pipeline when updating images Thomas Huth
2023-05-15 13:02 ` [PULL 12/21] docs/about/emulation: fix typo Thomas Huth
2023-05-15 13:02 ` [PULL 13/21] hw/core: Use a callback for target specific query-cpus-fast information Thomas Huth
2023-05-15 13:02 ` [PULL 14/21] cpu: Introduce a wrapper for being able to use TARGET_NAME in common code Thomas Huth
2023-05-15 13:02 ` [PULL 15/21] hw/core: Move machine-qmp-cmds.c into the target independent source set Thomas Huth
2023-05-15 13:02 ` [PULL 16/21] hw/net: Move xilinx_ethlite.c to the target-independent " Thomas Huth
2023-05-15 13:02 ` [PULL 17/21] s390x/tcg: Fix LDER instruction format Thomas Huth
2023-05-15 13:02 ` [PULL 18/21] tests/tcg/multiarch: Make the system memory test work on big-endian Thomas Huth
2023-05-15 13:02 ` [PULL 19/21] tests/tcg/s390x: Enable the multiarch system tests Thomas Huth
2023-05-15 13:02 ` [PULL 20/21] target/s390x: Fix EXECUTE of relative branches Thomas Huth
2023-05-15 13:02 ` [PULL 21/21] tests/tcg/s390x: Test " Thomas Huth
2023-05-15 20:53 ` [PULL 00/21] Tests, docs, s390x and misc patches Richard Henderson
2023-05-16  7:11   ` Thomas Huth

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230515130233.418183-6-thuth@redhat.com \
    --to=thuth@redhat.com \
    --cc=Jonathan.Cameron@huawei.com \
    --cc=qemu-devel@nongnu.org \
    --cc=richard.henderson@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.