qemu/hw/core
Peter Xu 6621883f93 migration: Fix potential race on postcopy_qemufile_src
postcopy_qemufile_src object should be owned by one thread, either the main
thread (e.g. when at the beginning, or at the end of migration), or by the
return path thread (when during a preempt enabled postcopy migration).  If
that's not the case the access to the object might be racy.

postcopy_preempt_shutdown_file() can be potentially racy, because it's
called at the end phase of migration on the main thread, however during
which the return path thread hasn't yet been recycled; the recycle happens
in await_return_path_close_on_source() which is after this point.

It means, logically it's posslbe the main thread and the return path thread
are both operating on the same qemufile.  While I don't think qemufile is
thread safe at all.

postcopy_preempt_shutdown_file() used to be needed because that's where we
send EOS to dest so that dest can safely shutdown the preempt thread.

To avoid the possible race, remove this only place that a race can happen.
Instead we figure out another way to safely close the preempt thread on
dest.

The core idea during postcopy on deciding "when to stop" is that dest will
send a postcopy SHUT message to src, telling src that all data is there.
Hence to shut the dest preempt thread maybe better to do it directly on
dest node.

This patch proposed such a way that we change postcopy_prio_thread_created
into PreemptThreadStatus, so that we kick the preempt thread on dest qemu
by a sequence of:

  mis->preempt_thread_status = PREEMPT_THREAD_QUIT;
  qemu_file_shutdown(mis->postcopy_qemufile_dst);

While here shutdown() is probably so far the easiest way to kick preempt
thread from a blocked qemu_get_be64().  Then it reads preempt_thread_status
to make sure it's not a network failure but a willingness to quit the
thread.

We could have avoided that extra status but just rely on migration status.
The problem is postcopy_ram_incoming_cleanup() is just called early enough
so we're still during POSTCOPY_ACTIVE no matter what.. So just make it
simple to have the status introduced.

One flag x-preempt-pre-7-2 is added to keep old pre-7.2 behaviors of
postcopy preempt.

