All of lore.kernel.org
 help / color / mirror / Atom feed
From: Anushree Mathur <anushree.mathur@linux.ibm.com>
To: "Harsh Prateek Bora" <harshpb@linux.ibm.com>,
	"Cédric Le Goater" <clg@redhat.com>,
	"Nicholas Piggin" <npiggin@gmail.com>,
	qemu-devel@nongnu.org, qemu-ppc@nongnu.org
Cc: Anushree Mathur <anushree.mathur@linux.ibm.com>,
	Thomas Huth <thuth@redhat.com>,
	Amit Machhiwal <amachhiw@linux.ibm.com>,
	Miles Glenn <milesg@linux.ibm.com>,
	Aditya Gupta <adityag@linux.ibm.com>,
	misanjum@linux.ibm.com, Chinmay Rath <rathc@linux.ibm.com>,
	Utkarsh Verma <uverma@linux.ibm.com>
Subject: [PATCH v3 1/4] tests/functional/ppc64: add pseries CPU hotplug test
Date: Tue, 24 Nov 2026 16:30:27 +0530	[thread overview]
Message-ID: <20261124110030.30097-2-anushree.mathur@linux.ibm.com> (raw)
In-Reply-To: <20261124110030.30097-1-anushree.mathur@linux.ibm.com>

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>
---
v3: put the -smp option on its own line in the comments and use plain
ASCII instead of arrows; added snapshot=on to the rootfs drive; bounded
the wait loops (45 seconds) and report a failure message on timeout;
wait for the QMP DEVICE_DELETED event after device_del.

 tests/functional/ppc64/meson.build         |   2 +
 tests/functional/ppc64/test_hotplug_cpu.py | 207 +++++++++++++++++++++
 2 files changed, 209 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..57bcf9af63
--- /dev/null
+++ b/tests/functional/ppc64/test_hotplug_cpu.py
@@ -0,0 +1,207 @@
+#!/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 (passed as the -smp option):
+        2,threads=2,cores=1,maxcpus=4
+    This gives 1 active core (cpu0+cpu1 online) and room for a second
+    one.  Hot-add core-id=2 and online goes from 0-1 to 0-3; hot-remove
+    it and online goes back to 0-1.  Verified via
+    /sys/devices/system/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.
+
+        The -smp option used here:
+            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()
+        # 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.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 (for up to 45 seconds) until cpu2+cpu3 are online.  The
+        # sentinel is built using a shell variable (echo hotplug_cpu_$R)
+        # so the full strings 'hotplug_cpu_done' and 'hotplug_cpu_timeout'
+        # never appear 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,
+            'for i in $(seq 1 90); do'
+            ' [ "$(cat /sys/devices/system/cpu/online)" = "0-3" ] && break;'
+            ' sleep 0.5; done;'
+            ' if [ "$(cat /sys/devices/system/cpu/online)" = "0-3" ];'
+            ' then R=done; else R=timeout; fi; echo hotplug_cpu_$R',
+            'hotplug_cpu_done',
+            failure_message='hotplug_cpu_timeout',
+        )
+
+        # Hot-remove the core and wait for QEMU to confirm it is gone.
+        self.vm.cmd('device_del', id='hotcpu1')
+        event = self.vm.event_wait(
+            'DEVICE_DELETED',
+            timeout=60.0,
+            match={'data': {'device': 'hotcpu1'}},
+        )
+        self.assertIsNotNone(event, 'DEVICE_DELETED not received for hotcpu1')
+
+        # Poll (for up to 45 seconds) until the guest sees the core gone.
+        exec_command_and_wait_for_pattern(
+            self,
+            'for i in $(seq 1 90); do'
+            ' [ "$(cat /sys/devices/system/cpu/online)" = "0-1" ] && break;'
+            ' sleep 0.5; done;'
+            ' if [ "$(cat /sys/devices/system/cpu/online)" = "0-1" ];'
+            ' then R=done; else R=timeout; fi; echo unplug_cpu_$R',
+            'unplug_cpu_done',
+            failure_message='unplug_cpu_timeout',
+        )
+
+        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



  reply	other threads:[~2026-11-24 11:02 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 11:00 [PATCH v3 0/4] tests/functional/ppc64: add pseries hotplug and caps tests Anushree Mathur
2026-11-24 11:00 ` Anushree Mathur [this message]
2026-11-27  1:57   ` [PATCH v3 1/4] tests/functional/ppc64: add pseries CPU hotplug test Utkarsh Verma
2026-11-24 11:00 ` [PATCH v3 2/4] tests/functional/ppc64: add pseries security caps test Anushree Mathur
2026-11-27 12:15   ` Utkarsh Verma
2026-11-24 11:00 ` [PATCH v3 3/4] tests/functional/ppc64: add pseries virtio-blk hotplug test Anushree Mathur
2026-11-27 12:51   ` Utkarsh Verma
2026-11-24 11:00 ` [PATCH v3 4/4] MAINTAINERS: add pseries hotplug and caps functional tests Anushree Mathur

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=20261124110030.30097-2-anushree.mathur@linux.ibm.com \
    --to=anushree.mathur@linux.ibm.com \
    --cc=adityag@linux.ibm.com \
    --cc=amachhiw@linux.ibm.com \
    --cc=clg@redhat.com \
    --cc=harshpb@linux.ibm.com \
    --cc=milesg@linux.ibm.com \
    --cc=misanjum@linux.ibm.com \
    --cc=npiggin@gmail.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-ppc@nongnu.org \
    --cc=rathc@linux.ibm.com \
    --cc=thuth@redhat.com \
    --cc=uverma@linux.ibm.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.