qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Michael S. Tsirkin" <mst@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Laszlo Ersek" <lersek@redhat.com>, "Ani Sinha" <ani@anisinha.ca>,
	"Ard Biesheuvel" <ardb@kernel.org>,
	"Igor Mammedov" <imammedo@redhat.com>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Peter Maydell" <peter.maydell@linaro.org>,
	"Philippe Mathieu-Daudé" <philmd@linaro.org>,
	qemu-stable@nongnu.org
Subject: [PULL v2 50/51] acpi: cpuhp: fix guest-visible maximum access size to the legacy reg block
Date: Thu, 5 Jan 2023 11:30:27 -0500	[thread overview]
Message-ID: <20230105162836.275244-1-mst@redhat.com> (raw)
In-Reply-To: <20230105091310.263867-1-mst@redhat.com>

From: Laszlo Ersek <lersek@redhat.com>

The modern ACPI CPU hotplug interface was introduced in the following
series (aa1dd39ca307..679dd1a957df), released in v2.7.0:

  1  abd49bc2ed2f docs: update ACPI CPU hotplug spec with new protocol
  2  16bcab97eb9f pc: piix4/ich9: add 'cpu-hotplug-legacy' property
  3  5e1b5d93887b acpi: cpuhp: add CPU devices AML with _STA method
  4  ac35f13ba8f8 pc: acpi: introduce AcpiDeviceIfClass.madt_cpu hook
  5  d2238cb6781d acpi: cpuhp: implement hot-add parts of CPU hotplug
                  interface
  6  8872c25a26cc acpi: cpuhp: implement hot-remove parts of CPU hotplug
                  interface
  7  76623d00ae57 acpi: cpuhp: add cpu._OST handling
  8  679dd1a957df pc: use new CPU hotplug interface since 2.7 machine type

Before patch#1, "docs/specs/acpi_cpu_hotplug.txt" only specified 1-byte
accesses for the hotplug register block.  Patch#1 preserved the same
restriction for the legacy register block, but:

- it specified DWORD accesses for some of the modern registers,

- in particular, the switch from the legacy block to the modern block
  would require a DWORD write to the *legacy* block.

The latter functionality was then implemented in cpu_status_write()
[hw/acpi/cpu_hotplug.c], in patch#8.

