Skip to contentSkip to conversation

kvm: limited x86 CPU power management

PATCH Superseded Michael S. Tsirkin 3 participants 9 messages Jun 22, 2018 KVM

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v4 cover kvm: limited x86 CPU power management

This adds ability to expose some host CPU power management capabilities to guests. For intel guests, this is sufficient for guest to enable low power CPU states on idle. For AMD guests it isn't sufficient, deeper C-states are entered using System-IO.

When enabled this puts CPU in a low power state with exit latencies that can go up to multiple milliseconds, and makes host scheduler as well as host utilities such as top and powertop think the CPU is constantly busy. Thus it has the effect of dedicating a host CPU for this guest.

mwait based power management is tied closely to specifics of CPUID, making migration challenging. At this point only the non-migrateable -cpu host is supported.

With this patch applied, VM latency is within the noise of baremetal for some benchmarks.

perf bench sched pipe results:
Before:

    6.452 sec
After:
    4.382 sec
Baremetal:
    4.136 sec

Changes since v3:
At Paolo's suggestion, rename -dedicated to -overcommit.

Changes since v2:
At Daniel's suggestion, don't use the -realtime flag.

At Paolo's suggestion, group this with memory lock flag which has a similar effect of dedicating memory to this VM.

Michael S. Tsirkin (2):
kvm: support -overcommit cpu-pm=on|off
i386/cpu: make -cpu host support monitor/mwait

 include/sysemu/sysemu.h |  1 +
 target/i386/cpu.h       |  9 +++++++++
 target/i386/cpu.c       | 19 ++++++++++++++-----
 target/i386/kvm.c       | 32 ++++++++++++++++++++++++++++++++
 vl.c                    | 32 +++++++++++++++++++++++++++++++-
 qemu-options.hx         | 18 ++++++++++++++++++
 6 files changed, 105 insertions(+), 6 deletions(-)
Signature · 1 line
MST
#1 lore raw

PATCH v4 1/2 kvm: support -overcommit cpu-pm=on|off

With this flag, kvm allows guest to control host CPU power state. This increases latency for other processes using same host CPU in an unpredictable way, but if decreases idle entry/exit times for the running VCPU, so to use it QEMU needs a hint about whether host CPU is overcommitted, hence the flag name.

Follow-up patches will expose this capability to guest (using mwait leaf).

Based on a patch by Wanpeng Li <[email protected]> .

---
 include/sysemu/sysemu.h |  1 +
 target/i386/kvm.c       | 23 +++++++++++++++++++++++
 vl.c                    | 32 +++++++++++++++++++++++++++++++-
 qemu-options.hx         | 18 ++++++++++++++++++
 4 files changed, 73 insertions(+), 1 deletion(-)
include/sysemu/sysemu.h +1 -0
index e893f72f3b..b921c6f3b7 100644
@@ -128,6 +128,7 @@ extern bool boot_strict;
 extern uint8_t *boot_splash_filedata;
 extern size_t boot_splash_filedata_size;
 extern bool enable_mlock;
+extern bool enable_cpu_pm;
 extern uint8_t qemu_extra_params_fw[2];
 extern QEMUClockType rtc_clock;
 extern const char *mem_path;
target/i386/kvm.c +23 -0
index 44f70733e7..cf9107be4b 100644
@@ -1357,6 +1357,29 @@ int kvm_arch_init(MachineState *ms, KVMState *s)
         smram_machine_done.notify = register_smram_listener;
         qemu_add_machine_init_done_notifier(&smram_machine_done);
     }
+
+    if (enable_cpu_pm) {
+        int disable_exits = kvm_check_extension(s, KVM_CAP_X86_DISABLE_EXITS);
+        int ret;
+
+/* Work around for kernel header with a typo. TODO: fix header and drop. */
+#if defined(KVM_X86_DISABLE_EXITS_HTL) && !defined(KVM_X86_DISABLE_EXITS_HLT)
+#define KVM_X86_DISABLE_EXITS_HLT KVM_X86_DISABLE_EXITS_HTL
+#endif
+        if (disable_exits) {
+            disable_exits &= (KVM_X86_DISABLE_EXITS_MWAIT |
+                              KVM_X86_DISABLE_EXITS_HLT |
+                              KVM_X86_DISABLE_EXITS_PAUSE);
+        }
+
+        ret = kvm_vm_enable_cap(s, KVM_CAP_X86_DISABLE_EXITS, 0,
+                                disable_exits);
+        if (ret < 0) {
+            error_report("kvm: guest stopping CPU not supported: %s",
+                         strerror(-ret));
+        }
+    }
+
     return 0;
 }
 
