[PATCH 0/3] tests/functional/ppc64: add pseries CPU hotplug and security caps tests

Anushree Mathur posted 3 patches 3 weeks, 2 days ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20261030131025.32219-1-anushree.mathur@linux.ibm.com
Maintainers: Chinmay Rath <rathc@linux.ibm.com>, Nicholas Piggin <npiggin@gmail.com>, Glenn Miles <milesg@linux.ibm.com>, Harsh Prateek Bora <harshpb@linux.ibm.com>, Amit Machhiwal <amachhiw@linux.ibm.com>
MAINTAINERS                                 |   2 +
tests/functional/ppc64/meson.build          |   3 +
tests/functional/ppc64/test_hotplug_cpu.py  | 183 ++++++++++++++++++++
tests/functional/ppc64/test_pseries_caps.py | 178 +++++++++++++++++++
4 files changed, 366 insertions(+)
create mode 100755 tests/functional/ppc64/test_hotplug_cpu.py
create mode 100755 tests/functional/ppc64/test_pseries_caps.py
[PATCH 0/3] tests/functional/ppc64: add pseries CPU hotplug and security caps tests
Posted by Anushree Mathur 3 weeks, 2 days ago
This series adds two new functional tests for the pseries (sPAPR/PAPR)
machine type:

Patch 1 adds a DLPAR CPU hotplug/unplug test.  It boots a minimal
buildroot Linux guest with one active core and uses QMP device_add /
device_del to attach and detach a second core at runtime, verifying
the guest kernel's view via /sys/devices/system/cpu/online.  Two
variants are provided: TCG (runs on any host) and KVM (ppc64le only,
with an explicit kvm-hv module check).

Patch 2 adds a lightweight no-boot capability test suite.  Each test
starts QEMU halted (-S) and exercises the QMP interface to verify
that security capability properties (cap-cfpc, cap-sbbc, cap-ibs,
cap-ccf-assist, cap-nested-hv) are accepted, that versioned compat
machines (pseries-7.2, pseries-9.2, pseries-10.0) can be
instantiated and are correctly marked non-current, and that
cap-hpt-max-page-size is reachable via the QOM tree.  All tests
complete in under 5 seconds and are registered in the quick suite.

Patch 3 registers both new test files in MAINTAINERS under the
sPAPR (pseries) section.

Both tests have been run on a ppc64le Power11 LPAR and pass cleanly
under both TCG and KVM.

GitLab CI pipeline (check-dco, check-patch, build-system-* pass):
  https://gitlab.com/Anushree-Mathur1/qemu/-/pipelines/3003562135

Anushree Mathur (3):
  tests/functional/ppc64: add pseries CPU hotplug test
  tests/functional/ppc64: add pseries security caps test
  MAINTAINERS: add pseries CPU hotplug and caps functional tests

 MAINTAINERS                                 |   2 +
 tests/functional/ppc64/meson.build          |   3 +
 tests/functional/ppc64/test_hotplug_cpu.py  | 183 ++++++++++++++++++++
 tests/functional/ppc64/test_pseries_caps.py | 178 +++++++++++++++++++
 4 files changed, 366 insertions(+)
 create mode 100755 tests/functional/ppc64/test_hotplug_cpu.py
 create mode 100755 tests/functional/ppc64/test_pseries_caps.py


base-commit: b574eaf640f02c4edf28cbee6417378f0d79cda0
-- 
2.55.0
[PATCH v2 0/4] tests/functional/ppc64: add pseries hotplug and caps tests
Posted by Anushree Mathur 3 days, 21 hours ago
Hi all,

This series adds three new functional tests for the pseries (sPAPR/PAPR)
machine type and registers them in MAINTAINERS.

Patch 1 adds test_hotplug_cpu.py.  It boots a buildroot guest with one
core (-smp 2,threads=2,cores=1,maxcpus=4), hot-adds a second core with
QMP device_add and waits until /sys/devices/system/cpu/online changes
from 0-1 to 0-3.  It then hot-removes the core with device_del and waits
for the guest to be back at 0-1.  It runs with TCG and, if available,
with KVM.

Patch 2 adds test_pseries_caps.py, a quick test that does not boot a
guest.  QEMU is started halted (-S) and QMP is used to check that
pseries starts with the security capability options (cap-cfpc,
cap-sbbc, cap-ibs, cap-ccf-assist, cap-nested-hv), that pseries-7.2,
9.2 and 10.0 can be instantiated, and that cap-hpt-max-page-size is
visible under /machine in the QOM tree.

Patch 3 adds test_hotplug_blk.py.  It boots a similar guest with a
second PCI host bridge (phb1), hot-adds a virtio-blk-pci disk backed by
a null-co node with QMP device_add and waits for /dev/vda to appear in
the guest.  It then hot-removes the disk with device_del and waits for
the DEVICE_DELETED event and for /dev/vda to disappear.  The guest
needs rtas_errd and drmgr to enumerate the new device, so the test uses
a rootfs that includes them.  I raised the patch that adds this image
(rootfs-hotplug.ext2) to qemu-ppc-boot, see
https://github.com/raphael502/qemu-ppc-boot/pull/1.  It has been merged,
so the test downloads the image from that repository.  The test runs
with TCG, and also with KVM if KVM is enabled.