Unfortunately, all DWORD accesses depended on a dormant bug: the one
introduced in earlier commit a014ed07bd5a ("memory: accept mismatching
sizes in memory_region_access_valid", 2013-05-29); first released in
v1.6.0.  Due to commit a014ed07bd5a, the DWORD accesses to the *legacy*
CPU hotplug register block would work in spite of the above series *not*
relaxing "valid.max_access_size = 1" in "hw/acpi/cpu_hotplug.c":

> static const MemoryRegionOps AcpiCpuHotplug_ops = {
>     .read = cpu_status_read,
>     .write = cpu_status_write,
>     .endianness = DEVICE_LITTLE_ENDIAN,
>     .valid = {
>         .min_access_size = 1,
>         .max_access_size = 1,
>     },
> };

Later, in commits e6d0c3ce6895 ("acpi: cpuhp: introduce 'Command data 2'
field", 2020-01-22) and ae340aa3d256 ("acpi: cpuhp: spec: add typical
usecases", 2020-01-22), first released in v5.0.0, the modern CPU hotplug
interface (including the documentation) was extended with another DWORD
*read* access, namely to the "Command data 2" register, which would be
important for the guest to confirm whether it managed to switch the
register block from legacy to modern.

This functionality too silently depended on the bug from commit
a014ed07bd5a.

In commit 5d971f9e6725 ('memory: Revert "memory: accept mismatching sizes
in memory_region_access_valid"', 2020-06-26), first released in v5.1.0,
the bug from commit a014ed07bd5a was fixed (the commit was reverted).
That swiftly exposed the bug in "AcpiCpuHotplug_ops", still present from
the v2.7.0 series quoted at the top -- namely the fact that
"valid.max_access_size = 1" didn't match what the guest was supposed to
do, according to the spec ("docs/specs/acpi_cpu_hotplug.txt").

The symptom is that the "modern interface negotiation protocol"
described in commit ae340aa3d256:

> +      Use following steps to detect and enable modern CPU hotplug interface:
> +        1. Store 0x0 to the 'CPU selector' register,
> +           attempting to switch to modern mode
> +        2. Store 0x0 to the 'CPU selector' register,
> +           to ensure valid selector value
> +        3. Store 0x0 to the 'Command field' register,
> +        4. Read the 'Command data 2' register.
> +           If read value is 0x0, the modern interface is enabled.
> +           Otherwise legacy or no CPU hotplug interface available

falls apart for the guest: steps 1 and 2 are lost, because they are DWORD
writes; so no switching happens.  Step 3 (a single-byte write) is not
lost, but it has no effect; see the condition in cpu_status_write() in
patch#8.  And step 4 *misleads* the guest into thinking that the switch
worked: the DWORD read is lost again -- it returns zero to the guest
without ever reaching the device model, so the guest never learns the
switch didn't work.

This means that guest behavior centered on the "Command data 2" register
worked *only* in the v5.0.0 release; it got effectively regressed in
v5.1.0.

To make things *even more* complicated, the breakage was (and remains, as
of today) visible with TCG acceleration only.  Commit 5d971f9e6725 makes
no difference with KVM acceleration -- the DWORD accesses still work,
despite "valid.max_access_size = 1".

As commit 5d971f9e6725 suggests, fix the problem by raising
"valid.max_access_size" to 4 -- the spec now clearly instructs the guest
to perform DWORD accesses to the legacy register block too, for enabling
(and verifying!) the modern block.  In order to keep compatibility for the
device model implementation though, set "impl.max_access_size = 1", so
that wide accesses be split before they reach the legacy read/write
handlers, like they always have been on KVM, and like they were on TCG
before 5d971f9e6725 (v5.1.0).

Tested with:

- OVMF IA32 + qemu-system-i386, CPU hotplug/hot-unplug with SMM,
  intermixed with ACPI S3 suspend/resume, using KVM accel
  (regression-test);

- OVMF IA32X64 + qemu-system-x86_64, CPU hotplug/hot-unplug with SMM,
  intermixed with ACPI S3 suspend/resume, using KVM accel
  (regression-test);

- OVMF IA32 + qemu-system-i386, SMM enabled, using TCG accel; verified the
  register block switch and the present/possible CPU counting through the
  modern hotplug interface, during OVMF boot (bugfix test);

- I do not have any testcase (guest payload) for regression-testing CPU
  hotplug through the *legacy* CPU hotplug register block.

Cc: "Michael S. Tsirkin" <mst@redhat.com>
Cc: Ani Sinha <ani@anisinha.ca>
Cc: Ard Biesheuvel <ardb@kernel.org>
Cc: Igor Mammedov <imammedo@redhat.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Peter Maydell <peter.maydell@linaro.org>
Cc: Philippe Mathieu-Daudé <philmd@linaro.org>
Cc: qemu-stable@nongnu.org
Ref: "IO port write width clamping differs between TCG and KVM"
Link: http://mid.mail-archive.com/aaedee84-d3ed-a4f9-21e7-d221a28d1683@redhat.com
Link: https://lists.gnu.org/archive/html/qemu-devel/2023-01/msg00199.html
Reported-by: Ard Biesheuvel <ardb@kernel.org>
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Tested-by: Ard Biesheuvel <ardb@kernel.org>
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Tested-by: Igor Mammedov <imammedo@redhat.com>
Message-Id: <20230105161804.82486-1-lersek@redhat.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---
 hw/acpi/cpu_hotplug.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/hw/acpi/cpu_hotplug.c b/hw/acpi/cpu_hotplug.c
index 53654f8638..ff14c3f410 100644
--- a/hw/acpi/cpu_hotplug.c
+++ b/hw/acpi/cpu_hotplug.c
@@ -52,6 +52,9 @@ static const MemoryRegionOps AcpiCpuHotplug_ops = {
     .endianness = DEVICE_LITTLE_ENDIAN,
     .valid = {
         .min_access_size = 1,
+        .max_access_size = 4,
+    },
+    .impl = {
         .max_access_size = 1,
     },
 };
-- 
MST



      parent reply	other threads:[~2023-01-05 16:30 UTC|newest]

Thread overview: 72+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-05  9:14 [PULL 00/51] virtio,pc,pci: features, cleanups, fixes Michael S. Tsirkin
2023-01-05  9:14 ` [PULL 01/51] virtio_net: Modify virtio_net_get_config to early return Michael S. Tsirkin
2023-01-05  9:14 ` [PULL 02/51] virtio_net: copy VIRTIO_NET_S_ANNOUNCE if device model has it Michael S. Tsirkin
2023-01-05  9:14 ` [PULL 03/51] vdpa: handle VIRTIO_NET_CTRL_ANNOUNCE in vhost_vdpa_net_handle_ctrl_avail Michael S. Tsirkin
2023-01-05  9:14 ` [PULL 04/51] vdpa: do not handle VIRTIO_NET_F_GUEST_ANNOUNCE in vhost-vdpa Michael S. Tsirkin
2023-01-05  9:14 ` [PULL 05/51] hw/acpi/Kconfig: Rename ACPI_X86_ICH to ACPI_ICH9 Michael S. Tsirkin
2023-01-05  9:14 ` [PULL 06/51] hw/acpi/Kconfig: Add missing dependencies " Michael S. Tsirkin
2023-01-05  9:14 ` [PULL 07/51] hw/acpi/Kconfig: Do not needlessly build TYPE_PIIX4_PM in non-PC/Malta machines Michael S. Tsirkin
2023-01-05  9:14 ` [PULL 08/51] hw/acpi/Kconfig: Add missing dependencies to ACPI_PIIX4 Michael S. Tsirkin
2023-01-05  9:14 ` [PULL 09/51] hw/isa/Kconfig: Add missing dependency to VT82C686 Michael S. Tsirkin
2023-01-05  9:14 ` [PULL 10/51] i386, mips: Resolve redundant ACPI and APM dependencies Michael S. Tsirkin
2023-01-05  9:14 ` [PULL 11/51] hw/ppc/Kconfig: Remove unused dependencies from PEGASOS2 Michael S. Tsirkin
2023-01-05  9:14 ` [PULL 12/51] vhost-user: Refactor vhost acked features saving Michael S. Tsirkin
2023-01-05  9:14 ` [PULL 13/51] vhost-user: Refactor the chr_closed_bh Michael S. Tsirkin
2023-01-05  9:15 ` [PULL 14/51] vhost-user: Fix the virtio features negotiation flaw Michael S. Tsirkin
2023-01-05  9:15 ` [PULL 15/51] virtio: introduce macro VIRTIO_CONFIG_IRQ_IDX Michael S. Tsirkin
2023-01-05  9:15 ` [PULL 16/51] virtio-pci: decouple notifier from interrupt process Michael S. Tsirkin
2023-01-05  9:15 ` [PULL 17/51] virtio-pci: decouple the single vector from the " Michael S. Tsirkin
2023-01-05  9:15 ` [PULL 18/51] vhost: introduce new VhostOps vhost_set_config_call Michael S. Tsirkin
2023-01-05  9:15 ` [PULL 19/51] vhost-vdpa: add support for config interrupt Michael S. Tsirkin
2023-01-05  9:15 ` [PULL 20/51] virtio: add support for configure interrupt Michael S. Tsirkin
2023-01-05  9:15 ` [PULL 21/51] vhost: " Michael S. Tsirkin
2023-01-05  9:21   ` Michael S. Tsirkin
2023-01-05  9:15 ` [PULL 22/51] virtio-net: " Michael S. Tsirkin
2023-01-05  9:15 ` [PULL 23/51] virtio-mmio: " Michael S. Tsirkin
2023-01-05  9:15 ` [PULL 24/51] virtio-pci: " Michael S. Tsirkin
2023-01-05  9:15 ` [PULL 25/51] hw/virtio: Rename virtio_device_find() -> qmp_find_virtio_device() Michael S. Tsirkin
2023-01-05  9:15 ` [PULL 26/51] hw/virtio: Extract QMP QOM-specific functions to virtio-qmp.c Michael S. Tsirkin
2023-01-05  9:15 ` [PULL 27/51] include/hw/pci: Break inclusion loop pci_bridge.h and cxl.h Michael S. Tsirkin
2023-01-05  9:21   ` Michael S. Tsirkin
2023-01-05  9:15 ` [PULL 29/51] include/hw/cxl: Include hw/cxl/*.h where needed Michael S. Tsirkin
2023-01-05  9:15 ` [PULL 30/51] include/hw/pci: Clean up a few things checkpatch.pl would flag Michael S. Tsirkin
2023-01-05  9:15 ` [PULL 31/51] include/hw/pci: Split pci_device.h off pci.h Michael S. Tsirkin
2023-01-05  9:16 ` [PULL 34/51] include/hw/virtio: Break inclusion loop Michael S. Tsirkin
2023-01-05  9:16 ` [PULL 35/51] include: Include headers where needed Michael S. Tsirkin
2023-01-05  9:16 ` [PULL 36/51] include: Don't include qemu/osdep.h Michael S. Tsirkin
2023-01-05  9:16 ` [PULL 37/51] docs/devel: Rules on #include in headers Michael S. Tsirkin
2023-01-09 12:03   ` Markus Armbruster
2023-01-05  9:16 ` [PULL 38/51] vdpa-dev: get iova range explicitly Michael S. Tsirkin
2023-01-05  9:16 ` [PULL 39/51] vdpa: harden the error path if get_iova_range failed Michael S. Tsirkin
2023-01-05  9:16 ` [PULL 40/51] vhost: simplify vhost_dev_enable_notifiers Michael S. Tsirkin
2023-01-05  9:16 ` [PULL 41/51] vhost: configure all host notifiers in a single MR transaction Michael S. Tsirkin
2023-01-05  9:16 ` [PULL 42/51] vdpa: commit all host notifier MRs " Michael S. Tsirkin
2023-01-05  9:16 ` [PULL 43/51] virtio-pci: fix proxy->vector_irqfd leak in virtio_pci_set_guest_notifiers Michael S. Tsirkin
2023-01-05  9:16 ` [PULL 44/51] tests: virt: Allow changes to PPTT test table Michael S. Tsirkin
2023-01-05  9:16 ` [PULL 45/51] hw/acpi/aml-build: Only generate cluster node in PPTT when specified Michael S. Tsirkin
2023-05-10 10:13   ` Philippe Mathieu-Daudé
2023-05-13  7:22     ` Yicong Yang via
2023-01-05  9:16 ` [PULL 46/51] tests: virt: Update expected ACPI tables for virt test Michael S. Tsirkin
2023-01-05  9:16 ` [PULL 47/51] tests: acpi: Add and whitelist *.topology blobs Michael S. Tsirkin
2023-01-05  9:16 ` [PULL 48/51] tests: acpi: aarch64: Add topology test for aarch64 Michael S. Tsirkin
2023-01-05  9:17 ` [PULL 49/51] tests: acpi: aarch64: Add *.topology tables Michael S. Tsirkin
2023-01-05  9:17 ` [PULL 50/51] acpi: cpuhp: fix guest-visible maximum access size to the legacy reg block Michael S. Tsirkin
2023-01-05  9:56   ` Michael S. Tsirkin
2023-01-05 16:01     ` Laszlo Ersek
2023-01-05 16:29       ` Philippe Mathieu-Daudé
2023-01-05 16:35         ` Michael S. Tsirkin
2023-01-05  9:17 ` [PULL 51/51] vhost-scsi: fix memleak of vsc->inflight Michael S. Tsirkin
2023-01-05  9:21 ` [PULL 28/51] include/hw/cxl: Move typedef PXBDev to cxl.h, and put it to use Michael S. Tsirkin
2023-01-05  9:22 ` [PULL 32/51] include/hw/pci: Include hw/pci/pci.h where needed Michael S. Tsirkin
2023-01-05  9:22 ` [PULL 33/51] include/hw/cxl: Break inclusion loop cxl_pci.h and cxl_cdat_h Michael S. Tsirkin
2023-01-05  9:56 ` [PULL 00/51] virtio,pc,pci: features, cleanups, fixes Michael S. Tsirkin
2023-01-05 16:31   ` Michael S. Tsirkin
2023-01-05 21:04     ` Peter Maydell
2023-01-05 21:50       ` Michael S. Tsirkin
2023-01-05 21:53       ` Michael S. Tsirkin
2023-01-06 15:29         ` Peter Maydell
2023-01-08 13:57           ` Michael S. Tsirkin
2023-01-09 13:43             ` Markus Armbruster
2023-01-10 18:28               ` Markus Armbruster
2023-01-09 15:54             ` Peter Maydell
2023-01-05 16:30 ` Michael S. Tsirkin [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=20230105162836.275244-1-mst@redhat.com \
    --to=mst@redhat.com \
    --cc=ani@anisinha.ca \
    --cc=ardb@kernel.org \
    --cc=imammedo@redhat.com \
    --cc=lersek@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=philmd@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-stable@nongnu.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 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).