vl.c +31 -1
index 06031715ac..d53a9abcde 100644
@@ -142,6 +142,7 @@ ram_addr_t ram_size;
 const char *mem_path = NULL;
 int mem_prealloc = 0; /* force preallocation of physical target memory */
 bool enable_mlock = false;
+bool enable_cpu_pm = false;
 int nb_nics;
 NICInfo nd_table[MAX_NICS];
 int autostart;
@@ -390,6 +391,22 @@ static QemuOptsList qemu_realtime_opts = {
     },
 };
 
+static QemuOptsList qemu_dedicated_opts = {
+    .name = "dedicated",
+    .head = QTAILQ_HEAD_INITIALIZER(qemu_dedicated_opts.head),
+    .desc = {
+        {
+            .name = "mem-lock",
+            .type = QEMU_OPT_BOOL,
+        },
+        {
+            .name = "cpu-pm",
+            .type = QEMU_OPT_BOOL,
+        },
+        { /* end of list */ }
+    },
+};
+
 static QemuOptsList qemu_msg_opts = {
     .name = "msg",
     .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
@@ -3903,7 +3920,20 @@ int main(int argc, char **argv, char **envp)
                 if (!opts) {
                     exit(1);
                 }
-                enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
+                /* Don't override the -dedicated option if set */
+                enable_mlock = enable_mlock ||
+                    qemu_opt_get_bool(opts, "mlock", true);
+                break;
+            case QEMU_OPTION_dedicated:
+                opts = qemu_opts_parse_noisily(qemu_find_opts("dedicated"),
+                                               optarg, false);
+                if (!opts) {
+                    exit(1);
+                }
+                /* Don't override the -realtime option if set */
+                enable_mlock = enable_mlock ||
+                    qemu_opt_get_bool(opts, "mem-lock", false);
+                enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", false);
                 break;
             case QEMU_OPTION_msg:
                 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
qemu-options.hx +18 -0
index c0d3951e9f..ddedb7eb92 100644
@@ -3337,6 +3337,24 @@ mlocking qemu and guest memory can be enabled via @option{mlock=on}
 (enabled by default).
 ETEXI
 
+DEF("dedicated", HAS_ARG, QEMU_OPTION_dedicated,
+    "-dedicated [mem-lock=on|off][cpu-pm=on|off]\n"
+    "                run qemu with realtime features\n"
+    "                mem-lock=on|off controls memory lock support (default: off)\n"
+    "                cpu-pm=on|off controls cpu power management (default: off)\n",
+    QEMU_ARCH_ALL)
+STEXI
+@item -dedicated mem-lock=on|off
+@item -dedicated cpu-pm=on|off
+@findex -dedicated
+Run qemu using dedicated host resources.
+Locking qemu and guest memory can be enabled via @option{mem-lock=on}
+(disabled by default). This is equivalent to @option{realtime}.
+Guest ability to manage power state of host cpus (increasing latency for other
+processes on the same host cpu, but decreasing latency for guest)
+can be enabled via @option{cpu-pm=on} (disabled by default).
+ETEXI
+
 DEF("gdb", HAS_ARG, QEMU_OPTION_gdb, \
     "-gdb dev        wait for gdb connection on 'dev'\n", QEMU_ARCH_ALL)
 STEXI
Signature · 1 line
MST
#6 lore raw

PATCH v4 2/2 i386/cpu: make -cpu host support monitor/mwait

In reply to Michael S. Tsirkin

When guest CPU PM is enabled, and with -cpu host, expose the host CPU MWAIT leaf in the CPUID so guest can make good PM decisions.

Note: the result is 100% CPU utilization reported by host as host no longer knows that the CPU is halted.

---
 target/i386/cpu.h |  9 +++++++++
 target/i386/cpu.c | 19 ++++++++++++++-----
 target/i386/kvm.c |  9 +++++++++
 3 files changed, 32 insertions(+), 5 deletions(-)
target/i386/cpu.h +9 -0
index 664504610e..309f804573 100644
@@ -1378,6 +1378,15 @@ struct X86CPU {
     /* if true the CPUID code directly forward host cache leaves to the guest */
     bool cache_info_passthrough;
 
+    /* if true the CPUID code directly forwards
+     * host monitor/mwait leaves to the guest */
+    struct {
+        uint32_t eax;
+        uint32_t ebx;
+        uint32_t ecx;
+        uint32_t edx;
+    } mwait;
+
     /* Features that were filtered out because of missing host capabilities */
     uint32_t filtered_features[FEATURE_WORDS];
 
target/i386/cpu.c +14 -5
index 94260412e2..a4fb856d58 100644
@@ -3760,11 +3760,11 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
         }
         break;
     case 5:
-        /* mwait info: needed for Core compatibility */
-        *eax = 0; /* Smallest monitor-line size in bytes */
-        *ebx = 0; /* Largest monitor-line size in bytes */
-        *ecx = CPUID_MWAIT_EMX | CPUID_MWAIT_IBE;
-        *edx = 0;
+        /* MONITOR/MWAIT Leaf */
+        *eax = cpu->mwait.eax; /* Smallest monitor-line size in bytes */
+        *ebx = cpu->mwait.ebx; /* Largest monitor-line size in bytes */
+        *ecx = cpu->mwait.ecx; /* flags */
+        *edx = cpu->mwait.edx; /* mwait substates */
         break;
     case 6:
         /* Thermal and Power Leaf */
@@ -4595,6 +4595,15 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp)
         goto out;
     }
 
