Skip to content

Commit 690705c

Browse files
mcaylandbonzini
authored andcommitted
softmmu/ioport.c: make MemoryRegionPortioList owner of portio_list MemoryRegions
Currently when portio_list MemoryRegions are freed using portio_list_destroy() the RCU thread segfaults generating a backtrace similar to that below: #0 0x5555599a34b6 in phys_section_destroy ../softmmu/physmem.c:996 #1 0x5555599a37a3 in phys_sections_free ../softmmu/physmem.c:1011 qemu#2 0x5555599b24aa in address_space_dispatch_free ../softmmu/physmem.c:2430 qemu#3 0x55555996a283 in flatview_destroy ../softmmu/memory.c:292 qemu#4 0x55555a2cb9fb in call_rcu_thread ../util/rcu.c:284 qemu#5 0x55555a29b71d in qemu_thread_start ../util/qemu-thread-posix.c:541 qemu#6 0x7ffff4a0cea6 in start_thread nptl/pthread_create.c:477 qemu#7 0x7ffff492ca2e in __clone (/lib/x86_64-linux-gnu/libc.so.6+0xfca2e) The problem here is that portio_list_destroy() unparents the portio_list MemoryRegions causing them to be freed immediately, however the flatview still has a reference to the MemoryRegion and so causes a use-after-free segfault when the RCU thread next updates the flatview. Solve the lifetime issue by making MemoryRegionPortioList the owner of the portio_list MemoryRegions, and then reparenting them to the portio_list owner. This ensures that they can be accessed as QOM children via the portio_list owner, yet the MemoryRegionPortioList owns the refcount. Update portio_list_destroy() to unparent the MemoryRegion from the portio_list owner (while keeping mrpio->mr live until finalization of the MemoryRegionPortioList), so that the portio_list MemoryRegions remain allocated until flatview_destroy() removes the final refcount upon the next flatview update. Signed-off-by: Mark Cave-Ayland <[email protected]> Reviewed-by: Philippe Mathieu-Daudé <[email protected]> Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 2877068 commit 690705c

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

softmmu/ioport.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ static void portio_list_add_1(PortioList *piolist,
229229
unsigned off_low, unsigned off_high)
230230
{
231231
MemoryRegionPortioList *mrpio;
232+
Object *owner;
233+
char *name;
232234
unsigned i;
233235

234236
/* Copy the sub-list and null-terminate it. */
@@ -245,8 +247,25 @@ static void portio_list_add_1(PortioList *piolist,
245247
mrpio->ports[i].base = start + off_low;
246248
}
247249

248-
memory_region_init_io(&mrpio->mr, piolist->owner, &portio_ops, mrpio,
250+
/*
251+
* The MemoryRegion owner is the MemoryRegionPortioList since that manages
252+
* the lifecycle via the refcount
253+
*/
254+
memory_region_init_io(&mrpio->mr, OBJECT(mrpio), &portio_ops, mrpio,
249255
piolist->name, off_high - off_low);
256+
257+
/* Reparent the MemoryRegion to the piolist owner */
258+
object_ref(&mrpio->mr);
259+
object_unparent(OBJECT(&mrpio->mr));
260+
if (!piolist->owner) {
261+
owner = container_get(qdev_get_machine(), "/unattached");
262+
} else {
263+
owner = piolist->owner;
264+
}
265+
name = g_strdup_printf("%s[*]", piolist->name);
266+
object_property_add_child(owner, name, OBJECT(&mrpio->mr));
267+
g_free(name);
268+
250269
if (piolist->flush_coalesced_mmio) {
251270
memory_region_set_flush_coalesced(&mrpio->mr);
252271
}
@@ -308,6 +327,7 @@ static void memory_region_portio_list_finalize(Object *obj)
308327
{
309328
MemoryRegionPortioList *mrpio = MEMORY_REGION_PORTIO_LIST(obj);
310329

330+
object_unref(&mrpio->mr);
311331
g_free(mrpio->ports);
312332
}
313333

0 commit comments

Comments
 (0)