Fixes: 9358982744 ("migration: Send requested page directly in rp-return thread")
Signed-off-by: Peter Xu <peterx@redhat.com>
Reviewed-by: Juan Quintela <quintela@redhat.com>
Signed-off-by: Juan Quintela <quintela@redhat.com>
2023-04-12 21:44:38 +02:00
..
bus.c qbus: Rename qbus_create() to qbus_new() 2021-09-30 13:44:08 +01:00
clock-vmstate.c clock-vmstate: Add missing END_OF_LIST 2022-03-02 18:12:40 +00:00
clock.c misc: fix commonly doubled up words 2022-08-01 11:58:02 +02:00
cpu-common.c accel: introduce accelerator blocker API 2023-01-11 09:59:39 +01:00
cpu-sysemu.c hw/core/cpu-sysemu: used cached class in cpu_asidx_from_attrs 2022-10-03 20:53:30 -07:00
fw-path-provider.c Include qemu/module.h where needed, drop it from qemu-common.h 2019-06-12 13:18:33 +02:00
generic-loader.c hw/core/loader: return image sizes as ssize_t 2022-06-10 09:31:42 +10:00
gpio.c hw/qdev: Rename qdev_connect_gpio_out*() 'input_pin' parameter 2021-12-31 13:21:36 +01:00
guest-loader.c Mark remaining global TypeInfo instances as const 2022-02-21 13:30:20 +00:00
guest-loader.h hw/core: implement a guest-loader to support static hypervisor guests 2021-03-10 15:34:11 +00:00
hotplug-stubs.c hw/core: Restrict hotplug to system emulation 2021-11-01 19:44:11 +01:00
hotplug.c call HotplugHandler->plug() as the last step in device realization 2018-10-19 13:44:12 +02:00
irq.c hw/irq: Declare QOM macros using OBJECT_DECLARE_SIMPLE_TYPE() 2023-02-27 13:27:05 +00:00
Kconfig hw/core: Only build guest-loader if libfdt is available 2021-03-17 07:17:46 +00:00
loader-fit.c hw/mips/boston: Fix Lesser GPL version number 2020-11-03 16:51:13 +01:00
loader.c *: Add missing includes of qemu/error-report.h 2023-03-22 15:06:57 +00:00
machine-hmp-cmds.c machine: Move HMP commands from monitor/ to hw/core/ 2023-02-04 07:56:54 +01:00
machine-qmp-cmds.c qapi: Add 'acpi' field to 'query-machines' output 2023-03-01 11:36:53 +01:00
machine-smp.c *: Add missing includes of qemu/error-report.h 2023-03-22 15:06:57 +00:00
machine.c migration: Fix potential race on postcopy_qemufile_src 2023-04-12 21:44:38 +02:00
meson.build hw/core: Move the ARM sysbus-fdt to core 2022-04-29 10:48:26 +10:00
nmi.c hw/core: Improve error message when machine doesn't provide NMIs 2023-02-23 14:10:17 +01:00
null-machine.c Do not include sysemu/sysemu.h if it's not really necessary 2021-05-02 17:24:50 +02:00
numa.c qapi machine: Elide redundant has_FOO in generated C 2022-12-14 20:04:47 +01:00
or-irq.c hw: Replace qemu_or_irq typedef by OrIRQState 2023-02-27 13:27:05 +00:00
platform-bus.c nomaintainer: Fix Lesser GPL version number 2020-11-15 17:04:40 +01:00
ptimer.c replay: Extract core API to 'exec/replay-core.h' 2023-02-27 22:29:01 +01:00
qdev-clock.c Drop more useless casts from void * to pointer 2022-12-14 16:19:35 +01:00
qdev-fw.c Include hw/qdev-properties.h less 2019-08-16 13:31:53 +02:00
qdev-hotplug.c hw/core: Extract hotplug-related functions to qdev-hotplug.c 2021-11-01 19:44:11 +01:00
qdev-prop-internal.h qdev: Make qdev_propinfo_get_uint16() static 2020-12-15 10:02:07 -05:00
qdev-properties-system.c hw/core/qdev-properties-system: Allow the 'slew' policy only on x86 2023-01-13 16:22:57 +01:00
qdev-properties.c qdev-properties: Add a new macro with bitmask check for uint64_t property 2022-05-14 12:32:41 +02:00
qdev.c hw/qdev: Constify DeviceState* argument of qdev_get_parent_bus() 2023-02-27 22:29:01 +01:00
register.c hw/core/register: Add more 64-bit utilities 2021-09-01 11:59:12 +10:00
reset.c reset: allow registering handlers that aren't called by snapshot loading 2022-10-27 11:34:31 +01:00
resettable.c hw/core/resettable: fix reset level counting 2022-10-27 10:27:23 +01:00
split-irq.c qdev: set properties with device_class_set_props() 2020-01-24 20:59:15 +01:00
stream.c hw/core/stream: Rename StreamSlave as StreamSink 2020-12-10 12:15:04 -05:00
sysbus-fdt.c hw/core: Tidy up unnecessary casting away of const 2022-10-22 22:50:27 +02:00
sysbus.c bulk: Rename TARGET_FMT_plx -> HWADDR_FMT_plx 2023-01-18 11:14:34 +01:00
trace-events hw: Remove device_legacy_reset() 2022-12-16 15:55:32 +00:00
trace.h trace: switch position of headers to what Meson requires 2020-08-21 06:18:24 -04:00
uboot_image.h hw/core: Sync uboot_image.h from U-Boot v2022.01 2022-05-24 10:38:50 +10:00
vm-change-state-handler.c sysemu: Split sysemu/runstate.h off sysemu/sysemu.h 2019-08-16 13:37:36 +02:00
vmstate-if.c vmstate: add qom interface to get id 2020-01-06 18:41:32 +04:00