+    if (xcc->host_cpuid_required && enable_cpu_pm) {
+        host_cpuid(5, 0, &cpu->mwait.eax, &cpu->mwait.ebx,
+                   &cpu->mwait.ecx, &cpu->mwait.edx);
+        env->features[FEAT_1_ECX] |= CPUID_EXT_MONITOR;
+    }
+    /* mwait extended info: needed for Core compatibility */
+    /* We always wake on interrupt even if host does not have the capability */
+    cpu->mwait.ecx |= CPUID_MWAIT_EMX | CPUID_MWAIT_IBE;
+
     if (cpu->apic_id == UNASSIGNED_APIC_ID) {
         error_setg(errp, "apic-id property was not initialized properly");
         return;
target/i386/kvm.c +9 -0
index cf9107be4b..805968d5b7 100644
@@ -366,6 +366,15 @@ uint32_t kvm_arch_get_supported_cpuid(KVMState *s, uint32_t function,
         if (!kvm_irqchip_in_kernel()) {
             ret &= ~CPUID_EXT_X2APIC;
         }
+
+        if (enable_cpu_pm) {
+            int disable_exits = kvm_check_extension(s,
+                                                    KVM_CAP_X86_DISABLE_EXITS);
+
+            if (disable_exits & KVM_X86_DISABLE_EXITS_MWAIT) {
+                ret |= CPUID_EXT_MONITOR;
+            }
+        }
     } else if (function == 6 && reg == R_EAX) {
         ret |= CPUID_6_EAX_ARAT; /* safe to allow because of emulated APIC */
     } else if (function == 7 && index == 0 && reg == R_EBX) {
Signature · 1 line
MST
#8 lore raw
In reply to Michael S. Tsirkin
This series seems to have some coding style problems. See output below for more information:

Hi,

This series seems to have some coding style problems. See output below for more information:

Type: series
Message-id: [email protected]
Subject: [Qemu-devel] [PATCH v4 0/2] kvm: limited x86 CPU power management

=== TEST SCRIPT BEGIN ===
#!/bin/bash

BASE=base
n=1
total=$(git log --oneline $BASE.. | wc -l)
failed=0

git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram

commits="$(git log --format=%H --reverse $BASE..)" for c in $commits; do

    echo "Checking PATCH $n/$total: $(git log -n 1 --format=%s $c)..."
    if ! git show $c --format=email | ./scripts/checkpatch.pl --mailback -; then
        failed=1
        echo
    fi
    n=$((n+1))

done

exit $failed
=== TEST SCRIPT END ===

Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384
From https://github.com/patchew-project/qemu

 t [tag update]            patchew/[email protected] -> patchew/[email protected]
 * [new tag]               patchew/[email protected] -> patchew/[email protected]
 * [new tag]               patchew/[email protected] -> patchew/[email protected]

Switched to a new branch 'test'
ce1bdc31af i386/cpu: make -cpu host support monitor/mwait 577f258b15 kvm: support -overcommit cpu-pm=on|off

=== OUTPUT BEGIN ===
Checking PATCH 1/2: kvm: support -overcommit cpu-pm=on|off...
ERROR: do not initialise globals to 0 or NULL
#103: FILE: vl.c:145:
+bool enable_cpu_pm = false;

total: 1 errors, 0 warnings, 110 lines checked

Your patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.

Checking PATCH 2/2: i386/cpu: make -cpu host support monitor/mwait... === OUTPUT END ===

Test command exited with code: 1

---
Email generated automatically by Patchew [http://patchew.org/].
Please send your feedback to [email protected]

#2 lore raw

Re: [PATCH v4 1/2] kvm: support -overcommit cpu-pm=on|off

In reply to Michael S. Tsirkin
Quoted 5 lines · Michael S. Tsirkin

With this flag, kvm allows guest to control host CPU power state. This increases latency for other processes using same host CPU in an unpredictable way, but if decreases idle entry/exit times for the running VCPU, so to use it QEMU needs a hint about whether host CPU is overcommitted, hence the flag name.

The flag name in the code below is still "-dedicated".

[...]

Quoted 2 lines
vl.c +2 -0
+static QemuOptsList qemu_dedicated_opts = {
+    .name = "dedicated",

[...]

Quoted 3 lines
vl.c +3 -0
+            case QEMU_OPTION_dedicated:
+                opts = qemu_opts_parse_noisily(qemu_find_opts("dedicated"),
+                                               optarg, false);

[...]

Quoted 17 lines
qemu-options.hx +17 -0
+DEF("dedicated", HAS_ARG, QEMU_OPTION_dedicated,
+    "-dedicated [mem-lock=on|off][cpu-pm=on|off]\n"
+    "                run qemu with realtime features\n"
+    "                mem-lock=on|off controls memory lock support (default: off)\n"
+    "                cpu-pm=on|off controls cpu power management (default: off)\n",
+    QEMU_ARCH_ALL)
+STEXI
+@item -dedicated mem-lock=on|off
+@item -dedicated cpu-pm=on|off
+@findex -dedicated
+Run qemu using dedicated host resources.
+Locking qemu and guest memory can be enabled via @option{mem-lock=on}
+(disabled by default). This is equivalent to @option{realtime}.
+Guest ability to manage power state of host cpus (increasing latency for other
+processes on the same host cpu, but decreasing latency for guest)
+can be enabled via @option{cpu-pm=on} (disabled by default).
+ETEXI

[...]

Signature · 1 line
Eduardo
#4 lore raw

Re: [PATCH v4 1/2] kvm: support -overcommit cpu-pm=on|off

In reply to Michael S. Tsirkin

On Fri, Jun 22, 2018 at 03:31:48AM +0300, Michael S. Tsirkin wrote:
[...]

Quoted 5 lines
vl.c +4 -1
-                enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
+                /* Don't override the -dedicated option if set */
+                enable_mlock = enable_mlock ||
+                    qemu_opt_get_bool(opts, "mlock", true);
+                break;

vl.c int main(int argc, char **argv, char **envp) · line 3936

Quoted 10 lines
vl.c +10 -0
+            case QEMU_OPTION_dedicated:
+                opts = qemu_opts_parse_noisily(qemu_find_opts("dedicated"),
+                                               optarg, false);
+                if (!opts) {
+                    exit(1);
+                }
+                /* Don't override the -realtime option if set */
+                enable_mlock = enable_mlock ||
+                    qemu_opt_get_bool(opts, "mem-lock", false);
+                enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", false);

I would prefer to simply make the last option win, because it would be more obvious. But not a big deal if we are going to deprecate -realtime.

BTW, are you going to send a qemu-doc.texi patch documenting -realtime as deprecated?

Signature · 1 line
Eduardo
Reviewed-by on v4 2/2
#7 lore raw

Re: [PATCH v4 2/2] i386/cpu: make -cpu host support monitor/mwait

In reply to Michael S. Tsirkin
Quoted 7 lines · Michael S. Tsirkin

When guest CPU PM is enabled, and with -cpu host, expose the host CPU MWAIT leaf in the CPUID so guest can make good PM decisions.

Note: the result is 100% CPU utilization reported by host as host no longer knows that the CPU is halted.

Signature · 1 line
Eduardo
#3 lore raw

Re: [PATCH v4 1/2] kvm: support -overcommit cpu-pm=on|off

In reply to Eduardo Habkost
Quoted 8 lines · Eduardo Habkost
Quoted 5 lines · Michael S. Tsirkin

With this flag, kvm allows guest to control host CPU power state. This increases latency for other processes using same host CPU in an unpredictable way, but if decreases idle entry/exit times for the running VCPU, so to use it QEMU needs a hint about whether host CPU is overcommitted, hence the flag name.

The flag name in the code below is still "-dedicated".

Weird, somehow got the wrong patch sent.
Will repost the fixed one now

Quoted 29 lines

[...]

Quoted 2 lines
vl.c +2 -0
+static QemuOptsList qemu_dedicated_opts = {
+    .name = "dedicated",

[...]

Quoted 3 lines
vl.c +3 -0
+            case QEMU_OPTION_dedicated:
+                opts = qemu_opts_parse_noisily(qemu_find_opts("dedicated"),
+                                               optarg, false);

[...]

Quoted 17 lines
qemu-options.hx +17 -0
+DEF("dedicated", HAS_ARG, QEMU_OPTION_dedicated,
+    "-dedicated [mem-lock=on|off][cpu-pm=on|off]\n"
+    "                run qemu with realtime features\n"
+    "                mem-lock=on|off controls memory lock support (default: off)\n"
+    "                cpu-pm=on|off controls cpu power management (default: off)\n",
+    QEMU_ARCH_ALL)
+STEXI
+@item -dedicated mem-lock=on|off
+@item -dedicated cpu-pm=on|off
+@findex -dedicated
+Run qemu using dedicated host resources.
+Locking qemu and guest memory can be enabled via @option{mem-lock=on}
+(disabled by default). This is equivalent to @option{realtime}.
+Guest ability to manage power state of host cpus (increasing latency for other
+processes on the same host cpu, but decreasing latency for guest)
+can be enabled via @option{cpu-pm=on} (disabled by default).
+ETEXI

[...]

Signature · 1 line
Eduardo
#5 lore raw

Re: [PATCH v4 1/2] kvm: support -overcommit cpu-pm=on|off

In reply to Eduardo Habkost
Quoted 18 lines · Eduardo Habkost

On Fri, Jun 22, 2018 at 03:31:48AM +0300, Michael S. Tsirkin wrote:
[...]

Quoted 5 lines
vl.c +4 -1
-                enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
+                /* Don't override the -dedicated option if set */
+                enable_mlock = enable_mlock ||
+                    qemu_opt_get_bool(opts, "mlock", true);
+                break;

vl.c int main(int argc, char **argv, char **envp) · line 3936

Quoted 10 lines
vl.c +10 -0
+            case QEMU_OPTION_dedicated:
+                opts = qemu_opts_parse_noisily(qemu_find_opts("dedicated"),
+                                               optarg, false);
+                if (!opts) {
+                    exit(1);
+                }
+                /* Don't override the -realtime option if set */
+                enable_mlock = enable_mlock ||
+                    qemu_opt_get_bool(opts, "mem-lock", false);
+                enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", false);
Quoted 6 lines

I would prefer to simply make the last option win, because it would be more obvious. But not a big deal if we are going to deprecate -realtime.

BTW, are you going to send a qemu-doc.texi patch documenting -realtime as deprecated?

Once this is merged, as a patch on top, I can do this, sure.

Quoted 2 lines
Signature · 1 line
Eduardo