Patch 4 adds the MAINTAINERS entries for the three tests.

Testing
-------
v1 of the CPU hotplug, caps and virtio-blk tests were run on a ppc64le
POWER host with both TCG and KVM and passed.  The changes below do not
touch the test logic.  checkpatch.pl reports no errors or warnings on
any patch (apart from the expected "MAINTAINERS needs updating" notices
on patches 1-3, resolved by patch 4), and pylint with
tests/functional/pylintrc reports nothing for the new files.

CI
--
GitLab pipeline: https://gitlab.com/Anushree-Mathur1/qemu/-/pipelines/3084104292
The jobs that fail are all allow_failure: Rust nightly (bitops.rs),
tuxrun/armbian asset downloads, and aarch64 timeout/migration tests.
None of them involve this series, which only adds ppc64 tests and
MAINTAINERS entries.

Links to v1:
- v1 for CPU hotplug and caps tests:
  https://lore.kernel.org/qemu-devel/20261030131025.32219-1-anushree.mathur@linux.ibm.com/
- RFC v1 for virtio-blk hotplug test:
  https://lore.kernel.org/qemu-devel/20261103141938.41147-1-anushree.mathur@linux.ibm.com/

Changes in v2:
- Combined the two v1 postings above into one series.
- Patch 1: buildroot kernel and rootfs assets now point to
  raphael502/qemu-ppc-boot instead of legoater/qemu-ppc-boot.  They are
  the same files; the sha256 values are unchanged and were re-verified.
  Wrapped the long URL lines to keep checkpatch quiet.
- Patch 2: reworded the commit message to say what is actually checked
  (the options are accepted and the machine starts), fixed docstrings
  that did not match the code, and dropped the skipped
  query-cpu-model-expansion placeholder test.
- Patch 3: no longer an RFC.  v1 was an RFC because rootfs-hotplug.ext2
  was not available yet; it is now merged in qemu-ppc-boot and the
  asset URLs point to it (the sha256 is unchanged).  The vmlinux asset
  also points to qemu-ppc-boot, and the test file is now executable
  like the other functional tests.
- Patch 4: a single MAINTAINERS patch covering all three tests
  (previously a separate patch for the CPU and caps tests, and an
  entry inside the virtio-blk patch).
- Rebased on current master (6883fb508c).

Anushree Mathur (4):
  tests/functional/ppc64: add pseries CPU hotplug test
  tests/functional/ppc64: add pseries security caps test
  tests/functional/ppc64: add pseries virtio-blk hotplug test
  MAINTAINERS: add pseries hotplug and caps functional tests

 MAINTAINERS                                 |   3 +
 tests/functional/ppc64/meson.build          |   5 +
 tests/functional/ppc64/test_hotplug_blk.py  | 188 ++++++++++++++++++++
 tests/functional/ppc64/test_hotplug_cpu.py  | 185 +++++++++++++++++++
 tests/functional/ppc64/test_pseries_caps.py | 162 +++++++++++++++++
 5 files changed, 543 insertions(+)
 create mode 100755 tests/functional/ppc64/test_hotplug_blk.py
 create mode 100755 tests/functional/ppc64/test_hotplug_cpu.py
 create mode 100755 tests/functional/ppc64/test_pseries_caps.py


base-commit: 6883fb508cb7a93e2e7b44c0582bfd9fc2196d4d
-- 
2.55.0
[PATCH v2 1/4] tests/functional/ppc64: add pseries CPU hotplug test
Posted by Anushree Mathur 3 days, 21 hours ago
Add a functional test that exercises DLPAR (Dynamic Logical
Partitioning) CPU hotplug and unplug on the pseries machine type.

The pseries machine supports adding and removing CPU cores at runtime
via RTAS calls made by the guest.  This test boots a minimal Linux
kernel with a single core (-smp 2,threads=2,cores=1,maxcpus=4), then
uses QMP device_add to attach a second core, verifies the guest sees
it via /sys/devices/system/cpu/online, and then removes it again.

The CPU driver name is detected dynamically via query-hotpluggable-cpus
so the test remains correct when the default pseries CPU changes in a
future QEMU release (e.g. power10 -> power11).

Two variants are provided:
  - test_pseries_cpu_hotplug_tcg: runs under TCG on any host arch.
  - test_pseries_cpu_hotplug_kvm: runs under KVM on ppc64le hosts with
    kvm-hv loaded; skipped automatically elsewhere.  An explicit
    /proc/modules check for kvm_hv is added because on ppc64le the base
    kvm module can create /dev/kvm without kvm-hv, which would cause the
    test to error rather than skip.

x-vof=on is intentionally NOT used: VOF skips the SLOF firmware path
that sets up the RTAS event interrupt, causing CPU hotplug events to be
silently dropped by the guest kernel.

