From: Stefan Hajnoczi <stefanha@gmail.com>
To: Aleksey Kuleshov <rndfax@yandex.ru>
Cc: qemu-devel <qemu-devel@nongnu.org>,
"Michael S. Tsirkin" <mst@redhat.com>,
Marcel Apfelbaum <marcel@redhat.com>
Subject: Re: [Qemu-devel] BUG? Using memory after freeing
Date: Thu, 8 Mar 2018 15:51:45 +0000 [thread overview]
Message-ID: <20180308155145.GD2769@stefanha-x1.localdomain> (raw)
In-Reply-To: <4509561520419741@web56g.yandex.ru>
[-- Attachment #1: Type: text/plain, Size: 2181 bytes --]
On Wed, Mar 07, 2018 at 01:49:01PM +0300, Aleksey Kuleshov wrote:
> Hello!
Thanks for the email! I have CCed the PCI maintainers, Michael Tsirkin
and Marcel Apfelbaum.
In the future you can find out who to contact using:
$ scripts/get_maintainers -f hw/pci/pci_bridge.c
> Explanation of what I saw is follows.
>
> In hw/pci/pci_bridge.c function pci_bridge_update_mappings does follows:
> ```
> void pci_bridge_update_mappings(PCIBridge *br)
> {
> 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);
> br->windows = pci_bridge_region_init(br);
> memory_region_transaction_commit();
> pci_bridge_region_cleanup(br, w);
> }
> ```
>
> It calls memory_region_transaction_commit which calls flatview_unref:
> ```
> static void flatview_unref(FlatView *view)
> {
> if (atomic_fetch_dec(&view->ref) == 1) {
> trace_flatview_destroy_rcu(view, view->root);
> assert(view->root);
> call_rcu(view, flatview_destroy, rcu);
> }
> }
> ```
> As far as I understood, call_rcu can be considered as a deferred call to flatview_destroy.
>
> Then in pci_bridge_update_mappings there is a call to pci_bridge_region_cleanup which does:
> ```
> static void pci_bridge_region_cleanup(PCIBridge *br, PCIBridgeWindows *w)
> {
> object_unparent(OBJECT(&w->alias_io));
> object_unparent(OBJECT(&w->alias_mem));
> object_unparent(OBJECT(&w->alias_pref_mem));
> 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);
> }
> ```
> Note g_free(w). "w" holds MemoryRegions, which are part of that FlatView
> which is going to be destroyed some time later in the future.
>
> When RCU thread kicks in, flatview_destroy is called on MemoryRegions which were
> part of that "w" which is now freed and QEMU seg faults.
>
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 455 bytes --]
prev parent reply other threads:[~2018-03-08 15:51 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-03-07 10:49 [Qemu-devel] BUG? Using memory after freeing Aleksey Kuleshov
2018-03-08 15:51 ` Stefan Hajnoczi [this message]
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=20180308155145.GD2769@stefanha-x1.localdomain \
--to=stefanha@gmail.com \
--cc=marcel@redhat.com \
--cc=mst@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=rndfax@yandex.ru \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).