Signed-off-by: Anushree Mathur <anushree.mathur@linux.ibm.com>
---
v2: assets now from raphael502/qemu-ppc-boot instead of
legoater/qemu-ppc-boot (same files, same sha256); wrapped long URL
lines for checkpatch.

 tests/functional/ppc64/meson.build         |   2 +
 tests/functional/ppc64/test_hotplug_cpu.py | 185 +++++++++++++++++++++
 2 files changed, 187 insertions(+)
 create mode 100755 tests/functional/ppc64/test_hotplug_cpu.py

diff --git a/tests/functional/ppc64/meson.build b/tests/functional/ppc64/meson.build
index cb3c745624..45468087cb 100644
--- a/tests/functional/ppc64/meson.build
+++ b/tests/functional/ppc64/meson.build
@@ -2,6 +2,7 @@
 
 test_ppc64_timeouts = {
   'fadump' : 480,
+  'hotplug_cpu' : 480,
   'hv' : 1000,
   'mac99' : 120,
   'openbsd' : 240,
@@ -19,6 +20,7 @@ tests_ppc64_system_quick = [
 tests_ppc64_system_thorough = [
   'e500',
   'fadump',
+  'hotplug_cpu',
   'hv',
   'mac99',
   'openbsd',
diff --git a/tests/functional/ppc64/test_hotplug_cpu.py b/tests/functional/ppc64/test_hotplug_cpu.py
new file mode 100755
index 0000000000..bd01bc2022
--- /dev/null
+++ b/tests/functional/ppc64/test_hotplug_cpu.py
@@ -0,0 +1,185 @@
+#!/usr/bin/env python3
+# Copyright (c) 2025 IBM Corporation
+#
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+import platform
+
+from qemu_test import LinuxKernelTest, Asset
+from qemu_test import exec_command_and_wait_for_pattern
+
+
+class PseriesCpuHotplug(LinuxKernelTest):
+    """
+    Test DLPAR CPU hotplug/unplug on the pseries machine type.
+
+    The pseries machine implements the PAPR CPU hotplug protocol: the
+    hypervisor presents Dynamic Reconfiguration Connectors (DRCs) to
+    the guest, and the guest uses RTAS calls to accept or reject CPUs
+    that are offered or withdrawn.
+
+    Topology used: -smp 2,threads=2,cores=1,maxcpus=4 — 1 active core
+    (cpu0+cpu1 online), max 2 cores.  Hot-add core-id=2 → online goes
+    0-3; hot-remove → back to 0-1.  Verified via /sys/.../cpu/online.
+
+    Two variants are provided:
+      * test_pseries_cpu_hotplug_tcg — uses TCG, runs on any host arch.
+      * test_pseries_cpu_hotplug_kvm — uses KVM, requires a ppc64le host
+        with kvm-hv loaded; skipped automatically on other hosts.
+    """
+
+    timeout = 480
+
+    # Buildroot image built for pseries (ppc64le little-endian paravirtual).
+    ASSET_KERNEL = Asset(
+        ('https://github.com/raphael502/qemu-ppc-boot/raw/refs/heads/main/'
+         'buildroot/qemu_ppc64le_pseries-2023.11-8-gdcd9f0f6eb-20240104/'
+         'vmlinux'),
+        'fc20d5820ad83ed514ac5f88af7e2692d47ab7f36dab290cdb137d05b98502f5')
+
+    ASSET_ROOTFS = Asset(
+        ('https://github.com/raphael502/qemu-ppc-boot/raw/refs/heads/main/'
+         'buildroot/qemu_ppc64le_pseries-2023.11-8-gdcd9f0f6eb-20240104/'
+         'rootfs.ext2'),
+        '6fd31e2300e9921d1192aa5c997760fc8bc40a18513cc466c56902f72d61fd23')
+
+    def _boot_pseries(self, accel):
+        """
+        Boot pseries with 1 core present and room for a second core.
+
+        -smp 2,threads=2,cores=1,maxcpus=4 gives us:
+          * 1 active core  → cpu0 + cpu1 (2 SMT threads)
+          * 1 spare core slot that can be filled by device_add
+
+        accel must be 'tcg' or 'kvm'.
+        """
+        kernel_path = self.ASSET_KERNEL.fetch()
+        rootfs_path = self.ASSET_ROOTFS.fetch()
+
+        self.set_machine('pseries')
+        # Do NOT use x-vof=on here — VOF skips the SLOF firmware path that
+        # sets up the RTAS event interrupt (epow/check-exception).  Without
+        # SLOF, the guest kernel's pseries_hp_wq workqueue is never triggered
+        # and CPU hotplug RTAS events are silently dropped.
+        if accel == 'tcg':
+            self.vm.add_args('-accel', 'tcg,thread=multi')
+        else:
+            self.vm.add_args('-accel', 'kvm')
+        self.vm.add_args('-smp', '2,threads=2,cores=1,maxcpus=4')
+        self.vm.add_args('-m', '512M')
+        self.vm.set_console()
+        self.vm.add_args('-drive',
+                         f'file={rootfs_path},if=scsi,index=0,format=raw')
+        self.vm.add_args(
+            '-kernel', kernel_path,
+            '-append', 'root=/dev/sda console=hvc0 rootwait printk.time=0',
+        )
+        self.vm.launch()
+        self.wait_for_console_pattern('buildroot login:')
+        exec_command_and_wait_for_pattern(self, 'root', '#')
+
+    def _get_hotplug_cpu_driver(self):
+        """
+        Return the spapr-cpu-core driver name for an empty hotplug slot.
+
+        query-hotpluggable-cpus lists all possible CPU slots; entries
+        without a 'qom-path' key are currently empty.  The 'type' field
+        of any empty slot gives the driver name to pass to device_add.
+        This avoids hard-coding a CPU version string (e.g.
+        'power11_v2.0-spapr-cpu-core') that would break when the default
+        pseries CPU changes in a future QEMU release.
+        """
+        cpus = self.vm.cmd('query-hotpluggable-cpus')
+        for entry in cpus:
+            if 'qom-path' not in entry:
+                return entry['type']
+        self.fail('No empty hotpluggable CPU slot found')
+
+    def _do_cpu_hotplug(self):
+        """
+        Shared hotplug/unplug logic, called after the guest has booted.
+
+        Confirms cpu0+cpu1 are online, hot-adds a second core (cpu2+cpu3),
+        verifies they come online, hot-removes the core, and verifies they
+        go offline.  Finishes with a clean guest poweroff.
+        """
+        # Confirm initial state: cpu0 and cpu1 online, cpu2 offline/absent.
+        # Use /sys/devices/system/cpu/online which lists only truly online
+        # CPUs — sysfs cpu dirs exist for possible CPUs too so ls is wrong.
+        exec_command_and_wait_for_pattern(
+            self,
+            'cat /sys/devices/system/cpu/online',
+            '0-1',
+        )
+
+        # Detect the correct driver for the empty hotplug slot dynamically
+        # so the test works regardless of which CPU version pseries defaults
+        # to (power10, power11, etc.).
+        driver = self._get_hotplug_cpu_driver()
+
+        # Hot-add core at core-id=2 (supplies threads cpu2 and cpu3 in SMT-2).
+        # With threads=2 the available hotplug slot is core-id=2.
+        self.vm.cmd(
+            'device_add',
+            driver=driver,
+            id='hotcpu1',
+            **{'core-id': 2},
+        )
+
+        # Poll until cpu2+cpu3 are online.  The sentinel is built using a
+        # shell variable (echo hotplug_cpu_$R) so the full string
+        # 'hotplug_cpu_done' never appears in the command text.  This avoids
+        # a false match on the shell's echo of the typed command, which
+        # exec_command_and_wait_for_pattern scans before any output arrives.
+        exec_command_and_wait_for_pattern(
+            self,
+            'until [ "$(cat /sys/devices/system/cpu/online)" = "0-3" ];'
+            ' do sleep 0.5; done; R=done; echo hotplug_cpu_$R',
+            'hotplug_cpu_done',
+        )
+
+        # Hot-remove the core.
+        self.vm.cmd('device_del', id='hotcpu1')
+
+        # Wait until the kernel acknowledges the removal.
+        exec_command_and_wait_for_pattern(
+            self,
+            'until [ "$(cat /sys/devices/system/cpu/online)" = "0-1" ];'
+            ' do sleep 0.2; done; R=done; echo unplug_cpu_$R',
+            'unplug_cpu_done',
+        )
+
+        exec_command_and_wait_for_pattern(self, 'poweroff', '#')
+        self.vm.wait()
+
+    def test_pseries_cpu_hotplug_tcg(self):
+        """Hot-add then hot-remove a CPU core on pseries using TCG."""
+        self.require_accelerator('tcg')
+        self._boot_pseries('tcg')
+        self._do_cpu_hotplug()
+
+    def test_pseries_cpu_hotplug_kvm(self):
+        """Hot-add then hot-remove a CPU core on pseries using KVM.
+
+        Requires a ppc64le host with kvm-hv loaded; skipped automatically
+        on other hosts.
+        """
+        self.require_accelerator('kvm')
+        # On ppc64le the base kvm module can be loaded (and /dev/kvm created)
+        # without kvm-hv, so checking /dev/kvm alone is not sufficient.
+        # Verify that kvm_hv is actually present in /proc/modules.
+        # This check is ppc64-specific; other arches (x86, arm, ...) use
+        # different KVM modules and rely solely on /dev/kvm.
+        if platform.machine() in ('ppc64le', 'ppc64'):
+            try:
+                with open('/proc/modules', encoding='utf-8') as f:
+                    if not any(line.split()[0] == 'kvm_hv' for line in f):
+                        self.skipTest('kvm-hv module is not loaded')
+            except OSError:
+                pass
+        self._boot_pseries('kvm')
+        self._do_cpu_hotplug()
+
+
+if __name__ == '__main__':
+    PseriesCpuHotplug.main()
-- 
2.55.0


[PATCH v2 2/4] tests/functional/ppc64: add pseries security caps test
Posted by Anushree Mathur 3 days, 21 hours ago
Add a quick functional test (no Linux boot required) that checks that
the pseries machine type accepts its security capability options and
that backward-compatible versioned machine types can be instantiated,
using the QMP interface.

Each test starts QEMU with -S (halted) so no firmware or kernel is
needed.  The tests cover:

  * test_pseries_in_query_machines: pseries appears in query-machines
    and is marked is-default=true.
  * test_pseries_security_caps_override: cap-cfpc, cap-sbbc, cap-ibs
    and cap-ccf-assist overrides are accepted and the machine starts.
  * test_pseries_nested_hv_cap: cap-nested-hv=on is accepted.
  * test_pseries_compat_7_2/9_2/10_0: versioned compat machines can be
    instantiated and are not marked is-current.
  * test_pseries_cap_hpt_max_page_size: cap-hpt-max-page-size is
    reachable via the QOM tree at /machine.

All tests complete in under 5 seconds each and are registered in the
tests_ppc64_system_quick list.

Signed-off-by: Anushree Mathur <anushree.mathur@linux.ibm.com>
---
v2: reworded the commit message to say what is actually checked,
fixed docstrings that did not match the code and dropped the skipped
query-cpu-model-expansion placeholder test.

 tests/functional/ppc64/meson.build          |   1 +
 tests/functional/ppc64/test_pseries_caps.py | 162 ++++++++++++++++++++
 2 files changed, 163 insertions(+)
 create mode 100755 tests/functional/ppc64/test_pseries_caps.py

diff --git a/tests/functional/ppc64/meson.build b/tests/functional/ppc64/meson.build
index 45468087cb..d91209f041 100644
--- a/tests/functional/ppc64/meson.build
+++ b/tests/functional/ppc64/meson.build
@@ -14,6 +14,7 @@ test_ppc64_timeouts = {
 
 tests_ppc64_system_quick = [
   'migration',
+  'pseries_caps',
   'vmstate',
 ]
 
diff --git a/tests/functional/ppc64/test_pseries_caps.py b/tests/functional/ppc64/test_pseries_caps.py
new file mode 100755
index 0000000000..9ef3f48bb1
--- /dev/null
+++ b/tests/functional/ppc64/test_pseries_caps.py
@@ -0,0 +1,162 @@
+#!/usr/bin/env python3
+# Copyright (c) 2025 IBM Corporation
+#
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+from qemu_test import QemuSystemTest
+
+
+class PseriesCapsTest(QemuSystemTest):
+    """
+    Quick (no-boot) tests for pseries machine capabilities.
+
+    Each test starts QEMU with -S (halted) so no firmware or kernel is
+    needed.  The tests exercise the QMP interface to verify that:
+
+      * The machine type is visible in 'query-machines' output.
+      * Known security capability properties (cap-cfpc, cap-sbbc,
+        cap-ibs, cap-ccf-assist, cap-nested-hv) are accepted.
+      * Versioned compat machines (pseries-7.2, pseries-9.2 and
+        pseries-10.0) can be instantiated and are not marked
+        is-current.
+      * cap-hpt-max-page-size is reachable via the QOM tree.
+    """
+
+    timeout = 30
+
+    # ------------------------------------------------------------------ #
+    # Helpers
+    # ------------------------------------------------------------------ #
+
+    def _launch_stopped(self, machine='pseries', extra_args=None):
+        """Launch QEMU paused (-S) for the named machine and return."""
+        self.set_machine(machine)
+        self.vm.add_args('-S', '-nodefaults')
+        if extra_args:
+            self.vm.add_args(*extra_args)
+        self.vm.launch()
+
+    def _query_machine_info(self, name):
+        """Return the entry from query-machines that matches *name* or alias."""
+        machines = self.vm.cmd('query-machines')
+        for m in machines:
+            if m['name'] == name or m.get('alias') == name:
+                return m
+        return None
+
+    # ------------------------------------------------------------------ #
+    # Test: basic pseries machine is visible in query-machines
+    # ------------------------------------------------------------------ #
+
+    def test_pseries_in_query_machines(self):
+        """pseries (latest) must appear in query-machines output."""
+        self._launch_stopped('pseries')
+        info = self._query_machine_info('pseries')
+        self.assertIsNotNone(info,
+                             'pseries not found in query-machines output')
+        # The default/latest versioned machine carries is-default=True.
+        self.assertTrue(
+            info.get('is-default', False),
+            'pseries alias should be marked is-default=true',
+        )
+
+    # ------------------------------------------------------------------ #
+    # Test: security cap overrides are accepted without error
+    # ------------------------------------------------------------------ #
+
+    def test_pseries_security_caps_override(self):
+        """
+        Verify pseries accepts all well-known security cap overrides.
+
+        These are the options used by the KVM nested-HV path in
+        test_hv.py.  We only check that QEMU starts cleanly (no launch
+        exception); the actual cap values are enforced by separate
+        migration / capability tests.
+        """
+        caps = (
+            'cap-cfpc=broken,'
+            'cap-sbbc=broken,'
+            'cap-ibs=broken,'
+            'cap-ccf-assist=off'
+        )
+        self._launch_stopped(
+            'pseries',
+            extra_args=['-machine', f'pseries,{caps}'],
+        )
+        # If we get here the machine launched without errors.
+        machines = self.vm.cmd('query-machines')
+        aliases = [m.get('alias') for m in machines]
+        self.assertIn('pseries', aliases)
+
+    # ------------------------------------------------------------------ #
+    # Test: nested-HV cap is accepted
+    # ------------------------------------------------------------------ #
+
+    def test_pseries_nested_hv_cap(self):
+        """pseries must accept cap-nested-hv=on without raising an error."""
+        self._launch_stopped(
+            'pseries',
+            extra_args=['-machine', 'pseries,x-vof=on,cap-nested-hv=on'],
+        )
+        machines = self.vm.cmd('query-machines')
+        aliases = [m.get('alias') for m in machines]
+        self.assertIn('pseries', aliases)
+
+    # ------------------------------------------------------------------ #
+    # Test: versioned compat machines can be instantiated
+    # ------------------------------------------------------------------ #
+
+    def _test_compat_machine(self, version_str):
+        """
+        Helper: launch a versioned compat machine and verify that the
+        name appears in query-machines and that it is not marked
+        is-current (compat machines are never the latest version).
+        """
+        self._launch_stopped(version_str)
+        info = self._query_machine_info(version_str)
+        self.assertIsNotNone(
+            info,
+            f'compat machine {version_str!r} not found in query-machines',
+        )
+        # Compat (versioned) machines are not the latest, so is-current
+        # must be absent or False.
+        self.assertFalse(
+            info.get('is-current', False),
+            f'{version_str} should not be marked is-current=true',
+        )
+
+    def test_pseries_compat_7_2(self):
+        """pseries-7.2 (vmstate reference version) can be instantiated."""
+        self._test_compat_machine('pseries-7.2')
+
+    def test_pseries_compat_9_2(self):
+        """pseries-9.2 can be instantiated."""
+        self._test_compat_machine('pseries-9.2')
+
+    def test_pseries_compat_10_0(self):
+        """pseries-10.0 can be instantiated."""
+        self._test_compat_machine('pseries-10.0')
+
+    # ------------------------------------------------------------------ #
+    # Test: cap-hpt-max-page-size is reachable via the QOM tree
+    # ------------------------------------------------------------------ #
+
+    def test_pseries_cap_hpt_max_page_size(self):
+        """
+        Verify that pseries exposes cap-hpt-max-page-size in its
+        machine properties (it is used by migration compat checks).
+        """
+        self._launch_stopped('pseries')
+        # query-machine-props is not a real QMP command; we use the
+        # object model instead.
+        props = self.vm.cmd('qom-list', path='/machine')
+        prop_names = [p['name'] for p in props]
+        # The property should be reachable via the QOM tree.
+        self.assertIn(
+            'cap-hpt-max-page-size', prop_names,
+            'pseries /machine must expose cap-hpt-max-page-size via QOM',
+        )
+
+
+if __name__ == '__main__':
+    PseriesCapsTest.main()
-- 
2.55.0
[PATCH v2 3/4] tests/functional/ppc64: add pseries virtio-blk hotplug test
Posted by Anushree Mathur 3 days, 21 hours ago
Add a functional test that hot-plugs and hot-unplugs a virtio-blk-pci
disk on the pseries machine.  It runs with TCG, and also with KVM if
KVM is enabled.

On pseries a PCI device added through QMP is only enumerated by the
guest once rtas_errd (ppc64-diag) handles the RTAS hotplug event and
runs drmgr (powerpc-utils).  Neither is in the buildroot image used by
the other pseries tests, so the test uses rootfs-hotplug.ext2: the same
image with those two tools added.

The test boots with a second PHB (phb1) present so that the guest
registers its slots, adds a null-co backend, hot-plugs virtio-blk-pci
into phb1.0 and waits for /dev/vda, then unplugs it and waits for both
the QMP DEVICE_DELETED event and /dev/vda to go away.  The root drive is
used with snapshot=on so the cached asset is not modified by the guest.

Signed-off-by: Anushree Mathur <anushree.mathur@linux.ibm.com>
---
v2: no longer RFC.  I raised the patch adding rootfs-hotplug.ext2 to
qemu-ppc-boot (https://github.com/raphael502/qemu-ppc-boot/pull/1) and
it has been merged, so the asset URLs now point to that repository
(sha256 unchanged).  The vmlinux asset also points to qemu-ppc-boot, and
the test file is now executable like the other functional tests.

 tests/functional/ppc64/meson.build         |   2 +
 tests/functional/ppc64/test_hotplug_blk.py | 188 +++++++++++++++++++++
 2 files changed, 190 insertions(+)
 create mode 100755 tests/functional/ppc64/test_hotplug_blk.py

diff --git a/tests/functional/ppc64/meson.build b/tests/functional/ppc64/meson.build
index d91209f041..f483ce4d76 100644
--- a/tests/functional/ppc64/meson.build
+++ b/tests/functional/ppc64/meson.build
@@ -2,6 +2,7 @@
 
 test_ppc64_timeouts = {
   'fadump' : 480,
+  'hotplug_blk' : 600,
   'hotplug_cpu' : 480,
   'hv' : 1000,
   'mac99' : 120,
@@ -21,6 +22,7 @@ tests_ppc64_system_quick = [
 tests_ppc64_system_thorough = [
   'e500',
   'fadump',
+  'hotplug_blk',
   'hotplug_cpu',
   'hv',
   'mac99',
diff --git a/tests/functional/ppc64/test_hotplug_blk.py b/tests/functional/ppc64/test_hotplug_blk.py
new file mode 100755
index 0000000000..5740922977
--- /dev/null
+++ b/tests/functional/ppc64/test_hotplug_blk.py
@@ -0,0 +1,188 @@
+#!/usr/bin/env python3
+#
+# Functional test that hot-plugs and hot-unplugs a virtio-blk-pci disk
+# on the pseries machine type.
+#
+# Copyright (c) 2025 IBM Corporation
+#
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+import platform
+
+from qemu_test import LinuxKernelTest, Asset
+from qemu_test import exec_command_and_wait_for_pattern
+
+
+class PseriesHotplugBlk(LinuxKernelTest):
+    """
+    Test virtio-blk-pci hotplug/unplug on the pseries machine type.
+
+    A pre-built rootfs image (based on the buildroot image used by
+    test_hotplug_cpu.py) is used as the guest OS.  The image includes
+    rtas_errd from ppc64-diag, which is required on pseries to call the
+    ibm,configure-connector RTAS function that makes a QMP-hotplugged
+    PCI device visible to the kernel.
+
+    ppc64-diag is not packaged in Alpine Linux, so the Alpine+apk
+    approach used by test_hv.py cannot be used here.  Instead a
+    self-contained rootfs with rtas_errd and drmgr (from powerpc-utils)
+    already installed is referenced as an Asset, following the same
+    pattern as test_hotplug_cpu.py.
+
+    The test is run with both TCG and KVM; the KVM variant needs a
+    ppc64 host with kvm-hv loaded and is skipped automatically otherwise.
+
+    A second spapr-pci-host-bridge (phb1, index=1) is present at VM
+    launch so the guest kernel registers its PCI slots with rpaphp
+    during boot-time PCI probe.  Runtime DLPAR hotplug of
+    virtio-blk-pci into phb1.0 then works via the rtas_errd path.
+    """
+
+    timeout = 600
+
+    ASSET_KERNEL = Asset(
+        ('https://github.com/raphael502/qemu-ppc-boot/raw/refs/heads/main/'
+         'buildroot/qemu_ppc64le_pseries-2023.11-8-gdcd9f0f6eb-20240104/'
+         'vmlinux'),
+        'fc20d5820ad83ed514ac5f88af7e2692d47ab7f36dab290cdb137d05b98502f5')
+
+    # Pre-built rootfs based on the buildroot pseries image above, with
+    # rtas_errd (from ppc64-diag) and drmgr (from powerpc-utils) added.
+    # rtas_errd is required to process RTAS hotplug events on pseries and
+    # it execs drmgr to configure the device; without them rpaphp
+    # registers the slots but the device is never enumerated by the kernel.
+    ASSET_ROOTFS = Asset(
+        ('https://github.com/raphael502/qemu-ppc-boot/raw/refs/heads/main/'
+         'buildroot/qemu_ppc64le_pseries-2023.11-8-gdcd9f0f6eb-20240104/'
+         'rootfs-hotplug.ext2'),
+        '01ab3421d313e7765176eb27193fdb5837281dc3c8484eed140de8a3e65908eb')
+
+    def _boot_pseries(self, accel):
+        """Boot pseries with phb1 present and rtas_errd running.
+
+        accel must be 'tcg' or 'kvm'.
+        """
+        kernel_path = self.ASSET_KERNEL.fetch()
+        rootfs_path = self.ASSET_ROOTFS.fetch()
+
+        self.set_machine('pseries')
+        if accel == 'tcg':
+            self.vm.add_args('-accel', 'tcg,thread=multi')
+        else:
+            self.vm.add_args('-accel', 'kvm')
+        self.vm.add_args('-smp', '2', '-m', '512M')
+        # Second PHB present at boot so the guest kernel registers its
+        # slots with rpaphp.  Required for runtime DLPAR hotplug of
+        # virtio-blk-pci into phb1.0.
+        self.vm.add_args('-device', 'spapr-pci-host-bridge,id=phb1,index=1')
+        # snapshot=on keeps the cached rootfs asset unmodified
+        self.vm.add_args(
+            '-drive',
+            f'file={rootfs_path},if=scsi,index=0,format=raw,snapshot=on')
+        self.vm.add_args(
+            '-kernel', kernel_path,
+            '-append', 'root=/dev/sda console=hvc0 rootwait printk.time=0',
+        )
+        self.vm.set_console()
+        self.vm.launch()
+
+        self.wait_for_console_pattern('buildroot login:')
+        exec_command_and_wait_for_pattern(self, 'root', '#')
+
+        # Start rtas_errd so that RTAS hotplug events are processed.
+        # rtas_errd daemonizes by default (no -d flag).  Run mkdir and
+        # rtas_errd unconditionally then echo a sentinel so the pattern
+        # match never depends on their exit codes.
+        exec_command_and_wait_for_pattern(
+            self,
+            'touch /var/log/platform; mkdir -p /var/log/dump;'
+            ' /usr/sbin/rtas_errd;'
+            ' echo rtas_errd_started',
+            'rtas_errd_started',
+        )
+
+    def _do_blk_hotplug(self):
+
+        # Confirm /dev/vda does not exist before hotplug.
+        exec_command_and_wait_for_pattern(
+            self,
+            'test -b /dev/vda && echo present || echo absent',
+            'absent',
+        )
+
+        # Add a null-co block backend (no image file needed).
+        self.vm.cmd(
+            'blockdev-add',
+            **{
+                'driver': 'null-co',
+                'size': 1073741824,
+                'node-name': 'testdisk',
+            },
+        )
+
+        # Hotplug virtio-blk-pci into phb1.0.  The RTAS hotplug event
+        # (type 229) is delivered to the guest; rtas_errd calls
+        # ibm,configure-connector, the kernel enumerates the device and
+        # udev creates /dev/vda.
+        self.vm.cmd(
+            'device_add',
+            driver='virtio-blk-pci',
+            drive='testdisk',
+            id='hotblk0',
+            bus='phb1.0',
+            addr='0x0',
+        )
+
+        # Poll until /dev/vda appears.  Use a shell variable sentinel so
+        # the string 'vda_present' never appears in the typed command,
+        # avoiding a false match on the echoed command text.
+        exec_command_and_wait_for_pattern(
+            self,
+            'until test -b /dev/vda; do sleep 0.2; done'
+            '; R=present; echo vda_$R',
+            'vda_present',
+        )
+
+        # Unplug the device and wait for the QMP deletion event.
+        self.vm.cmd('device_del', id='hotblk0')
+        self.vm.event_wait(
+            'DEVICE_DELETED',
+            match={'data': {'device': 'hotblk0'}},
+        )
+
+        # Poll until /dev/vda disappears.
+        exec_command_and_wait_for_pattern(
+            self,
+            'while test -b /dev/vda; do sleep 0.2; done'
+            '; R=absent; echo vda_$R',
+            'vda_absent',
+        )
+
+    def test_pseries_hotplug_blk_tcg(self):
+        """Hotplug then unplug a virtio-blk-pci device on pseries (TCG)."""
+        self.require_accelerator('tcg')
+        self._boot_pseries('tcg')
+        self._do_blk_hotplug()
+
+    def test_pseries_hotplug_blk_kvm(self):
+        """Hotplug then unplug a virtio-blk-pci device on pseries (KVM).
+
+        Requires a ppc64 host with kvm-hv loaded; skipped automatically
+        on other hosts.
+        """
+        self.require_accelerator('kvm')
+        # On ppc64 the base kvm module can be loaded (and /dev/kvm created)
+        # without kvm-hv, so checking /dev/kvm alone is not sufficient.
+        if platform.machine() in ('ppc64le', 'ppc64'):
+            try:
+                with open('/proc/modules', encoding='utf-8') as f:
+                    if not any(line.split()[0] == 'kvm_hv' for line in f):
+                        self.skipTest('kvm-hv module is not loaded')
+            except OSError:
+                pass
+        self._boot_pseries('kvm')
+        self._do_blk_hotplug()
+
+
+if __name__ == '__main__':
+    PseriesHotplugBlk.main()
-- 
2.55.0
[PATCH v2 4/4] MAINTAINERS: add pseries hotplug and caps functional tests
Posted by Anushree Mathur 3 days, 21 hours ago
Add entries for the three new pseries functional tests introduced in
the preceding commits:
  - tests/functional/ppc64/test_hotplug_blk.py
  - tests/functional/ppc64/test_hotplug_cpu.py
  - tests/functional/ppc64/test_pseries_caps.py

All of them belong under the sPAPR (pseries) section.

Signed-off-by: Anushree Mathur <anushree.mathur@linux.ibm.com>
---
v2: now a single patch covering all three tests (previously a
separate patch plus an entry in the virtio-blk patch).

 MAINTAINERS | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index 15942aaba3..8456c5fbec 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -1705,6 +1705,9 @@ F: tests/qtest/libqos/rtas*
 F: tests/functional/ppc64/test_pseries.py
 F: tests/functional/ppc64/test_hv.py
 F: tests/functional/ppc64/test_tuxrun.py
+F: tests/functional/ppc64/test_hotplug_blk.py
+F: tests/functional/ppc64/test_hotplug_cpu.py
+F: tests/functional/ppc64/test_pseries_caps.py
 
 PowerNV (Non-Virtualized)
 M: Nicholas Piggin <npiggin@gmail.com>
-- 
2.55.0